home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD1.mdf / magazine / msysjour / vol04 / 01b / macsl / mpmwin.c < prev    next >
C/C++ Source or Header  |  1988-10-04  |  16KB  |  574 lines

  1. /*-----------------------------------------------------------------*/
  2. /* MpmWin.c                                                        */
  3. /* Miscellaneous windowing functions                               */
  4. /*-----------------------------------------------------------------*/
  5.  
  6. #include "MacPM.h"
  7.  
  8. /*-----------------------------------------------------------------*/
  9.  
  10. LOCAL BOOL      MpmShowWindow( HWND hwnd, BOOL fShow, BOOL fPaint );
  11.  
  12. /*-----------------------------------------------------------------*/
  13. /* Default window function.  The only message currently handled    */
  14. /* specially here is WM_CLOSE, which posts a WM_QUIT message.      */
  15. /* All other messages just return 0L.                              */
  16. /*-----------------------------------------------------------------*/
  17.  
  18. MRESULT APIENTRY WinDefWindowProc( hwnd, msg, mp1, mp2 )
  19.     HWND        hwnd;
  20.     USHORT      msg;
  21.     MPARAM      mp1;
  22.     MPARAM      mp2;
  23. {
  24.     if( ! MpmValidateWindow(hwnd) )
  25.       return 0L;
  26.  
  27.     switch( msg )
  28.     {
  29.       case WM_CLOSE:
  30.         WinPostQueueMsg( (HAB)NULL, WM_QUIT, 0L, 0L );
  31.         break;
  32.     }
  33.     
  34.     return 0L;
  35. }
  36.  
  37. /*-----------------------------------------------------------------*/
  38. /* Enable or disable hwnd for keyboard/mouse input, according to   */
  39. /* fEnable.
  40. /*-----------------------------------------------------------------*/
  41.  
  42. BOOL APIENTRY WinEnableWindow( hwnd, fEnable )
  43.     HWND        hwnd;
  44.     BOOL        fEnable;
  45. {
  46.     if( ! MpmValidateWindow(hwnd) )
  47.       return FALSE;
  48.  
  49.     if( ISDESKTOPWINDOW(hwnd) || ISOBJECTWINDOW(hwnd) )
  50.       return FALSE;
  51.  
  52.     fEnable = !! fEnable;  /* normalize boolean value */
  53.  
  54.     /* Notify window if state is changing */
  55.  
  56.     if( fEnable == !( MYWNDOF(hwnd).flStyle & WS_DISABLED ) )
  57.       WinSendMsg( hwnd, WM_ENABLE, MPFROMSHORT(fEnable), 0 );
  58.  
  59.     /* Get rid of input focus if this window has it and it's
  60.        being disabled */
  61.  
  62.     if( ! fEnable  &&  hwnd == _hwndFocus )
  63.       WinSetFocus( _hwndDesktop, NULL );
  64.  
  65.     /* Tweak the style bits */
  66.  
  67.     if( fEnable )
  68.       MYWNDOF(hwnd).flStyle &= ~WS_DISABLED;
  69.     else
  70.       MYWNDOF(hwnd).flStyle |= WS_DISABLED;
  71.  
  72.     return TRUE;
  73. }
  74.  
  75. /*-----------------------------------------------------------------*/
  76. /* Set or reset hwnd's WS_VISIBLE flag, without causing any window */
  77. /* painting.                                                       */
  78. /*-----------------------------------------------------------------*/
  79.  
  80. BOOL APIENTRY WinEnableWindowUpdate( hwnd, fEnable )
  81.     HWND        hwnd;
  82.     BOOL        fEnable;
  83. {
  84.     if( ! MpmValidateWindow(hwnd) )
  85.       return FALSE;
  86.  
  87.     if( ISDESKTOPWINDOW(hwnd) || ISOBJECTWINDOW(hwnd) )
  88.       return FALSE;
  89.  
  90.     return MpmShowWindow( hwnd, fEnable, FALSE ); 
  91. }
  92.  
  93. /*-----------------------------------------------------------------*/
  94. /* Set the active window to hwndSetActive or its top-level parent. */
  95. /*-----------------------------------------------------------------*/
  96.  
  97. BOOL APIENTRY WinSetActiveWindow( hwndDesktop, hwndSetActive )
  98.     HWND        hwndDesktop, hwndSetActive;
  99. {
  100.     QMSG        qmsg;
  101.  
  102.     if( ! MpmValidateWindow(hwndSetActive) )
  103.       return FALSE;
  104.  
  105.     /* Get to the main Mac window if this is a child */
  106.  
  107.     hwndSetActive = MAINHWND(hwndSetActive);
  108.  
  109.     /* Get out if this window is already active */
  110.  
  111.     if( hwndSetActive == _hwndActive )
  112.       return FALSE;
  113.  
  114.     /* Tell the Mac to make it the active window */
  115.  
  116.     SelectWindow( PWINOFHWND(hwndSetActive) );
  117.  
  118.     /* Force the activate event to get to the window function now */
  119.  
  120.     while( WinPeekMsg( NULL, &qmsg, NULL,
  121.                        WM_ACTIVATE, WM_ACTIVATE, PM_REMOVE ) )
  122.       WinDispatchMsg( NULL, &qmsg );
  123.  
  124.     return TRUE;
  125. }
  126.  
  127. /*-----------------------------------------------------------------*/
  128. /* Set the input focus to hwndSetFocus.                            */
  129. /*-----------------------------------------------------------------*/
  130.  
  131. BOOL APIENTRY WinSetFocus( hwndDesktop, hwndSetFocus )
  132.     HWND        hwndDesktop, hwndSetFocus;
  133. {
  134.     if( hwndSetFocus && ! MpmValidateWindow(hwndSetFocus) )
  135.       return FALSE;
  136.  
  137.     /* Nothing to do if it already has the focus */
  138.  
  139.     if( hwndSetFocus == _hwndFocus )
  140.       return TRUE;
  141.  
  142.     _sSetFocusDepth++;  /* Avoid recursive call to WinSetFocus! */
  143.  
  144.     /* Notify the previous focus window it's losing it */
  145.  
  146.     if( _hwndFocus )
  147.       WinSendMsg( _hwndFocus, WM_SETFOCUS,
  148.                   MPFROMHWND(_hwndFocus), MPFROMSHORT(FALSE) );
  149.  
  150.     /* Set the active window and the new focus */
  151.  
  152.     if( hwndSetFocus )
  153.       WinSetActiveWindow( NULL, hwndSetFocus );
  154.  
  155.     _hwndFocus = hwndSetFocus;
  156.  
  157.     /* Notify the window it's receiving the focus */
  158.  
  159.     if( _hwndFocus )
  160.       WinSendMsg( _hwndFocus, WM_SETFOCUS,
  161.                   MPFROMHWND(_hwndFocus), MPFROMSHORT(TRUE) );
  162.  
  163.     _sSetFocusDepth--;
  164.  
  165.     return TRUE;
  166. }
  167.  
  168. /*-----------------------------------------------------------------*/
  169. /* Set the position and size of the window(s) listed in *pswp.     */
  170. /* This function is lazy - instead of invalidating only the child  */
  171. /* windows actually being moved or resized, it simply invalidates  */
  172. /* the entire main Mac window.  (See the code under #ifdef INVAL)  */
  173. /* Also, the function doesn't handle Z-ordering.                   */
  174. /*-----------------------------------------------------------------*/
  175.  
  176. BOOL APIENTRY WinSetMultWindowPos( hab, pswp, cswp )
  177.     HAB         hab;
  178.     PSWP        pswp;
  179.     SHORT       cswp;
  180. {
  181.     HWND        hwnd;
  182.     PMYWND      pwnd;
  183.     UCHAR       ucKind;
  184.     HWND        hwndParent;
  185.     SHORT       xParent, yParent, cxParent, cyParent;
  186.     SHORT       cxOld, cyOld;
  187.     ULONG       flStyle;
  188.     Rect        rect;
  189.     Rect*       prect;
  190.     Rect*       prectAdj;
  191.  
  192.     ucKind = MYWNDOF(pswp->hwnd).ucKind;
  193.  
  194.     ASSERT( ucKind >= WK_MAIN,
  195.             "WinSetMultWindowPos: Desktop or object window illegal" );
  196.  
  197.     /* Don't worry, be happy - just invalidate the whole thing */
  198.  
  199. #ifndef INVAL
  200.     {
  201.       WindowPeek  pwin;
  202.       thePort = &PWINOFHWND(pswp->hwnd)->port;
  203.       rect = thePort->portRect;
  204.       InvalRect( &rect );
  205.       EraseRect( &rect );
  206.     }
  207. #endif
  208.  
  209.     hwndParent = NULL;
  210.  
  211.     /* Loop through all the windows in *pswp */
  212.  
  213.     for( ;  cswp > 0;  pswp++, cswp-- )
  214.     {
  215.       hwnd = pswp->hwnd;
  216.       if( ! hwnd )
  217.         continue;
  218.  
  219.       if( ! MpmValidateWindow(hwnd) )
  220.         return FALSE;
  221.  
  222.       flStyle = MYWNDOF(hwnd).flStyle;
  223.  
  224.       /* Pick up parent window info, make sure all windows have the
  225.          same parent */
  226.  
  227.       if( ! hwndParent )
  228.       {
  229.         hwndParent = MYWNDOF(hwnd).hwndParent;
  230.         pwnd = PMYWNDOF(hwndParent);
  231.         xParent  = pwnd->x;
  232.         yParent  = pwnd->y;
  233.         cxParent = pwnd->cx;
  234.         cyParent = pwnd->cy;
  235.       }
  236.  
  237.       ASSERT( MYWNDOF(hwnd).hwndParent == hwndParent,
  238.               "WinSetMultWindowPos: Windows have different parents" );
  239.  
  240.       /* Handle moving and sizing if requested */
  241.  
  242.       if( pswp->fs & (SWP_MOVE|SWP_SIZE) )
  243.       {
  244. #ifdef INVAL
  245.         if( flStyle & WS_VISIBLE )
  246.           MpmInvalidateWindow( hwnd );
  247. #endif
  248.  
  249.         if( ! MYWNDOF(hwnd).hwndTopChild && (flStyle & WS_VISIBLE) )
  250.           MYWNDOF(hwnd).flStyle &= ~WS_VISIBLE;  /* hack! */
  251.  
  252.         /* Set up new coordinates */
  253.  
  254.         pwnd = PMYWNDOF(hwnd);
  255.         cxOld = pwnd->cx;
  256.         cyOld = pwnd->cy;
  257.         if( pswp->fs & SWP_MOVE )
  258.         {
  259.           pwnd->x = pswp->x;
  260.           pwnd->y = pswp->y;
  261.         }
  262.         if( pswp->fs & SWP_SIZE )
  263.         {
  264.           pwnd->cx = pswp->cx;
  265.           pwnd->cy = pswp->cy;
  266.         }
  267.  
  268.         /* Give the window a chance to tweak the coordinates with
  269.            a WM_ADJUSTWINDOWPOS message */
  270.  
  271.         if( ! ( pswp->fs & SWP_NOADJUST ) )
  272.           WinSendMsg( hwnd, WM_ADJUSTWINDOWPOS, MPFROMP(pswp), 0L );
  273.  
  274.         /* any fancy BitBlts could go here... */
  275.  
  276.         /* For a top-level window, ask the Mac to move and size it */
  277.  
  278.         if( ucKind == WK_MAIN )
  279.         {
  280.           pwnd = PMYWNDOF(hwnd);
  281.           prectAdj  = &pwnd->rectAdj;
  282.           MoveWindow( MYWNDOF(hwnd).pwin,
  283.                       pwnd->x + prectAdj->left,
  284.                       _alSysVal[SV_CYSCREEN] - pwnd->y - pwnd->cy
  285.                         + prectAdj->top,
  286.                       FALSE );
  287.           SizeWindow( MYWNDOF(hwnd).pwin,
  288.                       pwnd->cx - prectAdj->left + prectAdj->right,
  289.                       pwnd->cy - prectAdj->top  + prectAdj->bottom,
  290.                       FALSE );
  291.         }
  292.  
  293.         /* Notify the window of the new position and size */
  294.  
  295.         if( MYCLASSOF(hwnd).class.flClassStyle & CS_MOVENOTIFY )
  296.           WinSendMsg( hwnd, WM_MOVE, 0L, 0L );
  297.  
  298.         WinSendMsg( hwnd, WM_SIZE,
  299.                     MPFROM2SHORT( cxOld, cyOld ),
  300.                     MPFROM2SHORT( pwnd->cx, pwnd->cy ) );
  301.  
  302. #ifdef INVAL
  303.         if( flStyle & WS_VISIBLE )
  304.           MpmInvalidateWindow( hwnd );
  305. #endif
  306.  
  307.         if( ! MYWNDOF(hwnd).hwndTopChild && (flStyle & WS_VISIBLE) )
  308.           MYWNDOF(hwnd).flStyle |= WS_VISIBLE;  /* hack! */
  309.       }
  310.     }
  311.  
  312.     return TRUE;
  313. }
  314.  
  315. /*-----------------------------------------------------------------*/
  316. /* Set one window's position and size.  This is easy - just call   */
  317. /* WinSetMultWindowPos!  (Note that a SWP structure is defined to  */
  318. /* exactly match the parameters on the stack for WinSetWindowPos.) */
  319. /*-----------------------------------------------------------------*/
  320.  
  321. BOOL APIENTRY WinSetWindowPos( hwnd, hwndBehind, x, y, cx, cy, fs )
  322.     HWND        hwnd, hwndBehind;
  323.     SHORT       x, y, cx, cy;
  324.     USHORT      fs;
  325. {
  326.     return WinSetMultWindowPos( (HAB)NULL, (PSWP)&hwnd, 1 );
  327. }
  328.  
  329. /*-----------------------------------------------------------------*/
  330. /* Set a window's text, by sending a WM_SETWINDOWPARAMS message.   */
  331. /*-----------------------------------------------------------------*/
  332.  
  333. BOOL APIENTRY WinSetWindowText( hwnd, pszText )
  334.     HWND        hwnd;
  335.     PSZ         pszText;
  336. {
  337.     WNDPARAMS   wprm;
  338.  
  339.     memzero( &wprm );
  340.  
  341.     wprm.fsStatus = WPM_TEXT | WPM_CCHTEXT;
  342.     wprm.pszText = pszText;
  343.     wprm.cchText = strlen( pszText );
  344.  
  345.     return (BOOL)WinSendMsg( hwnd, WM_SETWINDOWPARAMS,
  346.                                    MPFROMP(&wprm), 0 );
  347. }
  348.  
  349. /*-----------------------------------------------------------------*/
  350. /* Show or hide hwnd, according to fShow.                          */
  351. /*-----------------------------------------------------------------*/
  352.  
  353. BOOL APIENTRY WinShowWindow( hwnd, fShow )
  354.     HWND        hwnd;
  355.     BOOL        fShow;
  356. {
  357.     if( ! MpmValidateWindow(hwnd) )
  358.       return FALSE;
  359.  
  360.     if( ISDESKTOPWINDOW(hwnd) || ISOBJECTWINDOW(hwnd) )
  361.       return FALSE;
  362.  
  363.     return MpmShowWindow( hwnd, fShow, TRUE );  
  364. }
  365.  
  366. /*-----------------------------------------------------------------*/
  367. /* Do the real work for WinShowWindow and WinEnableWindowUpdate.   */
  368. /* Recurse through all the child windows to take care of them, set */
  369. /* WS_VISIBLE as requested, ask the Mac to show or hide a top      */
  370. /* level window, and send the WM_SHOW message.                     */
  371. /*-----------------------------------------------------------------*/
  372.  
  373. LOCAL BOOL MpmShowWindow( hwnd, fShow, fPaint )
  374.     HWND        hwnd;
  375.     BOOL        fShow;
  376.     BOOL        fPaint;
  377. {
  378.     HWND        hwndChild;
  379.  
  380.     /* Call this function recursively for all children */
  381.  
  382.     for( hwndChild = MYWNDOF(hwnd).hwndTopChild;
  383.          hwndChild;
  384.          hwndChild = MYWNDOF(hwndChild).hwndNextSibling )
  385.       MpmShowWindow( hwndChild, fShow, FALSE );
  386.  
  387.     /* Tweak the WS_VISIBLE flag */
  388.  
  389.     if( fShow )
  390.     {
  391.       fShow = TRUE;  /* normalize fShow */
  392.  
  393.       if( MYWNDOF(hwnd).flStyle & WS_VISIBLE )
  394.         return FALSE;
  395.  
  396.       MYWNDOF(hwnd).flStyle |= WS_VISIBLE;
  397.     }
  398.     else
  399.     {
  400.       if( !( MYWNDOF(hwnd).flStyle & WS_VISIBLE ) )
  401.         return FALSE;
  402.  
  403.       MYWNDOF(hwnd).flStyle &= ~WS_VISIBLE;
  404.     }
  405.  
  406.     /* Update the screen if requested */
  407.  
  408.     if( fPaint )
  409.     {
  410.       if( ISCHILDWINDOW(hwnd) )
  411. #ifdef INVAL
  412.         MpmInvalidateWindow( hwnd );
  413. #else
  414.         ;
  415. #endif
  416.       else if( fShow )
  417.         ShowWindow( PWINOFHWND(hwnd) );
  418.       else
  419.         HideWindow( PWINOFHWND(hwnd) );
  420.     }
  421.  
  422.     /* Let the window know with a WM_SHOW */
  423.  
  424.     WinSendMsg( hwnd, WM_SHOW, MPFROMSHORT(fShow), 0L );
  425.  
  426.     return TRUE;  
  427. }
  428.  
  429. /*-----------------------------------------------------------------*/
  430. /* Window function for button controls.  (Unimplemented!)          */
  431. /*-----------------------------------------------------------------*/
  432.  
  433. MRESULT EXPENTRY MpmFnwpButton( hwnd, msg, mp1, mp2 )
  434.     HWND        hwnd;
  435.     USHORT      msg;
  436.     MPARAM      mp1;
  437.     MPARAM      mp2;
  438. {
  439.     if( ! MpmValidateWindow(hwnd) )
  440.       return FALSE;
  441.  
  442.     switch( msg )
  443.     {
  444.     }
  445.     
  446.     return WinDefWindowProc( hwnd, msg, mp1, mp2 );
  447. }
  448.  
  449. /*-----------------------------------------------------------------*/
  450. /* Window function for the desktop window.  Nothing to do!         */
  451. /*-----------------------------------------------------------------*/
  452.  
  453. MRESULT EXPENTRY MpmFnwpDesktop( hwnd, msg, mp1, mp2 )
  454.     HWND        hwnd;
  455.     USHORT      msg;
  456.     MPARAM      mp1;
  457.     MPARAM      mp2;
  458. {
  459.     if( ! MpmValidateWindow(hwnd) )
  460.       return FALSE;
  461.  
  462.     switch( msg )
  463.     {
  464.     }
  465.     
  466.     return WinDefWindowProc( hwnd, msg, mp1, mp2 );
  467. }
  468.  
  469. /*-----------------------------------------------------------------*/
  470. /* Window function for dialog box class.  (Unimplemented!)         */
  471. /*-----------------------------------------------------------------*/
  472.  
  473. #ifdef FUTURE
  474.  
  475. MRESULT EXPENTRY MpmFnwpDialog( hwnd, msg, mp1, mp2 )
  476.     HWND        hwnd;
  477.     USHORT      msg;
  478.     MPARAM      mp1;
  479.     MPARAM      mp2;
  480. {
  481.     if( ! MpmValidateWindow(hwnd) )
  482.       return FALSE;
  483.  
  484.     switch( msg )
  485.     {
  486.     }
  487.     
  488.     return WinDefWindowProc( hwnd, msg, mp1, mp2 );
  489. }
  490.  
  491. #endif
  492.  
  493. /*-----------------------------------------------------------------*/
  494. /* Window function for edit fields.  (Unimplemented!)              */
  495. /*-----------------------------------------------------------------*/
  496.  
  497. MRESULT EXPENTRY MpmFnwpEntryField( hwnd, msg, mp1, mp2 )
  498.     HWND        hwnd;
  499.     USHORT      msg;
  500.     MPARAM      mp1;
  501.     MPARAM      mp2;
  502. {
  503.     if( ! MpmValidateWindow(hwnd) )
  504.       return FALSE;
  505.  
  506.     switch( msg )
  507.     {
  508.     }
  509.     
  510.     return WinDefWindowProc( hwnd, msg, mp1, mp2 );
  511. }
  512.  
  513. /*-----------------------------------------------------------------*/
  514. /* Window function for list boxes.  (Unimplemented!)               */
  515. /*-----------------------------------------------------------------*/
  516.  
  517. MRESULT EXPENTRY MpmFnwpListBox( hwnd, msg, mp1, mp2 )
  518.     HWND        hwnd;
  519.     USHORT      msg;
  520.     MPARAM      mp1;
  521.     MPARAM      mp2;
  522. {
  523.     if( ! MpmValidateWindow(hwnd) )
  524.       return FALSE;
  525.  
  526.     switch( msg )
  527.     {
  528.     }
  529.     
  530.     return WinDefWindowProc( hwnd, msg, mp1, mp2 );
  531. }
  532.  
  533. /*-----------------------------------------------------------------*/
  534. /* Window function for object window.  Nothing to do!              */
  535. /*-----------------------------------------------------------------*/
  536.  
  537. MRESULT EXPENTRY MpmFnwpObject( hwnd, msg, mp1, mp2 )
  538.     HWND        hwnd;
  539.     USHORT      msg;
  540.     MPARAM      mp1;
  541.     MPARAM      mp2;
  542. {
  543.     if( ! MpmValidateWindow(hwnd) )
  544.       return FALSE;
  545.  
  546.     switch( msg )
  547.     {
  548.     }
  549.     
  550.     return WinDefWindowProc( hwnd, msg, mp1, mp2 );
  551. }
  552.  
  553. /*-----------------------------------------------------------------*/
  554. /* Window function for static text fields.  (Unimplemented!)       */
  555. /*-----------------------------------------------------------------*/
  556.  
  557. MRESULT EXPENTRY MpmFnwpStatic( hwnd, msg, mp1, mp2 )
  558.     HWND        hwnd;
  559.     USHORT      msg;
  560.     MPARAM      mp1;
  561.     MPARAM      mp2;
  562. {
  563.     if( ! MpmValidateWindow(hwnd) )
  564.       return FALSE;
  565.  
  566.     switch( msg )
  567.     {
  568.     }
  569.     
  570.     return WinDefWindowProc( hwnd, msg, mp1, mp2 );
  571. }
  572.  
  573. /*-----------------------------------------------------------------*/
  574.