]>
Commit | Line | Data |
---|---|---|
2bda0e17 | 1 | ///////////////////////////////////////////////////////////////////////////// |
1e6feb95 | 2 | // Name: msw/control.cpp |
2bda0e17 KB |
3 | // Purpose: wxControl class |
4 | // Author: Julian Smart | |
5 | // Modified by: | |
6 | // Created: 01/02/97 | |
7 | // RCS-ID: $Id$ | |
6c9a19aa | 8 | // Copyright: (c) Julian Smart |
a23fd0e1 | 9 | // Licence: wxWindows licence |
2bda0e17 KB |
10 | ///////////////////////////////////////////////////////////////////////////// |
11 | ||
14f355c2 | 12 | #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA) |
1e6feb95 | 13 | #pragma implementation "control.h" |
2bda0e17 KB |
14 | #endif |
15 | ||
16 | // For compilers that support precompilation, includes "wx.h". | |
17 | #include "wx/wxprec.h" | |
18 | ||
19 | #ifdef __BORLANDC__ | |
1e6feb95 | 20 | #pragma hdrstop |
2bda0e17 KB |
21 | #endif |
22 | ||
1e6feb95 VZ |
23 | #if wxUSE_CONTROLS |
24 | ||
2bda0e17 | 25 | #ifndef WX_PRECOMP |
1e6feb95 VZ |
26 | #include "wx/event.h" |
27 | #include "wx/app.h" | |
28 | #include "wx/dcclient.h" | |
29 | #include "wx/log.h" | |
2bda0e17 KB |
30 | #endif |
31 | ||
2432b92d JS |
32 | #include "wx/control.h" |
33 | ||
2bda0e17 KB |
34 | #include "wx/msw/private.h" |
35 | ||
b39dbf34 | 36 | #if defined(__WIN95__) && !(defined(__GNUWIN32_OLD__) && !defined(__CYGWIN10__)) |
c42404a5 | 37 | #include <commctrl.h> |
2bda0e17 KB |
38 | #endif |
39 | ||
2bda0e17 KB |
40 | IMPLEMENT_ABSTRACT_CLASS(wxControl, wxWindow) |
41 | ||
42 | BEGIN_EVENT_TABLE(wxControl, wxWindow) | |
a23fd0e1 | 43 | EVT_ERASE_BACKGROUND(wxControl::OnEraseBackground) |
2bda0e17 | 44 | END_EVENT_TABLE() |
2bda0e17 KB |
45 | |
46 | // Item members | |
42e69d6b | 47 | wxControl::wxControl() |
2bda0e17 | 48 | { |
42e69d6b | 49 | #if WXWIN_COMPATIBILITY |
3f2711d5 | 50 | m_callback = 0; |
42e69d6b | 51 | #endif // WXWIN_COMPATIBILITY |
2bda0e17 KB |
52 | } |
53 | ||
42e69d6b | 54 | wxControl::~wxControl() |
2bda0e17 | 55 | { |
42e69d6b | 56 | m_isBeingDeleted = TRUE; |
2bda0e17 KB |
57 | } |
58 | ||
8d772832 | 59 | |
5b2f31eb VZ |
60 | bool wxControl::Create(wxWindow *parent, |
61 | wxWindowID id, | |
8d772832 | 62 | const wxPoint& pos, |
5b2f31eb VZ |
63 | const wxSize& size, |
64 | long style, | |
8d772832 | 65 | const wxValidator& validator, |
8d772832 RD |
66 | const wxString& name) |
67 | { | |
5b2f31eb VZ |
68 | if ( !wxWindow::Create(parent, id, pos, size, style, name) ) |
69 | return FALSE; | |
70 | ||
8d772832 | 71 | #if wxUSE_VALIDATORS |
5b2f31eb | 72 | SetValidator(validator); |
8d772832 | 73 | #endif |
5b2f31eb VZ |
74 | |
75 | return TRUE; | |
76 | } | |
77 | ||
78 | bool wxControl::MSWCreateControl(const wxChar *classname, | |
79 | const wxString& label, | |
80 | const wxPoint& pos, | |
6dd16e4f | 81 | const wxSize& size) |
5b2f31eb VZ |
82 | { |
83 | WXDWORD exstyle; | |
6dd16e4f | 84 | WXDWORD msStyle = MSWGetStyle(GetWindowStyle(), &exstyle); |
5b2f31eb | 85 | |
2eb4c3aa | 86 | return MSWCreateControl(classname, msStyle, pos, size, label, exstyle); |
8d772832 RD |
87 | } |
88 | ||
222594ea VZ |
89 | bool wxControl::MSWCreateControl(const wxChar *classname, |
90 | WXDWORD style, | |
91 | const wxPoint& pos, | |
92 | const wxSize& size, | |
93 | const wxString& label, | |
2eb4c3aa | 94 | WXDWORD exstyle) |
8d99be5f | 95 | { |
222594ea VZ |
96 | // if no extended style given, determine it ourselves |
97 | if ( exstyle == (WXDWORD)-1 ) | |
98 | { | |
fe3d9123 | 99 | exstyle = 0; |
65bc172c | 100 | (void) MSWGetStyle(GetWindowStyle(), &exstyle); |
222594ea VZ |
101 | } |
102 | ||
bdf5c30d VZ |
103 | // all controls should have this style |
104 | style |= WS_CHILD; | |
105 | ||
106 | // create the control visible if it's currently shown for wxWindows | |
107 | if ( m_isShown ) | |
108 | { | |
109 | style |= WS_VISIBLE; | |
110 | } | |
3f2711d5 | 111 | |
a63cbfa3 VZ |
112 | int x = pos.x == -1 ? 0 : pos.x, |
113 | y = pos.y == -1 ? 0 : pos.y, | |
114 | w = size.x == -1 ? 0 : size.x, | |
115 | h = size.y == -1 ? 0 : size.y; | |
116 | ||
8d99be5f VZ |
117 | m_hWnd = (WXHWND)::CreateWindowEx |
118 | ( | |
222594ea | 119 | exstyle, // extended style |
8d99be5f | 120 | classname, // the kind of control to create |
222594ea | 121 | label, // the window name |
8d99be5f | 122 | style, // the window style |
a63cbfa3 | 123 | x, y, w, h, // the window position and size |
8d99be5f VZ |
124 | GetHwndOf(GetParent()), // parent |
125 | (HMENU)GetId(), // child id | |
126 | wxGetInstance(), // app instance | |
127 | NULL // creation parameters | |
128 | ); | |
129 | ||
130 | if ( !m_hWnd ) | |
131 | { | |
f6bcfd97 BP |
132 | wxLogDebug(wxT("Failed to create a control of class '%s'"), classname); |
133 | wxFAIL_MSG(_T("something is very wrong")); | |
8d99be5f VZ |
134 | |
135 | return FALSE; | |
136 | } | |
137 | ||
3f2711d5 VZ |
138 | #if wxUSE_CTL3D |
139 | if ( want3D ) | |
140 | { | |
141 | Ctl3dSubclassCtl(GetHwnd()); | |
142 | m_useCtl3D = TRUE; | |
143 | } | |
144 | #endif // wxUSE_CTL3D | |
145 | ||
b225f659 | 146 | // install wxWindows window proc for this window |
8d99be5f VZ |
147 | SubclassWin(m_hWnd); |
148 | ||
149 | // controls use the same font and colours as their parent dialog by default | |
150 | InheritAttributes(); | |
151 | ||
a63cbfa3 | 152 | // set the size now if no initial size specified |
d45720c5 | 153 | if ( w <= 0 || h <= 0 ) |
a63cbfa3 VZ |
154 | { |
155 | SetBestSize(size); | |
156 | } | |
157 | ||
8d99be5f VZ |
158 | return TRUE; |
159 | } | |
160 | ||
65bc172c VZ |
161 | wxBorder wxControl::GetDefaultBorder() const |
162 | { | |
163 | // we want to automatically give controls a sunken style (confusingly, | |
164 | // it may not really mean sunken at all as we map it to WS_EX_CLIENTEDGE | |
165 | // which is not sunken at all under Windows XP -- rather, just the default) | |
166 | return wxBORDER_SUNKEN; | |
167 | } | |
168 | ||
f68586e5 | 169 | wxSize wxControl::DoGetBestSize() const |
4438caf4 VZ |
170 | { |
171 | return wxSize(DEFAULT_ITEM_WIDTH, DEFAULT_ITEM_HEIGHT); | |
172 | } | |
173 | ||
42e69d6b | 174 | bool wxControl::ProcessCommand(wxCommandEvent& event) |
2bda0e17 | 175 | { |
42e69d6b VZ |
176 | #if WXWIN_COMPATIBILITY |
177 | if ( m_callback ) | |
178 | { | |
d8ecfb85 | 179 | (void)(*m_callback)(*this, event); |
2bda0e17 | 180 | |
42e69d6b VZ |
181 | return TRUE; |
182 | } | |
183 | else | |
184 | #endif // WXWIN_COMPATIBILITY | |
2bda0e17 | 185 | |
42e69d6b | 186 | return GetEventHandler()->ProcessEvent(event); |
2bda0e17 KB |
187 | } |
188 | ||
a23fd0e1 VZ |
189 | #ifdef __WIN95__ |
190 | bool wxControl::MSWOnNotify(int idCtrl, | |
191 | WXLPARAM lParam, | |
192 | WXLPARAM* result) | |
2bda0e17 | 193 | { |
a23fd0e1 | 194 | wxEventType eventType = wxEVT_NULL; |
b6e5eaa5 VZ |
195 | |
196 | NMHDR *hdr = (NMHDR*) lParam; | |
197 | switch ( hdr->code ) | |
a23fd0e1 VZ |
198 | { |
199 | case NM_CLICK: | |
200 | eventType = wxEVT_COMMAND_LEFT_CLICK; | |
201 | break; | |
2bda0e17 | 202 | |
a23fd0e1 VZ |
203 | case NM_DBLCLK: |
204 | eventType = wxEVT_COMMAND_LEFT_DCLICK; | |
205 | break; | |
2bda0e17 | 206 | |
a23fd0e1 VZ |
207 | case NM_RCLICK: |
208 | eventType = wxEVT_COMMAND_RIGHT_CLICK; | |
209 | break; | |
2bda0e17 | 210 | |
a23fd0e1 VZ |
211 | case NM_RDBLCLK: |
212 | eventType = wxEVT_COMMAND_RIGHT_DCLICK; | |
213 | break; | |
2bda0e17 | 214 | |
a23fd0e1 VZ |
215 | case NM_SETFOCUS: |
216 | eventType = wxEVT_COMMAND_SET_FOCUS; | |
217 | break; | |
debe6624 | 218 | |
a23fd0e1 VZ |
219 | case NM_KILLFOCUS: |
220 | eventType = wxEVT_COMMAND_KILL_FOCUS; | |
221 | break; | |
2bda0e17 | 222 | |
a23fd0e1 VZ |
223 | case NM_RETURN: |
224 | eventType = wxEVT_COMMAND_ENTER; | |
225 | break; | |
226 | ||
227 | default: | |
228 | return wxWindow::MSWOnNotify(idCtrl, lParam, result); | |
229 | } | |
fd3f686c | 230 | |
b6e5eaa5 | 231 | wxCommandEvent event(wxEVT_NULL, m_windowId); |
2bda0e17 | 232 | event.SetEventType(eventType); |
a23fd0e1 | 233 | event.SetEventObject(this); |
2bda0e17 | 234 | |
a23fd0e1 | 235 | return GetEventHandler()->ProcessEvent(event); |
2bda0e17 | 236 | } |
a23fd0e1 | 237 | #endif // Win95 |
2bda0e17 | 238 | |
2bda0e17 KB |
239 | void wxControl::OnEraseBackground(wxEraseEvent& event) |
240 | { | |
5bd3a2da VZ |
241 | // notice that this 'dumb' implementation may cause flicker for some of the |
242 | // controls in which case they should intercept wxEraseEvent and process it | |
243 | // themselves somehow | |
42e69d6b VZ |
244 | |
245 | RECT rect; | |
3f2711d5 | 246 | ::GetClientRect(GetHwnd(), &rect); |
42e69d6b | 247 | |
3f2711d5 | 248 | HBRUSH hBrush = ::CreateSolidBrush(wxColourToRGB(GetBackgroundColour())); |
42e69d6b | 249 | |
3f2711d5 | 250 | HDC hdc = GetHdcOf((*event.GetDC())); |
4676948b JS |
251 | |
252 | #ifndef __WXWINCE__ | |
3f2711d5 | 253 | int mode = ::SetMapMode(hdc, MM_TEXT); |
4676948b | 254 | #endif |
3f2711d5 VZ |
255 | |
256 | ::FillRect(hdc, &rect, hBrush); | |
42e69d6b | 257 | ::DeleteObject(hBrush); |
4676948b JS |
258 | |
259 | #ifndef __WXWINCE__ | |
3f2711d5 | 260 | ::SetMapMode(hdc, mode); |
4676948b | 261 | #endif |
2bda0e17 KB |
262 | } |
263 | ||
33ac7e6f | 264 | WXHBRUSH wxControl::OnCtlColor(WXHDC pDC, WXHWND WXUNUSED(pWnd), WXUINT WXUNUSED(nCtlColor), |
788722ac JS |
265 | #if wxUSE_CTL3D |
266 | WXUINT message, | |
267 | WXWPARAM wParam, | |
268 | WXLPARAM lParam | |
269 | #else | |
33ac7e6f KB |
270 | WXUINT WXUNUSED(message), |
271 | WXWPARAM WXUNUSED(wParam), | |
788722ac JS |
272 | WXLPARAM WXUNUSED(lParam) |
273 | #endif | |
274 | ) | |
f048e32f VZ |
275 | { |
276 | #if wxUSE_CTL3D | |
277 | if ( m_useCtl3D ) | |
278 | { | |
279 | HBRUSH hbrush = Ctl3dCtlColorEx(message, wParam, lParam); | |
280 | return (WXHBRUSH) hbrush; | |
281 | } | |
282 | #endif // wxUSE_CTL3D | |
283 | ||
284 | HDC hdc = (HDC)pDC; | |
f6bcfd97 BP |
285 | wxColour colBack = GetBackgroundColour(); |
286 | ||
f048e32f VZ |
287 | ::SetBkColor(hdc, wxColourToRGB(colBack)); |
288 | ::SetTextColor(hdc, wxColourToRGB(GetForegroundColour())); | |
289 | ||
290 | wxBrush *brush = wxTheBrushList->FindOrCreateBrush(colBack, wxSOLID); | |
291 | ||
292 | return (WXHBRUSH)brush->GetResourceHandle(); | |
293 | } | |
294 | ||
5b2f31eb | 295 | WXDWORD wxControl::MSWGetStyle(long style, WXDWORD *exstyle) const |
8d99be5f | 296 | { |
5b2f31eb | 297 | long msStyle = wxWindow::MSWGetStyle(style, exstyle); |
8d99be5f | 298 | |
5b2f31eb VZ |
299 | if ( AcceptsFocus() ) |
300 | { | |
301 | msStyle |= WS_TABSTOP; | |
302 | } | |
8d99be5f | 303 | |
5b2f31eb | 304 | return msStyle; |
8d99be5f VZ |
305 | } |
306 | ||
42e69d6b VZ |
307 | // --------------------------------------------------------------------------- |
308 | // global functions | |
309 | // --------------------------------------------------------------------------- | |
2bda0e17 | 310 | |
42e69d6b VZ |
311 | // Call this repeatedly for several wnds to find the overall size |
312 | // of the widget. | |
313 | // Call it initially with -1 for all values in rect. | |
314 | // Keep calling for other widgets, and rect will be modified | |
315 | // to calculate largest bounding rectangle. | |
316 | void wxFindMaxSize(WXHWND wnd, RECT *rect) | |
2bda0e17 | 317 | { |
42e69d6b VZ |
318 | int left = rect->left; |
319 | int right = rect->right; | |
320 | int top = rect->top; | |
321 | int bottom = rect->bottom; | |
2bda0e17 | 322 | |
42e69d6b | 323 | GetWindowRect((HWND) wnd, rect); |
2bda0e17 | 324 | |
42e69d6b VZ |
325 | if (left < 0) |
326 | return; | |
2bda0e17 | 327 | |
42e69d6b VZ |
328 | if (left < rect->left) |
329 | rect->left = left; | |
2bda0e17 | 330 | |
42e69d6b VZ |
331 | if (right > rect->right) |
332 | rect->right = right; | |
2bda0e17 | 333 | |
42e69d6b VZ |
334 | if (top < rect->top) |
335 | rect->top = top; | |
2bda0e17 | 336 | |
42e69d6b VZ |
337 | if (bottom > rect->bottom) |
338 | rect->bottom = bottom; | |
2bda0e17 KB |
339 | } |
340 | ||
1e6feb95 | 341 | #endif // wxUSE_CONTROLS |