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