You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Dialog.cxx 9.9KB

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