]>
Commit | Line | Data |
---|---|---|
1 | ///////////////////////////////////////////////////////////////////////////// | |
2 | // Name: src/msw/control.cpp | |
3 | // Purpose: wxControl class | |
4 | // Author: Julian Smart | |
5 | // Modified by: | |
6 | // Created: 01/02/97 | |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) Julian Smart | |
9 | // Licence: wxWindows licence | |
10 | ///////////////////////////////////////////////////////////////////////////// | |
11 | ||
12 | // ============================================================================ | |
13 | // declarations | |
14 | // ============================================================================ | |
15 | ||
16 | // ---------------------------------------------------------------------------- | |
17 | // headers | |
18 | // ---------------------------------------------------------------------------- | |
19 | ||
20 | // For compilers that support precompilation, includes "wx.h". | |
21 | #include "wx/wxprec.h" | |
22 | ||
23 | #ifdef __BORLANDC__ | |
24 | #pragma hdrstop | |
25 | #endif | |
26 | ||
27 | #if wxUSE_CONTROLS | |
28 | ||
29 | #include "wx/control.h" | |
30 | ||
31 | #ifndef WX_PRECOMP | |
32 | #include "wx/msw/wrapcctl.h" // include <commctrl.h> "properly" | |
33 | #include "wx/event.h" | |
34 | #include "wx/app.h" | |
35 | #include "wx/dcclient.h" | |
36 | #include "wx/log.h" | |
37 | #include "wx/settings.h" | |
38 | #include "wx/ctrlsub.h" | |
39 | #endif | |
40 | ||
41 | #if wxUSE_LISTCTRL | |
42 | #include "wx/listctrl.h" | |
43 | #endif // wxUSE_LISTCTRL | |
44 | ||
45 | #if wxUSE_TREECTRL | |
46 | #include "wx/treectrl.h" | |
47 | #endif // wxUSE_TREECTRL | |
48 | ||
49 | #include "wx/msw/private.h" | |
50 | #include "wx/msw/uxtheme.h" | |
51 | ||
52 | // ---------------------------------------------------------------------------- | |
53 | // wxWin macros | |
54 | // ---------------------------------------------------------------------------- | |
55 | ||
56 | IMPLEMENT_ABSTRACT_CLASS(wxControl, wxWindow) | |
57 | ||
58 | // ============================================================================ | |
59 | // wxControl implementation | |
60 | // ============================================================================ | |
61 | ||
62 | // ---------------------------------------------------------------------------- | |
63 | // control window creation | |
64 | // ---------------------------------------------------------------------------- | |
65 | ||
66 | bool wxControl::Create(wxWindow *parent, | |
67 | wxWindowID id, | |
68 | const wxPoint& pos, | |
69 | const wxSize& size, | |
70 | long style, | |
71 | const wxValidator& wxVALIDATOR_PARAM(validator), | |
72 | const wxString& name) | |
73 | { | |
74 | if ( !wxWindow::Create(parent, id, pos, size, style, name) ) | |
75 | return false; | |
76 | ||
77 | #if wxUSE_VALIDATORS | |
78 | SetValidator(validator); | |
79 | #endif | |
80 | ||
81 | return true; | |
82 | } | |
83 | ||
84 | bool wxControl::MSWCreateControl(const wxChar *classname, | |
85 | const wxString& label, | |
86 | const wxPoint& pos, | |
87 | const wxSize& size) | |
88 | { | |
89 | WXDWORD exstyle; | |
90 | WXDWORD msStyle = MSWGetStyle(GetWindowStyle(), &exstyle); | |
91 | ||
92 | return MSWCreateControl(classname, msStyle, pos, size, label, exstyle); | |
93 | } | |
94 | ||
95 | bool wxControl::MSWCreateControl(const wxChar *classname, | |
96 | WXDWORD style, | |
97 | const wxPoint& pos, | |
98 | const wxSize& size, | |
99 | const wxString& label, | |
100 | WXDWORD exstyle) | |
101 | { | |
102 | // if no extended style given, determine it ourselves | |
103 | if ( exstyle == (WXDWORD)-1 ) | |
104 | { | |
105 | exstyle = 0; | |
106 | (void) MSWGetStyle(GetWindowStyle(), &exstyle); | |
107 | } | |
108 | ||
109 | // all controls should have this style | |
110 | style |= WS_CHILD; | |
111 | ||
112 | // create the control visible if it's currently shown for wxWidgets | |
113 | if ( m_isShown ) | |
114 | { | |
115 | style |= WS_VISIBLE; | |
116 | } | |
117 | ||
118 | // choose the position for the control: we have a problem with default size | |
119 | // here as we can't calculate the best size before the control exists | |
120 | // (DoGetBestSize() may need to use m_hWnd), so just choose the minimal | |
121 | // possible but non 0 size because 0 window width/height result in problems | |
122 | // elsewhere | |
123 | int x = pos.x == wxDefaultCoord ? 0 : pos.x, | |
124 | y = pos.y == wxDefaultCoord ? 0 : pos.y, | |
125 | w = size.x == wxDefaultCoord ? 1 : size.x, | |
126 | h = size.y == wxDefaultCoord ? 1 : size.y; | |
127 | ||
128 | // ... and adjust it to account for a possible parent frames toolbar | |
129 | AdjustForParentClientOrigin(x, y); | |
130 | ||
131 | m_hWnd = (WXHWND)::CreateWindowEx | |
132 | ( | |
133 | exstyle, // extended style | |
134 | classname, // the kind of control to create | |
135 | label.wx_str(), // the window name | |
136 | style, // the window style | |
137 | x, y, w, h, // the window position and size | |
138 | GetHwndOf(GetParent()), // parent | |
139 | (HMENU)wxUIntToPtr(GetId()), // child id | |
140 | wxGetInstance(), // app instance | |
141 | NULL // creation parameters | |
142 | ); | |
143 | ||
144 | if ( !m_hWnd ) | |
145 | { | |
146 | #ifdef __WXDEBUG__ | |
147 | wxLogLastError(wxString::Format | |
148 | ( | |
149 | _T("CreateWindowEx(\"%s\", flags=%08lx, ex=%08lx)"), | |
150 | classname, style, exstyle | |
151 | )); | |
152 | #endif // __WXDEBUG__ | |
153 | ||
154 | return false; | |
155 | } | |
156 | ||
157 | #if !wxUSE_UNICODE | |
158 | // Text labels starting with the character 0xff (which is a valid character | |
159 | // in many code pages) don't appear correctly as CreateWindowEx() has some | |
160 | // special treatment for this case, apparently the strings starting with -1 | |
161 | // are not really strings but something called "ordinals". There is no | |
162 | // documentation about it but the fact is that the label gets mangled or | |
163 | // not displayed at all if we don't do this, see #9572. | |
164 | // | |
165 | // Notice that 0xffff is not a valid Unicode character so the problem | |
166 | // doesn't arise in Unicode build. | |
167 | if ( !label.empty() && label[0] == -1 ) | |
168 | ::SetWindowText(GetHwnd(), label.wx_str()); | |
169 | #endif // !wxUSE_UNICODE | |
170 | ||
171 | // saving the label in m_labelOrig to return it verbatim | |
172 | // later in GetLabel() | |
173 | m_labelOrig = label; | |
174 | ||
175 | // install wxWidgets window proc for this window | |
176 | SubclassWin(m_hWnd); | |
177 | ||
178 | // set up fonts and colours | |
179 | InheritAttributes(); | |
180 | if ( !m_hasFont ) | |
181 | { | |
182 | bool setFont = true; | |
183 | ||
184 | wxFont font = GetDefaultAttributes().font; | |
185 | ||
186 | // if we set a font for {list,tree}ctrls and the font size is changed in | |
187 | // the display properties then the font size for these controls doesn't | |
188 | // automatically adjust when they receive WM_SETTINGCHANGE | |
189 | ||
190 | // FIXME: replace the dynamic casts with virtual function calls!! | |
191 | #if wxUSE_LISTCTRL || wxUSE_TREECTRL | |
192 | bool testFont = false; | |
193 | #if wxUSE_LISTCTRL | |
194 | if ( wxDynamicCastThis(wxListCtrl) ) | |
195 | testFont = true; | |
196 | #endif // wxUSE_LISTCTRL | |
197 | #if wxUSE_TREECTRL | |
198 | if ( wxDynamicCastThis(wxTreeCtrl) ) | |
199 | testFont = true; | |
200 | #endif // wxUSE_TREECTRL | |
201 | ||
202 | if ( testFont ) | |
203 | { | |
204 | // not sure if we need to explicitly set the font here for Win95/NT4 | |
205 | // but we definitely can't do it for any newer version | |
206 | // see wxGetCCDefaultFont() in src/msw/settings.cpp for explanation | |
207 | // of why this test works | |
208 | ||
209 | // TODO: test Win95/NT4 to see if this is needed or breaks the | |
210 | // font resizing as it does on newer versions | |
211 | if ( font != wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT) ) | |
212 | { | |
213 | setFont = false; | |
214 | } | |
215 | } | |
216 | #endif // wxUSE_LISTCTRL || wxUSE_TREECTRL | |
217 | ||
218 | if ( setFont ) | |
219 | { | |
220 | SetFont(GetDefaultAttributes().font); | |
221 | } | |
222 | } | |
223 | ||
224 | // set the size now if no initial size specified | |
225 | SetInitialSize(size); | |
226 | ||
227 | return true; | |
228 | } | |
229 | ||
230 | // ---------------------------------------------------------------------------- | |
231 | // various accessors | |
232 | // ---------------------------------------------------------------------------- | |
233 | ||
234 | WXDWORD wxControl::MSWGetStyle(long style, WXDWORD *exstyle) const | |
235 | { | |
236 | long msStyle = wxWindow::MSWGetStyle(style, exstyle); | |
237 | ||
238 | if ( AcceptsFocusFromKeyboard() ) | |
239 | { | |
240 | msStyle |= WS_TABSTOP; | |
241 | } | |
242 | ||
243 | return msStyle; | |
244 | } | |
245 | ||
246 | wxSize wxControl::DoGetBestSize() const | |
247 | { | |
248 | if (m_windowSizer) | |
249 | return wxControlBase::DoGetBestSize(); | |
250 | ||
251 | return wxSize(DEFAULT_ITEM_WIDTH, DEFAULT_ITEM_HEIGHT); | |
252 | } | |
253 | ||
254 | wxBorder wxControl::GetDefaultBorder() const | |
255 | { | |
256 | return wxControlBase::GetDefaultBorder(); | |
257 | } | |
258 | ||
259 | // This is a helper for all wxControls made with UPDOWN native control. | |
260 | // In wxMSW it was only wxSpinCtrl derived from wxSpinButton but in | |
261 | // WinCE of Smartphones this happens also for native wxTextCtrl, | |
262 | // wxChoice and others. | |
263 | wxSize wxControl::GetBestSpinnerSize(const bool is_vertical) const | |
264 | { | |
265 | // take size according to layout | |
266 | wxSize bestSize( | |
267 | #if defined(__SMARTPHONE__) && defined(__WXWINCE__) | |
268 | 0,GetCharHeight() | |
269 | #else | |
270 | ::GetSystemMetrics(is_vertical ? SM_CXVSCROLL : SM_CXHSCROLL), | |
271 | ::GetSystemMetrics(is_vertical ? SM_CYVSCROLL : SM_CYHSCROLL) | |
272 | #endif | |
273 | ); | |
274 | ||
275 | // correct size as for undocumented MSW variants cases (WinCE and perhaps others) | |
276 | if (bestSize.x==0) | |
277 | bestSize.x = bestSize.y; | |
278 | if (bestSize.y==0) | |
279 | bestSize.y = bestSize.x; | |
280 | ||
281 | // double size according to layout | |
282 | if (is_vertical) | |
283 | bestSize.y *= 2; | |
284 | else | |
285 | bestSize.x *= 2; | |
286 | ||
287 | return bestSize; | |
288 | } | |
289 | ||
290 | /* static */ wxVisualAttributes | |
291 | wxControl::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant)) | |
292 | { | |
293 | wxVisualAttributes attrs; | |
294 | ||
295 | // old school (i.e. not "common") controls use the standard dialog font | |
296 | // by default | |
297 | attrs.font = wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT); | |
298 | ||
299 | // most, or at least many, of the controls use the same colours as the | |
300 | // buttons -- others will have to override this (and possibly simply call | |
301 | // GetCompositeControlsDefaultAttributes() from their versions) | |
302 | attrs.colFg = wxSystemSettings::GetColour(wxSYS_COLOUR_BTNTEXT); | |
303 | attrs.colBg = wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE); | |
304 | ||
305 | return attrs; | |
306 | } | |
307 | ||
308 | // another version for the "composite", i.e. non simple controls | |
309 | /* static */ wxVisualAttributes | |
310 | wxControl::GetCompositeControlsDefaultAttributes(wxWindowVariant WXUNUSED(variant)) | |
311 | { | |
312 | wxVisualAttributes attrs; | |
313 | attrs.font = wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT); | |
314 | attrs.colFg = wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT); | |
315 | attrs.colBg = wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW); | |
316 | ||
317 | return attrs; | |
318 | } | |
319 | ||
320 | // ---------------------------------------------------------------------------- | |
321 | // message handling | |
322 | // ---------------------------------------------------------------------------- | |
323 | ||
324 | bool wxControl::ProcessCommand(wxCommandEvent& event) | |
325 | { | |
326 | return HandleWindowEvent(event); | |
327 | } | |
328 | ||
329 | bool wxControl::MSWOnNotify(int idCtrl, | |
330 | WXLPARAM lParam, | |
331 | WXLPARAM* result) | |
332 | { | |
333 | wxEventType eventType wxDUMMY_INITIALIZE(wxEVT_NULL); | |
334 | ||
335 | NMHDR *hdr = (NMHDR*) lParam; | |
336 | switch ( hdr->code ) | |
337 | { | |
338 | case NM_CLICK: | |
339 | eventType = wxEVT_COMMAND_LEFT_CLICK; | |
340 | break; | |
341 | ||
342 | case NM_DBLCLK: | |
343 | eventType = wxEVT_COMMAND_LEFT_DCLICK; | |
344 | break; | |
345 | ||
346 | case NM_RCLICK: | |
347 | eventType = wxEVT_COMMAND_RIGHT_CLICK; | |
348 | break; | |
349 | ||
350 | case NM_RDBLCLK: | |
351 | eventType = wxEVT_COMMAND_RIGHT_DCLICK; | |
352 | break; | |
353 | ||
354 | case NM_SETFOCUS: | |
355 | eventType = wxEVT_COMMAND_SET_FOCUS; | |
356 | break; | |
357 | ||
358 | case NM_KILLFOCUS: | |
359 | eventType = wxEVT_COMMAND_KILL_FOCUS; | |
360 | break; | |
361 | ||
362 | case NM_RETURN: | |
363 | eventType = wxEVT_COMMAND_ENTER; | |
364 | break; | |
365 | ||
366 | default: | |
367 | return wxWindow::MSWOnNotify(idCtrl, lParam, result); | |
368 | } | |
369 | ||
370 | wxCommandEvent event(wxEVT_NULL, m_windowId); | |
371 | event.SetEventType(eventType); | |
372 | event.SetEventObject(this); | |
373 | ||
374 | return HandleWindowEvent(event); | |
375 | } | |
376 | ||
377 | WXHBRUSH wxControl::DoMSWControlColor(WXHDC pDC, wxColour colBg, WXHWND hWnd) | |
378 | { | |
379 | HDC hdc = (HDC)pDC; | |
380 | if ( m_hasFgCol ) | |
381 | { | |
382 | ::SetTextColor(hdc, wxColourToRGB(GetForegroundColour())); | |
383 | } | |
384 | ||
385 | WXHBRUSH hbr = 0; | |
386 | if ( !colBg.Ok() ) | |
387 | { | |
388 | hbr = MSWGetBgBrush(pDC, hWnd); | |
389 | ||
390 | // if the control doesn't have any bg colour, foreground colour will be | |
391 | // ignored as the return value would be 0 -- so forcefully give it a | |
392 | // non default background brush in this case | |
393 | if ( !hbr && m_hasFgCol ) | |
394 | colBg = GetBackgroundColour(); | |
395 | } | |
396 | ||
397 | // use the background colour override if a valid colour is given | |
398 | if ( colBg.Ok() ) | |
399 | { | |
400 | ::SetBkColor(hdc, wxColourToRGB(colBg)); | |
401 | ||
402 | // draw children with the same colour as the parent | |
403 | wxBrush *brush = wxTheBrushList->FindOrCreateBrush(colBg, | |
404 | wxBRUSHSTYLE_SOLID); | |
405 | hbr = (WXHBRUSH)brush->GetResourceHandle(); | |
406 | } | |
407 | ||
408 | // if we use custom background, we should set foreground ourselves too | |
409 | if ( hbr && !m_hasFgCol ) | |
410 | { | |
411 | ::SetTextColor(hdc, ::GetSysColor(COLOR_WINDOWTEXT)); | |
412 | } | |
413 | //else: already set above | |
414 | ||
415 | return hbr; | |
416 | } | |
417 | ||
418 | WXHBRUSH wxControl::MSWControlColor(WXHDC pDC, WXHWND hWnd) | |
419 | { | |
420 | wxColour colBg; | |
421 | ||
422 | if ( HasTransparentBackground() ) | |
423 | ::SetBkMode((HDC)pDC, TRANSPARENT); | |
424 | else // if the control is opaque it shouldn't use the parents background | |
425 | colBg = GetBackgroundColour(); | |
426 | ||
427 | return DoMSWControlColor(pDC, colBg, hWnd); | |
428 | } | |
429 | ||
430 | WXHBRUSH wxControl::MSWControlColorDisabled(WXHDC pDC) | |
431 | { | |
432 | return DoMSWControlColor(pDC, | |
433 | wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE), | |
434 | GetHWND()); | |
435 | } | |
436 | ||
437 | // ---------------------------------------------------------------------------- | |
438 | // wxControlWithItems | |
439 | // ---------------------------------------------------------------------------- | |
440 | ||
441 | void wxControlWithItems::MSWAllocStorage(const wxArrayStringsAdapter& items, | |
442 | unsigned wm) | |
443 | { | |
444 | const unsigned numItems = items.GetCount(); | |
445 | unsigned long totalTextLength = numItems; // for trailing '\0' characters | |
446 | for ( unsigned i = 0; i < numItems; ++i ) | |
447 | { | |
448 | totalTextLength += items[i].length(); | |
449 | } | |
450 | ||
451 | if ( SendMessage((HWND)MSWGetItemsHWND(), wm, numItems, | |
452 | (LPARAM)totalTextLength*sizeof(wxChar)) == LB_ERRSPACE ) | |
453 | { | |
454 | wxLogLastError(wxT("SendMessage(XX_INITSTORAGE)")); | |
455 | } | |
456 | } | |
457 | ||
458 | int wxControlWithItems::MSWInsertOrAppendItem(unsigned pos, | |
459 | const wxString& item, | |
460 | unsigned wm) | |
461 | { | |
462 | LRESULT n = SendMessage((HWND)MSWGetItemsHWND(), wm, pos, | |
463 | (LPARAM)item.wx_str()); | |
464 | if ( n == CB_ERR || n == CB_ERRSPACE ) | |
465 | { | |
466 | wxLogLastError(wxT("SendMessage(XX_ADD/INSERTSTRING)")); | |
467 | return wxNOT_FOUND; | |
468 | } | |
469 | ||
470 | return n; | |
471 | } | |
472 | ||
473 | // --------------------------------------------------------------------------- | |
474 | // global functions | |
475 | // --------------------------------------------------------------------------- | |
476 | ||
477 | // this is used in radiobox.cpp and slider95.cpp and should be removed as soon | |
478 | // as it is not needed there any more! | |
479 | // | |
480 | // Call this repeatedly for several wnds to find the overall size | |
481 | // of the widget. | |
482 | // Call it initially with wxDefaultCoord for all values in rect. | |
483 | // Keep calling for other widgets, and rect will be modified | |
484 | // to calculate largest bounding rectangle. | |
485 | void wxFindMaxSize(WXHWND wnd, RECT *rect) | |
486 | { | |
487 | int left = rect->left; | |
488 | int right = rect->right; | |
489 | int top = rect->top; | |
490 | int bottom = rect->bottom; | |
491 | ||
492 | GetWindowRect((HWND) wnd, rect); | |
493 | ||
494 | if (left < 0) | |
495 | return; | |
496 | ||
497 | if (left < rect->left) | |
498 | rect->left = left; | |
499 | ||
500 | if (right > rect->right) | |
501 | rect->right = right; | |
502 | ||
503 | if (top < rect->top) | |
504 | rect->top = top; | |
505 | ||
506 | if (bottom > rect->bottom) | |
507 | rect->bottom = bottom; | |
508 | } | |
509 | ||
510 | #endif // wxUSE_CONTROLS |