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