listbox.h

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

Generated on Wed May 31 15:26:46 2006 for TNTsdk by  doxygen 1.3.8-20040913