]>
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$ | |
8 | // Copyright: (c) Julian Smart and Markus Holzem | |
a23fd0e1 | 9 | // Licence: wxWindows licence |
2bda0e17 KB |
10 | ///////////////////////////////////////////////////////////////////////////// |
11 | ||
12 | #ifdef __GNUG__ | |
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 | ||
ae090fdb | 36 | #if defined(__WIN95__) && !((defined(__GNUWIN32_OLD__) || defined(__TWIN32__)) && !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, | |
81 | const wxSize& size, | |
2eb4c3aa | 82 | long style) |
5b2f31eb VZ |
83 | { |
84 | WXDWORD exstyle; | |
85 | WXDWORD msStyle = MSWGetStyle(style, &exstyle); | |
86 | ||
2eb4c3aa | 87 | return MSWCreateControl(classname, msStyle, pos, size, label, exstyle); |
8d772832 RD |
88 | } |
89 | ||
222594ea VZ |
90 | bool wxControl::MSWCreateControl(const wxChar *classname, |
91 | WXDWORD style, | |
92 | const wxPoint& pos, | |
93 | const wxSize& size, | |
94 | const wxString& label, | |
2eb4c3aa | 95 | WXDWORD exstyle) |
8d99be5f | 96 | { |
f6bcfd97 BP |
97 | // want3D tells us whether or not the style specified a 3D border. |
98 | // If so, under WIN16 we can use Ctl3D to give it an appropriate style. | |
99 | // Sometimes want3D is used to indicate that the non-extended style should have | |
100 | // WS_BORDER. | |
101 | bool want3D = TRUE; | |
3f2711d5 | 102 | |
222594ea VZ |
103 | // if no extended style given, determine it ourselves |
104 | if ( exstyle == (WXDWORD)-1 ) | |
105 | { | |
5b2f31eb | 106 | exstyle = Determine3DEffects(WS_EX_CLIENTEDGE, &want3D); |
222594ea VZ |
107 | } |
108 | ||
bdf5c30d VZ |
109 | // all controls should have this style |
110 | style |= WS_CHILD; | |
111 | ||
112 | // create the control visible if it's currently shown for wxWindows | |
113 | if ( m_isShown ) | |
114 | { | |
115 | style |= WS_VISIBLE; | |
116 | } | |
3f2711d5 | 117 | |
a63cbfa3 VZ |
118 | int x = pos.x == -1 ? 0 : pos.x, |
119 | y = pos.y == -1 ? 0 : pos.y, | |
120 | w = size.x == -1 ? 0 : size.x, | |
121 | h = size.y == -1 ? 0 : size.y; | |
122 | ||
8d99be5f VZ |
123 | m_hWnd = (WXHWND)::CreateWindowEx |
124 | ( | |
222594ea | 125 | exstyle, // extended style |
8d99be5f | 126 | classname, // the kind of control to create |
222594ea | 127 | label, // the window name |
8d99be5f | 128 | style, // the window style |
a63cbfa3 | 129 | x, y, w, h, // the window position and size |
8d99be5f VZ |
130 | GetHwndOf(GetParent()), // parent |
131 | (HMENU)GetId(), // child id | |
132 | wxGetInstance(), // app instance | |
133 | NULL // creation parameters | |
134 | ); | |
135 | ||
136 | if ( !m_hWnd ) | |
137 | { | |
f6bcfd97 BP |
138 | wxLogDebug(wxT("Failed to create a control of class '%s'"), classname); |
139 | wxFAIL_MSG(_T("something is very wrong")); | |
8d99be5f VZ |
140 | |
141 | return FALSE; | |
142 | } | |
143 | ||
3f2711d5 VZ |
144 | #if wxUSE_CTL3D |
145 | if ( want3D ) | |
146 | { | |
147 | Ctl3dSubclassCtl(GetHwnd()); | |
148 | m_useCtl3D = TRUE; | |
149 | } | |
150 | #endif // wxUSE_CTL3D | |
151 | ||
b225f659 | 152 | // install wxWindows window proc for this window |
8d99be5f VZ |
153 | SubclassWin(m_hWnd); |
154 | ||
155 | // controls use the same font and colours as their parent dialog by default | |
156 | InheritAttributes(); | |
157 | ||
a63cbfa3 | 158 | // set the size now if no initial size specified |
d45720c5 | 159 | if ( w <= 0 || h <= 0 ) |
a63cbfa3 VZ |
160 | { |
161 | SetBestSize(size); | |
162 | } | |
163 | ||
8d99be5f VZ |
164 | return TRUE; |
165 | } | |
166 | ||
f68586e5 | 167 | wxSize wxControl::DoGetBestSize() const |
4438caf4 VZ |
168 | { |
169 | return wxSize(DEFAULT_ITEM_WIDTH, DEFAULT_ITEM_HEIGHT); | |
170 | } | |
171 | ||
42e69d6b | 172 | bool wxControl::ProcessCommand(wxCommandEvent& event) |
2bda0e17 | 173 | { |
42e69d6b VZ |
174 | #if WXWIN_COMPATIBILITY |
175 | if ( m_callback ) | |
176 | { | |
d8ecfb85 | 177 | (void)(*m_callback)(*this, event); |
2bda0e17 | 178 | |
42e69d6b VZ |
179 | return TRUE; |
180 | } | |
181 | else | |
182 | #endif // WXWIN_COMPATIBILITY | |
2bda0e17 | 183 | |
42e69d6b | 184 | return GetEventHandler()->ProcessEvent(event); |
2bda0e17 KB |
185 | } |
186 | ||
a23fd0e1 VZ |
187 | #ifdef __WIN95__ |
188 | bool wxControl::MSWOnNotify(int idCtrl, | |
189 | WXLPARAM lParam, | |
190 | WXLPARAM* result) | |
2bda0e17 | 191 | { |
a23fd0e1 | 192 | wxEventType eventType = wxEVT_NULL; |
b6e5eaa5 VZ |
193 | |
194 | NMHDR *hdr = (NMHDR*) lParam; | |
195 | switch ( hdr->code ) | |
a23fd0e1 VZ |
196 | { |
197 | case NM_CLICK: | |
198 | eventType = wxEVT_COMMAND_LEFT_CLICK; | |
199 | break; | |
2bda0e17 | 200 | |
a23fd0e1 VZ |
201 | case NM_DBLCLK: |
202 | eventType = wxEVT_COMMAND_LEFT_DCLICK; | |
203 | break; | |
2bda0e17 | 204 | |
a23fd0e1 VZ |
205 | case NM_RCLICK: |
206 | eventType = wxEVT_COMMAND_RIGHT_CLICK; | |
207 | break; | |
2bda0e17 | 208 | |
a23fd0e1 VZ |
209 | case NM_RDBLCLK: |
210 | eventType = wxEVT_COMMAND_RIGHT_DCLICK; | |
211 | break; | |
2bda0e17 | 212 | |
a23fd0e1 VZ |
213 | case NM_SETFOCUS: |
214 | eventType = wxEVT_COMMAND_SET_FOCUS; | |
215 | break; | |
debe6624 | 216 | |
a23fd0e1 VZ |
217 | case NM_KILLFOCUS: |
218 | eventType = wxEVT_COMMAND_KILL_FOCUS; | |
219 | break; | |
2bda0e17 | 220 | |
a23fd0e1 VZ |
221 | case NM_RETURN: |
222 | eventType = wxEVT_COMMAND_ENTER; | |
223 | break; | |
224 | ||
225 | default: | |
226 | return wxWindow::MSWOnNotify(idCtrl, lParam, result); | |
227 | } | |
fd3f686c | 228 | |
b6e5eaa5 | 229 | wxCommandEvent event(wxEVT_NULL, m_windowId); |
2bda0e17 | 230 | event.SetEventType(eventType); |
a23fd0e1 | 231 | event.SetEventObject(this); |
2bda0e17 | 232 | |
a23fd0e1 | 233 | return GetEventHandler()->ProcessEvent(event); |
2bda0e17 | 234 | } |
a23fd0e1 | 235 | #endif // Win95 |
2bda0e17 | 236 | |
2bda0e17 KB |
237 | void wxControl::OnEraseBackground(wxEraseEvent& event) |
238 | { | |
5bd3a2da VZ |
239 | // notice that this 'dumb' implementation may cause flicker for some of the |
240 | // controls in which case they should intercept wxEraseEvent and process it | |
241 | // themselves somehow | |
42e69d6b VZ |
242 | |
243 | RECT rect; | |
3f2711d5 | 244 | ::GetClientRect(GetHwnd(), &rect); |
42e69d6b | 245 | |
3f2711d5 | 246 | HBRUSH hBrush = ::CreateSolidBrush(wxColourToRGB(GetBackgroundColour())); |
42e69d6b | 247 | |
3f2711d5 VZ |
248 | HDC hdc = GetHdcOf((*event.GetDC())); |
249 | int mode = ::SetMapMode(hdc, MM_TEXT); | |
250 | ||
251 | ::FillRect(hdc, &rect, hBrush); | |
42e69d6b | 252 | ::DeleteObject(hBrush); |
3f2711d5 | 253 | ::SetMapMode(hdc, mode); |
2bda0e17 KB |
254 | } |
255 | ||
33ac7e6f | 256 | WXHBRUSH wxControl::OnCtlColor(WXHDC pDC, WXHWND WXUNUSED(pWnd), WXUINT WXUNUSED(nCtlColor), |
788722ac JS |
257 | #if wxUSE_CTL3D |
258 | WXUINT message, | |
259 | WXWPARAM wParam, | |
260 | WXLPARAM lParam | |
261 | #else | |
33ac7e6f KB |
262 | WXUINT WXUNUSED(message), |
263 | WXWPARAM WXUNUSED(wParam), | |
788722ac JS |
264 | WXLPARAM WXUNUSED(lParam) |
265 | #endif | |
266 | ) | |
f048e32f VZ |
267 | { |
268 | #if wxUSE_CTL3D | |
269 | if ( m_useCtl3D ) | |
270 | { | |
271 | HBRUSH hbrush = Ctl3dCtlColorEx(message, wParam, lParam); | |
272 | return (WXHBRUSH) hbrush; | |
273 | } | |
274 | #endif // wxUSE_CTL3D | |
275 | ||
276 | HDC hdc = (HDC)pDC; | |
277 | if (GetParent()->GetTransparentBackground()) | |
278 | SetBkMode(hdc, TRANSPARENT); | |
279 | else | |
280 | SetBkMode(hdc, OPAQUE); | |
281 | ||
f6bcfd97 BP |
282 | wxColour colBack = GetBackgroundColour(); |
283 | ||
f048e32f VZ |
284 | ::SetBkColor(hdc, wxColourToRGB(colBack)); |
285 | ::SetTextColor(hdc, wxColourToRGB(GetForegroundColour())); | |
286 | ||
287 | wxBrush *brush = wxTheBrushList->FindOrCreateBrush(colBack, wxSOLID); | |
288 | ||
289 | return (WXHBRUSH)brush->GetResourceHandle(); | |
290 | } | |
291 | ||
5b2f31eb | 292 | WXDWORD wxControl::MSWGetStyle(long style, WXDWORD *exstyle) const |
8d99be5f | 293 | { |
5b2f31eb | 294 | long msStyle = wxWindow::MSWGetStyle(style, exstyle); |
8d99be5f | 295 | |
5b2f31eb VZ |
296 | if ( AcceptsFocus() ) |
297 | { | |
298 | msStyle |= WS_TABSTOP; | |
299 | } | |
8d99be5f | 300 | |
5b2f31eb | 301 | return msStyle; |
8d99be5f VZ |
302 | } |
303 | ||
42e69d6b VZ |
304 | // --------------------------------------------------------------------------- |
305 | // global functions | |
306 | // --------------------------------------------------------------------------- | |
2bda0e17 | 307 | |
42e69d6b VZ |
308 | // Call this repeatedly for several wnds to find the overall size |
309 | // of the widget. | |
310 | // Call it initially with -1 for all values in rect. | |
311 | // Keep calling for other widgets, and rect will be modified | |
312 | // to calculate largest bounding rectangle. | |
313 | void wxFindMaxSize(WXHWND wnd, RECT *rect) | |
2bda0e17 | 314 | { |
42e69d6b VZ |
315 | int left = rect->left; |
316 | int right = rect->right; | |
317 | int top = rect->top; | |
318 | int bottom = rect->bottom; | |
2bda0e17 | 319 | |
42e69d6b | 320 | GetWindowRect((HWND) wnd, rect); |
2bda0e17 | 321 | |
42e69d6b VZ |
322 | if (left < 0) |
323 | return; | |
2bda0e17 | 324 | |
42e69d6b VZ |
325 | if (left < rect->left) |
326 | rect->left = left; | |
2bda0e17 | 327 | |
42e69d6b VZ |
328 | if (right > rect->right) |
329 | rect->right = right; | |
2bda0e17 | 330 | |
42e69d6b VZ |
331 | if (top < rect->top) |
332 | rect->top = top; | |
2bda0e17 | 333 | |
42e69d6b VZ |
334 | if (bottom > rect->bottom) |
335 | rect->bottom = bottom; | |
2bda0e17 KB |
336 | } |
337 | ||
1e6feb95 | 338 | #endif // wxUSE_CONTROLS |