]>
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; |
8cc4850c | 186 | #ifndef __WXWINCE__ |
8a094d7b JS |
187 | // flat 2d buttons |
188 | if ( style & wxNO_BORDER ) | |
189 | msStyle |= BS_FLAT; | |
8cc4850c | 190 | #endif // __WXWINCE__ |
5b2f31eb | 191 | #endif // __WIN32__ |
edccf428 | 192 | |
5b2f31eb | 193 | return msStyle; |
2bda0e17 KB |
194 | } |
195 | ||
edccf428 VZ |
196 | // ---------------------------------------------------------------------------- |
197 | // size management including autosizing | |
198 | // ---------------------------------------------------------------------------- | |
199 | ||
f68586e5 | 200 | wxSize wxButton::DoGetBestSize() const |
2bda0e17 | 201 | { |
4438caf4 | 202 | int wBtn; |
1e023926 | 203 | GetTextExtent(wxGetWindowText(GetHWND()), &wBtn, NULL); |
edccf428 | 204 | |
4438caf4 VZ |
205 | int wChar, hChar; |
206 | wxGetCharSize(GetHWND(), &wChar, &hChar, &GetFont()); | |
207 | ||
1e023926 | 208 | // add a margin -- the button is wider than just its label |
4438caf4 VZ |
209 | wBtn += 3*wChar; |
210 | ||
211 | // the button height is proportional to the height of the font used | |
212 | int hBtn = BUTTON_HEIGHT_FROM_CHAR_HEIGHT(hChar); | |
edccf428 | 213 | |
1e023926 VZ |
214 | // all buttons have at least the standard size unless the user explicitly |
215 | // wants them to be of smaller size and used wxBU_EXACTFIT style when | |
216 | // creating the button | |
217 | if ( !HasFlag(wxBU_EXACTFIT) ) | |
188b9f2e RD |
218 | { |
219 | wxSize sz = GetDefaultSize(); | |
1e023926 VZ |
220 | if (wBtn > sz.x) |
221 | sz.x = wBtn; | |
222 | if (hBtn > sz.y) | |
223 | sz.y = hBtn; | |
224 | ||
188b9f2e RD |
225 | return sz; |
226 | } | |
a63f2bec | 227 | |
1e023926 | 228 | return wxSize(wBtn, hBtn); |
2bda0e17 KB |
229 | } |
230 | ||
e1f36ff8 | 231 | /* static */ |
1e6feb95 | 232 | wxSize wxButtonBase::GetDefaultSize() |
e1f36ff8 | 233 | { |
8c3c31d4 | 234 | static wxSize s_sizeBtn; |
e1f36ff8 | 235 | |
8c3c31d4 VZ |
236 | if ( s_sizeBtn.x == 0 ) |
237 | { | |
238 | wxScreenDC dc; | |
a756f210 | 239 | dc.SetFont(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT)); |
8c3c31d4 VZ |
240 | |
241 | // the size of a standard button in the dialog units is 50x14, | |
242 | // translate this to pixels | |
243 | // NB1: the multipliers come from the Windows convention | |
244 | // NB2: the extra +1/+2 were needed to get the size be the same as the | |
245 | // size of the buttons in the standard dialog - I don't know how | |
246 | // this happens, but on my system this size is 75x23 in pixels and | |
247 | // 23*8 isn't even divisible by 14... Would be nice to understand | |
248 | // why these constants are needed though! | |
249 | s_sizeBtn.x = (50 * (dc.GetCharWidth() + 1))/4; | |
250 | s_sizeBtn.y = ((14 * dc.GetCharHeight()) + 2)/8; | |
251 | } | |
e1f36ff8 | 252 | |
8c3c31d4 | 253 | return s_sizeBtn; |
e1f36ff8 VZ |
254 | } |
255 | ||
4438caf4 | 256 | // ---------------------------------------------------------------------------- |
036da5e3 | 257 | // default button handling |
4438caf4 VZ |
258 | // ---------------------------------------------------------------------------- |
259 | ||
d78f09e2 VZ |
260 | /* |
261 | "Everything you ever wanted to know about the default buttons" or "Why do we | |
262 | have to do all this?" | |
263 | ||
264 | In MSW the default button should be activated when the user presses Enter | |
265 | and the current control doesn't process Enter itself somehow. This is | |
266 | handled by ::DefWindowProc() (or maybe ::DefDialogProc()) using DM_SETDEFID | |
267 | Another aspect of "defaultness" is that the default button has different | |
268 | appearance: this is due to BS_DEFPUSHBUTTON style which is completely | |
7fb1b2b4 VZ |
269 | separate from DM_SETDEFID stuff (!). Also note that BS_DEFPUSHBUTTON should |
270 | be unset if our parent window is not active so it should be unset whenever | |
271 | we lose activation and set back when we regain it. | |
d78f09e2 VZ |
272 | |
273 | Final complication is that when a button is active, it should be the default | |
274 | one, i.e. pressing Enter on a button always activates it and not another | |
275 | one. | |
276 | ||
277 | We handle this by maintaining a permanent and a temporary default items in | |
278 | wxControlContainer (both may be NULL). When a button becomes the current | |
279 | control (i.e. gets focus) it sets itself as the temporary default which | |
280 | ensures that it has the right appearance and that Enter will be redirected | |
281 | to it. When the button loses focus, it unsets the temporary default and so | |
282 | the default item will be the permanent default -- that is the default button | |
283 | if any had been set or none otherwise, which is just what we want. | |
284 | ||
7fb1b2b4 VZ |
285 | NB: all this is quite complicated by now and the worst is that normally |
286 | it shouldn't be necessary at all as for the normal Windows programs | |
287 | DefWindowProc() and IsDialogMessage() take care of all this | |
288 | automatically -- however in wxWindows programs this doesn't work for | |
289 | nested hierarchies (i.e. a notebook inside a notebook) for unknown | |
290 | reason and so we have to reproduce all this code ourselves. It would be | |
291 | very nice if we could avoid doing it. | |
d78f09e2 VZ |
292 | */ |
293 | ||
036da5e3 | 294 | // set this button as the (permanently) default one in its panel |
edccf428 | 295 | void wxButton::SetDefault() |
2bda0e17 | 296 | { |
edccf428 | 297 | wxWindow *parent = GetParent(); |
2bda0e17 | 298 | |
036da5e3 VZ |
299 | wxCHECK_RET( parent, _T("button without parent?") ); |
300 | ||
7fb1b2b4 | 301 | // set this one as the default button both for wxWindows ... |
036da5e3 | 302 | wxWindow *winOldDefault = parent->SetDefaultItem(this); |
036da5e3 | 303 | |
7fb1b2b4 VZ |
304 | // ... and Windows |
305 | SetDefaultStyle(wxDynamicCast(winOldDefault, wxButton), FALSE); | |
306 | SetDefaultStyle(this, TRUE); | |
036da5e3 VZ |
307 | } |
308 | ||
7fb1b2b4 | 309 | // set this button as being currently default |
036da5e3 VZ |
310 | void wxButton::SetTmpDefault() |
311 | { | |
312 | wxWindow *parent = GetParent(); | |
313 | ||
314 | wxCHECK_RET( parent, _T("button without parent?") ); | |
315 | ||
316 | wxWindow *winOldDefault = parent->GetDefaultItem(); | |
317 | parent->SetTmpDefaultItem(this); | |
7fb1b2b4 VZ |
318 | |
319 | SetDefaultStyle(wxDynamicCast(winOldDefault, wxButton), FALSE); | |
320 | SetDefaultStyle(this, TRUE); | |
036da5e3 VZ |
321 | } |
322 | ||
7fb1b2b4 | 323 | // unset this button as currently default, it may still stay permanent default |
036da5e3 VZ |
324 | void wxButton::UnsetTmpDefault() |
325 | { | |
326 | wxWindow *parent = GetParent(); | |
327 | ||
328 | wxCHECK_RET( parent, _T("button without parent?") ); | |
329 | ||
330 | parent->SetTmpDefaultItem(NULL); | |
331 | ||
332 | wxWindow *winOldDefault = parent->GetDefaultItem(); | |
7fb1b2b4 VZ |
333 | |
334 | SetDefaultStyle(this, FALSE); | |
335 | SetDefaultStyle(wxDynamicCast(winOldDefault, wxButton), TRUE); | |
036da5e3 | 336 | } |
8ed57d93 | 337 | |
036da5e3 VZ |
338 | /* static */ |
339 | void | |
7fb1b2b4 | 340 | wxButton::SetDefaultStyle(wxButton *btn, bool on) |
036da5e3 | 341 | { |
7fb1b2b4 VZ |
342 | // we may be called with NULL pointer -- simpler to do the check here than |
343 | // in the caller which does wxDynamicCast() | |
344 | if ( !btn ) | |
345 | return; | |
346 | ||
347 | // first, let DefDlgProc() know about the new default button | |
348 | if ( on ) | |
5d1d2d46 | 349 | { |
7fb1b2b4 VZ |
350 | // we shouldn't set BS_DEFPUSHBUTTON for any button if we don't have |
351 | // focus at all any more | |
352 | if ( !wxTheApp->IsActive() ) | |
353 | return; | |
cd0b1709 | 354 | |
7fb1b2b4 VZ |
355 | // look for a panel-like window |
356 | wxWindow *win = btn->GetParent(); | |
357 | while ( win && !win->HasFlag(wxTAB_TRAVERSAL) ) | |
358 | win = win->GetParent(); | |
359 | ||
360 | if ( win ) | |
be4017f8 | 361 | { |
7fb1b2b4 VZ |
362 | ::SendMessage(GetHwndOf(win), DM_SETDEFID, btn->GetId(), 0L); |
363 | ||
364 | // sending DM_SETDEFID also changes the button style to | |
365 | // BS_DEFPUSHBUTTON so there is nothing more to do | |
be4017f8 | 366 | } |
5d1d2d46 VZ |
367 | } |
368 | ||
7fb1b2b4 VZ |
369 | // then also change the style as needed |
370 | long style = ::GetWindowLong(GetHwndOf(btn), GWL_STYLE); | |
371 | if ( !(style & BS_DEFPUSHBUTTON) == on ) | |
be4017f8 | 372 | { |
7fb1b2b4 VZ |
373 | // don't do it with the owner drawn buttons because it will |
374 | // reset BS_OWNERDRAW style bit too (as BS_OWNERDRAW & | |
375 | // BS_DEFPUSHBUTTON != 0)! | |
036da5e3 VZ |
376 | if ( (style & BS_OWNERDRAW) != BS_OWNERDRAW ) |
377 | { | |
7fb1b2b4 VZ |
378 | ::SendMessage(GetHwndOf(btn), BM_SETSTYLE, |
379 | on ? style | BS_DEFPUSHBUTTON | |
380 | : style & ~BS_DEFPUSHBUTTON, | |
381 | 1L /* redraw */); | |
036da5e3 | 382 | } |
7fb1b2b4 | 383 | else // owner drawn |
036da5e3 | 384 | { |
7fb1b2b4 VZ |
385 | // redraw the button - it will notice itself that it's |
386 | // [not] the default one [any longer] | |
387 | btn->Refresh(); | |
036da5e3 | 388 | } |
be4017f8 | 389 | } |
7fb1b2b4 | 390 | //else: already has correct style |
2bda0e17 KB |
391 | } |
392 | ||
edccf428 VZ |
393 | // ---------------------------------------------------------------------------- |
394 | // helpers | |
395 | // ---------------------------------------------------------------------------- | |
396 | ||
397 | bool wxButton::SendClickEvent() | |
2bda0e17 | 398 | { |
edccf428 VZ |
399 | wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED, GetId()); |
400 | event.SetEventObject(this); | |
401 | ||
402 | return ProcessCommand(event); | |
2bda0e17 KB |
403 | } |
404 | ||
edccf428 | 405 | void wxButton::Command(wxCommandEvent & event) |
2bda0e17 | 406 | { |
edccf428 | 407 | ProcessCommand(event); |
2bda0e17 KB |
408 | } |
409 | ||
edccf428 VZ |
410 | // ---------------------------------------------------------------------------- |
411 | // event/message handlers | |
412 | // ---------------------------------------------------------------------------- | |
2bda0e17 | 413 | |
33ac7e6f | 414 | bool wxButton::MSWCommand(WXUINT param, WXWORD WXUNUSED(id)) |
edccf428 VZ |
415 | { |
416 | bool processed = FALSE; | |
57c0af52 | 417 | switch ( param ) |
edccf428 | 418 | { |
5aab763c RD |
419 | // NOTE: Apparently older versions (NT 4?) of the common controls send |
420 | // BN_DOUBLECLICKED but not a second BN_CLICKED for owner-drawn | |
421 | // buttons, so in order to send two EVET_BUTTON events we should | |
422 | // catch both types. Currently (Feb 2003) up-to-date versions of | |
423 | // win98, win2k and winXP all send two BN_CLICKED messages for | |
424 | // all button types, so we don't catch BN_DOUBLECLICKED anymore | |
425 | // in order to not get 3 EVT_BUTTON events. If this is a problem | |
426 | // then we need to figure out which version of the comctl32 changed | |
427 | // this behaviour and test for it. | |
428 | ||
a95e38c0 VZ |
429 | case 1: // message came from an accelerator |
430 | case BN_CLICKED: // normal buttons send this | |
57c0af52 VZ |
431 | processed = SendClickEvent(); |
432 | break; | |
edccf428 | 433 | } |
2bda0e17 | 434 | |
edccf428 | 435 | return processed; |
2bda0e17 KB |
436 | } |
437 | ||
c140b7e7 | 438 | WXLRESULT wxButton::MSWWindowProc(WXUINT nMsg, WXWPARAM wParam, WXLPARAM lParam) |
678cd6de | 439 | { |
036da5e3 VZ |
440 | // when we receive focus, we want to temporary become the default button in |
441 | // our parent panel so that pressing "Enter" would activate us -- and when | |
442 | // losing it we should restore the previous default button as well | |
be4017f8 | 443 | if ( nMsg == WM_SETFOCUS ) |
678cd6de | 444 | { |
036da5e3 | 445 | SetTmpDefault(); |
be4017f8 | 446 | |
036da5e3 VZ |
447 | // let the default processing take place too |
448 | } | |
449 | else if ( nMsg == WM_KILLFOCUS ) | |
450 | { | |
451 | UnsetTmpDefault(); | |
678cd6de | 452 | } |
a95e38c0 VZ |
453 | else if ( nMsg == WM_LBUTTONDBLCLK ) |
454 | { | |
f6bcfd97 BP |
455 | // emulate a click event to force an owner-drawn button to change its |
456 | // appearance - without this, it won't do it | |
457 | (void)wxControl::MSWWindowProc(WM_LBUTTONDOWN, wParam, lParam); | |
458 | ||
036da5e3 | 459 | // and continue with processing the message normally as well |
a95e38c0 | 460 | } |
678cd6de VZ |
461 | |
462 | // let the base class do all real processing | |
463 | return wxControl::MSWWindowProc(nMsg, wParam, lParam); | |
464 | } | |
cd0b1709 VZ |
465 | |
466 | // ---------------------------------------------------------------------------- | |
467 | // owner-drawn buttons support | |
468 | // ---------------------------------------------------------------------------- | |
469 | ||
470 | #ifdef __WIN32__ | |
471 | ||
472 | // drawing helpers | |
473 | ||
474 | static void DrawButtonText(HDC hdc, | |
475 | RECT *pRect, | |
476 | const wxString& text, | |
477 | COLORREF col) | |
478 | { | |
479 | COLORREF colOld = SetTextColor(hdc, col); | |
480 | int modeOld = SetBkMode(hdc, TRANSPARENT); | |
481 | ||
91f2c154 JS |
482 | // Note: we must have DT_SINGLELINE for DT_VCENTER to work. |
483 | ::DrawText(hdc, text, text.length(), pRect, DT_SINGLELINE | DT_CENTER | DT_VCENTER); | |
cd0b1709 VZ |
484 | |
485 | SetBkMode(hdc, modeOld); | |
486 | SetTextColor(hdc, colOld); | |
487 | } | |
488 | ||
489 | static void DrawRect(HDC hdc, const RECT& r) | |
490 | { | |
4676948b JS |
491 | wxDrawLine(hdc, r.left, r.top, r.right, r.top); |
492 | wxDrawLine(hdc, r.right, r.top, r.right, r.bottom); | |
493 | wxDrawLine(hdc, r.right, r.bottom, r.left, r.bottom); | |
494 | wxDrawLine(hdc, r.left, r.bottom, r.left, r.top); | |
cd0b1709 VZ |
495 | } |
496 | ||
497 | void wxButton::MakeOwnerDrawn() | |
498 | { | |
499 | long style = GetWindowLong(GetHwnd(), GWL_STYLE); | |
9750fc42 | 500 | if ( (style & BS_OWNERDRAW) != BS_OWNERDRAW ) |
cd0b1709 VZ |
501 | { |
502 | // make it so | |
503 | style |= BS_OWNERDRAW; | |
504 | SetWindowLong(GetHwnd(), GWL_STYLE, style); | |
505 | } | |
506 | } | |
507 | ||
508 | bool wxButton::SetBackgroundColour(const wxColour &colour) | |
509 | { | |
510 | if ( !wxControl::SetBackgroundColour(colour) ) | |
511 | { | |
512 | // nothing to do | |
513 | return FALSE; | |
514 | } | |
515 | ||
516 | MakeOwnerDrawn(); | |
517 | ||
518 | Refresh(); | |
519 | ||
520 | return TRUE; | |
521 | } | |
522 | ||
523 | bool wxButton::SetForegroundColour(const wxColour &colour) | |
524 | { | |
525 | if ( !wxControl::SetForegroundColour(colour) ) | |
526 | { | |
527 | // nothing to do | |
528 | return FALSE; | |
529 | } | |
530 | ||
531 | MakeOwnerDrawn(); | |
532 | ||
533 | Refresh(); | |
534 | ||
535 | return TRUE; | |
536 | } | |
537 | ||
538 | /* | |
539 | The button frame looks like this normally: | |
540 | ||
541 | WWWWWWWWWWWWWWWWWWB | |
16162a64 GRG |
542 | WHHHHHHHHHHHHHHHHGB W = white (HILIGHT) |
543 | WH GB H = light grey (LIGHT) | |
544 | WH GB G = dark grey (SHADOW) | |
545 | WH GB B = black (DKSHADOW) | |
546 | WH GB | |
cd0b1709 VZ |
547 | WGGGGGGGGGGGGGGGGGB |
548 | BBBBBBBBBBBBBBBBBBB | |
549 | ||
550 | When the button is selected, the button becomes like this (the total button | |
551 | size doesn't change): | |
552 | ||
553 | BBBBBBBBBBBBBBBBBBB | |
554 | BWWWWWWWWWWWWWWWWBB | |
16162a64 GRG |
555 | BWHHHHHHHHHHHHHHGBB |
556 | BWH GBB | |
557 | BWH GBB | |
cd0b1709 VZ |
558 | BWGGGGGGGGGGGGGGGBB |
559 | BBBBBBBBBBBBBBBBBBB | |
560 | BBBBBBBBBBBBBBBBBBB | |
561 | ||
562 | When the button is pushed (while selected) it is like: | |
563 | ||
564 | BBBBBBBBBBBBBBBBBBB | |
565 | BGGGGGGGGGGGGGGGGGB | |
566 | BG GB | |
567 | BG GB | |
568 | BG GB | |
16162a64 | 569 | BG GB |
cd0b1709 VZ |
570 | BGGGGGGGGGGGGGGGGGB |
571 | BBBBBBBBBBBBBBBBBBB | |
572 | */ | |
573 | ||
574 | static void DrawButtonFrame(HDC hdc, const RECT& rectBtn, | |
575 | bool selected, bool pushed) | |
576 | { | |
577 | RECT r; | |
578 | CopyRect(&r, &rectBtn); | |
579 | ||
16162a64 GRG |
580 | HPEN hpenBlack = CreatePen(PS_SOLID, 1, GetSysColor(COLOR_3DDKSHADOW)), |
581 | hpenGrey = CreatePen(PS_SOLID, 1, GetSysColor(COLOR_3DSHADOW)), | |
582 | hpenLightGr = CreatePen(PS_SOLID, 1, GetSysColor(COLOR_3DLIGHT)), | |
583 | hpenWhite = CreatePen(PS_SOLID, 1, GetSysColor(COLOR_3DHILIGHT)); | |
cd0b1709 VZ |
584 | |
585 | HPEN hpenOld = (HPEN)SelectObject(hdc, hpenBlack); | |
586 | ||
587 | r.right--; | |
588 | r.bottom--; | |
589 | ||
590 | if ( pushed ) | |
591 | { | |
592 | DrawRect(hdc, r); | |
593 | ||
594 | (void)SelectObject(hdc, hpenGrey); | |
595 | InflateRect(&r, -1, -1); | |
596 | ||
597 | DrawRect(hdc, r); | |
598 | } | |
599 | else // !pushed | |
600 | { | |
601 | if ( selected ) | |
602 | { | |
603 | DrawRect(hdc, r); | |
604 | ||
605 | InflateRect(&r, -1, -1); | |
606 | } | |
607 | ||
4676948b JS |
608 | wxDrawLine(hdc, r.left, r.bottom, r.right, r.bottom); |
609 | wxDrawLine(hdc, r.right, r.bottom, r.right, r.top - 1); | |
cd0b1709 VZ |
610 | |
611 | (void)SelectObject(hdc, hpenWhite); | |
4676948b JS |
612 | wxDrawLine(hdc, r.left, r.bottom - 1, r.left, r.top); |
613 | wxDrawLine(hdc, r.left, r.top, r.right, r.top); | |
cd0b1709 | 614 | |
16162a64 | 615 | (void)SelectObject(hdc, hpenLightGr); |
4676948b JS |
616 | wxDrawLine(hdc, r.left + 1, r.bottom - 2, r.left + 1, r.top + 1); |
617 | wxDrawLine(hdc, r.left + 1, r.top + 1, r.right - 1, r.top + 1); | |
16162a64 | 618 | |
cd0b1709 | 619 | (void)SelectObject(hdc, hpenGrey); |
4676948b JS |
620 | wxDrawLine(hdc, r.left + 1, r.bottom - 1, r.right - 1, r.bottom - 1); |
621 | wxDrawLine(hdc, r.right - 1, r.bottom - 1, r.right - 1, r.top); | |
cd0b1709 VZ |
622 | } |
623 | ||
624 | (void)SelectObject(hdc, hpenOld); | |
625 | DeleteObject(hpenWhite); | |
16162a64 | 626 | DeleteObject(hpenLightGr); |
cd0b1709 VZ |
627 | DeleteObject(hpenGrey); |
628 | DeleteObject(hpenBlack); | |
629 | } | |
630 | ||
631 | bool wxButton::MSWOnDraw(WXDRAWITEMSTRUCT *wxdis) | |
632 | { | |
633 | LPDRAWITEMSTRUCT lpDIS = (LPDRAWITEMSTRUCT)wxdis; | |
634 | ||
635 | RECT rectBtn; | |
636 | CopyRect(&rectBtn, &lpDIS->rcItem); | |
637 | ||
638 | COLORREF colBg = wxColourToRGB(GetBackgroundColour()), | |
639 | colFg = wxColourToRGB(GetForegroundColour()); | |
640 | ||
641 | HDC hdc = lpDIS->hDC; | |
642 | UINT state = lpDIS->itemState; | |
643 | ||
644 | // first, draw the background | |
645 | HBRUSH hbrushBackground = ::CreateSolidBrush(colBg); | |
646 | ||
647 | FillRect(hdc, &rectBtn, hbrushBackground); | |
648 | ||
649 | // draw the border for the current state | |
650 | bool selected = (state & ODS_SELECTED) != 0; | |
651 | if ( !selected ) | |
652 | { | |
653 | wxPanel *panel = wxDynamicCast(GetParent(), wxPanel); | |
654 | if ( panel ) | |
655 | { | |
656 | selected = panel->GetDefaultItem() == this; | |
657 | } | |
658 | } | |
659 | bool pushed = (SendMessage(GetHwnd(), BM_GETSTATE, 0, 0) & BST_PUSHED) != 0; | |
660 | ||
661 | DrawButtonFrame(hdc, rectBtn, selected, pushed); | |
662 | ||
663 | // draw the focus rect if needed | |
664 | if ( state & ODS_FOCUS ) | |
665 | { | |
666 | RECT rectFocus; | |
667 | CopyRect(&rectFocus, &rectBtn); | |
668 | ||
669 | // I don't know where does this constant come from, but this is how | |
670 | // Windows draws them | |
671 | InflateRect(&rectFocus, -4, -4); | |
672 | ||
673 | DrawFocusRect(hdc, &rectFocus); | |
674 | } | |
675 | ||
9750fc42 VZ |
676 | if ( pushed ) |
677 | { | |
678 | // the label is shifted by 1 pixel to create "pushed" effect | |
679 | OffsetRect(&rectBtn, 1, 1); | |
680 | } | |
681 | ||
cd0b1709 VZ |
682 | DrawButtonText(hdc, &rectBtn, GetLabel(), |
683 | state & ODS_DISABLED ? GetSysColor(COLOR_GRAYTEXT) | |
684 | : colFg); | |
685 | ||
686 | ::DeleteObject(hbrushBackground); | |
687 | ||
cd0b1709 VZ |
688 | return TRUE; |
689 | } | |
690 | ||
691 | #endif // __WIN32__ | |
1e6feb95 VZ |
692 | |
693 | #endif // wxUSE_BUTTON | |
694 |