home *** CD-ROM | disk | FTP | other *** search
/ Generous Efforts of Many / gemcd.zip / GEM.CD.E.po / NDA:CLOCKS:CLOCK / CLOCK.ASM.txt < prev    next >
Text File  |  2010-05-14  |  15KB  |  508 lines

  1. *******************************************
  2. * This is the source file for a New Desk  *
  3. * Accessory (NDA) which displays the      *
  4. * current date and time in a window on    *
  5. * the GS desktop. After assembling, use   *
  6. * the APW FILETYPE command to change the  *
  7. * file type code of CLOCK to $B8, then    *
  8. * transfer CLOCK to the SYSTEM/DESK.ACCS/ *
  9. * directory of the ProDOS 16 boot disk.   *
  10. *                                         *
  11. * by Gary Little   April 5, 1987          *
  12. *                                         *
  13. * (This program is adapted from a similar *
  14. *  one in the Desk Accessories chapter of *
  15. *  my "Exploring the Apple IIGS" book, to *
  16. *  be published in July, 1987 by          *
  17. *  Addison-Wesley.)                       *
  18. *                                         *
  19. *******************************************
  20.            LIST    OFF
  21.            SYMBOL  OFF
  22.            ABSADDR ON
  23.            INSTIME ON
  24.            GEN     ON
  25.  
  26.            KEEP    CLOCK            ;Code file
  27.  
  28.            MCOPY   CLOCK.MAC        ;Macro file
  29.  
  30. Period     GEQU     60              ;Ask for "run" action every second
  31. EventMask  GEQU     $FFFF           ;Handle all events
  32.  
  33. NDA_Clock  START
  34.  
  35.            DC      I4'NDA_Open'     ;Open the NDA
  36.            DC      I4'NDA_Close'    ;Close the NDA
  37.            DC      I4'NDA_Action'   ;Perform NDA action
  38.            DC      I4'NDA_Init'     ;Startup/Shutdown the NDA
  39.            DC      I2'Period'       ;Periodicity of "run" action
  40.            DC      I2'EventMask'    ;Permitted events
  41.            DC      C'##'            ;Name in menu item form
  42.            DC      C'Clock'         ;Text for NDA name
  43.            DC      C'\H**',I1'0'    ;ID field + terminator
  44.  
  45. ; Open the NDA if it has not been previously opened. This routine
  46. ; must return a pointer to the NDA window on the stack, just above
  47. ; the 3-byte return address. The Desk Manager reserves this result
  48. ; space just before calling NDA_Open with a JSL instruction.
  49.  
  50. NDA_Open   ANOP
  51.  
  52. Result     EQU     $05             ;Result stack offset after JSL, PHB
  53.  
  54.            PHB
  55.            PHK
  56.            PLB                      ;data bank = code bank
  57.  
  58.            LDA     ClockOpen        ;Clock window already open?
  59.            BNE     Ignore           ;if so, branch
  60.  
  61.            PHA                      ;Space for result
  62.            PHA
  63.            PushPtr WindowDef
  64.            _NewWindow               ;Create and open NDA window
  65.  
  66.            PLX                      ;Pop pointer (low)
  67.            PLA                      ;Pop pointer (high)
  68.  
  69.            STX     WindowPtr        ;Save pointer to window
  70.            STA     WindowPtr+2
  71.  
  72.            STA     Result+2,S       ;Save result on stack (high)
  73.            TXA
  74.            STA     Result,S         ; (low)
  75.  
  76.            PushLong WindowPtr
  77.            _SetSysWindow            ;Mark this as a DA window
  78.  
  79.            LDA     #$FFFF
  80.            STA     ClockOpen        ;Set "open" flag
  81.  
  82.            PHA                      ;space for result
  83.            PHA
  84.            _GetCursorAdr
  85.            PopLong OldCursor        ;Save pointer to regular cursor
  86.  
  87. Ignore     PLB
  88.            RTL
  89.  
  90.  
  91. ; Close the NDA if it is not already closed:
  92.  
  93. NDA_Close  ANOP
  94.  
  95.            PHB
  96.            PHK
  97.            PLB                      ;data bank = code bank
  98.  
  99.            LDA     ClockOpen        ;Is the clock window open?
  100.            BEQ     Ignore           ;No, so branch
  101.            
  102.            PushLong WindowPtr
  103.            _CloseWindow             ;Get rid of the window
  104.  
  105.            STZ     ClockOpen        ;Mark clock as closed
  106.  
  107.            PushLong OldCursor
  108.            _SetCursor               ;Restore application cursor
  109.  
  110.            PLB
  111.            RTL
  112.  
  113.  
  114. ; Perform the NDA action:
  115.  
  116. NDA_Action ANOP
  117.  
  118.            PHB                      ;Save data bank
  119.            PHK
  120.            PLB                      ;Make data bank = program bank
  121.  
  122.            PHY                      ;Save incoming parameters
  123.            PHX                      ;(event record or menu info)
  124.  
  125.            ASL     A                ;x2 to step into table
  126.            TAX
  127.            JSR     (ActionTbl,X)
  128.  
  129.            PLX                      ;Fix up the stack
  130.            PLY
  131.  
  132.            PLB
  133.            RTL
  134.  
  135. ActionTbl  ANOP
  136.            DC      I2'NoAction'
  137.            DC      I2'NDA_Event'
  138.            DC      I2'NDA_Run'
  139.            DC      I2'NDA_Cursor'
  140.            DC      I2'NDA_Menu'
  141.            DC      I2'NDA_Undo'
  142.            DC      I2'NDA_Cut'
  143.            DC      I2'NDA_Copy'
  144.            DC      I2'NDA_Paste'
  145.            DC      I2'NDA_Clear'
  146.  
  147. NoAction   ANOP
  148.            RTS
  149.  
  150. NDA_Menu   ANOP
  151.            RTS
  152.  
  153. NDA_Undo   ANOP
  154. NDA_Cut    ANOP
  155. NDA_Copy   ANOP
  156. NDA_Paste  ANOP
  157. NDA_Clear  ANOP
  158.            
  159. ; On exit A=0 if edit command wasn't handled; non-zero if it was.
  160. ; You will usually want to say it was handled, because the application
  161. ; will not be active and so shouldn't be dealing with edit commands.
  162.  
  163.            LDA     #$FFFF           ;Say we handled it.
  164.            RTS
  165.  
  166. ; Display a wristwatch cursor if the cursor is over top of
  167. ; the content region of the window. This routine is only called
  168. ; when the DA window is the front window.
  169.  
  170. NDA_Cursor ANOP
  171.  
  172.            PHA
  173.            PHA
  174.            _GetPort                 ;Save current GrafPort
  175.  
  176.            PushLong WindowPtr
  177.            _SetPort                 ;Make clock window active GrafPort
  178.  
  179.            PushPtr PortRect
  180.            _GetPortRect             ;Get the port rectangle (local coords)
  181.  
  182.            PushPtr MousePosn        ;Return mouse position in GrafPort coords
  183.            _GetMouse                ;Get cursor position
  184.  
  185.            PHA                      ;space for result
  186.            PushPtr MousePosn        ;pointer to mouse coordinate
  187.            PushPtr PortRect         ;pointer to content region rectangle
  188.            _PtInRect
  189.            PLA                      ;Is it in content region?
  190.            BEQ     NDA_Curs1        ;No, so branch
  191.  
  192. ; Switch to watch cursor, but only if it's not already active:
  193.  
  194.            PHA
  195.            PHA
  196.            _GetCursorAdr            ;Get current cursor pointer
  197.            PLA
  198.            PLX
  199.  
  200.            CMP     #WatchCurs       ;Is it the watch?
  201.            BNE     NDA_Curs0        ;Definitely not
  202.            CPX     #^WatchCurs      ;Is it the watch?
  203.            BEQ     NDA_Curs3        ;Yes, so do nothing
  204.  
  205. NDA_Curs0  PushPtr WatchCurs
  206.            BRA     NDA_Curs2
  207.  
  208. ; Switch to application cursor, but only if it's not already active:
  209.  
  210. NDA_Curs1  PHA
  211.            PHA
  212.            _GetCursorAdr            ;Get current cursor pointer
  213.            PLA
  214.            PLX
  215.  
  216.            CMP     #WatchCurs       ;Is it the watch?
  217.            BNE     NDA_Curs3        ;No, so don't do anything
  218.            CPX     #^WatchCurs      ;Is it the watch?
  219.            BNE     NDA_Curs3        ;No, so don't do anything
  220.  
  221.            PushLong OldCursor       ;Switch to application cursor
  222.  
  223. NDA_Curs2  _SetCursor
  224.  
  225. NDA_Curs3  _SetPort                 ;Restore GrafPort
  226.            RTS
  227.  
  228. ; This subroutine is called once every "Period" ticks:
  229.  
  230. NDA_Run    ANOP
  231.  
  232.            PHA                      ;Space for result
  233.            PHA
  234.            _GetPort                 ;Save current port
  235.  
  236.            PushLong WindowPtr
  237.            _SetPort                 ;Switch to clock window for drawing
  238.  
  239.            JSR ShowTime             ;Display the new time
  240.  
  241.            _SetPort                 ;(Pointer still on stack)
  242.  
  243.            RTS
  244.  
  245. ; X and Y (pushed on stack) contain pointer to event record
  246.  
  247. NDA_Event  ANOP
  248.  
  249. TheEvent   EQU     $05              ;1 (base) + 2 (JSR) + 2 (PHD)
  250.  
  251.            PHD
  252.            TSC
  253.            TCD                      ;Align d.p. with stack
  254.  
  255.            LDA     [TheEvent]       ;Get "what" code
  256.            CMP     #9               ;Anything we support?
  257.            BCS     TE1              ;No, so branch
  258.  
  259.            ASL     A                ;x2 to step into table
  260.            TAX
  261.            JSR     (EventTbl,X)
  262.  
  263. TE1        PLD                      ;Restore direct page
  264.            RTS
  265.  
  266. EventTbl   ANOP
  267.            DC      I2'NoEvent'      ;Not supported
  268.            DC      I2'DoMouseDwn'   ; Mouse down
  269.            DC      I2'DoMouseUp'    ; Mouse up
  270.            DC      I2'DoKeyDwn'     ; Key down
  271.            DC      I2'NoEvent'      ;Not supported
  272.            DC      I2'DoAutoKey'    ; Autokey
  273.            DC      I2'DoUpdate'     ; Update
  274.            DC      I2'NoEvent'      ;Not supported
  275.            DC      I2'DoActivate'   ; Activate
  276.  
  277. DoMouseUp  ANOP
  278. DoMouseDwn ANOP
  279. DoKeyDwn   ANOP
  280. DoAutoKey  ANOP
  281.  
  282. NoEvent    RTS
  283.  
  284. DoUpdate   ANOP
  285.  
  286.            PushLong WindowPtr
  287.            _BeginUpdate             ;Visible region = update region
  288.  
  289.            JSR     ShowTime         ;Display the current time
  290.  
  291.            PushLong WindowPtr
  292.            _EndUpdate               ;Restore entire visible region
  293.  
  294.            RTS
  295.  
  296. ; If NDA window is deactivated, return to original cursor.
  297.  
  298. DoActivate ANOP
  299.  
  300.            LDY     #14              ;Access modifiers field
  301.            LDA     [TheEvent],Y
  302.            AND     #$01             ;Isolate activate/deactivate flag
  303.            BEQ     NDA_Off          ;If 0, deactivate
  304.            RTS
  305.  
  306. NDA_Off    PushLong OldCursor
  307.            _SetCursor               ;Switch to previous cursor
  308.            RTS
  309.  
  310. ShowTime   ANOP
  311.  
  312.            PushPtr TheTime
  313.            _ReadASCIITime           ;Read the clock
  314.  
  315.            SEP     #$20             ;8-bit A register for byte accesses
  316.            LONGA   OFF
  317.  
  318.            LDY     #19
  319. ST1        LDA     TheTime,Y
  320.            AND     #$7F             ;Convert to standard ASCII
  321.            STA     TheTime,Y
  322.            DEY
  323.            BPL     ST1
  324.  
  325.            REP     #$20             ;Back to 16-bit A register
  326.            LONGA   ON
  327.  
  328. ;Get the name of the day of the week:
  329.  
  330.            PHA                      ;Space for 8 bytes of result
  331.            PHA
  332.            PHA
  333.            PHA
  334.            _ReadTimeHex
  335.            PLA                      ;Pop minute/second
  336.            PLA                      ;Pop year/hour
  337.            PLA                      ;Pop month/day
  338.            PLA                      ;Pop day of week (high byte)
  339.  
  340.            XBA                      ;Put day of week in low byte
  341.            AND     #$0F             ;Strip unused bits
  342.            DEC     A                ;Convert 1..7 to 0..6 (1=Sunday)
  343.  
  344. ; Look for the Nth entry in the table:
  345.  
  346.            TAY
  347.            LDX     #0
  348.  
  349.            SEP     #$20             ;Use 8-bit accumulator
  350.            LONGA   OFF
  351.  
  352. Find_DOW   CPY     #0               ;At correct name?
  353.            BEQ     Move_DOW         ;Yes, so branch
  354.  
  355. FE1        LDA     DayTable,X
  356.            BEQ     FE2              ;Branch if at end of name
  357.            INX                      ;Move to next character
  358.            BRA     FE1
  359.  
  360. FE2        INX                      ;Move to start of next name           
  361.            DEY                      ;Decrement day-of-week counter
  362.            BRA     Find_DOW
  363.  
  364. ; Transfer the name to the buffer area:
  365.  
  366. Move_DOW   ANOP
  367.            LDY     #0
  368. SR1        LDA     DayTable,X
  369.            BEQ     SR2              ;Branch if at end of name
  370.            STA     TheDay,Y
  371.            INX
  372.            INY
  373.            BRA     SR1
  374.            
  375. SR2        REP     #$20             ;Back to 16-bit accumulator
  376.            LONGA   ON
  377.  
  378.            PushWord #2              ;horizontal
  379.            PushWord #9              ;vertical
  380.            _MoveTo
  381.  
  382.            PushPtr TheTime
  383.            _DrawCString             ;Draw the time string
  384.  
  385.            PushWord #10             ;horizontal
  386.            PushWord #19             ;vertical
  387.            _MoveTo
  388.  
  389.            PHA
  390.            _GetForeColor            ;Save foreground color
  391.  
  392.            Pushword #5              ;Dark Green letters
  393.            _SetForeColor
  394.  
  395.            PRINTG  'Copyright (c) 1987 Gary B. Little'
  396.  
  397.            _SetForeColor            ;Restore foreground color
  398.  
  399.            RTS
  400.  
  401.  
  402. ; Startup or shutdown the NDA. On entry, A=0 for DeskShutdown,
  403. ; A is nonzero for DeskStartup.
  404.  
  405. NDA_Init   ANOP
  406.  
  407.            PHB
  408.            PHK
  409.            PLB
  410.  
  411.            CMP     #0               ;Starting up?
  412.            BNE     NDA_Init1        ;Yes, so do nothing
  413.  
  414.            LDA     ClockOpen        ;Clock window open?
  415.            BEQ     NDA_Init1        ;No, so branch
  416.  
  417.            PushLong WindowPtr
  418.            _CloseWindow             ;Close the window (releases memory)
  419.  
  420.            STZ     ClockOpen        ;Set "closed" flag
  421.  
  422. NDA_Init1  PLB
  423.            RTL
  424.  
  425. ; The data area begins here:
  426.  
  427. NDA_Title  STR 'Calendar/Clock'     ;Window title
  428.  
  429. WindowDef  ANOP
  430.            DC    I2'EndWind-WindowDef'
  431.            DC    I2'%1100000010100000' ;Window with close box, title
  432.            DC    I4'NDA_Title'      ;Pointer to window name
  433.            DC    I4'0'
  434.            DC    I2'0,0,0,0'
  435.            DC    I4'0'
  436.            DC    I4'0'              ;Origin at (0,0)
  437.            DC    I4'0'
  438.            DC    I4'0'
  439.            DC    I4'0'
  440.            DC    I4'0'
  441.            DC    I4'0'
  442.            DC    I2'0'
  443.            DC    I4'0'
  444.            DC    I4'0'
  445.            DC    I4'0'              ;(Handle our own updates)
  446.            DC    I'30,33,52,284'    ;Dimensions of window
  447.            DC    I4'-1'             ;Put clock window in front
  448.            DC    I4'0'
  449. EndWind    ANOP
  450.  
  451. WindowPtr  DS      4                ;Pointer to window record
  452. ClockOpen  DS      2                ;Used as a flag
  453.  
  454. PortRect   DS      8                ;Content region rectangle
  455. MousePosn  DS      4                ;Current mouse position (local)
  456.  
  457. TheTime    DS      20               ;ReadASCIITime returns 20 bytes here
  458.            DC      C'  --  '
  459. TheDay     DS      9                ;Day of week inserted here
  460.            DC      C'      '        ;Add padding
  461.            DC      I1'0'            ;(terminator for DrawCString)
  462.  
  463. DayTable   ANOP
  464.            DC      C'Sunday   ',I1'0'
  465.            DC      C'Monday   ',I1'0'
  466.            DC      C'Tuesday  ',I1'0'
  467.            DC      C'Wednesday',I1'0'
  468.            DC      C'Thursday ',I1'0'
  469.            DC      C'Friday   ',I1'0'
  470.            DC      C'Saturday ',I1'0'
  471.  
  472. OldCursor  DS      4                ;Pointer to application's cursor record
  473.  
  474. ; This the is the cursor record for a "wristwatch" cursor:
  475.  
  476. WatchCurs  DC      I2'12'           ;Rows in cursor image
  477.            DC      I2'3'            ;Cursor width (in words)
  478.  
  479.            DC      H'000000000000'  ;The cursor image
  480.            DC      H'000FF0000000'
  481.            DC      H'000FF0000000'
  482.            DC      H'00F00F000000'
  483.            DC      H'0F00F0F00000'
  484.            DC      H'0F00F0F00000'
  485.            DC      H'0F0FF0FF0000'
  486.            DC      H'0F0000F00000'
  487.            DC      H'00F00F000000'
  488.            DC      H'000FF0000000'
  489.            DC      H'000FF0000000'
  490.            DC      H'000000000000'
  491.  
  492.            DC      H'000FF0000000'  ;The cursor mask
  493.            DC      H'00FFFF000000'
  494.            DC      H'00FFFF000000'
  495.            DC      H'0FFFFFF00000'
  496.            DC      H'FFFFFFFF0000'
  497.            DC      H'FFFFFFFF0000'
  498.            DC      H'FFFFFFFFF000'
  499.            DC      H'FFFFFFFF0000'
  500.            DC      H'0FFFFFF00000'
  501.            DC      H'00FFFF000000'
  502.            DC      H'00FFFF000000'
  503.            DC      H'000FF0000000'
  504.  
  505.            DC      I2'6,8'          ;Hot spot (y,x)
  506.  
  507.            END
  508.