]> git.saurik.com Git - wxWidgets.git/blame - src/msw/control.cpp
no changes, just a typo fix
[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
8d99be5f
VZ
147 GetHwndOf(GetParent()), // parent
148 (HMENU)GetId(), // child id
149 wxGetInstance(), // app instance
150 NULL // creation parameters
151 );
152
153 if ( !m_hWnd )
154 {
658252ef
VZ
155#ifdef __WXDEBUG__
156 wxFAIL_MSG(wxString::Format
157 (
158 _T("CreateWindowEx(\"%s\", flags=%08x, ex=%08x) failed"),
f62ff2f1 159 classname, (unsigned int)style, (unsigned int)exstyle
658252ef
VZ
160 ));
161#endif // __WXDEBUG__
8d99be5f 162
d95de154 163 return false;
8d99be5f
VZ
164 }
165
39bc0347
VZ
166 // saving the label in m_labelOrig to return it verbatim
167 // later in GetLabel()
168 m_labelOrig = label;
169
77ffb593 170 // install wxWidgets window proc for this window
8d99be5f
VZ
171 SubclassWin(m_hWnd);
172
34040e31 173 // set up fonts and colours
8d99be5f 174 InheritAttributes();
3c96418b
JG
175 if ( !m_hasFont )
176 {
82c591d7
VZ
177 bool setFont = true;
178
179 wxFont font = GetDefaultAttributes().font;
180
3c96418b
JG
181 // if we set a font for {list,tree}ctrls and the font size is changed in
182 // the display properties then the font size for these controls doesn't
183 // automatically adjust when they receive WM_SETTINGCHANGE
82c591d7
VZ
184
185 // FIXME: replace the dynamic casts with virtual function calls!!
186#if wxUSE_LISTCTRL || wxUSE_TREECTRL
187 bool testFont = false;
188#if wxUSE_LISTCTRL
189 if ( wxDynamicCastThis(wxListCtrl) )
190 testFont = true;
191#endif // wxUSE_LISTCTRL
192#if wxUSE_TREECTRL
193 if ( wxDynamicCastThis(wxTreeCtrl) )
194 testFont = true;
28f9f58c 195#endif // wxUSE_TREECTRL
82c591d7
VZ
196
197 if ( testFont )
3c96418b
JG
198 {
199 // not sure if we need to explicitly set the font here for Win95/NT4
200 // but we definitely can't do it for any newer version
201 // see wxGetCCDefaultFont() in src/msw/settings.cpp for explanation
202 // of why this test works
203
204 // TODO: test Win95/NT4 to see if this is needed or breaks the
205 // font resizing as it does on newer versions
82c591d7 206 if ( font != wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT) )
3c96418b 207 {
82c591d7 208 setFont = false;
3c96418b
JG
209 }
210 }
3c96418b 211#endif // wxUSE_LISTCTRL || wxUSE_TREECTRL
82c591d7
VZ
212
213 if ( setFont )
3c96418b
JG
214 {
215 SetFont(GetDefaultAttributes().font);
216 }
217 }
8d99be5f 218
a63cbfa3 219 // set the size now if no initial size specified
170acdc9 220 SetInitialSize(size);
a63cbfa3 221
d95de154 222 return true;
8d99be5f
VZ
223}
224
34040e31
VZ
225// ----------------------------------------------------------------------------
226// various accessors
227// ----------------------------------------------------------------------------
228
65bc172c
VZ
229wxBorder wxControl::GetDefaultBorder() const
230{
231 // we want to automatically give controls a sunken style (confusingly,
232 // it may not really mean sunken at all as we map it to WS_EX_CLIENTEDGE
233 // which is not sunken at all under Windows XP -- rather, just the default)
edea6281
JS
234#if defined(__POCKETPC__) || defined(__SMARTPHONE__)
235 return wxBORDER_SIMPLE;
236#else
65bc172c 237 return wxBORDER_SUNKEN;
edea6281 238#endif
65bc172c
VZ
239}
240
34040e31
VZ
241WXDWORD wxControl::MSWGetStyle(long style, WXDWORD *exstyle) const
242{
243 long msStyle = wxWindow::MSWGetStyle(style, exstyle);
244
ad02525d 245 if ( AcceptsFocusFromKeyboard() )
34040e31
VZ
246 {
247 msStyle |= WS_TABSTOP;
248 }
249
250 return msStyle;
251}
252
f68586e5 253wxSize wxControl::DoGetBestSize() const
4438caf4
VZ
254{
255 return wxSize(DEFAULT_ITEM_WIDTH, DEFAULT_ITEM_HEIGHT);
256}
257
4bf45c9e
WS
258// This is a helper for all wxControls made with UPDOWN native control.
259// In wxMSW it was only wxSpinCtrl derived from wxSpinButton but in
260// WinCE of Smartphones this happens also for native wxTextCtrl,
261// wxChoice and others.
5aaabb37 262wxSize wxControl::GetBestSpinnerSize(const bool is_vertical) const
4bf45c9e 263{
b081046a 264 // take size according to layout
1550d5f8 265 wxSize bestSize(
285f605a 266#if defined(__SMARTPHONE__) && defined(__WXWINCE__)
1550d5f8 267 0,GetCharHeight()
3180bc0e 268#else
46697f31
WS
269 ::GetSystemMetrics(is_vertical ? SM_CXVSCROLL : SM_CXHSCROLL),
270 ::GetSystemMetrics(is_vertical ? SM_CYVSCROLL : SM_CYHSCROLL)
3180bc0e 271#endif
1550d5f8 272 );
b081046a
WS
273
274 // correct size as for undocumented MSW variants cases (WinCE and perhaps others)
275 if (bestSize.x==0)
276 bestSize.x = bestSize.y;
277 if (bestSize.y==0)
278 bestSize.y = bestSize.x;
279
280 // double size according to layout
4bf45c9e 281 if (is_vertical)
b081046a 282 bestSize.y *= 2;
4bf45c9e 283 else
b081046a
WS
284 bestSize.x *= 2;
285
286 return bestSize;
4bf45c9e
WS
287}
288
34040e31
VZ
289/* static */ wxVisualAttributes
290wxControl::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant))
291{
292 wxVisualAttributes attrs;
293
294 // old school (i.e. not "common") controls use the standard dialog font
295 // by default
296 attrs.font = wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT);
297
298 // most, or at least many, of the controls use the same colours as the
299 // buttons -- others will have to override this (and possibly simply call
300 // GetCompositeControlsDefaultAttributes() from their versions)
301 attrs.colFg = wxSystemSettings::GetColour(wxSYS_COLOUR_BTNTEXT);
302 attrs.colBg = wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE);
303
304 return attrs;
305}
306
307// another version for the "composite", i.e. non simple controls
308/* static */ wxVisualAttributes
fbfe58cb 309wxControl::GetCompositeControlsDefaultAttributes(wxWindowVariant WXUNUSED(variant))
34040e31
VZ
310{
311 wxVisualAttributes attrs;
312 attrs.font = wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT);
313 attrs.colFg = wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT);
314 attrs.colBg = wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW);
315
316 return attrs;
317}
318
319// ----------------------------------------------------------------------------
320// message handling
321// ----------------------------------------------------------------------------
322
42e69d6b 323bool wxControl::ProcessCommand(wxCommandEvent& event)
2bda0e17 324{
42e69d6b 325 return GetEventHandler()->ProcessEvent(event);
2bda0e17
KB
326}
327
a23fd0e1
VZ
328bool wxControl::MSWOnNotify(int idCtrl,
329 WXLPARAM lParam,
330 WXLPARAM* result)
2bda0e17 331{
5cb598ae 332 wxEventType eventType wxDUMMY_INITIALIZE(wxEVT_NULL);
b6e5eaa5
VZ
333
334 NMHDR *hdr = (NMHDR*) lParam;
335 switch ( hdr->code )
a23fd0e1
VZ
336 {
337 case NM_CLICK:
338 eventType = wxEVT_COMMAND_LEFT_CLICK;
339 break;
2bda0e17 340
a23fd0e1
VZ
341 case NM_DBLCLK:
342 eventType = wxEVT_COMMAND_LEFT_DCLICK;
343 break;
2bda0e17 344
a23fd0e1
VZ
345 case NM_RCLICK:
346 eventType = wxEVT_COMMAND_RIGHT_CLICK;
347 break;
2bda0e17 348
a23fd0e1
VZ
349 case NM_RDBLCLK:
350 eventType = wxEVT_COMMAND_RIGHT_DCLICK;
351 break;
2bda0e17 352
a23fd0e1
VZ
353 case NM_SETFOCUS:
354 eventType = wxEVT_COMMAND_SET_FOCUS;
355 break;
debe6624 356
a23fd0e1
VZ
357 case NM_KILLFOCUS:
358 eventType = wxEVT_COMMAND_KILL_FOCUS;
359 break;
2bda0e17 360
a23fd0e1
VZ
361 case NM_RETURN:
362 eventType = wxEVT_COMMAND_ENTER;
363 break;
364
365 default:
366 return wxWindow::MSWOnNotify(idCtrl, lParam, result);
367 }
fd3f686c 368
b6e5eaa5 369 wxCommandEvent event(wxEVT_NULL, m_windowId);
2bda0e17 370 event.SetEventType(eventType);
a23fd0e1 371 event.SetEventObject(this);
2bda0e17 372
a23fd0e1 373 return GetEventHandler()->ProcessEvent(event);
2bda0e17
KB
374}
375
2bae4332 376WXHBRUSH wxControl::DoMSWControlColor(WXHDC pDC, wxColour colBg, WXHWND hWnd)
f048e32f 377{
01c500af
VZ
378 HDC hdc = (HDC)pDC;
379 if ( m_hasFgCol )
6ed16512 380 {
01c500af 381 ::SetTextColor(hdc, wxColourToRGB(GetForegroundColour()));
6ed16512 382 }
01c500af 383
c3732409
VZ
384 WXHBRUSH hbr = 0;
385 if ( !colBg.Ok() )
386 {
2bae4332 387 hbr = MSWGetBgBrush(pDC, hWnd);
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
d1a47dfe 396 // use the background colour override if a valid colour is given
5c836c46 397 if ( colBg.Ok() )
f048e32f 398 {
bcc4aa97
VZ
399 ::SetBkColor(hdc, wxColourToRGB(colBg));
400
d1a47dfe 401 // draw children with the same colour as the parent
5c836c46 402 wxBrush *brush = wxTheBrushList->FindOrCreateBrush(colBg, wxSOLID);
01c500af 403
d1a47dfe 404 hbr = (WXHBRUSH)brush->GetResourceHandle();
3d2f4457 405
d1a47dfe 406 }
f048e32f 407
155acb0c
JS
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
d1a47dfe 415 return hbr;
5c836c46
VZ
416}
417
2bae4332 418WXHBRUSH wxControl::MSWControlColor(WXHDC pDC, WXHWND hWnd)
5c836c46 419{
c3732409
VZ
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();
01c500af 426
2bae4332 427 return DoMSWControlColor(pDC, colBg, hWnd);
5c836c46
VZ
428}
429
430WXHBRUSH wxControl::MSWControlColorDisabled(WXHDC pDC)
431{
dd12ce22 432 return DoMSWControlColor(pDC,
2bae4332
VZ
433 wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE),
434 GetHWND());
f048e32f
VZ
435}
436
a236aa20
VZ
437// ----------------------------------------------------------------------------
438// wxControlWithItems
439// ----------------------------------------------------------------------------
440
441void 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
0d5b49cf 451 if ( SendMessage((HWND)MSWGetItemsHWND(), wm, numItems,
a236aa20
VZ
452 (LPARAM)totalTextLength*sizeof(wxChar)) == LB_ERRSPACE )
453 {
454 wxLogLastError(wxT("SendMessage(XX_INITSTORAGE)"));
455 }
456}
457
458int wxControlWithItems::MSWInsertOrAppendItem(unsigned pos,
459 const wxString& item,
460 unsigned wm)
461{
0d5b49cf
VZ
462 LRESULT n = SendMessage((HWND)MSWGetItemsHWND(), wm, pos,
463 (LPARAM)item.wx_str());
a236aa20
VZ
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
42e69d6b
VZ
473// ---------------------------------------------------------------------------
474// global functions
475// ---------------------------------------------------------------------------
2bda0e17 476
34040e31
VZ
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//
42e69d6b
VZ
480// Call this repeatedly for several wnds to find the overall size
481// of the widget.
d95de154 482// Call it initially with wxDefaultCoord for all values in rect.
42e69d6b
VZ
483// Keep calling for other widgets, and rect will be modified
484// to calculate largest bounding rectangle.
485void wxFindMaxSize(WXHWND wnd, RECT *rect)
2bda0e17 486{
42e69d6b
VZ
487 int left = rect->left;
488 int right = rect->right;
489 int top = rect->top;
490 int bottom = rect->bottom;
2bda0e17 491
42e69d6b 492 GetWindowRect((HWND) wnd, rect);
2bda0e17 493
42e69d6b
VZ
494 if (left < 0)
495 return;
2bda0e17 496
42e69d6b
VZ
497 if (left < rect->left)
498 rect->left = left;
2bda0e17 499
42e69d6b
VZ
500 if (right > rect->right)
501 rect->right = right;
2bda0e17 502
42e69d6b
VZ
503 if (top < rect->top)
504 rect->top = top;
2bda0e17 505
42e69d6b
VZ
506 if (bottom > rect->bottom)
507 rect->bottom = bottom;
2bda0e17
KB
508}
509
1e6feb95 510#endif // wxUSE_CONTROLS