| 1 | /////////////////////////////////////////////////////////////////////////////// |
| 2 | // Name: wx/compositewin.h |
| 3 | // Purpose: wxCompositeWindow<> declaration |
| 4 | // Author: Vadim Zeitlin |
| 5 | // Created: 2011-01-02 |
| 6 | // RCS-ID: $Id$ |
| 7 | // Copyright: (c) 2011 Vadim Zeitlin <vadim@wxwidgets.org> |
| 8 | // Licence: wxWindows licence |
| 9 | /////////////////////////////////////////////////////////////////////////////// |
| 10 | |
| 11 | #ifndef _WX_COMPOSITEWIN_H_ |
| 12 | #define _WX_COMPOSITEWIN_H_ |
| 13 | |
| 14 | #include "wx/window.h" |
| 15 | |
| 16 | class WXDLLIMPEXP_FWD_CORE wxToolTip; |
| 17 | |
| 18 | // NB: This is an experimental and, as for now, undocumented class used only by |
| 19 | // wxWidgets itself internally. Don't use it in your code until its API is |
| 20 | // officially stabilized unless you are ready to change it with the next |
| 21 | // wxWidgets release. |
| 22 | |
| 23 | // FIXME-VC6: This compiler can't compile DoSetForAllParts() template function, |
| 24 | // it can't determine whether the deduced type should be "T" or "const T&". And |
| 25 | // without this function wxCompositeWindow is pretty useless so simply disable |
| 26 | // this code for it, this does mean that setting colours/fonts/... for |
| 27 | // composite controls won't work in the library compiled with it but so far |
| 28 | // this only affects the generic wxDatePickerCtrl which is not used by default |
| 29 | // under MSW anyhow so it doesn't seem to be worth it to spend time and uglify |
| 30 | // the code to fix it. |
| 31 | #ifndef __VISUALC6__ |
| 32 | |
| 33 | // ---------------------------------------------------------------------------- |
| 34 | // wxCompositeWindow is a helper for implementing composite windows: to define |
| 35 | // a class using subwindows, simply inherit from it specialized with the real |
| 36 | // base class name and implement GetCompositeWindowParts() pure virtual method. |
| 37 | // ---------------------------------------------------------------------------- |
| 38 | |
| 39 | // The template parameter W must be a wxWindow-derived class. |
| 40 | template <class W> |
| 41 | class wxCompositeWindow : public W |
| 42 | { |
| 43 | public: |
| 44 | typedef W BaseWindowClass; |
| 45 | |
| 46 | // Default ctor doesn't do anything. |
| 47 | wxCompositeWindow() { } |
| 48 | |
| 49 | // Override all wxWindow methods which must be forwarded to the composite |
| 50 | // window parts. |
| 51 | |
| 52 | // Attribute setters group. |
| 53 | // |
| 54 | // NB: Unfortunately we can't factor out the call for the setter itself |
| 55 | // into DoSetForAllParts() because we can't call the function passed to |
| 56 | // it non-virtually and we need to do this to avoid infinite recursion, |
| 57 | // so we work around this by calling the method of this object itself |
| 58 | // manually in each function. |
| 59 | virtual bool SetForegroundColour(const wxColour& colour) |
| 60 | { |
| 61 | if ( !BaseWindowClass::SetForegroundColour(colour) ) |
| 62 | return false; |
| 63 | |
| 64 | SetForAllParts(&wxWindowBase::SetForegroundColour, colour); |
| 65 | |
| 66 | return true; |
| 67 | } |
| 68 | |
| 69 | virtual bool SetBackgroundColour(const wxColour& colour) |
| 70 | { |
| 71 | if ( !BaseWindowClass::SetBackgroundColour(colour) ) |
| 72 | return false; |
| 73 | |
| 74 | SetForAllParts(&wxWindowBase::SetBackgroundColour, colour); |
| 75 | |
| 76 | return true; |
| 77 | } |
| 78 | |
| 79 | virtual bool SetFont(const wxFont& font) |
| 80 | { |
| 81 | if ( !BaseWindowClass::SetFont(font) ) |
| 82 | return false; |
| 83 | |
| 84 | SetForAllParts(&wxWindowBase::SetFont, font); |
| 85 | |
| 86 | return true; |
| 87 | } |
| 88 | |
| 89 | virtual bool SetCursor(const wxCursor& cursor) |
| 90 | { |
| 91 | if ( !BaseWindowClass::SetCursor(cursor) ) |
| 92 | return false; |
| 93 | |
| 94 | SetForAllParts(&wxWindowBase::SetCursor, cursor); |
| 95 | |
| 96 | return true; |
| 97 | } |
| 98 | |
| 99 | #if wxUSE_TOOLTIPS |
| 100 | virtual void DoSetToolTip(wxToolTip *tip) |
| 101 | { |
| 102 | BaseWindowClass::DoSetToolTip(tip); |
| 103 | |
| 104 | SetForAllParts(&wxWindowBase::CopyToolTip, tip); |
| 105 | } |
| 106 | #endif // wxUSE_TOOLTIPS |
| 107 | |
| 108 | private: |
| 109 | // Must be implemented by the derived class to return all children to which |
| 110 | // the public methods we override should forward to. |
| 111 | virtual wxWindowList GetCompositeWindowParts() const = 0; |
| 112 | |
| 113 | template <class T> |
| 114 | void SetForAllParts(bool (wxWindowBase::*func)(const T&), const T& arg) |
| 115 | { |
| 116 | DoSetForAllParts<const T&>(func, arg); |
| 117 | } |
| 118 | |
| 119 | template <class T> |
| 120 | void SetForAllParts(bool (wxWindowBase::*func)(T*), T* arg) |
| 121 | { |
| 122 | DoSetForAllParts<T*>(func, arg); |
| 123 | } |
| 124 | |
| 125 | template <class T> |
| 126 | void DoSetForAllParts(bool (wxWindowBase::*func)(T), T arg) |
| 127 | { |
| 128 | // Simply call the setters for all parts of this composite window. |
| 129 | const wxWindowList parts = GetCompositeWindowParts(); |
| 130 | for ( wxWindowList::const_iterator i = parts.begin(); |
| 131 | i != parts.end(); |
| 132 | ++i ) |
| 133 | { |
| 134 | wxWindow * const child = *i; |
| 135 | |
| 136 | // Allow NULL elements in the list, this makes the code of derived |
| 137 | // composite controls which may have optionally shown children |
| 138 | // simpler and it doesn't cost us much here. |
| 139 | if ( child ) |
| 140 | (child->*func)(arg); |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | wxDECLARE_NO_COPY_TEMPLATE_CLASS(wxCompositeWindow, W); |
| 145 | }; |
| 146 | |
| 147 | #else // __VISUALC6__ |
| 148 | |
| 149 | template <class W> |
| 150 | class wxCompositeWindow : public W |
| 151 | { |
| 152 | }; |
| 153 | |
| 154 | #endif // !__VISUALC6__/__VISUALC6__ |
| 155 | |
| 156 | #endif // _WX_COMPOSITEWIN_H_ |