services: Save service dependencies.
[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     static const WCHAR min_width[]  = {'6','4','0',0};
193     static const WCHAR min_height[] = {'4','8','0',0};
194     WCHAR *width, *height, *new;
195     const WCHAR *desktop_name = current_app ? current_app : defaultW;
196
197     if (updating_ui) return;
198     
199     WINE_TRACE("\n");
200
201     width = get_textW(dialog, IDC_DESKTOP_WIDTH);
202     height = get_textW(dialog, IDC_DESKTOP_HEIGHT);
203
204     if (!width || !width[0]) {
205         HeapFree(GetProcessHeap(), 0, width);
206         width = strdupW(def_width);
207     }
208     else if (atoiW(width) < atoiW(min_width))
209     {
210         HeapFree(GetProcessHeap(), 0, width);
211         width = strdupW(min_width);
212     }
213     if (!height || !height[0]) {
214         HeapFree(GetProcessHeap(), 0, height);
215         height = strdupW(def_height);
216     }
217     else if (atoiW(height) < atoiW(min_height))
218     {
219         HeapFree(GetProcessHeap(), 0, height);
220         height = strdupW(min_height);
221     }
222
223     new = HeapAlloc(GetProcessHeap(), 0, (strlenW(width) + strlenW(height) + 2) * sizeof(WCHAR));
224     strcpyW( new, width );
225     strcatW( new, x );
226     strcatW( new, height );
227     set_reg_keyW(config_key, explorer_desktopsW, desktop_name, new);
228     set_reg_keyW(config_key, keypathW(explorerW), desktopW, desktop_name);
229
230     HeapFree(GetProcessHeap(), 0, width);
231     HeapFree(GetProcessHeap(), 0, height);
232     HeapFree(GetProcessHeap(), 0, new);
233 }
234
235 static void on_enable_desktop_clicked(HWND dialog) {
236     WINE_TRACE("\n");
237     
238     if (IsDlgButtonChecked(dialog, IDC_ENABLE_DESKTOP) == BST_CHECKED) {
239         set_from_desktop_edits(dialog);
240     } else {
241         set_reg_key(config_key, keypath("Explorer"), "Desktop", NULL);
242     }
243     
244     update_gui_for_desktop_mode(dialog);
245 }
246
247 static void on_enable_managed_clicked(HWND dialog) {
248     WINE_TRACE("\n");
249     
250     if (IsDlgButtonChecked(dialog, IDC_ENABLE_MANAGED) == BST_CHECKED) {
251         set_reg_key(config_key, keypath("X11 Driver"), "Managed", "Y");
252     } else {
253         set_reg_key(config_key, keypath("X11 Driver"), "Managed", "N");
254     }
255 }
256
257 static void on_enable_decorated_clicked(HWND dialog) {
258     WINE_TRACE("\n");
259
260     if (IsDlgButtonChecked(dialog, IDC_ENABLE_DECORATED) == BST_CHECKED) {
261         set_reg_key(config_key, keypath("X11 Driver"), "Decorated", "Y");
262     } else {
263         set_reg_key(config_key, keypath("X11 Driver"), "Decorated", "N");
264     }
265 }
266
267 static void on_dx_mouse_grab_clicked(HWND dialog) {
268     if (IsDlgButtonChecked(dialog, IDC_DX_MOUSE_GRAB) == BST_CHECKED) 
269         set_reg_key(config_key, keypath("X11 Driver"), "DXGrab", "Y");
270     else
271         set_reg_key(config_key, keypath("X11 Driver"), "DXGrab", "N");
272 }
273
274 static void on_d3d_vshader_mode_changed(HWND dialog) {
275   int selected_mode = SendDlgItemMessage(dialog, IDC_D3D_VSHADER_MODE, CB_GETCURSEL, 0, 0);  
276   set_reg_key(config_key, keypath("Direct3D"), "VertexShaderMode",
277       D3D_VS_Modes[selected_mode].settingStr); 
278 }
279
280 static void on_d3d_pshader_mode_clicked(HWND dialog) {
281     if (IsDlgButtonChecked(dialog, IDC_D3D_PSHADER_MODE) == BST_CHECKED)
282         set_reg_key(config_key, keypath("Direct3D"), "PixelShaderMode", "enabled");
283     else
284         set_reg_key(config_key, keypath("Direct3D"), "PixelShaderMode", "disabled");
285 }
286 static INT read_logpixels_reg(void)
287 {
288     DWORD dwLogPixels;
289     WCHAR *buf = get_reg_keyW(HKEY_LOCAL_MACHINE, logpixels_reg, logpixels, NULL);
290     dwLogPixels = buf ? *buf : DEFDPI;
291     HeapFree(GetProcessHeap(), 0, buf);
292     return dwLogPixels;
293 }
294
295 static void init_dpi_editbox(HWND hDlg)
296 {
297     DWORD dwLogpixels;
298     char szLogpixels[MAXBUFLEN];
299
300     updating_ui = TRUE;
301
302     dwLogpixels = read_logpixels_reg();
303     WINE_TRACE("%u\n", dwLogpixels);
304
305     sprintf(szLogpixels, "%u", dwLogpixels);
306     SetDlgItemText(hDlg, IDC_RES_DPIEDIT, szLogpixels);
307
308     updating_ui = FALSE;
309 }
310
311 static void init_trackbar(HWND hDlg)
312 {
313     HWND hTrackBar = GetDlgItem(hDlg, IDC_RES_TRACKBAR);
314     DWORD dwLogpixels;
315
316     updating_ui = TRUE;
317
318     dwLogpixels = read_logpixels_reg();
319
320     SendMessageW(hTrackBar, TBM_SETRANGE, TRUE, MAKELONG(MINDPI, MAXDPI));
321     SendMessageW(hTrackBar, TBM_SETPOS, TRUE, dwLogpixels);
322
323     updating_ui = FALSE;
324 }
325
326 static void update_dpi_trackbar_from_edit(HWND hDlg, BOOL fix)
327 {
328     DWORD dpi;
329
330     updating_ui = TRUE;
331
332     dpi = GetDlgItemInt(hDlg, IDC_RES_DPIEDIT, NULL, FALSE);
333
334     if (fix)
335     {
336         DWORD fixed_dpi = dpi;
337
338         if (dpi < MINDPI) fixed_dpi = MINDPI;
339         if (dpi > MAXDPI) fixed_dpi = MAXDPI;
340
341         if (fixed_dpi != dpi)
342         {
343             char buf[16];
344
345             dpi = fixed_dpi;
346             sprintf(buf, "%u", dpi);
347             SetDlgItemText(hDlg, IDC_RES_DPIEDIT, buf);
348         }
349     }
350
351     if (dpi >= MINDPI && dpi <= MAXDPI)
352     {
353         SendDlgItemMessage(hDlg, IDC_RES_TRACKBAR, TBM_SETPOS, TRUE, dpi);
354         set_reg_key_dwordW(HKEY_LOCAL_MACHINE, logpixels_reg, logpixels, dpi);
355     }
356
357     updating_ui = FALSE;
358 }
359
360 static void update_font_preview(HWND hDlg)
361 {
362     DWORD dpi;
363
364     updating_ui = TRUE;
365
366     dpi = GetDlgItemInt(hDlg, IDC_RES_DPIEDIT, NULL, FALSE);
367
368     if (dpi >= MINDPI && dpi <= MAXDPI)
369     {
370         LOGFONT lf;
371         HFONT hfont;
372
373         hfont = (HFONT)SendDlgItemMessage(hDlg, IDC_RES_FONT_PREVIEW, WM_GETFONT, 0, 0);
374
375         GetObject(hfont, sizeof(lf), &lf);
376
377         if (lstrcmp(lf.lfFaceName, "Tahoma") != 0)
378             lstrcpy(lf.lfFaceName, "Tahoma");
379         else
380             DeleteObject(hfont);
381
382         lf.lfHeight = MulDiv(-10, dpi, 72);
383         hfont = CreateFontIndirect(&lf);
384         SendDlgItemMessage(hDlg, IDC_RES_FONT_PREVIEW, WM_SETFONT, (WPARAM)hfont, 1);
385     }
386
387     updating_ui = FALSE;
388 }
389
390 INT_PTR CALLBACK
391 GraphDlgProc(HWND hDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
392 {
393     switch (uMsg) {
394         case WM_INITDIALOG:
395             init_dpi_editbox(hDlg);
396             init_trackbar(hDlg);
397             update_font_preview(hDlg);
398             break;
399
400         case WM_SHOWWINDOW:
401             set_window_title(hDlg);
402             break;
403
404         case WM_TIMER:
405             if (wParam == IDT_DPIEDIT)
406             {
407                 KillTimer(hDlg, IDT_DPIEDIT);
408                 update_dpi_trackbar_from_edit(hDlg, TRUE);
409                 update_font_preview(hDlg);
410             }
411             break;
412             
413         case WM_COMMAND:
414             switch(HIWORD(wParam)) {
415                 case EN_CHANGE: {
416                     if (updating_ui) break;
417                     SendMessage(GetParent(hDlg), PSM_CHANGED, 0, 0);
418                     if ( ((LOWORD(wParam) == IDC_DESKTOP_WIDTH) || (LOWORD(wParam) == IDC_DESKTOP_HEIGHT)) && !updating_ui )
419                         set_from_desktop_edits(hDlg);
420                     else if (LOWORD(wParam) == IDC_RES_DPIEDIT)
421                     {
422                         update_dpi_trackbar_from_edit(hDlg, FALSE);
423                         update_font_preview(hDlg);
424                         SetTimer(hDlg, IDT_DPIEDIT, 1500, NULL);
425                     }
426                     break;
427                 }
428                 case BN_CLICKED: {
429                     if (updating_ui) break;
430                     SendMessage(GetParent(hDlg), PSM_CHANGED, 0, 0);
431                     switch(LOWORD(wParam)) {
432                         case IDC_ENABLE_DESKTOP: on_enable_desktop_clicked(hDlg); break;
433                         case IDC_ENABLE_MANAGED: on_enable_managed_clicked(hDlg); break;
434                         case IDC_ENABLE_DECORATED: on_enable_decorated_clicked(hDlg); break;
435                         case IDC_DX_MOUSE_GRAB:  on_dx_mouse_grab_clicked(hDlg); break;
436                         case IDC_D3D_PSHADER_MODE: on_d3d_pshader_mode_clicked(hDlg); break;
437                     }
438                     break;
439                 }
440                 case CBN_SELCHANGE: {
441                     SendMessage(GetParent(hDlg), PSM_CHANGED, 0, 0);
442                     switch (LOWORD(wParam)) {
443                     case IDC_D3D_VSHADER_MODE: on_d3d_vshader_mode_changed(hDlg); break;
444                     }
445                     break;
446                 }
447                     
448                 default:
449                     break;
450             }
451             break;
452         
453         
454         case WM_NOTIFY:
455             switch (((LPNMHDR)lParam)->code) {
456                 case PSN_KILLACTIVE: {
457                     SetWindowLongPtr(hDlg, DWLP_MSGRESULT, FALSE);
458                     break;
459                 }
460                 case PSN_APPLY: {
461                     apply();
462                     SetWindowLongPtr(hDlg, DWLP_MSGRESULT, PSNRET_NOERROR);
463                     break;
464                 }
465                 case PSN_SETACTIVE: {
466                     init_dialog (hDlg);
467                     break;
468                 }
469             }
470             break;
471
472         case WM_HSCROLL:
473             switch (wParam) {
474                 default: {
475                     char buf[MAXBUFLEN];
476                     int i = SendMessageW(GetDlgItem(hDlg, IDC_RES_TRACKBAR), TBM_GETPOS, 0, 0);
477                     buf[0] = 0;
478                     sprintf(buf, "%d", i);
479                     SendMessage(GetDlgItem(hDlg, IDC_RES_DPIEDIT), WM_SETTEXT, 0, (LPARAM) buf);
480                     update_font_preview(hDlg);
481                     set_reg_key_dwordW(HKEY_LOCAL_MACHINE, logpixels_reg, logpixels, i);
482                     break;
483                 }
484             }
485             break;
486
487         default:
488             break;
489     }
490     return FALSE;
491 }