]>
Commit | Line | Data |
---|---|---|
2bda0e17 | 1 | ///////////////////////////////////////////////////////////////////////////// |
1e6feb95 | 2 | // Name: msw/control.cpp |
2bda0e17 KB |
3 | // Purpose: wxControl class |
4 | // Author: Julian Smart | |
5 | // Modified by: | |
6 | // Created: 01/02/97 | |
7 | // RCS-ID: $Id$ | |
6c9a19aa | 8 | // Copyright: (c) Julian Smart |
65571936 | 9 | // Licence: wxWindows licence |
2bda0e17 KB |
10 | ///////////////////////////////////////////////////////////////////////////// |
11 | ||
34040e31 VZ |
12 | // ============================================================================ |
13 | // declarations | |
14 | // ============================================================================ | |
15 | ||
16 | // ---------------------------------------------------------------------------- | |
17 | // headers | |
18 | // ---------------------------------------------------------------------------- | |
19 | ||
14f355c2 | 20 | #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA) |
1e6feb95 | 21 | #pragma implementation "control.h" |
2bda0e17 KB |
22 | #endif |
23 | ||
24 | // For compilers that support precompilation, includes "wx.h". | |
25 | #include "wx/wxprec.h" | |
26 | ||
27 | #ifdef __BORLANDC__ | |
1e6feb95 | 28 | #pragma hdrstop |
2bda0e17 KB |
29 | #endif |
30 | ||
1e6feb95 VZ |
31 | #if wxUSE_CONTROLS |
32 | ||
2bda0e17 | 33 | #ifndef WX_PRECOMP |
1e6feb95 VZ |
34 | #include "wx/event.h" |
35 | #include "wx/app.h" | |
36 | #include "wx/dcclient.h" | |
37 | #include "wx/log.h" | |
34040e31 | 38 | #include "wx/settings.h" |
2bda0e17 KB |
39 | #endif |
40 | ||
2432b92d JS |
41 | #include "wx/control.h" |
42 | ||
01c500af VZ |
43 | #if wxUSE_NOTEBOOK |
44 | #include "wx/notebook.h" | |
45 | #endif // wxUSE_NOTEBOOK | |
46 | ||
2bda0e17 | 47 | #include "wx/msw/private.h" |
01c500af | 48 | #include "wx/msw/uxtheme.h" |
2bda0e17 | 49 | |
b39dbf34 | 50 | #if defined(__WIN95__) && !(defined(__GNUWIN32_OLD__) && !defined(__CYGWIN10__)) |
c42404a5 | 51 | #include <commctrl.h> |
2bda0e17 KB |
52 | #endif |
53 | ||
34040e31 VZ |
54 | // ---------------------------------------------------------------------------- |
55 | // wxWin macros | |
56 | // ---------------------------------------------------------------------------- | |
57 | ||
2bda0e17 KB |
58 | IMPLEMENT_ABSTRACT_CLASS(wxControl, wxWindow) |
59 | ||
34040e31 VZ |
60 | // ============================================================================ |
61 | // wxControl implementation | |
62 | // ============================================================================ | |
63 | ||
64 | // ---------------------------------------------------------------------------- | |
65 | // wxControl ctor/dtor | |
66 | // ---------------------------------------------------------------------------- | |
2bda0e17 | 67 | |
42e69d6b | 68 | wxControl::~wxControl() |
2bda0e17 | 69 | { |
d95de154 | 70 | m_isBeingDeleted = true; |
2bda0e17 KB |
71 | } |
72 | ||
34040e31 VZ |
73 | // ---------------------------------------------------------------------------- |
74 | // control window creation | |
75 | // ---------------------------------------------------------------------------- | |
8d772832 | 76 | |
5b2f31eb VZ |
77 | bool wxControl::Create(wxWindow *parent, |
78 | wxWindowID id, | |
8d772832 | 79 | const wxPoint& pos, |
5b2f31eb VZ |
80 | const wxSize& size, |
81 | long style, | |
ac8d0c11 | 82 | const wxValidator& wxVALIDATOR_PARAM(validator), |
8d772832 RD |
83 | const wxString& name) |
84 | { | |
5b2f31eb | 85 | if ( !wxWindow::Create(parent, id, pos, size, style, name) ) |
d95de154 | 86 | return false; |
5b2f31eb | 87 | |
8d772832 | 88 | #if wxUSE_VALIDATORS |
5b2f31eb | 89 | SetValidator(validator); |
8d772832 | 90 | #endif |
5b2f31eb | 91 | |
d95de154 | 92 | return true; |
5b2f31eb VZ |
93 | } |
94 | ||
95 | bool wxControl::MSWCreateControl(const wxChar *classname, | |
96 | const wxString& label, | |
97 | const wxPoint& pos, | |
6dd16e4f | 98 | const wxSize& size) |
5b2f31eb VZ |
99 | { |
100 | WXDWORD exstyle; | |
6dd16e4f | 101 | WXDWORD msStyle = MSWGetStyle(GetWindowStyle(), &exstyle); |
5b2f31eb | 102 | |
2eb4c3aa | 103 | return MSWCreateControl(classname, msStyle, pos, size, label, exstyle); |
8d772832 RD |
104 | } |
105 | ||
222594ea VZ |
106 | bool wxControl::MSWCreateControl(const wxChar *classname, |
107 | WXDWORD style, | |
108 | const wxPoint& pos, | |
109 | const wxSize& size, | |
110 | const wxString& label, | |
2eb4c3aa | 111 | WXDWORD exstyle) |
8d99be5f | 112 | { |
222594ea VZ |
113 | // if no extended style given, determine it ourselves |
114 | if ( exstyle == (WXDWORD)-1 ) | |
115 | { | |
fe3d9123 | 116 | exstyle = 0; |
65bc172c | 117 | (void) MSWGetStyle(GetWindowStyle(), &exstyle); |
222594ea VZ |
118 | } |
119 | ||
bdf5c30d VZ |
120 | // all controls should have this style |
121 | style |= WS_CHILD; | |
122 | ||
77ffb593 | 123 | // create the control visible if it's currently shown for wxWidgets |
bdf5c30d VZ |
124 | if ( m_isShown ) |
125 | { | |
126 | style |= WS_VISIBLE; | |
127 | } | |
3f2711d5 | 128 | |
7fe985ee VZ |
129 | // choose the position for the control: we have a problem with default size |
130 | // here as we can't calculate the best size before the control exists | |
131 | // (DoGetBestSize() may need to use m_hWnd), so just choose the minimal | |
132 | // possible but non 0 size because 0 window width/height result in problems | |
133 | // elsewhere | |
d95de154 WS |
134 | int x = pos.x == wxDefaultCoord ? 0 : pos.x, |
135 | y = pos.y == wxDefaultCoord ? 0 : pos.y, | |
7fe985ee VZ |
136 | w = size.x == wxDefaultCoord ? 1 : size.x, |
137 | h = size.y == wxDefaultCoord ? 1 : size.y; | |
a63cbfa3 | 138 | |
7c0a023d | 139 | // ... and adjust it to account for a possible parent frames toolbar |
4e9d23cd VZ |
140 | AdjustForParentClientOrigin(x, y); |
141 | ||
8d99be5f VZ |
142 | m_hWnd = (WXHWND)::CreateWindowEx |
143 | ( | |
222594ea | 144 | exstyle, // extended style |
8d99be5f | 145 | classname, // the kind of control to create |
222594ea | 146 | label, // the window name |
8d99be5f | 147 | style, // the window style |
a63cbfa3 | 148 | x, y, w, h, // the window position and size |
8d99be5f VZ |
149 | GetHwndOf(GetParent()), // parent |
150 | (HMENU)GetId(), // child id | |
151 | wxGetInstance(), // app instance | |
152 | NULL // creation parameters | |
153 | ); | |
154 | ||
155 | if ( !m_hWnd ) | |
156 | { | |
658252ef VZ |
157 | #ifdef __WXDEBUG__ |
158 | wxFAIL_MSG(wxString::Format | |
159 | ( | |
160 | _T("CreateWindowEx(\"%s\", flags=%08x, ex=%08x) failed"), | |
f62ff2f1 | 161 | classname, (unsigned int)style, (unsigned int)exstyle |
658252ef VZ |
162 | )); |
163 | #endif // __WXDEBUG__ | |
8d99be5f | 164 | |
d95de154 | 165 | return false; |
8d99be5f VZ |
166 | } |
167 | ||
77ffb593 | 168 | // install wxWidgets window proc for this window |
8d99be5f VZ |
169 | SubclassWin(m_hWnd); |
170 | ||
34040e31 | 171 | // set up fonts and colours |
8d99be5f | 172 | InheritAttributes(); |
e0176dd9 VZ |
173 | if (!m_hasFont) |
174 | SetFont(GetDefaultAttributes().font); | |
8d99be5f | 175 | |
a63cbfa3 | 176 | // set the size now if no initial size specified |
085dd1e9 | 177 | SetInitialBestSize(size); |
a63cbfa3 | 178 | |
d95de154 | 179 | return true; |
8d99be5f VZ |
180 | } |
181 | ||
34040e31 VZ |
182 | // ---------------------------------------------------------------------------- |
183 | // various accessors | |
184 | // ---------------------------------------------------------------------------- | |
185 | ||
65bc172c VZ |
186 | wxBorder wxControl::GetDefaultBorder() const |
187 | { | |
188 | // we want to automatically give controls a sunken style (confusingly, | |
189 | // it may not really mean sunken at all as we map it to WS_EX_CLIENTEDGE | |
190 | // which is not sunken at all under Windows XP -- rather, just the default) | |
edea6281 JS |
191 | #if defined(__POCKETPC__) || defined(__SMARTPHONE__) |
192 | return wxBORDER_SIMPLE; | |
193 | #else | |
65bc172c | 194 | return wxBORDER_SUNKEN; |
edea6281 | 195 | #endif |
65bc172c VZ |
196 | } |
197 | ||
34040e31 VZ |
198 | WXDWORD wxControl::MSWGetStyle(long style, WXDWORD *exstyle) const |
199 | { | |
200 | long msStyle = wxWindow::MSWGetStyle(style, exstyle); | |
201 | ||
202 | if ( AcceptsFocus() ) | |
203 | { | |
204 | msStyle |= WS_TABSTOP; | |
205 | } | |
206 | ||
207 | return msStyle; | |
208 | } | |
209 | ||
f68586e5 | 210 | wxSize wxControl::DoGetBestSize() const |
4438caf4 VZ |
211 | { |
212 | return wxSize(DEFAULT_ITEM_WIDTH, DEFAULT_ITEM_HEIGHT); | |
213 | } | |
214 | ||
4bf45c9e WS |
215 | // This is a helper for all wxControls made with UPDOWN native control. |
216 | // In wxMSW it was only wxSpinCtrl derived from wxSpinButton but in | |
217 | // WinCE of Smartphones this happens also for native wxTextCtrl, | |
218 | // wxChoice and others. | |
5aaabb37 | 219 | wxSize wxControl::GetBestSpinnerSize(const bool is_vertical) const |
4bf45c9e | 220 | { |
b081046a | 221 | // take size according to layout |
1550d5f8 | 222 | wxSize bestSize( |
285f605a | 223 | #if defined(__SMARTPHONE__) && defined(__WXWINCE__) |
1550d5f8 | 224 | 0,GetCharHeight() |
3180bc0e | 225 | #else |
46697f31 WS |
226 | ::GetSystemMetrics(is_vertical ? SM_CXVSCROLL : SM_CXHSCROLL), |
227 | ::GetSystemMetrics(is_vertical ? SM_CYVSCROLL : SM_CYHSCROLL) | |
3180bc0e | 228 | #endif |
1550d5f8 | 229 | ); |
b081046a WS |
230 | |
231 | // correct size as for undocumented MSW variants cases (WinCE and perhaps others) | |
232 | if (bestSize.x==0) | |
233 | bestSize.x = bestSize.y; | |
234 | if (bestSize.y==0) | |
235 | bestSize.y = bestSize.x; | |
236 | ||
237 | // double size according to layout | |
4bf45c9e | 238 | if (is_vertical) |
b081046a | 239 | bestSize.y *= 2; |
4bf45c9e | 240 | else |
b081046a WS |
241 | bestSize.x *= 2; |
242 | ||
243 | return bestSize; | |
4bf45c9e WS |
244 | } |
245 | ||
34040e31 VZ |
246 | /* static */ wxVisualAttributes |
247 | wxControl::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant)) | |
248 | { | |
249 | wxVisualAttributes attrs; | |
250 | ||
251 | // old school (i.e. not "common") controls use the standard dialog font | |
252 | // by default | |
253 | attrs.font = wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT); | |
254 | ||
255 | // most, or at least many, of the controls use the same colours as the | |
256 | // buttons -- others will have to override this (and possibly simply call | |
257 | // GetCompositeControlsDefaultAttributes() from their versions) | |
258 | attrs.colFg = wxSystemSettings::GetColour(wxSYS_COLOUR_BTNTEXT); | |
259 | attrs.colBg = wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE); | |
260 | ||
261 | return attrs; | |
262 | } | |
263 | ||
264 | // another version for the "composite", i.e. non simple controls | |
265 | /* static */ wxVisualAttributes | |
fbfe58cb | 266 | wxControl::GetCompositeControlsDefaultAttributes(wxWindowVariant WXUNUSED(variant)) |
34040e31 VZ |
267 | { |
268 | wxVisualAttributes attrs; | |
269 | attrs.font = wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT); | |
270 | attrs.colFg = wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT); | |
271 | attrs.colBg = wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW); | |
272 | ||
273 | return attrs; | |
274 | } | |
275 | ||
276 | // ---------------------------------------------------------------------------- | |
277 | // message handling | |
278 | // ---------------------------------------------------------------------------- | |
279 | ||
42e69d6b | 280 | bool wxControl::ProcessCommand(wxCommandEvent& event) |
2bda0e17 | 281 | { |
42e69d6b | 282 | return GetEventHandler()->ProcessEvent(event); |
2bda0e17 KB |
283 | } |
284 | ||
a23fd0e1 VZ |
285 | #ifdef __WIN95__ |
286 | bool wxControl::MSWOnNotify(int idCtrl, | |
287 | WXLPARAM lParam, | |
288 | WXLPARAM* result) | |
2bda0e17 | 289 | { |
5cb598ae | 290 | wxEventType eventType wxDUMMY_INITIALIZE(wxEVT_NULL); |
b6e5eaa5 VZ |
291 | |
292 | NMHDR *hdr = (NMHDR*) lParam; | |
293 | switch ( hdr->code ) | |
a23fd0e1 VZ |
294 | { |
295 | case NM_CLICK: | |
296 | eventType = wxEVT_COMMAND_LEFT_CLICK; | |
297 | break; | |
2bda0e17 | 298 | |
a23fd0e1 VZ |
299 | case NM_DBLCLK: |
300 | eventType = wxEVT_COMMAND_LEFT_DCLICK; | |
301 | break; | |
2bda0e17 | 302 | |
a23fd0e1 VZ |
303 | case NM_RCLICK: |
304 | eventType = wxEVT_COMMAND_RIGHT_CLICK; | |
305 | break; | |
2bda0e17 | 306 | |
a23fd0e1 VZ |
307 | case NM_RDBLCLK: |
308 | eventType = wxEVT_COMMAND_RIGHT_DCLICK; | |
309 | break; | |
2bda0e17 | 310 | |
a23fd0e1 VZ |
311 | case NM_SETFOCUS: |
312 | eventType = wxEVT_COMMAND_SET_FOCUS; | |
313 | break; | |
debe6624 | 314 | |
a23fd0e1 VZ |
315 | case NM_KILLFOCUS: |
316 | eventType = wxEVT_COMMAND_KILL_FOCUS; | |
317 | break; | |
2bda0e17 | 318 | |
a23fd0e1 VZ |
319 | case NM_RETURN: |
320 | eventType = wxEVT_COMMAND_ENTER; | |
321 | break; | |
322 | ||
323 | default: | |
324 | return wxWindow::MSWOnNotify(idCtrl, lParam, result); | |
325 | } | |
fd3f686c | 326 | |
b6e5eaa5 | 327 | wxCommandEvent event(wxEVT_NULL, m_windowId); |
2bda0e17 | 328 | event.SetEventType(eventType); |
a23fd0e1 | 329 | event.SetEventObject(this); |
2bda0e17 | 330 | |
a23fd0e1 | 331 | return GetEventHandler()->ProcessEvent(event); |
2bda0e17 | 332 | } |
a23fd0e1 | 333 | #endif // Win95 |
2bda0e17 | 334 | |
dd12ce22 | 335 | WXHBRUSH wxControl::DoMSWControlColor(WXHDC pDC, wxColour colBg) |
f048e32f | 336 | { |
01c500af VZ |
337 | HDC hdc = (HDC)pDC; |
338 | if ( m_hasFgCol ) | |
6ed16512 | 339 | { |
01c500af | 340 | ::SetTextColor(hdc, wxColourToRGB(GetForegroundColour())); |
6ed16512 | 341 | } |
01c500af | 342 | |
d1a47dfe VZ |
343 | // use the background colour override if a valid colour is given |
344 | WXHBRUSH hbr; | |
5c836c46 | 345 | if ( colBg.Ok() ) |
f048e32f | 346 | { |
bcc4aa97 VZ |
347 | ::SetBkColor(hdc, wxColourToRGB(colBg)); |
348 | ||
d1a47dfe | 349 | // draw children with the same colour as the parent |
5c836c46 | 350 | wxBrush *brush = wxTheBrushList->FindOrCreateBrush(colBg, wxSOLID); |
01c500af | 351 | |
d1a47dfe VZ |
352 | hbr = (WXHBRUSH)brush->GetResourceHandle(); |
353 | } | |
354 | else // use our own background colour and recurse upwards if necessary | |
355 | { | |
356 | hbr = MSWGetBgBrush(pDC); | |
01c500af | 357 | } |
f048e32f | 358 | |
d1a47dfe | 359 | return hbr; |
5c836c46 VZ |
360 | } |
361 | ||
362 | WXHBRUSH wxControl::MSWControlColor(WXHDC pDC) | |
363 | { | |
dd12ce22 VZ |
364 | // by default consider that the controls text shouldn't erase the |
365 | // background under it (this is true for all static controls, check boxes, | |
d40a56d0 | 366 | // radio buttons, ...) |
5c836c46 | 367 | ::SetBkMode((HDC)pDC, TRANSPARENT); |
01c500af | 368 | |
dd12ce22 | 369 | return DoMSWControlColor(pDC, wxNullColour); |
5c836c46 VZ |
370 | } |
371 | ||
372 | WXHBRUSH wxControl::MSWControlColorDisabled(WXHDC pDC) | |
373 | { | |
dd12ce22 VZ |
374 | return DoMSWControlColor(pDC, |
375 | wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE)); | |
f048e32f VZ |
376 | } |
377 | ||
42e69d6b VZ |
378 | // --------------------------------------------------------------------------- |
379 | // global functions | |
380 | // --------------------------------------------------------------------------- | |
2bda0e17 | 381 | |
34040e31 VZ |
382 | // this is used in radiobox.cpp and slider95.cpp and should be removed as soon |
383 | // as it is not needed there any more! | |
384 | // | |
42e69d6b VZ |
385 | // Call this repeatedly for several wnds to find the overall size |
386 | // of the widget. | |
d95de154 | 387 | // Call it initially with wxDefaultCoord for all values in rect. |
42e69d6b VZ |
388 | // Keep calling for other widgets, and rect will be modified |
389 | // to calculate largest bounding rectangle. | |
390 | void wxFindMaxSize(WXHWND wnd, RECT *rect) | |
2bda0e17 | 391 | { |
42e69d6b VZ |
392 | int left = rect->left; |
393 | int right = rect->right; | |
394 | int top = rect->top; | |
395 | int bottom = rect->bottom; | |
2bda0e17 | 396 | |
42e69d6b | 397 | GetWindowRect((HWND) wnd, rect); |
2bda0e17 | 398 | |
42e69d6b VZ |
399 | if (left < 0) |
400 | return; | |
2bda0e17 | 401 | |
42e69d6b VZ |
402 | if (left < rect->left) |
403 | rect->left = left; | |
2bda0e17 | 404 | |
42e69d6b VZ |
405 | if (right > rect->right) |
406 | rect->right = right; | |
2bda0e17 | 407 | |
42e69d6b VZ |
408 | if (top < rect->top) |
409 | rect->top = top; | |
2bda0e17 | 410 | |
42e69d6b VZ |
411 | if (bottom > rect->bottom) |
412 | rect->bottom = bottom; | |
2bda0e17 KB |
413 | } |
414 | ||
1e6feb95 | 415 | #endif // wxUSE_CONTROLS |