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