| 1 | ///////////////////////////////////////////////////////////////////////////// |
| 2 | // Name: wx/odcombo.h |
| 3 | // Purpose: wxOwnerDrawnComboBox and wxVListBoxPopup |
| 4 | // Author: Jaakko Salli |
| 5 | // Modified by: |
| 6 | // Created: Apr-30-2006 |
| 7 | // RCS-ID: $Id$ |
| 8 | // Copyright: (c) Jaakko Salli |
| 9 | // Licence: wxWindows licence |
| 10 | ///////////////////////////////////////////////////////////////////////////// |
| 11 | |
| 12 | #ifndef _WX_ODCOMBO_H_ |
| 13 | #define _WX_ODCOMBO_H_ |
| 14 | |
| 15 | #include "wx/defs.h" |
| 16 | |
| 17 | #if wxUSE_ODCOMBOBOX |
| 18 | |
| 19 | #include "wx/combo.h" |
| 20 | #include "wx/ctrlsub.h" |
| 21 | #include "wx/vlbox.h" |
| 22 | |
| 23 | |
| 24 | // |
| 25 | // New window styles for wxOwnerDrawnComboBox |
| 26 | // |
| 27 | enum |
| 28 | { |
| 29 | // Double-clicking cycles item if wxCB_READONLY is also used. |
| 30 | wxODCB_DCLICK_CYCLES = wxCC_SPECIAL_DCLICK, |
| 31 | |
| 32 | // If used, control itself is not custom paint using callback. |
| 33 | // Even if this is not used, writable combo is never custom paint |
| 34 | // until SetCustomPaintWidth is called |
| 35 | wxODCB_STD_CONTROL_PAINT = 0x1000 |
| 36 | }; |
| 37 | |
| 38 | |
| 39 | // |
| 40 | // Callback flags |
| 41 | // |
| 42 | enum |
| 43 | { |
| 44 | // when set, we are painting the selected item in control, |
| 45 | // not in the popup |
| 46 | wxCP_PAINTING_CONTROL = 0x0001 |
| 47 | }; |
| 48 | |
| 49 | |
| 50 | // ---------------------------------------------------------------------------- |
| 51 | // wxVListBoxComboPopup is a wxVListBox customized to act as a popup control. |
| 52 | // |
| 53 | // Notes: |
| 54 | // wxOwnerDrawnComboBox uses this as its popup. However, it always derives |
| 55 | // from native wxComboCtrl. If you need to use this popup with |
| 56 | // wxGenericComboControl, then remember that vast majority of item manipulation |
| 57 | // functionality is implemented in the wxVListBoxComboPopup class itself. |
| 58 | // |
| 59 | // ---------------------------------------------------------------------------- |
| 60 | |
| 61 | |
| 62 | class WXDLLIMPEXP_ADV wxVListBoxComboPopup : public wxVListBox, |
| 63 | public wxComboPopup |
| 64 | { |
| 65 | friend class wxOwnerDrawnComboBox; |
| 66 | public: |
| 67 | |
| 68 | // init and dtor |
| 69 | wxVListBoxComboPopup() : wxVListBox(), wxComboPopup() { } |
| 70 | virtual ~wxVListBoxComboPopup(); |
| 71 | |
| 72 | // required virtuals |
| 73 | virtual void Init(); |
| 74 | virtual bool Create(wxWindow* parent); |
| 75 | virtual wxWindow *GetControl() { return this; } |
| 76 | virtual void SetStringValue( const wxString& value ); |
| 77 | virtual wxString GetStringValue() const; |
| 78 | |
| 79 | // more customization |
| 80 | virtual void OnPopup(); |
| 81 | virtual wxSize GetAdjustedSize( int minWidth, int prefHeight, int maxHeight ); |
| 82 | virtual void PaintComboControl( wxDC& dc, const wxRect& rect ); |
| 83 | virtual void OnComboKeyEvent( wxKeyEvent& event ); |
| 84 | virtual void OnComboDoubleClick(); |
| 85 | virtual bool LazyCreate(); |
| 86 | |
| 87 | // Callbacks for drawing and measuring items. Override in a derived class for |
| 88 | // owner-drawnness. |
| 89 | // item: item index to be drawn, may be wxNOT_FOUND when painting combo control itself |
| 90 | // and there is no valid selection |
| 91 | // flags: wxCP_PAINTING_CONTROL is set if painting to combo control instead of list |
| 92 | virtual void OnDrawItem( wxDC& dc, const wxRect& rect, int item, int flags ) const; |
| 93 | |
| 94 | // Return item width, or -1 for calculating from text extent (default) |
| 95 | virtual wxCoord OnMeasureItemWidth( size_t item ) const; |
| 96 | |
| 97 | |
| 98 | // Item management |
| 99 | void SetSelection( int item ); |
| 100 | void Insert( const wxString& item, int pos ); |
| 101 | int Append(const wxString& item); |
| 102 | void Clear(); |
| 103 | void Delete( unsigned int item ); |
| 104 | void SetItemClientData(unsigned int n, void* clientData, wxClientDataType clientDataItemsType); |
| 105 | void *GetItemClientData(unsigned int n) const; |
| 106 | void SetString( int item, const wxString& str ); |
| 107 | wxString GetString( int item ) const; |
| 108 | unsigned int GetCount() const; |
| 109 | int FindString(const wxString& s, bool bCase = false) const; |
| 110 | int GetSelection() const; |
| 111 | |
| 112 | //void Populate( int n, const wxString choices[] ); |
| 113 | void Populate( const wxArrayString& choices ); |
| 114 | void ClearClientDatas(); |
| 115 | |
| 116 | // helpers |
| 117 | int GetItemAtPosition( const wxPoint& pos ) { return HitTest(pos); } |
| 118 | wxCoord GetTotalHeight() const { return EstimateTotalHeight(); } |
| 119 | wxCoord GetLineHeight(int line) const { return OnGetLineHeight(line); } |
| 120 | |
| 121 | protected: |
| 122 | |
| 123 | // Called by OnComboDoubleClick and OnComboKeyEvent |
| 124 | bool HandleKey( int keycode, bool saturate ); |
| 125 | |
| 126 | // sends combobox select event from the parent combo control |
| 127 | void SendComboBoxEvent( int selection ); |
| 128 | |
| 129 | // gets value, sends event and dismisses |
| 130 | void DismissWithEvent(); |
| 131 | |
| 132 | // Re-calculates width for given item |
| 133 | void CheckWidth( int pos ); |
| 134 | |
| 135 | // wxVListBox implementation |
| 136 | virtual void OnDrawItem(wxDC& dc, const wxRect& rect, size_t n) const; |
| 137 | //virtual wxCoord OnMeasureItem(size_t n) const; |
| 138 | void OnDrawBackground(wxDC& dc, const wxRect& rect, size_t n) const; |
| 139 | |
| 140 | // Return item height |
| 141 | virtual wxCoord OnMeasureItem( size_t item ) const; |
| 142 | |
| 143 | // filter mouse move events happening outside the list box |
| 144 | // move selection with cursor |
| 145 | void OnMouseMove(wxMouseEvent& event); |
| 146 | void OnMouseWheel(wxMouseEvent& event); |
| 147 | void OnKey(wxKeyEvent& event); |
| 148 | void OnLeftClick(wxMouseEvent& event); |
| 149 | |
| 150 | wxArrayString m_strings; |
| 151 | wxArrayPtrVoid m_clientDatas; |
| 152 | wxArrayInt m_widths; // cached line widths |
| 153 | |
| 154 | wxFont m_useFont; |
| 155 | |
| 156 | //wxString m_stringValue; // displayed text (may be different than m_strings[m_value]) |
| 157 | int m_value; // selection |
| 158 | int m_itemHover; // on which item the cursor is |
| 159 | int m_widestWidth; // width of widest item thus far |
| 160 | int m_avgCharWidth; |
| 161 | int m_baseImageWidth; // how much per item drawn in addition to text |
| 162 | int m_itemHeight; // default item height (calculate from font size |
| 163 | // and used in the absence of callback) |
| 164 | wxClientDataType m_clientDataItemsType; |
| 165 | |
| 166 | private: |
| 167 | |
| 168 | // has the mouse been released on this control? |
| 169 | bool m_clicked; |
| 170 | |
| 171 | DECLARE_EVENT_TABLE() |
| 172 | }; |
| 173 | |
| 174 | |
| 175 | // ---------------------------------------------------------------------------- |
| 176 | // wxOwnerDrawnComboBox: a generic wxComboBox that allows custom paint items |
| 177 | // in addition to many other types of customization already allowed by |
| 178 | // the wxComboCtrl. |
| 179 | // ---------------------------------------------------------------------------- |
| 180 | |
| 181 | class WXDLLIMPEXP_ADV wxOwnerDrawnComboBox : public wxComboCtrl, |
| 182 | public wxItemContainer |
| 183 | { |
| 184 | friend class wxComboPopupWindow; |
| 185 | friend class wxComboCtrlBase; |
| 186 | public: |
| 187 | |
| 188 | // ctors and such |
| 189 | wxOwnerDrawnComboBox() : wxComboCtrl() { Init(); } |
| 190 | |
| 191 | wxOwnerDrawnComboBox(wxWindow *parent, |
| 192 | wxWindowID id, |
| 193 | const wxString& value, |
| 194 | const wxPoint& pos, |
| 195 | const wxSize& size, |
| 196 | int n, |
| 197 | const wxString choices[], |
| 198 | long style = 0, |
| 199 | const wxValidator& validator = wxDefaultValidator, |
| 200 | const wxString& name = wxComboBoxNameStr) |
| 201 | : wxComboCtrl() |
| 202 | { |
| 203 | Init(); |
| 204 | |
| 205 | (void)Create(parent, id, value, pos, size, n, |
| 206 | choices, style, validator, name); |
| 207 | } |
| 208 | |
| 209 | bool Create(wxWindow *parent, |
| 210 | wxWindowID id, |
| 211 | const wxString& value = wxEmptyString, |
| 212 | const wxPoint& pos = wxDefaultPosition, |
| 213 | const wxSize& size = wxDefaultSize, |
| 214 | long style = 0, |
| 215 | const wxValidator& validator = wxDefaultValidator, |
| 216 | const wxString& name = wxComboBoxNameStr); |
| 217 | |
| 218 | wxOwnerDrawnComboBox(wxWindow *parent, |
| 219 | wxWindowID id, |
| 220 | const wxString& value, |
| 221 | const wxPoint& pos, |
| 222 | const wxSize& size, |
| 223 | const wxArrayString& choices, |
| 224 | long style, |
| 225 | const wxValidator& validator = wxDefaultValidator, |
| 226 | const wxString& name = wxComboBoxNameStr); |
| 227 | |
| 228 | bool Create(wxWindow *parent, |
| 229 | wxWindowID id, |
| 230 | const wxString& value, |
| 231 | const wxPoint& pos, |
| 232 | const wxSize& size, |
| 233 | int n, |
| 234 | const wxString choices[], |
| 235 | long style = 0, |
| 236 | const wxValidator& validator = wxDefaultValidator, |
| 237 | const wxString& name = wxComboBoxNameStr); |
| 238 | |
| 239 | bool Create(wxWindow *parent, |
| 240 | wxWindowID id, |
| 241 | const wxString& value, |
| 242 | const wxPoint& pos, |
| 243 | const wxSize& size, |
| 244 | const wxArrayString& choices, |
| 245 | long style = 0, |
| 246 | const wxValidator& validator = wxDefaultValidator, |
| 247 | const wxString& name = wxComboBoxNameStr); |
| 248 | |
| 249 | virtual ~wxOwnerDrawnComboBox(); |
| 250 | |
| 251 | // NULL popup can be used to indicate default interface |
| 252 | virtual void SetPopupControl( wxComboPopup* popup ); |
| 253 | |
| 254 | // wxControlWithItems methods |
| 255 | virtual void Clear(); |
| 256 | virtual void Delete(unsigned int n); |
| 257 | virtual unsigned int GetCount() const; |
| 258 | virtual wxString GetString(unsigned int n) const; |
| 259 | virtual void SetString(unsigned int n, const wxString& s); |
| 260 | virtual int FindString(const wxString& s, bool bCase = false) const; |
| 261 | virtual void Select(int n); |
| 262 | virtual int GetSelection() const; |
| 263 | virtual void SetSelection(int n) { Select(n); } |
| 264 | |
| 265 | |
| 266 | // Prevent a method from being hidden |
| 267 | virtual void SetSelection(long from, long to) |
| 268 | { |
| 269 | wxComboCtrl::SetSelection(from,to); |
| 270 | } |
| 271 | |
| 272 | wxCONTROL_ITEMCONTAINER_CLIENTDATAOBJECT_RECAST |
| 273 | |
| 274 | protected: |
| 275 | |
| 276 | // clears all allocated client datas |
| 277 | void ClearClientDatas(); |
| 278 | |
| 279 | virtual int DoAppend(const wxString& item); |
| 280 | virtual int DoInsert(const wxString& item, unsigned int pos); |
| 281 | virtual void DoSetItemClientData(unsigned int n, void* clientData); |
| 282 | virtual void* DoGetItemClientData(unsigned int n) const; |
| 283 | virtual void DoSetItemClientObject(unsigned int n, wxClientData* clientData); |
| 284 | virtual wxClientData* DoGetItemClientObject(unsigned int n) const; |
| 285 | |
| 286 | // overload m_popupInterface member so we can access specific popup interface easier |
| 287 | wxVListBoxComboPopup* m_popupInterface; |
| 288 | |
| 289 | // temporary storage for the initial choices |
| 290 | //const wxString* m_baseChoices; |
| 291 | //int m_baseChoicesCount; |
| 292 | wxArrayString m_initChs; |
| 293 | |
| 294 | private: |
| 295 | void Init(); |
| 296 | |
| 297 | DECLARE_EVENT_TABLE() |
| 298 | |
| 299 | DECLARE_DYNAMIC_CLASS(wxOwnerDrawnComboBox) |
| 300 | }; |
| 301 | |
| 302 | |
| 303 | #endif // wxUSE_ODCOMBOBOX |
| 304 | |
| 305 | #endif |
| 306 | // _WX_ODCOMBO_H_ |