]>
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 |
65571936 | 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 | ||
01c500af VZ |
43 | #if wxUSE_NOTEBOOK |
44 | #include "wx/notebook.h" | |
45 | #endif // wxUSE_NOTEBOOK | |
46 | ||
2bda0e17 | 47 | #include "wx/msw/private.h" |
01c500af | 48 | #include "wx/msw/uxtheme.h" |
2bda0e17 | 49 | |
b39dbf34 | 50 | #if defined(__WIN95__) && !(defined(__GNUWIN32_OLD__) && !defined(__CYGWIN10__)) |
c42404a5 | 51 | #include <commctrl.h> |
2bda0e17 KB |
52 | #endif |
53 | ||
34040e31 VZ |
54 | // ---------------------------------------------------------------------------- |
55 | // wxWin macros | |
56 | // ---------------------------------------------------------------------------- | |
57 | ||
2bda0e17 KB |
58 | IMPLEMENT_ABSTRACT_CLASS(wxControl, wxWindow) |
59 | ||
34040e31 VZ |
60 | // ============================================================================ |
61 | // wxControl implementation | |
62 | // ============================================================================ | |
63 | ||
64 | // ---------------------------------------------------------------------------- | |
65 | // wxControl ctor/dtor | |
66 | // ---------------------------------------------------------------------------- | |
2bda0e17 | 67 | |
42e69d6b | 68 | wxControl::~wxControl() |
2bda0e17 | 69 | { |
d95de154 | 70 | m_isBeingDeleted = true; |
2bda0e17 KB |
71 | } |
72 | ||
34040e31 VZ |
73 | // ---------------------------------------------------------------------------- |
74 | // control window creation | |
75 | // ---------------------------------------------------------------------------- | |
8d772832 | 76 | |
5b2f31eb VZ |
77 | bool wxControl::Create(wxWindow *parent, |
78 | wxWindowID id, | |
8d772832 | 79 | const wxPoint& pos, |
5b2f31eb VZ |
80 | const wxSize& size, |
81 | long style, | |
ac8d0c11 | 82 | const wxValidator& wxVALIDATOR_PARAM(validator), |
8d772832 RD |
83 | const wxString& name) |
84 | { | |
5b2f31eb | 85 | if ( !wxWindow::Create(parent, id, pos, size, style, name) ) |
d95de154 | 86 | return false; |
5b2f31eb | 87 | |
8d772832 | 88 | #if wxUSE_VALIDATORS |
5b2f31eb | 89 | SetValidator(validator); |
8d772832 | 90 | #endif |
5b2f31eb | 91 | |
d95de154 | 92 | return true; |
5b2f31eb VZ |
93 | } |
94 | ||
95 | bool wxControl::MSWCreateControl(const wxChar *classname, | |
96 | const wxString& label, | |
97 | const wxPoint& pos, | |
6dd16e4f | 98 | const wxSize& size) |
5b2f31eb VZ |
99 | { |
100 | WXDWORD exstyle; | |
6dd16e4f | 101 | WXDWORD msStyle = MSWGetStyle(GetWindowStyle(), &exstyle); |
5b2f31eb | 102 | |
2eb4c3aa | 103 | return MSWCreateControl(classname, msStyle, pos, size, label, exstyle); |
8d772832 RD |
104 | } |
105 | ||
222594ea VZ |
106 | bool wxControl::MSWCreateControl(const wxChar *classname, |
107 | WXDWORD style, | |
108 | const wxPoint& pos, | |
109 | const wxSize& size, | |
110 | const wxString& label, | |
2eb4c3aa | 111 | WXDWORD exstyle) |
8d99be5f | 112 | { |
222594ea VZ |
113 | // if no extended style given, determine it ourselves |
114 | if ( exstyle == (WXDWORD)-1 ) | |
115 | { | |
fe3d9123 | 116 | exstyle = 0; |
65bc172c | 117 | (void) MSWGetStyle(GetWindowStyle(), &exstyle); |
222594ea VZ |
118 | } |
119 | ||
bdf5c30d VZ |
120 | // all controls should have this style |
121 | style |= WS_CHILD; | |
122 | ||
77ffb593 | 123 | // create the control visible if it's currently shown for wxWidgets |
bdf5c30d VZ |
124 | if ( m_isShown ) |
125 | { | |
126 | style |= WS_VISIBLE; | |
127 | } | |
3f2711d5 | 128 | |
4e9d23cd | 129 | // choose the position for the control |
d95de154 WS |
130 | int x = pos.x == wxDefaultCoord ? 0 : pos.x, |
131 | y = pos.y == wxDefaultCoord ? 0 : pos.y, | |
132 | w = size.x == wxDefaultCoord ? 0 : size.x, | |
133 | h = size.y == wxDefaultCoord ? 0 : size.y; | |
a63cbfa3 | 134 | |
7c0a023d | 135 | // ... and adjust it to account for a possible parent frames toolbar |
4e9d23cd VZ |
136 | AdjustForParentClientOrigin(x, y); |
137 | ||
8d99be5f VZ |
138 | m_hWnd = (WXHWND)::CreateWindowEx |
139 | ( | |
222594ea | 140 | exstyle, // extended style |
8d99be5f | 141 | classname, // the kind of control to create |
222594ea | 142 | label, // the window name |
8d99be5f | 143 | style, // the window style |
a63cbfa3 | 144 | x, y, w, h, // the window position and size |
8d99be5f VZ |
145 | GetHwndOf(GetParent()), // parent |
146 | (HMENU)GetId(), // child id | |
147 | wxGetInstance(), // app instance | |
148 | NULL // creation parameters | |
149 | ); | |
150 | ||
151 | if ( !m_hWnd ) | |
152 | { | |
f6bcfd97 | 153 | wxLogDebug(wxT("Failed to create a control of class '%s'"), classname); |
90af2514 | 154 | wxFAIL_MSG(_T("something is very wrong, CreateWindowEx failed")); |
8d99be5f | 155 | |
d95de154 | 156 | return false; |
8d99be5f VZ |
157 | } |
158 | ||
3f2711d5 VZ |
159 | #if wxUSE_CTL3D |
160 | if ( want3D ) | |
161 | { | |
162 | Ctl3dSubclassCtl(GetHwnd()); | |
d95de154 | 163 | m_useCtl3D = true; |
3f2711d5 VZ |
164 | } |
165 | #endif // wxUSE_CTL3D | |
166 | ||
77ffb593 | 167 | // install wxWidgets window proc for this window |
8d99be5f VZ |
168 | SubclassWin(m_hWnd); |
169 | ||
34040e31 | 170 | // set up fonts and colours |
8d99be5f | 171 | InheritAttributes(); |
e0176dd9 VZ |
172 | if (!m_hasFont) |
173 | SetFont(GetDefaultAttributes().font); | |
8d99be5f | 174 | |
a63cbfa3 | 175 | // set the size now if no initial size specified |
085dd1e9 | 176 | SetInitialBestSize(size); |
a63cbfa3 | 177 | |
d95de154 | 178 | return true; |
8d99be5f VZ |
179 | } |
180 | ||
34040e31 VZ |
181 | // ---------------------------------------------------------------------------- |
182 | // various accessors | |
183 | // ---------------------------------------------------------------------------- | |
184 | ||
65bc172c VZ |
185 | wxBorder wxControl::GetDefaultBorder() const |
186 | { | |
187 | // we want to automatically give controls a sunken style (confusingly, | |
188 | // it may not really mean sunken at all as we map it to WS_EX_CLIENTEDGE | |
189 | // which is not sunken at all under Windows XP -- rather, just the default) | |
190 | return wxBORDER_SUNKEN; | |
191 | } | |
192 | ||
34040e31 VZ |
193 | WXDWORD wxControl::MSWGetStyle(long style, WXDWORD *exstyle) const |
194 | { | |
195 | long msStyle = wxWindow::MSWGetStyle(style, exstyle); | |
196 | ||
197 | if ( AcceptsFocus() ) | |
198 | { | |
199 | msStyle |= WS_TABSTOP; | |
200 | } | |
201 | ||
202 | return msStyle; | |
203 | } | |
204 | ||
f68586e5 | 205 | wxSize wxControl::DoGetBestSize() const |
4438caf4 VZ |
206 | { |
207 | return wxSize(DEFAULT_ITEM_WIDTH, DEFAULT_ITEM_HEIGHT); | |
208 | } | |
209 | ||
4bf45c9e WS |
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::GetBestSpinerSize(const bool is_vertical) const | |
215 | { | |
b081046a | 216 | // take size according to layout |
1550d5f8 | 217 | wxSize bestSize( |
285f605a | 218 | #if defined(__SMARTPHONE__) && defined(__WXWINCE__) |
1550d5f8 | 219 | 0,GetCharHeight() |
3180bc0e | 220 | #else |
1550d5f8 WS |
221 | GetSystemMetrics(is_vertical ? SM_CXVSCROLL : SM_CXHSCROLL), |
222 | GetSystemMetrics(is_vertical ? SM_CYVSCROLL : SM_CYHSCROLL) | |
3180bc0e | 223 | #endif |
1550d5f8 | 224 | ); |
b081046a WS |
225 | |
226 | // correct size as for undocumented MSW variants cases (WinCE and perhaps others) | |
227 | if (bestSize.x==0) | |
228 | bestSize.x = bestSize.y; | |
229 | if (bestSize.y==0) | |
230 | bestSize.y = bestSize.x; | |
231 | ||
232 | // double size according to layout | |
4bf45c9e | 233 | if (is_vertical) |
b081046a | 234 | bestSize.y *= 2; |
4bf45c9e | 235 | else |
b081046a WS |
236 | bestSize.x *= 2; |
237 | ||
238 | return bestSize; | |
4bf45c9e WS |
239 | } |
240 | ||
34040e31 VZ |
241 | /* static */ wxVisualAttributes |
242 | wxControl::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant)) | |
243 | { | |
244 | wxVisualAttributes attrs; | |
245 | ||
246 | // old school (i.e. not "common") controls use the standard dialog font | |
247 | // by default | |
248 | attrs.font = wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT); | |
249 | ||
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); | |
255 | ||
256 | return attrs; | |
257 | } | |
258 | ||
259 | // another version for the "composite", i.e. non simple controls | |
260 | /* static */ wxVisualAttributes | |
fbfe58cb | 261 | wxControl::GetCompositeControlsDefaultAttributes(wxWindowVariant WXUNUSED(variant)) |
34040e31 VZ |
262 | { |
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); | |
267 | ||
268 | return attrs; | |
269 | } | |
270 | ||
271 | // ---------------------------------------------------------------------------- | |
272 | // message handling | |
273 | // ---------------------------------------------------------------------------- | |
274 | ||
42e69d6b | 275 | bool wxControl::ProcessCommand(wxCommandEvent& event) |
2bda0e17 | 276 | { |
42e69d6b | 277 | return GetEventHandler()->ProcessEvent(event); |
2bda0e17 KB |
278 | } |
279 | ||
a23fd0e1 VZ |
280 | #ifdef __WIN95__ |
281 | bool wxControl::MSWOnNotify(int idCtrl, | |
282 | WXLPARAM lParam, | |
283 | WXLPARAM* result) | |
2bda0e17 | 284 | { |
5cb598ae | 285 | wxEventType eventType wxDUMMY_INITIALIZE(wxEVT_NULL); |
b6e5eaa5 VZ |
286 | |
287 | NMHDR *hdr = (NMHDR*) lParam; | |
288 | switch ( hdr->code ) | |
a23fd0e1 VZ |
289 | { |
290 | case NM_CLICK: | |
291 | eventType = wxEVT_COMMAND_LEFT_CLICK; | |
292 | break; | |
2bda0e17 | 293 | |
a23fd0e1 VZ |
294 | case NM_DBLCLK: |
295 | eventType = wxEVT_COMMAND_LEFT_DCLICK; | |
296 | break; | |
2bda0e17 | 297 | |
a23fd0e1 VZ |
298 | case NM_RCLICK: |
299 | eventType = wxEVT_COMMAND_RIGHT_CLICK; | |
300 | break; | |
2bda0e17 | 301 | |
a23fd0e1 VZ |
302 | case NM_RDBLCLK: |
303 | eventType = wxEVT_COMMAND_RIGHT_DCLICK; | |
304 | break; | |
2bda0e17 | 305 | |
a23fd0e1 VZ |
306 | case NM_SETFOCUS: |
307 | eventType = wxEVT_COMMAND_SET_FOCUS; | |
308 | break; | |
debe6624 | 309 | |
a23fd0e1 VZ |
310 | case NM_KILLFOCUS: |
311 | eventType = wxEVT_COMMAND_KILL_FOCUS; | |
312 | break; | |
2bda0e17 | 313 | |
a23fd0e1 VZ |
314 | case NM_RETURN: |
315 | eventType = wxEVT_COMMAND_ENTER; | |
316 | break; | |
317 | ||
318 | default: | |
319 | return wxWindow::MSWOnNotify(idCtrl, lParam, result); | |
320 | } | |
fd3f686c | 321 | |
b6e5eaa5 | 322 | wxCommandEvent event(wxEVT_NULL, m_windowId); |
2bda0e17 | 323 | event.SetEventType(eventType); |
a23fd0e1 | 324 | event.SetEventObject(this); |
2bda0e17 | 325 | |
a23fd0e1 | 326 | return GetEventHandler()->ProcessEvent(event); |
2bda0e17 | 327 | } |
a23fd0e1 | 328 | #endif // Win95 |
2bda0e17 | 329 | |
5c836c46 | 330 | WXHBRUSH wxControl::MSWControlColorSolid(WXHDC pDC, wxColour colBg) |
f048e32f | 331 | { |
01c500af VZ |
332 | HDC hdc = (HDC)pDC; |
333 | if ( m_hasFgCol ) | |
334 | ::SetTextColor(hdc, wxColourToRGB(GetForegroundColour())); | |
335 | ||
5c836c46 | 336 | if ( colBg.Ok() ) |
f048e32f | 337 | { |
5c836c46 | 338 | ::SetBkColor(hdc, wxColourToRGB(colBg)); |
f6bcfd97 | 339 | |
5c836c46 | 340 | wxBrush *brush = wxTheBrushList->FindOrCreateBrush(colBg, wxSOLID); |
01c500af VZ |
341 | |
342 | return (WXHBRUSH)brush->GetResourceHandle(); | |
343 | } | |
f048e32f | 344 | |
5c836c46 VZ |
345 | return 0; |
346 | } | |
347 | ||
348 | WXHBRUSH wxControl::MSWControlColor(WXHDC pDC) | |
349 | { | |
350 | WXHBRUSH hbr = MSWControlColorSolid(pDC, m_hasBgCol ? m_backgroundColour | |
351 | : wxNullColour); | |
352 | if ( hbr ) | |
353 | return hbr; | |
354 | ||
355 | ::SetBkMode((HDC)pDC, TRANSPARENT); | |
01c500af VZ |
356 | |
357 | #if wxUSE_UXTHEME && wxUSE_NOTEBOOK | |
358 | if ( wxUxThemeEngine::GetIfActive() ) | |
359 | { | |
360 | for ( wxWindow *win = this; win; win = win->GetParent() ) | |
361 | { | |
362 | wxNotebook *nbook = wxDynamicCast(win, wxNotebook); | |
363 | if ( nbook ) | |
364 | { | |
5c836c46 VZ |
365 | // return value may be NULL but it is ok: if the first parent |
366 | // notebook doesn't use themes, then we don't have to process | |
367 | // this message at all, so let default processing take place | |
368 | return nbook->GetThemeBackgroundBrush(pDC, this); | |
01c500af VZ |
369 | } |
370 | } | |
371 | } | |
372 | #endif // wxUSE_UXTHEME | |
f048e32f | 373 | |
5c836c46 VZ |
374 | return ::GetStockObject(NULL_BRUSH); |
375 | } | |
376 | ||
377 | WXHBRUSH wxControl::MSWControlColorDisabled(WXHDC pDC) | |
378 | { | |
379 | return MSWControlColorSolid | |
380 | ( | |
381 | pDC, | |
382 | wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE) | |
383 | ); | |
f048e32f VZ |
384 | } |
385 | ||
42e69d6b VZ |
386 | // --------------------------------------------------------------------------- |
387 | // global functions | |
388 | // --------------------------------------------------------------------------- | |
2bda0e17 | 389 | |
34040e31 VZ |
390 | // this is used in radiobox.cpp and slider95.cpp and should be removed as soon |
391 | // as it is not needed there any more! | |
392 | // | |
42e69d6b VZ |
393 | // Call this repeatedly for several wnds to find the overall size |
394 | // of the widget. | |
d95de154 | 395 | // Call it initially with wxDefaultCoord for all values in rect. |
42e69d6b VZ |
396 | // Keep calling for other widgets, and rect will be modified |
397 | // to calculate largest bounding rectangle. | |
398 | void wxFindMaxSize(WXHWND wnd, RECT *rect) | |
2bda0e17 | 399 | { |
42e69d6b VZ |
400 | int left = rect->left; |
401 | int right = rect->right; | |
402 | int top = rect->top; | |
403 | int bottom = rect->bottom; | |
2bda0e17 | 404 | |
42e69d6b | 405 | GetWindowRect((HWND) wnd, rect); |
2bda0e17 | 406 | |
42e69d6b VZ |
407 | if (left < 0) |
408 | return; | |
2bda0e17 | 409 | |
42e69d6b VZ |
410 | if (left < rect->left) |
411 | rect->left = left; | |
2bda0e17 | 412 | |
42e69d6b VZ |
413 | if (right > rect->right) |
414 | rect->right = right; | |
2bda0e17 | 415 | |
42e69d6b VZ |
416 | if (top < rect->top) |
417 | rect->top = top; | |
2bda0e17 | 418 | |
42e69d6b VZ |
419 | if (bottom > rect->bottom) |
420 | rect->bottom = bottom; | |
2bda0e17 KB |
421 | } |
422 | ||
1e6feb95 | 423 | #endif // wxUSE_CONTROLS |