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