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