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