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 / custom.wind.c next >
Text File  |  1996-06-22  |  11KB  |  408 lines

  1. /*
  2.  *--------------------------------------------------------------
  3.  * custom.wind.c
  4.  *--------------------------------------------------------------
  5.  *    I don't think this source is a good sample because I wrote
  6.  *    this just for fun rather than providing a versatile purposes.
  7.  *    Hence I named it custom.wind.c.
  8.  *    In this sample source, button drawings require EmbossDraw.c
  9.  *    that provides 3D-like lines, frames and texts drawing.
  10.  *--------------------------------------------------------------
  11.  */
  12. #include <AppleEvents.h>
  13.  
  14. #include "sample.h"
  15. #include "sample.dlog.h"
  16. #include "sample.send.h"
  17. #include "sample.drag.h"
  18. #include "custom.wind.h"
  19. #include "EmbossDraw.h"
  20.  
  21. /*
  22.  *--------------------------------------------------------------
  23.  * main window resource IDs
  24.  *--------------------------------------------------------------
  25.  */
  26. enum customWindowIDs {
  27.     rMainWindID    = 128,
  28.     rMainNameID    = 128
  29. };
  30.  
  31. /*
  32.  *--------------------------------------------------------------
  33.  * button number constants
  34.  *--------------------------------------------------------------
  35.  */
  36. enum customWindowItems {
  37.     mainOpenBttn    = 0,
  38.     mainPrintBttn,
  39.     mainSendBttn,
  40.     mainAboutBttn,
  41.     mainQuitBttn,
  42.     kNumOfMainBttns
  43. };
  44.  
  45. /*
  46.  *--------------------------------------------------------------
  47.  * button sizes
  48.  *--------------------------------------------------------------
  49.  */
  50. enum customWindowSizes{
  51.     kBttnHigh    = 24,
  52.     kBttnWide    = 96,
  53.     kMainHigh    = kBttnHigh * kNumOfMainBttns,
  54.     kMainWide    = kBttnWide
  55. };
  56.  
  57. /*
  58.  *--------------------------------------------------------------
  59.  *mouse status
  60.  *--------------------------------------------------------------
  61.  */
  62. enum constMouseStatus {
  63.     kMouseReleased,
  64.     kMousePressed
  65. };
  66.  
  67. /*
  68.  *--------------------------------------------------------------
  69.  *    global of the main window
  70.  *--------------------------------------------------------------
  71.  */
  72. WindowRef    gMainWindow;
  73.  
  74. /*
  75.  *--------------------------------------------------------------
  76.  *    staic variables only used in this source
  77.  *--------------------------------------------------------------
  78.  */
  79. static Rect    gMainBttns[kNumOfMainBttns];
  80. static Boolean    gBigState;
  81.  
  82. /*
  83.  *--------------------------------------------------------------
  84.  *    static functions only used in this source
  85.  *--------------------------------------------------------------
  86.  */
  87. static void SetTheWindowStandardState(WindowRef, const RectPtr);
  88. static short FindMyButton(Point);
  89. static Boolean TrackMyButton(const short, Point);
  90. static void DrawEmbossedBttn(const short, Boolean);
  91.  
  92. /*
  93.  *--------------------------------------------------------------
  94.  *    macros
  95.  *--------------------------------------------------------------
  96.  */
  97. extern RectPtr GetTheButtonRect(const short item);
  98. #define GetTheButtonRect(item)    (&gMainBttns[(item)])
  99.  
  100. /*
  101.  *--------------------------------------------------------------
  102.  * SetUpMyMainWindow
  103.  *--------------------------------------------------------------
  104.  *    create my main window, it will appear later
  105.  *--------------------------------------------------------------
  106.  */
  107. Boolean SetUpMyMainWindow(void)
  108. {
  109.     GrafPtr    savePort;
  110.     Str63    chicagoName;
  111.     short    chicagoFNum;
  112.     int    i;
  113.  
  114.     /* load window resource */
  115.     gMainWindow = GetNewCWindow(rMainWindID, kInHeap, kFrontMost);
  116.     if (gMainWindow == nil) return (false);
  117.  
  118.     GetPort(&savePort);
  119.     SetPortWindowPort(gMainWindow);
  120.  
  121.     /* set window sizes */
  122.     SizeWindow(gMainWindow, kMainWide, kMainHigh, false);
  123.     gBigState = true;
  124.  
  125.     /* set window font to Chicago at any international Systems */
  126.     GetIndString(chicagoName, rFontNameID, iChicagoIndex);
  127.     GetFNum(chicagoName, &chicagoFNum);
  128.     TextFont(chicagoFNum);
  129.     TextSize(12);
  130.  
  131.     /* set button sizes and positions */
  132.     for (i = 0; i < kNumOfMainBttns; ++i) {
  133.         SetRect(&gMainBttns[i], 0, 0, kBttnWide, kBttnHigh);
  134.         OffsetRect(&gMainBttns[i], 0, kBttnHigh * i);
  135.     }
  136.  
  137.     SetUpMyDragAndDrop(gMainWindow);    /* this is not essential */
  138.  
  139.     SetPort(savePort);
  140.     return (true);
  141. }
  142. /*
  143.  *--------------------------------------------------------------
  144.  * DrawMyMainWindow
  145.  *--------------------------------------------------------------
  146.  *    draw contents of the main window
  147.  *--------------------------------------------------------------
  148.  */
  149. void DrawMyMainWindow(void)
  150. {
  151.     GrafPtr    savePort;
  152.     short    i;
  153.  
  154.     GetPort(&savePort);
  155.     SetPortWindowPort(gMainWindow);
  156.  
  157.     for (i = mainOpenBttn; i < kNumOfMainBttns; ++i) {
  158.         DrawEmbossedBttn(i, kMouseReleased);
  159.     }
  160.     SetPort(savePort);
  161. }
  162. /*
  163.  *--------------------------------------------------------------
  164.  * ZoomMyMainWindow
  165.  *--------------------------------------------------------------
  166.  *    custom window zooming handler that alters full size and
  167.  *    one-button size
  168.  *--------------------------------------------------------------
  169.  */
  170. void ZoomMyMainWindow(const short zoom)
  171. {
  172.     GrafPtr    savedPort;
  173.     Rect    newRect;
  174.     Point    offsetPt;
  175.  
  176.     GetPort(&savedPort);
  177.     SetPortWindowPort(gMainWindow);
  178.  
  179.     if (gBigState) {
  180.         /* set window to one button big */
  181.         SetRect(&newRect, 0, 0, kMainWide, kBttnHigh);
  182.         gBigState = false;
  183.     } else {
  184.         /* set window to full size */
  185.         SetRect(&newRect, 0, 0, kMainWide, kMainHigh);
  186.         gBigState = true;
  187.     }
  188.     /* set the rectangle to global position */
  189.     offsetPt.v = 0, offsetPt.h = 0;
  190.     LocalToGlobal(&offsetPt);
  191.     OffsetRect(&newRect, offsetPt.h, offsetPt.v);
  192.  
  193.     if (zoom == inZoomOut) {
  194.         SetTheWindowStandardState(gMainWindow, &newRect);
  195.     }
  196.     ZoomWindow(gMainWindow, zoom, false);
  197.     SetPort(savedPort);
  198. }
  199. /*
  200.  *--------------------------------------------------------------
  201.  * SetTheWindowStandardState
  202.  *--------------------------------------------------------------
  203.  *    set the window zoom state
  204.  *--------------------------------------------------------------
  205.  */
  206. static void SetTheWindowStandardState(WindowRef theWindow, const RectPtr theRect)
  207. {
  208.     /* use a new Window Accessor macro in the Windows.h */
  209.     SetWindowStandardState(theWindow, theRect);
  210. }
  211. /*
  212.  *--------------------------------------------------------------
  213.  * DoMyMainWindow
  214.  *--------------------------------------------------------------
  215.  *    handle mouse event of main window
  216.  *--------------------------------------------------------------
  217.  */
  218. void DoMyMainWindow(Point mousePt)
  219. {
  220.     GrafPtr    savePort;
  221.     short    aButton;
  222.  
  223.     GetPort(&savePort);
  224.     SetPortWindowPort(gMainWindow);
  225.  
  226.     GlobalToLocal(&mousePt);
  227.     aButton = FindMyButton(mousePt);
  228.     if (aButton >= mainOpenBttn && aButton <= mainQuitBttn) {
  229.         if (TrackMyButton(aButton, mousePt)) {
  230.             switch (aButton) {
  231.             case mainOpenBttn:    DoOpenFile();        break;
  232.             case mainPrintBttn:    DoPrintFile();        break;
  233.             case mainSendBttn:    DoHoldFile();        break;
  234.             case mainAboutBttn:    DoAboutMe();        break;
  235.             case mainQuitBttn:    gForever = false;    break;
  236.             }
  237.         }
  238.     }
  239.     SetPort(savePort);
  240. }
  241. /*
  242.  *--------------------------------------------------------------
  243.  * FindMyButton
  244.  *--------------------------------------------------------------
  245.  *    find one of the button of main window
  246.  *--------------------------------------------------------------
  247.  */
  248. static short FindMyButton(Point thePt)
  249. {
  250.     int    i;
  251.  
  252.     for (i = mainOpenBttn; i < kNumOfMainBttns; ++i) {
  253.         if (PtInRect(thePt, &gMainBttns[i])) {
  254.             return (i);
  255.         }
  256.     }
  257.     return (-1);
  258. }
  259. /*
  260.  *--------------------------------------------------------------
  261.  * TrackMyButton
  262.  *--------------------------------------------------------------
  263.  *    track mouse position in the button rect
  264.  *--------------------------------------------------------------
  265.  */
  266. static Boolean TrackMyButton(
  267.     const short theItem,
  268.     Point thePt)
  269. {
  270.     RectPtr    aRect = GetTheButtonRect(theItem);
  271.     Boolean    inTheRect = false;
  272.  
  273.     if (StillDown()) {
  274.         do {
  275.             GetMouse(&thePt);
  276.             if (PtInRect(thePt, aRect)) {
  277.                 if (inTheRect == false) {
  278.                     DrawEmbossedBttn(theItem, inTheRect = true);
  279.                 }
  280.             } else {
  281.                 if (inTheRect) {
  282.                     DrawEmbossedBttn(theItem, inTheRect = false);
  283.                 }
  284.             }
  285.         } while (WaitMyMouseUp());
  286.  
  287.         if (inTheRect) {
  288.             DrawEmbossedBttn(theItem, false);
  289.         }
  290.     }
  291.     return (inTheRect);
  292. }
  293. /*
  294.  *--------------------------------------------------------------
  295.  * DrawEmbossedBttn
  296.  *--------------------------------------------------------------
  297.  *    draw embossed frame of main buttons
  298.  *    it assumes that the GrafPort has been already set
  299.  *--------------------------------------------------------------
  300.  */
  301. static void DrawEmbossedBttn(const short theItem, Boolean isPressed)
  302. {
  303.     RectPtr    aRect = GetTheButtonRect(theItem);
  304.     Str255    itsName;
  305.  
  306.     /* draw the button title */
  307.     GetIndString(itsName, rMainNameID, theItem + 1);
  308.     EmbossTextBox(&itsName[1], itsName[0], aRect, teCenter, isPressed, false);
  309.  
  310.     /* draw the 3D frame */
  311.     if (isPressed) {
  312.         DentedRect(aRect);
  313.     } else {
  314.         RaisedRect(aRect);
  315.     }
  316. }
  317. /*
  318.  *--------------------------------------------------------------
  319.  * IsMouseInTheButtons
  320.  *--------------------------------------------------------------
  321.  *    this routine is an alternative of WaitMouseUp
  322.  *    it can give more working chance to background tasks
  323.  *--------------------------------------------------------------
  324.  */
  325. Boolean IsMouseInTheButtons(Point mousePt)
  326. {
  327.     GrafPtr            savePort;
  328.     RectPtr            aRect = GetTheButtonRect(mainOpenBttn);
  329.     short            currButton;
  330.     static short    prevButton = -1;
  331.     Boolean            nowIntheButton;
  332.  
  333.     GetPort(&savePort);
  334.     SetPortWindowPort(gMainWindow);
  335.  
  336.     /* check mouse location */
  337.     GlobalToLocal(&mousePt);
  338.  
  339.     currButton = FindMyButton(mousePt);
  340.     nowIntheButton = ((currButton >= mainOpenBttn) && (currButton <= mainSendBttn));
  341.  
  342.     if (prevButton != currButton) {
  343.         if ((prevButton >= mainOpenBttn) && (prevButton <= mainSendBttn)) {
  344.             /* draw previous button up */
  345.             DrawEmbossedBttn(prevButton, false);
  346.         }
  347.         if (nowIntheButton) {
  348.             /* draw current button down */
  349.             DrawEmbossedBttn(currButton, true);
  350.         }
  351.         prevButton = currButton;
  352.     }
  353.     SetPort(savePort);
  354.     return (nowIntheButton);
  355. }
  356. /*
  357.  *--------------------------------------------------------------
  358.  * DoReceiveInTheButtons
  359.  *--------------------------------------------------------------
  360.  *    
  361.  *--------------------------------------------------------------
  362.  */
  363. OSErr DoReceiveOnTheButtons(const FSSpecPtr theSpec, Point mousePt)
  364. {
  365.     GrafPtr    savePort;
  366.     short    aButton;
  367.     OSErr    result = noErr;
  368.  
  369.     GetPort(&savePort);
  370.     SetPortWindowPort(gMainWindow);
  371.  
  372.     GlobalToLocal(&mousePt);
  373.     aButton = FindMyButton(mousePt);
  374.  
  375.     switch (aButton) {
  376.     case mainOpenBttn:
  377.         result = SendSpecToMyself(theSpec, kAEOpenDocuments);
  378.         break;
  379.     case mainPrintBttn:
  380.         result = SendAEToFinder(theSpec, kAEPrintDocuments);
  381.         break;
  382.     case mainSendBttn:
  383.         OpenMyViewDialog(theSpec);
  384.         break;
  385.     }
  386.     SetPort(savePort);
  387.     return (result);
  388. }
  389. /*
  390.  *--------------------------------------------------------------
  391.  * WaitMyMouseUp
  392.  *--------------------------------------------------------------
  393.  *    this routine is an alternative of WaitMouseUp
  394.  *    it can give more working chance to background tasks
  395.  *--------------------------------------------------------------
  396.  */
  397. Boolean WaitMyMouseUp(void)
  398. {
  399.     EventRecord    anEvent;
  400.  
  401.     if (WaitNextEvent(mUpMask, &anEvent, -1, nil)) {
  402.         if (anEvent.what == mouseUp) {
  403.             return (false);
  404.         }
  405.     }
  406.     return (true);
  407. }
  408.