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__)
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
;
58 wxControl::~wxControl(void)
60 m_isBeingDeleted
= TRUE
;
62 // If we delete an item, we should initialize the parent panel,
63 // because it could now be invalid.
64 wxWindow
*parent
= (wxWindow
*)GetParent();
67 if (parent
->GetDefaultItem() == (wxButton
*) this)
68 parent
->SetDefaultItem(NULL
);
72 void wxControl::SetLabel(const wxString
& label
)
75 SetWindowText((HWND
) GetHWND(), (const char *)label
);
78 wxString
wxControl::GetLabel(void) const
83 int len
= GetWindowText((HWND
)GetHWND(), wxBuffer
, 256);
87 return wxString(wxBuffer
);
90 // Call this repeatedly for several wnds to find the overall size
92 // Call it initially with -1 for all values in rect.
93 // Keep calling for other widgets, and rect will be modified
94 // to calculate largest bounding rectangle.
95 void wxFindMaxSize(WXHWND wnd
, RECT
*rect
)
97 int left
= rect
->left
;
98 int right
= rect
->right
;
100 int bottom
= rect
->bottom
;
102 GetWindowRect((HWND
) wnd
, rect
);
107 if (left
< rect
->left
)
110 if (right
> rect
->right
)
116 if (bottom
> rect
->bottom
)
117 rect
->bottom
= bottom
;
122 // Not currently used
123 void wxConvertDialogToPixels(wxWindow *control, int *x, int *y)
125 if (control->m_windowParent && control->m_windowParent->is_dialog)
127 DWORD word = GetDialogBaseUnits();
128 int xs = LOWORD(word);
129 int ys = HIWORD(word);
130 *x = (int)(*x * xs/4);
131 *y = (int)(*y * ys/8);
141 void wxControl::MSWOnMouseMove(int x
, int y
, WXUINT flags
)
144 // Trouble with this is that it sets the cursor for controls too :-(
145 if (m_windowCursor.Ok() && !wxIsBusy())
146 ::SetCursor(m_windowCursor.GetHCURSOR());
149 if (!m_mouseInWindow
)
151 // Generate an ENTER event
152 m_mouseInWindow
= TRUE
;
153 MSWOnMouseEnter(x
, y
, flags
);
156 wxMouseEvent
event(wxEVT_MOTION
);
158 event
.m_x
= x
; event
.m_y
= y
;
159 event
.m_shiftDown
= ((flags
& MK_SHIFT
) != 0);
160 event
.m_controlDown
= ((flags
& MK_CONTROL
) != 0);
161 event
.m_leftDown
= ((flags
& MK_LBUTTON
) != 0);
162 event
.m_middleDown
= ((flags
& MK_MBUTTON
) != 0);
163 event
.m_rightDown
= ((flags
& MK_RBUTTON
) != 0);
164 event
.SetTimestamp(wxApp::sm_lastMessageTime
);
165 event
.SetEventObject( this );
167 // Window gets a click down message followed by a mouse move
168 // message even if position isn't changed! We want to discard
169 // the trailing move event if x and y are the same.
170 if ((m_lastEvent
== wxEVT_RIGHT_DOWN
|| m_lastEvent
== wxEVT_LEFT_DOWN
||
171 m_lastEvent
== wxEVT_MIDDLE_DOWN
) &&
172 (m_lastXPos
== event
.GetX() && m_lastYPos
== event
.GetY()))
174 m_lastXPos
= event
.GetX(); m_lastYPos
= event
.GetY();
175 m_lastEvent
= wxEVT_MOTION
;
179 m_lastEvent
= wxEVT_MOTION
;
180 m_lastXPos
= event
.GetX(); m_lastYPos
= event
.GetY();
182 if (!GetEventHandler()->ProcessEvent(event
))
186 long wxControl::MSWWindowProc(WXUINT nMsg
, WXWPARAM wParam
, WXLPARAM lParam
)
188 return wxWindow::MSWWindowProc(nMsg
, wParam
, lParam
);
191 bool wxControl::MSWNotify(WXWPARAM wParam
, WXLPARAM lParam
,
192 WXLPARAM
* WXUNUSED(result
))
194 #if defined(__WIN95__)
195 wxCommandEvent
event(wxEVT_NULL
, m_windowId
);
196 wxEventType eventType
= wxEVT_NULL
;
197 NMHDR
*hdr1
= (NMHDR
*) lParam
;
198 switch ( hdr1
->code
)
202 eventType
= wxEVT_COMMAND_LEFT_CLICK
;
207 eventType
= wxEVT_COMMAND_LEFT_DCLICK
;
212 eventType
= wxEVT_COMMAND_RIGHT_CLICK
;
217 eventType
= wxEVT_COMMAND_RIGHT_DCLICK
;
222 eventType
= wxEVT_COMMAND_SET_FOCUS
;
227 eventType
= wxEVT_COMMAND_KILL_FOCUS
;
232 eventType
= wxEVT_COMMAND_ENTER
;
238 eventType = wxEVT_COMMAND_OUT_OF_MEMORY;
246 event
.SetEventType(eventType
);
247 event
.SetEventObject(this);
249 if ( !GetEventHandler()->ProcessEvent(event
) )
258 * Allocates control IDs within the appropriate range
262 int NewControlId(void)
264 static int controlId
= 0;
269 void wxControl::ProcessCommand (wxCommandEvent
& event
)
272 // 1) A callback function (to become obsolete)
273 // 2) OnCommand, starting at this window and working up parent hierarchy
274 // 3) OnCommand then calls ProcessEvent to search the event tables.
277 (void) (*(m_callback
)) (*this, event
);
281 GetEventHandler()->OnCommand(*this, event
);
285 void wxControl::OnEraseBackground(wxEraseEvent
& event
)
287 // In general, you don't want to erase the background of a control,
288 // or you'll get a flicker.
289 // TODO: move this 'null' function into each control that
293 ::GetClientRect((HWND
) GetHWND(), &rect
);
295 HBRUSH hBrush
= ::CreateSolidBrush(PALETTERGB(GetBackgroundColour().Red(), GetBackgroundColour().Green(), GetBackgroundColour().Blue()));
296 int mode
= ::SetMapMode((HDC
) event
.GetDC()->GetHDC(), MM_TEXT
);
298 ::FillRect ((HDC
) event
.GetDC()->GetHDC(), &rect
, hBrush
);
299 ::DeleteObject(hBrush
);
300 ::SetMapMode((HDC
) event
.GetDC()->GetHDC(), mode
);
303 void wxControl::SetClientSize (int width
, int height
)
305 SetSize (-1, -1, width
, height
);
308 void wxControl::Centre (int direction
)
310 int x
, y
, width
, height
, panel_width
, panel_height
, new_x
, new_y
;
312 wxWindow
*parent
= (wxWindow
*) GetParent ();
316 parent
->GetClientSize (&panel_width
, &panel_height
);
317 GetSize (&width
, &height
);
318 GetPosition (&x
, &y
);
323 if (direction
& wxHORIZONTAL
)
324 new_x
= (int) ((panel_width
- width
) / 2);
326 if (direction
& wxVERTICAL
)
327 new_y
= (int) ((panel_height
- height
) / 2);
329 SetSize (new_x
, new_y
, width
, height
);
331 GetPosition (&temp_x
, &temp_y
);
332 GetPosition (&temp_x
, &temp_y
);