Leaked source code of windows server 2003
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.

653 lines
15 KiB

  1. /*++
  2. Copyright (c) 1990-2003 Microsoft Corporation
  3. All Rights Reserved
  4. Module Name:
  5. cpsuisam.c
  6. Abstract:
  7. This module contrains sample prototype for the
  8. Windows 2000/Windows XP/Windows Server 2003 common Property
  9. Sheet user interface
  10. --*/
  11. #include "precomp.h"
  12. #pragma hdrstop
  13. #define DBG_CPSUIFILENAME DbgCPSUISam
  14. #define DBG_WINMAINPROC 0x00000001
  15. DEFINE_DBGVAR(0);
  16. #define HAS_TVTEST_PAGES 0x01
  17. #define HAS_DOCPROP_PAGES 0x02
  18. extern HINSTANCE hInstApp;
  19. extern TCHAR TitleName[];
  20. extern TCHAR ClassName[];
  21. extern TCHAR MenuName[];
  22. extern TCHAR szWinSpool[];
  23. extern CHAR szDocPropSheets[];
  24. extern BOOL UpdatePermission;
  25. extern BOOL UseStdAbout;
  26. HWND hWndApp = NULL;
  27. typedef struct _CPSUISAMPLE
  28. {
  29. HANDLE hParent;
  30. HANDLE hCPSUI;
  31. HANDLE hDocProp;
  32. PFNCOMPROPSHEET pfnCPS;
  33. COMPROPSHEETUI CPSUI;
  34. DOCUMENTPROPERTYHEADER DPHdr;
  35. HGLOBAL hDevMode;
  36. HGLOBAL hDevNames;
  37. } CPSUISAMPLE, *PCPSUISAMPLE;
  38. BOOL
  39. GetDefPrinter
  40. (
  41. PCPSUISAMPLE pCPSUISample
  42. )
  43. {
  44. HGLOBAL h;
  45. LPTSTR pszPrinterName;
  46. DEVMODE *pDM;
  47. DEVNAMES *pDN;
  48. HANDLE hPrinter = NULL;
  49. PAGESETUPDLG PSD;
  50. BOOL Ok = FALSE;
  51. ZeroMemory(&PSD, sizeof(PSD));
  52. PSD.lStructSize = sizeof(PSD);
  53. PSD.Flags = PSD_RETURNDEFAULT;
  54. if (PageSetupDlg(&PSD))
  55. {
  56. if ( (h = PSD.hDevMode)
  57. && (pDM = (DEVMODE *)GlobalLock(h))
  58. && (h = PSD.hDevNames)
  59. && (pDN = (DEVNAMES *)GlobalLock(h))
  60. && (pszPrinterName = (LPTSTR)((LPBYTE)pDN + pDN->wDeviceOffset))
  61. && (OpenPrinter(pszPrinterName, &hPrinter, NULL))
  62. )
  63. {
  64. Ok = TRUE;
  65. }
  66. if (Ok)
  67. {
  68. if (h = pCPSUISample->hDevMode)
  69. {
  70. GlobalUnlock(h);
  71. GlobalFree(h);
  72. }
  73. if (h = pCPSUISample->hDevNames)
  74. {
  75. GlobalUnlock(h);
  76. GlobalFree(h);
  77. }
  78. if (pCPSUISample->DPHdr.hPrinter)
  79. {
  80. ClosePrinter(pCPSUISample->DPHdr.hPrinter);
  81. }
  82. pCPSUISample->hDevMode = PSD.hDevMode;
  83. pCPSUISample->hDevNames = PSD.hDevNames;
  84. pCPSUISample->DPHdr.cbSize = sizeof(DOCUMENTPROPERTYHEADER);
  85. pCPSUISample->DPHdr.hPrinter = hPrinter;
  86. pCPSUISample->DPHdr.pszPrinterName = pszPrinterName;
  87. pCPSUISample->DPHdr.pdmIn =
  88. pCPSUISample->DPHdr.pdmOut = pDM;
  89. pCPSUISample->DPHdr.fMode = (DM_IN_BUFFER | DM_IN_PROMPT | DM_OUT_BUFFER);
  90. if (!UpdatePermission)
  91. {
  92. pCPSUISample->DPHdr.fMode |= DM_NOPERMISSION;
  93. }
  94. }
  95. else
  96. {
  97. if (h = PSD.hDevMode)
  98. {
  99. GlobalUnlock(h);
  100. GlobalFree(h);
  101. }
  102. if (h = PSD.hDevNames)
  103. {
  104. GlobalUnlock(h);
  105. GlobalFree(h);
  106. }
  107. if (hPrinter)
  108. {
  109. ClosePrinter(hPrinter);
  110. }
  111. }
  112. }
  113. return Ok;
  114. }
  115. LONG
  116. CALLBACK
  117. CPSUIFunc
  118. (
  119. PPROPSHEETUI_INFO pPSUIInfo,
  120. LPARAM lParam
  121. )
  122. {
  123. PPROPSHEETUI_INFO_HEADER pPSUIInfoHdr;
  124. PCPSUISAMPLE pCPSUISample;
  125. HANDLE h;
  126. INSERTPSUIPAGE_INFO InsPI;
  127. if (!pPSUIInfo)
  128. {
  129. return FALSE;
  130. }
  131. switch (pPSUIInfo->Reason)
  132. {
  133. case PROPSHEETUI_REASON_INIT:
  134. if (!(pCPSUISample = (PCPSUISAMPLE)LocalAlloc(LPTR, sizeof(CPSUISAMPLE))))
  135. {
  136. return(-1);
  137. }
  138. pPSUIInfo->UserData = (ULONG_PTR)pCPSUISample;
  139. pCPSUISample->hParent = pPSUIInfo->hComPropSheet;
  140. pCPSUISample->pfnCPS = pPSUIInfo->pfnComPropSheet;
  141. //
  142. // Add Document Property Sheet for current default Printer
  143. //
  144. switch (pPSUIInfo->lParamInit)
  145. {
  146. case IDM_DOCPROP:
  147. case IDM_DOCPROP_TVTEST:
  148. if (GetDefPrinter(pCPSUISample))
  149. {
  150. InsPI.cbSize = sizeof(INSERTPSUIPAGE_INFO);
  151. InsPI.Type = PSUIPAGEINSERT_DLL;
  152. InsPI.Mode = INSPSUIPAGE_MODE_FIRST_CHILD;
  153. InsPI.dwData1 = (ULONG_PTR)szWinSpool;
  154. InsPI.dwData2 = (ULONG_PTR)szDocPropSheets;
  155. InsPI.dwData3 = (ULONG_PTR)&(pCPSUISample->DPHdr);
  156. pCPSUISample->hDocProp =
  157. (HANDLE)pCPSUISample->pfnCPS( pCPSUISample->hParent,
  158. CPSFUNC_INSERT_PSUIPAGE,
  159. (LPARAM)0,
  160. (LPARAM)&InsPI
  161. );
  162. }
  163. break;
  164. }
  165. //
  166. // Add TreeView Page
  167. //
  168. switch (pPSUIInfo->lParamInit)
  169. {
  170. case IDM_TVTEST:
  171. case IDM_DOCPROP_TVTEST:
  172. if (SetupComPropSheetUI(&(pCPSUISample->CPSUI)))
  173. {
  174. InsPI.cbSize = sizeof(INSERTPSUIPAGE_INFO);
  175. InsPI.Type = PSUIPAGEINSERT_PCOMPROPSHEETUI;
  176. InsPI.Mode = INSPSUIPAGE_MODE_FIRST_CHILD;
  177. InsPI.dwData1 = (ULONG_PTR)&(pCPSUISample->CPSUI);
  178. InsPI.dwData2 =
  179. InsPI.dwData3 = 0;
  180. pCPSUISample->hCPSUI =
  181. (HANDLE)pCPSUISample->pfnCPS( pCPSUISample->hParent,
  182. CPSFUNC_INSERT_PSUIPAGE,
  183. (LPARAM)0,
  184. (LPARAM)&InsPI
  185. );
  186. }
  187. break;
  188. }
  189. if ( (pCPSUISample->hCPSUI)
  190. || (pCPSUISample->hDocProp)
  191. )
  192. {
  193. return 1;
  194. }
  195. break;
  196. case PROPSHEETUI_REASON_GET_INFO_HEADER:
  197. if (pPSUIInfoHdr = (PPROPSHEETUI_INFO_HEADER)lParam)
  198. {
  199. pPSUIInfoHdr->pTitle = (LPTSTR)TitleName;
  200. switch (pPSUIInfo->lParamInit)
  201. {
  202. case IDM_DOCPROP:
  203. pPSUIInfoHdr->IconID = IDI_CPSUI_PRINTER2;
  204. break;
  205. case IDM_TVTEST:
  206. pPSUIInfoHdr->IconID = IDI_CPSUI_OPTION2;
  207. break;
  208. case IDM_DOCPROP_TVTEST:
  209. pPSUIInfoHdr->IconID = IDI_CPSUI_RUN_DIALOG;
  210. break;
  211. }
  212. pPSUIInfoHdr->Flags = PSUIHDRF_PROPTITLE | PSUIHDRF_NOAPPLYNOW;
  213. pPSUIInfoHdr->hWndParent = hWndApp;
  214. pPSUIInfoHdr->hInst = hInstApp;
  215. return 1;
  216. }
  217. break;
  218. case PROPSHEETUI_REASON_SET_RESULT:
  219. if ( (pCPSUISample = (PCPSUISAMPLE)pPSUIInfo->UserData)
  220. && (pCPSUISample->hCPSUI == ((PSETRESULT_INFO)lParam)->hSetResult)
  221. )
  222. {
  223. //
  224. // Save the result and propagate to its owner
  225. //
  226. pPSUIInfo->Result = ((PSETRESULT_INFO)lParam)->Result;
  227. return 1;
  228. }
  229. break;
  230. case PROPSHEETUI_REASON_DESTROY:
  231. if (pCPSUISample = (PCPSUISAMPLE)pPSUIInfo->UserData)
  232. {
  233. if (h = pCPSUISample->hDevMode)
  234. {
  235. GlobalUnlock(h);
  236. GlobalFree(h);
  237. }
  238. if (h = pCPSUISample->hDevNames)
  239. {
  240. GlobalUnlock(h);
  241. GlobalFree(h);
  242. }
  243. if (pCPSUISample->DPHdr.hPrinter)
  244. {
  245. ClosePrinter(pCPSUISample->DPHdr.hPrinter);
  246. }
  247. LocalFree((HLOCAL)pCPSUISample);
  248. pPSUIInfo->UserData = 0;
  249. }
  250. return 1;
  251. }
  252. return -1;
  253. }
  254. LRESULT
  255. CALLBACK
  256. MainWndProc
  257. (
  258. HWND hWnd,
  259. UINT Msg,
  260. WPARAM wParam,
  261. LPARAM lParam
  262. )
  263. /*++
  264. Routine Description:
  265. This is the main window procedure to the testing program
  266. Arguments:
  267. See SDK
  268. Return Value:
  269. See SDK
  270. --*/
  271. {
  272. LONG Result;
  273. LONG Ret;
  274. switch (Msg)
  275. {
  276. case WM_INITMENUPOPUP:
  277. if (!HIWORD(lParam))
  278. {
  279. CheckMenuItem( (HMENU)UIntToPtr((UINT)wParam),
  280. IDM_PERMISSION,
  281. MF_BYCOMMAND | ((UpdatePermission) ? MF_CHECKED : MF_UNCHECKED)
  282. );
  283. CheckMenuItem( (HMENU)UIntToPtr((UINT)wParam),
  284. IDM_USESTDABOUT,
  285. MF_BYCOMMAND | ((UseStdAbout) ? MF_CHECKED : MF_UNCHECKED)
  286. );
  287. }
  288. break;
  289. case WM_COMMAND:
  290. switch (wParam)
  291. {
  292. case IDM_USESTDABOUT:
  293. UseStdAbout = !UseStdAbout;
  294. break;
  295. case IDM_PERMISSION:
  296. UpdatePermission = !UpdatePermission;
  297. break;
  298. case IDM_DOCPROP:
  299. case IDM_TVTEST:
  300. case IDM_DOCPROP_TVTEST:
  301. Ret = CommonPropertySheetUI( hWnd,
  302. (PFNPROPSHEETUI)CPSUIFunc,
  303. (LPARAM)LOWORD(wParam),
  304. &Result
  305. );
  306. CPSUIDBG( DBG_WINMAINPROC,
  307. ("CommonPropertySheetUI()=%ld, Result=%ld", Ret, Result)
  308. );
  309. break;
  310. default:
  311. break;
  312. }
  313. break;
  314. case WM_DESTROY:
  315. PostQuitMessage(0);
  316. break;
  317. default:
  318. return (DefWindowProc(hWnd, Msg, wParam, lParam));
  319. }
  320. return 0L;
  321. }
  322. BOOL
  323. InitInstance
  324. (
  325. HANDLE hInstance,
  326. INT nCmdShow
  327. )
  328. /*++
  329. Routine Description:
  330. Saves instance handle and creates main window
  331. This function is called at initialization time for every instance of
  332. this application. This function performs initialization tasks that
  333. cannot be shared by multiple instances.
  334. In this case, we save the instance handle in a static variable and
  335. create and display the main program window.
  336. Arguments:
  337. hInstance - Current instance identifier
  338. nComShow - Param for first ShowWindow() call.
  339. Return Value:
  340. TRUE/FALSE
  341. --*/
  342. {
  343. //
  344. // Save the instance handle in static variable, which will be used in
  345. // many subsequence calls from this application to Windows.
  346. //
  347. hInstApp = hInstance;
  348. //
  349. // Create a main window for this application instance.
  350. //
  351. if (hWndApp = CreateWindow( ClassName,
  352. TitleName,
  353. WS_OVERLAPPEDWINDOW,
  354. CW_USEDEFAULT,
  355. CW_USEDEFAULT,
  356. CW_USEDEFAULT,
  357. CW_USEDEFAULT,
  358. NULL,
  359. NULL,
  360. hInstance,
  361. NULL
  362. )
  363. )
  364. {
  365. //
  366. // Make the window visible; update its client area;
  367. // and send WM_PAINT message
  368. //
  369. ShowWindow(hWndApp, nCmdShow);
  370. UpdateWindow(hWndApp);
  371. }
  372. return ((hWndApp) ? TRUE : FALSE);
  373. }
  374. BOOL
  375. InitApplication
  376. (
  377. HANDLE hInstance
  378. )
  379. /*++
  380. Routine Description:
  381. Initializes window data and registers window class
  382. This function is called at initialization time only if no other
  383. instances of the application are running. This function performs
  384. initialization tasks that can be done once for any number of running
  385. instances.
  386. In this case, we initialize a window class by filling out a data
  387. structure of type WNDCLASS and calling the Windows RegisterClass()
  388. function. Since all instances of this application use the same window
  389. class, we only need to do this when the first instance is initialized.
  390. Arguments:
  391. hInstance - current instance
  392. Return Value:
  393. BOOLEAN
  394. --*/
  395. {
  396. WNDCLASS wc;
  397. //
  398. // Fill in window class structure with parameters that describe the
  399. // main window.
  400. //
  401. wc.style = 0L;
  402. wc.lpfnWndProc = MainWndProc;
  403. wc.cbClsExtra = 0;
  404. wc.cbWndExtra = 0;
  405. wc.hInstance = hInstance;
  406. wc.hIcon = LoadIcon(hInstance, (LPCTSTR)IDI_CPSUISAMPLE);
  407. wc.hCursor = LoadCursor(NULL, IDC_ARROW);
  408. wc.hbrBackground = GetStockObject(WHITE_BRUSH);
  409. wc.lpszMenuName = MenuName;
  410. wc.lpszClassName = ClassName;
  411. //
  412. // Register the window class and return success/failure code.
  413. //
  414. return RegisterClass(&wc);
  415. }
  416. INT
  417. APIENTRY
  418. WinMain
  419. (
  420. HINSTANCE hInstance,
  421. HINSTANCE hPrevInstance,
  422. LPSTR lpCmdLine,
  423. INT nCmdShow
  424. )
  425. /*++
  426. Routine Description:
  427. calls initialization function, processes message loop
  428. Windows recognizes this function by name as the initial entry point
  429. for the program. This function calls the application initialization
  430. routine, if no other instance of the program is running, and always
  431. calls the instance initialization routine. It then executes a message
  432. retrieval and dispatch loop that is the top-level control structure
  433. for the remainder of execution. The loop is terminated when a WM_QUIT
  434. message is received, at which time this function exits the application
  435. instance by returning the value passed by PostQuitMessage().
  436. If this function must abort before entering the message loop, it
  437. returns the conventional value NULL.
  438. Arguments:
  439. Return Value:
  440. Integer
  441. --*/
  442. {
  443. MSG Msg;
  444. UNREFERENCED_PARAMETER(lpCmdLine);
  445. //
  446. // Other instances of app running?
  447. //
  448. if (!hPrevInstance)
  449. {
  450. if (!InitApplication(hInstance))
  451. {
  452. //
  453. // Initialize shared things, Exits if unable to initialize
  454. //
  455. return FALSE;
  456. }
  457. }
  458. //
  459. // Perform initializations that apply to a specific instance
  460. //
  461. if (!InitInstance(hInstance, nCmdShow))
  462. {
  463. return FALSE;
  464. }
  465. //
  466. // Acquire and dispatch messages until a WM_QUIT message is received.
  467. //
  468. while (GetMessage(&Msg, NULL, 0L, 0L))
  469. {
  470. //
  471. // Translates virtual key codes and Dispatches message to window
  472. //
  473. TranslateMessage(&Msg);
  474. DispatchMessage(&Msg);
  475. }
  476. //
  477. // Returns the value from PostQuitMessage
  478. //
  479. return((INT)Msg.wParam);
  480. }