1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxControl class
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart and Markus Holzem
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
13 #pragma implementation "control.h"
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
25 #include "wx/dcclient.h"
28 #include "wx/msw/private.h"
30 #if defined(__WIN95__) && !defined(__GNUWIN32__)
39 #if !USE_SHARED_LIBRARY
40 IMPLEMENT_ABSTRACT_CLASS(wxControl
, wxWindow
)
42 BEGIN_EVENT_TABLE(wxControl
, wxWindow
)
43 EVT_ERASE_BACKGROUND(wxControl::OnEraseBackground
)
48 wxControl::wxControl(void)
50 m_backgroundColour
= *wxWHITE
;
51 m_foregroundColour
= *wxBLACK
;
55 wxControl::~wxControl(void)
57 m_isBeingDeleted
= TRUE
;
59 // If we delete an item, we should initialize the parent panel,
60 // because it could now be invalid.
61 wxWindow
*parent
= (wxWindow
*)GetParent();
64 if (parent
->GetDefaultItem() == this)
65 parent
->SetDefaultItem(NULL
);
69 void wxControl::SetLabel(const wxString
& label
)
72 SetWindowText((HWND
) GetHWND(), (const char *)label
);
75 wxString
wxControl::GetLabel(void) const
79 GetWindowText((HWND
)GetHWND(), wxBuffer
, 1000);
81 return wxString(wxBuffer
);
84 // Call this repeatedly for several wnds to find the overall size
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
)
91 int left
= rect
->left
;
92 int right
= rect
->right
;
94 int bottom
= rect
->bottom
;
96 GetWindowRect((HWND
) wnd
, rect
);
101 if (left
< rect
->left
)
104 if (right
> rect
->right
)
110 if (bottom
> rect
->bottom
)
111 rect
->bottom
= bottom
;
116 // Not currently used
117 void wxConvertDialogToPixels(wxWindow *control, int *x, int *y)
119 if (control->m_windowParent && control->m_windowParent->is_dialog)
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);
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
)
143 item
->GetPosition(&x
, &y
);
147 wxNode
*node
= parent
->GetChildren()->Last();
150 wxControl
*newItem
= (wxControl
*)node
->Data();
151 if (newItem
->IsSelected() && newItem
->SelectionHandleHitTest(event
.x
, event
.GetY()))
153 // This event belongs to the panel.
154 parent
->GetEventHandler()->OldOnMouseEvent(event
);
157 else if (newItem
->HitTest(event
.x
, event
.GetY()))
160 newItem
->GetPosition(&x1
, &y1
);
163 newItem
->OldOnMouseEvent(event
);
166 node
= node
->Previous();
168 // No takers, so do what we would have done anyway.
171 item
->OldOnMouseEvent(event
);
176 void wxControl::MSWOnMouseMove(const int x
, const int y
, const WXUINT flags
)
178 // 'normal' move event...
179 // Set cursor, but only if we're not in 'busy' mode
182 // Trouble with this is that it sets the cursor for controls too :-(
183 if (m_windowCursor.Ok() && !wxIsBusy())
184 ::SetCursor(m_windowCursor.GetHCURSOR());
187 wxMouseEvent
event(wxEVENT_TYPE_MOTION
);
193 MSWDeviceToLogical(&px, &py);
195 CalcUnscrolledPosition((int)px, (int)py, &event.m_x, &event.m_y);
198 event
.m_x
= x
; event
.m_y
= y
;
199 event
.m_shiftDown
= ((flags
& MK_SHIFT
) != 0);
200 event
.m_controlDown
= ((flags
& MK_CONTROL
) != 0);
201 event
.m_leftDown
= ((flags
& MK_LBUTTON
) != 0);
202 event
.m_middleDown
= ((flags
& MK_MBUTTON
) != 0);
203 event
.m_rightDown
= ((flags
& MK_RBUTTON
) != 0);
204 event
.SetTimestamp(wxApp::sm_lastMessageTime
);
205 event
.SetEventObject( this );
207 // Window gets a click down message followed by a mouse move
208 // message even if position isn't changed! We want to discard
209 // the trailing move event if x and y are the same.
210 if ((m_lastEvent
== wxEVENT_TYPE_RIGHT_DOWN
|| m_lastEvent
== wxEVENT_TYPE_LEFT_DOWN
||
211 m_lastEvent
== wxEVENT_TYPE_MIDDLE_DOWN
) &&
212 (m_lastXPos
== event
.GetX() && m_lastYPos
== event
.GetY()))
214 m_lastXPos
= event
.GetX(); m_lastYPos
= event
.GetY();
215 m_lastEvent
= wxEVENT_TYPE_MOTION
;
219 m_lastEvent
= wxEVENT_TYPE_MOTION
;
220 m_lastXPos
= event
.GetX(); m_lastYPos
= event
.GetY();
221 GetEventHandler()->OldOnMouseEvent(event
);
224 long wxControl::MSWWindowProc(WXUINT nMsg
, WXWPARAM wParam
, WXLPARAM lParam
)
226 return wxWindow::MSWWindowProc(nMsg
, wParam
, lParam
);
229 bool wxControl::MSWNotify(const WXWPARAM wParam
, const WXLPARAM lParam
)
231 #if defined(__WIN95__)
232 wxCommandEvent
event(0, m_windowId
);
234 NMHDR
*hdr1
= (NMHDR
*) lParam
;
235 switch ( hdr1
->code
)
239 eventType
= wxEVT_COMMAND_LEFT_CLICK
;
244 eventType
= wxEVT_COMMAND_LEFT_DCLICK
;
249 eventType
= wxEVT_COMMAND_RIGHT_CLICK
;
254 eventType
= wxEVT_COMMAND_RIGHT_DCLICK
;
259 eventType
= wxEVT_COMMAND_SET_FOCUS
;
264 eventType
= wxEVT_COMMAND_KILL_FOCUS
;
269 eventType
= wxEVT_COMMAND_ENTER
;
275 eventType = wxEVT_COMMAND_OUT_OF_MEMORY;
283 event
.SetEventType(eventType
);
284 event
.SetEventObject(this);
286 if ( !ProcessEvent(event
) )
295 * Allocates control IDs within the appropriate range
299 int NewControlId(void)
301 static int controlId
= 0;
306 void wxControl::ProcessCommand (wxCommandEvent
& event
)
309 // 1) A callback function (to become obsolete)
310 // 2) OnCommand, starting at this window and working up parent hierarchy
311 // 3) OnCommand then calls ProcessEvent to search the event tables.
314 (void) (*(m_callback
)) (*this, event
);
318 GetEventHandler()->OnCommand(*this, event
);
322 void wxControl::OnEraseBackground(wxEraseEvent
& event
)
324 // In general, you don't want to erase the background of a control,
325 // or you'll get a flicker.
326 // TODO: move this 'null' function into each control that
330 ::GetClientRect((HWND
) GetHWND(), &rect
);
332 HBRUSH hBrush
= ::CreateSolidBrush(PALETTERGB(GetBackgroundColour().Red(), GetBackgroundColour().Green(), GetBackgroundColour().Blue()));
333 int mode
= ::SetMapMode((HDC
) event
.GetDC()->GetHDC(), MM_TEXT
);
335 ::FillRect ((HDC
) event
.GetDC()->GetHDC(), &rect
, hBrush
);
336 ::DeleteObject(hBrush
);
337 ::SetMapMode((HDC
) event
.GetDC()->GetHDC(), mode
);
340 void wxControl::SetClientSize (const int width
, const int height
)
342 SetSize (-1, -1, width
, height
);
345 void wxControl::Centre (const int direction
)
347 int x
, y
, width
, height
, panel_width
, panel_height
, new_x
, new_y
;
349 wxWindow
*parent
= (wxWindow
*) GetParent ();
353 parent
->GetClientSize (&panel_width
, &panel_height
);
354 GetSize (&width
, &height
);
355 GetPosition (&x
, &y
);
360 if (direction
& wxHORIZONTAL
)
361 new_x
= (int) ((panel_width
- width
) / 2);
363 if (direction
& wxVERTICAL
)
364 new_y
= (int) ((panel_height
- height
) / 2);
366 SetSize (new_x
, new_y
, width
, height
);
368 GetPosition (&temp_x
, &temp_y
);
369 GetPosition (&temp_x
, &temp_y
);