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