wined3d: Use width while converting surfaces.
[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/debug.h>
32
33 #include "resource.h"
34 #include "winecfg.h"
35
36 WINE_DEFAULT_DEBUG_CHANNEL(winecfg);
37
38 #define RES_MAXLEN 5 /* the maximum number of characters in a screen dimension. 5 digits should be plenty, what kind of crazy person runs their screen >10,000 pixels across? */
39
40
41 static struct SHADERMODE
42 {
43   UINT displayStrID;
44   const char* settingStr;
45 } const D3D_VS_Modes[] = {
46   {IDS_SHADER_MODE_HARDWARE,  "hardware"},
47   {IDS_SHADER_MODE_EMULATION, "emulation"},
48   {IDS_SHADER_MODE_NONE,      "none"},
49   {0, 0}
50 };
51
52
53 int updating_ui;
54
55 static void update_gui_for_desktop_mode(HWND dialog) {
56     int desktopenabled = FALSE;
57
58     WINE_TRACE("\n");
59     updating_ui = TRUE;
60
61     if (current_app)
62     {
63         disable(IDC_ENABLE_DESKTOP);
64         disable(IDC_DESKTOP_WIDTH);
65         disable(IDC_DESKTOP_HEIGHT);
66         disable(IDC_DESKTOP_SIZE);
67         disable(IDC_DESKTOP_BY);
68         return;
69     }
70     enable(IDC_ENABLE_DESKTOP);
71
72     /* do we have desktop mode enabled? */
73     if (reg_key_exists(config_key, keypath("X11 Driver"), "Desktop"))
74     {
75         char* buf, *bufindex;
76         CheckDlgButton(dialog, IDC_ENABLE_DESKTOP, BST_CHECKED);
77
78         buf = get_reg_key(config_key, keypath("X11 Driver"), "Desktop", "640x480");
79         /* note: this test must match the one in x11drv */
80         if( buf[0] != 'n' &&  buf[0] != 'N' &&  buf[0] != 'F' &&  buf[0] != 'f'
81                 &&  buf[0] != '0') {
82             desktopenabled = TRUE;
83             enable(IDC_DESKTOP_WIDTH);
84             enable(IDC_DESKTOP_HEIGHT);
85             enable(IDC_DESKTOP_SIZE);
86             enable(IDC_DESKTOP_BY);
87
88             bufindex = strchr(buf, 'x');
89             if (bufindex) {
90                 *bufindex = 0;
91                 ++bufindex;
92                 SetWindowText(GetDlgItem(dialog, IDC_DESKTOP_WIDTH), buf);
93                 SetWindowText(GetDlgItem(dialog, IDC_DESKTOP_HEIGHT), bufindex);
94             } else {
95                 WINE_TRACE("Desktop registry entry is malformed\n");
96                 SetWindowText(GetDlgItem(dialog, IDC_DESKTOP_WIDTH), "640");
97                 SetWindowText(GetDlgItem(dialog, IDC_DESKTOP_HEIGHT), "480");
98             }
99         }
100         HeapFree(GetProcessHeap(), 0, buf);
101     }
102     if (!desktopenabled)
103     {
104         CheckDlgButton(dialog, IDC_ENABLE_DESKTOP, BST_UNCHECKED);
105         
106         disable(IDC_DESKTOP_WIDTH);
107         disable(IDC_DESKTOP_HEIGHT);
108         disable(IDC_DESKTOP_SIZE);
109         disable(IDC_DESKTOP_BY);
110
111         SetWindowText(GetDlgItem(dialog, IDC_DESKTOP_WIDTH), "");
112         SetWindowText(GetDlgItem(dialog, IDC_DESKTOP_HEIGHT), "");
113     }
114
115     updating_ui = FALSE;
116 }
117
118 static void init_dialog(HWND dialog)
119 {
120     unsigned int it;
121     char* buf;
122
123     update_gui_for_desktop_mode(dialog);
124
125     updating_ui = TRUE;
126     
127     SendDlgItemMessage(dialog, IDC_DESKTOP_WIDTH, EM_LIMITTEXT, RES_MAXLEN, 0);
128     SendDlgItemMessage(dialog, IDC_DESKTOP_HEIGHT, EM_LIMITTEXT, RES_MAXLEN, 0);
129
130     buf = get_reg_key(config_key, keypath("X11 Driver"), "DXGrab", "N");
131     if (IS_OPTION_TRUE(*buf))
132         CheckDlgButton(dialog, IDC_DX_MOUSE_GRAB, BST_CHECKED);
133     else
134         CheckDlgButton(dialog, IDC_DX_MOUSE_GRAB, BST_UNCHECKED);
135     HeapFree(GetProcessHeap(), 0, buf);
136
137     buf = get_reg_key(config_key, keypath("X11 Driver"), "DesktopDoubleBuffered", "Y");
138     if (IS_OPTION_TRUE(*buf))
139         CheckDlgButton(dialog, IDC_DOUBLE_BUFFER, BST_CHECKED);
140     else
141         CheckDlgButton(dialog, IDC_DOUBLE_BUFFER, BST_UNCHECKED);
142     HeapFree(GetProcessHeap(), 0, buf);
143
144     buf = get_reg_key(config_key, keypath("X11 Driver"), "Managed", "Y");
145     if (IS_OPTION_TRUE(*buf))
146         CheckDlgButton(dialog, IDC_ENABLE_MANAGED, BST_CHECKED);
147     else
148         CheckDlgButton(dialog, IDC_ENABLE_MANAGED, BST_UNCHECKED);
149     HeapFree(GetProcessHeap(), 0, buf);
150
151     SendDlgItemMessage(dialog, IDC_D3D_VSHADER_MODE, CB_RESETCONTENT, 0, 0);
152     for (it = 0; 0 != D3D_VS_Modes[it].displayStrID; ++it) {
153       SendDlgItemMessageW (dialog, IDC_D3D_VSHADER_MODE, CB_ADDSTRING, 0,
154           (LPARAM)load_string (D3D_VS_Modes[it].displayStrID));
155     }  
156     buf = get_reg_key(config_key, keypath("Direct3D"), "VertexShaderMode", "hardware"); 
157     for (it = 0; NULL != D3D_VS_Modes[it].settingStr; ++it) {
158       if (strcmp(buf, D3D_VS_Modes[it].settingStr) == 0) {
159         SendDlgItemMessage(dialog, IDC_D3D_VSHADER_MODE, CB_SETCURSEL, it, 0);
160         break ;
161       }
162     }
163     if (NULL == D3D_VS_Modes[it].settingStr) {
164       WINE_ERR("Invalid Direct3D VertexShader Mode read from registry (%s)\n", buf);
165     }
166     HeapFree(GetProcessHeap(), 0, buf);
167
168     buf = get_reg_key(config_key, keypath("Direct3D"), "PixelShaderMode", "enabled");
169     if (!strcmp(buf, "enabled"))
170       CheckDlgButton(dialog, IDC_D3D_PSHADER_MODE, BST_CHECKED);
171     else
172       CheckDlgButton(dialog, IDC_D3D_PSHADER_MODE, BST_UNCHECKED);
173     HeapFree(GetProcessHeap(), 0, buf);
174
175     updating_ui = FALSE;
176 }
177
178 static void set_from_desktop_edits(HWND dialog) {
179     char *width, *height, *new;
180
181     if (updating_ui) return;
182     
183     WINE_TRACE("\n");
184
185     width = get_text(dialog, IDC_DESKTOP_WIDTH);
186     height = get_text(dialog, IDC_DESKTOP_HEIGHT);
187
188     if (width == NULL || strcmp(width, "") == 0) {
189         HeapFree(GetProcessHeap(), 0, width);
190         width = strdupA("640");
191     }
192     
193     if (height == NULL || strcmp(height, "") == 0) {
194         HeapFree(GetProcessHeap(), 0, height);
195         height = strdupA("480");
196     }
197
198     new = HeapAlloc(GetProcessHeap(), 0, strlen(width) + strlen(height) + 2 /* x + terminator */);
199     sprintf(new, "%sx%s", width, height);
200     set_reg_key(config_key, keypath("X11 Driver"), "Desktop", new);
201     
202     HeapFree(GetProcessHeap(), 0, width);
203     HeapFree(GetProcessHeap(), 0, height);
204     HeapFree(GetProcessHeap(), 0, new);
205 }
206
207 static void on_enable_desktop_clicked(HWND dialog) {
208     WINE_TRACE("\n");
209     
210     if (IsDlgButtonChecked(dialog, IDC_ENABLE_DESKTOP) == BST_CHECKED) {
211         set_from_desktop_edits(dialog);
212     } else {
213         set_reg_key(config_key, keypath("X11 Driver"), "Desktop", NULL);
214     }
215     
216     update_gui_for_desktop_mode(dialog);
217 }
218
219 static void on_enable_managed_clicked(HWND dialog) {
220     WINE_TRACE("\n");
221     
222     if (IsDlgButtonChecked(dialog, IDC_ENABLE_MANAGED) == BST_CHECKED) {
223         set_reg_key(config_key, keypath("X11 Driver"), "Managed", "Y");
224     } else {
225         set_reg_key(config_key, keypath("X11 Driver"), "Managed", "N");
226     }
227 }
228
229 static void on_dx_mouse_grab_clicked(HWND dialog) {
230     if (IsDlgButtonChecked(dialog, IDC_DX_MOUSE_GRAB) == BST_CHECKED) 
231         set_reg_key(config_key, keypath("X11 Driver"), "DXGrab", "Y");
232     else
233         set_reg_key(config_key, keypath("X11 Driver"), "DXGrab", "N");
234 }
235
236
237 static void on_double_buffer_clicked(HWND dialog) {
238     if (IsDlgButtonChecked(dialog, IDC_DOUBLE_BUFFER) == BST_CHECKED)
239         set_reg_key(config_key, keypath("X11 Driver"), "DesktopDoubleBuffered", "Y");
240     else
241         set_reg_key(config_key, keypath("X11 Driver"), "DesktopDoubleBuffered", "N");
242 }
243
244 static void on_d3d_vshader_mode_changed(HWND dialog) {
245   int selected_mode = SendDlgItemMessage(dialog, IDC_D3D_VSHADER_MODE, CB_GETCURSEL, 0, 0);  
246   set_reg_key(config_key, keypath("Direct3D"), "VertexShaderMode",
247       D3D_VS_Modes[selected_mode].settingStr); 
248 }
249
250 static void on_d3d_pshader_mode_clicked(HWND dialog) {
251     if (IsDlgButtonChecked(dialog, IDC_D3D_PSHADER_MODE) == BST_CHECKED)
252         set_reg_key(config_key, keypath("Direct3D"), "PixelShaderMode", "enabled");
253     else
254         set_reg_key(config_key, keypath("Direct3D"), "PixelShaderMode", "disabled");
255 }
256
257 INT_PTR CALLBACK
258 GraphDlgProc(HWND hDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
259 {
260     switch (uMsg) {
261         case WM_INITDIALOG:
262             break;
263
264         case WM_SHOWWINDOW:
265             set_window_title(hDlg);
266             break;
267             
268         case WM_COMMAND:
269             switch(HIWORD(wParam)) {
270                 case EN_CHANGE: {
271                     if (updating_ui) break;
272                     SendMessage(GetParent(hDlg), PSM_CHANGED, 0, 0);
273                     if ( ((LOWORD(wParam) == IDC_DESKTOP_WIDTH) || (LOWORD(wParam) == IDC_DESKTOP_HEIGHT)) && !updating_ui )
274                         set_from_desktop_edits(hDlg);
275                     break;
276                 }
277                 case BN_CLICKED: {
278                     if (updating_ui) break;
279                     SendMessage(GetParent(hDlg), PSM_CHANGED, 0, 0);
280                     switch(LOWORD(wParam)) {
281                         case IDC_ENABLE_DESKTOP: on_enable_desktop_clicked(hDlg); break;
282                         case IDC_ENABLE_MANAGED: on_enable_managed_clicked(hDlg); break;
283                         case IDC_DX_MOUSE_GRAB:  on_dx_mouse_grab_clicked(hDlg); break;
284                         case IDC_DOUBLE_BUFFER:  on_double_buffer_clicked(hDlg); break;
285                         case IDC_D3D_PSHADER_MODE: on_d3d_pshader_mode_clicked(hDlg); break;
286                     }
287                     break;
288                 }
289                 case CBN_SELCHANGE: {
290                     SendMessage(GetParent(hDlg), PSM_CHANGED, 0, 0);
291                     switch (LOWORD(wParam)) {
292                     case IDC_D3D_VSHADER_MODE: on_d3d_vshader_mode_changed(hDlg); break;
293                     }
294                     break;
295                 }
296                     
297                 default:
298                     break;
299             }
300             break;
301         
302         
303         case WM_NOTIFY:
304             switch (((LPNMHDR)lParam)->code) {
305                 case PSN_KILLACTIVE: {
306                     SetWindowLongPtr(hDlg, DWLP_MSGRESULT, FALSE);
307                     break;
308                 }
309                 case PSN_APPLY: {
310                     apply();
311                     SetWindowLongPtr(hDlg, DWLP_MSGRESULT, PSNRET_NOERROR);
312                     break;
313                 }
314                 case PSN_SETACTIVE: {
315                     init_dialog (hDlg);
316                     break;
317                 }
318             }
319             break;
320
321         default:
322             break;
323     }
324     return FALSE;
325 }