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