]> git.saurik.com Git - wxWidgets.git/blob - src/msw/control.cpp
some compilation fixes for mingw32 and not only
[wxWidgets.git] / src / msw / control.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: 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 #ifndef WX_PRECOMP
24 #include "wx/event.h"
25 #include "wx/app.h"
26 #include "wx/dcclient.h"
27 #endif
28
29 #include "wx/control.h"
30
31 #include "wx/msw/private.h"
32
33 #if (defined(__WIN95__) && !defined(__GNUWIN32__)) || defined(__TWIN32__) || defined(wxUSE_NORLANDER_HEADERS)
34 #include <commctrl.h>
35 #endif
36
37 IMPLEMENT_ABSTRACT_CLASS(wxControl, wxWindow)
38
39 BEGIN_EVENT_TABLE(wxControl, wxWindow)
40 EVT_ERASE_BACKGROUND(wxControl::OnEraseBackground)
41 END_EVENT_TABLE()
42
43 // Item members
44 wxControl::wxControl()
45 {
46 m_backgroundColour = *wxWHITE;
47 m_foregroundColour = *wxBLACK;
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 // VZ: if someone could put a comment here explaining what exactly this is
83 // needed for, it would be nice...
84 bool want3D;
85
86 // if no extended style given, determine it ourselves
87 if ( exstyle == (WXDWORD)-1 )
88 {
89 exstyle = GetExStyle(style, &want3D);
90 }
91
92 // all controls have these childs (wxWindows creates all controls visible
93 // by default)
94 style |= WS_CHILD | WS_VISIBLE;
95
96 m_hWnd = (WXHWND)::CreateWindowEx
97 (
98 exstyle, // extended style
99 classname, // the kind of control to create
100 label, // the window name
101 style, // the window style
102 pos.x, pos.y, // the window position
103 size.x, size.y, // and size
104 GetHwndOf(GetParent()), // parent
105 (HMENU)GetId(), // child id
106 wxGetInstance(), // app instance
107 NULL // creation parameters
108 );
109
110 if ( !m_hWnd )
111 {
112 #ifdef __WXDEBUG__
113 wxLogError(wxT("Failed to create a control of class '%s'"), classname);
114 #endif // DEBUG
115
116 return FALSE;
117 }
118
119 #if wxUSE_CTL3D
120 if ( want3D )
121 {
122 Ctl3dSubclassCtl(GetHwnd());
123 m_useCtl3D = TRUE;
124 }
125 #endif // wxUSE_CTL3D
126
127 // subclass again for purposes of dialog editing mode
128 SubclassWin(m_hWnd);
129
130 // controls use the same font and colours as their parent dialog by default
131 InheritAttributes();
132
133 return TRUE;
134 }
135
136 wxSize wxControl::DoGetBestSize() const
137 {
138 return wxSize(DEFAULT_ITEM_WIDTH, DEFAULT_ITEM_HEIGHT);
139 }
140
141 bool wxControl::ProcessCommand(wxCommandEvent& event)
142 {
143 #if WXWIN_COMPATIBILITY
144 if ( m_callback )
145 {
146 (void)(*m_callback)(this, event);
147
148 return TRUE;
149 }
150 else
151 #endif // WXWIN_COMPATIBILITY
152
153 return GetEventHandler()->ProcessEvent(event);
154 }
155
156 #ifdef __WIN95__
157 bool wxControl::MSWOnNotify(int idCtrl,
158 WXLPARAM lParam,
159 WXLPARAM* result)
160 {
161 wxCommandEvent event(wxEVT_NULL, m_windowId);
162 wxEventType eventType = wxEVT_NULL;
163 NMHDR *hdr1 = (NMHDR*) lParam;
164 switch ( hdr1->code )
165 {
166 case NM_CLICK:
167 eventType = wxEVT_COMMAND_LEFT_CLICK;
168 break;
169
170 case NM_DBLCLK:
171 eventType = wxEVT_COMMAND_LEFT_DCLICK;
172 break;
173
174 case NM_RCLICK:
175 eventType = wxEVT_COMMAND_RIGHT_CLICK;
176 break;
177
178 case NM_RDBLCLK:
179 eventType = wxEVT_COMMAND_RIGHT_DCLICK;
180 break;
181
182 case NM_SETFOCUS:
183 eventType = wxEVT_COMMAND_SET_FOCUS;
184 break;
185
186 case NM_KILLFOCUS:
187 eventType = wxEVT_COMMAND_KILL_FOCUS;
188 break;
189
190 case NM_RETURN:
191 eventType = wxEVT_COMMAND_ENTER;
192 break;
193
194 default:
195 return wxWindow::MSWOnNotify(idCtrl, lParam, result);
196 }
197
198 event.SetEventType(eventType);
199 event.SetEventObject(this);
200
201 return GetEventHandler()->ProcessEvent(event);
202 }
203 #endif // Win95
204
205 void wxControl::OnEraseBackground(wxEraseEvent& event)
206 {
207 // In general, you don't want to erase the background of a control,
208 // or you'll get a flicker.
209 // TODO: move this 'null' function into each control that
210 // might flicker.
211
212 RECT rect;
213 ::GetClientRect(GetHwnd(), &rect);
214
215 HBRUSH hBrush = ::CreateSolidBrush(wxColourToRGB(GetBackgroundColour()));
216
217 HDC hdc = GetHdcOf((*event.GetDC()));
218 int mode = ::SetMapMode(hdc, MM_TEXT);
219
220 ::FillRect(hdc, &rect, hBrush);
221 ::DeleteObject(hBrush);
222 ::SetMapMode(hdc, mode);
223 }
224
225 WXHBRUSH wxControl::OnCtlColor(WXHDC pDC, WXHWND pWnd, WXUINT nCtlColor,
226 WXUINT message,
227 WXWPARAM wParam,
228 WXLPARAM lParam)
229 {
230 #if wxUSE_CTL3D
231 if ( m_useCtl3D )
232 {
233 HBRUSH hbrush = Ctl3dCtlColorEx(message, wParam, lParam);
234 return (WXHBRUSH) hbrush;
235 }
236 #endif // wxUSE_CTL3D
237
238 HDC hdc = (HDC)pDC;
239 if (GetParent()->GetTransparentBackground())
240 SetBkMode(hdc, TRANSPARENT);
241 else
242 SetBkMode(hdc, OPAQUE);
243
244 const wxColour& colBack = GetBackgroundColour();
245 ::SetBkColor(hdc, wxColourToRGB(colBack));
246 ::SetTextColor(hdc, wxColourToRGB(GetForegroundColour()));
247
248 wxBrush *brush = wxTheBrushList->FindOrCreateBrush(colBack, wxSOLID);
249
250 return (WXHBRUSH)brush->GetResourceHandle();
251 }
252
253 WXDWORD wxControl::GetExStyle(WXDWORD& style, bool *want3D) const
254 {
255 WXDWORD exStyle = Determine3DEffects(WS_EX_CLIENTEDGE, want3D);
256
257 // Even with extended styles, need to combine with WS_BORDER for them to
258 // look right.
259 if ( *want3D || wxStyleHasBorder(m_windowStyle) )
260 style |= WS_BORDER;
261
262 return exStyle;
263 }
264
265 // ---------------------------------------------------------------------------
266 // global functions
267 // ---------------------------------------------------------------------------
268
269 // Call this repeatedly for several wnds to find the overall size
270 // of the widget.
271 // Call it initially with -1 for all values in rect.
272 // Keep calling for other widgets, and rect will be modified
273 // to calculate largest bounding rectangle.
274 void wxFindMaxSize(WXHWND wnd, RECT *rect)
275 {
276 int left = rect->left;
277 int right = rect->right;
278 int top = rect->top;
279 int bottom = rect->bottom;
280
281 GetWindowRect((HWND) wnd, rect);
282
283 if (left < 0)
284 return;
285
286 if (left < rect->left)
287 rect->left = left;
288
289 if (right > rect->right)
290 rect->right = right;
291
292 if (top < rect->top)
293 rect->top = top;
294
295 if (bottom > rect->bottom)
296 rect->bottom = bottom;
297 }
298