X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/a236aa2058ccf3d36e9cafc20fa7375080c4be50..11e3c6ef36393fb5863ea2f9601d8facd73acb12:/include/wx/ctrlsub.h?ds=sidebyside diff --git a/include/wx/ctrlsub.h b/include/wx/ctrlsub.h index 901f52c45d..a55e124148 100644 --- a/include/wx/ctrlsub.h +++ b/include/wx/ctrlsub.h @@ -30,7 +30,7 @@ // implements an extended interface deriving from this one) // ---------------------------------------------------------------------------- -class WXDLLEXPORT wxItemContainerImmutable +class WXDLLIMPEXP_CORE wxItemContainerImmutable { public: wxItemContainerImmutable() { } @@ -73,7 +73,7 @@ public: bool SetStringSelection(const wxString& s); // return the selected string or empty string if none - wxString GetStringSelection() const; + virtual wxString GetStringSelection() const; // this is the same as SetSelection( for single-selection controls but // reads better for multi-selection ones @@ -97,7 +97,7 @@ protected: // in this case DoInsertItem() needs to be overridden. // ---------------------------------------------------------------------------- -class WXDLLEXPORT wxItemContainer : public wxItemContainerImmutable +class WXDLLIMPEXP_CORE wxItemContainer : public wxItemContainerImmutable { private: // AppendItems() and InsertItems() helpers just call DoAppend/InsertItems() @@ -121,8 +121,8 @@ private: int AppendItems(const wxArrayStringsAdapter& items, void **clientData) { - wxASSERT_MSG( m_clientDataItemsType != wxClientData_Object, - _T("can't mix different types of client data") ); + wxASSERT_MSG( GetClientDataType() != wxClientData_Object, + wxT("can't mix different types of client data") ); return AppendItems(items, clientData, wxClientData_Void); } @@ -130,10 +130,10 @@ private: int AppendItems(const wxArrayStringsAdapter& items, wxClientData **clientData) { - wxASSERT_MSG( m_clientDataItemsType != wxClientData_Void, - _T("can't mix different types of client data") ); + wxASSERT_MSG( GetClientDataType() != wxClientData_Void, + wxT("can't mix different types of client data") ); - return AppendItems(items, wx_reinterpret_cast(void **, clientData), + return AppendItems(items, reinterpret_cast(clientData), wxClientData_Object); } @@ -142,17 +142,17 @@ private: void **clientData, wxClientDataType type) { - wxASSERT_MSG( !IsSorted(), _T("can't insert items in sorted control") ); + wxASSERT_MSG( !IsSorted(), wxT("can't insert items in sorted control") ); wxCHECK_MSG( pos <= GetCount(), wxNOT_FOUND, - _T("position out of range") ); + wxT("position out of range") ); // not all derived classes handle empty arrays correctly in // DoInsertItems() and besides it really doesn't make much sense to do // this (for append it could correspond to creating an initially empty // control but why would anybody need to insert 0 items?) wxCHECK_MSG( !items.IsEmpty(), wxNOT_FOUND, - _T("need something to insert") ); + wxT("need something to insert") ); return DoInsertItems(items, pos, clientData, type); } @@ -166,8 +166,8 @@ private: unsigned int pos, void **clientData) { - wxASSERT_MSG( m_clientDataItemsType != wxClientData_Object, - _T("can't mix different types of client data") ); + wxASSERT_MSG( GetClientDataType() != wxClientData_Object, + wxT("can't mix different types of client data") ); return InsertItems(items, pos, clientData, wxClientData_Void); } @@ -176,11 +176,11 @@ private: unsigned int pos, wxClientData **clientData) { - wxASSERT_MSG( m_clientDataItemsType != wxClientData_Void, - _T("can't mix different types of client data") ); + wxASSERT_MSG( GetClientDataType() != wxClientData_Void, + wxT("can't mix different types of client data") ); return InsertItems(items, pos, - wx_reinterpret_cast(void **, clientData), + reinterpret_cast(clientData), wxClientData_Object); } @@ -280,28 +280,47 @@ public: void Delete(unsigned int pos); + // various accessors + // ----------------- + + // The control may maintain its items in a sorted order in which case + // items are automatically inserted at the right position when they are + // inserted or appended. Derived classes have to override this method if + // they implement sorting, typically by returning HasFlag(wxXX_SORT) + virtual bool IsSorted() const { return false; } + + // client data stuff // ----------------- void SetClientData(unsigned int n, void* clientData); void* GetClientData(unsigned int n) const; + // SetClientObject() takes ownership of the pointer, GetClientObject() + // returns it but keeps the ownership while DetachClientObject() expects + // the caller to delete the pointer and also resets the internally stored + // one to NULL for this item void SetClientObject(unsigned int n, wxClientData* clientData); wxClientData* GetClientObject(unsigned int n) const; + wxClientData* DetachClientObject(unsigned int n); + + // return the type of client data stored in this control: usually it just + // returns m_clientDataItemsType but must be overridden in the controls + // which delegate their client data storage to another one (e.g. wxChoice + // in wxUniv which stores data in wxListBox which it uses anyhow); don't + // forget to override SetClientDataType() if you override this one + // + // NB: for this to work no code should ever access m_clientDataItemsType + // directly but only via this function! + virtual wxClientDataType GetClientDataType() const + { return m_clientDataItemsType; } bool HasClientData() const - { return m_clientDataItemsType != wxClientData_None; } + { return GetClientDataType() != wxClientData_None; } bool HasClientObjectData() const - { return m_clientDataItemsType == wxClientData_Object; } + { return GetClientDataType() == wxClientData_Object; } bool HasClientUntypedData() const - { return m_clientDataItemsType == wxClientData_Void; } - - - // The control may maintain its items in a sorted order in which case - // items are automatically inserted at the right position when they are - // inserted or appended. Derived classes have to override this method if - // they implement sorting, typically by returning HasFlag(wxXX_SORT) - virtual bool IsSorted() const { return false; } + { return GetClientDataType() == wxClientData_Void; } protected: // there is usually no need to override this method but you can do it if it @@ -364,50 +383,70 @@ protected: // set it to NULL (must only be called if HasClientObjectData()) void ResetItemClientObject(unsigned int n); + // set the type of the client data stored in this control: override this if + // you override GetClientDataType() + virtual void SetClientDataType(wxClientDataType clientDataItemsType) + { + m_clientDataItemsType = clientDataItemsType; + } +private: // the type of the client data for the items wxClientDataType m_clientDataItemsType; }; -// this macro must (unfortunately) be used in any class deriving from both -// wxItemContainer and wxControl because otherwise there is ambiguity when -// calling GetClientXXX() functions -- the compiler can't choose between the -// two versions -#define wxCONTROL_ITEMCONTAINER_CLIENTDATAOBJECT_RECAST \ - void SetClientData(void *data) \ - { wxEvtHandler::SetClientData(data); } \ - void *GetClientData() const \ - { return wxEvtHandler::GetClientData(); } \ - void SetClientObject(wxClientData *data) \ - { wxEvtHandler::SetClientObject(data); } \ - wxClientData *GetClientObject() const \ - { return wxEvtHandler::GetClientObject(); } \ - void SetClientData(unsigned int n, void* clientData) \ - { wxItemContainer::SetClientData(n, clientData); } \ - void* GetClientData(unsigned int n) const \ - { return wxItemContainer::GetClientData(n); } \ - void SetClientObject(unsigned int n, wxClientData* clientData) \ - { wxItemContainer::SetClientObject(n, clientData); } \ - wxClientData* GetClientObject(unsigned int n) const \ +// Inheriting directly from a wxWindow-derived class and wxItemContainer +// unfortunately introduces an ambiguity for all GetClientXXX() methods as they +// are inherited twice: the "global" versions from wxWindow and the per-item +// versions taking the index from wxItemContainer. +// +// So we need to explicitly resolve them and this helper template class is +// provided to do it. To use it, simply inherit from wxWindowWithItems instead of Window and Container interface directly. +template +class wxWindowWithItems : public W, public C +{ +public: + typedef W BaseWindowClass; + typedef C BaseContainerInterface; + + wxWindowWithItems() { } + + void SetClientData(void *data) + { BaseWindowClass::SetClientData(data); } + void *GetClientData() const + { return BaseWindowClass::GetClientData(); } + void SetClientObject(wxClientData *data) + { BaseWindowClass::SetClientObject(data); } + wxClientData *GetClientObject() const + { return BaseWindowClass::GetClientObject(); } + + void SetClientData(unsigned int n, void* clientData) + { wxItemContainer::SetClientData(n, clientData); } + void* GetClientData(unsigned int n) const + { return wxItemContainer::GetClientData(n); } + void SetClientObject(unsigned int n, wxClientData* clientData) + { wxItemContainer::SetClientObject(n, clientData); } + wxClientData* GetClientObject(unsigned int n) const { return wxItemContainer::GetClientObject(n); } +}; -class WXDLLEXPORT wxControlWithItemsBase : public wxControl, - public wxItemContainer +class WXDLLIMPEXP_CORE wxControlWithItemsBase : + public wxWindowWithItems { public: wxControlWithItemsBase() { } - // we have to redefine these functions here to avoid ambiguities in classes - // deriving from us which would arise otherwise because both base classses - // have the methods with the same names - hopefully, a smart compiler can - // optimize away these simple inline wrappers so we don't suffer much from - // this - wxCONTROL_ITEMCONTAINER_CLIENTDATAOBJECT_RECAST - // usually the controls like list/combo boxes have their own background // colour virtual bool ShouldInheritColours() const { return false; } + + // Implementation only from now on. + + // Generate an event of the given type for the selection change. + void SendSelectionChangedEvent(wxEventType eventType); + protected: // fill in the client object or data field of the event as appropriate // @@ -416,7 +455,7 @@ protected: void InitCommandEventWithItems(wxCommandEvent& event, int n); private: - DECLARE_NO_COPY_CLASS(wxControlWithItemsBase) + wxDECLARE_NO_COPY_CLASS(wxControlWithItemsBase); }; // define the platform-specific wxControlWithItems class @@ -425,14 +464,14 @@ private: #elif defined(__WXMOTIF__) #include "wx/motif/ctrlsub.h" #else - class wxControlWithItems : public wxControlWithItemsBase + class WXDLLIMPEXP_CORE wxControlWithItems : public wxControlWithItemsBase { public: wxControlWithItems() { } private: DECLARE_ABSTRACT_CLASS(wxControlWithItems) - DECLARE_NO_COPY_CLASS(wxControlWithItems) + wxDECLARE_NO_COPY_CLASS(wxControlWithItems); }; #endif