]> git.saurik.com Git - wxWidgets.git/blob - src/msw/control.cpp
b265d56be0491b3d236238a604113769fe194710
[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 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
13 #pragma implementation "control.h"
14 #endif
15
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
18
19 #ifdef __BORLANDC__
20 #pragma hdrstop
21 #endif
22
23 #if wxUSE_CONTROLS
24
25 #ifndef WX_PRECOMP
26 #include "wx/event.h"
27 #include "wx/app.h"
28 #include "wx/dcclient.h"
29 #include "wx/log.h"
30 #endif
31
32 #include "wx/control.h"
33
34 #include "wx/msw/private.h"
35
36 #if defined(__WIN95__) && !(defined(__GNUWIN32_OLD__) && !defined(__CYGWIN10__))
37 #include <commctrl.h>
38 #endif
39
40 IMPLEMENT_ABSTRACT_CLASS(wxControl, wxWindow)
41
42 BEGIN_EVENT_TABLE(wxControl, wxWindow)
43 EVT_ERASE_BACKGROUND(wxControl::OnEraseBackground)
44 END_EVENT_TABLE()
45
46 // Item members
47 wxControl::wxControl()
48 {
49 #if WXWIN_COMPATIBILITY
50 m_callback = 0;
51 #endif // WXWIN_COMPATIBILITY
52 }
53
54 wxControl::~wxControl()
55 {
56 m_isBeingDeleted = TRUE;
57 }
58
59
60 bool wxControl::Create(wxWindow *parent,
61 wxWindowID id,
62 const wxPoint& pos,
63 const wxSize& size,
64 long style,
65 const wxValidator& validator,
66 const wxString& name)
67 {
68 if ( !wxWindow::Create(parent, id, pos, size, style, name) )
69 return FALSE;
70
71 #if wxUSE_VALIDATORS
72 SetValidator(validator);
73 #endif
74
75 return TRUE;
76 }
77
78 bool wxControl::MSWCreateControl(const wxChar *classname,
79 const wxString& label,
80 const wxPoint& pos,
81 const wxSize& size)
82 {
83 WXDWORD exstyle;
84 WXDWORD msStyle = MSWGetStyle(GetWindowStyle(), &exstyle);
85
86 return MSWCreateControl(classname, msStyle, pos, size, label, exstyle);
87 }
88
89 bool wxControl::MSWCreateControl(const wxChar *classname,
90 WXDWORD style,
91 const wxPoint& pos,
92 const wxSize& size,
93 const wxString& label,
94 WXDWORD exstyle)
95 {
96 // if no extended style given, determine it ourselves
97 if ( exstyle == (WXDWORD)-1 )
98 {
99 exstyle = 0;
100 (void) MSWGetStyle(GetWindowStyle(), &exstyle);
101 }
102
103 // all controls should have this style
104 style |= WS_CHILD;
105
106 // create the control visible if it's currently shown for wxWindows
107 if ( m_isShown )
108 {
109 style |= WS_VISIBLE;
110 }
111
112 int x = pos.x == -1 ? 0 : pos.x,
113 y = pos.y == -1 ? 0 : pos.y,
114 w = size.x == -1 ? 0 : size.x,
115 h = size.y == -1 ? 0 : size.y;
116
117 m_hWnd = (WXHWND)::CreateWindowEx
118 (
119 exstyle, // extended style
120 classname, // the kind of control to create
121 label, // the window name
122 style, // the window style
123 x, y, w, h, // the window position and size
124 GetHwndOf(GetParent()), // parent
125 (HMENU)GetId(), // child id
126 wxGetInstance(), // app instance
127 NULL // creation parameters
128 );
129
130 if ( !m_hWnd )
131 {
132 wxLogDebug(wxT("Failed to create a control of class '%s'"), classname);
133 wxFAIL_MSG(_T("something is very wrong"));
134
135 return FALSE;
136 }
137
138 #if wxUSE_CTL3D
139 if ( want3D )
140 {
141 Ctl3dSubclassCtl(GetHwnd());
142 m_useCtl3D = TRUE;
143 }
144 #endif // wxUSE_CTL3D
145
146 // install wxWindows window proc for this window
147 SubclassWin(m_hWnd);
148
149 // controls use the same font and colours as their parent dialog by default
150 InheritAttributes();
151
152 // set the size now if no initial size specified
153 if ( w <= 0 || h <= 0 )
154 {
155 SetBestSize(size);
156 }
157
158 return TRUE;
159 }
160
161 wxBorder wxControl::GetDefaultBorder() const
162 {
163 // we want to automatically give controls a sunken style (confusingly,
164 // it may not really mean sunken at all as we map it to WS_EX_CLIENTEDGE
165 // which is not sunken at all under Windows XP -- rather, just the default)
166 return wxBORDER_SUNKEN;
167 }
168
169 wxSize wxControl::DoGetBestSize() const
170 {
171 return wxSize(DEFAULT_ITEM_WIDTH, DEFAULT_ITEM_HEIGHT);
172 }
173
174 bool wxControl::ProcessCommand(wxCommandEvent& event)
175 {
176 #if WXWIN_COMPATIBILITY
177 if ( m_callback )
178 {
179 (void)(*m_callback)(*this, event);
180
181 return TRUE;
182 }
183 else
184 #endif // WXWIN_COMPATIBILITY
185
186 return GetEventHandler()->ProcessEvent(event);
187 }
188
189 #ifdef __WIN95__
190 bool wxControl::MSWOnNotify(int idCtrl,
191 WXLPARAM lParam,
192 WXLPARAM* result)
193 {
194 wxEventType eventType = wxEVT_NULL;
195
196 NMHDR *hdr = (NMHDR*) lParam;
197 switch ( hdr->code )
198 {
199 case NM_CLICK:
200 eventType = wxEVT_COMMAND_LEFT_CLICK;
201 break;
202
203 case NM_DBLCLK:
204 eventType = wxEVT_COMMAND_LEFT_DCLICK;
205 break;
206
207 case NM_RCLICK:
208 eventType = wxEVT_COMMAND_RIGHT_CLICK;
209 break;
210
211 case NM_RDBLCLK:
212 eventType = wxEVT_COMMAND_RIGHT_DCLICK;
213 break;
214
215 case NM_SETFOCUS:
216 eventType = wxEVT_COMMAND_SET_FOCUS;
217 break;
218
219 case NM_KILLFOCUS:
220 eventType = wxEVT_COMMAND_KILL_FOCUS;
221 break;
222
223 case NM_RETURN:
224 eventType = wxEVT_COMMAND_ENTER;
225 break;
226
227 default:
228 return wxWindow::MSWOnNotify(idCtrl, lParam, result);
229 }
230
231 wxCommandEvent event(wxEVT_NULL, m_windowId);
232 event.SetEventType(eventType);
233 event.SetEventObject(this);
234
235 return GetEventHandler()->ProcessEvent(event);
236 }
237 #endif // Win95
238
239 void wxControl::OnEraseBackground(wxEraseEvent& event)
240 {
241 // notice that this 'dumb' implementation may cause flicker for some of the
242 // controls in which case they should intercept wxEraseEvent and process it
243 // themselves somehow
244
245 RECT rect;
246 ::GetClientRect(GetHwnd(), &rect);
247
248 HBRUSH hBrush = ::CreateSolidBrush(wxColourToRGB(GetBackgroundColour()));
249
250 HDC hdc = GetHdcOf((*event.GetDC()));
251
252 #ifndef __WXWINCE__
253 int mode = ::SetMapMode(hdc, MM_TEXT);
254 #endif
255
256 ::FillRect(hdc, &rect, hBrush);
257 ::DeleteObject(hBrush);
258
259 #ifndef __WXWINCE__
260 ::SetMapMode(hdc, mode);
261 #endif
262 }
263
264 WXHBRUSH wxControl::OnCtlColor(WXHDC pDC, WXHWND WXUNUSED(pWnd), WXUINT WXUNUSED(nCtlColor),
265 #if wxUSE_CTL3D
266 WXUINT message,
267 WXWPARAM wParam,
268 WXLPARAM lParam
269 #else
270 WXUINT WXUNUSED(message),
271 WXWPARAM WXUNUSED(wParam),
272 WXLPARAM WXUNUSED(lParam)
273 #endif
274 )
275 {
276 #if wxUSE_CTL3D
277 if ( m_useCtl3D )
278 {
279 HBRUSH hbrush = Ctl3dCtlColorEx(message, wParam, lParam);
280 return (WXHBRUSH) hbrush;
281 }
282 #endif // wxUSE_CTL3D
283
284 HDC hdc = (HDC)pDC;
285 wxColour colBack = GetBackgroundColour();
286
287 ::SetBkColor(hdc, wxColourToRGB(colBack));
288 ::SetTextColor(hdc, wxColourToRGB(GetForegroundColour()));
289
290 wxBrush *brush = wxTheBrushList->FindOrCreateBrush(colBack, wxSOLID);
291
292 return (WXHBRUSH)brush->GetResourceHandle();
293 }
294
295 WXDWORD wxControl::MSWGetStyle(long style, WXDWORD *exstyle) const
296 {
297 long msStyle = wxWindow::MSWGetStyle(style, exstyle);
298
299 if ( AcceptsFocus() )
300 {
301 msStyle |= WS_TABSTOP;
302 }
303
304 return msStyle;
305 }
306
307 // ---------------------------------------------------------------------------
308 // global functions
309 // ---------------------------------------------------------------------------
310
311 // Call this repeatedly for several wnds to find the overall size
312 // of the widget.
313 // Call it initially with -1 for all values in rect.
314 // Keep calling for other widgets, and rect will be modified
315 // to calculate largest bounding rectangle.
316 void wxFindMaxSize(WXHWND wnd, RECT *rect)
317 {
318 int left = rect->left;
319 int right = rect->right;
320 int top = rect->top;
321 int bottom = rect->bottom;
322
323 GetWindowRect((HWND) wnd, rect);
324
325 if (left < 0)
326 return;
327
328 if (left < rect->left)
329 rect->left = left;
330
331 if (right > rect->right)
332 rect->right = right;
333
334 if (top < rect->top)
335 rect->top = top;
336
337 if (bottom > rect->bottom)
338 rect->bottom = bottom;
339 }
340
341 #endif // wxUSE_CONTROLS