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

  1.  
  2. 'From Squeak 2.2 of Sept 23, 1998 on 9 October 1998 at 2:53:44 pm'!
  3. Object subclass: #Clock
  4.     instanceVariableNames: 'time timer displayFormat position '
  5.     classVariableNames: ''
  6.     poolDictionaries: ''
  7.     category: 'ClockWorks'!
  8.  
  9.  
  10. !Clock methodsFor: 'time management'!
  11. addMinute
  12.     time _ time addTime: (Time fromSeconds: 60)
  13.  !
  14. subtractHour
  15.     time _ time subtractTime: (Time fromSeconds: 60*60)
  16.  !
  17. nextSecond
  18.     time _ time addTime: (Time fromSeconds: 1).
  19.     self timeDisplay.
  20.  
  21.  !
  22. addHour
  23.     time _ time addTime: (Time fromSeconds: 60*60)
  24.  !
  25. subtractMinute
  26.     time _ time subtractTime: (Time fromSeconds: 60)
  27.  ! !
  28.  
  29. !Clock methodsFor: 'interface-accessing'!
  30. position: aPoint
  31.     position := aPoint.
  32. !
  33. position
  34.     ^ position! !
  35.  
  36. !Clock methodsFor: 'window'!
  37. catchEvents
  38.     | hourPlus hourMinus minutePlus minuteMinus click |
  39.     "Define the regions where we care about mouse clicks"
  40.     hourPlus _ (position x) @ ((position y)+100) extent: 100@50.
  41.     hourMinus _ ((position x)+100) @ ((position y)+100) extent: 100@50.
  42.     minutePlus _ (position x) @ ((position y)+150) extent: 100@50.
  43.     minuteMinus _ ((position x)+100) @ ((position y)+150) extent: 100@50.
  44.  
  45.     "Enter into an event loop"
  46.     [Sensor yellowButtonPressed] whileFalse: "Yellow button press ends the clock"
  47.         ["Give other processes a chance, and give user a chance to pick up."
  48.         (Delay forMilliseconds: 500) wait. 
  49.         "self timeDisplay."
  50.         (Sensor redButtonPressed) ifTrue: "Red button press could go to a button"
  51.             [click _ Sensor mousePoint.
  52.             (hourPlus containsPoint: click) ifTrue: [self addHour].
  53.             (hourMinus containsPoint: click) ifTrue: [self subtractHour].
  54.             (minutePlus containsPoint: click) ifTrue: [self addMinute].
  55.             (minuteMinus containsPoint: click) ifTrue: [self subtractMinute].]].
  56. !
  57. openWindow
  58.     | pen |
  59.     "Open the blank frame"
  60.     (Form extent: 200@200) fillWhite displayAt: position.
  61.  
  62.     "Draw the Buttons"
  63.     pen := Pen new.
  64.     pen up. pen goto: (position x) @ ((position y)+100). pen down.
  65.     pen north. pen turn: 90.
  66.     pen go: 200.
  67.     pen up. pen goto: (position x) @ ((position y)+150). pen down.
  68.     pen go: 200.
  69.     pen up. pen goto: ((position x)+100) @ ((position y)+100). pen down.
  70.     pen turn: 90.
  71.     pen go: 100.
  72.     'Hours +' displayAt: ((position x)+25) @ ((position y)+125).
  73.     'Hours -' displayAt: ((position x)+125) @ ((position y)+125).
  74.     'Minutes +' displayAt: ((position x)+25) @ ((position y)+175).
  75.     'Minutes -' displayAt: ((position x)+125) @ ((position y)+175).! !
  76.  
  77. !Clock methodsFor: 'timedisplay'!
  78. timeDisplay
  79.     '           ' displayAt: position + (50@50).  "Erase whatever time was there before"
  80.     self display displayAt: position + (50 @ 50).! !
  81.  
  82.  
  83.  
  84.