home *** CD-ROM | disk | FTP | other *** search
/ European Smalltalk User Group 2004 September / esugcd.iso / Books-Tutorial-Lectures / Books / FreeBooks / GuzdialBookDrafts / clock-ui2.cs < prev    next >
Text File  |  2003-03-31  |  4KB  |  142 lines

  1.  
  2. 'From Squeak 2.2 of Sept 23, 1998 on 2 November 1998 at 2:47:18 pm'!
  3. Object subclass: #Clock
  4.     instanceVariableNames: 'time timer window displayFormat '
  5.     classVariableNames: ''
  6.     poolDictionaries: ''
  7.     category: 'ClockWorks'!
  8. Object subclass: #ClockButton
  9.     instanceVariableNames: 'model frame action name '
  10.     classVariableNames: ''
  11.     poolDictionaries: ''
  12.     category: 'ClockWorks'!
  13. Object subclass: #ClockWindow
  14.     instanceVariableNames: 'position clock buttons '
  15.     classVariableNames: ''
  16.     poolDictionaries: ''
  17.     category: 'ClockWorks'!
  18.  
  19.  
  20. !Clock methodsFor: 'accessing'!
  21. window
  22.     ^window!
  23. window: aWindow
  24.     window := aWindow.
  25. ! !
  26.  
  27. !Clock methodsFor: 'time management'!
  28. nextSecond
  29.     time _ time addTime: (Time fromSeconds: 1).
  30.     window timeDisplay.
  31.  
  32.  ! !
  33.  
  34. !ClockButton methodsFor: 'accessing'!
  35. name
  36.     ^name!
  37. action
  38.     ^action!
  39. model: aModel
  40.     model _ aModel.
  41. !
  42. action: aMessage
  43.     action _ aMessage
  44. !
  45. frame: aRect
  46.     frame _ aRect!
  47. model
  48.     ^model!
  49. frame
  50.     ^frame!
  51. name: aString
  52.     name _ aString
  53. ! !
  54.  
  55. !ClockButton methodsFor: 'processing'!
  56. draw
  57.     | pen |
  58.     pen := Pen new.
  59.     pen color: (Color black).
  60.     pen up. pen goto: (frame origin).
  61.     pen north. pen turn: 90. pen down.
  62.     pen goto: (frame topRight).
  63.     pen turn: 90. pen goto: (frame bottomRight).
  64.     pen turn: 90. pen goto: (frame bottomLeft).
  65.     pen turn: 90. pen goto: (frame origin).
  66.     name displayAt: (frame leftCenter) + (25@-10). "Offset in a bit, and up a bit for aesthetics"
  67. !
  68. inControl: aPoint
  69.     ^frame containsPoint: aPoint!
  70. process
  71.     model perform: action
  72. ! !
  73.  
  74. !ClockButton class methodsFor: 'creation'!
  75. make: aName at: aRect for: aModel triggering: aMessage
  76.     | newButton |
  77.     newButton _ self new.
  78.     newButton name: aName.
  79.     newButton frame: aRect.
  80.     newButton model: aModel.
  81.     newButton action: aMessage.
  82.     newButton draw.
  83.     ^newButton.
  84. ! !
  85.  
  86. !ClockWindow methodsFor: 'accessing'!
  87. position: aPoint
  88.     position := aPoint.!
  89. addButton: aButton
  90.     buttons isNil ifTrue: [buttons := OrderedCollection new].
  91.     buttons add: aButton.
  92. !
  93. clock
  94.     ^clock!
  95. position
  96.     ^position!
  97. buttons
  98.     ^buttons
  99. !
  100. clock: aClock
  101.     clock := aClock.
  102. ! !
  103.  
  104. !ClockWindow methodsFor: 'window processing'!
  105. timeDisplay
  106.     '           ' displayAt: position + (50@50).  "Erase whatever time was there before"
  107.     (clock display) displayAt: position + (50 @ 50).!
  108. openOn: aModel
  109.     | button |
  110.     position isNil ifTrue: [self error: 'Must set position first.'].
  111.  
  112.     "Set this model as this window's clock"
  113.     clock := aModel.
  114.  
  115.     "Open the blank frame"
  116.     (Form extent: 200@200) fillWhite displayAt: position.
  117.  
  118.     "Draw the Buttons"
  119.     button := ClockButton make: 'Hours +' at: ((position x) @ ((position y)+100) extent: 100@50) for: aModel triggering: #addHour.
  120.     self addButton: button.
  121.     button := ClockButton make: 'Hours -' at: (((position x)+100) @ ((position y)+100) extent: 100@50) for: aModel triggering: #subtractHour.
  122.     self addButton: button.
  123.     button := ClockButton make: 'Minutes +' at: ((position x) @ ((position y)+150) extent: 100@50) for: aModel triggering: #addMinute.
  124.     self addButton: button.
  125.     button := ClockButton make: 'Minutes -' at: (((position x)+100) @ ((position y)+150) extent: 100@50) for: aModel triggering: #subtractMinute.
  126.     self addButton: button.
  127. !
  128. processEvents
  129.     "Enter into an event loop"
  130.     | click |
  131.     [Sensor yellowButtonPressed] whileFalse: "Yellow button press ends the clock"
  132.         ["Give other processes a chance, and give user a chance to pick up."
  133.         (Delay forMilliseconds: 500) wait. 
  134.         (Sensor redButtonPressed) ifTrue: "Red button press could go to a button"
  135.             [click _ Sensor mousePoint.
  136.             buttons do: [:b |
  137.                 (b inControl: click) ifTrue: [b process]].]].
  138. ! !
  139.  
  140.  
  141.  
  142.