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 10.0KB

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