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