Release 950727
[wine] / windows / nonclient.c
1 /*
2  * Non-client area window functions
3  *
4  * Copyright 1994 Alexandre Julliard
5  *
6  */
7
8 #include "win.h"
9 #include "class.h"
10 #include "message.h"
11 #include "sysmetrics.h"
12 #include "user.h"
13 #include "shell.h"
14 #include "dialog.h"
15 #include "syscolor.h"
16 #include "menu.h"
17 #include "winpos.h"
18 #include "scroll.h"
19 #include "nonclient.h"
20 #include "graphics.h"
21 #include "selectors.h"
22 #include "stddebug.h"
23 /* #define DEBUG_NONCLIENT */
24 #include "debug.h"
25
26
27 static HBITMAP hbitmapClose = 0;
28 static HBITMAP hbitmapMinimize = 0;
29 static HBITMAP hbitmapMinimizeD = 0;
30 static HBITMAP hbitmapMaximize = 0;
31 static HBITMAP hbitmapMaximizeD = 0;
32 static HBITMAP hbitmapRestore = 0;
33 static HBITMAP hbitmapRestoreD = 0;
34
35 #define SC_ABOUTWINE            (SC_SCREENSAVE+1)
36
37   /* Some useful macros */
38 #define HAS_DLGFRAME(style,exStyle) \
39     (((exStyle) & WS_EX_DLGMODALFRAME) || \
40      (((style) & WS_DLGFRAME) && !((style) & WS_BORDER)))
41
42 #define HAS_THICKFRAME(style) \
43     (((style) & WS_THICKFRAME) && \
44      !(((style) & (WS_DLGFRAME|WS_BORDER)) == WS_DLGFRAME))
45
46 #define HAS_MENU(w)  (!((w)->dwStyle & WS_CHILD) && ((w)->wIDmenu != 0))
47
48 #define ON_LEFT_BORDER(hit) \
49  (((hit) == HTLEFT) || ((hit) == HTTOPLEFT) || ((hit) == HTBOTTOMLEFT))
50 #define ON_RIGHT_BORDER(hit) \
51  (((hit) == HTRIGHT) || ((hit) == HTTOPRIGHT) || ((hit) == HTBOTTOMRIGHT))
52 #define ON_TOP_BORDER(hit) \
53  (((hit) == HTTOP) || ((hit) == HTTOPLEFT) || ((hit) == HTTOPRIGHT))
54 #define ON_BOTTOM_BORDER(hit) \
55  (((hit) == HTBOTTOM) || ((hit) == HTBOTTOMLEFT) || ((hit) == HTBOTTOMRIGHT))
56
57 /***********************************************************************
58  *           NC_AdjustRect
59  *
60  * Compute the size of the window rectangle from the size of the
61  * client rectangle.
62  */
63 static void NC_AdjustRect( LPRECT rect, DWORD style, BOOL menu, DWORD exStyle )
64 {
65     if (style & WS_ICONIC) return;  /* Nothing to change for an icon */
66     if (HAS_DLGFRAME( style, exStyle ))
67         InflateRect( rect, SYSMETRICS_CXDLGFRAME, SYSMETRICS_CYDLGFRAME );
68     else
69     {
70         if (HAS_THICKFRAME(style))
71             InflateRect( rect, SYSMETRICS_CXFRAME, SYSMETRICS_CYFRAME );
72         if (style & WS_BORDER)
73             InflateRect( rect, SYSMETRICS_CXBORDER, SYSMETRICS_CYBORDER );
74     }
75
76     if ((style & WS_CAPTION) == WS_CAPTION)
77         rect->top -= SYSMETRICS_CYCAPTION - SYSMETRICS_CYBORDER;
78     if (menu) rect->top -= SYSMETRICS_CYMENU + SYSMETRICS_CYBORDER;
79
80     if (style & WS_VSCROLL) rect->right  += SYSMETRICS_CXVSCROLL;
81     if (style & WS_HSCROLL) rect->bottom += SYSMETRICS_CYHSCROLL;
82 }
83
84
85 /***********************************************************************
86  *           AdjustWindowRect    (USER.102)
87  */
88 void AdjustWindowRect( LPRECT rect, DWORD style, BOOL menu )
89 {
90     AdjustWindowRectEx( rect, style, menu, 0 );
91 }
92
93
94 /***********************************************************************
95  *           AdjustWindowRectEx    (USER.454)
96  */
97 void AdjustWindowRectEx( LPRECT rect, DWORD style, BOOL menu, DWORD exStyle )
98 {
99       /* Correct the window style */
100
101     if (!(style & (WS_POPUP | WS_CHILD)))  /* Overlapped window */
102         style |= WS_CAPTION;
103     if (exStyle & WS_EX_DLGMODALFRAME) style &= ~WS_THICKFRAME;
104
105     dprintf_nonclient(stddeb, "AdjustWindowRectEx: (%d,%d)-(%d,%d) %08lx %d %08lx\n",
106       rect->left, rect->top, rect->right, rect->bottom, style, menu, exStyle );
107
108     NC_AdjustRect( rect, style, menu, exStyle );
109 }
110
111
112 /*******************************************************************
113  *         NC_GetMinMaxInfo
114  *
115  * Get the minimized and maximized information for a window.
116  */
117 void NC_GetMinMaxInfo( HWND hwnd, POINT *maxSize, POINT *maxPos,
118                        POINT *minTrack, POINT *maxTrack )
119 {
120     HANDLE minmaxHandle;
121     MINMAXINFO MinMax, *pMinMax;
122     short xinc, yinc;
123     WND *wndPtr = WIN_FindWndPtr( hwnd );
124
125       /* Compute default values */
126
127     MinMax.ptMaxSize.x = SYSMETRICS_CXSCREEN;
128     MinMax.ptMaxSize.y = SYSMETRICS_CYSCREEN;
129     MinMax.ptMinTrackSize.x = SYSMETRICS_CXMINTRACK;
130     MinMax.ptMinTrackSize.y = SYSMETRICS_CYMINTRACK;
131     MinMax.ptMaxTrackSize.x = SYSMETRICS_CXSCREEN;
132     MinMax.ptMaxTrackSize.y = SYSMETRICS_CYSCREEN;
133
134     if (HAS_DLGFRAME( wndPtr->dwStyle, wndPtr->dwExStyle ))
135     {
136         xinc = SYSMETRICS_CXDLGFRAME;
137         yinc = SYSMETRICS_CYDLGFRAME;
138     }
139     else
140     {
141         xinc = yinc = 0;
142         if (HAS_THICKFRAME(wndPtr->dwStyle))
143         {
144             xinc += SYSMETRICS_CXFRAME;
145             yinc += SYSMETRICS_CYFRAME;
146         }
147         if (wndPtr->dwStyle & WS_BORDER)
148         {
149             xinc += SYSMETRICS_CXBORDER;
150             yinc += SYSMETRICS_CYBORDER;
151         }
152     }
153     MinMax.ptMaxSize.x += 2 * xinc;
154     MinMax.ptMaxSize.y += 2 * yinc;
155
156     /* Note: The '+' in the following test should really be a ||, but
157      * that would cause gcc-2.7.0 to generate incorrect code.
158      */
159     if ((wndPtr->ptMaxPos.x != -1) + (wndPtr->ptMaxPos.y != -1))
160         MinMax.ptMaxPosition = wndPtr->ptMaxPos;
161     else
162     {
163         MinMax.ptMaxPosition.x = -xinc;
164         MinMax.ptMaxPosition.y = -yinc;
165     }
166
167     minmaxHandle = USER_HEAP_ALLOC( sizeof(MINMAXINFO) );
168     if (minmaxHandle)
169     {
170         pMinMax = (MINMAXINFO *) USER_HEAP_LIN_ADDR( minmaxHandle );
171         memcpy( pMinMax, &MinMax, sizeof(MinMax) );     
172         SendMessage( hwnd, WM_GETMINMAXINFO, 0,
173                      USER_HEAP_SEG_ADDR(minmaxHandle) );
174     }
175     else pMinMax = &MinMax;
176
177       /* Some sanity checks */
178
179     pMinMax->ptMaxTrackSize.x = max( pMinMax->ptMaxTrackSize.x,
180                                      pMinMax->ptMinTrackSize.x );
181     pMinMax->ptMaxTrackSize.y = max( pMinMax->ptMaxTrackSize.y,
182                                      pMinMax->ptMinTrackSize.y );
183     
184     if (maxSize) *maxSize = pMinMax->ptMaxSize;
185     if (maxPos) *maxPos = pMinMax->ptMaxPosition;
186     if (minTrack) *minTrack = pMinMax->ptMinTrackSize;
187     if (maxTrack) *maxTrack = pMinMax->ptMaxTrackSize;
188     if (minmaxHandle) USER_HEAP_FREE( minmaxHandle );
189 }
190
191
192 /***********************************************************************
193  *           NC_HandleNCCalcSize
194  *
195  * Handle a WM_NCCALCSIZE message. Called from DefWindowProc().
196  */
197 LONG NC_HandleNCCalcSize( HWND hwnd, NCCALCSIZE_PARAMS *params )
198 {
199     RECT tmpRect = { 0, 0, 0, 0 };
200     WND *wndPtr = WIN_FindWndPtr( hwnd );    
201
202     if (!wndPtr) return 0;
203     NC_AdjustRect( &tmpRect, wndPtr->dwStyle, FALSE, wndPtr->dwExStyle );
204     params->rgrc[0].left   -= tmpRect.left;
205     params->rgrc[0].top    -= tmpRect.top;
206     params->rgrc[0].right  -= tmpRect.right;
207     params->rgrc[0].bottom -= tmpRect.bottom;
208
209     if (HAS_MENU(wndPtr))
210     {
211         params->rgrc[0].top += MENU_GetMenuBarHeight( hwnd,
212                                   params->rgrc[0].right - params->rgrc[0].left,
213                                   -tmpRect.left, -tmpRect.top ) + 1;
214     }
215     return 0;
216 }
217
218
219 /***********************************************************************
220  *           NC_GetInsideRect
221  *
222  * Get the 'inside' rectangle of a window, i.e. the whole window rectangle
223  * but without the borders (if any).
224  * The rectangle is in window coordinates (for drawing with GetWindowDC()).
225  */
226 void NC_GetInsideRect( HWND hwnd, RECT *rect )
227 {
228     WND * wndPtr = WIN_FindWndPtr( hwnd );
229
230     rect->top    = rect->left = 0;
231     rect->right  = wndPtr->rectWindow.right - wndPtr->rectWindow.left;
232     rect->bottom = wndPtr->rectWindow.bottom - wndPtr->rectWindow.top;
233
234     if (wndPtr->dwStyle & WS_ICONIC) return;  /* No border to remove */
235
236       /* Remove frame from rectangle */
237     if (HAS_DLGFRAME( wndPtr->dwStyle, wndPtr->dwExStyle ))
238     {
239         InflateRect( rect, -SYSMETRICS_CXDLGFRAME, -SYSMETRICS_CYDLGFRAME);
240         if (wndPtr->dwExStyle & WS_EX_DLGMODALFRAME) InflateRect( rect, -1, 0);
241     }
242     else
243     {
244         if (HAS_THICKFRAME( wndPtr->dwStyle ))
245             InflateRect( rect, -SYSMETRICS_CXFRAME, -SYSMETRICS_CYFRAME );
246         if (wndPtr->dwStyle & WS_BORDER)
247             InflateRect( rect, -SYSMETRICS_CXBORDER, -SYSMETRICS_CYBORDER );
248     }
249 }
250
251
252 /***********************************************************************
253  *           NC_HandleNCHitTest
254  *
255  * Handle a WM_NCHITTEST message. Called from DefWindowProc().
256  */
257 LONG NC_HandleNCHitTest( HWND hwnd, POINT pt )
258 {
259     RECT rect;
260     WND *wndPtr = WIN_FindWndPtr( hwnd );
261     if (!wndPtr) return HTERROR;
262
263     dprintf_nonclient(stddeb, "NC_HandleNCHitTest: hwnd=%x pt=%d,%d\n", 
264                       hwnd, pt.x, pt.y );
265
266     GetWindowRect( hwnd, &rect );
267     if (!PtInRect( &rect, pt )) return HTNOWHERE;
268
269     /*
270      * if this is a iconic window, we don't care were the hit
271      * occured, only that it did occur, just return HTCAPTION 
272      * so the caller knows the icon did get hit
273      */
274     if (IsIconic(hwnd))
275     {
276         return HTCAPTION;       /* change this to something meaningful? */
277     }
278
279       /* Check borders */
280     if (HAS_THICKFRAME( wndPtr->dwStyle ))
281     {
282         InflateRect( &rect, -SYSMETRICS_CXFRAME, -SYSMETRICS_CYFRAME );
283         if (wndPtr->dwStyle & WS_BORDER)
284             InflateRect( &rect, -SYSMETRICS_CXBORDER, -SYSMETRICS_CYBORDER );
285         if (!PtInRect( &rect, pt ))
286         {
287               /* Check top sizing border */
288             if (pt.y < rect.top)
289             {
290                 if (pt.x < rect.left+SYSMETRICS_CXSIZE) return HTTOPLEFT;
291                 if (pt.x >= rect.right-SYSMETRICS_CXSIZE) return HTTOPRIGHT;
292                 return HTTOP;
293             }
294               /* Check bottom sizing border */
295             if (pt.y >= rect.bottom)
296             {
297                 if (pt.x < rect.left+SYSMETRICS_CXSIZE) return HTBOTTOMLEFT;
298                 if (pt.x >= rect.right-SYSMETRICS_CXSIZE) return HTBOTTOMRIGHT;
299                 return HTBOTTOM;
300             }
301               /* Check left sizing border */
302             if (pt.x < rect.left)
303             {
304                 if (pt.y < rect.top+SYSMETRICS_CYSIZE) return HTTOPLEFT;
305                 if (pt.y >= rect.bottom-SYSMETRICS_CYSIZE) return HTBOTTOMLEFT;
306                 return HTLEFT;
307             }
308               /* Check right sizing border */
309             if (pt.x >= rect.right)
310             {
311                 if (pt.y < rect.top+SYSMETRICS_CYSIZE) return HTTOPRIGHT;
312                 if (pt.y >= rect.bottom-SYSMETRICS_CYSIZE) return HTBOTTOMRIGHT;
313                 return HTRIGHT;
314             }
315         }
316     }
317     else  /* No thick frame */
318     {
319         if (HAS_DLGFRAME( wndPtr->dwStyle, wndPtr->dwExStyle ))
320             InflateRect(&rect, -SYSMETRICS_CXDLGFRAME, -SYSMETRICS_CYDLGFRAME);
321         else if (wndPtr->dwStyle & WS_BORDER)
322             InflateRect(&rect, -SYSMETRICS_CXBORDER, -SYSMETRICS_CYBORDER);
323         if (!PtInRect( &rect, pt )) return HTBORDER;
324     }
325
326       /* Check caption */
327
328     if ((wndPtr->dwStyle & WS_CAPTION) == WS_CAPTION)
329     {
330         rect.top += SYSMETRICS_CYCAPTION - 1;
331         if (!PtInRect( &rect, pt ))
332         {
333               /* Check system menu */
334             if (wndPtr->dwStyle & WS_SYSMENU)
335                 rect.left += SYSMETRICS_CXSIZE;
336             if (pt.x <= rect.left) return HTSYSMENU;
337               /* Check maximize box */
338             if (wndPtr->dwStyle & WS_MAXIMIZEBOX)
339                 rect.right -= SYSMETRICS_CXSIZE + 1;
340             if (pt.x >= rect.right) return HTMAXBUTTON;
341               /* Check minimize box */
342             if (wndPtr->dwStyle & WS_MINIMIZEBOX)
343                 rect.right -= SYSMETRICS_CXSIZE + 1;
344             if (pt.x >= rect.right) return HTMINBUTTON;
345             return HTCAPTION;
346         }
347     }
348
349       /* Check client area */
350
351     ScreenToClient( hwnd, &pt );
352     GetClientRect( hwnd, &rect );
353     if (PtInRect( &rect, pt )) return HTCLIENT;
354
355       /* Check vertical scroll bar */
356
357     if (wndPtr->dwStyle & WS_VSCROLL)
358     {
359         rect.right += SYSMETRICS_CXVSCROLL;
360         if (PtInRect( &rect, pt )) return HTVSCROLL;
361     }
362
363       /* Check horizontal scroll bar */
364
365     if (wndPtr->dwStyle & WS_HSCROLL)
366     {
367         rect.bottom += SYSMETRICS_CYHSCROLL;
368         if (PtInRect( &rect, pt ))
369         {
370               /* Check size box */
371             if ((wndPtr->dwStyle & WS_VSCROLL) &&
372                 (pt.x >= rect.right - SYSMETRICS_CXVSCROLL))
373                 return HTSIZE;
374             return HTHSCROLL;
375         }
376     }
377
378       /* Check menu bar */
379
380     if (HAS_MENU(wndPtr))
381     {
382         if ((pt.y < 0) && (pt.x >= 0) && (pt.x < rect.right))
383             return HTMENU;
384     }
385
386       /* Should never get here */
387     return HTERROR;
388 }
389
390
391 /***********************************************************************
392  *           NC_DrawSysButton
393  */
394 void NC_DrawSysButton( HWND hwnd, HDC hdc, BOOL down )
395 {
396     RECT rect;
397     HDC hdcMem;
398     HBITMAP hbitmap;
399     WND *wndPtr = WIN_FindWndPtr( hwnd );
400
401     NC_GetInsideRect( hwnd, &rect );
402     hdcMem = CreateCompatibleDC( hdc );
403     hbitmap = SelectObject( hdcMem, hbitmapClose );
404     BitBlt( hdc, rect.left, rect.top, SYSMETRICS_CXSIZE, SYSMETRICS_CYSIZE,
405             hdcMem, (wndPtr->dwStyle & WS_CHILD) ? SYSMETRICS_CXSIZE : 0, 0,
406             down ? NOTSRCCOPY : SRCCOPY );
407     SelectObject( hdcMem, hbitmap );
408     DeleteDC( hdcMem );
409 }
410
411
412 /***********************************************************************
413  *           NC_DrawMaxButton
414  */
415 static void NC_DrawMaxButton( HWND hwnd, HDC hdc, BOOL down )
416 {
417     RECT rect;
418     NC_GetInsideRect( hwnd, &rect );
419     GRAPH_DrawBitmap( hdc, (IsZoomed(hwnd) ?
420                             (down ? hbitmapRestoreD : hbitmapRestore) :
421                             (down ? hbitmapMaximizeD : hbitmapMaximize)),
422                      rect.right - SYSMETRICS_CXSIZE - 1, rect.top,
423                      0, 0, SYSMETRICS_CXSIZE+1, SYSMETRICS_CYSIZE );
424 }
425
426
427 /***********************************************************************
428  *           NC_DrawMinButton
429  */
430 static void NC_DrawMinButton( HWND hwnd, HDC hdc, BOOL down )
431 {
432     RECT rect;
433     WND *wndPtr = WIN_FindWndPtr( hwnd );
434     NC_GetInsideRect( hwnd, &rect );
435     if (wndPtr->dwStyle & WS_MAXIMIZEBOX) rect.right -= SYSMETRICS_CXSIZE + 1;
436     GRAPH_DrawBitmap( hdc, (down ? hbitmapMinimizeD : hbitmapMinimize),
437                      rect.right - SYSMETRICS_CXSIZE - 1, rect.top,
438                      0, 0, SYSMETRICS_CXSIZE+1, SYSMETRICS_CYSIZE );
439 }
440
441
442 /***********************************************************************
443  *           NC_DrawFrame
444  *
445  * Draw a window frame inside the given rectangle, and update the rectangle.
446  * The correct pen for the frame must be selected in the DC.
447  */
448 static void NC_DrawFrame( HDC hdc, RECT *rect, BOOL dlgFrame, BOOL active )
449 {
450     short width, height, tmp;
451
452     if (dlgFrame)
453     {
454         width = SYSMETRICS_CXDLGFRAME - 1;
455         height = SYSMETRICS_CYDLGFRAME - 1;
456         SelectObject( hdc, active ? sysColorObjects.hbrushActiveCaption :
457                                     sysColorObjects.hbrushInactiveCaption );
458     }
459     else
460     {
461         width = SYSMETRICS_CXFRAME - 1;
462         height = SYSMETRICS_CYFRAME - 1;
463         SelectObject( hdc, active ? sysColorObjects.hbrushActiveBorder :
464                                     sysColorObjects.hbrushInactiveBorder );
465     }
466
467       /* Draw frame */
468     PatBlt( hdc, rect->left, rect->top,
469             rect->right - rect->left, height, PATCOPY );
470     PatBlt( hdc, rect->left, rect->top,
471             width, rect->bottom - rect->top, PATCOPY );
472     PatBlt( hdc, rect->left, rect->bottom,
473             rect->right - rect->left, -height, PATCOPY );
474     PatBlt( hdc, rect->right, rect->top,
475             -width, rect->bottom - rect->top, PATCOPY );
476
477     if (dlgFrame)
478     {
479         InflateRect( rect, -width, -height );
480         return;
481     }
482     
483       /* Draw inner rectangle */
484     MoveTo( hdc, rect->left+width, rect->top+height );
485     LineTo( hdc, rect->right-width-1, rect->top+height );
486     LineTo( hdc, rect->right-width-1, rect->bottom-height-1 );
487     LineTo( hdc, rect->left+width, rect->bottom-height-1 );
488     LineTo( hdc, rect->left+width, rect->top+height );
489
490       /* Draw the decorations */
491     tmp = rect->top + SYSMETRICS_CYFRAME + SYSMETRICS_CYSIZE;
492     MoveTo( hdc, rect->left, tmp);
493     LineTo( hdc, rect->left+width, tmp );
494     MoveTo( hdc, rect->right-width-1, tmp );
495     LineTo( hdc, rect->right-1, tmp );
496
497     tmp = rect->bottom - 1 - SYSMETRICS_CYFRAME - SYSMETRICS_CYSIZE;
498     MoveTo( hdc, rect->left, tmp );
499     LineTo( hdc, rect->left+width, tmp );
500     MoveTo( hdc, rect->right-width-1, tmp );
501     LineTo( hdc, rect->right-1, tmp );
502
503     tmp = rect->left + SYSMETRICS_CXFRAME + SYSMETRICS_CXSIZE;
504     MoveTo( hdc, tmp, rect->top );
505     LineTo( hdc, tmp, rect->top+height );
506     MoveTo( hdc, tmp, rect->bottom-height-1 );
507     LineTo( hdc, tmp, rect->bottom-1 );
508
509     tmp = rect->right - 1 - SYSMETRICS_CXFRAME - SYSMETRICS_CYSIZE;
510     MoveTo( hdc, tmp, rect->top );
511     LineTo( hdc, tmp, rect->top+height );
512     MoveTo( hdc, tmp, rect->bottom-height-1 );
513     LineTo( hdc, tmp, rect->bottom-1 );
514
515     InflateRect( rect, -width-1, -height-1 );
516 }
517
518
519 /***********************************************************************
520  *           NC_DrawMovingFrame
521  *
522  * Draw the frame used when moving or resizing window.
523  */
524 static void NC_DrawMovingFrame( HDC hdc, RECT *rect, BOOL thickframe )
525 {
526     if (thickframe)
527     {
528         SelectObject( hdc, GetStockObject( GRAY_BRUSH ) );
529         PatBlt( hdc, rect->left, rect->top,
530                 rect->right - rect->left - SYSMETRICS_CXFRAME,
531                 SYSMETRICS_CYFRAME, PATINVERT );
532         PatBlt( hdc, rect->left, rect->top + SYSMETRICS_CYFRAME,
533                 SYSMETRICS_CXFRAME, 
534                 rect->bottom - rect->top - SYSMETRICS_CYFRAME, PATINVERT );
535         PatBlt( hdc, rect->left + SYSMETRICS_CXFRAME, rect->bottom,
536                 rect->right - rect->left - SYSMETRICS_CXFRAME,
537                 -SYSMETRICS_CYFRAME, PATINVERT );
538         PatBlt( hdc, rect->right, rect->top, -SYSMETRICS_CXFRAME, 
539                 rect->bottom - rect->top - SYSMETRICS_CYFRAME, PATINVERT );
540     }
541     else DrawFocusRect( hdc, rect );
542 }
543
544
545 /***********************************************************************
546  *           NC_DrawCaption
547  *
548  * Draw the window caption.
549  * The correct pen for the window frame must be selected in the DC.
550  */
551 static void NC_DrawCaption( HDC hdc, RECT *rect, HWND hwnd,
552                             DWORD style, BOOL active )
553 {
554     RECT r = *rect;
555     WND * wndPtr = WIN_FindWndPtr( hwnd );
556     char buffer[256];
557
558     if (!hbitmapClose)
559     {
560         if (!(hbitmapClose = LoadBitmap( 0, MAKEINTRESOURCE(OBM_CLOSE) )))
561             return;
562         hbitmapMinimize  = LoadBitmap( 0, MAKEINTRESOURCE(OBM_REDUCE) );
563         hbitmapMinimizeD = LoadBitmap( 0, MAKEINTRESOURCE(OBM_REDUCED) );
564         hbitmapMaximize  = LoadBitmap( 0, MAKEINTRESOURCE(OBM_ZOOM) );
565         hbitmapMaximizeD = LoadBitmap( 0, MAKEINTRESOURCE(OBM_ZOOMD) );
566         hbitmapRestore   = LoadBitmap( 0, MAKEINTRESOURCE(OBM_RESTORE) );
567         hbitmapRestoreD  = LoadBitmap( 0, MAKEINTRESOURCE(OBM_RESTORED) );
568     }
569     
570     if (wndPtr->dwExStyle & WS_EX_DLGMODALFRAME)
571     {
572         HBRUSH hbrushOld = SelectObject( hdc, sysColorObjects.hbrushWindow );
573         PatBlt( hdc, r.left, r.top, 1, r.bottom-r.top+1,PATCOPY );
574         PatBlt( hdc, r.right-1, r.top, 1, r.bottom-r.top+1, PATCOPY );
575         PatBlt( hdc, r.left, r.top-1, r.right-r.left, 1, PATCOPY );
576         r.left++;
577         r.right--;
578         SelectObject( hdc, hbrushOld );
579     }
580
581     MoveTo( hdc, r.left, r.bottom );
582     LineTo( hdc, r.right-1, r.bottom );
583
584     if (style & WS_SYSMENU)
585     {
586         NC_DrawSysButton( hwnd, hdc, FALSE );
587         r.left += SYSMETRICS_CXSIZE + 1;
588         MoveTo( hdc, r.left - 1, r.top );
589         LineTo( hdc, r.left - 1, r.bottom );
590     }
591     if (style & WS_MAXIMIZEBOX)
592     {
593         NC_DrawMaxButton( hwnd, hdc, FALSE );
594         r.right -= SYSMETRICS_CXSIZE + 1;
595     }
596     if (style & WS_MINIMIZEBOX)
597     {
598         NC_DrawMinButton( hwnd, hdc, FALSE );
599         r.right -= SYSMETRICS_CXSIZE + 1;
600     }
601
602     FillRect( hdc, &r, active ? sysColorObjects.hbrushActiveCaption : 
603                                 sysColorObjects.hbrushInactiveCaption );
604
605     if (GetWindowText( hwnd, buffer, 256 ))
606     {
607         if (active) SetTextColor( hdc, GetSysColor( COLOR_CAPTIONTEXT ) );
608         else SetTextColor( hdc, GetSysColor( COLOR_INACTIVECAPTIONTEXT ) );
609         SetBkMode( hdc, TRANSPARENT );
610         DrawText( hdc, buffer, -1, &r, DT_SINGLELINE | DT_CENTER | DT_VCENTER);
611     }
612 }
613
614
615 /***********************************************************************
616  *           NC_DoNCPaint
617  *
618  * Paint the non-client area.
619  */
620 void NC_DoNCPaint( HWND hwnd, BOOL active, BOOL suppress_menupaint )
621 {
622     HDC hdc;
623     RECT rect;
624
625     WND *wndPtr = WIN_FindWndPtr( hwnd );
626
627     dprintf_nonclient(stddeb, "NC_DoNCPaint: %x %d\n", hwnd, active );
628     if (!wndPtr || !(wndPtr->dwStyle & WS_VISIBLE)) return; /* Nothing to do */
629
630     if (!(hdc = GetDCEx( hwnd, 0, DCX_USESTYLE | DCX_WINDOW ))) return;
631
632     /*
633      * If this is an icon, we don't want to do any more nonclient painting
634      * of the window manager.
635      * If there is a class icon to draw, draw it
636      */
637     if (IsIconic(hwnd))
638     {
639         HICON hIcon = WIN_CLASS_INFO(wndPtr).hIcon;
640         if (hIcon)  
641         {
642             SendMessage(hwnd, WM_ICONERASEBKGND, hdc, 0);
643             DrawIcon(hdc, 0, 0, hIcon);
644         }
645         ReleaseDC(hwnd, hdc);
646         return;
647     }
648
649     if (ExcludeVisRect( hdc, wndPtr->rectClient.left-wndPtr->rectWindow.left,
650                         wndPtr->rectClient.top-wndPtr->rectWindow.top,
651                         wndPtr->rectClient.right-wndPtr->rectWindow.left,
652                         wndPtr->rectClient.bottom-wndPtr->rectWindow.top )
653         == NULLREGION)
654     {
655         ReleaseDC( hwnd, hdc );
656         return;
657     }
658
659     rect.top = rect.left = 0;
660     rect.right  = wndPtr->rectWindow.right - wndPtr->rectWindow.left;
661     rect.bottom = wndPtr->rectWindow.bottom - wndPtr->rectWindow.top;
662
663     SelectObject( hdc, sysColorObjects.hpenWindowFrame );
664
665     if ((wndPtr->dwStyle & WS_BORDER) || (wndPtr->dwStyle & WS_DLGFRAME) ||
666         (wndPtr->dwExStyle & WS_EX_DLGMODALFRAME))
667     {
668         MoveTo( hdc, 0, 0 );
669         LineTo( hdc, rect.right-1, 0 );
670         LineTo( hdc, rect.right-1, rect.bottom-1 );
671         LineTo( hdc, 0, rect.bottom-1 );
672         LineTo( hdc, 0, 0 );
673         InflateRect( &rect, -1, -1 );
674     }
675
676     if (HAS_DLGFRAME( wndPtr->dwStyle, wndPtr->dwExStyle )) 
677         NC_DrawFrame( hdc, &rect, TRUE, active );
678     else if (wndPtr->dwStyle & WS_THICKFRAME)
679         NC_DrawFrame(hdc, &rect, FALSE, active );
680
681     if ((wndPtr->dwStyle & WS_CAPTION) == WS_CAPTION)
682     {
683         RECT r = rect;
684         r.bottom = rect.top + SYSMETRICS_CYSIZE;
685         rect.top += SYSMETRICS_CYSIZE + SYSMETRICS_CYBORDER;
686         NC_DrawCaption( hdc, &r, hwnd, wndPtr->dwStyle, active );
687     }
688
689     if (HAS_MENU(wndPtr))
690     {
691         RECT r = rect;
692         r.bottom = rect.top + SYSMETRICS_CYMENU;  /* default height */
693         rect.top += MENU_DrawMenuBar( hdc, &r, hwnd, suppress_menupaint );
694     }
695
696       /* Draw the scroll-bars */
697
698     if (wndPtr->dwStyle & WS_VSCROLL) SCROLL_DrawScrollBar(hwnd, hdc, SB_VERT);
699     if (wndPtr->dwStyle & WS_HSCROLL) SCROLL_DrawScrollBar(hwnd, hdc, SB_HORZ);
700
701       /* Draw the "size-box" */
702
703     if ((wndPtr->dwStyle & WS_VSCROLL) && (wndPtr->dwStyle & WS_HSCROLL))
704     {
705         RECT r = rect;
706         r.left = r.right - SYSMETRICS_CXVSCROLL + 1;
707         r.top  = r.bottom - SYSMETRICS_CYHSCROLL + 1;
708         FillRect( hdc, &r, sysColorObjects.hbrushScrollbar );
709     }    
710
711     ReleaseDC( hwnd, hdc );
712 }
713
714
715
716 /***********************************************************************
717  *           NC_HandleNCPaint
718  *
719  * Handle a WM_NCPAINT message. Called from DefWindowProc().
720  */
721 LONG NC_HandleNCPaint( HWND hwnd )
722 {
723     NC_DoNCPaint( hwnd, hwnd == GetActiveWindow(), FALSE );
724     return 0;
725 }
726
727
728 /***********************************************************************
729  *           NC_HandleNCActivate
730  *
731  * Handle a WM_NCACTIVATE message. Called from DefWindowProc().
732  */
733 LONG NC_HandleNCActivate( HWND hwnd, WORD wParam )
734 {
735     NC_DoNCPaint( hwnd, wParam, FALSE );
736     return TRUE;
737 }
738
739
740 /***********************************************************************
741  *           NC_HandleSetCursor
742  *
743  * Handle a WM_SETCURSOR message. Called from DefWindowProc().
744  */
745 LONG NC_HandleSetCursor( HWND hwnd, WORD wParam, LONG lParam )
746 {
747     if (hwnd != wParam) return 0;  /* Don't set the cursor for child windows */
748
749     switch(LOWORD(lParam))
750     {
751     case HTERROR:
752         {
753             WORD msg = HIWORD( lParam );
754             if ((msg == WM_LBUTTONDOWN) || (msg == WM_MBUTTONDOWN) ||
755                 (msg == WM_RBUTTONDOWN))
756                 MessageBeep(0);
757         }
758         break;
759
760     case HTCLIENT:
761         {
762             WND *wndPtr;
763             CLASS *classPtr;
764             if (!(wndPtr = WIN_FindWndPtr( hwnd ))) break;
765             if (!(classPtr = CLASS_FindClassPtr( wndPtr->hClass ))) break;
766             if (classPtr->wc.hCursor)
767             {
768                 SetCursor( classPtr->wc.hCursor );
769                 return TRUE;
770             }
771             else return FALSE;
772         }
773
774     case HTLEFT:
775     case HTRIGHT:
776         return SetCursor( LoadCursor( 0, IDC_SIZEWE ) );
777
778     case HTTOP:
779     case HTBOTTOM:
780         return SetCursor( LoadCursor( 0, IDC_SIZENS ) );
781
782     case HTTOPLEFT:
783     case HTBOTTOMRIGHT: 
784         return SetCursor( LoadCursor( 0, IDC_SIZENWSE ) );
785
786     case HTTOPRIGHT:
787     case HTBOTTOMLEFT:
788         return SetCursor( LoadCursor( 0, IDC_SIZENESW ) );
789     }
790
791     /* Default cursor: arrow */
792     return SetCursor( LoadCursor( 0, IDC_ARROW ) );
793 }
794
795
796 /***********************************************************************
797  *           NC_StartSizeMove
798  *
799  * Initialisation of a move or resize, when initiatied from a menu choice.
800  * Return hit test code for caption or sizing border.
801  */
802 static LONG NC_StartSizeMove( HWND hwnd, WORD wParam, POINT *capturePoint )
803 {
804     LONG hittest = 0;
805     POINT pt;
806     MSG msg;
807     WND * wndPtr = WIN_FindWndPtr( hwnd );
808
809     if ((wParam & 0xfff0) == SC_MOVE)
810     {
811           /* Move pointer at the center of the caption */
812         RECT rect;
813         NC_GetInsideRect( hwnd, &rect );
814         if (wndPtr->dwStyle & WS_SYSMENU)
815             rect.left += SYSMETRICS_CXSIZE + 1;
816         if (wndPtr->dwStyle & WS_MINIMIZEBOX)
817             rect.right -= SYSMETRICS_CXSIZE + 1;
818         if (wndPtr->dwStyle & WS_MAXIMIZEBOX)
819             rect.right -= SYSMETRICS_CXSIZE + 1;
820         pt.x = wndPtr->rectWindow.left + (rect.right - rect.left) / 2;
821         pt.y = wndPtr->rectWindow.top + rect.top + SYSMETRICS_CYSIZE/2;
822         if (wndPtr->dwStyle & WS_CHILD)
823             ClientToScreen( wndPtr->hwndParent, &pt );
824         hittest = HTCAPTION;
825     }
826     else  /* SC_SIZE */
827     {
828         SetCapture(hwnd);
829         while(!hittest)
830         {
831             MSG_GetHardwareMessage( &msg );
832             switch(msg.message)
833             {
834             case WM_MOUSEMOVE:
835                 hittest = NC_HandleNCHitTest( hwnd, msg.pt );
836                 pt = msg.pt;
837                 if ((hittest < HTLEFT) || (hittest > HTBOTTOMRIGHT))
838                     hittest = 0;
839                 break;
840
841             case WM_LBUTTONUP:
842                 return 0;
843
844             case WM_KEYDOWN:
845                 switch(msg.wParam)
846                 {
847                 case VK_UP:
848                     hittest = HTTOP;
849                     pt.x =(wndPtr->rectWindow.left+wndPtr->rectWindow.right)/2;
850                     pt.y = wndPtr->rectWindow.top + SYSMETRICS_CYFRAME / 2;
851                     break;
852                 case VK_DOWN:
853                     hittest = HTBOTTOM;
854                     pt.x =(wndPtr->rectWindow.left+wndPtr->rectWindow.right)/2;
855                     pt.y = wndPtr->rectWindow.bottom - SYSMETRICS_CYFRAME / 2;
856                     break;
857                 case VK_LEFT:
858                     hittest = HTLEFT;
859                     pt.x = wndPtr->rectWindow.left + SYSMETRICS_CXFRAME / 2;
860                     pt.y =(wndPtr->rectWindow.top+wndPtr->rectWindow.bottom)/2;
861                     break;
862                 case VK_RIGHT:
863                     hittest = HTRIGHT;
864                     pt.x = wndPtr->rectWindow.right - SYSMETRICS_CXFRAME / 2;
865                     pt.y =(wndPtr->rectWindow.top+wndPtr->rectWindow.bottom)/2;
866                     break;
867                 case VK_RETURN:
868                 case VK_ESCAPE: return 0;
869                 }
870             }
871         }
872     }
873     *capturePoint = pt;
874     SetCursorPos( capturePoint->x, capturePoint->y );
875     NC_HandleSetCursor( hwnd, hwnd, MAKELONG( hittest, WM_MOUSEMOVE ));
876     return hittest;
877 }
878
879
880 /***********************************************************************
881  *           NC_DoSizeMove
882  *
883  * Perform SC_MOVE and SC_SIZE commands.
884  */
885 static void NC_DoSizeMove( HWND hwnd, WORD wParam, POINT pt )
886 {
887     MSG msg;
888     LONG hittest;
889     RECT sizingRect, mouseRect;
890     HDC hdc;
891     BOOL thickframe;
892     POINT minTrack, maxTrack, capturePoint = pt;
893     WND * wndPtr = WIN_FindWndPtr( hwnd );
894
895     if (IsZoomed(hwnd) || !IsWindowVisible(hwnd)) return;
896     hittest = wParam & 0x0f;
897     thickframe = HAS_THICKFRAME( wndPtr->dwStyle );
898
899     if ((wParam & 0xfff0) == SC_MOVE)
900     {
901         if (!(wndPtr->dwStyle & WS_CAPTION)) return;
902         if (!hittest) hittest = NC_StartSizeMove( hwnd, wParam, &capturePoint );
903         if (!hittest) return;
904     }
905     else  /* SC_SIZE */
906     {
907         if (!thickframe) return;
908         if (hittest) hittest += HTLEFT-1;
909         else
910         {
911             SetCapture(hwnd);
912             hittest = NC_StartSizeMove( hwnd, wParam, &capturePoint );
913             if (!hittest)
914             {
915                 ReleaseCapture();
916                 return;
917             }
918         }
919     }
920
921       /* Get min/max info */
922
923     NC_GetMinMaxInfo( hwnd, NULL, NULL, &minTrack, &maxTrack );
924     sizingRect = wndPtr->rectWindow;
925     if (wndPtr->dwStyle & WS_CHILD)
926         GetClientRect( wndPtr->hwndParent, &mouseRect );
927     else SetRect( &mouseRect, 0, 0, SYSMETRICS_CXSCREEN, SYSMETRICS_CYSCREEN );
928     if (ON_LEFT_BORDER(hittest))
929     {
930         mouseRect.left  = max( mouseRect.left, sizingRect.right-maxTrack.x );
931         mouseRect.right = min( mouseRect.right, sizingRect.right-minTrack.x );
932     }
933     else if (ON_RIGHT_BORDER(hittest))
934     {
935         mouseRect.left  = max( mouseRect.left, sizingRect.left+minTrack.x );
936         mouseRect.right = min( mouseRect.right, sizingRect.left+maxTrack.x );
937     }
938     if (ON_TOP_BORDER(hittest))
939     {
940         mouseRect.top    = max( mouseRect.top, sizingRect.bottom-maxTrack.y );
941         mouseRect.bottom = min( mouseRect.bottom,sizingRect.bottom-minTrack.y);
942     }
943     else if (ON_BOTTOM_BORDER(hittest))
944     {
945         mouseRect.top    = max( mouseRect.top, sizingRect.top+minTrack.y );
946         mouseRect.bottom = min( mouseRect.bottom, sizingRect.top+maxTrack.y );
947     }
948     SendMessage( hwnd, WM_ENTERSIZEMOVE, 0, 0 );
949
950     if (GetCapture() != hwnd) SetCapture( hwnd );    
951
952     if (wndPtr->dwStyle & WS_CHILD)
953     {
954           /* Retrieve a default cache DC (without using the window style) */
955         hdc = GetDCEx( wndPtr->hwndParent, 0, DCX_CACHE );
956     }
957     else
958     {  /* Grab the server only when moving top-level windows without desktop */
959         hdc = GetDC( 0 );
960         if (rootWindow == DefaultRootWindow(display)) XGrabServer( display );
961     }
962     NC_DrawMovingFrame( hdc, &sizingRect, thickframe );
963
964     while(1)
965     {
966         int dx = 0, dy = 0;
967
968         MSG_GetHardwareMessage( &msg );
969
970           /* Exit on button-up, Return, or Esc */
971         if ((msg.message == WM_LBUTTONUP) ||
972             ((msg.message == WM_KEYDOWN) && 
973              ((msg.wParam == VK_RETURN) || (msg.wParam == VK_ESCAPE)))) break;
974
975         if ((msg.message != WM_KEYDOWN) && (msg.message != WM_MOUSEMOVE))
976             continue;  /* We are not interested in other messages */
977
978         pt = msg.pt;
979         if (wndPtr->dwStyle & WS_CHILD)
980             ScreenToClient( wndPtr->hwndParent, &pt );
981
982         
983         if (msg.message == WM_KEYDOWN) switch(msg.wParam)
984         {
985             case VK_UP:    pt.y -= 8; break;
986             case VK_DOWN:  pt.y += 8; break;
987             case VK_LEFT:  pt.x -= 8; break;
988             case VK_RIGHT: pt.x += 8; break;            
989         }
990
991         pt.x = max( pt.x, mouseRect.left );
992         pt.x = min( pt.x, mouseRect.right );
993         pt.y = max( pt.y, mouseRect.top );
994         pt.y = min( pt.y, mouseRect.bottom );
995
996         dx = pt.x - capturePoint.x;
997         dy = pt.y - capturePoint.y;
998
999         if (dx || dy)
1000         {
1001             if (msg.message == WM_KEYDOWN) SetCursorPos( pt.x, pt.y );
1002             else
1003             {
1004                 RECT newRect = sizingRect;
1005
1006                 if (hittest == HTCAPTION) OffsetRect( &newRect, dx, dy );
1007                 if (ON_LEFT_BORDER(hittest)) newRect.left += dx;
1008                 else if (ON_RIGHT_BORDER(hittest)) newRect.right += dx;
1009                 if (ON_TOP_BORDER(hittest)) newRect.top += dy;
1010                 else if (ON_BOTTOM_BORDER(hittest)) newRect.bottom += dy;
1011                 NC_DrawMovingFrame( hdc, &sizingRect, thickframe );
1012                 NC_DrawMovingFrame( hdc, &newRect, thickframe );
1013                 capturePoint = pt;
1014                 sizingRect = newRect;
1015             }
1016         }
1017     }
1018
1019     NC_DrawMovingFrame( hdc, &sizingRect, thickframe );
1020     ReleaseCapture();
1021     if (wndPtr->dwStyle & WS_CHILD) ReleaseDC( wndPtr->hwndParent, hdc );
1022     else
1023     {
1024         ReleaseDC( 0, hdc );
1025         if (rootWindow == DefaultRootWindow(display)) XUngrabServer( display );
1026     }
1027     SendMessage( hwnd, WM_EXITSIZEMOVE, 0, 0 );
1028
1029       /* If Esc key, don't move the window */
1030     if ((msg.message == WM_KEYDOWN) && (msg.wParam == VK_ESCAPE)) return;
1031
1032     if (hittest != HTCAPTION)
1033         SetWindowPos( hwnd, 0, sizingRect.left, sizingRect.top,
1034                      sizingRect.right - sizingRect.left,
1035                      sizingRect.bottom - sizingRect.top,
1036                      SWP_NOACTIVATE | SWP_NOZORDER );
1037     else SetWindowPos( hwnd, 0, sizingRect.left, sizingRect.top, 0, 0,
1038                       SWP_NOACTIVATE | SWP_NOSIZE | SWP_NOZORDER );
1039 }
1040
1041
1042 /***********************************************************************
1043  *           NC_TrackMinMaxBox
1044  *
1045  * Track a mouse button press on the minimize or maximize box.
1046  */
1047 static void NC_TrackMinMaxBox( HWND hwnd, WORD wParam )
1048 {
1049     MSG msg;
1050     HDC hdc = GetWindowDC( hwnd );
1051     BOOL pressed = TRUE;
1052
1053     SetCapture( hwnd );
1054     if (wParam == HTMINBUTTON) NC_DrawMinButton( hwnd, hdc, TRUE );
1055     else NC_DrawMaxButton( hwnd, hdc, TRUE );
1056
1057     do
1058     {
1059         BOOL oldstate = pressed;
1060         MSG_GetHardwareMessage( &msg );
1061
1062         pressed = (NC_HandleNCHitTest( hwnd, msg.pt ) == wParam);
1063         if (pressed != oldstate)
1064         {
1065             if (wParam == HTMINBUTTON) NC_DrawMinButton( hwnd, hdc, pressed );
1066             else NC_DrawMaxButton( hwnd, hdc, pressed );            
1067         }
1068     } while (msg.message != WM_LBUTTONUP);
1069
1070     if (wParam == HTMINBUTTON) NC_DrawMinButton( hwnd, hdc, FALSE );
1071     else NC_DrawMaxButton( hwnd, hdc, FALSE );
1072
1073     ReleaseCapture();
1074     ReleaseDC( hwnd, hdc );
1075     if (!pressed) return;
1076
1077     if (wParam == HTMINBUTTON) 
1078         SendMessage( hwnd, WM_SYSCOMMAND, SC_MINIMIZE, *(LONG*)&msg.pt );
1079     else
1080         SendMessage( hwnd, WM_SYSCOMMAND, 
1081                   IsZoomed(hwnd) ? SC_RESTORE : SC_MAXIMIZE, *(LONG*)&msg.pt );
1082 }
1083
1084
1085 /***********************************************************************
1086  *           NC_TrackScrollBar
1087  *
1088  * Track a mouse button press on the horizontal or vertical scroll-bar.
1089  */
1090 static void NC_TrackScrollBar( HWND hwnd, WORD wParam, POINT pt )
1091 {
1092     MSG *msg;
1093     HLOCAL hMsg;
1094     WORD scrollbar;
1095     WND *wndPtr = WIN_FindWndPtr( hwnd );
1096
1097     if ((wParam & 0xfff0) == SC_HSCROLL)
1098     {
1099         if ((wParam & 0x0f) != HTHSCROLL) return;
1100         scrollbar = SB_HORZ;
1101     }
1102     else  /* SC_VSCROLL */
1103     {
1104         if ((wParam & 0x0f) != HTVSCROLL) return;
1105         scrollbar = SB_VERT;
1106     }
1107
1108     hMsg = USER_HEAP_ALLOC( sizeof(MSG) );
1109     msg  = (MSG *) USER_HEAP_LIN_ADDR( hMsg );
1110     pt.x -= wndPtr->rectWindow.left;
1111     pt.y -= wndPtr->rectWindow.top;
1112     SetCapture( hwnd );
1113     SCROLL_HandleScrollEvent( hwnd, scrollbar, WM_LBUTTONDOWN, pt );
1114
1115     do
1116     {
1117         GetMessage( USER_HEAP_SEG_ADDR(hMsg), 0, 0, 0 );
1118         switch(msg->message)
1119         {
1120         case WM_LBUTTONUP:
1121         case WM_MOUSEMOVE:
1122         case WM_SYSTIMER:
1123             pt = MAKEPOINT(msg->lParam);
1124             pt.x += wndPtr->rectClient.left - wndPtr->rectWindow.left;
1125             pt.y += wndPtr->rectClient.top - wndPtr->rectWindow.top;
1126             SCROLL_HandleScrollEvent( hwnd, scrollbar, msg->message, pt );
1127             break;
1128         default:
1129             TranslateMessage( msg );
1130             DispatchMessage( msg );
1131             break;
1132         }
1133         if (!IsWindow( hwnd ))
1134         {
1135             ReleaseCapture();
1136             break;
1137         }
1138     } while (msg->message != WM_LBUTTONUP);
1139     USER_HEAP_FREE( hMsg );
1140 }
1141
1142 /***********************************************************************
1143  *           NC_TrackSysMenu
1144  *
1145  * Track a mouse button press on the system menu.
1146  */
1147 static void NC_TrackSysMenu( HWND hwnd, HDC hdc, POINT pt )
1148 {
1149     RECT rect;
1150     WND *wndPtr = WIN_FindWndPtr( hwnd );
1151
1152     if (!(wndPtr->dwStyle & WS_SYSMENU)) return;
1153       /* If window has a menu, track the menu bar normally */
1154     if (HAS_MENU(wndPtr)) MENU_TrackMouseMenuBar( hwnd, pt );
1155     else
1156     {
1157           /* Otherwise track the system menu like a normal popup menu */
1158         NC_GetInsideRect( hwnd, &rect );
1159         OffsetRect( &rect, wndPtr->rectWindow.left, wndPtr->rectWindow.top );
1160         if (wndPtr->dwStyle & WS_CHILD)
1161             ClientToScreen( wndPtr->hwndParent, (POINT *)&rect );
1162         rect.right = rect.left + SYSMETRICS_CXSIZE;
1163         rect.bottom = rect.top + SYSMETRICS_CYSIZE;
1164         NC_DrawSysButton( hwnd, hdc, TRUE );
1165         TrackPopupMenu( wndPtr->hSysMenu, TPM_LEFTALIGN | TPM_LEFTBUTTON,
1166                         rect.left, rect.bottom, 0, hwnd, &rect );
1167         NC_DrawSysButton( hwnd, hdc, FALSE );
1168     }
1169 }
1170
1171
1172 /***********************************************************************
1173  *           NC_HandleNCLButtonDown
1174  *
1175  * Handle a WM_NCLBUTTONDOWN message. Called from DefWindowProc().
1176  */
1177 LONG NC_HandleNCLButtonDown( HWND hwnd, WORD wParam, LONG lParam )
1178 {
1179     HDC hdc = GetWindowDC( hwnd );
1180
1181     switch(wParam)  /* Hit test */
1182     {
1183     case HTCAPTION:
1184         SendMessage( hwnd, WM_SYSCOMMAND, SC_MOVE + HTCAPTION, lParam );
1185         break;
1186
1187     case HTSYSMENU:
1188         NC_TrackSysMenu( hwnd, hdc, MAKEPOINT(lParam) );
1189         break;
1190
1191     case HTMENU:
1192         SendMessage( hwnd, WM_SYSCOMMAND, SC_MOUSEMENU, lParam );
1193         break;
1194
1195     case HTHSCROLL:
1196         SendMessage( hwnd, WM_SYSCOMMAND, SC_HSCROLL + HTHSCROLL, lParam );
1197         break;
1198
1199     case HTVSCROLL:
1200         SendMessage( hwnd, WM_SYSCOMMAND, SC_VSCROLL + HTVSCROLL, lParam );
1201         break;
1202
1203     case HTMINBUTTON:
1204     case HTMAXBUTTON:
1205         NC_TrackMinMaxBox( hwnd, wParam );
1206         break;
1207
1208     case HTLEFT:
1209     case HTRIGHT:
1210     case HTTOP:
1211     case HTTOPLEFT:
1212     case HTTOPRIGHT:
1213     case HTBOTTOM:
1214     case HTBOTTOMLEFT:
1215     case HTBOTTOMRIGHT:
1216         SendMessage( hwnd, WM_SYSCOMMAND, SC_SIZE + wParam - HTLEFT+1, lParam);
1217         break;
1218
1219     case HTBORDER:
1220         break;
1221     }
1222
1223     ReleaseDC( hwnd, hdc );
1224     return 0;
1225 }
1226
1227
1228 /***********************************************************************
1229  *           NC_HandleNCLButtonDblClk
1230  *
1231  * Handle a WM_NCLBUTTONDBLCLK message. Called from DefWindowProc().
1232  */
1233 LONG NC_HandleNCLButtonDblClk( HWND hwnd, WORD wParam, LONG lParam )
1234 {
1235     /*
1236      * if this is an icon, send a restore since we are handling
1237      * a double click
1238      */
1239     if (IsIconic(hwnd))
1240     {
1241       SendMessage(hwnd, WM_SYSCOMMAND, SC_RESTORE, lParam);
1242       return 0;
1243     } 
1244
1245     switch(wParam)  /* Hit test */
1246     {
1247     case HTCAPTION:
1248         SendMessage( hwnd, WM_SYSCOMMAND,
1249                      IsZoomed(hwnd) ? SC_RESTORE : SC_MAXIMIZE, lParam );
1250         break;
1251
1252     case HTSYSMENU:
1253         SendMessage( hwnd, WM_SYSCOMMAND, SC_CLOSE, lParam );
1254         break;
1255     }
1256     return 0;
1257 }
1258
1259
1260 /***********************************************************************
1261  *           NC_HandleSysCommand
1262  *
1263  * Handle a WM_SYSCOMMAND message. Called from DefWindowProc().
1264  */
1265 LONG NC_HandleSysCommand( HWND hwnd, WORD wParam, POINT pt )
1266 {
1267     WND *wndPtr = WIN_FindWndPtr( hwnd );
1268
1269     dprintf_nonclient(stddeb, "Handling WM_SYSCOMMAND %x %d,%d\n", 
1270                       wParam, pt.x, pt.y );
1271
1272     if (wndPtr->dwStyle & WS_CHILD) ScreenToClient( wndPtr->hwndParent, &pt );
1273
1274     switch (wParam & 0xfff0)
1275     {
1276     case SC_SIZE:
1277     case SC_MOVE:
1278         NC_DoSizeMove( hwnd, wParam, pt );
1279         break;
1280
1281     case SC_MINIMIZE:
1282         ShowWindow( hwnd, SW_MINIMIZE ); 
1283         break;
1284
1285     case SC_MAXIMIZE:
1286         ShowWindow( hwnd, SW_MAXIMIZE );
1287         break;
1288
1289     case SC_RESTORE:
1290         ShowWindow( hwnd, SW_RESTORE );
1291         break;
1292
1293     case SC_NEXTWINDOW:
1294     case SC_PREVWINDOW:
1295         break;
1296
1297     case SC_CLOSE:
1298         return SendMessage( hwnd, WM_CLOSE, 0, 0 );
1299
1300     case SC_VSCROLL:
1301     case SC_HSCROLL:
1302         NC_TrackScrollBar( hwnd, wParam, pt );
1303         break;
1304
1305     case SC_MOUSEMENU:
1306         MENU_TrackMouseMenuBar( hwnd, pt );
1307         break;
1308
1309     case SC_KEYMENU:
1310         MENU_TrackKbdMenuBar( hwnd, wParam );
1311         break;
1312         
1313     case SC_ARRANGE:
1314         break;
1315
1316     case SC_TASKLIST:
1317         WinExec( "taskman.exe", SW_SHOWNORMAL ); 
1318         break;
1319
1320     case SC_HOTKEY:
1321         break;
1322
1323     case SC_SCREENSAVE:
1324         if (wParam == SC_ABOUTWINE)
1325         {   
1326           extern const char people[];
1327           ShellAbout(hwnd,"WINE",people,0);
1328         }
1329         break;
1330     }
1331     return 0;
1332 }