winmultiwindowwndproc.c 36.9 KB
Newer Older
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
1
2
/*
 *Copyright (C) 1994-2000 The XFree86 Project, Inc. All Rights Reserved.
3
 *Copyright (C) Colin Harrison 2005-2008
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
 *
 *Permission is hereby granted, free of charge, to any person obtaining
 * a copy of this software and associated documentation files (the
 *"Software"), to deal in the Software without restriction, including
 *without limitation the rights to use, copy, modify, merge, publish,
 *distribute, sublicense, and/or sell copies of the Software, and to
 *permit persons to whom the Software is furnished to do so, subject to
 *the following conditions:
 *
 *The above copyright notice and this permission notice shall be
 *included in all copies or substantial portions of the Software.
 *
 *THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 *EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 *MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 *NONINFRINGEMENT. IN NO EVENT SHALL THE XFREE86 PROJECT BE LIABLE FOR
 *ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
 *CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 *WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 *
 *Except as contained in this notice, the name of the XFree86 Project
 *shall not be used in advertising or otherwise to promote the sale, use
 *or other dealings in this Software without prior written authorization
 *from the XFree86 Project.
 *
 * Authors:	Kensuke Matsuzaki
 *		Earle F. Philhower, III
 *		Harold L Hunt II
32
 *              Colin Harrison
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
33
34
 */

35
36
37
#ifdef HAVE_XWIN_CONFIG_H
#include <xwin-config.h>
#endif
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
38
39
40
41
#include "win.h"
#include "dixevents.h"
#include "winmultiwindowclass.h"
#include "winprefs.h"
Alexander Gottwald's avatar
Alexander Gottwald committed
42
#include "winmsg.h"
Colin Harrison's avatar
Colin Harrison committed
43
#include "inputstr.h"
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
44

45
extern void winUpdateWindowPosition(HWND hWnd, HWND * zstyle);
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
46
47
48
49
50

/*
 * Local globals
 */

51
static UINT_PTR g_uipMousePollingTimerID = 0;
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
52
53
54
55
56
57
58
59
60
61
62
63

/*
 * Constant defines
 */

#define WIN_MULTIWINDOW_SHAPE		YES

/*
 * ConstrainSize - Taken from TWM sources - Respects hints for sizing
 */
#define makemult(a,b) ((b==1) ? (a) : (((int)((a)/(b))) * (b)) )
static void
64
ConstrainSize(WinXSizeHints hints, int *widthp, int *heightp)
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
65
{
66
67
68
69
70
71
72
    int minWidth, minHeight, maxWidth, maxHeight, xinc, yinc, delta;
    int baseWidth, baseHeight;
    int dwidth = *widthp, dheight = *heightp;

    if (hints.flags & PMinSize) {
        minWidth = hints.min_width;
        minHeight = hints.min_height;
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
73
    }
74
75
76
    else if (hints.flags & PBaseSize) {
        minWidth = hints.base_width;
        minHeight = hints.base_height;
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
77
    }
78
79
80
81
82
83
    else
        minWidth = minHeight = 1;

    if (hints.flags & PBaseSize) {
        baseWidth = hints.base_width;
        baseHeight = hints.base_height;
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
84
    }
85
86
87
88
89
90
    else if (hints.flags & PMinSize) {
        baseWidth = hints.min_width;
        baseHeight = hints.min_height;
    }
    else
        baseWidth = baseHeight = 0;
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
91

92
93
94
    if (hints.flags & PMaxSize) {
        maxWidth = hints.max_width;
        maxHeight = hints.max_height;
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
95
    }
96
97
98
    else {
        maxWidth = MAXINT;
        maxHeight = MAXINT;
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
99
100
    }

101
102
103
    if (hints.flags & PResizeInc) {
        xinc = hints.width_inc;
        yinc = hints.height_inc;
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
104
    }
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
    else
        xinc = yinc = 1;

    /*
     * First, clamp to min and max values
     */
    if (dwidth < minWidth)
        dwidth = minWidth;
    if (dheight < minHeight)
        dheight = minHeight;

    if (dwidth > maxWidth)
        dwidth = maxWidth;
    if (dheight > maxHeight)
        dheight = maxHeight;

    /*
     * Second, fit to base + N * inc
     */
    dwidth = ((dwidth - baseWidth) / xinc * xinc) + baseWidth;
    dheight = ((dheight - baseHeight) / yinc * yinc) + baseHeight;

    /*
     * Third, adjust for aspect ratio
     */

    /*
     * The math looks like this:
     *
     * minAspectX    dwidth     maxAspectX
     * ---------- <= ------- <= ----------
     * minAspectY    dheight    maxAspectY
     *
     * If that is multiplied out, then the width and height are
     * invalid in the following situations:
     *
     * minAspectX * dheight > minAspectY * dwidth
     * maxAspectX * dheight < maxAspectY * dwidth
     * 
     */

    if (hints.flags & PAspect) {
        if (hints.min_aspect.x * dheight > hints.min_aspect.y * dwidth) {
            delta =
                makemult(hints.min_aspect.x * dheight / hints.min_aspect.y -
                         dwidth, xinc);
            if (dwidth + delta <= maxWidth)
                dwidth += delta;
            else {
                delta =
                    makemult(dheight -
                             dwidth * hints.min_aspect.y / hints.min_aspect.x,
                             yinc);
                if (dheight - delta >= minHeight)
                    dheight -= delta;
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
160
161
            }
        }
162
163
164
165
166
167
168
169
170
171
172
173
174
175

        if (hints.max_aspect.x * dheight < hints.max_aspect.y * dwidth) {
            delta =
                makemult(dwidth * hints.max_aspect.y / hints.max_aspect.x -
                         dheight, yinc);
            if (dheight + delta <= maxHeight)
                dheight += delta;
            else {
                delta =
                    makemult(dwidth -
                             hints.max_aspect.x * dheight / hints.max_aspect.y,
                             xinc);
                if (dwidth - delta >= minWidth)
                    dwidth -= delta;
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
176
177
178
179
            }
        }
    }

180
181
182
183
    /* Return computed values */
    *widthp = dwidth;
    *heightp = dheight;
}
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
184

