1 /////////////////////////////////////////////////////////////////////////////
2 // Name: msw/control.cpp
3 // Purpose: wxControl class
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart and Markus Holzem
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
13 #pragma implementation "control.h"
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
28 #include "wx/dcclient.h"
32 #include "wx/control.h"
34 #include "wx/msw/private.h"
36 #if defined(__WIN95__) && !((defined(__GNUWIN32_OLD__) || defined(__TWIN32__)) && !defined(__CYGWIN10__))
40 IMPLEMENT_ABSTRACT_CLASS(wxControl
, wxWindow
)
42 BEGIN_EVENT_TABLE(wxControl
, wxWindow
)
43 EVT_ERASE_BACKGROUND(wxControl::OnEraseBackground
)
47 wxControl::wxControl()
49 #if WXWIN_COMPATIBILITY
51 #endif // WXWIN_COMPATIBILITY
54 wxControl::~wxControl()
56 m_isBeingDeleted
= TRUE
;
60 bool wxControl::Create(wxWindow
*parent
,
65 const wxValidator
& validator
,
68 if ( !wxWindow::Create(parent
, id
, pos
, size
, style
, name
) )
72 SetValidator(validator
);
78 bool wxControl::MSWCreateControl(const wxChar
*classname
,
79 const wxString
& label
,
86 WXDWORD msStyle
= MSWGetStyle(style
, &exstyle
);
88 return MSWCreateControl(classname
, msStyle
, pos
, size
, label
, exstyle
, visible
);
91 bool wxControl::MSWCreateControl(const wxChar
*classname
,
95 const wxString
& label
,
99 // want3D tells us whether or not the style specified a 3D border.
100 // If so, under WIN16 we can use Ctl3D to give it an appropriate style.
101 // Sometimes want3D is used to indicate that the non-extended style should have
105 // if no extended style given, determine it ourselves
106 if ( exstyle
== (WXDWORD
)-1 )
108 exstyle
= Determine3DEffects(WS_EX_CLIENTEDGE
, &want3D
);
111 // all controls should have these styles (wxWindows creates all controls
112 // visible by default)
115 // sometimes, controls will defer showing the window until
116 // all configuration, sizing, and positioning is completed
126 int x
= pos
.x
== -1 ? 0 : pos
.x
,
127 y
= pos
.y
== -1 ? 0 : pos
.y
,
128 w
= size
.x
== -1 ? 0 : size
.x
,
129 h
= size
.y
== -1 ? 0 : size
.y
;
131 m_hWnd
= (WXHWND
)::CreateWindowEx
133 exstyle
, // extended style
134 classname
, // the kind of control to create
135 label
, // the window name
136 style
, // the window style
137 x
, y
, w
, h
, // the window position and size
138 GetHwndOf(GetParent()), // parent
139 (HMENU
)GetId(), // child id
140 wxGetInstance(), // app instance
141 NULL
// creation parameters
146 wxLogDebug(wxT("Failed to create a control of class '%s'"), classname
);
147 wxFAIL_MSG(_T("something is very wrong"));
155 Ctl3dSubclassCtl(GetHwnd());
158 #endif // wxUSE_CTL3D
160 // install wxWindows window proc for this window
163 // controls use the same font and colours as their parent dialog by default
166 // set the size now if no initial size specified
167 if ( w
== 0 || h
== 0 )
175 wxSize
wxControl::DoGetBestSize() const
177 return wxSize(DEFAULT_ITEM_WIDTH
, DEFAULT_ITEM_HEIGHT
);
180 bool wxControl::ProcessCommand(wxCommandEvent
& event
)
182 #if WXWIN_COMPATIBILITY
185 (void)(*m_callback
)(*this, event
);
190 #endif // WXWIN_COMPATIBILITY
192 return GetEventHandler()->ProcessEvent(event
);
196 bool wxControl::MSWOnNotify(int idCtrl
,
200 wxEventType eventType
= wxEVT_NULL
;
202 NMHDR
*hdr
= (NMHDR
*) lParam
;
206 eventType
= wxEVT_COMMAND_LEFT_CLICK
;
210 eventType
= wxEVT_COMMAND_LEFT_DCLICK
;
214 eventType
= wxEVT_COMMAND_RIGHT_CLICK
;
218 eventType
= wxEVT_COMMAND_RIGHT_DCLICK
;
222 eventType
= wxEVT_COMMAND_SET_FOCUS
;
226 eventType
= wxEVT_COMMAND_KILL_FOCUS
;
230 eventType
= wxEVT_COMMAND_ENTER
;
234 return wxWindow::MSWOnNotify(idCtrl
, lParam
, result
);
237 wxCommandEvent
event(wxEVT_NULL
, m_windowId
);
238 event
.SetEventType(eventType
);
239 event
.SetEventObject(this);
241 return GetEventHandler()->ProcessEvent(event
);
245 void wxControl::OnEraseBackground(wxEraseEvent
& event
)
247 // notice that this 'dumb' implementation may cause flicker for some of the
248 // controls in which case they should intercept wxEraseEvent and process it
249 // themselves somehow
252 ::GetClientRect(GetHwnd(), &rect
);
254 HBRUSH hBrush
= ::CreateSolidBrush(wxColourToRGB(GetBackgroundColour()));
256 HDC hdc
= GetHdcOf((*event
.GetDC()));
257 int mode
= ::SetMapMode(hdc
, MM_TEXT
);
259 ::FillRect(hdc
, &rect
, hBrush
);
260 ::DeleteObject(hBrush
);
261 ::SetMapMode(hdc
, mode
);
264 WXHBRUSH
wxControl::OnCtlColor(WXHDC pDC
, WXHWND
WXUNUSED(pWnd
), WXUINT
WXUNUSED(nCtlColor
),
270 WXUINT
WXUNUSED(message
),
271 WXWPARAM
WXUNUSED(wParam
),
272 WXLPARAM
WXUNUSED(lParam
)
279 HBRUSH hbrush
= Ctl3dCtlColorEx(message
, wParam
, lParam
);
280 return (WXHBRUSH
) hbrush
;
282 #endif // wxUSE_CTL3D
285 if (GetParent()->GetTransparentBackground())
286 SetBkMode(hdc
, TRANSPARENT
);
288 SetBkMode(hdc
, OPAQUE
);
290 wxColour colBack
= GetBackgroundColour();
292 ::SetBkColor(hdc
, wxColourToRGB(colBack
));
293 ::SetTextColor(hdc
, wxColourToRGB(GetForegroundColour()));
295 wxBrush
*brush
= wxTheBrushList
->FindOrCreateBrush(colBack
, wxSOLID
);
297 return (WXHBRUSH
)brush
->GetResourceHandle();
300 WXDWORD
wxControl::MSWGetStyle(long style
, WXDWORD
*exstyle
) const
302 long msStyle
= wxWindow::MSWGetStyle(style
, exstyle
);
304 if ( AcceptsFocus() )
306 msStyle
|= WS_TABSTOP
;
312 // ---------------------------------------------------------------------------
314 // ---------------------------------------------------------------------------
316 // Call this repeatedly for several wnds to find the overall size
318 // Call it initially with -1 for all values in rect.
319 // Keep calling for other widgets, and rect will be modified
320 // to calculate largest bounding rectangle.
321 void wxFindMaxSize(WXHWND wnd
, RECT
*rect
)
323 int left
= rect
->left
;
324 int right
= rect
->right
;
326 int bottom
= rect
->bottom
;
328 GetWindowRect((HWND
) wnd
, rect
);
333 if (left
< rect
->left
)
336 if (right
> rect
->right
)
342 if (bottom
> rect
->bottom
)
343 rect
->bottom
= bottom
;
346 #endif // wxUSE_CONTROLS