home *** CD-ROM | disk | FTP | other *** search
/ Boot Disc 8 / boot-disc-1997-04.iso / PDA_Soft / Pilot / Apps / Scribble / SCRIBBLE.ASM < prev    next >
Assembly Source File  |  1997-01-05  |  20KB  |  475 lines

  1. ; *******************************************************************
  2. ; *                                                                 *
  3. ; *                          scribble_16.asm                        *
  4. ; *                                                                 *
  5. ; *   This aplet is based on Darrin Massena's PILA sample code.     *
  6. ; *   It requires Darrin's PILOT.INC and STARTUP.INC include files. *
  7. ; *   For info on how to write apps for the world's best palmtop,   *
  8. ; *   aim your browser at                                           *
  9. ; *   http://www.massena.com/darrin/pilot                           *
  10. ; *                                                                 *
  11. ; *   If you have any questions about this source code,             *
  12. ; *   please send them to howlett@iosphere.net                      *
  13. ; *                                                                 *
  14. ; *   This code is freeware and you are free to modify it as you    *
  15. ; *   wish, however you must register your own Creator ID before    *
  16. ; *   you distribute any modifications.                             *
  17. ; *                                                                 *
  18. ; *                                                                 *
  19. ; *   Andrew Howlett                                                *
  20. ; *   howlett@iosphere.net                                          *
  21. ; *   2 Jan 97                                                      *
  22. ; *                                                                 *
  23. ; *******************************************************************
  24.  
  25.         Appl    "Scribble", 'Scrb'                      ; define name and creator ID
  26.                                                         ; creator ID is sometimes called AppType
  27.                                                         ; creator type is #$53637262
  28.  
  29.         include "Pilot.inc"                             ; structs, constants
  30.         include "Startup.inc"                           ; common startup code
  31.  
  32. kidbAIB         equ             $7FFE                   ; Application-defined resource ids
  33. kidrPREF        equ             1                       ; don't know who made up these names
  34. kidrTVER        equ             1
  35. kidrTAIB        equ             1000
  36. kidfMain        equ             1000
  37. kidmMain        equ             1000
  38. kidmClear       equ             1001
  39. kidmAbout       equ             1002
  40. kidmNewPage     equ             1003
  41. kidmRemovePage  equ             1004
  42. kidmForwardPage equ             1005
  43. kidmBackwardPage        equ     1006
  44. kidrAboutAlert  equ             1000
  45. ScribbleAppType equ             $53637262               ; 'Scrb'
  46. ScribbleDBType  equ             $44617461               ; 'Data'
  47.  
  48.         data                                            ; global variables
  49.  
  50. global lastX.w                                          ; globals are offsets from a5
  51. global lastY.w
  52. global lastX1.w
  53. global lastY1.w
  54. global SkipNextEvent.w
  55. global dbR.l
  56. global CurrentIndex.w
  57. global dbNumRecords.w
  58.  
  59. ScribbleDBName
  60.         dc.b    'ScribbleDB',0
  61. NumString
  62.         dc.b    '00',0
  63.  
  64.         code
  65.  
  66. ; *******************************************************************
  67. ; *                                                                 *
  68. ; *   DWord PilotMain(Word cmd, void *cmdPBP, Word launchflags)     *
  69. ; *                                                                 *
  70. ; *   PilotMain is called by the startup code and implements        *
  71. ; *   a simple event handling loop.                                 *
  72. ; *                                                                 *
  73. ; *******************************************************************
  74.  
  75. proc PilotMain(cmd.w, cmdPBP.l, launchFlags.w)
  76. local   err.w
  77. local   evt.EventType
  78. beginproc
  79.         tst.w   cmd(a6)                                 ;sysAppLaunchCmdNormalLaunch is 0
  80.         bne     PmReturn                                ;not a normal launch, bag out
  81.  
  82.         systrap FrmGotoForm(#kidfMain.w)
  83.  
  84. PmEventLoop                                             ; Doze until an event arrives
  85.         systrap EvtGetEvent(&evt(a6), #evtWaitForever.w)
  86.  
  87.         systrap SysHandleEvent(&evt(a6))                ; First handler is system
  88.         tst.b   d0                                      ; handled?
  89.         bne.s   PmEventDone                             ; yep
  90.  
  91.                                                         ; Second is menu handler
  92.         systrap MenuHandleEvent(&0, &evt(a6), &err(a6))
  93.         tst.b   d0                                      ; handled?
  94.         bne.s   PmEventDone                             ; yep
  95.  
  96.                                                         ; Third is application handler
  97.         call    ApplicationHandleEvent(&evt(a6))
  98.         tst.b   d0                                      ; handled?
  99.         bne.s   PmEventDone                             ; yep
  100.  
  101.                                                         ; Fourth is form handler.
  102.                                                         ; Form handler is where our
  103.                                                         ; custom code is located
  104.         call    MainFormHandleEvent(&evt(a6))
  105.         tst.b   d0                                      ; handled?
  106.         bne.s   PmEventDone                             ; yep
  107.  
  108.                                                         ; Still not handled.
  109.                                                         ; We're not interested
  110.                                                         ; in it anymore so let the                                          
  111.         systrap FrmGetActiveForm()                      ; default form handler take it.
  112.         systrap FrmHandleEvent(a0.l, &evt(a6))
  113.  
  114.                                                         ; Return from PilotMain when an
  115. PmEventDone                                             ; appStopEvent is received
  116.         cmpi.w  #appStopEvent,evt+EventType.eType(a6)   ; time to stop?
  117.         bne.s   PmEventLoop                             ; nope, loop until it is
  118.  
  119. PmReturn
  120.         moveq   #0, d0
  121. endproc
  122.  
  123.  
  124. ; *******************************************************************
  125. ; *                                                                 *
  126. ; *   Boolean ApplicationHandleEvent(EventType *pevt)               *
  127. ; *                                                                 *
  128. ; *   Handles these events:    frmLoadEvent                         *
  129. ; *                                                                 *
  130. ; *******************************************************************
  131.  
  132. proc ApplicationHandleEvent(pevt.l)
  133. beginproc
  134.         movea.l pevt(a6),a0                             ;a0 = pevt
  135.  
  136.         cmp.w   #frmLoadEvent,EventType.eType(a0) 
  137.         beq     FormLoadHandler
  138.         cmpi.w  #appStopEvent,EventType.eType(a0) 
  139.         beq     ApplicationStopHandler
  140.         bra     ApplicationEventNotHandled
  141.  
  142. FormLoadHandler                                         ; Initialize the form and make it active
  143.  
  144.         systrap FrmInitForm(EventType.data+frmLoad.formID(a0).w)
  145.         systrap FrmSetActiveForm(a0.l)        
  146.  
  147. OpenDatabase                                            ; try to open the database
  148.         systrap DmOpenDatabaseByTypeCreator (#ScribbleDBType.l, #ScribbleAppType.l, #dmModeReadWrite.w)
  149.         move.l  a0, dbR(a5)
  150.         cmpa    #0, a0
  151.         bne DrawForm                                    ; if the database exists, goto DrawForm
  152.  
  153. CreateDatabase                                          ; if the database doesn't exist, create it
  154.         systrap DmCreateDatabase(#0.w, &ScribbleDBName(a5), #ScribbleAppType.l, #ScribbleDBType.l, #false.w)
  155.         systrap DmOpenDatabaseByTypeCreator (#ScribbleDBType.l, #ScribbleAppType.l, #dmModeReadWrite.w)
  156.         move.l  a0, dbR(a5)
  157.         clr.w   CurrentIndex(a5)                                ; create a first record
  158.         clr.w   dbNumRecords(a5)
  159.         systrap DmNewRecord (dbR(a5).l, &CurrentIndex(a5), #2840.l)
  160.         systrap DmReleaseRecord (dbR(a5).l, CurrentIndex(a5).w, #true.w)
  161.         call    ClearScreen()
  162.         bra     ApplicationEventNotHandled
  163.  
  164. DrawForm
  165.         clr.w   CurrentIndex(a5)
  166.         systrap PrefGetAppPreferences(#ScribbleAppType.l, #15.w, &CurrentIndex(a5), #2.w)
  167.  
  168. DrawForm1
  169.         systrap DmNumRecords(dbR(a5).l)
  170.         subq.w  #$0001, d0
  171.         move.w  d0, dbNumRecords(a5)
  172.         call    LoadScreen()
  173.         bra     ApplicationEventNotHandled
  174.  
  175. ApplicationStopHandler                                  ; Application Stop Handler
  176.         call    SaveScreen()
  177.         systrap PrefSetAppPreferences(#ScribbleAppType.l, #15.w, &CurrentIndex(a5), #2.w)
  178.  
  179. ApplicationEventNotHandled
  180.         clr.b   d0
  181.  
  182. ApplicationEventReturn
  183.  
  184. endproc
  185.  
  186.  
  187. ; *******************************************************************
  188. ; *                                                                 *
  189. ; *   Boolean MainFormHandleEvent(EventType *pevt)                  *
  190. ; *                                                                 *
  191. ; *   Handles these events:    frmOpenEvent                         *
  192. ; *                            frmMenuEvent                         *
  193. ; *                            penDownEvent                         *
  194. ; *                                                                 *
  195. ; *******************************************************************
  196.  
  197. proc MainFormHandleEvent(pevt.l)
  198.  
  199. beginproc
  200.  
  201.         movea.l pevt(a6),a0                             ;  a0 = pevt
  202.         move.w  EventType.eType(a0),d0                  ;  d0 = event type
  203.  
  204.         cmp.w   #menuEvent,d0                           ; Handle Menu Events
  205.         beq     MenuHandler
  206.         cmp.w   #keyDownEvent,d0                        ; Handle key down Events
  207.         beq     KeyboardHandler
  208. PenHandler
  209.         cmp.w   #penDownEvent, d0                       ; Handle pen Down Events
  210.         beq     PenDownEventHandler
  211.         cmp.w   #penUpEvent, d0                         ; Handle pen Up Events
  212.         beq     PenEventHandler
  213.         cmp.w   #penMoveEvent, d0                       ; Handle pen Move Events
  214.         beq     PenEventHandler
  215.         clr.b   d0                                      ; Not handled, d0=0
  216.         bra MFHReturn
  217.  
  218. MenuHandler                                             ; Show "About" info box
  219.         move.w  EventType.data+menu.itemID(a0), d0
  220.         cmp.w   #kidmAbout, d0
  221.         beq     MenuAboutHandler
  222.         cmp.w   #kidmClear, d0
  223.         beq     MenuClearHandler
  224.         cmp.w   #kidmNewPage, d0
  225.         beq     MenuNewPageHandler
  226.         cmp.w   #kidmRemovePage, d0
  227.         beq     MenuRemovePageHandler
  228.         cmp.w   #kidmForwardPage, d0
  229.         beq     MenuForwardPageHandler
  230.         cmp.w   #kidmBackwardPage, d0
  231.         beq     MenuBackwardPageHandler
  232.         bra     MFHHandled
  233.  
  234. MenuAboutHandler
  235.         systrap FrmAlert(#kidrAboutAlert.w)
  236.         bra     MFHHandled
  237.  
  238. MenuClearHandler                                        ; Clear menu chosen
  239.         call    ClearScreen ()
  240.         bra     MFHHandled
  241.  
  242. MenuNewPageHandler
  243.         call    SaveScreen()
  244.         addq.w  #$0001, CurrentIndex(a5)
  245.         systrap DmNewRecord (dbR(a5).l, &CurrentIndex(a5), #2840.l)
  246.         cmpa    #0, a0
  247.         bne     MenuNewPageHandler1
  248.         subq.w  #$0001, CurrentIndex(a5)
  249.         bra     MFHHandled
  250.         
  251. MenuNewPageHandler1
  252.         systrap DmReleaseRecord (dbR(a5).l, CurrentIndex(a5).w, #true.w)
  253.         call    ClearScreen()
  254.         addq.w  #$0001, dbNumRecords(a5)
  255.         bra MFHHandled
  256.  
  257. MenuRemovePageHandler
  258.         tst.w   dbNumRecords(a5)
  259.         beq     MFHHandled
  260.         
  261. MenuRemovePageHandler1
  262.         subq.w  #$0001, dbNumRecords(a5)
  263.         systrap DmRemoveRecord (dbR(a5).l, CurrentIndex(a5).w)
  264.         subq.w  #$0001, CurrentIndex(a5)
  265.         bpl     MenuRemovePageHandler2
  266.         clr     CurrentIndex(a5)
  267.  
  268. MenuRemovePageHandler2
  269.         call LoadScreen()
  270.         bra     MFHHandled
  271.  
  272. MenuForwardPageHandler
  273.         move.w  CurrentIndex(a5),d0
  274.         cmp.w   dbNumRecords(a5), d0
  275.         beq     MFHHandled
  276.         call    SaveScreen()
  277.         addq.w  #$0001, CurrentIndex(a5)
  278.         call    LoadScreen()
  279.         bra     MFHHandled
  280.  
  281. MenuBackwardPageHandler
  282.         tst.w   CurrentIndex(a5)
  283.         beq     MFHHandled
  284.         call    SaveScreen()
  285.         subq.w  #$0001, CurrentIndex(a5)
  286.         call    LoadScreen()
  287.         bra     MFHHandled
  288.  
  289. PenDownEventHandler
  290.         move.w  EventType.screenX(a0), lastX(a5)         ; move x into lastX
  291.         move.w  EventType.screenY(a0), lastY(a5)         ; move y into lastY
  292.         move.w  #1,SkipNextEvent(a5)
  293.         bra     MFHHandled
  294.  
  295. PenEventHandler
  296.         tst.w   SkipNextEvent(a5)                 ; skip first event
  297.         beq     MFHReturn
  298.         clr.l d3
  299.         clr.l d4
  300.         move.w EventType.screenX(a0), d3                ; move x into d1
  301.         move.w EventType.screenY(a0), d4                ; move y into d2
  302.         cmp.w   #159, d4
  303.         blt     CheckPenUpEvent
  304.         move.w  #159, d4
  305.  
  306. CheckPenUpEvent
  307.         cmp.w   #penUpEvent, d0
  308.         bne     DrawLineHandler
  309.         clr.w   SkipNextEvent(a5)
  310.  
  311. DrawLineHandler
  312.         systrap WinDrawLine(lastX(a5).w, lastY(a5).w, d3.w, d4.w)
  313.         move.w d3, lastX(a5)
  314.         move.w d4, lastY(a5)
  315.         bra     MFHHandled
  316.  
  317. KeyboardHandler
  318.         move.w  EventType.data+keyDown.chr(a0), d0
  319.         andi.w  #$1f, d0                                ;  mask #$1f for case-insensitive
  320.         cmp.w   #01, d0                                 ;  a is 1
  321.         beq     MenuAboutHandler
  322.         cmp.w   #03, d0                                 ;  c is 3
  323.         beq     MenuClearHandler
  324.         cmp.w   #06, d0                                 ;  f is 6
  325.         beq     MenuForwardPageHandler
  326.         cmp.w   #02, d0                                 ;  b is 2
  327.         beq     MenuBackwardPageHandler
  328.         cmp.w   #14, d0                                 ;  e is 14
  329.         beq     MenuNewPageHandler
  330.         cmp.w   #18, d0                                 ;  r is 18
  331.         beq     MenuRemovePageHandler
  332.         cmp.w   #00, d0                                 ;  space is 0
  333.         beq     MenuForwardPageHandler
  334.         cmp.w   #08, d0                                 ;  backspace is 8
  335.         beq     MenuBackwardPageHandler
  336.  
  337. MFHHandled
  338.         moveq.l #1,d0
  339.  
  340. MFHReturn
  341.  
  342. endproc
  343.  
  344. ; *******************************************************************
  345. ; *                                                                 *
  346. ; *    Void  ClearScreen (void)                                     *
  347. ; *                                                                 *
  348. ; *******************************************************************
  349.  
  350. proc ClearScreen()
  351.  
  352.         systrap WinEraseWindow()                        ; Erase the window
  353.         systrap FrmGetActiveForm()                      ; Get form point a0
  354.         systrap FrmDrawForm(a0.l)                       ; Draw the form
  355.         call ShowCurrentIndex()
  356.  
  357. beginproc
  358.  
  359.  
  360. endproc
  361.  
  362. ; *******************************************************************
  363. ; *                                                                 *
  364. ; *    Void   SaveScreen (void)                                     *
  365. ; *                                                                 *
  366. ; *******************************************************************
  367.  
  368. proc SaveScreen()
  369.  
  370. beginproc
  371.  
  372.         systrap DmGetRecord (dbR(a5).l, CurrentIndex(a5).w)
  373.         move.l  a0, a3                                          ; a3=RecordHandle
  374.         systrap MemHandleLock (a3.l)
  375.         systrap DmWrite (a0.l, #$00000000.l, #$00006518.l, #2840.l)
  376.         systrap MemHandleUnlock (a3.l)
  377.         systrap DmReleaseRecord (dbR(a5).l, CurrentIndex(a5).w, #true.w)
  378.  
  379. endproc
  380.  
  381. ; *******************************************************************
  382. ; *                                                                 *
  383. ; *    Void   LoadScreen (void)                                     *
  384. ; *                                                                 *
  385. ; *******************************************************************
  386.  
  387. proc LoadScreen()
  388.  
  389. beginproc
  390.  
  391.         call ClearScreen()
  392.         systrap DmQueryRecord (dbR(a5).l, CurrentIndex(a5).w)
  393.         move.l  a0, a2                                  ; a2=RecordHandle
  394.         systrap MemHandleLock (a0.l)
  395.  
  396.         move.l  #$00006518, a3                          ; copy screen from record
  397. MoveLoop
  398.         move.l  (a0)+, (a3)+
  399.         cmpa    #$00007030, a3                          ; $6518 + 2840 = $7030
  400.         blt     MoveLoop
  401.  
  402.         systrap MemHandleUnlock (a2.l)                  ; unlock record
  403.                                                         ; record doesn't have to be released
  404.                                                         ; because it was only queried
  405.  
  406.         call ShowCurrentIndex()
  407.  
  408. endproc
  409.  
  410. ; *******************************************************************
  411. ; *                                                                 *
  412. ; *    Void   ShowCurrentIndex (void)                               *
  413. ; *                                                                 *
  414. ; *******************************************************************
  415.  
  416. proc ShowCurrentIndex()
  417.  
  418. beginproc
  419.  
  420.         move.w  CurrentIndex(a5),d0                     ; show page number
  421.         addq.l  #1, d0
  422.         move.l  d0,-(a7)
  423.         pea.l   NumString(a5)
  424.         trap    #15
  425.         dc.w    sysTrapStrIToA
  426.         addq.l  #8,a7
  427.         systrap StrLen(&NumString(a5))
  428.         systrap WinDrawChars (&NumString(a5),d0.w, #150.w, #150.w)
  429.  
  430. endproc
  431.  
  432.  
  433. ; *******************************************************************
  434. ; *                                                                 *
  435. ; *   Resources                                                     *
  436. ; *                                                                 *
  437. ; *   WBMP - Application Icon Bitmap resource.  Automatically       *
  438. ; *          converted from Windows format to Pilot format and      *
  439. ; *          written as a 'tAIB' rather than 'Tbmp' because kidbAIB *
  440. ; *          is a special value ($7ffe).                            *
  441. ; *   pref - Preference resource.  Defines app launch flags,        *
  442. ; *          stack and heap size.                                   *
  443. ; *   tFRM - Form resource.  Defined in scribble.rcp                *
  444. ; *   tver - Version resource                                       *                      *
  445. ; *   MBAR - Menu resource.  Defined in scribble.rcp                *
  446. ; *   Talt - Alert resource.  Defined in scribble.rcp               *
  447. ; *                                                                 *
  448. ; *   Filenames for scribble.rcp generated binaries are the         *
  449. ; *   resource type, followed by the ID in hexadecimal.             *
  450. ; *   E.g.  MBAR ID 1000 ==> mbar03e8.bin                           *
  451. ; *                                                                 *
  452. ; *******************************************************************
  453.  
  454.         res 'WBMP', kidbAIB, "Scribble.bmp"
  455.  
  456.         res 'pref', kidrPREF
  457.         dc.w    sysAppLaunchFlagNewStack|sysAppLaunchFlagNewGlobals|sysAppLaunchFlagUIApp|sysAppLaunchFlagSubCall
  458.         dc.l    $1000                   ; stack size
  459.         dc.l    $1000                   ; heap size
  460.  
  461.         res 'tFRM', kidfMain, "tfrm03e8.bin"
  462.  
  463.         res 'tver', kidrTVER            
  464.         dc.b    '1.5', 0
  465.  
  466.         res 'MBAR', kidmMain, "mbar03e8.bin"
  467.  
  468.         res 'Talt', kidrAboutAlert, "talt03e8.bin"
  469.  
  470. ; ********************   debug code
  471. ;
  472. ;  this line can be used to insert a breakpoint
  473. ;
  474. ;       systrap ErrDisplayFileLineMsg (&ErrorString(a5), #163.w, &ErrorString(a5))
  475. ;