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