Source code of Windows XP (NT5)
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.

338 lines
12 KiB

  1. /***************************************************************************/
  2. /** Microsoft Windows **/
  3. /** Copyright(c) Microsoft Corp., 1993 **/
  4. /***************************************************************************/
  5. /****************************************************************************
  6. ICONLBOX.H
  7. Header file for IconListBox class (hard tabs at 4)
  8. May 93, JimH (combobox support added by VladS)
  9. IconListBox is constructed by specifying the application's hInst and
  10. the ID of the listbox control. All the properties of the listbox (font,
  11. single or muliple selection, etc.) are defined in the .RC file. It must
  12. include the LBS_OWNERDRAWFIXED style.
  13. Icons are really bitmaps or parts of bitmaps. They must all be the same
  14. size. If they are not 16 by 16 this size must be specified in the
  15. constructor. By default, bright green (RGB(0,255,0)) is the transparent
  16. color but this can be overridden for each individual icon.
  17. Construction of the IconListBox object should occur when the dialog is
  18. created. You cannot wait until WM_INITDIALOG time because you need to
  19. handle the WM_MEASUREITEM message which comes first by passing to the
  20. constructed IconListBox. Similarly, the IconListBox cannot be destructed
  21. during OK or Cancel handling because you will have to handle subsequent
  22. WM_DRAWITEM messages.
  23. Follow these steps to use IconListBox (assumes 16 by 16 icons)
  24. 1. Construct IconListBox(hInst, IDC_MYLISTBOX);
  25. 2. Handle WM_MEASUREITEM by calling
  26. mylistbox->SetHeight(hwndDlg, (MEASUREITESTRUCT FAR *)lParam);
  27. You MUST do this step even if you are using the default height of 16
  28. pixels.
  29. 3. During WM_INITDIALOG processing, register each possible icon by
  30. specifying your internal identifier for this icon, a bitmap id, and
  31. if necessary, the x and y offset in the bitmap of this icon.
  32. Don't load the bitmap yourself. Just pass the .RC file id.
  33. mylistbox->RegisterIcon(typeFission, IDB_MushroomCloud);
  34. Note that you can register several icons from the same .RC file
  35. bitmap. These may or may not have different x and y offsets but
  36. they must all have the same transparent color. There is no
  37. extra overhead associated with reusing bitmaps or parts of bitmaps.
  38. 4 Also during WM_INITDIALOG, add the listbox entries.
  39. mylistbox->AddString(typeColdFusion, "Pons & Fleishman");
  40. 5. Handle the WM_DRAWITEM message by calling
  41. if (wParam == IDC_MYLISTBOX)
  42. mylistbox->DrawItem((DRAWITEMSTRUCT FAR *)lParam);
  43. 6. When handling OK, do whatever listbox stuff you have to do.
  44. n = mylistbox->GetCurrentSelection();
  45. mylistbox->GetString(n, pbuffer);
  46. 7. Destruct IconListBox
  47. Inline wrappers for common listbox functions are at the end of this file.
  48. ****************************************************************************/
  49. #ifndef _ICONLBOX_H_
  50. #define _ICONLBOX_H_
  51. struct IconList {
  52. int nID; // ID icon was registered with
  53. int nResID; // .RC file resource ID
  54. int x, y; // offset within specified icon
  55. HBITMAP hbmSelected;
  56. HBITMAP hbmUnselected;
  57. };
  58. const int MAXICONS = 10; // max number that can be registered
  59. const int MAXTABS = 10; // max number of tabs in string
  60. const int MAXSTRINGLEN = MAX_PATH; // AddString and InsertString limit
  61. const int ICONSPACE = 3; // whitespace around Icons in listbox
  62. class IconListBox {
  63. public:
  64. IconListBox(HINSTANCE hInst, int nID,
  65. int iconWidth = 16, int iconHeight = 16);
  66. ~IconListBox();
  67. int AddString(int nIcon, const char far *string);
  68. void Clear();
  69. void DeleteString(int nIndex);
  70. virtual void DrawItem(LPDRAWITEMSTRUCT lpd);
  71. int FindString(const char far *string, int nIndexStart = -1) const;
  72. int FindStringExact(const char far *string, int nIndexStart = -1) const;
  73. int GetCount();
  74. int GetCurrentSelection(void) const;
  75. int GetIconID(int nIndex) const;
  76. BOOL GetSel(int nIndex);
  77. int GetSelCount() const;
  78. int GetSelItems(int cItems, int FAR *lpItems) const;
  79. int GetString(int nIndex, char far *string) const;
  80. int InsertString(int nIcon, const char far *string, int nIndex);
  81. void RegisterIcon(int nIconID, int nResID, int x=0, int y=0,
  82. COLORREF colTransparent = RGB(0, 255, 0));
  83. int SelectString(int nIndex, const char far *string);
  84. int SetCurrentSelection(int nIndex = -1) const;
  85. void SetHeight(HWND hwndDlg, LPMEASUREITEMSTRUCT lpm, int height=16);
  86. void SetRedraw(BOOL bRedraw = TRUE) const;
  87. void SetSel(int nIndex, BOOL bSelected = TRUE) const;
  88. void SetTabStops(int cTabs, const int *pTabs);
  89. protected:
  90. int SetItemData(int nIndex, int nIconID) const;
  91. int UpdateHorizontalExtent(int nIconID,const char *string);
  92. int _cIcons; // number of icons registered
  93. IconList _aIcons[MAXICONS]; // registered icons
  94. int _cTabs; // number of tabs registered
  95. int _aTabs[MAXTABS]; // registered tabs
  96. int _iconWidth, _iconHeight; // size of icons
  97. int _iCurrentMaxHorzExt; // Currently maximum horizontal extent
  98. COLORREF _colSel, _colSelText, _colUnsel, _colUnselText;
  99. HINSTANCE _hInst; // application's hInst
  100. int _nCtlID; // id of listbox control
  101. int _nTextOffset; // vertical DrawText offset
  102. BOOL _fCombo; // Dropdown combo box ?
  103. HWND _hwndDialog;
  104. HWND _hwndListBox;
  105. HBRUSH _hbrSelected; // background colours
  106. HBRUSH _hbrUnselected;
  107. };
  108. // AddString - returns index of new string, or LB_ERR or LB_ERRSPACE
  109. inline int IconListBox::AddString(int nIcon, const char far *string)
  110. {
  111. int nIndex = (int) ::SendDlgItemMessage(_hwndDialog, _nCtlID,
  112. _fCombo ? CB_ADDSTRING : LB_ADDSTRING,
  113. 0, (LPARAM) ((LPSTR) string));
  114. SetItemData(nIndex, nIcon);
  115. UpdateHorizontalExtent(nIcon,string);
  116. return nIndex;
  117. }
  118. // Clear - clears contents of listbox
  119. inline void IconListBox::Clear()
  120. {
  121. ::SendMessage(_hwndListBox,
  122. _fCombo ? CB_RESETCONTENT : LB_RESETCONTENT, 0, 0);
  123. _iCurrentMaxHorzExt = 0;
  124. }
  125. // DeleteString - removes a string specified by the index
  126. inline void IconListBox::DeleteString(int nIndex)
  127. {
  128. ::SendMessage(_hwndListBox,
  129. _fCombo ? CB_DELETESTRING : LB_DELETESTRING, nIndex, 0);
  130. // May be horizontal extent changed - recalculate again
  131. UpdateHorizontalExtent(0,NULL);
  132. }
  133. // FindString & FindStringExact
  134. //
  135. // These functions find a listbox entry that begins with the characters
  136. // specifed in string (FindString) or exactly matches string (FindStringExact)
  137. // They return LB_ERR if the string is not found. Otherwise, you can call
  138. // GetString on the returned index.
  139. //
  140. // nIndexStart defaults to -1 which means search from the beginning of the
  141. // listbox or combobox.
  142. inline int IconListBox::FindString(const char far *string, int nIndexStart) const
  143. {
  144. return (int) ::SendDlgItemMessage(_hwndDialog, _nCtlID,
  145. _fCombo ? CB_FINDSTRING : LB_FINDSTRING, nIndexStart, (LPARAM)string);
  146. }
  147. inline int IconListBox::FindStringExact(const char far *string, int nIndexStart) const
  148. {
  149. return (int) ::SendDlgItemMessage(_hwndDialog, _nCtlID,
  150. _fCombo ? CB_FINDSTRINGEXACT : LB_FINDSTRINGEXACT,
  151. nIndexStart, (LPARAM)string);
  152. }
  153. // GetCount - returns the current number of listbox entries
  154. inline int IconListBox::GetCount()
  155. {
  156. return (int) ::SendMessage(_hwndListBox,
  157. _fCombo ? CB_GETCOUNT : LB_GETCOUNT, 0, 0);
  158. }
  159. // GetCurrentSelection - returns index or LB_ERR if no selection.
  160. // This function is not useful for multi-select listboxen.
  161. inline int IconListBox::GetCurrentSelection() const
  162. {
  163. return (int) ::SendMessage(_hwndListBox,
  164. _fCombo ? CB_GETCURSEL : LB_GETCURSEL, 0, 0);
  165. }
  166. // GetItemData - retrieve ICON id
  167. inline int IconListBox::GetIconID(int nIndex) const
  168. {
  169. return (int) ::SendMessage(_hwndListBox,
  170. _fCombo ? CB_GETITEMDATA : LB_GETITEMDATA, nIndex, 0);
  171. }
  172. // GetSel - returns nonzero if nIndex is selected
  173. inline BOOL IconListBox::GetSel(int nIndex)
  174. {
  175. return (BOOL) ::SendMessage(_hwndListBox, LB_GETSEL, nIndex, 0);
  176. }
  177. // GetSelCount - returns number of selected entries in multi-select listbox
  178. inline int IconListBox::GetSelCount() const
  179. {
  180. return (int) ::SendMessage(_hwndListBox, LB_GETSELCOUNT, 0, 0);
  181. }
  182. // GetSelItems - places index of each selected item in array. Returns
  183. // LB_ERR if not multi-select listbox, otherwise number of items in array.
  184. inline int IconListBox::GetSelItems(int cItems, int FAR *lpItems) const
  185. {
  186. return (int)
  187. ::SendMessage(_hwndListBox, LB_GETSELITEMS, cItems, (LPARAM) lpItems);
  188. }
  189. // GetString - returns length of string returned or LB_ERR if nIndex invalid
  190. inline int IconListBox::GetString(int nIndex, char far *string) const
  191. {
  192. return (int) ::SendDlgItemMessage(_hwndDialog, _nCtlID,
  193. _fCombo ? CB_GETLBTEXT : LB_GETTEXT, nIndex,
  194. (LPARAM) ((LPSTR) string));
  195. }
  196. // InsertString - same returns as AddString
  197. inline int IconListBox::InsertString(int nIcon, const char far *string, int nIndex)
  198. {
  199. int nNewIndex = (int) ::SendDlgItemMessage(_hwndDialog, _nCtlID,
  200. _fCombo ? CB_INSERTSTRING : LB_INSERTSTRING,
  201. (WPARAM) nIndex, (LPARAM) ((LPSTR) string));
  202. SetItemData(nNewIndex, nIcon);
  203. UpdateHorizontalExtent(nIcon,string);
  204. return(nNewIndex);
  205. }
  206. // SelectString
  207. // nIndex specifies where to start searching (-1 means from top).
  208. // string specifies the initial characters of the string to match.
  209. // Function returns index or LB_ERR if string not found
  210. inline int IconListBox::SelectString(int nIndex, const char far *string)
  211. {
  212. return (int) ::SendMessage(_hwndListBox,
  213. _fCombo ? CB_SELECTSTRING : LB_SELECTSTRING,
  214. nIndex,(LRESULT)string);
  215. }
  216. // SetCurrentSelection -
  217. // This function sets the current selection in a single-select style
  218. // listbox. It returns LB_ERR if an error occurs, or if nIndex is -1
  219. // (the default) meaning no current selection.
  220. inline int IconListBox::SetCurrentSelection(int nIndex) const
  221. {
  222. return (int) ::SendMessage(_hwndListBox,
  223. _fCombo ? CB_SETCURSEL : LB_SETCURSEL, nIndex, 0);
  224. }
  225. // SetItemData - used to store icon id, returns LB_ERR if error occurs
  226. // Use GetIconID to retrieve this id later.
  227. inline int IconListBox::SetItemData(int nIndex, int nData) const
  228. {
  229. return (int) ::SendMessage(_hwndListBox,
  230. _fCombo ? CB_SETITEMDATA : LB_SETITEMDATA,
  231. nIndex, nData);
  232. }
  233. // SetRedraw - turn on (TRUE) or off (FALSE) visual updates
  234. inline void IconListBox::SetRedraw(BOOL bRedraw) const
  235. {
  236. ::SendMessage(_hwndListBox, WM_SETREDRAW, bRedraw, 0);
  237. }
  238. // SetSel - used in multiselect listboxen. TRUE selects, FALSE deselects.
  239. // bSelected defaults to TRUE. nIndex == -1 means select all.
  240. inline void IconListBox::SetSel(int nIndex, BOOL bSelected) const
  241. {
  242. ::SendMessage(_hwndListBox, LB_SETSEL, bSelected, nIndex);
  243. }
  244. #endif // _ICONLBOX_H_