home *** CD-ROM | disk | FTP | other *** search
/ QBasic & Borland Pascal & C / Delphi5.iso / C / BC_502 / LISTCTRL.PAK / LISTCTRL.C < prev    next >
Encoding:
C/C++ Source or Header  |  1997-05-06  |  9.4 KB  |  399 lines

  1. // THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF
  2. // ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO
  3. // THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
  4. // PARTICULAR PURPOSE.
  5. //
  6. // Copyright (C) 1993-1995  Microsoft Corporation.  All Rights Reserved.
  7. //
  8. //  MODULE:   listctrl.c
  9. //
  10. //  PURPOSE:   Implement the windows procedure for the main application
  11. //    windows.  Also implement the generic message and command dispatchers.
  12. //
  13. //  FUNCTIONS:
  14. //    WndProc      - Processes messages for the main window.
  15. //    MsgCreate    - Handle the WM_CREATE message 
  16. //    MsgSize      - Handle the WM_SIZE messages for the main window.
  17. //    MsgCommand   - Handle the WM_COMMAND messages for the main window.
  18. //    MsgSysColorChange - Pass the WM_SYSCOLORCHANGE message to the 
  19. //                        listview control
  20. //    MsgDestroy   - Handles the WM_DESTROY message by calling 
  21. //                   PostQuitMessage().
  22. //    CmdLargeIcons- Switch main view to large icons
  23. //    CmdSmallIcons- Switch main view to small icons
  24. //    CmdList      - Switch main view to list view
  25. //    CmdDetails   - Switch main view to details view
  26. //    CmdExit      - Handles the file exit command by calling destory 
  27. //                   window on the main window.
  28. //
  29. //  COMMENTS:
  30. //
  31.  
  32. #include <windows.h>            // required for all Windows applications
  33. #include <windowsx.h>
  34. #include <commctrl.h>
  35. #include "globals.h"            // prototypes specific to this application
  36. #include "listview.h"
  37. #include "resource.h"
  38.  
  39. // Main window message table definition.
  40. MSD rgmsd[] =
  41. {
  42.     {WM_CREATE,         MsgCreate        },
  43.     {WM_COMMAND,        MsgCommand       },
  44.     {WM_SIZE,           MsgSize          },
  45.     {WM_NOTIFY,         MsgNotify        },
  46.     {WM_SYSCOLORCHANGE, MsgSysColorChange},
  47.     {WM_DESTROY,        MsgDestroy       }
  48. };
  49.  
  50.  
  51. MSDI msdiMain =
  52. {
  53.     sizeof(rgmsd) / sizeof(MSD),
  54.     rgmsd,
  55.     edwpWindow
  56. };
  57.  
  58.  
  59. // Main window command table definition.
  60. CMD rgcmd[] =
  61. {
  62.     {IDM_EXIT,          CmdExit},
  63.     {IDM_LARGEICONS,    CmdLargeIcons},
  64.     {IDM_SMALLICONS,    CmdSmallIcons},
  65.     {IDM_LIST,          CmdList},
  66.     {IDM_DETAILS,       CmdDetails},
  67.     {IDM_EDITSTOCKINFO, CmdEditStockInfo},
  68.     {IDM_ABOUT,         CmdAbout}
  69. };
  70.  
  71. CMDI cmdiMain =
  72. {
  73.     sizeof(rgcmd) / sizeof(CMD),
  74.     rgcmd,
  75.     edwpWindow
  76. };
  77.  
  78.  
  79. //
  80. //  FUNCTION: WndProc(HWND, UINT, WPARAM, LPARAM)
  81. //
  82. //  PURPOSE:  Processes messages for the main window.
  83. //
  84. //  PARAMETERS:
  85. //    hwnd     - window handle
  86. //    uMessage - message number
  87. //    wparam   - additional information (dependant on message number)
  88. //    lparam   - additional information (dependant on message number)
  89. //
  90. //  RETURN VALUE:
  91. //    The return value depends on the message number.  If the message
  92. //    is implemented in the message dispatch table, the return value is
  93. //    the value returned by the message handling function.  Otherwise,
  94. //    the return value is the value returned by the default window procedure.
  95. //
  96. //  COMMENTS:
  97. //    Call the DispMessage() function with the main window's message dispatch
  98. //    information (msdiMain) and the message specific information.
  99. //
  100.  
  101. LRESULT CALLBACK WndProc(HWND   hwnd, 
  102.                          UINT   uMessage, 
  103.                          WPARAM wparam, 
  104.                          LPARAM lparam)
  105. {
  106.     return DispMessage(&msdiMain, hwnd, uMessage, wparam, lparam);
  107. }
  108.  
  109.  
  110. //
  111. //  FUNCTION: MsgCommand(HWND, UINT, WPARAM, LPARAM)
  112. //
  113. //  PURPOSE: Handle the WM_COMMAND messages for the main window.
  114. //
  115. //  PARAMETERS:
  116. //    hwnd     - window handle
  117. //    uMessage - WM_COMMAND (Unused)
  118. //    GET_WM_COMMAND_ID(wparam, lparam)   - Command identifier
  119. //    GET_WM_COMMAND_HWND(wparam, lparam) - Control handle
  120. //
  121. //  RETURN VALUE:
  122. //    The return value depends on the message number.  If the message
  123. //    is implemented in the message dispatch table, the return value is
  124. //    the value returned by the message handling function.  Otherwise,
  125. //    the return value is the value returned by the default window procedure.
  126. //
  127. //  COMMENTS:
  128. //    Call the DispCommand() function with the main window's command dispatch
  129. //    information (cmdiMain) and the command specific information.
  130. //
  131.  
  132. #pragma argsused
  133. LRESULT MsgCommand(HWND hwnd, UINT uMessage, WPARAM wparam, LPARAM lparam)
  134. {
  135.     return DispCommand(&cmdiMain, hwnd, wparam, lparam);
  136. }
  137.  
  138.  
  139. //
  140. //  FUNCTION: MsgCreate(HWND, UINT, WPARAM, LPARAM)
  141. //
  142. //  PURPOSE: Handle the WM_CREATE messages for the main window.
  143. //           Calls CreateListView() to create the listview for the main window
  144. //
  145. //  PARAMETERS:
  146. //    hwnd     - window handle
  147. //
  148. //  RETURN VALUE:
  149. //    Always return 0;
  150. //    
  151. //
  152. //  COMMENTS:
  153. //    
  154. //
  155. //
  156.  
  157. #pragma argsused
  158. LRESULT MsgCreate(HWND hwnd, UINT uMessage, WPARAM wparam, LPARAM lparam)
  159. {
  160. #pragma warn -aus
  161.      HWND hwndLV;
  162.  
  163.      if (NULL == (hwndLV = CreateListView(hwnd)))
  164.           MessageBox(hwnd,
  165.                          "Failed to create listview window.",
  166.                          SZAPPNAME,
  167.                          MB_OK);
  168.  
  169.      return 0;
  170. }
  171. #pragma warn .aus
  172.  
  173.  
  174.  
  175. //
  176. //  FUNCTION: MsgSize(HWND, UINT, WPARAM, LPARAM)
  177. //
  178. //  PURPOSE: Handle the WM_SIZE messages for the main window.
  179. //           
  180. //
  181. //  PARAMETERS:
  182. //    hwnd     - window handle
  183. //
  184. //  RETURN VALUE:
  185. //    Always return 0;
  186. //    
  187. //
  188. //  COMMENTS:
  189. //    
  190. //
  191. //
  192.  
  193. #pragma argsused
  194. LRESULT MsgSize(HWND hwnd, UINT uMessage, WPARAM wparam, LPARAM lparam)
  195. {
  196.      MoveWindow(GetDlgItem(hwnd, IDD_LISTVIEW),
  197.                     0, 0,
  198.                     LOWORD(lparam),
  199.                     HIWORD(lparam),
  200.                     FALSE);
  201.     return 0;    
  202. }
  203.  
  204.  
  205. //
  206. //  FUNCTION: MsgSysColorChange(HWND, UINT, WPARAM, LPARAM)
  207. //
  208. //  PURPOSE: Passed the WM_SYSCOLORCHANGE message to the listview control
  209. //
  210. //  PARAMETERS:
  211. //
  212. //    hwnd      - Window handle  
  213. //    uMessage  - Message number 
  214. //    wparam    - Extra data     
  215. //    lparam    - Extra data     
  216. //
  217. //  RETURN VALUE:
  218. //
  219. //    Call DefWindowProc() and return value from DWP
  220. //
  221. //  COMMENTS:
  222. //
  223. //
  224.  
  225. LRESULT MsgSysColorChange(HWND hwnd,
  226.                           UINT uMessage,
  227.                           WPARAM wparam,
  228.                           LPARAM lparam)
  229. {
  230.     PostMessage(GetDlgItem(hwnd, IDD_LISTVIEW), 
  231.                 WM_SYSCOLORCHANGE, 
  232.                 wparam, 
  233.                 lparam);
  234.     return DefWindowProc(hwnd, uMessage, wparam, lparam);
  235. }
  236.  
  237.  
  238. //
  239. //  FUNCTION: MsgDestroy(HWND, UINT, WPARAM, LPARAM)
  240. //
  241. //  PURPOSE: Calls PostQuitMessage().
  242. //
  243. //  PARAMETERS:
  244. //
  245. //    hwnd      - Window handle  (Unused)
  246. //    uMessage  - Message number (Unused)
  247. //    wparam    - Extra data     (Unused)
  248. //    lparam    - Extra data     (Unused)
  249. //
  250. //  RETURN VALUE:
  251. //
  252. //    Always returns 0 - Message handled
  253. //
  254. //  COMMENTS:
  255. //
  256. //
  257.  
  258. #pragma argsused
  259. LRESULT MsgDestroy(HWND hwnd, UINT uMessage, WPARAM wparam, LPARAM lparam)
  260. {
  261.     PostQuitMessage(0);
  262.     return 0;
  263. }
  264.  
  265.  
  266. //
  267. //  FUNCTION: CmdLargeIcons(HWND, WORD, WORD, HWND)
  268. //
  269. //  PURPOSE: 
  270. //
  271. //  PARAMETERS:
  272. //    hwnd      - Window handle
  273. //    wCommand  - IDM_LARGEICONS (unused)
  274. //    wNotify   - Notification number (unused)
  275. //    hwndCtrl  - NULL (unused)
  276. //
  277. //  RETURN VALUE:
  278. //
  279. //    Always returns 0 - Message handled
  280. //
  281. //  COMMENTS:
  282. //   
  283. //
  284.  
  285. #pragma argsused
  286. LRESULT CmdLargeIcons(HWND hwnd, WORD wCommand, WORD wNotify, HWND hwndCtrl)
  287. {
  288.     SwitchView(GetDlgItem(hwnd, IDD_LISTVIEW), LVS_ICON);
  289.     return 0;
  290. }
  291.  
  292.  
  293. //
  294. //  FUNCTION: CmdSmallIcons(HWND, WORD, WORD, HWND)
  295. //
  296. //  PURPOSE: 
  297. //
  298. //  PARAMETERS:
  299. //    hwnd      - Window handle
  300. //    wCommand  - IDM_SMALLICONS (unused)
  301. //    wNotify   - Notification number (unused)
  302. //    hwndCtrl  - NULL (unused)
  303. //
  304. //  RETURN VALUE:
  305. //
  306. //    Always returns 0 - Message handled
  307. //
  308. //  COMMENTS:
  309. //   
  310. //
  311.  
  312. #pragma argsused
  313. LRESULT CmdSmallIcons(HWND hwnd, WORD wCommand, WORD wNotify, HWND hwndCtrl)
  314. {
  315.     SwitchView(GetDlgItem(hwnd, IDD_LISTVIEW), LVS_SMALLICON);
  316.     return 0;
  317. }
  318.  
  319.  
  320. //
  321. //  FUNCTION: CmdList(HWND, WORD, WORD, HWND)
  322. //
  323. //  PURPOSE: 
  324. //
  325. //  PARAMETERS:
  326. //    hwnd      - Window handle
  327. //    wCommand  - IDM_LIST (unused)
  328. //    wNotify   - Notification number (unused)
  329. //    hwndCtrl  - NULL (unused)
  330. //
  331. //  RETURN VALUE:
  332. //
  333. //    Always returns 0 - Message handled
  334. //
  335. //  COMMENTS:
  336. //   
  337. //
  338.  
  339. #pragma argsused
  340. LRESULT CmdList(HWND hwnd, WORD wCommand, WORD wNotify, HWND hwndCtrl)
  341. {
  342.     SwitchView(GetDlgItem(hwnd, IDD_LISTVIEW), LVS_LIST);
  343.     return 0;
  344. }
  345.  
  346.  
  347. //
  348. //  FUNCTION: CmdDetails(HWND, WORD, WORD, HWND)
  349. //
  350. //  PURPOSE: 
  351. //
  352. //  PARAMETERS:
  353. //    hwnd      - Window handle
  354. //    wCommand  - IDM_DETAILS (unused)
  355. //    wNotify   - Notification number (unused)
  356. //    hwndCtrl  - NULL (unused)
  357. //
  358. //  RETURN VALUE:
  359. //
  360. //    Always returns 0 - Message handled
  361. //
  362. //  COMMENTS:
  363. //
  364. //
  365.  
  366. #pragma argsused
  367. LRESULT CmdDetails(HWND hwnd, WORD wCommand, WORD wNotify, HWND hwndCtrl)
  368. {
  369.     SwitchView(GetDlgItem(hwnd, IDD_LISTVIEW), LVS_REPORT);
  370.     return 0;
  371. }
  372.  
  373.  
  374. //
  375. //  FUNCTION: CmdExit(HWND, WORD, WORD, HWND)
  376. //
  377. //  PURPOSE: Exit the application.
  378. //
  379. //  PARAMETERS:
  380. //    hwnd     - The window.
  381. //    wCommand - IDM_EXIT (unused)
  382. //    wNotify  - Notification number (unused)
  383. //    hwndCtrl - NULL (unused)
  384. //
  385. //  RETURN VALUE:
  386. //    Always returns 0 - command handled.
  387. //
  388. //  COMMENTS:
  389. //
  390. //
  391.  
  392. #pragma argsused
  393. LRESULT CmdExit(HWND hwnd, WORD wCommand, WORD wNotify, HWND hwndCtrl)
  394. {
  395.     DestroyWindow(hwnd);
  396.     return 0;
  397. }
  398.  
  399.