user32: Implement GetWindowModuleFileName.
[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/unicode.h>
32 #include <wine/debug.h>
33
34 #include "resource.h"
35 #include "winecfg.h"
36
37 WINE_DEFAULT_DEBUG_CHANNEL(winecfg);
38
39 #define RES_MAXLEN 5 /* max number of digits in a screen dimension. 5 digits should be plenty */
40 #define MINDPI 96
41 #define MAXDPI 480
42 #define DEFDPI 96
43
44 #define IDT_DPIEDIT 0x1234
45
46 static const WCHAR logpixels_reg[] = {'S','y','s','t','e','m','\\','C','u','r','r','e','n','t','C','o','n','t','r','o','l','S','e','t','\\','H','a','r','d','w','a','r','e',' ','P','r','o','f','i','l','e','s','\\','C','u','r','r','e','n','t','\\','S','o','f','t','w','a','r','e','\\','F','o','n','t','s',0};
47 static const WCHAR logpixels[] = {'L','o','g','P','i','x','e','l','s',0};
48
49 static const WCHAR desktopW[] = {'D','e','s','k','t','o','p',0};
50 static const WCHAR defaultW[] = {'D','e','f','a','u','l','t',0};
51 static const WCHAR explorerW[] = {'E','x','p','l','o','r','e','r',0};
52 static const WCHAR explorer_desktopsW[] = {'E','x','p','l','o','r','e','r','\\',
53                                            'D','e','s','k','t','o','p','s',0};
54 static const WCHAR x11_driverW[] = {'X','1','1',' ','D','r','i','v','e','r',0};
55 static const WCHAR default_resW[] = {'8','0','0','x','6','0','0',0};
56
57
58 static struct SHADERMODE
59 {
60   UINT displayStrID;
61   const char* settingStr;
62 } const D3D_VS_Modes[] = {
63   {IDS_SHADER_MODE_HARDWARE,  "hardware"},
64   {IDS_SHADER_MODE_NONE,      "none"},
65   {0, 0}
66 };
67
68
69 int updating_ui;
70
71 /* convert the x11 desktop key to the new explorer config */
72 static void convert_x11_desktop_key(void)
73 {
74     char *buf;
75
76     if (!(buf = get_reg_key(config_key, "X11 Driver", "Desktop", NULL))) return;
77     set_reg_key(config_key, "Explorer\\Desktops", "Default", buf);
78     set_reg_key(config_key, "Explorer", "Desktop", "Default");
79     set_reg_key(config_key, "X11 Driver", "Desktop", NULL);
80     HeapFree(GetProcessHeap(), 0, buf);
81 }
82
83 static void update_gui_for_desktop_mode(HWND dialog)
84 {
85     WCHAR *buf, *bufindex;
86     const WCHAR *desktop_name = current_app ? current_app : defaultW;
87
88     WINE_TRACE("\n");
89     updating_ui = TRUE;
90
91     buf = get_reg_keyW(config_key, explorer_desktopsW, desktop_name, NULL);
92     if (buf && (bufindex = strchrW(buf, 'x')))
93     {
94         *bufindex = 0;
95         ++bufindex;
96         SetWindowTextW(GetDlgItem(dialog, IDC_DESKTOP_WIDTH), buf);
97         SetWindowTextW(GetDlgItem(dialog, IDC_DESKTOP_HEIGHT), bufindex);
98     } else {
99         SetWindowText(GetDlgItem(dialog, IDC_DESKTOP_WIDTH), "800");
100         SetWindowText(GetDlgItem(dialog, IDC_DESKTOP_HEIGHT), "600");
101     }
102     HeapFree(GetProcessHeap(), 0, buf);
103
104     /* do we have desktop mode enabled? */
105     if (reg_key_exists(config_key, keypath("Explorer"), "Desktop"))
106     {
107         CheckDlgButton(dialog, IDC_ENABLE_DESKTOP, BST_CHECKED);
108         enable(IDC_DESKTOP_WIDTH);
109         enable(IDC_DESKTOP_HEIGHT);
110         enable(IDC_DESKTOP_SIZE);
111         enable(IDC_DESKTOP_BY);
112     }
113     else
114     {
115         CheckDlgButton(dialog, IDC_ENABLE_DESKTOP, BST_UNCHECKED);
116         disable(IDC_DESKTOP_WIDTH);
117         disable(IDC_DESKTOP_HEIGHT);
118         disable(IDC_DESKTOP_SIZE);
119         disable(IDC_DESKTOP_BY);
120     }
121
122     updating_ui = FALSE;
123 }
124
125 static void init_dialog(HWND dialog)
126 {
127     unsigned int it;
128     char* buf;
129
130     convert_x11_desktop_key();
131     update_gui_for_desktop_mode(dialog);
132
133     updating_ui = TRUE;
134     
135     SendDlgItemMessage(dialog, IDC_DESKTOP_WIDTH, EM_LIMITTEXT, RES_MAXLEN, 0);
136     SendDlgItemMessage(dialog, IDC_DESKTOP_HEIGHT, EM_LIMITTEXT, RES_MAXLEN, 0);
137
138     buf = get_reg_key(config_key, keypath("X11 Driver"), "DXGrab", "N");
139     if (IS_OPTION_TRUE(*buf))
140         CheckDlgButton(dialog, IDC_DX_MOUSE_GRAB, BST_CHECKED);
141     else
142         CheckDlgButton(dialog, IDC_DX_MOUSE_GRAB, BST_UNCHECKED);
143     HeapFree(GetProcessHeap(), 0, buf);
144
145     buf = get_reg_key(config_key, keypath("X11 Driver"), "Managed", "Y");
146     if (IS_OPTION_TRUE(*buf))
147         CheckDlgButton(dialog, IDC_ENABLE_MANAGED, BST_CHECKED);
148     else
149         CheckDlgButton(dialog, IDC_ENABLE_MANAGED, BST_UNCHECKED);
150     HeapFree(GetProcessHeap(), 0, buf);
151
152     buf = get_reg_key(config_key, keypath("X11 Driver"), "Decorated", "Y");
153     if (IS_OPTION_TRUE(*buf))
154         CheckDlgButton(dialog, IDC_ENABLE_DECORATED, BST_CHECKED);
155     else
156         CheckDlgButton(dialog, IDC_ENABLE_DECORATED, BST_UNCHECKED);
157     HeapFree(GetProcessHeap(), 0, buf);
158
159
160     SendDlgItemMessage(dialog, IDC_D3D_VSHADER_MODE, CB_RESETCONTENT, 0, 0);
161     for (it = 0; 0 != D3D_VS_Modes[it].displayStrID; ++it) {
162       SendDlgItemMessageW (dialog, IDC_D3D_VSHADER_MODE, CB_ADDSTRING, 0,
163           (LPARAM)load_string (D3D_VS_Modes[it].displayStrID));
164     }  
165     buf = get_reg_key(config_key, keypath("Direct3D"), "VertexShaderMode", "hardware"); 
166     for (it = 0; NULL != D3D_VS_Modes[it].settingStr; ++it) {
167       if (strcmp(buf, D3D_VS_Modes[it].settingStr) == 0) {
168         SendDlgItemMessage(dialog, IDC_D3D_VSHADER_MODE, CB_SETCURSEL, it, 0);
169         break ;
170       }
171     }
172     if (NULL == D3D_VS_Modes[it].settingStr) {
173       WINE_ERR("Invalid Direct3D VertexShader Mode read from registry (%s)\n", buf);
174     }
175     HeapFree(GetProcessHeap(), 0, buf);
176
177     buf = get_reg_key(config_key, keypath("Direct3D"), "PixelShaderMode", "enabled");
178     if (!strcmp(buf, "enabled"))
179       CheckDlgButton(dialog, IDC_D3D_PSHADER_MODE, BST_CHECKED);
180     else
181       CheckDlgButton(dialog, IDC_D3D_PSHADER_MODE, BST_UNCHECKED);
182     HeapFree(GetProcessHeap(), 0, buf);
183
184     updating_ui = FALSE;
185 }
186
187 static void set_from_desktop_edits(HWND dialog)
188 {
189     static const WCHAR x[] = {'x',0};
190     static const WCHAR def_width[]  = {'8','0','0',0};
191     static const WCHAR def_height[] = {'6','0','0',0};
192     WCHAR *width, *height, *new;
193     const WCHAR *desktop_name = current_app ? current_app : defaultW;
194
195     if (updating_ui) return;
196     
197     WINE_TRACE("\n");
198
199     width = get_textW(dialog, IDC_DESKTOP_WIDTH);
200     height = get_textW(dialog, IDC_DESKTOP_HEIGHT);
201
202     if (!width || !width[0]) {
203         HeapFree(GetProcessHeap(), 0, width);
204         width = strdupW(def_width);
205     }
206     if (!height || !height[0]) {
207         HeapFree(GetProcessHeap(), 0, height);
208         height = strdupW(def_height);
209     }
210
211     new = HeapAlloc(GetProcessHeap(), 0, (strlenW(width) + strlenW(height) + 2) * sizeof(WCHAR));
212     strcpyW( new, width );
213     strcatW( new, x );
214     strcatW( new, height );
215     set_reg_keyW(config_key, explorer_desktopsW, desktop_name, new);
216     set_reg_keyW(config_key, keypathW(explorerW), desktopW, desktop_name);
217
218     HeapFree(GetProcessHeap(), 0, width);
219     HeapFree(GetProcessHeap(), 0, height);
220     HeapFree(GetProcessHeap(), 0, new);
221 }
222
223 static void on_enable_desktop_clicked(HWND dialog) {
224     WINE_TRACE("\n");
225     
226     if (IsDlgButtonChecked(dialog, IDC_ENABLE_DESKTOP) == BST_CHECKED) {
227         set_from_desktop_edits(dialog);
228     } else {
229         set_reg_key(config_key, keypath("Explorer"), "Desktop", NULL);
230     }
231     
232     update_gui_for_desktop_mode(dialog);
233 }
234
235 static void on_enable_managed_clicked(HWND dialog) {
236     WINE_TRACE("\n");
237     
238     if (IsDlgButtonChecked(dialog, IDC_ENABLE_MANAGED) == BST_CHECKED) {
239         set_reg_key(config_key, keypath("X11 Driver"), "Managed", "Y");
240     } else {
241         set_reg_key(config_key, keypath("X11 Driver"), "Managed", "N");
242     }
243 }
244
245 static void on_enable_decorated_clicked(HWND dialog) {
246     WINE_TRACE("\n");
247
248     if (IsDlgButtonChecked(dialog, IDC_ENABLE_DECORATED) == BST_CHECKED) {
249         set_reg_key(config_key, keypath("X11 Driver"), "Decorated", "Y");
250     } else {
251         set_reg_key(config_key, keypath("X11 Driver"), "Decorated", "N");
252     }
253 }
254
255 static void on_dx_mouse_grab_clicked(HWND dialog) {
256     if (IsDlgButtonChecked(dialog, IDC_DX_MOUSE_GRAB) == BST_CHECKED) 
257         set_reg_key(config_key, keypath("X11 Driver"), "DXGrab", "Y");
258     else
259         set_reg_key(config_key, keypath("X11 Driver"), "DXGrab", "N");
260 }
261
262 static void on_d3d_vshader_mode_changed(HWND dialog) {
263   int selected_mode = SendDlgItemMessage(dialog, IDC_D3D_VSHADER_MODE, CB_GETCURSEL, 0, 0);  
264   set_reg_key(config_key, keypath("Direct3D"), "VertexShaderMode",
265       D3D_VS_Modes[selected_mode].settingStr); 
266 }
267
268 static void on_d3d_pshader_mode_clicked(HWND dialog) {
269     if (IsDlgButtonChecked(dialog, IDC_D3D_PSHADER_MODE) == BST_CHECKED)
270         set_reg_key(config_key, keypath("Direct3D"), "PixelShaderMode", "enabled");
271     else
272         set_reg_key(config_key, keypath("Direct3D"), "PixelShaderMode", "disabled");
273 }
274 static INT read_logpixels_reg(void)
275 {
276     DWORD dwLogPixels;
277     WCHAR *buf = get_reg_keyW(HKEY_LOCAL_MACHINE, logpixels_reg, logpixels, NULL);
278     dwLogPixels = buf ? *buf : DEFDPI;
279     HeapFree(GetProcessHeap(), 0, buf);
280     return dwLogPixels;
281 }
282
283 static void init_dpi_editbox(HWND hDlg)
284 {
285     DWORD dwLogpixels;
286     char szLogpixels[MAXBUFLEN];
287
288     updating_ui = TRUE;
289
290     dwLogpixels = read_logpixels_reg();
291     WINE_TRACE("%u\n", dwLogpixels);
292
293     sprintf(szLogpixels, "%u", dwLogpixels);
294     SetDlgItemText(hDlg, IDC_RES_DPIEDIT, szLogpixels);
295
296     updating_ui = FALSE;
297 }
298
299 static void init_trackbar(HWND hDlg)
300 {
301     HWND hTrackBar = GetDlgItem(hDlg, IDC_RES_TRACKBAR);
302     DWORD dwLogpixels;
303
304     updating_ui = TRUE;
305
306     dwLogpixels = read_logpixels_reg();
307
308     SendMessageW(hTrackBar, TBM_SETRANGE, TRUE, MAKELONG(MINDPI, MAXDPI));
309     SendMessageW(hTrackBar, TBM_SETPOS, TRUE, dwLogpixels);
310
311     updating_ui = FALSE;
312 }
313
314 static void update_dpi_trackbar_from_edit(HWND hDlg, BOOL fix)
315 {
316     DWORD dpi;
317
318     updating_ui = TRUE;
319
320     dpi = GetDlgItemInt(hDlg, IDC_RES_DPIEDIT, NULL, FALSE);
321
322     if (fix)
323     {
324         DWORD fixed_dpi = dpi;
325
326         if (dpi < MINDPI) fixed_dpi = MINDPI;
327         if (dpi > MAXDPI) fixed_dpi = MAXDPI;
328
329         if (fixed_dpi != dpi)
330         {
331             char buf[16];
332
333             dpi = fixed_dpi;
334             sprintf(buf, "%u", dpi);
335             SetDlgItemText(hDlg, IDC_RES_DPIEDIT, buf);
336         }
337     }
338
339     if (dpi >= MINDPI && dpi <= MAXDPI)
340     {
341         SendDlgItemMessage(hDlg, IDC_RES_TRACKBAR, TBM_SETPOS, TRUE, dpi);
342         set_reg_key_dwordW(HKEY_LOCAL_MACHINE, logpixels_reg, logpixels, dpi);
343     }
344
345     updating_ui = FALSE;
346 }
347
348 static void update_font_preview(HWND hDlg)
349 {
350     DWORD dpi;
351
352     updating_ui = TRUE;
353
354     dpi = GetDlgItemInt(hDlg, IDC_RES_DPIEDIT, NULL, FALSE);
355
356     if (dpi >= MINDPI && dpi <= MAXDPI)
357     {
358         LOGFONT lf;
359         HFONT hfont;
360
361         hfont = (HFONT)SendDlgItemMessage(hDlg, IDC_RES_FONT_PREVIEW, WM_GETFONT, 0, 0);
362
363         GetObject(hfont, sizeof(lf), &lf);
364
365         if (lstrcmp(lf.lfFaceName, "Tahoma") != 0)
366             lstrcpy(lf.lfFaceName, "Tahoma");
367         else
368             DeleteObject(hfont);
369
370         lf.lfHeight = MulDiv(-10, dpi, 72);
371         hfont = CreateFontIndirect(&lf);
372         SendDlgItemMessage(hDlg, IDC_RES_FONT_PREVIEW, WM_SETFONT, (WPARAM)hfont, 1);
373     }
374
375     updating_ui = FALSE;
376 }
377
378 INT_PTR CALLBACK
379 GraphDlgProc(HWND hDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
380 {
381     switch (uMsg) {
382         case WM_INITDIALOG:
383             init_dpi_editbox(hDlg);
384             init_trackbar(hDlg);
385             update_font_preview(hDlg);
386             break;
387
388         case WM_SHOWWINDOW:
389             set_window_title(hDlg);
390             break;
391
392         case WM_TIMER:
393             if (wParam == IDT_DPIEDIT)
394             {
395                 KillTimer(hDlg, IDT_DPIEDIT);
396                 update_dpi_trackbar_from_edit(hDlg, TRUE);
397                 update_font_preview(hDlg);
398             }
399             break;
400             
401         case WM_COMMAND:
402             switch(HIWORD(wParam)) {
403                 case EN_CHANGE: {
404                     if (updating_ui) break;
405                     SendMessage(GetParent(hDlg), PSM_CHANGED, 0, 0);
406                     if ( ((LOWORD(wParam) == IDC_DESKTOP_WIDTH) || (LOWORD(wParam) == IDC_DESKTOP_HEIGHT)) && !updating_ui )
407                         set_from_desktop_edits(hDlg);
408                     else if (LOWORD(wParam) == IDC_RES_DPIEDIT)
409                     {
410                         update_dpi_trackbar_from_edit(hDlg, FALSE);
411                         update_font_preview(hDlg);
412                         SetTimer(hDlg, IDT_DPIEDIT, 1500, NULL);
413                     }
414                     break;
415                 }
416                 case BN_CLICKED: {
417                     if (updating_ui) break;
418                     SendMessage(GetParent(hDlg), PSM_CHANGED, 0, 0);
419                     switch(LOWORD(wParam)) {
420                         case IDC_ENABLE_DESKTOP: on_enable_desktop_clicked(hDlg); break;
421                         case IDC_ENABLE_MANAGED: on_enable_managed_clicked(hDlg); break;
422                         case IDC_ENABLE_DECORATED: on_enable_decorated_clicked(hDlg); break;
423                         case IDC_DX_MOUSE_GRAB:  on_dx_mouse_grab_clicked(hDlg); break;
424                         case IDC_D3D_PSHADER_MODE: on_d3d_pshader_mode_clicked(hDlg); break;
425                     }
426                     break;
427                 }
428                 case CBN_SELCHANGE: {
429                     SendMessage(GetParent(hDlg), PSM_CHANGED, 0, 0);
430                     switch (LOWORD(wParam)) {
431                     case IDC_D3D_VSHADER_MODE: on_d3d_vshader_mode_changed(hDlg); break;
432                     }
433                     break;
434                 }
435                     
436                 default:
437                     break;
438             }
439             break;
440         
441         
442         case WM_NOTIFY:
443             switch (((LPNMHDR)lParam)->code) {
444                 case PSN_KILLACTIVE: {
445                     SetWindowLongPtr(hDlg, DWLP_MSGRESULT, FALSE);
446                     break;
447                 }
448                 case PSN_APPLY: {
449                     apply();
450                     SetWindowLongPtr(hDlg, DWLP_MSGRESULT, PSNRET_NOERROR);
451                     break;
452                 }
453                 case PSN_SETACTIVE: {
454                     init_dialog (hDlg);
455                     break;
456                 }
457             }
458             break;
459
460         case WM_HSCROLL:
461             switch (wParam) {
462                 default: {
463                     char buf[MAXBUFLEN];
464                     int i = SendMessageW(GetDlgItem(hDlg, IDC_RES_TRACKBAR), TBM_GETPOS, 0, 0);
465                     buf[0] = 0;
466                     sprintf(buf, "%d", i);
467                     SendMessage(GetDlgItem(hDlg, IDC_RES_DPIEDIT), WM_SETTEXT, 0, (LPARAM) buf);
468                     update_font_preview(hDlg);
469                     set_reg_key_dwordW(HKEY_LOCAL_MACHINE, logpixels_reg, logpixels, i);
470                     break;
471                 }
472             }
473             break;
474
475         default:
476             break;
477     }
478     return FALSE;
479 }