]> git.saurik.com Git - wxWidgets.git/blame - src/msw/control.cpp
Remove support for Gnome printing from wxGTK.
[wxWidgets.git] / src / msw / control.cpp
CommitLineData
2bda0e17 1/////////////////////////////////////////////////////////////////////////////
a71d815b 2// Name: src/msw/control.cpp
2bda0e17
KB
3// Purpose: wxControl class
4// Author: Julian Smart
5// Modified by:
6// Created: 01/02/97
6c9a19aa 7// Copyright: (c) Julian Smart
65571936 8// Licence: wxWindows licence
2bda0e17
KB
9/////////////////////////////////////////////////////////////////////////////
10
34040e31
VZ
11// ============================================================================
12// declarations
13// ============================================================================
14
15// ----------------------------------------------------------------------------
16// headers
17// ----------------------------------------------------------------------------
18
2bda0e17
KB
19// For compilers that support precompilation, includes "wx.h".
20#include "wx/wxprec.h"
21
22#ifdef __BORLANDC__
1e6feb95 23 #pragma hdrstop
2bda0e17
KB
24#endif
25
1e6feb95
VZ
26#if wxUSE_CONTROLS
27
93fbbe07
WS
28#include "wx/control.h"
29
2bda0e17 30#ifndef WX_PRECOMP
57bd4c60 31 #include "wx/msw/wrapcctl.h" // include <commctrl.h> "properly"
1e6feb95
VZ
32 #include "wx/event.h"
33 #include "wx/app.h"
34 #include "wx/dcclient.h"
35 #include "wx/log.h"
34040e31 36 #include "wx/settings.h"
a236aa20 37 #include "wx/ctrlsub.h"
2bda0e17
KB
38#endif
39
3c96418b
JG
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
01c500af 47
2bda0e17 48#include "wx/msw/private.h"
01c500af 49#include "wx/msw/uxtheme.h"
2bda0e17 50
34040e31
VZ
51// ----------------------------------------------------------------------------
52// wxWin macros
53// ----------------------------------------------------------------------------
54
2bda0e17
KB
55IMPLEMENT_ABSTRACT_CLASS(wxControl, wxWindow)
56
34040e31
VZ
57// ============================================================================
58// wxControl implementation
59// ============================================================================
60
34040e31
VZ
61// ----------------------------------------------------------------------------
62// control window creation
63// ----------------------------------------------------------------------------
8d772832 64
5b2f31eb
VZ
65bool wxControl::Create(wxWindow *parent,
66 wxWindowID id,
8d772832 67 const wxPoint& pos,
5b2f31eb
VZ
68 const wxSize& size,
69 long style,
ac8d0c11 70 const wxValidator& wxVALIDATOR_PARAM(validator),
8d772832
RD
71 const wxString& name)
72{
5b2f31eb 73 if ( !wxWindow::Create(parent, id, pos, size, style, name) )
d95de154 74 return false;
5b2f31eb 75
8d772832 76#if wxUSE_VALIDATORS
5b2f31eb 77 SetValidator(validator);
8d772832 78#endif
5b2f31eb 79
d95de154 80 return true;
5b2f31eb
VZ
81}
82
83bool wxControl::MSWCreateControl(const wxChar *classname,
84 const wxString& label,
85 const wxPoint& pos,
6dd16e4f 86 const wxSize& size)
5b2f31eb
VZ
87{
88 WXDWORD exstyle;
6dd16e4f 89 WXDWORD msStyle = MSWGetStyle(GetWindowStyle(), &exstyle);
5b2f31eb 90
2eb4c3aa 91 return MSWCreateControl(classname, msStyle, pos, size, label, exstyle);
8d772832
RD
92}
93
222594ea
VZ
94bool wxControl::MSWCreateControl(const wxChar *classname,
95 WXDWORD style,
96 const wxPoint& pos,
97 const wxSize& size,
98 const wxString& label,
2eb4c3aa 99 WXDWORD exstyle)
8d99be5f 100{
222594ea
VZ
101 // if no extended style given, determine it ourselves
102 if ( exstyle == (WXDWORD)-1 )
103 {
fe3d9123 104 exstyle = 0;
65bc172c 105 (void) MSWGetStyle(GetWindowStyle(), &exstyle);
222594ea
VZ
106 }
107
bdf5c30d
VZ
108 // all controls should have this style
109 style |= WS_CHILD;
110
77ffb593 111 // create the control visible if it's currently shown for wxWidgets
bdf5c30d
VZ
112 if ( m_isShown )
113 {
114 style |= WS_VISIBLE;
115 }
3f2711d5 116
7fe985ee
VZ
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
d95de154
WS
122 int x = pos.x == wxDefaultCoord ? 0 : pos.x,
123 y = pos.y == wxDefaultCoord ? 0 : pos.y,
7fe985ee
VZ
124 w = size.x == wxDefaultCoord ? 1 : size.x,
125 h = size.y == wxDefaultCoord ? 1 : size.y;
a63cbfa3 126
7c0a023d 127 // ... and adjust it to account for a possible parent frames toolbar
4e9d23cd
VZ
128 AdjustForParentClientOrigin(x, y);
129
8d99be5f
VZ
130 m_hWnd = (WXHWND)::CreateWindowEx
131 (
222594ea 132 exstyle, // extended style
8d99be5f 133 classname, // the kind of control to create
017dc06b 134 label.t_str(), // the window name
8d99be5f 135 style, // the window style
a63cbfa3 136 x, y, w, h, // the window position and size
dca0f651
VZ
137 GetHwndOf(GetParent()), // parent
138 (HMENU)wxUIntToPtr(GetId()), // child id
8d99be5f
VZ
139 wxGetInstance(), // app instance
140 NULL // creation parameters
141 );
142
143 if ( !m_hWnd )
144 {
0d801d09
VZ
145 wxLogLastError(wxString::Format
146 (
9a83f860 147 wxT("CreateWindowEx(\"%s\", flags=%08lx, ex=%08lx)"),
0d801d09
VZ
148 classname, style, exstyle
149 ));
8d99be5f 150
d95de154 151 return false;
8d99be5f
VZ
152 }
153
e453fe96
VZ
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 )
017dc06b 165 ::SetWindowText(GetHwnd(), label.t_str());
e453fe96
VZ
166#endif // !wxUSE_UNICODE
167
39bc0347
VZ
168 // saving the label in m_labelOrig to return it verbatim
169 // later in GetLabel()
170 m_labelOrig = label;
171
77ffb593 172 // install wxWidgets window proc for this window
8d99be5f
VZ
173 SubclassWin(m_hWnd);
174
34040e31 175 // set up fonts and colours
8d99be5f 176 InheritAttributes();
3c96418b
JG
177 if ( !m_hasFont )
178 {
82c591d7
VZ
179 bool setFont = true;
180
181 wxFont font = GetDefaultAttributes().font;
182
3c96418b
JG
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
82c591d7
VZ
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;
28f9f58c 197#endif // wxUSE_TREECTRL
82c591d7
VZ
198
199 if ( testFont )
3c96418b
JG
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
82c591d7 208 if ( font != wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT) )
3c96418b 209 {
82c591d7 210 setFont = false;
3c96418b
JG
211 }
212 }
3c96418b 213#endif // wxUSE_LISTCTRL || wxUSE_TREECTRL
82c591d7
VZ
214
215 if ( setFont )
3c96418b
JG
216 {
217 SetFont(GetDefaultAttributes().font);
218 }
219 }
8d99be5f 220
a63cbfa3 221 // set the size now if no initial size specified
170acdc9 222 SetInitialSize(size);
a63cbfa3 223
d95de154 224 return true;
8d99be5f
VZ
225}
226
34040e31
VZ
227// ----------------------------------------------------------------------------
228// various accessors
229// ----------------------------------------------------------------------------
230
34040e31
VZ
231WXDWORD wxControl::MSWGetStyle(long style, WXDWORD *exstyle) const
232{
233 long msStyle = wxWindow::MSWGetStyle(style, exstyle);
234
ad02525d 235 if ( AcceptsFocusFromKeyboard() )
34040e31
VZ
236 {
237 msStyle |= WS_TABSTOP;
238 }
239
240 return msStyle;
241}
242
f68586e5 243wxSize wxControl::DoGetBestSize() const
4438caf4 244{
30b80439
RR
245 if (m_windowSizer)
246 return wxControlBase::DoGetBestSize();
03647350 247
4438caf4
VZ
248 return wxSize(DEFAULT_ITEM_WIDTH, DEFAULT_ITEM_HEIGHT);
249}
250
4c0d2cd3
JS
251wxBorder wxControl::GetDefaultBorder() const
252{
dc797d8e 253 return wxControlBase::GetDefaultBorder();
4c0d2cd3
JS
254}
255
4bf45c9e
WS
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.
5aaabb37 260wxSize wxControl::GetBestSpinnerSize(const bool is_vertical) const
4bf45c9e 261{
b081046a 262 // take size according to layout
1550d5f8 263 wxSize bestSize(
285f605a 264#if defined(__SMARTPHONE__) && defined(__WXWINCE__)
1550d5f8 265 0,GetCharHeight()
3180bc0e 266#else
46697f31
WS
267 ::GetSystemMetrics(is_vertical ? SM_CXVSCROLL : SM_CXHSCROLL),
268 ::GetSystemMetrics(is_vertical ? SM_CYVSCROLL : SM_CYHSCROLL)
3180bc0e 269#endif
1550d5f8 270 );
b081046a
WS
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
4bf45c9e 279 if (is_vertical)
b081046a 280 bestSize.y *= 2;
4bf45c9e 281 else
b081046a
WS
282 bestSize.x *= 2;
283
284 return bestSize;
4bf45c9e
WS
285}
286
34040e31
VZ
287/* static */ wxVisualAttributes
288wxControl::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
34040e31
VZ
305// ----------------------------------------------------------------------------
306// message handling
307// ----------------------------------------------------------------------------
308
42e69d6b 309bool wxControl::ProcessCommand(wxCommandEvent& event)
2bda0e17 310{
937013e0 311 return HandleWindowEvent(event);
2bda0e17
KB
312}
313
a23fd0e1
VZ
314bool wxControl::MSWOnNotify(int idCtrl,
315 WXLPARAM lParam,
316 WXLPARAM* result)
2bda0e17 317{
5cb598ae 318 wxEventType eventType wxDUMMY_INITIALIZE(wxEVT_NULL);
b6e5eaa5
VZ
319
320 NMHDR *hdr = (NMHDR*) lParam;
321 switch ( hdr->code )
a23fd0e1
VZ
322 {
323 case NM_CLICK:
324 eventType = wxEVT_COMMAND_LEFT_CLICK;
325 break;
2bda0e17 326
a23fd0e1
VZ
327 case NM_DBLCLK:
328 eventType = wxEVT_COMMAND_LEFT_DCLICK;
329 break;
2bda0e17 330
a23fd0e1
VZ
331 case NM_RCLICK:
332 eventType = wxEVT_COMMAND_RIGHT_CLICK;
333 break;
2bda0e17 334
a23fd0e1
VZ
335 case NM_RDBLCLK:
336 eventType = wxEVT_COMMAND_RIGHT_DCLICK;
337 break;
2bda0e17 338
a23fd0e1
VZ
339 case NM_SETFOCUS:
340 eventType = wxEVT_COMMAND_SET_FOCUS;
341 break;
debe6624 342
a23fd0e1
VZ
343 case NM_KILLFOCUS:
344 eventType = wxEVT_COMMAND_KILL_FOCUS;
345 break;
2bda0e17 346
a23fd0e1
VZ
347 case NM_RETURN:
348 eventType = wxEVT_COMMAND_ENTER;
349 break;
350
351 default:
352 return wxWindow::MSWOnNotify(idCtrl, lParam, result);
353 }
fd3f686c 354
b6e5eaa5 355 wxCommandEvent event(wxEVT_NULL, m_windowId);
2bda0e17 356 event.SetEventType(eventType);
a23fd0e1 357 event.SetEventObject(this);
2bda0e17 358
937013e0 359 return HandleWindowEvent(event);
2bda0e17
KB
360}
361
2bae4332 362WXHBRUSH wxControl::DoMSWControlColor(WXHDC pDC, wxColour colBg, WXHWND hWnd)
f048e32f 363{
01c500af 364 HDC hdc = (HDC)pDC;
01c500af 365
c3732409 366 WXHBRUSH hbr = 0;
a1b806b9 367 if ( !colBg.IsOk() )
c3732409 368 {
5ddb3b8c
VZ
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 )
ebfee179 386 hbr = win->MSWGetBgBrush(pDC);
c3732409
VZ
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
db5a2bff
VZ
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
a1b806b9 398 if ( colBg.IsOk() )
f048e32f 399 {
52c5093f 400 wxBrush *brush = wxTheBrushList->FindOrCreateBrush(colBg);
d1a47dfe
VZ
401 hbr = (WXHBRUSH)brush->GetResourceHandle();
402 }
f048e32f 403
3316192f
VZ
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 )
155acb0c 409 {
3316192f 410 ::SetTextColor(hdc, wxColourToRGB(GetForegroundColour()));
155acb0c 411 }
155acb0c 412
db5a2bff
VZ
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
d1a47dfe 424 return hbr;
5c836c46
VZ
425}
426
2bae4332 427WXHBRUSH wxControl::MSWControlColor(WXHDC pDC, WXHWND hWnd)
5c836c46 428{
c3732409
VZ
429 if ( HasTransparentBackground() )
430 ::SetBkMode((HDC)pDC, TRANSPARENT);
01c500af 431
52c5093f
VZ
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);
5c836c46
VZ
436}
437
438WXHBRUSH wxControl::MSWControlColorDisabled(WXHDC pDC)
439{
dd12ce22 440 return DoMSWControlColor(pDC,
2bae4332
VZ
441 wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE),
442 GetHWND());
f048e32f
VZ
443}
444
a236aa20
VZ
445// ----------------------------------------------------------------------------
446// wxControlWithItems
447// ----------------------------------------------------------------------------
448
449void 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
0d5b49cf 459 if ( SendMessage((HWND)MSWGetItemsHWND(), wm, numItems,
a236aa20
VZ
460 (LPARAM)totalTextLength*sizeof(wxChar)) == LB_ERRSPACE )
461 {
462 wxLogLastError(wxT("SendMessage(XX_INITSTORAGE)"));
463 }
464}
465
466int wxControlWithItems::MSWInsertOrAppendItem(unsigned pos,
467 const wxString& item,
468 unsigned wm)
469{
0d5b49cf 470 LRESULT n = SendMessage((HWND)MSWGetItemsHWND(), wm, pos,
017dc06b 471 wxMSW_CONV_LPARAM(item));
a236aa20
VZ
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
42e69d6b
VZ
481// ---------------------------------------------------------------------------
482// global functions
483// ---------------------------------------------------------------------------
2bda0e17 484
34040e31
VZ
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//
42e69d6b
VZ
488// Call this repeatedly for several wnds to find the overall size
489// of the widget.
d95de154 490// Call it initially with wxDefaultCoord for all values in rect.
42e69d6b
VZ
491// Keep calling for other widgets, and rect will be modified
492// to calculate largest bounding rectangle.
493void wxFindMaxSize(WXHWND wnd, RECT *rect)
2bda0e17 494{
42e69d6b
VZ
495 int left = rect->left;
496 int right = rect->right;
497 int top = rect->top;
498 int bottom = rect->bottom;
2bda0e17 499
42e69d6b 500 GetWindowRect((HWND) wnd, rect);
2bda0e17 501
42e69d6b
VZ
502 if (left < 0)
503 return;
2bda0e17 504
42e69d6b
VZ
505 if (left < rect->left)
506 rect->left = left;
2bda0e17 507
42e69d6b
VZ
508 if (right > rect->right)
509 rect->right = right;
2bda0e17 510
42e69d6b
VZ
511 if (top < rect->top)
512 rect->top = top;
2bda0e17 513
42e69d6b
VZ
514 if (bottom > rect->bottom)
515 rect->bottom = bottom;
2bda0e17
KB
516}
517
1e6feb95 518#endif // wxUSE_CONTROLS