]> git.saurik.com Git - wxWidgets.git/blob - src/msw/control.cpp
Upported wxListBox key handling and SetFirstItem()
[wxWidgets.git] / src / msw / control.cpp
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
9 // Licence: wxWindows licence
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
24 #include "wx/event.h"
25 #include "wx/app.h"
26 #include "wx/dcclient.h"
27 #endif
28
29 #include "wx/control.h"
30
31 #include "wx/msw/private.h"
32
33 #if defined(__WIN95__) && !(defined(__GNUWIN32_OLD__) || defined(__TWIN32__))
34 #include <commctrl.h>
35 #endif
36
37 IMPLEMENT_ABSTRACT_CLASS(wxControl, wxWindow)
38
39 BEGIN_EVENT_TABLE(wxControl, wxWindow)
40 EVT_ERASE_BACKGROUND(wxControl::OnEraseBackground)
41 END_EVENT_TABLE()
42
43 // Item members
44 wxControl::wxControl()
45 {
46 m_backgroundColour = *wxWHITE;
47 m_foregroundColour = *wxBLACK;
48
49 #if WXWIN_COMPATIBILITY
50 m_callback = 0;
51 #endif // WXWIN_COMPATIBILITY
52 }
53
54 wxControl::~wxControl()
55 {
56 m_isBeingDeleted = TRUE;
57 }
58
59
60 bool wxControl::Create(wxWindow *parent, wxWindowID id,
61 const wxPoint& pos,
62 const wxSize& size, long style,
63 const wxValidator& validator,
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
75 bool wxControl::MSWCreateControl(const wxChar *classname,
76 WXDWORD style,
77 const wxPoint& pos,
78 const wxSize& size,
79 const wxString& label,
80 WXDWORD exstyle)
81 {
82 // want3D tells us whether or not the style specified a 3D border.
83 // If so, under WIN16 we can use Ctl3D to give it an appropriate style.
84 // Sometimes want3D is used to indicate that the non-extended style should have
85 // WS_BORDER.
86 bool want3D = TRUE;
87
88 // if no extended style given, determine it ourselves
89 if ( exstyle == (WXDWORD)-1 )
90 {
91 exstyle = GetExStyle(style, &want3D);
92 }
93
94 // all controls have these styles (wxWindows creates all controls visible
95 // by default)
96 style |= WS_CHILD | WS_VISIBLE;
97
98 m_hWnd = (WXHWND)::CreateWindowEx
99 (
100 exstyle, // extended style
101 classname, // the kind of control to create
102 label, // the window name
103 style, // the window style
104 pos.x, pos.y, // the window position
105 size.x, size.y, // and size
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 wxLogDebug(wxT("Failed to create a control of class '%s'"), classname);
115 wxFAIL_MSG(_T("something is very wrong"));
116
117 return FALSE;
118 }
119
120 #if wxUSE_CTL3D
121 if ( want3D )
122 {
123 Ctl3dSubclassCtl(GetHwnd());
124 m_useCtl3D = TRUE;
125 }
126 #endif // wxUSE_CTL3D
127
128 // subclass again for purposes of dialog editing mode
129 SubclassWin(m_hWnd);
130
131 // controls use the same font and colours as their parent dialog by default
132 InheritAttributes();
133
134 return TRUE;
135 }
136
137 wxSize wxControl::DoGetBestSize() const
138 {
139 return wxSize(DEFAULT_ITEM_WIDTH, DEFAULT_ITEM_HEIGHT);
140 }
141
142 bool wxControl::ProcessCommand(wxCommandEvent& event)
143 {
144 #if WXWIN_COMPATIBILITY
145 if ( m_callback )
146 {
147 (void)(*m_callback)(*this, event);
148
149 return TRUE;
150 }
151 else
152 #endif // WXWIN_COMPATIBILITY
153
154 return GetEventHandler()->ProcessEvent(event);
155 }
156
157 #ifdef __WIN95__
158 bool wxControl::MSWOnNotify(int idCtrl,
159 WXLPARAM lParam,
160 WXLPARAM* result)
161 {
162 wxCommandEvent event(wxEVT_NULL, m_windowId);
163 wxEventType eventType = wxEVT_NULL;
164 NMHDR *hdr1 = (NMHDR*) lParam;
165 switch ( hdr1->code )
166 {
167 case NM_CLICK:
168 eventType = wxEVT_COMMAND_LEFT_CLICK;
169 break;
170
171 case NM_DBLCLK:
172 eventType = wxEVT_COMMAND_LEFT_DCLICK;
173 break;
174
175 case NM_RCLICK:
176 eventType = wxEVT_COMMAND_RIGHT_CLICK;
177 break;
178
179 case NM_RDBLCLK:
180 eventType = wxEVT_COMMAND_RIGHT_DCLICK;
181 break;
182
183 case NM_SETFOCUS:
184 eventType = wxEVT_COMMAND_SET_FOCUS;
185 break;
186
187 case NM_KILLFOCUS:
188 eventType = wxEVT_COMMAND_KILL_FOCUS;
189 break;
190
191 case NM_RETURN:
192 eventType = wxEVT_COMMAND_ENTER;
193 break;
194
195 default:
196 return wxWindow::MSWOnNotify(idCtrl, lParam, result);
197 }
198
199 event.SetEventType(eventType);
200 event.SetEventObject(this);
201
202 return GetEventHandler()->ProcessEvent(event);
203 }
204 #endif // Win95
205
206 void wxControl::OnEraseBackground(wxEraseEvent& event)
207 {
208 // notice that this 'dumb' implementation may cause flicker for some of the
209 // controls in which case they should intercept wxEraseEvent and process it
210 // themselves somehow
211
212 RECT rect;
213 ::GetClientRect(GetHwnd(), &rect);
214
215 HBRUSH hBrush = ::CreateSolidBrush(wxColourToRGB(GetBackgroundColour()));
216
217 HDC hdc = GetHdcOf((*event.GetDC()));
218 int mode = ::SetMapMode(hdc, MM_TEXT);
219
220 ::FillRect(hdc, &rect, hBrush);
221 ::DeleteObject(hBrush);
222 ::SetMapMode(hdc, mode);
223 }
224
225 WXHBRUSH wxControl::OnCtlColor(WXHDC pDC, WXHWND pWnd, WXUINT nCtlColor,
226 WXUINT message,
227 WXWPARAM wParam,
228 WXLPARAM lParam)
229 {
230 #if wxUSE_CTL3D
231 if ( m_useCtl3D )
232 {
233 HBRUSH hbrush = Ctl3dCtlColorEx(message, wParam, lParam);
234 return (WXHBRUSH) hbrush;
235 }
236 #endif // wxUSE_CTL3D
237
238 HDC hdc = (HDC)pDC;
239 if (GetParent()->GetTransparentBackground())
240 SetBkMode(hdc, TRANSPARENT);
241 else
242 SetBkMode(hdc, OPAQUE);
243
244 wxColour colBack = GetBackgroundColour();
245
246 ::SetBkColor(hdc, wxColourToRGB(colBack));
247 ::SetTextColor(hdc, wxColourToRGB(GetForegroundColour()));
248
249 wxBrush *brush = wxTheBrushList->FindOrCreateBrush(colBack, wxSOLID);
250
251 return (WXHBRUSH)brush->GetResourceHandle();
252 }
253
254 WXDWORD wxControl::GetExStyle(WXDWORD& style, bool *want3D) const
255 {
256 WXDWORD exStyle = Determine3DEffects(WS_EX_CLIENTEDGE, want3D);
257
258 // Even with extended styles, need to combine with WS_BORDER for them to
259 // look right.
260 if ( *want3D || wxStyleHasBorder(m_windowStyle) )
261 style |= WS_BORDER;
262
263 return exStyle;
264 }
265
266 // ---------------------------------------------------------------------------
267 // global functions
268 // ---------------------------------------------------------------------------
269
270 // Call this repeatedly for several wnds to find the overall size
271 // of the widget.
272 // Call it initially with -1 for all values in rect.
273 // Keep calling for other widgets, and rect will be modified
274 // to calculate largest bounding rectangle.
275 void wxFindMaxSize(WXHWND wnd, RECT *rect)
276 {
277 int left = rect->left;
278 int right = rect->right;
279 int top = rect->top;
280 int bottom = rect->bottom;
281
282 GetWindowRect((HWND) wnd, rect);
283
284 if (left < 0)
285 return;
286
287 if (left < rect->left)
288 rect->left = left;
289
290 if (right > rect->right)
291 rect->right = right;
292
293 if (top < rect->top)
294 rect->top = top;
295
296 if (bottom > rect->bottom)
297 rect->bottom = bottom;
298 }
299