home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1995 January / macformat-020.iso / Shareware City / Developers / apps.to.go / DTS.Draw / PenSizeDialog.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-05-05  |  4.7 KB  |  190 lines  |  [TEXT/MPS ]

  1. /*
  2. ** Apple Macintosh Developer Technical Support
  3. **
  4. ** File:        PenSizeDialog.c
  5. ** Written by:    Eric Soldan
  6. **
  7. ** Copyright © 1993 Apple Computer, Inc.
  8. ** All rights reserved.
  9. */
  10.  
  11. /* You may incorporate this sample code into your applications without
  12. ** restriction, though the sample code has been provided "AS IS" and the
  13. ** responsibility for its operation is 100% yours.  However, what you are
  14. ** not permitted to do is to redistribute the source as "DSC Sample Code"
  15. ** after having made changes. If you're going to re-distribute the source,
  16. ** we require that you make it clear in the source that the code was
  17. ** descended from Apple Sample Code, but that you've made changes. */
  18.  
  19.  
  20.  
  21. /*****************************************************************************/
  22.  
  23.  
  24.  
  25. #include "App.h"            /* Get the application includes/typedefs, etc.    */
  26. #include "App.defs.h"        /* Get various application definitions.            */
  27. #include "App.protos.h"        /* Get the prototypes for the application.        */
  28.  
  29. #ifndef __TREEOBJ2__
  30. #include "TreeObj2.h"
  31. #endif
  32.  
  33.  
  34.  
  35. extern short    gPenHeight, gPenWidth, gCtlNum;
  36.  
  37.  
  38.  
  39. /*****************************************************************************/
  40. /*****************************************************************************/
  41.  
  42.  
  43.  
  44. /* Find out which tool was clicked or double-clicked on. */
  45.  
  46. #pragma segment PENS
  47.  
  48. static Boolean    PenSizeFilter(TEHandle teHndl, EventRecord *event, short *handled);
  49. static Boolean    PenSizeFilter(TEHandle teHndl, EventRecord *event, short *handled)
  50. {
  51. #ifndef __MWERKS__
  52. #pragma unused (teHndl, handled)
  53. #endif
  54.  
  55.     char    key;
  56.     short    i;
  57.  
  58.     key = event->message   & charCodeMask;
  59.     i   = event->modifiers & keyCodeMask;
  60.     if (i & cmdKey)                    return(false);
  61.     if (i & optionKey)                return(false);
  62.     if (key == 8)                    return(false);
  63.     if (key == 9)                    return(false);
  64.     if ((key >= 28) && (key <= 31)) return(false);
  65.  
  66.     if ((key < '0') || (key > '9')) return(true);
  67.  
  68.     return(false);
  69. }
  70.  
  71. OSErr    PENSInitContent(FileRecHndl frHndl, WindowPtr window)
  72. {
  73.     OSErr            err;
  74.     TreeObjHndl        root, cobj;
  75.     ControlHandle    ctl;
  76.     TEHandle        te;
  77.     Handle            txt;
  78.     short            penHeight, penWidth, ph, pw, i;
  79.     Boolean            gotSize;
  80.     Str15            pstr;
  81.  
  82.     err = AddControlSet(window, (*frHndl)->fileState.sfType, kwStandardVis, 0, 0, nil);
  83.     if (err) return(err);
  84.  
  85.     penHeight = gPenHeight;        /* Assume that there are none selected. */
  86.     penWidth  = gPenWidth;
  87.     gotSize   = false;
  88.  
  89.     frHndl = GetNextDocument(window, kDocFileType);
  90.     root   = (*frHndl)->d.doc.root;
  91.     for (i = (*root)->numChildren; i;) {
  92.         cobj = GetChildHndl(root, --i);
  93.         if (DoTreeObjMethod(cobj, GETSELECTMESSAGE, 0)) {
  94.             if ((*cobj)->type < GROUPOBJ) {
  95.                 ph = mDerefCommon(cobj)->penHeight;
  96.                 pw = mDerefCommon(cobj)->penWidth;
  97.                 if (!gotSize) {
  98.                     gotSize = true;
  99.                     penHeight = ph;
  100.                     penWidth  = pw;
  101.                     continue;
  102.                 }
  103.                 if (penHeight != ph) penHeight = -1;
  104.                 if (penWidth  != pw) penWidth  = -1;
  105.             }
  106.         }
  107.     }
  108.  
  109.     pstr[0] = 0;
  110.     if (!penHeight)
  111.         ++penHeight;
  112.     if (penHeight > -1)
  113.         pcpydec(pstr, penHeight);
  114.     txt = NewHandle(pstr[0]);
  115.     BlockMove(pstr + 1, *txt, pstr[0]);
  116.     CNum2Ctl(window, 100, &ctl);
  117.     te = (TEHandle)GetControlReference(ctl);
  118.     UseControlStyle(ctl);
  119.     DisposeHandle(CTESwapText(te, txt, nil, true));
  120.     TESetSelect(0, 32767, te);
  121.     CTESetKeyFilter(te, PenSizeFilter);
  122.  
  123.     pstr[0] = 0;
  124.     if (!penWidth)
  125.         ++penWidth;
  126.     if (penWidth > -1)
  127.         pcpydec(pstr, penWidth);
  128.     txt = NewHandle(pstr[0]);
  129.     BlockMove(pstr + 1, *txt, pstr[0]);
  130.     CNum2Ctl(window, 101, &ctl);
  131.     te = (TEHandle)GetControlReference(ctl);
  132.     DisposeHandle(CTESwapText(te, txt, nil, true));
  133.     TESetSelect(0, 32767, te);
  134.     CTESetKeyFilter(te, PenSizeFilter);
  135.  
  136.     return(err);
  137. }
  138.  
  139.  
  140.  
  141. /*****************************************************************************/
  142.  
  143.  
  144.  
  145. #pragma segment PENS
  146. OSErr    PENSFreeWindow(FileRecHndl frHndl, WindowPtr window)
  147. {
  148.     ControlHandle    ctl;
  149.     TEHandle        te;
  150.     Str15            pstr;
  151.     short            i;
  152.     TreeObjHndl        root, cobj;
  153.     OSErr            err;
  154.  
  155.     if (gCtlNum == -1) {
  156.         CNum2Ctl(window, 100, &ctl);
  157.         te = (TEHandle)GetControlReference(ctl);
  158.         BlockMove(*((*te)->hText), pstr + 1, (pstr[0] = (*te)->teLength));
  159.         gPenHeight = p2dec(pstr, nil);
  160.         if (!gPenHeight)
  161.             ++gPenHeight;
  162.         CNum2Ctl(window, 101, &ctl);
  163.         te = (TEHandle)GetControlReference(ctl);
  164.         BlockMove(*((*te)->hText), pstr + 1, (pstr[0] = (*te)->teLength));
  165.         gPenWidth = p2dec(pstr, nil);
  166.         if (!gPenWidth)
  167.             ++gPenWidth;
  168.         frHndl = GetNextDocument(window, kDocFileType);
  169.         root   = (*frHndl)->d.doc.root;
  170.         for (i = (*root)->numChildren; i;) {
  171.             cobj = GetChildHndl(root, --i);
  172.             if (DoTreeObjMethod(cobj, GETSELECTMESSAGE, 0)) {
  173.                 if ((*cobj)->type < GROUPOBJ) {
  174.                     err = ModifyChild(PENSIZE_EDIT, root, i, false);
  175.                     if (err) return(err);
  176.                     mDerefCommon(cobj)->penHeight = gPenHeight;
  177.                     mDerefCommon(cobj)->penWidth  = gPenWidth;
  178.                 }
  179.             }
  180.         }
  181.         HideWindow(window);
  182.         DoImageDocument(frHndl);
  183.     }
  184.  
  185.     return(noErr);
  186. }
  187.  
  188.  
  189.  
  190.