]> git.saurik.com Git - wxWidgets.git/blob - src/msw/control.cpp
added GetIfActive() method returning non-NULL engine only if themes are not only...
[wxWidgets.git] / src / msw / control.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: msw/control.cpp
3 // Purpose: wxControl class
4 // Author: Julian Smart
5 // Modified by:
6 // Created: 01/02/97
7 // RCS-ID: $Id$
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 // ----------------------------------------------------------------------------
17 // headers
18 // ----------------------------------------------------------------------------
19
20 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
21 #pragma implementation "control.h"
22 #endif
23
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
26
27 #ifdef __BORLANDC__
28 #pragma hdrstop
29 #endif
30
31 #if wxUSE_CONTROLS
32
33 #ifndef WX_PRECOMP
34 #include "wx/event.h"
35 #include "wx/app.h"
36 #include "wx/dcclient.h"
37 #include "wx/log.h"
38 #include "wx/settings.h"
39 #endif
40
41 #include "wx/control.h"
42
43 #include "wx/msw/private.h"
44
45 #if defined(__WIN95__) && !(defined(__GNUWIN32_OLD__) && !defined(__CYGWIN10__))
46 #include <commctrl.h>
47 #endif
48
49 // ----------------------------------------------------------------------------
50 // wxWin macros
51 // ----------------------------------------------------------------------------
52
53 IMPLEMENT_ABSTRACT_CLASS(wxControl, wxWindow)
54
55 // ============================================================================
56 // wxControl implementation
57 // ============================================================================
58
59 // ----------------------------------------------------------------------------
60 // wxControl ctor/dtor
61 // ----------------------------------------------------------------------------
62
63 wxControl::~wxControl()
64 {
65 m_isBeingDeleted = true;
66 }
67
68 // ----------------------------------------------------------------------------
69 // control window creation
70 // ----------------------------------------------------------------------------
71
72 bool wxControl::Create(wxWindow *parent,
73 wxWindowID id,
74 const wxPoint& pos,
75 const wxSize& size,
76 long style,
77 const wxValidator& wxVALIDATOR_PARAM(validator),
78 const wxString& name)
79 {
80 if ( !wxWindow::Create(parent, id, pos, size, style, name) )
81 return false;
82
83 #if wxUSE_VALIDATORS
84 SetValidator(validator);
85 #endif
86
87 return true;
88 }
89
90 bool wxControl::MSWCreateControl(const wxChar *classname,
91 const wxString& label,
92 const wxPoint& pos,
93 const wxSize& size)
94 {
95 WXDWORD exstyle;
96 WXDWORD msStyle = MSWGetStyle(GetWindowStyle(), &exstyle);
97
98 return MSWCreateControl(classname, msStyle, pos, size, label, exstyle);
99 }
100
101 bool wxControl::MSWCreateControl(const wxChar *classname,
102 WXDWORD style,
103 const wxPoint& pos,
104 const wxSize& size,
105 const wxString& label,
106 WXDWORD exstyle)
107 {
108 // if no extended style given, determine it ourselves
109 if ( exstyle == (WXDWORD)-1 )
110 {
111 exstyle = 0;
112 (void) MSWGetStyle(GetWindowStyle(), &exstyle);
113 }
114
115 // all controls should have this style
116 style |= WS_CHILD;
117
118 // create the control visible if it's currently shown for wxWidgets
119 if ( m_isShown )
120 {
121 style |= WS_VISIBLE;
122 }
123
124 // choose the position for the control
125 int x = pos.x == wxDefaultCoord ? 0 : pos.x,
126 y = pos.y == wxDefaultCoord ? 0 : pos.y,
127 w = size.x == wxDefaultCoord ? 0 : size.x,
128 h = size.y == wxDefaultCoord ? 0 : size.y;
129
130 // ... and adjust it to account for a possible parent frames toolbar
131 AdjustForParentClientOrigin(x, y);
132
133 m_hWnd = (WXHWND)::CreateWindowEx
134 (
135 exstyle, // extended style
136 classname, // the kind of control to create
137 label, // the window name
138 style, // the window style
139 x, y, w, h, // the window position and size
140 GetHwndOf(GetParent()), // parent
141 (HMENU)GetId(), // child id
142 wxGetInstance(), // app instance
143 NULL // creation parameters
144 );
145
146 if ( !m_hWnd )
147 {
148 wxLogDebug(wxT("Failed to create a control of class '%s'"), classname);
149 wxFAIL_MSG(_T("something is very wrong, CreateWindowEx failed"));
150
151 return false;
152 }
153
154 #if wxUSE_CTL3D
155 if ( want3D )
156 {
157 Ctl3dSubclassCtl(GetHwnd());
158 m_useCtl3D = true;
159 }
160 #endif // wxUSE_CTL3D
161
162 // install wxWidgets window proc for this window
163 SubclassWin(m_hWnd);
164
165 // set up fonts and colours
166 InheritAttributes();
167 if (!m_hasFont)
168 SetFont(GetDefaultAttributes().font);
169
170 // set the size now if no initial size specified
171 SetInitialBestSize(size);
172
173 return true;
174 }
175
176 // ----------------------------------------------------------------------------
177 // various accessors
178 // ----------------------------------------------------------------------------
179
180 wxBorder wxControl::GetDefaultBorder() const
181 {
182 // we want to automatically give controls a sunken style (confusingly,
183 // it may not really mean sunken at all as we map it to WS_EX_CLIENTEDGE
184 // which is not sunken at all under Windows XP -- rather, just the default)
185 return wxBORDER_SUNKEN;
186 }
187
188 WXDWORD wxControl::MSWGetStyle(long style, WXDWORD *exstyle) const
189 {
190 long msStyle = wxWindow::MSWGetStyle(style, exstyle);
191
192 if ( AcceptsFocus() )
193 {
194 msStyle |= WS_TABSTOP;
195 }
196
197 return msStyle;
198 }
199
200 wxSize wxControl::DoGetBestSize() const
201 {
202 return wxSize(DEFAULT_ITEM_WIDTH, DEFAULT_ITEM_HEIGHT);
203 }
204
205 // This is a helper for all wxControls made with UPDOWN native control.
206 // In wxMSW it was only wxSpinCtrl derived from wxSpinButton but in
207 // WinCE of Smartphones this happens also for native wxTextCtrl,
208 // wxChoice and others.
209 wxSize wxControl::GetBestSpinerSize(const bool is_vertical) const
210 {
211 // take size according to layout
212 wxSize bestSize(
213 #if defined(__SMARTPHONE__) && defined(__WXWINCE__)
214 0,GetCharHeight()
215 #else
216 GetSystemMetrics(is_vertical ? SM_CXVSCROLL : SM_CXHSCROLL),
217 GetSystemMetrics(is_vertical ? SM_CYVSCROLL : SM_CYHSCROLL)
218 #endif
219 );
220
221 // correct size as for undocumented MSW variants cases (WinCE and perhaps others)
222 if (bestSize.x==0)
223 bestSize.x = bestSize.y;
224 if (bestSize.y==0)
225 bestSize.y = bestSize.x;
226
227 // double size according to layout
228 if (is_vertical)
229 bestSize.y *= 2;
230 else
231 bestSize.x *= 2;
232
233 return bestSize;
234 }
235
236 /* static */ wxVisualAttributes
237 wxControl::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant))
238 {
239 wxVisualAttributes attrs;
240
241 // old school (i.e. not "common") controls use the standard dialog font
242 // by default
243 attrs.font = wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT);
244
245 // most, or at least many, of the controls use the same colours as the
246 // buttons -- others will have to override this (and possibly simply call
247 // GetCompositeControlsDefaultAttributes() from their versions)
248 attrs.colFg = wxSystemSettings::GetColour(wxSYS_COLOUR_BTNTEXT);
249 attrs.colBg = wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE);
250
251 return attrs;
252 }
253
254 // another version for the "composite", i.e. non simple controls
255 /* static */ wxVisualAttributes
256 wxControl::GetCompositeControlsDefaultAttributes(wxWindowVariant WXUNUSED(variant))
257 {
258 wxVisualAttributes attrs;
259 attrs.font = wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT);
260 attrs.colFg = wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT);
261 attrs.colBg = wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW);
262
263 return attrs;
264 }
265
266 // ----------------------------------------------------------------------------
267 // message handling
268 // ----------------------------------------------------------------------------
269
270 bool wxControl::ProcessCommand(wxCommandEvent& event)
271 {
272 return GetEventHandler()->ProcessEvent(event);
273 }
274
275 #ifdef __WIN95__
276 bool wxControl::MSWOnNotify(int idCtrl,
277 WXLPARAM lParam,
278 WXLPARAM* result)
279 {
280 wxEventType eventType wxDUMMY_INITIALIZE(wxEVT_NULL);
281
282 NMHDR *hdr = (NMHDR*) lParam;
283 switch ( hdr->code )
284 {
285 case NM_CLICK:
286 eventType = wxEVT_COMMAND_LEFT_CLICK;
287 break;
288
289 case NM_DBLCLK:
290 eventType = wxEVT_COMMAND_LEFT_DCLICK;
291 break;
292
293 case NM_RCLICK:
294 eventType = wxEVT_COMMAND_RIGHT_CLICK;
295 break;
296
297 case NM_RDBLCLK:
298 eventType = wxEVT_COMMAND_RIGHT_DCLICK;
299 break;
300
301 case NM_SETFOCUS:
302 eventType = wxEVT_COMMAND_SET_FOCUS;
303 break;
304
305 case NM_KILLFOCUS:
306 eventType = wxEVT_COMMAND_KILL_FOCUS;
307 break;
308
309 case NM_RETURN:
310 eventType = wxEVT_COMMAND_ENTER;
311 break;
312
313 default:
314 return wxWindow::MSWOnNotify(idCtrl, lParam, result);
315 }
316
317 wxCommandEvent event(wxEVT_NULL, m_windowId);
318 event.SetEventType(eventType);
319 event.SetEventObject(this);
320
321 return GetEventHandler()->ProcessEvent(event);
322 }
323 #endif // Win95
324
325 WXHBRUSH wxControl::OnCtlColor(WXHDC pDC, WXHWND WXUNUSED(pWnd), WXUINT WXUNUSED(nCtlColor),
326 #if wxUSE_CTL3D
327 WXUINT message,
328 WXWPARAM wParam,
329 WXLPARAM lParam
330 #else
331 WXUINT WXUNUSED(message),
332 WXWPARAM WXUNUSED(wParam),
333 WXLPARAM WXUNUSED(lParam)
334 #endif
335 )
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;
346 wxColour colBack = GetBackgroundColour();
347
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
356 // ---------------------------------------------------------------------------
357 // global functions
358 // ---------------------------------------------------------------------------
359
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 //
363 // Call this repeatedly for several wnds to find the overall size
364 // of the widget.
365 // Call it initially with wxDefaultCoord 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)
369 {
370 int left = rect->left;
371 int right = rect->right;
372 int top = rect->top;
373 int bottom = rect->bottom;
374
375 GetWindowRect((HWND) wnd, rect);
376
377 if (left < 0)
378 return;
379
380 if (left < rect->left)
381 rect->left = left;
382
383 if (right > rect->right)
384 rect->right = right;
385
386 if (top < rect->top)
387 rect->top = top;
388
389 if (bottom > rect->bottom)
390 rect->bottom = bottom;
391 }
392
393 #endif // wxUSE_CONTROLS