]> git.saurik.com Git - wxWidgets.git/blame - src/msw/control.cpp
fixed an over-optimisation
[wxWidgets.git] / src / msw / control.cpp
CommitLineData
2bda0e17
KB
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
a23fd0e1 9// Licence: wxWindows licence
2bda0e17
KB
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
2432b92d 24#include "wx/event.h"
2bda0e17
KB
25#include "wx/app.h"
26#include "wx/dcclient.h"
27#endif
28
2432b92d
JS
29#include "wx/control.h"
30
2bda0e17
KB
31#include "wx/msw/private.h"
32
57c208c5 33#if (defined(__WIN95__) && !defined(__GNUWIN32__)) || defined(__TWIN32__)
2bda0e17
KB
34#include <commctrl.h>
35#endif
36
37#ifdef GetCharWidth
38#undef GetCharWidth
39#undef GetWindowProc
40#endif
41
42#if !USE_SHARED_LIBRARY
43IMPLEMENT_ABSTRACT_CLASS(wxControl, wxWindow)
44
45BEGIN_EVENT_TABLE(wxControl, wxWindow)
a23fd0e1 46 EVT_ERASE_BACKGROUND(wxControl::OnEraseBackground)
2bda0e17
KB
47END_EVENT_TABLE()
48#endif
49
50// Item members
51wxControl::wxControl(void)
52{
53 m_backgroundColour = *wxWHITE;
54 m_foregroundColour = *wxBLACK;
55 m_callback = 0;
2a47d3c1 56// m_windowCursor = wxNullCursor; // To avoid the standard cursor being used
2bda0e17
KB
57}
58
59wxControl::~wxControl(void)
60{
61 m_isBeingDeleted = TRUE;
62
63 // If we delete an item, we should initialize the parent panel,
64 // because it could now be invalid.
65 wxWindow *parent = (wxWindow *)GetParent();
66 if (parent)
67 {
2432b92d 68 if (parent->GetDefaultItem() == (wxButton*) this)
2bda0e17
KB
69 parent->SetDefaultItem(NULL);
70 }
71}
72
73void wxControl::SetLabel(const wxString& label)
74{
75 if (GetHWND())
76 SetWindowText((HWND) GetHWND(), (const char *)label);
77}
78
79wxString wxControl::GetLabel(void) const
80{
9c331ded
JS
81 wxBuffer[0] = 0;
82 if (GetHWND())
83 {
84 int len = GetWindowText((HWND)GetHWND(), wxBuffer, 256);
85 wxBuffer[len] = 0;
86 }
2bda0e17
KB
87
88 return wxString(wxBuffer);
89}
90
91// Call this repeatedly for several wnds to find the overall size
92// of the widget.
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.
96void wxFindMaxSize(WXHWND wnd, RECT *rect)
97{
98 int left = rect->left;
99 int right = rect->right;
100 int top = rect->top;
101 int bottom = rect->bottom;
102
103 GetWindowRect((HWND) wnd, rect);
104
105 if (left < 0)
106 return;
107
108 if (left < rect->left)
109 rect->left = left;
110
111 if (right > rect->right)
112 rect->right = right;
113
114 if (top < rect->top)
115 rect->top = top;
116
117 if (bottom > rect->bottom)
118 rect->bottom = bottom;
119
120}
121
a23fd0e1
VZ
122#ifdef __WIN95__
123bool wxControl::MSWOnNotify(int idCtrl,
124 WXLPARAM lParam,
125 WXLPARAM* result)
2bda0e17 126{
a23fd0e1
VZ
127 wxCommandEvent event(wxEVT_NULL, m_windowId);
128 wxEventType eventType = wxEVT_NULL;
129 NMHDR *hdr1 = (NMHDR*) lParam;
130 switch ( hdr1->code )
131 {
132 case NM_CLICK:
133 eventType = wxEVT_COMMAND_LEFT_CLICK;
134 break;
2bda0e17 135
a23fd0e1
VZ
136 case NM_DBLCLK:
137 eventType = wxEVT_COMMAND_LEFT_DCLICK;
138 break;
2bda0e17 139
a23fd0e1
VZ
140 case NM_RCLICK:
141 eventType = wxEVT_COMMAND_RIGHT_CLICK;
142 break;
2bda0e17 143
a23fd0e1
VZ
144 case NM_RDBLCLK:
145 eventType = wxEVT_COMMAND_RIGHT_DCLICK;
146 break;
2bda0e17 147
a23fd0e1
VZ
148 case NM_SETFOCUS:
149 eventType = wxEVT_COMMAND_SET_FOCUS;
150 break;
debe6624 151
a23fd0e1
VZ
152 case NM_KILLFOCUS:
153 eventType = wxEVT_COMMAND_KILL_FOCUS;
154 break;
2bda0e17 155
a23fd0e1
VZ
156 case NM_RETURN:
157 eventType = wxEVT_COMMAND_ENTER;
158 break;
159
160 default:
161 return wxWindow::MSWOnNotify(idCtrl, lParam, result);
162 }
fd3f686c 163
2bda0e17 164 event.SetEventType(eventType);
a23fd0e1 165 event.SetEventObject(this);
2bda0e17 166
a23fd0e1 167 return GetEventHandler()->ProcessEvent(event);
2bda0e17 168}
a23fd0e1 169#endif // Win95
2bda0e17 170
2bda0e17
KB
171void wxControl::ProcessCommand (wxCommandEvent & event)
172{
173 // Tries:
174 // 1) A callback function (to become obsolete)
175 // 2) OnCommand, starting at this window and working up parent hierarchy
176 // 3) OnCommand then calls ProcessEvent to search the event tables.
177 if (m_callback)
178 {
179 (void) (*(m_callback)) (*this, event);
180 }
181 else
182 {
183 GetEventHandler()->OnCommand(*this, event);
184 }
185}
186
187void wxControl::OnEraseBackground(wxEraseEvent& event)
188{
189 // In general, you don't want to erase the background of a control,
190 // or you'll get a flicker.
191 // TODO: move this 'null' function into each control that
192 // might flicker.
193
194 RECT rect;
195 ::GetClientRect((HWND) GetHWND(), &rect);
196
197 HBRUSH hBrush = ::CreateSolidBrush(PALETTERGB(GetBackgroundColour().Red(), GetBackgroundColour().Green(), GetBackgroundColour().Blue()));
198 int mode = ::SetMapMode((HDC) event.GetDC()->GetHDC(), MM_TEXT);
199
200 ::FillRect ((HDC) event.GetDC()->GetHDC(), &rect, hBrush);
201 ::DeleteObject(hBrush);
202 ::SetMapMode((HDC) event.GetDC()->GetHDC(), mode);
203}
204
debe6624 205void wxControl::SetClientSize (int width, int height)
2bda0e17
KB
206{
207 SetSize (-1, -1, width, height);
208}
209
debe6624 210void wxControl::Centre (int direction)
2bda0e17
KB
211{
212 int x, y, width, height, panel_width, panel_height, new_x, new_y;
213
214 wxWindow *parent = (wxWindow *) GetParent ();
215 if (!parent)
216 return;
217
218 parent->GetClientSize (&panel_width, &panel_height);
219 GetSize (&width, &height);
220 GetPosition (&x, &y);
221
222 new_x = x;
223 new_y = y;
224
225 if (direction & wxHORIZONTAL)
226 new_x = (int) ((panel_width - width) / 2);
227
228 if (direction & wxVERTICAL)
229 new_y = (int) ((panel_height - height) / 2);
230
231 SetSize (new_x, new_y, width, height);
232 int temp_x, temp_y;
233 GetPosition (&temp_x, &temp_y);
234 GetPosition (&temp_x, &temp_y);
235}
236
237