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
80 int len
= GetWindowText((HWND
)GetHWND(), wxBuffer
, 256);
84 return wxString(wxBuffer
);
87 // Call this repeatedly for several wnds to find the overall size
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
)
94 int left
= rect
->left
;
95 int right
= rect
->right
;
97 int bottom
= rect
->bottom
;
99 GetWindowRect((HWND
) wnd
, rect
);
104 if (left
< rect
->left
)
107 if (right
> rect
->right
)
113 if (bottom
> rect
->bottom
)
114 rect
->bottom
= bottom
;
119 // Not currently used
120 void wxConvertDialogToPixels(wxWindow *control, int *x, int *y)
122 if (control->m_windowParent && control->m_windowParent->is_dialog)
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);
138 void wxControl::MSWOnMouseMove(int x
, int y
, WXUINT flags
)
141 // Trouble with this is that it sets the cursor for controls too :-(
142 if (m_windowCursor.Ok() && !wxIsBusy())
143 ::SetCursor(m_windowCursor.GetHCURSOR());
146 if (!m_mouseInWindow
)
148 // Generate an ENTER event
149 m_mouseInWindow
= TRUE
;
150 MSWOnMouseEnter(x
, y
, flags
);
153 wxMouseEvent
event(wxEVT_MOTION
);
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 );
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()))
171 m_lastXPos
= event
.GetX(); m_lastYPos
= event
.GetY();
172 m_lastEvent
= wxEVT_MOTION
;
176 m_lastEvent
= wxEVT_MOTION
;
177 m_lastXPos
= event
.GetX(); m_lastYPos
= event
.GetY();
179 if (!GetEventHandler()->ProcessEvent(event
))
183 long wxControl::MSWWindowProc(WXUINT nMsg
, WXWPARAM wParam
, WXLPARAM lParam
)
185 return wxWindow::MSWWindowProc(nMsg
, wParam
, lParam
);
188 bool wxControl::MSWNotify(WXWPARAM wParam
, WXLPARAM lParam
,
189 WXLPARAM
* WXUNUSED(result
))
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
)
199 eventType
= wxEVT_COMMAND_LEFT_CLICK
;
204 eventType
= wxEVT_COMMAND_LEFT_DCLICK
;
209 eventType
= wxEVT_COMMAND_RIGHT_CLICK
;
214 eventType
= wxEVT_COMMAND_RIGHT_DCLICK
;
219 eventType
= wxEVT_COMMAND_SET_FOCUS
;
224 eventType
= wxEVT_COMMAND_KILL_FOCUS
;
229 eventType
= wxEVT_COMMAND_ENTER
;
235 eventType = wxEVT_COMMAND_OUT_OF_MEMORY;
243 event
.SetEventType(eventType
);
244 event
.SetEventObject(this);
246 if ( !GetEventHandler()->ProcessEvent(event
) )
255 * Allocates control IDs within the appropriate range
259 int NewControlId(void)
261 static int controlId
= 0;
266 void wxControl::ProcessCommand (wxCommandEvent
& event
)
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.
274 (void) (*(m_callback
)) (*this, event
);
278 GetEventHandler()->OnCommand(*this, event
);
282 void wxControl::OnEraseBackground(wxEraseEvent
& event
)
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
290 ::GetClientRect((HWND
) GetHWND(), &rect
);
292 HBRUSH hBrush
= ::CreateSolidBrush(PALETTERGB(GetBackgroundColour().Red(), GetBackgroundColour().Green(), GetBackgroundColour().Blue()));
293 int mode
= ::SetMapMode((HDC
) event
.GetDC()->GetHDC(), MM_TEXT
);
295 ::FillRect ((HDC
) event
.GetDC()->GetHDC(), &rect
, hBrush
);
296 ::DeleteObject(hBrush
);
297 ::SetMapMode((HDC
) event
.GetDC()->GetHDC(), mode
);
300 void wxControl::SetClientSize (int width
, int height
)
302 SetSize (-1, -1, width
, height
);
305 void wxControl::Centre (int direction
)
307 int x
, y
, width
, height
, panel_width
, panel_height
, new_x
, new_y
;
309 wxWindow
*parent
= (wxWindow
*) GetParent ();
313 parent
->GetClientSize (&panel_width
, &panel_height
);
314 GetSize (&width
, &height
);
315 GetPosition (&x
, &y
);
320 if (direction
& wxHORIZONTAL
)
321 new_x
= (int) ((panel_width
- width
) / 2);
323 if (direction
& wxVERTICAL
)
324 new_y
= (int) ((panel_height
- height
) / 2);
326 SetSize (new_x
, new_y
, width
, height
);
328 GetPosition (&temp_x
, &temp_y
);
329 GetPosition (&temp_x
, &temp_y
);