Rename 'General tab' to 'About', move to the last position.
[wine] / programs / winecfg / x11drvdlg.c
1 /*
2  * X11DRV configuration code
3  *
4  * Copyright 2003 Mark Westcott
5  * Copyright 2003 Mike Hearn
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20  *
21  */
22
23 #include <stdarg.h>
24 #include <stdlib.h>
25 #include <stdio.h>
26
27 #include <windef.h>
28 #include <winbase.h>
29 #include <winreg.h>
30 #include <wine/debug.h>
31
32 #include "resource.h"
33 #include "winecfg.h"
34
35 WINE_DEFAULT_DEBUG_CHANNEL(winecfg);
36
37 #define RES_MAXLEN 5 /* the maximum number of characters in a screen dimension. 5 digits should be plenty, what kind of crazy person runs their screen >10,000 pixels across? */
38 #define section (appSettings == EDITING_GLOBAL ? "x11drv" : (getSectionForApp("x11drv")))
39
40 int updatingUI;
41
42 int appSettings = EDITING_GLOBAL; /* start by editing global */
43 char *currentApp; /* the app we are currently editing, or NULL if editing global */
44
45 char *getSectionForApp(char *pSection)
46 {
47     static char *lastResult = NULL;
48     if (lastResult) HeapFree(GetProcessHeap(), 0, lastResult);
49     lastResult = HeapAlloc(GetProcessHeap(), 0, strlen("AppDefaults\\") + strlen(currentApp) + 2 /* \\ */ + strlen(pSection) + 1 /* terminator */);
50     sprintf(lastResult, "AppDefaults\\%s\\%s", currentApp, pSection);
51     return lastResult;
52 }
53
54 void updateGUIForDesktopMode(HWND dialog) {
55     WINE_TRACE("\n");
56
57     updatingUI = TRUE;
58     
59     /* do we have desktop mode enabled? */
60     if (doesConfigValueExist(section, "Desktop") == S_OK) {
61         CheckDlgButton(dialog, IDC_ENABLE_DESKTOP, BST_CHECKED);
62         /* enable the controls */
63         enable(IDC_DESKTOP_WIDTH);
64         enable(IDC_DESKTOP_HEIGHT);
65         enable(IDC_DESKTOP_SIZE);
66         enable(IDC_DESKTOP_BY);
67         
68         SetWindowText(GetDlgItem(dialog, IDC_DESKTOP_WIDTH), "640");
69         SetWindowText(GetDlgItem(dialog, IDC_DESKTOP_HEIGHT), "480");   
70     }
71     else {
72         CheckDlgButton(dialog, IDC_ENABLE_DESKTOP, BST_UNCHECKED);
73         /* disable the controls */
74         disable(IDC_DESKTOP_WIDTH);
75         disable(IDC_DESKTOP_HEIGHT);
76         disable(IDC_DESKTOP_SIZE);
77         disable(IDC_DESKTOP_BY);
78
79         SetWindowText(GetDlgItem(dialog, IDC_DESKTOP_WIDTH), "");
80         SetWindowText(GetDlgItem(dialog, IDC_DESKTOP_HEIGHT), "");
81     }
82
83     updatingUI = FALSE;
84 }
85
86 /* pokes the win32 api to setup the dialog from the config struct */
87 void initX11DrvDlg (HWND hDlg)
88 {
89     char *buf;
90     char *bufindex;
91
92     updateGUIForDesktopMode(hDlg);
93
94     updatingUI = TRUE;
95     
96     /* desktop size */
97     buf = getConfigValue(section, "Desktop", "640x480");
98     bufindex = strchr(buf, 'x');
99     *bufindex = '\0';
100     bufindex++;
101     SetWindowText(GetDlgItem(hDlg, IDC_DESKTOP_WIDTH), buf);
102     SetWindowText(GetDlgItem(hDlg, IDC_DESKTOP_HEIGHT), bufindex);
103     free(buf);
104
105     SendDlgItemMessage(hDlg, IDC_SCREEN_DEPTH, CB_RESETCONTENT, 0, 0);
106     SendDlgItemMessage(hDlg, IDC_SCREEN_DEPTH, CB_ADDSTRING, 0, (LPARAM) "8 bit");
107     SendDlgItemMessage(hDlg, IDC_SCREEN_DEPTH, CB_ADDSTRING, 0, (LPARAM) "16 bit");
108     SendDlgItemMessage(hDlg, IDC_SCREEN_DEPTH, CB_ADDSTRING, 0, (LPARAM) "24 bit");
109     SendDlgItemMessage(hDlg, IDC_SCREEN_DEPTH, CB_ADDSTRING, 0, (LPARAM) "32 bit"); /* is this valid? */
110
111     buf = getConfigValue(section, "ScreenDepth", "24");
112     if (strcmp(buf, "8") == 0)
113         SendDlgItemMessage(hDlg, IDC_SCREEN_DEPTH, CB_SETCURSEL, 0, 0);
114     else if (strcmp(buf, "16") == 0)
115         SendDlgItemMessage(hDlg, IDC_SCREEN_DEPTH, CB_SETCURSEL, 1, 0);
116     else if (strcmp(buf, "24") == 0)
117         SendDlgItemMessage(hDlg, IDC_SCREEN_DEPTH, CB_SETCURSEL, 2, 0);
118     else if (strcmp(buf, "32") == 0)
119         SendDlgItemMessage(hDlg, IDC_SCREEN_DEPTH, CB_SETCURSEL, 3, 0);
120     else
121         WINE_ERR("Invalid screen depth read from registry (%s)\n", buf);
122     free(buf);
123
124     SendDlgItemMessage(hDlg, IDC_DESKTOP_WIDTH, EM_LIMITTEXT, RES_MAXLEN, 0);
125     SendDlgItemMessage(hDlg, IDC_DESKTOP_HEIGHT, EM_LIMITTEXT, RES_MAXLEN, 0);
126
127     buf = getConfigValue(section, "DXGrab", "Y");
128     if (IS_OPTION_TRUE(*buf))
129         CheckDlgButton(hDlg, IDC_DX_MOUSE_GRAB, BST_CHECKED);
130     else
131         CheckDlgButton(hDlg, IDC_DX_MOUSE_GRAB, BST_UNCHECKED);
132     free(buf);
133
134     buf = getConfigValue(section, "DesktopDoubleBuffered", "Y");
135     if (IS_OPTION_TRUE(*buf))
136         CheckDlgButton(hDlg, IDC_DOUBLE_BUFFER, BST_CHECKED);
137     else
138         CheckDlgButton(hDlg, IDC_DOUBLE_BUFFER, BST_UNCHECKED);
139     free(buf);
140     
141     buf = getConfigValue(section, "UseTakeFocus", "N");
142     if (IS_OPTION_TRUE(*buf))
143         CheckDlgButton(hDlg, IDC_USE_TAKE_FOCUS, BST_CHECKED);
144     else
145         CheckDlgButton(hDlg, IDC_USE_TAKE_FOCUS, BST_UNCHECKED);
146     free(buf);
147     
148     updatingUI = FALSE;
149 }
150
151
152
153 void setFromDesktopSizeEdits(HWND hDlg) {
154     char *width = malloc(RES_MAXLEN+1);
155     char *height = malloc(RES_MAXLEN+1);
156     char *newStr = malloc((RES_MAXLEN*2) + 2);
157
158     if (updatingUI) return;
159     
160     WINE_TRACE("\n");
161     
162     GetWindowText(GetDlgItem(hDlg, IDC_DESKTOP_WIDTH), width, RES_MAXLEN+1);
163     GetWindowText(GetDlgItem(hDlg, IDC_DESKTOP_HEIGHT), height, RES_MAXLEN+1);
164
165     if (strcmp(width, "") == 0) strcpy(width, "640");
166     if (strcmp(height, "") == 0) strcpy(height, "480");
167     
168     sprintf(newStr, "%sx%s", width, height);
169     addTransaction(section, "Desktop", ACTION_SET, newStr);
170
171     free(width);
172     free(height);
173     free(newStr);
174 }
175
176 void onEnableDesktopClicked(HWND hDlg) {
177     WINE_TRACE("\n");
178     if (IsDlgButtonChecked(hDlg, IDC_ENABLE_DESKTOP) == BST_CHECKED) {
179         /* it was just unchecked, so read the values of the edit boxes, set the config value */
180         setFromDesktopSizeEdits(hDlg);
181     } else {
182         /* it was just checked, so remove the config values */
183         addTransaction(section, "Desktop", ACTION_REMOVE, NULL);
184     }
185     updateGUIForDesktopMode(hDlg);
186 }
187
188 void onScreenDepthChanged(HWND hDlg) {
189     char *newvalue = getDialogItemText(hDlg, IDC_SCREEN_DEPTH);
190     char *spaceIndex = strchr(newvalue, ' ');
191     
192     WINE_TRACE("newvalue=%s\n", newvalue);
193     if (updatingUI) return;
194
195     *spaceIndex = '\0';
196     addTransaction(section, "ScreenDepth", ACTION_SET, newvalue);
197     free(newvalue);
198 }
199
200 void onDXMouseGrabClicked(HWND hDlg) {
201     if (IsDlgButtonChecked(hDlg, IDC_DX_MOUSE_GRAB) == BST_CHECKED)
202         addTransaction(section, "DXGrab", ACTION_SET, "Y");
203     else
204         addTransaction(section, "DXGrab", ACTION_SET, "N");
205 }
206
207
208 void onDoubleBufferClicked(HWND hDlg) {
209     if (IsDlgButtonChecked(hDlg, IDC_DOUBLE_BUFFER) == BST_CHECKED)
210         addTransaction(section, "DesktopDoubleBuffered", ACTION_SET, "Y");
211     else
212         addTransaction(section, "DesktopDoubleBuffered", ACTION_SET, "N");
213 }
214
215 void onUseTakeFocusClicked(HWND hDlg) {
216     if (IsDlgButtonChecked(hDlg, IDC_USE_TAKE_FOCUS) == BST_CHECKED)
217         addTransaction(section, "UseTakeFocus", ACTION_SET, "Y");
218     else
219         addTransaction(section, "UseTakeFocus", ACTION_SET, "N");
220 }
221
222
223 INT_PTR CALLBACK
224 X11DrvDlgProc (HWND hDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
225 {
226     switch (uMsg) {
227         case WM_INITDIALOG:
228             break;
229             
230         case WM_COMMAND:
231             switch(HIWORD(wParam)) {
232                 case EN_CHANGE: {
233                     SendMessage(GetParent(hDlg), PSM_CHANGED, 0, 0);
234                     if ( ((LOWORD(wParam) == IDC_DESKTOP_WIDTH) || (LOWORD(wParam) == IDC_DESKTOP_HEIGHT)) && !updatingUI )
235                         setFromDesktopSizeEdits(hDlg);
236                     break;
237                 }
238                 case BN_CLICKED: {
239                     if (updatingUI) break;
240                     switch(LOWORD(wParam)) {
241                         case IDC_ENABLE_DESKTOP: onEnableDesktopClicked(hDlg); break;
242                         case IDC_DX_MOUSE_GRAB:  onDXMouseGrabClicked(hDlg); break;
243                         case IDC_USE_TAKE_FOCUS: onUseTakeFocusClicked(hDlg); break;
244                         case IDC_DOUBLE_BUFFER:  onDoubleBufferClicked(hDlg); break;
245                     };
246                     break;
247                 }
248                 case CBN_SELCHANGE: {
249                     if (LOWORD(wParam) == IDC_SCREEN_DEPTH) onScreenDepthChanged(hDlg);
250                     break;
251                 }
252                     
253                 default:
254                     break;
255             }
256             break;
257         
258         
259         case WM_NOTIFY:
260             switch (((LPNMHDR)lParam)->code) {
261                 case PSN_KILLACTIVE: {
262                     SetWindowLong(hDlg, DWL_MSGRESULT, FALSE);
263                     break;
264                 }
265                 case PSN_APPLY: {
266                     SetWindowLong(hDlg, DWL_MSGRESULT, PSNRET_NOERROR);
267                     break;
268                 }
269                 case PSN_SETACTIVE: {
270                     initX11DrvDlg (hDlg);
271                     break;
272                 }
273             }
274             break;
275
276         default:
277             break;
278     }
279     return FALSE;
280 }