00001 /** 00002 * \file mgui/listbox.h 00003 * \brief MGUI::CTRL_LISTBOX definitions 00004 * 00005 * \if NODOC 00006 * $Id: listbox.h_v 1.12 2004/11/10 01:46:12 dfriberg Exp $ 00007 * 00008 * $Log: listbox.h_v $ 00009 * Revision 1.12 2004/11/10 01:46:12 dfriberg 00010 * Initialize double-click function pointer in convenience class. 00011 * 00012 * Revision 1.11 2004/11/08 18:41:23 dwilliss 00013 * Added ExpandIfNeeded style 00014 * 00015 * Revision 1.10 2004/11/05 23:46:19 dwilliss 00016 * Added a STYLE value for ResizeIfPossible 00017 * Template version now lets you set OnDoubleClick 00018 * 00019 * Revision 1.9 2003/10/03 19:58:19 linux32build!build 00020 * Doxygen 00021 * 00022 * Revision 1.8 2003/09/15 13:49:32 fileserver!dwilliss 00023 * Doxygen 00024 * 00025 * Revision 1.7 2003/09/11 17:05:59 mju 00026 * Use cast in iniwrite to resolve ambiguity. 00027 * 00028 * Revision 1.6 2003/09/10 21:15:29 mju 00029 * Use ID for id. 00030 * 00031 * Revision 1.5 2003/09/10 16:42:23 mju 00032 * nc. 00033 * 00034 * Revision 1.4 2003/09/05 17:10:06 mju 00035 * AddItem using const MISTRING&. 00036 * 00037 * Revision 1.3 2003/04/09 20:51:31 dwilliss 00038 * Don't use _T in templates. Mac's ctype.h defines a global _T 00039 * 00040 * Revision 1.2 2003/01/16 21:35:22 mju 00041 * Use correct incl guard test. 00042 * 00043 * Revision 1.1 2003/01/16 21:12:50 mju 00044 * Initial revision 00045 * \endif 00046 **/ 00047 00048 #ifndef INC_MGUI_LISTBOX_H 00049 #define INC_MGUI_LISTBOX_H 00050 00051 #ifndef INC_MGUI_CTRL_H 00052 #include <mgui/ctrl.h> 00053 #endif 00054 00055 #ifndef INC_MGUI_LISTITEM_H 00056 #include <mgui/listitem.h> 00057 #endif 00058 00059 namespace MGUI { 00060 00061 #ifndef GENERATING_DOXYGEN_OUTPUT 00062 #ifdef X_NATIVE 00063 class XMLIST; 00064 #endif 00065 #endif // GENERATING_DOXYGEN_OUTPUT 00066 00067 //=================================================================================================================== 00068 //! Display simple list of items which user can view and select. 00069 //! In a single-selection list box, the user can select only one item. 00070 //! In a multiple-selection list box, a range of items can be selected. 00071 class CTRL_LISTBOX : public MGUI::CTRL { 00072 public: 00073 00074 //! Listbox Style used by Create(). 00075 enum STYLE { 00076 STYLE_Default = 0x00, 00077 STYLE_Sorted = 0x01, 00078 STYLE_MultiSelect = 0x02, //!< Multiple items selectable by simple toggle of each item 00079 STYLE_ExtendedSelect = 0x04, //!< Multiple items selectable by SHIFT/CTRL key and mouse. 00080 STYLE_ResizeIfPossible = 0x08, //!< Try to expand and shrink to fit widest item. (uses scrollbar if it can't resize) 00081 STYLE_ExpandIfNeeded = 0x10 //!< Try to expand fit widest item. (uses scrollbar if it can't resize) 00082 }; 00083 00084 //! Constructor. 00085 CTRL_LISTBOX ( 00086 ); 00087 00088 //! Destructor. 00089 virtual ~CTRL_LISTBOX ( 00090 ); 00091 00092 //! Add item to list box with MISTRING or TEXTID. 00093 //! If list box was created with STYLE_Sorted then the list will be resorted after the item 00094 //! is added, otherwise the item will be added to the end of the list. 00095 //! @return Zero-based index of item added or error < 0. 00096 int AddItem ( 00097 ID id, //!< ID to attach to item 00098 const MISTRING& string, //!< String to show in list 00099 bool resize = true //!< Attempt to resize if needed to fit new item string 00100 ); 00101 00102 //! Add item to list box with UNICODE* string. 00103 //! If list box was created with STYLE_Sorted then the list will be resorted after the item 00104 //! is added, otherwise the item will be added to the end of the list. 00105 //! @return Zero-based index of item added or error < 0. 00106 int AddItem ( 00107 ID id, //!< ID to attach to item 00108 const UNICODE *string, //!< String to show in list 00109 bool resize = true //!< Attempt to resize if needed to fit new item string 00110 ) { return (AddItem(id,MISTRING(string),resize)); } 00111 00112 //! Add item to list box with string from resource lookup 00113 //! If list box was created with STYLE_Sorted then the list will be resorted after the item 00114 //! is added, otherwise the item will be added to the end of the list. 00115 //! @return Zero-based index of item added or error < 0. 00116 int AddItem ( 00117 ID id, //!< ID to attach to item 00118 const char *string, //!< String for resource lookup 00119 bool resize = true //!< Attempt to resize if needed to fit new item string 00120 ); 00121 00122 //! Add multiple items. 00123 void AddItems ( 00124 const MGUI::LISTITEMDEF *items, //!< Items to add, terminated by item with NULL string 00125 bool resize = true //!< Attempt to resize if needed to fit largest item string 00126 ); 00127 00128 //! Create the list box control. 00129 void Create ( 00130 MGUI::LAYOUT_PANE_BASE& ParentPane, //!< Parent pane 00131 const MGUI::LISTITEMDEF *items = 0, //!< Initial item set, 0 for none 00132 int height = 5, //!< Height in characters 00133 int width = 0, //!< Width in 'typical' characters 00134 MGUI::LAYOUT_SIZEALIGN sizealign = MGUI::LAYOUT_SIZEALIGN_Expand, 00135 STYLE style = STYLE_Default 00136 ); 00137 00138 //! Delete all items from list box. 00139 void DeleteAllItems ( 00140 ); 00141 00142 //! Delete item from list box. 00143 void DeleteItem ( 00144 int index //!< Zero-based index of list box item to delete 00145 ); 00146 00147 //! Delete item from list box given ID. 00148 void DeleteItemID ( 00149 ID id //!< ID of item to delete 00150 ); 00151 00152 //! Deselect all items in multiple-selection list box. 00153 //! This method should not be used on single-selection list boxes. 00154 void DeselectAllItems ( 00155 ); 00156 00157 //! Retrieve number of items in the list box. 00158 int GetCount ( 00159 ) const; 00160 00161 #ifdef WIN32_MFC 00162 //! Get reference to MFC control (MFC only). 00163 CListBox& GetCtrl ( 00164 ) { return (m_ctrl); } 00165 #endif 00166 00167 //! Get index to item from ID. 00168 //! @return Zero-based index or -1 if no matching item found. 00169 int GetIndexFromID ( 00170 ID id 00171 ) const; 00172 00173 //! Get ID associated with list box item. 00174 //! @return ID specified when item was added. 00175 ID GetItemID ( 00176 int index //!< Zero-based index of list box item 00177 ) const; 00178 00179 //! Get text string associated with list box item. 00180 //! @return String containing text, will be empty if invalid item. 00181 MISTRING GetItemString ( 00182 int index //!< Zero-based index of list box item 00183 ) const; 00184 00185 //! Get number of selected items in multiple-selection list box. 00186 //! @return Number of selected items or 0 if none selected. 00187 int GetSelectedCount ( 00188 ) const; 00189 00190 //! Get currently selected item in single-selection list box. 00191 //! @return Zero-based index of selected item or -1 if none selected. 00192 int GetSelectedItem ( 00193 ) const; 00194 00195 //! Get ID of currently selected item in single-selection list box. 00196 //! @return ID or -1 if none selected. 00197 ID GetSelectedItemID ( 00198 ) const; 00199 00200 //! Retrieve indices of selected items in multiple-selection list box. 00201 ERRVALUE GetSelectedItems ( 00202 SIMPLE_ARRAY<int>& items 00203 ) const; 00204 00205 //! Retrieve zero-based index of first visible item in list box. 00206 int GetTopIndex ( 00207 ) const; 00208 00209 //! Read setting from INI file. 00210 //! If Create() has already been performed the control's state will be updated. 00211 void IniRead ( 00212 INIHANDLE IniHandle, //!< Handle to INI file to read from, 0 for default 00213 const char *IniGroup, //!< INI group to read from 00214 const char *IniField, //!< INI field to read from 00215 bool notify = true //!< Call OnSelection() if control has already been created 00216 ); 00217 00218 //! Write currently selected item ID to INI file. 00219 void IniWrite ( 00220 INIHANDLE IniHandle, //!< Handle to INI file to read from, 0 for default 00221 const char *IniGroup, //!< INI group to read from 00222 const char *IniField //!< INI field to read from 00223 ) const { ::IniWrite(IniHandle,IniGroup,IniField,static_cast<INT32>(GetSelectedItemID())); } 00224 00225 //! Insert item at specified position in list box. 00226 //! Unlike the AddItem method this does not cause the list to be resorted. 00227 //! @return Zero-based index of item added or error < 0. 00228 int InsertItem ( 00229 int index, //!< Zero-based index of position at which item should be inserted 00230 ID id, //!< ID to attach to item 00231 const MISTRING& string //!< String to show in list 00232 ); 00233 00234 //! Determine if specified item in list box is selected. 00235 bool IsItemSelected ( 00236 int index //!< Zero-based index of list box item 00237 ) const; 00238 00239 //! Select or deselect item in multiple-selection list box. 00240 //! This method should not be used on single-selection list boxes. 00241 void SetItemSelected ( 00242 int index, //!< Zero-based index of list box item. 00243 bool select = true //!< Specifies how to set the selection, true to select, false to deselect. 00244 ); 00245 00246 //! Select item in single-selection list box. 00247 //! If item is not visible the list box will be scrolled to make it visible. 00248 //! This method should not be used on multiple-selection list boxes. 00249 void SetSelectedItem ( 00250 int index //!< Zero-based index of list box item, -1 to have no item selected 00251 ); 00252 00253 //! Select item in list box using item ID. 00254 //! If item does not exist then no item will be selected. 00255 //! If item is not visible the list box will be scrolled to make it visible. 00256 void SetSelectedItemID ( 00257 ID id //!< ID of item to select 00258 ); 00259 00260 //! Scroll list box until item specified by index is at the top or maximum scroll range reached. 00261 void SetTopIndex ( 00262 int index //!< Zero-based index of list box item 00263 ); 00264 00265 protected: 00266 00267 //! Derived class must call corresponding base class method BEFORE performing its own processing. 00268 virtual void OnChangeSelection (); 00269 00270 //! User has double-clicked on item in list box. 00271 //! Derived class must call corresponding base class method BEFORE performing its own processing. 00272 virtual void OnDoubleClick (); 00273 00274 private: 00275 #ifndef GENERATING_DOXYGEN_OUTPUT 00276 00277 #ifdef WIN32_MFC 00278 class MyListBox : public CListBox { 00279 public: 00280 MyListBox ( 00281 MGUI::CTRL_LISTBOX& guictrl 00282 ): m_guictrl(guictrl) 00283 { } 00284 virtual ~MyListBox ( 00285 ) { } 00286 virtual BOOL PreCreateWindow (CREATESTRUCT& cs); 00287 protected: 00288 afx_msg void OnSelChange (); 00289 DECLARE_MESSAGE_MAP() 00290 private: 00291 MGUI::CTRL_LISTBOX& m_guictrl; 00292 }; 00293 friend class MyListBox; 00294 00295 MyListBox m_ctrl; 00296 #endif 00297 00298 #ifdef X_NATIVE 00299 XMLIST *m_pxmlist; 00300 static void CB_ChangeSelection (Widget, CTRL_LISTBOX*, void*); 00301 static void CB_DoubleClick (Widget, CTRL_LISTBOX*, void*); 00302 #endif 00303 00304 CTRL_LISTBOX (const CTRL_LISTBOX&); 00305 CTRL_LISTBOX& operator= (const CTRL_LISTBOX&); 00306 #endif // GENERATING_DOXYGEN_OUTPUT 00307 }; 00308 00309 DEFINE_ENUM_OPERATORS(MGUI::CTRL_LISTBOX::STYLE); 00310 00311 00312 //------------------------------------------------------------------------------------------------------------------- 00313 //! Convenience template for List box control to allow method in container class to be called. 00314 template <class _CT> class CTRL_LISTBOX_T : public MGUI::CTRL_LISTBOX { 00315 public: 00316 //! Constructor. 00317 CTRL_LISTBOX_T ( 00318 ): 00319 m_pContainer(0), 00320 m_pfOnChangeSelection(0), 00321 m_pfOnDoubleClick(0) 00322 { } 00323 00324 //! Create the list box control. 00325 void Create ( 00326 MGUI::LAYOUT_PANE_BASE& ParentPane, //!< Parent pane 00327 _CT *pContainer, //!< Pointer to callback container class 00328 void (_CT::*pfOnChangeSelection)(), //! Callback function pointer 00329 const MGUI::LISTITEMDEF *items = 0, //!< Initial item set, 0 for none 00330 int height = 5, //!< Height in characters 00331 int width = 0, //!< Width in 'typical' characters 00332 MGUI::LAYOUT_SIZEALIGN sizealign = MGUI::LAYOUT_SIZEALIGN_Expand, 00333 STYLE style = STYLE_Default 00334 ) { 00335 m_pContainer = pContainer; 00336 m_pfOnChangeSelection = pfOnChangeSelection; 00337 CTRL_LISTBOX::Create(ParentPane,items,height,width,sizealign,style); 00338 } 00339 00340 void SetFuncDoubleClick ( 00341 void (_CT::*pfOnDoubleClick)() 00342 ) { 00343 m_pfOnDoubleClick = pfOnDoubleClick; 00344 } 00345 00346 private: 00347 #ifndef GENERATING_DOXYGEN_OUTPUT 00348 _CT *m_pContainer; 00349 void (_CT::*m_pfOnChangeSelection)(); 00350 void (_CT::*m_pfOnDoubleClick)(); 00351 00352 virtual void OnChangeSelection ( 00353 ) { 00354 CTRL_LISTBOX::OnChangeSelection(); 00355 if (m_pContainer != 0 && m_pfOnChangeSelection != 0) (m_pContainer->*m_pfOnChangeSelection)(); 00356 return; 00357 } 00358 00359 virtual void OnDoubleClick ( 00360 ) { 00361 CTRL_LISTBOX::OnDoubleClick(); 00362 if (m_pContainer != 0 && m_pfOnDoubleClick != 0) (m_pContainer->*m_pfOnDoubleClick)(); 00363 return; 00364 } 00365 #endif // GENERATING_DOXYGEN_OUTPUT 00366 }; 00367 00368 //=================================================================================================================== 00369 00370 } // End namespace MGUI 00371 00372 #endif //INC_MGUI_LISTBOX_H
1.3.8-20040913