]> git.saurik.com Git - wxWidgets.git/blame - src/msw/control.cpp
fixed wxUniv/GTK linking by getting rid of GENERICOBJS and putting them into GUIOBJS...
[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
a63cbfa3
VZ
101 int x = pos.x == -1 ? 0 : pos.x,
102 y = pos.y == -1 ? 0 : pos.y,
103 w = size.x == -1 ? 0 : size.x,
104 h = size.y == -1 ? 0 : size.y;
105
8d99be5f
VZ
106 m_hWnd = (WXHWND)::CreateWindowEx
107 (
222594ea 108 exstyle, // extended style
8d99be5f 109 classname, // the kind of control to create
222594ea 110 label, // the window name
8d99be5f 111 style, // the window style
a63cbfa3 112 x, y, w, h, // the window position and size
8d99be5f
VZ
113 GetHwndOf(GetParent()), // parent
114 (HMENU)GetId(), // child id
115 wxGetInstance(), // app instance
116 NULL // creation parameters
117 );
118
119 if ( !m_hWnd )
120 {
f6bcfd97
BP
121 wxLogDebug(wxT("Failed to create a control of class '%s'"), classname);
122 wxFAIL_MSG(_T("something is very wrong"));
8d99be5f
VZ
123
124 return FALSE;
125 }
126
3f2711d5
VZ
127#if wxUSE_CTL3D
128 if ( want3D )
129 {
130 Ctl3dSubclassCtl(GetHwnd());
131 m_useCtl3D = TRUE;
132 }
133#endif // wxUSE_CTL3D
134
b225f659 135 // install wxWindows window proc for this window
8d99be5f
VZ
136 SubclassWin(m_hWnd);
137
138 // controls use the same font and colours as their parent dialog by default
139 InheritAttributes();
140
a63cbfa3
VZ
141 // set the size now if no initial size specified
142 if ( w == 0 || h == 0 )
143 {
144 SetBestSize(size);
145 }
146
8d99be5f
VZ
147 return TRUE;
148}
149
f68586e5 150wxSize wxControl::DoGetBestSize() const
4438caf4
VZ
151{
152 return wxSize(DEFAULT_ITEM_WIDTH, DEFAULT_ITEM_HEIGHT);
153}
154
42e69d6b 155bool wxControl::ProcessCommand(wxCommandEvent& event)
2bda0e17 156{
42e69d6b
VZ
157#if WXWIN_COMPATIBILITY
158 if ( m_callback )
159 {
d8ecfb85 160 (void)(*m_callback)(*this, event);
2bda0e17 161
42e69d6b
VZ
162 return TRUE;
163 }
164 else
165#endif // WXWIN_COMPATIBILITY
2bda0e17 166
42e69d6b 167 return GetEventHandler()->ProcessEvent(event);
2bda0e17
KB
168}
169
a23fd0e1
VZ
170#ifdef __WIN95__
171bool wxControl::MSWOnNotify(int idCtrl,
172 WXLPARAM lParam,
173 WXLPARAM* result)
2bda0e17 174{
a23fd0e1 175 wxEventType eventType = wxEVT_NULL;
b6e5eaa5
VZ
176
177 NMHDR *hdr = (NMHDR*) lParam;
178 switch ( hdr->code )
a23fd0e1
VZ
179 {
180 case NM_CLICK:
181 eventType = wxEVT_COMMAND_LEFT_CLICK;
182 break;
2bda0e17 183
a23fd0e1
VZ
184 case NM_DBLCLK:
185 eventType = wxEVT_COMMAND_LEFT_DCLICK;
186 break;
2bda0e17 187
a23fd0e1
VZ
188 case NM_RCLICK:
189 eventType = wxEVT_COMMAND_RIGHT_CLICK;
190 break;
2bda0e17 191
a23fd0e1
VZ
192 case NM_RDBLCLK:
193 eventType = wxEVT_COMMAND_RIGHT_DCLICK;
194 break;
2bda0e17 195
a23fd0e1
VZ
196 case NM_SETFOCUS:
197 eventType = wxEVT_COMMAND_SET_FOCUS;
198 break;
debe6624 199
a23fd0e1
VZ
200 case NM_KILLFOCUS:
201 eventType = wxEVT_COMMAND_KILL_FOCUS;
202 break;
2bda0e17 203
a23fd0e1
VZ
204 case NM_RETURN:
205 eventType = wxEVT_COMMAND_ENTER;
206 break;
207
208 default:
209 return wxWindow::MSWOnNotify(idCtrl, lParam, result);
210 }
fd3f686c 211
b6e5eaa5 212 wxCommandEvent event(wxEVT_NULL, m_windowId);
2bda0e17 213 event.SetEventType(eventType);
a23fd0e1 214 event.SetEventObject(this);
2bda0e17 215
a23fd0e1 216 return GetEventHandler()->ProcessEvent(event);
2bda0e17 217}
a23fd0e1 218#endif // Win95
2bda0e17 219
2bda0e17
KB
220void wxControl::OnEraseBackground(wxEraseEvent& event)
221{
5bd3a2da
VZ
222 // notice that this 'dumb' implementation may cause flicker for some of the
223 // controls in which case they should intercept wxEraseEvent and process it
224 // themselves somehow
42e69d6b
VZ
225
226 RECT rect;
3f2711d5 227 ::GetClientRect(GetHwnd(), &rect);
42e69d6b 228
3f2711d5 229 HBRUSH hBrush = ::CreateSolidBrush(wxColourToRGB(GetBackgroundColour()));
42e69d6b 230
3f2711d5
VZ
231 HDC hdc = GetHdcOf((*event.GetDC()));
232 int mode = ::SetMapMode(hdc, MM_TEXT);
233
234 ::FillRect(hdc, &rect, hBrush);
42e69d6b 235 ::DeleteObject(hBrush);
3f2711d5 236 ::SetMapMode(hdc, mode);
2bda0e17
KB
237}
238
33ac7e6f 239WXHBRUSH wxControl::OnCtlColor(WXHDC pDC, WXHWND WXUNUSED(pWnd), WXUINT WXUNUSED(nCtlColor),
788722ac
JS
240#if wxUSE_CTL3D
241 WXUINT message,
242 WXWPARAM wParam,
243 WXLPARAM lParam
244#else
33ac7e6f
KB
245 WXUINT WXUNUSED(message),
246 WXWPARAM WXUNUSED(wParam),
788722ac
JS
247 WXLPARAM WXUNUSED(lParam)
248#endif
249 )
f048e32f
VZ
250{
251#if wxUSE_CTL3D
252 if ( m_useCtl3D )
253 {
254 HBRUSH hbrush = Ctl3dCtlColorEx(message, wParam, lParam);
255 return (WXHBRUSH) hbrush;
256 }
257#endif // wxUSE_CTL3D
258
259 HDC hdc = (HDC)pDC;
260 if (GetParent()->GetTransparentBackground())
261 SetBkMode(hdc, TRANSPARENT);
262 else
263 SetBkMode(hdc, OPAQUE);
264
f6bcfd97
BP
265 wxColour colBack = GetBackgroundColour();
266
f048e32f
VZ
267 ::SetBkColor(hdc, wxColourToRGB(colBack));
268 ::SetTextColor(hdc, wxColourToRGB(GetForegroundColour()));
269
270 wxBrush *brush = wxTheBrushList->FindOrCreateBrush(colBack, wxSOLID);
271
272 return (WXHBRUSH)brush->GetResourceHandle();
273}
274
3f2711d5 275WXDWORD wxControl::GetExStyle(WXDWORD& style, bool *want3D) const
8d99be5f 276{
3f2711d5 277 WXDWORD exStyle = Determine3DEffects(WS_EX_CLIENTEDGE, want3D);
8d99be5f 278
3f2711d5
VZ
279 // Even with extended styles, need to combine with WS_BORDER for them to
280 // look right.
281 if ( *want3D || wxStyleHasBorder(m_windowStyle) )
8d99be5f
VZ
282 style |= WS_BORDER;
283
284 return exStyle;
285}
286
42e69d6b
VZ
287// ---------------------------------------------------------------------------
288// global functions
289// ---------------------------------------------------------------------------
2bda0e17 290
42e69d6b
VZ
291// Call this repeatedly for several wnds to find the overall size
292// of the widget.
293// Call it initially with -1 for all values in rect.
294// Keep calling for other widgets, and rect will be modified
295// to calculate largest bounding rectangle.
296void wxFindMaxSize(WXHWND wnd, RECT *rect)
2bda0e17 297{
42e69d6b
VZ
298 int left = rect->left;
299 int right = rect->right;
300 int top = rect->top;
301 int bottom = rect->bottom;
2bda0e17 302
42e69d6b 303 GetWindowRect((HWND) wnd, rect);
2bda0e17 304
42e69d6b
VZ
305 if (left < 0)
306 return;
2bda0e17 307
42e69d6b
VZ
308 if (left < rect->left)
309 rect->left = left;
2bda0e17 310
42e69d6b
VZ
311 if (right > rect->right)
312 rect->right = right;
2bda0e17 313
42e69d6b
VZ
314 if (top < rect->top)
315 rect->top = top;
2bda0e17 316
42e69d6b
VZ
317 if (bottom > rect->bottom)
318 rect->bottom = bottom;
2bda0e17
KB
319}
320
1e6feb95 321#endif // wxUSE_CONTROLS