1 /////////////////////////////////////////////////////////////////////////////
2 // Name: 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 #if defined(__WIN95__) && !(defined(__GNUWIN32_OLD__) && !defined(__CYGWIN10__))
50 // ----------------------------------------------------------------------------
52 // ----------------------------------------------------------------------------
54 IMPLEMENT_ABSTRACT_CLASS(wxControl
, wxWindow
)
56 // ============================================================================
57 // wxControl implementation
58 // ============================================================================
60 // ----------------------------------------------------------------------------
61 // wxControl ctor/dtor
62 // ----------------------------------------------------------------------------
64 wxControl::~wxControl()
66 m_isBeingDeleted
= true;
69 // ----------------------------------------------------------------------------
70 // control window creation
71 // ----------------------------------------------------------------------------
73 bool wxControl::Create(wxWindow
*parent
,
78 const wxValidator
& wxVALIDATOR_PARAM(validator
),
81 if ( !wxWindow::Create(parent
, id
, pos
, size
, style
, name
) )
85 SetValidator(validator
);
91 bool wxControl::MSWCreateControl(const wxChar
*classname
,
92 const wxString
& label
,
97 WXDWORD msStyle
= MSWGetStyle(GetWindowStyle(), &exstyle
);
99 return MSWCreateControl(classname
, msStyle
, pos
, size
, label
, exstyle
);
102 bool wxControl::MSWCreateControl(const wxChar
*classname
,
106 const wxString
& label
,
109 // if no extended style given, determine it ourselves
110 if ( exstyle
== (WXDWORD
)-1 )
113 (void) MSWGetStyle(GetWindowStyle(), &exstyle
);
116 // all controls should have this style
119 // create the control visible if it's currently shown for wxWidgets
125 // choose the position for the control: we have a problem with default size
126 // here as we can't calculate the best size before the control exists
127 // (DoGetBestSize() may need to use m_hWnd), so just choose the minimal
128 // possible but non 0 size because 0 window width/height result in problems
130 int x
= pos
.x
== wxDefaultCoord
? 0 : pos
.x
,
131 y
= pos
.y
== wxDefaultCoord
? 0 : pos
.y
,
132 w
= size
.x
== wxDefaultCoord
? 1 : size
.x
,
133 h
= size
.y
== wxDefaultCoord
? 1 : size
.y
;
135 // ... and adjust it to account for a possible parent frames toolbar
136 AdjustForParentClientOrigin(x
, y
);
138 m_hWnd
= (WXHWND
)::CreateWindowEx
140 exstyle
, // extended style
141 classname
, // the kind of control to create
142 label
, // the window name
143 style
, // the window style
144 x
, y
, w
, h
, // the window position and size
145 GetHwndOf(GetParent()), // parent
146 (HMENU
)GetId(), // child id
147 wxGetInstance(), // app instance
148 NULL
// creation parameters
154 wxFAIL_MSG(wxString::Format
156 _T("CreateWindowEx(\"%s\", flags=%08x, ex=%08x) failed"),
157 classname
, (unsigned int)style
, (unsigned int)exstyle
159 #endif // __WXDEBUG__
164 // install wxWidgets window proc for this window
167 // set up fonts and colours
170 SetFont(GetDefaultAttributes().font
);
172 // set the size now if no initial size specified
173 SetInitialBestSize(size
);
178 // ----------------------------------------------------------------------------
180 // ----------------------------------------------------------------------------
182 wxBorder
wxControl::GetDefaultBorder() const
184 // we want to automatically give controls a sunken style (confusingly,
185 // it may not really mean sunken at all as we map it to WS_EX_CLIENTEDGE
186 // which is not sunken at all under Windows XP -- rather, just the default)
187 #if defined(__POCKETPC__) || defined(__SMARTPHONE__)
188 return wxBORDER_SIMPLE
;
190 return wxBORDER_SUNKEN
;
194 WXDWORD
wxControl::MSWGetStyle(long style
, WXDWORD
*exstyle
) const
196 long msStyle
= wxWindow::MSWGetStyle(style
, exstyle
);
198 if ( AcceptsFocus() )
200 msStyle
|= WS_TABSTOP
;
206 wxSize
wxControl::DoGetBestSize() const
208 return wxSize(DEFAULT_ITEM_WIDTH
, DEFAULT_ITEM_HEIGHT
);
211 // This is a helper for all wxControls made with UPDOWN native control.
212 // In wxMSW it was only wxSpinCtrl derived from wxSpinButton but in
213 // WinCE of Smartphones this happens also for native wxTextCtrl,
214 // wxChoice and others.
215 wxSize
wxControl::GetBestSpinnerSize(const bool is_vertical
) const
217 // take size according to layout
219 #if defined(__SMARTPHONE__) && defined(__WXWINCE__)
222 ::GetSystemMetrics(is_vertical
? SM_CXVSCROLL
: SM_CXHSCROLL
),
223 ::GetSystemMetrics(is_vertical
? SM_CYVSCROLL
: SM_CYHSCROLL
)
227 // correct size as for undocumented MSW variants cases (WinCE and perhaps others)
229 bestSize
.x
= bestSize
.y
;
231 bestSize
.y
= bestSize
.x
;
233 // double size according to layout
242 /* static */ wxVisualAttributes
243 wxControl::GetClassDefaultAttributes(wxWindowVariant
WXUNUSED(variant
))
245 wxVisualAttributes attrs
;
247 // old school (i.e. not "common") controls use the standard dialog font
249 attrs
.font
= wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
);
251 // most, or at least many, of the controls use the same colours as the
252 // buttons -- others will have to override this (and possibly simply call
253 // GetCompositeControlsDefaultAttributes() from their versions)
254 attrs
.colFg
= wxSystemSettings::GetColour(wxSYS_COLOUR_BTNTEXT
);
255 attrs
.colBg
= wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE
);
260 // another version for the "composite", i.e. non simple controls
261 /* static */ wxVisualAttributes
262 wxControl::GetCompositeControlsDefaultAttributes(wxWindowVariant
WXUNUSED(variant
))
264 wxVisualAttributes attrs
;
265 attrs
.font
= wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
);
266 attrs
.colFg
= wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT
);
267 attrs
.colBg
= wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
);
272 // ----------------------------------------------------------------------------
274 // ----------------------------------------------------------------------------
276 bool wxControl::ProcessCommand(wxCommandEvent
& event
)
278 return GetEventHandler()->ProcessEvent(event
);
282 bool wxControl::MSWOnNotify(int idCtrl
,
286 wxEventType eventType
wxDUMMY_INITIALIZE(wxEVT_NULL
);
288 NMHDR
*hdr
= (NMHDR
*) lParam
;
292 eventType
= wxEVT_COMMAND_LEFT_CLICK
;
296 eventType
= wxEVT_COMMAND_LEFT_DCLICK
;
300 eventType
= wxEVT_COMMAND_RIGHT_CLICK
;
304 eventType
= wxEVT_COMMAND_RIGHT_DCLICK
;
308 eventType
= wxEVT_COMMAND_SET_FOCUS
;
312 eventType
= wxEVT_COMMAND_KILL_FOCUS
;
316 eventType
= wxEVT_COMMAND_ENTER
;
320 return wxWindow::MSWOnNotify(idCtrl
, lParam
, result
);
323 wxCommandEvent
event(wxEVT_NULL
, m_windowId
);
324 event
.SetEventType(eventType
);
325 event
.SetEventObject(this);
327 return GetEventHandler()->ProcessEvent(event
);
331 WXHBRUSH
wxControl::DoMSWControlColor(WXHDC pDC
, wxColour colBg
, WXHWND hWnd
)
336 ::SetTextColor(hdc
, wxColourToRGB(GetForegroundColour()));
342 hbr
= MSWGetBgBrush(pDC
, hWnd
);
344 // if the control doesn't have any bg colour, foreground colour will be
345 // ignored as the return value would be 0 -- so forcefully give it a
346 // non default background brush in this case
347 if ( !hbr
&& m_hasFgCol
)
348 colBg
= GetBackgroundColour();
351 // use the background colour override if a valid colour is given
354 ::SetBkColor(hdc
, wxColourToRGB(colBg
));
356 // draw children with the same colour as the parent
357 wxBrush
*brush
= wxTheBrushList
->FindOrCreateBrush(colBg
, wxSOLID
);
359 hbr
= (WXHBRUSH
)brush
->GetResourceHandle();
361 // if we use custom background, we should set foreground ourselves too
364 ::SetTextColor(hdc
, ::GetSysColor(COLOR_WINDOWTEXT
));
366 //else: already set above
372 WXHBRUSH
wxControl::MSWControlColor(WXHDC pDC
, WXHWND hWnd
)
376 if ( HasTransparentBackground() )
377 ::SetBkMode((HDC
)pDC
, TRANSPARENT
);
378 else // if the control is opaque it shouldn't use the parents background
379 colBg
= GetBackgroundColour();
381 return DoMSWControlColor(pDC
, colBg
, hWnd
);
384 WXHBRUSH
wxControl::MSWControlColorDisabled(WXHDC pDC
)
386 return DoMSWControlColor(pDC
,
387 wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE
),
391 // ---------------------------------------------------------------------------
393 // ---------------------------------------------------------------------------
395 // this is used in radiobox.cpp and slider95.cpp and should be removed as soon
396 // as it is not needed there any more!
398 // Call this repeatedly for several wnds to find the overall size
400 // Call it initially with wxDefaultCoord for all values in rect.
401 // Keep calling for other widgets, and rect will be modified
402 // to calculate largest bounding rectangle.
403 void wxFindMaxSize(WXHWND wnd
, RECT
*rect
)
405 int left
= rect
->left
;
406 int right
= rect
->right
;
408 int bottom
= rect
->bottom
;
410 GetWindowRect((HWND
) wnd
, rect
);
415 if (left
< rect
->left
)
418 if (right
> rect
->right
)
424 if (bottom
> rect
->bottom
)
425 rect
->bottom
= bottom
;
428 #endif // wxUSE_CONTROLS