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