]>
Commit | Line | Data |
---|---|---|
2bda0e17 | 1 | ///////////////////////////////////////////////////////////////////////////// |
f1e01716 | 2 | // Name: src/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 | 8 | // Copyright: (c) Julian Smart |
65571936 | 9 | // Licence: wxWindows licence |
2bda0e17 KB |
10 | ///////////////////////////////////////////////////////////////////////////// |
11 | ||
edccf428 VZ |
12 | // ============================================================================ |
13 | // declarations | |
14 | // ============================================================================ | |
15 | ||
16 | // ---------------------------------------------------------------------------- | |
17 | // headers | |
18 | // ---------------------------------------------------------------------------- | |
cd0b1709 | 19 | |
2bda0e17 KB |
20 | // For compilers that support precompilation, includes "wx.h". |
21 | #include "wx/wxprec.h" | |
22 | ||
23 | #ifdef __BORLANDC__ | |
edccf428 | 24 | #pragma hdrstop |
2bda0e17 KB |
25 | #endif |
26 | ||
1e6feb95 VZ |
27 | #if wxUSE_BUTTON |
28 | ||
f1e01716 WS |
29 | #include "wx/button.h" |
30 | ||
2bda0e17 | 31 | #ifndef WX_PRECOMP |
bac409a0 | 32 | #include "wx/app.h" |
edccf428 | 33 | #include "wx/brush.h" |
4e938f5b | 34 | #include "wx/panel.h" |
8a4df159 | 35 | #include "wx/bmpbuttn.h" |
fb39c7ec RR |
36 | #include "wx/settings.h" |
37 | #include "wx/dcscreen.h" | |
61e6a2ab | 38 | #include "wx/dcclient.h" |
b84aec03 | 39 | #include "wx/toplevel.h" |
2bda0e17 KB |
40 | #endif |
41 | ||
5f7bcb48 | 42 | #include "wx/stockitem.h" |
2bda0e17 | 43 | #include "wx/msw/private.h" |
533171c2 | 44 | #include "wx/msw/private/button.h" |
2bda0e17 | 45 | |
4e9da8b7 RD |
46 | #if wxUSE_UXTHEME |
47 | #include "wx/msw/uxtheme.h" | |
48 | ||
49 | // no need to include tmschema.h | |
50 | #ifndef BP_PUSHBUTTON | |
51 | #define BP_PUSHBUTTON 1 | |
52 | ||
53 | #define PBS_NORMAL 1 | |
54 | #define PBS_HOT 2 | |
55 | #define PBS_PRESSED 3 | |
56 | #define PBS_DISABLED 4 | |
57 | #define PBS_DEFAULTED 5 | |
58 | ||
59 | #define TMT_CONTENTMARGINS 3602 | |
60 | #endif | |
61 | #endif // wxUSE_UXTHEME | |
62 | ||
63 | #ifndef WM_THEMECHANGED | |
64 | #define WM_THEMECHANGED 0x031A | |
65 | #endif | |
66 | ||
67 | #ifndef ODS_NOACCEL | |
68 | #define ODS_NOACCEL 0x0100 | |
69 | #endif | |
70 | ||
71 | #ifndef ODS_NOFOCUSRECT | |
72 | #define ODS_NOFOCUSRECT 0x0200 | |
73 | #endif | |
74 | ||
edccf428 VZ |
75 | // ---------------------------------------------------------------------------- |
76 | // macros | |
77 | // ---------------------------------------------------------------------------- | |
78 | ||
51596bcb | 79 | #if wxUSE_EXTENDED_RTTI |
067e9be6 | 80 | |
f3291a82 SC |
81 | WX_DEFINE_FLAGS( wxButtonStyle ) |
82 | ||
3ff066a4 | 83 | wxBEGIN_FLAGS( wxButtonStyle ) |
f3291a82 SC |
84 | // new style border flags, we put them first to |
85 | // use them for streaming out | |
3ff066a4 SC |
86 | wxFLAGS_MEMBER(wxBORDER_SIMPLE) |
87 | wxFLAGS_MEMBER(wxBORDER_SUNKEN) | |
88 | wxFLAGS_MEMBER(wxBORDER_DOUBLE) | |
89 | wxFLAGS_MEMBER(wxBORDER_RAISED) | |
90 | wxFLAGS_MEMBER(wxBORDER_STATIC) | |
91 | wxFLAGS_MEMBER(wxBORDER_NONE) | |
fcf90ee1 | 92 | |
f3291a82 | 93 | // old style border flags |
3ff066a4 SC |
94 | wxFLAGS_MEMBER(wxSIMPLE_BORDER) |
95 | wxFLAGS_MEMBER(wxSUNKEN_BORDER) | |
96 | wxFLAGS_MEMBER(wxDOUBLE_BORDER) | |
97 | wxFLAGS_MEMBER(wxRAISED_BORDER) | |
98 | wxFLAGS_MEMBER(wxSTATIC_BORDER) | |
cb0afb26 | 99 | wxFLAGS_MEMBER(wxBORDER) |
f3291a82 | 100 | |
bc9fb572 | 101 | // standard window styles |
3ff066a4 SC |
102 | wxFLAGS_MEMBER(wxTAB_TRAVERSAL) |
103 | wxFLAGS_MEMBER(wxCLIP_CHILDREN) | |
104 | wxFLAGS_MEMBER(wxTRANSPARENT_WINDOW) | |
105 | wxFLAGS_MEMBER(wxWANTS_CHARS) | |
cb0afb26 | 106 | wxFLAGS_MEMBER(wxFULL_REPAINT_ON_RESIZE) |
3ff066a4 SC |
107 | wxFLAGS_MEMBER(wxALWAYS_SHOW_SB ) |
108 | wxFLAGS_MEMBER(wxVSCROLL) | |
109 | wxFLAGS_MEMBER(wxHSCROLL) | |
110 | ||
111 | wxFLAGS_MEMBER(wxBU_LEFT) | |
112 | wxFLAGS_MEMBER(wxBU_RIGHT) | |
113 | wxFLAGS_MEMBER(wxBU_TOP) | |
114 | wxFLAGS_MEMBER(wxBU_BOTTOM) | |
115 | wxFLAGS_MEMBER(wxBU_EXACTFIT) | |
116 | wxEND_FLAGS( wxButtonStyle ) | |
067e9be6 | 117 | |
51596bcb SC |
118 | IMPLEMENT_DYNAMIC_CLASS_XTI(wxButton, wxControl,"wx/button.h") |
119 | ||
3ff066a4 | 120 | wxBEGIN_PROPERTIES_TABLE(wxButton) |
fcf90ee1 | 121 | wxEVENT_PROPERTY( Click , wxEVT_COMMAND_BUTTON_CLICKED , wxCommandEvent) |
067e9be6 | 122 | |
fcf90ee1 WS |
123 | wxPROPERTY( Font , wxFont , SetFont , GetFont , EMPTY_MACROVALUE, 0 /*flags*/ , wxT("Helpstring") , wxT("group")) |
124 | wxPROPERTY( Label, wxString , SetLabel, GetLabel, wxString(), 0 /*flags*/ , wxT("Helpstring") , wxT("group") ) | |
067e9be6 | 125 | |
af498247 | 126 | wxPROPERTY_FLAGS( WindowStyle , wxButtonStyle , long , SetWindowStyleFlag , GetWindowStyleFlag , EMPTY_MACROVALUE , 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style |
51741307 | 127 | |
3ff066a4 | 128 | wxEND_PROPERTIES_TABLE() |
51596bcb | 129 | |
3ff066a4 SC |
130 | wxBEGIN_HANDLERS_TABLE(wxButton) |
131 | wxEND_HANDLERS_TABLE() | |
51596bcb | 132 | |
3ff066a4 | 133 | wxCONSTRUCTOR_6( wxButton , wxWindow* , Parent , wxWindowID , Id , wxString , Label , wxPoint , Position , wxSize , Size , long , WindowStyle ) |
51596bcb SC |
134 | |
135 | ||
136 | #else | |
cd0b1709 | 137 | IMPLEMENT_DYNAMIC_CLASS(wxButton, wxControl) |
51596bcb | 138 | #endif |
2bda0e17 | 139 | |
edccf428 VZ |
140 | // ============================================================================ |
141 | // implementation | |
142 | // ============================================================================ | |
143 | ||
4af4dec6 VZ |
144 | // ---------------------------------------------------------------------------- |
145 | // helper functions from wx/msw/private/button.h | |
146 | // ---------------------------------------------------------------------------- | |
147 | ||
148 | void wxMSWButton::UpdateMultilineStyle(HWND hwnd, const wxString& label) | |
149 | { | |
150 | // update BS_MULTILINE style depending on the new label (resetting it | |
151 | // doesn't seem to do anything very useful but it shouldn't hurt and we do | |
152 | // have to set it whenever the label becomes multi line as otherwise it | |
153 | // wouldn't be shown correctly as we don't use BS_MULTILINE when creating | |
154 | // the control unless it already has new lines in its label) | |
155 | long styleOld = ::GetWindowLong(hwnd, GWL_STYLE), | |
156 | styleNew; | |
157 | if ( label.find(_T('\n')) != wxString::npos ) | |
158 | styleNew = styleOld | BS_MULTILINE; | |
159 | else | |
160 | styleNew = styleOld & ~BS_MULTILINE; | |
161 | ||
162 | if ( styleNew != styleOld ) | |
163 | ::SetWindowLong(hwnd, GWL_STYLE, styleNew); | |
164 | } | |
165 | ||
166 | wxSize wxMSWButton::ComputeBestSize(wxControl *btn) | |
167 | { | |
168 | wxClientDC dc(btn); | |
169 | ||
170 | wxCoord wBtn, | |
171 | hBtn; | |
172 | dc.GetMultiLineTextExtent(btn->GetLabelText(), &wBtn, &hBtn); | |
173 | ||
174 | // FIXME: this is pure guesswork, need to retrieve the real button margins | |
175 | wBtn += 3*btn->GetCharWidth(); | |
176 | hBtn = 11*EDIT_HEIGHT_FROM_CHAR_HEIGHT(hBtn)/10; | |
177 | ||
178 | // all buttons have at least the standard size unless the user explicitly | |
179 | // wants them to be of smaller size and used wxBU_EXACTFIT style when | |
180 | // creating the button | |
181 | if ( !btn->HasFlag(wxBU_EXACTFIT) ) | |
182 | { | |
183 | wxSize sz = wxButton::GetDefaultSize(); | |
184 | if ( wBtn < sz.x ) | |
185 | wBtn = sz.x; | |
186 | if ( hBtn < sz.y ) | |
187 | hBtn = sz.y; | |
188 | } | |
189 | ||
190 | wxSize best(wBtn, hBtn); | |
191 | btn->CacheBestSize(best); | |
192 | return best; | |
193 | } | |
194 | ||
edccf428 VZ |
195 | // ---------------------------------------------------------------------------- |
196 | // creation/destruction | |
197 | // ---------------------------------------------------------------------------- | |
198 | ||
199 | bool wxButton::Create(wxWindow *parent, | |
200 | wxWindowID id, | |
5f7bcb48 | 201 | const wxString& lbl, |
edccf428 VZ |
202 | const wxPoint& pos, |
203 | const wxSize& size, | |
204 | long style, | |
205 | const wxValidator& validator, | |
206 | const wxString& name) | |
2bda0e17 | 207 | { |
5f7bcb48 VS |
208 | wxString label(lbl); |
209 | if (label.empty() && wxIsStockID(id)) | |
c87fc285 | 210 | { |
c4e1d0fc VZ |
211 | // On Windows, some buttons aren't supposed to have mnemonics |
212 | label = wxGetStockLabel | |
213 | ( | |
214 | id, | |
215 | id == wxID_OK || id == wxID_CANCEL || id == wxID_CLOSE | |
216 | ? wxSTOCK_NOFLAGS | |
217 | : wxSTOCK_WITH_MNEMONIC | |
218 | ); | |
f1e01716 WS |
219 | } |
220 | ||
5b2f31eb | 221 | if ( !CreateControl(parent, id, pos, size, style, validator, name) ) |
fcf90ee1 | 222 | return false; |
edccf428 | 223 | |
8292017c VZ |
224 | WXDWORD exstyle; |
225 | WXDWORD msStyle = MSWGetStyle(style, &exstyle); | |
226 | ||
8292017c VZ |
227 | // if the label contains several lines we must explicitly tell the button |
228 | // about it or it wouldn't draw it correctly ("\n"s would just appear as | |
229 | // black boxes) | |
230 | // | |
231 | // NB: we do it here and not in MSWGetStyle() because we need the label | |
d94de683 | 232 | // value and the label is not set yet when MSWGetStyle() is called |
533171c2 | 233 | msStyle |= wxMSWButton::GetMultilineStyle(label); |
8292017c VZ |
234 | |
235 | return MSWCreateControl(_T("BUTTON"), msStyle, pos, size, label, exstyle); | |
5b2f31eb VZ |
236 | } |
237 | ||
238 | wxButton::~wxButton() | |
239 | { | |
6c20e8f8 VZ |
240 | wxTopLevelWindow *tlw = wxDynamicCast(wxGetTopLevelParent(this), wxTopLevelWindow); |
241 | if ( tlw && tlw->GetTmpDefaultItem() == this ) | |
789367e1 VZ |
242 | { |
243 | UnsetTmpDefault(); | |
244 | } | |
5b2f31eb | 245 | } |
edccf428 | 246 | |
5b2f31eb VZ |
247 | // ---------------------------------------------------------------------------- |
248 | // flags | |
249 | // ---------------------------------------------------------------------------- | |
cd0b1709 | 250 | |
5b2f31eb VZ |
251 | WXDWORD wxButton::MSWGetStyle(long style, WXDWORD *exstyle) const |
252 | { | |
253 | // buttons never have an external border, they draw their own one | |
254 | WXDWORD msStyle = wxControl::MSWGetStyle | |
255 | ( | |
256 | (style & ~wxBORDER_MASK) | wxBORDER_NONE, exstyle | |
257 | ); | |
f6bcfd97 | 258 | |
5b2f31eb VZ |
259 | // we must use WS_CLIPSIBLINGS with the buttons or they would draw over |
260 | // each other in any resizeable dialog which has more than one button in | |
261 | // the bottom | |
262 | msStyle |= WS_CLIPSIBLINGS; | |
b0766406 | 263 | |
5b2f31eb VZ |
264 | // don't use "else if" here: weird as it is, but you may combine wxBU_LEFT |
265 | // and wxBU_RIGHT to get BS_CENTER! | |
266 | if ( style & wxBU_LEFT ) | |
f6bcfd97 | 267 | msStyle |= BS_LEFT; |
5b2f31eb | 268 | if ( style & wxBU_RIGHT ) |
f6bcfd97 | 269 | msStyle |= BS_RIGHT; |
5b2f31eb | 270 | if ( style & wxBU_TOP ) |
f6bcfd97 | 271 | msStyle |= BS_TOP; |
5b2f31eb | 272 | if ( style & wxBU_BOTTOM ) |
f6bcfd97 | 273 | msStyle |= BS_BOTTOM; |
8cc4850c | 274 | #ifndef __WXWINCE__ |
8a094d7b JS |
275 | // flat 2d buttons |
276 | if ( style & wxNO_BORDER ) | |
277 | msStyle |= BS_FLAT; | |
8cc4850c | 278 | #endif // __WXWINCE__ |
edccf428 | 279 | |
5b2f31eb | 280 | return msStyle; |
2bda0e17 KB |
281 | } |
282 | ||
d94de683 VZ |
283 | void wxButton::SetLabel(const wxString& label) |
284 | { | |
533171c2 | 285 | wxMSWButton::UpdateMultilineStyle(GetHwnd(), label); |
d94de683 VZ |
286 | |
287 | wxButtonBase::SetLabel(label); | |
288 | } | |
289 | ||
edccf428 VZ |
290 | // ---------------------------------------------------------------------------- |
291 | // size management including autosizing | |
292 | // ---------------------------------------------------------------------------- | |
293 | ||
f68586e5 | 294 | wxSize wxButton::DoGetBestSize() const |
2bda0e17 | 295 | { |
9016f3ad | 296 | return wxMSWButton::ComputeBestSize(wx_const_cast(wxButton *, this)); |
2bda0e17 KB |
297 | } |
298 | ||
e1f36ff8 | 299 | /* static */ |
1e6feb95 | 300 | wxSize wxButtonBase::GetDefaultSize() |
e1f36ff8 | 301 | { |
8c3c31d4 | 302 | static wxSize s_sizeBtn; |
e1f36ff8 | 303 | |
8c3c31d4 VZ |
304 | if ( s_sizeBtn.x == 0 ) |
305 | { | |
306 | wxScreenDC dc; | |
a756f210 | 307 | dc.SetFont(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT)); |
8c3c31d4 VZ |
308 | |
309 | // the size of a standard button in the dialog units is 50x14, | |
310 | // translate this to pixels | |
311 | // NB1: the multipliers come from the Windows convention | |
312 | // NB2: the extra +1/+2 were needed to get the size be the same as the | |
313 | // size of the buttons in the standard dialog - I don't know how | |
314 | // this happens, but on my system this size is 75x23 in pixels and | |
315 | // 23*8 isn't even divisible by 14... Would be nice to understand | |
316 | // why these constants are needed though! | |
317 | s_sizeBtn.x = (50 * (dc.GetCharWidth() + 1))/4; | |
318 | s_sizeBtn.y = ((14 * dc.GetCharHeight()) + 2)/8; | |
319 | } | |
e1f36ff8 | 320 | |
8c3c31d4 | 321 | return s_sizeBtn; |
e1f36ff8 VZ |
322 | } |
323 | ||
4438caf4 | 324 | // ---------------------------------------------------------------------------- |
036da5e3 | 325 | // default button handling |
4438caf4 VZ |
326 | // ---------------------------------------------------------------------------- |
327 | ||
d78f09e2 VZ |
328 | /* |
329 | "Everything you ever wanted to know about the default buttons" or "Why do we | |
330 | have to do all this?" | |
331 | ||
332 | In MSW the default button should be activated when the user presses Enter | |
333 | and the current control doesn't process Enter itself somehow. This is | |
334 | handled by ::DefWindowProc() (or maybe ::DefDialogProc()) using DM_SETDEFID | |
335 | Another aspect of "defaultness" is that the default button has different | |
336 | appearance: this is due to BS_DEFPUSHBUTTON style which is completely | |
7fb1b2b4 VZ |
337 | separate from DM_SETDEFID stuff (!). Also note that BS_DEFPUSHBUTTON should |
338 | be unset if our parent window is not active so it should be unset whenever | |
339 | we lose activation and set back when we regain it. | |
d78f09e2 VZ |
340 | |
341 | Final complication is that when a button is active, it should be the default | |
342 | one, i.e. pressing Enter on a button always activates it and not another | |
343 | one. | |
344 | ||
345 | We handle this by maintaining a permanent and a temporary default items in | |
346 | wxControlContainer (both may be NULL). When a button becomes the current | |
347 | control (i.e. gets focus) it sets itself as the temporary default which | |
348 | ensures that it has the right appearance and that Enter will be redirected | |
349 | to it. When the button loses focus, it unsets the temporary default and so | |
350 | the default item will be the permanent default -- that is the default button | |
351 | if any had been set or none otherwise, which is just what we want. | |
352 | ||
7fb1b2b4 VZ |
353 | NB: all this is quite complicated by now and the worst is that normally |
354 | it shouldn't be necessary at all as for the normal Windows programs | |
355 | DefWindowProc() and IsDialogMessage() take care of all this | |
77ffb593 | 356 | automatically -- however in wxWidgets programs this doesn't work for |
7fb1b2b4 VZ |
357 | nested hierarchies (i.e. a notebook inside a notebook) for unknown |
358 | reason and so we have to reproduce all this code ourselves. It would be | |
359 | very nice if we could avoid doing it. | |
d78f09e2 VZ |
360 | */ |
361 | ||
036da5e3 | 362 | // set this button as the (permanently) default one in its panel |
94aff5ff | 363 | wxWindow *wxButton::SetDefault() |
2bda0e17 | 364 | { |
77ffb593 | 365 | // set this one as the default button both for wxWidgets ... |
94aff5ff | 366 | wxWindow *winOldDefault = wxButtonBase::SetDefault(); |
036da5e3 | 367 | |
7fb1b2b4 | 368 | // ... and Windows |
fcf90ee1 WS |
369 | SetDefaultStyle(wxDynamicCast(winOldDefault, wxButton), false); |
370 | SetDefaultStyle(this, true); | |
ebc0b155 VZ |
371 | |
372 | return winOldDefault; | |
036da5e3 VZ |
373 | } |
374 | ||
627c8d89 VZ |
375 | // return the top level parent window if it's not being deleted yet, otherwise |
376 | // return NULL | |
05c5f281 VZ |
377 | static wxTopLevelWindow *GetTLWParentIfNotBeingDeleted(wxWindow *win) |
378 | { | |
627c8d89 | 379 | for ( ;; ) |
05c5f281 | 380 | { |
627c8d89 VZ |
381 | // IsTopLevel() will return false for a wxTLW being deleted, so we also |
382 | // need the parent test for this case | |
383 | wxWindow * const parent = win->GetParent(); | |
384 | if ( !parent || win->IsTopLevel() ) | |
9617f65b VZ |
385 | { |
386 | if ( win->IsBeingDeleted() ) | |
387 | return NULL; | |
627c8d89 | 388 | |
05c5f281 | 389 | break; |
9617f65b | 390 | } |
627c8d89 VZ |
391 | |
392 | win = parent; | |
05c5f281 VZ |
393 | } |
394 | ||
395 | wxASSERT_MSG( win, _T("button without top level parent?") ); | |
396 | ||
627c8d89 VZ |
397 | wxTopLevelWindow * const tlw = wxDynamicCast(win, wxTopLevelWindow); |
398 | wxASSERT_MSG( tlw, _T("logic error in GetTLWParentIfNotBeingDeleted()") ); | |
399 | ||
400 | return tlw; | |
05c5f281 VZ |
401 | } |
402 | ||
7fb1b2b4 | 403 | // set this button as being currently default |
036da5e3 VZ |
404 | void wxButton::SetTmpDefault() |
405 | { | |
05c5f281 VZ |
406 | wxTopLevelWindow * const tlw = GetTLWParentIfNotBeingDeleted(GetParent()); |
407 | if ( !tlw ) | |
408 | return; | |
036da5e3 | 409 | |
6c20e8f8 VZ |
410 | wxWindow *winOldDefault = tlw->GetDefaultItem(); |
411 | tlw->SetTmpDefaultItem(this); | |
7fb1b2b4 | 412 | |
fcf90ee1 WS |
413 | SetDefaultStyle(wxDynamicCast(winOldDefault, wxButton), false); |
414 | SetDefaultStyle(this, true); | |
036da5e3 VZ |
415 | } |
416 | ||
7fb1b2b4 | 417 | // unset this button as currently default, it may still stay permanent default |
036da5e3 VZ |
418 | void wxButton::UnsetTmpDefault() |
419 | { | |
05c5f281 VZ |
420 | wxTopLevelWindow * const tlw = GetTLWParentIfNotBeingDeleted(GetParent()); |
421 | if ( !tlw ) | |
422 | return; | |
036da5e3 | 423 | |
6c20e8f8 | 424 | tlw->SetTmpDefaultItem(NULL); |
036da5e3 | 425 | |
6c20e8f8 | 426 | wxWindow *winOldDefault = tlw->GetDefaultItem(); |
7fb1b2b4 | 427 | |
fcf90ee1 WS |
428 | SetDefaultStyle(this, false); |
429 | SetDefaultStyle(wxDynamicCast(winOldDefault, wxButton), true); | |
036da5e3 | 430 | } |
8ed57d93 | 431 | |
036da5e3 VZ |
432 | /* static */ |
433 | void | |
7fb1b2b4 | 434 | wxButton::SetDefaultStyle(wxButton *btn, bool on) |
036da5e3 | 435 | { |
7fb1b2b4 VZ |
436 | // we may be called with NULL pointer -- simpler to do the check here than |
437 | // in the caller which does wxDynamicCast() | |
438 | if ( !btn ) | |
439 | return; | |
440 | ||
441 | // first, let DefDlgProc() know about the new default button | |
442 | if ( on ) | |
5d1d2d46 | 443 | { |
7fb1b2b4 VZ |
444 | // we shouldn't set BS_DEFPUSHBUTTON for any button if we don't have |
445 | // focus at all any more | |
446 | if ( !wxTheApp->IsActive() ) | |
447 | return; | |
cd0b1709 | 448 | |
6c20e8f8 VZ |
449 | wxWindow * const tlw = wxGetTopLevelParent(btn); |
450 | wxCHECK_RET( tlw, _T("button without top level window?") ); | |
cef55d64 | 451 | |
6c20e8f8 | 452 | ::SendMessage(GetHwndOf(tlw), DM_SETDEFID, btn->GetId(), 0L); |
cef55d64 VZ |
453 | |
454 | // sending DM_SETDEFID also changes the button style to | |
455 | // BS_DEFPUSHBUTTON so there is nothing more to do | |
5d1d2d46 VZ |
456 | } |
457 | ||
7fb1b2b4 VZ |
458 | // then also change the style as needed |
459 | long style = ::GetWindowLong(GetHwndOf(btn), GWL_STYLE); | |
460 | if ( !(style & BS_DEFPUSHBUTTON) == on ) | |
be4017f8 | 461 | { |
7fb1b2b4 VZ |
462 | // don't do it with the owner drawn buttons because it will |
463 | // reset BS_OWNERDRAW style bit too (as BS_OWNERDRAW & | |
464 | // BS_DEFPUSHBUTTON != 0)! | |
036da5e3 VZ |
465 | if ( (style & BS_OWNERDRAW) != BS_OWNERDRAW ) |
466 | { | |
7fb1b2b4 VZ |
467 | ::SendMessage(GetHwndOf(btn), BM_SETSTYLE, |
468 | on ? style | BS_DEFPUSHBUTTON | |
469 | : style & ~BS_DEFPUSHBUTTON, | |
470 | 1L /* redraw */); | |
036da5e3 | 471 | } |
7fb1b2b4 | 472 | else // owner drawn |
036da5e3 | 473 | { |
7fb1b2b4 VZ |
474 | // redraw the button - it will notice itself that it's |
475 | // [not] the default one [any longer] | |
476 | btn->Refresh(); | |
036da5e3 | 477 | } |
be4017f8 | 478 | } |
7fb1b2b4 | 479 | //else: already has correct style |
2bda0e17 KB |
480 | } |
481 | ||
edccf428 VZ |
482 | // ---------------------------------------------------------------------------- |
483 | // helpers | |
484 | // ---------------------------------------------------------------------------- | |
485 | ||
486 | bool wxButton::SendClickEvent() | |
2bda0e17 | 487 | { |
edccf428 VZ |
488 | wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED, GetId()); |
489 | event.SetEventObject(this); | |
490 | ||
491 | return ProcessCommand(event); | |
2bda0e17 KB |
492 | } |
493 | ||
edccf428 | 494 | void wxButton::Command(wxCommandEvent & event) |
2bda0e17 | 495 | { |
edccf428 | 496 | ProcessCommand(event); |
2bda0e17 KB |
497 | } |
498 | ||
edccf428 VZ |
499 | // ---------------------------------------------------------------------------- |
500 | // event/message handlers | |
501 | // ---------------------------------------------------------------------------- | |
2bda0e17 | 502 | |
33ac7e6f | 503 | bool wxButton::MSWCommand(WXUINT param, WXWORD WXUNUSED(id)) |
edccf428 | 504 | { |
fcf90ee1 | 505 | bool processed = false; |
57c0af52 | 506 | switch ( param ) |
edccf428 | 507 | { |
5aab763c RD |
508 | // NOTE: Apparently older versions (NT 4?) of the common controls send |
509 | // BN_DOUBLECLICKED but not a second BN_CLICKED for owner-drawn | |
87b6002d | 510 | // buttons, so in order to send two EVT_BUTTON events we should |
5aab763c RD |
511 | // catch both types. Currently (Feb 2003) up-to-date versions of |
512 | // win98, win2k and winXP all send two BN_CLICKED messages for | |
513 | // all button types, so we don't catch BN_DOUBLECLICKED anymore | |
514 | // in order to not get 3 EVT_BUTTON events. If this is a problem | |
515 | // then we need to figure out which version of the comctl32 changed | |
516 | // this behaviour and test for it. | |
517 | ||
a95e38c0 VZ |
518 | case 1: // message came from an accelerator |
519 | case BN_CLICKED: // normal buttons send this | |
57c0af52 VZ |
520 | processed = SendClickEvent(); |
521 | break; | |
edccf428 | 522 | } |
2bda0e17 | 523 | |
edccf428 | 524 | return processed; |
2bda0e17 KB |
525 | } |
526 | ||
c140b7e7 | 527 | WXLRESULT wxButton::MSWWindowProc(WXUINT nMsg, WXWPARAM wParam, WXLPARAM lParam) |
678cd6de | 528 | { |
87b6002d | 529 | // when we receive focus, we want to temporarily become the default button in |
036da5e3 VZ |
530 | // our parent panel so that pressing "Enter" would activate us -- and when |
531 | // losing it we should restore the previous default button as well | |
be4017f8 | 532 | if ( nMsg == WM_SETFOCUS ) |
678cd6de | 533 | { |
036da5e3 | 534 | SetTmpDefault(); |
be4017f8 | 535 | |
036da5e3 VZ |
536 | // let the default processing take place too |
537 | } | |
538 | else if ( nMsg == WM_KILLFOCUS ) | |
539 | { | |
540 | UnsetTmpDefault(); | |
678cd6de | 541 | } |
a95e38c0 VZ |
542 | else if ( nMsg == WM_LBUTTONDBLCLK ) |
543 | { | |
f6bcfd97 BP |
544 | // emulate a click event to force an owner-drawn button to change its |
545 | // appearance - without this, it won't do it | |
546 | (void)wxControl::MSWWindowProc(WM_LBUTTONDOWN, wParam, lParam); | |
547 | ||
036da5e3 | 548 | // and continue with processing the message normally as well |
a95e38c0 | 549 | } |
4e9da8b7 RD |
550 | #if wxUSE_UXTHEME |
551 | else if ( nMsg == WM_THEMECHANGED ) | |
552 | { | |
553 | // need to recalculate the best size here | |
554 | // as the theme size might have changed | |
555 | InvalidateBestSize(); | |
556 | } | |
557 | else if ( wxUxThemeEngine::GetIfActive() ) | |
558 | { | |
559 | // we need to Refresh() if mouse has entered or left window | |
560 | // so we can update the hot tracking state | |
561 | // must use m_mouseInWindow here instead of IsMouseInWindow() | |
562 | // since we need to know the first time the mouse enters the window | |
563 | // and IsMouseInWindow() would return true in this case | |
564 | if ( ( nMsg == WM_MOUSEMOVE && !m_mouseInWindow ) || | |
565 | nMsg == WM_MOUSELEAVE ) | |
566 | { | |
567 | Refresh(); | |
568 | } | |
569 | } | |
570 | #endif // wxUSE_UXTHEME | |
678cd6de VZ |
571 | |
572 | // let the base class do all real processing | |
573 | return wxControl::MSWWindowProc(nMsg, wParam, lParam); | |
574 | } | |
cd0b1709 VZ |
575 | |
576 | // ---------------------------------------------------------------------------- | |
577 | // owner-drawn buttons support | |
578 | // ---------------------------------------------------------------------------- | |
579 | ||
cd0b1709 VZ |
580 | // drawing helpers |
581 | ||
582 | static void DrawButtonText(HDC hdc, | |
583 | RECT *pRect, | |
584 | const wxString& text, | |
585 | COLORREF col) | |
586 | { | |
587 | COLORREF colOld = SetTextColor(hdc, col); | |
588 | int modeOld = SetBkMode(hdc, TRANSPARENT); | |
589 | ||
cbbb6724 VZ |
590 | if ( text.find(_T('\n')) != wxString::npos ) |
591 | { | |
592 | // draw multiline label | |
593 | ||
594 | // first we need to compute its bounding rect | |
595 | RECT rc; | |
596 | ::CopyRect(&rc, pRect); | |
e0a050e3 VS |
597 | ::DrawText(hdc, text.wx_str(), text.length(), &rc, |
598 | DT_CENTER | DT_CALCRECT); | |
cbbb6724 VZ |
599 | |
600 | // now center this rect inside the entire button area | |
601 | const LONG w = rc.right - rc.left; | |
602 | const LONG h = rc.bottom - rc.top; | |
603 | rc.left = (pRect->right - pRect->left)/2 - w/2; | |
604 | rc.right = rc.left+w; | |
605 | rc.top = (pRect->bottom - pRect->top)/2 - h/2; | |
606 | rc.bottom = rc.top+h; | |
607 | ||
e0a050e3 | 608 | ::DrawText(hdc, text.wx_str(), text.length(), &rc, DT_CENTER); |
cbbb6724 VZ |
609 | } |
610 | else // single line label | |
611 | { | |
612 | // Note: we must have DT_SINGLELINE for DT_VCENTER to work. | |
e0a050e3 | 613 | ::DrawText(hdc, text.wx_str(), text.length(), pRect, |
cbbb6724 VZ |
614 | DT_SINGLELINE | DT_CENTER | DT_VCENTER); |
615 | } | |
cd0b1709 VZ |
616 | |
617 | SetBkMode(hdc, modeOld); | |
618 | SetTextColor(hdc, colOld); | |
619 | } | |
620 | ||
621 | static void DrawRect(HDC hdc, const RECT& r) | |
622 | { | |
4676948b JS |
623 | wxDrawLine(hdc, r.left, r.top, r.right, r.top); |
624 | wxDrawLine(hdc, r.right, r.top, r.right, r.bottom); | |
625 | wxDrawLine(hdc, r.right, r.bottom, r.left, r.bottom); | |
626 | wxDrawLine(hdc, r.left, r.bottom, r.left, r.top); | |
cd0b1709 VZ |
627 | } |
628 | ||
629 | void wxButton::MakeOwnerDrawn() | |
630 | { | |
631 | long style = GetWindowLong(GetHwnd(), GWL_STYLE); | |
9750fc42 | 632 | if ( (style & BS_OWNERDRAW) != BS_OWNERDRAW ) |
cd0b1709 VZ |
633 | { |
634 | // make it so | |
635 | style |= BS_OWNERDRAW; | |
636 | SetWindowLong(GetHwnd(), GWL_STYLE, style); | |
637 | } | |
638 | } | |
639 | ||
640 | bool wxButton::SetBackgroundColour(const wxColour &colour) | |
641 | { | |
642 | if ( !wxControl::SetBackgroundColour(colour) ) | |
643 | { | |
644 | // nothing to do | |
fcf90ee1 | 645 | return false; |
cd0b1709 VZ |
646 | } |
647 | ||
648 | MakeOwnerDrawn(); | |
649 | ||
650 | Refresh(); | |
651 | ||
fcf90ee1 | 652 | return true; |
cd0b1709 VZ |
653 | } |
654 | ||
655 | bool wxButton::SetForegroundColour(const wxColour &colour) | |
656 | { | |
657 | if ( !wxControl::SetForegroundColour(colour) ) | |
658 | { | |
659 | // nothing to do | |
fcf90ee1 | 660 | return false; |
cd0b1709 VZ |
661 | } |
662 | ||
663 | MakeOwnerDrawn(); | |
664 | ||
665 | Refresh(); | |
666 | ||
fcf90ee1 | 667 | return true; |
cd0b1709 VZ |
668 | } |
669 | ||
670 | /* | |
671 | The button frame looks like this normally: | |
672 | ||
673 | WWWWWWWWWWWWWWWWWWB | |
16162a64 GRG |
674 | WHHHHHHHHHHHHHHHHGB W = white (HILIGHT) |
675 | WH GB H = light grey (LIGHT) | |
676 | WH GB G = dark grey (SHADOW) | |
677 | WH GB B = black (DKSHADOW) | |
678 | WH GB | |
cd0b1709 VZ |
679 | WGGGGGGGGGGGGGGGGGB |
680 | BBBBBBBBBBBBBBBBBBB | |
681 | ||
682 | When the button is selected, the button becomes like this (the total button | |
683 | size doesn't change): | |
684 | ||
685 | BBBBBBBBBBBBBBBBBBB | |
686 | BWWWWWWWWWWWWWWWWBB | |
16162a64 GRG |
687 | BWHHHHHHHHHHHHHHGBB |
688 | BWH GBB | |
689 | BWH GBB | |
cd0b1709 VZ |
690 | BWGGGGGGGGGGGGGGGBB |
691 | BBBBBBBBBBBBBBBBBBB | |
692 | BBBBBBBBBBBBBBBBBBB | |
693 | ||
694 | When the button is pushed (while selected) it is like: | |
695 | ||
696 | BBBBBBBBBBBBBBBBBBB | |
697 | BGGGGGGGGGGGGGGGGGB | |
698 | BG GB | |
699 | BG GB | |
700 | BG GB | |
16162a64 | 701 | BG GB |
cd0b1709 VZ |
702 | BGGGGGGGGGGGGGGGGGB |
703 | BBBBBBBBBBBBBBBBBBB | |
704 | */ | |
705 | ||
706 | static void DrawButtonFrame(HDC hdc, const RECT& rectBtn, | |
707 | bool selected, bool pushed) | |
708 | { | |
709 | RECT r; | |
710 | CopyRect(&r, &rectBtn); | |
711 | ||
16162a64 GRG |
712 | HPEN hpenBlack = CreatePen(PS_SOLID, 1, GetSysColor(COLOR_3DDKSHADOW)), |
713 | hpenGrey = CreatePen(PS_SOLID, 1, GetSysColor(COLOR_3DSHADOW)), | |
714 | hpenLightGr = CreatePen(PS_SOLID, 1, GetSysColor(COLOR_3DLIGHT)), | |
715 | hpenWhite = CreatePen(PS_SOLID, 1, GetSysColor(COLOR_3DHILIGHT)); | |
cd0b1709 VZ |
716 | |
717 | HPEN hpenOld = (HPEN)SelectObject(hdc, hpenBlack); | |
718 | ||
719 | r.right--; | |
720 | r.bottom--; | |
721 | ||
722 | if ( pushed ) | |
723 | { | |
724 | DrawRect(hdc, r); | |
725 | ||
726 | (void)SelectObject(hdc, hpenGrey); | |
fcf90ee1 | 727 | ::InflateRect(&r, -1, -1); |
cd0b1709 VZ |
728 | |
729 | DrawRect(hdc, r); | |
730 | } | |
731 | else // !pushed | |
732 | { | |
733 | if ( selected ) | |
734 | { | |
735 | DrawRect(hdc, r); | |
736 | ||
fcf90ee1 | 737 | ::InflateRect(&r, -1, -1); |
cd0b1709 VZ |
738 | } |
739 | ||
4676948b JS |
740 | wxDrawLine(hdc, r.left, r.bottom, r.right, r.bottom); |
741 | wxDrawLine(hdc, r.right, r.bottom, r.right, r.top - 1); | |
cd0b1709 VZ |
742 | |
743 | (void)SelectObject(hdc, hpenWhite); | |
4676948b JS |
744 | wxDrawLine(hdc, r.left, r.bottom - 1, r.left, r.top); |
745 | wxDrawLine(hdc, r.left, r.top, r.right, r.top); | |
cd0b1709 | 746 | |
16162a64 | 747 | (void)SelectObject(hdc, hpenLightGr); |
4676948b JS |
748 | wxDrawLine(hdc, r.left + 1, r.bottom - 2, r.left + 1, r.top + 1); |
749 | wxDrawLine(hdc, r.left + 1, r.top + 1, r.right - 1, r.top + 1); | |
16162a64 | 750 | |
cd0b1709 | 751 | (void)SelectObject(hdc, hpenGrey); |
4676948b JS |
752 | wxDrawLine(hdc, r.left + 1, r.bottom - 1, r.right - 1, r.bottom - 1); |
753 | wxDrawLine(hdc, r.right - 1, r.bottom - 1, r.right - 1, r.top); | |
cd0b1709 VZ |
754 | } |
755 | ||
756 | (void)SelectObject(hdc, hpenOld); | |
757 | DeleteObject(hpenWhite); | |
16162a64 | 758 | DeleteObject(hpenLightGr); |
cd0b1709 VZ |
759 | DeleteObject(hpenGrey); |
760 | DeleteObject(hpenBlack); | |
761 | } | |
762 | ||
4e9da8b7 RD |
763 | #if wxUSE_UXTHEME |
764 | static | |
765 | void MSWDrawXPBackground(wxButton *button, WXDRAWITEMSTRUCT *wxdis) | |
cd0b1709 VZ |
766 | { |
767 | LPDRAWITEMSTRUCT lpDIS = (LPDRAWITEMSTRUCT)wxdis; | |
4e9da8b7 RD |
768 | HDC hdc = lpDIS->hDC; |
769 | UINT state = lpDIS->itemState; | |
cd0b1709 VZ |
770 | RECT rectBtn; |
771 | CopyRect(&rectBtn, &lpDIS->rcItem); | |
772 | ||
4e9da8b7 RD |
773 | wxUxThemeHandle theme(button, L"BUTTON"); |
774 | int iState; | |
775 | ||
776 | if ( state & ODS_SELECTED ) | |
777 | { | |
778 | iState = PBS_PRESSED; | |
779 | } | |
780 | else if ( button->HasCapture() || button->IsMouseInWindow() ) | |
781 | { | |
782 | iState = PBS_HOT; | |
783 | } | |
784 | else if ( state & ODS_FOCUS ) | |
785 | { | |
786 | iState = PBS_DEFAULTED; | |
787 | } | |
788 | else if ( state & ODS_DISABLED ) | |
789 | { | |
790 | iState = PBS_DISABLED; | |
791 | } | |
792 | else | |
793 | { | |
794 | iState = PBS_NORMAL; | |
795 | } | |
cd0b1709 | 796 | |
4e9da8b7 RD |
797 | // draw parent background if needed |
798 | if ( wxUxThemeEngine::Get()->IsThemeBackgroundPartiallyTransparent(theme, | |
799 | BP_PUSHBUTTON, | |
800 | iState) ) | |
801 | { | |
802 | wxUxThemeEngine::Get()->DrawThemeParentBackground(GetHwndOf(button), hdc, &rectBtn); | |
803 | } | |
804 | ||
805 | // draw background | |
806 | wxUxThemeEngine::Get()->DrawThemeBackground(theme, hdc, BP_PUSHBUTTON, iState, | |
807 | &rectBtn, NULL); | |
808 | ||
809 | // calculate content area margins | |
810 | MARGINS margins; | |
811 | wxUxThemeEngine::Get()->GetThemeMargins(theme, hdc, BP_PUSHBUTTON, iState, | |
812 | TMT_CONTENTMARGINS, &rectBtn, &margins); | |
813 | RECT rectClient; | |
814 | ::CopyRect(&rectClient, &rectBtn); | |
815 | ::InflateRect(&rectClient, -margins.cxLeftWidth, -margins.cyTopHeight); | |
816 | ||
817 | // if focused and !nofocus rect | |
818 | if ( (state & ODS_FOCUS) && !(state & ODS_NOFOCUSRECT) ) | |
819 | { | |
820 | DrawFocusRect(hdc, &rectClient); | |
821 | } | |
822 | ||
823 | if ( button->UseBgCol() ) | |
824 | { | |
825 | COLORREF colBg = wxColourToRGB(button->GetBackgroundColour()); | |
826 | HBRUSH hbrushBackground = ::CreateSolidBrush(colBg); | |
827 | ||
828 | // don't overwrite the focus rect | |
829 | ::InflateRect(&rectClient, -1, -1); | |
830 | FillRect(hdc, &rectClient, hbrushBackground); | |
831 | ::DeleteObject(hbrushBackground); | |
832 | } | |
833 | } | |
834 | #endif // wxUSE_UXTHEME | |
835 | ||
836 | bool wxButton::MSWOnDraw(WXDRAWITEMSTRUCT *wxdis) | |
837 | { | |
838 | LPDRAWITEMSTRUCT lpDIS = (LPDRAWITEMSTRUCT)wxdis; | |
cd0b1709 VZ |
839 | HDC hdc = lpDIS->hDC; |
840 | UINT state = lpDIS->itemState; | |
4e9da8b7 RD |
841 | RECT rectBtn; |
842 | CopyRect(&rectBtn, &lpDIS->rcItem); | |
cd0b1709 | 843 | |
4e9da8b7 RD |
844 | #if wxUSE_UXTHEME |
845 | if ( wxUxThemeEngine::GetIfActive() ) | |
846 | { | |
847 | MSWDrawXPBackground(this, wxdis); | |
848 | } | |
849 | else | |
850 | #endif // wxUSE_UXTHEME | |
851 | { | |
852 | COLORREF colBg = wxColourToRGB(GetBackgroundColour()); | |
cd0b1709 | 853 | |
4e9da8b7 RD |
854 | // first, draw the background |
855 | HBRUSH hbrushBackground = ::CreateSolidBrush(colBg); | |
856 | FillRect(hdc, &rectBtn, hbrushBackground); | |
857 | ::DeleteObject(hbrushBackground); | |
cd0b1709 | 858 | |
4e9da8b7 RD |
859 | // draw the border for the current state |
860 | bool selected = (state & ODS_SELECTED) != 0; | |
861 | if ( !selected ) | |
cd0b1709 | 862 | { |
6c20e8f8 VZ |
863 | wxTopLevelWindow *tlw = wxDynamicCast(wxGetTopLevelParent(this), wxTopLevelWindow); |
864 | if ( tlw ) | |
4e9da8b7 | 865 | { |
6c20e8f8 | 866 | selected = tlw->GetDefaultItem() == this; |
4e9da8b7 | 867 | } |
cd0b1709 | 868 | } |
4e9da8b7 | 869 | bool pushed = (SendMessage(GetHwnd(), BM_GETSTATE, 0, 0) & BST_PUSHED) != 0; |
cd0b1709 | 870 | |
4e9da8b7 | 871 | DrawButtonFrame(hdc, rectBtn, selected, pushed); |
cd0b1709 | 872 | |
4e9da8b7 RD |
873 | // if focused and !nofocus rect |
874 | if ( (state & ODS_FOCUS) && !(state & ODS_NOFOCUSRECT) ) | |
875 | { | |
876 | RECT rectFocus; | |
877 | CopyRect(&rectFocus, &rectBtn); | |
cd0b1709 | 878 | |
4e9da8b7 RD |
879 | // I don't know where does this constant come from, but this is how |
880 | // Windows draws them | |
881 | InflateRect(&rectFocus, -4, -4); | |
cd0b1709 | 882 | |
4e9da8b7 RD |
883 | DrawFocusRect(hdc, &rectFocus); |
884 | } | |
cd0b1709 | 885 | |
4e9da8b7 RD |
886 | if ( pushed ) |
887 | { | |
888 | // the label is shifted by 1 pixel to create "pushed" effect | |
889 | OffsetRect(&rectBtn, 1, 1); | |
890 | } | |
9750fc42 VZ |
891 | } |
892 | ||
4e9da8b7 | 893 | COLORREF colFg = wxColourToRGB(GetForegroundColour()); |
047aa296 CE |
894 | if ( state & ODS_DISABLED ) colFg = GetSysColor(COLOR_GRAYTEXT) ; |
895 | wxString label = GetLabel(); | |
896 | if ( state & ODS_NOACCEL ) label = GetLabelText() ; | |
897 | DrawButtonText(hdc, &rectBtn, label, colFg); | |
cd0b1709 | 898 | |
fcf90ee1 | 899 | return true; |
cd0b1709 VZ |
900 | } |
901 | ||
1e6feb95 | 902 | #endif // wxUSE_BUTTON |
4af4dec6 | 903 |