]>
Commit | Line | Data |
---|---|---|
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 | |
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 | #if 0 | |
136 | // We can't rely on Windows giving us events corresponding to the wxWindows Z-ordering. | |
137 | // E.g. we can't push a wxGroupBox to the back for editing purposes. | |
138 | // Convert the item event to parent coordinates, then search for | |
139 | // an item that could receive this event. | |
140 | wxControl *wxFakeItemEvent(wxWindow *parent, wxControl *item, wxMouseEvent& event) | |
141 | { | |
142 | int x, y; | |
143 | item->GetPosition(&x, &y); | |
144 | event.m_x += x; | |
145 | event.m_y += y; | |
146 | ||
147 | wxNode *node = parent->GetChildren()->Last(); | |
148 | while (node) | |
149 | { | |
150 | wxControl *newItem = (wxControl *)node->Data(); | |
151 | if (newItem->IsSelected() && newItem->SelectionHandleHitTest(event.x, event.GetY())) | |
152 | { | |
153 | // This event belongs to the panel. | |
154 | parent->GetEventHandler()->OldOnMouseEvent(event); | |
155 | return NULL; | |
156 | } | |
157 | else if (newItem->HitTest(event.x, event.GetY())) | |
158 | { | |
159 | int x1, y1; | |
160 | newItem->GetPosition(&x1, &y1); | |
161 | event.x -= x1; | |
162 | event.GetY() -= y1; | |
163 | newItem->OldOnMouseEvent(event); | |
164 | return newItem; | |
165 | } | |
166 | node = node->Previous(); | |
167 | } | |
168 | // No takers, so do what we would have done anyway. | |
169 | event.x -= x; | |
170 | event.y -= y; | |
171 | item->OldOnMouseEvent(event); | |
172 | return item; | |
173 | } | |
174 | #endif | |
175 | ||
176 | void wxControl::MSWOnMouseMove(const int x, const int y, const WXUINT flags) | |
177 | { | |
2bda0e17 KB |
178 | /* |
179 | // Trouble with this is that it sets the cursor for controls too :-( | |
180 | if (m_windowCursor.Ok() && !wxIsBusy()) | |
181 | ::SetCursor(m_windowCursor.GetHCURSOR()); | |
182 | */ | |
183 | ||
43d811ea JS |
184 | if (!m_mouseInWindow) |
185 | { | |
186 | // Generate an ENTER event | |
187 | m_mouseInWindow = TRUE; | |
188 | MSWOnMouseEnter(x, y, flags); | |
189 | } | |
2bda0e17 | 190 | |
43d811ea | 191 | wxMouseEvent event(wxEVT_MOTION); |
2bda0e17 KB |
192 | |
193 | event.m_x = x; event.m_y = y; | |
194 | event.m_shiftDown = ((flags & MK_SHIFT) != 0); | |
195 | event.m_controlDown = ((flags & MK_CONTROL) != 0); | |
196 | event.m_leftDown = ((flags & MK_LBUTTON) != 0); | |
197 | event.m_middleDown = ((flags & MK_MBUTTON) != 0); | |
198 | event.m_rightDown = ((flags & MK_RBUTTON) != 0); | |
199 | event.SetTimestamp(wxApp::sm_lastMessageTime); | |
200 | event.SetEventObject( this ); | |
201 | ||
202 | // Window gets a click down message followed by a mouse move | |
203 | // message even if position isn't changed! We want to discard | |
204 | // the trailing move event if x and y are the same. | |
43d811ea JS |
205 | if ((m_lastEvent == wxEVT_RIGHT_DOWN || m_lastEvent == wxEVT_LEFT_DOWN || |
206 | m_lastEvent == wxEVT_MIDDLE_DOWN) && | |
2bda0e17 KB |
207 | (m_lastXPos == event.GetX() && m_lastYPos == event.GetY())) |
208 | { | |
209 | m_lastXPos = event.GetX(); m_lastYPos = event.GetY(); | |
43d811ea | 210 | m_lastEvent = wxEVT_MOTION; |
2bda0e17 KB |
211 | return; |
212 | } | |
213 | ||
43d811ea | 214 | m_lastEvent = wxEVT_MOTION; |
2bda0e17 KB |
215 | m_lastXPos = event.GetX(); m_lastYPos = event.GetY(); |
216 | GetEventHandler()->OldOnMouseEvent(event); | |
217 | } | |
218 | ||
219 | long wxControl::MSWWindowProc(WXUINT nMsg, WXWPARAM wParam, WXLPARAM lParam) | |
220 | { | |
221 | return wxWindow::MSWWindowProc(nMsg, wParam, lParam); | |
222 | } | |
223 | ||
224 | bool wxControl::MSWNotify(const WXWPARAM wParam, const WXLPARAM lParam) | |
225 | { | |
226 | #if defined(__WIN95__) | |
227 | wxCommandEvent event(0, m_windowId); | |
228 | int eventType = 0; | |
229 | NMHDR *hdr1 = (NMHDR*) lParam; | |
230 | switch ( hdr1->code ) | |
231 | { | |
232 | case NM_CLICK: | |
233 | { | |
234 | eventType = wxEVT_COMMAND_LEFT_CLICK; | |
235 | break; | |
236 | } | |
237 | case NM_DBLCLK: | |
238 | { | |
239 | eventType = wxEVT_COMMAND_LEFT_DCLICK; | |
240 | break; | |
241 | } | |
242 | case NM_RCLICK: | |
243 | { | |
244 | eventType = wxEVT_COMMAND_RIGHT_CLICK; | |
245 | break; | |
246 | } | |
247 | case NM_RDBLCLK: | |
248 | { | |
249 | eventType = wxEVT_COMMAND_RIGHT_DCLICK; | |
250 | break; | |
251 | } | |
252 | case NM_SETFOCUS: | |
253 | { | |
254 | eventType = wxEVT_COMMAND_SET_FOCUS; | |
255 | break; | |
256 | } | |
257 | case NM_KILLFOCUS: | |
258 | { | |
259 | eventType = wxEVT_COMMAND_KILL_FOCUS; | |
260 | break; | |
261 | } | |
262 | case NM_RETURN: | |
263 | { | |
264 | eventType = wxEVT_COMMAND_ENTER; | |
265 | break; | |
266 | } | |
267 | /* Not implemented | |
268 | case NM_OUTOFMEMORY: | |
269 | { | |
270 | eventType = wxEVT_COMMAND_OUT_OF_MEMORY; | |
271 | break; | |
272 | } | |
273 | */ | |
274 | default : | |
275 | return FALSE; | |
276 | break; | |
277 | } | |
278 | event.SetEventType(eventType); | |
279 | event.SetEventObject(this); | |
280 | ||
281 | if ( !ProcessEvent(event) ) | |
282 | return FALSE; | |
283 | return TRUE; | |
284 | #else | |
285 | return FALSE; | |
286 | #endif | |
287 | } | |
288 | ||
289 | /* | |
290 | * Allocates control IDs within the appropriate range | |
291 | */ | |
292 | ||
293 | ||
294 | int NewControlId(void) | |
295 | { | |
296 | static int controlId = 0; | |
297 | controlId ++; | |
298 | return controlId; | |
299 | } | |
300 | ||
301 | void wxControl::ProcessCommand (wxCommandEvent & event) | |
302 | { | |
303 | // Tries: | |
304 | // 1) A callback function (to become obsolete) | |
305 | // 2) OnCommand, starting at this window and working up parent hierarchy | |
306 | // 3) OnCommand then calls ProcessEvent to search the event tables. | |
307 | if (m_callback) | |
308 | { | |
309 | (void) (*(m_callback)) (*this, event); | |
310 | } | |
311 | else | |
312 | { | |
313 | GetEventHandler()->OnCommand(*this, event); | |
314 | } | |
315 | } | |
316 | ||
317 | void wxControl::OnEraseBackground(wxEraseEvent& event) | |
318 | { | |
319 | // In general, you don't want to erase the background of a control, | |
320 | // or you'll get a flicker. | |
321 | // TODO: move this 'null' function into each control that | |
322 | // might flicker. | |
323 | ||
324 | RECT rect; | |
325 | ::GetClientRect((HWND) GetHWND(), &rect); | |
326 | ||
327 | HBRUSH hBrush = ::CreateSolidBrush(PALETTERGB(GetBackgroundColour().Red(), GetBackgroundColour().Green(), GetBackgroundColour().Blue())); | |
328 | int mode = ::SetMapMode((HDC) event.GetDC()->GetHDC(), MM_TEXT); | |
329 | ||
330 | ::FillRect ((HDC) event.GetDC()->GetHDC(), &rect, hBrush); | |
331 | ::DeleteObject(hBrush); | |
332 | ::SetMapMode((HDC) event.GetDC()->GetHDC(), mode); | |
333 | } | |
334 | ||
335 | void wxControl::SetClientSize (const int width, const int height) | |
336 | { | |
337 | SetSize (-1, -1, width, height); | |
338 | } | |
339 | ||
340 | void wxControl::Centre (const int direction) | |
341 | { | |
342 | int x, y, width, height, panel_width, panel_height, new_x, new_y; | |
343 | ||
344 | wxWindow *parent = (wxWindow *) GetParent (); | |
345 | if (!parent) | |
346 | return; | |
347 | ||
348 | parent->GetClientSize (&panel_width, &panel_height); | |
349 | GetSize (&width, &height); | |
350 | GetPosition (&x, &y); | |
351 | ||
352 | new_x = x; | |
353 | new_y = y; | |
354 | ||
355 | if (direction & wxHORIZONTAL) | |
356 | new_x = (int) ((panel_width - width) / 2); | |
357 | ||
358 | if (direction & wxVERTICAL) | |
359 | new_y = (int) ((panel_height - height) / 2); | |
360 | ||
361 | SetSize (new_x, new_y, width, height); | |
362 | int temp_x, temp_y; | |
363 | GetPosition (&temp_x, &temp_y); | |
364 | GetPosition (&temp_x, &temp_y); | |
365 | } | |
366 | ||
367 |