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