widl: Write proper C++ forward declarations for interfaces inside namespace.
[wine.git] / programs / winecfg / x11drvdlg.c
blob809068e205a595695bf601e2e1ea2b54567cb47c
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[] = {'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};
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};
56 static BOOL updating_ui;
58 /* convert the x11 desktop key to the new explorer config */
59 static void convert_x11_desktop_key(void)
61 char *buf;
63 if (!(buf = get_reg_key(config_key, "X11 Driver", "Desktop", NULL))) return;
64 set_reg_key(config_key, "Explorer\\Desktops", "Default", buf);
65 set_reg_key(config_key, "Explorer", "Desktop", "Default");
66 set_reg_key(config_key, "X11 Driver", "Desktop", NULL);
67 HeapFree(GetProcessHeap(), 0, buf);
70 static void update_gui_for_desktop_mode(HWND dialog)
72 WCHAR *buf, *bufindex;
73 const WCHAR *desktop_name = current_app ? current_app : defaultW;
75 WINE_TRACE("\n");
76 updating_ui = TRUE;
78 buf = get_reg_keyW(config_key, explorer_desktopsW, desktop_name, NULL);
79 if (buf && (bufindex = strchrW(buf, 'x')))
81 *bufindex = 0;
82 ++bufindex;
83 SetWindowTextW(GetDlgItem(dialog, IDC_DESKTOP_WIDTH), buf);
84 SetWindowTextW(GetDlgItem(dialog, IDC_DESKTOP_HEIGHT), bufindex);
85 } else {
86 SetWindowTextA(GetDlgItem(dialog, IDC_DESKTOP_WIDTH), "800");
87 SetWindowTextA(GetDlgItem(dialog, IDC_DESKTOP_HEIGHT), "600");
89 HeapFree(GetProcessHeap(), 0, buf);
91 /* do we have desktop mode enabled? */
92 if (reg_key_exists(config_key, keypath("Explorer"), "Desktop"))
94 CheckDlgButton(dialog, IDC_ENABLE_DESKTOP, BST_CHECKED);
95 enable(IDC_DESKTOP_WIDTH);
96 enable(IDC_DESKTOP_HEIGHT);
97 enable(IDC_DESKTOP_SIZE);
98 enable(IDC_DESKTOP_BY);
100 else
102 CheckDlgButton(dialog, IDC_ENABLE_DESKTOP, BST_UNCHECKED);
103 disable(IDC_DESKTOP_WIDTH);
104 disable(IDC_DESKTOP_HEIGHT);
105 disable(IDC_DESKTOP_SIZE);
106 disable(IDC_DESKTOP_BY);
109 updating_ui = FALSE;
112 static void init_dialog(HWND dialog)
114 char* buf;
116 convert_x11_desktop_key();
117 update_gui_for_desktop_mode(dialog);
119 updating_ui = TRUE;
121 SendDlgItemMessageW(dialog, IDC_DESKTOP_WIDTH, EM_LIMITTEXT, RES_MAXLEN, 0);
122 SendDlgItemMessageW(dialog, IDC_DESKTOP_HEIGHT, EM_LIMITTEXT, RES_MAXLEN, 0);
124 buf = get_reg_key(config_key, keypath("X11 Driver"), "GrabFullscreen", "N");
125 if (IS_OPTION_TRUE(*buf))
126 CheckDlgButton(dialog, IDC_FULLSCREEN_GRAB, BST_CHECKED);
127 else
128 CheckDlgButton(dialog, IDC_FULLSCREEN_GRAB, BST_UNCHECKED);
129 HeapFree(GetProcessHeap(), 0, buf);
131 buf = get_reg_key(config_key, keypath("X11 Driver"), "Managed", "Y");
132 if (IS_OPTION_TRUE(*buf))
133 CheckDlgButton(dialog, IDC_ENABLE_MANAGED, BST_CHECKED);
134 else
135 CheckDlgButton(dialog, IDC_ENABLE_MANAGED, BST_UNCHECKED);
136 HeapFree(GetProcessHeap(), 0, buf);
138 buf = get_reg_key(config_key, keypath("X11 Driver"), "Decorated", "Y");
139 if (IS_OPTION_TRUE(*buf))
140 CheckDlgButton(dialog, IDC_ENABLE_DECORATED, BST_CHECKED);
141 else
142 CheckDlgButton(dialog, IDC_ENABLE_DECORATED, BST_UNCHECKED);
143 HeapFree(GetProcessHeap(), 0, buf);
145 updating_ui = FALSE;
148 static void set_from_desktop_edits(HWND dialog)
150 static const WCHAR x[] = {'x',0};
151 static const WCHAR def_width[] = {'8','0','0',0};
152 static const WCHAR def_height[] = {'6','0','0',0};
153 static const WCHAR min_width[] = {'6','4','0',0};
154 static const WCHAR min_height[] = {'4','8','0',0};
155 WCHAR *width, *height, *new;
156 const WCHAR *desktop_name = current_app ? current_app : defaultW;
158 if (updating_ui) return;
160 WINE_TRACE("\n");
162 width = get_textW(dialog, IDC_DESKTOP_WIDTH);
163 height = get_textW(dialog, IDC_DESKTOP_HEIGHT);
165 if (!width || !width[0]) {
166 HeapFree(GetProcessHeap(), 0, width);
167 width = strdupW(def_width);
169 else if (atoiW(width) < atoiW(min_width))
171 HeapFree(GetProcessHeap(), 0, width);
172 width = strdupW(min_width);
174 if (!height || !height[0]) {
175 HeapFree(GetProcessHeap(), 0, height);
176 height = strdupW(def_height);
178 else if (atoiW(height) < atoiW(min_height))
180 HeapFree(GetProcessHeap(), 0, height);
181 height = strdupW(min_height);
184 new = HeapAlloc(GetProcessHeap(), 0, (strlenW(width) + strlenW(height) + 2) * sizeof(WCHAR));
185 strcpyW( new, width );
186 strcatW( new, x );
187 strcatW( new, height );
188 set_reg_keyW(config_key, explorer_desktopsW, desktop_name, new);
189 set_reg_keyW(config_key, keypathW(explorerW), desktopW, desktop_name);
191 HeapFree(GetProcessHeap(), 0, width);
192 HeapFree(GetProcessHeap(), 0, height);
193 HeapFree(GetProcessHeap(), 0, new);
196 static void on_enable_desktop_clicked(HWND dialog) {
197 WINE_TRACE("\n");
199 if (IsDlgButtonChecked(dialog, IDC_ENABLE_DESKTOP) == BST_CHECKED) {
200 set_from_desktop_edits(dialog);
201 } else {
202 set_reg_key(config_key, keypath("Explorer"), "Desktop", NULL);
205 update_gui_for_desktop_mode(dialog);
208 static void on_enable_managed_clicked(HWND dialog) {
209 WINE_TRACE("\n");
211 if (IsDlgButtonChecked(dialog, IDC_ENABLE_MANAGED) == BST_CHECKED) {
212 set_reg_key(config_key, keypath("X11 Driver"), "Managed", "Y");
213 } else {
214 set_reg_key(config_key, keypath("X11 Driver"), "Managed", "N");
218 static void on_enable_decorated_clicked(HWND dialog) {
219 WINE_TRACE("\n");
221 if (IsDlgButtonChecked(dialog, IDC_ENABLE_DECORATED) == BST_CHECKED) {
222 set_reg_key(config_key, keypath("X11 Driver"), "Decorated", "Y");
223 } else {
224 set_reg_key(config_key, keypath("X11 Driver"), "Decorated", "N");
228 static void on_fullscreen_grab_clicked(HWND dialog)
230 if (IsDlgButtonChecked(dialog, IDC_FULLSCREEN_GRAB) == BST_CHECKED)
231 set_reg_key(config_key, keypath("X11 Driver"), "GrabFullscreen", "Y");
232 else
233 set_reg_key(config_key, keypath("X11 Driver"), "GrabFullscreen", "N");
236 static INT read_logpixels_reg(void)
238 DWORD dwLogPixels;
239 WCHAR *buf = get_reg_keyW(HKEY_LOCAL_MACHINE, logpixels_reg, logpixels, NULL);
240 dwLogPixels = buf ? *buf : DEFDPI;
241 HeapFree(GetProcessHeap(), 0, buf);
242 return dwLogPixels;
245 static void init_dpi_editbox(HWND hDlg)
247 static const WCHAR fmtW[] = {'%','u',0};
248 DWORD dwLogpixels;
249 WCHAR szLogpixels[MAXBUFLEN];
251 updating_ui = TRUE;
253 dwLogpixels = read_logpixels_reg();
254 WINE_TRACE("%u\n", dwLogpixels);
256 sprintfW(szLogpixels, fmtW, dwLogpixels);
257 SetDlgItemTextW(hDlg, IDC_RES_DPIEDIT, szLogpixels);
259 updating_ui = FALSE;
262 static void init_trackbar(HWND hDlg)
264 HWND hTrackBar = GetDlgItem(hDlg, IDC_RES_TRACKBAR);
265 DWORD dwLogpixels;
267 updating_ui = TRUE;
269 dwLogpixels = read_logpixels_reg();
271 SendMessageW(hTrackBar, TBM_SETRANGE, TRUE, MAKELONG(MINDPI, MAXDPI));
272 SendMessageW(hTrackBar, TBM_SETPOS, TRUE, dwLogpixels);
274 updating_ui = FALSE;
277 static void update_dpi_trackbar_from_edit(HWND hDlg, BOOL fix)
279 static const WCHAR fmtW[] = {'%','u',0};
280 DWORD dpi;
282 updating_ui = TRUE;
284 dpi = GetDlgItemInt(hDlg, IDC_RES_DPIEDIT, NULL, FALSE);
286 if (fix)
288 DWORD fixed_dpi = dpi;
290 if (dpi < MINDPI) fixed_dpi = MINDPI;
291 if (dpi > MAXDPI) fixed_dpi = MAXDPI;
293 if (fixed_dpi != dpi)
295 WCHAR buf[16];
297 dpi = fixed_dpi;
298 sprintfW(buf, fmtW, dpi);
299 SetDlgItemTextW(hDlg, IDC_RES_DPIEDIT, buf);
303 if (dpi >= MINDPI && dpi <= MAXDPI)
305 SendDlgItemMessageW(hDlg, IDC_RES_TRACKBAR, TBM_SETPOS, TRUE, dpi);
306 set_reg_key_dwordW(HKEY_LOCAL_MACHINE, logpixels_reg, logpixels, dpi);
309 updating_ui = FALSE;
312 static void update_font_preview(HWND hDlg)
314 DWORD dpi;
316 updating_ui = TRUE;
318 dpi = GetDlgItemInt(hDlg, IDC_RES_DPIEDIT, NULL, FALSE);
320 if (dpi >= MINDPI && dpi <= MAXDPI)
322 static const WCHAR tahomaW[] = {'T','a','h','o','m','a',0};
323 LOGFONTW lf;
324 HFONT hfont;
326 hfont = (HFONT)SendDlgItemMessageW(hDlg, IDC_RES_FONT_PREVIEW, WM_GETFONT, 0, 0);
328 GetObjectW(hfont, sizeof(lf), &lf);
330 if (strcmpW(lf.lfFaceName, tahomaW) != 0)
331 strcpyW(lf.lfFaceName, tahomaW);
332 else
333 DeleteObject(hfont);
334 lf.lfHeight = MulDiv(-10, dpi, 72);
335 hfont = CreateFontIndirectW(&lf);
336 SendDlgItemMessageW(hDlg, IDC_RES_FONT_PREVIEW, WM_SETFONT, (WPARAM)hfont, 1);
339 updating_ui = FALSE;
342 INT_PTR CALLBACK
343 GraphDlgProc(HWND hDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
345 switch (uMsg) {
346 case WM_INITDIALOG:
347 init_dpi_editbox(hDlg);
348 init_trackbar(hDlg);
349 update_font_preview(hDlg);
350 break;
352 case WM_SHOWWINDOW:
353 set_window_title(hDlg);
354 break;
356 case WM_TIMER:
357 if (wParam == IDT_DPIEDIT)
359 KillTimer(hDlg, IDT_DPIEDIT);
360 update_dpi_trackbar_from_edit(hDlg, TRUE);
361 update_font_preview(hDlg);
363 break;
365 case WM_COMMAND:
366 switch(HIWORD(wParam)) {
367 case EN_CHANGE: {
368 if (updating_ui) break;
369 SendMessageW(GetParent(hDlg), PSM_CHANGED, 0, 0);
370 if ( ((LOWORD(wParam) == IDC_DESKTOP_WIDTH) || (LOWORD(wParam) == IDC_DESKTOP_HEIGHT)) && !updating_ui )
371 set_from_desktop_edits(hDlg);
372 else if (LOWORD(wParam) == IDC_RES_DPIEDIT)
374 update_dpi_trackbar_from_edit(hDlg, FALSE);
375 update_font_preview(hDlg);
376 SetTimer(hDlg, IDT_DPIEDIT, 1500, NULL);
378 break;
380 case BN_CLICKED: {
381 if (updating_ui) break;
382 SendMessageW(GetParent(hDlg), PSM_CHANGED, 0, 0);
383 switch(LOWORD(wParam)) {
384 case IDC_ENABLE_DESKTOP: on_enable_desktop_clicked(hDlg); break;
385 case IDC_ENABLE_MANAGED: on_enable_managed_clicked(hDlg); break;
386 case IDC_ENABLE_DECORATED: on_enable_decorated_clicked(hDlg); break;
387 case IDC_FULLSCREEN_GRAB: on_fullscreen_grab_clicked(hDlg); break;
389 break;
391 case CBN_SELCHANGE: {
392 SendMessageW(GetParent(hDlg), PSM_CHANGED, 0, 0);
393 break;
396 default:
397 break;
399 break;
402 case WM_NOTIFY:
403 switch (((LPNMHDR)lParam)->code) {
404 case PSN_KILLACTIVE: {
405 SetWindowLongPtrW(hDlg, DWLP_MSGRESULT, FALSE);
406 break;
408 case PSN_APPLY: {
409 apply();
410 SetWindowLongPtrW(hDlg, DWLP_MSGRESULT, PSNRET_NOERROR);
411 break;
413 case PSN_SETACTIVE: {
414 init_dialog (hDlg);
415 break;
418 break;
420 case WM_HSCROLL:
421 switch (wParam) {
422 default: {
423 static const WCHAR fmtW[] = {'%','d',0};
424 WCHAR buf[MAXBUFLEN];
425 int i = SendMessageW(GetDlgItem(hDlg, IDC_RES_TRACKBAR), TBM_GETPOS, 0, 0);
426 buf[0] = 0;
427 sprintfW(buf, fmtW, i);
428 SendMessageW(GetDlgItem(hDlg, IDC_RES_DPIEDIT), WM_SETTEXT, 0, (LPARAM) buf);
429 update_font_preview(hDlg);
430 set_reg_key_dwordW(HKEY_LOCAL_MACHINE, logpixels_reg, logpixels, i);
431 break;
434 break;
436 default:
437 break;
439 return FALSE;