home *** CD-ROM | disk | FTP | other *** search
/ Otherware / Otherware_1_SB_Development.iso / mac / developm / scnote / editcdev.010 / EditCdev.p < prev    next >
Text File  |  1988-08-17  |  5KB  |  163 lines

  1. {------------------------------------------------------------------------------
  2. #
  3. #    Macintosh Developer Technical Support
  4. #
  5. #    EditText Sample Control Panel Device
  6. #
  7. #    EditCdev
  8. #
  9. #    EditCdev.p    -    Pascal Source
  10. #
  11. #    Copyright ⌐ 1988 Apple Computer, Inc.
  12. #    All rights reserved.
  13. #
  14. #    Versions:    1.0                    8/88
  15. #
  16. #    Components:    EditCdev.p            August 1, 1988
  17. #                EditCdev.c            August 1, 1988
  18. #                EditCdev.r            August 1, 1988
  19. #                PEditCdev.make        August 1, 1988
  20. #                CEditCdev.make        August 1, 1988
  21. #
  22. #    EditCdev is a sample Control Panel device (cdev) that 
  23. #    demonstrates the usage of the edit-related messages.  
  24. #    EditCdev demonstrates how to implement an editText item
  25. #    in a Control Panel Device.  It utilizes the new undo, cut, copy,
  26. #    paste, and delete messages that are sent to cdevs in
  27. #    response to user menu selections.
  28. #
  29. #    It is comprised of two editText items that can be edited 
  30. #    and moved between via the mouse or tab key.
  31. #
  32. ------------------------------------------------------------------------------}
  33.  
  34.  
  35. UNIT EditTextCDEV;
  36.  
  37. INTERFACE
  38.  
  39. USES
  40.  
  41.     MemTypes, QuickDraw, OSIntf, ToolIntf, PackIntf;
  42.  
  43. CONST
  44.  
  45.     textItm        = 1;            {first editTExt item in cdev}
  46.  
  47.     undoDev        = 9;            {cdev edit messages}
  48.     cutDev        = 10;
  49.     copyDev        = 11;
  50.     pasteDev    = 12;
  51.     clearDev    = 13;
  52.  
  53. TYPE
  54.  
  55.     CDEVRec    = RECORD
  56.                 dummyData    : INTEGER;
  57.                 END;
  58.     CDEVPtr    = ^CDEVRec;
  59.     CDEVHnd    = ^CDEVPtr;
  60.     
  61. FUNCTION TextCDEV (message, item, numItems, CPanelID: INTEGER;
  62.                                             VAR theEvent: EventRecord;
  63.                                             cdevStorage: Handle;
  64.                                             CPDialog: DialogPtr) : Handle;
  65.                                                 
  66. IMPLEMENTATION
  67.  
  68. PROCEDURE DoEditCommand (message: INTEGER; CPDialog: DialogPtr); FORWARD;
  69.  
  70. FUNCTION TextCDEV (message, item, numItems, CPanelID: INTEGER;
  71.                                             VAR theEvent: EventRecord;
  72.                                             cdevStorage: Handle;
  73.                                             CPDialog: DialogPtr) : Handle;
  74. {This is the main dispatcher. It must be the first code in the cdev.
  75.  EditCdev's dispatcher responds only to the following messages from
  76.  the Control Panel:
  77.      
  78.     macDev        - To indicate what machines it is available on.
  79.     initDev        - To set up some temporary storage and get the caret started.
  80.     keyEvtDev    - To check for an edit command and do the appropriate action.
  81.     cutDev        - To cut the current selection.
  82.     copyDev        - To copy the current selection.
  83.     pasteDev    - To paste the contents of the clipboard.
  84.     clearDev    - To delete the current selection.
  85.  
  86.  The Dialog Manager's services are used to handle entry of text, selection
  87.  of text, editing of text, and moving between the editText items via the
  88.  tab key. Since the Dialog Manager handles the selection of text, we do not
  89.  have to be concerned with hitDev messages for the editText items. The only
  90.  things we have to take care of are calling the Dialog Manager editing
  91.  routines in response to an edit command, and getting the caret to show up
  92.  at the beginning. In response to an edit command that was the result of
  93.  a command-key equivalent, we must also eliminate the event so that it does
  94.  not get processed as a keyDown by the Dialog Manager. Otherwise, an 'x'
  95.  would show up in the editText item when the user did a command-x to cut
  96.  the text.}
  97.  
  98. VAR
  99.     tempChar    : CHAR;
  100. BEGIN
  101. IF message = macDev THEN TextCDEV := Handle(1)            {we work on every machine}
  102. ELSE IF cdevStorage <> NIL THEN BEGIN
  103.     CASE message OF
  104.     initDev:                                            {initialize cdev}
  105.         BEGIN
  106.         cdevStorage := NewHandle(SIZEOF(CDEVRec));        {create private storage}
  107.         SelIText(CPDialog, numItems + textItm, 0, 999);    {make caret show up}
  108.         END;
  109.     hitDev:;
  110.     closeDev:;
  111.     nulDev:;
  112.     updateDev:;
  113.     activDev:;
  114.     deActivDev:;
  115.     keyEvtDev:                                            {respond to key down}
  116.         BEGIN
  117.         {first, get the character}
  118.         tempChar := CHR(BAnd(theEvent.message, charCodeMask));
  119.         {then see if the command key was down}
  120.         IF BAnd(theEvent.modifiers, cmdKey) <> 0 THEN BEGIN
  121.             message := nulDev;                            {start off with no message}
  122.             theEvent.what := nullEvent;                    {wipe out event}
  123.             CASE tempChar OF                            {set appropriate message}
  124.                 'X','x':
  125.                     message := cutDev;
  126.                 'C','c':
  127.                     message := copyDev;
  128.                 'V','v':
  129.                     message := pasteDev;
  130.                 END;
  131.             DoEditCommand(message, CPDialog);            {let edit command handler take it}
  132.             END;
  133.         END;
  134.     macDev:;
  135.     undoDev:;
  136.     cutDev, copyDev, pasteDev, clearDev:
  137.         DoEditCommand(message, CPDialog);                {respond to edit command}
  138.     END;  {CASE message}
  139.     TextCDEV := cdevStorage;
  140.     {if cdevStorage = NIL then ControlPanel will put up memory error}
  141.     END;  {cdevStorage <> NIL}
  142. END; {TextCDEV}
  143.  
  144. PROCEDURE DoEditCommand (message: INTEGER; CPDialog: DialogPtr);
  145.  
  146. {Call the apprpriate Dialog Manager routine to handle an edit command for
  147.  an editText item. It will do all the work regarding the TEScrap.}
  148.  
  149. BEGIN
  150. CASE message OF
  151.     cutDev:
  152.         DlgCut(CPDialog);
  153.     copyDev:
  154.         DlgCopy(CPDialog);
  155.     pasteDev:
  156.         DlgPaste(CPDialog);
  157.     clearDev:
  158.         DlgDelete(CPDialog);
  159.     END;
  160. END; {DoEditCommand}
  161.  
  162. END.  {EditTextCDEV}
  163.