odcombo.h

Go to the documentation of this file.
00001 // $Id: odcombo.h 1496 2006-07-22 13:09:45Z alex $
00002 /* @@tag:xara-cn-tp@@ THIRD PARTY COPYRIGHT */
00003 // The following line makes normalize.pl skip type fixing
00004 /* SKIPFIXTYPES: START */
00005 
00007 // Name:        wx/odcombo.h
00008 // Purpose:     wxOwnerDrawnComboBox and wxVListBoxPopup
00009 // Author:      Jaakko Salli
00010 // Modified by:
00011 // Created:     Apr-30-2006
00012 // RCS-ID:      
00013 // Copyright:   (c) Jaakko Salli
00014 // Licence:     wxWindows licence
00016 
00017 #ifndef _WXXTRA_ODCOMBO_H_
00018 #define _WXXTRA_ODCOMBO_H_
00019 
00020 #include <wx/wx.h>
00021 
00022 #if wxUSE_ODCOMBOBOX
00023 #undef wxXTRA_ODCOMBOBOX
00024 #include <wx/odcombo.h>
00025 #else
00026 #define wxXTRA_ODCOMBOBOX 1
00027 
00028 #include <wx/ctrlsub.h>
00029 #include <wx/vlbox.h>
00030 #include "combo.h"
00031 
00032 
00033 //
00034 // New window styles for wxOwnerDrawnComboBox
00035 //
00036 enum
00037 {
00038     // Double-clicking cycles item if wxCB_READONLY is also used.
00039     wxODCB_DCLICK_CYCLES            = wxCC_SPECIAL_DCLICK,
00040 
00041     // If used, control itself is not custom paint using callback.
00042     // Even if this is not used, writable combo is never custom paint
00043     // until SetCustomPaintWidth is called
00044     wxODCB_STD_CONTROL_PAINT        = 0x1000
00045 };
00046 
00047 
00048 //
00049 // Callback flags (see wxOwnerDrawnComboBox::OnDrawItem)
00050 //
00051 enum
00052 {
00053     // when set, we are painting the selected item in control,
00054     // not in the popup
00055     wxODCB_PAINTING_CONTROL         = 0x0001
00056 };
00057 
00058 
00059 // ----------------------------------------------------------------------------
00060 // wxVListBoxComboPopup is a wxVListBox customized to act as a popup control.
00061 //
00062 // Notes:
00063 //   wxOwnerDrawnComboBox uses this as its popup. However, it always derives
00064 //   from native wxComboCtrl. If you need to use this popup with
00065 //   wxGenericComboControl, then remember that vast majority of item manipulation
00066 //   functionality is implemented in the wxVListBoxComboPopup class itself.
00067 //
00068 // ----------------------------------------------------------------------------
00069 
00070 
00071 class WXDLLIMPEXP_ADV wxVListBoxComboPopup : public wxVListBox,
00072                                              public wxComboPopup
00073 {
00074     friend class wxOwnerDrawnComboBox;
00075 public:
00076 
00077     // init and dtor
00078     wxVListBoxComboPopup() : wxVListBox(), wxComboPopup() { }
00079     virtual ~wxVListBoxComboPopup();
00080 
00081     // required virtuals
00082     virtual void Init();
00083     virtual bool Create(wxWindow* parent);
00084     virtual wxWindow *GetControl() { return this; }
00085     virtual void SetStringValue( const wxString& value );
00086     virtual wxString GetStringValue() const;
00087 
00088     // more customization
00089     virtual void OnPopup();
00090     virtual wxSize GetAdjustedSize( int minWidth, int prefHeight, int maxHeight );
00091     virtual void PaintComboControl( wxDC& dc, const wxRect& rect );
00092     virtual void OnComboKeyEvent( wxKeyEvent& event );
00093     virtual void OnComboDoubleClick();
00094     virtual bool LazyCreate();
00095 
00096     // Item management
00097     void SetSelection( int item );
00098     void Insert( const wxString& item, int pos );
00099     int Append(const wxString& item);
00100     void Clear();
00101     void Delete( unsigned int item );
00102     void SetItemClientData(unsigned int n, void* clientData, wxClientDataType clientDataItemsType);
00103     void *GetItemClientData(unsigned int n) const;
00104     void SetString( int item, const wxString& str );
00105     wxString GetString( int item ) const;
00106     unsigned int GetCount() const;
00107     int FindString(const wxString& s, bool bCase = false) const;
00108     int GetSelection() const;
00109 
00110     //void Populate( int n, const wxString choices[] );
00111     void Populate( const wxArrayString& choices );
00112     void ClearClientDatas();
00113 
00114     // helpers
00115     int GetItemAtPosition( const wxPoint& pos ) { return HitTest(pos); }
00116     wxCoord GetTotalHeight() const { return EstimateTotalHeight(); }
00117     wxCoord GetLineHeight(int line) const { return OnGetLineHeight(line); }
00118 
00119 protected:
00120 
00121     // Called by OnComboDoubleClick and OnComboKeyEvent
00122     bool HandleKey( int keycode, bool saturate, wxChar unicode = 0 );
00123 
00124     // sends combobox select event from the parent combo control
00125     void SendComboBoxEvent( int selection );
00126 
00127     // gets value, sends event and dismisses
00128     void DismissWithEvent();
00129 
00130     // OnMeasureItemWidth will be called on next GetAdjustedSize.
00131     void ItemWidthChanged(unsigned int item)
00132     {
00133         m_widths[item] = -1;
00134         m_widthsDirty = true;
00135     }
00136 
00137     // Callbacks for drawing and measuring items. Override in a derived class for
00138     // owner-drawnness. Font, background and text colour have been prepared according
00139     // to selection, focus and such.
00140     //
00141     // item: item index to be drawn, may be wxNOT_FOUND when painting combo control itself
00142     //       and there is no valid selection
00143     // flags: wxODCB_PAINTING_CONTROL is set if painting to combo control instead of list
00144     // NOTE: If wxVListBoxComboPopup is used with wxComboCtrl class not derived from
00145     //       wxOwnerDrawnComboBox, this method must be overridden.
00146     virtual void OnDrawItem( wxDC& dc, const wxRect& rect, int item, int flags ) const;
00147 
00148     // This is same as in wxVListBox
00149     virtual wxCoord OnMeasureItem( size_t item ) const;
00150 
00151     // Return item width, or -1 for calculating from text extent (default)
00152     virtual wxCoord OnMeasureItemWidth( size_t item ) const;
00153 
00154     // Draw item and combo control background. Flags are same as with OnDrawItem.
00155     // NB: Can't use name OnDrawBackground because of virtual function hiding warnings.
00156     virtual void OnDrawBg(wxDC& dc, const wxRect& rect, int item, int flags) const;
00157 
00158     // Additional wxVListBox implementation (no need to override in derived classes)
00159     virtual void OnDrawItem(wxDC& dc, const wxRect& rect, size_t n) const;
00160     void OnDrawBackground(wxDC& dc, const wxRect& rect, size_t n) const;
00161 
00162     // filter mouse move events happening outside the list box
00163     // move selection with cursor
00164     void OnMouseMove(wxMouseEvent& event);
00165     void OnMouseWheel(wxMouseEvent& event);
00166     void OnKey(wxKeyEvent& event);
00167     void OnLeftClick(wxMouseEvent& event);
00168 
00169     // Return the widest item width (recalculating it if necessary)
00170     int GetWidestItemWidth() { CalcWidths(); return m_widestWidth; }
00171 
00172     // Return the index of the widest item (recalculating it if necessary)
00173     int GetWidestItem() { CalcWidths(); return m_widestItem; }
00174 
00175     // Stop partial completion (when some other event occurs)
00176     void StopPartialCompletion();
00177 
00178     wxArrayString           m_strings;
00179     wxArrayPtrVoid          m_clientDatas;
00180 
00181     wxFont                  m_useFont;
00182 
00183     //wxString                m_stringValue; // displayed text (may be different than m_strings[m_value])
00184     int                     m_value; // selection
00185     int                     m_itemHover; // on which item the cursor is
00186     int                     m_itemHeight; // default item height (calculate from font size
00187                                           // and used in the absence of callback)
00188     wxClientDataType        m_clientDataItemsType;
00189 
00190 private:
00191 
00192     // Cached item widths (in pixels).
00193     wxArrayInt              m_widths;
00194 
00195     // Width of currently widest item.
00196     int                     m_widestWidth;
00197 
00198     // Index of currently widest item.
00199     int                     m_widestItem;
00200 
00201     // Measure some items in next GetAdjustedSize?
00202     bool                    m_widthsDirty;
00203 
00204     // Find widest item in next GetAdjustedSize?
00205     bool                    m_findWidest;
00206 
00207     // has the mouse been released on this control?
00208     bool                    m_clicked;
00209 
00210     // Recalculate widths if they are dirty
00211     void CalcWidths();
00212 
00213     // Partial completion string
00214     wxString                m_partialCompletionString;
00215 
00216 #if wxUSE_TIMER
00217     // Partial completion timer
00218     wxTimer                 m_partialCompletionTimer;
00219 #endif // wxUSE_TIMER
00220 
00221     DECLARE_EVENT_TABLE()
00222 };
00223 
00224 
00225 // ----------------------------------------------------------------------------
00226 // wxOwnerDrawnComboBox: a generic wxComboBox that allows custom paint items
00227 // in addition to many other types of customization already allowed by
00228 // the wxComboCtrl.
00229 // ----------------------------------------------------------------------------
00230 
00231 class WXDLLIMPEXP_ADV wxOwnerDrawnComboBox : public wxComboCtrl,
00232                                              public wxItemContainer
00233 {
00234     //friend class wxComboPopupWindow;
00235     friend class wxVListBoxComboPopup;
00236 public:
00237 
00238     // ctors and such
00239     wxOwnerDrawnComboBox() : wxComboCtrl() { Init(); }
00240 
00241     wxOwnerDrawnComboBox(wxWindow *parent,
00242                          wxWindowID id,
00243                          const wxString& value,
00244                          const wxPoint& pos,
00245                          const wxSize& size,
00246                          int n,
00247                          const wxString choices[],
00248                          long style = 0,
00249                          const wxValidator& validator = wxDefaultValidator,
00250                          const wxString& name = wxComboBoxNameStr)
00251         : wxComboCtrl()
00252     {
00253         Init();
00254 
00255         (void)Create(parent, id, value, pos, size, n,
00256                      choices, style, validator, name);
00257     }
00258 
00259     bool Create(wxWindow *parent,
00260                 wxWindowID id,
00261                 const wxString& value = wxEmptyString,
00262                 const wxPoint& pos = wxDefaultPosition,
00263                 const wxSize& size = wxDefaultSize,
00264                 long style = 0,
00265                 const wxValidator& validator = wxDefaultValidator,
00266                 const wxString& name = wxComboBoxNameStr);
00267 
00268     wxOwnerDrawnComboBox(wxWindow *parent,
00269                          wxWindowID id,
00270                          const wxString& value,
00271                          const wxPoint& pos,
00272                          const wxSize& size,
00273                          const wxArrayString& choices,
00274                          long style,
00275                          const wxValidator& validator = wxDefaultValidator,
00276                          const wxString& name = wxComboBoxNameStr);
00277 
00278     bool Create(wxWindow *parent,
00279                 wxWindowID id,
00280                 const wxString& value,
00281                 const wxPoint& pos,
00282                 const wxSize& size,
00283                 int n,
00284                 const wxString choices[],
00285                 long style = 0,
00286                 const wxValidator& validator = wxDefaultValidator,
00287                 const wxString& name = wxComboBoxNameStr);
00288 
00289     bool Create(wxWindow *parent,
00290                 wxWindowID id,
00291                 const wxString& value,
00292                 const wxPoint& pos,
00293                 const wxSize& size,
00294                 const wxArrayString& choices,
00295                 long style = 0,
00296                 const wxValidator& validator = wxDefaultValidator,
00297                 const wxString& name = wxComboBoxNameStr);
00298 
00299     virtual ~wxOwnerDrawnComboBox();
00300 
00301     // Prevent app from using wxComboPopup
00302     void SetPopupControl(wxVListBoxComboPopup* popup)
00303     {
00304         DoSetPopupControl(popup);
00305     }
00306 
00307     // wxControlWithItems methods
00308     virtual void Clear();
00309     virtual void Delete(wxIndex n);
00310     virtual wxIndex GetCount() const;
00311     virtual wxString GetString(wxIndex n) const;
00312     virtual void SetString(wxIndex n, const wxString& s);
00313     virtual int FindString(const wxString& s, bool bCase /*= false*/) const;
00314     virtual int FindString(const wxString& s) const {return FindString (s, false);}
00315     virtual void Select(int n);
00316     virtual int GetSelection() const;
00317     virtual void SetSelection(int n) { Select(n); }
00318 
00319 
00320     // Prevent a method from being hidden
00321     virtual void SetSelection(long from, long to)
00322     {
00323         wxComboCtrl::SetSelection(from,to);
00324     }
00325 
00326     // Return the widest item width (recalculating it if necessary)
00327     virtual int GetWidestItemWidth() { EnsurePopupControl(); return GetVListBoxComboPopup()->GetWidestItemWidth(); }
00328 
00329     // Return the index of the widest item (recalculating it if necessary)
00330     virtual int GetWidestItem() { EnsurePopupControl(); return GetVListBoxComboPopup()->GetWidestItem(); }
00331 
00332     wxCONTROL_ITEMCONTAINER_CLIENTDATAOBJECT_RECAST
00333 
00334 protected:
00335 
00336     // Callback for drawing. Font, background and text colour have been
00337     // prepared according to selection, focus and such.
00338     // item: item index to be drawn, may be wxNOT_FOUND when painting combo control itself
00339     //       and there is no valid selection
00340     // flags: wxODCB_PAINTING_CONTROL is set if painting to combo control instead of list
00341     virtual void OnDrawItem( wxDC& dc, const wxRect& rect, int item, int flags ) const;
00342 
00343     // Callback for item height, or -1 for default
00344     virtual wxCoord OnMeasureItem( size_t item ) const;
00345 
00346     // Callback for item width, or -1 for default/undetermined
00347     virtual wxCoord OnMeasureItemWidth( size_t item ) const;
00348 
00349     // Callback for background drawing. Flags are same as with
00350     // OnDrawItem.
00351     virtual void OnDrawBackground( wxDC& dc, const wxRect& rect, int item, int flags ) const;
00352 
00353     // NULL popup can be used to indicate default interface
00354     virtual void DoSetPopupControl(wxComboPopup* popup);
00355 
00356     // clears all allocated client datas
00357     void ClearClientDatas();
00358 
00359     wxVListBoxComboPopup* GetVListBoxComboPopup() const
00360     {
00361         return (wxVListBoxComboPopup*) m_popupInterface;
00362     }
00363 
00364     virtual int DoAppend(const wxString& item);
00365     virtual int DoInsert(const wxString& item, wxIndex pos);
00366     virtual void DoSetItemClientData(wxIndex n, void* clientData);
00367     virtual void* DoGetItemClientData(wxIndex n) const;
00368     virtual void DoSetItemClientObject(wxIndex n, wxClientData* clientData);
00369     virtual wxClientData* DoGetItemClientObject(wxIndex n) const;
00370 
00371     // temporary storage for the initial choices
00372     //const wxString*         m_baseChoices;
00373     //int                     m_baseChoicesCount;
00374     wxArrayString           m_initChs;
00375 
00376 private:
00377     void Init();
00378 
00379     DECLARE_EVENT_TABLE()
00380 
00381     DECLARE_DYNAMIC_CLASS(wxOwnerDrawnComboBox)
00382 };
00383 
00384 #endif
00385 
00386 #endif
00387     // _WX_ODCOMBO_H_
00388 

Generated on Sat Nov 10 03:49:02 2007 for Camelot by  doxygen 1.4.4