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