winecfg: Add trackbar to set screen resolution in graphics tab.
[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", (const char *)MAXDPI);
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     dwLogpixels = read_logpixels_reg();
261     WINE_TRACE("%d\n", (int) dwLogpixels);
262
263     szLogpixels[0] = 0;
264     sprintf(szLogpixels, "%d", dwLogpixels);
265     SendMessage(hDpiEditBox, WM_SETTEXT, 0, (LPARAM) szLogpixels);
266 }
267
268 static void init_trackbar(HWND hDlg)
269 {
270     HWND hTrackBar = GetDlgItem(hDlg, IDC_RES_TRACKBAR);
271     DWORD dwLogpixels;
272
273     dwLogpixels = read_logpixels_reg();
274
275     SendMessageW(hTrackBar, TBM_SETRANGE, TRUE, MAKELONG(MINDPI, MAXDPI));
276     SendMessageW(hTrackBar, TBM_SETPOS, TRUE, dwLogpixels);
277 }
278
279 INT_PTR CALLBACK
280 GraphDlgProc(HWND hDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
281 {
282     switch (uMsg) {
283         case WM_INITDIALOG:
284             init_dpi_editbox(hDlg);
285             init_trackbar(hDlg);
286             break;
287
288         case WM_SHOWWINDOW:
289             set_window_title(hDlg);
290             break;
291             
292         case WM_COMMAND:
293             switch(HIWORD(wParam)) {
294                 case EN_CHANGE: {
295                     if (updating_ui) break;
296                     SendMessage(GetParent(hDlg), PSM_CHANGED, 0, 0);
297                     if ( ((LOWORD(wParam) == IDC_DESKTOP_WIDTH) || (LOWORD(wParam) == IDC_DESKTOP_HEIGHT)) && !updating_ui )
298                         set_from_desktop_edits(hDlg);
299                     break;
300                 }
301                 case BN_CLICKED: {
302                     if (updating_ui) break;
303                     SendMessage(GetParent(hDlg), PSM_CHANGED, 0, 0);
304                     switch(LOWORD(wParam)) {
305                         case IDC_ENABLE_DESKTOP: on_enable_desktop_clicked(hDlg); break;
306                         case IDC_ENABLE_MANAGED: on_enable_managed_clicked(hDlg); break;
307                         case IDC_DX_MOUSE_GRAB:  on_dx_mouse_grab_clicked(hDlg); break;
308                         case IDC_D3D_PSHADER_MODE: on_d3d_pshader_mode_clicked(hDlg); break;
309                     }
310                     break;
311                 }
312                 case CBN_SELCHANGE: {
313                     SendMessage(GetParent(hDlg), PSM_CHANGED, 0, 0);
314                     switch (LOWORD(wParam)) {
315                     case IDC_D3D_VSHADER_MODE: on_d3d_vshader_mode_changed(hDlg); break;
316                     }
317                     break;
318                 }
319                     
320                 default:
321                     break;
322             }
323             break;
324         
325         
326         case WM_NOTIFY:
327             switch (((LPNMHDR)lParam)->code) {
328                 case PSN_KILLACTIVE: {
329                     SetWindowLongPtr(hDlg, DWLP_MSGRESULT, FALSE);
330                     break;
331                 }
332                 case PSN_APPLY: {
333                     int i = SendMessageW(GetDlgItem(hDlg, IDC_RES_TRACKBAR), TBM_GETPOS, 0, 0);
334                     set_reg_key_dword(HKEY_LOCAL_MACHINE, logpixels_reg, "LogPixels", i);
335                     apply();
336                     SetWindowLongPtr(hDlg, DWLP_MSGRESULT, PSNRET_NOERROR);
337                     break;
338                 }
339                 case PSN_SETACTIVE: {
340                     init_dialog (hDlg);
341                     break;
342                 }
343             }
344             break;
345
346         case WM_HSCROLL:
347             switch (wParam) {
348                 default: {
349                     char buf[MAXBUFLEN];
350                     int i = SendMessageW(GetDlgItem(hDlg, IDC_RES_TRACKBAR), TBM_GETPOS, 0, 0);
351                     buf[0] = 0;
352                     sprintf(buf, "%d", i);
353                     SendMessage(GetDlgItem(hDlg, IDC_RES_DPIEDIT), WM_SETTEXT, 0, (LPARAM) buf);
354                     break;
355                 }
356             }
357             break;
358
359         default:
360             break;
361     }
362     return FALSE;
363 }