1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/msw/control.cpp
3 // Purpose: wxControl class
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
29 #include "wx/control.h"
32 #include "wx/msw/wrapcctl.h" // include <commctrl.h> "properly"
35 #include "wx/dcclient.h"
37 #include "wx/settings.h"
38 #include "wx/ctrlsub.h"
42 #include "wx/listctrl.h"
43 #endif // wxUSE_LISTCTRL
46 #include "wx/treectrl.h"
47 #endif // wxUSE_TREECTRL
49 #include "wx/msw/private.h"
50 #include "wx/msw/uxtheme.h"
52 // ----------------------------------------------------------------------------
54 // ----------------------------------------------------------------------------
56 IMPLEMENT_ABSTRACT_CLASS(wxControl
, wxWindow
)
58 // ============================================================================
59 // wxControl implementation
60 // ============================================================================
62 // ----------------------------------------------------------------------------
63 // control window creation
64 // ----------------------------------------------------------------------------
66 bool wxControl::Create(wxWindow
*parent
,
71 const wxValidator
& wxVALIDATOR_PARAM(validator
),
74 if ( !wxWindow::Create(parent
, id
, pos
, size
, style
, name
) )
78 SetValidator(validator
);
84 bool wxControl::MSWCreateControl(const wxChar
*classname
,
85 const wxString
& label
,
90 WXDWORD msStyle
= MSWGetStyle(GetWindowStyle(), &exstyle
);
92 return MSWCreateControl(classname
, msStyle
, pos
, size
, label
, exstyle
);
95 bool wxControl::MSWCreateControl(const wxChar
*classname
,
99 const wxString
& label
,
102 // if no extended style given, determine it ourselves
103 if ( exstyle
== (WXDWORD
)-1 )
106 (void) MSWGetStyle(GetWindowStyle(), &exstyle
);
109 // all controls should have this style
112 // create the control visible if it's currently shown for wxWidgets
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
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
;
128 // ... and adjust it to account for a possible parent frames toolbar
129 AdjustForParentClientOrigin(x
, y
);
131 m_hWnd
= (WXHWND
)::CreateWindowEx
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
146 wxLogLastError(wxString::Format
148 wxT("CreateWindowEx(\"%s\", flags=%08lx, ex=%08lx)"),
149 classname
, style
, exstyle
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.
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
169 // saving the label in m_labelOrig to return it verbatim
170 // later in GetLabel()
173 // install wxWidgets window proc for this window
176 // set up fonts and colours
182 wxFont font
= GetDefaultAttributes().font
;
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
188 // FIXME: replace the dynamic casts with virtual function calls!!
189 #if wxUSE_LISTCTRL || wxUSE_TREECTRL
190 bool testFont
= false;
192 if ( wxDynamicCastThis(wxListCtrl
) )
194 #endif // wxUSE_LISTCTRL
196 if ( wxDynamicCastThis(wxTreeCtrl
) )
198 #endif // wxUSE_TREECTRL
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
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
) )
214 #endif // wxUSE_LISTCTRL || wxUSE_TREECTRL
218 SetFont(GetDefaultAttributes().font
);
222 // set the size now if no initial size specified
223 SetInitialSize(size
);
228 // ----------------------------------------------------------------------------
230 // ----------------------------------------------------------------------------
232 WXDWORD
wxControl::MSWGetStyle(long style
, WXDWORD
*exstyle
) const
234 long msStyle
= wxWindow::MSWGetStyle(style
, exstyle
);
236 if ( AcceptsFocusFromKeyboard() )
238 msStyle
|= WS_TABSTOP
;
244 wxSize
wxControl::DoGetBestSize() const
247 return wxControlBase::DoGetBestSize();
249 return wxSize(DEFAULT_ITEM_WIDTH
, DEFAULT_ITEM_HEIGHT
);
252 wxBorder
wxControl::GetDefaultBorder() const
254 return wxControlBase::GetDefaultBorder();
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
263 // take size according to layout
265 #if defined(__SMARTPHONE__) && defined(__WXWINCE__)
268 ::GetSystemMetrics(is_vertical
? SM_CXVSCROLL
: SM_CXHSCROLL
),
269 ::GetSystemMetrics(is_vertical
? SM_CYVSCROLL
: SM_CYHSCROLL
)
273 // correct size as for undocumented MSW variants cases (WinCE and perhaps others)
275 bestSize
.x
= bestSize
.y
;
277 bestSize
.y
= bestSize
.x
;
279 // double size according to layout
288 /* static */ wxVisualAttributes
289 wxControl::GetClassDefaultAttributes(wxWindowVariant
WXUNUSED(variant
))
291 wxVisualAttributes attrs
;
293 // old school (i.e. not "common") controls use the standard dialog font
295 attrs
.font
= wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
);
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
);
306 // ----------------------------------------------------------------------------
308 // ----------------------------------------------------------------------------
310 bool wxControl::ProcessCommand(wxCommandEvent
& event
)
312 return HandleWindowEvent(event
);
315 bool wxControl::MSWOnNotify(int idCtrl
,
319 wxEventType eventType
wxDUMMY_INITIALIZE(wxEVT_NULL
);
321 NMHDR
*hdr
= (NMHDR
*) lParam
;
325 eventType
= wxEVT_COMMAND_LEFT_CLICK
;
329 eventType
= wxEVT_COMMAND_LEFT_DCLICK
;
333 eventType
= wxEVT_COMMAND_RIGHT_CLICK
;
337 eventType
= wxEVT_COMMAND_RIGHT_DCLICK
;
341 eventType
= wxEVT_COMMAND_SET_FOCUS
;
345 eventType
= wxEVT_COMMAND_KILL_FOCUS
;
349 eventType
= wxEVT_COMMAND_ENTER
;
353 return wxWindow::MSWOnNotify(idCtrl
, lParam
, result
);
356 wxCommandEvent
event(wxEVT_NULL
, m_windowId
);
357 event
.SetEventType(eventType
);
358 event
.SetEventObject(this);
360 return HandleWindowEvent(event
);
363 WXHBRUSH
wxControl::DoMSWControlColor(WXHDC pDC
, wxColour colBg
, WXHWND hWnd
)
370 hbr
= MSWGetBgBrush(pDC
, hWnd
);
372 // if the control doesn't have any bg colour, foreground colour will be
373 // ignored as the return value would be 0 -- so forcefully give it a
374 // non default background brush in this case
375 if ( !hbr
&& m_hasFgCol
)
376 colBg
= GetBackgroundColour();
379 // use the background colour override if a valid colour is given: this is
380 // used when the control is disabled to grey it out and also if colBg was
384 wxBrush
*brush
= wxTheBrushList
->FindOrCreateBrush(colBg
);
385 hbr
= (WXHBRUSH
)brush
->GetResourceHandle();
388 // always set the foreground colour if we changed the background, whether
389 // m_hasFgCol is true or not: if it true, we must do it, of course, but
390 // even if it isn't, we must set the default foreground explicitly as by
391 // default just the simple black is used
394 ::SetTextColor(hdc
, wxColourToRGB(GetForegroundColour()));
397 // finally also set the background colour for text drawing: without this,
398 // the text in an edit control is drawn using the default background even
399 // if we return a valid brush
400 if ( colBg
.IsOk() || m_hasBgCol
)
403 colBg
= GetBackgroundColour();
405 ::SetBkColor(hdc
, wxColourToRGB(colBg
));
411 WXHBRUSH
wxControl::MSWControlColor(WXHDC pDC
, WXHWND hWnd
)
413 if ( HasTransparentBackground() )
414 ::SetBkMode((HDC
)pDC
, TRANSPARENT
);
416 // don't pass any background colour to DoMSWControlColor(), our own
417 // background colour will be used by it only if it is set, otherwise the
418 // defaults will be used
419 return DoMSWControlColor(pDC
, wxColour(), hWnd
);
422 WXHBRUSH
wxControl::MSWControlColorDisabled(WXHDC pDC
)
424 return DoMSWControlColor(pDC
,
425 wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE
),
429 // ----------------------------------------------------------------------------
430 // wxControlWithItems
431 // ----------------------------------------------------------------------------
433 void wxControlWithItems::MSWAllocStorage(const wxArrayStringsAdapter
& items
,
436 const unsigned numItems
= items
.GetCount();
437 unsigned long totalTextLength
= numItems
; // for trailing '\0' characters
438 for ( unsigned i
= 0; i
< numItems
; ++i
)
440 totalTextLength
+= items
[i
].length();
443 if ( SendMessage((HWND
)MSWGetItemsHWND(), wm
, numItems
,
444 (LPARAM
)totalTextLength
*sizeof(wxChar
)) == LB_ERRSPACE
)
446 wxLogLastError(wxT("SendMessage(XX_INITSTORAGE)"));
450 int wxControlWithItems::MSWInsertOrAppendItem(unsigned pos
,
451 const wxString
& item
,
454 LRESULT n
= SendMessage((HWND
)MSWGetItemsHWND(), wm
, pos
,
455 (LPARAM
)item
.wx_str());
456 if ( n
== CB_ERR
|| n
== CB_ERRSPACE
)
458 wxLogLastError(wxT("SendMessage(XX_ADD/INSERTSTRING)"));
465 // ---------------------------------------------------------------------------
467 // ---------------------------------------------------------------------------
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!
472 // Call this repeatedly for several wnds to find the overall size
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
)
479 int left
= rect
->left
;
480 int right
= rect
->right
;
482 int bottom
= rect
->bottom
;
484 GetWindowRect((HWND
) wnd
, rect
);
489 if (left
< rect
->left
)
492 if (right
> rect
->right
)
498 if (bottom
> rect
->bottom
)
499 rect
->bottom
= bottom
;
502 #endif // wxUSE_CONTROLS