]> git.saurik.com Git - wxWidgets.git/blob - src/msw/control.cpp
Peparation for native Smartphone controls based on Spinners.
[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 BEGIN_EVENT_TABLE(wxControl, wxWindow)
56 EVT_ERASE_BACKGROUND(wxControl::OnEraseBackground)
57 END_EVENT_TABLE()
58
59 // ============================================================================
60 // wxControl implementation
61 // ============================================================================
62
63 // ----------------------------------------------------------------------------
64 // wxControl ctor/dtor
65 // ----------------------------------------------------------------------------
66
67 wxControl::~wxControl()
68 {
69 m_isBeingDeleted = true;
70 }
71
72 // ----------------------------------------------------------------------------
73 // control window creation
74 // ----------------------------------------------------------------------------
75
76 bool wxControl::Create(wxWindow *parent,
77 wxWindowID id,
78 const wxPoint& pos,
79 const wxSize& size,
80 long style,
81 const wxValidator& wxVALIDATOR_PARAM(validator),
82 const wxString& name)
83 {
84 if ( !wxWindow::Create(parent, id, pos, size, style, name) )
85 return false;
86
87 #if wxUSE_VALIDATORS
88 SetValidator(validator);
89 #endif
90
91 return true;
92 }
93
94 bool wxControl::MSWCreateControl(const wxChar *classname,
95 const wxString& label,
96 const wxPoint& pos,
97 const wxSize& size)
98 {
99 WXDWORD exstyle;
100 WXDWORD msStyle = MSWGetStyle(GetWindowStyle(), &exstyle);
101
102 return MSWCreateControl(classname, msStyle, pos, size, label, exstyle);
103 }
104
105 bool wxControl::MSWCreateControl(const wxChar *classname,
106 WXDWORD style,
107 const wxPoint& pos,
108 const wxSize& size,
109 const wxString& label,
110 WXDWORD exstyle)
111 {
112 // if no extended style given, determine it ourselves
113 if ( exstyle == (WXDWORD)-1 )
114 {
115 exstyle = 0;
116 (void) MSWGetStyle(GetWindowStyle(), &exstyle);
117 }
118
119 // all controls should have this style
120 style |= WS_CHILD;
121
122 // create the control visible if it's currently shown for wxWidgets
123 if ( m_isShown )
124 {
125 style |= WS_VISIBLE;
126 }
127
128 // choose the position for the control
129 int x = pos.x == wxDefaultCoord ? 0 : pos.x,
130 y = pos.y == wxDefaultCoord ? 0 : pos.y,
131 w = size.x == wxDefaultCoord ? 0 : size.x,
132 h = size.y == wxDefaultCoord ? 0 : size.y;
133
134 // ... and adjust it to account for a possible parent frames toolbar
135 AdjustForParentClientOrigin(x, y);
136
137 m_hWnd = (WXHWND)::CreateWindowEx
138 (
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
148 );
149
150 if ( !m_hWnd )
151 {
152 wxLogDebug(wxT("Failed to create a control of class '%s'"), classname);
153 wxFAIL_MSG(_T("something is very wrong, CreateWindowEx failed"));
154
155 return false;
156 }
157
158 #if wxUSE_CTL3D
159 if ( want3D )
160 {
161 Ctl3dSubclassCtl(GetHwnd());
162 m_useCtl3D = true;
163 }
164 #endif // wxUSE_CTL3D
165
166 // install wxWidgets window proc for this window
167 SubclassWin(m_hWnd);
168
169 // set up fonts and colours
170 InheritAttributes();
171 if (!m_hasFont)
172 SetFont(GetDefaultAttributes().font);
173
174 // set the size now if no initial size specified
175 SetInitialBestSize(size);
176
177 return true;
178 }
179
180 // ----------------------------------------------------------------------------
181 // various accessors
182 // ----------------------------------------------------------------------------
183
184 wxBorder wxControl::GetDefaultBorder() const
185 {
186 // we want to automatically give controls a sunken style (confusingly,
187 // it may not really mean sunken at all as we map it to WS_EX_CLIENTEDGE
188 // which is not sunken at all under Windows XP -- rather, just the default)
189 return wxBORDER_SUNKEN;
190 }
191
192 WXDWORD wxControl::MSWGetStyle(long style, WXDWORD *exstyle) const
193 {
194 long msStyle = wxWindow::MSWGetStyle(style, exstyle);
195
196 if ( AcceptsFocus() )
197 {
198 msStyle |= WS_TABSTOP;
199 }
200
201 return msStyle;
202 }
203
204 wxSize wxControl::DoGetBestSize() const
205 {
206 return wxSize(DEFAULT_ITEM_WIDTH, DEFAULT_ITEM_HEIGHT);
207 }
208
209 // This is a helper for all wxControls made with UPDOWN native control.
210 // In wxMSW it was only wxSpinCtrl derived from wxSpinButton but in
211 // WinCE of Smartphones this happens also for native wxTextCtrl,
212 // wxChoice and others.
213 wxSize wxControl::GetBestSpinerSize(const bool is_vertical) const
214 {
215 if (is_vertical)
216 {
217 // vertical control
218 return wxSize(GetSystemMetrics(SM_CXVSCROLL),
219 2*GetSystemMetrics(SM_CYVSCROLL));
220 }
221 else
222 {
223 // horizontal control
224 return wxSize(2*GetSystemMetrics(SM_CXHSCROLL),
225 GetSystemMetrics(SM_CYHSCROLL));
226 }
227 }
228
229 /* static */ wxVisualAttributes
230 wxControl::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant))
231 {
232 wxVisualAttributes attrs;
233
234 // old school (i.e. not "common") controls use the standard dialog font
235 // by default
236 attrs.font = wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT);
237
238 // most, or at least many, of the controls use the same colours as the
239 // buttons -- others will have to override this (and possibly simply call
240 // GetCompositeControlsDefaultAttributes() from their versions)
241 attrs.colFg = wxSystemSettings::GetColour(wxSYS_COLOUR_BTNTEXT);
242 attrs.colBg = wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE);
243
244 return attrs;
245 }
246
247 // another version for the "composite", i.e. non simple controls
248 /* static */ wxVisualAttributes
249 wxControl::GetCompositeControlsDefaultAttributes(wxWindowVariant WXUNUSED(variant))
250 {
251 wxVisualAttributes attrs;
252 attrs.font = wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT);
253 attrs.colFg = wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT);
254 attrs.colBg = wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW);
255
256 return attrs;
257 }
258
259 // ----------------------------------------------------------------------------
260 // message handling
261 // ----------------------------------------------------------------------------
262
263 bool wxControl::ProcessCommand(wxCommandEvent& event)
264 {
265 return GetEventHandler()->ProcessEvent(event);
266 }
267
268 #ifdef __WIN95__
269 bool wxControl::MSWOnNotify(int idCtrl,
270 WXLPARAM lParam,
271 WXLPARAM* result)
272 {
273 wxEventType eventType wxDUMMY_INITIALIZE(wxEVT_NULL);
274
275 NMHDR *hdr = (NMHDR*) lParam;
276 switch ( hdr->code )
277 {
278 case NM_CLICK:
279 eventType = wxEVT_COMMAND_LEFT_CLICK;
280 break;
281
282 case NM_DBLCLK:
283 eventType = wxEVT_COMMAND_LEFT_DCLICK;
284 break;
285
286 case NM_RCLICK:
287 eventType = wxEVT_COMMAND_RIGHT_CLICK;
288 break;
289
290 case NM_RDBLCLK:
291 eventType = wxEVT_COMMAND_RIGHT_DCLICK;
292 break;
293
294 case NM_SETFOCUS:
295 eventType = wxEVT_COMMAND_SET_FOCUS;
296 break;
297
298 case NM_KILLFOCUS:
299 eventType = wxEVT_COMMAND_KILL_FOCUS;
300 break;
301
302 case NM_RETURN:
303 eventType = wxEVT_COMMAND_ENTER;
304 break;
305
306 default:
307 return wxWindow::MSWOnNotify(idCtrl, lParam, result);
308 }
309
310 wxCommandEvent event(wxEVT_NULL, m_windowId);
311 event.SetEventType(eventType);
312 event.SetEventObject(this);
313
314 return GetEventHandler()->ProcessEvent(event);
315 }
316 #endif // Win95
317
318 void wxControl::OnEraseBackground(wxEraseEvent& event)
319 {
320 // notice that this 'dumb' implementation may cause flicker for some of the
321 // controls in which case they should intercept wxEraseEvent and process it
322 // themselves somehow
323
324 RECT rect;
325 ::GetClientRect(GetHwnd(), &rect);
326
327 HBRUSH hBrush = ::CreateSolidBrush(wxColourToRGB(GetBackgroundColour()));
328
329 HDC hdc = GetHdcOf((*event.GetDC()));
330
331 #ifndef __WXWINCE__
332 int mode = ::SetMapMode(hdc, MM_TEXT);
333 #endif
334
335 ::FillRect(hdc, &rect, hBrush);
336 ::DeleteObject(hBrush);
337
338 #ifndef __WXWINCE__
339 ::SetMapMode(hdc, mode);
340 #endif
341 }
342
343 WXHBRUSH wxControl::OnCtlColor(WXHDC pDC, WXHWND WXUNUSED(pWnd), WXUINT WXUNUSED(nCtlColor),
344 #if wxUSE_CTL3D
345 WXUINT message,
346 WXWPARAM wParam,
347 WXLPARAM lParam
348 #else
349 WXUINT WXUNUSED(message),
350 WXWPARAM WXUNUSED(wParam),
351 WXLPARAM WXUNUSED(lParam)
352 #endif
353 )
354 {
355 #if wxUSE_CTL3D
356 if ( m_useCtl3D )
357 {
358 HBRUSH hbrush = Ctl3dCtlColorEx(message, wParam, lParam);
359 return (WXHBRUSH) hbrush;
360 }
361 #endif // wxUSE_CTL3D
362
363 HDC hdc = (HDC)pDC;
364 wxColour colBack = GetBackgroundColour();
365
366 ::SetBkColor(hdc, wxColourToRGB(colBack));
367 ::SetTextColor(hdc, wxColourToRGB(GetForegroundColour()));
368
369 wxBrush *brush = wxTheBrushList->FindOrCreateBrush(colBack, wxSOLID);
370
371 return (WXHBRUSH)brush->GetResourceHandle();
372 }
373
374 // ---------------------------------------------------------------------------
375 // global functions
376 // ---------------------------------------------------------------------------
377
378 // this is used in radiobox.cpp and slider95.cpp and should be removed as soon
379 // as it is not needed there any more!
380 //
381 // Call this repeatedly for several wnds to find the overall size
382 // of the widget.
383 // Call it initially with wxDefaultCoord for all values in rect.
384 // Keep calling for other widgets, and rect will be modified
385 // to calculate largest bounding rectangle.
386 void wxFindMaxSize(WXHWND wnd, RECT *rect)
387 {
388 int left = rect->left;
389 int right = rect->right;
390 int top = rect->top;
391 int bottom = rect->bottom;
392
393 GetWindowRect((HWND) wnd, rect);
394
395 if (left < 0)
396 return;
397
398 if (left < rect->left)
399 rect->left = left;
400
401 if (right > rect->right)
402 rect->right = right;
403
404 if (top < rect->top)
405 rect->top = top;
406
407 if (bottom > rect->bottom)
408 rect->bottom = bottom;
409 }
410
411 #endif // wxUSE_CONTROLS