]> git.saurik.com Git - wxWidgets.git/blame - src/msw/control.cpp
generic/wizard.h added
[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
65fd5cb0 33#if (defined(__WIN95__) && !defined(__GNUWIN32__)) || defined(__TWIN32__) || defined(wxUSE_NORLANDER_HEADERS)
2bda0e17
KB
34#include <commctrl.h>
35#endif
36
2bda0e17
KB
37#if !USE_SHARED_LIBRARY
38IMPLEMENT_ABSTRACT_CLASS(wxControl, wxWindow)
39
40BEGIN_EVENT_TABLE(wxControl, wxWindow)
a23fd0e1 41 EVT_ERASE_BACKGROUND(wxControl::OnEraseBackground)
2bda0e17
KB
42END_EVENT_TABLE()
43#endif
44
45// Item members
42e69d6b 46wxControl::wxControl()
2bda0e17 47{
3f2711d5
VZ
48 m_backgroundColour = *wxWHITE;
49 m_foregroundColour = *wxBLACK;
42e69d6b
VZ
50
51#if WXWIN_COMPATIBILITY
3f2711d5 52 m_callback = 0;
42e69d6b 53#endif // WXWIN_COMPATIBILITY
2bda0e17
KB
54}
55
42e69d6b 56wxControl::~wxControl()
2bda0e17 57{
42e69d6b 58 m_isBeingDeleted = TRUE;
2bda0e17
KB
59}
60
222594ea
VZ
61bool wxControl::MSWCreateControl(const wxChar *classname,
62 WXDWORD style,
63 const wxPoint& pos,
64 const wxSize& size,
65 const wxString& label,
66 WXDWORD exstyle)
8d99be5f 67{
3f2711d5
VZ
68 // VZ: if someone could put a comment here explaining what exactly this is
69 // needed for, it would be nice...
70 bool want3D;
71
222594ea
VZ
72 // if no extended style given, determine it ourselves
73 if ( exstyle == (WXDWORD)-1 )
74 {
75 exstyle = GetExStyle(style, &want3D);
76 }
77
3f2711d5
VZ
78 // all controls have these childs (wxWindows creates all controls visible
79 // by default)
80 style |= WS_CHILD | WS_VISIBLE;
81
8d99be5f
VZ
82 m_hWnd = (WXHWND)::CreateWindowEx
83 (
222594ea 84 exstyle, // extended style
8d99be5f 85 classname, // the kind of control to create
222594ea 86 label, // the window name
8d99be5f 87 style, // the window style
222594ea
VZ
88 pos.x, pos.y, // the window position
89 size.x, size.y, // and size
8d99be5f
VZ
90 GetHwndOf(GetParent()), // parent
91 (HMENU)GetId(), // child id
92 wxGetInstance(), // app instance
93 NULL // creation parameters
94 );
95
96 if ( !m_hWnd )
97 {
98#ifdef __WXDEBUG__
223d09f6 99 wxLogError(wxT("Failed to create a control of class '%s'"), classname);
8d99be5f
VZ
100#endif // DEBUG
101
102 return FALSE;
103 }
104
3f2711d5
VZ
105#if wxUSE_CTL3D
106 if ( want3D )
107 {
108 Ctl3dSubclassCtl(GetHwnd());
109 m_useCtl3D = TRUE;
110 }
111#endif // wxUSE_CTL3D
112
8d99be5f
VZ
113 // subclass again for purposes of dialog editing mode
114 SubclassWin(m_hWnd);
115
116 // controls use the same font and colours as their parent dialog by default
117 InheritAttributes();
118
119 return TRUE;
120}
121
f68586e5 122wxSize wxControl::DoGetBestSize() const
4438caf4
VZ
123{
124 return wxSize(DEFAULT_ITEM_WIDTH, DEFAULT_ITEM_HEIGHT);
125}
126
42e69d6b 127bool wxControl::ProcessCommand(wxCommandEvent& event)
2bda0e17 128{
42e69d6b
VZ
129#if WXWIN_COMPATIBILITY
130 if ( m_callback )
131 {
132 (void)(*m_callback)(this, event);
2bda0e17 133
42e69d6b
VZ
134 return TRUE;
135 }
136 else
137#endif // WXWIN_COMPATIBILITY
2bda0e17 138
42e69d6b 139 return GetEventHandler()->ProcessEvent(event);
2bda0e17
KB
140}
141
a23fd0e1
VZ
142#ifdef __WIN95__
143bool wxControl::MSWOnNotify(int idCtrl,
144 WXLPARAM lParam,
145 WXLPARAM* result)
2bda0e17 146{
a23fd0e1
VZ
147 wxCommandEvent event(wxEVT_NULL, m_windowId);
148 wxEventType eventType = wxEVT_NULL;
149 NMHDR *hdr1 = (NMHDR*) lParam;
150 switch ( hdr1->code )
151 {
152 case NM_CLICK:
153 eventType = wxEVT_COMMAND_LEFT_CLICK;
154 break;
2bda0e17 155
a23fd0e1
VZ
156 case NM_DBLCLK:
157 eventType = wxEVT_COMMAND_LEFT_DCLICK;
158 break;
2bda0e17 159
a23fd0e1
VZ
160 case NM_RCLICK:
161 eventType = wxEVT_COMMAND_RIGHT_CLICK;
162 break;
2bda0e17 163
a23fd0e1
VZ
164 case NM_RDBLCLK:
165 eventType = wxEVT_COMMAND_RIGHT_DCLICK;
166 break;
2bda0e17 167
a23fd0e1
VZ
168 case NM_SETFOCUS:
169 eventType = wxEVT_COMMAND_SET_FOCUS;
170 break;
debe6624 171
a23fd0e1
VZ
172 case NM_KILLFOCUS:
173 eventType = wxEVT_COMMAND_KILL_FOCUS;
174 break;
2bda0e17 175
a23fd0e1
VZ
176 case NM_RETURN:
177 eventType = wxEVT_COMMAND_ENTER;
178 break;
179
180 default:
181 return wxWindow::MSWOnNotify(idCtrl, lParam, result);
182 }
fd3f686c 183
2bda0e17 184 event.SetEventType(eventType);
a23fd0e1 185 event.SetEventObject(this);
2bda0e17 186
a23fd0e1 187 return GetEventHandler()->ProcessEvent(event);
2bda0e17 188}
a23fd0e1 189#endif // Win95
2bda0e17 190
2bda0e17
KB
191void wxControl::OnEraseBackground(wxEraseEvent& event)
192{
42e69d6b
VZ
193 // In general, you don't want to erase the background of a control,
194 // or you'll get a flicker.
195 // TODO: move this 'null' function into each control that
196 // might flicker.
197
198 RECT rect;
3f2711d5 199 ::GetClientRect(GetHwnd(), &rect);
42e69d6b 200
3f2711d5 201 HBRUSH hBrush = ::CreateSolidBrush(wxColourToRGB(GetBackgroundColour()));
42e69d6b 202
3f2711d5
VZ
203 HDC hdc = GetHdcOf((*event.GetDC()));
204 int mode = ::SetMapMode(hdc, MM_TEXT);
205
206 ::FillRect(hdc, &rect, hBrush);
42e69d6b 207 ::DeleteObject(hBrush);
3f2711d5 208 ::SetMapMode(hdc, mode);
2bda0e17
KB
209}
210
f048e32f
VZ
211WXHBRUSH wxControl::OnCtlColor(WXHDC pDC, WXHWND pWnd, WXUINT nCtlColor,
212 WXUINT message,
213 WXWPARAM wParam,
214 WXLPARAM lParam)
215{
216#if wxUSE_CTL3D
217 if ( m_useCtl3D )
218 {
219 HBRUSH hbrush = Ctl3dCtlColorEx(message, wParam, lParam);
220 return (WXHBRUSH) hbrush;
221 }
222#endif // wxUSE_CTL3D
223
224 HDC hdc = (HDC)pDC;
225 if (GetParent()->GetTransparentBackground())
226 SetBkMode(hdc, TRANSPARENT);
227 else
228 SetBkMode(hdc, OPAQUE);
229
230 const wxColour& colBack = GetBackgroundColour();
231 ::SetBkColor(hdc, wxColourToRGB(colBack));
232 ::SetTextColor(hdc, wxColourToRGB(GetForegroundColour()));
233
234 wxBrush *brush = wxTheBrushList->FindOrCreateBrush(colBack, wxSOLID);
235
236 return (WXHBRUSH)brush->GetResourceHandle();
237}
238
3f2711d5 239WXDWORD wxControl::GetExStyle(WXDWORD& style, bool *want3D) const
8d99be5f 240{
3f2711d5 241 WXDWORD exStyle = Determine3DEffects(WS_EX_CLIENTEDGE, want3D);
8d99be5f 242
3f2711d5
VZ
243 // Even with extended styles, need to combine with WS_BORDER for them to
244 // look right.
245 if ( *want3D || wxStyleHasBorder(m_windowStyle) )
8d99be5f
VZ
246 style |= WS_BORDER;
247
248 return exStyle;
249}
250
42e69d6b
VZ
251// ---------------------------------------------------------------------------
252// global functions
253// ---------------------------------------------------------------------------
2bda0e17 254
42e69d6b
VZ
255// Call this repeatedly for several wnds to find the overall size
256// of the widget.
257// Call it initially with -1 for all values in rect.
258// Keep calling for other widgets, and rect will be modified
259// to calculate largest bounding rectangle.
260void wxFindMaxSize(WXHWND wnd, RECT *rect)
2bda0e17 261{
42e69d6b
VZ
262 int left = rect->left;
263 int right = rect->right;
264 int top = rect->top;
265 int bottom = rect->bottom;
2bda0e17 266
42e69d6b 267 GetWindowRect((HWND) wnd, rect);
2bda0e17 268
42e69d6b
VZ
269 if (left < 0)
270 return;
2bda0e17 271
42e69d6b
VZ
272 if (left < rect->left)
273 rect->left = left;
2bda0e17 274
42e69d6b
VZ
275 if (right > rect->right)
276 rect->right = right;
2bda0e17 277
42e69d6b
VZ
278 if (top < rect->top)
279 rect->top = top;
2bda0e17 280
42e69d6b
VZ
281 if (bottom > rect->bottom)
282 rect->bottom = bottom;
2bda0e17
KB
283}
284