]> git.saurik.com Git - wxWidgets.git/blob - src/msw/control.cpp
cab5151e1cf0bdf551ff4e71d1c7986b9c1748cc
[wxWidgets.git] / src / msw / control.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/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 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
22
23 #ifdef __BORLANDC__
24 #pragma hdrstop
25 #endif
26
27 #if wxUSE_CONTROLS
28
29 #include "wx/control.h"
30
31 #ifndef WX_PRECOMP
32 #include "wx/msw/wrapcctl.h" // include <commctrl.h> "properly"
33 #include "wx/event.h"
34 #include "wx/app.h"
35 #include "wx/dcclient.h"
36 #include "wx/log.h"
37 #include "wx/settings.h"
38 #include "wx/ctrlsub.h"
39 #endif
40
41 #if wxUSE_LISTCTRL
42 #include "wx/listctrl.h"
43 #endif // wxUSE_LISTCTRL
44
45 #if wxUSE_TREECTRL
46 #include "wx/treectrl.h"
47 #endif // wxUSE_TREECTRL
48
49 #include "wx/msw/private.h"
50 #include "wx/msw/uxtheme.h"
51
52 // ----------------------------------------------------------------------------
53 // wxWin macros
54 // ----------------------------------------------------------------------------
55
56 IMPLEMENT_ABSTRACT_CLASS(wxControl, wxWindow)
57
58 // ============================================================================
59 // wxControl implementation
60 // ============================================================================
61
62 // ----------------------------------------------------------------------------
63 // control window creation
64 // ----------------------------------------------------------------------------
65
66 bool wxControl::Create(wxWindow *parent,
67 wxWindowID id,
68 const wxPoint& pos,
69 const wxSize& size,
70 long style,
71 const wxValidator& wxVALIDATOR_PARAM(validator),
72 const wxString& name)
73 {
74 if ( !wxWindow::Create(parent, id, pos, size, style, name) )
75 return false;
76
77 #if wxUSE_VALIDATORS
78 SetValidator(validator);
79 #endif
80
81 return true;
82 }
83
84 bool wxControl::MSWCreateControl(const wxChar *classname,
85 const wxString& label,
86 const wxPoint& pos,
87 const wxSize& size)
88 {
89 WXDWORD exstyle;
90 WXDWORD msStyle = MSWGetStyle(GetWindowStyle(), &exstyle);
91
92 return MSWCreateControl(classname, msStyle, pos, size, label, exstyle);
93 }
94
95 bool wxControl::MSWCreateControl(const wxChar *classname,
96 WXDWORD style,
97 const wxPoint& pos,
98 const wxSize& size,
99 const wxString& label,
100 WXDWORD exstyle)
101 {
102 // if no extended style given, determine it ourselves
103 if ( exstyle == (WXDWORD)-1 )
104 {
105 exstyle = 0;
106 (void) MSWGetStyle(GetWindowStyle(), &exstyle);
107 }
108
109 // all controls should have this style
110 style |= WS_CHILD;
111
112 // create the control visible if it's currently shown for wxWidgets
113 if ( m_isShown )
114 {
115 style |= WS_VISIBLE;
116 }
117
118 // choose the position for the control: we have a problem with default size
119 // here as we can't calculate the best size before the control exists
120 // (DoGetBestSize() may need to use m_hWnd), so just choose the minimal
121 // possible but non 0 size because 0 window width/height result in problems
122 // elsewhere
123 int x = pos.x == wxDefaultCoord ? 0 : pos.x,
124 y = pos.y == wxDefaultCoord ? 0 : pos.y,
125 w = size.x == wxDefaultCoord ? 1 : size.x,
126 h = size.y == wxDefaultCoord ? 1 : size.y;
127
128 // ... and adjust it to account for a possible parent frames toolbar
129 AdjustForParentClientOrigin(x, y);
130
131 m_hWnd = (WXHWND)::CreateWindowEx
132 (
133 exstyle, // extended style
134 classname, // the kind of control to create
135 label.wx_str(), // the window name
136 style, // the window style
137 x, y, w, h, // the window position and size
138 GetHwndOf(GetParent()), // parent
139 (HMENU)wxUIntToPtr(GetId()), // child id
140 wxGetInstance(), // app instance
141 NULL // creation parameters
142 );
143
144 if ( !m_hWnd )
145 {
146 wxLogLastError(wxString::Format
147 (
148 wxT("CreateWindowEx(\"%s\", flags=%08lx, ex=%08lx)"),
149 classname, style, exstyle
150 ));
151
152 return false;
153 }
154
155 #if !wxUSE_UNICODE
156 // Text labels starting with the character 0xff (which is a valid character
157 // in many code pages) don't appear correctly as CreateWindowEx() has some
158 // special treatment for this case, apparently the strings starting with -1
159 // are not really strings but something called "ordinals". There is no
160 // documentation about it but the fact is that the label gets mangled or
161 // not displayed at all if we don't do this, see #9572.
162 //
163 // Notice that 0xffff is not a valid Unicode character so the problem
164 // doesn't arise in Unicode build.
165 if ( !label.empty() && label[0] == -1 )
166 ::SetWindowText(GetHwnd(), label.wx_str());
167 #endif // !wxUSE_UNICODE
168
169 // saving the label in m_labelOrig to return it verbatim
170 // later in GetLabel()
171 m_labelOrig = label;
172
173 // install wxWidgets window proc for this window
174 SubclassWin(m_hWnd);
175
176 // set up fonts and colours
177 InheritAttributes();
178 if ( !m_hasFont )
179 {
180 bool setFont = true;
181
182 wxFont font = GetDefaultAttributes().font;
183
184 // if we set a font for {list,tree}ctrls and the font size is changed in
185 // the display properties then the font size for these controls doesn't
186 // automatically adjust when they receive WM_SETTINGCHANGE
187
188 // FIXME: replace the dynamic casts with virtual function calls!!
189 #if wxUSE_LISTCTRL || wxUSE_TREECTRL
190 bool testFont = false;
191 #if wxUSE_LISTCTRL
192 if ( wxDynamicCastThis(wxListCtrl) )
193 testFont = true;
194 #endif // wxUSE_LISTCTRL
195 #if wxUSE_TREECTRL
196 if ( wxDynamicCastThis(wxTreeCtrl) )
197 testFont = true;
198 #endif // wxUSE_TREECTRL
199
200 if ( testFont )
201 {
202 // not sure if we need to explicitly set the font here for Win95/NT4
203 // but we definitely can't do it for any newer version
204 // see wxGetCCDefaultFont() in src/msw/settings.cpp for explanation
205 // of why this test works
206
207 // TODO: test Win95/NT4 to see if this is needed or breaks the
208 // font resizing as it does on newer versions
209 if ( font != wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT) )
210 {
211 setFont = false;
212 }
213 }
214 #endif // wxUSE_LISTCTRL || wxUSE_TREECTRL
215
216 if ( setFont )
217 {
218 SetFont(GetDefaultAttributes().font);
219 }
220 }
221
222 // set the size now if no initial size specified
223 SetInitialSize(size);
224
225 return true;
226 }
227
228 // ----------------------------------------------------------------------------
229 // various accessors
230 // ----------------------------------------------------------------------------
231
232 WXDWORD wxControl::MSWGetStyle(long style, WXDWORD *exstyle) const
233 {
234 long msStyle = wxWindow::MSWGetStyle(style, exstyle);
235
236 if ( AcceptsFocusFromKeyboard() )
237 {
238 msStyle |= WS_TABSTOP;
239 }
240
241 return msStyle;
242 }
243
244 wxSize wxControl::DoGetBestSize() const
245 {
246 if (m_windowSizer)
247 return wxControlBase::DoGetBestSize();
248
249 return wxSize(DEFAULT_ITEM_WIDTH, DEFAULT_ITEM_HEIGHT);
250 }
251
252 wxBorder wxControl::GetDefaultBorder() const
253 {
254 return wxControlBase::GetDefaultBorder();
255 }
256
257 // This is a helper for all wxControls made with UPDOWN native control.
258 // In wxMSW it was only wxSpinCtrl derived from wxSpinButton but in
259 // WinCE of Smartphones this happens also for native wxTextCtrl,
260 // wxChoice and others.
261 wxSize wxControl::GetBestSpinnerSize(const bool is_vertical) const
262 {
263 // take size according to layout
264 wxSize bestSize(
265 #if defined(__SMARTPHONE__) && defined(__WXWINCE__)
266 0,GetCharHeight()
267 #else
268 ::GetSystemMetrics(is_vertical ? SM_CXVSCROLL : SM_CXHSCROLL),
269 ::GetSystemMetrics(is_vertical ? SM_CYVSCROLL : SM_CYHSCROLL)
270 #endif
271 );
272
273 // correct size as for undocumented MSW variants cases (WinCE and perhaps others)
274 if (bestSize.x==0)
275 bestSize.x = bestSize.y;
276 if (bestSize.y==0)
277 bestSize.y = bestSize.x;
278
279 // double size according to layout
280 if (is_vertical)
281 bestSize.y *= 2;
282 else
283 bestSize.x *= 2;
284
285 return bestSize;
286 }
287
288 /* static */ wxVisualAttributes
289 wxControl::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant))
290 {
291 wxVisualAttributes attrs;
292
293 // old school (i.e. not "common") controls use the standard dialog font
294 // by default
295 attrs.font = wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT);
296
297 // most, or at least many, of the controls use the same colours as the
298 // buttons -- others will have to override this (and possibly simply call
299 // GetCompositeControlsDefaultAttributes() from their versions)
300 attrs.colFg = wxSystemSettings::GetColour(wxSYS_COLOUR_BTNTEXT);
301 attrs.colBg = wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE);
302
303 return attrs;
304 }
305
306 // another version for the "composite", i.e. non simple controls
307 /* static */ wxVisualAttributes
308 wxControl::GetCompositeControlsDefaultAttributes(wxWindowVariant WXUNUSED(variant))
309 {
310 wxVisualAttributes attrs;
311 attrs.font = wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT);
312 attrs.colFg = wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT);
313 attrs.colBg = wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW);
314
315 return attrs;
316 }
317
318 // ----------------------------------------------------------------------------
319 // message handling
320 // ----------------------------------------------------------------------------
321
322 bool wxControl::ProcessCommand(wxCommandEvent& event)
323 {
324 return HandleWindowEvent(event);
325 }
326
327 bool wxControl::MSWOnNotify(int idCtrl,
328 WXLPARAM lParam,
329 WXLPARAM* result)
330 {
331 wxEventType eventType wxDUMMY_INITIALIZE(wxEVT_NULL);
332
333 NMHDR *hdr = (NMHDR*) lParam;
334 switch ( hdr->code )
335 {
336 case NM_CLICK:
337 eventType = wxEVT_COMMAND_LEFT_CLICK;
338 break;
339
340 case NM_DBLCLK:
341 eventType = wxEVT_COMMAND_LEFT_DCLICK;
342 break;
343
344 case NM_RCLICK:
345 eventType = wxEVT_COMMAND_RIGHT_CLICK;
346 break;
347
348 case NM_RDBLCLK:
349 eventType = wxEVT_COMMAND_RIGHT_DCLICK;
350 break;
351
352 case NM_SETFOCUS:
353 eventType = wxEVT_COMMAND_SET_FOCUS;
354 break;
355
356 case NM_KILLFOCUS:
357 eventType = wxEVT_COMMAND_KILL_FOCUS;
358 break;
359
360 case NM_RETURN:
361 eventType = wxEVT_COMMAND_ENTER;
362 break;
363
364 default:
365 return wxWindow::MSWOnNotify(idCtrl, lParam, result);
366 }
367
368 wxCommandEvent event(wxEVT_NULL, m_windowId);
369 event.SetEventType(eventType);
370 event.SetEventObject(this);
371
372 return HandleWindowEvent(event);
373 }
374
375 WXHBRUSH wxControl::DoMSWControlColor(WXHDC pDC, wxColour colBg, WXHWND hWnd)
376 {
377 HDC hdc = (HDC)pDC;
378 if ( m_hasFgCol )
379 {
380 ::SetTextColor(hdc, wxColourToRGB(GetForegroundColour()));
381 }
382
383 WXHBRUSH hbr = 0;
384 if ( !colBg.Ok() )
385 {
386 hbr = MSWGetBgBrush(pDC, hWnd);
387
388 // if the control doesn't have any bg colour, foreground colour will be
389 // ignored as the return value would be 0 -- so forcefully give it a
390 // non default background brush in this case
391 if ( !hbr && m_hasFgCol )
392 colBg = GetBackgroundColour();
393 }
394
395 // use the background colour override if a valid colour is given
396 if ( colBg.Ok() )
397 {
398 ::SetBkColor(hdc, wxColourToRGB(colBg));
399
400 // draw children with the same colour as the parent
401 wxBrush *brush = wxTheBrushList->FindOrCreateBrush(colBg);
402 hbr = (WXHBRUSH)brush->GetResourceHandle();
403 }
404
405 // if we use custom background, we should set foreground ourselves too
406 if ( hbr && !m_hasFgCol )
407 {
408 ::SetTextColor(hdc, ::GetSysColor(COLOR_WINDOWTEXT));
409 }
410 //else: already set above
411
412 return hbr;
413 }
414
415 WXHBRUSH wxControl::MSWControlColor(WXHDC pDC, WXHWND hWnd)
416 {
417 if ( HasTransparentBackground() )
418 ::SetBkMode((HDC)pDC, TRANSPARENT);
419
420 // don't pass any background colour to DoMSWControlColor(), our own
421 // background colour will be used by it only if it is set, otherwise the
422 // defaults will be used
423 return DoMSWControlColor(pDC, wxColour(), hWnd);
424 }
425
426 WXHBRUSH wxControl::MSWControlColorDisabled(WXHDC pDC)
427 {
428 return DoMSWControlColor(pDC,
429 wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE),
430 GetHWND());
431 }
432
433 // ----------------------------------------------------------------------------
434 // wxControlWithItems
435 // ----------------------------------------------------------------------------
436
437 void wxControlWithItems::MSWAllocStorage(const wxArrayStringsAdapter& items,
438 unsigned wm)
439 {
440 const unsigned numItems = items.GetCount();
441 unsigned long totalTextLength = numItems; // for trailing '\0' characters
442 for ( unsigned i = 0; i < numItems; ++i )
443 {
444 totalTextLength += items[i].length();
445 }
446
447 if ( SendMessage((HWND)MSWGetItemsHWND(), wm, numItems,
448 (LPARAM)totalTextLength*sizeof(wxChar)) == LB_ERRSPACE )
449 {
450 wxLogLastError(wxT("SendMessage(XX_INITSTORAGE)"));
451 }
452 }
453
454 int wxControlWithItems::MSWInsertOrAppendItem(unsigned pos,
455 const wxString& item,
456 unsigned wm)
457 {
458 LRESULT n = SendMessage((HWND)MSWGetItemsHWND(), wm, pos,
459 (LPARAM)item.wx_str());
460 if ( n == CB_ERR || n == CB_ERRSPACE )
461 {
462 wxLogLastError(wxT("SendMessage(XX_ADD/INSERTSTRING)"));
463 return wxNOT_FOUND;
464 }
465
466 return n;
467 }
468
469 // ---------------------------------------------------------------------------
470 // global functions
471 // ---------------------------------------------------------------------------
472
473 // this is used in radiobox.cpp and slider95.cpp and should be removed as soon
474 // as it is not needed there any more!
475 //
476 // Call this repeatedly for several wnds to find the overall size
477 // of the widget.
478 // Call it initially with wxDefaultCoord for all values in rect.
479 // Keep calling for other widgets, and rect will be modified
480 // to calculate largest bounding rectangle.
481 void wxFindMaxSize(WXHWND wnd, RECT *rect)
482 {
483 int left = rect->left;
484 int right = rect->right;
485 int top = rect->top;
486 int bottom = rect->bottom;
487
488 GetWindowRect((HWND) wnd, rect);
489
490 if (left < 0)
491 return;
492
493 if (left < rect->left)
494 rect->left = left;
495
496 if (right > rect->right)
497 rect->right = right;
498
499 if (top < rect->top)
500 rect->top = top;
501
502 if (bottom > rect->bottom)
503 rect->bottom = bottom;
504 }
505
506 #endif // wxUSE_CONTROLS