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