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"
32 #include "wx/dcclient.h"
34 #include "wx/settings.h"
37 #include "wx/control.h"
40 #include "wx/notebook.h"
41 #endif // wxUSE_NOTEBOOK
43 #include "wx/msw/private.h"
44 #include "wx/msw/uxtheme.h"
46 // include <commctrl.h> "properly"
47 #include "wx/msw/wrapcctl.h"
49 // ----------------------------------------------------------------------------
51 // ----------------------------------------------------------------------------
53 IMPLEMENT_ABSTRACT_CLASS(wxControl
, wxWindow
)
55 // ============================================================================
56 // wxControl implementation
57 // ============================================================================
59 // ----------------------------------------------------------------------------
60 // wxControl ctor/dtor
61 // ----------------------------------------------------------------------------
63 wxControl::~wxControl()
65 m_isBeingDeleted
= true;
68 // ----------------------------------------------------------------------------
69 // control window creation
70 // ----------------------------------------------------------------------------
72 bool wxControl::Create(wxWindow
*parent
,
77 const wxValidator
& wxVALIDATOR_PARAM(validator
),
80 if ( !wxWindow::Create(parent
, id
, pos
, size
, style
, name
) )
84 SetValidator(validator
);
90 bool wxControl::MSWCreateControl(const wxChar
*classname
,
91 const wxString
& label
,
96 WXDWORD msStyle
= MSWGetStyle(GetWindowStyle(), &exstyle
);
98 return MSWCreateControl(classname
, msStyle
, pos
, size
, label
, exstyle
);
101 bool wxControl::MSWCreateControl(const wxChar
*classname
,
105 const wxString
& label
,
108 // if no extended style given, determine it ourselves
109 if ( exstyle
== (WXDWORD
)-1 )
112 (void) MSWGetStyle(GetWindowStyle(), &exstyle
);
115 // all controls should have this style
118 // create the control visible if it's currently shown for wxWidgets
124 // choose the position for the control: we have a problem with default size
125 // here as we can't calculate the best size before the control exists
126 // (DoGetBestSize() may need to use m_hWnd), so just choose the minimal
127 // possible but non 0 size because 0 window width/height result in problems
129 int x
= pos
.x
== wxDefaultCoord
? 0 : pos
.x
,
130 y
= pos
.y
== wxDefaultCoord
? 0 : pos
.y
,
131 w
= size
.x
== wxDefaultCoord
? 1 : size
.x
,
132 h
= size
.y
== wxDefaultCoord
? 1 : size
.y
;
134 // ... and adjust it to account for a possible parent frames toolbar
135 AdjustForParentClientOrigin(x
, y
);
137 m_hWnd
= (WXHWND
)::CreateWindowEx
139 exstyle
, // extended style
140 classname
, // the kind of control to create
141 label
, // the window name
142 style
, // the window style
143 x
, y
, w
, h
, // the window position and size
144 GetHwndOf(GetParent()), // parent
145 (HMENU
)GetId(), // child id
146 wxGetInstance(), // app instance
147 NULL
// creation parameters
153 wxFAIL_MSG(wxString::Format
155 _T("CreateWindowEx(\"%s\", flags=%08x, ex=%08x) failed"),
156 classname
, (unsigned int)style
, (unsigned int)exstyle
158 #endif // __WXDEBUG__
163 // install wxWidgets window proc for this window
166 // set up fonts and colours
169 SetFont(GetDefaultAttributes().font
);
171 // set the size now if no initial size specified
172 SetInitialBestSize(size
);
177 // ----------------------------------------------------------------------------
179 // ----------------------------------------------------------------------------
181 wxBorder
wxControl::GetDefaultBorder() const
183 // we want to automatically give controls a sunken style (confusingly,
184 // it may not really mean sunken at all as we map it to WS_EX_CLIENTEDGE
185 // which is not sunken at all under Windows XP -- rather, just the default)
186 #if defined(__POCKETPC__) || defined(__SMARTPHONE__)
187 return wxBORDER_SIMPLE
;
189 return wxBORDER_SUNKEN
;
193 WXDWORD
wxControl::MSWGetStyle(long style
, WXDWORD
*exstyle
) const
195 long msStyle
= wxWindow::MSWGetStyle(style
, exstyle
);
197 if ( AcceptsFocus() )
199 msStyle
|= WS_TABSTOP
;
205 wxSize
wxControl::DoGetBestSize() const
207 return wxSize(DEFAULT_ITEM_WIDTH
, DEFAULT_ITEM_HEIGHT
);
210 // This is a helper for all wxControls made with UPDOWN native control.
211 // In wxMSW it was only wxSpinCtrl derived from wxSpinButton but in
212 // WinCE of Smartphones this happens also for native wxTextCtrl,
213 // wxChoice and others.
214 wxSize
wxControl::GetBestSpinnerSize(const bool is_vertical
) const
216 // take size according to layout
218 #if defined(__SMARTPHONE__) && defined(__WXWINCE__)
221 ::GetSystemMetrics(is_vertical
? SM_CXVSCROLL
: SM_CXHSCROLL
),
222 ::GetSystemMetrics(is_vertical
? SM_CYVSCROLL
: SM_CYHSCROLL
)
226 // correct size as for undocumented MSW variants cases (WinCE and perhaps others)
228 bestSize
.x
= bestSize
.y
;
230 bestSize
.y
= bestSize
.x
;
232 // double size according to layout
241 /* static */ wxVisualAttributes
242 wxControl::GetClassDefaultAttributes(wxWindowVariant
WXUNUSED(variant
))
244 wxVisualAttributes attrs
;
246 // old school (i.e. not "common") controls use the standard dialog font
248 attrs
.font
= wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
);
250 // most, or at least many, of the controls use the same colours as the
251 // buttons -- others will have to override this (and possibly simply call
252 // GetCompositeControlsDefaultAttributes() from their versions)
253 attrs
.colFg
= wxSystemSettings::GetColour(wxSYS_COLOUR_BTNTEXT
);
254 attrs
.colBg
= wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE
);
259 // another version for the "composite", i.e. non simple controls
260 /* static */ wxVisualAttributes
261 wxControl::GetCompositeControlsDefaultAttributes(wxWindowVariant
WXUNUSED(variant
))
263 wxVisualAttributes attrs
;
264 attrs
.font
= wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
);
265 attrs
.colFg
= wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT
);
266 attrs
.colBg
= wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
);
271 // ----------------------------------------------------------------------------
273 // ----------------------------------------------------------------------------
275 bool wxControl::ProcessCommand(wxCommandEvent
& event
)
277 return GetEventHandler()->ProcessEvent(event
);
280 bool wxControl::MSWOnNotify(int idCtrl
,
284 wxEventType eventType
wxDUMMY_INITIALIZE(wxEVT_NULL
);
286 NMHDR
*hdr
= (NMHDR
*) lParam
;
290 eventType
= wxEVT_COMMAND_LEFT_CLICK
;
294 eventType
= wxEVT_COMMAND_LEFT_DCLICK
;
298 eventType
= wxEVT_COMMAND_RIGHT_CLICK
;
302 eventType
= wxEVT_COMMAND_RIGHT_DCLICK
;
306 eventType
= wxEVT_COMMAND_SET_FOCUS
;
310 eventType
= wxEVT_COMMAND_KILL_FOCUS
;
314 eventType
= wxEVT_COMMAND_ENTER
;
318 return wxWindow::MSWOnNotify(idCtrl
, lParam
, result
);
321 wxCommandEvent
event(wxEVT_NULL
, m_windowId
);
322 event
.SetEventType(eventType
);
323 event
.SetEventObject(this);
325 return GetEventHandler()->ProcessEvent(event
);
328 WXHBRUSH
wxControl::DoMSWControlColor(WXHDC pDC
, wxColour colBg
, WXHWND hWnd
)
333 ::SetTextColor(hdc
, wxColourToRGB(GetForegroundColour()));
339 hbr
= MSWGetBgBrush(pDC
, hWnd
);
341 // if the control doesn't have any bg colour, foreground colour will be
342 // ignored as the return value would be 0 -- so forcefully give it a
343 // non default background brush in this case
344 if ( !hbr
&& m_hasFgCol
)
345 colBg
= GetBackgroundColour();
348 // use the background colour override if a valid colour is given
351 ::SetBkColor(hdc
, wxColourToRGB(colBg
));
353 // draw children with the same colour as the parent
354 wxBrush
*brush
= wxTheBrushList
->FindOrCreateBrush(colBg
, wxSOLID
);
356 hbr
= (WXHBRUSH
)brush
->GetResourceHandle();
358 // if we use custom background, we should set foreground ourselves too
361 ::SetTextColor(hdc
, ::GetSysColor(COLOR_WINDOWTEXT
));
363 //else: already set above
369 WXHBRUSH
wxControl::MSWControlColor(WXHDC pDC
, WXHWND hWnd
)
373 if ( HasTransparentBackground() )
374 ::SetBkMode((HDC
)pDC
, TRANSPARENT
);
375 else // if the control is opaque it shouldn't use the parents background
376 colBg
= GetBackgroundColour();
378 return DoMSWControlColor(pDC
, colBg
, hWnd
);
381 WXHBRUSH
wxControl::MSWControlColorDisabled(WXHDC pDC
)
383 return DoMSWControlColor(pDC
,
384 wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE
),
388 // ---------------------------------------------------------------------------
390 // ---------------------------------------------------------------------------
392 // this is used in radiobox.cpp and slider95.cpp and should be removed as soon
393 // as it is not needed there any more!
395 // Call this repeatedly for several wnds to find the overall size
397 // Call it initially with wxDefaultCoord for all values in rect.
398 // Keep calling for other widgets, and rect will be modified
399 // to calculate largest bounding rectangle.
400 void wxFindMaxSize(WXHWND wnd
, RECT
*rect
)
402 int left
= rect
->left
;
403 int right
= rect
->right
;
405 int bottom
= rect
->bottom
;
407 GetWindowRect((HWND
) wnd
, rect
);
412 if (left
< rect
->left
)
415 if (right
> rect
->right
)
421 if (bottom
> rect
->bottom
)
422 rect
->bottom
= bottom
;
425 #endif // wxUSE_CONTROLS