]>
Commit | Line | Data |
---|---|---|
2bda0e17 | 1 | ///////////////////////////////////////////////////////////////////////////// |
cd0b1709 | 2 | // Name: msw/button.cpp |
2bda0e17 KB |
3 | // Purpose: wxButton |
4 | // Author: Julian Smart | |
5 | // Modified by: | |
6 | // Created: 04/01/98 | |
7 | // RCS-ID: $Id$ | |
6c9a19aa JS |
8 | // Copyright: (c) Julian Smart |
9 | // Licence: wxWindows licence | |
2bda0e17 KB |
10 | ///////////////////////////////////////////////////////////////////////////// |
11 | ||
edccf428 VZ |
12 | // ============================================================================ |
13 | // declarations | |
14 | // ============================================================================ | |
15 | ||
16 | // ---------------------------------------------------------------------------- | |
17 | // headers | |
18 | // ---------------------------------------------------------------------------- | |
cd0b1709 | 19 | |
14f355c2 | 20 | #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA) |
edccf428 | 21 | #pragma implementation "button.h" |
2bda0e17 KB |
22 | #endif |
23 | ||
24 | // For compilers that support precompilation, includes "wx.h". | |
25 | #include "wx/wxprec.h" | |
26 | ||
27 | #ifdef __BORLANDC__ | |
edccf428 | 28 | #pragma hdrstop |
2bda0e17 KB |
29 | #endif |
30 | ||
1e6feb95 VZ |
31 | #if wxUSE_BUTTON |
32 | ||
2bda0e17 | 33 | #ifndef WX_PRECOMP |
bac409a0 | 34 | #include "wx/app.h" |
edccf428 VZ |
35 | #include "wx/button.h" |
36 | #include "wx/brush.h" | |
4e938f5b | 37 | #include "wx/panel.h" |
8a4df159 | 38 | #include "wx/bmpbuttn.h" |
fb39c7ec RR |
39 | #include "wx/settings.h" |
40 | #include "wx/dcscreen.h" | |
2bda0e17 KB |
41 | #endif |
42 | ||
43 | #include "wx/msw/private.h" | |
44 | ||
edccf428 VZ |
45 | // ---------------------------------------------------------------------------- |
46 | // macros | |
47 | // ---------------------------------------------------------------------------- | |
48 | ||
51596bcb | 49 | #if wxUSE_EXTENDED_RTTI |
067e9be6 | 50 | |
f3291a82 SC |
51 | WX_DEFINE_FLAGS( wxButtonStyle ) |
52 | ||
3ff066a4 | 53 | wxBEGIN_FLAGS( wxButtonStyle ) |
f3291a82 SC |
54 | // new style border flags, we put them first to |
55 | // use them for streaming out | |
3ff066a4 SC |
56 | wxFLAGS_MEMBER(wxBORDER_SIMPLE) |
57 | wxFLAGS_MEMBER(wxBORDER_SUNKEN) | |
58 | wxFLAGS_MEMBER(wxBORDER_DOUBLE) | |
59 | wxFLAGS_MEMBER(wxBORDER_RAISED) | |
60 | wxFLAGS_MEMBER(wxBORDER_STATIC) | |
61 | wxFLAGS_MEMBER(wxBORDER_NONE) | |
f3291a82 SC |
62 | |
63 | // old style border flags | |
3ff066a4 SC |
64 | wxFLAGS_MEMBER(wxSIMPLE_BORDER) |
65 | wxFLAGS_MEMBER(wxSUNKEN_BORDER) | |
66 | wxFLAGS_MEMBER(wxDOUBLE_BORDER) | |
67 | wxFLAGS_MEMBER(wxRAISED_BORDER) | |
68 | wxFLAGS_MEMBER(wxSTATIC_BORDER) | |
cb0afb26 | 69 | wxFLAGS_MEMBER(wxBORDER) |
f3291a82 | 70 | |
bc9fb572 | 71 | // standard window styles |
3ff066a4 SC |
72 | wxFLAGS_MEMBER(wxTAB_TRAVERSAL) |
73 | wxFLAGS_MEMBER(wxCLIP_CHILDREN) | |
74 | wxFLAGS_MEMBER(wxTRANSPARENT_WINDOW) | |
75 | wxFLAGS_MEMBER(wxWANTS_CHARS) | |
cb0afb26 | 76 | wxFLAGS_MEMBER(wxFULL_REPAINT_ON_RESIZE) |
3ff066a4 SC |
77 | wxFLAGS_MEMBER(wxALWAYS_SHOW_SB ) |
78 | wxFLAGS_MEMBER(wxVSCROLL) | |
79 | wxFLAGS_MEMBER(wxHSCROLL) | |
80 | ||
81 | wxFLAGS_MEMBER(wxBU_LEFT) | |
82 | wxFLAGS_MEMBER(wxBU_RIGHT) | |
83 | wxFLAGS_MEMBER(wxBU_TOP) | |
84 | wxFLAGS_MEMBER(wxBU_BOTTOM) | |
85 | wxFLAGS_MEMBER(wxBU_EXACTFIT) | |
86 | wxEND_FLAGS( wxButtonStyle ) | |
067e9be6 | 87 | |
51596bcb SC |
88 | IMPLEMENT_DYNAMIC_CLASS_XTI(wxButton, wxControl,"wx/button.h") |
89 | ||
3ff066a4 SC |
90 | wxBEGIN_PROPERTIES_TABLE(wxButton) |
91 | wxEVENT_PROPERTY( Click , wxEVT_COMMAND_BUTTON_CLICKED , wxCommandEvent) | |
067e9be6 | 92 | |
3ff066a4 SC |
93 | wxPROPERTY( Font , wxFont , SetFont , GetFont , , 0 /*flags*/ , wxT("Helpstring") , wxT("group")) |
94 | wxPROPERTY( Label, wxString , SetLabel, GetLabel, wxString(), 0 /*flags*/ , wxT("Helpstring") , wxT("group") ) | |
067e9be6 | 95 | |
3ff066a4 | 96 | wxPROPERTY_FLAGS( WindowStyle , wxButtonStyle , long , SetWindowStyleFlag , GetWindowStyleFlag , , 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style |
51741307 | 97 | |
3ff066a4 | 98 | wxEND_PROPERTIES_TABLE() |
51596bcb | 99 | |
3ff066a4 SC |
100 | wxBEGIN_HANDLERS_TABLE(wxButton) |
101 | wxEND_HANDLERS_TABLE() | |
51596bcb | 102 | |
3ff066a4 | 103 | wxCONSTRUCTOR_6( wxButton , wxWindow* , Parent , wxWindowID , Id , wxString , Label , wxPoint , Position , wxSize , Size , long , WindowStyle ) |
51596bcb SC |
104 | |
105 | ||
106 | #else | |
cd0b1709 | 107 | IMPLEMENT_DYNAMIC_CLASS(wxButton, wxControl) |
51596bcb | 108 | #endif |
2bda0e17 | 109 | |
edccf428 VZ |
110 | // this macro tries to adjust the default button height to a reasonable value |
111 | // using the char height as the base | |
1c4a764c | 112 | #define BUTTON_HEIGHT_FROM_CHAR_HEIGHT(cy) (11*EDIT_HEIGHT_FROM_CHAR_HEIGHT(cy)/10) |
2bda0e17 | 113 | |
edccf428 VZ |
114 | // ============================================================================ |
115 | // implementation | |
116 | // ============================================================================ | |
117 | ||
118 | // ---------------------------------------------------------------------------- | |
119 | // creation/destruction | |
120 | // ---------------------------------------------------------------------------- | |
121 | ||
122 | bool wxButton::Create(wxWindow *parent, | |
123 | wxWindowID id, | |
124 | const wxString& label, | |
125 | const wxPoint& pos, | |
126 | const wxSize& size, | |
127 | long style, | |
128 | const wxValidator& validator, | |
129 | const wxString& name) | |
2bda0e17 | 130 | { |
5b2f31eb | 131 | if ( !CreateControl(parent, id, pos, size, style, validator, name) ) |
edccf428 VZ |
132 | return FALSE; |
133 | ||
8292017c VZ |
134 | WXDWORD exstyle; |
135 | WXDWORD msStyle = MSWGetStyle(style, &exstyle); | |
136 | ||
137 | #ifdef __WIN32__ | |
138 | // if the label contains several lines we must explicitly tell the button | |
139 | // about it or it wouldn't draw it correctly ("\n"s would just appear as | |
140 | // black boxes) | |
141 | // | |
142 | // NB: we do it here and not in MSWGetStyle() because we need the label | |
143 | // value and m_label is not set yet when MSWGetStyle() is called; | |
144 | // besides changing BS_MULTILINE during run-time is pointless anyhow | |
145 | if ( label.find(_T('\n')) != wxString::npos ) | |
146 | { | |
147 | msStyle |= BS_MULTILINE; | |
148 | } | |
149 | #endif // __WIN32__ | |
150 | ||
151 | return MSWCreateControl(_T("BUTTON"), msStyle, pos, size, label, exstyle); | |
5b2f31eb VZ |
152 | } |
153 | ||
154 | wxButton::~wxButton() | |
155 | { | |
156 | } | |
edccf428 | 157 | |
5b2f31eb VZ |
158 | // ---------------------------------------------------------------------------- |
159 | // flags | |
160 | // ---------------------------------------------------------------------------- | |
cd0b1709 | 161 | |
5b2f31eb VZ |
162 | WXDWORD wxButton::MSWGetStyle(long style, WXDWORD *exstyle) const |
163 | { | |
164 | // buttons never have an external border, they draw their own one | |
165 | WXDWORD msStyle = wxControl::MSWGetStyle | |
166 | ( | |
167 | (style & ~wxBORDER_MASK) | wxBORDER_NONE, exstyle | |
168 | ); | |
f6bcfd97 | 169 | |
5b2f31eb VZ |
170 | // we must use WS_CLIPSIBLINGS with the buttons or they would draw over |
171 | // each other in any resizeable dialog which has more than one button in | |
172 | // the bottom | |
173 | msStyle |= WS_CLIPSIBLINGS; | |
b0766406 | 174 | |
f6bcfd97 | 175 | #ifdef __WIN32__ |
5b2f31eb VZ |
176 | // don't use "else if" here: weird as it is, but you may combine wxBU_LEFT |
177 | // and wxBU_RIGHT to get BS_CENTER! | |
178 | if ( style & wxBU_LEFT ) | |
f6bcfd97 | 179 | msStyle |= BS_LEFT; |
5b2f31eb | 180 | if ( style & wxBU_RIGHT ) |
f6bcfd97 | 181 | msStyle |= BS_RIGHT; |
5b2f31eb | 182 | if ( style & wxBU_TOP ) |
f6bcfd97 | 183 | msStyle |= BS_TOP; |
5b2f31eb | 184 | if ( style & wxBU_BOTTOM ) |
f6bcfd97 | 185 | msStyle |= BS_BOTTOM; |
8a094d7b JS |
186 | // flat 2d buttons |
187 | if ( style & wxNO_BORDER ) | |
188 | msStyle |= BS_FLAT; | |
5b2f31eb | 189 | #endif // __WIN32__ |
edccf428 | 190 | |
5b2f31eb | 191 | return msStyle; |
2bda0e17 KB |
192 | } |
193 | ||
edccf428 VZ |
194 | // ---------------------------------------------------------------------------- |
195 | // size management including autosizing | |
196 | // ---------------------------------------------------------------------------- | |
197 | ||
f68586e5 | 198 | wxSize wxButton::DoGetBestSize() const |
2bda0e17 | 199 | { |
4438caf4 | 200 | int wBtn; |
1e023926 | 201 | GetTextExtent(wxGetWindowText(GetHWND()), &wBtn, NULL); |
edccf428 | 202 | |
4438caf4 VZ |
203 | int wChar, hChar; |
204 | wxGetCharSize(GetHWND(), &wChar, &hChar, &GetFont()); | |
205 | ||
1e023926 | 206 | // add a margin -- the button is wider than just its label |
4438caf4 VZ |
207 | wBtn += 3*wChar; |
208 | ||
209 | // the button height is proportional to the height of the font used | |
210 | int hBtn = BUTTON_HEIGHT_FROM_CHAR_HEIGHT(hChar); | |
edccf428 | 211 | |
1e023926 VZ |
212 | // all buttons have at least the standard size unless the user explicitly |
213 | // wants them to be of smaller size and used wxBU_EXACTFIT style when | |
214 | // creating the button | |
215 | if ( !HasFlag(wxBU_EXACTFIT) ) | |
188b9f2e RD |
216 | { |
217 | wxSize sz = GetDefaultSize(); | |
1e023926 VZ |
218 | if (wBtn > sz.x) |
219 | sz.x = wBtn; | |
220 | if (hBtn > sz.y) | |
221 | sz.y = hBtn; | |
222 | ||
188b9f2e RD |
223 | return sz; |
224 | } | |
a63f2bec | 225 | |
1e023926 | 226 | return wxSize(wBtn, hBtn); |
2bda0e17 KB |
227 | } |
228 | ||
e1f36ff8 | 229 | /* static */ |
1e6feb95 | 230 | wxSize wxButtonBase::GetDefaultSize() |
e1f36ff8 | 231 | { |
8c3c31d4 | 232 | static wxSize s_sizeBtn; |
e1f36ff8 | 233 | |
8c3c31d4 VZ |
234 | if ( s_sizeBtn.x == 0 ) |
235 | { | |
236 | wxScreenDC dc; | |
a756f210 | 237 | dc.SetFont(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT)); |
8c3c31d4 VZ |
238 | |
239 | // the size of a standard button in the dialog units is 50x14, | |
240 | // translate this to pixels | |
241 | // NB1: the multipliers come from the Windows convention | |
242 | // NB2: the extra +1/+2 were needed to get the size be the same as the | |
243 | // size of the buttons in the standard dialog - I don't know how | |
244 | // this happens, but on my system this size is 75x23 in pixels and | |
245 | // 23*8 isn't even divisible by 14... Would be nice to understand | |
246 | // why these constants are needed though! | |
247 | s_sizeBtn.x = (50 * (dc.GetCharWidth() + 1))/4; | |
248 | s_sizeBtn.y = ((14 * dc.GetCharHeight()) + 2)/8; | |
249 | } | |
e1f36ff8 | 250 | |
8c3c31d4 | 251 | return s_sizeBtn; |
e1f36ff8 VZ |
252 | } |
253 | ||
4438caf4 | 254 | // ---------------------------------------------------------------------------- |
036da5e3 | 255 | // default button handling |
4438caf4 VZ |
256 | // ---------------------------------------------------------------------------- |
257 | ||
d78f09e2 VZ |
258 | /* |
259 | "Everything you ever wanted to know about the default buttons" or "Why do we | |
260 | have to do all this?" | |
261 | ||
262 | In MSW the default button should be activated when the user presses Enter | |
263 | and the current control doesn't process Enter itself somehow. This is | |
264 | handled by ::DefWindowProc() (or maybe ::DefDialogProc()) using DM_SETDEFID | |
265 | Another aspect of "defaultness" is that the default button has different | |
266 | appearance: this is due to BS_DEFPUSHBUTTON style which is completely | |
7fb1b2b4 VZ |
267 | separate from DM_SETDEFID stuff (!). Also note that BS_DEFPUSHBUTTON should |
268 | be unset if our parent window is not active so it should be unset whenever | |
269 | we lose activation and set back when we regain it. | |
d78f09e2 VZ |
270 | |
271 | Final complication is that when a button is active, it should be the default | |
272 | one, i.e. pressing Enter on a button always activates it and not another | |
273 | one. | |
274 | ||
275 | We handle this by maintaining a permanent and a temporary default items in | |
276 | wxControlContainer (both may be NULL). When a button becomes the current | |
277 | control (i.e. gets focus) it sets itself as the temporary default which | |
278 | ensures that it has the right appearance and that Enter will be redirected | |
279 | to it. When the button loses focus, it unsets the temporary default and so | |
280 | the default item will be the permanent default -- that is the default button | |
281 | if any had been set or none otherwise, which is just what we want. | |
282 | ||
7fb1b2b4 VZ |
283 | NB: all this is quite complicated by now and the worst is that normally |
284 | it shouldn't be necessary at all as for the normal Windows programs | |
285 | DefWindowProc() and IsDialogMessage() take care of all this | |
286 | automatically -- however in wxWindows programs this doesn't work for | |
287 | nested hierarchies (i.e. a notebook inside a notebook) for unknown | |
288 | reason and so we have to reproduce all this code ourselves. It would be | |
289 | very nice if we could avoid doing it. | |
d78f09e2 VZ |
290 | */ |
291 | ||
036da5e3 | 292 | // set this button as the (permanently) default one in its panel |
edccf428 | 293 | void wxButton::SetDefault() |
2bda0e17 | 294 | { |
edccf428 | 295 | wxWindow *parent = GetParent(); |
2bda0e17 | 296 | |
036da5e3 VZ |
297 | wxCHECK_RET( parent, _T("button without parent?") ); |
298 | ||
7fb1b2b4 | 299 | // set this one as the default button both for wxWindows ... |
036da5e3 | 300 | wxWindow *winOldDefault = parent->SetDefaultItem(this); |
036da5e3 | 301 | |
7fb1b2b4 VZ |
302 | // ... and Windows |
303 | SetDefaultStyle(wxDynamicCast(winOldDefault, wxButton), FALSE); | |
304 | SetDefaultStyle(this, TRUE); | |
036da5e3 VZ |
305 | } |
306 | ||
7fb1b2b4 | 307 | // set this button as being currently default |
036da5e3 VZ |
308 | void wxButton::SetTmpDefault() |
309 | { | |
310 | wxWindow *parent = GetParent(); | |
311 | ||
312 | wxCHECK_RET( parent, _T("button without parent?") ); | |
313 | ||
314 | wxWindow *winOldDefault = parent->GetDefaultItem(); | |
315 | parent->SetTmpDefaultItem(this); | |
7fb1b2b4 VZ |
316 | |
317 | SetDefaultStyle(wxDynamicCast(winOldDefault, wxButton), FALSE); | |
318 | SetDefaultStyle(this, TRUE); | |
036da5e3 VZ |
319 | } |
320 | ||
7fb1b2b4 | 321 | // unset this button as currently default, it may still stay permanent default |
036da5e3 VZ |
322 | void wxButton::UnsetTmpDefault() |
323 | { | |
324 | wxWindow *parent = GetParent(); | |
325 | ||
326 | wxCHECK_RET( parent, _T("button without parent?") ); | |
327 | ||
328 | parent->SetTmpDefaultItem(NULL); | |
329 | ||
330 | wxWindow *winOldDefault = parent->GetDefaultItem(); | |
7fb1b2b4 VZ |
331 | |
332 | SetDefaultStyle(this, FALSE); | |
333 | SetDefaultStyle(wxDynamicCast(winOldDefault, wxButton), TRUE); | |
036da5e3 | 334 | } |
8ed57d93 | 335 | |
036da5e3 VZ |
336 | /* static */ |
337 | void | |
7fb1b2b4 | 338 | wxButton::SetDefaultStyle(wxButton *btn, bool on) |
036da5e3 | 339 | { |
7fb1b2b4 VZ |
340 | // we may be called with NULL pointer -- simpler to do the check here than |
341 | // in the caller which does wxDynamicCast() | |
342 | if ( !btn ) | |
343 | return; | |
344 | ||
345 | // first, let DefDlgProc() know about the new default button | |
346 | if ( on ) | |
5d1d2d46 | 347 | { |
7fb1b2b4 VZ |
348 | // we shouldn't set BS_DEFPUSHBUTTON for any button if we don't have |
349 | // focus at all any more | |
350 | if ( !wxTheApp->IsActive() ) | |
351 | return; | |
cd0b1709 | 352 | |
7fb1b2b4 VZ |
353 | // look for a panel-like window |
354 | wxWindow *win = btn->GetParent(); | |
355 | while ( win && !win->HasFlag(wxTAB_TRAVERSAL) ) | |
356 | win = win->GetParent(); | |
357 | ||
358 | if ( win ) | |
be4017f8 | 359 | { |
7fb1b2b4 VZ |
360 | ::SendMessage(GetHwndOf(win), DM_SETDEFID, btn->GetId(), 0L); |
361 | ||
362 | // sending DM_SETDEFID also changes the button style to | |
363 | // BS_DEFPUSHBUTTON so there is nothing more to do | |
be4017f8 | 364 | } |
5d1d2d46 VZ |
365 | } |
366 | ||
7fb1b2b4 VZ |
367 | // then also change the style as needed |
368 | long style = ::GetWindowLong(GetHwndOf(btn), GWL_STYLE); | |
369 | if ( !(style & BS_DEFPUSHBUTTON) == on ) | |
be4017f8 | 370 | { |
7fb1b2b4 VZ |
371 | // don't do it with the owner drawn buttons because it will |
372 | // reset BS_OWNERDRAW style bit too (as BS_OWNERDRAW & | |
373 | // BS_DEFPUSHBUTTON != 0)! | |
036da5e3 VZ |
374 | if ( (style & BS_OWNERDRAW) != BS_OWNERDRAW ) |
375 | { | |
7fb1b2b4 VZ |
376 | ::SendMessage(GetHwndOf(btn), BM_SETSTYLE, |
377 | on ? style | BS_DEFPUSHBUTTON | |
378 | : style & ~BS_DEFPUSHBUTTON, | |
379 | 1L /* redraw */); | |
036da5e3 | 380 | } |
7fb1b2b4 | 381 | else // owner drawn |
036da5e3 | 382 | { |
7fb1b2b4 VZ |
383 | // redraw the button - it will notice itself that it's |
384 | // [not] the default one [any longer] | |
385 | btn->Refresh(); | |
036da5e3 | 386 | } |
be4017f8 | 387 | } |
7fb1b2b4 | 388 | //else: already has correct style |
2bda0e17 KB |
389 | } |
390 | ||
edccf428 VZ |
391 | // ---------------------------------------------------------------------------- |
392 | // helpers | |
393 | // ---------------------------------------------------------------------------- | |
394 | ||
395 | bool wxButton::SendClickEvent() | |
2bda0e17 | 396 | { |
edccf428 VZ |
397 | wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED, GetId()); |
398 | event.SetEventObject(this); | |
399 | ||
400 | return ProcessCommand(event); | |
2bda0e17 KB |
401 | } |
402 | ||
edccf428 | 403 | void wxButton::Command(wxCommandEvent & event) |
2bda0e17 | 404 | { |
edccf428 | 405 | ProcessCommand(event); |
2bda0e17 KB |
406 | } |
407 | ||
edccf428 VZ |
408 | // ---------------------------------------------------------------------------- |
409 | // event/message handlers | |
410 | // ---------------------------------------------------------------------------- | |
2bda0e17 | 411 | |
33ac7e6f | 412 | bool wxButton::MSWCommand(WXUINT param, WXWORD WXUNUSED(id)) |
edccf428 VZ |
413 | { |
414 | bool processed = FALSE; | |
57c0af52 | 415 | switch ( param ) |
edccf428 | 416 | { |
5aab763c RD |
417 | // NOTE: Apparently older versions (NT 4?) of the common controls send |
418 | // BN_DOUBLECLICKED but not a second BN_CLICKED for owner-drawn | |
419 | // buttons, so in order to send two EVET_BUTTON events we should | |
420 | // catch both types. Currently (Feb 2003) up-to-date versions of | |
421 | // win98, win2k and winXP all send two BN_CLICKED messages for | |
422 | // all button types, so we don't catch BN_DOUBLECLICKED anymore | |
423 | // in order to not get 3 EVT_BUTTON events. If this is a problem | |
424 | // then we need to figure out which version of the comctl32 changed | |
425 | // this behaviour and test for it. | |
426 | ||
a95e38c0 VZ |
427 | case 1: // message came from an accelerator |
428 | case BN_CLICKED: // normal buttons send this | |
57c0af52 VZ |
429 | processed = SendClickEvent(); |
430 | break; | |
edccf428 | 431 | } |
2bda0e17 | 432 | |
edccf428 | 433 | return processed; |
2bda0e17 KB |
434 | } |
435 | ||
678cd6de VZ |
436 | long wxButton::MSWWindowProc(WXUINT nMsg, WXWPARAM wParam, WXLPARAM lParam) |
437 | { | |
036da5e3 VZ |
438 | // when we receive focus, we want to temporary become the default button in |
439 | // our parent panel so that pressing "Enter" would activate us -- and when | |
440 | // losing it we should restore the previous default button as well | |
be4017f8 | 441 | if ( nMsg == WM_SETFOCUS ) |
678cd6de | 442 | { |
036da5e3 | 443 | SetTmpDefault(); |
be4017f8 | 444 | |
036da5e3 VZ |
445 | // let the default processing take place too |
446 | } | |
447 | else if ( nMsg == WM_KILLFOCUS ) | |
448 | { | |
449 | UnsetTmpDefault(); | |
678cd6de | 450 | } |
a95e38c0 VZ |
451 | else if ( nMsg == WM_LBUTTONDBLCLK ) |
452 | { | |
f6bcfd97 BP |
453 | // emulate a click event to force an owner-drawn button to change its |
454 | // appearance - without this, it won't do it | |
455 | (void)wxControl::MSWWindowProc(WM_LBUTTONDOWN, wParam, lParam); | |
456 | ||
036da5e3 | 457 | // and continue with processing the message normally as well |
a95e38c0 | 458 | } |
678cd6de VZ |
459 | |
460 | // let the base class do all real processing | |
461 | return wxControl::MSWWindowProc(nMsg, wParam, lParam); | |
462 | } | |
cd0b1709 VZ |
463 | |
464 | // ---------------------------------------------------------------------------- | |
465 | // owner-drawn buttons support | |
466 | // ---------------------------------------------------------------------------- | |
467 | ||
468 | #ifdef __WIN32__ | |
469 | ||
470 | // drawing helpers | |
471 | ||
472 | static void DrawButtonText(HDC hdc, | |
473 | RECT *pRect, | |
474 | const wxString& text, | |
475 | COLORREF col) | |
476 | { | |
477 | COLORREF colOld = SetTextColor(hdc, col); | |
478 | int modeOld = SetBkMode(hdc, TRANSPARENT); | |
479 | ||
91f2c154 JS |
480 | // Note: we must have DT_SINGLELINE for DT_VCENTER to work. |
481 | ::DrawText(hdc, text, text.length(), pRect, DT_SINGLELINE | DT_CENTER | DT_VCENTER); | |
cd0b1709 VZ |
482 | |
483 | SetBkMode(hdc, modeOld); | |
484 | SetTextColor(hdc, colOld); | |
485 | } | |
486 | ||
487 | static void DrawRect(HDC hdc, const RECT& r) | |
488 | { | |
4676948b JS |
489 | wxDrawLine(hdc, r.left, r.top, r.right, r.top); |
490 | wxDrawLine(hdc, r.right, r.top, r.right, r.bottom); | |
491 | wxDrawLine(hdc, r.right, r.bottom, r.left, r.bottom); | |
492 | wxDrawLine(hdc, r.left, r.bottom, r.left, r.top); | |
cd0b1709 VZ |
493 | } |
494 | ||
495 | void wxButton::MakeOwnerDrawn() | |
496 | { | |
497 | long style = GetWindowLong(GetHwnd(), GWL_STYLE); | |
9750fc42 | 498 | if ( (style & BS_OWNERDRAW) != BS_OWNERDRAW ) |
cd0b1709 VZ |
499 | { |
500 | // make it so | |
501 | style |= BS_OWNERDRAW; | |
502 | SetWindowLong(GetHwnd(), GWL_STYLE, style); | |
503 | } | |
504 | } | |
505 | ||
506 | bool wxButton::SetBackgroundColour(const wxColour &colour) | |
507 | { | |
508 | if ( !wxControl::SetBackgroundColour(colour) ) | |
509 | { | |
510 | // nothing to do | |
511 | return FALSE; | |
512 | } | |
513 | ||
514 | MakeOwnerDrawn(); | |
515 | ||
516 | Refresh(); | |
517 | ||
518 | return TRUE; | |
519 | } | |
520 | ||
521 | bool wxButton::SetForegroundColour(const wxColour &colour) | |
522 | { | |
523 | if ( !wxControl::SetForegroundColour(colour) ) | |
524 | { | |
525 | // nothing to do | |
526 | return FALSE; | |
527 | } | |
528 | ||
529 | MakeOwnerDrawn(); | |
530 | ||
531 | Refresh(); | |
532 | ||
533 | return TRUE; | |
534 | } | |
535 | ||
536 | /* | |
537 | The button frame looks like this normally: | |
538 | ||
539 | WWWWWWWWWWWWWWWWWWB | |
16162a64 GRG |
540 | WHHHHHHHHHHHHHHHHGB W = white (HILIGHT) |
541 | WH GB H = light grey (LIGHT) | |
542 | WH GB G = dark grey (SHADOW) | |
543 | WH GB B = black (DKSHADOW) | |
544 | WH GB | |
cd0b1709 VZ |
545 | WGGGGGGGGGGGGGGGGGB |
546 | BBBBBBBBBBBBBBBBBBB | |
547 | ||
548 | When the button is selected, the button becomes like this (the total button | |
549 | size doesn't change): | |
550 | ||
551 | BBBBBBBBBBBBBBBBBBB | |
552 | BWWWWWWWWWWWWWWWWBB | |
16162a64 GRG |
553 | BWHHHHHHHHHHHHHHGBB |
554 | BWH GBB | |
555 | BWH GBB | |
cd0b1709 VZ |
556 | BWGGGGGGGGGGGGGGGBB |
557 | BBBBBBBBBBBBBBBBBBB | |
558 | BBBBBBBBBBBBBBBBBBB | |
559 | ||
560 | When the button is pushed (while selected) it is like: | |
561 | ||
562 | BBBBBBBBBBBBBBBBBBB | |
563 | BGGGGGGGGGGGGGGGGGB | |
564 | BG GB | |
565 | BG GB | |
566 | BG GB | |
16162a64 | 567 | BG GB |
cd0b1709 VZ |
568 | BGGGGGGGGGGGGGGGGGB |
569 | BBBBBBBBBBBBBBBBBBB | |
570 | */ | |
571 | ||
572 | static void DrawButtonFrame(HDC hdc, const RECT& rectBtn, | |
573 | bool selected, bool pushed) | |
574 | { | |
575 | RECT r; | |
576 | CopyRect(&r, &rectBtn); | |
577 | ||
16162a64 GRG |
578 | HPEN hpenBlack = CreatePen(PS_SOLID, 1, GetSysColor(COLOR_3DDKSHADOW)), |
579 | hpenGrey = CreatePen(PS_SOLID, 1, GetSysColor(COLOR_3DSHADOW)), | |
580 | hpenLightGr = CreatePen(PS_SOLID, 1, GetSysColor(COLOR_3DLIGHT)), | |
581 | hpenWhite = CreatePen(PS_SOLID, 1, GetSysColor(COLOR_3DHILIGHT)); | |
cd0b1709 VZ |
582 | |
583 | HPEN hpenOld = (HPEN)SelectObject(hdc, hpenBlack); | |
584 | ||
585 | r.right--; | |
586 | r.bottom--; | |
587 | ||
588 | if ( pushed ) | |
589 | { | |
590 | DrawRect(hdc, r); | |
591 | ||
592 | (void)SelectObject(hdc, hpenGrey); | |
593 | InflateRect(&r, -1, -1); | |
594 | ||
595 | DrawRect(hdc, r); | |
596 | } | |
597 | else // !pushed | |
598 | { | |
599 | if ( selected ) | |
600 | { | |
601 | DrawRect(hdc, r); | |
602 | ||
603 | InflateRect(&r, -1, -1); | |
604 | } | |
605 | ||
4676948b JS |
606 | wxDrawLine(hdc, r.left, r.bottom, r.right, r.bottom); |
607 | wxDrawLine(hdc, r.right, r.bottom, r.right, r.top - 1); | |
cd0b1709 VZ |
608 | |
609 | (void)SelectObject(hdc, hpenWhite); | |
4676948b JS |
610 | wxDrawLine(hdc, r.left, r.bottom - 1, r.left, r.top); |
611 | wxDrawLine(hdc, r.left, r.top, r.right, r.top); | |
cd0b1709 | 612 | |
16162a64 | 613 | (void)SelectObject(hdc, hpenLightGr); |
4676948b JS |
614 | wxDrawLine(hdc, r.left + 1, r.bottom - 2, r.left + 1, r.top + 1); |
615 | wxDrawLine(hdc, r.left + 1, r.top + 1, r.right - 1, r.top + 1); | |
16162a64 | 616 | |
cd0b1709 | 617 | (void)SelectObject(hdc, hpenGrey); |
4676948b JS |
618 | wxDrawLine(hdc, r.left + 1, r.bottom - 1, r.right - 1, r.bottom - 1); |
619 | wxDrawLine(hdc, r.right - 1, r.bottom - 1, r.right - 1, r.top); | |
cd0b1709 VZ |
620 | } |
621 | ||
622 | (void)SelectObject(hdc, hpenOld); | |
623 | DeleteObject(hpenWhite); | |
16162a64 | 624 | DeleteObject(hpenLightGr); |
cd0b1709 VZ |
625 | DeleteObject(hpenGrey); |
626 | DeleteObject(hpenBlack); | |
627 | } | |
628 | ||
629 | bool wxButton::MSWOnDraw(WXDRAWITEMSTRUCT *wxdis) | |
630 | { | |
631 | LPDRAWITEMSTRUCT lpDIS = (LPDRAWITEMSTRUCT)wxdis; | |
632 | ||
633 | RECT rectBtn; | |
634 | CopyRect(&rectBtn, &lpDIS->rcItem); | |
635 | ||
636 | COLORREF colBg = wxColourToRGB(GetBackgroundColour()), | |
637 | colFg = wxColourToRGB(GetForegroundColour()); | |
638 | ||
639 | HDC hdc = lpDIS->hDC; | |
640 | UINT state = lpDIS->itemState; | |
641 | ||
642 | // first, draw the background | |
643 | HBRUSH hbrushBackground = ::CreateSolidBrush(colBg); | |
644 | ||
645 | FillRect(hdc, &rectBtn, hbrushBackground); | |
646 | ||
647 | // draw the border for the current state | |
648 | bool selected = (state & ODS_SELECTED) != 0; | |
649 | if ( !selected ) | |
650 | { | |
651 | wxPanel *panel = wxDynamicCast(GetParent(), wxPanel); | |
652 | if ( panel ) | |
653 | { | |
654 | selected = panel->GetDefaultItem() == this; | |
655 | } | |
656 | } | |
657 | bool pushed = (SendMessage(GetHwnd(), BM_GETSTATE, 0, 0) & BST_PUSHED) != 0; | |
658 | ||
659 | DrawButtonFrame(hdc, rectBtn, selected, pushed); | |
660 | ||
661 | // draw the focus rect if needed | |
662 | if ( state & ODS_FOCUS ) | |
663 | { | |
664 | RECT rectFocus; | |
665 | CopyRect(&rectFocus, &rectBtn); | |
666 | ||
667 | // I don't know where does this constant come from, but this is how | |
668 | // Windows draws them | |
669 | InflateRect(&rectFocus, -4, -4); | |
670 | ||
671 | DrawFocusRect(hdc, &rectFocus); | |
672 | } | |
673 | ||
9750fc42 VZ |
674 | if ( pushed ) |
675 | { | |
676 | // the label is shifted by 1 pixel to create "pushed" effect | |
677 | OffsetRect(&rectBtn, 1, 1); | |
678 | } | |
679 | ||
cd0b1709 VZ |
680 | DrawButtonText(hdc, &rectBtn, GetLabel(), |
681 | state & ODS_DISABLED ? GetSysColor(COLOR_GRAYTEXT) | |
682 | : colFg); | |
683 | ||
684 | ::DeleteObject(hbrushBackground); | |
685 | ||
cd0b1709 VZ |
686 | return TRUE; |
687 | } | |
688 | ||
689 | #endif // __WIN32__ | |
1e6feb95 VZ |
690 | |
691 | #endif // wxUSE_BUTTON | |
692 |