]> git.saurik.com Git - wxWidgets.git/blame - src/msw/control.cpp
argh, last compilation fix had a typo, fixed now
[wxWidgets.git] / src / msw / control.cpp
CommitLineData
2bda0e17 1/////////////////////////////////////////////////////////////////////////////
1e6feb95 2// Name: msw/control.cpp
2bda0e17
KB
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
a23fd0e1 9// Licence: wxWindows licence
2bda0e17
KB
10/////////////////////////////////////////////////////////////////////////////
11
12#ifdef __GNUG__
1e6feb95 13 #pragma implementation "control.h"
2bda0e17
KB
14#endif
15
16// For compilers that support precompilation, includes "wx.h".
17#include "wx/wxprec.h"
18
19#ifdef __BORLANDC__
1e6feb95 20 #pragma hdrstop
2bda0e17
KB
21#endif
22
1e6feb95
VZ
23#if wxUSE_CONTROLS
24
2bda0e17 25#ifndef WX_PRECOMP
1e6feb95
VZ
26 #include "wx/event.h"
27 #include "wx/app.h"
28 #include "wx/dcclient.h"
29 #include "wx/log.h"
2bda0e17
KB
30#endif
31
2432b92d
JS
32#include "wx/control.h"
33
2bda0e17
KB
34#include "wx/msw/private.h"
35
ae090fdb 36#if defined(__WIN95__) && !((defined(__GNUWIN32_OLD__) || defined(__TWIN32__)) && !defined(__CYGWIN10__))
c42404a5 37 #include <commctrl.h>
2bda0e17
KB
38#endif
39
2bda0e17
KB
40IMPLEMENT_ABSTRACT_CLASS(wxControl, wxWindow)
41
42BEGIN_EVENT_TABLE(wxControl, wxWindow)
a23fd0e1 43 EVT_ERASE_BACKGROUND(wxControl::OnEraseBackground)
2bda0e17 44END_EVENT_TABLE()
2bda0e17
KB
45
46// Item members
42e69d6b 47wxControl::wxControl()
2bda0e17 48{
42e69d6b 49#if WXWIN_COMPATIBILITY
3f2711d5 50 m_callback = 0;
42e69d6b 51#endif // WXWIN_COMPATIBILITY
2bda0e17
KB
52}
53
42e69d6b 54wxControl::~wxControl()
2bda0e17 55{
42e69d6b 56 m_isBeingDeleted = TRUE;
2bda0e17
KB
57}
58
8d772832 59
5b2f31eb
VZ
60bool wxControl::Create(wxWindow *parent,
61 wxWindowID id,
8d772832 62 const wxPoint& pos,
5b2f31eb
VZ
63 const wxSize& size,
64 long style,
8d772832 65 const wxValidator& validator,
8d772832
RD
66 const wxString& name)
67{
5b2f31eb
VZ
68 if ( !wxWindow::Create(parent, id, pos, size, style, name) )
69 return FALSE;
70
8d772832 71#if wxUSE_VALIDATORS
5b2f31eb 72 SetValidator(validator);
8d772832 73#endif
5b2f31eb
VZ
74
75 return TRUE;
76}
77
78bool 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);
8d772832
RD
88}
89
222594ea
VZ
90bool wxControl::MSWCreateControl(const wxChar *classname,
91 WXDWORD style,
92 const wxPoint& pos,
93 const wxSize& size,
94 const wxString& label,
95 WXDWORD exstyle)
8d99be5f 96{
f6bcfd97
BP
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;
3f2711d5 102
222594ea
VZ
103 // if no extended style given, determine it ourselves
104 if ( exstyle == (WXDWORD)-1 )
105 {
5b2f31eb 106 exstyle = Determine3DEffects(WS_EX_CLIENTEDGE, &want3D);
222594ea
VZ
107 }
108
5b2f31eb
VZ
109 // all controls should have these styles (wxWindows creates all controls
110 // visible by default)
3f2711d5
VZ
111 style |= WS_CHILD | WS_VISIBLE;
112
a63cbfa3
VZ
113 int x = pos.x == -1 ? 0 : pos.x,
114 y = pos.y == -1 ? 0 : pos.y,
115 w = size.x == -1 ? 0 : size.x,
116 h = size.y == -1 ? 0 : size.y;
117
8d99be5f
VZ
118 m_hWnd = (WXHWND)::CreateWindowEx
119 (
222594ea 120 exstyle, // extended style
8d99be5f 121 classname, // the kind of control to create
222594ea 122 label, // the window name
8d99be5f 123 style, // the window style
a63cbfa3 124 x, y, w, h, // the window position and size
8d99be5f
VZ
125 GetHwndOf(GetParent()), // parent
126 (HMENU)GetId(), // child id
127 wxGetInstance(), // app instance
128 NULL // creation parameters
129 );
130
131 if ( !m_hWnd )
132 {
f6bcfd97
BP
133 wxLogDebug(wxT("Failed to create a control of class '%s'"), classname);
134 wxFAIL_MSG(_T("something is very wrong"));
8d99be5f
VZ
135
136 return FALSE;
137 }
138
3f2711d5
VZ
139#if wxUSE_CTL3D
140 if ( want3D )
141 {
142 Ctl3dSubclassCtl(GetHwnd());
143 m_useCtl3D = TRUE;
144 }
145#endif // wxUSE_CTL3D
146
b225f659 147 // install wxWindows window proc for this window
8d99be5f
VZ
148 SubclassWin(m_hWnd);
149
150 // controls use the same font and colours as their parent dialog by default
151 InheritAttributes();
152
a63cbfa3
VZ
153 // set the size now if no initial size specified
154 if ( w == 0 || h == 0 )
155 {
156 SetBestSize(size);
157 }
158
8d99be5f
VZ
159 return TRUE;
160}
161
f68586e5 162wxSize wxControl::DoGetBestSize() const
4438caf4
VZ
163{
164 return wxSize(DEFAULT_ITEM_WIDTH, DEFAULT_ITEM_HEIGHT);
165}
166
42e69d6b 167bool wxControl::ProcessCommand(wxCommandEvent& event)
2bda0e17 168{
42e69d6b
VZ
169#if WXWIN_COMPATIBILITY
170 if ( m_callback )
171 {
d8ecfb85 172 (void)(*m_callback)(*this, event);
2bda0e17 173
42e69d6b
VZ
174 return TRUE;
175 }
176 else
177#endif // WXWIN_COMPATIBILITY
2bda0e17 178
42e69d6b 179 return GetEventHandler()->ProcessEvent(event);
2bda0e17
KB
180}
181
a23fd0e1
VZ
182#ifdef __WIN95__
183bool wxControl::MSWOnNotify(int idCtrl,
184 WXLPARAM lParam,
185 WXLPARAM* result)
2bda0e17 186{
a23fd0e1 187 wxEventType eventType = wxEVT_NULL;
b6e5eaa5
VZ
188
189 NMHDR *hdr = (NMHDR*) lParam;
190 switch ( hdr->code )
a23fd0e1
VZ
191 {
192 case NM_CLICK:
193 eventType = wxEVT_COMMAND_LEFT_CLICK;
194 break;
2bda0e17 195
a23fd0e1
VZ
196 case NM_DBLCLK:
197 eventType = wxEVT_COMMAND_LEFT_DCLICK;
198 break;
2bda0e17 199
a23fd0e1
VZ
200 case NM_RCLICK:
201 eventType = wxEVT_COMMAND_RIGHT_CLICK;
202 break;
2bda0e17 203
a23fd0e1
VZ
204 case NM_RDBLCLK:
205 eventType = wxEVT_COMMAND_RIGHT_DCLICK;
206 break;
2bda0e17 207
a23fd0e1
VZ
208 case NM_SETFOCUS:
209 eventType = wxEVT_COMMAND_SET_FOCUS;
210 break;
debe6624 211
a23fd0e1
VZ
212 case NM_KILLFOCUS:
213 eventType = wxEVT_COMMAND_KILL_FOCUS;
214 break;
2bda0e17 215
a23fd0e1
VZ
216 case NM_RETURN:
217 eventType = wxEVT_COMMAND_ENTER;
218 break;
219
220 default:
221 return wxWindow::MSWOnNotify(idCtrl, lParam, result);
222 }
fd3f686c 223
b6e5eaa5 224 wxCommandEvent event(wxEVT_NULL, m_windowId);
2bda0e17 225 event.SetEventType(eventType);
a23fd0e1 226 event.SetEventObject(this);
2bda0e17 227
a23fd0e1 228 return GetEventHandler()->ProcessEvent(event);
2bda0e17 229}
a23fd0e1 230#endif // Win95
2bda0e17 231
2bda0e17
KB
232void wxControl::OnEraseBackground(wxEraseEvent& event)
233{
5bd3a2da
VZ
234 // notice that this 'dumb' implementation may cause flicker for some of the
235 // controls in which case they should intercept wxEraseEvent and process it
236 // themselves somehow
42e69d6b
VZ
237
238 RECT rect;
3f2711d5 239 ::GetClientRect(GetHwnd(), &rect);
42e69d6b 240
3f2711d5 241 HBRUSH hBrush = ::CreateSolidBrush(wxColourToRGB(GetBackgroundColour()));
42e69d6b 242
3f2711d5
VZ
243 HDC hdc = GetHdcOf((*event.GetDC()));
244 int mode = ::SetMapMode(hdc, MM_TEXT);
245
246 ::FillRect(hdc, &rect, hBrush);
42e69d6b 247 ::DeleteObject(hBrush);
3f2711d5 248 ::SetMapMode(hdc, mode);
2bda0e17
KB
249}
250
33ac7e6f 251WXHBRUSH wxControl::OnCtlColor(WXHDC pDC, WXHWND WXUNUSED(pWnd), WXUINT WXUNUSED(nCtlColor),
788722ac
JS
252#if wxUSE_CTL3D
253 WXUINT message,
254 WXWPARAM wParam,
255 WXLPARAM lParam
256#else
33ac7e6f
KB
257 WXUINT WXUNUSED(message),
258 WXWPARAM WXUNUSED(wParam),
788722ac
JS
259 WXLPARAM WXUNUSED(lParam)
260#endif
261 )
f048e32f
VZ
262{
263#if wxUSE_CTL3D
264 if ( m_useCtl3D )
265 {
266 HBRUSH hbrush = Ctl3dCtlColorEx(message, wParam, lParam);
267 return (WXHBRUSH) hbrush;
268 }
269#endif // wxUSE_CTL3D
270
271 HDC hdc = (HDC)pDC;
272 if (GetParent()->GetTransparentBackground())
273 SetBkMode(hdc, TRANSPARENT);
274 else
275 SetBkMode(hdc, OPAQUE);
276
f6bcfd97
BP
277 wxColour colBack = GetBackgroundColour();
278
f048e32f
VZ
279 ::SetBkColor(hdc, wxColourToRGB(colBack));
280 ::SetTextColor(hdc, wxColourToRGB(GetForegroundColour()));
281
282 wxBrush *brush = wxTheBrushList->FindOrCreateBrush(colBack, wxSOLID);
283
284 return (WXHBRUSH)brush->GetResourceHandle();
285}
286
5b2f31eb 287WXDWORD wxControl::MSWGetStyle(long style, WXDWORD *exstyle) const
8d99be5f 288{
5b2f31eb 289 long msStyle = wxWindow::MSWGetStyle(style, exstyle);
8d99be5f 290
5b2f31eb
VZ
291 if ( AcceptsFocus() )
292 {
293 msStyle |= WS_TABSTOP;
294 }
8d99be5f 295
5b2f31eb 296 return msStyle;
8d99be5f
VZ
297}
298
42e69d6b
VZ
299// ---------------------------------------------------------------------------
300// global functions
301// ---------------------------------------------------------------------------
2bda0e17 302
42e69d6b
VZ
303// Call this repeatedly for several wnds to find the overall size
304// of the widget.
305// Call it initially with -1 for all values in rect.
306// Keep calling for other widgets, and rect will be modified
307// to calculate largest bounding rectangle.
308void wxFindMaxSize(WXHWND wnd, RECT *rect)
2bda0e17 309{
42e69d6b
VZ
310 int left = rect->left;
311 int right = rect->right;
312 int top = rect->top;
313 int bottom = rect->bottom;
2bda0e17 314
42e69d6b 315 GetWindowRect((HWND) wnd, rect);
2bda0e17 316
42e69d6b
VZ
317 if (left < 0)
318 return;
2bda0e17 319
42e69d6b
VZ
320 if (left < rect->left)
321 rect->left = left;
2bda0e17 322
42e69d6b
VZ
323 if (right > rect->right)
324 rect->right = right;
2bda0e17 325
42e69d6b
VZ
326 if (top < rect->top)
327 rect->top = top;
2bda0e17 328
42e69d6b
VZ
329 if (bottom > rect->bottom)
330 rect->bottom = bottom;
2bda0e17
KB
331}
332
1e6feb95 333#endif // wxUSE_CONTROLS