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"
26 #include "wx/dcclient.h"
29 #include "wx/control.h"
31 #include "wx/msw/private.h"
33 #if (defined(__WIN95__) && !defined(__GNUWIN32__)) || defined(__TWIN32__)
42 #if !USE_SHARED_LIBRARY
43 IMPLEMENT_ABSTRACT_CLASS(wxControl
, wxWindow
)
45 BEGIN_EVENT_TABLE(wxControl
, wxWindow
)
46 EVT_ERASE_BACKGROUND(wxControl::OnEraseBackground
)
51 wxControl::wxControl(void)
53 m_backgroundColour
= *wxWHITE
;
54 m_foregroundColour
= *wxBLACK
;
56 // m_windowCursor = wxNullCursor; // To avoid the standard cursor being used
59 wxControl::~wxControl(void)
61 m_isBeingDeleted
= TRUE
;
63 // If we delete an item, we should initialize the parent panel,
64 // because it could now be invalid.
65 wxWindow
*parent
= (wxWindow
*)GetParent();
68 if (parent
->GetDefaultItem() == (wxButton
*) this)
69 parent
->SetDefaultItem(NULL
);
73 void wxControl::SetLabel(const wxString
& label
)
76 SetWindowText((HWND
) GetHWND(), (const char *)label
);
79 wxString
wxControl::GetLabel(void) const
84 int len
= GetWindowText((HWND
)GetHWND(), wxBuffer
, 256);
88 return wxString(wxBuffer
);
91 // Call this repeatedly for several wnds to find the overall size
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
)
98 int left
= rect
->left
;
99 int right
= rect
->right
;
101 int bottom
= rect
->bottom
;
103 GetWindowRect((HWND
) wnd
, rect
);
108 if (left
< rect
->left
)
111 if (right
> rect
->right
)
117 if (bottom
> rect
->bottom
)
118 rect
->bottom
= bottom
;
123 // Not currently used
124 void wxConvertDialogToPixels(wxWindow *control, int *x, int *y)
126 if (control->m_windowParent && control->m_windowParent->is_dialog)
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);
142 void wxControl::MSWOnMouseMove(int x
, int y
, WXUINT flags
)
145 // Trouble with this is that it sets the cursor for controls too :-(
146 if (m_windowCursor.Ok() && !wxIsBusy())
147 ::SetCursor(m_windowCursor.GetHCURSOR());
150 if (!m_mouseInWindow
)
152 // Generate an ENTER event
153 m_mouseInWindow
= TRUE
;
154 MSWOnMouseEnter(x
, y
, flags
);
157 wxMouseEvent
event(wxEVT_MOTION
);
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 );
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_lastMouseEvent
== wxEVT_RIGHT_DOWN
|| m_lastMouseEvent
== wxEVT_LEFT_DOWN
||
172 m_lastMouseEvent
== wxEVT_MIDDLE_DOWN
) &&
173 (m_lastMouseX
== event
.GetX() && m_lastMouseY
== event
.GetY()))
175 m_lastMouseX
= event
.GetX(); m_lastMouseY
= event
.GetY();
176 m_lastMouseEvent
= wxEVT_MOTION
;
180 m_lastMouseEvent
= wxEVT_MOTION
;
181 m_lastMouseX
= event
.GetX(); m_lastMouseY
= event
.GetY();
183 if (!GetEventHandler()->ProcessEvent(event
))
187 bool wxControl::MSWNotify(WXWPARAM wParam
, WXLPARAM lParam
,
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
)
198 eventType
= wxEVT_COMMAND_LEFT_CLICK
;
203 eventType
= wxEVT_COMMAND_LEFT_DCLICK
;
208 eventType
= wxEVT_COMMAND_RIGHT_CLICK
;
213 eventType
= wxEVT_COMMAND_RIGHT_DCLICK
;
218 eventType
= wxEVT_COMMAND_SET_FOCUS
;
223 eventType
= wxEVT_COMMAND_KILL_FOCUS
;
228 eventType
= wxEVT_COMMAND_ENTER
;
234 eventType = wxEVT_COMMAND_OUT_OF_MEMORY;
239 return wxWindow::MSWNotify(wParam
, lParam
, result
);
242 event
.SetEventType(eventType
);
243 event
.SetEventObject(this);
245 if ( !GetEventHandler()->ProcessEvent(event
) )
253 void wxControl::ProcessCommand (wxCommandEvent
& event
)
256 // 1) A callback function (to become obsolete)
257 // 2) OnCommand, starting at this window and working up parent hierarchy
258 // 3) OnCommand then calls ProcessEvent to search the event tables.
261 (void) (*(m_callback
)) (*this, event
);
265 GetEventHandler()->OnCommand(*this, event
);
269 void wxControl::OnEraseBackground(wxEraseEvent
& event
)
271 // In general, you don't want to erase the background of a control,
272 // or you'll get a flicker.
273 // TODO: move this 'null' function into each control that
277 ::GetClientRect((HWND
) GetHWND(), &rect
);
279 HBRUSH hBrush
= ::CreateSolidBrush(PALETTERGB(GetBackgroundColour().Red(), GetBackgroundColour().Green(), GetBackgroundColour().Blue()));
280 int mode
= ::SetMapMode((HDC
) event
.GetDC()->GetHDC(), MM_TEXT
);
282 ::FillRect ((HDC
) event
.GetDC()->GetHDC(), &rect
, hBrush
);
283 ::DeleteObject(hBrush
);
284 ::SetMapMode((HDC
) event
.GetDC()->GetHDC(), mode
);
287 void wxControl::SetClientSize (int width
, int height
)
289 SetSize (-1, -1, width
, height
);
292 void wxControl::Centre (int direction
)
294 int x
, y
, width
, height
, panel_width
, panel_height
, new_x
, new_y
;
296 wxWindow
*parent
= (wxWindow
*) GetParent ();
300 parent
->GetClientSize (&panel_width
, &panel_height
);
301 GetSize (&width
, &height
);
302 GetPosition (&x
, &y
);
307 if (direction
& wxHORIZONTAL
)
308 new_x
= (int) ((panel_width
- width
) / 2);
310 if (direction
& wxVERTICAL
)
311 new_y
= (int) ((panel_height
- height
) / 2);
313 SetSize (new_x
, new_y
, width
, height
);
315 GetPosition (&temp_x
, &temp_y
);
316 GetPosition (&temp_x
, &temp_y
);