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