]> git.saurik.com Git - wxWidgets.git/blob - src/msw/control.cpp
inform the IM context about focus changes
[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 == -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
134 // ... and adjust it to account for ap ossible 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"));
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 SetFont(GetDefaultAttributes().font);
172
173 // set the size now if no initial size specified
174 SetInitialBestSize(size);
175
176 return TRUE;
177 }
178
179 // ----------------------------------------------------------------------------
180 // various accessors
181 // ----------------------------------------------------------------------------
182
183 wxBorder wxControl::GetDefaultBorder() const
184 {
185 // we want to automatically give controls a sunken style (confusingly,
186 // it may not really mean sunken at all as we map it to WS_EX_CLIENTEDGE
187 // which is not sunken at all under Windows XP -- rather, just the default)
188 return wxBORDER_SUNKEN;
189 }
190
191 WXDWORD wxControl::MSWGetStyle(long style, WXDWORD *exstyle) const
192 {
193 long msStyle = wxWindow::MSWGetStyle(style, exstyle);
194
195 if ( AcceptsFocus() )
196 {
197 msStyle |= WS_TABSTOP;
198 }
199
200 return msStyle;
201 }
202
203 wxSize wxControl::DoGetBestSize() const
204 {
205 return wxSize(DEFAULT_ITEM_WIDTH, DEFAULT_ITEM_HEIGHT);
206 }
207
208 /* static */ wxVisualAttributes
209 wxControl::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant))
210 {
211 wxVisualAttributes attrs;
212
213 // old school (i.e. not "common") controls use the standard dialog font
214 // by default
215 attrs.font = wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT);
216
217 // most, or at least many, of the controls use the same colours as the
218 // buttons -- others will have to override this (and possibly simply call
219 // GetCompositeControlsDefaultAttributes() from their versions)
220 attrs.colFg = wxSystemSettings::GetColour(wxSYS_COLOUR_BTNTEXT);
221 attrs.colBg = wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE);
222
223 return attrs;
224 }
225
226 // another version for the "composite", i.e. non simple controls
227 /* static */ wxVisualAttributes
228 wxControl::GetCompositeControlsDefaultAttributes(wxWindowVariant WXUNUSED(variant))
229 {
230 wxVisualAttributes attrs;
231 attrs.font = wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT);
232 attrs.colFg = wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT);
233 attrs.colBg = wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW);
234
235 return attrs;
236 }
237
238 // ----------------------------------------------------------------------------
239 // message handling
240 // ----------------------------------------------------------------------------
241
242 bool wxControl::ProcessCommand(wxCommandEvent& event)
243 {
244 return GetEventHandler()->ProcessEvent(event);
245 }
246
247 #ifdef __WIN95__
248 bool wxControl::MSWOnNotify(int idCtrl,
249 WXLPARAM lParam,
250 WXLPARAM* result)
251 {
252 wxEventType eventType = wxEVT_NULL;
253
254 NMHDR *hdr = (NMHDR*) lParam;
255 switch ( hdr->code )
256 {
257 case NM_CLICK:
258 eventType = wxEVT_COMMAND_LEFT_CLICK;
259 break;
260
261 case NM_DBLCLK:
262 eventType = wxEVT_COMMAND_LEFT_DCLICK;
263 break;
264
265 case NM_RCLICK:
266 eventType = wxEVT_COMMAND_RIGHT_CLICK;
267 break;
268
269 case NM_RDBLCLK:
270 eventType = wxEVT_COMMAND_RIGHT_DCLICK;
271 break;
272
273 case NM_SETFOCUS:
274 eventType = wxEVT_COMMAND_SET_FOCUS;
275 break;
276
277 case NM_KILLFOCUS:
278 eventType = wxEVT_COMMAND_KILL_FOCUS;
279 break;
280
281 case NM_RETURN:
282 eventType = wxEVT_COMMAND_ENTER;
283 break;
284
285 default:
286 return wxWindow::MSWOnNotify(idCtrl, lParam, result);
287 }
288
289 wxCommandEvent event(wxEVT_NULL, m_windowId);
290 event.SetEventType(eventType);
291 event.SetEventObject(this);
292
293 return GetEventHandler()->ProcessEvent(event);
294 }
295 #endif // Win95
296
297 void wxControl::OnEraseBackground(wxEraseEvent& event)
298 {
299 // notice that this 'dumb' implementation may cause flicker for some of the
300 // controls in which case they should intercept wxEraseEvent and process it
301 // themselves somehow
302
303 RECT rect;
304 ::GetClientRect(GetHwnd(), &rect);
305
306 HBRUSH hBrush = ::CreateSolidBrush(wxColourToRGB(GetBackgroundColour()));
307
308 HDC hdc = GetHdcOf((*event.GetDC()));
309
310 #ifndef __WXWINCE__
311 int mode = ::SetMapMode(hdc, MM_TEXT);
312 #endif
313
314 ::FillRect(hdc, &rect, hBrush);
315 ::DeleteObject(hBrush);
316
317 #ifndef __WXWINCE__
318 ::SetMapMode(hdc, mode);
319 #endif
320 }
321
322 WXHBRUSH wxControl::OnCtlColor(WXHDC pDC, WXHWND WXUNUSED(pWnd), WXUINT WXUNUSED(nCtlColor),
323 #if wxUSE_CTL3D
324 WXUINT message,
325 WXWPARAM wParam,
326 WXLPARAM lParam
327 #else
328 WXUINT WXUNUSED(message),
329 WXWPARAM WXUNUSED(wParam),
330 WXLPARAM WXUNUSED(lParam)
331 #endif
332 )
333 {
334 #if wxUSE_CTL3D
335 if ( m_useCtl3D )
336 {
337 HBRUSH hbrush = Ctl3dCtlColorEx(message, wParam, lParam);
338 return (WXHBRUSH) hbrush;
339 }
340 #endif // wxUSE_CTL3D
341
342 HDC hdc = (HDC)pDC;
343 wxColour colBack = GetBackgroundColour();
344
345 ::SetBkColor(hdc, wxColourToRGB(colBack));
346 ::SetTextColor(hdc, wxColourToRGB(GetForegroundColour()));
347
348 wxBrush *brush = wxTheBrushList->FindOrCreateBrush(colBack, wxSOLID);
349
350 return (WXHBRUSH)brush->GetResourceHandle();
351 }
352
353 // ---------------------------------------------------------------------------
354 // global functions
355 // ---------------------------------------------------------------------------
356
357 // this is used in radiobox.cpp and slider95.cpp and should be removed as soon
358 // as it is not needed there any more!
359 //
360 // Call this repeatedly for several wnds to find the overall size
361 // of the widget.
362 // Call it initially with -1 for all values in rect.
363 // Keep calling for other widgets, and rect will be modified
364 // to calculate largest bounding rectangle.
365 void wxFindMaxSize(WXHWND wnd, RECT *rect)
366 {
367 int left = rect->left;
368 int right = rect->right;
369 int top = rect->top;
370 int bottom = rect->bottom;
371
372 GetWindowRect((HWND) wnd, rect);
373
374 if (left < 0)
375 return;
376
377 if (left < rect->left)
378 rect->left = left;
379
380 if (right > rect->right)
381 rect->right = right;
382
383 if (top < rect->top)
384 rect->top = top;
385
386 if (bottom > rect->bottom)
387 rect->bottom = bottom;
388 }
389
390 #endif // wxUSE_CONTROLS