home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1995 April / macformat-023.iso / Shareware City / Developers / CW CDEV Framework / Sample CDEV / CDEV.c next >
Encoding:
C/C++ Source or Header  |  1994-12-09  |  4.1 KB  |  205 lines  |  [TEXT/MMCC]

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