Constantin Kaplinsky | 729598c | 2006-05-25 05:12:25 +0000 | [diff] [blame] | 1 | /* Copyright (C) 2002-2005 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 | |
| 29 | #ifdef _DIALOG_CAPTURE |
| 30 | #ifdef PropSheet_IndexToId |
| 31 | #include <rfb_win32/DeviceFrameBuffer.h> |
| 32 | #include <extra/LoadBMP.cxx> |
| 33 | #else |
| 34 | #undef _DIALOG_CAPTURE |
| 35 | #pragma message(" NOTE: Not building Dialog Capture support.") |
| 36 | #endif |
| 37 | #endif |
| 38 | |
| 39 | using namespace rfb; |
| 40 | using namespace rfb::win32; |
| 41 | |
| 42 | static LogWriter dlog("Dialog"); |
| 43 | static LogWriter plog("PropSheet"); |
| 44 | |
| 45 | |
| 46 | Dialog::Dialog(HINSTANCE inst_) |
Peter Åstrand | dc30f8e | 2008-12-10 10:20:54 +0000 | [diff] [blame] | 47 | : inst(inst_), handle(0), alreadyShowing(false) |
Constantin Kaplinsky | 729598c | 2006-05-25 05:12:25 +0000 | [diff] [blame] | 48 | { |
| 49 | } |
| 50 | |
| 51 | Dialog::~Dialog() |
| 52 | { |
| 53 | } |
| 54 | |
| 55 | |
| 56 | bool Dialog::showDialog(const TCHAR* resource, HWND owner) |
| 57 | { |
| 58 | if (alreadyShowing) return false; |
| 59 | handle = 0; |
| 60 | alreadyShowing = true; |
| 61 | INT_PTR result = DialogBoxParam(inst, resource, owner, |
| 62 | staticDialogProc, (LPARAM)this); |
| 63 | if (result<0) |
| 64 | throw rdr::SystemException("DialogBoxParam failed", GetLastError()); |
| 65 | alreadyShowing = false; |
| 66 | return (result == 1); |
| 67 | } |
| 68 | |
| 69 | |
| 70 | bool Dialog::isItemChecked(int id) { |
| 71 | return SendMessage(GetDlgItem(handle, id), BM_GETCHECK, 0, 0) == BST_CHECKED; |
| 72 | } |
| 73 | int Dialog::getItemInt(int id) { |
| 74 | BOOL trans; |
| 75 | int result = GetDlgItemInt(handle, id, &trans, TRUE); |
| 76 | if (!trans) |
| 77 | throw rdr::Exception("unable to read dialog Int"); |
| 78 | return result; |
| 79 | } |
| 80 | TCHAR* Dialog::getItemString(int id) { |
| 81 | TCharArray tmp(256); |
| 82 | if (!GetDlgItemText(handle, id, tmp.buf, 256)) |
| 83 | tmp.buf[0] = 0; |
| 84 | return tmp.takeBuf(); |
| 85 | } |
| 86 | |
| 87 | void Dialog::setItemChecked(int id, bool state) { |
| 88 | dlog.debug("bool[%d]=%d", id, (int)state); |
| 89 | SendMessage(GetDlgItem(handle, id), BM_SETCHECK, state ? BST_CHECKED : BST_UNCHECKED, 0); |
| 90 | } |
| 91 | void Dialog::setItemInt(int id, int value) { |
| 92 | dlog.debug("int[%d]=%d", id, value); |
| 93 | SetDlgItemInt(handle, id, value, TRUE); |
| 94 | } |
| 95 | void Dialog::setItemString(int id, const TCHAR* s) { |
| 96 | dlog.debug("string[%d]=%s", id, (const char*)CStr(s)); |
| 97 | SetDlgItemText(handle, id, s); |
| 98 | } |
| 99 | |
| 100 | |
| 101 | void Dialog::enableItem(int id, bool state) { |
| 102 | dlog.debug("enable[%d]=%d", id, (int)state); |
| 103 | EnableWindow(GetDlgItem(handle, id), state); |
| 104 | } |
| 105 | |
| 106 | |
| 107 | |
| 108 | |
| 109 | BOOL CALLBACK Dialog::staticDialogProc(HWND hwnd, UINT msg, |
| 110 | WPARAM wParam, LPARAM lParam) |
| 111 | { |
| 112 | if (msg == WM_INITDIALOG) |
| 113 | SetWindowLong(hwnd, GWL_USERDATA, (LONG)lParam); |
| 114 | |
| 115 | LONG self = GetWindowLong(hwnd, GWL_USERDATA); |
| 116 | if (!self) return FALSE; |
| 117 | |
| 118 | return ((Dialog*)self)->dialogProc(hwnd, msg, wParam, lParam); |
| 119 | } |
| 120 | |
| 121 | BOOL Dialog::dialogProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) |
| 122 | { |
| 123 | switch (msg) { |
| 124 | |
| 125 | case WM_INITDIALOG: |
| 126 | handle = hwnd; |
| 127 | initDialog(); |
| 128 | return TRUE; |
| 129 | |
| 130 | case WM_COMMAND: |
| 131 | switch (LOWORD(wParam)) { |
| 132 | case IDOK: |
| 133 | if (onOk()) { |
| 134 | EndDialog(hwnd, 1); |
| 135 | return TRUE; |
| 136 | } |
| 137 | return FALSE; |
| 138 | case IDCANCEL: |
| 139 | EndDialog(hwnd, 0); |
| 140 | return TRUE; |
| 141 | default: |
| 142 | return onCommand(LOWORD(wParam), HIWORD(wParam)); |
| 143 | }; |
| 144 | |
| 145 | case WM_HELP: |
| 146 | return onHelp(((HELPINFO*)lParam)->iCtrlId); |
| 147 | |
| 148 | } |
| 149 | |
| 150 | return FALSE; |
| 151 | } |
| 152 | |
| 153 | |
| 154 | PropSheetPage::PropSheetPage(HINSTANCE inst, const TCHAR* id) : Dialog(inst), propSheet(0) { |
| 155 | page.dwSize = sizeof(page); |
| 156 | page.dwFlags = 0; // PSP_USECALLBACK; |
| 157 | page.hInstance = inst; |
| 158 | page.pszTemplate = id; |
| 159 | page.pfnDlgProc = staticPageProc; |
| 160 | page.lParam = (LPARAM)this; |
| 161 | page.pfnCallback = 0; // staticPageProc; |
| 162 | } |
| 163 | |
| 164 | PropSheetPage::~PropSheetPage() { |
| 165 | } |
| 166 | |
| 167 | |
| 168 | BOOL CALLBACK PropSheetPage::staticPageProc(HWND hwnd, UINT msg, |
| 169 | WPARAM wParam, LPARAM lParam) |
| 170 | { |
| 171 | if (msg == WM_INITDIALOG) |
| 172 | SetWindowLong(hwnd, GWL_USERDATA, ((PROPSHEETPAGE*)lParam)->lParam); |
| 173 | |
| 174 | LONG self = GetWindowLong(hwnd, GWL_USERDATA); |
| 175 | if (!self) return FALSE; |
| 176 | |
| 177 | return ((PropSheetPage*)self)->dialogProc(hwnd, msg, wParam, lParam); |
| 178 | } |
| 179 | |
| 180 | BOOL PropSheetPage::dialogProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) |
| 181 | { |
| 182 | switch (msg) { |
| 183 | |
| 184 | case WM_INITDIALOG: |
| 185 | handle = hwnd; |
| 186 | initDialog(); |
| 187 | return TRUE; |
| 188 | |
| 189 | case WM_NOTIFY: |
| 190 | switch (((NMHDR*)lParam)->code) { |
| 191 | case PSN_APPLY: |
| 192 | onOk(); |
| 193 | return FALSE; |
| 194 | }; |
| 195 | return FALSE; |
| 196 | |
| 197 | case WM_COMMAND: |
| 198 | return onCommand(LOWORD(wParam), HIWORD(wParam)); |
| 199 | |
| 200 | case WM_HELP: |
| 201 | return onHelp(((HELPINFO*)lParam)->iCtrlId); |
| 202 | |
| 203 | } |
| 204 | |
| 205 | return FALSE; |
| 206 | } |
| 207 | |
| 208 | |
| 209 | PropSheet::PropSheet(HINSTANCE inst_, const TCHAR* title_, std::list<PropSheetPage*> pages_, HICON icon_) |
Peter Åstrand | dc30f8e | 2008-12-10 10:20:54 +0000 | [diff] [blame] | 210 | : icon(icon_), pages(pages_), inst(inst_), title(tstrDup(title_)), handle(0), alreadyShowing(0) { |
Constantin Kaplinsky | 729598c | 2006-05-25 05:12:25 +0000 | [diff] [blame] | 211 | } |
| 212 | |
| 213 | PropSheet::~PropSheet() { |
| 214 | } |
| 215 | |
| 216 | |
| 217 | // For some reason, DLGTEMPLATEEX isn't defined in the Windows headers - go figure... |
| 218 | struct DLGTEMPLATEEX { |
| 219 | WORD dlgVer; |
| 220 | WORD signature; |
| 221 | DWORD helpID; |
| 222 | DWORD exStyle; |
| 223 | DWORD style; |
| 224 | WORD cDlgItems; |
| 225 | short x; |
| 226 | short y; |
| 227 | short cx; |
| 228 | short cy; |
| 229 | }; |
| 230 | |
| 231 | static int CALLBACK removeCtxtHelp(HWND hwnd, UINT message, LPARAM lParam) { |
| 232 | if (message == PSCB_PRECREATE) { |
| 233 | // Remove the context-help style, to remove the titlebar ? button |
| 234 | // *** Nasty hack to cope with new & old dialog template formats... |
| 235 | if (((DLGTEMPLATEEX*)lParam)->signature == 0xffff) |
| 236 | ((DLGTEMPLATEEX*)lParam)->style &= ~DS_CONTEXTHELP; |
| 237 | else |
| 238 | ((LPDLGTEMPLATE)lParam)->style &= ~DS_CONTEXTHELP; |
| 239 | } |
| 240 | return TRUE; |
| 241 | } |
| 242 | |
| 243 | |
| 244 | bool PropSheet::showPropSheet(HWND owner, bool showApply, bool showCtxtHelp, bool capture) { |
| 245 | if (alreadyShowing) return false; |
| 246 | alreadyShowing = true; |
| 247 | int count = pages.size(); |
| 248 | |
| 249 | HPROPSHEETPAGE* hpages = new HPROPSHEETPAGE[count]; |
| 250 | try { |
| 251 | // Create the PropertSheet page GDI objects. |
| 252 | std::list<PropSheetPage*>::iterator pspi; |
| 253 | int i = 0; |
| 254 | for (pspi=pages.begin(); pspi!=pages.end(); pspi++) { |
| 255 | hpages[i] = CreatePropertySheetPage(&((*pspi)->page)); |
| 256 | (*pspi)->setPropSheet(this); |
| 257 | i++; |
| 258 | } |
| 259 | |
| 260 | // Initialise and create the PropertySheet itself |
| 261 | PROPSHEETHEADER header; |
Peter Åstrand | cf75be1 | 2008-12-09 10:50:40 +0000 | [diff] [blame] | 262 | header.dwSize = sizeof(PROPSHEETHEADER); // Requires comctl32.dll 4.71 or greater, ie IE 4 or later |
Constantin Kaplinsky | 729598c | 2006-05-25 05:12:25 +0000 | [diff] [blame] | 263 | header.dwFlags = PSH_MODELESS | (showApply ? 0 : PSH_NOAPPLYNOW) | (showCtxtHelp ? 0 : PSH_USECALLBACK); |
| 264 | header.pfnCallback = removeCtxtHelp; |
| 265 | header.hwndParent = owner; |
| 266 | header.hInstance = inst; |
| 267 | header.pszCaption = title.buf; |
| 268 | header.nPages = count; |
| 269 | header.nStartPage = 0; |
| 270 | header.phpage = hpages; |
| 271 | if (icon) { |
| 272 | header.hIcon = icon; |
| 273 | header.dwFlags |= PSH_USEHICON; |
| 274 | } |
| 275 | |
| 276 | handle = (HWND)PropertySheet(&header); |
| 277 | if ((handle == 0) || (handle == (HWND)-1)) |
| 278 | throw rdr::SystemException("PropertySheet failed", GetLastError()); |
| 279 | centerWindow(handle, owner); |
| 280 | plog.info("created %lx", handle); |
| 281 | |
| 282 | #ifdef _DIALOG_CAPTURE |
| 283 | if (capture) { |
| 284 | plog.info("capturing \"%s\"", (const char*)CStr(title.buf)); |
| 285 | char* tmpdir = getenv("TEMP"); |
| 286 | HDC dc = GetWindowDC(handle); |
| 287 | DeviceFrameBuffer fb(dc); |
| 288 | int i=0; |
| 289 | while (true) { |
| 290 | int id = PropSheet_IndexToId(handle, i); |
| 291 | if (!id) break; |
| 292 | PropSheet_SetCurSelByID(handle, id); |
| 293 | MSG msg; |
| 294 | while (PeekMessage(&msg, handle, 0, 0, PM_REMOVE)) { |
| 295 | if (!PropSheet_IsDialogMessage(handle, &msg)) |
| 296 | DispatchMessage(&msg); |
| 297 | } |
| 298 | fb.grabRect(fb.getRect()); |
| 299 | TCHAR title[128]; |
| 300 | if (!GetWindowText(PropSheet_GetCurrentPageHwnd(handle), title, sizeof(title))) |
| 301 | _stprintf(title, _T("capture%d"), i); |
| 302 | CharArray pageTitle(strDup(title)); |
| 303 | for (int j=0; j<strlen(pageTitle.buf); j++) { |
| 304 | if (pageTitle.buf[j] == '/' || pageTitle.buf[j] == '\\' || pageTitle.buf[j] == ':') |
| 305 | pageTitle.buf[j] = '-'; |
| 306 | } |
| 307 | char filename[256]; |
| 308 | sprintf(filename, "%s\\%s.bmp", tmpdir, pageTitle.buf); |
| 309 | vlog.debug("writing to %s", filename); |
| 310 | saveBMP(filename, &fb); |
| 311 | i++; |
| 312 | } |
| 313 | ReleaseDC(handle, dc); |
| 314 | } else { |
| 315 | #endif |
| 316 | try { |
| 317 | if (owner) |
| 318 | EnableWindow(owner, FALSE); |
| 319 | // Run the PropertySheet |
| 320 | MSG msg; |
| 321 | while (GetMessage(&msg, 0, 0, 0)) { |
| 322 | if (!PropSheet_IsDialogMessage(handle, &msg)) |
| 323 | DispatchMessage(&msg); |
| 324 | if (!PropSheet_GetCurrentPageHwnd(handle)) |
| 325 | break; |
| 326 | } |
| 327 | if (owner) |
| 328 | EnableWindow(owner, TRUE); |
| 329 | } catch (...) { |
| 330 | if (owner) |
| 331 | EnableWindow(owner, TRUE); |
| 332 | throw; |
| 333 | } |
| 334 | #ifdef _DIALOG_CAPTURE |
| 335 | } |
| 336 | #endif |
| 337 | |
| 338 | plog.info("finished %lx", handle); |
| 339 | |
| 340 | DestroyWindow(handle); |
| 341 | handle = 0; |
| 342 | alreadyShowing = false; |
| 343 | |
| 344 | // Clear up the pages' GDI objects |
| 345 | for (pspi=pages.begin(); pspi!=pages.end(); pspi++) |
| 346 | (*pspi)->setPropSheet(0); |
| 347 | delete [] hpages; hpages = 0; |
| 348 | |
| 349 | return true; |
| 350 | } catch (rdr::Exception) { |
| 351 | alreadyShowing = false; |
| 352 | |
| 353 | std::list<PropSheetPage*>::iterator pspi; |
| 354 | for (pspi=pages.begin(); pspi!=pages.end(); pspi++) |
| 355 | (*pspi)->setPropSheet(0); |
| 356 | delete [] hpages; hpages = 0; |
| 357 | |
| 358 | throw; |
| 359 | } |
| 360 | } |
| 361 | |
| 362 | void PropSheet::reInitPages() { |
| 363 | plog.debug("reInitPages %lx", handle); |
| 364 | std::list<PropSheetPage*>::iterator pspi; |
| 365 | for (pspi=pages.begin(); pspi!=pages.end(); pspi++) { |
| 366 | if ((*pspi)->handle) |
| 367 | (*pspi)->initDialog(); |
| 368 | } |
| 369 | } |
| 370 | |
| 371 | bool PropSheet::commitPages() { |
| 372 | plog.debug("commitPages %lx", handle); |
| 373 | bool result = true; |
| 374 | std::list<PropSheetPage*>::iterator pspi; |
| 375 | for (pspi=pages.begin(); pspi!=pages.end(); pspi++) { |
| 376 | if ((*pspi)->handle) |
| 377 | result = result && (*pspi)->onOk(); |
| 378 | } |
| 379 | return result; |
| 380 | } |
| 381 | |
| 382 | |
| 383 | void PropSheetPage::setChanged(bool changed) { |
| 384 | if (propSheet) { |
| 385 | plog.debug("setChanged[%lx(%lx)]=%d", handle, propSheet->handle, (int)changed); |
| 386 | if (changed) |
| 387 | PropSheet_Changed(propSheet->handle, handle); |
| 388 | else |
| 389 | PropSheet_UnChanged(propSheet->handle, handle); |
| 390 | } |
| 391 | } |