]> git.saurik.com Git - wxWidgets.git/blame - src/msw/control.cpp
fixed incorrect calls to SelectObject(hdc, NULL)
[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
RD
59
60bool wxControl::Create(wxWindow *parent, wxWindowID id,
61 const wxPoint& pos,
62 const wxSize& size, long style,
8d772832 63 const wxValidator& validator,
8d772832
RD
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
222594ea
VZ
75bool wxControl::MSWCreateControl(const wxChar *classname,
76 WXDWORD style,
77 const wxPoint& pos,
78 const wxSize& size,
79 const wxString& label,
80 WXDWORD exstyle)
8d99be5f 81{
f6bcfd97
BP
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;
3f2711d5 87
222594ea
VZ
88 // if no extended style given, determine it ourselves
89 if ( exstyle == (WXDWORD)-1 )
90 {
91 exstyle = GetExStyle(style, &want3D);
92 }
93
f6bcfd97 94 // all controls have these styles (wxWindows creates all controls visible
3f2711d5
VZ
95 // by default)
96 style |= WS_CHILD | WS_VISIBLE;
97
a63cbfa3
VZ
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
8d99be5f
VZ
103 m_hWnd = (WXHWND)::CreateWindowEx
104 (
222594ea 105 exstyle, // extended style
8d99be5f 106 classname, // the kind of control to create
222594ea 107 label, // the window name
8d99be5f 108 style, // the window style
a63cbfa3 109 x, y, w, h, // the window position and size
8d99be5f
VZ
110 GetHwndOf(GetParent()), // parent
111 (HMENU)GetId(), // child id
112 wxGetInstance(), // app instance
113 NULL // creation parameters
114 );
115
116 if ( !m_hWnd )
117 {
f6bcfd97
BP
118 wxLogDebug(wxT("Failed to create a control of class '%s'"), classname);
119 wxFAIL_MSG(_T("something is very wrong"));
8d99be5f
VZ
120
121 return FALSE;
122 }
123
3f2711d5
VZ
124#if wxUSE_CTL3D
125 if ( want3D )
126 {
127 Ctl3dSubclassCtl(GetHwnd());
128 m_useCtl3D = TRUE;
129 }
130#endif // wxUSE_CTL3D
131
b225f659 132 // install wxWindows window proc for this window
8d99be5f
VZ
133 SubclassWin(m_hWnd);
134
135 // controls use the same font and colours as their parent dialog by default
136 InheritAttributes();
137
a63cbfa3
VZ
138 // set the size now if no initial size specified
139 if ( w == 0 || h == 0 )
140 {
141 SetBestSize(size);
142 }
143
8d99be5f
VZ
144 return TRUE;
145}
146
f68586e5 147wxSize wxControl::DoGetBestSize() const
4438caf4
VZ
148{
149 return wxSize(DEFAULT_ITEM_WIDTH, DEFAULT_ITEM_HEIGHT);
150}
151
42e69d6b 152bool wxControl::ProcessCommand(wxCommandEvent& event)
2bda0e17 153{
42e69d6b
VZ
154#if WXWIN_COMPATIBILITY
155 if ( m_callback )
156 {
d8ecfb85 157 (void)(*m_callback)(*this, event);
2bda0e17 158
42e69d6b
VZ
159 return TRUE;
160 }
161 else
162#endif // WXWIN_COMPATIBILITY
2bda0e17 163
42e69d6b 164 return GetEventHandler()->ProcessEvent(event);
2bda0e17
KB
165}
166
a23fd0e1
VZ
167#ifdef __WIN95__
168bool wxControl::MSWOnNotify(int idCtrl,
169 WXLPARAM lParam,
170 WXLPARAM* result)
2bda0e17 171{
a23fd0e1 172 wxEventType eventType = wxEVT_NULL;
b6e5eaa5
VZ
173
174 NMHDR *hdr = (NMHDR*) lParam;
175 switch ( hdr->code )
a23fd0e1
VZ
176 {
177 case NM_CLICK:
178 eventType = wxEVT_COMMAND_LEFT_CLICK;
179 break;
2bda0e17 180
a23fd0e1
VZ
181 case NM_DBLCLK:
182 eventType = wxEVT_COMMAND_LEFT_DCLICK;
183 break;
2bda0e17 184
a23fd0e1
VZ
185 case NM_RCLICK:
186 eventType = wxEVT_COMMAND_RIGHT_CLICK;
187 break;
2bda0e17 188
a23fd0e1
VZ
189 case NM_RDBLCLK:
190 eventType = wxEVT_COMMAND_RIGHT_DCLICK;
191 break;
2bda0e17 192
a23fd0e1
VZ
193 case NM_SETFOCUS:
194 eventType = wxEVT_COMMAND_SET_FOCUS;
195 break;
debe6624 196
a23fd0e1
VZ
197 case NM_KILLFOCUS:
198 eventType = wxEVT_COMMAND_KILL_FOCUS;
199 break;
2bda0e17 200
a23fd0e1
VZ
201 case NM_RETURN:
202 eventType = wxEVT_COMMAND_ENTER;
203 break;
204
205 default:
206 return wxWindow::MSWOnNotify(idCtrl, lParam, result);
207 }
fd3f686c 208
b6e5eaa5 209 wxCommandEvent event(wxEVT_NULL, m_windowId);
2bda0e17 210 event.SetEventType(eventType);
a23fd0e1 211 event.SetEventObject(this);
2bda0e17 212
a23fd0e1 213 return GetEventHandler()->ProcessEvent(event);
2bda0e17 214}
a23fd0e1 215#endif // Win95
2bda0e17 216
2bda0e17
KB
217void wxControl::OnEraseBackground(wxEraseEvent& event)
218{
5bd3a2da
VZ
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
42e69d6b
VZ
222
223 RECT rect;
3f2711d5 224 ::GetClientRect(GetHwnd(), &rect);
42e69d6b 225
3f2711d5 226 HBRUSH hBrush = ::CreateSolidBrush(wxColourToRGB(GetBackgroundColour()));
42e69d6b 227
3f2711d5
VZ
228 HDC hdc = GetHdcOf((*event.GetDC()));
229 int mode = ::SetMapMode(hdc, MM_TEXT);
230
231 ::FillRect(hdc, &rect, hBrush);
42e69d6b 232 ::DeleteObject(hBrush);
3f2711d5 233 ::SetMapMode(hdc, mode);
2bda0e17
KB
234}
235
33ac7e6f 236WXHBRUSH wxControl::OnCtlColor(WXHDC pDC, WXHWND WXUNUSED(pWnd), WXUINT WXUNUSED(nCtlColor),
788722ac
JS
237#if wxUSE_CTL3D
238 WXUINT message,
239 WXWPARAM wParam,
240 WXLPARAM lParam
241#else
33ac7e6f
KB
242 WXUINT WXUNUSED(message),
243 WXWPARAM WXUNUSED(wParam),
788722ac
JS
244 WXLPARAM WXUNUSED(lParam)
245#endif
246 )
f048e32f
VZ
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
f6bcfd97
BP
262 wxColour colBack = GetBackgroundColour();
263
f048e32f
VZ
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
3f2711d5 272WXDWORD wxControl::GetExStyle(WXDWORD& style, bool *want3D) const
8d99be5f 273{
3f2711d5 274 WXDWORD exStyle = Determine3DEffects(WS_EX_CLIENTEDGE, want3D);
8d99be5f 275
3f2711d5
VZ
276 // Even with extended styles, need to combine with WS_BORDER for them to
277 // look right.
278 if ( *want3D || wxStyleHasBorder(m_windowStyle) )
8d99be5f
VZ
279 style |= WS_BORDER;
280
281 return exStyle;
282}
283
42e69d6b
VZ
284// ---------------------------------------------------------------------------
285// global functions
286// ---------------------------------------------------------------------------
2bda0e17 287
42e69d6b
VZ
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.
293void wxFindMaxSize(WXHWND wnd, RECT *rect)
2bda0e17 294{
42e69d6b
VZ
295 int left = rect->left;
296 int right = rect->right;
297 int top = rect->top;
298 int bottom = rect->bottom;
2bda0e17 299
42e69d6b 300 GetWindowRect((HWND) wnd, rect);
2bda0e17 301
42e69d6b
VZ
302 if (left < 0)
303 return;
2bda0e17 304
42e69d6b
VZ
305 if (left < rect->left)
306 rect->left = left;
2bda0e17 307
42e69d6b
VZ
308 if (right > rect->right)
309 rect->right = right;
2bda0e17 310
42e69d6b
VZ
311 if (top < rect->top)
312 rect->top = top;
2bda0e17 313
42e69d6b
VZ
314 if (bottom > rect->bottom)
315 rect->bottom = bottom;
2bda0e17
KB
316}
317
1e6feb95 318#endif // wxUSE_CONTROLS