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