home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / C / Frameworks / PennyWise™ Framework / PennyView / TEMPLATE_WINDOW.c < prev    next >
Encoding:
Text File  |  1994-08-11  |  15.4 KB  |  452 lines  |  [TEXT/KAHL]

  1. //••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••
  2. //                                                                                //
  3. //                                                                                //
  4. //                    Copyright PennyWise Software, 1994.                            //
  5. //                                                                                //
  6. //            Part of the PennyWise Software Application Framework                //
  7. //                                                                                //
  8. //                                                                                //
  9. //            TEMPLATE_WINDOW.c            Written by Peter Kaplan                    //
  10. //                                                                                //
  11. //                                                                                //
  12. //••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••
  13. //••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••
  14. //
  15. //    This is a template for a PennyWise Software Application Framework window
  16. //
  17. //••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••
  18. #include "PWFramework.h"
  19. #include "PWWindowList.h"
  20. #include "WindowID.h"
  21. #include "TEMPLATE_WINDOW.h"
  22. //••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••
  23. //        Directions for customizing this templete to work
  24. //        with your own window types
  25. //
  26. //        1 —    Rename this file according to the function of the window (Unique Name).
  27. //            Ex. If it is a window for text call the file TextWindow.c
  28. //        2 - Reame the header file according to the function of the window.
  29. //        3 - Do a search and replace in this file and the header file.
  30. //            Search for the word TEMPLATE_WINDOW and replace it with the new 
  31. //            name of the file from step 1 & 2. 
  32. //        4 — Add this file to the project (Source Menu)
  33. //        5 - Open "WindowID.h" and add kWINDOW_ID_TEMPLATE_WINDOW value to 
  34. //            the list. Just take the next number available
  35. //        6 - Also in "WindowID.h" increment kMAX_WINDOW_IDS by one.
  36. //        7 — Add functionality as needed
  37. //        8 - Add the header file to InitApplication.c file and
  38. //            call Init[xxx]Handlers from InitAppliaction. [xxx] is the name from 1,2 & 3
  39. //        9 -    Add Open calls where needed.
  40. //
  41. //••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••
  42. // These procedures are static. They will only be called by name from here
  43. // outside refrences will use our window proc list.
  44. static    void    ThisWindowCreate    (EventRecord* theEvent, WindowPtr theWindow);
  45. static    short    ThisWindowDispose    (EventRecord* theEvent, WindowPtr theWindow);
  46. static    void    ThisWindowZoomIn    (EventRecord* theEvent, WindowPtr theWindow);
  47. static    void    ThisWindowZoomOut    (EventRecord* theEvent, WindowPtr theWindow);
  48. static    void    ThisWindowResize    (EventRecord* theEvent, WindowPtr theWindow);
  49. static    void    ThisWindowClick        (EventRecord* theEvent, WindowPtr theWindow);
  50. static    void    ThisWindowUpdate    (EventRecord* theEvent, WindowPtr theWindow);
  51. static    void    ThisWindowActivate    (EventRecord* theEvent, WindowPtr theWindow);
  52. static    void    ThisWindowDeactivate(EventRecord* theEvent, WindowPtr theWindow);
  53. static    void    ThisWindowDrag        (EventRecord* theEvent, WindowPtr theWindow);
  54. static    void    ThisWindowIdle        (EventRecord* theEvent, WindowPtr theWindow);
  55. static    void    ThisWindowCursor    (EventRecord* theEvent, WindowPtr theWindow);
  56. static    void    ThisWindowKeyDown    (EventRecord* theEvent, WindowPtr theWindow);
  57. static    void    ThisWindowPreMenu    (EventRecord* theEvent, WindowPtr theWindow);
  58. static    void    ThisWindowPostMenu    (EventRecord* theEvent, WindowPtr theWindow);
  59. static    short    ThisWindowDoMenu    (EventRecord* theEvent, WindowPtr theWindow, short theMenu, short theItem, short theWindowID);
  60. static    void    ThisWindowGrowRect    (EventRecord* theEvent, WindowPtr theWindow, Rect* theRect);
  61. static    void    ThisWindowBackground(EventRecord* theEvent, WindowPtr theWindow);
  62. static    void    ThisWindowGetScrap    (EventRecord* theEvent, WindowPtr theWindow);
  63. static    void    ThisWindowPutScrap    (EventRecord* theEvent, WindowPtr theWindow);
  64. //••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••
  65. // This record holds all the information about this window
  66. // You can add fields to this record.
  67. // NOTE:ALL WINDOW RECORDS MUST START WITH THIS
  68. //        HEADER OR ELSE THE FRAMEWORK WILL NOT 
  69. //        FUNCTION PROPERLY. YOU'VE BEEN WARNED!
  70. typedef struct    OurWinRecord {
  71.     WindowParamHeader    theHeader;
  72.     short                isDirty;
  73.     }OurWinRecord, *OurWinPtr, **OurWinHandle;
  74. //••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••
  75. // Macros for accessing the header data
  76. // you can add your own as you add fields
  77. // to the OurWinRecord. 
  78. // NOTE:THESE MACROS ASSUME theData HOLDS
  79. //        A VALID COPY OF OurWinHandle. 
  80. #define THE_ID        (*theData)->theHeader.theID
  81. #define IS_DIRTY    (*theData)->isDirty
  82. //••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••
  83. void InitTEMPLATE_WINDOWHandlers()
  84. {
  85.     PWInstallWindowType    (kWINDOW_ID_TEMPLATE_WINDOW, kWINDOW_TYPE_APPLICATION);
  86.     PWInstallCreate        (kWINDOW_ID_TEMPLATE_WINDOW, ThisWindowCreate);
  87.     PWInstallDispose    (kWINDOW_ID_TEMPLATE_WINDOW, ThisWindowDispose);
  88.     PWInstallZoomIn        (kWINDOW_ID_TEMPLATE_WINDOW, ThisWindowZoomIn);
  89.     PWInstallZoomOut    (kWINDOW_ID_TEMPLATE_WINDOW, ThisWindowZoomOut);
  90.     PWInstallResize        (kWINDOW_ID_TEMPLATE_WINDOW, ThisWindowResize);
  91.     PWInstallClick        (kWINDOW_ID_TEMPLATE_WINDOW, ThisWindowClick);
  92.     PWInstallUpdate        (kWINDOW_ID_TEMPLATE_WINDOW, ThisWindowUpdate);
  93.     PWInstallActivate    (kWINDOW_ID_TEMPLATE_WINDOW, ThisWindowActivate);
  94.     PWInstallDeactivate    (kWINDOW_ID_TEMPLATE_WINDOW, ThisWindowDeactivate);
  95.     PWInstallIdle        (kWINDOW_ID_TEMPLATE_WINDOW, ThisWindowIdle);
  96.     PWInstallCursor        (kWINDOW_ID_TEMPLATE_WINDOW, ThisWindowCursor);
  97.     PWInstallKeyDown    (kWINDOW_ID_TEMPLATE_WINDOW, ThisWindowKeyDown);
  98.     PWInstallDrag        (kWINDOW_ID_TEMPLATE_WINDOW, ThisWindowDrag);
  99.     PWInstallPreMenu    (kWINDOW_ID_TEMPLATE_WINDOW, ThisWindowPreMenu);
  100.     PWInstallMenu        (kWINDOW_ID_TEMPLATE_WINDOW, ThisWindowDoMenu);
  101.     PWInstallPostMenu    (kWINDOW_ID_TEMPLATE_WINDOW, ThisWindowPostMenu);
  102.     PWInstallGrowRect    (kWINDOW_ID_TEMPLATE_WINDOW, ThisWindowGrowRect);
  103.     PWInstallBackground    (kWINDOW_ID_TEMPLATE_WINDOW, ThisWindowBackground);
  104.     PWInstallScrap2Appl    (kWINDOW_ID_TEMPLATE_WINDOW, ThisWindowGetScrap);
  105.     PWInstallAppl2Scrap    (kWINDOW_ID_TEMPLATE_WINDOW, ThisWindowPutScrap);
  106. }
  107. //•••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••
  108. static    void    ThisWindowCreate    (EventRecord* theEvent, WindowPtr theWindow)
  109. {
  110. // This routine will very rarely contain anything worthwhile
  111. // You will make custom routines for most windows that will be exported
  112. // via the include file
  113. }
  114. //•••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••
  115. //    Since we will be exporting this we don't have to conform to any particular input format
  116. void    TEMPLATE_WINDOWOpen(void)
  117. {
  118. WindowPtr        theWindow;
  119. OurWinHandle    theData;
  120. Rect            windowBounds;
  121.  
  122.     SetRect(&windowBounds, 40, 40, 200,200);
  123.     theWindow = NewWindow(NULL,&windowBounds,"\p",FALSE,zoomNoGrow,(WindowPtr)-1,TRUE,0);
  124.     if (theWindow) {
  125.         // We sucessfully got the window
  126.         
  127.         // Now we have to allocate our storage
  128.                 theData = (OurWinHandle) NewHandle(sizeof(OurWinRecord));
  129.         if (theData) {
  130.             // We sucessfully allocated storage
  131.             
  132.             // So Lets set the port to our new window
  133.             SetPort(theWindow);
  134.             
  135.             // Here we would do any screen/size manipulations
  136.             // before we show the window
  137.             
  138.             
  139.             // Here we would allocate any other 
  140.             // objects that we need. (Controls, etc.)
  141.         
  142.             // Now lets set the id
  143.             THE_ID        = kWINDOW_ID_TEMPLATE_WINDOW;
  144.             IS_DIRTY    = FALSE;
  145.             
  146.             // Install our routines
  147.             SetWRefCon(theWindow, (long) theData);
  148.                     
  149.             // Show it & select it before we leave
  150.             ShowWindow(theWindow);
  151.             SelectWindow(theWindow);
  152.             }
  153.         else {
  154.             // We could not allocate memory for our window's data
  155.             // So lets get rid of the data
  156.             DisposeWindow(theWindow);
  157.             theWindow = NULL;
  158.             }
  159.         }
  160.     
  161.     if (!theWindow) {
  162.         SysBeep(1);
  163.         // We did not create the window
  164.         // You will probably want to put up a dialog here to explain why
  165.         }
  166. }
  167. //•••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••
  168. static    short    ThisWindowDispose    (EventRecord* theEvent, WindowPtr theWindow)
  169. {
  170. OurWinHandle    theData;
  171. short            theResults;
  172.  
  173.     theData = (OurWinHandle) GetWRefCon(theWindow);
  174.     
  175.     // Want to save it
  176.     theResults = TRUE;
  177.     
  178.     if (IS_DIRTY) {
  179.         // Data has been changed we would probably want to put up 
  180.         // a “Save Changes to x” dialog here 
  181.         // But I'm just going to reset IS_DIRTY
  182.         IS_DIRTY = FALSE;
  183.         }
  184.     
  185.     // Do whatever saving you need to do here
  186.     
  187.     // Now lets break it down
  188.     HideWindow(theWindow);
  189.     DisposeHandle((Handle)theData);
  190.     DisposeWindow(theWindow);
  191.     
  192. return theResults;    // True if we closed it, false if we did not [ex. pressed cancel in save dialog]    
  193. }
  194. //•••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••
  195. static    void    ThisWindowZoomIn    (EventRecord* theEvent, WindowPtr theWindow)
  196. {    // The defaults will do the right thing
  197. }
  198. //•••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••
  199. static    void    ThisWindowZoomOut    (EventRecord* theEvent, WindowPtr theWindow)
  200. {    // The defaults will do the right thing
  201. }
  202. //•••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••
  203. static    void    ThisWindowResize    (EventRecord* theEvent, WindowPtr theWindow)
  204. {
  205. OurWinHandle    theData;
  206. GrafPtr            oldPort;
  207.  
  208.  
  209.     GetPort(&oldPort);
  210.     SetPort(theWindow);
  211.  
  212.     theData = (OurWinHandle) GetWRefCon(theWindow);
  213.     
  214.     EraseRect(&theWindow->portRect);
  215.  
  216.     // Do any control moving here
  217.     //•••••••••••••••••••••••••••
  218.         
  219.     InvalRect(&theWindow->portRect);
  220.  
  221.     SetPort(oldPort);
  222. }
  223. //•••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••
  224. static    void    ThisWindowClick        (EventRecord* theEvent, WindowPtr theWindow)
  225. {
  226. OurWinHandle    theData;
  227. GrafPtr            oldPort;
  228.  
  229.  
  230.     GetPort(&oldPort);
  231.     SetPort(theWindow);
  232.  
  233.     theData = (OurWinHandle) GetWRefCon(theWindow);
  234.     
  235.     // Test for hits here
  236.     //•••••••••••••••••••••••••••
  237.     
  238.     SetPort(oldPort);
  239. }
  240. //•••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••
  241. static    void    ThisWindowUpdate    (EventRecord* theEvent, WindowPtr theWindow)
  242. {
  243. OurWinHandle    theData;
  244. GrafPtr            oldPort;
  245.  
  246.  
  247.     GetPort(&oldPort);
  248.     SetPort(theWindow);
  249.  
  250.     theData = (OurWinHandle) GetWRefCon(theWindow);
  251.     
  252.     // Draw the window here
  253.     //•••••••••••••••••••••••••••
  254.     
  255.     SetPort(oldPort);
  256. }
  257. //•••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••
  258. static    void    ThisWindowActivate    (EventRecord* theEvent, WindowPtr theWindow)
  259. {
  260. OurWinHandle    theData;
  261. GrafPtr            oldPort;
  262.  
  263.  
  264.     GetPort(&oldPort);
  265.     SetPort(theWindow);
  266.  
  267.     theData = (OurWinHandle) GetWRefCon(theWindow);
  268.     
  269.     // Activate items here
  270.     //•••••••••••••••••••••••••••
  271.     
  272.     SetPort(oldPort);
  273. }
  274. //•••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••
  275. static    void    ThisWindowDeactivate(EventRecord* theEvent, WindowPtr theWindow)
  276. {
  277. OurWinHandle    theData;
  278. GrafPtr            oldPort;
  279.  
  280.  
  281.     GetPort(&oldPort);
  282.     SetPort(theWindow);
  283.  
  284.     theData = (OurWinHandle) GetWRefCon(theWindow);
  285.     
  286.     // Deactivate items here
  287.     //•••••••••••••••••••••••••••
  288.  
  289.     SetPort(oldPort);
  290. }
  291. //•••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••
  292. static    void    ThisWindowDrag        (EventRecord* theEvent, WindowPtr theWindow)
  293. {    // The default does the right thing
  294. }
  295. //•••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••
  296. static    void    ThisWindowIdle        (EventRecord* theEvent, WindowPtr theWindow)
  297. {
  298. OurWinHandle    theData;
  299. GrafPtr            oldPort;
  300.  
  301.  
  302.     GetPort(&oldPort);
  303.     SetPort(theWindow);
  304.  
  305.     theData = (OurWinHandle) GetWRefCon(theWindow);
  306.     
  307.     // do your idle tasks here
  308.     //•••••••••••••••••••••••••••    
  309.     
  310.     SetPort(oldPort);
  311. }
  312. //•••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••
  313. static    void    ThisWindowCursor    (EventRecord* theEvent, WindowPtr theWindow)
  314. {
  315. OurWinHandle    theData;
  316. GrafPtr            oldPort;
  317.  
  318.  
  319.     GetPort(&oldPort);
  320.     SetPort(theWindow);
  321.  
  322.     theData = (OurWinHandle) GetWRefCon(theWindow);
  323.     
  324.     // Adjust the cursor here
  325.     if (PtInRgn( theEvent->where, ((WindowPeek)theWindow)->strucRgn))  {
  326.         // We are in our window
  327.         
  328.         // More complex comparisons should be happening here
  329.         CopyRgn( ((WindowPeek)theWindow)->strucRgn,gMouseMovedRgn);
  330.         
  331.         // You would want to set it to a different cursor
  332.         SetCursor(&arrow);
  333.         }
  334.     else {    // We are outside our window
  335.     
  336.         // Lets make the rgn the whole qd coord
  337.         SetRectRgn(gMouseMovedRgn, -32768, -32768, 32767, 32767);
  338.         // except for our window
  339.         DiffRgn(gMouseMovedRgn,((WindowPeek)theWindow)->strucRgn,gMouseMovedRgn);
  340.         // and set it to an arrow
  341.         SetCursor(&arrow);
  342.         }
  343.     
  344.     SetPort(oldPort);
  345. }
  346. //•••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••
  347. static    void    ThisWindowKeyDown    (EventRecord* theEvent, WindowPtr theWindow)
  348. {
  349. OurWinHandle    theData;
  350. GrafPtr            oldPort;
  351. short            keyCode;
  352. short            charCode;
  353. Point            localPoint;
  354.  
  355.     GetPort(&oldPort);
  356.     SetPort(theWindow);
  357.  
  358.     theData = (OurWinHandle) GetWRefCon(theWindow);
  359.  
  360.     localPoint = theEvent->where;
  361.     GlobalToLocal(&localPoint);
  362.     
  363.     keyCode  = (theEvent->message&keyCodeMask)>>8;
  364.     charCode = theEvent->message&charCodeMask;
  365.  
  366.     // Handle the key click here
  367.     
  368.     
  369.     SetPort(oldPort);
  370. }
  371. //•••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••
  372. static    void    ThisWindowPreMenu    (EventRecord* theEvent, WindowPtr theWindow)
  373. {
  374. OurWinHandle    theData;
  375. GrafPtr            oldPort;
  376.  
  377.  
  378.     GetPort(&oldPort);
  379.     SetPort(theWindow);
  380.  
  381.     theData = (OurWinHandle) GetWRefCon(theWindow);
  382.     
  383.     // Enable and disable items
  384.     // change item names etc.
  385.     
  386.     
  387.     SetPort(oldPort);
  388. }
  389. //•••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••
  390. static    void    ThisWindowPostMenu    (EventRecord* theEvent, WindowPtr theWindow)
  391. {
  392. OurWinHandle    theData;
  393. GrafPtr            oldPort;
  394.  
  395.  
  396.     GetPort(&oldPort);
  397.     SetPort(theWindow);
  398.  
  399.     theData = (OurWinHandle) GetWRefCon(theWindow);
  400.     
  401.     // Enable and disable items
  402.     // change item names etc.
  403.     
  404.     
  405.     SetPort(oldPort);
  406. }
  407. //•••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••
  408. static    short    ThisWindowDoMenu    (EventRecord* theEvent, WindowPtr theWindow, short theMenu, short theItem, short theWindowID)
  409. {
  410. short    theResult;
  411.  
  412.     theResult = TRUE;
  413.     
  414.     // if we don't handle it return false so the defaults will pick it up
  415.     switch (theMenu) {
  416.         default:
  417.             theResult = FALSE;
  418.         }
  419.         
  420. return theResult;
  421. }
  422. //•••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••
  423. static    void    ThisWindowGrowRect    (EventRecord* theEvent, WindowPtr theWindow, Rect* theRect)
  424. {
  425. // This simply is the size of the rect the window can grow to
  426. SetRect(theRect, 64, 64, 32767, 32767);
  427. }
  428. //•••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••
  429. static    void    ThisWindowBackground(EventRecord* theEvent, WindowPtr theWindow)
  430. {
  431. // This routine will get called while the window is in the background
  432. // you may want a window to complete some task while not in the forground, etc.
  433. // NOTE:     If you do not do anything in this routine you can set the 
  434. //            windowBackground field to NULL. That will avoid a call
  435. //            THIS IS THE ONLY function that you can do that with!!
  436. }
  437. //•••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••
  438. static    void    ThisWindowGetScrap(EventRecord* theEvent, WindowPtr theWindow)
  439. {
  440. // This routine will get called when your application gets a resumeEvent 
  441. // AND the convertClipboardFlag bit is set
  442. // What must be done is the convertion of the clipboard to a private scrap
  443. TEFromScrap();
  444. }
  445. //•••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••
  446. static    void    ThisWindowPutScrap(EventRecord* theEvent, WindowPtr theWindow)
  447. {
  448. // This routine will get called when your application gets a suspendEvent 
  449. // What must be done is the convertion of the private scrap to the clipboard
  450. TEToScrap();
  451. }
  452. //•••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••