185
#undef makemult
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
186
187
188
189
190

/*
 * ValidateSizing - Ensures size request respects hints
 */
static int
191
ValidateSizing(HWND hwnd, WindowPtr pWin, WPARAM wParam, LPARAM lParam)
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
192
{
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
    WinXSizeHints sizeHints;
    RECT *rect;
    int iWidth, iHeight;
    RECT rcClient, rcWindow;
    int iBorderWidthX, iBorderWidthY;

    /* Invalid input checking */
    if (pWin == NULL || lParam == 0)
        return FALSE;

    /* No size hints, no checking */
    if (!winMultiWindowGetWMNormalHints(pWin, &sizeHints))
        return FALSE;

    /* Avoid divide-by-zero */
    if (sizeHints.flags & PResizeInc) {
        if (sizeHints.width_inc == 0)
            sizeHints.width_inc = 1;
        if (sizeHints.height_inc == 0)
            sizeHints.height_inc = 1;
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
213
    }
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262

    rect = (RECT *) lParam;

    iWidth = rect->right - rect->left;
    iHeight = rect->bottom - rect->top;

    /* Now remove size of any borders and title bar */
    GetClientRect(hwnd, &rcClient);
    GetWindowRect(hwnd, &rcWindow);
    iBorderWidthX =
        (rcWindow.right - rcWindow.left) - (rcClient.right - rcClient.left);
    iBorderWidthY =
        (rcWindow.bottom - rcWindow.top) - (rcClient.bottom - rcClient.top);
    iWidth -= iBorderWidthX;
    iHeight -= iBorderWidthY;

    /* Constrain the size to legal values */
    ConstrainSize(sizeHints, &iWidth, &iHeight);

    /* Add back the size of borders and title bar */
    iWidth += iBorderWidthX;
    iHeight += iBorderWidthY;

    /* Adjust size according to where we're dragging from */
    switch (wParam) {
    case WMSZ_TOP:
    case WMSZ_TOPRIGHT:
    case WMSZ_BOTTOM:
    case WMSZ_BOTTOMRIGHT:
    case WMSZ_RIGHT:
        rect->right = rect->left + iWidth;
        break;
    default:
        rect->left = rect->right - iWidth;
        break;
    }
    switch (wParam) {
    case WMSZ_BOTTOM:
    case WMSZ_BOTTOMRIGHT:
    case WMSZ_BOTTOMLEFT:
    case WMSZ_RIGHT:
    case WMSZ_LEFT:
        rect->bottom = rect->top + iHeight;
        break;
    default:
        rect->top = rect->bottom - iHeight;
        break;
    }
    return TRUE;
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
263
264
}

265
extern Bool winInDestroyWindowsWindow;
266
static Bool winInRaiseWindow = FALSE;
267
268
static void
winRaiseWindow(WindowPtr pWin)
269
{
270
271
272
273
274
275
276
277
278
279
    if (!winInDestroyWindowsWindow && !winInRaiseWindow) {
        BOOL oldstate = winInRaiseWindow;
        XID vlist[1] = { 0 };
        winInRaiseWindow = TRUE;
        /* Call configure window directly to make sure it gets processed 
         * in time
         */
        ConfigureWindow(pWin, CWStackMode, vlist, serverClient);
        winInRaiseWindow = oldstate;
    }
280
281
}

282
static
283
284
    void
winStartMousePolling(winPrivScreenPtr s_pScreenPriv)
285
{
286
287
288
289
290
291
292
293
294
    /*
     * Timer to poll mouse position.  This is needed to make
     * programs like xeyes follow the mouse properly when the
     * mouse pointer is outside of any X window.
     */
    if (g_uipMousePollingTimerID == 0)
        g_uipMousePollingTimerID = SetTimer(s_pScreenPriv->hwndScreen,
                                            WIN_POLLING_MOUSE_TIMER_ID,
                                            MOUSE_POLLING_INTERVAL, NULL);
295
}
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
296
297
298
299
300
301

/*
 * winTopLevelWindowProc - Window procedure for all top-level Windows windows.
 */

