]> git.saurik.com Git - wxWidgets.git/blame - src/msw/control.cpp
cvs to svn
[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"
2bda0e17
KB
38#endif
39
3c96418b
JG
40#if wxUSE_LISTCTRL
41 #include "wx/listctrl.h"
42#endif // wxUSE_LISTCTRL
43
44#if wxUSE_TREECTRL
45 #include "wx/treectrl.h"
46#endif // wxUSE_TREECTRL
01c500af 47
2bda0e17 48#include "wx/msw/private.h"
01c500af 49#include "wx/msw/uxtheme.h"
2bda0e17 50
34040e31
VZ
51// ----------------------------------------------------------------------------
52// wxWin macros
53// ----------------------------------------------------------------------------
54
2bda0e17
KB
55IMPLEMENT_ABSTRACT_CLASS(wxControl, wxWindow)
56
34040e31
VZ
57// ============================================================================
58// wxControl implementation
59// ============================================================================
60
61// ----------------------------------------------------------------------------
62// wxControl ctor/dtor
63// ----------------------------------------------------------------------------
2bda0e17 64
42e69d6b 65wxControl::~wxControl()
2bda0e17 66{
d95de154 67 m_isBeingDeleted = true;
2bda0e17
KB
68}
69
34040e31
VZ
70// ----------------------------------------------------------------------------
71// control window creation
72// ----------------------------------------------------------------------------
8d772832 73
5b2f31eb
VZ
74bool wxControl::Create(wxWindow *parent,
75 wxWindowID id,
8d772832 76 const wxPoint& pos,
5b2f31eb
VZ
77 const wxSize& size,
78 long style,
ac8d0c11 79 const wxValidator& wxVALIDATOR_PARAM(validator),
8d772832
RD
80 const wxString& name)
81{
5b2f31eb 82 if ( !wxWindow::Create(parent, id, pos, size, style, name) )
d95de154 83 return false;
5b2f31eb 84
8d772832 85#if wxUSE_VALIDATORS
5b2f31eb 86 SetValidator(validator);
8d772832 87#endif
5b2f31eb 88
d95de154 89 return true;
5b2f31eb
VZ
90}
91
92bool wxControl::MSWCreateControl(const wxChar *classname,
93 const wxString& label,
94 const wxPoint& pos,
6dd16e4f 95 const wxSize& size)
5b2f31eb
VZ
96{
97 WXDWORD exstyle;
6dd16e4f 98 WXDWORD msStyle = MSWGetStyle(GetWindowStyle(), &exstyle);
5b2f31eb 99
2eb4c3aa 100 return MSWCreateControl(classname, msStyle, pos, size, label, exstyle);
8d772832
RD
101}
102
222594ea
VZ
103bool wxControl::MSWCreateControl(const wxChar *classname,
104 WXDWORD style,
105 const wxPoint& pos,
106 const wxSize& size,
107 const wxString& label,
2eb4c3aa 108 WXDWORD exstyle)
8d99be5f 109{
222594ea
VZ
110 // if no extended style given, determine it ourselves
111 if ( exstyle == (WXDWORD)-1 )
112 {
fe3d9123 113 exstyle = 0;
65bc172c 114 (void) MSWGetStyle(GetWindowStyle(), &exstyle);
222594ea
VZ
115 }
116
bdf5c30d
VZ
117 // all controls should have this style
118 style |= WS_CHILD;
119
77ffb593 120 // create the control visible if it's currently shown for wxWidgets
bdf5c30d
VZ
121 if ( m_isShown )
122 {
123 style |= WS_VISIBLE;
124 }
3f2711d5 125
7fe985ee
VZ
126 // choose the position for the control: we have a problem with default size
127 // here as we can't calculate the best size before the control exists
128 // (DoGetBestSize() may need to use m_hWnd), so just choose the minimal
129 // possible but non 0 size because 0 window width/height result in problems
130 // elsewhere
d95de154
WS
131 int x = pos.x == wxDefaultCoord ? 0 : pos.x,
132 y = pos.y == wxDefaultCoord ? 0 : pos.y,
7fe985ee
VZ
133 w = size.x == wxDefaultCoord ? 1 : size.x,
134 h = size.y == wxDefaultCoord ? 1 : size.y;
a63cbfa3 135
7c0a023d 136 // ... and adjust it to account for a possible parent frames toolbar
4e9d23cd
VZ
137 AdjustForParentClientOrigin(x, y);
138
8d99be5f
VZ
139 m_hWnd = (WXHWND)::CreateWindowEx
140 (
222594ea 141 exstyle, // extended style
8d99be5f 142 classname, // the kind of control to create
e0a050e3 143 label.wx_str(), // the window name
8d99be5f 144 style, // the window style
a63cbfa3 145 x, y, w, h, // the window position and size
8d99be5f
VZ
146 GetHwndOf(GetParent()), // parent
147 (HMENU)GetId(), // child id
148 wxGetInstance(), // app instance
149 NULL // creation parameters
150 );
151
152 if ( !m_hWnd )
153 {
658252ef
VZ
154#ifdef __WXDEBUG__
155 wxFAIL_MSG(wxString::Format
156 (
157 _T("CreateWindowEx(\"%s\", flags=%08x, ex=%08x) failed"),
f62ff2f1 158 classname, (unsigned int)style, (unsigned int)exstyle
658252ef
VZ
159 ));
160#endif // __WXDEBUG__
8d99be5f 161
d95de154 162 return false;
8d99be5f
VZ
163 }
164
39bc0347
VZ
165 // saving the label in m_labelOrig to return it verbatim
166 // later in GetLabel()
167 m_labelOrig = label;
168
77ffb593 169 // install wxWidgets window proc for this window
8d99be5f
VZ
170 SubclassWin(m_hWnd);
171
34040e31 172 // set up fonts and colours
8d99be5f 173 InheritAttributes();
3c96418b
JG
174 if ( !m_hasFont )
175 {
82c591d7
VZ
176 bool setFont = true;
177
178 wxFont font = GetDefaultAttributes().font;
179
3c96418b
JG
180 // if we set a font for {list,tree}ctrls and the font size is changed in
181 // the display properties then the font size for these controls doesn't
182 // automatically adjust when they receive WM_SETTINGCHANGE
82c591d7
VZ
183
184 // FIXME: replace the dynamic casts with virtual function calls!!
185#if wxUSE_LISTCTRL || wxUSE_TREECTRL
186 bool testFont = false;
187#if wxUSE_LISTCTRL
188 if ( wxDynamicCastThis(wxListCtrl) )
189 testFont = true;
190#endif // wxUSE_LISTCTRL
191#if wxUSE_TREECTRL
192 if ( wxDynamicCastThis(wxTreeCtrl) )
193 testFont = true;
28f9f58c 194#endif // wxUSE_TREECTRL
82c591d7
VZ
195
196 if ( testFont )
3c96418b
JG
197 {
198 // not sure if we need to explicitly set the font here for Win95/NT4
199 // but we definitely can't do it for any newer version
200 // see wxGetCCDefaultFont() in src/msw/settings.cpp for explanation
201 // of why this test works
202
203 // TODO: test Win95/NT4 to see if this is needed or breaks the
204 // font resizing as it does on newer versions
82c591d7 205 if ( font != wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT) )
3c96418b 206 {
82c591d7 207 setFont = false;
3c96418b
JG
208 }
209 }
3c96418b 210#endif // wxUSE_LISTCTRL || wxUSE_TREECTRL
82c591d7
VZ
211
212 if ( setFont )
3c96418b
JG
213 {
214 SetFont(GetDefaultAttributes().font);
215 }
216 }
8d99be5f 217
a63cbfa3 218 // set the size now if no initial size specified
170acdc9 219 SetInitialSize(size);
a63cbfa3 220
d95de154 221 return true;
8d99be5f
VZ
222}
223
34040e31
VZ
224// ----------------------------------------------------------------------------
225// various accessors
226// ----------------------------------------------------------------------------
227
65bc172c
VZ
228wxBorder wxControl::GetDefaultBorder() const
229{
230 // we want to automatically give controls a sunken style (confusingly,
231 // it may not really mean sunken at all as we map it to WS_EX_CLIENTEDGE
232 // which is not sunken at all under Windows XP -- rather, just the default)
edea6281
JS
233#if defined(__POCKETPC__) || defined(__SMARTPHONE__)
234 return wxBORDER_SIMPLE;
235#else
65bc172c 236 return wxBORDER_SUNKEN;
edea6281 237#endif
65bc172c
VZ
238}
239
34040e31
VZ
240WXDWORD wxControl::MSWGetStyle(long style, WXDWORD *exstyle) const
241{
242 long msStyle = wxWindow::MSWGetStyle(style, exstyle);
243
ad02525d 244 if ( AcceptsFocusFromKeyboard() )
34040e31
VZ
245 {
246 msStyle |= WS_TABSTOP;
247 }
248
249 return msStyle;
250}
251
f68586e5 252wxSize wxControl::DoGetBestSize() const
4438caf4
VZ
253{
254 return wxSize(DEFAULT_ITEM_WIDTH, DEFAULT_ITEM_HEIGHT);
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
306// another version for the "composite", i.e. non simple controls
307/* static */ wxVisualAttributes
fbfe58cb 308wxControl::GetCompositeControlsDefaultAttributes(wxWindowVariant WXUNUSED(variant))
34040e31
VZ
309{
310 wxVisualAttributes attrs;
311 attrs.font = wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT);
312 attrs.colFg = wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT);
313 attrs.colBg = wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW);
314
315 return attrs;
316}
317
318// ----------------------------------------------------------------------------
319// message handling
320// ----------------------------------------------------------------------------
321
42e69d6b 322bool wxControl::ProcessCommand(wxCommandEvent& event)
2bda0e17 323{
42e69d6b 324 return GetEventHandler()->ProcessEvent(event);
2bda0e17
KB
325}
326
a23fd0e1
VZ
327bool wxControl::MSWOnNotify(int idCtrl,
328 WXLPARAM lParam,
329 WXLPARAM* result)
2bda0e17 330{
5cb598ae 331 wxEventType eventType wxDUMMY_INITIALIZE(wxEVT_NULL);
b6e5eaa5
VZ
332
333 NMHDR *hdr = (NMHDR*) lParam;
334 switch ( hdr->code )
a23fd0e1
VZ
335 {
336 case NM_CLICK:
337 eventType = wxEVT_COMMAND_LEFT_CLICK;
338 break;
2bda0e17 339
a23fd0e1
VZ
340 case NM_DBLCLK:
341 eventType = wxEVT_COMMAND_LEFT_DCLICK;
342 break;
2bda0e17 343
a23fd0e1
VZ
344 case NM_RCLICK:
345 eventType = wxEVT_COMMAND_RIGHT_CLICK;
346 break;
2bda0e17 347
a23fd0e1
VZ
348 case NM_RDBLCLK:
349 eventType = wxEVT_COMMAND_RIGHT_DCLICK;
350 break;
2bda0e17 351
a23fd0e1
VZ
352 case NM_SETFOCUS:
353 eventType = wxEVT_COMMAND_SET_FOCUS;
354 break;
debe6624 355
a23fd0e1
VZ
356 case NM_KILLFOCUS:
357 eventType = wxEVT_COMMAND_KILL_FOCUS;
358 break;
2bda0e17 359
a23fd0e1
VZ
360 case NM_RETURN:
361 eventType = wxEVT_COMMAND_ENTER;
362 break;
363
364 default:
365 return wxWindow::MSWOnNotify(idCtrl, lParam, result);
366 }
fd3f686c 367
b6e5eaa5 368 wxCommandEvent event(wxEVT_NULL, m_windowId);
2bda0e17 369 event.SetEventType(eventType);
a23fd0e1 370 event.SetEventObject(this);
2bda0e17 371
a23fd0e1 372 return GetEventHandler()->ProcessEvent(event);
2bda0e17
KB
373}
374
2bae4332 375WXHBRUSH wxControl::DoMSWControlColor(WXHDC pDC, wxColour colBg, WXHWND hWnd)
f048e32f 376{
01c500af
VZ
377 HDC hdc = (HDC)pDC;
378 if ( m_hasFgCol )
6ed16512 379 {
01c500af 380 ::SetTextColor(hdc, wxColourToRGB(GetForegroundColour()));
6ed16512 381 }
01c500af 382
c3732409
VZ
383 WXHBRUSH hbr = 0;
384 if ( !colBg.Ok() )
385 {
2bae4332 386 hbr = MSWGetBgBrush(pDC, hWnd);
c3732409
VZ
387
388 // if the control doesn't have any bg colour, foreground colour will be
389 // ignored as the return value would be 0 -- so forcefully give it a
390 // non default background brush in this case
391 if ( !hbr && m_hasFgCol )
392 colBg = GetBackgroundColour();
393 }
394
d1a47dfe 395 // use the background colour override if a valid colour is given
5c836c46 396 if ( colBg.Ok() )
f048e32f 397 {
bcc4aa97
VZ
398 ::SetBkColor(hdc, wxColourToRGB(colBg));
399
d1a47dfe 400 // draw children with the same colour as the parent
5c836c46 401 wxBrush *brush = wxTheBrushList->FindOrCreateBrush(colBg, wxSOLID);
01c500af 402
d1a47dfe 403 hbr = (WXHBRUSH)brush->GetResourceHandle();
3d2f4457 404
d1a47dfe 405 }
f048e32f 406
155acb0c
JS
407 // if we use custom background, we should set foreground ourselves too
408 if ( hbr && !m_hasFgCol )
409 {
410 ::SetTextColor(hdc, ::GetSysColor(COLOR_WINDOWTEXT));
411 }
412 //else: already set above
413
d1a47dfe 414 return hbr;
5c836c46
VZ
415}
416
2bae4332 417WXHBRUSH wxControl::MSWControlColor(WXHDC pDC, WXHWND hWnd)
5c836c46 418{
c3732409
VZ
419 wxColour colBg;
420
421 if ( HasTransparentBackground() )
422 ::SetBkMode((HDC)pDC, TRANSPARENT);
423 else // if the control is opaque it shouldn't use the parents background
424 colBg = GetBackgroundColour();
01c500af 425
2bae4332 426 return DoMSWControlColor(pDC, colBg, hWnd);
5c836c46
VZ
427}
428
429WXHBRUSH wxControl::MSWControlColorDisabled(WXHDC pDC)
430{
dd12ce22 431 return DoMSWControlColor(pDC,
2bae4332
VZ
432 wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE),
433 GetHWND());
f048e32f
VZ
434}
435
42e69d6b
VZ
436// ---------------------------------------------------------------------------
437// global functions
438// ---------------------------------------------------------------------------
2bda0e17 439
34040e31
VZ
440// this is used in radiobox.cpp and slider95.cpp and should be removed as soon
441// as it is not needed there any more!
442//
42e69d6b
VZ
443// Call this repeatedly for several wnds to find the overall size
444// of the widget.
d95de154 445// Call it initially with wxDefaultCoord for all values in rect.
42e69d6b
VZ
446// Keep calling for other widgets, and rect will be modified
447// to calculate largest bounding rectangle.
448void wxFindMaxSize(WXHWND wnd, RECT *rect)
2bda0e17 449{
42e69d6b
VZ
450 int left = rect->left;
451 int right = rect->right;
452 int top = rect->top;
453 int bottom = rect->bottom;
2bda0e17 454
42e69d6b 455 GetWindowRect((HWND) wnd, rect);
2bda0e17 456
42e69d6b
VZ
457 if (left < 0)
458 return;
2bda0e17 459
42e69d6b
VZ
460 if (left < rect->left)
461 rect->left = left;
2bda0e17 462
42e69d6b
VZ
463 if (right > rect->right)
464 rect->right = right;
2bda0e17 465
42e69d6b
VZ
466 if (top < rect->top)
467 rect->top = top;
2bda0e17 468
42e69d6b
VZ
469 if (bottom > rect->bottom)
470 rect->bottom = bottom;
2bda0e17
KB
471}
472
1e6feb95 473#endif // wxUSE_CONTROLS