- switch x11drvdlg to kernel_style, and clean up some superflous code
[wine] / programs / winecfg / x11drvdlg.c
1 /*
2  * Graphics configuration code
3  *
4  * Copyright 2003 Mark Westcott
5  * Copyright 2003-2004 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
39 int updating_ui;
40
41 void update_gui_for_desktop_mode(HWND dialog) {
42     WINE_TRACE("\n");
43
44     updating_ui = TRUE;
45     
46     /* do we have desktop mode enabled? */
47     if (exists(keypath("x11drv"), "Desktop"))
48     {
49         CheckDlgButton(dialog, IDC_ENABLE_DESKTOP, BST_CHECKED);
50         
51         enable(IDC_DESKTOP_WIDTH);
52         enable(IDC_DESKTOP_HEIGHT);
53         enable(IDC_DESKTOP_SIZE);
54         enable(IDC_DESKTOP_BY);
55         
56         SetWindowText(GetDlgItem(dialog, IDC_DESKTOP_WIDTH), "640");
57         SetWindowText(GetDlgItem(dialog, IDC_DESKTOP_HEIGHT), "480");   
58     }
59     else
60     {
61         CheckDlgButton(dialog, IDC_ENABLE_DESKTOP, BST_UNCHECKED);
62         
63         disable(IDC_DESKTOP_WIDTH);
64         disable(IDC_DESKTOP_HEIGHT);
65         disable(IDC_DESKTOP_SIZE);
66         disable(IDC_DESKTOP_BY);
67
68         SetWindowText(GetDlgItem(dialog, IDC_DESKTOP_WIDTH), "");
69         SetWindowText(GetDlgItem(dialog, IDC_DESKTOP_HEIGHT), "");
70     }
71
72     updating_ui = FALSE;
73 }
74
75 static void init_dialog (HWND dialog)
76 {
77     static char *default_desktop = "640x480";
78     char *buf;
79     char *bufindex;
80
81     update_gui_for_desktop_mode(dialog);
82
83     updating_ui = TRUE;
84     
85     /* desktop size */
86     buf = get(keypath("x11drv"), "Desktop", default_desktop);
87     bufindex = strchr(buf, 'x');
88     
89     if(!bufindex) /* handle invalid "Desktop" values */
90     {
91         HeapFree(GetProcessHeap(), 0, buf);
92         buf = strdupA(default_desktop);
93         bufindex = strchr(buf, 'x');
94     }
95     
96     *bufindex = '\0';
97     bufindex++;
98     SetWindowText(GetDlgItem(dialog, IDC_DESKTOP_WIDTH), buf);
99     SetWindowText(GetDlgItem(dialog, IDC_DESKTOP_HEIGHT), bufindex);
100     HeapFree(GetProcessHeap(), 0, buf);
101
102     SendDlgItemMessage(dialog, IDC_SCREEN_DEPTH, CB_RESETCONTENT, 0, 0);
103     SendDlgItemMessage(dialog, IDC_SCREEN_DEPTH, CB_ADDSTRING, 0, (LPARAM) "8 bit");
104     SendDlgItemMessage(dialog, IDC_SCREEN_DEPTH, CB_ADDSTRING, 0, (LPARAM) "16 bit");
105     SendDlgItemMessage(dialog, IDC_SCREEN_DEPTH, CB_ADDSTRING, 0, (LPARAM) "24 bit");
106     SendDlgItemMessage(dialog, IDC_SCREEN_DEPTH, CB_ADDSTRING, 0, (LPARAM) "32 bit"); /* is this valid? */
107
108     buf = get(keypath("x11drv"), "ScreenDepth", "24");
109     if (strcmp(buf, "8") == 0)
110         SendDlgItemMessage(dialog, IDC_SCREEN_DEPTH, CB_SETCURSEL, 0, 0);
111     else if (strcmp(buf, "16") == 0)
112         SendDlgItemMessage(dialog, IDC_SCREEN_DEPTH, CB_SETCURSEL, 1, 0);
113     else if (strcmp(buf, "24") == 0)
114         SendDlgItemMessage(dialog, IDC_SCREEN_DEPTH, CB_SETCURSEL, 2, 0);
115     else if (strcmp(buf, "32") == 0)
116         SendDlgItemMessage(dialog, IDC_SCREEN_DEPTH, CB_SETCURSEL, 3, 0);
117     else
118         WINE_ERR("Invalid screen depth read from registry (%s)\n", buf);
119     HeapFree(GetProcessHeap(), 0, buf);
120
121     SendDlgItemMessage(dialog, IDC_DESKTOP_WIDTH, EM_LIMITTEXT, RES_MAXLEN, 0);
122     SendDlgItemMessage(dialog, IDC_DESKTOP_HEIGHT, EM_LIMITTEXT, RES_MAXLEN, 0);
123
124     buf = get(keypath("x11drv"), "DXGrab", "Y");
125     if (IS_OPTION_TRUE(*buf))
126         CheckDlgButton(dialog, IDC_DX_MOUSE_GRAB, BST_CHECKED);
127     else
128         CheckDlgButton(dialog, IDC_DX_MOUSE_GRAB, BST_UNCHECKED);
129     HeapFree(GetProcessHeap(), 0, buf);
130
131     buf = get(keypath("x11drv"), "DesktopDoubleBuffered", "Y");
132     if (IS_OPTION_TRUE(*buf))
133         CheckDlgButton(dialog, IDC_DOUBLE_BUFFER, BST_CHECKED);
134     else
135         CheckDlgButton(dialog, IDC_DOUBLE_BUFFER, BST_UNCHECKED);
136     HeapFree(GetProcessHeap(), 0, buf);
137     
138     updating_ui = FALSE;
139 }
140
141 static void set_from_desktop_edits(HWND dialog) {
142     char *width, *height, *new;
143
144     if (updating_ui) return;
145     
146     WINE_TRACE("\n");
147
148     width = get_control_text(dialog, IDC_DESKTOP_WIDTH);
149     height = get_control_text(dialog, IDC_DESKTOP_HEIGHT);
150
151     if (strcmp(width, "") == 0)
152     {
153         HeapFree(GetProcessHeap(), 0, width);
154         width = strdupA("640");
155     }
156     
157     if (strcmp(height, "") == 0)
158     {
159         HeapFree(GetProcessHeap(), 0, height);
160         height = strdupA("480");
161     }
162
163     new = HeapAlloc(GetProcessHeap(), 0, strlen(width) + strlen(height) + 2 /* x + terminator */);
164     sprintf(new, "%sx%s", width, height);
165     set(keypath("x11drv"), "Desktop", new);
166     
167     HeapFree(GetProcessHeap(), 0, width);
168     HeapFree(GetProcessHeap(), 0, height);
169     HeapFree(GetProcessHeap(), 0, new);
170 }
171
172 void on_enable_desktop_clicked(HWND dialog) {
173     WINE_TRACE("\n");
174     
175     if (IsDlgButtonChecked(dialog, IDC_ENABLE_DESKTOP) == BST_CHECKED) {
176         /* it was just unchecked, so read the values of the edit boxes, set the config value */
177         set_from_desktop_edits(dialog);
178     } else {
179         /* it was just checked, so remove the config values */
180         set(keypath("x11drv"), "Desktop", NULL);
181     }
182     
183     update_gui_for_desktop_mode(dialog);
184 }
185
186 static void on_screen_depth_changed(HWND dialog) {
187     char *newvalue = get_control_text(dialog, IDC_SCREEN_DEPTH);
188     char *spaceIndex = strchr(newvalue, ' ');
189     
190     WINE_TRACE("newvalue=%s\n", newvalue);
191     if (updating_ui) return;
192
193     *spaceIndex = '\0';
194     set(keypath("x11drv"), "ScreenDepth", newvalue);
195     HeapFree(GetProcessHeap(), 0, newvalue);
196 }
197
198 static void on_dx_mouse_grab_clicked(HWND dialog) {
199     if (IsDlgButtonChecked(dialog, IDC_DX_MOUSE_GRAB) == BST_CHECKED)
200         set(keypath("x11drv"), "DXGrab", "Y");
201     else
202         set(keypath("x11drv"), "DXGrab", "N");
203 }
204
205
206 static void on_double_buffer_clicked(HWND dialog) {
207     if (IsDlgButtonChecked(dialog, IDC_DOUBLE_BUFFER) == BST_CHECKED)
208         set(keypath("x11drv"), "DesktopDoubleBuffered", "Y");
209     else
210         set(keypath("x11drv"), "DesktopDoubleBuffered", "N");
211 }
212
213 INT_PTR CALLBACK
214 GraphDlgProc (HWND hDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
215 {
216     switch (uMsg) {
217         case WM_INITDIALOG:
218             break;
219
220         case WM_SHOWWINDOW:
221             set_window_title(hDlg);
222             break;
223             
224         case WM_COMMAND:
225             switch(HIWORD(wParam)) {
226                 case EN_CHANGE: {
227                     SendMessage(GetParent(hDlg), PSM_CHANGED, 0, 0);
228                     if ( ((LOWORD(wParam) == IDC_DESKTOP_WIDTH) || (LOWORD(wParam) == IDC_DESKTOP_HEIGHT)) && !updating_ui )
229                         set_from_desktop_edits(hDlg);
230                     break;
231                 }
232                 case BN_CLICKED: {
233                     if (updating_ui) break;
234                     switch(LOWORD(wParam)) {
235                         case IDC_ENABLE_DESKTOP: on_enable_desktop_clicked(hDlg); break;
236                         case IDC_DX_MOUSE_GRAB:  on_dx_mouse_grab_clicked(hDlg); break;
237                         case IDC_DOUBLE_BUFFER:  on_double_buffer_clicked(hDlg); break;
238                     }
239                     break;
240                 }
241                 case CBN_SELCHANGE: {
242                     if (LOWORD(wParam) == IDC_SCREEN_DEPTH) on_screen_depth_changed(hDlg);
243                     break;
244                 }
245                     
246                 default:
247                     break;
248             }
249             break;
250         
251         
252         case WM_NOTIFY:
253             switch (((LPNMHDR)lParam)->code) {
254                 case PSN_KILLACTIVE: {
255                     SetWindowLong(hDlg, DWL_MSGRESULT, FALSE);
256                     break;
257                 }
258                 case PSN_APPLY: {
259                     apply();
260                     SetWindowLong(hDlg, DWL_MSGRESULT, PSNRET_NOERROR);
261                     break;
262                 }
263                 case PSN_SETACTIVE: {
264                     init_dialog (hDlg);
265                     break;
266                 }
267             }
268             break;
269
270         default:
271             break;
272     }
273     return FALSE;
274 }