-
Notifications
You must be signed in to change notification settings - Fork 266
/
Copy pathwinpos.c
3095 lines (2678 loc) · 101 KB
/
winpos.c
1
2
3
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
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
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
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
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
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
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
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
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
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
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Window position related functions.
*
* Copyright 1993, 1994, 1995 Alexandre Julliard
* 1995, 1996, 1999 Alex Korobka
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
*/
#include <string.h>
#include "ntstatus.h"
#define WIN32_NO_STATUS
#include "user_private.h"
#include "winerror.h"
#include "controls.h"
#include "win.h"
#include "wine/server.h"
#include "wine/debug.h"
WINE_DEFAULT_DEBUG_CHANNEL(win);
#define SWP_AGG_NOGEOMETRYCHANGE \
(SWP_NOSIZE | SWP_NOCLIENTSIZE | SWP_NOZORDER)
#define SWP_AGG_NOPOSCHANGE \
(SWP_NOSIZE | SWP_NOMOVE | SWP_NOCLIENTSIZE | SWP_NOCLIENTMOVE | SWP_NOZORDER)
#define SWP_AGG_STATUSFLAGS \
(SWP_AGG_NOPOSCHANGE | SWP_FRAMECHANGED | SWP_HIDEWINDOW | SWP_SHOWWINDOW)
#define SWP_AGG_NOCLIENTCHANGE \
(SWP_NOCLIENTSIZE | SWP_NOCLIENTMOVE)
#define HAS_DLGFRAME(style,exStyle) \
(((exStyle) & WS_EX_DLGMODALFRAME) || \
(((style) & WS_DLGFRAME) && !((style) & WS_BORDER)))
#define HAS_THICKFRAME(style) \
(((style) & WS_THICKFRAME) && \
!(((style) & (WS_DLGFRAME|WS_BORDER)) == WS_DLGFRAME))
#define EMPTYPOINT(pt) ((pt).x == -1 && (pt).y == -1)
#define ON_LEFT_BORDER(hit) \
(((hit) == HTLEFT) || ((hit) == HTTOPLEFT) || ((hit) == HTBOTTOMLEFT))
#define ON_RIGHT_BORDER(hit) \
(((hit) == HTRIGHT) || ((hit) == HTTOPRIGHT) || ((hit) == HTBOTTOMRIGHT))
#define ON_TOP_BORDER(hit) \
(((hit) == HTTOP) || ((hit) == HTTOPLEFT) || ((hit) == HTTOPRIGHT))
#define ON_BOTTOM_BORDER(hit) \
(((hit) == HTBOTTOM) || ((hit) == HTBOTTOMLEFT) || ((hit) == HTBOTTOMRIGHT))
#define PLACE_MIN 0x0001
#define PLACE_MAX 0x0002
#define PLACE_RECT 0x0004
typedef struct
{
struct user_object obj;
INT actualCount;
INT suggestedCount;
HWND hwndParent;
WINDOWPOS *winPos;
} DWP;
/***********************************************************************
* SwitchToThisWindow (USER32.@)
*/
void WINAPI SwitchToThisWindow( HWND hwnd, BOOL alt_tab )
{
if (IsIconic( hwnd )) ShowWindow( hwnd, SW_RESTORE );
else BringWindowToTop( hwnd );
}
/***********************************************************************
* GetWindowRect (USER32.@)
*/
BOOL WINAPI GetWindowRect( HWND hwnd, LPRECT rect )
{
BOOL ret = WIN_GetRectangles( hwnd, COORDS_SCREEN, rect, NULL );
if (ret) TRACE( "hwnd %p %s\n", hwnd, wine_dbgstr_rect(rect) );
return ret;
}
/***********************************************************************
* GetWindowRgn (USER32.@)
*/
int WINAPI GetWindowRgn ( HWND hwnd, HRGN hrgn )
{
int nRet = ERROR;
NTSTATUS status;
HRGN win_rgn = 0;
RGNDATA *data;
size_t size = 256;
do
{
if (!(data = HeapAlloc( GetProcessHeap(), 0, sizeof(*data) + size - 1 )))
{
SetLastError( ERROR_OUTOFMEMORY );
return ERROR;
}
SERVER_START_REQ( get_window_region )
{
req->window = wine_server_user_handle( hwnd );
wine_server_set_reply( req, data->Buffer, size );
if (!(status = wine_server_call( req )))
{
size_t reply_size = wine_server_reply_size( reply );
if (reply_size)
{
data->rdh.dwSize = sizeof(data->rdh);
data->rdh.iType = RDH_RECTANGLES;
data->rdh.nCount = reply_size / sizeof(RECT);
data->rdh.nRgnSize = reply_size;
win_rgn = ExtCreateRegion( NULL, data->rdh.dwSize + data->rdh.nRgnSize, data );
}
}
else size = reply->total_size;
}
SERVER_END_REQ;
HeapFree( GetProcessHeap(), 0, data );
} while (status == STATUS_BUFFER_OVERFLOW);
if (status) SetLastError( RtlNtStatusToDosError(status) );
else if (win_rgn)
{
nRet = CombineRgn( hrgn, win_rgn, 0, RGN_COPY );
DeleteObject( win_rgn );
}
return nRet;
}
/***********************************************************************
* GetWindowRgnBox (USER32.@)
*/
int WINAPI GetWindowRgnBox( HWND hwnd, LPRECT prect )
{
int ret = ERROR;
HRGN hrgn;
if (!prect)
return ERROR;
if ((hrgn = CreateRectRgn(0, 0, 0, 0)))
{
if ((ret = GetWindowRgn( hwnd, hrgn )) != ERROR )
ret = GetRgnBox( hrgn, prect );
DeleteObject(hrgn);
}
return ret;
}
/***********************************************************************
* SetWindowRgn (USER32.@)
*/
int WINAPI SetWindowRgn( HWND hwnd, HRGN hrgn, BOOL bRedraw )
{
static const RECT empty_rect;
BOOL ret;
if (hrgn)
{
RGNDATA *data;
DWORD size;
if (!(size = GetRegionData( hrgn, 0, NULL ))) return FALSE;
if (!(data = HeapAlloc( GetProcessHeap(), 0, size ))) return FALSE;
if (!GetRegionData( hrgn, size, data ))
{
HeapFree( GetProcessHeap(), 0, data );
return FALSE;
}
SERVER_START_REQ( set_window_region )
{
req->window = wine_server_user_handle( hwnd );
req->redraw = (bRedraw != 0);
if (data->rdh.nCount)
wine_server_add_data( req, data->Buffer, data->rdh.nCount * sizeof(RECT) );
else
wine_server_add_data( req, &empty_rect, sizeof(empty_rect) );
ret = !wine_server_call_err( req );
}
SERVER_END_REQ;
HeapFree( GetProcessHeap(), 0, data );
}
else /* clear existing region */
{
SERVER_START_REQ( set_window_region )
{
req->window = wine_server_user_handle( hwnd );
req->redraw = (bRedraw != 0);
ret = !wine_server_call_err( req );
}
SERVER_END_REQ;
}
if (ret)
{
UINT swp_flags = SWP_NOSIZE|SWP_NOMOVE|SWP_NOZORDER|SWP_NOACTIVATE|SWP_FRAMECHANGED|SWP_NOCLIENTSIZE|SWP_NOCLIENTMOVE;
if (!bRedraw) swp_flags |= SWP_NOREDRAW;
USER_Driver->pSetWindowRgn( hwnd, hrgn, bRedraw );
SetWindowPos( hwnd, 0, 0, 0, 0, 0, swp_flags );
if (hrgn) DeleteObject( hrgn );
}
return ret;
}
/***********************************************************************
* GetClientRect (USER32.@)
*/
BOOL WINAPI GetClientRect( HWND hwnd, LPRECT rect )
{
return WIN_GetRectangles( hwnd, COORDS_CLIENT, NULL, rect );
}
/***********************************************************************
* list_children_from_point
*
* Get the list of children that can contain point from the server.
* Point is in screen coordinates.
* Returned list must be freed by caller.
*/
static HWND *list_children_from_point( HWND hwnd, POINT pt )
{
HWND *list;
int i, size = 128;
for (;;)
{
int count = 0;
if (!(list = HeapAlloc( GetProcessHeap(), 0, size * sizeof(HWND) ))) break;
SERVER_START_REQ( get_window_children_from_point )
{
req->parent = wine_server_user_handle( hwnd );
req->x = pt.x;
req->y = pt.y;
req->dpi = get_thread_dpi();
wine_server_set_reply( req, list, (size-1) * sizeof(user_handle_t) );
if (!wine_server_call( req )) count = reply->count;
}
SERVER_END_REQ;
if (count && count < size)
{
/* start from the end since HWND is potentially larger than user_handle_t */
for (i = count - 1; i >= 0; i--)
list[i] = wine_server_ptr_handle( ((user_handle_t *)list)[i] );
list[count] = 0;
return list;
}
HeapFree( GetProcessHeap(), 0, list );
if (!count) break;
size = count + 1; /* restart with a large enough buffer */
}
return NULL;
}
/***********************************************************************
* WINPOS_WindowFromPoint
*
* Find the window and hittest for a given point.
*/
HWND WINPOS_WindowFromPoint( HWND hwndScope, POINT pt, INT *hittest )
{
int i, res;
HWND ret, *list;
POINT win_pt;
if (!hwndScope) hwndScope = GetDesktopWindow();
*hittest = HTNOWHERE;
if (!(list = list_children_from_point( hwndScope, pt ))) return 0;
/* now determine the hittest */
for (i = 0; list[i]; i++)
{
LONG style = GetWindowLongW( list[i], GWL_STYLE );
/* If window is minimized or disabled, return at once */
if (style & WS_DISABLED)
{
*hittest = HTERROR;
break;
}
/* Send WM_NCCHITTEST (if same thread) */
if (!WIN_IsCurrentThread( list[i] ))
{
*hittest = HTCLIENT;
break;
}
win_pt = point_thread_to_win_dpi( list[i], pt );
res = SendMessageW( list[i], WM_NCHITTEST, 0, MAKELPARAM( win_pt.x, win_pt.y ));
if (res != HTTRANSPARENT)
{
*hittest = res; /* Found the window */
break;
}
/* continue search with next window in z-order */
}
ret = list[i];
HeapFree( GetProcessHeap(), 0, list );
TRACE( "scope %p (%d,%d) returning %p\n", hwndScope, pt.x, pt.y, ret );
return ret;
}
/*******************************************************************
* WindowFromPoint (USER32.@)
*/
HWND WINAPI WindowFromPoint( POINT pt )
{
INT hittest;
return WINPOS_WindowFromPoint( 0, pt, &hittest );
}
/*******************************************************************
* ChildWindowFromPoint (USER32.@)
*/
HWND WINAPI ChildWindowFromPoint( HWND hwndParent, POINT pt )
{
return ChildWindowFromPointEx( hwndParent, pt, CWP_ALL );
}
/*******************************************************************
* RealChildWindowFromPoint (USER32.@)
*/
HWND WINAPI RealChildWindowFromPoint( HWND hwndParent, POINT pt )
{
return ChildWindowFromPointEx( hwndParent, pt, CWP_SKIPTRANSPARENT | CWP_SKIPINVISIBLE );
}
/*******************************************************************
* ChildWindowFromPointEx (USER32.@)
*/
HWND WINAPI ChildWindowFromPointEx( HWND hwndParent, POINT pt, UINT uFlags)
{
/* pt is in the client coordinates */
HWND *list;
int i;
RECT rect;
HWND retvalue;
GetClientRect( hwndParent, &rect );
if (!PtInRect( &rect, pt )) return 0;
if (!(list = WIN_ListChildren( hwndParent ))) return hwndParent;
for (i = 0; list[i]; i++)
{
if (!WIN_GetRectangles( list[i], COORDS_PARENT, &rect, NULL )) continue;
if (!PtInRect( &rect, pt )) continue;
if (uFlags & (CWP_SKIPINVISIBLE|CWP_SKIPDISABLED))
{
LONG style = GetWindowLongW( list[i], GWL_STYLE );
if ((uFlags & CWP_SKIPINVISIBLE) && !(style & WS_VISIBLE)) continue;
if ((uFlags & CWP_SKIPDISABLED) && (style & WS_DISABLED)) continue;
}
if (uFlags & CWP_SKIPTRANSPARENT)
{
if (GetWindowLongW( list[i], GWL_EXSTYLE ) & WS_EX_TRANSPARENT) continue;
}
break;
}
retvalue = list[i];
HeapFree( GetProcessHeap(), 0, list );
if (!retvalue) retvalue = hwndParent;
return retvalue;
}
/*******************************************************************
* WINPOS_GetWinOffset
*
* Calculate the offset between the origin of the two windows. Used
* to implement MapWindowPoints.
*/
static BOOL WINPOS_GetWinOffset( HWND hwndFrom, HWND hwndTo, BOOL *mirrored, POINT *ret_offset )
{
WND * wndPtr;
POINT offset;
BOOL mirror_from, mirror_to, ret;
HWND hwnd;
offset.x = offset.y = 0;
*mirrored = mirror_from = mirror_to = FALSE;
/* Translate source window origin to screen coords */
if (hwndFrom)
{
if (!(wndPtr = WIN_GetPtr( hwndFrom )))
{
SetLastError( ERROR_INVALID_WINDOW_HANDLE );
return FALSE;
}
if (wndPtr == WND_OTHER_PROCESS) goto other_process;
if (wndPtr != WND_DESKTOP)
{
if (wndPtr->dwExStyle & WS_EX_LAYOUTRTL)
{
mirror_from = TRUE;
offset.x += wndPtr->client_rect.right - wndPtr->client_rect.left;
}
while (wndPtr->parent)
{
offset.x += wndPtr->client_rect.left;
offset.y += wndPtr->client_rect.top;
hwnd = wndPtr->parent;
WIN_ReleasePtr( wndPtr );
if (!(wndPtr = WIN_GetPtr( hwnd ))) break;
if (wndPtr == WND_OTHER_PROCESS) goto other_process;
if (wndPtr == WND_DESKTOP) break;
if (wndPtr->flags & WIN_CHILDREN_MOVED)
{
WIN_ReleasePtr( wndPtr );
goto other_process;
}
}
if (wndPtr && wndPtr != WND_DESKTOP) WIN_ReleasePtr( wndPtr );
offset = point_win_to_thread_dpi( hwndFrom, offset );
}
}
/* Translate origin to destination window coords */
if (hwndTo)
{
if (!(wndPtr = WIN_GetPtr( hwndTo )))
{
SetLastError( ERROR_INVALID_WINDOW_HANDLE );
return FALSE;
}
if (wndPtr == WND_OTHER_PROCESS) goto other_process;
if (wndPtr != WND_DESKTOP)
{
POINT pt = { 0, 0 };
if (wndPtr->dwExStyle & WS_EX_LAYOUTRTL)
{
mirror_to = TRUE;
pt.x += wndPtr->client_rect.right - wndPtr->client_rect.left;
}
while (wndPtr->parent)
{
pt.x += wndPtr->client_rect.left;
pt.y += wndPtr->client_rect.top;
hwnd = wndPtr->parent;
WIN_ReleasePtr( wndPtr );
if (!(wndPtr = WIN_GetPtr( hwnd ))) break;
if (wndPtr == WND_OTHER_PROCESS) goto other_process;
if (wndPtr == WND_DESKTOP) break;
if (wndPtr->flags & WIN_CHILDREN_MOVED)
{
WIN_ReleasePtr( wndPtr );
goto other_process;
}
}
if (wndPtr && wndPtr != WND_DESKTOP) WIN_ReleasePtr( wndPtr );
pt = point_win_to_thread_dpi( hwndTo, pt );
offset.x -= pt.x;
offset.y -= pt.y;
}
}
*mirrored = mirror_from ^ mirror_to;
if (mirror_from) offset.x = -offset.x;
*ret_offset = offset;
return TRUE;
other_process: /* one of the parents may belong to another process, do it the hard way */
SERVER_START_REQ( get_windows_offset )
{
req->from = wine_server_user_handle( hwndFrom );
req->to = wine_server_user_handle( hwndTo );
req->dpi = get_thread_dpi();
if ((ret = !wine_server_call_err( req )))
{
ret_offset->x = reply->x;
ret_offset->y = reply->y;
*mirrored = reply->mirror;
}
}
SERVER_END_REQ;
return ret;
}
/* map coordinates of a window region */
void map_window_region( HWND from, HWND to, HRGN hrgn )
{
BOOL mirrored;
POINT offset;
UINT i, size;
RGNDATA *data;
HRGN new_rgn;
RECT *rect;
if (!WINPOS_GetWinOffset( from, to, &mirrored, &offset )) return;
if (!mirrored)
{
OffsetRgn( hrgn, offset.x, offset.y );
return;
}
if (!(size = GetRegionData( hrgn, 0, NULL ))) return;
if (!(data = HeapAlloc( GetProcessHeap(), 0, size ))) return;
GetRegionData( hrgn, size, data );
rect = (RECT *)data->Buffer;
for (i = 0; i < data->rdh.nCount; i++)
{
int tmp = -(rect[i].left + offset.x);
rect[i].left = -(rect[i].right + offset.x);
rect[i].right = tmp;
rect[i].top += offset.y;
rect[i].bottom += offset.y;
}
if ((new_rgn = ExtCreateRegion( NULL, data->rdh.dwSize + data->rdh.nRgnSize, data )))
{
CombineRgn( hrgn, new_rgn, 0, RGN_COPY );
DeleteObject( new_rgn );
}
HeapFree( GetProcessHeap(), 0, data );
}
/*******************************************************************
* MapWindowPoints (USER32.@)
*/
INT WINAPI MapWindowPoints( HWND hwndFrom, HWND hwndTo, LPPOINT lppt, UINT count )
{
BOOL mirrored;
POINT offset;
UINT i;
if (!WINPOS_GetWinOffset( hwndFrom, hwndTo, &mirrored, &offset )) return 0;
for (i = 0; i < count; i++)
{
lppt[i].x += offset.x;
lppt[i].y += offset.y;
if (mirrored) lppt[i].x = -lppt[i].x;
}
if (mirrored && count == 2) /* special case for rectangle */
{
int tmp = lppt[0].x;
lppt[0].x = lppt[1].x;
lppt[1].x = tmp;
}
return MAKELONG( LOWORD(offset.x), LOWORD(offset.y) );
}
/*******************************************************************
* ClientToScreen (USER32.@)
*/
BOOL WINAPI ClientToScreen( HWND hwnd, LPPOINT lppnt )
{
POINT offset;
BOOL mirrored;
if (!hwnd)
{
SetLastError( ERROR_INVALID_WINDOW_HANDLE );
return FALSE;
}
if (!WINPOS_GetWinOffset( hwnd, 0, &mirrored, &offset )) return FALSE;
lppnt->x += offset.x;
lppnt->y += offset.y;
if (mirrored) lppnt->x = -lppnt->x;
return TRUE;
}
/*******************************************************************
* ScreenToClient (USER32.@)
*/
BOOL WINAPI ScreenToClient( HWND hwnd, LPPOINT lppnt )
{
POINT offset;
BOOL mirrored;
if (!hwnd)
{
SetLastError( ERROR_INVALID_WINDOW_HANDLE );
return FALSE;
}
if (!WINPOS_GetWinOffset( 0, hwnd, &mirrored, &offset )) return FALSE;
lppnt->x += offset.x;
lppnt->y += offset.y;
if (mirrored) lppnt->x = -lppnt->x;
return TRUE;
}
/***********************************************************************
* IsIconic (USER32.@)
*/
BOOL WINAPI IsIconic(HWND hWnd)
{
return (GetWindowLongW( hWnd, GWL_STYLE ) & WS_MINIMIZE) != 0;
}
/***********************************************************************
* IsZoomed (USER32.@)
*/
BOOL WINAPI IsZoomed(HWND hWnd)
{
return (GetWindowLongW( hWnd, GWL_STYLE ) & WS_MAXIMIZE) != 0;
}
/*******************************************************************
* AllowSetForegroundWindow (USER32.@)
*/
BOOL WINAPI AllowSetForegroundWindow( DWORD procid )
{
/* FIXME: If Win98/2000 style SetForegroundWindow behavior is
* implemented, then fix this function. */
return TRUE;
}
/*******************************************************************
* LockSetForegroundWindow (USER32.@)
*/
BOOL WINAPI LockSetForegroundWindow( UINT lockcode )
{
/* FIXME: If Win98/2000 style SetForegroundWindow behavior is
* implemented, then fix this function. */
return TRUE;
}
/***********************************************************************
* BringWindowToTop (USER32.@)
*/
BOOL WINAPI BringWindowToTop( HWND hwnd )
{
return SetWindowPos( hwnd, HWND_TOP, 0, 0, 0, 0, SWP_NOMOVE|SWP_NOSIZE );
}
/***********************************************************************
* MoveWindow (USER32.@)
*/
BOOL WINAPI MoveWindow( HWND hwnd, INT x, INT y, INT cx, INT cy,
BOOL repaint )
{
int flags = SWP_NOZORDER | SWP_NOACTIVATE;
if (!repaint) flags |= SWP_NOREDRAW;
TRACE("%p %d,%d %dx%d %d\n", hwnd, x, y, cx, cy, repaint );
return SetWindowPos( hwnd, 0, x, y, cx, cy, flags );
}
/*******************************************************************
* get_work_rect
*
* Get the work area that a maximized window can cover, depending on style.
*/
static BOOL get_work_rect( HWND hwnd, RECT *rect )
{
HMONITOR monitor = MonitorFromWindow( hwnd, MONITOR_DEFAULTTOPRIMARY );
MONITORINFO mon_info;
DWORD style;
if (!monitor) return FALSE;
mon_info.cbSize = sizeof(mon_info);
GetMonitorInfoW( monitor, &mon_info );
*rect = mon_info.rcMonitor;
style = GetWindowLongW( hwnd, GWL_STYLE );
if (style & WS_MAXIMIZEBOX)
{
if ((style & WS_CAPTION) == WS_CAPTION || !(style & (WS_CHILD | WS_POPUP)))
*rect = mon_info.rcWork;
}
return TRUE;
}
/*******************************************************************
* WINPOS_GetMinMaxInfo
*
* Get the minimized and maximized information for a window.
*/
MINMAXINFO WINPOS_GetMinMaxInfo( HWND hwnd )
{
DPI_AWARENESS_CONTEXT context;
RECT rc_work, rc_primary;
MINMAXINFO MinMax;
INT xinc, yinc;
LONG style = GetWindowLongW( hwnd, GWL_STYLE );
LONG adjustedStyle;
LONG exstyle = GetWindowLongW( hwnd, GWL_EXSTYLE );
RECT rc;
WND *win;
context = SetThreadDpiAwarenessContext( GetWindowDpiAwarenessContext( hwnd ));
/* Compute default values */
GetWindowRect(hwnd, &rc);
MinMax.ptReserved.x = rc.left;
MinMax.ptReserved.y = rc.top;
if ((style & WS_CAPTION) == WS_CAPTION)
adjustedStyle = style & ~WS_BORDER; /* WS_CAPTION = WS_DLGFRAME | WS_BORDER */
else
adjustedStyle = style;
GetClientRect(GetAncestor(hwnd,GA_PARENT), &rc);
AdjustWindowRectEx(&rc, adjustedStyle, ((style & WS_POPUP) && GetMenu(hwnd)), exstyle);
xinc = -rc.left;
yinc = -rc.top;
MinMax.ptMaxSize.x = rc.right - rc.left;
MinMax.ptMaxSize.y = rc.bottom - rc.top;
if (style & (WS_DLGFRAME | WS_BORDER))
{
MinMax.ptMinTrackSize.x = GetSystemMetrics(SM_CXMINTRACK);
MinMax.ptMinTrackSize.y = GetSystemMetrics(SM_CYMINTRACK);
}
else
{
MinMax.ptMinTrackSize.x = 2 * xinc;
MinMax.ptMinTrackSize.y = 2 * yinc;
}
MinMax.ptMaxTrackSize.x = GetSystemMetrics(SM_CXMAXTRACK);
MinMax.ptMaxTrackSize.y = GetSystemMetrics(SM_CYMAXTRACK);
MinMax.ptMaxPosition.x = -xinc;
MinMax.ptMaxPosition.y = -yinc;
if ((win = WIN_GetPtr( hwnd )) && win != WND_DESKTOP && win != WND_OTHER_PROCESS)
{
if (!EMPTYPOINT(win->max_pos)) MinMax.ptMaxPosition = win->max_pos;
WIN_ReleasePtr( win );
}
SendMessageW( hwnd, WM_GETMINMAXINFO, 0, (LPARAM)&MinMax );
/* if the app didn't change the values, adapt them for the current monitor */
if (get_work_rect( hwnd, &rc_work ))
{
rc_primary = get_primary_monitor_rect();
if (MinMax.ptMaxSize.x == (rc_primary.right - rc_primary.left) + 2 * xinc &&
MinMax.ptMaxSize.y == (rc_primary.bottom - rc_primary.top) + 2 * yinc)
{
MinMax.ptMaxSize.x = (rc_work.right - rc_work.left) + 2 * xinc;
MinMax.ptMaxSize.y = (rc_work.bottom - rc_work.top) + 2 * yinc;
}
if (MinMax.ptMaxPosition.x == -xinc && MinMax.ptMaxPosition.y == -yinc)
{
MinMax.ptMaxPosition.x = rc_work.left - xinc;
MinMax.ptMaxPosition.y = rc_work.top - yinc;
}
}
/* Some sanity checks */
TRACE("%d %d / %d %d / %d %d / %d %d\n",
MinMax.ptMaxSize.x, MinMax.ptMaxSize.y,
MinMax.ptMaxPosition.x, MinMax.ptMaxPosition.y,
MinMax.ptMaxTrackSize.x, MinMax.ptMaxTrackSize.y,
MinMax.ptMinTrackSize.x, MinMax.ptMinTrackSize.y);
MinMax.ptMaxTrackSize.x = max( MinMax.ptMaxTrackSize.x,
MinMax.ptMinTrackSize.x );
MinMax.ptMaxTrackSize.y = max( MinMax.ptMaxTrackSize.y,
MinMax.ptMinTrackSize.y );
SetThreadDpiAwarenessContext( context );
return MinMax;
}
static POINT get_first_minimized_child_pos( const RECT *parent, const MINIMIZEDMETRICS *mm,
int width, int height )
{
POINT ret;
if (mm->iArrange & ARW_STARTRIGHT)
ret.x = parent->right - mm->iHorzGap - width;
else
ret.x = parent->left + mm->iHorzGap;
if (mm->iArrange & ARW_STARTTOP)
ret.y = parent->top + mm->iVertGap;
else
ret.y = parent->bottom - mm->iVertGap - height;
return ret;
}
static void get_next_minimized_child_pos( const RECT *parent, const MINIMIZEDMETRICS *mm,
int width, int height, POINT *pos )
{
BOOL next;
if (mm->iArrange & ARW_UP) /* == ARW_DOWN */
{
if (mm->iArrange & ARW_STARTTOP)
{
pos->y += height + mm->iVertGap;
if ((next = pos->y + height > parent->bottom))
pos->y = parent->top + mm->iVertGap;
}
else
{
pos->y -= height + mm->iVertGap;
if ((next = pos->y < parent->top))
pos->y = parent->bottom - mm->iVertGap - height;
}
if (next)
{
if (mm->iArrange & ARW_STARTRIGHT)
pos->x -= width + mm->iHorzGap;
else
pos->x += width + mm->iHorzGap;
}
}
else
{
if (mm->iArrange & ARW_STARTRIGHT)
{
pos->x -= width + mm->iHorzGap;
if ((next = pos->x < parent->left))
pos->x = parent->right - mm->iHorzGap - width;
}
else
{
pos->x += width + mm->iHorzGap;
if ((next = pos->x + width > parent->right))
pos->x = parent->left + mm->iHorzGap;
}
if (next)
{
if (mm->iArrange & ARW_STARTTOP)
pos->y += height + mm->iVertGap;
else
pos->y -= height + mm->iVertGap;
}
}
}
static POINT get_minimized_pos( HWND hwnd, POINT pt )
{
RECT rect, rectParent;
HWND parent, child;
HRGN hrgn, tmp;
MINIMIZEDMETRICS metrics;
int width, height;
parent = GetAncestor( hwnd, GA_PARENT );
if (parent == GetDesktopWindow())
{
MONITORINFO mon_info;
HMONITOR monitor = MonitorFromWindow( hwnd, MONITOR_DEFAULTTOPRIMARY );
mon_info.cbSize = sizeof( mon_info );
GetMonitorInfoW( monitor, &mon_info );
rectParent = mon_info.rcWork;
}
else GetClientRect( parent, &rectParent );
if ((pt.x >= rectParent.left) && (pt.x + GetSystemMetrics( SM_CXMINIMIZED ) < rectParent.right) &&
(pt.y >= rectParent.top) && (pt.y + GetSystemMetrics( SM_CYMINIMIZED ) < rectParent.bottom))
return pt; /* The icon already has a suitable position */
width = GetSystemMetrics( SM_CXMINIMIZED );
height = GetSystemMetrics( SM_CYMINIMIZED );
metrics.cbSize = sizeof(metrics);
SystemParametersInfoW( SPI_GETMINIMIZEDMETRICS, sizeof(metrics), &metrics, 0 );
/* Check if another icon already occupies this spot */
/* FIXME: this is completely inefficient */
hrgn = CreateRectRgn( 0, 0, 0, 0 );
tmp = CreateRectRgn( 0, 0, 0, 0 );
for (child = GetWindow( parent, GW_CHILD ); child; child = GetWindow( child, GW_HWNDNEXT ))
{
if (child == hwnd) continue;
if ((GetWindowLongW( child, GWL_STYLE ) & (WS_VISIBLE|WS_MINIMIZE)) != (WS_VISIBLE|WS_MINIMIZE))
continue;
if (WIN_GetRectangles( child, COORDS_PARENT, &rect, NULL ))
{
SetRectRgn( tmp, rect.left, rect.top, rect.right, rect.bottom );
CombineRgn( hrgn, hrgn, tmp, RGN_OR );
}
}
DeleteObject( tmp );
pt = get_first_minimized_child_pos( &rectParent, &metrics, width, height );
for (;;)
{
SetRect( &rect, pt.x, pt.y, pt.x + width, pt.y + height );
if (!RectInRegion( hrgn, &rect ))
break;
get_next_minimized_child_pos( &rectParent, &metrics, width, height, &pt );
}
DeleteObject( hrgn );
return pt;
}
/***********************************************************************
* WINPOS_MinMaximize
*/
UINT WINPOS_MinMaximize( HWND hwnd, UINT cmd, LPRECT rect )
{
UINT swpFlags = 0;
LONG old_style;
MINMAXINFO minmax;
WINDOWPLACEMENT wpl;
TRACE("%p %u\n", hwnd, cmd );
wpl.length = sizeof(wpl);
GetWindowPlacement( hwnd, &wpl );
if (HOOK_CallHooks( WH_CBT, HCBT_MINMAX, (WPARAM)hwnd, cmd, TRUE ))
return SWP_NOSIZE | SWP_NOMOVE;
if (IsIconic( hwnd ))
{
switch (cmd)
{
case SW_SHOWMINNOACTIVE:
case SW_SHOWMINIMIZED:
case SW_FORCEMINIMIZE:
case SW_MINIMIZE:
wpl.ptMinPosition = get_minimized_pos( hwnd, wpl.ptMinPosition );
SetRect( rect, wpl.ptMinPosition.x, wpl.ptMinPosition.y,
wpl.ptMinPosition.x + GetSystemMetrics(SM_CXMINIMIZED),
wpl.ptMinPosition.y + GetSystemMetrics(SM_CYMINIMIZED) );
return SWP_NOSIZE | SWP_NOMOVE;
}
if (!SendMessageW( hwnd, WM_QUERYOPEN, 0, 0 )) return SWP_NOSIZE | SWP_NOMOVE;
swpFlags |= SWP_NOCOPYBITS;
}
switch( cmd )
{
case SW_SHOWMINNOACTIVE:
case SW_SHOWMINIMIZED:
case SW_FORCEMINIMIZE:
case SW_MINIMIZE:
if (IsZoomed( hwnd )) win_set_flags( hwnd, WIN_RESTORE_MAX, 0 );
else win_set_flags( hwnd, 0, WIN_RESTORE_MAX );
if (GetFocus() == hwnd)
{
if (GetWindowLongW(hwnd, GWL_STYLE) & WS_CHILD)
SetFocus(GetAncestor(hwnd, GA_PARENT));
else
SetFocus(0);
}
old_style = WIN_SetStyle( hwnd, WS_MINIMIZE, WS_MAXIMIZE );
wpl.ptMinPosition = get_minimized_pos( hwnd, wpl.ptMinPosition );
if (!(old_style & WS_MINIMIZE)) swpFlags |= SWP_STATECHANGED;
SetRect( rect, wpl.ptMinPosition.x, wpl.ptMinPosition.y,
wpl.ptMinPosition.x + GetSystemMetrics(SM_CXMINIMIZED),
wpl.ptMinPosition.y + GetSystemMetrics(SM_CYMINIMIZED) );
swpFlags |= SWP_NOCOPYBITS;
break;
case SW_MAXIMIZE:
old_style = GetWindowLongW( hwnd, GWL_STYLE );
if ((old_style & WS_MAXIMIZE) && (old_style & WS_VISIBLE)) return SWP_NOSIZE | SWP_NOMOVE;
minmax = WINPOS_GetMinMaxInfo( hwnd );
old_style = WIN_SetStyle( hwnd, WS_MAXIMIZE, WS_MINIMIZE );
if (old_style & WS_MINIMIZE)