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