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