winecfg: Fix apply button always available in graphics tab bug.
[wine] / programs / winecfg / x11drvdlg.c
1 /*
2  * Graphics configuration code
3  *
4  * Copyright 2003 Mark Westcott
5  * Copyright 2003-2004 Mike Hearn
6  * Copyright 2005 Raphael Junqueira
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with this library; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21  *
22  */
23
24 #define WIN32_LEAN_AND_MEAN
25
26 #include <stdarg.h>
27 #include <stdlib.h>
28 #include <stdio.h>
29
30 #include <windows.h>
31 #include <wine/debug.h>
32
33 #include "resource.h"
34 #include "winecfg.h"
35
36 WINE_DEFAULT_DEBUG_CHANNEL(winecfg);
37
38 #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? */
39 #define MINDPI 96
40 #define MAXDPI 120
41 #define DEFDPI 96
42
43 static const char logpixels_reg[] = "System\\CurrentControlSet\\Hardware Profiles\\Current\\Software\\Fonts";
44
45 static struct SHADERMODE
46 {
47   UINT displayStrID;
48   const char* settingStr;
49 } const D3D_VS_Modes[] = {
50   {IDS_SHADER_MODE_HARDWARE,  "hardware"},
51   {IDS_SHADER_MODE_NONE,      "none"},
52   {0, 0}
53 };
54
55
56 int updating_ui;
57
58 static void update_gui_for_desktop_mode(HWND dialog) {
59     int desktopenabled = FALSE;
60
61     WINE_TRACE("\n");
62     updating_ui = TRUE;
63
64     if (current_app)
65     {
66         disable(IDC_ENABLE_DESKTOP);
67         disable(IDC_DESKTOP_WIDTH);
68         disable(IDC_DESKTOP_HEIGHT);
69         disable(IDC_DESKTOP_SIZE);
70         disable(IDC_DESKTOP_BY);
71         return;
72     }
73     enable(IDC_ENABLE_DESKTOP);
74
75     /* do we have desktop mode enabled? */
76     if (reg_key_exists(config_key, keypath("X11 Driver"), "Desktop"))
77     {
78         char* buf, *bufindex;
79         CheckDlgButton(dialog, IDC_ENABLE_DESKTOP, BST_CHECKED);
80
81         buf = get_reg_key(config_key, keypath("X11 Driver"), "Desktop", "640x480");
82         /* note: this test must match the one in x11drv */
83         if( buf[0] != 'n' &&  buf[0] != 'N' &&  buf[0] != 'F' &&  buf[0] != 'f'
84                 &&  buf[0] != '0') {
85             desktopenabled = TRUE;
86             enable(IDC_DESKTOP_WIDTH);
87             enable(IDC_DESKTOP_HEIGHT);
88             enable(IDC_DESKTOP_SIZE);
89             enable(IDC_DESKTOP_BY);
90
91             bufindex = strchr(buf, 'x');
92             if (bufindex) {
93                 *bufindex = 0;
94                 ++bufindex;
95                 SetWindowText(GetDlgItem(dialog, IDC_DESKTOP_WIDTH), buf);
96                 SetWindowText(GetDlgItem(dialog, IDC_DESKTOP_HEIGHT), bufindex);
97             } else {
98                 WINE_TRACE("Desktop registry entry is malformed\n");
99                 SetWindowText(GetDlgItem(dialog, IDC_DESKTOP_WIDTH), "640");
100                 SetWindowText(GetDlgItem(dialog, IDC_DESKTOP_HEIGHT), "480");
101             }
102         }
103         HeapFree(GetProcessHeap(), 0, buf);
104     }
105     if (!desktopenabled)
106     {
107         CheckDlgButton(dialog, IDC_ENABLE_DESKTOP, BST_UNCHECKED);
108         
109         disable(IDC_DESKTOP_WIDTH);
110         disable(IDC_DESKTOP_HEIGHT);
111         disable(IDC_DESKTOP_SIZE);
112         disable(IDC_DESKTOP_BY);
113
114         SetWindowText(GetDlgItem(dialog, IDC_DESKTOP_WIDTH), "");
115         SetWindowText(GetDlgItem(dialog, IDC_DESKTOP_HEIGHT), "");
116     }
117
118     updating_ui = FALSE;
119 }
120
121 static void init_dialog(HWND dialog)
122 {
123     unsigned int it;
124     char* buf;
125
126     update_gui_for_desktop_mode(dialog);
127
128     updating_ui = TRUE;
129     
130     SendDlgItemMessage(dialog, IDC_DESKTOP_WIDTH, EM_LIMITTEXT, RES_MAXLEN, 0);
131     SendDlgItemMessage(dialog, IDC_DESKTOP_HEIGHT, EM_LIMITTEXT, RES_MAXLEN, 0);
132
133     buf = get_reg_key(config_key, keypath("X11 Driver"), "DXGrab", "N");
134     if (IS_OPTION_TRUE(*buf))
135         CheckDlgButton(dialog, IDC_DX_MOUSE_GRAB, BST_CHECKED);
136     else
137         CheckDlgButton(dialog, IDC_DX_MOUSE_GRAB, BST_UNCHECKED);
138     HeapFree(GetProcessHeap(), 0, buf);
139
140     buf = get_reg_key(config_key, keypath("X11 Driver"), "Managed", "Y");
141     if (IS_OPTION_TRUE(*buf))
142         CheckDlgButton(dialog, IDC_ENABLE_MANAGED, BST_CHECKED);
143     else
144         CheckDlgButton(dialog, IDC_ENABLE_MANAGED, BST_UNCHECKED);
145     HeapFree(GetProcessHeap(), 0, buf);
146
147     SendDlgItemMessage(dialog, IDC_D3D_VSHADER_MODE, CB_RESETCONTENT, 0, 0);
148     for (it = 0; 0 != D3D_VS_Modes[it].displayStrID; ++it) {
149       SendDlgItemMessageW (dialog, IDC_D3D_VSHADER_MODE, CB_ADDSTRING, 0,
150           (LPARAM)load_string (D3D_VS_Modes[it].displayStrID));
151     }  
152     buf = get_reg_key(config_key, keypath("Direct3D"), "VertexShaderMode", "hardware"); 
153     for (it = 0; NULL != D3D_VS_Modes[it].settingStr; ++it) {
154       if (strcmp(buf, D3D_VS_Modes[it].settingStr) == 0) {
155         SendDlgItemMessage(dialog, IDC_D3D_VSHADER_MODE, CB_SETCURSEL, it, 0);
156         break ;
157       }
158     }
159     if (NULL == D3D_VS_Modes[it].settingStr) {
160       WINE_ERR("Invalid Direct3D VertexShader Mode read from registry (%s)\n", buf);
161     }
162     HeapFree(GetProcessHeap(), 0, buf);
163
164     buf = get_reg_key(config_key, keypath("Direct3D"), "PixelShaderMode", "enabled");
165     if (!strcmp(buf, "enabled"))
166       CheckDlgButton(dialog, IDC_D3D_PSHADER_MODE, BST_CHECKED);
167     else
168       CheckDlgButton(dialog, IDC_D3D_PSHADER_MODE, BST_UNCHECKED);
169     HeapFree(GetProcessHeap(), 0, buf);
170
171     updating_ui = FALSE;
172 }
173
174 static void set_from_desktop_edits(HWND dialog) {
175     char *width, *height, *new;
176
177     if (updating_ui) return;
178     
179     WINE_TRACE("\n");
180
181     width = get_text(dialog, IDC_DESKTOP_WIDTH);
182     height = get_text(dialog, IDC_DESKTOP_HEIGHT);
183
184     if (width == NULL || strcmp(width, "") == 0) {
185         HeapFree(GetProcessHeap(), 0, width);
186         width = strdupA("640");
187     }
188     
189     if (height == NULL || strcmp(height, "") == 0) {
190         HeapFree(GetProcessHeap(), 0, height);
191         height = strdupA("480");
192     }
193
194     new = HeapAlloc(GetProcessHeap(), 0, strlen(width) + strlen(height) + 2 /* x + terminator */);
195     sprintf(new, "%sx%s", width, height);
196     set_reg_key(config_key, keypath("X11 Driver"), "Desktop", new);
197     
198     HeapFree(GetProcessHeap(), 0, width);
199     HeapFree(GetProcessHeap(), 0, height);
200     HeapFree(GetProcessHeap(), 0, new);
201 }
202
203 static void on_enable_desktop_clicked(HWND dialog) {
204     WINE_TRACE("\n");
205     
206     if (IsDlgButtonChecked(dialog, IDC_ENABLE_DESKTOP) == BST_CHECKED) {
207         set_from_desktop_edits(dialog);
208     } else {
209         set_reg_key(config_key, keypath("X11 Driver"), "Desktop", NULL);
210     }
211     
212     update_gui_for_desktop_mode(dialog);
213 }
214
215 static void on_enable_managed_clicked(HWND dialog) {
216     WINE_TRACE("\n");
217     
218     if (IsDlgButtonChecked(dialog, IDC_ENABLE_MANAGED) == BST_CHECKED) {
219         set_reg_key(config_key, keypath("X11 Driver"), "Managed", "Y");
220     } else {
221         set_reg_key(config_key, keypath("X11 Driver"), "Managed", "N");
222     }
223 }
224
225 static void on_dx_mouse_grab_clicked(HWND dialog) {
226     if (IsDlgButtonChecked(dialog, IDC_DX_MOUSE_GRAB) == BST_CHECKED) 
227         set_reg_key(config_key, keypath("X11 Driver"), "DXGrab", "Y");
228     else
229         set_reg_key(config_key, keypath("X11 Driver"), "DXGrab", "N");
230 }
231
232 static void on_d3d_vshader_mode_changed(HWND dialog) {
233   int selected_mode = SendDlgItemMessage(dialog, IDC_D3D_VSHADER_MODE, CB_GETCURSEL, 0, 0);  
234   set_reg_key(config_key, keypath("Direct3D"), "VertexShaderMode",
235       D3D_VS_Modes[selected_mode].settingStr); 
236 }
237
238 static void on_d3d_pshader_mode_clicked(HWND dialog) {
239     if (IsDlgButtonChecked(dialog, IDC_D3D_PSHADER_MODE) == BST_CHECKED)
240         set_reg_key(config_key, keypath("Direct3D"), "PixelShaderMode", "enabled");
241     else
242         set_reg_key(config_key, keypath("Direct3D"), "PixelShaderMode", "disabled");
243 }
244 static INT read_logpixels_reg(void)
245 {
246     DWORD dwLogPixels;
247     char *buf  = get_reg_key(HKEY_LOCAL_MACHINE, logpixels_reg,
248                              "LogPixels", NULL);
249     dwLogPixels = *buf;
250     HeapFree(GetProcessHeap(), 0, buf);
251     return dwLogPixels;
252 }
253
254 static void init_dpi_editbox(HWND hDlg)
255 {
256     HWND hDpiEditBox = GetDlgItem(hDlg, IDC_RES_DPIEDIT);
257     DWORD dwLogpixels;
258     char szLogpixels[MAXBUFLEN];
259
260     updating_ui = TRUE;
261
262     dwLogpixels = read_logpixels_reg();
263     WINE_TRACE("%d\n", (int) dwLogpixels);
264
265     szLogpixels[0] = 0;
266     sprintf(szLogpixels, "%d", dwLogpixels);
267     SendMessage(hDpiEditBox, WM_SETTEXT, 0, (LPARAM) szLogpixels);
268
269     updating_ui = FALSE;
270 }
271
272 static void init_trackbar(HWND hDlg)
273 {
274     HWND hTrackBar = GetDlgItem(hDlg, IDC_RES_TRACKBAR);
275     DWORD dwLogpixels;
276
277     updating_ui = TRUE;
278
279     dwLogpixels = read_logpixels_reg();
280
281     SendMessageW(hTrackBar, TBM_SETRANGE, TRUE, MAKELONG(MINDPI, MAXDPI));
282     SendMessageW(hTrackBar, TBM_SETPOS, TRUE, dwLogpixels);
283
284     updating_ui = FALSE;
285 }
286
287 INT_PTR CALLBACK
288 GraphDlgProc(HWND hDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
289 {
290     switch (uMsg) {
291         case WM_INITDIALOG:
292             init_dpi_editbox(hDlg);
293             init_trackbar(hDlg);
294             break;
295
296         case WM_SHOWWINDOW:
297             set_window_title(hDlg);
298             break;
299             
300         case WM_COMMAND:
301             switch(HIWORD(wParam)) {
302                 case EN_CHANGE: {
303                     if (updating_ui) break;
304                     SendMessage(GetParent(hDlg), PSM_CHANGED, 0, 0);
305                     if ( ((LOWORD(wParam) == IDC_DESKTOP_WIDTH) || (LOWORD(wParam) == IDC_DESKTOP_HEIGHT)) && !updating_ui )
306                         set_from_desktop_edits(hDlg);
307                     break;
308                 }
309                 case BN_CLICKED: {
310                     if (updating_ui) break;
311                     SendMessage(GetParent(hDlg), PSM_CHANGED, 0, 0);
312                     switch(LOWORD(wParam)) {
313                         case IDC_ENABLE_DESKTOP: on_enable_desktop_clicked(hDlg); break;
314                         case IDC_ENABLE_MANAGED: on_enable_managed_clicked(hDlg); break;
315                         case IDC_DX_MOUSE_GRAB:  on_dx_mouse_grab_clicked(hDlg); break;
316                         case IDC_D3D_PSHADER_MODE: on_d3d_pshader_mode_clicked(hDlg); break;
317                     }
318                     break;
319                 }
320                 case CBN_SELCHANGE: {
321                     SendMessage(GetParent(hDlg), PSM_CHANGED, 0, 0);
322                     switch (LOWORD(wParam)) {
323                     case IDC_D3D_VSHADER_MODE: on_d3d_vshader_mode_changed(hDlg); break;
324                     }
325                     break;
326                 }
327                     
328                 default:
329                     break;
330             }
331             break;
332         
333         
334         case WM_NOTIFY:
335             switch (((LPNMHDR)lParam)->code) {
336                 case PSN_KILLACTIVE: {
337                     SetWindowLongPtr(hDlg, DWLP_MSGRESULT, FALSE);
338                     break;
339                 }
340                 case PSN_APPLY: {
341                     apply();
342                     SetWindowLongPtr(hDlg, DWLP_MSGRESULT, PSNRET_NOERROR);
343                     break;
344                 }
345                 case PSN_SETACTIVE: {
346                     init_dialog (hDlg);
347                     break;
348                 }
349             }
350             break;
351
352         case WM_HSCROLL:
353             switch (wParam) {
354                 default: {
355                     char buf[MAXBUFLEN];
356                     int i = SendMessageW(GetDlgItem(hDlg, IDC_RES_TRACKBAR), TBM_GETPOS, 0, 0);
357                     buf[0] = 0;
358                     sprintf(buf, "%d", i);
359                     SendMessage(GetDlgItem(hDlg, IDC_RES_DPIEDIT), WM_SETTEXT, 0, (LPARAM) buf);
360                     set_reg_key_dword(HKEY_LOCAL_MACHINE, logpixels_reg, "LogPixels", i);
361                     break;
362                 }
363             }
364             break;
365
366         default:
367             break;
368     }
369     return FALSE;
370 }