home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1996 January / macformat-033.iso / mac / Shareware City / Graphics / GifScan 1.6 / Sources / Lib Sources / DropShell.c next >
Encoding:
C/C++ Source or Header  |  1995-10-09  |  12.7 KB  |  647 lines  |  [TEXT/MMCC]

  1. /******************************************************************************
  2. **
  3. **  Project Name:    DropShell
  4. **     File Name:    DropShell.c
  5. **
  6. **   Description:    Main application code for the QuickShell
  7. **
  8. *******************************************************************************
  9. **                       A U T H O R   I D E N T I T Y
  10. *******************************************************************************
  11. **
  12. **    Initials    Name
  13. **    --------    -----------------------------------------------
  14. **    LDR            Leonard Rosenthol
  15. **    MTC            Marshall Clow
  16. **    SCS            Stephan Somogyi
  17. **
  18. ******************************************************************************/
  19. #include "GifScan.h"
  20.  
  21. #include "DSGlobals.h"
  22. #include "DSUserProcs.h"
  23. #include "DSAppleEvents.h"
  24.  
  25. #include "DropShell.h"
  26.  
  27. #include "Prefs.h"
  28. #include "GSDrag.h"
  29.  
  30. Boolean        gDone, gOApped, gHasAppleEvents, gCmdHeldDown, gInBackground, gHasDrag;
  31. extern thePrefsHandle    gPrefs;
  32.  
  33. void InitToolbox (void) 
  34. {
  35.     MoreMasters ();
  36.     MoreMasters ();
  37.     MoreMasters ();
  38.     MoreMasters ();
  39.  
  40.     InitGraf ( &qd.thePort );
  41.     InitFonts ();
  42.     InitWindows ();
  43.     InitMenus ();
  44.     TEInit ();
  45.     InitDialogs (nil);        // use of ResumeProcs no longer approved by Apple
  46.     InitCursor ();
  47.     FlushEvents ( everyEvent, 0 );    
  48. }
  49.  
  50. //     Let's setup those global variables that the DropShell uses.
  51. //     If you add any globals for your own use,
  52. //     init them in the InitUserGlobals routine in DSUserProcs.c
  53.  
  54. Boolean InitGlobals(void) 
  55. {
  56.     long aLong;
  57.  
  58.     gDone            = false;
  59.     gOApped            = false;    // probably not since users are supposed to DROP things!
  60.     gHasAppleEvents    = Gestalt ( gestaltAppleEventsAttr, &aLong ) == noErr;
  61.     gHasDrag        = IsDragManagerInstalled();
  62.  
  63.     return(InitUserGlobals());    // call the user proc
  64. }
  65.  
  66. // Again, nothing fancy.  Just setting up the menus.
  67. //    If you add any menus to your DropBox - insert them here!
  68.  
  69.  
  70. void SetUpMenus (void) 
  71. {
  72.     MenuHandle    menu;
  73.     Handle        menuBar;
  74.     
  75.     menuBar = GetNewMBar(128);
  76.     if ( menuBar == nil ) ErrorAlert(kErrStringID, eMemError, 12900, true);
  77.     
  78.     SetMenuBar(menuBar);
  79.     DisposeHandle(menuBar);
  80.     
  81.     menu = GetMenuHandle(kAppleNum);
  82.     AddResMenu(menu, 'DRVR');
  83.     
  84.     DrawMenuBar ();
  85. }
  86.  
  87.  
  88. //    --------------- Standard Event Handling routines ---------------------- 
  89.  
  90. void ShowAbout ( void ) 
  91. {
  92.     (void) Alert ( 128, nil );
  93. }
  94.  
  95. void DoMenu ( long retVal ) 
  96. {
  97.     short            menuID, itemID;
  98.     Str255            itemStr;
  99.     OSErr            saveErr;
  100.     TEHandle        te;
  101.     Handle            aHandle;
  102.     long            oldSize, newSize;
  103.     long            total, contig;
  104.     
  105.     menuID = HiWord ( retVal );
  106.     itemID = LoWord ( retVal );
  107.     
  108.     switch ( menuID ) 
  109.     {
  110.         case kAppleNum:
  111.             if ( itemID == 1 )
  112.                 ShowAbout ();    //    Show the about box 
  113.             else
  114.             {
  115.                 GetMenuItemText(GetMenuHandle(kAppleNum), itemID, itemStr);
  116.                 OpenDeskAcc(itemStr);
  117.             }
  118.             break;
  119.             
  120.         case kFileNum:
  121.             switch (itemID)
  122.             {
  123.                 case iSelectFile:
  124.                     SelectFile();    // call file selection userProc
  125.                     break;
  126.                 
  127.                 case iShowHide:        // Show and Hide window
  128.                     if ( gOutWindow != nil )
  129.                     {
  130.                           if ( FrontWindow() != nil ) 
  131.                               HideWindow(gOutWindow);
  132.                           else 
  133.                               ShowWindow(gOutWindow);
  134.                           SetCursor(&qd.arrow);
  135.                     }
  136.                     break;
  137.                 
  138.                 case iSaveAs:        // Save file
  139.                     HandleTextFileSave();
  140.                     break;
  141.                     
  142.                 case iPreferences:        // Preferences 
  143.                     SetPreferences();
  144.                     break;
  145.                 
  146.                 case iQuit:
  147.                     SendQuitToSelf();    // send self a 'quit' event
  148.                     break;
  149.                 
  150.                 default:
  151.                     break;
  152.             }
  153.             break;
  154.         
  155.         case kEditNum:
  156.             if ( !SystemEdit(menuID-1) ) 
  157.             {
  158.                 te = ((DocumentPeek)FrontWindow())->docTE;
  159.                 switch ( itemID ) 
  160.                 {
  161.                     case iCut:        // Cut
  162.                         if ( ZeroScrap() == noErr ) 
  163.                         {
  164.                             PurgeSpace(&total, &contig);
  165.                             if ((*te)->selEnd - (*te)->selStart + kTESlop > contig)
  166.                                 SysBeep(20);
  167.                             else 
  168.                             {
  169.                                 TECut(te);
  170.                                 if ( TEToScrap() != noErr ) 
  171.                                 {
  172.                                     SysBeep(20);
  173.                                     ZeroScrap();
  174.                                 }
  175.                             }
  176.                         }
  177.                         break;
  178.                     case iCopy:        // Copy
  179.                         if ( ZeroScrap() == noErr ) 
  180.                         {
  181.                             TECopy(te);
  182.                             if ( TEToScrap() != noErr ) 
  183.                             {
  184.                                 SysBeep(20);
  185.                                 ZeroScrap();
  186.                             }
  187.                         }
  188.                         break;
  189.                         
  190.                     case iPaste:        // Paste
  191.                         if ( TEFromScrap() == noErr ) 
  192.                         {
  193.                             if ( TEGetScrapLen() + ((*te)->teLength -
  194.                                 ((*te)->selEnd - (*te)->selStart)) > kMaxTELength )
  195.                                 SysBeep(20);
  196.                             else 
  197.                             {
  198.                                 aHandle = (Handle) TEGetText(te);
  199.                                 oldSize = GetHandleSize(aHandle);
  200.                                 newSize = oldSize + TEGetScrapLen() + kTESlop;
  201.                                 SetHandleSize(aHandle, newSize);
  202.                                 saveErr = MemError();
  203.                                 SetHandleSize(aHandle, oldSize);
  204.                                 if (saveErr != noErr)
  205.                                     SysBeep(20);
  206.                                 else
  207.                                     TEPaste(te);
  208.                             }
  209.                         }
  210.                         else
  211.                             SysBeep(20);
  212.                         break;
  213.                         
  214.                     case iClear:        // Clear
  215.                         TEDelete(te);
  216.                         break;
  217.                     
  218.                     case iSelectAll:        // Select All
  219.                         TESetSelect(0, kMaxTELength, te);
  220.                         break;
  221.                 }
  222.             AdjustScrollbars(FrontWindow(), false);
  223.             AdjustTE(FrontWindow());
  224.             }
  225.             break;
  226.  
  227.         default:
  228.             break;
  229.             
  230.         }
  231.     HiliteMenu(0);        // turn it off!
  232. }
  233.  
  234. void DoMouseDown ( EventRecord *curEvent ) 
  235. {    
  236.     WindowRef        whichWindow;
  237.     short            whichPart;
  238.     Point            mouse;
  239.     DocumentPeek    doc;
  240.  
  241.     whichPart = FindWindow ( curEvent->where, &whichWindow );
  242.     switch ( whichPart ) 
  243.     {
  244.         case inMenuBar:
  245.             DoMenu( MenuSelect ( curEvent->where ));
  246.             break;
  247.         
  248.         case inSysWindow:
  249.             SystemClick( curEvent, whichWindow );
  250.             break;
  251.         
  252.         case inContent:
  253.             if ( whichWindow != FrontWindow() ) 
  254.             {
  255.                 SelectWindow(whichWindow);
  256.             }
  257.             else
  258.             {
  259.                 if ( whichWindow == gOutWindow )
  260.                 {
  261.                     doc = (DocumentPeek)whichWindow;
  262.                     mouse = curEvent->where;
  263.                     GlobalToLocal(&mouse);
  264.                     
  265.                     // Stuff to handle drags
  266.                     if ( gHasDrag ) 
  267.                     {
  268.                         TEGetHiliteRgn(doc->docHiliteRgn, doc->docTE);
  269.                         if ( PtInRgn(mouse, doc->docHiliteRgn) )
  270.                         {
  271.                             HandleDragSelection(curEvent, doc->docHiliteRgn);
  272.                         }
  273.                         else
  274.                         {
  275.                             DoContentClick(gOutWindow, curEvent);
  276.                         }
  277.                     }
  278.                     else
  279.                     {
  280.                         DoContentClick(gOutWindow, curEvent);
  281.                     }
  282.                 }
  283.             }
  284.             break;
  285.                 
  286.         case inDrag:
  287.             {
  288.                 Rect    boundsRect = (*GetGrayRgn())->rgnBBox;
  289.                 
  290.                 DragWindow ( whichWindow, curEvent->where, &boundsRect );
  291.                 AdjustDragRgn();
  292.             }
  293.             break;
  294.         
  295.         case inGoAway:
  296.             if ( whichWindow == gOutWindow )
  297.             {
  298.                 if ( TrackGoAway(gOutWindow, curEvent->where) )
  299.                 {
  300.                     HideWindow(gOutWindow);
  301.                     AdjustDragRgn();
  302.                 }
  303.             }
  304.             break;
  305.         
  306.         case inZoomIn:
  307.         case inZoomOut:
  308.             if ( whichWindow == gOutWindow )
  309.             {
  310.                 if( TrackBox(gOutWindow, curEvent->where, whichPart) )
  311.                 {
  312.                     DoZoomWindow(gOutWindow, whichPart);
  313.                     AdjustDragRgn();
  314.                 }
  315.             }
  316.             break;
  317.         
  318.         case inGrow:
  319.             if ( whichWindow == gOutWindow )
  320.             {
  321.                 DoGrowWindow(gOutWindow, curEvent);
  322.                 AdjustDragRgn();
  323.             }
  324.             break;
  325.             
  326.         default:
  327.             break;
  328.         }
  329.     }
  330.  
  331.  
  332.  
  333. void DoKeyDown(EventRecord *event)
  334. {
  335.     WindowPtr    window;
  336.     char        key;
  337.     TEHandle    te;
  338.  
  339.     window = FrontWindow();
  340.     if ( IsAppWindow(window) ) 
  341.     {
  342.         te = ((DocumentPeek)window)->docTE;
  343.         key = event->message & charCodeMask;
  344.  
  345.         if ( key == kDelChar || 
  346.             (*te)->teLength - ((*te)->selEnd - (*te)->selStart) + 1 < kMaxTELength ) 
  347.         {
  348.             TEKey(key, te);
  349.             AdjustScrollbars(window, false);
  350.             AdjustTE(window);
  351.         } 
  352.         else
  353.             SysBeep(20); 
  354.     }
  355. }
  356.  
  357. void DoUpdate(WindowRef    window)
  358. {
  359.     GrafPtr            savePort;
  360.     RGBColor        theHiliteColor, theUsedColor;
  361.     DocumentPeek    doc;
  362.     PenState        thePnState;
  363.     
  364.     if ( IsAppWindow(window) ) 
  365.     {
  366.         BeginUpdate(window);
  367.         if ( ! EmptyRgn(window->visRgn) )
  368.         {
  369.             DrawWindow(window);
  370.             AdjustDragRgn();
  371.         }
  372.         EndUpdate(window);
  373.     }
  374. }
  375.  
  376. void DoActivate(WindowRef window, Boolean becomingActive)
  377. {
  378.     RgnHandle        tempRgn, clipRgn;
  379.     Rect            growRect;
  380.     DocumentPeek    doc;
  381.     
  382.     if ( IsAppWindow(window) ) 
  383.     {
  384.         doc = (DocumentPeek) window;
  385.         if ( becomingActive ) 
  386.         {
  387.             tempRgn = NewRgn();
  388.             clipRgn = NewRgn();
  389.             GetLocalUpdateRgn(window, tempRgn);
  390.             GetClip(clipRgn);
  391.             DiffRgn(clipRgn, tempRgn, tempRgn);
  392.             SetClip(tempRgn);
  393.             TEActivate(doc->docTE);
  394.             SetClip(clipRgn);
  395.             DisposeRgn(tempRgn);
  396.             DisposeRgn(clipRgn);
  397.             
  398.             (*doc->docVScroll)->contrlVis = kControlVisible;
  399.             (*doc->docHScroll)->contrlVis = kControlVisible;
  400.             InvalRect(&(*doc->docVScroll)->contrlRect);
  401.             InvalRect(&(*doc->docHScroll)->contrlRect);
  402.  
  403.             growRect = window->portRect;
  404.  
  405.             growRect.top = growRect.bottom - kScrollbarAdjust;
  406.             growRect.left = growRect.right - kScrollbarAdjust;
  407.             InvalRect(&growRect);
  408.             
  409.             if ( gHasDrag ) TEGetHiliteRgn(doc->docHiliteRgn, doc->docTE);
  410.         }
  411.         else
  412.         {        
  413.             TEDeactivate(doc->docTE);
  414.  
  415.             HideControl(doc->docVScroll);
  416.             HideControl(doc->docHScroll);
  417.  
  418.             DrawGrowIcon(window);
  419.         }
  420.         AdjustDragRgn();
  421.     }
  422. }
  423.  
  424. static void AdjustCursor(void)
  425. {
  426.     WindowPtr    window;
  427.     RgnHandle    iBeamRgn, dragArrowRgn;
  428.     Rect        iBeamRect;
  429.     Point        mouse;
  430.  
  431.     window = FrontWindow();
  432.     if ( (!gInBackground) && ( !IsDAWindow(window) ) ) 
  433.     {
  434.         if ( IsAppWindow(window) ) 
  435.         {
  436.             SetPort(window);
  437.             
  438.             GetMouse(&mouse);
  439.  
  440.             iBeamRgn = NewRgn();
  441.             iBeamRect = (*((DocumentPeek)window)->docTE)->viewRect;
  442.             RectRgn(iBeamRgn, &iBeamRect);
  443.             
  444.             if ( gHasDrag )
  445.             {
  446.                 // TEGetHiliteRgn get the region handle in LOCAL points 
  447.                 dragArrowRgn = NewRgn();
  448.                 TEGetHiliteRgn(dragArrowRgn, ((DocumentPeek)window)->docTE);
  449.                 
  450.                 if ( PtInRgn(mouse, dragArrowRgn) )
  451.                 {
  452.                     SetCursor(&qd.arrow);
  453.                 }
  454.                 else if ( PtInRgn(mouse, iBeamRgn) ) 
  455.                 {
  456.                     SetCursor(*GetCursor(iBeamCursor));
  457.                 }
  458.                 else
  459.                 {
  460.                     SetCursor(&qd.arrow);
  461.                 }
  462.                 DisposeRgn(dragArrowRgn);
  463.             }
  464.             else
  465.             {
  466.                 if ( PtInRgn(mouse, iBeamRgn) ) 
  467.                 {
  468.                     SetCursor(*GetCursor(iBeamCursor));
  469.                 }
  470.                 else
  471.                 {
  472.                     SetCursor(&qd.arrow);
  473.                 }
  474.             }
  475.             DisposeRgn(iBeamRgn);
  476.         }
  477.     }
  478. }
  479.  
  480. static void DoIdle(void)
  481. {
  482.     WindowPtr    window;
  483.  
  484.     window = FrontWindow();
  485.     if ( IsAppWindow(window) )
  486.     {
  487.         TEIdle(((DocumentPeek) window)->docTE);        // Blink the caret
  488.         AdjustCursor();    // Always
  489.     }
  490. }
  491.  
  492. static void AdjustMenus(void)
  493. {
  494.     StringHandle        itemString;
  495.     TEHandle            teh;
  496.     static MenuRef        fileMenu, editMenu;
  497.     
  498.     fileMenu = GetMenuHandle(kFileNum);
  499.     editMenu = GetMenuHandle(kEditNum);
  500.     if ( FrontWindow() != nil )
  501.     {
  502.         itemString = GetString(130);
  503.         SetMenuItemText(fileMenu, 2, itemString[0]);
  504.         SetItemCmd(fileMenu, 2, 'W');
  505.         
  506.         teh = ((DocumentPeek)gOutWindow)->docTE;
  507.         if ( ((*teh)->selEnd - (*teh)->selStart) == 0 )
  508.         {
  509.             DisableItem(editMenu, iCut);
  510.             DisableItem(editMenu, iCopy);
  511.             DisableItem(editMenu, iClear);
  512.         }
  513.         else
  514.         {
  515.             EnableItem(editMenu, iCut);
  516.             EnableItem(editMenu, iCopy);
  517.             EnableItem(editMenu, iClear);
  518.         }
  519.         
  520.         if ( TEFromScrap() )
  521.         {
  522.             if ( !TEScrapHandle() )
  523.                 DisableItem(editMenu, iPaste);
  524.             else
  525.                 EnableItem(editMenu, iPaste);
  526.         }
  527.     }
  528.     else
  529.     {
  530.         itemString = GetString(129);
  531.         SetMenuItemText(fileMenu, 2, itemString[0]);
  532.         SetItemCmd(fileMenu, 2, 'K');
  533.     }
  534. }
  535.  
  536. void AdjustDragRgn(void)
  537. {
  538.     DragInfoHandle    dragInfo;
  539.     Rect            receiveRect;
  540.     
  541.     if ( gHasDrag )
  542.     {
  543.         dragInfo = (DragInfoHandle)GetWRefCon(gOutWindow);
  544.         receiveRect = gOutWindow->portRect;
  545.         receiveRect.bottom -= kScrollbarAdjust;
  546.         receiveRect.right -= kScrollbarAdjust;
  547.         RectRgn((*dragInfo)->receiveRgn, &receiveRect);
  548.     }
  549. }
  550.  
  551. void main ( void ) 
  552. {
  553.     char                key;
  554.     EventRecord            event;
  555.     
  556.     InitToolbox ();
  557.     
  558.     gInBackground = false;
  559.     
  560.     if ( InitGlobals () ) 
  561.     {    
  562.         // if we succeeding in initting self
  563.         if ( !gHasAppleEvents )
  564.         {
  565.             ErrorAlert ( kErrStringID, kCantRunErr, 0, true );
  566.         }
  567.         else
  568.         {
  569.             InitAEVTStuff ();
  570.             SetUpMenus ();
  571.             
  572.             while ( !gDone ) 
  573.             {
  574.                 if ( WaitNextEvent (everyEvent, &event, 0, nil) )
  575.                 {
  576.                     switch ( event.what ) 
  577.                     {
  578.                         case kHighLevelEvent:
  579.                             DoHighLevelEvent ( &event );
  580.                             break;
  581.                             
  582.                         case mouseDown:
  583.                             DoMouseDown ( &event );
  584.                             break;
  585.                             
  586.                         case keyDown:
  587.                         case autoKey:
  588.                             key = event.message & charCodeMask;
  589.                             if ( event.modifiers & cmdKey ) 
  590.                             {
  591.                                 if ( event.what == keyDown ) 
  592.                                 {
  593.                                     DoMenu(MenuKey(key));
  594.                                 }
  595.                             } 
  596.                             else
  597.                                 DoKeyDown(&event);
  598.                             break;
  599.                         
  600.                         case kOSEvent:
  601.                             {
  602.                                 switch ((event.message >> 24) & 0x0FF)
  603.                                 {
  604.                                     case kMouseMovedMessage:
  605.                                         DoIdle();
  606.                                         break;
  607.                                         
  608.                                     case kSuspendResumeMessage:    
  609.                                         gInBackground = (event.message & kResumeMask) == 0;
  610.                                         DoActivate(FrontWindow(), !gInBackground);
  611.                                         break;
  612.                                 }
  613.                             }
  614.                             break;
  615.                             
  616.                         case diskEvt:
  617.                             if (HiWord(event.message)) 
  618.                             {
  619.                                 Point diskInitPt;
  620.                                 
  621.                                 diskInitPt.v = diskInitPt.h = 100;
  622.                                 DILoad();
  623.                                 DIBadMount(diskInitPt, event.message);
  624.                                 DIUnload();
  625.                             }
  626.                             break;
  627.                         
  628.                         case updateEvt:
  629.                             DoUpdate((WindowRef)(event.message));
  630.                             break;
  631.                         
  632.                         case activateEvt:
  633.                             DoActivate((WindowRef)(event.message), (event.modifiers & activeFlag) != 0);
  634.                             break;
  635.                     }
  636.                     AdjustCursor();
  637.                     AdjustMenus();
  638.                 }
  639.                 else
  640.                 {
  641.                     DoIdle();
  642.                 }
  643.             }
  644.         }
  645.     }
  646.     DisposeUserGlobals();    // call the userproc to clean itself up
  647. }