d3d9/tests: Correct nv40 results.
[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 int updating_ui;
59
60 /* convert the x11 desktop key to the new explorer config */
61 static void convert_x11_desktop_key(void)
62 {
63     char *buf;
64
65     if (!(buf = get_reg_key(config_key, "X11 Driver", "Desktop", NULL))) return;
66     set_reg_key(config_key, "Explorer\\Desktops", "Default", buf);
67     set_reg_key(config_key, "Explorer", "Desktop", "Default");
68     set_reg_key(config_key, "X11 Driver", "Desktop", NULL);
69     HeapFree(GetProcessHeap(), 0, buf);
70 }
71
72 static void update_gui_for_desktop_mode(HWND dialog)
73 {
74     WCHAR *buf, *bufindex;
75     const WCHAR *desktop_name = current_app ? current_app : defaultW;
76
77     WINE_TRACE("\n");
78     updating_ui = TRUE;
79
80     buf = get_reg_keyW(config_key, explorer_desktopsW, desktop_name, NULL);
81     if (buf && (bufindex = strchrW(buf, 'x')))
82     {
83         *bufindex = 0;
84         ++bufindex;
85         SetWindowTextW(GetDlgItem(dialog, IDC_DESKTOP_WIDTH), buf);
86         SetWindowTextW(GetDlgItem(dialog, IDC_DESKTOP_HEIGHT), bufindex);
87     } else {
88         SetWindowTextA(GetDlgItem(dialog, IDC_DESKTOP_WIDTH), "800");
89         SetWindowTextA(GetDlgItem(dialog, IDC_DESKTOP_HEIGHT), "600");
90     }
91     HeapFree(GetProcessHeap(), 0, buf);
92
93     /* do we have desktop mode enabled? */
94     if (reg_key_exists(config_key, keypath("Explorer"), "Desktop"))
95     {
96         CheckDlgButton(dialog, IDC_ENABLE_DESKTOP, BST_CHECKED);
97         enable(IDC_DESKTOP_WIDTH);
98         enable(IDC_DESKTOP_HEIGHT);
99         enable(IDC_DESKTOP_SIZE);
100         enable(IDC_DESKTOP_BY);
101     }
102     else
103     {
104         CheckDlgButton(dialog, IDC_ENABLE_DESKTOP, BST_UNCHECKED);
105         disable(IDC_DESKTOP_WIDTH);
106         disable(IDC_DESKTOP_HEIGHT);
107         disable(IDC_DESKTOP_SIZE);
108         disable(IDC_DESKTOP_BY);
109     }
110
111     updating_ui = FALSE;
112 }
113
114 static void init_dialog(HWND dialog)
115 {
116     char* buf;
117
118     convert_x11_desktop_key();
119     update_gui_for_desktop_mode(dialog);
120
121     updating_ui = TRUE;
122
123     SendDlgItemMessageW(dialog, IDC_DESKTOP_WIDTH, EM_LIMITTEXT, RES_MAXLEN, 0);
124     SendDlgItemMessageW(dialog, IDC_DESKTOP_HEIGHT, EM_LIMITTEXT, RES_MAXLEN, 0);
125
126     buf = get_reg_key(config_key, keypath("X11 Driver"), "GrabFullscreen", "N");
127     if (IS_OPTION_TRUE(*buf))
128         CheckDlgButton(dialog, IDC_FULLSCREEN_GRAB, BST_CHECKED);
129     else
130         CheckDlgButton(dialog, IDC_FULLSCREEN_GRAB, BST_UNCHECKED);
131     HeapFree(GetProcessHeap(), 0, buf);
132
133     buf = get_reg_key(config_key, keypath("X11 Driver"), "Managed", "Y");
134     if (IS_OPTION_TRUE(*buf))
135         CheckDlgButton(dialog, IDC_ENABLE_MANAGED, BST_CHECKED);
136     else
137         CheckDlgButton(dialog, IDC_ENABLE_MANAGED, BST_UNCHECKED);
138     HeapFree(GetProcessHeap(), 0, buf);
139
140     buf = get_reg_key(config_key, keypath("X11 Driver"), "Decorated", "Y");
141     if (IS_OPTION_TRUE(*buf))
142         CheckDlgButton(dialog, IDC_ENABLE_DECORATED, BST_CHECKED);
143     else
144         CheckDlgButton(dialog, IDC_ENABLE_DECORATED, BST_UNCHECKED);
145     HeapFree(GetProcessHeap(), 0, buf);
146
147     updating_ui = FALSE;
148 }
149
150 static void set_from_desktop_edits(HWND dialog)
151 {
152     static const WCHAR x[] = {'x',0};
153     static const WCHAR def_width[]  = {'8','0','0',0};
154     static const WCHAR def_height[] = {'6','0','0',0};
155     static const WCHAR min_width[]  = {'6','4','0',0};
156     static const WCHAR min_height[] = {'4','8','0',0};
157     WCHAR *width, *height, *new;
158     const WCHAR *desktop_name = current_app ? current_app : defaultW;
159
160     if (updating_ui) return;
161     
162     WINE_TRACE("\n");
163
164     width = get_textW(dialog, IDC_DESKTOP_WIDTH);
165     height = get_textW(dialog, IDC_DESKTOP_HEIGHT);
166
167     if (!width || !width[0]) {
168         HeapFree(GetProcessHeap(), 0, width);
169         width = strdupW(def_width);
170     }
171     else if (atoiW(width) < atoiW(min_width))
172     {
173         HeapFree(GetProcessHeap(), 0, width);
174         width = strdupW(min_width);
175     }
176     if (!height || !height[0]) {
177         HeapFree(GetProcessHeap(), 0, height);
178         height = strdupW(def_height);
179     }
180     else if (atoiW(height) < atoiW(min_height))
181     {
182         HeapFree(GetProcessHeap(), 0, height);
183         height = strdupW(min_height);
184     }
185
186     new = HeapAlloc(GetProcessHeap(), 0, (strlenW(width) + strlenW(height) + 2) * sizeof(WCHAR));
187     strcpyW( new, width );
188     strcatW( new, x );
189     strcatW( new, height );
190     set_reg_keyW(config_key, explorer_desktopsW, desktop_name, new);
191     set_reg_keyW(config_key, keypathW(explorerW), desktopW, desktop_name);
192
193     HeapFree(GetProcessHeap(), 0, width);
194     HeapFree(GetProcessHeap(), 0, height);
195     HeapFree(GetProcessHeap(), 0, new);
196 }
197
198 static void on_enable_desktop_clicked(HWND dialog) {
199     WINE_TRACE("\n");
200     
201     if (IsDlgButtonChecked(dialog, IDC_ENABLE_DESKTOP) == BST_CHECKED) {
202         set_from_desktop_edits(dialog);
203     } else {
204         set_reg_key(config_key, keypath("Explorer"), "Desktop", NULL);
205     }
206     
207     update_gui_for_desktop_mode(dialog);
208 }
209
210 static void on_enable_managed_clicked(HWND dialog) {
211     WINE_TRACE("\n");
212     
213     if (IsDlgButtonChecked(dialog, IDC_ENABLE_MANAGED) == BST_CHECKED) {
214         set_reg_key(config_key, keypath("X11 Driver"), "Managed", "Y");
215     } else {
216         set_reg_key(config_key, keypath("X11 Driver"), "Managed", "N");
217     }
218 }
219
220 static void on_enable_decorated_clicked(HWND dialog) {
221     WINE_TRACE("\n");
222
223     if (IsDlgButtonChecked(dialog, IDC_ENABLE_DECORATED) == BST_CHECKED) {
224         set_reg_key(config_key, keypath("X11 Driver"), "Decorated", "Y");
225     } else {
226         set_reg_key(config_key, keypath("X11 Driver"), "Decorated", "N");
227     }
228 }
229
230 static void on_fullscreen_grab_clicked(HWND dialog)
231 {
232     if (IsDlgButtonChecked(dialog, IDC_FULLSCREEN_GRAB) == BST_CHECKED)
233         set_reg_key(config_key, keypath("X11 Driver"), "GrabFullscreen", "Y");
234     else
235         set_reg_key(config_key, keypath("X11 Driver"), "GrabFullscreen", "N");
236 }
237
238 static INT read_logpixels_reg(void)
239 {
240     DWORD dwLogPixels;
241     WCHAR *buf = get_reg_keyW(HKEY_LOCAL_MACHINE, logpixels_reg, logpixels, NULL);
242     dwLogPixels = buf ? *buf : DEFDPI;
243     HeapFree(GetProcessHeap(), 0, buf);
244     return dwLogPixels;
245 }
246
247 static void init_dpi_editbox(HWND hDlg)
248 {
249     static const WCHAR fmtW[] = {'%','u',0};
250     DWORD dwLogpixels;
251     WCHAR szLogpixels[MAXBUFLEN];
252
253     updating_ui = TRUE;
254
255     dwLogpixels = read_logpixels_reg();
256     WINE_TRACE("%u\n", dwLogpixels);
257
258     sprintfW(szLogpixels, fmtW, dwLogpixels);
259     SetDlgItemTextW(hDlg, IDC_RES_DPIEDIT, szLogpixels);
260
261     updating_ui = FALSE;
262 }
263
264 static void init_trackbar(HWND hDlg)
265 {
266     HWND hTrackBar = GetDlgItem(hDlg, IDC_RES_TRACKBAR);
267     DWORD dwLogpixels;
268
269     updating_ui = TRUE;
270
271     dwLogpixels = read_logpixels_reg();
272
273     SendMessageW(hTrackBar, TBM_SETRANGE, TRUE, MAKELONG(MINDPI, MAXDPI));
274     SendMessageW(hTrackBar, TBM_SETPOS, TRUE, dwLogpixels);
275
276     updating_ui = FALSE;
277 }
278
279 static void update_dpi_trackbar_from_edit(HWND hDlg, BOOL fix)
280 {
281     static const WCHAR fmtW[] = {'%','u',0};
282     DWORD dpi;
283
284     updating_ui = TRUE;
285
286     dpi = GetDlgItemInt(hDlg, IDC_RES_DPIEDIT, NULL, FALSE);
287
288     if (fix)
289     {
290         DWORD fixed_dpi = dpi;
291
292         if (dpi < MINDPI) fixed_dpi = MINDPI;
293         if (dpi > MAXDPI) fixed_dpi = MAXDPI;
294
295         if (fixed_dpi != dpi)
296         {
297             WCHAR buf[16];
298
299             dpi = fixed_dpi;
300             sprintfW(buf, fmtW, dpi);
301             SetDlgItemTextW(hDlg, IDC_RES_DPIEDIT, buf);
302         }
303     }
304
305     if (dpi >= MINDPI && dpi <= MAXDPI)
306     {
307         SendDlgItemMessageW(hDlg, IDC_RES_TRACKBAR, TBM_SETPOS, TRUE, dpi);
308         set_reg_key_dwordW(HKEY_LOCAL_MACHINE, logpixels_reg, logpixels, dpi);
309     }
310
311     updating_ui = FALSE;
312 }
313
314 static void update_font_preview(HWND hDlg)
315 {
316     DWORD dpi;
317
318     updating_ui = TRUE;
319
320     dpi = GetDlgItemInt(hDlg, IDC_RES_DPIEDIT, NULL, FALSE);
321
322     if (dpi >= MINDPI && dpi <= MAXDPI)
323     {
324         static const WCHAR tahomaW[] = {'T','a','h','o','m','a',0};
325         LOGFONTW lf;
326         HFONT hfont;
327
328         hfont = (HFONT)SendDlgItemMessageW(hDlg, IDC_RES_FONT_PREVIEW, WM_GETFONT, 0, 0);
329
330         GetObjectW(hfont, sizeof(lf), &lf);
331
332         if (strcmpW(lf.lfFaceName, tahomaW) != 0)
333             strcpyW(lf.lfFaceName, tahomaW);
334         else
335             DeleteObject(hfont);
336         lf.lfHeight = MulDiv(-10, dpi, 72);
337         hfont = CreateFontIndirectW(&lf);
338         SendDlgItemMessageW(hDlg, IDC_RES_FONT_PREVIEW, WM_SETFONT, (WPARAM)hfont, 1);
339     }
340
341     updating_ui = FALSE;
342 }
343
344 INT_PTR CALLBACK
345 GraphDlgProc(HWND hDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
346 {
347     switch (uMsg) {
348         case WM_INITDIALOG:
349             init_dpi_editbox(hDlg);
350             init_trackbar(hDlg);
351             update_font_preview(hDlg);
352             break;
353
354         case WM_SHOWWINDOW:
355             set_window_title(hDlg);
356             break;
357
358         case WM_TIMER:
359             if (wParam == IDT_DPIEDIT)
360             {
361                 KillTimer(hDlg, IDT_DPIEDIT);
362                 update_dpi_trackbar_from_edit(hDlg, TRUE);
363                 update_font_preview(hDlg);
364             }
365             break;
366             
367         case WM_COMMAND:
368             switch(HIWORD(wParam)) {
369                 case EN_CHANGE: {
370                     if (updating_ui) break;
371                     SendMessageW(GetParent(hDlg), PSM_CHANGED, 0, 0);
372                     if ( ((LOWORD(wParam) == IDC_DESKTOP_WIDTH) || (LOWORD(wParam) == IDC_DESKTOP_HEIGHT)) && !updating_ui )
373                         set_from_desktop_edits(hDlg);
374                     else if (LOWORD(wParam) == IDC_RES_DPIEDIT)
375                     {
376                         update_dpi_trackbar_from_edit(hDlg, FALSE);
377                         update_font_preview(hDlg);
378                         SetTimer(hDlg, IDT_DPIEDIT, 1500, NULL);
379                     }
380                     break;
381                 }
382                 case BN_CLICKED: {
383                     if (updating_ui) break;
384                     SendMessageW(GetParent(hDlg), PSM_CHANGED, 0, 0);
385                     switch(LOWORD(wParam)) {
386                         case IDC_ENABLE_DESKTOP: on_enable_desktop_clicked(hDlg); break;
387                         case IDC_ENABLE_MANAGED: on_enable_managed_clicked(hDlg); break;
388                         case IDC_ENABLE_DECORATED: on_enable_decorated_clicked(hDlg); break;
389                         case IDC_FULLSCREEN_GRAB:  on_fullscreen_grab_clicked(hDlg); break;
390                     }
391                     break;
392                 }
393                 case CBN_SELCHANGE: {
394                     SendMessageW(GetParent(hDlg), PSM_CHANGED, 0, 0);
395                     break;
396                 }
397                     
398                 default:
399                     break;
400             }
401             break;
402         
403         
404         case WM_NOTIFY:
405             switch (((LPNMHDR)lParam)->code) {
406                 case PSN_KILLACTIVE: {
407                     SetWindowLongPtrW(hDlg, DWLP_MSGRESULT, FALSE);
408                     break;
409                 }
410                 case PSN_APPLY: {
411                     apply();
412                     SetWindowLongPtrW(hDlg, DWLP_MSGRESULT, PSNRET_NOERROR);
413                     break;
414                 }
415                 case PSN_SETACTIVE: {
416                     init_dialog (hDlg);
417                     break;
418                 }
419             }
420             break;
421
422         case WM_HSCROLL:
423             switch (wParam) {
424                 default: {
425                     static const WCHAR fmtW[] = {'%','d',0};
426                     WCHAR buf[MAXBUFLEN];
427                     int i = SendMessageW(GetDlgItem(hDlg, IDC_RES_TRACKBAR), TBM_GETPOS, 0, 0);
428                     buf[0] = 0;
429                     sprintfW(buf, fmtW, i);
430                     SendMessageW(GetDlgItem(hDlg, IDC_RES_DPIEDIT), WM_SETTEXT, 0, (LPARAM) buf);
431                     update_font_preview(hDlg);
432                     set_reg_key_dwordW(HKEY_LOCAL_MACHINE, logpixels_reg, logpixels, i);
433                     break;
434                 }
435             }
436             break;
437
438         default:
439             break;
440     }
441     return FALSE;
442 }