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