]> git.saurik.com Git - wxWidgets.git/blame - src/msw/control.cpp
argc == 0 bug fixed
[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
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
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
57c208c5 33#if (defined(__WIN95__) && !defined(__GNUWIN32__)) || defined(__TWIN32__)
2bda0e17
KB
34#include <commctrl.h>
35#endif
36
37#ifdef GetCharWidth
38#undef GetCharWidth
39#undef GetWindowProc
40#endif
41
42#if !USE_SHARED_LIBRARY
43IMPLEMENT_ABSTRACT_CLASS(wxControl, wxWindow)
44
45BEGIN_EVENT_TABLE(wxControl, wxWindow)
46 EVT_ERASE_BACKGROUND(wxControl::OnEraseBackground)
47END_EVENT_TABLE()
48#endif
49
50// Item members
51wxControl::wxControl(void)
52{
53 m_backgroundColour = *wxWHITE;
54 m_foregroundColour = *wxBLACK;
55 m_callback = 0;
2a47d3c1 56// m_windowCursor = wxNullCursor; // To avoid the standard cursor being used
2bda0e17
KB
57}
58
59wxControl::~wxControl(void)
60{
61 m_isBeingDeleted = TRUE;
62
63 // If we delete an item, we should initialize the parent panel,
64 // because it could now be invalid.
65 wxWindow *parent = (wxWindow *)GetParent();
66 if (parent)
67 {
2432b92d 68 if (parent->GetDefaultItem() == (wxButton*) this)
2bda0e17
KB
69 parent->SetDefaultItem(NULL);
70 }
71}
72
73void wxControl::SetLabel(const wxString& label)
74{
75 if (GetHWND())
76 SetWindowText((HWND) GetHWND(), (const char *)label);
77}
78
79wxString wxControl::GetLabel(void) const
80{
9c331ded
JS
81 wxBuffer[0] = 0;
82 if (GetHWND())
83 {
84 int len = GetWindowText((HWND)GetHWND(), wxBuffer, 256);
85 wxBuffer[len] = 0;
86 }
2bda0e17
KB
87
88 return wxString(wxBuffer);
89}
90
91// Call this repeatedly for several wnds to find the overall size
92// of the widget.
93// Call it initially with -1 for all values in rect.
94// Keep calling for other widgets, and rect will be modified
95// to calculate largest bounding rectangle.
96void wxFindMaxSize(WXHWND wnd, RECT *rect)
97{
98 int left = rect->left;
99 int right = rect->right;
100 int top = rect->top;
101 int bottom = rect->bottom;
102
103 GetWindowRect((HWND) wnd, rect);
104
105 if (left < 0)
106 return;
107
108 if (left < rect->left)
109 rect->left = left;
110
111 if (right > rect->right)
112 rect->right = right;
113
114 if (top < rect->top)
115 rect->top = top;
116
117 if (bottom > rect->bottom)
118 rect->bottom = bottom;
119
120}
121
122/*
123// Not currently used
124void wxConvertDialogToPixels(wxWindow *control, int *x, int *y)
125{
126 if (control->m_windowParent && control->m_windowParent->is_dialog)
127 {
128 DWORD word = GetDialogBaseUnits();
129 int xs = LOWORD(word);
130 int ys = HIWORD(word);
131 *x = (int)(*x * xs/4);
132 *y = (int)(*y * ys/8);
133 }
134 else
135 {
136 *x = *x;
137 *y = *y;
138 }
139}
140*/
141
debe6624 142void wxControl::MSWOnMouseMove(int x, int y, WXUINT flags)
2bda0e17 143{
2bda0e17
KB
144/*
145 // Trouble with this is that it sets the cursor for controls too :-(
146 if (m_windowCursor.Ok() && !wxIsBusy())
147 ::SetCursor(m_windowCursor.GetHCURSOR());
148*/
149
43d811ea
JS
150 if (!m_mouseInWindow)
151 {
152 // Generate an ENTER event
153 m_mouseInWindow = TRUE;
154 MSWOnMouseEnter(x, y, flags);
155 }
2bda0e17 156
43d811ea 157 wxMouseEvent event(wxEVT_MOTION);
2bda0e17
KB
158
159 event.m_x = x; event.m_y = y;
160 event.m_shiftDown = ((flags & MK_SHIFT) != 0);
161 event.m_controlDown = ((flags & MK_CONTROL) != 0);
162 event.m_leftDown = ((flags & MK_LBUTTON) != 0);
163 event.m_middleDown = ((flags & MK_MBUTTON) != 0);
164 event.m_rightDown = ((flags & MK_RBUTTON) != 0);
165 event.SetTimestamp(wxApp::sm_lastMessageTime);
166 event.SetEventObject( this );
167
168 // Window gets a click down message followed by a mouse move
169 // message even if position isn't changed! We want to discard
170 // the trailing move event if x and y are the same.
43d811ea
JS
171 if ((m_lastEvent == wxEVT_RIGHT_DOWN || m_lastEvent == wxEVT_LEFT_DOWN ||
172 m_lastEvent == wxEVT_MIDDLE_DOWN) &&
2bda0e17
KB
173 (m_lastXPos == event.GetX() && m_lastYPos == event.GetY()))
174 {
175 m_lastXPos = event.GetX(); m_lastYPos = event.GetY();
43d811ea 176 m_lastEvent = wxEVT_MOTION;
2bda0e17
KB
177 return;
178 }
179
43d811ea 180 m_lastEvent = wxEVT_MOTION;
2bda0e17 181 m_lastXPos = event.GetX(); m_lastYPos = event.GetY();
debe6624
JS
182
183 if (!GetEventHandler()->ProcessEvent(event))
184 Default();
2bda0e17
KB
185}
186
fd3f686c 187bool wxControl::MSWNotify(WXWPARAM wParam, WXLPARAM lParam,
fff86b29 188 WXLPARAM* result)
2bda0e17
KB
189{
190#if defined(__WIN95__)
7798a18e
JS
191 wxCommandEvent event(wxEVT_NULL, m_windowId);
192 wxEventType eventType = wxEVT_NULL;
2bda0e17
KB
193 NMHDR *hdr1 = (NMHDR*) lParam;
194 switch ( hdr1->code )
195 {
196 case NM_CLICK:
197 {
198 eventType = wxEVT_COMMAND_LEFT_CLICK;
199 break;
200 }
201 case NM_DBLCLK:
202 {
203 eventType = wxEVT_COMMAND_LEFT_DCLICK;
204 break;
205 }
206 case NM_RCLICK:
207 {
208 eventType = wxEVT_COMMAND_RIGHT_CLICK;
209 break;
210 }
211 case NM_RDBLCLK:
212 {
213 eventType = wxEVT_COMMAND_RIGHT_DCLICK;
214 break;
215 }
216 case NM_SETFOCUS:
217 {
218 eventType = wxEVT_COMMAND_SET_FOCUS;
219 break;
220 }
221 case NM_KILLFOCUS:
222 {
223 eventType = wxEVT_COMMAND_KILL_FOCUS;
224 break;
225 }
226 case NM_RETURN:
227 {
228 eventType = wxEVT_COMMAND_ENTER;
229 break;
230 }
231/* Not implemented
232 case NM_OUTOFMEMORY:
233 {
234 eventType = wxEVT_COMMAND_OUT_OF_MEMORY;
235 break;
236 }
237*/
fd3f686c 238 default:
fff86b29 239 return wxWindow::MSWNotify(wParam, lParam, result);
2bda0e17 240 }
fd3f686c 241
2bda0e17
KB
242 event.SetEventType(eventType);
243 event.SetEventObject(this);
244
02800301 245 if ( !GetEventHandler()->ProcessEvent(event) )
2bda0e17
KB
246 return FALSE;
247 return TRUE;
fd3f686c
VZ
248#else // !Win95
249 return FALSE;
2bda0e17
KB
250#endif
251}
252
253/*
254 * Allocates control IDs within the appropriate range
255 */
256
257
258int NewControlId(void)
259{
260 static int controlId = 0;
261 controlId ++;
262 return controlId;
263}
264
265void wxControl::ProcessCommand (wxCommandEvent & event)
266{
267 // Tries:
268 // 1) A callback function (to become obsolete)
269 // 2) OnCommand, starting at this window and working up parent hierarchy
270 // 3) OnCommand then calls ProcessEvent to search the event tables.
271 if (m_callback)
272 {
273 (void) (*(m_callback)) (*this, event);
274 }
275 else
276 {
277 GetEventHandler()->OnCommand(*this, event);
278 }
279}
280
281void wxControl::OnEraseBackground(wxEraseEvent& event)
282{
283 // In general, you don't want to erase the background of a control,
284 // or you'll get a flicker.
285 // TODO: move this 'null' function into each control that
286 // might flicker.
287
288 RECT rect;
289 ::GetClientRect((HWND) GetHWND(), &rect);
290
291 HBRUSH hBrush = ::CreateSolidBrush(PALETTERGB(GetBackgroundColour().Red(), GetBackgroundColour().Green(), GetBackgroundColour().Blue()));
292 int mode = ::SetMapMode((HDC) event.GetDC()->GetHDC(), MM_TEXT);
293
294 ::FillRect ((HDC) event.GetDC()->GetHDC(), &rect, hBrush);
295 ::DeleteObject(hBrush);
296 ::SetMapMode((HDC) event.GetDC()->GetHDC(), mode);
297}
298
debe6624 299void wxControl::SetClientSize (int width, int height)
2bda0e17
KB
300{
301 SetSize (-1, -1, width, height);
302}
303
debe6624 304void wxControl::Centre (int direction)
2bda0e17
KB
305{
306 int x, y, width, height, panel_width, panel_height, new_x, new_y;
307
308 wxWindow *parent = (wxWindow *) GetParent ();
309 if (!parent)
310 return;
311
312 parent->GetClientSize (&panel_width, &panel_height);
313 GetSize (&width, &height);
314 GetPosition (&x, &y);
315
316 new_x = x;
317 new_y = y;
318
319 if (direction & wxHORIZONTAL)
320 new_x = (int) ((panel_width - width) / 2);
321
322 if (direction & wxVERTICAL)
323 new_y = (int) ((panel_height - height) / 2);
324
325 SetSize (new_x, new_y, width, height);
326 int temp_x, temp_y;
327 GetPosition (&temp_x, &temp_y);
328 GetPosition (&temp_x, &temp_y);
329}
330
331