]> git.saurik.com Git - wxWidgets.git/blame - src/msw/control.cpp
rewrote some sizer code using wxSizerFlags
[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
14f355c2 20#if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
1e6feb95 21 #pragma implementation "control.h"
2bda0e17
KB
22#endif
23
24// For compilers that support precompilation, includes "wx.h".
25#include "wx/wxprec.h"
26
27#ifdef __BORLANDC__
1e6feb95 28 #pragma hdrstop
2bda0e17
KB
29#endif
30
1e6feb95
VZ
31#if wxUSE_CONTROLS
32
2bda0e17 33#ifndef WX_PRECOMP
1e6feb95
VZ
34 #include "wx/event.h"
35 #include "wx/app.h"
36 #include "wx/dcclient.h"
37 #include "wx/log.h"
34040e31 38 #include "wx/settings.h"
2bda0e17
KB
39#endif
40
2432b92d
JS
41#include "wx/control.h"
42
2bda0e17
KB
43#include "wx/msw/private.h"
44
b39dbf34 45#if defined(__WIN95__) && !(defined(__GNUWIN32_OLD__) && !defined(__CYGWIN10__))
c42404a5 46 #include <commctrl.h>
2bda0e17
KB
47#endif
48
34040e31
VZ
49// ----------------------------------------------------------------------------
50// wxWin macros
51// ----------------------------------------------------------------------------
52
2bda0e17
KB
53IMPLEMENT_ABSTRACT_CLASS(wxControl, wxWindow)
54
55BEGIN_EVENT_TABLE(wxControl, wxWindow)
a23fd0e1 56 EVT_ERASE_BACKGROUND(wxControl::OnEraseBackground)
2bda0e17 57END_EVENT_TABLE()
2bda0e17 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
4e9d23cd 128 // choose the position for the control
d95de154
WS
129 int x = pos.x == wxDefaultCoord ? 0 : pos.x,
130 y = pos.y == wxDefaultCoord ? 0 : pos.y,
131 w = size.x == wxDefaultCoord ? 0 : size.x,
132 h = size.y == wxDefaultCoord ? 0 : size.y;
a63cbfa3 133
7c0a023d 134 // ... and adjust it to account for a possible parent frames toolbar
4e9d23cd
VZ
135 AdjustForParentClientOrigin(x, y);
136
8d99be5f
VZ
137 m_hWnd = (WXHWND)::CreateWindowEx
138 (
222594ea 139 exstyle, // extended style
8d99be5f 140 classname, // the kind of control to create
222594ea 141 label, // the window name
8d99be5f 142 style, // the window style
a63cbfa3 143 x, y, w, h, // the window position and size
8d99be5f
VZ
144 GetHwndOf(GetParent()), // parent
145 (HMENU)GetId(), // child id
146 wxGetInstance(), // app instance
147 NULL // creation parameters
148 );
149
150 if ( !m_hWnd )
151 {
f6bcfd97 152 wxLogDebug(wxT("Failed to create a control of class '%s'"), classname);
90af2514 153 wxFAIL_MSG(_T("something is very wrong, CreateWindowEx failed"));
8d99be5f 154
d95de154 155 return false;
8d99be5f
VZ
156 }
157
3f2711d5
VZ
158#if wxUSE_CTL3D
159 if ( want3D )
160 {
161 Ctl3dSubclassCtl(GetHwnd());
d95de154 162 m_useCtl3D = true;
3f2711d5
VZ
163 }
164#endif // wxUSE_CTL3D
165
77ffb593 166 // install wxWidgets window proc for this window
8d99be5f
VZ
167 SubclassWin(m_hWnd);
168
34040e31 169 // set up fonts and colours
8d99be5f 170 InheritAttributes();
e0176dd9
VZ
171 if (!m_hasFont)
172 SetFont(GetDefaultAttributes().font);
8d99be5f 173
a63cbfa3 174 // set the size now if no initial size specified
085dd1e9 175 SetInitialBestSize(size);
a63cbfa3 176
d95de154 177 return true;
8d99be5f
VZ
178}
179
34040e31
VZ
180// ----------------------------------------------------------------------------
181// various accessors
182// ----------------------------------------------------------------------------
183
65bc172c
VZ
184wxBorder wxControl::GetDefaultBorder() const
185{
186 // we want to automatically give controls a sunken style (confusingly,
187 // it may not really mean sunken at all as we map it to WS_EX_CLIENTEDGE
188 // which is not sunken at all under Windows XP -- rather, just the default)
189 return wxBORDER_SUNKEN;
190}
191
34040e31
VZ
192WXDWORD wxControl::MSWGetStyle(long style, WXDWORD *exstyle) const
193{
194 long msStyle = wxWindow::MSWGetStyle(style, exstyle);
195
196 if ( AcceptsFocus() )
197 {
198 msStyle |= WS_TABSTOP;
199 }
200
201 return msStyle;
202}
203
f68586e5 204wxSize wxControl::DoGetBestSize() const
4438caf4
VZ
205{
206 return wxSize(DEFAULT_ITEM_WIDTH, DEFAULT_ITEM_HEIGHT);
207}
208
4bf45c9e
WS
209// This is a helper for all wxControls made with UPDOWN native control.
210// In wxMSW it was only wxSpinCtrl derived from wxSpinButton but in
211// WinCE of Smartphones this happens also for native wxTextCtrl,
212// wxChoice and others.
213wxSize wxControl::GetBestSpinerSize(const bool is_vertical) const
214{
b081046a 215 // take size according to layout
1550d5f8
WS
216 wxSize bestSize(
217#ifdef __SMARTPHONE__
218 0,GetCharHeight()
ada9a70e 219#else // !__SMARTPHONE__
1550d5f8
WS
220 GetSystemMetrics(is_vertical ? SM_CXVSCROLL : SM_CXHSCROLL),
221 GetSystemMetrics(is_vertical ? SM_CYVSCROLL : SM_CYHSCROLL)
ada9a70e 222#endif // __SMARTPHONE__/!__SMARTPHONE__
1550d5f8 223 );
b081046a
WS
224
225 // correct size as for undocumented MSW variants cases (WinCE and perhaps others)
226 if (bestSize.x==0)
227 bestSize.x = bestSize.y;
228 if (bestSize.y==0)
229 bestSize.y = bestSize.x;
230
231 // double size according to layout
4bf45c9e 232 if (is_vertical)
b081046a 233 bestSize.y *= 2;
4bf45c9e 234 else
b081046a
WS
235 bestSize.x *= 2;
236
237 return bestSize;
4bf45c9e
WS
238}
239
34040e31
VZ
240/* static */ wxVisualAttributes
241wxControl::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant))
242{
243 wxVisualAttributes attrs;
244
245 // old school (i.e. not "common") controls use the standard dialog font
246 // by default
247 attrs.font = wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT);
248
249 // most, or at least many, of the controls use the same colours as the
250 // buttons -- others will have to override this (and possibly simply call
251 // GetCompositeControlsDefaultAttributes() from their versions)
252 attrs.colFg = wxSystemSettings::GetColour(wxSYS_COLOUR_BTNTEXT);
253 attrs.colBg = wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE);
254
255 return attrs;
256}
257
258// another version for the "composite", i.e. non simple controls
259/* static */ wxVisualAttributes
fbfe58cb 260wxControl::GetCompositeControlsDefaultAttributes(wxWindowVariant WXUNUSED(variant))
34040e31
VZ
261{
262 wxVisualAttributes attrs;
263 attrs.font = wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT);
264 attrs.colFg = wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT);
265 attrs.colBg = wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW);
266
267 return attrs;
268}
269
270// ----------------------------------------------------------------------------
271// message handling
272// ----------------------------------------------------------------------------
273
42e69d6b 274bool wxControl::ProcessCommand(wxCommandEvent& event)
2bda0e17 275{
42e69d6b 276 return GetEventHandler()->ProcessEvent(event);
2bda0e17
KB
277}
278
a23fd0e1
VZ
279#ifdef __WIN95__
280bool wxControl::MSWOnNotify(int idCtrl,
281 WXLPARAM lParam,
282 WXLPARAM* result)
2bda0e17 283{
5cb598ae 284 wxEventType eventType wxDUMMY_INITIALIZE(wxEVT_NULL);
b6e5eaa5
VZ
285
286 NMHDR *hdr = (NMHDR*) lParam;
287 switch ( hdr->code )
a23fd0e1
VZ
288 {
289 case NM_CLICK:
290 eventType = wxEVT_COMMAND_LEFT_CLICK;
291 break;
2bda0e17 292
a23fd0e1
VZ
293 case NM_DBLCLK:
294 eventType = wxEVT_COMMAND_LEFT_DCLICK;
295 break;
2bda0e17 296
a23fd0e1
VZ
297 case NM_RCLICK:
298 eventType = wxEVT_COMMAND_RIGHT_CLICK;
299 break;
2bda0e17 300
a23fd0e1
VZ
301 case NM_RDBLCLK:
302 eventType = wxEVT_COMMAND_RIGHT_DCLICK;
303 break;
2bda0e17 304
a23fd0e1
VZ
305 case NM_SETFOCUS:
306 eventType = wxEVT_COMMAND_SET_FOCUS;
307 break;
debe6624 308
a23fd0e1
VZ
309 case NM_KILLFOCUS:
310 eventType = wxEVT_COMMAND_KILL_FOCUS;
311 break;
2bda0e17 312
a23fd0e1
VZ
313 case NM_RETURN:
314 eventType = wxEVT_COMMAND_ENTER;
315 break;
316
317 default:
318 return wxWindow::MSWOnNotify(idCtrl, lParam, result);
319 }
fd3f686c 320
b6e5eaa5 321 wxCommandEvent event(wxEVT_NULL, m_windowId);
2bda0e17 322 event.SetEventType(eventType);
a23fd0e1 323 event.SetEventObject(this);
2bda0e17 324
a23fd0e1 325 return GetEventHandler()->ProcessEvent(event);
2bda0e17 326}
a23fd0e1 327#endif // Win95
2bda0e17 328
2bda0e17
KB
329void wxControl::OnEraseBackground(wxEraseEvent& event)
330{
5bd3a2da
VZ
331 // notice that this 'dumb' implementation may cause flicker for some of the
332 // controls in which case they should intercept wxEraseEvent and process it
333 // themselves somehow
42e69d6b
VZ
334
335 RECT rect;
3f2711d5 336 ::GetClientRect(GetHwnd(), &rect);
42e69d6b 337
3f2711d5 338 HBRUSH hBrush = ::CreateSolidBrush(wxColourToRGB(GetBackgroundColour()));
42e69d6b 339
3f2711d5 340 HDC hdc = GetHdcOf((*event.GetDC()));
4676948b
JS
341
342#ifndef __WXWINCE__
3f2711d5 343 int mode = ::SetMapMode(hdc, MM_TEXT);
4676948b 344#endif
3f2711d5
VZ
345
346 ::FillRect(hdc, &rect, hBrush);
42e69d6b 347 ::DeleteObject(hBrush);
4676948b
JS
348
349#ifndef __WXWINCE__
3f2711d5 350 ::SetMapMode(hdc, mode);
4676948b 351#endif
2bda0e17
KB
352}
353
33ac7e6f 354WXHBRUSH wxControl::OnCtlColor(WXHDC pDC, WXHWND WXUNUSED(pWnd), WXUINT WXUNUSED(nCtlColor),
788722ac
JS
355#if wxUSE_CTL3D
356 WXUINT message,
357 WXWPARAM wParam,
358 WXLPARAM lParam
359#else
33ac7e6f
KB
360 WXUINT WXUNUSED(message),
361 WXWPARAM WXUNUSED(wParam),
788722ac
JS
362 WXLPARAM WXUNUSED(lParam)
363#endif
364 )
f048e32f
VZ
365{
366#if wxUSE_CTL3D
367 if ( m_useCtl3D )
368 {
369 HBRUSH hbrush = Ctl3dCtlColorEx(message, wParam, lParam);
370 return (WXHBRUSH) hbrush;
371 }
372#endif // wxUSE_CTL3D
373
374 HDC hdc = (HDC)pDC;
f6bcfd97
BP
375 wxColour colBack = GetBackgroundColour();
376
f048e32f
VZ
377 ::SetBkColor(hdc, wxColourToRGB(colBack));
378 ::SetTextColor(hdc, wxColourToRGB(GetForegroundColour()));
379
380 wxBrush *brush = wxTheBrushList->FindOrCreateBrush(colBack, wxSOLID);
381
382 return (WXHBRUSH)brush->GetResourceHandle();
383}
384
42e69d6b
VZ
385// ---------------------------------------------------------------------------
386// global functions
387// ---------------------------------------------------------------------------
2bda0e17 388
34040e31
VZ
389// this is used in radiobox.cpp and slider95.cpp and should be removed as soon
390// as it is not needed there any more!
391//
42e69d6b
VZ
392// Call this repeatedly for several wnds to find the overall size
393// of the widget.
d95de154 394// Call it initially with wxDefaultCoord for all values in rect.
42e69d6b
VZ
395// Keep calling for other widgets, and rect will be modified
396// to calculate largest bounding rectangle.
397void wxFindMaxSize(WXHWND wnd, RECT *rect)
2bda0e17 398{
42e69d6b
VZ
399 int left = rect->left;
400 int right = rect->right;
401 int top = rect->top;
402 int bottom = rect->bottom;
2bda0e17 403
42e69d6b 404 GetWindowRect((HWND) wnd, rect);
2bda0e17 405
42e69d6b
VZ
406 if (left < 0)
407 return;
2bda0e17 408
42e69d6b
VZ
409 if (left < rect->left)
410 rect->left = left;
2bda0e17 411
42e69d6b
VZ
412 if (right > rect->right)
413 rect->right = right;
2bda0e17 414
42e69d6b
VZ
415 if (top < rect->top)
416 rect->top = top;
2bda0e17 417
42e69d6b
VZ
418 if (bottom > rect->bottom)
419 rect->bottom = bottom;
2bda0e17
KB
420}
421
1e6feb95 422#endif // wxUSE_CONTROLS