0e9246eade2e02c4ca49d4b1b9c6f9947327cff8
[xboard.git] / winboard / wlayout.c
1 /*\r
2  * Layout management\r
3  *\r
4  * Author: Alessandro Scotti (Dec 2005)\r
5  *\r
6  * Copyright 2005 Alessandro Scotti\r
7  *\r
8  * ------------------------------------------------------------------------\r
9  *\r
10  * GNU XBoard is free software: you can redistribute it and/or modify\r
11  * it under the terms of the GNU General Public License as published by\r
12  * the Free Software Foundation, either version 3 of the License, or (at\r
13  * your option) any later version.\r
14  *\r
15  * GNU XBoard is distributed in the hope that it will be useful, but\r
16  * WITHOUT ANY WARRANTY; without even the implied warranty of\r
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU\r
18  * General Public License for more details.\r
19  *\r
20  * You should have received a copy of the GNU General Public License\r
21  * along with this program. If not, see http://www.gnu.org/licenses/.  *\r
22  *\r
23  *------------------------------------------------------------------------\r
24  ** See the file ChangeLog for a revision history.  */\r
25 \r
26 #include "config.h"\r
27 \r
28 #include <windows.h> /* required for all Windows applications */\r
29 #include <stdio.h>\r
30 #include <stdlib.h>\r
31 #include <malloc.h>\r
32 #include <commdlg.h>\r
33 #include <dlgs.h>\r
34 \r
35 #include "common.h"\r
36 #include "frontend.h"\r
37 #include "winboard.h"\r
38 \r
39 VOID RestoreWindowPlacement( HWND hWnd, WindowPlacement * wp )\r
40 {\r
41     if( wp->x != CW_USEDEFAULT || \r
42         wp->y != CW_USEDEFAULT ||\r
43         wp->width != CW_USEDEFAULT || \r
44         wp->height != CW_USEDEFAULT )\r
45     {\r
46         WINDOWPLACEMENT stWP;\r
47 \r
48         ZeroMemory( &stWP, sizeof(stWP) );\r
49 \r
50         EnsureOnScreen( &wp->x, &wp->y, 0, 0);\r
51 \r
52         stWP.length = sizeof(stWP);\r
53         stWP.flags = 0;\r
54         stWP.showCmd = SW_SHOW;\r
55         stWP.ptMaxPosition.x = 0;\r
56         stWP.ptMaxPosition.y = 0;\r
57         stWP.rcNormalPosition.left = wp->x;\r
58         stWP.rcNormalPosition.right = wp->x + wp->width;\r
59         stWP.rcNormalPosition.top = wp->y;\r
60         stWP.rcNormalPosition.bottom = wp->y + wp->height;\r
61 \r
62         SetWindowPlacement(hWnd, &stWP);\r
63     }\r
64 }\r
65 \r
66 VOID InitWindowPlacement( WindowPlacement * wp )\r
67 {\r
68     wp->visible = TRUE;\r
69     wp->x = CW_USEDEFAULT;\r
70     wp->y = CW_USEDEFAULT;\r
71     wp->width = CW_USEDEFAULT;\r
72     wp->height = CW_USEDEFAULT;\r
73 }\r
74 \r
75 static BOOL IsAttachDistance( int a, int b )\r
76 {\r
77     BOOL result = FALSE;\r
78 \r
79     if( a == b ) {\r
80         result = TRUE;\r
81     }\r
82 \r
83     return result;\r
84 }\r
85 \r
86 static BOOL IsDefaultPlacement( WindowPlacement * wp )\r
87 {\r
88     BOOL result = FALSE;\r
89 \r
90     if( wp->x == CW_USEDEFAULT || wp->y == CW_USEDEFAULT || wp->width == CW_USEDEFAULT || wp->height == CW_USEDEFAULT ) {\r
91         result = TRUE;\r
92     }\r
93 \r
94     return result;\r
95 }\r
96 \r
97 BOOL GetActualPlacement( HWND hWnd, WindowPlacement * wp )\r
98 {\r
99     BOOL result = FALSE;\r
100 \r
101     if( hWnd != NULL ) {\r
102         WINDOWPLACEMENT stWP;\r
103 \r
104         ZeroMemory( &stWP, sizeof(stWP) );\r
105 \r
106         stWP.length = sizeof(stWP);\r
107 \r
108         GetWindowPlacement( hWnd, &stWP );\r
109 \r
110         wp->x = stWP.rcNormalPosition.left;\r
111         wp->y = stWP.rcNormalPosition.top;\r
112         wp->width = stWP.rcNormalPosition.right - stWP.rcNormalPosition.left;\r
113         wp->height = stWP.rcNormalPosition.bottom - stWP.rcNormalPosition.top;\r
114 \r
115         result = TRUE;\r
116     }\r
117 \r
118     return result;\r
119 }\r
120 \r
121 static BOOL IsAttachedByWindowPlacement( LPRECT lprcMain, WindowPlacement * wp )\r
122 {\r
123     BOOL result = FALSE;\r
124 \r
125     if( ! IsDefaultPlacement(wp) ) {\r
126         if( IsAttachDistance( lprcMain->right, wp->x ) ||\r
127             IsAttachDistance( lprcMain->bottom, wp->y ) ||\r
128             IsAttachDistance( lprcMain->left, (wp->x + wp->width) ) ||\r
129             IsAttachDistance( lprcMain->top, (wp->y + wp->height) ) )\r
130         {\r
131             result = TRUE;\r
132         }\r
133     }\r
134 \r
135     return result;\r
136 }\r
137 \r
138 VOID ReattachAfterMove( LPRECT lprcOldPos, int new_x, int new_y, HWND hWndChild, WindowPlacement * pwpChild )\r
139 {\r
140     if( ! IsDefaultPlacement( pwpChild ) ) {\r
141         GetActualPlacement( hWndChild, pwpChild );\r
142 \r
143         if( IsAttachedByWindowPlacement( lprcOldPos, pwpChild ) ) {\r
144             /* Get position delta */\r
145             int delta_x = pwpChild->x - lprcOldPos->left;\r
146             int delta_y = pwpChild->y - lprcOldPos->top;\r
147 \r
148             /* Adjust placement */\r
149             pwpChild->x = new_x + delta_x;\r
150             pwpChild->y = new_y + delta_y;\r
151 \r
152             /* Move window */\r
153             if( hWndChild != NULL ) {\r
154                 SetWindowPos( hWndChild, HWND_TOP,\r
155                     pwpChild->x, pwpChild->y,\r
156                     0, 0,\r
157                     SWP_NOZORDER | SWP_NOSIZE );\r
158             }\r
159         }\r
160     }\r
161 }\r
162 \r
163 extern FILE *debugFP;\r
164 VOID ReattachAfterSize( LPRECT lprcOldPos, int new_w, int new_h, HWND hWndChild, WindowPlacement * pwpChild )\r
165 {\r
166     if( ! IsDefaultPlacement( pwpChild ) ) {\r
167         GetActualPlacement( hWndChild, pwpChild );\r
168 \r
169         if( IsAttachedByWindowPlacement( lprcOldPos, pwpChild ) ) {\r
170             /* Get delta of lower right corner */\r
171             int delta_x = new_w - (lprcOldPos->right  - lprcOldPos->left);\r
172             int delta_y = new_h - (lprcOldPos->bottom - lprcOldPos->top);\r
173 \r
174             /* Adjust size & placement */\r
175             if(pwpChild->x + pwpChild->width  >= lprcOldPos->right &&\r
176               (pwpChild->x + pwpChild->width  < screenGeometry.right - 5 || delta_x > 0) ) // keep right edge glued to display edge if touching\r
177                 pwpChild->width += delta_x;\r
178             if(pwpChild->x + pwpChild->width  >= screenGeometry.right  ) // don't move right edge off screen\r
179                 pwpChild->width = screenGeometry.right - pwpChild->x;\r
180             if(pwpChild->y + pwpChild->height >= lprcOldPos->bottom &&\r
181               (pwpChild->y + pwpChild->height < screenGeometry.bottom - 35 || delta_y > 0) ) // keep bottom edge glued to display edge if touching\r
182                 pwpChild->height += delta_y;\r
183             if(pwpChild->y + pwpChild->height >= screenGeometry.bottom - 30 ) // don't move bottom edge off screen\r
184                 pwpChild->height = screenGeometry.bottom - 30 - pwpChild->y;\r
185             if(pwpChild->x >= lprcOldPos->right)  pwpChild->width  -= delta_x, pwpChild->x += delta_x;\r
186             if(pwpChild->y >= lprcOldPos->bottom) pwpChild->height -= delta_y, pwpChild->y += delta_y;\r
187             if(pwpChild->width  < 30) pwpChild->width = 30;  // force minimum width\r
188             if(pwpChild->height < 50) pwpChild->height = 50; // force minimum height\r
189             /* Move window */\r
190             if( hWndChild != NULL ) {\r
191                 SetWindowPos( hWndChild, HWND_TOP,\r
192                     pwpChild->x, pwpChild->y,\r
193                     pwpChild->width, pwpChild->height,\r
194                     SWP_NOZORDER );\r
195             }\r
196         }\r
197     }\r
198 }\r