]>
Commit | Line | Data |
---|---|---|
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" | |
f94dfb38 | 27 | #include "wx/log.h" |
2bda0e17 KB |
28 | #endif |
29 | ||
2432b92d JS |
30 | #include "wx/control.h" |
31 | ||
2bda0e17 KB |
32 | #include "wx/msw/private.h" |
33 | ||
c42404a5 VZ |
34 | #if defined(__WIN95__) && !(defined(__GNUWIN32_OLD__) || defined(__TWIN32__)) |
35 | #include <commctrl.h> | |
2bda0e17 KB |
36 | #endif |
37 | ||
2bda0e17 KB |
38 | IMPLEMENT_ABSTRACT_CLASS(wxControl, wxWindow) |
39 | ||
40 | BEGIN_EVENT_TABLE(wxControl, wxWindow) | |
a23fd0e1 | 41 | EVT_ERASE_BACKGROUND(wxControl::OnEraseBackground) |
2bda0e17 | 42 | END_EVENT_TABLE() |
2bda0e17 KB |
43 | |
44 | // Item members | |
42e69d6b | 45 | wxControl::wxControl() |
2bda0e17 | 46 | { |
3f2711d5 VZ |
47 | m_backgroundColour = *wxWHITE; |
48 | m_foregroundColour = *wxBLACK; | |
42e69d6b VZ |
49 | |
50 | #if WXWIN_COMPATIBILITY | |
3f2711d5 | 51 | m_callback = 0; |
42e69d6b | 52 | #endif // WXWIN_COMPATIBILITY |
2bda0e17 KB |
53 | } |
54 | ||
42e69d6b | 55 | wxControl::~wxControl() |
2bda0e17 | 56 | { |
42e69d6b | 57 | m_isBeingDeleted = TRUE; |
2bda0e17 KB |
58 | } |
59 | ||
8d772832 RD |
60 | |
61 | bool wxControl::Create(wxWindow *parent, wxWindowID id, | |
62 | const wxPoint& pos, | |
63 | const wxSize& size, long style, | |
8d772832 | 64 | const wxValidator& validator, |
8d772832 RD |
65 | const wxString& name) |
66 | { | |
67 | bool rval = wxWindow::Create(parent, id, pos, size, style, name); | |
68 | if (rval) { | |
69 | #if wxUSE_VALIDATORS | |
70 | SetValidator(validator); | |
71 | #endif | |
72 | } | |
73 | return rval; | |
74 | } | |
75 | ||
222594ea VZ |
76 | bool wxControl::MSWCreateControl(const wxChar *classname, |
77 | WXDWORD style, | |
78 | const wxPoint& pos, | |
79 | const wxSize& size, | |
80 | const wxString& label, | |
81 | WXDWORD exstyle) | |
8d99be5f | 82 | { |
f6bcfd97 BP |
83 | // want3D tells us whether or not the style specified a 3D border. |
84 | // If so, under WIN16 we can use Ctl3D to give it an appropriate style. | |
85 | // Sometimes want3D is used to indicate that the non-extended style should have | |
86 | // WS_BORDER. | |
87 | bool want3D = TRUE; | |
3f2711d5 | 88 | |
222594ea VZ |
89 | // if no extended style given, determine it ourselves |
90 | if ( exstyle == (WXDWORD)-1 ) | |
91 | { | |
92 | exstyle = GetExStyle(style, &want3D); | |
93 | } | |
94 | ||
f6bcfd97 | 95 | // all controls have these styles (wxWindows creates all controls visible |
3f2711d5 VZ |
96 | // by default) |
97 | style |= WS_CHILD | WS_VISIBLE; | |
98 | ||
8d99be5f VZ |
99 | m_hWnd = (WXHWND)::CreateWindowEx |
100 | ( | |
222594ea | 101 | exstyle, // extended style |
8d99be5f | 102 | classname, // the kind of control to create |
222594ea | 103 | label, // the window name |
8d99be5f | 104 | style, // the window style |
222594ea VZ |
105 | pos.x, pos.y, // the window position |
106 | size.x, size.y, // and size | |
8d99be5f VZ |
107 | GetHwndOf(GetParent()), // parent |
108 | (HMENU)GetId(), // child id | |
109 | wxGetInstance(), // app instance | |
110 | NULL // creation parameters | |
111 | ); | |
112 | ||
113 | if ( !m_hWnd ) | |
114 | { | |
f6bcfd97 BP |
115 | wxLogDebug(wxT("Failed to create a control of class '%s'"), classname); |
116 | wxFAIL_MSG(_T("something is very wrong")); | |
8d99be5f VZ |
117 | |
118 | return FALSE; | |
119 | } | |
120 | ||
3f2711d5 VZ |
121 | #if wxUSE_CTL3D |
122 | if ( want3D ) | |
123 | { | |
124 | Ctl3dSubclassCtl(GetHwnd()); | |
125 | m_useCtl3D = TRUE; | |
126 | } | |
127 | #endif // wxUSE_CTL3D | |
128 | ||
8d99be5f VZ |
129 | // subclass again for purposes of dialog editing mode |
130 | SubclassWin(m_hWnd); | |
131 | ||
132 | // controls use the same font and colours as their parent dialog by default | |
133 | InheritAttributes(); | |
134 | ||
135 | return TRUE; | |
136 | } | |
137 | ||
f68586e5 | 138 | wxSize wxControl::DoGetBestSize() const |
4438caf4 VZ |
139 | { |
140 | return wxSize(DEFAULT_ITEM_WIDTH, DEFAULT_ITEM_HEIGHT); | |
141 | } | |
142 | ||
42e69d6b | 143 | bool wxControl::ProcessCommand(wxCommandEvent& event) |
2bda0e17 | 144 | { |
42e69d6b VZ |
145 | #if WXWIN_COMPATIBILITY |
146 | if ( m_callback ) | |
147 | { | |
d8ecfb85 | 148 | (void)(*m_callback)(*this, event); |
2bda0e17 | 149 | |
42e69d6b VZ |
150 | return TRUE; |
151 | } | |
152 | else | |
153 | #endif // WXWIN_COMPATIBILITY | |
2bda0e17 | 154 | |
42e69d6b | 155 | return GetEventHandler()->ProcessEvent(event); |
2bda0e17 KB |
156 | } |
157 | ||
a23fd0e1 VZ |
158 | #ifdef __WIN95__ |
159 | bool wxControl::MSWOnNotify(int idCtrl, | |
160 | WXLPARAM lParam, | |
161 | WXLPARAM* result) | |
2bda0e17 | 162 | { |
a23fd0e1 VZ |
163 | wxCommandEvent event(wxEVT_NULL, m_windowId); |
164 | wxEventType eventType = wxEVT_NULL; | |
165 | NMHDR *hdr1 = (NMHDR*) lParam; | |
166 | switch ( hdr1->code ) | |
167 | { | |
168 | case NM_CLICK: | |
169 | eventType = wxEVT_COMMAND_LEFT_CLICK; | |
170 | break; | |
2bda0e17 | 171 | |
a23fd0e1 VZ |
172 | case NM_DBLCLK: |
173 | eventType = wxEVT_COMMAND_LEFT_DCLICK; | |
174 | break; | |
2bda0e17 | 175 | |
a23fd0e1 VZ |
176 | case NM_RCLICK: |
177 | eventType = wxEVT_COMMAND_RIGHT_CLICK; | |
178 | break; | |
2bda0e17 | 179 | |
a23fd0e1 VZ |
180 | case NM_RDBLCLK: |
181 | eventType = wxEVT_COMMAND_RIGHT_DCLICK; | |
182 | break; | |
2bda0e17 | 183 | |
a23fd0e1 VZ |
184 | case NM_SETFOCUS: |
185 | eventType = wxEVT_COMMAND_SET_FOCUS; | |
186 | break; | |
debe6624 | 187 | |
a23fd0e1 VZ |
188 | case NM_KILLFOCUS: |
189 | eventType = wxEVT_COMMAND_KILL_FOCUS; | |
190 | break; | |
2bda0e17 | 191 | |
a23fd0e1 VZ |
192 | case NM_RETURN: |
193 | eventType = wxEVT_COMMAND_ENTER; | |
194 | break; | |
195 | ||
196 | default: | |
197 | return wxWindow::MSWOnNotify(idCtrl, lParam, result); | |
198 | } | |
fd3f686c | 199 | |
2bda0e17 | 200 | event.SetEventType(eventType); |
a23fd0e1 | 201 | event.SetEventObject(this); |
2bda0e17 | 202 | |
a23fd0e1 | 203 | return GetEventHandler()->ProcessEvent(event); |
2bda0e17 | 204 | } |
a23fd0e1 | 205 | #endif // Win95 |
2bda0e17 | 206 | |
2bda0e17 KB |
207 | void wxControl::OnEraseBackground(wxEraseEvent& event) |
208 | { | |
5bd3a2da VZ |
209 | // notice that this 'dumb' implementation may cause flicker for some of the |
210 | // controls in which case they should intercept wxEraseEvent and process it | |
211 | // themselves somehow | |
42e69d6b VZ |
212 | |
213 | RECT rect; | |
3f2711d5 | 214 | ::GetClientRect(GetHwnd(), &rect); |
42e69d6b | 215 | |
3f2711d5 | 216 | HBRUSH hBrush = ::CreateSolidBrush(wxColourToRGB(GetBackgroundColour())); |
42e69d6b | 217 | |
3f2711d5 VZ |
218 | HDC hdc = GetHdcOf((*event.GetDC())); |
219 | int mode = ::SetMapMode(hdc, MM_TEXT); | |
220 | ||
221 | ::FillRect(hdc, &rect, hBrush); | |
42e69d6b | 222 | ::DeleteObject(hBrush); |
3f2711d5 | 223 | ::SetMapMode(hdc, mode); |
2bda0e17 KB |
224 | } |
225 | ||
33ac7e6f | 226 | WXHBRUSH wxControl::OnCtlColor(WXHDC pDC, WXHWND WXUNUSED(pWnd), WXUINT WXUNUSED(nCtlColor), |
788722ac JS |
227 | #if wxUSE_CTL3D |
228 | WXUINT message, | |
229 | WXWPARAM wParam, | |
230 | WXLPARAM lParam | |
231 | #else | |
33ac7e6f KB |
232 | WXUINT WXUNUSED(message), |
233 | WXWPARAM WXUNUSED(wParam), | |
788722ac JS |
234 | WXLPARAM WXUNUSED(lParam) |
235 | #endif | |
236 | ) | |
f048e32f VZ |
237 | { |
238 | #if wxUSE_CTL3D | |
239 | if ( m_useCtl3D ) | |
240 | { | |
241 | HBRUSH hbrush = Ctl3dCtlColorEx(message, wParam, lParam); | |
242 | return (WXHBRUSH) hbrush; | |
243 | } | |
244 | #endif // wxUSE_CTL3D | |
245 | ||
246 | HDC hdc = (HDC)pDC; | |
247 | if (GetParent()->GetTransparentBackground()) | |
248 | SetBkMode(hdc, TRANSPARENT); | |
249 | else | |
250 | SetBkMode(hdc, OPAQUE); | |
251 | ||
f6bcfd97 BP |
252 | wxColour colBack = GetBackgroundColour(); |
253 | ||
f048e32f VZ |
254 | ::SetBkColor(hdc, wxColourToRGB(colBack)); |
255 | ::SetTextColor(hdc, wxColourToRGB(GetForegroundColour())); | |
256 | ||
257 | wxBrush *brush = wxTheBrushList->FindOrCreateBrush(colBack, wxSOLID); | |
258 | ||
259 | return (WXHBRUSH)brush->GetResourceHandle(); | |
260 | } | |
261 | ||
3f2711d5 | 262 | WXDWORD wxControl::GetExStyle(WXDWORD& style, bool *want3D) const |
8d99be5f | 263 | { |
3f2711d5 | 264 | WXDWORD exStyle = Determine3DEffects(WS_EX_CLIENTEDGE, want3D); |
8d99be5f | 265 | |
3f2711d5 VZ |
266 | // Even with extended styles, need to combine with WS_BORDER for them to |
267 | // look right. | |
268 | if ( *want3D || wxStyleHasBorder(m_windowStyle) ) | |
8d99be5f VZ |
269 | style |= WS_BORDER; |
270 | ||
271 | return exStyle; | |
272 | } | |
273 | ||
42e69d6b VZ |
274 | // --------------------------------------------------------------------------- |
275 | // global functions | |
276 | // --------------------------------------------------------------------------- | |
2bda0e17 | 277 | |
42e69d6b VZ |
278 | // Call this repeatedly for several wnds to find the overall size |
279 | // of the widget. | |
280 | // Call it initially with -1 for all values in rect. | |
281 | // Keep calling for other widgets, and rect will be modified | |
282 | // to calculate largest bounding rectangle. | |
283 | void wxFindMaxSize(WXHWND wnd, RECT *rect) | |
2bda0e17 | 284 | { |
42e69d6b VZ |
285 | int left = rect->left; |
286 | int right = rect->right; | |
287 | int top = rect->top; | |
288 | int bottom = rect->bottom; | |
2bda0e17 | 289 | |
42e69d6b | 290 | GetWindowRect((HWND) wnd, rect); |
2bda0e17 | 291 | |
42e69d6b VZ |
292 | if (left < 0) |
293 | return; | |
2bda0e17 | 294 | |
42e69d6b VZ |
295 | if (left < rect->left) |
296 | rect->left = left; | |
2bda0e17 | 297 | |
42e69d6b VZ |
298 | if (right > rect->right) |
299 | rect->right = right; | |
2bda0e17 | 300 | |
42e69d6b VZ |
301 | if (top < rect->top) |
302 | rect->top = top; | |
2bda0e17 | 303 | |
42e69d6b VZ |
304 | if (bottom > rect->bottom) |
305 | rect->bottom = bottom; | |
2bda0e17 KB |
306 | } |
307 |