]>
Commit | Line | Data |
---|---|---|
2bda0e17 | 1 | ///////////////////////////////////////////////////////////////////////////// |
1e6feb95 | 2 | // Name: msw/control.cpp |
2bda0e17 KB |
3 | // Purpose: wxControl class |
4 | // Author: Julian Smart | |
5 | // Modified by: | |
6 | // Created: 01/02/97 | |
7 | // RCS-ID: $Id$ | |
6c9a19aa | 8 | // Copyright: (c) Julian Smart |
a23fd0e1 | 9 | // Licence: wxWindows licence |
2bda0e17 KB |
10 | ///////////////////////////////////////////////////////////////////////////// |
11 | ||
34040e31 VZ |
12 | // ============================================================================ |
13 | // declarations | |
14 | // ============================================================================ | |
15 | ||
16 | // ---------------------------------------------------------------------------- | |
17 | // headers | |
18 | // ---------------------------------------------------------------------------- | |
19 | ||
14f355c2 | 20 | #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA) |
1e6feb95 | 21 | #pragma implementation "control.h" |
2bda0e17 KB |
22 | #endif |
23 | ||
24 | // For compilers that support precompilation, includes "wx.h". | |
25 | #include "wx/wxprec.h" | |
26 | ||
27 | #ifdef __BORLANDC__ | |
1e6feb95 | 28 | #pragma hdrstop |
2bda0e17 KB |
29 | #endif |
30 | ||
1e6feb95 VZ |
31 | #if wxUSE_CONTROLS |
32 | ||
2bda0e17 | 33 | #ifndef WX_PRECOMP |
1e6feb95 VZ |
34 | #include "wx/event.h" |
35 | #include "wx/app.h" | |
36 | #include "wx/dcclient.h" | |
37 | #include "wx/log.h" | |
34040e31 | 38 | #include "wx/settings.h" |
2bda0e17 KB |
39 | #endif |
40 | ||
2432b92d JS |
41 | #include "wx/control.h" |
42 | ||
2bda0e17 KB |
43 | #include "wx/msw/private.h" |
44 | ||
b39dbf34 | 45 | #if defined(__WIN95__) && !(defined(__GNUWIN32_OLD__) && !defined(__CYGWIN10__)) |
c42404a5 | 46 | #include <commctrl.h> |
2bda0e17 KB |
47 | #endif |
48 | ||
34040e31 VZ |
49 | // ---------------------------------------------------------------------------- |
50 | // wxWin macros | |
51 | // ---------------------------------------------------------------------------- | |
52 | ||
2bda0e17 KB |
53 | IMPLEMENT_ABSTRACT_CLASS(wxControl, wxWindow) |
54 | ||
55 | BEGIN_EVENT_TABLE(wxControl, wxWindow) | |
a23fd0e1 | 56 | EVT_ERASE_BACKGROUND(wxControl::OnEraseBackground) |
2bda0e17 | 57 | END_EVENT_TABLE() |
2bda0e17 | 58 | |
34040e31 VZ |
59 | // ============================================================================ |
60 | // wxControl implementation | |
61 | // ============================================================================ | |
62 | ||
63 | // ---------------------------------------------------------------------------- | |
64 | // wxControl ctor/dtor | |
65 | // ---------------------------------------------------------------------------- | |
2bda0e17 | 66 | |
42e69d6b | 67 | wxControl::~wxControl() |
2bda0e17 | 68 | { |
42e69d6b | 69 | m_isBeingDeleted = TRUE; |
2bda0e17 KB |
70 | } |
71 | ||
34040e31 VZ |
72 | // ---------------------------------------------------------------------------- |
73 | // control window creation | |
74 | // ---------------------------------------------------------------------------- | |
8d772832 | 75 | |
5b2f31eb VZ |
76 | bool wxControl::Create(wxWindow *parent, |
77 | wxWindowID id, | |
8d772832 | 78 | const wxPoint& pos, |
5b2f31eb VZ |
79 | const wxSize& size, |
80 | long style, | |
ac8d0c11 | 81 | const wxValidator& wxVALIDATOR_PARAM(validator), |
8d772832 RD |
82 | const wxString& name) |
83 | { | |
5b2f31eb VZ |
84 | if ( !wxWindow::Create(parent, id, pos, size, style, name) ) |
85 | return FALSE; | |
86 | ||
8d772832 | 87 | #if wxUSE_VALIDATORS |
5b2f31eb | 88 | SetValidator(validator); |
8d772832 | 89 | #endif |
5b2f31eb VZ |
90 | |
91 | return TRUE; | |
92 | } | |
93 | ||
94 | bool wxControl::MSWCreateControl(const wxChar *classname, | |
95 | const wxString& label, | |
96 | const wxPoint& pos, | |
6dd16e4f | 97 | const wxSize& size) |
5b2f31eb VZ |
98 | { |
99 | WXDWORD exstyle; | |
6dd16e4f | 100 | WXDWORD msStyle = MSWGetStyle(GetWindowStyle(), &exstyle); |
5b2f31eb | 101 | |
2eb4c3aa | 102 | return MSWCreateControl(classname, msStyle, pos, size, label, exstyle); |
8d772832 RD |
103 | } |
104 | ||
222594ea VZ |
105 | bool wxControl::MSWCreateControl(const wxChar *classname, |
106 | WXDWORD style, | |
107 | const wxPoint& pos, | |
108 | const wxSize& size, | |
109 | const wxString& label, | |
2eb4c3aa | 110 | WXDWORD exstyle) |
8d99be5f | 111 | { |
222594ea VZ |
112 | // if no extended style given, determine it ourselves |
113 | if ( exstyle == (WXDWORD)-1 ) | |
114 | { | |
fe3d9123 | 115 | exstyle = 0; |
65bc172c | 116 | (void) MSWGetStyle(GetWindowStyle(), &exstyle); |
222594ea VZ |
117 | } |
118 | ||
bdf5c30d VZ |
119 | // all controls should have this style |
120 | style |= WS_CHILD; | |
121 | ||
122 | // create the control visible if it's currently shown for wxWindows | |
123 | if ( m_isShown ) | |
124 | { | |
125 | style |= WS_VISIBLE; | |
126 | } | |
3f2711d5 | 127 | |
4e9d23cd | 128 | // choose the position for the control |
a63cbfa3 VZ |
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; | |
133 | ||
4e9d23cd VZ |
134 | // ... and adjust it to account for ap ossible parent frames toolbar |
135 | AdjustForParentClientOrigin(x, y); | |
136 | ||
8d99be5f VZ |
137 | m_hWnd = (WXHWND)::CreateWindowEx |
138 | ( | |
222594ea | 139 | exstyle, // extended style |
8d99be5f | 140 | classname, // the kind of control to create |
222594ea | 141 | label, // the window name |
8d99be5f | 142 | style, // the window style |
a63cbfa3 | 143 | x, y, w, h, // the window position and size |
8d99be5f VZ |
144 | GetHwndOf(GetParent()), // parent |
145 | (HMENU)GetId(), // child id | |
146 | wxGetInstance(), // app instance | |
147 | NULL // creation parameters | |
148 | ); | |
149 | ||
150 | if ( !m_hWnd ) | |
151 | { | |
f6bcfd97 BP |
152 | wxLogDebug(wxT("Failed to create a control of class '%s'"), classname); |
153 | wxFAIL_MSG(_T("something is very wrong")); | |
8d99be5f VZ |
154 | |
155 | return FALSE; | |
156 | } | |
157 | ||
3f2711d5 VZ |
158 | #if wxUSE_CTL3D |
159 | if ( want3D ) | |
160 | { | |
161 | Ctl3dSubclassCtl(GetHwnd()); | |
162 | m_useCtl3D = TRUE; | |
163 | } | |
164 | #endif // wxUSE_CTL3D | |
165 | ||
b225f659 | 166 | // install wxWindows window proc for this window |
8d99be5f VZ |
167 | SubclassWin(m_hWnd); |
168 | ||
34040e31 | 169 | // set up fonts and colours |
8d99be5f | 170 | InheritAttributes(); |
34040e31 | 171 | SetFont(GetDefaultAttributes().font); |
8d99be5f | 172 | |
a63cbfa3 | 173 | // set the size now if no initial size specified |
d45720c5 | 174 | if ( w <= 0 || h <= 0 ) |
a63cbfa3 VZ |
175 | { |
176 | SetBestSize(size); | |
177 | } | |
178 | ||
8d99be5f VZ |
179 | return TRUE; |
180 | } | |
181 | ||
34040e31 VZ |
182 | // ---------------------------------------------------------------------------- |
183 | // various accessors | |
184 | // ---------------------------------------------------------------------------- | |
185 | ||
65bc172c VZ |
186 | wxBorder wxControl::GetDefaultBorder() const |
187 | { | |
188 | // we want to automatically give controls a sunken style (confusingly, | |
189 | // it may not really mean sunken at all as we map it to WS_EX_CLIENTEDGE | |
190 | // which is not sunken at all under Windows XP -- rather, just the default) | |
191 | return wxBORDER_SUNKEN; | |
192 | } | |
193 | ||
34040e31 VZ |
194 | WXDWORD wxControl::MSWGetStyle(long style, WXDWORD *exstyle) const |
195 | { | |
196 | long msStyle = wxWindow::MSWGetStyle(style, exstyle); | |
197 | ||
198 | if ( AcceptsFocus() ) | |
199 | { | |
200 | msStyle |= WS_TABSTOP; | |
201 | } | |
202 | ||
203 | return msStyle; | |
204 | } | |
205 | ||
f68586e5 | 206 | wxSize wxControl::DoGetBestSize() const |
4438caf4 VZ |
207 | { |
208 | return wxSize(DEFAULT_ITEM_WIDTH, DEFAULT_ITEM_HEIGHT); | |
209 | } | |
210 | ||
34040e31 VZ |
211 | /* static */ wxVisualAttributes |
212 | wxControl::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant)) | |
213 | { | |
214 | wxVisualAttributes attrs; | |
215 | ||
216 | // old school (i.e. not "common") controls use the standard dialog font | |
217 | // by default | |
218 | attrs.font = wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT); | |
219 | ||
220 | // most, or at least many, of the controls use the same colours as the | |
221 | // buttons -- others will have to override this (and possibly simply call | |
222 | // GetCompositeControlsDefaultAttributes() from their versions) | |
223 | attrs.colFg = wxSystemSettings::GetColour(wxSYS_COLOUR_BTNTEXT); | |
224 | attrs.colBg = wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE); | |
225 | ||
226 | return attrs; | |
227 | } | |
228 | ||
229 | // another version for the "composite", i.e. non simple controls | |
230 | /* static */ wxVisualAttributes | |
fbfe58cb | 231 | wxControl::GetCompositeControlsDefaultAttributes(wxWindowVariant WXUNUSED(variant)) |
34040e31 VZ |
232 | { |
233 | wxVisualAttributes attrs; | |
234 | attrs.font = wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT); | |
235 | attrs.colFg = wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT); | |
236 | attrs.colBg = wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW); | |
237 | ||
238 | return attrs; | |
239 | } | |
240 | ||
241 | // ---------------------------------------------------------------------------- | |
242 | // message handling | |
243 | // ---------------------------------------------------------------------------- | |
244 | ||
42e69d6b | 245 | bool wxControl::ProcessCommand(wxCommandEvent& event) |
2bda0e17 | 246 | { |
42e69d6b | 247 | return GetEventHandler()->ProcessEvent(event); |
2bda0e17 KB |
248 | } |
249 | ||
a23fd0e1 VZ |
250 | #ifdef __WIN95__ |
251 | bool wxControl::MSWOnNotify(int idCtrl, | |
252 | WXLPARAM lParam, | |
253 | WXLPARAM* result) | |
2bda0e17 | 254 | { |
a23fd0e1 | 255 | wxEventType eventType = wxEVT_NULL; |
b6e5eaa5 VZ |
256 | |
257 | NMHDR *hdr = (NMHDR*) lParam; | |
258 | switch ( hdr->code ) | |
a23fd0e1 VZ |
259 | { |
260 | case NM_CLICK: | |
261 | eventType = wxEVT_COMMAND_LEFT_CLICK; | |
262 | break; | |
2bda0e17 | 263 | |
a23fd0e1 VZ |
264 | case NM_DBLCLK: |
265 | eventType = wxEVT_COMMAND_LEFT_DCLICK; | |
266 | break; | |
2bda0e17 | 267 | |
a23fd0e1 VZ |
268 | case NM_RCLICK: |
269 | eventType = wxEVT_COMMAND_RIGHT_CLICK; | |
270 | break; | |
2bda0e17 | 271 | |
a23fd0e1 VZ |
272 | case NM_RDBLCLK: |
273 | eventType = wxEVT_COMMAND_RIGHT_DCLICK; | |
274 | break; | |
2bda0e17 | 275 | |
a23fd0e1 VZ |
276 | case NM_SETFOCUS: |
277 | eventType = wxEVT_COMMAND_SET_FOCUS; | |
278 | break; | |
debe6624 | 279 | |
a23fd0e1 VZ |
280 | case NM_KILLFOCUS: |
281 | eventType = wxEVT_COMMAND_KILL_FOCUS; | |
282 | break; | |
2bda0e17 | 283 | |
a23fd0e1 VZ |
284 | case NM_RETURN: |
285 | eventType = wxEVT_COMMAND_ENTER; | |
286 | break; | |
287 | ||
288 | default: | |
289 | return wxWindow::MSWOnNotify(idCtrl, lParam, result); | |
290 | } | |
fd3f686c | 291 | |
b6e5eaa5 | 292 | wxCommandEvent event(wxEVT_NULL, m_windowId); |
2bda0e17 | 293 | event.SetEventType(eventType); |
a23fd0e1 | 294 | event.SetEventObject(this); |
2bda0e17 | 295 | |
a23fd0e1 | 296 | return GetEventHandler()->ProcessEvent(event); |
2bda0e17 | 297 | } |
a23fd0e1 | 298 | #endif // Win95 |
2bda0e17 | 299 | |
2bda0e17 KB |
300 | void wxControl::OnEraseBackground(wxEraseEvent& event) |
301 | { | |
5bd3a2da VZ |
302 | // notice that this 'dumb' implementation may cause flicker for some of the |
303 | // controls in which case they should intercept wxEraseEvent and process it | |
304 | // themselves somehow | |
42e69d6b VZ |
305 | |
306 | RECT rect; | |
3f2711d5 | 307 | ::GetClientRect(GetHwnd(), &rect); |
42e69d6b | 308 | |
3f2711d5 | 309 | HBRUSH hBrush = ::CreateSolidBrush(wxColourToRGB(GetBackgroundColour())); |
42e69d6b | 310 | |
3f2711d5 | 311 | HDC hdc = GetHdcOf((*event.GetDC())); |
4676948b JS |
312 | |
313 | #ifndef __WXWINCE__ | |
3f2711d5 | 314 | int mode = ::SetMapMode(hdc, MM_TEXT); |
4676948b | 315 | #endif |
3f2711d5 VZ |
316 | |
317 | ::FillRect(hdc, &rect, hBrush); | |
42e69d6b | 318 | ::DeleteObject(hBrush); |
4676948b JS |
319 | |
320 | #ifndef __WXWINCE__ | |
3f2711d5 | 321 | ::SetMapMode(hdc, mode); |
4676948b | 322 | #endif |
2bda0e17 KB |
323 | } |
324 | ||
33ac7e6f | 325 | WXHBRUSH wxControl::OnCtlColor(WXHDC pDC, WXHWND WXUNUSED(pWnd), WXUINT WXUNUSED(nCtlColor), |
788722ac JS |
326 | #if wxUSE_CTL3D |
327 | WXUINT message, | |
328 | WXWPARAM wParam, | |
329 | WXLPARAM lParam | |
330 | #else | |
33ac7e6f KB |
331 | WXUINT WXUNUSED(message), |
332 | WXWPARAM WXUNUSED(wParam), | |
788722ac JS |
333 | WXLPARAM WXUNUSED(lParam) |
334 | #endif | |
335 | ) | |
f048e32f VZ |
336 | { |
337 | #if wxUSE_CTL3D | |
338 | if ( m_useCtl3D ) | |
339 | { | |
340 | HBRUSH hbrush = Ctl3dCtlColorEx(message, wParam, lParam); | |
341 | return (WXHBRUSH) hbrush; | |
342 | } | |
343 | #endif // wxUSE_CTL3D | |
344 | ||
345 | HDC hdc = (HDC)pDC; | |
f6bcfd97 BP |
346 | wxColour colBack = GetBackgroundColour(); |
347 | ||
f048e32f VZ |
348 | ::SetBkColor(hdc, wxColourToRGB(colBack)); |
349 | ::SetTextColor(hdc, wxColourToRGB(GetForegroundColour())); | |
350 | ||
351 | wxBrush *brush = wxTheBrushList->FindOrCreateBrush(colBack, wxSOLID); | |
352 | ||
353 | return (WXHBRUSH)brush->GetResourceHandle(); | |
354 | } | |
355 | ||
42e69d6b VZ |
356 | // --------------------------------------------------------------------------- |
357 | // global functions | |
358 | // --------------------------------------------------------------------------- | |
2bda0e17 | 359 | |
34040e31 VZ |
360 | // this is used in radiobox.cpp and slider95.cpp and should be removed as soon |
361 | // as it is not needed there any more! | |
362 | // | |
42e69d6b VZ |
363 | // Call this repeatedly for several wnds to find the overall size |
364 | // of the widget. | |
365 | // Call it initially with -1 for all values in rect. | |
366 | // Keep calling for other widgets, and rect will be modified | |
367 | // to calculate largest bounding rectangle. | |
368 | void wxFindMaxSize(WXHWND wnd, RECT *rect) | |
2bda0e17 | 369 | { |
42e69d6b VZ |
370 | int left = rect->left; |
371 | int right = rect->right; | |
372 | int top = rect->top; | |
373 | int bottom = rect->bottom; | |
2bda0e17 | 374 | |
42e69d6b | 375 | GetWindowRect((HWND) wnd, rect); |
2bda0e17 | 376 | |
42e69d6b VZ |
377 | if (left < 0) |
378 | return; | |
2bda0e17 | 379 | |
42e69d6b VZ |
380 | if (left < rect->left) |
381 | rect->left = left; | |
2bda0e17 | 382 | |
42e69d6b VZ |
383 | if (right > rect->right) |
384 | rect->right = right; | |
2bda0e17 | 385 | |
42e69d6b VZ |
386 | if (top < rect->top) |
387 | rect->top = top; | |
2bda0e17 | 388 | |
42e69d6b VZ |
389 | if (bottom > rect->bottom) |
390 | rect->bottom = bottom; | |
2bda0e17 KB |
391 | } |
392 | ||
1e6feb95 | 393 | #endif // wxUSE_CONTROLS |