1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/msw/control.cpp
3 // Purpose: wxControl class
4 // Author: Julian Smart
7 // Copyright: (c) Julian Smart
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
11 // ============================================================================
13 // ============================================================================
15 // ----------------------------------------------------------------------------
17 // ----------------------------------------------------------------------------
19 // For compilers that support precompilation, includes "wx.h".
20 #include "wx/wxprec.h"
28 #include "wx/control.h"
31 #include "wx/msw/wrapcctl.h" // include <commctrl.h> "properly"
34 #include "wx/dcclient.h"
36 #include "wx/settings.h"
37 #include "wx/ctrlsub.h"
41 #include "wx/listctrl.h"
42 #endif // wxUSE_LISTCTRL
45 #include "wx/treectrl.h"
46 #endif // wxUSE_TREECTRL
48 #include "wx/msw/private.h"
49 #include "wx/msw/uxtheme.h"
51 // ----------------------------------------------------------------------------
53 // ----------------------------------------------------------------------------
55 IMPLEMENT_ABSTRACT_CLASS(wxControl
, wxWindow
)
57 // ============================================================================
58 // wxControl implementation
59 // ============================================================================
61 // ----------------------------------------------------------------------------
62 // control window creation
63 // ----------------------------------------------------------------------------
65 bool wxControl::Create(wxWindow
*parent
,
70 const wxValidator
& wxVALIDATOR_PARAM(validator
),
73 if ( !wxWindow::Create(parent
, id
, pos
, size
, style
, name
) )
77 SetValidator(validator
);
83 bool wxControl::MSWCreateControl(const wxChar
*classname
,
84 const wxString
& label
,
89 WXDWORD msStyle
= MSWGetStyle(GetWindowStyle(), &exstyle
);
91 return MSWCreateControl(classname
, msStyle
, pos
, size
, label
, exstyle
);
94 bool wxControl::MSWCreateControl(const wxChar
*classname
,
98 const wxString
& label
,
101 // if no extended style given, determine it ourselves
102 if ( exstyle
== (WXDWORD
)-1 )
105 (void) MSWGetStyle(GetWindowStyle(), &exstyle
);
108 // all controls should have this style
111 // create the control visible if it's currently shown for wxWidgets
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
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
;
127 // ... and adjust it to account for a possible parent frames toolbar
128 AdjustForParentClientOrigin(x
, y
);
130 m_hWnd
= (WXHWND
)::CreateWindowEx
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
145 wxLogLastError(wxString::Format
147 wxT("CreateWindowEx(\"%s\", flags=%08lx, ex=%08lx)"),
148 classname
, style
, exstyle
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.
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
168 // saving the label in m_labelOrig to return it verbatim
169 // later in GetLabel()
172 // install wxWidgets window proc for this window
175 // set up fonts and colours
181 wxFont font
= GetDefaultAttributes().font
;
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
187 // FIXME: replace the dynamic casts with virtual function calls!!
188 #if wxUSE_LISTCTRL || wxUSE_TREECTRL
189 bool testFont
= false;
191 if ( wxDynamicCastThis(wxListCtrl
) )
193 #endif // wxUSE_LISTCTRL
195 if ( wxDynamicCastThis(wxTreeCtrl
) )
197 #endif // wxUSE_TREECTRL
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
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
) )
213 #endif // wxUSE_LISTCTRL || wxUSE_TREECTRL
217 SetFont(GetDefaultAttributes().font
);
221 // set the size now if no initial size specified
222 SetInitialSize(size
);
227 // ----------------------------------------------------------------------------
229 // ----------------------------------------------------------------------------
231 WXDWORD
wxControl::MSWGetStyle(long style
, WXDWORD
*exstyle
) const
233 long msStyle
= wxWindow::MSWGetStyle(style
, exstyle
);
235 if ( AcceptsFocusFromKeyboard() )
237 msStyle
|= WS_TABSTOP
;
243 wxSize
wxControl::DoGetBestSize() const
246 return wxControlBase::DoGetBestSize();
248 return wxSize(DEFAULT_ITEM_WIDTH
, DEFAULT_ITEM_HEIGHT
);
251 wxBorder
wxControl::GetDefaultBorder() const
253 return wxControlBase::GetDefaultBorder();
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
262 // take size according to layout
264 #if defined(__SMARTPHONE__) && defined(__WXWINCE__)
267 ::GetSystemMetrics(is_vertical
? SM_CXVSCROLL
: SM_CXHSCROLL
),
268 ::GetSystemMetrics(is_vertical
? SM_CYVSCROLL
: SM_CYHSCROLL
)
272 // correct size as for undocumented MSW variants cases (WinCE and perhaps others)
274 bestSize
.x
= bestSize
.y
;
276 bestSize
.y
= bestSize
.x
;
278 // double size according to layout
287 /* static */ wxVisualAttributes
288 wxControl::GetClassDefaultAttributes(wxWindowVariant
WXUNUSED(variant
))
290 wxVisualAttributes attrs
;
292 // old school (i.e. not "common") controls use the standard dialog font
294 attrs
.font
= wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
);
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
);
305 // ----------------------------------------------------------------------------
307 // ----------------------------------------------------------------------------
309 bool wxControl::ProcessCommand(wxCommandEvent
& event
)
311 return HandleWindowEvent(event
);
314 bool wxControl::MSWOnNotify(int idCtrl
,
318 wxEventType eventType
wxDUMMY_INITIALIZE(wxEVT_NULL
);
320 NMHDR
*hdr
= (NMHDR
*) lParam
;
324 eventType
= wxEVT_COMMAND_LEFT_CLICK
;
328 eventType
= wxEVT_COMMAND_LEFT_DCLICK
;
332 eventType
= wxEVT_COMMAND_RIGHT_CLICK
;
336 eventType
= wxEVT_COMMAND_RIGHT_DCLICK
;
340 eventType
= wxEVT_COMMAND_SET_FOCUS
;
344 eventType
= wxEVT_COMMAND_KILL_FOCUS
;
348 eventType
= wxEVT_COMMAND_ENTER
;
352 return wxWindow::MSWOnNotify(idCtrl
, lParam
, result
);
355 wxCommandEvent
event(wxEVT_NULL
, m_windowId
);
356 event
.SetEventType(eventType
);
357 event
.SetEventObject(this);
359 return HandleWindowEvent(event
);
362 WXHBRUSH
wxControl::DoMSWControlColor(WXHDC pDC
, wxColour colBg
, WXHWND hWnd
)
369 wxWindow
*win
= wxFindWinFromHandle( hWnd
);
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
);
379 wxWindow
*winParent
= wxFindWinFromHandle( parent
);
380 if( winParent
&& winParent
->ContainsHWND( hWnd
) )
386 hbr
= win
->MSWGetBgBrush(pDC
);
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();
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
400 wxBrush
*brush
= wxTheBrushList
->FindOrCreateBrush(colBg
);
401 hbr
= (WXHBRUSH
)brush
->GetResourceHandle();
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
410 ::SetTextColor(hdc
, wxColourToRGB(GetForegroundColour()));
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
)
419 colBg
= GetBackgroundColour();
421 ::SetBkColor(hdc
, wxColourToRGB(colBg
));
427 WXHBRUSH
wxControl::MSWControlColor(WXHDC pDC
, WXHWND hWnd
)
429 if ( HasTransparentBackground() )
430 ::SetBkMode((HDC
)pDC
, TRANSPARENT
);
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
);
438 WXHBRUSH
wxControl::MSWControlColorDisabled(WXHDC pDC
)
440 return DoMSWControlColor(pDC
,
441 wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE
),
445 // ----------------------------------------------------------------------------
446 // wxControlWithItems
447 // ----------------------------------------------------------------------------
449 void wxControlWithItems::MSWAllocStorage(const wxArrayStringsAdapter
& items
,
452 const unsigned numItems
= items
.GetCount();
453 unsigned long totalTextLength
= numItems
; // for trailing '\0' characters
454 for ( unsigned i
= 0; i
< numItems
; ++i
)
456 totalTextLength
+= items
[i
].length();
459 if ( SendMessage((HWND
)MSWGetItemsHWND(), wm
, numItems
,
460 (LPARAM
)totalTextLength
*sizeof(wxChar
)) == LB_ERRSPACE
)
462 wxLogLastError(wxT("SendMessage(XX_INITSTORAGE)"));
466 int wxControlWithItems::MSWInsertOrAppendItem(unsigned pos
,
467 const wxString
& item
,
470 LRESULT n
= SendMessage((HWND
)MSWGetItemsHWND(), wm
, pos
,
471 wxMSW_CONV_LPARAM(item
));
472 if ( n
== CB_ERR
|| n
== CB_ERRSPACE
)
474 wxLogLastError(wxT("SendMessage(XX_ADD/INSERTSTRING)"));
481 // ---------------------------------------------------------------------------
483 // ---------------------------------------------------------------------------
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!
488 // Call this repeatedly for several wnds to find the overall size
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
)
495 int left
= rect
->left
;
496 int right
= rect
->right
;
498 int bottom
= rect
->bottom
;
500 GetWindowRect((HWND
) wnd
, rect
);
505 if (left
< rect
->left
)
508 if (right
> rect
->right
)
514 if (bottom
> rect
->bottom
)
515 rect
->bottom
= bottom
;
518 #endif // wxUSE_CONTROLS