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 // wxControl ctor/dtor
64 // ----------------------------------------------------------------------------
66 wxControl::~wxControl()
68 m_isBeingDeleted
= true;
71 // ----------------------------------------------------------------------------
72 // control window creation
73 // ----------------------------------------------------------------------------
75 bool wxControl::Create(wxWindow
*parent
,
80 const wxValidator
& wxVALIDATOR_PARAM(validator
),
83 if ( !wxWindow::Create(parent
, id
, pos
, size
, style
, name
) )
87 SetValidator(validator
);
93 bool wxControl::MSWCreateControl(const wxChar
*classname
,
94 const wxString
& label
,
99 WXDWORD msStyle
= MSWGetStyle(GetWindowStyle(), &exstyle
);
101 return MSWCreateControl(classname
, msStyle
, pos
, size
, label
, exstyle
);
104 bool wxControl::MSWCreateControl(const wxChar
*classname
,
108 const wxString
& label
,
111 // if no extended style given, determine it ourselves
112 if ( exstyle
== (WXDWORD
)-1 )
115 (void) MSWGetStyle(GetWindowStyle(), &exstyle
);
118 // all controls should have this style
121 // create the control visible if it's currently shown for wxWidgets
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
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
;
137 // ... and adjust it to account for a possible parent frames toolbar
138 AdjustForParentClientOrigin(x
, y
);
140 m_hWnd
= (WXHWND
)::CreateWindowEx
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
156 wxLogLastError(wxString::Format
158 _T("CreateWindowEx(\"%s\", flags=%08lx, ex=%08lx)"),
159 classname
, style
, exstyle
161 #endif // __WXDEBUG__
167 // Text labels starting with the character 0xff (which is a valid character
168 // in many code pages) don't appear correctly as CreateWindowEx() has some
169 // special treatment for this case, apparently the strings starting with -1
170 // are not really strings but something called "ordinals". There is no
171 // documentation about it but the fact is that the label gets mangled or
172 // not displayed at all if we don't do this, see #9572.
174 // Notice that 0xffff is not a valid Unicode character so the problem
175 // doesn't arise in Unicode build.
176 if ( !label
.empty() && label
[0] == -1 )
177 ::SetWindowText(GetHwnd(), label
.wx_str());
178 #endif // !wxUSE_UNICODE
180 // saving the label in m_labelOrig to return it verbatim
181 // later in GetLabel()
184 // install wxWidgets window proc for this window
187 // set up fonts and colours
193 wxFont font
= GetDefaultAttributes().font
;
195 // if we set a font for {list,tree}ctrls and the font size is changed in
196 // the display properties then the font size for these controls doesn't
197 // automatically adjust when they receive WM_SETTINGCHANGE
199 // FIXME: replace the dynamic casts with virtual function calls!!
200 #if wxUSE_LISTCTRL || wxUSE_TREECTRL
201 bool testFont
= false;
203 if ( wxDynamicCastThis(wxListCtrl
) )
205 #endif // wxUSE_LISTCTRL
207 if ( wxDynamicCastThis(wxTreeCtrl
) )
209 #endif // wxUSE_TREECTRL
213 // not sure if we need to explicitly set the font here for Win95/NT4
214 // but we definitely can't do it for any newer version
215 // see wxGetCCDefaultFont() in src/msw/settings.cpp for explanation
216 // of why this test works
218 // TODO: test Win95/NT4 to see if this is needed or breaks the
219 // font resizing as it does on newer versions
220 if ( font
!= wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
) )
225 #endif // wxUSE_LISTCTRL || wxUSE_TREECTRL
229 SetFont(GetDefaultAttributes().font
);
233 // set the size now if no initial size specified
234 SetInitialSize(size
);
239 // ----------------------------------------------------------------------------
241 // ----------------------------------------------------------------------------
243 WXDWORD
wxControl::MSWGetStyle(long style
, WXDWORD
*exstyle
) const
245 long msStyle
= wxWindow::MSWGetStyle(style
, exstyle
);
247 if ( AcceptsFocusFromKeyboard() )
249 msStyle
|= WS_TABSTOP
;
255 wxSize
wxControl::DoGetBestSize() const
257 return wxSize(DEFAULT_ITEM_WIDTH
, DEFAULT_ITEM_HEIGHT
);
260 wxBorder
wxControl::GetDefaultBorder() const
262 return wxControlBase::GetDefaultBorder();
265 // This is a helper for all wxControls made with UPDOWN native control.
266 // In wxMSW it was only wxSpinCtrl derived from wxSpinButton but in
267 // WinCE of Smartphones this happens also for native wxTextCtrl,
268 // wxChoice and others.
269 wxSize
wxControl::GetBestSpinnerSize(const bool is_vertical
) const
271 // take size according to layout
273 #if defined(__SMARTPHONE__) && defined(__WXWINCE__)
276 ::GetSystemMetrics(is_vertical
? SM_CXVSCROLL
: SM_CXHSCROLL
),
277 ::GetSystemMetrics(is_vertical
? SM_CYVSCROLL
: SM_CYHSCROLL
)
281 // correct size as for undocumented MSW variants cases (WinCE and perhaps others)
283 bestSize
.x
= bestSize
.y
;
285 bestSize
.y
= bestSize
.x
;
287 // double size according to layout
296 /* static */ wxVisualAttributes
297 wxControl::GetClassDefaultAttributes(wxWindowVariant
WXUNUSED(variant
))
299 wxVisualAttributes attrs
;
301 // old school (i.e. not "common") controls use the standard dialog font
303 attrs
.font
= wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
);
305 // most, or at least many, of the controls use the same colours as the
306 // buttons -- others will have to override this (and possibly simply call
307 // GetCompositeControlsDefaultAttributes() from their versions)
308 attrs
.colFg
= wxSystemSettings::GetColour(wxSYS_COLOUR_BTNTEXT
);
309 attrs
.colBg
= wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE
);
314 // another version for the "composite", i.e. non simple controls
315 /* static */ wxVisualAttributes
316 wxControl::GetCompositeControlsDefaultAttributes(wxWindowVariant
WXUNUSED(variant
))
318 wxVisualAttributes attrs
;
319 attrs
.font
= wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
);
320 attrs
.colFg
= wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT
);
321 attrs
.colBg
= wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
);
326 // ----------------------------------------------------------------------------
328 // ----------------------------------------------------------------------------
330 bool wxControl::ProcessCommand(wxCommandEvent
& event
)
332 return HandleWindowEvent(event
);
335 bool wxControl::MSWOnNotify(int idCtrl
,
339 wxEventType eventType
wxDUMMY_INITIALIZE(wxEVT_NULL
);
341 NMHDR
*hdr
= (NMHDR
*) lParam
;
345 eventType
= wxEVT_COMMAND_LEFT_CLICK
;
349 eventType
= wxEVT_COMMAND_LEFT_DCLICK
;
353 eventType
= wxEVT_COMMAND_RIGHT_CLICK
;
357 eventType
= wxEVT_COMMAND_RIGHT_DCLICK
;
361 eventType
= wxEVT_COMMAND_SET_FOCUS
;
365 eventType
= wxEVT_COMMAND_KILL_FOCUS
;
369 eventType
= wxEVT_COMMAND_ENTER
;
373 return wxWindow::MSWOnNotify(idCtrl
, lParam
, result
);
376 wxCommandEvent
event(wxEVT_NULL
, m_windowId
);
377 event
.SetEventType(eventType
);
378 event
.SetEventObject(this);
380 return HandleWindowEvent(event
);
383 WXHBRUSH
wxControl::DoMSWControlColor(WXHDC pDC
, wxColour colBg
, WXHWND hWnd
)
388 ::SetTextColor(hdc
, wxColourToRGB(GetForegroundColour()));
394 hbr
= MSWGetBgBrush(pDC
, hWnd
);
396 // if the control doesn't have any bg colour, foreground colour will be
397 // ignored as the return value would be 0 -- so forcefully give it a
398 // non default background brush in this case
399 if ( !hbr
&& m_hasFgCol
)
400 colBg
= GetBackgroundColour();
403 // use the background colour override if a valid colour is given
406 ::SetBkColor(hdc
, wxColourToRGB(colBg
));
408 // draw children with the same colour as the parent
409 wxBrush
*brush
= wxTheBrushList
->FindOrCreateBrush(colBg
,
411 hbr
= (WXHBRUSH
)brush
->GetResourceHandle();
414 // if we use custom background, we should set foreground ourselves too
415 if ( hbr
&& !m_hasFgCol
)
417 ::SetTextColor(hdc
, ::GetSysColor(COLOR_WINDOWTEXT
));
419 //else: already set above
424 WXHBRUSH
wxControl::MSWControlColor(WXHDC pDC
, WXHWND hWnd
)
428 if ( HasTransparentBackground() )
429 ::SetBkMode((HDC
)pDC
, TRANSPARENT
);
430 else // if the control is opaque it shouldn't use the parents background
431 colBg
= GetBackgroundColour();
433 return DoMSWControlColor(pDC
, colBg
, hWnd
);
436 WXHBRUSH
wxControl::MSWControlColorDisabled(WXHDC pDC
)
438 return DoMSWControlColor(pDC
,
439 wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE
),
443 // ----------------------------------------------------------------------------
444 // wxControlWithItems
445 // ----------------------------------------------------------------------------
447 void wxControlWithItems::MSWAllocStorage(const wxArrayStringsAdapter
& items
,
450 const unsigned numItems
= items
.GetCount();
451 unsigned long totalTextLength
= numItems
; // for trailing '\0' characters
452 for ( unsigned i
= 0; i
< numItems
; ++i
)
454 totalTextLength
+= items
[i
].length();
457 if ( SendMessage((HWND
)MSWGetItemsHWND(), wm
, numItems
,
458 (LPARAM
)totalTextLength
*sizeof(wxChar
)) == LB_ERRSPACE
)
460 wxLogLastError(wxT("SendMessage(XX_INITSTORAGE)"));
464 int wxControlWithItems::MSWInsertOrAppendItem(unsigned pos
,
465 const wxString
& item
,
468 LRESULT n
= SendMessage((HWND
)MSWGetItemsHWND(), wm
, pos
,
469 (LPARAM
)item
.wx_str());
470 if ( n
== CB_ERR
|| n
== CB_ERRSPACE
)
472 wxLogLastError(wxT("SendMessage(XX_ADD/INSERTSTRING)"));
479 // ---------------------------------------------------------------------------
481 // ---------------------------------------------------------------------------
483 // this is used in radiobox.cpp and slider95.cpp and should be removed as soon
484 // as it is not needed there any more!
486 // Call this repeatedly for several wnds to find the overall size
488 // Call it initially with wxDefaultCoord for all values in rect.
489 // Keep calling for other widgets, and rect will be modified
490 // to calculate largest bounding rectangle.
491 void wxFindMaxSize(WXHWND wnd
, RECT
*rect
)
493 int left
= rect
->left
;
494 int right
= rect
->right
;
496 int bottom
= rect
->bottom
;
498 GetWindowRect((HWND
) wnd
, rect
);
503 if (left
< rect
->left
)
506 if (right
> rect
->right
)
512 if (bottom
> rect
->bottom
)
513 rect
->bottom
= bottom
;
516 #endif // wxUSE_CONTROLS