]> git.saurik.com Git - wxWidgets.git/blob - src/msw/control.cpp
GDI double-deletion fix, wxBitmap depth bug
[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 {
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 FALSE;
240 break;
241 }
242 event.SetEventType(eventType);
243 event.SetEventObject(this);
244
245 if ( !ProcessEvent(event) )
246 return FALSE;
247 return TRUE;
248 #else
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