]> git.saurik.com Git - wxWidgets.git/blob - src/msw/control.cpp
configure didn't define __WXDEBUG__, fixed
[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 #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(__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 int mode = ::SetMapMode(hdc, MM_TEXT);
252
253 ::FillRect(hdc, &rect, hBrush);
254 ::DeleteObject(hBrush);
255 ::SetMapMode(hdc, mode);
256 }
257
258 WXHBRUSH wxControl::OnCtlColor(WXHDC pDC, WXHWND WXUNUSED(pWnd), WXUINT WXUNUSED(nCtlColor),
259 #if wxUSE_CTL3D
260 WXUINT message,
261 WXWPARAM wParam,
262 WXLPARAM lParam
263 #else
264 WXUINT WXUNUSED(message),
265 WXWPARAM WXUNUSED(wParam),
266 WXLPARAM WXUNUSED(lParam)
267 #endif
268 )
269 {
270 #if wxUSE_CTL3D
271 if ( m_useCtl3D )
272 {
273 HBRUSH hbrush = Ctl3dCtlColorEx(message, wParam, lParam);
274 return (WXHBRUSH) hbrush;
275 }
276 #endif // wxUSE_CTL3D
277
278 HDC hdc = (HDC)pDC;
279 if (GetParent()->GetTransparentBackground() /* || (GetParent()->GetExtraStyle() & wxWS_EX_THEMED_BACKGROUND) */ )
280 SetBkMode(hdc, TRANSPARENT);
281 else
282 SetBkMode(hdc, OPAQUE);
283
284 wxColour colBack = GetBackgroundColour();
285
286 ::SetBkColor(hdc, wxColourToRGB(colBack));
287 ::SetTextColor(hdc, wxColourToRGB(GetForegroundColour()));
288
289 wxBrush *brush = wxTheBrushList->FindOrCreateBrush(colBack, wxSOLID);
290
291 return (WXHBRUSH)brush->GetResourceHandle();
292 }
293
294 WXDWORD wxControl::MSWGetStyle(long style, WXDWORD *exstyle) const
295 {
296 long msStyle = wxWindow::MSWGetStyle(style, exstyle);
297
298 if ( AcceptsFocus() )
299 {
300 msStyle |= WS_TABSTOP;
301 }
302
303 return msStyle;
304 }
305
306 // ---------------------------------------------------------------------------
307 // global functions
308 // ---------------------------------------------------------------------------
309
310 // Call this repeatedly for several wnds to find the overall size
311 // of the widget.
312 // Call it initially with -1 for all values in rect.
313 // Keep calling for other widgets, and rect will be modified
314 // to calculate largest bounding rectangle.
315 void wxFindMaxSize(WXHWND wnd, RECT *rect)
316 {
317 int left = rect->left;
318 int right = rect->right;
319 int top = rect->top;
320 int bottom = rect->bottom;
321
322 GetWindowRect((HWND) wnd, rect);
323
324 if (left < 0)
325 return;
326
327 if (left < rect->left)
328 rect->left = left;
329
330 if (right > rect->right)
331 rect->right = right;
332
333 if (top < rect->top)
334 rect->top = top;
335
336 if (bottom > rect->bottom)
337 rect->bottom = bottom;
338 }
339
340 #endif // wxUSE_CONTROLS