]> git.saurik.com Git - wxWidgets.git/blame - src/msw/control.cpp
*** empty log message ***
[wxWidgets.git] / src / msw / control.cpp
CommitLineData
2bda0e17
KB
1/////////////////////////////////////////////////////////////////////////////
2// Name: control.cpp
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__
13#pragma implementation "control.h"
14#endif
15
16// For compilers that support precompilation, includes "wx.h".
17#include "wx/wxprec.h"
18
19#ifdef __BORLANDC__
20#pragma hdrstop
21#endif
22
23#ifndef WX_PRECOMP
2432b92d 24#include "wx/event.h"
2bda0e17
KB
25#include "wx/app.h"
26#include "wx/dcclient.h"
27#endif
28
2432b92d
JS
29#include "wx/control.h"
30
2bda0e17
KB
31#include "wx/msw/private.h"
32
c42404a5
VZ
33#if defined(__WIN95__) && !(defined(__GNUWIN32_OLD__) || defined(__TWIN32__))
34 #include <commctrl.h>
2bda0e17
KB
35#endif
36
2bda0e17
KB
37IMPLEMENT_ABSTRACT_CLASS(wxControl, wxWindow)
38
39BEGIN_EVENT_TABLE(wxControl, wxWindow)
a23fd0e1 40 EVT_ERASE_BACKGROUND(wxControl::OnEraseBackground)
2bda0e17 41END_EVENT_TABLE()
2bda0e17
KB
42
43// Item members
42e69d6b 44wxControl::wxControl()
2bda0e17 45{
3f2711d5
VZ
46 m_backgroundColour = *wxWHITE;
47 m_foregroundColour = *wxBLACK;
42e69d6b
VZ
48
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{
3f2711d5
VZ
82 // VZ: if someone could put a comment here explaining what exactly this is
83 // needed for, it would be nice...
84 bool want3D;
85
222594ea
VZ
86 // if no extended style given, determine it ourselves
87 if ( exstyle == (WXDWORD)-1 )
88 {
89 exstyle = GetExStyle(style, &want3D);
90 }
91
3f2711d5
VZ
92 // all controls have these childs (wxWindows creates all controls visible
93 // by default)
94 style |= WS_CHILD | WS_VISIBLE;
95
8d99be5f
VZ
96 m_hWnd = (WXHWND)::CreateWindowEx
97 (
222594ea 98 exstyle, // extended style
8d99be5f 99 classname, // the kind of control to create
222594ea 100 label, // the window name
8d99be5f 101 style, // the window style
222594ea
VZ
102 pos.x, pos.y, // the window position
103 size.x, size.y, // and size
8d99be5f
VZ
104 GetHwndOf(GetParent()), // parent
105 (HMENU)GetId(), // child id
106 wxGetInstance(), // app instance
107 NULL // creation parameters
108 );
109
110 if ( !m_hWnd )
111 {
112#ifdef __WXDEBUG__
223d09f6 113 wxLogError(wxT("Failed to create a control of class '%s'"), classname);
8d99be5f
VZ
114#endif // DEBUG
115
116 return FALSE;
117 }
118
3f2711d5
VZ
119#if wxUSE_CTL3D
120 if ( want3D )
121 {
122 Ctl3dSubclassCtl(GetHwnd());
123 m_useCtl3D = TRUE;
124 }
125#endif // wxUSE_CTL3D
126
8d99be5f
VZ
127 // subclass again for purposes of dialog editing mode
128 SubclassWin(m_hWnd);
129
130 // controls use the same font and colours as their parent dialog by default
131 InheritAttributes();
132
133 return TRUE;
134}
135
f68586e5 136wxSize wxControl::DoGetBestSize() const
4438caf4
VZ
137{
138 return wxSize(DEFAULT_ITEM_WIDTH, DEFAULT_ITEM_HEIGHT);
139}
140
42e69d6b 141bool wxControl::ProcessCommand(wxCommandEvent& event)
2bda0e17 142{
42e69d6b
VZ
143#if WXWIN_COMPATIBILITY
144 if ( m_callback )
145 {
146 (void)(*m_callback)(this, event);
2bda0e17 147
42e69d6b
VZ
148 return TRUE;
149 }
150 else
151#endif // WXWIN_COMPATIBILITY
2bda0e17 152
42e69d6b 153 return GetEventHandler()->ProcessEvent(event);
2bda0e17
KB
154}
155
a23fd0e1
VZ
156#ifdef __WIN95__
157bool wxControl::MSWOnNotify(int idCtrl,
158 WXLPARAM lParam,
159 WXLPARAM* result)
2bda0e17 160{
a23fd0e1
VZ
161 wxCommandEvent event(wxEVT_NULL, m_windowId);
162 wxEventType eventType = wxEVT_NULL;
163 NMHDR *hdr1 = (NMHDR*) lParam;
164 switch ( hdr1->code )
165 {
166 case NM_CLICK:
167 eventType = wxEVT_COMMAND_LEFT_CLICK;
168 break;
2bda0e17 169
a23fd0e1
VZ
170 case NM_DBLCLK:
171 eventType = wxEVT_COMMAND_LEFT_DCLICK;
172 break;
2bda0e17 173
a23fd0e1
VZ
174 case NM_RCLICK:
175 eventType = wxEVT_COMMAND_RIGHT_CLICK;
176 break;
2bda0e17 177
a23fd0e1
VZ
178 case NM_RDBLCLK:
179 eventType = wxEVT_COMMAND_RIGHT_DCLICK;
180 break;
2bda0e17 181
a23fd0e1
VZ
182 case NM_SETFOCUS:
183 eventType = wxEVT_COMMAND_SET_FOCUS;
184 break;
debe6624 185
a23fd0e1
VZ
186 case NM_KILLFOCUS:
187 eventType = wxEVT_COMMAND_KILL_FOCUS;
188 break;
2bda0e17 189
a23fd0e1
VZ
190 case NM_RETURN:
191 eventType = wxEVT_COMMAND_ENTER;
192 break;
193
194 default:
195 return wxWindow::MSWOnNotify(idCtrl, lParam, result);
196 }
fd3f686c 197
2bda0e17 198 event.SetEventType(eventType);
a23fd0e1 199 event.SetEventObject(this);
2bda0e17 200
a23fd0e1 201 return GetEventHandler()->ProcessEvent(event);
2bda0e17 202}
a23fd0e1 203#endif // Win95
2bda0e17 204
2bda0e17
KB
205void wxControl::OnEraseBackground(wxEraseEvent& event)
206{
5bd3a2da
VZ
207 // notice that this 'dumb' implementation may cause flicker for some of the
208 // controls in which case they should intercept wxEraseEvent and process it
209 // themselves somehow
42e69d6b
VZ
210
211 RECT rect;
3f2711d5 212 ::GetClientRect(GetHwnd(), &rect);
42e69d6b 213
3f2711d5 214 HBRUSH hBrush = ::CreateSolidBrush(wxColourToRGB(GetBackgroundColour()));
42e69d6b 215
3f2711d5
VZ
216 HDC hdc = GetHdcOf((*event.GetDC()));
217 int mode = ::SetMapMode(hdc, MM_TEXT);
218
219 ::FillRect(hdc, &rect, hBrush);
42e69d6b 220 ::DeleteObject(hBrush);
3f2711d5 221 ::SetMapMode(hdc, mode);
2bda0e17
KB
222}
223
f048e32f
VZ
224WXHBRUSH wxControl::OnCtlColor(WXHDC pDC, WXHWND pWnd, WXUINT nCtlColor,
225 WXUINT message,
226 WXWPARAM wParam,
227 WXLPARAM lParam)
228{
229#if wxUSE_CTL3D
230 if ( m_useCtl3D )
231 {
232 HBRUSH hbrush = Ctl3dCtlColorEx(message, wParam, lParam);
233 return (WXHBRUSH) hbrush;
234 }
235#endif // wxUSE_CTL3D
236
237 HDC hdc = (HDC)pDC;
238 if (GetParent()->GetTransparentBackground())
239 SetBkMode(hdc, TRANSPARENT);
240 else
241 SetBkMode(hdc, OPAQUE);
242
243 const wxColour& colBack = GetBackgroundColour();
244 ::SetBkColor(hdc, wxColourToRGB(colBack));
245 ::SetTextColor(hdc, wxColourToRGB(GetForegroundColour()));
246
247 wxBrush *brush = wxTheBrushList->FindOrCreateBrush(colBack, wxSOLID);
248
249 return (WXHBRUSH)brush->GetResourceHandle();
250}
251
3f2711d5 252WXDWORD wxControl::GetExStyle(WXDWORD& style, bool *want3D) const
8d99be5f 253{
3f2711d5 254 WXDWORD exStyle = Determine3DEffects(WS_EX_CLIENTEDGE, want3D);
8d99be5f 255
3f2711d5
VZ
256 // Even with extended styles, need to combine with WS_BORDER for them to
257 // look right.
258 if ( *want3D || wxStyleHasBorder(m_windowStyle) )
8d99be5f
VZ
259 style |= WS_BORDER;
260
261 return exStyle;
262}
263
42e69d6b
VZ
264// ---------------------------------------------------------------------------
265// global functions
266// ---------------------------------------------------------------------------
2bda0e17 267
42e69d6b
VZ
268// Call this repeatedly for several wnds to find the overall size
269// of the widget.
270// Call it initially with -1 for all values in rect.
271// Keep calling for other widgets, and rect will be modified
272// to calculate largest bounding rectangle.
273void wxFindMaxSize(WXHWND wnd, RECT *rect)
2bda0e17 274{
42e69d6b
VZ
275 int left = rect->left;
276 int right = rect->right;
277 int top = rect->top;
278 int bottom = rect->bottom;
2bda0e17 279
42e69d6b 280 GetWindowRect((HWND) wnd, rect);
2bda0e17 281
42e69d6b
VZ
282 if (left < 0)
283 return;
2bda0e17 284
42e69d6b
VZ
285 if (left < rect->left)
286 rect->left = left;
2bda0e17 287
42e69d6b
VZ
288 if (right > rect->right)
289 rect->right = right;
2bda0e17 290
42e69d6b
VZ
291 if (top < rect->top)
292 rect->top = top;
2bda0e17 293
42e69d6b
VZ
294 if (bottom > rect->bottom)
295 rect->bottom = bottom;
2bda0e17
KB
296}
297