winhlp32: Remove redundant comparison.
[wine.git] / programs / winecfg / x11drvdlg.c
blob2be6832d253a69f5288dec7acbe04f1a485a8d16
1 /*
2 * Graphics configuration code
4 * Copyright 2003 Mark Westcott
5 * Copyright 2003-2004 Mike Hearn
6 * Copyright 2005 Raphael Junqueira
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.
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.
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
24 #define WIN32_LEAN_AND_MEAN
26 #include <stdarg.h>
27 #include <stdlib.h>
28 #include <stdio.h>
30 #include <windows.h>
31 #include <wine/unicode.h>
32 #include <wine/debug.h>
34 #include "resource.h"
35 #include "winecfg.h"
37 WINE_DEFAULT_DEBUG_CHANNEL(winecfg);
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
44 #define IDT_DPIEDIT 0x1234
46 static const WCHAR logpixels_reg[] = {'C','o','n','t','r','o','l',' ','P','a','n','e','l','\\','D','e','s','k','t','o','p','\0'};
47 static const WCHAR def_logpixels_reg[] = {'S','o','f','t','w','a','r','e','\\','F','o','n','t','s','\0'};
48 static const WCHAR logpixels[] = {'L','o','g','P','i','x','e','l','s',0};
50 static const WCHAR desktopW[] = {'D','e','s','k','t','o','p',0};
51 static const WCHAR defaultW[] = {'D','e','f','a','u','l','t',0};
52 static const WCHAR explorerW[] = {'E','x','p','l','o','r','e','r',0};
53 static const WCHAR explorer_desktopsW[] = {'E','x','p','l','o','r','e','r','\\',
54 'D','e','s','k','t','o','p','s',0};
57 static BOOL updating_ui;
59 /* convert the x11 desktop key to the new explorer config */
60 static void convert_x11_desktop_key(void)
62 char *buf;
64 if (!(buf = get_reg_key(config_key, "X11 Driver", "Desktop", NULL))) return;
65 set_reg_key(config_key, "Explorer\\Desktops", "Default", buf);
66 set_reg_key(config_key, "Explorer", "Desktop", "Default");
67 set_reg_key(config_key, "X11 Driver", "Desktop", NULL);
68 HeapFree(GetProcessHeap(), 0, buf);
71 static void update_gui_for_desktop_mode(HWND dialog)
73 WCHAR *buf, *bufindex;
74 const WCHAR *desktop_name = current_app ? current_app : defaultW;
76 WINE_TRACE("\n");
77 updating_ui = TRUE;
79 buf = get_reg_keyW(config_key, explorer_desktopsW, desktop_name, NULL);
80 if (buf && (bufindex = strchrW(buf, 'x')))
82 *bufindex++ = 0;
84 SetDlgItemTextW(dialog, IDC_DESKTOP_WIDTH, buf);
85 SetDlgItemTextW(dialog, IDC_DESKTOP_HEIGHT, bufindex);
86 } else {
87 SetDlgItemTextA(dialog, IDC_DESKTOP_WIDTH, "800");
88 SetDlgItemTextA(dialog, IDC_DESKTOP_HEIGHT, "600");
90 HeapFree(GetProcessHeap(), 0, buf);
92 /* do we have desktop mode enabled? */
93 if (reg_key_exists(config_key, keypath("Explorer"), "Desktop"))
95 CheckDlgButton(dialog, IDC_ENABLE_DESKTOP, BST_CHECKED);
96 enable(IDC_DESKTOP_WIDTH);
97 enable(IDC_DESKTOP_HEIGHT);
98 enable(IDC_DESKTOP_SIZE);
99 enable(IDC_DESKTOP_BY);
101 else
103 CheckDlgButton(dialog, IDC_ENABLE_DESKTOP, BST_UNCHECKED);
104 disable(IDC_DESKTOP_WIDTH);
105 disable(IDC_DESKTOP_HEIGHT);
106 disable(IDC_DESKTOP_SIZE);
107 disable(IDC_DESKTOP_BY);
110 updating_ui = FALSE;
113 static void init_dialog(HWND dialog)
115 char* buf;
117 convert_x11_desktop_key();
118 update_gui_for_desktop_mode(dialog);
120 updating_ui = TRUE;
122 SendDlgItemMessageW(dialog, IDC_DESKTOP_WIDTH, EM_LIMITTEXT, RES_MAXLEN, 0);
123 SendDlgItemMessageW(dialog, IDC_DESKTOP_HEIGHT, EM_LIMITTEXT, RES_MAXLEN, 0);
125 buf = get_reg_key(config_key, keypath("X11 Driver"), "GrabFullscreen", "N");
126 if (IS_OPTION_TRUE(*buf))
127 CheckDlgButton(dialog, IDC_FULLSCREEN_GRAB, BST_CHECKED);
128 else
129 CheckDlgButton(dialog, IDC_FULLSCREEN_GRAB, BST_UNCHECKED);
130 HeapFree(GetProcessHeap(), 0, buf);
132 buf = get_reg_key(config_key, keypath("X11 Driver"), "Managed", "Y");
133 if (IS_OPTION_TRUE(*buf))
134 CheckDlgButton(dialog, IDC_ENABLE_MANAGED, BST_CHECKED);
135 else
136 CheckDlgButton(dialog, IDC_ENABLE_MANAGED, BST_UNCHECKED);
137 HeapFree(GetProcessHeap(), 0, buf);
139 buf = get_reg_key(config_key, keypath("X11 Driver"), "Decorated", "Y");
140 if (IS_OPTION_TRUE(*buf))
141 CheckDlgButton(dialog, IDC_ENABLE_DECORATED, BST_CHECKED);
142 else
143 CheckDlgButton(dialog, IDC_ENABLE_DECORATED, BST_UNCHECKED);
144 HeapFree(GetProcessHeap(), 0, buf);
146 updating_ui = FALSE;
149 static void set_from_desktop_edits(HWND dialog)
151 static const WCHAR x[] = {'x',0};
152 static const WCHAR def_width[] = {'8','0','0',0};
153 static const WCHAR def_height[] = {'6','0','0',0};
154 static const WCHAR min_width[] = {'6','4','0',0};
155 static const WCHAR min_height[] = {'4','8','0',0};
156 WCHAR *width, *height, *new;
157 const WCHAR *desktop_name = current_app ? current_app : defaultW;
159 if (updating_ui) return;
161 WINE_TRACE("\n");
163 width = get_textW(dialog, IDC_DESKTOP_WIDTH);
164 height = get_textW(dialog, IDC_DESKTOP_HEIGHT);
166 if (!width || !width[0]) {
167 HeapFree(GetProcessHeap(), 0, width);
168 width = strdupW(def_width);
170 else if (atoiW(width) < atoiW(min_width))
172 HeapFree(GetProcessHeap(), 0, width);
173 width = strdupW(min_width);
175 if (!height || !height[0]) {
176 HeapFree(GetProcessHeap(), 0, height);
177 height = strdupW(def_height);
179 else if (atoiW(height) < atoiW(min_height))
181 HeapFree(GetProcessHeap(), 0, height);
182 height = strdupW(min_height);
185 new = HeapAlloc(GetProcessHeap(), 0, (strlenW(width) + strlenW(height) + 2) * sizeof(WCHAR));
186 strcpyW( new, width );
187 strcatW( new, x );
188 strcatW( new, height );
189 set_reg_keyW(config_key, explorer_desktopsW, desktop_name, new);
190 set_reg_keyW(config_key, keypathW(explorerW), desktopW, desktop_name);
192 HeapFree(GetProcessHeap(), 0, width);
193 HeapFree(GetProcessHeap(), 0, height);
194 HeapFree(GetProcessHeap(), 0, new);
197 static void on_enable_desktop_clicked(HWND dialog) {
198 WINE_TRACE("\n");
200 if (IsDlgButtonChecked(dialog, IDC_ENABLE_DESKTOP) == BST_CHECKED) {
201 set_from_desktop_edits(dialog);
202 } else {
203 set_reg_key(config_key, keypath("Explorer"), "Desktop", NULL);
206 update_gui_for_desktop_mode(dialog);
209 static void on_enable_managed_clicked(HWND dialog) {
210 WINE_TRACE("\n");
212 if (IsDlgButtonChecked(dialog, IDC_ENABLE_MANAGED) == BST_CHECKED) {
213 set_reg_key(config_key, keypath("X11 Driver"), "Managed", "Y");
214 } else {
215 set_reg_key(config_key, keypath("X11 Driver"), "Managed", "N");
219 static void on_enable_decorated_clicked(HWND dialog) {
220 WINE_TRACE("\n");
222 if (IsDlgButtonChecked(dialog, IDC_ENABLE_DECORATED) == BST_CHECKED) {
223 set_reg_key(config_key, keypath("X11 Driver"), "Decorated", "Y");
224 } else {
225 set_reg_key(config_key, keypath("X11 Driver"), "Decorated", "N");
229 static void on_fullscreen_grab_clicked(HWND dialog)
231 if (IsDlgButtonChecked(dialog, IDC_FULLSCREEN_GRAB) == BST_CHECKED)
232 set_reg_key(config_key, keypath("X11 Driver"), "GrabFullscreen", "Y");
233 else
234 set_reg_key(config_key, keypath("X11 Driver"), "GrabFullscreen", "N");
237 static INT read_logpixels_reg(void)
239 DWORD dwLogPixels;
240 WCHAR *buf = get_reg_keyW(HKEY_CURRENT_USER, logpixels_reg, logpixels, NULL);
241 if (!buf) buf = get_reg_keyW(HKEY_CURRENT_CONFIG, def_logpixels_reg, logpixels, NULL);
242 dwLogPixels = buf ? *buf : DEFDPI;
243 HeapFree(GetProcessHeap(), 0, buf);
244 return dwLogPixels;
247 static void init_dpi_editbox(HWND hDlg)
249 DWORD dwLogpixels;
251 updating_ui = TRUE;
253 dwLogpixels = read_logpixels_reg();
254 WINE_TRACE("%u\n", dwLogpixels);
256 SetDlgItemInt(hDlg, IDC_RES_DPIEDIT, dwLogpixels, FALSE);
258 updating_ui = FALSE;
261 static void init_trackbar(HWND hDlg)
263 HWND hTrackBar = GetDlgItem(hDlg, IDC_RES_TRACKBAR);
264 DWORD dwLogpixels;
266 updating_ui = TRUE;
268 dwLogpixels = read_logpixels_reg();
270 SendMessageW(hTrackBar, TBM_SETRANGE, TRUE, MAKELONG(MINDPI, MAXDPI));
271 SendMessageW(hTrackBar, TBM_SETPOS, TRUE, dwLogpixels);
273 updating_ui = FALSE;
276 static void update_dpi_trackbar_from_edit(HWND hDlg, BOOL fix)
278 DWORD dpi;
280 updating_ui = TRUE;
282 dpi = GetDlgItemInt(hDlg, IDC_RES_DPIEDIT, NULL, FALSE);
284 if (fix)
286 DWORD fixed_dpi = dpi;
288 if (dpi < MINDPI) fixed_dpi = MINDPI;
289 if (dpi > MAXDPI) fixed_dpi = MAXDPI;
291 if (fixed_dpi != dpi)
293 dpi = fixed_dpi;
294 SetDlgItemInt(hDlg, IDC_RES_DPIEDIT, dpi, FALSE);
298 if (dpi >= MINDPI && dpi <= MAXDPI)
300 SendDlgItemMessageW(hDlg, IDC_RES_TRACKBAR, TBM_SETPOS, TRUE, dpi);
301 set_reg_key_dwordW(HKEY_CURRENT_USER, logpixels_reg, logpixels, dpi);
304 updating_ui = FALSE;
307 static void update_font_preview(HWND hDlg)
309 DWORD dpi;
311 updating_ui = TRUE;
313 dpi = GetDlgItemInt(hDlg, IDC_RES_DPIEDIT, NULL, FALSE);
315 if (dpi >= MINDPI && dpi <= MAXDPI)
317 static const WCHAR tahomaW[] = {'T','a','h','o','m','a',0};
318 LOGFONTW lf;
319 HFONT hfont;
321 hfont = (HFONT)SendDlgItemMessageW(hDlg, IDC_RES_FONT_PREVIEW, WM_GETFONT, 0, 0);
323 GetObjectW(hfont, sizeof(lf), &lf);
325 if (strcmpW(lf.lfFaceName, tahomaW) != 0)
326 strcpyW(lf.lfFaceName, tahomaW);
327 else
328 DeleteObject(hfont);
329 lf.lfHeight = MulDiv(-10, dpi, 72);
330 hfont = CreateFontIndirectW(&lf);
331 SendDlgItemMessageW(hDlg, IDC_RES_FONT_PREVIEW, WM_SETFONT, (WPARAM)hfont, 1);
334 updating_ui = FALSE;
337 INT_PTR CALLBACK
338 GraphDlgProc(HWND hDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
340 switch (uMsg) {
341 case WM_INITDIALOG:
342 init_dpi_editbox(hDlg);
343 init_trackbar(hDlg);
344 update_font_preview(hDlg);
345 break;
347 case WM_SHOWWINDOW:
348 set_window_title(hDlg);
349 break;
351 case WM_TIMER:
352 if (wParam == IDT_DPIEDIT)
354 KillTimer(hDlg, IDT_DPIEDIT);
355 update_dpi_trackbar_from_edit(hDlg, TRUE);
356 update_font_preview(hDlg);
358 break;
360 case WM_COMMAND:
361 switch(HIWORD(wParam)) {
362 case EN_CHANGE: {
363 if (updating_ui) break;
364 SendMessageW(GetParent(hDlg), PSM_CHANGED, 0, 0);
365 if ( ((LOWORD(wParam) == IDC_DESKTOP_WIDTH) || (LOWORD(wParam) == IDC_DESKTOP_HEIGHT)) && !updating_ui )
366 set_from_desktop_edits(hDlg);
367 else if (LOWORD(wParam) == IDC_RES_DPIEDIT)
369 update_dpi_trackbar_from_edit(hDlg, FALSE);
370 update_font_preview(hDlg);
371 SetTimer(hDlg, IDT_DPIEDIT, 1500, NULL);
373 break;
375 case BN_CLICKED: {
376 if (updating_ui) break;
377 SendMessageW(GetParent(hDlg), PSM_CHANGED, 0, 0);
378 switch(LOWORD(wParam)) {
379 case IDC_ENABLE_DESKTOP: on_enable_desktop_clicked(hDlg); break;
380 case IDC_ENABLE_MANAGED: on_enable_managed_clicked(hDlg); break;
381 case IDC_ENABLE_DECORATED: on_enable_decorated_clicked(hDlg); break;
382 case IDC_FULLSCREEN_GRAB: on_fullscreen_grab_clicked(hDlg); break;
384 break;
386 case CBN_SELCHANGE: {
387 SendMessageW(GetParent(hDlg), PSM_CHANGED, 0, 0);
388 break;
391 default:
392 break;
394 break;
397 case WM_NOTIFY:
398 switch (((LPNMHDR)lParam)->code) {
399 case PSN_KILLACTIVE: {
400 SetWindowLongPtrW(hDlg, DWLP_MSGRESULT, FALSE);
401 break;
403 case PSN_APPLY: {
404 apply();
405 SetWindowLongPtrW(hDlg, DWLP_MSGRESULT, PSNRET_NOERROR);
406 break;
408 case PSN_SETACTIVE: {
409 init_dialog (hDlg);
410 break;
413 break;
415 case WM_HSCROLL:
416 switch (wParam) {
417 default: {
418 int i = SendMessageW(GetDlgItem(hDlg, IDC_RES_TRACKBAR), TBM_GETPOS, 0, 0);
419 SetDlgItemInt(hDlg, IDC_RES_DPIEDIT, i, TRUE);
420 update_font_preview(hDlg);
421 set_reg_key_dwordW(HKEY_CURRENT_USER, logpixels_reg, logpixels, i);
422 break;
425 break;
427 default:
428 break;
430 return FALSE;