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

  1. //••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••
  2. //                                                                                //
  3. //                                                                                //
  4. //                    Copyright PennyWise Software, 1994.                            //
  5. //                                                                                //
  6. //            Part of the PennyWise Software Application Framework                //
  7. //                                                                                //
  8. //                                                                                //
  9. //            PictWindow.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 "PictWindow.h"
  22. #include <PWWindowUtils.h>
  23. #include "HandleMenus.h"
  24. #include <PWMenuUtils.h>
  25. //••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••
  26. PicHandle    GetOurPictHandle(FSSpecPtr theSpec);
  27. //••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••
  28. // These procedures are static. They will only be called by name from here
  29. // outside refrences will use our window proc list.
  30. static    void    ThisWindowCreate    (EventRecord* theEvent, WindowPtr theWindow);
  31. static    short    ThisWindowDispose    (EventRecord* theEvent, WindowPtr theWindow);
  32. static    void    ThisWindowZoomOut    (EventRecord* theEvent, WindowPtr theWindow);
  33. static    void    ThisWindowResize    (EventRecord* theEvent, WindowPtr theWindow);
  34. static    void    ThisWindowUpdate    (EventRecord* theEvent, WindowPtr theWindow);
  35. static    void    ThisWindowActivate    (EventRecord* theEvent, WindowPtr theWindow);
  36. static    void    ThisWindowDeactivate(EventRecord* theEvent, WindowPtr theWindow);
  37. static    void    ThisWindowIdle        (EventRecord* theEvent, WindowPtr theWindow);
  38. static    void    ThisWindowCursor    (EventRecord* theEvent, WindowPtr theWindow);
  39. static    void    ThisWindowPreMenu    (EventRecord* theEvent, WindowPtr theWindow);
  40. static    void    ThisWindowPostMenu    (EventRecord* theEvent, WindowPtr theWindow);
  41. static    short    ThisWindowDoMenu    (EventRecord* theEvent, WindowPtr theWindow, short theMenu, short theItem, short theWindowID);
  42. static    void    ThisWindowGrowRect    (EventRecord* theEvent, WindowPtr theWindow, Rect* theRect);
  43. //••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••
  44. // This record holds all the information about this window
  45. // You can add fields to this record.
  46. // NOTE:ALL WINDOW RECORDS MUST START WITH THIS
  47. //        HEADER OR ELSE THE FRAMEWORK WILL NOT 
  48. //        FUNCTION PROPERLY. YOU'VE BEEN WARNED!
  49. typedef struct    OurWinRecord {
  50.     WindowParamHeader    theHeader;
  51.     short                isDirty;
  52.     PicHandle            thePic;
  53.     }OurWinRecord, *OurWinPtr, **OurWinHandle;
  54. //••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••
  55. // Macros for accessing the header data
  56. // you can add your own as you add fields
  57. // to the OurWinRecord. 
  58. // NOTE:THESE MACROS ASSUME theData HOLDS
  59. //        A VALID COPY OF OurWinHandle. 
  60. #define THE_ID        (*theData)->theHeader.theID
  61. #define IS_DIRTY    (*theData)->isDirty
  62. #define THE_PIC        (*theData)->thePic
  63. //••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••
  64. #define PictWindow_RES_ID        128
  65. //••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••
  66. void InitPictWindowHandlers()
  67. {
  68.     PWInstallWindowType    (kWINDOW_ID_PictWindow, kWINDOW_TYPE_APPLICATION);
  69.     PWInstallCreate        (kWINDOW_ID_PictWindow, ThisWindowCreate);
  70.     PWInstallDispose    (kWINDOW_ID_PictWindow, ThisWindowDispose);
  71.     PWInstallZoomIn        (kWINDOW_ID_PictWindow, ThisWindowIdle);
  72.     PWInstallZoomOut    (kWINDOW_ID_PictWindow, ThisWindowZoomOut);
  73.     PWInstallResize        (kWINDOW_ID_PictWindow, ThisWindowResize);
  74.     PWInstallClick        (kWINDOW_ID_PictWindow, ThisWindowIdle);
  75.     PWInstallUpdate        (kWINDOW_ID_PictWindow, ThisWindowUpdate);
  76.     PWInstallActivate    (kWINDOW_ID_PictWindow, ThisWindowActivate);
  77.     PWInstallDeactivate    (kWINDOW_ID_PictWindow, ThisWindowDeactivate);
  78.     PWInstallIdle        (kWINDOW_ID_PictWindow, ThisWindowIdle);
  79.     PWInstallCursor        (kWINDOW_ID_PictWindow, ThisWindowCursor);
  80.     PWInstallKeyDown    (kWINDOW_ID_PictWindow, ThisWindowIdle);
  81.     PWInstallDrag        (kWINDOW_ID_PictWindow, ThisWindowIdle);
  82.     PWInstallPreMenu    (kWINDOW_ID_PictWindow, ThisWindowPreMenu);
  83.     PWInstallMenu        (kWINDOW_ID_PictWindow, ThisWindowDoMenu);
  84.     PWInstallPostMenu    (kWINDOW_ID_PictWindow, ThisWindowPostMenu);
  85.     PWInstallGrowRect    (kWINDOW_ID_PictWindow, ThisWindowGrowRect);
  86.     PWInstallBackground    (kWINDOW_ID_PictWindow, ThisWindowIdle);
  87.     PWInstallScrap2Appl    (kWINDOW_ID_PictWindow, ThisWindowIdle);
  88.     PWInstallAppl2Scrap    (kWINDOW_ID_PictWindow, ThisWindowIdle);
  89. }
  90. //•••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••
  91. static    void    ThisWindowCreate    (EventRecord* theEvent, WindowPtr theWindow)
  92. {
  93. // This routine will very rarely contain anything worthwhile
  94. // You will make custom routines for most windows that will be exported
  95. // via the include file
  96. }
  97. //•••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••
  98. //    Since we will be exporting this we don't have to conform to any particular input format
  99. void    PictWindowOpen(FSSpecPtr theSpec)
  100. {
  101. WindowPtr        theWindow;
  102. OurWinHandle    theData;
  103. PicHandle        thePic;
  104. Rect            pictRect;
  105.  
  106.     //SetRect(&windowBounds, 40, 40, 200,200);
  107.     theWindow = GetNewWindow(PictWindow_RES_ID, NULL, (WindowPtr)-1);
  108.     if (theWindow) {
  109.         // We sucessfully got the window
  110.         
  111.         // Now we have to allocate our storage
  112.                 theData = (OurWinHandle) NewHandle(sizeof(OurWinRecord));
  113.         if (theData) {
  114.             // We sucessfully allocated storage
  115.             
  116.             // So Lets set the port to our new window
  117.             SetPort(theWindow);
  118.             
  119.             // Here we would do any screen/size manipulations
  120.             // before we show the window
  121.             thePic = GetOurPictHandle( theSpec);
  122.             if (thePic) {
  123.                 pictRect = (*thePic)->picFrame;
  124.                 
  125.                 // We will not get update events now
  126.                 SetWTitle(theWindow, theSpec->name);
  127.                 
  128.                 // Here we would allocate any other 
  129.                 // objects that we need. (Controls, etc.)
  130.             
  131.                 // Now lets set the fields
  132.                 THE_ID        = kWINDOW_ID_PictWindow;
  133.                 IS_DIRTY    = FALSE;
  134.                 THE_PIC     = thePic;            
  135.                             
  136.                 // Install our routines
  137.                 SetWRefCon(theWindow, (long) theData);
  138.                         
  139.                 // Show it & select it before we leave
  140.                 ShowWindow(theWindow);
  141.                 SelectWindow(theWindow);
  142.                 }
  143.             else {
  144.                 // We could not allocate memory for our window's data
  145.                 // So lets get rid of the data
  146.                 DisposeWindow(theWindow);
  147.                 theWindow = NULL;
  148.                 DisposeHandle((Handle)theData);
  149.                 }
  150.             }
  151.         else {
  152.             // We could not allocate memory for our window's data
  153.             // So lets get rid of the data
  154.             DisposeWindow(theWindow);
  155.             theWindow = NULL;
  156.             }
  157.         }
  158.     
  159.     if (!theWindow) {
  160.         SysBeep(1);
  161.         // We did not create the window
  162.         // You will probably want to put up a dialog here to explain why
  163.         }
  164. }
  165. //•••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••
  166. static    short    ThisWindowDispose    (EventRecord* theEvent, WindowPtr theWindow)
  167. {
  168. OurWinHandle    theData;
  169. short            theResults;
  170.  
  171.     theData = (OurWinHandle) GetWRefCon(theWindow);
  172.     
  173.     // Want to save it
  174.     theResults = TRUE;
  175.     
  176.     if (IS_DIRTY) {
  177.         // Data has been changed we would probably want to put up 
  178.         // a “Save Changes to x” dialog here 
  179.         // But I'm just going to reset IS_DIRTY
  180.         IS_DIRTY = FALSE;
  181.         }
  182.     
  183.     // Do whatever saving you need to do here
  184.     
  185.     // Now lets break it down
  186.     HideWindow(theWindow);
  187.     DisposeHandle((Handle)THE_PIC);
  188.     DisposeHandle((Handle)theData);
  189.     DisposeWindow(theWindow);
  190.     
  191. return theResults;    // True if we closed it, false if we did not [ex. pressed cancel in save dialog]    
  192. }
  193. //•••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••
  194. //•••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••
  195. static    void    ThisWindowZoomOut    (EventRecord* theEvent, WindowPtr theWindow)
  196. {    
  197. OurWinHandle    theData;
  198. Rect            theRect;
  199. WStateData        *wsdp;
  200.  
  201.  
  202.     theData = (OurWinHandle) GetWRefCon(theWindow);
  203.  
  204.     // This makes the window only zoom out to the "Page Setup" size
  205.     // This will change the size depending on the Print, Etc.
  206.  
  207.     // De-ref the data--make sure we don't move memory
  208.     wsdp = (WStateData*) *(((WindowPeek)theWindow)->dataHandle);
  209.  
  210.     theRect = (*THE_PIC)->picFrame;
  211.     wsdp->stdState.right = wsdp->stdState.left +  (theRect.right- theRect.left);
  212.     wsdp->stdState.bottom = wsdp->stdState.top +  (theRect.bottom- theRect.top);
  213. }
  214. //•••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••
  215. static    void    ThisWindowResize    (EventRecord* theEvent, WindowPtr theWindow)
  216. {
  217. OurWinHandle    theData;
  218. GrafPtr            oldPort;
  219.  
  220.     GetPort(&oldPort);
  221.     SetPort(theWindow);
  222.  
  223.     theData = (OurWinHandle) GetWRefCon(theWindow);
  224.     
  225.     EraseRect(&theWindow->portRect);
  226.  
  227.     // Do any control moving here
  228.     //•••••••••••••••••••••••••••
  229.         
  230.     InvalRect(&theWindow->portRect);
  231.  
  232.     SetPort(oldPort);
  233. }
  234. //•••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••
  235. static    void    ThisWindowUpdate    (EventRecord* theEvent, WindowPtr theWindow)
  236. {
  237. OurWinHandle    theData;
  238. GrafPtr            oldPort;
  239. Rect            theRect;
  240.  
  241.     GetPort(&oldPort);
  242.     SetPort(theWindow);
  243.  
  244.     theData = (OurWinHandle) GetWRefCon(theWindow);
  245.     
  246.     // Draw the window here
  247.     //•••••••••••••••••••••••••••
  248.     DrawGrowIcon(theWindow);
  249.     theRect = theWindow->portRect;
  250.     theRect.right-=16;
  251.     theRect.bottom-=16;
  252.     DrawPicture(THE_PIC, &theRect);
  253.         
  254.     SetPort(oldPort);
  255. }
  256. //•••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••
  257. static    void    ThisWindowActivate    (EventRecord* theEvent, WindowPtr theWindow)
  258. {
  259. OurWinHandle    theData;
  260. GrafPtr            oldPort;
  261.  
  262.  
  263.     GetPort(&oldPort);
  264.     SetPort(theWindow);
  265.  
  266.     theData = (OurWinHandle) GetWRefCon(theWindow);
  267.     
  268.     // Activate items here
  269.     //•••••••••••••••••••••••••••
  270.     DrawGrowIcon(theWindow);
  271.     
  272.     SetPort(oldPort);
  273. }
  274. //•••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••
  275. static    void    ThisWindowDeactivate(EventRecord* theEvent, WindowPtr theWindow)
  276. {
  277. OurWinHandle    theData;
  278. GrafPtr            oldPort;
  279.  
  280.     GetPort(&oldPort);
  281.     SetPort(theWindow);
  282.  
  283.     theData = (OurWinHandle) GetWRefCon(theWindow);
  284.     
  285.     // Deactivate items here
  286.     //•••••••••••••••••••••••••••
  287.     DrawGrowIcon(theWindow);
  288.  
  289.     SetPort(oldPort);
  290. }
  291. //•••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••
  292. static    void    ThisWindowIdle        (EventRecord* theEvent, WindowPtr theWindow)
  293. {
  294.  
  295. }
  296. //•••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••
  297. static    void    ThisWindowCursor    (EventRecord* theEvent, WindowPtr theWindow)
  298. {
  299. OurWinHandle    theData;
  300. GrafPtr            oldPort;
  301. CursHandle        hCurs;
  302. Rect            controlRect;
  303. Point            localPoint;
  304. RgnHandle        theRgn;
  305.  
  306.     GetPort(&oldPort);
  307.     SetPort(theWindow);
  308.  
  309.     theData = (OurWinHandle) GetWRefCon(theWindow);
  310.     
  311.     theRgn = NewRgn();
  312.     if (theRgn) {
  313.         // Adjust the cursor here
  314.         if (PtInRgn( theEvent->where, ((WindowPeek)theWindow)->contRgn))  {
  315.         
  316.             // OK the mouse is in the window lets find out where
  317.             
  318.             localPoint = theEvent->where;
  319.             GlobalToLocal(&localPoint);
  320.         
  321.             // Make a box the size of viewRect
  322.             controlRect = theWindow->portRect;
  323.             controlRect.right-=16;
  324.             controlRect.bottom-=16;
  325.             
  326.             if (PtInRect(localPoint,&controlRect)) {
  327.                 // OK its in the viewRect area make the cursor an iBeam
  328.                 LocalToGlobalRect(&controlRect);
  329.                 
  330.                 // Set the mouse moved to be the box we are in
  331.                 RectRgn(gMouseMovedRgn,&controlRect);
  332.                 hCurs = GetCursor(crossCursor);
  333.                 HLock((Handle)hCurs);
  334.                 SetCursor(*hCurs);
  335.                 }
  336.             else {
  337.                 controlRect = theWindow->portRect;
  338.                 controlRect.top = controlRect.bottom -16;
  339.                     
  340.                 LocalToGlobalRect(&controlRect);
  341.                 RectRgn(theRgn,&controlRect);
  342.                 
  343.                 controlRect = theWindow->portRect;
  344.                 controlRect.top = controlRect.right -16;
  345.                 LocalToGlobalRect(&controlRect);
  346.                 RectRgn(gMouseMovedRgn,&controlRect);
  347.                 
  348.                 UnionRgn(theRgn,gMouseMovedRgn,gMouseMovedRgn);
  349.                 SetCursor(&arrow);
  350.                 }
  351.             }
  352.         else {
  353.             SetRectRgn(gMouseMovedRgn,-32768,-32768,32767,32767);
  354.             DiffRgn(gMouseMovedRgn,((WindowPeek)theWindow)->contRgn,gMouseMovedRgn);
  355.             SetCursor(&arrow);
  356.             }
  357.             
  358.         DisposeRgn(theRgn);
  359.         }
  360.         
  361.  
  362.     SetPort(oldPort);
  363. }
  364. //•••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••
  365. static    void    ThisWindowPreMenu    (EventRecord* theEvent, WindowPtr theWindow)
  366. {
  367. OurWinHandle    theData;
  368. GrafPtr            oldPort;
  369. Str63            theString;
  370. Boolean            quotes;
  371.  
  372.     GetPort(&oldPort);
  373.     SetPort(theWindow);
  374.  
  375.     theData = (OurWinHandle) GetWRefCon(theWindow);
  376.     
  377.     // Enable and disable items
  378.     // change item names etc.
  379.     
  380.     // When we inable undo, we can add this
  381.     DisableItem( gMenuEdit, kEDIT_UNDO);
  382.     DisableItem( gMenuEdit, kEDIT_CUT);
  383.     DisableItem( gMenuEdit, kEDIT_COPY);
  384.     DisableItem( gMenuEdit, kEDIT_PASTE);
  385.     DisableItem( gMenuEdit, kEDIT_CLEAR);
  386.     DisableItem( gMenuEdit, kEDIT_SEL_ALL);
  387.  
  388.     // Lets handle the file name stuff
  389.     DisableItem( gMenuFile, kFILE_SAVE);
  390.     DisableItem( gMenuFile, kFILE_SAVEAS);
  391.     DisableItem( gMenuFile, kFILE_PRINT);
  392.     DisableItem( gMenuFile, kFILE_PONE);
  393.  
  394.     GetWTitle(theWindow, theString);
  395.     AddNameToMenu( gMenuFile, kFILE_CLOSE, theString, FALSE, TRUE);
  396.     
  397.     SetPort(oldPort);
  398. }
  399. //•••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••
  400. static    void    ThisWindowPostMenu    (EventRecord* theEvent, WindowPtr theWindow)
  401. {
  402. OurWinHandle    theData;
  403. GrafPtr            oldPort;
  404.  
  405.     GetPort(&oldPort);
  406.     SetPort(theWindow);
  407.  
  408.     theData = (OurWinHandle) GetWRefCon(theWindow);
  409.     
  410.     // Enable and disable items
  411.     // change item names etc.
  412.     EnableItem( gMenuEdit, kEDIT_UNDO);
  413.     EnableItem( gMenuEdit, kEDIT_CUT);
  414.     EnableItem( gMenuEdit, kEDIT_COPY);
  415.     EnableItem( gMenuEdit, kEDIT_PASTE);
  416.     EnableItem( gMenuEdit, kEDIT_CLEAR);
  417.     EnableItem( gMenuEdit, kEDIT_SEL_ALL);
  418.  
  419.     // Lets handle the file name stuff
  420.     EnableItem( gMenuFile, kFILE_SAVE);
  421.     EnableItem( gMenuFile, kFILE_SAVEAS);
  422.     EnableItem( gMenuFile, kFILE_PRINT);
  423.     EnableItem( gMenuFile, kFILE_PONE);
  424.  
  425.     SetMenuItemToIndString(kMENU_ID_FILE, kFILE_CLOSE, gMenuFile);
  426.     
  427.     SetPort(oldPort);
  428. }
  429. //•••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••
  430. static    short    ThisWindowDoMenu    (EventRecord* theEvent, WindowPtr theWindow, short theMenu, short theItem, short theWindowID)
  431. {
  432. return FALSE;
  433. }
  434. //•••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••
  435. static    void    ThisWindowGrowRect    (EventRecord* theEvent, WindowPtr theWindow, Rect* theRect)
  436. {
  437. // This simply is the size of the rect the window can grow to
  438. SetRect(theRect, 64, 64, 32767, 32767);
  439. }
  440. //•••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••
  441. //•••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••
  442. PicHandle    GetOurPictHandle(FSSpecPtr theSpec)
  443. {
  444. Handle    thePic;
  445. OSErr    theErr;
  446. short    fRefNum;
  447. long    curEOF;
  448.  
  449.     thePic = NULL;
  450.     
  451.     theErr = FSpOpenDF(theSpec,fsRdPerm,&fRefNum);
  452.     if (theErr == noErr) {    
  453.         // The file opened ok
  454.         
  455.         // Get the EOF
  456.         theErr = GetEOF(fRefNum,&curEOF);
  457.         if (theErr == noErr) {
  458.             // got the eof ok
  459.             
  460.             // Set the file pos
  461.             theErr = SetFPos(fRefNum,fsFromStart,512);
  462.             if (theErr == noErr) {
  463.                 // Set the file pos OK
  464.                 
  465.                 // advance past the header
  466.                 curEOF-=512;
  467.                 if (curEOF>0) {
  468.                     // We still have pict :)
  469.                     
  470.                     
  471.                     thePic = NewHandle(curEOF);
  472.                     if (thePic) {
  473.                         HLock(thePic);
  474.                         
  475.                         theErr = FSRead(fRefNum,&curEOF,*thePic);
  476.                         if (theErr == noErr) {
  477.                             // All is OK
  478.                             }
  479.                         else {
  480.                             DisposeHandle(thePic);
  481.                             thePic = NULL;
  482.                             }
  483.                         HUnlock(thePic);
  484.                         }
  485.                     }
  486.                 }
  487.             }
  488.         }
  489. return (PicHandle)thePic;
  490. }