home *** CD-ROM | disk | FTP | other *** search
/ HyperLib 1997 Winter - Disc 1 / HYPERLIB-1997-Winter-CD1.ISO.7z / HYPERLIB-1997-Winter-CD1.ISO / オンラインウェア / PRG / AESample.sit / AESample / sources / sample.dlog.c < prev    next >
Text File  |  1996-06-22  |  13KB  |  497 lines

  1. /*
  2.  *--------------------------------------------------------------
  3.  * sample.dlog.c
  4.  *--------------------------------------------------------------
  5.  */
  6. #include <Palettes.h>
  7.  
  8. #include "sample.h"
  9. #include "sample.menu.h"
  10. #include "sample.subs.h"
  11. #include "sample.send.h"
  12. #include "sample.dlog.h"
  13. #include "custom.wind.h"
  14. #include "EmbossDraw.h"
  15.  
  16. /* command values */
  17. enum constFillCommand {
  18.     kClearIt,
  19.     kFillIt
  20. };
  21.  
  22. /* global dialog poiter for file view */
  23. DialogRef    gViewDialog;
  24.  
  25. /* custom event filter for modal dialog */
  26.  
  27. /*
  28.  *--------------------------------------------------------------
  29.  *    type definitions for custom drawing routine
  30.  *--------------------------------------------------------------
  31.  */
  32. typedef struct {
  33.     RectPtr    frame;
  34.     short    active;
  35. } DrawPrmRec, *DrawPrmPtr;
  36.  
  37. /*
  38.  *--------------------------------------------------------------
  39.  *    static functions only used within this source
  40.  *--------------------------------------------------------------
  41.  */
  42. static Boolean SetUpMyViewDialog(void);
  43. static void SetUpMyViewContents(Boolean);
  44. static Boolean HiliteMyButton(DialogRef, const short);
  45. static pascal void DrawEachBoldButton(short, short, GDHandle, long);
  46. static void DrawDeepBoldButton(DrawPrmPtr, GDHandle);
  47. static void DrawBMapBoldButton(DrawPrmPtr);
  48.  
  49. /*
  50.  *--------------------------------------------------------------
  51.  *    static UPPs
  52.  *--------------------------------------------------------------
  53.  */
  54. static    DeviceLoopDrawingUPP    gBoldLoopUPP = nil;
  55.  
  56. /*
  57.  *--------------------------------------------------------------
  58.  * SetUpMyDialogs
  59.  *--------------------------------------------------------------
  60.  *    setup dialogs
  61.  *--------------------------------------------------------------
  62.  */
  63. Boolean SetUpMyDialogs(void)
  64. {
  65.  
  66.     /*
  67.         setup other dialogs
  68.     */
  69.  
  70.     /* install button frame UPP */
  71.     gBoldLoopUPP = NewDeviceLoopDrawingProc(DrawEachBoldButton);
  72.  
  73.     if (gBoldLoopUPP &&
  74.         SetUpMyViewDialog()
  75.     ) return (true);
  76.     return (false);
  77. }
  78. /*
  79.  *--------------------------------------------------------------
  80.  * DoDialogs
  81.  *--------------------------------------------------------------
  82.  *    handle modeless dialog events here
  83.  *--------------------------------------------------------------
  84.  */
  85. void DoDialogs(EventRecord *theEvent)
  86. {
  87.     DialogRef    aDialog;
  88.     short        itemHit = -1;
  89.  
  90.     switch (theEvent->what) {
  91.     case keyDown:
  92.     case autoKey:
  93.         aDialog = FrontWindow();
  94.         DoModelessDlgKeyDown(theEvent, &itemHit);
  95.         break;
  96.     case activateEvt:
  97.         aDialog = (DialogRef)theEvent->message;
  98.         if (aDialog == gViewDialog) {
  99.             FrameBoldButton(gViewDialog, ok);
  100.         }
  101.         break;
  102.     case updateEvt:
  103.         aDialog = (DialogRef)theEvent->message;
  104.         if (aDialog == gViewDialog) {
  105.             FrameBoldButton(gViewDialog, ok);
  106.             RaisedWindow(GetDialogWindow(gViewDialog));
  107.         }
  108.         break;
  109.     case osEvt:
  110.         DoOSEvent(theEvent);
  111.         break;
  112.     case diskEvt:
  113.         DoMountDisk(theEvent);
  114.         break;
  115.     }
  116.  
  117.     /* StdFilterProc supports Button bolding */
  118.     if (DialogSelect(theEvent, &aDialog, &itemHit)) {
  119.         if (aDialog == gViewDialog) {
  120.             DoMyViewDialog(itemHit);
  121.         }
  122.         /*
  123.             handle other dialog events
  124.         */
  125.     }
  126. }
  127. /*
  128.  *--------------------------------------------------------------
  129.  * DoModelessDlgKeyDown
  130.  *--------------------------------------------------------------
  131.  *    handle modeless dialog key events
  132.  *--------------------------------------------------------------
  133.  */
  134. void DoModelessDlgKeyDown(EventRecord * theEvent, short *itemHit)
  135. {
  136.     DialogRef    aDialog  = FrontWindow();
  137.     char        charCode = theEvent->message & charCodeMask;
  138.  
  139.     if (theEvent->modifiers & cmdKey) {
  140.         if (charCode == '.') {
  141.             *itemHit = cancel;
  142.         } else {
  143.             DoMenus(MenuKey(charCode));
  144.         }
  145.     } else {
  146.         switch (charCode) {
  147.         case  kEnterKey:
  148.         case kReturnKey:    *itemHit =     ok;    break;
  149.         case kDeleteKey:
  150.         case kEscapeKey:    *itemHit = cancel;    break;
  151.         }
  152.     }
  153.     if (*itemHit >= ok) {
  154.         if ((aDialog == gViewDialog) && (*itemHit >= diGetNew)) {
  155.             HiliteMyButton(aDialog, *itemHit);
  156.         } else {
  157.             HiliteMyButton(aDialog, *itemHit);
  158.         }
  159.     }
  160. }
  161. /*
  162.  *--------------------------------------------------------------
  163.  * SetUpMyViewDialog
  164.  *--------------------------------------------------------------
  165.  *    setup file view modeless dialog
  166.  *--------------------------------------------------------------
  167.  */
  168. static Boolean SetUpMyViewDialog(void)
  169. {
  170.     gViewDialog = GetNewDialog(dlogViewID, kInHeap, kFrontMost);
  171.     if (gViewDialog != nil) {
  172.  
  173.         /* set dialog user item procedure */
  174.         SetDialogUserItemUPP(gViewDialog, diInfoRect, GrooveItemUPP);
  175.         return (true);
  176.     }
  177.     return (false);
  178. }
  179. /*
  180.  *--------------------------------------------------------------
  181.  * OpenMyViewDialog
  182.  *--------------------------------------------------------------
  183.  *    show file view modeless dialog
  184.  *--------------------------------------------------------------
  185.  */
  186. void OpenMyViewDialog(const FSSpecPtr theSpec)
  187. {
  188.     /* replace with the newly given spec */
  189.     gDocSpec = *theSpec;
  190.  
  191.     /* setup dialog elements */
  192.     SetUpMyViewContents(kFillIt);
  193.  
  194.     if (IsWindowVisible(gViewDialog) == false) {
  195.         ShowWindow(gViewDialog);
  196.     }
  197.     if (FrontWindow() != gViewDialog) {
  198.         SelectWindow(gViewDialog);
  199.     }
  200. }
  201. /*
  202.  *--------------------------------------------------------------
  203.  * DoMyViewDialog
  204.  *--------------------------------------------------------------
  205.  *    handle file view modeless dialog events
  206.  *--------------------------------------------------------------
  207.  */
  208. void DoMyViewDialog(const short theItem)
  209. {
  210.     switch (theItem) {
  211.     case diSendIt:
  212.         DoSendFile();
  213.         break;
  214.     case diClearIt:
  215.         SetUpMyViewContents(kClearIt);
  216.         break;
  217.     case diGetNew:
  218.         DoHoldFile();
  219.         break;
  220.     }
  221. }
  222. /*
  223.  *--------------------------------------------------------------
  224.  * SetUpMyViewContents
  225.  *--------------------------------------------------------------
  226.  *    set contents of the view dialog
  227.  *--------------------------------------------------------------
  228.  */
  229. static void SetUpMyViewContents(Boolean theCommand)
  230. {
  231.     FInfo    itsFInfo;
  232.     Rect    iRect;
  233.     Handle    iHndl;
  234.     short    iType;
  235.     Str15    vRefNStr, dirIDStr;
  236.     Str15    creatStr, fTypeStr;
  237.  
  238.  
  239.     if ((theCommand == kFillIt) &&
  240.         (FSpGetFInfo(&gDocSpec, &itsFInfo) == noErr)) {
  241.  
  242.         /* set the new document info */
  243.         NumToString(gDocSpec.vRefNum, vRefNStr);
  244.         NumToString(gDocSpec.parID, dirIDStr);
  245.  
  246.         creatStr[0] = (char)sizeof(OSType);
  247.         BlockMoveData(&itsFInfo.fdCreator, &creatStr[1], sizeof(OSType));
  248.  
  249.         fTypeStr[0] = (char)sizeof(OSType);
  250.         BlockMoveData(&itsFInfo.fdType, &fTypeStr[1], sizeof(OSType));
  251.  
  252.         /* activate buttons */
  253.         GetDialogItem(gViewDialog, diSendIt, &iType, &iHndl, &iRect);
  254.         HiliteControl((ControlRef)iHndl, 0);
  255.  
  256.         GetDialogItem(gViewDialog, diClearIt, &iType, &iHndl, &iRect);
  257.         HiliteControl((ControlRef)iHndl, 0);
  258.     } else {
  259.         /* clear the current document info */
  260.         gDocSpec.name[0] = 0;
  261.         vRefNStr[0] = dirIDStr[0] = 0;
  262.         creatStr[0] = fTypeStr[0] = 0;
  263.  
  264.         /* inactivate buttons */
  265.         GetDialogItem(gViewDialog, diSendIt, &iType, &iHndl, &iRect);
  266.         HiliteControl((ControlRef)iHndl, 255);
  267.  
  268.         GetDialogItem(gViewDialog, diClearIt, &iType, &iHndl, &iRect);
  269.         HiliteControl((ControlRef)iHndl, 255);
  270.  
  271.     }
  272.     /* set the info strings */
  273.     GetDialogItem(gViewDialog, diFileName, &iType, &iHndl, &iRect);
  274.     SetDialogItemText(iHndl, gDocSpec.name);
  275.  
  276.     GetDialogItem(gViewDialog, diVRefNum,  &iType, &iHndl, &iRect);
  277.     SetDialogItemText(iHndl, vRefNStr);
  278.  
  279.     GetDialogItem(gViewDialog, diParentID, &iType, &iHndl, &iRect);
  280.     SetDialogItemText(iHndl, dirIDStr);
  281.  
  282.     GetDialogItem(gViewDialog, diCreator,  &iType, &iHndl, &iRect);
  283.     SetDialogItemText(iHndl, creatStr);
  284.  
  285.     GetDialogItem(gViewDialog, diFileType, &iType, &iHndl, &iRect);
  286.     SetDialogItemText(iHndl, fTypeStr);
  287.  
  288.     /* redraw button frame according to the button status */
  289.     FrameBoldButton(gViewDialog, ok);
  290. }
  291. /*
  292.  *--------------------------------------------------------------
  293.  * DoAboutMe
  294.  *--------------------------------------------------------------
  295.  *    show about dialog
  296.  *--------------------------------------------------------------
  297.  */
  298. void DoAboutMe(void)
  299. {
  300.     DialogRef    aboutDialog = GetNewDialog(dlogAboutID, kInHeap, kFrontMost);
  301.     if (aboutDialog != nil) {
  302.  
  303.         WindowRef    thisWindow = GetDialogWindow(aboutDialog);
  304.         GrafPtr        savePort;
  305.         short        itemHit;
  306.  
  307.         GetPort(&savePort);
  308.         SetPortWindowPort(thisWindow);
  309.  
  310.         {    /* set the font, size and mode */
  311.             Str15    fntName;
  312.             short    fntNum;
  313.             GetIndString(fntName, rFontNameID, iGenevaIndex);
  314.             GetFNum(fntName, &fntNum);
  315.             TextFont(fntNum);
  316.             TextSize(9);
  317.             TextMode(srcCopy);
  318.         }
  319.  
  320.         ShowWindow(thisWindow);
  321.         SelectWindow(thisWindow);
  322.         RaisedWindow(thisWindow);
  323.     
  324.         do {
  325.             ModalDialog(nil, &itemHit);
  326.         } while (itemHit > aboutWholeArea);
  327.  
  328.         if (StillDown()) {
  329.             while (WaitMyMouseUp()) {
  330.                 /* do nothing */
  331.             }
  332.         }
  333.         SetPort(savePort);
  334.         DisposeDialog(aboutDialog);
  335.     }
  336. }
  337. /*
  338.  *--------------------------------------------------------------
  339.  * HiliteMyButton
  340.  *--------------------------------------------------------------
  341.  *    auto press button proc
  342.  *--------------------------------------------------------------
  343.  */
  344. static Boolean HiliteMyButton(DialogRef theDialog, const short theItem)
  345. {
  346.     ControlRef    aButton;
  347.     Rect    iRect;
  348.     short    iType;
  349.  
  350.     GetDialogItem(theDialog, theItem, &iType, (Handle *)&aButton, &iRect);
  351.  
  352.     if (iType < kStaticTextDialogItem) {
  353.         long    endTick;
  354.  
  355.         HiliteControl(aButton, inButton);
  356.         Delay(8, &endTick);
  357.         HiliteControl(aButton, 0);
  358.         return (true);
  359.     }
  360.     return (false);
  361. }
  362. /*
  363.  *--------------------------------------------------------------
  364.  * FrameBoldButton
  365.  *--------------------------------------------------------------
  366.  *    frame bold round rect to default button
  367.  *--------------------------------------------------------------
  368.  */
  369. void FrameBoldButton(DialogRef theDialog, const short theItem)
  370. {
  371. #if !__powerpc
  372.     long        oldA5 = SetA5(SetCurrentA5());
  373. #endif
  374.     Rect        bttnRect;
  375.     ControlRef    aButton;
  376.     short        aType;
  377.  
  378.     /* get button's rectangle */
  379.     GetDialogItem(theDialog, theItem, &aType, (Handle *)&aButton, &bttnRect);
  380.     if ((aType == kButtonDialogItem) || (aType == kResourceControlDialogItem)) {
  381.  
  382.         GrafPtr        savedPort;
  383.         PenState    saveState;
  384.         RgnHandle    bttnRgn = NewRgn();
  385.         RgnHandle    clipRgn = NewRgn();
  386.  
  387.         /* set graphic port and pen */
  388.         GetPort(&savedPort);
  389.         SetGrafPortOfDialog(theDialog);
  390.         GetPenState(&saveState);
  391.         PenSize(3, 3);
  392.  
  393.         /* set the drawing data record and call device loop */
  394.         InsetRect(&bttnRect, -4, -4);
  395.         RectRgn(bttnRgn, &bttnRect);
  396.         GetClip(clipRgn);
  397.         SectRgn(bttnRgn, clipRgn, bttnRgn);
  398.  
  399.         /* draw the button */
  400.         if (EmptyRgn(bttnRgn) == false) {
  401.  
  402.             DrawPrmRec    drawData;
  403.             long        gestaltAnswer;
  404.             OSErr        result;
  405.  
  406.             drawData.frame = &bttnRect;
  407.             drawData.active = (
  408.                 (gNowBackGround == false) &&
  409.                 (FrontWindow() == GetDialogWindow(theDialog)) &&
  410.                  ((*aButton)->contrlHilite != kControlInactivePart)
  411.             );
  412.  
  413.             result = Gestalt(gestaltQuickdrawVersion, &gestaltAnswer);
  414.             if ((result == noErr) && (gestaltAnswer >= gestalt8BitQD)) {
  415.                 /* call device loop to draw all devices in which the frame resides */
  416.                 DeviceLoop(bttnRgn, gBoldLoopUPP, (long)&drawData, 0);
  417.             } else {
  418.                 DrawBMapBoldButton(&drawData);
  419.             }
  420.         }
  421.         DisposeRgn(bttnRgn);
  422.         DisposeRgn(clipRgn);
  423.  
  424.         SetPenState(&saveState);
  425.         SetPort(savedPort);
  426.     }
  427. #if !__powerpc
  428.     (void)SetA5(oldA5);
  429. #endif
  430. }
  431. /*
  432.  *--------------------------------------------------------------
  433.  * DrawEachBoldButton
  434.  *--------------------------------------------------------------
  435.  *    bold frame drawing device loop callback
  436.  *--------------------------------------------------------------
  437.  */
  438. static pascal void DrawEachBoldButton(
  439.     short        howDeep,
  440.     short        theFlag,
  441.     GDHandle    theDevice,
  442.     long        userData)
  443. {
  444.     #pragma unused (theFlag)
  445.  
  446.     DrawPrmPtr    drawParam = (DrawPrmPtr)userData;
  447.     if (howDeep > 1) {
  448.         DrawDeepBoldButton(drawParam, theDevice);
  449.     } else {
  450.         DrawBMapBoldButton(drawParam);
  451.     }
  452. }
  453. /*
  454.  *--------------------------------------------------------------
  455.  * DrawDeepBoldButton
  456.  *--------------------------------------------------------------
  457.  *    bold rectangle drawing routine on the deep device
  458.  *--------------------------------------------------------------
  459.  */
  460. static void DrawDeepBoldButton(DrawPrmPtr theParam, GDHandle theDevice)
  461. {
  462.     RGBColor    foreColor, backColor, grayColor, boldColor;
  463.  
  464.     GetForeColor(&foreColor);
  465.     GetBackColor(&backColor);
  466.     grayColor = foreColor;
  467.  
  468.     if (GetGray(theDevice, &backColor, &grayColor)) {
  469.         if (theParam->active) {
  470.             boldColor.red = boldColor.green = 0;
  471.             boldColor.blue = 0x1111;
  472.             RGBForeColor(&boldColor);
  473.         } else {
  474.             RGBForeColor(&grayColor);
  475.         }
  476.         FrameRoundRect(theParam->frame, 16, 16);
  477.         RGBForeColor(&foreColor);
  478.     } else {
  479.         DrawBMapBoldButton(theParam);
  480.     }
  481. }
  482. /*
  483.  *--------------------------------------------------------------
  484.  * DrawBMapBoldButton
  485.  *--------------------------------------------------------------
  486.  *    bold rectangle drawing routine on the bitmap device
  487.  *--------------------------------------------------------------
  488.  */
  489. static void DrawBMapBoldButton(DrawPrmPtr theParam)
  490. {
  491.     long    boldPat[2];
  492.  
  493.     boldPat[0] = boldPat[1] = (theParam->active) ? 0xFFFFFFFF : 0xAA55AA55;
  494.     PenPat((PatPtr)boldPat);
  495.     FrameRoundRect(theParam->frame, 16, 16);
  496. }
  497.