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