]>
Commit | Line | Data |
---|---|---|
1 | /////////////////////////////////////////////////////////////////////////////// | |
2 | // Name: wx/msw/subwin.h | |
3 | // Purpose: helper for implementing the controls with subwindows | |
4 | // Author: Vadim Zeitlin | |
5 | // Modified by: | |
6 | // Created: 2004-12-11 | |
7 | // Copyright: (c) 2004 Vadim Zeitlin <vadim@wxwindows.org> | |
8 | // Licence: wxWindows licence | |
9 | /////////////////////////////////////////////////////////////////////////////// | |
10 | ||
11 | #ifndef _WX_MSW_SUBWIN_H_ | |
12 | #define _WX_MSW_SUBWIN_H_ | |
13 | ||
14 | #include "wx/msw/private.h" | |
15 | ||
16 | // ---------------------------------------------------------------------------- | |
17 | // wxSubwindows contains all HWNDs making part of a single wx control | |
18 | // ---------------------------------------------------------------------------- | |
19 | ||
20 | class WXDLLIMPEXP_CORE wxSubwindows | |
21 | { | |
22 | public: | |
23 | // the number of subwindows can be specified either as parameter to ctor or | |
24 | // later in Create() | |
25 | wxSubwindows(size_t n = 0) { Init(); if ( n ) Create(n); } | |
26 | ||
27 | // allocate enough space for the given number of windows | |
28 | void Create(size_t n) | |
29 | { | |
30 | wxASSERT_MSG( !m_hwnds, wxT("Create() called twice?") ); | |
31 | ||
32 | m_count = n; | |
33 | m_hwnds = (HWND *)calloc(n, sizeof(HWND)); | |
34 | m_ids = new wxWindowIDRef[n]; | |
35 | } | |
36 | ||
37 | // non-virtual dtor, this class is not supposed to be used polymorphically | |
38 | ~wxSubwindows() | |
39 | { | |
40 | for ( size_t n = 0; n < m_count; n++ ) | |
41 | { | |
42 | if ( m_hwnds[n] ) | |
43 | ::DestroyWindow(m_hwnds[n]); | |
44 | } | |
45 | ||
46 | free(m_hwnds); | |
47 | delete [] m_ids; | |
48 | } | |
49 | ||
50 | // get the number of subwindows | |
51 | size_t GetCount() const { return m_count; } | |
52 | ||
53 | // access a given window | |
54 | HWND& Get(size_t n) | |
55 | { | |
56 | wxASSERT_MSG( n < m_count, wxT("subwindow index out of range") ); | |
57 | ||
58 | return m_hwnds[n]; | |
59 | } | |
60 | ||
61 | HWND operator[](size_t n) const | |
62 | { | |
63 | return const_cast<wxSubwindows *>(this)->Get(n); | |
64 | } | |
65 | ||
66 | // initialize the given window: id will be stored in wxWindowIDRef ensuring | |
67 | // that it is not reused while this object exists | |
68 | void Set(size_t n, HWND hwnd, wxWindowID id) | |
69 | { | |
70 | wxASSERT_MSG( n < m_count, wxT("subwindow index out of range") ); | |
71 | ||
72 | m_hwnds[n] = hwnd; | |
73 | m_ids[n] = id; | |
74 | } | |
75 | ||
76 | // check if we have this window | |
77 | bool HasWindow(HWND hwnd) | |
78 | { | |
79 | for ( size_t n = 0; n < m_count; n++ ) | |
80 | { | |
81 | if ( m_hwnds[n] == hwnd ) | |
82 | return true; | |
83 | } | |
84 | ||
85 | return false; | |
86 | } | |
87 | ||
88 | ||
89 | // methods which are forwarded to all subwindows | |
90 | // --------------------------------------------- | |
91 | ||
92 | // show/hide everything | |
93 | void Show(bool show) | |
94 | { | |
95 | int sw = show ? SW_SHOW : SW_HIDE; | |
96 | for ( size_t n = 0; n < m_count; n++ ) | |
97 | { | |
98 | if ( m_hwnds[n] ) | |
99 | ::ShowWindow(m_hwnds[n], sw); | |
100 | } | |
101 | } | |
102 | ||
103 | // enable/disable everything | |
104 | void Enable(bool enable) | |
105 | { | |
106 | for ( size_t n = 0; n < m_count; n++ ) | |
107 | { | |
108 | if ( m_hwnds[n] ) | |
109 | ::EnableWindow(m_hwnds[n], enable); | |
110 | } | |
111 | } | |
112 | ||
113 | // set font for all windows | |
114 | void SetFont(const wxFont& font) | |
115 | { | |
116 | HFONT hfont = GetHfontOf(font); | |
117 | wxCHECK_RET( hfont, wxT("invalid font") ); | |
118 | ||
119 | for ( size_t n = 0; n < m_count; n++ ) | |
120 | { | |
121 | if ( m_hwnds[n] ) | |
122 | { | |
123 | ::SendMessage(m_hwnds[n], WM_SETFONT, (WPARAM)hfont, 0); | |
124 | ||
125 | // otherwise the window might not be redrawn correctly | |
126 | ::InvalidateRect(m_hwnds[n], NULL, FALSE /* don't erase bg */); | |
127 | } | |
128 | } | |
129 | } | |
130 | ||
131 | // find the bounding box for all windows | |
132 | wxRect GetBoundingBox() const | |
133 | { | |
134 | wxRect r; | |
135 | for ( size_t n = 0; n < m_count; n++ ) | |
136 | { | |
137 | if ( m_hwnds[n] ) | |
138 | { | |
139 | RECT rc; | |
140 | ||
141 | ::GetWindowRect(m_hwnds[n], &rc); | |
142 | ||
143 | r.Union(wxRectFromRECT(rc)); | |
144 | } | |
145 | } | |
146 | ||
147 | return r; | |
148 | } | |
149 | ||
150 | private: | |
151 | void Init() | |
152 | { | |
153 | m_count = 0; | |
154 | m_hwnds = NULL; | |
155 | } | |
156 | ||
157 | // number of elements in m_hwnds array | |
158 | size_t m_count; | |
159 | ||
160 | // the HWNDs we contain | |
161 | HWND *m_hwnds; | |
162 | ||
163 | // the IDs of the windows | |
164 | wxWindowIDRef *m_ids; | |
165 | ||
166 | ||
167 | wxDECLARE_NO_COPY_CLASS(wxSubwindows); | |
168 | }; | |
169 | ||
170 | // convenient macro to forward a few methods which are usually propagated to | |
171 | // subwindows to a wxSubwindows object | |
172 | // | |
173 | // parameters should be: | |
174 | // - cname the name of the class implementing these methods | |
175 | // - base the name of its base class | |
176 | // - subwins the name of the member variable of type wxSubwindows * | |
177 | #define WX_FORWARD_STD_METHODS_TO_SUBWINDOWS(cname, base, subwins) \ | |
178 | bool cname::ContainsHWND(WXHWND hWnd) const \ | |
179 | { \ | |
180 | return subwins && subwins->HasWindow((HWND)hWnd); \ | |
181 | } \ | |
182 | \ | |
183 | bool cname::Show(bool show) \ | |
184 | { \ | |
185 | if ( !base::Show(show) ) \ | |
186 | return false; \ | |
187 | \ | |
188 | if ( subwins ) \ | |
189 | subwins->Show(show); \ | |
190 | \ | |
191 | return true; \ | |
192 | } \ | |
193 | \ | |
194 | bool cname::Enable(bool enable) \ | |
195 | { \ | |
196 | if ( !base::Enable(enable) ) \ | |
197 | return false; \ | |
198 | \ | |
199 | if ( subwins ) \ | |
200 | subwins->Enable(enable); \ | |
201 | \ | |
202 | return true; \ | |
203 | } \ | |
204 | \ | |
205 | bool cname::SetFont(const wxFont& font) \ | |
206 | { \ | |
207 | if ( !base::SetFont(font) ) \ | |
208 | return false; \ | |
209 | \ | |
210 | if ( subwins ) \ | |
211 | subwins->SetFont(font); \ | |
212 | \ | |
213 | return true; \ | |
214 | } | |
215 | ||
216 | ||
217 | #endif // _WX_MSW_SUBWIN_H_ | |
218 |