home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Source Code / C / System / SmartDragWindow 1.0.1 / SmartDragWindow Demo / main.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-12-02  |  4.5 KB  |  178 lines  |  [TEXT/CWIE]

  1. /*
  2.     main.c
  3.     
  4.     A very simple app to show you how SmartDragWindow() works.
  5.     
  6.     Written by Hiep Dam
  7.     From The Witches' Brew
  8.     Dec 2 95
  9.     
  10.     Public domain give-away-ware.
  11.     I've learned a lot from all the source code people made available
  12.     out there. This is my thanks back to everyone.
  13.     
  14.     Version History
  15.         Nov 6 95: Created.
  16.         Dec 2 95: Added MySnapProc.
  17. */
  18.  
  19. #include "InitToolbox.h"
  20. #include "SmartDragWindow.h"
  21.  
  22. void EventLoop();
  23. void MySnapProc(WindowPtr windowToDrag, short snapToDistance, Rect *snapRect);
  24. void DrawGrowIconNoLines(WindowPtr wp, Boolean hideLines);
  25.  
  26. // ---------------------------------------------------------------------------
  27.  
  28. void main() {
  29.     WindowPtr theWindow;
  30.     Rect windowBounds;
  31.     short i;
  32.  
  33.     InitToolbox();
  34.  
  35.     windowBounds = (**GetMainDevice()).gdRect;
  36.     windowBounds.right = windowBounds.left + 350;
  37.     windowBounds.bottom = windowBounds.top + 120;
  38.     
  39.     for (i = 0; i < 3; i++) {
  40.         OffsetRect(&windowBounds, 20, 50);
  41.         theWindow = NewWindow(NULL, &windowBounds,
  42.             "\pSmartDragWindow Demo", true,
  43.             documentProc, (WindowPtr)-1, true, 0);
  44.         SetPort(theWindow);
  45.         TextFont(geneva);
  46.         TextSize(9);
  47.     }
  48.     
  49.     EventLoop();
  50. } // END main
  51.  
  52. // ---------------------------------------------------------------------------
  53.  
  54. void EventLoop() {
  55.     EventRecord theEvent;
  56.     short part;
  57.     WindowPtr theWindow;
  58.     Boolean done = false;
  59.     
  60.     while (!done)
  61.         if (WaitNextEvent(everyEvent, &theEvent, 50, NULL)) {
  62.             switch(theEvent.what) {
  63.                 case mouseDown:
  64.                     part = FindWindow(theEvent.where, &theWindow);
  65.  
  66.                     switch(part) {
  67.                         case inDrag: {
  68.                             // Aah. Our routine!
  69.                             // Give it a large snap-to-distance
  70.                             // to make it obvious...
  71.                             SuperSmartDragWindow(theWindow, theEvent.where, NULL, 25, MySnapProc);
  72.                             
  73.                             // Use the below for default dragging (snap to monitors only)
  74.                             //SmartDragWindow(theWindow, 20, NULL);
  75.                         } break;
  76.  
  77.                         case inGrow: {
  78.                             Rect sizeRect = { 40, 40, 400, 400 };
  79.                             long lSizeVH;
  80.                             
  81.                             lSizeVH = GrowWindow(theWindow, theEvent.where, &sizeRect);
  82.                             SizeWindow(theWindow,LoWord(lSizeVH),HiWord(lSizeVH),false);
  83.                             InvalRect(&theWindow->portRect);
  84.                         } break;
  85.                         
  86.                         case inGoAway:
  87.                             if (TrackGoAway(theWindow, theEvent.where)) {
  88.                                 DisposeWindow(theWindow);
  89.                                 if (FrontWindow() == NULL)
  90.                                     done = true;
  91.                             }
  92.                         break;
  93.  
  94.                         case inContent:
  95.                             if (FrontWindow() != theWindow)
  96.                                 SelectWindow(theWindow);
  97.                             else
  98.                                 SysBeep(10);
  99.                         break;
  100.                     }
  101.                 break; // mouseDown
  102.                 
  103.                 case keyDown:
  104.                     done = true;
  105.                 break;
  106.  
  107.                 case updateEvt: {
  108.                     WindowPtr theWindow;
  109.                     GrafPtr savePort;
  110.                     
  111.                     theWindow = (WindowPtr)theEvent.message;
  112.                     GetPort(&savePort);
  113.                     SetPort(theWindow);
  114.  
  115.                     BeginUpdate(theWindow);
  116.                     EraseRect(&theWindow->portRect);
  117.                     DrawGrowIconNoLines(theWindow, true);
  118.                     TextFace(bold);
  119.                     MoveTo(10, 25);
  120.                     DrawString("\pSmartDragWindow by Hiep Dam.");
  121.                     TextFace(0);
  122.                     DrawString("\p   From The Witches' Brew 1995");
  123.                     MoveTo(20, 50);
  124.                     DrawString("\pWatch the windows snap to the edge of the monitor and to");
  125.                     MoveTo(20, 65);
  126.                     DrawString("\pother windows as you drag.");
  127.                     MoveTo(20, 80);
  128.                     DrawString("\pTry dragging non-frontmost windows as well (using the cmd-key).");
  129.                     EndUpdate(theWindow);
  130.                 } break;
  131.                 
  132.                 case activateEvt:
  133.                 break;
  134.             }
  135.         }
  136. } // END EventLoop
  137.  
  138. // ---------------------------------------------------------------------------
  139.  
  140. void MySnapProc(WindowPtr windowToDrag, short snapToDistance, Rect *snapRect) {
  141.     WindowSnapProc(windowToDrag, snapToDistance, snapRect);
  142.     MonitorSnapProc(windowToDrag, snapToDistance, snapRect);
  143. } // END MySnapProc
  144.  
  145. // ---------------------------------------------------------------------------
  146.  
  147. #define kGrowBoxSize 16
  148.  
  149. void DrawGrowIconNoLines(WindowPtr wp, Boolean hideLines) {
  150.     RgnHandle  saveClip;
  151.     GrafPtr    savePort;
  152.     Rect       growBox;
  153.     
  154.     if( hideLines == false ){
  155.         DrawGrowIcon( wp );
  156.     }else{
  157.         saveClip = NewRgn();
  158.         if( saveClip == NULL )
  159.             return;
  160.  
  161.         GetPort( &savePort );
  162.         SetPort( wp );
  163.     
  164.         GetClip( saveClip );
  165.        
  166.         growBox.right  = wp->portRect.right;
  167.         growBox.bottom = wp->portRect.bottom;
  168.         growBox.left   = growBox.right - kGrowBoxSize;
  169.         growBox.top    = growBox.bottom - kGrowBoxSize;
  170.  
  171.         ClipRect( &growBox );
  172.         DrawGrowIcon( wp );
  173.  
  174.         SetClip( saveClip );
  175.         SetPort( savePort );
  176.         DisposeRgn( saveClip );
  177.     }
  178. } // DragGrowIconNoLines