home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1995 May / macformat-024.iso / Shareware City / Developers / CW CDEV Framework 1.1 / Sample CDEV / CDEV.cp next >
Encoding:
Text File  |  1995-01-09  |  4.5 KB  |  218 lines  |  [TEXT/MMCC]

  1. #include "CDEV.h"
  2.  
  3. // Simple Sample
  4. // A sample cdev created using the CW CDEV Framework
  5. // ©1994, Matthew E. Axsom
  6. // By: Matthew E. Axsom (chewey@nesw.mv.com)
  7. //--------------------------
  8. // Version History
  9. // 1.0.1    12/20/94
  10. //            Updated vers resources to contain CDEV name and copyright info.  Also
  11. //                corrected version information.
  12. //            
  13. //            Updated source code to be compatable w/v1.1 of the framework.
  14. //            Changed name of class to from myCDevObj to myCDEV
  15.  
  16. // 1.0         12/09/94 
  17. //            Initial release
  18.  
  19. // While it's not the most complicated cdev in the world, it
  20. // hopefully will give you the ideas behind the framework.
  21. // -Chewey
  22.  
  23.  
  24. // control panel items
  25. enum {
  26.     kUserItem=1,
  27.     kCheckBox,
  28.     kTextBox=4
  29. };
  30.  
  31. // base resource id
  32. enum {
  33.     kBaseID=-4064
  34. };
  35.  
  36. // items in STR# resource
  37. enum {
  38.     kDeactivatedText=1,
  39.     kAlertText,
  40.     kInitialText
  41. };
  42.  
  43. // prototypes for 2 required procedures
  44. long runable(void);
  45. TControlPanel *makeCDEV(short numItems,DialogPtr cp);
  46.  
  47. // return 1 so we can run.
  48. long runable(void)
  49. {
  50.     return 1;
  51. }
  52.  
  53. // code that allocates our cdev
  54. TControlPanel *makeCDEV(short numItems,DialogPtr cp)
  55. {
  56.     // all we need to do here is allocate *our* object that controls the cdev
  57.     return new myCDEV(numItems,cp);
  58. }
  59.  
  60. // constructor for our cdev
  61. myCDEV::myCDEV(short numItems,DialogPtr cp) : TControlPanel(numItems,cp)
  62. {
  63.     // set up our internal members
  64.     activated=false;
  65.     lastTime=0;
  66.     showSeconds=true;
  67.     
  68.     // set the check box up
  69.     setCheckBox();
  70.     
  71.     // put the initial text in the edit text box
  72.     
  73.     // get initial string
  74.     Str255    s;
  75.     GetIndString(s,kBaseID,kInitialText);
  76.  
  77.     // get the handle to the text, install and select all the text
  78.     Handle    h;
  79.     Rect    r;
  80.     short    type;
  81.     
  82.     GetDItem(fDialog,fLastItem+kTextBox,&type,&h,&r);
  83.     SetIText(h,s);
  84.     SelIText(fDialog,fLastItem+kTextBox,0,32000);
  85. }
  86.  
  87. // destructor for our cdev
  88. myCDEV::~myCDEV(void)
  89. {
  90.     // I have no dynamic storage that I need to get rid of before we close
  91. }
  92.  
  93. // handles a mouse hit in the control panel
  94. long myCDEV::ItemHit(short itemHit)
  95. {    
  96.     // the only hitable item I need to worry about is the checkbox...
  97.     switch (itemHit) {
  98.         case kCheckBox:
  99.             // modify our internal member to the checkbox's new state and update the check box
  100.             showSeconds=!showSeconds;
  101.             setCheckBox();
  102.             
  103.             // forces a redraw on the next idle
  104.             lastTime=0;
  105.             break;
  106.     }
  107.  
  108.     return noErr;    
  109. }
  110.  
  111. // handling of nulDev message
  112. long myCDEV::Idle(void)
  113. {
  114.     // only do this if we are in the forground
  115.     if (activated)
  116.         // if the time has changed, do an update
  117.         if (getTime())
  118.             Update();
  119.             
  120.     return noErr;
  121. }
  122.  
  123. // update user items
  124. long myCDEV::Update(void)
  125. {
  126.     Handle    h;
  127.     Rect    r;
  128.     short    type;
  129.     GrafPtr    savePort;
  130.     
  131.     // since this can get called from a variety of places, I want to make sure that
  132.     // we are drawing into the correct window.
  133.     GetPort(&savePort);
  134.     SetPort(fDialog);
  135.     
  136.     // get and draw the time sting
  137.     GetDItem(fDialog,fLastItem+kUserItem,&type,&h,&r);
  138.     TextBox(&timeString[1],timeString[0],&r,teFlushDefault);
  139.     
  140.     // restore the original drawing port
  141.     SetPort(savePort);
  142.     
  143.     return noErr;
  144. }
  145.  
  146. // activate user items
  147. long myCDEV::Activate(void)
  148. {
  149.     activated=true;    // we are being brought into the forground.
  150.     lastTime=0;        // force us to update on the next idle call
  151.  
  152.     return noErr;
  153. }
  154.  
  155. // deactivate user items
  156. long myCDEV::Deactivate(void)
  157. {
  158.     activated=false;    // we are being pushed into the background
  159.     
  160.     // get the deactivated text and draw it
  161.     GetIndString(timeString,kBaseID,kDeactivatedText);
  162.     Update();
  163.  
  164.     return noErr;
  165. }
  166.  
  167. // undo from edit menu or cmd-z
  168. long myCDEV::Undo(void)        
  169. {
  170.     Str255    s;
  171.     
  172.     // pop up an alert letting the user know that we don't support undo
  173.     GetIndString(s,kBaseID,kAlertText);
  174.     ParamText(s,nil,nil,nil);
  175.     NoteAlert(kBaseID+1,nil);
  176.     
  177.     return noErr;
  178. }
  179.  
  180. // gets the current time and puts it into a string if it's not the same as the time
  181. // we currently have
  182. Boolean myCDEV::getTime(void)
  183. {
  184.     unsigned long    nuTime;
  185.     Boolean            result=false;    // assume no update
  186.     
  187.     // get the time it is now
  188.     GetDateTime(&nuTime);
  189.     
  190.     // if it's different than the last posted time then...
  191.     if (nuTime != lastTime) {
  192.     
  193.         // create a time string based on the users settings
  194.         IUTimeString(nuTime,showSeconds,timeString);
  195.         
  196.         // lastTime is now the current time
  197.         lastTime=nuTime;
  198.         
  199.         // let the caller know that we updated the time
  200.         result=true;
  201.     }
  202.     
  203.     return result;
  204. }
  205.  
  206. // check and unchecks the checkbox
  207. void myCDEV::setCheckBox(void)
  208. {
  209.     short    type;
  210.     Handle    h;
  211.     Rect    r;
  212.     
  213.     // get the control handle to the checkbox and set to to whatever
  214.     // showSeconds is.
  215.     GetDItem(fDialog,fLastItem+kCheckBox,&type,&h,&r);
  216.     SetCtlValue((ControlHandle)h,showSeconds);
  217. }
  218.