1#
2# written by Chris Thomas for the article of DDJ May 2002.
3#
4
5require 'osx/cocoa'
6
7class HelloView < OSX::NSView
8
9  #
10  # When the Cocoa view system wants to draw a view,
11  # it calls the method -(void)drawRect:(NSRect)rect.
12  # The rectangle argument is relative to the origin
13  # of the view's frame, and it may only be a small
14  # portion of the view. For this reason, very
15  # simple views with only one or two graphical
16  # elements tend to ignore this parameter.
17  #
18  def drawRect(rect)
19    
20    # Set the window background to transparent
21    OSX::NSColor.clearColor.set
22    OSX::NSRectFill(bounds)
23
24    # Draw the text in a shade of red and in a large system font
25    attributes = OSX::NSMutableDictionary.alloc.init
26
27    attributes.setObject_forKey(	OSX::NSColor.redColor,
28				OSX::NSForegroundColorAttributeName )
29
30    attributes.setObject_forKey(	OSX::NSFont.boldSystemFontOfSize(48.0),
31				OSX::NSFontAttributeName )
32
33    string = OSX::NSString.alloc.initWithString( "Hello, Ruby Baby" )
34
35    string.drawAtPoint_withAttributes([0,0], attributes)
36    
37    #
38    # Turn the window's shadow off and on --
39    # This is a kludge to get the shadow to recalculate
40    # for the new shape of the opaque window content.
41    #
42    viewWindow = window
43    window.setHasShadow(0)
44    window.setHasShadow(1)
45  end
46end
47
48#
49# If this file is the main file, then perform the followjng commands.
50# (This construct is often useful for adding simple unit tests to
51# library code.)
52#
53if __FILE__ == $0
54  #
55  # First, to establish a connection to the window server,
56  # we must initialize the application
57  #
58  $stderr.print "just wait ..." ; $stderr.flush
59  application = OSX::NSApplication.sharedApplication
60
61  # Create the window
62  window = OSX::NSWindow.alloc.
63    objc_send(:initWithContentRect, [0, 0, 450, 200],
64						            :styleMask, OSX::NSBorderlessWindowMask,
65						              :backing, OSX::NSBackingStoreBuffered,
66						                :defer, 0)
67  # Allow the window to be partially transparent
68  window.setOpaque(0)
69
70  # Setup the window's root view
71  view = HelloView.alloc.initWithFrame([0, 0, 450, 200])
72  window.setContentView(view)
73
74  # Place the window near the top of the screen.
75  # (Screen coordinates in Cocoa are always PostScript
76  # coordinates, which start from the bottom of the screen
77  # and increase as they go up, so we have to do some math
78  # to place the window at 100 pixels from the top of the
79  # screen.
80  #
81  screenFrame = OSX::NSScreen.mainScreen.frame
82  windowOriginPoint = [40, screenFrame.origin.y + screenFrame.size.height - 100]
83  window.setFrameOrigin( windowOriginPoint )
84
85  # Show the window
86  window.makeKeyAndOrderFront(nil)
87  window.orderFrontRegardless()		## but this one does
88
89  # And start the application event loop
90  $stderr.print "\rtype `Ctrl-C' for quit !\n"
91  trap('SIGINT') { $stderr.puts "bye." ; exit 0 }
92  application.run
93end
94