home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / d / d020_1_4 / 6.ddi / DEFPROCS / DEFWND.C < prev   
Encoding:
Text File  |  1990-06-01  |  14.5 KB  |  549 lines

  1. /*--------------------------------------------------------------------------*/
  2. /*                                        */
  3. /*  DefWindowProc() -                                */
  4. /*                                        */
  5. /*--------------------------------------------------------------------------*/
  6.  
  7. LONG FAR PASCAL DefWindowProc(hwnd, message, wParam, lParam)
  8.  
  9. register HWND hwnd;
  10. WORD          message;
  11. register WORD wParam;
  12. LONG          lParam;
  13.  
  14. {
  15.   int          i;
  16.   HDC          hdc;
  17.   PAINTSTRUCT ps;
  18.   HICON          hIcon;
  19.   RECT          rc;
  20.   HANDLE      hCurs;
  21.   HBRUSH      hbr;
  22.   HWND          hwndT;
  23.  
  24.   if (!CheckHwnd(hwnd))
  25.       return((DWORD)FALSE);
  26.  
  27.   switch (message)
  28.     {
  29.       case WM_NCACTIVATE:
  30.       if (wParam != 0)
  31.           SetWF(hwnd, WFFRAMEON);
  32.       else
  33.           ClrWF(hwnd, WFFRAMEON);
  34.  
  35.       if (TestWF(hwnd, WFVISIBLE) && !TestWF(hwnd, WFNONCPAINT))
  36.         {
  37.           hdc = GetWindowDC(hwnd);
  38.           DrawCaption(hwnd, hdc, TRUE, TestWF(hwnd, WFFRAMEON));
  39.           InternalReleaseDC(hdc);
  40.           if (TestWF(hwnd,WFMINIMIZED))
  41.           RedrawIconTitle(hwnd);
  42.         }
  43.       return(TRUE);
  44.  
  45.       case WM_NCHITTEST:
  46.       return(FindNCHit(hwnd, lParam));
  47.  
  48.       case WM_NCCALCSIZE:
  49.       CalcClientRect(hwnd, (LPRECT)lParam);
  50.       break;
  51.  
  52.       case WM_NCLBUTTONDOWN:
  53.     {
  54.       WORD         cmd;
  55.       RECT         rcWindow;
  56.       RECT         rcCapt;
  57.       RECT         rcInvert;
  58.       RECT         rcWindowSave;
  59.  
  60.       cmd = 0;
  61.  
  62.       switch(wParam)
  63.         {
  64.           case HTZOOM:
  65.           case HTREDUCE:
  66.           GetWindowRect(hwnd, (LPRECT)&rcWindow);
  67.           CopyRect((LPRECT)&rcWindowSave, (LPRECT)&rcWindow);
  68.  
  69.           if (TestWF(hwnd, WFSIZEBOX))
  70.               InflateRect((LPRECT)&rcWindow, 
  71.                       -cxSzBorderPlus1, -cySzBorderPlus1);
  72.           else
  73.               InflateRect((LPRECT)&rcWindow, -cxBorder, -cyBorder);
  74.  
  75.           rcCapt.right = rcWindow.right + cxBorder;
  76.                   rcCapt.left = rcWindow.right - oemInfo.bmReduce.cx-cxBorder;
  77.  
  78.           if (wParam == HTREDUCE)
  79.               cmd = SC_MINIMIZE;
  80.           else if (TestWF(hwnd, WFMAXIMIZED))
  81.               cmd = SC_RESTORE;
  82.           else
  83.               cmd = SC_MAXIMIZE;
  84.  
  85.           if (wParam == HTREDUCE && TestWF(hwnd, WFMAXBOX))
  86.                       OffsetRect((LPRECT)&rcCapt, -oemInfo.bmReduce.cx, 0);
  87.  
  88.           rcCapt.top = rcWindow.top;
  89.           rcCapt.bottom = rcCapt.top + cyCaption;
  90.  
  91.           CopyRect((LPRECT)&rcInvert, (LPRECT)&rcCapt);
  92.                   InflateRect((LPRECT)&rcInvert, -cxBorder, -cyBorder);
  93.  
  94.                   rcInvert.right += cxBorder;
  95.           rcInvert.left += cxBorder;
  96.  
  97.           /* Converting to window coordinates. */
  98.           OffsetRect((LPRECT)&rcInvert, 
  99.                  -(rcWindowSave.left + cxBorder),
  100.                  -(rcWindowSave.top + cyBorder));
  101.           /* Wait for the BUTTONUP message and see if cursor is still
  102.            * in the Minimize or Maximize box.
  103.            *
  104.            * NOTE: rcInvert is in window coords, rcCapt is in screen 
  105.            * coords
  106.            */
  107.                   if (!DepressTitleButton(hwnd, rcCapt, rcInvert, wParam))
  108.               cmd = 0;
  109.  
  110.           break;
  111.  
  112.           default:
  113.           if (wParam >= HTSIZEFIRST && wParam <= HTSIZELAST)
  114.               /* Change HT into a MV command. */
  115.               cmd = SC_SIZE + (wParam - HTSIZEFIRST + MVSIZEFIRST);
  116.         }
  117.  
  118.       if (cmd != 0)
  119.         {
  120.           /* For SysCommands on system menu, don't do if
  121.            * menu item is disabled.
  122.            */
  123.           if (TestWF(hwnd, WFSYSMENU))
  124.         {
  125.                   /* don't check old app child windows
  126.            */
  127.           if (LOWORD(GetExpWinVer(hwnd->hInstance)) >= VER || 
  128.               !TestwndChild(hwnd))
  129.             {
  130.               SetSysMenu(hwnd);
  131.               if (GetMenuState(GetSysMenuHandle(hwnd), cmd & 0xFFF0,
  132.                 MF_BYCOMMAND) & (MF_DISABLED | MF_GRAYED))
  133.               break;
  134.             }
  135.         }
  136.           SendMessage(hwnd, WM_SYSCOMMAND, cmd, lParam);
  137.           break;
  138.         }
  139.       /*** FALL THRU ***/
  140.     }
  141.  
  142.       case WM_NCMOUSEMOVE:
  143.       case WM_NCLBUTTONUP:
  144.       case WM_NCLBUTTONDBLCLK:
  145.       HandleNCMouseGuys(hwnd, message, wParam, lParam);
  146.       break;
  147.  
  148.       case WM_CANCELMODE:
  149.       if (hwndCapture == hwnd && pfnSB != NULL)
  150.           EndScroll(hwnd, TRUE);
  151.  
  152.           if (fMenu && hwndMenu == hwnd)
  153.               EndMenu();
  154.  
  155.       /* If the capture is still set, just release at this point.
  156.        * Can put other End* functions in later.
  157.        */
  158.       if (hwnd == hwndCapture)
  159.           ReleaseCapture();
  160.       break;
  161.  
  162.       case WM_NCCREATE:
  163.       if (TestWF(hwnd, (WFHSCROLL | WFVSCROLL)))
  164.           if (InitPwSB(hwnd) == NULL)
  165.           return((LONG)FALSE);
  166.  
  167.       return((LONG)DefSetText(hwnd, ((LPCREATESTRUCT)lParam)->lpszName));
  168.  
  169.       case WM_NCDESTROY:
  170.       if (hwnd->hName)
  171.           hwnd->hName = TextFree(hwnd->hName);
  172.       break;
  173.  
  174.       case WM_NCPAINT:
  175.       /* Force the drawing of the menu. */
  176.       SetWF(hwnd, WFMENUDRAW);
  177.       DrawWindowFrame(hwnd, (HRGN)wParam);
  178.       ClrWF(hwnd, WFMENUDRAW);
  179.       break;
  180.  
  181.       case WM_SETTEXT:
  182.       DefSetText(hwnd, (LPSTR)lParam);
  183.       if (TestWF(hwnd, WFVISIBLE))
  184.             {
  185.           if (TestWF(hwnd,WFMINIMIZED))
  186.         {
  187.           ShowIconTitle(hwnd,FALSE);
  188.           ShowIconTitle(hwnd,TRUE);
  189.         }
  190.           else if (TestWF(hwnd, WFBORDERMASK) == (BYTE)LOBYTE(WFCAPTION))
  191.         {
  192.           hdc = GetWindowDC(hwnd);
  193.           DrawCaption(hwnd, hdc, FALSE, TestWF(hwnd, WFFRAMEON));
  194.           InternalReleaseDC(hdc);
  195.         }
  196.             }
  197.       break;
  198.  
  199.       case WM_GETTEXT:
  200.       if (wParam)
  201.             {
  202.               if (hwnd->hName)
  203.               return (DWORD)TextCopy(hwnd->hName,(LPSTR)lParam,wParam);
  204.  
  205.               /* else Null terminate the text buffer since there is no text.
  206.            */
  207.           ((LPSTR)lParam)[0] = NULL;
  208.             }
  209.       return (0L);
  210.  
  211.  
  212.       case WM_GETTEXTLENGTH:
  213.       if (hwnd->hName)
  214.           return(lstrlen(TextPointer(hwnd->hName)));
  215.  
  216.           /* else */
  217.           return(0L);
  218.  
  219.       case WM_CLOSE:
  220.       DestroyWindow(hwnd);
  221.       break;
  222.  
  223.       case WM_PAINT:
  224.       BeginPaint(hwnd, (LPPAINTSTRUCT)&ps);
  225.       EndPaint(hwnd, (LPPAINTSTRUCT)&ps);
  226.       break;
  227.  
  228.       case WM_PAINTICON:
  229.           /* Draw the icon through the window DC if app used own DC. If own DC
  230.        * is used the mapping mode may not be MM_TEXT.  
  231.        */
  232.           BeginPaint(hwnd, (LPPAINTSTRUCT)&ps);
  233.           if (TestCF(hwnd, CFOWNDC) || TestCF(hwnd, CFCLASSDC))
  234.             {
  235.               /* If owndc, do the end paint now so that the
  236.            * erasebackgrounds/validate regions go through properly. Then
  237.            * we get a clean window dc to draw the icon into. 
  238.            */
  239.               InternalEndPaint(hwnd, (LPPAINTSTRUCT)&ps, TRUE);
  240.               hdc = GetWindowDC(hwnd);
  241.             }
  242.           else
  243.             {
  244.               hdc = ps.hdc;
  245.             }
  246.  
  247.       /* wParam is TRUE to draw icon, FALSE to ignore paint. */
  248.       if (wParam)
  249.         {
  250.               hIcon = (HICON)(PCLS)(hwnd->pcls)->hIcon;
  251.               GetClientRect(hwnd, (LPRECT)&rc);
  252.  
  253.               rc.left = (rc.right - rgwSysMet[SM_CXICON]) >> 1;
  254.           rc.top = (rc.bottom - rgwSysMet[SM_CYICON]) >> 1;
  255.  
  256.           DrawIcon(hdc, rc.left, rc.top, hIcon);
  257.         }
  258.  
  259.       /* Delete the update region. */
  260.           if (TestCF(hwnd, CFOWNDC) || TestCF(hwnd, CFCLASSDC))
  261.             {
  262.               InternalReleaseDC(hdc);
  263.               /* ValidateRect(hwnd, NULL); */
  264.             }
  265.           else
  266.               InternalEndPaint(hwnd, (LPPAINTSTRUCT)&ps, TRUE);
  267.       break;
  268.  
  269.       case WM_ICONERASEBKGND:
  270.           /* Erase the icon through the window DC if app used own DC. If own
  271.        * DC is used the mapping mode may not be MM_TEXT. 
  272.        */
  273.           if (TestCF(hwnd, CFOWNDC) || TestCF(hwnd, CFCLASSDC))
  274.             hdc = GetWindowDC(hwnd);
  275.           else
  276.              hdc = (HDC)wParam;
  277.  
  278.           if (TestWF(hwnd, WFCHILD))    /* for MDI child icons */
  279.             {
  280.               if ((hbr = GetBackBrush(hwnd->hwndParent)) == NULL)
  281.         {
  282.                   /* No brush, punt. */
  283.                   goto AbortIconEraseBkGnd;
  284.                 }
  285.               else
  286.                   goto ICantBelieveIUsedAGoToStatement;
  287.         }
  288.  
  289.       if (hbmWallpaper)
  290.             {
  291.               /* Since desktop bitmaps are done on a wm_paint message (and not
  292.            * erasebkgnd), we need to call the paint proc with our dc 
  293.            */
  294.               PaintDesktop(hdc);
  295.               /* SendMessage(hwndDesktop, WM_ERASEBKGND, hdc, 0L);*/
  296.             }
  297.       else
  298.         {
  299.           hbr = sysClrObjects.hbrDesktop;
  300. ICantBelieveIUsedAGoToStatement:
  301.           FillWindow(hwnd->hwndParent,hwnd,hdc,hbr);
  302.         }
  303.  
  304. AbortIconEraseBkGnd:
  305.           if (TestCF(hwnd, CFOWNDC) || TestCF(hwnd, CFCLASSDC))
  306.             InternalReleaseDC(hdc);
  307.  
  308.       return((LONG)TRUE);
  309.  
  310.       case WM_ERASEBKGND:
  311.       if ((hbr = GetBackBrush(hwnd)) != NULL)
  312.         {
  313.           FillWindow(hwnd, hwnd, (HDC)wParam, hbr);
  314.           return((LONG)TRUE);
  315.         }
  316.       break;
  317.  
  318.       case WM_QUERYOPEN:
  319.       case WM_QUERYENDSESSION:
  320.       return((LONG)TRUE);
  321.  
  322.       case WM_SYSCOMMAND:
  323.       SysCommand(hwnd, wParam, lParam);
  324.       break;
  325.  
  326.       case WM_KEYDOWN:
  327.       if (wParam == VK_F10)
  328.           fF10Status = TRUE;
  329.       break;
  330.  
  331.       case WM_SYSKEYDOWN:
  332.       /* Is the ALT key down? */
  333.       if (HIWORD(lParam) & SYS_ALTERNATE)
  334.         {
  335.           /* Toggle the fMenuStatus iff this is NOT a repeat KEYDOWN
  336.            * message; Only if the prev key state was 0, then this is the
  337.            * first KEYDOWN message and then we consider toggling menu 
  338.                * status;
  339.            */
  340.           if((HIWORD(lParam) & SYS_PREVKEYSTATE) == 0)
  341.             {
  342.               /* Don't have to lock hwndActive because it's processing this 
  343.              key. */
  344.               if ((wParam == VK_MENU) && (!fMenuStatus))
  345.               fMenuStatus = TRUE;
  346.               else
  347.               fMenuStatus = FALSE;
  348.         }
  349.  
  350.           fF10Status = FALSE;
  351.  
  352.           DWP_ProcessVirtKey(wParam);
  353.         }
  354.       else
  355.         {
  356.           if (wParam == VK_F10)
  357.           fF10Status = TRUE;
  358.           else
  359.         {
  360.           if (wParam == VK_ESCAPE)
  361.             {
  362.               if(GetKeyState(VK_SHIFT) < 0)
  363.                    SendMessage(hwnd, WM_SYSCOMMAND, 
  364.                        SC_KEYMENU, (DWORD)MENUSYSMENU);
  365.             }
  366.         }
  367.         }
  368.       break;
  369.  
  370.       case WM_KEYUP:
  371.       case WM_SYSKEYUP:
  372.       /* press and release F10 or ALT.Send this only to top-level windows,
  373.        * otherwise MDI gets confused.  The fix in which DefMDIChildProc()
  374.        * passed up the message was insufficient in the case a child window
  375.        * of the MDI child had the focus.
  376.        */
  377.       if ((wParam == VK_MENU && (fMenuStatus == TRUE)) ||
  378.           (wParam == VK_F10 && fF10Status) )
  379.           SendMessage(GetTopLevelWindow(hwnd), WM_SYSCOMMAND, SC_KEYMENU,
  380.           (DWORD)0);
  381.  
  382.       fF10Status = fMenuStatus = FALSE;
  383.       break;
  384.  
  385.       case WM_SYSCHAR:
  386.       /* If syskey is down and we have a char... */
  387.       fMenuStatus = FALSE;
  388.       if ((HIWORD(lParam) & SYS_ALTERNATE) && wParam)
  389.         {
  390.           if (wParam == VK_TAB || wParam == VK_ESCAPE)
  391.           break;
  392.  
  393.           /* Send ALT-SPACE only to top-level windows. */
  394.           if ((wParam == MENUSYSMENU) && (TestwndChild(hwnd)))
  395.           SendMessage(hwnd->hwndParent, message, wParam, lParam);
  396.           else
  397.           SendMessage(hwnd, WM_SYSCOMMAND, SC_KEYMENU, (DWORD)wParam);
  398.         }
  399.       else
  400.           /* Ctrl-Esc produces a WM_SYSCHAR, But should not beep; */
  401.           if(wParam != VK_ESCAPE)
  402.               MessageBeep(0);
  403.       break;
  404.  
  405.       case WM_CHARTOITEM:
  406.       case WM_VKEYTOITEM:
  407.           /* Do default processing for keystrokes into owner draw 
  408.              listboxes. */
  409.           return(-1);
  410.  
  411.  
  412.       case WM_ACTIVATE:
  413.       if (wParam)
  414.           SetFocus(hwnd);
  415.       break;
  416.  
  417.       case WM_SETREDRAW:
  418.       DWP_SetRedraw(hwnd, wParam);
  419.       break;
  420.  
  421.       case WM_SHOWWINDOW:
  422.       /* Non null descriptor implies popup hide or show. */
  423.       /* We should check whether it is a popup window or Owned window */
  424.       if (LOWORD(lParam) != 0 && (TestwndPopup(hwnd) || hwnd -> hwndOwner))
  425.         {
  426.           /* IF NOT(showing, invisible, and not set as hidden) AND
  427.            *   NOT(hiding and not visible)
  428.            */
  429.           if (!(wParam != 0 && !TestWF(hwnd, WFVISIBLE) && 
  430.           !TestWF(hwnd, WFHIDDENPOPUP)) &&
  431.           !(wParam == 0 && !TestWF(hwnd, WFVISIBLE)))
  432.         {
  433.           /* Are we showing? */
  434.           if (wParam)
  435.               /* Yes, clear the hidden popup flag. */
  436.               ClrWF(hwnd, WFHIDDENPOPUP);
  437.           else
  438.               /* No, Set the hidden popup flag. */
  439.               SetWF(hwnd, WFHIDDENPOPUP);
  440.  
  441.           ShowWindow(hwnd, 
  442.                  (wParam ? SHOW_OPENNOACTIVATE : HIDE_WINDOW));
  443.         }
  444.         }
  445.       break;
  446.  
  447.       case WM_CTLCOLOR:
  448.       if (HIWORD(lParam) != CTLCOLOR_SCROLLBAR)
  449.         {
  450.           SetBkColor((HDC)wParam, sysColors.clrWindow);
  451.           SetTextColor((HDC)wParam, sysColors.clrWindowText);
  452.           hbr = sysClrObjects.hbrWindow;
  453.         }
  454.       else
  455.         {
  456.               SetBkColor((HDC)wParam, 0x00ffffff);
  457.               SetTextColor((HDC)wParam, (LONG)0x00000000);
  458.           hbr = sysClrObjects.hbrScrollbar;
  459.           UnrealizeObject(hbr);
  460.         }
  461.  
  462.       return((DWORD)hbr);
  463.  
  464.       case WM_SETCURSOR:
  465.       /* wParam  == hwnd that cursor is over
  466.        * lParamL == Hit test area code (result of WM_NCHITTEST)
  467.        * lParamH == Mouse message number
  468.        */
  469.       if (HIWORD(lParam) != 0 &&
  470.           LOWORD(lParam) >= HTSIZEFIRST &&
  471.           LOWORD(lParam) <= HTSIZELAST)
  472.         {
  473.           SetCursor(rghCursor[LOWORD(lParam) - HTSIZEFIRST + MVSIZEFIRST]);
  474.           break;
  475.         }
  476.  
  477.       if ((hwndT = GetChildParent(hwnd)) != NULL &&
  478.                (BOOL)SendMessage(hwndT, WM_SETCURSOR, wParam, lParam))
  479.           return((LONG)TRUE);
  480.  
  481.       if (HIWORD(lParam) == 0)
  482.         {
  483.           hCurs = hCursNormal;
  484.           SetCursor(hCurs);
  485.         }
  486.       else
  487.         {
  488.           switch (LOWORD(lParam))
  489.         {
  490.           case HTCLIENT:
  491.               if (((HWND)wParam)->pcls->hCursor != NULL)
  492.               SetCursor(((HWND)wParam)->pcls->hCursor);
  493.               break;
  494.  
  495.           case HTERROR:
  496.               switch (HIWORD(lParam))
  497.             {
  498.               case WM_LBUTTONDOWN:
  499.                   if ((hwndT = DWP_GetEnabledPopup(hwnd)) != NULL)
  500.                 {
  501.                   if (hwndT != hwndDesktop->hwndChild)
  502.                     {
  503.                       SetWindowPos(hwnd, NULL,
  504.                            0, 0, 0, 0,
  505.                            SWP_NOMOVE | SWP_NOSIZE | 
  506.                            SWP_NOACTIVATE);
  507.                       SetActiveWindow(hwndT);
  508.                       break;
  509.                     }
  510.                 }
  511.  
  512.                   /*** FALL THRU ***/
  513.  
  514.               case WM_RBUTTONDOWN:
  515.               case WM_MBUTTONDOWN:
  516.                               MessageBeep(0);
  517.                   break;
  518.             }
  519.               /*** FALL THRU ***/
  520.  
  521.           default:
  522.               SetCursor(hCursNormal);
  523.         }
  524.         }
  525.  
  526.       return((LONG)FALSE);
  527.  
  528.       case WM_MOUSEACTIVATE:
  529.       if ((hwndT = GetChildParent(hwnd)) != NULL &&
  530.           (i = (int)SendMessage(hwndT, WM_MOUSEACTIVATE, wParam, lParam))
  531.               != 0)
  532.           return((LONG)i);
  533.  
  534.       /* Moving, sizing or minimizing? Activate AFTER we take action. */
  535.       if (LOWORD(lParam) == HTCAPTION)
  536.           return((LONG)MA_NOACTIVATE);
  537.       else
  538.           return((LONG)MA_ACTIVATE);
  539.  
  540.       case WM_DRAWITEM:
  541.           if (((LPDRAWITEMSTRUCT)lParam)->CtlType == ODT_LISTBOX)
  542.               LBDefaultListboxDrawItem((LPDRAWITEMSTRUCT)lParam);
  543.           break;
  544.  
  545.     }
  546.  
  547.   return(0L);
  548. }
  549.