home *** CD-ROM | disk | FTP | other *** search
/ ETO Development Tools 2 / ETO Development Tools 2.iso / Tools - Objects / ResEdit / ResEdit 1.2 / Examples / CExamples / Source / ResXXXXEd.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-04-04  |  8.6 KB  |  275 lines  |  [TEXT/MPS ]

  1. /*
  2. File ResXXXXEd.c
  3.  
  4. Copyright Apple Computer, Inc. 1985-1989
  5. All rights reserved.
  6. */
  7.  
  8. #include    <types.h>
  9. #include    <memory.h>
  10. #include    <menus.h>
  11. #include    <resources.h>
  12.  
  13. #include    "ResEd.h"
  14.  
  15. #define windowWidth 300
  16. #define windowHeight 100
  17. #define deleteChr 8
  18. #define sizeOfMyResource 10
  19.  
  20. typedef struct rXXXXRec {
  21.     ParentHandle    father;     /* Back ptr to dad */
  22.     Str64            name;        /* The name of this editor */
  23.     WindowPtr        windPtr;    /* This view's window */
  24.     Boolean            rebuild;    /* Set true if things have changed */
  25.     Handle            hXXXX;        /* The resource we are working on */
  26. } rXXXXRec;
  27.  
  28. typedef    rXXXXRec    *rXXXXPtr;
  29. typedef    rXXXXPtr    *rXXXXHandle;
  30.  
  31. /* Function prototypes. */
  32. pascal void DoMenu(short menu, short item, rXXXXHandle myXXXX);
  33.  
  34.  
  35. /*- -    -  -  - -  -  -  - -  -  -    - -  -    -  - -    -  -  - -  -  -*/
  36.  
  37. /* Fix up the window name and title for our window. */
  38. void GetNameAndTitle (StringPtr windowTitle, StringPtr windowName, Handle thing)
  39. {    
  40.     strcpy(windowTitle, "\pXXXX");
  41.     SetETitle(thing, windowTitle);
  42.     strncpy(windowName, windowTitle, *windowTitle + 1);    /* Add 1 for the length byte */
  43. }
  44.  
  45. /*- -  -  -    - -  -    -  - -    -  -  - -  -  -  - -  -  -    - -  -    -*/
  46.  
  47. pascal void EditBirth(Handle thing, ParentHandle dad)
  48. {
  49.     rXXXXHandle    myXXXX;
  50.     rXXXXPtr    p;
  51.     WindowPtr    myWindow;
  52.     Str255        windowTitle,
  53.                 newName;
  54.  
  55.     /* Prepare window title and request creation of a new window */
  56.     GetNameAndTitle (&windowTitle, &newName, thing);
  57.     myWindow = EditorWindSetup(false, windowWidth, windowHeight, &windowTitle, &newName, true, dad);
  58.     
  59.     /* If we got a new window, then start up the editor */
  60.     if (myWindow != NULL) {
  61.         if ((GetHandleSize(thing)) == 0) {  /* This was called via a NEW, so make a new resource. */
  62.             FixHand(sizeOfMyResource, thing);
  63.         }
  64.  
  65.         /* Get memory for and handle to our instance record */
  66.         myXXXX = (rXXXXHandle)NewHandle(sizeof(rXXXXRec));
  67.         BubbleUp((Handle)myXXXX);
  68.         HLock((Handle)myXXXX);
  69.         
  70.         p = *myXXXX;
  71.         
  72.         /* Put information about this incarnation of the editor and the window it is */
  73.         /* serving into our record.(always passed around in the handle myXXXX).         */
  74.  
  75.         p->windPtr = myWindow;
  76.         p->father = dad;
  77.         
  78.         /* Use memcpy here since the editorNameChr is a null and would stop strncpy. */
  79.         memcpy(p->name, &newName, newName[0] + 1);    /* Add 1 for the length byte */
  80.         p->hXXXX = thing;
  81.  
  82.         /* Let the main program know who is to manage this window by giving it both    */
  83.         /* our resource ID number and our instance record handle.                    */
  84.         ((WindowPeek)myWindow)->windowKind = ResEdID();
  85.         ((WindowPeek)myWindow)->refCon = (long)myXXXX;
  86.  
  87.         /* Set up any menus,views, etc. for this window here. */
  88.         
  89.         HUnlock((Handle)myXXXX);
  90.     }
  91. }
  92.  
  93. /*- -  -  -    - -  -    -  - -    -  -  - -  -  -  - -  -  -    - -  -    -*/
  94.  
  95. /* Not used for editors. */
  96. pascal void PickBirth(ResType t,ParentHandle dad)
  97. {
  98.     #pragma    unused (t, dad)
  99. }
  100.  
  101. /*- -  -  -    - -  -    -  - -    -  -  - -  -  -  - -  -  -    - -  -    -*/
  102.  
  103. pascal void DoEvent(EventRecord *evt, rXXXXHandle myXXXX)
  104. {
  105.     Point    mousePoint;
  106.  
  107.     BubbleUp((Handle)myXXXX);        /* Move our item up in memory */
  108.     HLock((Handle)myXXXX);            /* Lock it down */
  109.     
  110.     /* Handle event passed to us by main program.  Just like a 'real' event loop, except…    */
  111.     /*  there is no loop and we don't have to handle as much because the main program        */
  112.     /*  will do all the stuff that doesn't apply to us.                                        */
  113.     
  114.     mousePoint = evt->where;        /* Point at which the event occured */
  115.     SetPort((*myXXXX)->windPtr);    /* Set the port to our window */
  116.     GlobalToLocal(&mousePoint);        /* Convert event location to local coords */
  117.     
  118.     switch (evt->what) {
  119.         case mouseDown:
  120.             break;                    /* Do any special mouse down processing here. */
  121.  
  122.         case activateEvt: 
  123.             AbleMenu(fileMenu, fileTop);
  124.             if (evt->modifiers & activeFlag) {
  125.                                     /* Do any activate processing here (such as inserting a menu) */
  126.             }
  127.             else {
  128.                                     /* Do any deactivate processing here (such as deleting a menu). */
  129.             }
  130.             
  131.             DrawMenuBar();            /* To make sure that everything is drawn properly if you are    */
  132.                                     /* installing a menu. */
  133.             break;
  134.  
  135.         case updateEvt:
  136.                                     /* Do the appropriate update processing here.  Remember that */
  137.                                     /* BeginUpdate has already been called. */
  138.             PaintRect(&(*myXXXX)->windPtr->portRect);
  139.             break;
  140.  
  141.         case keyDown:
  142.                                     /* Do any key processing here. */
  143.                     
  144.             /* Convert the delete character into a clear command. */
  145.             if ((evt->message & charCodeMask) == deleteChr) {
  146.                 DoMenu(editMenu, clearItem, myXXXX);
  147.             }
  148.  
  149.             break;
  150.  
  151.         case nullEvent:
  152.                                     /* Do any null event processing here (such as blinking a cursor).    */
  153.             break;
  154.     }
  155.     HUnlock((Handle)myXXXX);
  156. }
  157. /*- -  -  -    - -  -    -  - -    -  -  - -  -  -  - -  -  -    - -  -    -*/
  158.  
  159. pascal void DoInfoUpdate(short oldID, short newID, rXXXXHandle myXXXX)
  160. {
  161.     Str255    windowTitle,
  162.             windowName;
  163.  
  164.     /* Since our ID has changed, we need to change our window title */
  165.     GetNameAndTitle (&windowTitle, &windowName, (Handle)((*myXXXX)->hXXXX));
  166.     GetWindowTitle (&windowTitle, &windowName, true, (*myXXXX)->father);
  167.     
  168.     /* Save the new name in my data structure. */
  169.     /* Use memcpy here since the editorNameChr is a null and would stop strncpy. */
  170.     memcpy((*myXXXX)->name, &windowName, windowName[0] + 1);    /* Add 1 for the length byte. */
  171.     
  172.     SetWTitle((*myXXXX)->windPtr, &windowTitle);                /* Set the new window title. */
  173.  
  174.     /* Now, let our father object know that our ID has been changed */
  175.     (*((*myXXXX)->father))->rebuild = true;                        /* Rebuild the picker list. */
  176.     CallInfoUpdate(oldID, newID, (*((*myXXXX)->father))->wind->refCon,
  177.                    (*((*myXXXX)->father))->wind->windowKind);
  178. }
  179.  
  180. /*- -  -  -    - -  -    -  - -    -  -  - -  -  -  - -  -  -    - -  -    -*/
  181.  
  182. /* Close down the window and get rid of any memory that has been allocated. */
  183. static void DoClose(rXXXXHandle myXXXX)
  184. {
  185.     CloseWindow((*myXXXX)->windPtr);
  186.     WindReturn((*myXXXX)->windPtr);        /* Mark the window record as being available */
  187.     SetTheCursor(arrowCursor);            /* Make sure the cursor is the arrow cursor */
  188.     
  189.     /* Delete any menus that we added and redraw the menu bar.    */
  190.     /* Be sure to dispose of any handles you are done with.        */
  191.  
  192.     /* Release the resource if we were launched from a picker. */
  193.     if ((*((*myXXXX)->father))->name[1] != editorNameChr) {
  194.         ReleaseResource ((Handle)(*myXXXX)->hXXXX);              /* Let it be free (if it is not changed)! */
  195.     }
  196.  
  197.     DisposHandle((Handle)myXXXX);
  198. }
  199.  
  200. /*- -  -  -    - -  -    -  - -    -  -  - -  -  -  - -  -  -    - -  -    -*/
  201.  
  202. pascal void DoMenu(short menu, short item, rXXXXHandle myXXXX)
  203. {
  204.     short    saveRefNum;
  205.     Handle    hTemp;
  206.  
  207.     BubbleUp((Handle)myXXXX);
  208.     HLock((Handle)myXXXX);
  209.     SetPort((*myXXXX)->windPtr);        /* Set the port to our window */
  210.  
  211.     /* Again, we handle the menu stuff just as we would in a 'real' application    */
  212.     /* except that we only have to handle those items that apply to our editor.    */
  213.     
  214.     switch (menu) {
  215.         case fileMenu:
  216.             switch (item) {
  217.                 case closeItem:
  218.                     DoClose(myXXXX);    /* Close our window                        */
  219.                     return;                /* Return immediately since our resource is gone! */
  220.                     
  221.                 case saveItem:            /* Pass the save on to other windows.    */
  222.                     PassMenu (fileMenu, saveItem, (ParentHandle)myXXXX);
  223.                     break;
  224.         
  225.                 case revertItem:
  226.                     if (NeedToRevert ((*myXXXX)->windPtr, (Handle)(*myXXXX)->hXXXX)) {
  227.                         /* The area under the window will need to be updated         */
  228.                         InvalRect(&(*myXXXX)->windPtr->portRect);
  229.                         
  230.                         /* We will need to restore the current resource file        */
  231.                         /* reference number when we are done here.                    */
  232.                         saveRefNum = CurrentRes();
  233.                         
  234.                         /* We are going to be using the resource file we came from.    */
  235.                         UseResFile(HomeResFile((Handle)(*myXXXX)->hXXXX));
  236.                         
  237.                         /* Read in the old copy from disk (see documentation for     */
  238.                         /* revertResource).  Clear it out unless this was a newly     */
  239.                         /* created resource.                                        */
  240.                         if (!RevertResource((Handle)(*myXXXX)->hXXXX)) {
  241.                             /* The resource was newly added so we need to remove it.*/
  242.                             
  243.                             /* Save the handle so that we can dispose it later. */
  244.                             hTemp = (Handle)(*myXXXX)->hXXXX;
  245.                             
  246.                             /* Make sure that the picker list is rebuilt to remove    */
  247.                             /* this item.                                             */
  248.                             (*((*myXXXX)->father))->rebuild = true;
  249.                             
  250.                             DoClose(myXXXX);        /* Close the window.            */
  251.                             RemoveResource(hTemp);    /* Dispose the resource itself. */
  252.                             return;                    /* Since the resource is gone.    */
  253.                         }
  254.                         /* Go back to using the old resource file.                    */
  255.                         UseResFile(saveRefNum);
  256.                     }
  257.                     break;
  258.                     
  259.                 case getInfoItem:
  260.                     /* Put up the GetInfo window.                                */
  261.                     ShowInfo((Handle)(*myXXXX)->hXXXX, (ParentHandle)myXXXX);
  262.                     break;
  263.             }
  264.  
  265.         case editMenu:
  266.                 /* Implement the edit menu here.                                 */
  267.             switch (item) {
  268.                 case cutItem:    break;
  269.                 case copyItem:    break;
  270.                 case pasteItem:    break;
  271.                 case clearItem:    break;
  272.             }
  273.     }
  274. }
  275.