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