blob: 157cf5feaeae218e894b190aca8dc44ebc90492e [file] [log] [blame]
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +00001/* Copyright (C) 2002-2003 RealVNC Ltd. All Rights Reserved.
2 *
3 * This is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This software is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this software; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
16 * USA.
17 */
18
19// -=- Dialog.cxx
20
21// Base-class for any Dialog classes we might require
22
23#include <rfb_win32/Dialog.h>
24#include <rfb_win32/TCharArray.h>
25#include <rfb/LogWriter.h>
26#include <rdr/Exception.h>
27#include <rfb_win32/Win32Util.h>
28#ifdef _DIALOG_CAPTURE
29#include <rfb_win32/DeviceFrameBuffer.h>
30#include <extra/LoadBMP.cxx>
31#endif
32
33using namespace rfb;
34using namespace rfb::win32;
35
36static LogWriter dlog("Dialog");
37static LogWriter plog("PropSheet");
38
39
40Dialog::Dialog(HINSTANCE inst_)
41: inst(inst_), alreadyShowing(false), handle(0)
42{
43}
44
45Dialog::~Dialog()
46{
47}
48
49
50bool Dialog::showDialog(const TCHAR* resource, HWND owner)
51{
52 if (alreadyShowing) return false;
53 handle = 0;
54 alreadyShowing = true;
55 INT_PTR result = DialogBoxParam(inst, resource, owner,
56 staticDialogProc, (LPARAM)this);
57 if (result<0)
58 throw rdr::SystemException("DialogBoxParam failed", GetLastError());
59 alreadyShowing = false;
60 return (result == 1);
61}
62
63
64bool Dialog::isItemChecked(int id) {
65 return SendMessage(GetDlgItem(handle, id), BM_GETCHECK, 0, 0) == BST_CHECKED;
66}
67int Dialog::getItemInt(int id) {
68 BOOL trans;
69 int result = GetDlgItemInt(handle, id, &trans, TRUE);
70 if (!trans)
71 throw rdr::Exception("unable to read dialog Int");
72 return result;
73}
74TCHAR* Dialog::getItemString(int id) {
75 TCharArray tmp(256);
76 if (!GetDlgItemText(handle, id, tmp.buf, 256))
77 tmp.buf[0] = 0;
78 return tmp.takeBuf();
79}
80
81void Dialog::setItemChecked(int id, bool state) {
82 dlog.debug("bool[%d]=%d", id, (int)state);
83 SendMessage(GetDlgItem(handle, id), BM_SETCHECK, state ? BST_CHECKED : BST_UNCHECKED, 0);
84}
85void Dialog::setItemInt(int id, int value) {
86 dlog.debug("int[%d]=%d", id, value);
87 SetDlgItemInt(handle, id, value, TRUE);
88}
89void Dialog::setItemString(int id, const TCHAR* s) {
90 dlog.debug("string[%d]=%s", id, (const char*)CStr(s));
91 SetDlgItemText(handle, id, s);
92}
93
94
95void Dialog::enableItem(int id, bool state) {
96 dlog.debug("enable[%d]=%d", id, (int)state);
97 EnableWindow(GetDlgItem(handle, id), state);
98}
99
100
101
102
103BOOL CALLBACK Dialog::staticDialogProc(HWND hwnd, UINT msg,
104 WPARAM wParam, LPARAM lParam)
105{
106 if (msg == WM_INITDIALOG)
107 SetWindowLong(hwnd, GWL_USERDATA, (LONG)lParam);
108
109 LONG self = GetWindowLong(hwnd, GWL_USERDATA);
110 if (!self) return FALSE;
111
112 return ((Dialog*)self)->dialogProc(hwnd, msg, wParam, lParam);
113}
114
115BOOL Dialog::dialogProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
116{
117 switch (msg) {
118
119 case WM_INITDIALOG:
120 handle = hwnd;
121 initDialog();
122 return TRUE;
123
124 case WM_COMMAND:
125 switch (LOWORD(wParam)) {
126 case IDOK:
127 if (onOk()) {
128 EndDialog(hwnd, 1);
129 return TRUE;
130 }
131 return FALSE;
132 case IDCANCEL:
133 EndDialog(hwnd, 0);
134 return TRUE;
135 default:
136 return onCommand(LOWORD(wParam), HIWORD(wParam));
137 };
138
139 case WM_HELP:
140 return onHelp(((HELPINFO*)lParam)->iCtrlId);
141
142 }
143
144 return FALSE;
145}
146
147
148PropSheetPage::PropSheetPage(HINSTANCE inst, const TCHAR* id) : Dialog(inst), propSheet(0) {
149 page.dwSize = sizeof(page);
150 page.dwFlags = 0; // PSP_USECALLBACK;
151 page.hInstance = inst;
152 page.pszTemplate = id;
153 page.pfnDlgProc = staticPageProc;
154 page.lParam = (LPARAM)this;
155 page.pfnCallback = 0; // staticPageProc;
156}
157
158PropSheetPage::~PropSheetPage() {
159}
160
161
162BOOL CALLBACK PropSheetPage::staticPageProc(HWND hwnd, UINT msg,
163 WPARAM wParam, LPARAM lParam)
164{
165 if (msg == WM_INITDIALOG)
166 SetWindowLong(hwnd, GWL_USERDATA, ((PROPSHEETPAGE*)lParam)->lParam);
167
168 LONG self = GetWindowLong(hwnd, GWL_USERDATA);
169 if (!self) return FALSE;
170
171 return ((PropSheetPage*)self)->dialogProc(hwnd, msg, wParam, lParam);
172}
173
174BOOL PropSheetPage::dialogProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
175{
176 switch (msg) {
177
178 case WM_INITDIALOG:
179 handle = hwnd;
180 initDialog();
181 return TRUE;
182
183 case WM_NOTIFY:
184 switch (((NMHDR*)lParam)->code) {
185 case PSN_APPLY:
186 onOk();
187 return FALSE;
188 };
189 return FALSE;
190
191 case WM_COMMAND:
192 return onCommand(LOWORD(wParam), HIWORD(wParam));
193
194 case WM_HELP:
195 return onHelp(((HELPINFO*)lParam)->iCtrlId);
196
197 }
198
199 return FALSE;
200}
201
202
203PropSheet::PropSheet(HINSTANCE inst_, const TCHAR* title_, std::list<PropSheetPage*> pages_, HICON icon_)
204: title(tstrDup(title_)), inst(inst_), pages(pages_), alreadyShowing(0), handle(0), icon(icon_) {
205}
206
207PropSheet::~PropSheet() {
208}
209
210
211bool PropSheet::showPropSheet(HWND owner, bool showApply, bool showCtxtHelp, bool capture) {
212 if (alreadyShowing) return false;
213 alreadyShowing = true;
214 int count = pages.size();
215
216 HPROPSHEETPAGE* hpages = new HPROPSHEETPAGE[count];
217 try {
218 // Create the PropertSheet page GDI objects.
219 std::list<PropSheetPage*>::iterator pspi;
220 int i = 0;
221 for (pspi=pages.begin(); pspi!=pages.end(); pspi++) {
222 hpages[i] = CreatePropertySheetPage(&((*pspi)->page));
223 (*pspi)->setPropSheet(this);
224 i++;
225 }
226
227 // Initialise and create the PropertySheet itself
228 PROPSHEETHEADER header;
229 header.dwSize = PROPSHEETHEADER_V1_SIZE;
230 header.dwFlags = PSH_MODELESS | (showApply ? 0 : PSH_NOAPPLYNOW) /*| (showCtxtHelp ? 0 : PSH_NOCONTEXTHELP)*/;
231 header.hwndParent = owner;
232 header.hInstance = inst;
233 header.pszCaption = title.buf;
234 header.nPages = count;
235 header.nStartPage = 0;
236 header.phpage = hpages;
237 if (icon) {
238 header.hIcon = icon;
239 header.dwFlags |= PSH_USEHICON;
240 }
241
242 handle = (HWND)PropertySheet(&header);
243 if ((handle == 0) || (handle == (HWND)-1))
244 throw rdr::SystemException("PropertySheet failed", GetLastError());
245 centerWindow(handle, owner);
246 plog.info("created %lx", handle);
247
248#if (WINVER >= 0x0500)
249#ifdef _DIALOG_CAPTURE
250 // *** NOT TESTED
251 if (capture) {
252 plog.info("capturing \"%s\"", (const char*)CStr(title.buf));
253 char* tmpdir = getenv("TEMP");
254 HDC dc = GetWindowDC(handle);
255 DeviceFrameBuffer fb(dc);
256 int i=0;
257 while (true) {
258 int id = PropSheet_IndexToId(handle, i);
259 if (!id) break;
260 PropSheet_SetCurSelByID(handle, id);
261 MSG msg;
262 while (PeekMessage(&msg, handle, 0, 0, PM_REMOVE)) {
263 if (!PropSheet_IsDialogMessage(handle, &msg))
264 DispatchMessage(&msg);
265 }
266 fb.grabRect(fb.getRect());
267 char filename[256];
268 sprintf(filename, "%s\\capture%d.bmp", tmpdir, i);
269 saveBMP(filename, &fb);
270 i++;
271 }
272 ReleaseDC(handle, dc);
273 } else {
274#endif
275#endif
276 try {
277 if (owner)
278 EnableWindow(owner, FALSE);
279 // Run the PropertySheet
280 MSG msg;
281 while (GetMessage(&msg, 0, 0, 0)) {
282 if (!PropSheet_IsDialogMessage(handle, &msg))
283 DispatchMessage(&msg);
284 if (!PropSheet_GetCurrentPageHwnd(handle))
285 break;
286 }
287 if (owner)
288 EnableWindow(owner, TRUE);
289 } catch (...) {
290 if (owner)
291 EnableWindow(owner, TRUE);
292 throw;
293 }
294#if (WINVER >= 0x0500)
295#ifdef _DIALOG_CAPTURE
296 }
297#endif
298#endif
299
300 plog.info("finished %lx", handle);
301
302 DestroyWindow(handle);
303 handle = 0;
304 alreadyShowing = false;
305
306 // Clear up the pages' GDI objects
307 for (pspi=pages.begin(); pspi!=pages.end(); pspi++)
308 (*pspi)->setPropSheet(0);
309 delete [] hpages; hpages = 0;
310
311 return true;
312 } catch (rdr::Exception) {
313 alreadyShowing = false;
314
315 std::list<PropSheetPage*>::iterator pspi;
316 for (pspi=pages.begin(); pspi!=pages.end(); pspi++)
317 (*pspi)->setPropSheet(0);
318 delete [] hpages; hpages = 0;
319
320 throw;
321 }
322}
323
324void PropSheet::reInitPages() {
325 plog.debug("reInitPages %lx", handle);
326 std::list<PropSheetPage*>::iterator pspi;
327 for (pspi=pages.begin(); pspi!=pages.end(); pspi++) {
328 if ((*pspi)->handle)
329 (*pspi)->initDialog();
330 }
331}
332
333bool PropSheet::commitPages() {
334 plog.debug("commitPages %lx", handle);
335 bool result = true;
336 std::list<PropSheetPage*>::iterator pspi;
337 for (pspi=pages.begin(); pspi!=pages.end(); pspi++) {
338 if ((*pspi)->handle)
339 result = result && (*pspi)->onOk();
340 }
341 return result;
342}
343
344
345void PropSheetPage::setChanged(bool changed) {
346 if (propSheet) {
347 plog.debug("setChanged[%lx(%lx)]=%d", handle, propSheet->handle, (int)changed);
348 if (changed)
349 PropSheet_Changed(propSheet->handle, handle);
350 else
351 PropSheet_UnChanged(propSheet->handle, handle);
352 }
353}