]> git.saurik.com Git - wxWidgets.git/blob - src/msw/control.cpp
a020137ad7f7367a68f625f48d1704ac40256810
[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 // notice the apparently unnecessary label.empty() test: without it wse get
143 // crashes when creating controls (notebook to be precise) with empty
144 // labels as for some reason Windows looks not only at first but also at
145 // the second byte of the string which is not NUL in the buffer pointed to
146 // by label.c_str() and if the buffer contains "00 cc cc cc ..." (this is
147 // what happens in debug build where 0xcc is used as uninitialized memory
148 // marker) it creates a window not with empty label but with label
149 // consisting of many Unicode (!) 0xcccc characters
150 //
151 // this is not fatal in itself, but becomes so when wxGetWindowText() is
152 // called because GetWindowTextA() overwrites ASCII buffer with Unicode (!)
153 // characters without any qualms and so crashes the program...
154 m_hWnd = (WXHWND)::CreateWindowEx
155 (
156 exstyle, // extended style
157 classname, // the kind of control to create
158 label.empty() // the window name
159 ? _T("\0")
160 : label.c_str(),
161 style, // the window style
162 x, y, w, h, // the window position and size
163 GetHwndOf(GetParent()), // parent
164 (HMENU)GetId(), // child id
165 wxGetInstance(), // app instance
166 NULL // creation parameters
167 );
168
169 if ( !m_hWnd )
170 {
171 #ifdef __WXDEBUG__
172 wxFAIL_MSG(wxString::Format
173 (
174 _T("CreateWindowEx(\"%s\", flags=%08x, ex=%08x) failed"),
175 classname, (unsigned int)style, (unsigned int)exstyle
176 ));
177 #endif // __WXDEBUG__
178
179 return false;
180 }
181
182 // install wxWidgets window proc for this window
183 SubclassWin(m_hWnd);
184
185 // set up fonts and colours
186 InheritAttributes();
187 if (!m_hasFont)
188 SetFont(GetDefaultAttributes().font);
189
190 // set the size now if no initial size specified
191 SetInitialBestSize(size);
192
193 return true;
194 }
195
196 // ----------------------------------------------------------------------------
197 // various accessors
198 // ----------------------------------------------------------------------------
199
200 wxBorder wxControl::GetDefaultBorder() const
201 {
202 // we want to automatically give controls a sunken style (confusingly,
203 // it may not really mean sunken at all as we map it to WS_EX_CLIENTEDGE
204 // which is not sunken at all under Windows XP -- rather, just the default)
205 #if defined(__POCKETPC__) || defined(__SMARTPHONE__)
206 return wxBORDER_SIMPLE;
207 #else
208 return wxBORDER_SUNKEN;
209 #endif
210 }
211
212 WXDWORD wxControl::MSWGetStyle(long style, WXDWORD *exstyle) const
213 {
214 long msStyle = wxWindow::MSWGetStyle(style, exstyle);
215
216 if ( AcceptsFocus() )
217 {
218 msStyle |= WS_TABSTOP;
219 }
220
221 return msStyle;
222 }
223
224 wxSize wxControl::DoGetBestSize() const
225 {
226 return wxSize(DEFAULT_ITEM_WIDTH, DEFAULT_ITEM_HEIGHT);
227 }
228
229 // This is a helper for all wxControls made with UPDOWN native control.
230 // In wxMSW it was only wxSpinCtrl derived from wxSpinButton but in
231 // WinCE of Smartphones this happens also for native wxTextCtrl,
232 // wxChoice and others.
233 wxSize wxControl::GetBestSpinnerSize(const bool is_vertical) const
234 {
235 // take size according to layout
236 wxSize bestSize(
237 #if defined(__SMARTPHONE__) && defined(__WXWINCE__)
238 0,GetCharHeight()
239 #else
240 ::GetSystemMetrics(is_vertical ? SM_CXVSCROLL : SM_CXHSCROLL),
241 ::GetSystemMetrics(is_vertical ? SM_CYVSCROLL : SM_CYHSCROLL)
242 #endif
243 );
244
245 // correct size as for undocumented MSW variants cases (WinCE and perhaps others)
246 if (bestSize.x==0)
247 bestSize.x = bestSize.y;
248 if (bestSize.y==0)
249 bestSize.y = bestSize.x;
250
251 // double size according to layout
252 if (is_vertical)
253 bestSize.y *= 2;
254 else
255 bestSize.x *= 2;
256
257 return bestSize;
258 }
259
260 /* static */ wxVisualAttributes
261 wxControl::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant))
262 {
263 wxVisualAttributes attrs;
264
265 // old school (i.e. not "common") controls use the standard dialog font
266 // by default
267 attrs.font = wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT);
268
269 // most, or at least many, of the controls use the same colours as the
270 // buttons -- others will have to override this (and possibly simply call
271 // GetCompositeControlsDefaultAttributes() from their versions)
272 attrs.colFg = wxSystemSettings::GetColour(wxSYS_COLOUR_BTNTEXT);
273 attrs.colBg = wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE);
274
275 return attrs;
276 }
277
278 // another version for the "composite", i.e. non simple controls
279 /* static */ wxVisualAttributes
280 wxControl::GetCompositeControlsDefaultAttributes(wxWindowVariant WXUNUSED(variant))
281 {
282 wxVisualAttributes attrs;
283 attrs.font = wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT);
284 attrs.colFg = wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT);
285 attrs.colBg = wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW);
286
287 return attrs;
288 }
289
290 // ----------------------------------------------------------------------------
291 // message handling
292 // ----------------------------------------------------------------------------
293
294 bool wxControl::ProcessCommand(wxCommandEvent& event)
295 {
296 return GetEventHandler()->ProcessEvent(event);
297 }
298
299 #ifdef __WIN95__
300 bool wxControl::MSWOnNotify(int idCtrl,
301 WXLPARAM lParam,
302 WXLPARAM* result)
303 {
304 wxEventType eventType wxDUMMY_INITIALIZE(wxEVT_NULL);
305
306 NMHDR *hdr = (NMHDR*) lParam;
307 switch ( hdr->code )
308 {
309 case NM_CLICK:
310 eventType = wxEVT_COMMAND_LEFT_CLICK;
311 break;
312
313 case NM_DBLCLK:
314 eventType = wxEVT_COMMAND_LEFT_DCLICK;
315 break;
316
317 case NM_RCLICK:
318 eventType = wxEVT_COMMAND_RIGHT_CLICK;
319 break;
320
321 case NM_RDBLCLK:
322 eventType = wxEVT_COMMAND_RIGHT_DCLICK;
323 break;
324
325 case NM_SETFOCUS:
326 eventType = wxEVT_COMMAND_SET_FOCUS;
327 break;
328
329 case NM_KILLFOCUS:
330 eventType = wxEVT_COMMAND_KILL_FOCUS;
331 break;
332
333 case NM_RETURN:
334 eventType = wxEVT_COMMAND_ENTER;
335 break;
336
337 default:
338 return wxWindow::MSWOnNotify(idCtrl, lParam, result);
339 }
340
341 wxCommandEvent event(wxEVT_NULL, m_windowId);
342 event.SetEventType(eventType);
343 event.SetEventObject(this);
344
345 return GetEventHandler()->ProcessEvent(event);
346 }
347 #endif // Win95
348
349 WXHBRUSH wxControl::DoMSWControlColor(WXHDC pDC, wxColour colBg, WXHWND hWnd)
350 {
351 HDC hdc = (HDC)pDC;
352 if ( m_hasFgCol )
353 {
354 ::SetTextColor(hdc, wxColourToRGB(GetForegroundColour()));
355 }
356
357 WXHBRUSH hbr = 0;
358 if ( !colBg.Ok() )
359 {
360 hbr = MSWGetBgBrush(pDC, hWnd);
361
362 // if the control doesn't have any bg colour, foreground colour will be
363 // ignored as the return value would be 0 -- so forcefully give it a
364 // non default background brush in this case
365 if ( !hbr && m_hasFgCol )
366 colBg = GetBackgroundColour();
367 }
368
369 // use the background colour override if a valid colour is given
370 if ( colBg.Ok() )
371 {
372 ::SetBkColor(hdc, wxColourToRGB(colBg));
373
374 // draw children with the same colour as the parent
375 wxBrush *brush = wxTheBrushList->FindOrCreateBrush(colBg, wxSOLID);
376
377 hbr = (WXHBRUSH)brush->GetResourceHandle();
378
379 // if we use custom background, we should set foreground ourselves too
380 if ( !m_hasFgCol )
381 {
382 ::SetTextColor(hdc, ::GetSysColor(COLOR_WINDOWTEXT));
383 }
384 //else: already set above
385 }
386
387 return hbr;
388 }
389
390 WXHBRUSH wxControl::MSWControlColor(WXHDC pDC, WXHWND hWnd)
391 {
392 wxColour colBg;
393
394 if ( HasTransparentBackground() )
395 ::SetBkMode((HDC)pDC, TRANSPARENT);
396 else // if the control is opaque it shouldn't use the parents background
397 colBg = GetBackgroundColour();
398
399 return DoMSWControlColor(pDC, colBg, hWnd);
400 }
401
402 WXHBRUSH wxControl::MSWControlColorDisabled(WXHDC pDC)
403 {
404 return DoMSWControlColor(pDC,
405 wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE),
406 GetHWND());
407 }
408
409 // ---------------------------------------------------------------------------
410 // global functions
411 // ---------------------------------------------------------------------------
412
413 // this is used in radiobox.cpp and slider95.cpp and should be removed as soon
414 // as it is not needed there any more!
415 //
416 // Call this repeatedly for several wnds to find the overall size
417 // of the widget.
418 // Call it initially with wxDefaultCoord for all values in rect.
419 // Keep calling for other widgets, and rect will be modified
420 // to calculate largest bounding rectangle.
421 void wxFindMaxSize(WXHWND wnd, RECT *rect)
422 {
423 int left = rect->left;
424 int right = rect->right;
425 int top = rect->top;
426 int bottom = rect->bottom;
427
428 GetWindowRect((HWND) wnd, rect);
429
430 if (left < 0)
431 return;
432
433 if (left < rect->left)
434 rect->left = left;
435
436 if (right > rect->right)
437 rect->right = right;
438
439 if (top < rect->top)
440 rect->top = top;
441
442 if (bottom > rect->bottom)
443 rect->bottom = bottom;
444 }
445
446 #endif // wxUSE_CONTROLS