LRESULT CALLBACK
302
winTopLevelWindowProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
303
{
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
    POINT ptMouse;
    HDC hdcUpdate;
    PAINTSTRUCT ps;
    WindowPtr pWin = NULL;
    winPrivWinPtr pWinPriv = NULL;
    ScreenPtr s_pScreen = NULL;
    winPrivScreenPtr s_pScreenPriv = NULL;
    winScreenInfo *s_pScreenInfo = NULL;
    HWND hwndScreen = NULL;
    DrawablePtr pDraw = NULL;
    winWMMessageRec wmMsg;
    Bool fWMMsgInitialized = FALSE;
    static Bool s_fTracking = FALSE;
    Bool needRestack = FALSE;
    LRESULT ret;
319
320

#if CYGDEBUG
321
322
    winDebugWin32Message("winTopLevelWindowProc", hwnd, message, wParam,
                         lParam);
323
#endif
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
324

325
326
327
328
329
330
331
    /* Check if the Windows window property for our X window pointer is valid */
    if ((pWin = GetProp(hwnd, WIN_WINDOW_PROP)) != NULL) {
        /* Our X window pointer is valid */

        /* Get pointers to the drawable and the screen */
        pDraw = &pWin->drawable;
        s_pScreen = pWin->drawable.pScreen;
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
332

333
334
        /* Get a pointer to our window privates */
        pWinPriv = winGetWindowPriv(pWin);
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
335

336
337
338
        /* Get pointers to our screen privates and screen info */
        s_pScreenPriv = pWinPriv->pScreenPriv;
        s_pScreenInfo = s_pScreenPriv->pScreenInfo;
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
339

340
341
        /* Get the handle for our screen-sized window */
        hwndScreen = s_pScreenPriv->hwndScreen;
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
342

343
344
345
346
        /* */
        wmMsg.msg = 0;
        wmMsg.hwndWindow = hwnd;
        wmMsg.iWindow = (Window) GetProp(hwnd, WIN_WID_PROP);
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
347

348
349
350
351
        wmMsg.iX = pDraw->x;
        wmMsg.iY = pDraw->y;
        wmMsg.iWidth = pDraw->width;
        wmMsg.iHeight = pDraw->height;
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
352

353
        fWMMsgInitialized = TRUE;
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
354
355

#if 0
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
        /*
         * Print some debugging information
         */

        ErrorF("hWnd %08X\n", hwnd);
        ErrorF("pWin %08X\n", pWin);
        ErrorF("pDraw %08X\n", pDraw);
        ErrorF("\ttype %08X\n", pWin->drawable.type);
        ErrorF("\tclass %08X\n", pWin->drawable.class);
        ErrorF("\tdepth %08X\n", pWin->drawable.depth);
        ErrorF("\tbitsPerPixel %08X\n", pWin->drawable.bitsPerPixel);
        ErrorF("\tid %08X\n", pWin->drawable.id);
        ErrorF("\tx %08X\n", pWin->drawable.x);
        ErrorF("\ty %08X\n", pWin->drawable.y);
        ErrorF("\twidth %08X\n", pWin->drawable.width);
        ErrorF("\thenght %08X\n", pWin->drawable.height);
        ErrorF("\tpScreen %08X\n", pWin->drawable.pScreen);
        ErrorF("\tserialNumber %08X\n", pWin->drawable.serialNumber);
        ErrorF("g_iWindowPrivateKey %p\n", g_iWindowPrivateKey);
        ErrorF("pWinPriv %08X\n", pWinPriv);
        ErrorF("s_pScreenPriv %08X\n", s_pScreenPriv);
        ErrorF("s_pScreenInfo %08X\n", s_pScreenInfo);
        ErrorF("hwndScreen %08X\n", hwndScreen);
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
379
380
381
#endif
    }

382
383
    /* Branch on message type */
    switch (message) {
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
384
385
    case WM_CREATE:

386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
        /* */
        SetProp(hwnd,
                WIN_WINDOW_PROP,
                (HANDLE) ((LPCREATESTRUCT) lParam)->lpCreateParams);

        /* */
        SetProp(hwnd,
                WIN_WID_PROP,
                (HANDLE) winGetWindowID(((LPCREATESTRUCT) lParam)->
                                        lpCreateParams));

        /*
         * Make X windows' Z orders sync with Windows windows because
         * there can be AlwaysOnTop windows overlapped on the window
         * currently being created.
         */
        winReorderWindowsMultiWindow();

        /* Fix a 'round title bar corner background should be transparent not black' problem when first painted */
        {
            RECT rWindow;
            HRGN hRgnWindow;

            GetWindowRect(hwnd, &rWindow);
            hRgnWindow = CreateRectRgnIndirect(&rWindow);
            SetWindowRgn(hwnd, hRgnWindow, TRUE);
            DeleteObject(hRgnWindow);
        }

        SetWindowLongPtr(hwnd, GWLP_USERDATA, (LONG_PTR) XMING_SIGNATURE);

        return 0;
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
418
419

    case WM_INIT_SYS_MENU:
420
421
422
423
424
        /*
         * Add whatever the setup file wants to for this window
         */
        SetupSysMenu((unsigned long) hwnd);
        return 0;
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
425
426

    case WM_SYSCOMMAND:
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
        /*
         * Any window menu items go through here
         */
        if (HandleCustomWM_COMMAND((unsigned long) hwnd, LOWORD(wParam))) {
            /* Don't pass customized menus to DefWindowProc */
            return 0;
        }
        if (wParam == SC_RESTORE || wParam == SC_MAXIMIZE) {
            WINDOWPLACEMENT wndpl;

            wndpl.length = sizeof(wndpl);
            if (GetWindowPlacement(hwnd, &wndpl) &&
                wndpl.showCmd == SW_SHOWMINIMIZED)
                needRestack = TRUE;
        }
        break;
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
443
444

    case WM_INITMENU:
445
446
447
        /* Checks/Unchecks any menu items before they are displayed */
        HandleCustomWM_INITMENU((unsigned long) hwnd, wParam);
        break;
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
448

449
    case WM_ERASEBKGND:
450
451
452
453
454
455
        /*
         * Pretend that we did erase the background but we don't care,
         * since we repaint the entire region anyhow
         * This avoids some flickering when resizing.
         */
        return TRUE;
456

Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
457
    case WM_PAINT:
458
459
460
461
462
463
464
465
466
467
468
469
        /* Only paint if our window handle is valid */
        if (hwndScreen == NULL)
            break;

        /* BeginPaint gives us an hdc that clips to the invalidated region */
        hdcUpdate = BeginPaint(hwnd, &ps);
        /* Avoid the BitBlt's if the PAINTSTRUCT is bogus */
        if (ps.rcPaint.right == 0 && ps.rcPaint.bottom == 0 &&
            ps.rcPaint.left == 0 && ps.rcPaint.top == 0) {
            EndPaint(hwnd, &ps);
            return 0;
        }
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
470

471
#ifdef XWIN_GLX_WINDOWS
472
473
474
475
476
477
478
479
480
        if (pWinPriv->fWglUsed) {
            /*
               For regions which are being drawn by GL, the shadow framebuffer doesn't have the
               correct bits, so don't bitblt from the shadow framebuffer

               XXX: For now, just leave it alone, but ideally we want to send an expose event to
               the window so it really redraws the affected region...
             */
            ValidateRect(hwnd, &(ps.rcPaint));
481
        }
482
        else
483
#endif
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
            /* Try to copy from the shadow buffer */
        if (!BitBlt(hdcUpdate,
                        ps.rcPaint.left, ps.rcPaint.top,
                        ps.rcPaint.right - ps.rcPaint.left,
                        ps.rcPaint.bottom - ps.rcPaint.top,
                        s_pScreenPriv->hdcShadow,
                        ps.rcPaint.left + pWin->drawable.x,
                        ps.rcPaint.top + pWin->drawable.y, SRCCOPY)) {
            LPVOID lpMsgBuf;

            /* Display a fancy error message */
            FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
                          FORMAT_MESSAGE_FROM_SYSTEM |
                          FORMAT_MESSAGE_IGNORE_INSERTS,
                          NULL,
                          GetLastError(),
                          MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
501
                          (LPTSTR) &lpMsgBuf, 0, NULL);
502
503
504
505
506
507
508
509
510

            ErrorF("winTopLevelWindowProc - BitBlt failed: %s\n",
                   (LPSTR) lpMsgBuf);
            LocalFree(lpMsgBuf);
        }

        /* EndPaint frees the DC */
        EndPaint(hwnd, &ps);
        return 0;
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
511
512

    case WM_MOUSEMOVE:
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
        /* Unpack the client area mouse coordinates */
        ptMouse.x = GET_X_LPARAM(lParam);
        ptMouse.y = GET_Y_LPARAM(lParam);

        /* Translate the client area mouse coordinates to screen coordinates */
        ClientToScreen(hwnd, &ptMouse);

        /* Screen Coords from (-X, -Y) -> Root Window (0, 0) */
        ptMouse.x -= GetSystemMetrics(SM_XVIRTUALSCREEN);
        ptMouse.y -= GetSystemMetrics(SM_YVIRTUALSCREEN);

        /* We can't do anything without privates */
        if (s_pScreenPriv == NULL || s_pScreenInfo->fIgnoreInput)
            break;

        /* Has the mouse pointer crossed screens? */
        if (s_pScreen != miPointerGetScreen(g_pwinPointer))
            miPointerSetScreen(g_pwinPointer, s_pScreenInfo->dwScreen,
                               ptMouse.x - s_pScreenInfo->dwXOffset,
                               ptMouse.y - s_pScreenInfo->dwYOffset);

        /* Are we tracking yet? */
        if (!s_fTracking) {
            TRACKMOUSEEVENT tme;

            /* Setup data structure */
            ZeroMemory(&tme, sizeof(tme));
            tme.cbSize = sizeof(tme);
            tme.dwFlags = TME_LEAVE;
            tme.hwndTrack = hwnd;

            /* Call the tracking function */
            if (!TrackMouseEvent(&tme))
                ErrorF("winTopLevelWindowProc - TrackMouseEvent failed\n");

            /* Flag that we are tracking now */
            s_fTracking = TRUE;
        }

        /* Hide or show the Windows mouse cursor */
        if (g_fSoftwareCursor && g_fCursor) {
            /* Hide Windows cursor */
            g_fCursor = FALSE;
            ShowCursor(FALSE);
        }

        /* Kill the timer used to poll mouse events */
        if (g_uipMousePollingTimerID != 0) {
            KillTimer(s_pScreenPriv->hwndScreen, WIN_POLLING_MOUSE_TIMER_ID);
            g_uipMousePollingTimerID = 0;
        }

        /* Deliver absolute cursor position to X Server */
        winEnqueueMotion(ptMouse.x - s_pScreenInfo->dwXOffset,
                         ptMouse.y - s_pScreenInfo->dwYOffset);

        return 0;

Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
571
    case WM_NCMOUSEMOVE:
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
        /*
         * We break instead of returning 0 since we need to call
         * DefWindowProc to get the mouse cursor changes
         * and min/max/close button highlighting in Windows XP.
         * The Platform SDK says that you should return 0 if you
         * process this message, but it fails to mention that you
         * will give up any default functionality if you do return 0.
         */

        /* We can't do anything without privates */
        if (s_pScreenPriv == NULL || s_pScreenInfo->fIgnoreInput)
            break;

        /* Non-client mouse movement, show Windows cursor */
        if (g_fSoftwareCursor && !g_fCursor) {
            g_fCursor = TRUE;
            ShowCursor(TRUE);
        }

        winStartMousePolling(s_pScreenPriv);

        break;
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
594
595

    case WM_MOUSELEAVE:
596
        /* Mouse has left our client area */
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
597

598
599
        /* Flag that we are no longer tracking */
        s_fTracking = FALSE;
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
600

601
602
603
604
605
        /* Show the mouse cursor, if necessary */
        if (g_fSoftwareCursor && !g_fCursor) {
            g_fCursor = TRUE;
            ShowCursor(TRUE);
        }
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
606

607
        winStartMousePolling(s_pScreenPriv);
608

609
        return 0;
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
610
611
612

    case WM_LBUTTONDBLCLK:
    case WM_LBUTTONDOWN:
613
614
615
616
617
        if (s_pScreenPriv == NULL || s_pScreenInfo->fIgnoreInput)
            break;
        g_fButton[0] = TRUE;
        SetCapture(hwnd);
        return winMouseButtonsHandle(s_pScreen, ButtonPress, Button1, wParam);
618

Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
619
    case WM_LBUTTONUP:
620
621
622
623
624
625
        if (s_pScreenPriv == NULL || s_pScreenInfo->fIgnoreInput)
            break;
        g_fButton[0] = FALSE;
        ReleaseCapture();
        winStartMousePolling(s_pScreenPriv);
        return winMouseButtonsHandle(s_pScreen, ButtonRelease, Button1, wParam);
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
626
627
628

    case WM_MBUTTONDBLCLK:
    case WM_MBUTTONDOWN:
629
630
631
632
633
        if (s_pScreenPriv == NULL || s_pScreenInfo->fIgnoreInput)
            break;
        g_fButton[1] = TRUE;
        SetCapture(hwnd);
        return winMouseButtonsHandle(s_pScreen, ButtonPress, Button2, wParam);
634

Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
635
    case WM_MBUTTONUP:
636
637
638
639
640
641
        if (s_pScreenPriv == NULL || s_pScreenInfo->fIgnoreInput)
            break;
        g_fButton[1] = FALSE;
        ReleaseCapture();
        winStartMousePolling(s_pScreenPriv);
        return winMouseButtonsHandle(s_pScreen, ButtonRelease, Button2, wParam);
642

Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
643
644
    case WM_RBUTTONDBLCLK:
    case WM_RBUTTONDOWN:
645
646
647
648
649
        if (s_pScreenPriv == NULL || s_pScreenInfo->fIgnoreInput)
            break;
        g_fButton[2] = TRUE;
        SetCapture(hwnd);
        return winMouseButtonsHandle(s_pScreen, ButtonPress, Button3, wParam);
650

Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
651
    case WM_RBUTTONUP:
652
653
654
655
656
657
        if (s_pScreenPriv == NULL || s_pScreenInfo->fIgnoreInput)
            break;
        g_fButton[2] = FALSE;
        ReleaseCapture();
        winStartMousePolling(s_pScreenPriv);
        return winMouseButtonsHandle(s_pScreen, ButtonRelease, Button3, wParam);
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
658

659
660
    case WM_XBUTTONDBLCLK:
    case WM_XBUTTONDOWN:
661
662
663
664
665
        if (s_pScreenPriv == NULL || s_pScreenInfo->fIgnoreInput)
            break;
        SetCapture(hwnd);
        return winMouseButtonsHandle(s_pScreen, ButtonPress, HIWORD(wParam) + 5,
                                     wParam);
666

667
    case WM_XBUTTONUP:
668
669
670
671
672
673
        if (s_pScreenPriv == NULL || s_pScreenInfo->fIgnoreInput)
            break;
        ReleaseCapture();
        winStartMousePolling(s_pScreenPriv);
        return winMouseButtonsHandle(s_pScreen, ButtonRelease,
                                     HIWORD(wParam) + 5, wParam);
674

Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
675
    case WM_MOUSEWHEEL:
676
677
678
679
680
681
682
683
684
685
        if (SendMessage
            (hwnd, WM_NCHITTEST, 0,
             MAKELONG(GET_X_LPARAM(lParam),
                      GET_Y_LPARAM(lParam))) == HTCLIENT) {
            /* Pass the message to the root window */
            SendMessage(hwndScreen, message, wParam, lParam);
            return 0;
        }
        else
            break;
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
686

687
    case WM_SETFOCUS:
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
        if (s_pScreenPriv == NULL || s_pScreenInfo->fIgnoreInput)
            break;

        {
            /* Get the parent window for transient handling */
            HWND hParent = GetParent(hwnd);

            if (hParent && IsIconic(hParent))
                ShowWindow(hParent, SW_RESTORE);
        }

        winRestoreModeKeyStates();

        /* Add the keyboard hook if possible */
        if (g_fKeyboardHookLL)
            g_fKeyboardHookLL = winInstallKeyboardHookLL();
        return 0;

Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
706
    case WM_KILLFOCUS:
707
708
        /* Pop any pressed keys since we are losing keyboard focus */
        winKeybdReleaseKeys();
709

710
711
        /* Remove our keyboard hook if it is installed */
        winRemoveKeyboardHookLL();
712

713
714
715
        /* Revert the X focus as well, but only if the Windows focus is going to another window */
        if (!wParam && pWin)
            DeleteWindowFromAnyEvents(pWin, FALSE);
716

717
        return 0;
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
718

719
    case WM_SYSDEADCHAR:
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
720
    case WM_DEADCHAR:
721
722
723
724
725
        /*
         * NOTE: We do nothing with WM_*CHAR messages,
         * nor does the root window, so we can just toss these messages.
         */
        return 0;
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
726
727
728
729

    case WM_SYSKEYDOWN:
    case WM_KEYDOWN:

730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
        /*
         * Don't pass Alt-F4 key combo to root window,
         * let Windows translate to WM_CLOSE and close this top-level window.
         *
         * NOTE: We purposely don't check the fUseWinKillKey setting because
         * it should only apply to the key handling for the root window,
         * not for top-level window-manager windows.
         *
         * ALSO NOTE: We do pass Ctrl-Alt-Backspace to the root window
         * because that is a key combo that no X app should be expecting to
         * receive, since it has historically been used to shutdown the X server.
         * Passing Ctrl-Alt-Backspace to the root window preserves that
         * behavior, assuming that -unixkill has been passed as a parameter.
         */
        if (wParam == VK_F4 && (GetKeyState(VK_MENU) & 0x8000))
            break;
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
746

747
#if CYGWINDOWING_DEBUG
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
        if (wParam == VK_ESCAPE) {
            /* Place for debug: put any tests and dumps here */
            WINDOWPLACEMENT windPlace;
            RECT rc;
            LPRECT pRect;

            windPlace.length = sizeof(WINDOWPLACEMENT);
            GetWindowPlacement(hwnd, &windPlace);
            pRect = &windPlace.rcNormalPosition;
            ErrorF("\nCYGWINDOWING Dump:\n"
                   "\tdrawable: (%hd, %hd) - %hdx%hd\n", pDraw->x,
                   pDraw->y, pDraw->width, pDraw->height);
            ErrorF("\twindPlace: (%ld, %ld) - %ldx%ld\n", pRect->left,
                   pRect->top, pRect->right - pRect->left,
                   pRect->bottom - pRect->top);
            if (GetClientRect(hwnd, &rc)) {
                pRect = &rc;
                ErrorF("\tClientRect: (%ld, %ld) - %ldx%ld\n", pRect->left,
                       pRect->top, pRect->right - pRect->left,
                       pRect->bottom - pRect->top);
            }
            if (GetWindowRect(hwnd, &rc)) {
                pRect = &rc;
                ErrorF("\tWindowRect: (%ld, %ld) - %ldx%ld\n", pRect->left,
                       pRect->top, pRect->right - pRect->left,
                       pRect->bottom - pRect->top);
            }
            ErrorF("\n");
        }
777
#endif
778
779
780

        /* Pass the message to the root window */
        return winWindowProc(hwndScreen, message, wParam, lParam);
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
781
782
783
784

    case WM_SYSKEYUP:
    case WM_KEYUP:

785
786
        /* Pass the message to the root window */
        return winWindowProc(hwndScreen, message, wParam, lParam);
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
787
788
789

    case WM_HOTKEY:

790
791
792
        /* Pass the message to the root window */
        SendMessage(hwndScreen, message, wParam, lParam);
        return 0;
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
793
794
795

    case WM_ACTIVATE:

796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
        /* Pass the message to the root window */
        SendMessage(hwndScreen, message, wParam, lParam);

        if (LOWORD(wParam) != WA_INACTIVE) {
            /* Raise the window to the top in Z order */
            /* ago: Activate does not mean putting it to front! */
            /*
               wmMsg.msg = WM_WM_RAISE;
               if (fWMMsgInitialized)
               winSendMessageToWM (s_pScreenPriv->pWMInfo, &wmMsg);
             */

            /* Tell our Window Manager thread to activate the window */
            wmMsg.msg = WM_WM_ACTIVATE;
            if (fWMMsgInitialized)
                if (!pWin || !pWin->overrideRedirect)   /* for OOo menus */
                    winSendMessageToWM(s_pScreenPriv->pWMInfo, &wmMsg);
        }
        /* Prevent the mouse wheel from stalling when another window is minimized */
        if (HIWORD(wParam) == 0 && LOWORD(wParam) == WA_ACTIVE &&
            (HWND) lParam != NULL && (HWND) lParam != (HWND) GetParent(hwnd))
            SetFocus(hwnd);
        return 0;
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
819
820

    case WM_ACTIVATEAPP:
821
822
823
824
825
        /*
         * This message is also sent to the root window
         * so we do nothing for individual multiwindow windows
         */
        break;
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
826
827

    case WM_CLOSE:
828
829
        /* Removep AppUserModelID property */
        winSetAppUserModelID(hwnd, NULL);
830
831
832
833
834
835
836
837
838
839
840
841
        /* Branch on if the window was killed in X already */
        if (pWinPriv->fXKilled) {
            /* Window was killed, go ahead and destroy the window */
            DestroyWindow(hwnd);
        }
        else {
            /* Tell our Window Manager thread to kill the window */
            wmMsg.msg = WM_WM_KILL;
            if (fWMMsgInitialized)
                winSendMessageToWM(s_pScreenPriv->pWMInfo, &wmMsg);
        }
        return 0;
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
842
843
844

    case WM_DESTROY:

845
846
847
848
849
850
851
852
853
        /* Branch on if the window was killed in X already */
        if (pWinPriv && !pWinPriv->fXKilled) {
            ErrorF("winTopLevelWindowProc - WM_DESTROY - WM_WM_KILL\n");

            /* Tell our Window Manager thread to kill the window */
            wmMsg.msg = WM_WM_KILL;
            if (fWMMsgInitialized)
                winSendMessageToWM(s_pScreenPriv->pWMInfo, &wmMsg);
        }
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
854

855
856
857
        RemoveProp(hwnd, WIN_WINDOW_PROP);
        RemoveProp(hwnd, WIN_WID_PROP);
        RemoveProp(hwnd, WIN_NEEDMANAGE_PROP);
858

859
        break;
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
860
861

    case WM_MOVE:
862
863
864
        /* Adjust the X Window to the moved Windows window */
        winAdjustXWindow(pWin, hwnd);
        return 0;
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
865
866

    case WM_SHOWWINDOW:
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
        /* Bail out if the window is being hidden */
        if (!wParam)
            return 0;

        /* */
        if (!pWin->overrideRedirect) {
            /* Flag that this window needs to be made active when clicked */
            SetProp(hwnd, WIN_NEEDMANAGE_PROP, (HANDLE) 1);

            if (!(GetWindowLongPtr(hwnd, GWL_EXSTYLE) & WS_EX_APPWINDOW)) {
                HWND zstyle = HWND_NOTOPMOST;

                /* Set the window extended style flags */
                SetWindowLongPtr(hwnd, GWL_EXSTYLE, WS_EX_APPWINDOW);

                /* Set the transient style flags */
                if (GetParent(hwnd))
                    SetWindowLongPtr(hwnd, GWL_STYLE,
                                     WS_POPUP | WS_OVERLAPPED | WS_SYSMENU |
                                     WS_CLIPCHILDREN | WS_CLIPSIBLINGS);
                /* Set the window standard style flags */
                else
                    SetWindowLongPtr(hwnd, GWL_STYLE,
                                     (WS_POPUP | WS_OVERLAPPEDWINDOW |
                                      WS_CLIPCHILDREN | WS_CLIPSIBLINGS)
                                     & ~WS_CAPTION & ~WS_SIZEBOX);

894
                winUpdateWindowPosition(hwnd, &zstyle);
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916

                {
                    WinXWMHints hints;

                    if (winMultiWindowGetWMHints(pWin, &hints)) {
                        /*
                           Give the window focus, unless it has an InputHint
                           which is FALSE (this is used by e.g. glean to
                           avoid every test window grabbing the focus)
                         */
                        if (!((hints.flags & InputHint) && (!hints.input))) {
                            SetForegroundWindow(hwnd);
                        }
                    }
                }
            }
            wmMsg.msg = WM_WM_MAP3;
        }
        else {                  /* It is an overridden window so make it top of Z stack */

            HWND forHwnd = GetForegroundWindow();

917
#if CYGWINDOWING_DEBUG
918
            ErrorF("overridden window is shown\n");
919
#endif
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
            if (forHwnd != NULL) {
                if (GetWindowLongPtr(forHwnd, GWLP_USERDATA) & (LONG_PTR)
                    XMING_SIGNATURE) {
                    if (GetWindowLongPtr(forHwnd, GWL_EXSTYLE) & WS_EX_TOPMOST)
                        SetWindowPos(hwnd, HWND_TOPMOST, 0, 0, 0, 0,
                                     SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE);
                    else
                        SetWindowPos(hwnd, HWND_NOTOPMOST, 0, 0, 0, 0,
                                     SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE);
                }
            }
            wmMsg.msg = WM_WM_MAP2;
        }

        /* Tell our Window Manager thread to map the window */
        if (fWMMsgInitialized)
            winSendMessageToWM(s_pScreenPriv->pWMInfo, &wmMsg);

        winStartMousePolling(s_pScreenPriv);

        return 0;
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
941
942

    case WM_SIZING:
943
944
945
        /* Need to legalize the size according to WM_NORMAL_HINTS */
        /* for applications like xterm */
        return ValidateSizing(hwnd, pWin, wParam, lParam);
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
946

947
    case WM_WINDOWPOSCHANGED:
948
949
    {
        LPWINDOWPOS pWinPos = (LPWINDOWPOS) lParam;
Alexander Gottwald's avatar
Alexander Gottwald committed
950

951
        if (!(pWinPos->flags & SWP_NOZORDER)) {
Alexander Gottwald's avatar
Alexander Gottwald committed
952
#if CYGWINDOWING_DEBUG
953
            winDebug("\twindow z order was changed\n");
Alexander Gottwald's avatar
Alexander Gottwald committed
954
#endif
955
956
957
            if (pWinPos->hwndInsertAfter == HWND_TOP
                || pWinPos->hwndInsertAfter == HWND_TOPMOST
                || pWinPos->hwndInsertAfter == HWND_NOTOPMOST) {
Alexander Gottwald's avatar
Alexander Gottwald committed
958
#if CYGWINDOWING_DEBUG
959
                winDebug("\traise to top\n");
Alexander Gottwald's avatar
Alexander Gottwald committed
960
#endif
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
                /* Raise the window to the top in Z order */
                winRaiseWindow(pWin);
            }
            else if (pWinPos->hwndInsertAfter == HWND_BOTTOM) {
            }
            else {
                /* Check if this window is top of X windows. */
                HWND hWndAbove = NULL;
                DWORD dwCurrentProcessID = GetCurrentProcessId();
                DWORD dwWindowProcessID = 0;

                for (hWndAbove = pWinPos->hwndInsertAfter;
                     hWndAbove != NULL;
                     hWndAbove = GetNextWindow(hWndAbove, GW_HWNDPREV)) {
                    /* Ignore other XWin process's window */
                    GetWindowThreadProcessId(hWndAbove, &dwWindowProcessID);

                    if ((dwWindowProcessID == dwCurrentProcessID)
                        && GetProp(hWndAbove, WIN_WINDOW_PROP)
                        && !IsWindowVisible(hWndAbove)
                        && !IsIconic(hWndAbove))        /* ignore minimized windows */
                        break;
                }
                /* If this is top of X windows in Windows stack,
                   raise it in X stack. */
                if (hWndAbove == NULL) {
Alexander Gottwald's avatar
Alexander Gottwald committed
987
#if CYGWINDOWING_DEBUG
988
                    winDebug("\traise to top\n");
Alexander Gottwald's avatar
Alexander Gottwald committed
989
#endif
990
991
992
993
994
995
996
997
998
999
                    winRaiseWindow(pWin);
                }
            }
        }
    }
        /*
         * Pass the message to DefWindowProc to let the function
         * break down WM_WINDOWPOSCHANGED to WM_MOVE and WM_SIZE.
         */
        break;
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
1000
1001

    case WM_SIZE:
1002
        /* see dix/window.c */
1003
#if CYGWINDOWING_DEBUG
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
    {
        char buf[64];

        switch (wParam) {
        case SIZE_MINIMIZED:
            strcpy(buf, "SIZE_MINIMIZED");
            break;
        case SIZE_MAXIMIZED:
            strcpy(buf, "SIZE_MAXIMIZED");
            break;
        case SIZE_RESTORED:
            strcpy(buf, "SIZE_RESTORED");
            break;
        default:
            strcpy(buf, "UNKNOWN_FLAG");
        }
        ErrorF("winTopLevelWindowProc - WM_SIZE to %dx%d (%s) - %d ms\n",
               (int) LOWORD(lParam), (int) HIWORD(lParam), buf,
               (int) (GetTickCount()));
    }
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
1024
#endif
1025
1026
1027
        /* Adjust the X Window to the moved Windows window */
        winAdjustXWindow(pWin, hwnd);
        return 0;               /* end of WM_SIZE handler */
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
1028

1029
    case WM_STYLECHANGING:
1030
1031
1032
1033
1034
        /*
           When the style changes, adjust the Windows window size so the client area remains the same size,
           and adjust the Windows window position so that the client area remains in the same place.
         */
    {
1035
1036
1037
        RECT newWinRect;
        DWORD dwExStyle;
        DWORD dwStyle;
1038
        DWORD newStyle = ((STYLESTRUCT *) lParam)->styleNew;
1039
1040
        WINDOWINFO wi;

1041
1042
        dwExStyle = GetWindowLongPtr(hwnd, GWL_EXSTYLE);
        dwStyle = GetWindowLongPtr(hwnd, GWL_STYLE);
1043

1044
1045
        winDebug("winTopLevelWindowProc - WM_STYLECHANGING from %08x %08x\n",
                 dwStyle, dwExStyle);
1046
1047

        if (wParam == GWL_EXSTYLE)
1048
            dwExStyle = newStyle;
1049
1050

        if (wParam == GWL_STYLE)
1051
            dwStyle = newStyle;
1052

1053
1054
        winDebug("winTopLevelWindowProc - WM_STYLECHANGING to %08x %08x\n",
                 dwStyle, dwExStyle);
1055
1056
1057
1058
1059

        /* Get client rect in screen coordinates */
        wi.cbSize = sizeof(WINDOWINFO);
        GetWindowInfo(hwnd, &wi);

1060
1061
1062
1063
1064
        winDebug
            ("winTopLevelWindowProc - WM_STYLECHANGING client area {%d, %d, %d, %d}, {%d x %d}\n",
             wi.rcClient.left, wi.rcClient.top, wi.rcClient.right,
             wi.rcClient.bottom, wi.rcClient.right - wi.rcClient.left,
             wi.rcClient.bottom - wi.rcClient.top);
1065
1066
1067

        newWinRect = wi.rcClient;
        if (!AdjustWindowRectEx(&newWinRect, dwStyle, FALSE, dwExStyle))
1068
1069
            winDebug
                ("winTopLevelWindowProc - WM_STYLECHANGING AdjustWindowRectEx failed\n");
1070

1071
1072
1073
1074
1075
        winDebug
            ("winTopLevelWindowProc - WM_STYLECHANGING window area should be {%d, %d, %d, %d}, {%d x %d}\n",
             newWinRect.left, newWinRect.top, newWinRect.right,
             newWinRect.bottom, newWinRect.right - newWinRect.left,
             newWinRect.bottom - newWinRect.top);
1076
1077

        /*
1078
1079
1080
1081
           Style change hasn't happened yet, so we can't adjust the window size yet, as the winAdjustXWindow()
           which WM_SIZE does will use the current (unchanged) style.  Instead make a note to change it when
           WM_STYLECHANGED is received...
         */
1082
        pWinPriv->hDwp = BeginDeferWindowPos(1);
1083
1084
1085
1086
1087
1088
1089
        pWinPriv->hDwp =
            DeferWindowPos(pWinPriv->hDwp, hwnd, NULL, newWinRect.left,
                           newWinRect.top, newWinRect.right - newWinRect.left,
                           newWinRect.bottom - newWinRect.top,
                           SWP_NOACTIVATE | SWP_NOZORDER);
    }
        return 0;
1090
1091

    case WM_STYLECHANGED:
1092
1093
    {
        if (pWinPriv->hDwp) {
1094
1095
            EndDeferWindowPos(pWinPriv->hDwp);
            pWinPriv->hDwp = NULL;
1096
        }
1097
        winDebug("winTopLevelWindowProc - WM_STYLECHANGED done\n");
1098
1099
    }
        return 0;
1100

Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
1101
1102
    case WM_MOUSEACTIVATE:

1103
1104
        /* Check if this window needs to be made active when clicked */
        if (!GetProp(pWinPriv->hWnd, WIN_NEEDMANAGE_PROP)) {
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
1105
#if CYGMULTIWINDOW_DEBUG
1106
1107
            ErrorF("winTopLevelWindowProc - WM_MOUSEACTIVATE - "
                   "MA_NOACTIVATE\n");
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
1108
1109
#endif

1110
1111
1112
1113
            /* */
            return MA_NOACTIVATE;
        }
        break;
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
1114

1115
    case WM_SETCURSOR:
1116
1117
1118
1119
1120
1121
        if (LOWORD(lParam) == HTCLIENT) {
            if (!g_fSoftwareCursor)
                SetCursor(s_pScreenPriv->cursor.handle);
            return TRUE;
        }
        break;
1122

Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
1123
    default:
1124
        break;
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
1125
1126
    }

1127
1128
1129
1130
1131
1132
1133
1134
    ret = DefWindowProc(hwnd, message, wParam, lParam);
    /*
     * If the window was minized we get the stack change before the window is restored
     * and so it gets lost. Ensure there stacking order is correct.
     */
    if (needRestack)
        winReorderWindowsMultiWindow();
    return ret;
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
1135
}