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"
34 #include "wx/dcclient.h"
36 #include "wx/settings.h"
40 #include "wx/listctrl.h"
41 #endif // wxUSE_LISTCTRL
44 #include "wx/treectrl.h"
45 #endif // wxUSE_TREECTRL
47 #include "wx/msw/private.h"
48 #include "wx/msw/uxtheme.h"
50 // include <commctrl.h> "properly"
51 #include "wx/msw/wrapcctl.h"
53 // ----------------------------------------------------------------------------
55 // ----------------------------------------------------------------------------
57 IMPLEMENT_ABSTRACT_CLASS(wxControl
, wxWindow
)
59 // ============================================================================
60 // wxControl implementation
61 // ============================================================================
63 // ----------------------------------------------------------------------------
64 // wxControl ctor/dtor
65 // ----------------------------------------------------------------------------
67 wxControl::~wxControl()
69 m_isBeingDeleted
= true;
72 // ----------------------------------------------------------------------------
73 // control window creation
74 // ----------------------------------------------------------------------------
76 bool wxControl::Create(wxWindow
*parent
,
81 const wxValidator
& wxVALIDATOR_PARAM(validator
),
84 if ( !wxWindow::Create(parent
, id
, pos
, size
, style
, name
) )
88 SetValidator(validator
);
94 bool wxControl::MSWCreateControl(const wxChar
*classname
,
95 const wxString
& label
,
100 WXDWORD msStyle
= MSWGetStyle(GetWindowStyle(), &exstyle
);
102 return MSWCreateControl(classname
, msStyle
, pos
, size
, label
, exstyle
);
105 bool wxControl::MSWCreateControl(const wxChar
*classname
,
109 const wxString
& label
,
112 // if no extended style given, determine it ourselves
113 if ( exstyle
== (WXDWORD
)-1 )
116 (void) MSWGetStyle(GetWindowStyle(), &exstyle
);
119 // all controls should have this style
122 // create the control visible if it's currently shown for wxWidgets
128 // choose the position for the control: we have a problem with default size
129 // here as we can't calculate the best size before the control exists
130 // (DoGetBestSize() may need to use m_hWnd), so just choose the minimal
131 // possible but non 0 size because 0 window width/height result in problems
133 int x
= pos
.x
== wxDefaultCoord
? 0 : pos
.x
,
134 y
= pos
.y
== wxDefaultCoord
? 0 : pos
.y
,
135 w
= size
.x
== wxDefaultCoord
? 1 : size
.x
,
136 h
= size
.y
== wxDefaultCoord
? 1 : size
.y
;
138 // ... and adjust it to account for a possible parent frames toolbar
139 AdjustForParentClientOrigin(x
, y
);
141 m_hWnd
= (WXHWND
)::CreateWindowEx
143 exstyle
, // extended style
144 classname
, // the kind of control to create
145 label
, // the window name
146 style
, // the window style
147 x
, y
, w
, h
, // the window position and size
148 GetHwndOf(GetParent()), // parent
149 (HMENU
)GetId(), // child id
150 wxGetInstance(), // app instance
151 NULL
// creation parameters
157 wxFAIL_MSG(wxString::Format
159 _T("CreateWindowEx(\"%s\", flags=%08x, ex=%08x) failed"),
160 classname
, (unsigned int)style
, (unsigned int)exstyle
162 #endif // __WXDEBUG__
167 // install wxWidgets window proc for this window
170 // set up fonts and colours
174 #if wxUSE_LISTCTRL || wxUSE_TREECTRL
175 // if we set a font for {list,tree}ctrls and the font size is changed in
176 // the display properties then the font size for these controls doesn't
177 // automatically adjust when they receive WM_SETTINGCHANGE
178 if ( wxDynamicCastThis(wxListCtrl
) || wxDynamicCastThis(wxTreeCtrl
) )
180 // not sure if we need to explicitly set the font here for Win95/NT4
181 // but we definitely can't do it for any newer version
182 // see wxGetCCDefaultFont() in src/msw/settings.cpp for explanation
183 // of why this test works
185 // TODO: test Win95/NT4 to see if this is needed or breaks the
186 // font resizing as it does on newer versions
187 wxFont font
= GetDefaultAttributes().font
;
188 if ( font
== wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
) )
194 #endif // wxUSE_LISTCTRL || wxUSE_TREECTRL
196 SetFont(GetDefaultAttributes().font
);
200 // set the size now if no initial size specified
201 SetInitialBestSize(size
);
206 // ----------------------------------------------------------------------------
208 // ----------------------------------------------------------------------------
210 wxBorder
wxControl::GetDefaultBorder() const
212 // we want to automatically give controls a sunken style (confusingly,
213 // it may not really mean sunken at all as we map it to WS_EX_CLIENTEDGE
214 // which is not sunken at all under Windows XP -- rather, just the default)
215 #if defined(__POCKETPC__) || defined(__SMARTPHONE__)
216 return wxBORDER_SIMPLE
;
218 return wxBORDER_SUNKEN
;
222 WXDWORD
wxControl::MSWGetStyle(long style
, WXDWORD
*exstyle
) const
224 long msStyle
= wxWindow::MSWGetStyle(style
, exstyle
);
226 if ( AcceptsFocus() )
228 msStyle
|= WS_TABSTOP
;
234 wxSize
wxControl::DoGetBestSize() const
236 return wxSize(DEFAULT_ITEM_WIDTH
, DEFAULT_ITEM_HEIGHT
);
239 // This is a helper for all wxControls made with UPDOWN native control.
240 // In wxMSW it was only wxSpinCtrl derived from wxSpinButton but in
241 // WinCE of Smartphones this happens also for native wxTextCtrl,
242 // wxChoice and others.
243 wxSize
wxControl::GetBestSpinnerSize(const bool is_vertical
) const
245 // take size according to layout
247 #if defined(__SMARTPHONE__) && defined(__WXWINCE__)
250 ::GetSystemMetrics(is_vertical
? SM_CXVSCROLL
: SM_CXHSCROLL
),
251 ::GetSystemMetrics(is_vertical
? SM_CYVSCROLL
: SM_CYHSCROLL
)
255 // correct size as for undocumented MSW variants cases (WinCE and perhaps others)
257 bestSize
.x
= bestSize
.y
;
259 bestSize
.y
= bestSize
.x
;
261 // double size according to layout
270 /* static */ wxVisualAttributes
271 wxControl::GetClassDefaultAttributes(wxWindowVariant
WXUNUSED(variant
))
273 wxVisualAttributes attrs
;
275 // old school (i.e. not "common") controls use the standard dialog font
277 attrs
.font
= wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
);
279 // most, or at least many, of the controls use the same colours as the
280 // buttons -- others will have to override this (and possibly simply call
281 // GetCompositeControlsDefaultAttributes() from their versions)
282 attrs
.colFg
= wxSystemSettings::GetColour(wxSYS_COLOUR_BTNTEXT
);
283 attrs
.colBg
= wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE
);
288 // another version for the "composite", i.e. non simple controls
289 /* static */ wxVisualAttributes
290 wxControl::GetCompositeControlsDefaultAttributes(wxWindowVariant
WXUNUSED(variant
))
292 wxVisualAttributes attrs
;
293 attrs
.font
= wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
);
294 attrs
.colFg
= wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT
);
295 attrs
.colBg
= wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
);
300 // ----------------------------------------------------------------------------
302 // ----------------------------------------------------------------------------
304 bool wxControl::ProcessCommand(wxCommandEvent
& event
)
306 return GetEventHandler()->ProcessEvent(event
);
309 bool wxControl::MSWOnNotify(int idCtrl
,
313 wxEventType eventType
wxDUMMY_INITIALIZE(wxEVT_NULL
);
315 NMHDR
*hdr
= (NMHDR
*) lParam
;
319 eventType
= wxEVT_COMMAND_LEFT_CLICK
;
323 eventType
= wxEVT_COMMAND_LEFT_DCLICK
;
327 eventType
= wxEVT_COMMAND_RIGHT_CLICK
;
331 eventType
= wxEVT_COMMAND_RIGHT_DCLICK
;
335 eventType
= wxEVT_COMMAND_SET_FOCUS
;
339 eventType
= wxEVT_COMMAND_KILL_FOCUS
;
343 eventType
= wxEVT_COMMAND_ENTER
;
347 return wxWindow::MSWOnNotify(idCtrl
, lParam
, result
);
350 wxCommandEvent
event(wxEVT_NULL
, m_windowId
);
351 event
.SetEventType(eventType
);
352 event
.SetEventObject(this);
354 return GetEventHandler()->ProcessEvent(event
);
357 WXHBRUSH
wxControl::DoMSWControlColor(WXHDC pDC
, wxColour colBg
, WXHWND hWnd
)
362 ::SetTextColor(hdc
, wxColourToRGB(GetForegroundColour()));
368 hbr
= MSWGetBgBrush(pDC
, hWnd
);
370 // if the control doesn't have any bg colour, foreground colour will be
371 // ignored as the return value would be 0 -- so forcefully give it a
372 // non default background brush in this case
373 if ( !hbr
&& m_hasFgCol
)
374 colBg
= GetBackgroundColour();
377 // use the background colour override if a valid colour is given
380 ::SetBkColor(hdc
, wxColourToRGB(colBg
));
382 // draw children with the same colour as the parent
383 wxBrush
*brush
= wxTheBrushList
->FindOrCreateBrush(colBg
, wxSOLID
);
385 hbr
= (WXHBRUSH
)brush
->GetResourceHandle();
387 // if we use custom background, we should set foreground ourselves too
390 ::SetTextColor(hdc
, ::GetSysColor(COLOR_WINDOWTEXT
));
392 //else: already set above
398 WXHBRUSH
wxControl::MSWControlColor(WXHDC pDC
, WXHWND hWnd
)
402 if ( HasTransparentBackground() )
403 ::SetBkMode((HDC
)pDC
, TRANSPARENT
);
404 else // if the control is opaque it shouldn't use the parents background
405 colBg
= GetBackgroundColour();
407 return DoMSWControlColor(pDC
, colBg
, hWnd
);
410 WXHBRUSH
wxControl::MSWControlColorDisabled(WXHDC pDC
)
412 return DoMSWControlColor(pDC
,
413 wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE
),
417 // ---------------------------------------------------------------------------
419 // ---------------------------------------------------------------------------
421 // this is used in radiobox.cpp and slider95.cpp and should be removed as soon
422 // as it is not needed there any more!
424 // Call this repeatedly for several wnds to find the overall size
426 // Call it initially with wxDefaultCoord for all values in rect.
427 // Keep calling for other widgets, and rect will be modified
428 // to calculate largest bounding rectangle.
429 void wxFindMaxSize(WXHWND wnd
, RECT
*rect
)
431 int left
= rect
->left
;
432 int right
= rect
->right
;
434 int bottom
= rect
->bottom
;
436 GetWindowRect((HWND
) wnd
, rect
);
441 if (left
< rect
->left
)
444 if (right
> rect
->right
)
450 if (bottom
> rect
->bottom
)
451 rect
->bottom
= bottom
;
454 #endif // wxUSE_CONTROLS