home *** CD-ROM | disk | FTP | other *** search
/ MacHack 1999 / MacHack 1999.toast / The Hacks / MACtive Desktop / Source / Sources / BaseWindow.cc < prev    next >
Encoding:
C/C++ Source or Header  |  1998-01-15  |  8.5 KB  |  614 lines  |  [TEXT/CWIE]

  1. #include <Dialogs.h>
  2. #include "LayerManager.h"
  3. #include "QDContext.h"
  4. #include "WindowManager.h"
  5. #include "BaseWindow.h"
  6.  
  7.  
  8.  
  9.  
  10.  
  11. extern SystemWindowEventHandlerUPP            gSystemWindowEventHandlerProc;
  12. extern WindowManager                        *gWindowManager;
  13.  
  14.  
  15.  
  16.  
  17.  
  18. // Do nothing constructor varient
  19. BaseWindow::BaseWindow(void)
  20. {
  21.     
  22. }
  23.  
  24.  
  25.  
  26.  
  27.  
  28. // Resource based window constructor varient
  29. BaseWindow::BaseWindow(UInt32 windowID)
  30. {
  31.     fWindow = ::GetNewCWindow(windowID,NULL,(WindowPtr)-1L);
  32.     if (fWindow != NULL)
  33.     {
  34.         fFlags = 0L;
  35.         gWindowManager->DoAddWindow(this);
  36.     }
  37. }
  38.  
  39.  
  40.  
  41.  
  42.  
  43. // Runtime based window constructor varient
  44. BaseWindow::BaseWindow(Boolean isFloatingWindow,short procID,Boolean goAwayFlag)
  45. {
  46.     Rect    bounds;
  47.     
  48.     
  49.     SetRect(&bounds,32,48,160,144);
  50.     if (isFloatingWindow)
  51.     {
  52.         fWindow = NewSystemWindow(    &bounds,
  53.                                     "\p",
  54.                                     false,
  55.                                     procID,
  56.                                     (WindowPtr)(-1L),
  57.                                     goAwayFlag,
  58.                                     0L,
  59.                                     (SystemWindowEventHandlerProcPtr)gSystemWindowEventHandlerProc,
  60.                                     NULL);
  61.         
  62.         if (fWindow != NULL)
  63.         {
  64.             fFlags = kFloater;
  65.             gWindowManager->DoAddWindow(this);
  66.         }
  67.     }
  68.     else
  69.     {
  70.         fWindow = NewCWindow(    NULL,
  71.                                 &bounds,
  72.                                 "\p",
  73.                                 false,
  74.                                 procID,
  75.                                 (WindowPtr)(-1L),
  76.                                 goAwayFlag,
  77.                                 0L);
  78.         
  79.         if (fWindow != NULL)
  80.         {
  81.             fFlags = 0L;
  82.             gWindowManager->DoAddWindow(this);
  83.         }
  84.     }
  85. }
  86.  
  87.  
  88.  
  89.  
  90.  
  91. BaseWindow::~BaseWindow(void)
  92. {
  93.     if (fWindow != NULL)
  94.     {
  95.         gWindowManager->DoDeleteWindow(this);
  96.         if (fFlags & kFloater)
  97.             DisposeSystemWindow(fWindow);
  98.         else
  99.             DisposeWindow(fWindow);
  100.         fWindow = NULL;
  101.     }
  102. }
  103.  
  104.  
  105.  
  106.  
  107.  
  108. Boolean BaseWindow::DoGetParam(OSType param,SInt32 *value)
  109. {
  110.     return HandleGetParam(param,value);
  111. }
  112.  
  113.  
  114.  
  115.  
  116.  
  117. Boolean BaseWindow::DoSetParam(OSType param,SInt32 value)
  118. {
  119.     QDContext    context(fWindow);
  120.     
  121.     
  122.     return HandleSetParam(param,value);
  123. }
  124.  
  125.  
  126.  
  127.  
  128.  
  129. void BaseWindow::DoDialogEvent(EventRecord *event)
  130. {
  131.     DialogPtr    dialog;
  132.     short        item;
  133.     
  134.     
  135.     // Were really not a dialog...but
  136.     // lets do the right thing anyway
  137.     DialogSelect(event,&dialog,&item);
  138. }
  139.  
  140.  
  141.  
  142.  
  143.  
  144. void BaseWindow::DoClose(void)
  145. {
  146.     QDContext    context(fWindow);
  147.     
  148.     
  149.     HandleClose();
  150. }
  151.  
  152.  
  153.  
  154.  
  155.  
  156. void BaseWindow::DoKey(UInt32 key,UInt32 modifiers)
  157. {
  158.     QDContext    context(fWindow);
  159.     
  160.     
  161.     HandleKey(key,modifiers);
  162. }
  163.  
  164.  
  165.  
  166.  
  167.  
  168. void BaseWindow::DoClick(Point where,UInt32 modifiers,SInt32 part)
  169. {
  170.     QDContext    context(fWindow);
  171.     
  172.     
  173.     switch(part)
  174.     {
  175.         case inContent:
  176.             if (!(fFlags & (kActive | kFloater)))
  177.                 SelectWindow(fWindow);
  178.             else
  179.             {
  180.                 GlobalToLocal(&where);
  181.                 HandleClick(where,modifiers);
  182.             }
  183.             break;
  184.         
  185.         case inDrag:
  186.             HandleDrag(where);
  187.             break;
  188.         
  189.         case inGrow:
  190.             HandleGrow(where);
  191.             break;
  192.         
  193.         case inGoAway:
  194.             if (TrackGoAway(fWindow,where))
  195.                 HandleClose();
  196.             break;
  197.         
  198.         case inZoomIn:
  199.             if (TrackBox(fWindow,where,part))
  200.                 HandleZoomIn();
  201.             break;
  202.         
  203.         case inZoomOut:
  204.             if (TrackBox(fWindow,where,part))
  205.                 HandleZoomOut();
  206.             break;
  207.     }
  208. }
  209.  
  210.  
  211.  
  212.  
  213.  
  214. void BaseWindow::DoUpdate(void)
  215. {
  216.     QDContext    context(fWindow);
  217.     
  218.     
  219.     BeginUpdate((GrafPtr)fWindow);
  220.     HandleDraw();
  221.     EndUpdate((GrafPtr)fWindow);
  222. }
  223.  
  224.  
  225.  
  226.  
  227.  
  228. void BaseWindow::DoUpdateCursor(Point mouse,UInt32 modifiers)
  229. {
  230.     QDContext    context(fWindow);
  231.     
  232.     
  233.     GlobalToLocal(&mouse);
  234.     HandleCursorUpdate(mouse,modifiers);
  235. }
  236.  
  237.  
  238.  
  239.  
  240.  
  241. void BaseWindow::DoIdleTime(EventRecord *event,Point mouse,UInt32 modifiers)
  242. {
  243.     QDContext    context(fWindow);
  244.     
  245.     
  246.     GlobalToLocal(&mouse);
  247.     HandleIdleTime(mouse,modifiers);
  248.     
  249.     if ((fFlags & kDialog) && IsDialogEvent(event))
  250.         DoDialogEvent(event);
  251. }
  252.  
  253.  
  254.  
  255.  
  256.  
  257. void BaseWindow::DoSetActivationState(Boolean isActive)
  258. {
  259.     QDContext    context(fWindow);
  260.     
  261.     
  262.     if (isActive)
  263.     {
  264.         fFlags |= kActive;
  265.         HandleActivate();
  266.     }
  267.     else
  268.     {
  269.         fFlags &= ~kActive;
  270.         HandleDeactivate();
  271.     }
  272. }
  273.  
  274.  
  275.  
  276.  
  277.  
  278. void BaseWindow::DoSetSuspensionState(EventRecord *event,Boolean isSuspended)
  279. {
  280.     if (fFlags & kFloater)
  281.     {
  282.         if (isSuspended)
  283.         {
  284.             if (((WindowPeek)fWindow)->visible)
  285.             {
  286.                 fFlags |= kSuspended;
  287.                 ::HideWindow(fWindow);
  288.             }
  289.         }
  290.         else
  291.         {
  292.             if (fFlags & kSuspended)
  293.             {
  294.                 fFlags &= ~kSuspended;
  295.                 ::ShowWindow(fWindow);
  296.             }
  297.         }
  298.     }
  299.     else
  300.     {
  301.         if (isSuspended)
  302.         {
  303.             if (fFlags & kActive)
  304.             {
  305.                 fFlags |= kSuspended;
  306.                 DoSetActivationState(false);
  307.             }
  308.         }
  309.         else
  310.         {
  311.             if (fFlags & kSuspended)
  312.             {
  313.                 fFlags &= ~kSuspended;
  314.                 DoSetActivationState(true);
  315.             }
  316.         }
  317.     }
  318. }
  319.  
  320.  
  321. #pragma mark -
  322.  
  323.  
  324. Boolean BaseWindow::HandleGetParam(OSType param,SInt32 *value)
  325. {
  326.     switch(param)
  327.     {
  328.         case 'type':
  329.             *value = 'base';
  330.             return true;
  331.             break;
  332.         
  333.         default:
  334.             return false;
  335.             break;
  336.     }
  337. }
  338.  
  339.  
  340.  
  341.  
  342.  
  343. Boolean BaseWindow::HandleSetParam(OSType param,SInt32 value)
  344. {
  345.     switch(param)
  346.     {
  347.         default:
  348.             return false;
  349.             break;
  350.     }
  351. }
  352.  
  353.  
  354.  
  355.  
  356.  
  357. void BaseWindow::HandleClose(void)
  358. {
  359.     
  360. }
  361.  
  362.  
  363.  
  364.  
  365.  
  366. void BaseWindow::HandleZoomIn(void)
  367. {
  368.     
  369. }
  370.  
  371.  
  372.  
  373.  
  374.  
  375. void BaseWindow::HandleZoomOut(void)
  376. {
  377.     
  378. }
  379.  
  380.  
  381.  
  382.  
  383.  
  384. void BaseWindow::HandleDrag(Point start)
  385. {
  386.     Point    oldPos,newPos;
  387.     
  388.     
  389.     oldPos.h = fWindow->portRect.left;
  390.     oldPos.v = fWindow->portRect.top;
  391.     LocalToGlobal(&oldPos);
  392.     
  393.     DragWindow(fWindow,start,&qd.screenBits.bounds);
  394.     
  395.     newPos.h = fWindow->portRect.left;
  396.     newPos.v = fWindow->portRect.top;
  397.     LocalToGlobal(&newPos);
  398.     
  399.     if ((oldPos.h != newPos.h) || (oldPos.v != newPos.v))
  400.         HandleMove(newPos);
  401. }
  402.  
  403.  
  404.  
  405.  
  406.  
  407. void BaseWindow::HandleMove(Point where)
  408. {
  409.     
  410. }
  411.  
  412.  
  413.  
  414.  
  415.  
  416. void BaseWindow::HandleGrow(Point start)
  417. {
  418.     Rect    limits;
  419.     UInt32    dimensions;
  420.     
  421.     
  422.     limits.top = 64;
  423.     limits.left = 64;
  424.     limits.right = qd.screenBits.bounds.right;
  425.     limits.bottom = qd.screenBits.bounds.bottom;
  426.     
  427.     if (0 != (dimensions = GrowWindow(fWindow,start,&limits)))
  428.         HandleResize((dimensions >> 16),(dimensions & 0xFFFF));
  429. }
  430.  
  431.  
  432.  
  433.  
  434.  
  435. void BaseWindow::HandleResize(UInt32 height,UInt32 width)
  436. {
  437.     SizeWindow(fWindow,width,height,false);
  438.     InvalRect(&fWindow->portRect);
  439. }
  440.  
  441.  
  442.  
  443.  
  444.  
  445. void BaseWindow::HandleKey(UInt32 key,UInt32 modifiers)
  446. {
  447.     
  448. }
  449.  
  450.  
  451.  
  452.  
  453.  
  454. void BaseWindow::HandleClick(Point where,UInt32 modifiers)
  455. {
  456.     
  457. }
  458.  
  459.  
  460.  
  461.  
  462.  
  463. void BaseWindow::HandleActivate(void)
  464. {
  465.     
  466. }
  467.  
  468.  
  469.  
  470.  
  471.  
  472. void BaseWindow::HandleDeactivate(void)
  473. {
  474.     
  475. }
  476.  
  477.  
  478.  
  479.  
  480.  
  481. void BaseWindow::HandleCursorUpdate(Point mouse,UInt32 modifiers)
  482. {
  483.     
  484. }
  485.  
  486.  
  487.  
  488.  
  489.  
  490. void BaseWindow::HandleIdleTime(Point mouse,UInt32 modifiers)
  491. {
  492.     
  493. }
  494.  
  495.  
  496.  
  497.  
  498.  
  499. void BaseWindow::HandleDraw(void)
  500. {
  501.     
  502. }
  503.  
  504.  
  505. #pragma mark -
  506. #if GENERATINGPOWERPC
  507.  
  508.  
  509. #define RESULT_OFFSET(type)    ((sizeof(type) == 1) ? 3 : ((sizeof(type) == 2) ? 1 : 0))
  510.  
  511.  
  512. #if PRAGMA_ALIGN_SUPPORTED
  513. #pragma options align=mac68k
  514. #endif
  515.  
  516. typedef struct NewSystemWindowPB
  517. {
  518.     Rect                            *boundsRect;
  519.     StringPtr                        title;
  520.     Boolean                            visible;
  521.     short                            procID;
  522.     WindowPtr                        behind;
  523.     Boolean                            goAwayFlag;
  524.     long                             refCon;
  525.     SystemWindowEventHandlerProcPtr    eventProc;
  526.     RgnHandle                        mouseRgn;
  527. } NewSystemWindowPB;
  528.  
  529. #if PRAGMA_ALIGN_SUPPORTED
  530. #pragma options align=reset
  531. #endif
  532.  
  533.  
  534. short gNewSystemWindowGlue[] = {    0x4E56, 0x0000, 0x2F0A, 0x246E, 0x0008, 0x594F,
  535.                                     0x2F12, 0x2F2A, 0x0004, 0x1F2A, 0x0008, 0x3F2A,
  536.                                     0x000A, 0x2F2A, 0x000C, 0x1F2A, 0x0010, 0x2F2A,
  537.                                     0x0012, 0x2F2A, 0x0016, 0x2F2A, 0x001A, 0x3F3C,
  538.                                     0x005A, 0xA88F, 0x205F, 0x2008, 0x245F, 0x4E5E,
  539.                                     0x4E75 };
  540.  
  541.  
  542.  
  543.  
  544.  
  545. pascal WindowPtr NewSystemWindow(const Rect *boundsRect, ConstStr255Param title, Boolean visible, short procID, WindowPtr behind, Boolean goAwayFlag, long refCon, SystemWindowEventHandlerProcPtr eventProc, RgnHandle mouseRgn)
  546. {
  547.     NewSystemWindowPB    pb;
  548.     long                private_result;
  549.     
  550.     
  551.     pb.boundsRect = (Rect*)boundsRect;
  552.     pb.title = (StringPtr)title;
  553.     pb.visible = visible;
  554.     pb.procID = procID;
  555.     pb.behind = behind;
  556.     pb.goAwayFlag = goAwayFlag;
  557.     pb.refCon = refCon;
  558.     pb.eventProc = eventProc;
  559.     pb.mouseRgn = mouseRgn;
  560.     
  561.     private_result = CallUniversalProc((UniversalProcPtr)gNewSystemWindowGlue,
  562.         kCStackBased
  563.         | RESULT_SIZE(SIZE_CODE(sizeof(WindowPtr)))
  564.         | STACK_ROUTINE_PARAMETER(1, SIZE_CODE(sizeof(NewSystemWindowPB*))),
  565.         &pb);
  566.     
  567.     return *(((WindowPtr*)&private_result) + RESULT_OFFSET(WindowPtr));
  568. }
  569.  
  570.  
  571.  
  572.  
  573.  
  574. pascal OSErr DisposeSystemWindow(WindowPtr theWindow)
  575. {
  576.     long    private_result;
  577.     
  578.     
  579.     private_result = CallUniversalProc(NGetTrapAddress(0xA88F,ToolTrap),
  580.         kPascalStackBased
  581.         | RESULT_SIZE(SIZE_CODE(sizeof(OSErr)))
  582.         | STACK_ROUTINE_PARAMETER(1, SIZE_CODE(sizeof(WindowPtr)))
  583.         | STACK_ROUTINE_PARAMETER(2, SIZE_CODE(sizeof(UInt16))),
  584.         theWindow,
  585.         0x005B);
  586.     
  587.     return *(((OSErr*)&private_result) + RESULT_OFFSET(OSErr));
  588. }
  589.  
  590.  
  591.  
  592.  
  593.  
  594. pascal short FindSystemWindow(Point thePoint, WindowPtr *theWindow)
  595. {
  596.     long    private_result;
  597.     
  598.     
  599.     private_result = CallUniversalProc(NGetTrapAddress(0xA88F,ToolTrap),
  600.         kPascalStackBased
  601.         | RESULT_SIZE(SIZE_CODE(sizeof(short)))
  602.         | STACK_ROUTINE_PARAMETER(1, SIZE_CODE(sizeof(Point)))
  603.         | STACK_ROUTINE_PARAMETER(2, SIZE_CODE(sizeof(WindowPtr*)))
  604.         | STACK_ROUTINE_PARAMETER(3, SIZE_CODE(sizeof(UInt16))),
  605.         thePoint,
  606.         theWindow,
  607.         0x0064);
  608.     
  609.     return *(((short*)&private_result) + RESULT_OFFSET(short));
  610. }
  611.  
  612.  
  613. #endif
  614.