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