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
)GetId(), // child id
149 wxGetInstance(), // app instance
150 NULL
// creation parameters
156 wxFAIL_MSG(wxString::Format
158 _T("CreateWindowEx(\"%s\", flags=%08x, ex=%08x) failed"),
159 classname
, (unsigned int)style
, (unsigned int)exstyle
161 #endif // __WXDEBUG__
166 // saving the label in m_labelOrig to return it verbatim
167 // later in GetLabel()
170 // install wxWidgets window proc for this window
173 // set up fonts and colours
179 wxFont font
= GetDefaultAttributes().font
;
181 // if we set a font for {list,tree}ctrls and the font size is changed in
182 // the display properties then the font size for these controls doesn't
183 // automatically adjust when they receive WM_SETTINGCHANGE
185 // FIXME: replace the dynamic casts with virtual function calls!!
186 #if wxUSE_LISTCTRL || wxUSE_TREECTRL
187 bool testFont
= false;
189 if ( wxDynamicCastThis(wxListCtrl
) )
191 #endif // wxUSE_LISTCTRL
193 if ( wxDynamicCastThis(wxTreeCtrl
) )
195 #endif // wxUSE_TREECTRL
199 // not sure if we need to explicitly set the font here for Win95/NT4
200 // but we definitely can't do it for any newer version
201 // see wxGetCCDefaultFont() in src/msw/settings.cpp for explanation
202 // of why this test works
204 // TODO: test Win95/NT4 to see if this is needed or breaks the
205 // font resizing as it does on newer versions
206 if ( font
!= wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
) )
211 #endif // wxUSE_LISTCTRL || wxUSE_TREECTRL
215 SetFont(GetDefaultAttributes().font
);
219 // set the size now if no initial size specified
220 SetInitialSize(size
);
225 // ----------------------------------------------------------------------------
227 // ----------------------------------------------------------------------------
229 WXDWORD
wxControl::MSWGetStyle(long style
, WXDWORD
*exstyle
) const
231 long msStyle
= wxWindow::MSWGetStyle(style
, exstyle
);
233 if ( AcceptsFocusFromKeyboard() )
235 msStyle
|= WS_TABSTOP
;
241 wxSize
wxControl::DoGetBestSize() const
243 return wxSize(DEFAULT_ITEM_WIDTH
, DEFAULT_ITEM_HEIGHT
);
246 wxBorder
wxControl::GetDefaultBorder() const
248 return wxControlBase::GetDefaultBorder();
250 // return GetDefaultBorderForControl();
253 // This is a helper for all wxControls made with UPDOWN native control.
254 // In wxMSW it was only wxSpinCtrl derived from wxSpinButton but in
255 // WinCE of Smartphones this happens also for native wxTextCtrl,
256 // wxChoice and others.
257 wxSize
wxControl::GetBestSpinnerSize(const bool is_vertical
) const
259 // take size according to layout
261 #if defined(__SMARTPHONE__) && defined(__WXWINCE__)
264 ::GetSystemMetrics(is_vertical
? SM_CXVSCROLL
: SM_CXHSCROLL
),
265 ::GetSystemMetrics(is_vertical
? SM_CYVSCROLL
: SM_CYHSCROLL
)
269 // correct size as for undocumented MSW variants cases (WinCE and perhaps others)
271 bestSize
.x
= bestSize
.y
;
273 bestSize
.y
= bestSize
.x
;
275 // double size according to layout
284 /* static */ wxVisualAttributes
285 wxControl::GetClassDefaultAttributes(wxWindowVariant
WXUNUSED(variant
))
287 wxVisualAttributes attrs
;
289 // old school (i.e. not "common") controls use the standard dialog font
291 attrs
.font
= wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
);
293 // most, or at least many, of the controls use the same colours as the
294 // buttons -- others will have to override this (and possibly simply call
295 // GetCompositeControlsDefaultAttributes() from their versions)
296 attrs
.colFg
= wxSystemSettings::GetColour(wxSYS_COLOUR_BTNTEXT
);
297 attrs
.colBg
= wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE
);
302 // another version for the "composite", i.e. non simple controls
303 /* static */ wxVisualAttributes
304 wxControl::GetCompositeControlsDefaultAttributes(wxWindowVariant
WXUNUSED(variant
))
306 wxVisualAttributes attrs
;
307 attrs
.font
= wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
);
308 attrs
.colFg
= wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT
);
309 attrs
.colBg
= wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
);
314 // ----------------------------------------------------------------------------
316 // ----------------------------------------------------------------------------
318 bool wxControl::ProcessCommand(wxCommandEvent
& event
)
320 return GetEventHandler()->ProcessEvent(event
);
323 bool wxControl::MSWOnNotify(int idCtrl
,
327 wxEventType eventType
wxDUMMY_INITIALIZE(wxEVT_NULL
);
329 NMHDR
*hdr
= (NMHDR
*) lParam
;
333 eventType
= wxEVT_COMMAND_LEFT_CLICK
;
337 eventType
= wxEVT_COMMAND_LEFT_DCLICK
;
341 eventType
= wxEVT_COMMAND_RIGHT_CLICK
;
345 eventType
= wxEVT_COMMAND_RIGHT_DCLICK
;
349 eventType
= wxEVT_COMMAND_SET_FOCUS
;
353 eventType
= wxEVT_COMMAND_KILL_FOCUS
;
357 eventType
= wxEVT_COMMAND_ENTER
;
361 return wxWindow::MSWOnNotify(idCtrl
, lParam
, result
);
364 wxCommandEvent
event(wxEVT_NULL
, m_windowId
);
365 event
.SetEventType(eventType
);
366 event
.SetEventObject(this);
368 return GetEventHandler()->ProcessEvent(event
);
371 WXHBRUSH
wxControl::DoMSWControlColor(WXHDC pDC
, wxColour colBg
, WXHWND hWnd
)
376 ::SetTextColor(hdc
, wxColourToRGB(GetForegroundColour()));
382 hbr
= MSWGetBgBrush(pDC
, hWnd
);
384 // if the control doesn't have any bg colour, foreground colour will be
385 // ignored as the return value would be 0 -- so forcefully give it a
386 // non default background brush in this case
387 if ( !hbr
&& m_hasFgCol
)
388 colBg
= GetBackgroundColour();
391 // use the background colour override if a valid colour is given
394 ::SetBkColor(hdc
, wxColourToRGB(colBg
));
396 // draw children with the same colour as the parent
397 wxBrush
*brush
= wxTheBrushList
->FindOrCreateBrush(colBg
, wxSOLID
);
399 hbr
= (WXHBRUSH
)brush
->GetResourceHandle();
403 // if we use custom background, we should set foreground ourselves too
404 if ( hbr
&& !m_hasFgCol
)
406 ::SetTextColor(hdc
, ::GetSysColor(COLOR_WINDOWTEXT
));
408 //else: already set above
413 WXHBRUSH
wxControl::MSWControlColor(WXHDC pDC
, WXHWND hWnd
)
417 if ( HasTransparentBackground() )
418 ::SetBkMode((HDC
)pDC
, TRANSPARENT
);
419 else // if the control is opaque it shouldn't use the parents background
420 colBg
= GetBackgroundColour();
422 return DoMSWControlColor(pDC
, colBg
, hWnd
);
425 WXHBRUSH
wxControl::MSWControlColorDisabled(WXHDC pDC
)
427 return DoMSWControlColor(pDC
,
428 wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE
),
432 // ----------------------------------------------------------------------------
433 // wxControlWithItems
434 // ----------------------------------------------------------------------------
436 void wxControlWithItems::MSWAllocStorage(const wxArrayStringsAdapter
& items
,
439 const unsigned numItems
= items
.GetCount();
440 unsigned long totalTextLength
= numItems
; // for trailing '\0' characters
441 for ( unsigned i
= 0; i
< numItems
; ++i
)
443 totalTextLength
+= items
[i
].length();
446 if ( SendMessage((HWND
)MSWGetItemsHWND(), wm
, numItems
,
447 (LPARAM
)totalTextLength
*sizeof(wxChar
)) == LB_ERRSPACE
)
449 wxLogLastError(wxT("SendMessage(XX_INITSTORAGE)"));
453 int wxControlWithItems::MSWInsertOrAppendItem(unsigned pos
,
454 const wxString
& item
,
457 LRESULT n
= SendMessage((HWND
)MSWGetItemsHWND(), wm
, pos
,
458 (LPARAM
)item
.wx_str());
459 if ( n
== CB_ERR
|| n
== CB_ERRSPACE
)
461 wxLogLastError(wxT("SendMessage(XX_ADD/INSERTSTRING)"));
468 // ---------------------------------------------------------------------------
470 // ---------------------------------------------------------------------------
472 // this is used in radiobox.cpp and slider95.cpp and should be removed as soon
473 // as it is not needed there any more!
475 // Call this repeatedly for several wnds to find the overall size
477 // Call it initially with wxDefaultCoord for all values in rect.
478 // Keep calling for other widgets, and rect will be modified
479 // to calculate largest bounding rectangle.
480 void wxFindMaxSize(WXHWND wnd
, RECT
*rect
)
482 int left
= rect
->left
;
483 int right
= rect
->right
;
485 int bottom
= rect
->bottom
;
487 GetWindowRect((HWND
) wnd
, rect
);
492 if (left
< rect
->left
)
495 if (right
> rect
->right
)
501 if (bottom
> rect
->bottom
)
502 rect
->bottom
= bottom
;
505 #endif // wxUSE_CONTROLS