gdi32: Support the gamma correction for the subpixel rendering.
[wine.git] / programs / winecfg / x11drvdlg.c
blobfbc6716e94eb818f500158ac3c1d137199057d96
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};
56 static const UINT dpi_values[] = { 96, 120, 144, 168, 192, 216, 240, 288, 336, 384, 432, 480 };
58 static BOOL updating_ui;
60 /* convert the x11 desktop key to the new explorer config */
61 static void convert_x11_desktop_key(void)
63 char *buf;
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);
72 static void update_gui_for_desktop_mode(HWND dialog)
74 WCHAR *buf, *bufindex;
75 const WCHAR *desktop_name = current_app ? current_app : defaultW;
77 WINE_TRACE("\n");
78 updating_ui = TRUE;
80 buf = get_reg_keyW(config_key, explorer_desktopsW, desktop_name, NULL);
81 if (buf && (bufindex = strchrW(buf, 'x')))
83 *bufindex++ = 0;
85 SetDlgItemTextW(dialog, IDC_DESKTOP_WIDTH, buf);
86 SetDlgItemTextW(dialog, IDC_DESKTOP_HEIGHT, bufindex);
87 } else {
88 SetDlgItemTextA(dialog, IDC_DESKTOP_WIDTH, "800");
89 SetDlgItemTextA(dialog, IDC_DESKTOP_HEIGHT, "600");
91 HeapFree(GetProcessHeap(), 0, buf);
93 /* do we have desktop mode enabled? */
94 if (reg_key_exists(config_key, keypath("Explorer"), "Desktop"))
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);
102 else
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);
111 updating_ui = FALSE;
114 static void init_dialog(HWND dialog)
116 char* buf;
118 convert_x11_desktop_key();
119 update_gui_for_desktop_mode(dialog);
121 updating_ui = TRUE;
123 SendDlgItemMessageW(dialog, IDC_DESKTOP_WIDTH, EM_LIMITTEXT, RES_MAXLEN, 0);
124 SendDlgItemMessageW(dialog, IDC_DESKTOP_HEIGHT, EM_LIMITTEXT, RES_MAXLEN, 0);
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);
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);
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);
147 updating_ui = FALSE;
150 static void set_from_desktop_edits(HWND dialog)
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;
160 if (updating_ui) return;
162 WINE_TRACE("\n");
164 width = get_textW(dialog, IDC_DESKTOP_WIDTH);
165 height = get_textW(dialog, IDC_DESKTOP_HEIGHT);
167 if (!width || !width[0]) {
168 HeapFree(GetProcessHeap(), 0, width);
169 width = strdupW(def_width);
171 else if (atoiW(width) < atoiW(min_width))
173 HeapFree(GetProcessHeap(), 0, width);
174 width = strdupW(min_width);
176 if (!height || !height[0]) {
177 HeapFree(GetProcessHeap(), 0, height);
178 height = strdupW(def_height);
180 else if (atoiW(height) < atoiW(min_height))
182 HeapFree(GetProcessHeap(), 0, height);
183 height = strdupW(min_height);
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);
193 HeapFree(GetProcessHeap(), 0, width);
194 HeapFree(GetProcessHeap(), 0, height);
195 HeapFree(GetProcessHeap(), 0, new);
198 static void on_enable_desktop_clicked(HWND dialog) {
199 WINE_TRACE("\n");
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);
207 update_gui_for_desktop_mode(dialog);
210 static void on_enable_managed_clicked(HWND dialog) {
211 WINE_TRACE("\n");
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");
220 static void on_enable_decorated_clicked(HWND dialog) {
221 WINE_TRACE("\n");
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");
230 static void on_fullscreen_grab_clicked(HWND dialog)
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");
238 static INT read_logpixels_reg(void)
240 DWORD dwLogPixels;
241 WCHAR *buf = get_reg_keyW(HKEY_CURRENT_USER, logpixels_reg, logpixels, NULL);
242 if (!buf) buf = get_reg_keyW(HKEY_CURRENT_CONFIG, def_logpixels_reg, logpixels, NULL);
243 dwLogPixels = buf ? *buf : DEFDPI;
244 HeapFree(GetProcessHeap(), 0, buf);
245 return dwLogPixels;
248 static void init_dpi_editbox(HWND hDlg)
250 DWORD dwLogpixels;
252 updating_ui = TRUE;
254 dwLogpixels = read_logpixels_reg();
255 WINE_TRACE("%u\n", dwLogpixels);
257 SetDlgItemInt(hDlg, IDC_RES_DPIEDIT, dwLogpixels, FALSE);
259 updating_ui = FALSE;
262 static int get_trackbar_pos( UINT dpi )
264 UINT i;
266 for (i = 0; i < ARRAY_SIZE(dpi_values) - 1; i++)
267 if ((dpi_values[i] + dpi_values[i + 1]) / 2 >= dpi) break;
268 return i;
271 static void init_trackbar(HWND hDlg)
273 HWND hTrackBar = GetDlgItem(hDlg, IDC_RES_TRACKBAR);
274 DWORD dwLogpixels;
276 updating_ui = TRUE;
278 dwLogpixels = read_logpixels_reg();
280 SendMessageW(hTrackBar, TBM_SETRANGE, TRUE, MAKELONG(0, ARRAY_SIZE(dpi_values)-1));
281 SendMessageW(hTrackBar, TBM_SETPAGESIZE, 0, 1);
282 SendMessageW(hTrackBar, TBM_SETPOS, TRUE, get_trackbar_pos(dwLogpixels));
284 updating_ui = FALSE;
287 static void update_dpi_trackbar_from_edit(HWND hDlg, BOOL fix)
289 DWORD dpi;
291 updating_ui = TRUE;
293 dpi = GetDlgItemInt(hDlg, IDC_RES_DPIEDIT, NULL, FALSE);
295 if (fix)
297 DWORD fixed_dpi = dpi;
299 if (dpi < MINDPI) fixed_dpi = MINDPI;
300 if (dpi > MAXDPI) fixed_dpi = MAXDPI;
302 if (fixed_dpi != dpi)
304 dpi = fixed_dpi;
305 SetDlgItemInt(hDlg, IDC_RES_DPIEDIT, dpi, FALSE);
309 if (dpi >= MINDPI && dpi <= MAXDPI)
311 SendDlgItemMessageW(hDlg, IDC_RES_TRACKBAR, TBM_SETPOS, TRUE, get_trackbar_pos(dpi));
312 set_reg_key_dwordW(HKEY_CURRENT_USER, logpixels_reg, logpixels, dpi);
315 updating_ui = FALSE;
318 static void update_font_preview(HWND hDlg)
320 DWORD dpi;
322 updating_ui = TRUE;
324 dpi = GetDlgItemInt(hDlg, IDC_RES_DPIEDIT, NULL, FALSE);
326 if (dpi >= MINDPI && dpi <= MAXDPI)
328 static const WCHAR tahomaW[] = {'T','a','h','o','m','a',0};
329 LOGFONTW lf;
330 HFONT hfont;
332 hfont = (HFONT)SendDlgItemMessageW(hDlg, IDC_RES_FONT_PREVIEW, WM_GETFONT, 0, 0);
334 GetObjectW(hfont, sizeof(lf), &lf);
336 if (strcmpW(lf.lfFaceName, tahomaW) != 0)
337 strcpyW(lf.lfFaceName, tahomaW);
338 else
339 DeleteObject(hfont);
340 lf.lfHeight = MulDiv(-10, dpi, 72);
341 hfont = CreateFontIndirectW(&lf);
342 SendDlgItemMessageW(hDlg, IDC_RES_FONT_PREVIEW, WM_SETFONT, (WPARAM)hfont, 1);
345 updating_ui = FALSE;
348 INT_PTR CALLBACK
349 GraphDlgProc(HWND hDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
351 switch (uMsg) {
352 case WM_INITDIALOG:
353 init_dpi_editbox(hDlg);
354 init_trackbar(hDlg);
355 update_font_preview(hDlg);
356 break;
358 case WM_SHOWWINDOW:
359 set_window_title(hDlg);
360 break;
362 case WM_TIMER:
363 if (wParam == IDT_DPIEDIT)
365 KillTimer(hDlg, IDT_DPIEDIT);
366 update_dpi_trackbar_from_edit(hDlg, TRUE);
367 update_font_preview(hDlg);
369 break;
371 case WM_COMMAND:
372 switch(HIWORD(wParam)) {
373 case EN_CHANGE: {
374 if (updating_ui) break;
375 SendMessageW(GetParent(hDlg), PSM_CHANGED, 0, 0);
376 if ( ((LOWORD(wParam) == IDC_DESKTOP_WIDTH) || (LOWORD(wParam) == IDC_DESKTOP_HEIGHT)) && !updating_ui )
377 set_from_desktop_edits(hDlg);
378 else if (LOWORD(wParam) == IDC_RES_DPIEDIT)
380 update_dpi_trackbar_from_edit(hDlg, FALSE);
381 update_font_preview(hDlg);
382 SetTimer(hDlg, IDT_DPIEDIT, 1500, NULL);
384 break;
386 case BN_CLICKED: {
387 if (updating_ui) break;
388 SendMessageW(GetParent(hDlg), PSM_CHANGED, 0, 0);
389 switch(LOWORD(wParam)) {
390 case IDC_ENABLE_DESKTOP: on_enable_desktop_clicked(hDlg); break;
391 case IDC_ENABLE_MANAGED: on_enable_managed_clicked(hDlg); break;
392 case IDC_ENABLE_DECORATED: on_enable_decorated_clicked(hDlg); break;
393 case IDC_FULLSCREEN_GRAB: on_fullscreen_grab_clicked(hDlg); break;
395 break;
397 case CBN_SELCHANGE: {
398 SendMessageW(GetParent(hDlg), PSM_CHANGED, 0, 0);
399 break;
402 default:
403 break;
405 break;
408 case WM_NOTIFY:
409 switch (((LPNMHDR)lParam)->code) {
410 case PSN_KILLACTIVE: {
411 SetWindowLongPtrW(hDlg, DWLP_MSGRESULT, FALSE);
412 break;
414 case PSN_APPLY: {
415 apply();
416 SetWindowLongPtrW(hDlg, DWLP_MSGRESULT, PSNRET_NOERROR);
417 break;
419 case PSN_SETACTIVE: {
420 init_dialog (hDlg);
421 break;
424 break;
426 case WM_HSCROLL:
427 switch (wParam) {
428 default: {
429 int i = SendMessageW(GetDlgItem(hDlg, IDC_RES_TRACKBAR), TBM_GETPOS, 0, 0);
430 SetDlgItemInt(hDlg, IDC_RES_DPIEDIT, dpi_values[i], TRUE);
431 update_font_preview(hDlg);
432 set_reg_key_dwordW(HKEY_CURRENT_USER, logpixels_reg, logpixels, dpi_values[i]);
433 break;
436 break;
438 default:
439 break;
441 return FALSE;