home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c480 / 18.ddi / SAMPLES / DEFPROCS / DEFWND.C_ / DEFWND.C
Encoding:
Text File  |  1993-02-08  |  12.0 KB  |  457 lines

  1. /*--------------------------------------------------------------------------*/
  2. /*                                                                          */
  3. /*  DefWindowProc() -                                                       */
  4. /*                                                                          */
  5. /*--------------------------------------------------------------------------*/
  6.  
  7. LRESULT API IDefWindowProc(HWND hwnd, WORD message, WPARAM wParam, LPARAM lParam)
  8. {
  9.     int     i;
  10.     HBRUSH  hbr;
  11.     HWND    hwndT;
  12.  
  13.     switch (message)
  14.     {
  15.     case WM_NCCREATE:
  16.     // If WS_HSCROLL or WS_VSCROLL, initialize storage for scroll positions.
  17.     //
  18.     // NOTE: Scroll bar storage and text storage will be freed automatically
  19.     // by Windows during CreateWindow()
  20.     //
  21.     if (TestWF(hwnd, (WFHSCROLL | WFVSCROLL)))
  22.     {
  23.         // Initialize extra storage for
  24.  
  25.         if (InitPwSB(hwnd) == NULL)
  26.         return((LONG)FALSE);
  27.     }
  28.     // Store window text if present.
  29.     //
  30.     return((LRESULT)(LONG)DefSetText(hwnd, ((LPCREATESTRUCT)lParam)->lpszName));
  31.  
  32.     case WM_NCCALCSIZE:
  33.     //
  34.     // wParam = fCalcValidRects
  35.     // lParam = LPRECT rgrc[3]:
  36.     //        lprc[0] = rcWindowNew = New window rectangle
  37.     //    if fCalcValidRects:
  38.     //        lprc[1] = rcWindowOld = Old window rectangle
  39.     //        lprc[2] = rcClientOld = Old client rectangle
  40.     //
  41.     // On return:
  42.     //        rgrc[0] = rcClientNew = New client rect
  43.     //    if fCalcValidRects:
  44.     //        rgrc[1] = rcValidDst  = Destination valid rectangle
  45.     //        rgrc[2] = rcValidSrc  = Source valid rectangle
  46.     //
  47.     CalcClientRect(hwnd, (LPRECT)lParam);
  48.     break;
  49.  
  50.     case WM_NCHITTEST:
  51.     //
  52.     // Determine what area the passed coordinate is in.
  53.     //
  54.     return((LRESULT)(DWORD)FindNCHit(hwnd, (LONG)lParam));
  55.  
  56.     case WM_NCPAINT:
  57.     //
  58.     // Do non-client area drawing.
  59.     //
  60.     DWP_DoNCPaint(hwnd, (HRGN)wParam);
  61.     break;
  62.  
  63.     case WM_NCACTIVATE:
  64.     //
  65.     // Do non-client drawing in response to
  66.     // activation or deactivation.
  67.     //
  68.     DWP_DoNCActivate(hwnd, (BOOL)wParam);
  69.     return (LRESULT)(DWORD)TRUE;
  70.  
  71.     case WM_CANCELMODE:
  72.     //
  73.     // Terminate any modes the system might
  74.     // be in, such as scrollbar tracking, menu mode,
  75.     // button capture, etc.
  76.     //
  77.     DWP_DoCancelMode(hwnd);
  78.     break;
  79.  
  80.     case WM_SETTEXT:
  81.     // Set the new text and redraw the caption or icon title window.
  82.     //
  83.     DefSetText(hwnd, (LPCSTR)lParam);
  84.     DWP_RedrawTitle(hwnd);
  85.     break;
  86.  
  87.     case WM_GETTEXT:
  88.     //
  89.     // If the buffer size is > 0, copy as much of the window text as
  90.     // will fit (making sure to zero terminate the result).
  91.     //
  92.     if (wParam)
  93.     {
  94.         if (hwnd->hName)
  95.         return (LRESULT)(LONG)TextCopy(hwnd->hName, (LPSTR)lParam, (int)wParam);
  96.  
  97.         // No string: make sure we return an empty string.
  98.         //
  99.         ((LPSTR)lParam)[0] = 0;
  100.     }
  101.     return (0L);
  102.  
  103.     case WM_GETTEXTLENGTH:
  104.     //
  105.     // Just return the length of the window text (excluding 0 terminator)
  106.     //
  107.     if (hwnd->hName)
  108.         return((LRESULT)(LONG)lstrlen(TextPointer(hwnd->hName)));
  109.     return(0L);
  110.  
  111.     case WM_PAINT:
  112.     case WM_PAINTICON:
  113.     DWP_Paint(message, hwnd);
  114.     break;
  115.  
  116.     case WM_ERASEBKGND:
  117.     case WM_ICONERASEBKGND:
  118.     return (LRESULT)(LONG)DWP_EraseBkgnd(hwnd, message, (HDC)wParam);
  119.  
  120.     case WM_SYNCPAINT:
  121.     //
  122.     // This message is sent when SetWindowPos() is trying
  123.     // to get the screen looking nice after window rearrangement,
  124.     // and one of the windows involved is of another task.
  125.     // This message avoids lots of inter-app message traffic
  126.     // by switching to the other task and continuing the
  127.     // recursion there.
  128.     //
  129.     // wParam         = flags
  130.     // LOWORD(lParam) = hrgnClip
  131.     // HIWORD(lParam) = hwndSkip  (not used; always NULL)
  132.     //
  133.     // hwndSkip is now always NULL.
  134.     //
  135.     // NOTE: THIS MESSAGE IS FOR INTERNAL USE ONLY! ITS BEHAVIOR
  136.     // IS DIFFERENT IN 3.1 THAN IN 3.0!!
  137.     //
  138.     DoSyncPaint(hwnd, NULL, ((WORD)wParam | DSP_WM_SYNCPAINT));
  139.     break;
  140.  
  141.     case WM_SYSCOMMAND:
  142.     SysCommand(hwnd, (int)wParam, lParam);
  143.     break;
  144.  
  145.     case WM_ACTIVATE:
  146.     //
  147.     // By default, windows set the focus to themselves when activated.
  148.     //
  149.     if ((BOOL)wParam)
  150.         SetFocus(hwnd);
  151.     break;
  152.  
  153.     case WM_SETREDRAW:
  154.     //
  155.     // Set or clear the WS_VISIBLE bit, without invalidating the window.
  156.     // (Also performs some internal housekeeping to ensure that window
  157.     // DC clip regions are computed correctly).
  158.     //
  159.     DWP_SetRedraw(hwnd, (BOOL)wParam);
  160.     break;
  161.  
  162.     case WM_WINDOWPOSCHANGING:
  163.     //
  164.     // If the window's size is changing, and the window has
  165.     // a size border (WS_THICKFRAME) or is a main window (WS_OVERLAPPED),
  166.     // then adjust the new width and height by sending a WM_MINMAXINFO message.
  167.     //
  168.     #define ppos ((WINDOWPOS FAR *)lParam)
  169.     if (!(ppos->flags & SWP_NOSIZE))
  170.         AdjustSize(hwnd, &ppos->cx, &ppos->cy);
  171.     #undef ppos
  172.     break;
  173.  
  174.     case WM_WINDOWPOSCHANGED:
  175.     //
  176.     // If (!(lpswp->flags & SWP_NOCLIENTMOVE)
  177.     //    send WM_MOVE message
  178.     //
  179.     // If (!(lpswp->flags & SWP_NOCLIENTSIZE)
  180.     //    send WM_SIZE message with wParam set based on
  181.     //    current WS_MINIMIZED/WS_MAXIMIZED style.
  182.     //
  183.     // If DefWindowProc() is not called, WM_MOVE and WM_SIZE messages
  184.     // will not be sent to the window.
  185.     //
  186.     HandleWindowPosChanged(hwnd, (WINDOWPOS FAR *)lParam);
  187.     break;
  188.  
  189.     case WM_CTLCOLOR:
  190.     //
  191.     // Set up the supplied DC with the foreground and background
  192.     // colors we want to use in the control, and return a brush
  193.     // to use for filling.
  194.     //
  195.     switch (HIWORD(lParam))
  196.     {
  197.     case CTLCOLOR_SCROLLBAR:
  198.         //
  199.         // Background = white
  200.         // Foreground = black
  201.         // brush = COLOR_SCROLLBAR.
  202.         //
  203.         SetBkColor((HDC)wParam, RGB(255, 255, 255));
  204.         SetTextColor((HDC)wParam, RGB(0, 0, 0));
  205.         hbr = sysClrObjects.hbrScrollbar;
  206.  
  207.         // The scroll bar color may be dithered, so unrealize it.
  208.         //
  209.         UnrealizeObject(hbr);
  210.         break;
  211.  
  212.     default:
  213.         //
  214.         // Background = COLOR_WINDOW
  215.         // Foreground = COLOR_WINDOWTEXT
  216.         // Brush = COLOR_WINDOW
  217.         //
  218.         SetBkColor((HDC)wParam, sysColors.clrWindow);
  219.         SetTextColor((HDC)wParam, sysColors.clrWindowText);
  220.         hbr = sysClrObjects.hbrWindow;
  221.     }
  222.     return((LRESULT)(DWORD)(WORD)hbr);
  223.  
  224.     case WM_SETCURSOR:
  225.     //
  226.     // wParam  == hwndHit == hwnd that cursor is over
  227.     // lParamL == codeHT  == Hit test area code (result of WM_NCHITTEST)
  228.     // lParamH == msg     == Mouse message number (may be 0)
  229.     //
  230.     // Strategy: First forward WM_SETCURSOR message to parent.  If it
  231.     // returns TRUE (i.e., it set the cursor), just return.  Otherwise,
  232.     // set the cursor based on codeHT and msg.
  233.     //
  234.     return (LRESULT)(LONG)DWP_SetCursor(hwnd, (HWND)wParam,
  235.         LOWORD(lParam), HIWORD(lParam));
  236.  
  237.     case WM_MOUSEACTIVATE:
  238.     //
  239.     // First give the parent a chance to process the message.
  240.     //
  241.     hwndT = GetChildParent(hwnd);
  242.     if (hwndT)
  243.     {
  244.         i = (int)(DWORD)SendMessage(hwndT, WM_MOUSEACTIVATE,
  245.         wParam, lParam);
  246.  
  247.         if (i != 0)
  248.         return (LRESULT)(LONG)i;
  249.     }
  250.  
  251.     // If the user clicked in the title bar, don't activate now:
  252.     // the activation will take place later when the move or size
  253.     // occurs.
  254.     //
  255.     if (LOWORD(lParam) == HTCAPTION)
  256.         return((LRESULT)(LONG)MA_NOACTIVATE);
  257.  
  258.     return((LRESULT)(LONG)MA_ACTIVATE);
  259.  
  260.     case WM_SHOWWINDOW:
  261.     //
  262.     // If we are being called because our owner window is being shown,
  263.     // hidden, minimized, or un-minimized, then we must hide or show
  264.     // show ourself as appropriate.
  265.     //
  266.     // This behavior occurs for popup windows or owned windows only.
  267.     // It's not designed for use with child windows.
  268.     //
  269.     if (LOWORD(lParam) != 0 && (TestwndPopup(hwnd) || hwnd->hwndOwner))
  270.     {
  271.         // The WFHIDDENPOPUP flag is an internal flag that indicates
  272.         // that the window was hidden because its owner was hidden.
  273.         // This way we only show windows that were hidden by this code,
  274.         // not intentionally by the application.
  275.         //
  276.         // Go ahead and hide or show this window, but only if:
  277.         //
  278.         // a) we need to be hidden, or
  279.         // b) we need to be shown, and we were hidden by
  280.         //    an earlier WM_SHOWWINDOW message
  281.         //
  282.         if ((!wParam && TestWF(hwnd, WFVISIBLE)) ||
  283.            (wParam && !TestWF(hwnd, WFVISIBLE) && TestWF(hwnd, WFHIDDENPOPUP)))
  284.         {
  285.         // Remember that we were hidden by WM_SHOWWINDOW processing
  286.         //
  287.         ClrWF(hwnd, WFHIDDENPOPUP);
  288.         if (!wParam)
  289.             SetWF(hwnd, WFHIDDENPOPUP);
  290.  
  291.         ShowWindow(hwnd, (wParam ? SW_SHOWNOACTIVATE : SW_HIDE));
  292.         }
  293.     }
  294.     break;
  295.  
  296.     case WM_NCLBUTTONDOWN:
  297.     case WM_NCLBUTTONUP:
  298.     case WM_NCLBUTTONDBLCLK:
  299.     case WM_NCMOUSEMOVE:
  300.     //
  301.     // Deal with mouse messages in the non-client area.
  302.     //
  303.     DWP_NCMouse(hwnd, message, wParam, lParam);
  304.     break;
  305.  
  306.     case WM_KEYDOWN:
  307.     // Windows 2.0 backward compatibility:
  308.     // Alias F10 to the menu key
  309.     // (only for apps that don't handle WM_KEY* messages themselves)
  310.     //
  311.     if ((WORD)wParam == VK_F10)
  312.         fF10Status = TRUE;
  313.     break;
  314.  
  315.     case WM_SYSKEYDOWN:
  316.     // Is the ALT key down?
  317.     if (HIWORD(lParam) & SYS_ALTERNATE)
  318.     {
  319.         // Toggle only if this is not an autorepeat key
  320.         //
  321.         if ((HIWORD(lParam) & SYS_PREVKEYSTATE) == 0)
  322.         {
  323.         if (((WORD)wParam == VK_MENU) && (!fMenuStatus))
  324.             fMenuStatus = TRUE;
  325.         else
  326.             fMenuStatus = FALSE;
  327.         }
  328.  
  329.         fF10Status = FALSE;
  330.  
  331.         DWP_ProcessVirtKey((WORD)wParam);
  332.     }
  333.     else
  334.     {
  335.         if ((WORD)wParam == VK_F10)
  336.         {
  337.         fF10Status = TRUE;
  338.         }
  339.         else
  340.         {
  341.         if ((WORD)wParam == VK_ESCAPE)
  342.         {
  343.             if (GetKeyState(VK_SHIFT) < 0)
  344.             {
  345.             SendMessage(hwnd, WM_SYSCOMMAND,
  346.                 (WPARAM)SC_KEYMENU, (LPARAM)(DWORD)MENUSYSMENU);
  347.             }
  348.         }
  349.         }
  350.     }
  351.     break;
  352.  
  353.     case WM_KEYUP:
  354.     case WM_SYSKEYUP:
  355.     // Press and release F10 or ALT. Send this only to top-level
  356.     // windows, otherwise MDI gets confused.  The fix in which
  357.     // DefMDIChildProc() passed up the message was insufficient in the
  358.     // case a child window of the MDI child had the focus.
  359.     //
  360.     if ( ((WORD)wParam == VK_MENU && (fMenuStatus == TRUE)) ||
  361.         ((WORD)wParam == VK_F10 && fF10Status) )
  362.     {
  363.         SendMessage(GetTopLevelWindow(hwnd), WM_SYSCOMMAND,
  364.             (WPARAM)SC_KEYMENU, 0L);
  365.     }
  366.     fF10Status = fMenuStatus = FALSE;
  367.     break;
  368.  
  369.     case WM_SYSCHAR:
  370.     // If syskey is down and we have a char... */
  371.     fMenuStatus = FALSE;
  372.  
  373.     if ((WORD)wParam == VK_RETURN && TestWF(hwnd, WFMINIMIZED))
  374.     {
  375.         // If the window is iconic and user hits RETURN, we want to
  376.         // restore this window.
  377.         //
  378.         PostMessage(hwnd, WM_SYSCOMMAND, (WPARAM)SC_RESTORE, 0L);
  379.         break;
  380.     }
  381.  
  382.     if ((HIWORD(lParam) & SYS_ALTERNATE) && wParam)
  383.     {
  384.         if ((WORD)wParam == VK_TAB || (WORD)wParam == VK_ESCAPE)
  385.         break;
  386.  
  387.         // Send ALT-SPACE only to top-level windows.
  388.         if (((WORD)wParam == MENUSYSMENU) && (TestwndChild(hwnd)))
  389.         SendMessage(hwnd->hwndParent, message, wParam, lParam);
  390.         else
  391.         SendMessage(hwnd, WM_SYSCOMMAND, (WPARAM)SC_KEYMENU, (LPARAM)(DWORD)(WORD)wParam);
  392.     }
  393.     else
  394.     {
  395.         // Ctrl-Esc produces a WM_SYSCHAR, but should not beep
  396.         if ((WORD)wParam != VK_ESCAPE)
  397.         MessageBeep(0);
  398.     }
  399.     break;
  400.  
  401.     case WM_CLOSE:
  402.     //
  403.     // Default WM_CLOSE handling is to destroy the window.
  404.     //
  405.     DestroyWindow(hwnd);
  406.     break;
  407.  
  408.     case WM_QUERYOPEN:
  409.     case WM_QUERYENDSESSION:
  410.     return((LRESULT)(LONG)TRUE);
  411.  
  412.     case WM_ISACTIVEICON:
  413.     return ((LRESULT)(DWORD)(BOOL)(TestWF(hwnd, WFFRAMEON) != 0));
  414.  
  415.     case WM_CHARTOITEM:
  416.     case WM_VKEYTOITEM:
  417.     //
  418.     // Return -1 to cause default processing
  419.     //
  420.     return((LRESULT)-1L);
  421.  
  422.     case WM_DRAWITEM:
  423.     #define lpdis   ((LPDRAWITEMSTRUCT)lParam)
  424.     if (lpdis->CtlType == ODT_LISTBOX)
  425.         LBDefaultListboxDrawItem(lpdis);
  426.     #undef  lpdis
  427.     break;
  428.  
  429.     case WM_GETHOTKEY:
  430.     return((LRESULT)(LONG)DWP_GetHotKey(hwnd));
  431.     break;
  432.  
  433.     case WM_SETHOTKEY:
  434.     return((LRESULT)(LONG)SetHotKey(hwnd, (WORD)wParam));
  435.     break;
  436.  
  437.     case WM_QUERYDRAGICON:
  438.     return((LRESULT)(DWORD)(WORD)DWP_QueryDragIcon(hwnd));
  439.     break;
  440.  
  441.     case WM_QUERYDROPOBJECT:
  442.     //
  443.     // If the application is WS_EX_ACCEPTFILES, return TRUE.
  444.     //
  445.     if (TestWF(hwnd, WEFACCEPTFILES))
  446.         return (LRESULT)(DWORD)TRUE;
  447.  
  448.     return (LRESULT)(DWORD)FALSE;
  449.  
  450.     case WM_DROPOBJECT:
  451.     return (LRESULT)(DWORD)DO_DROPFILE;
  452.  
  453.     }   // end switch
  454.     return(0L);
  455. }
  456.  
  457.