]> git.saurik.com Git - wxWidgets.git/blob - src/msw/control.cpp
I seem to have fixed the DeleteSubGroups() bug - PLEASE CHECK!
[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__)) || defined(__TWIN32__)
34 #include <commctrl.h>
35 #endif
36
37 #ifdef GetCharWidth
38 #undef GetCharWidth
39 #undef GetWindowProc
40 #endif
41
42 #if !USE_SHARED_LIBRARY
43 IMPLEMENT_ABSTRACT_CLASS(wxControl, wxWindow)
44
45 BEGIN_EVENT_TABLE(wxControl, wxWindow)
46 EVT_ERASE_BACKGROUND(wxControl::OnEraseBackground)
47 END_EVENT_TABLE()
48 #endif
49
50 // Item members
51 wxControl::wxControl(void)
52 {
53 m_backgroundColour = *wxWHITE;
54 m_foregroundColour = *wxBLACK;
55 m_callback = 0;
56 // m_windowCursor = wxNullCursor; // To avoid the standard cursor being used
57 }
58
59 wxControl::~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 {
68 if (parent->GetDefaultItem() == (wxButton*) this)
69 parent->SetDefaultItem(NULL);
70 }
71 }
72
73 void wxControl::SetLabel(const wxString& label)
74 {
75 if (GetHWND())
76 SetWindowText((HWND) GetHWND(), (const char *)label);
77 }
78
79 wxString wxControl::GetLabel(void) const
80 {
81 wxBuffer[0] = 0;
82 if (GetHWND())
83 {
84 int len = GetWindowText((HWND)GetHWND(), wxBuffer, 256);
85 wxBuffer[len] = 0;
86 }
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.
96 void 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
124 void 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
142 void wxControl::MSWOnMouseMove(int x, int y, WXUINT flags)
143 {
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
150 if (!m_mouseInWindow)
151 {
152 // Generate an ENTER event
153 m_mouseInWindow = TRUE;
154 MSWOnMouseEnter(x, y, flags);
155 }
156
157 wxMouseEvent event(wxEVT_MOTION);
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.
171 if ((m_lastEvent == wxEVT_RIGHT_DOWN || m_lastEvent == wxEVT_LEFT_DOWN ||
172 m_lastEvent == wxEVT_MIDDLE_DOWN) &&
173 (m_lastXPos == event.GetX() && m_lastYPos == event.GetY()))
174 {
175 m_lastXPos = event.GetX(); m_lastYPos = event.GetY();
176 m_lastEvent = wxEVT_MOTION;
177 return;
178 }
179
180 m_lastEvent = wxEVT_MOTION;
181 m_lastXPos = event.GetX(); m_lastYPos = event.GetY();
182
183 if (!GetEventHandler()->ProcessEvent(event))
184 Default();
185 }
186
187 bool wxControl::MSWNotify(WXWPARAM wParam, WXLPARAM lParam,
188 WXLPARAM* result)
189 {
190 #if defined(__WIN95__)
191 wxCommandEvent event(wxEVT_NULL, m_windowId);
192 wxEventType eventType = wxEVT_NULL;
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 */
238 default:
239 return wxWindow::MSWNotify(wParam, lParam, result);
240 }
241
242 event.SetEventType(eventType);
243 event.SetEventObject(this);
244
245 if ( !GetEventHandler()->ProcessEvent(event) )
246 return FALSE;
247 return TRUE;
248 #else // !Win95
249 return FALSE;
250 #endif
251 }
252
253 /*
254 * Allocates control IDs within the appropriate range
255 */
256
257
258 int NewControlId(void)
259 {
260 static int controlId = 0;
261 controlId ++;
262 return controlId;
263 }
264
265 void 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
281 void 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
299 void wxControl::SetClientSize (int width, int height)
300 {
301 SetSize (-1, -1, width, height);
302 }
303
304 void wxControl::Centre (int direction)
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