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 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
21 #pragma implementation "control.h"
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
36 #include "wx/dcclient.h"
38 #include "wx/settings.h"
41 #include "wx/control.h"
43 #include "wx/msw/private.h"
45 #if defined(__WIN95__) && !(defined(__GNUWIN32_OLD__) && !defined(__CYGWIN10__))
49 // ----------------------------------------------------------------------------
51 // ----------------------------------------------------------------------------
53 IMPLEMENT_ABSTRACT_CLASS(wxControl
, wxWindow
)
55 BEGIN_EVENT_TABLE(wxControl
, wxWindow
)
56 EVT_ERASE_BACKGROUND(wxControl::OnEraseBackground
)
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 wxWindows
128 // choose the position for the control
129 int x
= pos
.x
== -1 ? 0 : pos
.x
,
130 y
= pos
.y
== -1 ? 0 : pos
.y
,
131 w
= size
.x
== -1 ? 0 : size
.x
,
132 h
= size
.y
== -1 ? 0 : size
.y
;
134 // ... and adjust it to account for ap ossible 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
152 wxLogDebug(wxT("Failed to create a control of class '%s'"), classname
);
153 wxFAIL_MSG(_T("something is very wrong"));
161 Ctl3dSubclassCtl(GetHwnd());
164 #endif // wxUSE_CTL3D
166 // install wxWindows window proc for this window
169 // set up fonts and colours
171 SetFont(GetDefaultAttributes().font
);
173 // set the size now if no initial size specified
174 SetInitialBestSize(size
);
179 // ----------------------------------------------------------------------------
181 // ----------------------------------------------------------------------------
183 wxBorder
wxControl::GetDefaultBorder() const
185 // we want to automatically give controls a sunken style (confusingly,
186 // it may not really mean sunken at all as we map it to WS_EX_CLIENTEDGE
187 // which is not sunken at all under Windows XP -- rather, just the default)
188 return wxBORDER_SUNKEN
;
191 WXDWORD
wxControl::MSWGetStyle(long style
, WXDWORD
*exstyle
) const
193 long msStyle
= wxWindow::MSWGetStyle(style
, exstyle
);
195 if ( AcceptsFocus() )
197 msStyle
|= WS_TABSTOP
;
203 wxSize
wxControl::DoGetBestSize() const
205 return wxSize(DEFAULT_ITEM_WIDTH
, DEFAULT_ITEM_HEIGHT
);
208 /* static */ wxVisualAttributes
209 wxControl::GetClassDefaultAttributes(wxWindowVariant
WXUNUSED(variant
))
211 wxVisualAttributes attrs
;
213 // old school (i.e. not "common") controls use the standard dialog font
215 attrs
.font
= wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
);
217 // most, or at least many, of the controls use the same colours as the
218 // buttons -- others will have to override this (and possibly simply call
219 // GetCompositeControlsDefaultAttributes() from their versions)
220 attrs
.colFg
= wxSystemSettings::GetColour(wxSYS_COLOUR_BTNTEXT
);
221 attrs
.colBg
= wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE
);
226 // another version for the "composite", i.e. non simple controls
227 /* static */ wxVisualAttributes
228 wxControl::GetCompositeControlsDefaultAttributes(wxWindowVariant
WXUNUSED(variant
))
230 wxVisualAttributes attrs
;
231 attrs
.font
= wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
);
232 attrs
.colFg
= wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT
);
233 attrs
.colBg
= wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
);
238 // ----------------------------------------------------------------------------
240 // ----------------------------------------------------------------------------
242 bool wxControl::ProcessCommand(wxCommandEvent
& event
)
244 return GetEventHandler()->ProcessEvent(event
);
248 bool wxControl::MSWOnNotify(int idCtrl
,
252 wxEventType eventType
= wxEVT_NULL
;
254 NMHDR
*hdr
= (NMHDR
*) lParam
;
258 eventType
= wxEVT_COMMAND_LEFT_CLICK
;
262 eventType
= wxEVT_COMMAND_LEFT_DCLICK
;
266 eventType
= wxEVT_COMMAND_RIGHT_CLICK
;
270 eventType
= wxEVT_COMMAND_RIGHT_DCLICK
;
274 eventType
= wxEVT_COMMAND_SET_FOCUS
;
278 eventType
= wxEVT_COMMAND_KILL_FOCUS
;
282 eventType
= wxEVT_COMMAND_ENTER
;
286 return wxWindow::MSWOnNotify(idCtrl
, lParam
, result
);
289 wxCommandEvent
event(wxEVT_NULL
, m_windowId
);
290 event
.SetEventType(eventType
);
291 event
.SetEventObject(this);
293 return GetEventHandler()->ProcessEvent(event
);
297 void wxControl::OnEraseBackground(wxEraseEvent
& event
)
299 // notice that this 'dumb' implementation may cause flicker for some of the
300 // controls in which case they should intercept wxEraseEvent and process it
301 // themselves somehow
304 ::GetClientRect(GetHwnd(), &rect
);
306 HBRUSH hBrush
= ::CreateSolidBrush(wxColourToRGB(GetBackgroundColour()));
308 HDC hdc
= GetHdcOf((*event
.GetDC()));
311 int mode
= ::SetMapMode(hdc
, MM_TEXT
);
314 ::FillRect(hdc
, &rect
, hBrush
);
315 ::DeleteObject(hBrush
);
318 ::SetMapMode(hdc
, mode
);
322 WXHBRUSH
wxControl::OnCtlColor(WXHDC pDC
, WXHWND
WXUNUSED(pWnd
), WXUINT
WXUNUSED(nCtlColor
),
328 WXUINT
WXUNUSED(message
),
329 WXWPARAM
WXUNUSED(wParam
),
330 WXLPARAM
WXUNUSED(lParam
)
337 HBRUSH hbrush
= Ctl3dCtlColorEx(message
, wParam
, lParam
);
338 return (WXHBRUSH
) hbrush
;
340 #endif // wxUSE_CTL3D
343 wxColour colBack
= GetBackgroundColour();
345 ::SetBkColor(hdc
, wxColourToRGB(colBack
));
346 ::SetTextColor(hdc
, wxColourToRGB(GetForegroundColour()));
348 wxBrush
*brush
= wxTheBrushList
->FindOrCreateBrush(colBack
, wxSOLID
);
350 return (WXHBRUSH
)brush
->GetResourceHandle();
353 // ---------------------------------------------------------------------------
355 // ---------------------------------------------------------------------------
357 // this is used in radiobox.cpp and slider95.cpp and should be removed as soon
358 // as it is not needed there any more!
360 // Call this repeatedly for several wnds to find the overall size
362 // Call it initially with -1 for all values in rect.
363 // Keep calling for other widgets, and rect will be modified
364 // to calculate largest bounding rectangle.
365 void wxFindMaxSize(WXHWND wnd
, RECT
*rect
)
367 int left
= rect
->left
;
368 int right
= rect
->right
;
370 int bottom
= rect
->bottom
;
372 GetWindowRect((HWND
) wnd
, rect
);
377 if (left
< rect
->left
)
380 if (right
> rect
->right
)
386 if (bottom
> rect
->bottom
)
387 rect
->bottom
= bottom
;
390 #endif // wxUSE_CONTROLS