home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / C++ / Libraries / RgnMaster 1.0 / Source / Meat 'n Guts / RgnMaster.c++ < prev    next >
Encoding:
C/C++ Source or Header  |  1994-04-25  |  9.9 KB  |  411 lines  |  [TEXT/KAHL]

  1. /*
  2.         RgnMaster.c++
  3.         by Hiep Dam, 3G Software.
  4.         March 1994.
  5.         Last Update April 1994
  6.         Contact: America Online: Starlabs
  7.                  Delphi        : StarLabs
  8.                  Internet      : starlabs@aol.com
  9.                               or starlabs@delphi.com
  10.  
  11.         Guts, or shell of the application; mostly Macintosh interface stuff.
  12. */
  13.  
  14. #include <QDOffscreen.h>
  15. #include "Compat.h"
  16. #include "QDUtils.h"
  17. #include "DialogUtils.h"
  18. #include "Regions.h"
  19. #include "RgnWork.h"
  20. #include "Help Window.h"
  21. #include "About.h"
  22. #include "StringUtils.h"    // Courtesy of Tony Myles.
  23. #include "AppCore.h"
  24. #include "KeyUtils.h"
  25. #include "RgnMaster.h"
  26.  
  27. // ---------------------------------------------------------------------------
  28.  
  29. void InitMac();
  30. void SetupDialog();
  31. void LoopIt();
  32. void HandleEvent(EventRecord *theEvent);
  33. void HandleMenu(long menuResult);
  34.  
  35. // ---------------------------------------------------------------------------
  36.  
  37. // Globals
  38. short gAppFileRef;
  39. Boolean gDone = false;
  40. DialogPtr gMainDialog;
  41. MenuHandle gFileMenu, gEditMenu, gAppleMenu;
  42. short gCurrentItem = -1;
  43. Boolean gShowHelp = false;
  44. Boolean gPreviewAlways = true;
  45.  
  46. // ---------------------------------------------------------------------------
  47.  
  48. void main() {
  49.     InitMac();
  50.     SetCursor(*GetCursor(watchCursor));
  51.     CheckEnviron();
  52.     if (gEnviron.sysVersion < 0x0700) {
  53.         (void)Alert(kWrongSysVersionAlertID, nil);
  54.         ExitToShell();
  55.     }
  56.     SetupDialog();
  57.     SetCursor(&arrow);
  58.     LoopIt();
  59. } // END main
  60.  
  61. void LoopIt() {
  62.     EventRecord theEvent;
  63.     short itemHit;
  64.     DialogPtr theDialog;
  65.     
  66.     do {
  67.         UpdateHelpStatus();
  68.         if (GetNextEvent(everyEvent, &theEvent)) {
  69.             if (IsDialogEvent(&theEvent) && DialogSelect(&theEvent, &theDialog, &itemHit)) {
  70.                 switch(itemHit) {
  71.                     case convertAndReplaceItem:
  72.                         ConvertAndReplace();
  73.                     break;
  74.  
  75.                     case convertOnlyItem:
  76.                         ConvertOnly();
  77.                     break;
  78.  
  79.                     case viewClipboardItem:
  80.                         ShowClipboard();
  81.                     break;
  82.                     
  83.                     case rgnInfoItem:
  84.                         ShowRegionInfo();
  85.                     break;
  86.  
  87.                     case viewRgnItem:
  88.                         ViewRegion();
  89.                     break;
  90.  
  91.                     case copyToClipItem:
  92.                         CopyRegionToClipboard();
  93.                     break;
  94.  
  95.                     case saveToAppItem:
  96.                         SaveRegionToApplication();
  97.                     break;
  98.  
  99.                     case saveToFileItem:
  100.                         SaveRegionToFile();
  101.                     break;
  102.  
  103.                     case optionsItem:
  104.                         DoOptionsDialog();
  105.                     break;
  106.  
  107.                     case helpWindItem:
  108.                         ShowHelpWindow();
  109.                     break;
  110.  
  111.                     case aboutItem:
  112.                         DoRgnMasterAbout();
  113.                     break;
  114.  
  115.                     case quitItem:
  116.                         gDone = true;
  117.                     break;
  118.  
  119.                     case previewCheckbx:
  120.                         gPreviewAlways = !GetCtlValue((ControlHandle)GetDItemHdl(gMainDialog, previewCheckbx));
  121.                         SetCtlValue((ControlHandle)GetDItemHdl(gMainDialog, previewCheckbx), gPreviewAlways);
  122.                     break;
  123.                     case helpCheckbx:
  124.                         gShowHelp = !GetCtlValue((ControlHandle)GetDItemHdl(gMainDialog, helpCheckbx));
  125.                         SetCtlValue((ControlHandle)GetDItemHdl(gMainDialog, helpCheckbx), gShowHelp);
  126.                         if (!gShowHelp) {
  127.                             Rect helpRect;
  128.                             GetDItemRect(gMainDialog, helpItem, &helpRect);
  129.                             InsetRect(&helpRect, 1, 1);
  130.                             EraseRect(&helpRect);
  131.                             gCurrentItem = -1;
  132.                         }
  133.                     break;
  134.                 }
  135.             }
  136.             else
  137.                 HandleEvent(&theEvent);
  138.         }
  139.     } while (!gDone);
  140.  
  141.     SaveOptions();
  142. } // END LoopIt
  143.  
  144. // ---------------------------------------------------------------------------
  145.  
  146. void HandleEvent(EventRecord *theEvent) {
  147.     short i;
  148.  
  149.     switch(theEvent->what) {
  150.         case mouseDown:
  151.             short windowPart;
  152.             WindowPtr theWindow;
  153.             windowPart = FindWindow(theEvent->where, &theWindow);
  154.             switch (windowPart) {
  155.                 case inMenuBar:
  156.                     HandleMenu(MenuSelect(theEvent->where));
  157.                 break;
  158.  
  159.                 case inDrag:
  160.                     Rect limitRect = screenBits.bounds;
  161.                     InsetRect(&limitRect, 20, 20);
  162.                     DragWindow(theWindow, theEvent->where, &limitRect);
  163.                 break;
  164.  
  165.                 case inContent:
  166.                     if (FrontWindow() != theWindow)
  167.                         SelectWindow(theWindow);
  168.                     else if (theWindow == gHelpWindow) {
  169.                         SetPort(gHelpWindow);
  170.                         GlobalToLocal(&theEvent->where);
  171.                         ScrollHelpWindow(theEvent->where);
  172.                     }
  173.                 break;
  174.  
  175.                 case inGoAway:
  176.                     if (TrackGoAway(theWindow, theEvent->where) && theWindow == gHelpWindow)
  177.                         HideHelpWindow();
  178.                 break;
  179.             } // END switch
  180.         break;
  181.  
  182.         case updateEvt:
  183.             BeginUpdate((WindowPtr)theEvent->message);
  184.             if ((WindowPtr)theEvent->message == gHelpWindow)
  185.                 UpdateHelpWindow();
  186.             EndUpdate((WindowPtr)theEvent->message);
  187.         break;
  188.  
  189.         case activateEvt:
  190.             if ((theEvent->modifiers & activeFlag) != 0) {    // Activate event
  191.                 SetCursor(&arrow);    // Don't get an I-beam from previous word-proc app!
  192.                 if ((WindowPtr)theEvent->message == gHelpWindow)
  193.                     HiliteControl(gScrollbar, 0);
  194.                 else if ((WindowPtr)theEvent->message == gMainDialog) {
  195.                     for (i = convertAndReplaceItem; i <= helpCheckbx; i++)
  196.                         HiliteControl((ControlHandle)GetDItemHdl(gMainDialog, i), 0);
  197.                 }
  198.             }
  199.             else {    // Deactivate event
  200.                 if ((WindowPtr)theEvent->message == gHelpWindow)
  201.                     HiliteControl(gScrollbar, 255);
  202.                 else if ((WindowPtr)theEvent->message == gMainDialog) {
  203.                     for (i = convertAndReplaceItem; i <= helpCheckbx; i++)
  204.                         HiliteControl((ControlHandle)GetDItemHdl(gMainDialog, i), 255);
  205.                 }                
  206.             }
  207.         break;
  208.         
  209.         case app4Evt:
  210.             if (theEvent->message & 1) {    // Resume event
  211.                 if (FrontWindow() == gMainDialog)
  212.                     for (i = convertAndReplaceItem; i <= helpCheckbx; i++)
  213.                         HiliteControl((ControlHandle)GetDItemHdl(gMainDialog, i), 0);
  214.                 else if (FrontWindow() == gHelpWindow)
  215.                     HiliteControl(gScrollbar, 0);                
  216.             }
  217.             else {    // Suspend event
  218.                 for (i = convertAndReplaceItem; i <= helpCheckbx; i++)
  219.                     HiliteControl((ControlHandle)GetDItemHdl(gMainDialog, i), 255);
  220.                 HiliteControl(gScrollbar, 255);                
  221.             }
  222.         break;
  223.  
  224.         case keyDown: case autoKey:
  225.             char theChar;
  226.             if (theEvent->modifiers & cmdKey) {
  227.                 theChar = theEvent->message & charCodeMask;
  228.                 switch(theChar) {
  229.                     case 'a': case 'A':
  230.                         if (FrontWindow() != gMainDialog)
  231.                             return;
  232.                         PushButton(gMainDialog, convertAndReplaceItem);
  233.                         ConvertAndReplace();
  234.                     break;
  235.  
  236.                     case 'b': case 'B':
  237.                         if (FrontWindow() != gMainDialog)
  238.                             return;
  239.                         PushButton(gMainDialog, convertOnlyItem);
  240.                         ConvertOnly();
  241.                     break;
  242.  
  243.                     case 'r': case 'R':
  244.                         if (FrontWindow() != gMainDialog)
  245.                             return;
  246.                         PushButton(gMainDialog, viewRgnItem);
  247.                         ViewRegion();
  248.                     break;
  249.  
  250.                     case 'c': case 'C':
  251.                         if (FrontWindow() != gMainDialog)
  252.                             return;
  253.                         PushButton(gMainDialog, copyToClipItem);
  254.                         HandleMenu(MenuKey(theChar));
  255.                     break;
  256.  
  257.                     case 'o': case 'O':
  258.                         if (FrontWindow() == gMainDialog)
  259.                             PushButton(gMainDialog, optionsItem);
  260.                         HandleMenu(MenuKey(theChar));
  261.                     break;
  262.  
  263.                     case 'i': case 'I':
  264.                         if (FrontWindow() != gMainDialog)
  265.                             return;
  266.                         PushButton(gMainDialog, rgnInfoItem);
  267.                         ShowRegionInfo();
  268.                     break;
  269.  
  270.                     case 'q': case 'Q':
  271.                         if (FrontWindow() == gMainDialog)
  272.                             PushButton(gMainDialog, quitItem);
  273.                         HandleMenu(MenuKey(theChar));
  274.                     break;
  275.  
  276.                     case 's': case 'S':
  277.                         if (FrontWindow() != gMainDialog)
  278.                             return;
  279.                         PushButton(gMainDialog, saveToFileItem);
  280.                         SaveRegionToFile();
  281.                     break;
  282.  
  283.                     case 'h': case 'H':
  284.                         if (FrontWindow() == gMainDialog)
  285.                             PushButton(gMainDialog, helpWindItem);
  286.                         HandleMenu(MenuKey(theChar));
  287.                     break;
  288.  
  289.                     case 'p': case 'P':
  290.                         if (FrontWindow() == gMainDialog)
  291.                             PushButton(gMainDialog, viewClipboardItem);
  292.                         HandleMenu(MenuKey(theChar));
  293.                     break;
  294.  
  295.                     default:
  296.                         HandleMenu(MenuKey(theChar));
  297.                     break;
  298.                 } // END switch
  299.             }
  300.         break;
  301.     } // END switch
  302. } // END HandleEvent
  303.  
  304. // ---------------------------------------------------------------------------
  305.  
  306. void HandleMenu(long menuResult) {
  307.     short menuID = HiWord(menuResult);
  308.     short menuItem = LoWord(menuResult);
  309.     HiliteMenu(0);
  310.     
  311.     switch(menuID) {
  312.         case kAppleMenuID:
  313.             if (menuItem == 1)
  314.                 DoRgnMasterAbout();
  315.         break;
  316.  
  317.         case kFileMenuID:
  318.             switch(menuItem) {
  319.                 case 1: // Options...
  320.                     DoOptionsDialog();
  321.                 break;
  322.                 case 3: // Help Window...
  323.                     ShowHelpWindow();
  324.                 break;
  325.                 case 5: // Quit
  326.                     gDone = true;
  327.                 break;
  328.             } // END switch
  329.         break;
  330.  
  331.         case kEditMenuID:
  332.             switch(menuItem) {
  333.                 case 4:
  334.                     CopyRegionToClipboard();
  335.                 break;
  336.     
  337.                 case 7:
  338.                     ShowClipboard();
  339.                 break;
  340.             } // END switch
  341.         break;
  342.     } // END switch
  343. } // END HandleMenu
  344.  
  345. // ---------------------------------------------------------------------------
  346.  
  347. void SetupDialog() {
  348.     Handle rgnTypeHdl;
  349.  
  350.     gAppFileRef = CurResFile();
  351.  
  352.     if (InitRegionMaker(&screenBits.bounds, 'Rgn ')) {
  353.         SysBeep(0);
  354.         ExitToShell();
  355.     }
  356.  
  357.     rgnTypeHdl = Get1NamedResource('OPTN', "\pOptions");
  358.     if (rgnTypeHdl == nil) {
  359.         rgnTypeHdl = NewHandleClear(sizeof(ResType));
  360.         **(ResType**)rgnTypeHdl = 'Rgn ';
  361.         AddResource(rgnTypeHdl, 'OPTN', Unique1ID('OPTN'), "\pOptions");
  362.         ChangedResource(rgnTypeHdl);
  363.         WriteResource(rgnTypeHdl);
  364.     }
  365.     SetRegionType(**(ResType**)rgnTypeHdl);
  366.     ReleaseResource(rgnTypeHdl);
  367.     
  368.     gAppleMenu = GetMenu(kAppleMenuID);
  369.     InsertMenu(gAppleMenu, 0);
  370.     gFileMenu = GetMenu(kFileMenuID);
  371.     InsertMenu(gFileMenu, 0);
  372.     gEditMenu = GetMenu(kEditMenuID);
  373.     InsertMenu(gEditMenu, 0);
  374.  
  375.     gMainDialog = GetNewDialog(kMainDialogID, nil, (WindowPtr)-1);
  376.     SetPort(gMainDialog);
  377.     TextFont(geneva);
  378.     TextSize(9);
  379.  
  380.     SetUserProc(gMainDialog, frameItem1, (ProcPtr)FrameItemRect);
  381.     SetUserProc(gMainDialog, frameItem2, (ProcPtr)FrameGrayRect);
  382.     SetUserProc(gMainDialog, frameItem3, (ProcPtr)FrameGrayRect);
  383.     SetUserProc(gMainDialog, frameItem4, (ProcPtr)FrameGrayRect);
  384.     SetUserProc(gMainDialog, helpItem, (ProcPtr)DrawHelpStatus);
  385.  
  386.     SetCtlValue((ControlHandle)GetDItemHdl(gMainDialog, helpCheckbx), gShowHelp);
  387.     SetCtlValue((ControlHandle)GetDItemHdl(gMainDialog, previewCheckbx), gPreviewAlways);
  388.  
  389.     InitHelpWindow();
  390.  
  391.     ShowWindow(gMainDialog);
  392.     DrawMenuBar();
  393. } // END SetupDialog
  394.  
  395.  
  396. void InitMac() {
  397.     InitGraf(&thePort);
  398.     InitFonts();
  399.     FlushEvents(everyEvent,0);
  400.     InitWindows();
  401.     InitMenus();
  402.     TEInit();
  403.     InitDialogs(0L);
  404.     InitCursor();
  405. } // END InitMac
  406.  
  407. // ---------------------------------------------------------------------------
  408.  
  409.  
  410.  
  411. // END RgnMaster.c++