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