]> git.saurik.com Git - wxWidgets.git/blame - src/msw/button.cpp
aligning definition of rtti
[wxWidgets.git] / src / msw / button.cpp
CommitLineData
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"
d2bc8725
PC
40 #include "wx/msw/wrapcctl.h"
41 #include "wx/msw/private.h"
42 #include "wx/msw/missing.h"
2bda0e17
KB
43#endif
44
bdf14bff 45#include "wx/imaglist.h"
5f7bcb48 46#include "wx/stockitem.h"
533171c2 47#include "wx/msw/private/button.h"
d8c89c48 48#include "wx/msw/private/dc.h"
5c3c1372 49#include "wx/private/window.h"
d8c89c48
VZ
50
51using namespace wxMSWImpl;
2bda0e17 52
4e9da8b7
RD
53#if wxUSE_UXTHEME
54 #include "wx/msw/uxtheme.h"
55
56 // no need to include tmschema.h
57 #ifndef BP_PUSHBUTTON
58 #define BP_PUSHBUTTON 1
59
60 #define PBS_NORMAL 1
61 #define PBS_HOT 2
62 #define PBS_PRESSED 3
63 #define PBS_DISABLED 4
64 #define PBS_DEFAULTED 5
65
66 #define TMT_CONTENTMARGINS 3602
67 #endif
2352862a 68
233f10bf
VZ
69 // provide the necessary declarations ourselves if they're missing from
70 // headers
2352862a
VZ
71 #ifndef BCM_SETIMAGELIST
72 #define BCM_SETIMAGELIST 0x1602
73 #define BCM_SETTEXTMARGIN 0x1604
233f10bf
VZ
74
75 enum
76 {
77 BUTTON_IMAGELIST_ALIGN_LEFT,
78 BUTTON_IMAGELIST_ALIGN_RIGHT,
79 BUTTON_IMAGELIST_ALIGN_TOP,
80 BUTTON_IMAGELIST_ALIGN_BOTTOM
81 };
82
83 struct BUTTON_IMAGELIST
84 {
85 HIMAGELIST himl;
86 RECT margin;
87 UINT uAlign;
88 };
2352862a 89 #endif
4e9da8b7
RD
90#endif // wxUSE_UXTHEME
91
92#ifndef WM_THEMECHANGED
93 #define WM_THEMECHANGED 0x031A
94#endif
95
96#ifndef ODS_NOACCEL
97 #define ODS_NOACCEL 0x0100
98#endif
99
100#ifndef ODS_NOFOCUSRECT
101 #define ODS_NOFOCUSRECT 0x0200
102#endif
103
f915b92e
VZ
104#ifndef DT_HIDEPREFIX
105 #define DT_HIDEPREFIX 0x00100000
106#endif
107
f2d7fdf7
VZ
108// set the value for BCM_SETSHIELD (for the UAC shield) if it's not defined in
109// the header
110#ifndef BCM_SETSHIELD
111 #define BCM_SETSHIELD 0x160c
112#endif
113
233f10bf
VZ
114// ----------------------------------------------------------------------------
115// button image data
116// ----------------------------------------------------------------------------
117
118// we use different data classes for owner drawn buttons and for themed XP ones
119
120class wxButtonImageData
121{
122public:
123 wxButtonImageData() { }
8f4745fe 124 virtual ~wxButtonImageData() { }
233f10bf
VZ
125
126 virtual wxBitmap GetBitmap(wxButton::State which) const = 0;
127 virtual void SetBitmap(const wxBitmap& bitmap, wxButton::State which) = 0;
128
129 virtual wxSize GetBitmapMargins() const = 0;
130 virtual void SetBitmapMargins(wxCoord x, wxCoord y) = 0;
131
8f4745fe 132 virtual wxDirection GetBitmapPosition() const = 0;
233f10bf
VZ
133 virtual void SetBitmapPosition(wxDirection dir) = 0;
134
135private:
136 wxDECLARE_NO_COPY_CLASS(wxButtonImageData);
137};
138
139namespace
140{
141
8f4745fe
VZ
142// the gap between button edge and the interior area used by Windows for the
143// standard buttons
144const int OD_BUTTON_MARGIN = 4;
145
233f10bf
VZ
146class wxODButtonImageData : public wxButtonImageData
147{
148public:
d5b98eb9 149 wxODButtonImageData(wxButton *btn, const wxBitmap& bitmap)
8f4745fe 150 {
d5b98eb9
VZ
151 SetBitmap(bitmap, wxButton::State_Normal);
152
8f4745fe
VZ
153 m_dir = wxLEFT;
154
a6fd73d3
VZ
155 // we use margins when we have both bitmap and text, but when we have
156 // only the bitmap it should take up the entire button area
a2117591 157 if ( btn->ShowsLabel() )
a6fd73d3
VZ
158 {
159 m_margin.x = btn->GetCharWidth();
160 m_margin.y = btn->GetCharHeight() / 2;
161 }
8f4745fe 162 }
233f10bf
VZ
163
164 virtual wxBitmap GetBitmap(wxButton::State which) const
165 {
166 return m_bitmaps[which];
167 }
168
169 virtual void SetBitmap(const wxBitmap& bitmap, wxButton::State which)
170 {
171 m_bitmaps[which] = bitmap;
172 }
173
174 virtual wxSize GetBitmapMargins() const
175 {
a6fd73d3 176 return m_margin;
233f10bf
VZ
177 }
178
179 virtual void SetBitmapMargins(wxCoord x, wxCoord y)
180 {
181 m_margin = wxSize(x, y);
182 }
183
8f4745fe 184 virtual wxDirection GetBitmapPosition() const
233f10bf 185 {
8f4745fe 186 return m_dir;
233f10bf
VZ
187 }
188
189 virtual void SetBitmapPosition(wxDirection dir)
190 {
191 m_dir = dir;
192 }
193
194private:
195 // just store the values passed to us to be able to retrieve them later
196 // from the drawing code
197 wxBitmap m_bitmaps[wxButton::State_Max];
198 wxSize m_margin;
199 wxDirection m_dir;
200
201 wxDECLARE_NO_COPY_CLASS(wxODButtonImageData);
202};
203
204#if wxUSE_UXTHEME
205
a6fd73d3
VZ
206// somehow the margin is one pixel greater than the value returned by
207// GetThemeMargins() call
208const int XP_BUTTON_EXTRA_MARGIN = 1;
209
233f10bf
VZ
210class wxXPButtonImageData : public wxButtonImageData
211{
212public:
213 // we must be constructed with the size of our images as we need to create
214 // the image list
d5b98eb9
VZ
215 wxXPButtonImageData(wxButton *btn, const wxBitmap& bitmap)
216 : m_iml(bitmap.GetWidth(), bitmap.GetHeight(), true /* use mask */,
217 wxButton::State_Max),
233f10bf
VZ
218 m_hwndBtn(GetHwndOf(btn))
219 {
d5b98eb9
VZ
220 // initialize all bitmaps to normal state
221 for ( int n = 0; n < wxButton::State_Max; n++ )
222 {
223 m_iml.Add(bitmap);
224 }
225
233f10bf
VZ
226 m_data.himl = GetHimagelistOf(&m_iml);
227
228 // use default margins
229 m_data.margin.left =
230 m_data.margin.right = btn->GetCharWidth();
231 m_data.margin.top =
232 m_data.margin.bottom = btn->GetCharHeight() / 2;
233
234 // and default alignment
235 m_data.uAlign = BUTTON_IMAGELIST_ALIGN_LEFT;
a6fd73d3
VZ
236
237 UpdateImageInfo();
233f10bf
VZ
238 }
239
240 virtual wxBitmap GetBitmap(wxButton::State which) const
241 {
242 return m_iml.GetBitmap(which);
243 }
244
245 virtual void SetBitmap(const wxBitmap& bitmap, wxButton::State which)
246 {
d5b98eb9 247 m_iml.Replace(which, bitmap);
233f10bf
VZ
248
249 UpdateImageInfo();
250 }
251
252 virtual wxSize GetBitmapMargins() const
253 {
254 return wxSize(m_data.margin.left, m_data.margin.top);
255 }
256
257 virtual void SetBitmapMargins(wxCoord x, wxCoord y)
258 {
259 RECT& margin = m_data.margin;
260 margin.left =
261 margin.right = x;
262 margin.top =
263 margin.bottom = y;
264
265 if ( !::SendMessage(m_hwndBtn, BCM_SETTEXTMARGIN, 0, (LPARAM)&margin) )
266 {
267 wxLogDebug("SendMessage(BCM_SETTEXTMARGIN) failed");
268 }
269 }
270
8f4745fe 271 virtual wxDirection GetBitmapPosition() const
233f10bf 272 {
8f4745fe
VZ
273 switch ( m_data.uAlign )
274 {
275 default:
276 wxFAIL_MSG( "invalid image alignment" );
277 // fall through
278
279 case BUTTON_IMAGELIST_ALIGN_LEFT:
280 return wxLEFT;
281
282 case BUTTON_IMAGELIST_ALIGN_RIGHT:
283 return wxRIGHT;
284
285 case BUTTON_IMAGELIST_ALIGN_TOP:
286 return wxTOP;
287
288 case BUTTON_IMAGELIST_ALIGN_BOTTOM:
289 return wxBOTTOM;
290 }
233f10bf
VZ
291 }
292
293 virtual void SetBitmapPosition(wxDirection dir)
294 {
295 UINT alignNew;
296 switch ( dir )
297 {
298 default:
299 wxFAIL_MSG( "invalid direction" );
300 // fall through
301
302 case wxLEFT:
303 alignNew = BUTTON_IMAGELIST_ALIGN_LEFT;
304 break;
305
306 case wxRIGHT:
307 alignNew = BUTTON_IMAGELIST_ALIGN_RIGHT;
308 break;
309
310 case wxTOP:
311 alignNew = BUTTON_IMAGELIST_ALIGN_TOP;
312 break;
313
314 case wxBOTTOM:
315 alignNew = BUTTON_IMAGELIST_ALIGN_BOTTOM;
316 break;
317 }
318
319 if ( alignNew != m_data.uAlign )
320 {
321 m_data.uAlign = alignNew;
322 UpdateImageInfo();
323 }
324 }
325
326private:
327 void UpdateImageInfo()
328 {
329 if ( !::SendMessage(m_hwndBtn, BCM_SETIMAGELIST, 0, (LPARAM)&m_data) )
330 {
331 wxLogDebug("SendMessage(BCM_SETIMAGELIST) failed");
332 }
333 }
334
335 // we store image list separately to be able to use convenient wxImageList
336 // methods instead of working with raw HIMAGELIST
337 wxImageList m_iml;
338
339 // store the rest of the data in BCM_SETIMAGELIST-friendly form
340 BUTTON_IMAGELIST m_data;
341
342 // the button we're associated with
343 const HWND m_hwndBtn;
344
345
346 wxDECLARE_NO_COPY_CLASS(wxXPButtonImageData);
347};
348
349#endif // wxUSE_UXTHEME
350
351} // anonymous namespace
352
edccf428
VZ
353// ----------------------------------------------------------------------------
354// macros
355// ----------------------------------------------------------------------------
356
edccf428
VZ
357// ============================================================================
358// implementation
359// ============================================================================
360
4af4dec6
VZ
361// ----------------------------------------------------------------------------
362// helper functions from wx/msw/private/button.h
363// ----------------------------------------------------------------------------
364
365void wxMSWButton::UpdateMultilineStyle(HWND hwnd, const wxString& label)
366{
367 // update BS_MULTILINE style depending on the new label (resetting it
368 // doesn't seem to do anything very useful but it shouldn't hurt and we do
369 // have to set it whenever the label becomes multi line as otherwise it
370 // wouldn't be shown correctly as we don't use BS_MULTILINE when creating
371 // the control unless it already has new lines in its label)
372 long styleOld = ::GetWindowLong(hwnd, GWL_STYLE),
373 styleNew;
9a83f860 374 if ( label.find(wxT('\n')) != wxString::npos )
4af4dec6
VZ
375 styleNew = styleOld | BS_MULTILINE;
376 else
377 styleNew = styleOld & ~BS_MULTILINE;
378
379 if ( styleNew != styleOld )
380 ::SetWindowLong(hwnd, GWL_STYLE, styleNew);
381}
382
f2d7fdf7
VZ
383wxSize wxMSWButton::GetFittingSize(wxWindow *win,
384 const wxSize& sizeLabel,
385 int flags)
433aca2d
VZ
386{
387 // FIXME: this is pure guesswork, need to retrieve the real button margins
388 wxSize sizeBtn = sizeLabel;
389
390 sizeBtn.x += 3*win->GetCharWidth();
391 sizeBtn.y = 11*EDIT_HEIGHT_FROM_CHAR_HEIGHT(sizeLabel.y)/10;
392
f2d7fdf7
VZ
393 // account for the shield UAC icon if we have it
394 if ( flags & Size_AuthNeeded )
395 sizeBtn.x += wxSystemSettings::GetMetric(wxSYS_SMALLICON_X);
396
433aca2d
VZ
397 return sizeBtn;
398}
399
f2d7fdf7 400wxSize wxMSWButton::ComputeBestSize(wxControl *btn, int flags)
4af4dec6
VZ
401{
402 wxClientDC dc(btn);
403
433aca2d
VZ
404 wxSize sizeBtn;
405 dc.GetMultiLineTextExtent(btn->GetLabelText(), &sizeBtn.x, &sizeBtn.y);
4af4dec6 406
f2d7fdf7 407 sizeBtn = GetFittingSize(btn, sizeBtn, flags);
4af4dec6
VZ
408
409 // all buttons have at least the standard size unless the user explicitly
410 // wants them to be of smaller size and used wxBU_EXACTFIT style when
411 // creating the button
412 if ( !btn->HasFlag(wxBU_EXACTFIT) )
413 {
8cb03510
VS
414 // The size of a standard button in the dialog units is 50x14, use it.
415 // Note that we intentionally don't use GetDefaultSize() here, because
416 // it's inexact -- dialog units depend on this dialog's font.
417 wxSize sizeDef = btn->ConvertDialogToPixels(wxSize(50, 14));
433aca2d
VZ
418 if ( sizeBtn.x < sizeDef.x )
419 sizeBtn.x = sizeDef.x;
420 if ( sizeBtn.y < sizeDef.y )
421 sizeBtn.y = sizeDef.y;
4af4dec6
VZ
422 }
423
433aca2d
VZ
424 btn->CacheBestSize(sizeBtn);
425
426 return sizeBtn;
4af4dec6
VZ
427}
428
edccf428
VZ
429// ----------------------------------------------------------------------------
430// creation/destruction
431// ----------------------------------------------------------------------------
432
433bool wxButton::Create(wxWindow *parent,
434 wxWindowID id,
5f7bcb48 435 const wxString& lbl,
edccf428
VZ
436 const wxPoint& pos,
437 const wxSize& size,
438 long style,
439 const wxValidator& validator,
440 const wxString& name)
2bda0e17 441{
f2d7fdf7
VZ
442 m_authNeeded = false;
443
5f7bcb48
VS
444 wxString label(lbl);
445 if (label.empty() && wxIsStockID(id))
c87fc285 446 {
c4e1d0fc
VZ
447 // On Windows, some buttons aren't supposed to have mnemonics
448 label = wxGetStockLabel
449 (
450 id,
451 id == wxID_OK || id == wxID_CANCEL || id == wxID_CLOSE
452 ? wxSTOCK_NOFLAGS
453 : wxSTOCK_WITH_MNEMONIC
454 );
f1e01716
WS
455 }
456
5b2f31eb 457 if ( !CreateControl(parent, id, pos, size, style, validator, name) )
fcf90ee1 458 return false;
edccf428 459
8292017c
VZ
460 WXDWORD exstyle;
461 WXDWORD msStyle = MSWGetStyle(style, &exstyle);
462
8292017c
VZ
463 // if the label contains several lines we must explicitly tell the button
464 // about it or it wouldn't draw it correctly ("\n"s would just appear as
465 // black boxes)
466 //
467 // NB: we do it here and not in MSWGetStyle() because we need the label
d94de683 468 // value and the label is not set yet when MSWGetStyle() is called
533171c2 469 msStyle |= wxMSWButton::GetMultilineStyle(label);
8292017c 470
9a83f860 471 return MSWCreateControl(wxT("BUTTON"), msStyle, pos, size, label, exstyle);
5b2f31eb
VZ
472}
473
474wxButton::~wxButton()
475{
6c20e8f8
VZ
476 wxTopLevelWindow *tlw = wxDynamicCast(wxGetTopLevelParent(this), wxTopLevelWindow);
477 if ( tlw && tlw->GetTmpDefaultItem() == this )
789367e1
VZ
478 {
479 UnsetTmpDefault();
480 }
233f10bf
VZ
481
482 delete m_imageData;
5b2f31eb 483}
edccf428 484
5b2f31eb
VZ
485// ----------------------------------------------------------------------------
486// flags
487// ----------------------------------------------------------------------------
cd0b1709 488
5b2f31eb
VZ
489WXDWORD wxButton::MSWGetStyle(long style, WXDWORD *exstyle) const
490{
491 // buttons never have an external border, they draw their own one
492 WXDWORD msStyle = wxControl::MSWGetStyle
493 (
494 (style & ~wxBORDER_MASK) | wxBORDER_NONE, exstyle
495 );
f6bcfd97 496
5b2f31eb
VZ
497 // we must use WS_CLIPSIBLINGS with the buttons or they would draw over
498 // each other in any resizeable dialog which has more than one button in
499 // the bottom
500 msStyle |= WS_CLIPSIBLINGS;
b0766406 501
5b2f31eb
VZ
502 // don't use "else if" here: weird as it is, but you may combine wxBU_LEFT
503 // and wxBU_RIGHT to get BS_CENTER!
504 if ( style & wxBU_LEFT )
f6bcfd97 505 msStyle |= BS_LEFT;
5b2f31eb 506 if ( style & wxBU_RIGHT )
f6bcfd97 507 msStyle |= BS_RIGHT;
5b2f31eb 508 if ( style & wxBU_TOP )
f6bcfd97 509 msStyle |= BS_TOP;
5b2f31eb 510 if ( style & wxBU_BOTTOM )
f6bcfd97 511 msStyle |= BS_BOTTOM;
8cc4850c 512#ifndef __WXWINCE__
8a094d7b
JS
513 // flat 2d buttons
514 if ( style & wxNO_BORDER )
515 msStyle |= BS_FLAT;
8cc4850c 516#endif // __WXWINCE__
edccf428 517
5b2f31eb 518 return msStyle;
2bda0e17
KB
519}
520
d94de683
VZ
521void wxButton::SetLabel(const wxString& label)
522{
533171c2 523 wxMSWButton::UpdateMultilineStyle(GetHwnd(), label);
d94de683
VZ
524
525 wxButtonBase::SetLabel(label);
526}
527
edccf428
VZ
528// ----------------------------------------------------------------------------
529// size management including autosizing
530// ----------------------------------------------------------------------------
531
3571e1ad
VZ
532void wxButton::AdjustForBitmapSize(wxSize &size) const
533{
534 if ( !m_imageData )
535 return;
536
537 // account for the bitmap size
538 const wxSize sizeBmp = m_imageData->GetBitmap(State_Normal).GetSize();
539 const wxDirection dirBmp = m_imageData->GetBitmapPosition();
540 if ( dirBmp == wxLEFT || dirBmp == wxRIGHT )
541 {
542 size.x += sizeBmp.x;
543 if ( sizeBmp.y > size.y )
544 size.y = sizeBmp.y;
545 }
546 else // bitmap on top/below the text
547 {
548 size.y += sizeBmp.y;
549 if ( sizeBmp.x > size.x )
550 size.x = sizeBmp.x;
551 }
552
553 // account for the user-specified margins
554 size += 2*m_imageData->GetBitmapMargins();
555
556 // and also for the margins we always add internally (unless we have no
557 // border at all in which case the button has exactly the same size as
558 // bitmap and so no margins should be used)
559 if ( !HasFlag(wxBORDER_NONE) )
560 {
561 int marginH = 0,
562 marginV = 0;
563#if wxUSE_UXTHEME
564 if ( wxUxThemeEngine::GetIfActive() )
565 {
566 wxUxThemeHandle theme(const_cast<wxButton *>(this), L"BUTTON");
567
568 MARGINS margins;
569 wxUxThemeEngine::Get()->GetThemeMargins(theme, NULL,
570 BP_PUSHBUTTON,
571 PBS_NORMAL,
572 TMT_CONTENTMARGINS,
573 NULL,
574 &margins);
575
576 // XP doesn't draw themed buttons correctly when the client
577 // area is smaller than 8x8 - enforce this minimum size for
578 // small bitmaps
579 size.IncTo(wxSize(8, 8));
580
581 marginH = margins.cxLeftWidth + margins.cxRightWidth
582 + 2*XP_BUTTON_EXTRA_MARGIN;
583 marginV = margins.cyTopHeight + margins.cyBottomHeight
584 + 2*XP_BUTTON_EXTRA_MARGIN;
585 }
586 else
587#endif // wxUSE_UXTHEME
588 {
589 marginH =
590 marginV = OD_BUTTON_MARGIN;
591 }
592
593 size.IncBy(marginH, marginV);
594 }
595}
596
f68586e5 597wxSize wxButton::DoGetBestSize() const
2bda0e17 598{
a6fd73d3
VZ
599 wxSize size;
600
4b601a59
VZ
601 // account for the text part if we have it or if we don't have any image at
602 // all (buttons initially created with empty label should still have a non
603 // zero size)
604 if ( ShowsLabel() || !m_imageData )
a6fd73d3 605 {
f2d7fdf7
VZ
606 int flags = 0;
607 if ( GetAuthNeeded() )
608 flags |= wxMSWButton::Size_AuthNeeded;
609
610 size = wxMSWButton::ComputeBestSize(const_cast<wxButton *>(this), flags);
a6fd73d3
VZ
611 }
612
233f10bf
VZ
613 if ( m_imageData )
614 {
3571e1ad 615 AdjustForBitmapSize(size);
a6fd73d3 616
233f10bf
VZ
617 CacheBestSize(size);
618 }
619
620 return size;
2bda0e17
KB
621}
622
e1f36ff8 623/* static */
1e6feb95 624wxSize wxButtonBase::GetDefaultSize()
e1f36ff8 625{
8c3c31d4 626 static wxSize s_sizeBtn;
e1f36ff8 627
8c3c31d4
VZ
628 if ( s_sizeBtn.x == 0 )
629 {
630 wxScreenDC dc;
a756f210 631 dc.SetFont(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT));
8c3c31d4 632
5c3c1372
VS
633 // The size of a standard button in the dialog units is 50x14,
634 // translate this to pixels.
635 //
636 // Windows' computes dialog units using average character width over
637 // upper- and lower-case ASCII alphabet and not using the average
638 // character width metadata stored in the font; see
639 // http://support.microsoft.com/default.aspx/kb/145994 for detailed
640 // discussion.
641 //
642 // NB: wxMulDivInt32() is used, because it correctly rounds the result
643
644 const wxSize base = wxPrivate::GetAverageASCIILetterSize(dc);
645 s_sizeBtn.x = wxMulDivInt32(50, base.x, 4);
646 s_sizeBtn.y = wxMulDivInt32(14, base.y, 8);
8c3c31d4 647 }
e1f36ff8 648
8c3c31d4 649 return s_sizeBtn;
e1f36ff8
VZ
650}
651
4438caf4 652// ----------------------------------------------------------------------------
036da5e3 653// default button handling
4438caf4
VZ
654// ----------------------------------------------------------------------------
655
d78f09e2 656/*
eb74a51f
VZ
657 The comment below and all this code is probably due to not using WM_NEXTDLGCTL
658 message when changing focus (but just SetFocus() which is not enough), see
659 http://blogs.msdn.com/oldnewthing/archive/2004/08/02/205624.aspx for the
660 full explanation.
661
662 TODO: Do use WM_NEXTDLGCTL and get rid of all this code.
663
664
d78f09e2
VZ
665 "Everything you ever wanted to know about the default buttons" or "Why do we
666 have to do all this?"
667
668 In MSW the default button should be activated when the user presses Enter
669 and the current control doesn't process Enter itself somehow. This is
670 handled by ::DefWindowProc() (or maybe ::DefDialogProc()) using DM_SETDEFID
671 Another aspect of "defaultness" is that the default button has different
672 appearance: this is due to BS_DEFPUSHBUTTON style which is completely
7fb1b2b4
VZ
673 separate from DM_SETDEFID stuff (!). Also note that BS_DEFPUSHBUTTON should
674 be unset if our parent window is not active so it should be unset whenever
675 we lose activation and set back when we regain it.
d78f09e2
VZ
676
677 Final complication is that when a button is active, it should be the default
678 one, i.e. pressing Enter on a button always activates it and not another
679 one.
680
681 We handle this by maintaining a permanent and a temporary default items in
682 wxControlContainer (both may be NULL). When a button becomes the current
683 control (i.e. gets focus) it sets itself as the temporary default which
684 ensures that it has the right appearance and that Enter will be redirected
685 to it. When the button loses focus, it unsets the temporary default and so
686 the default item will be the permanent default -- that is the default button
687 if any had been set or none otherwise, which is just what we want.
688
7fb1b2b4
VZ
689 NB: all this is quite complicated by now and the worst is that normally
690 it shouldn't be necessary at all as for the normal Windows programs
691 DefWindowProc() and IsDialogMessage() take care of all this
77ffb593 692 automatically -- however in wxWidgets programs this doesn't work for
7fb1b2b4
VZ
693 nested hierarchies (i.e. a notebook inside a notebook) for unknown
694 reason and so we have to reproduce all this code ourselves. It would be
695 very nice if we could avoid doing it.
d78f09e2
VZ
696 */
697
036da5e3 698// set this button as the (permanently) default one in its panel
94aff5ff 699wxWindow *wxButton::SetDefault()
2bda0e17 700{
77ffb593 701 // set this one as the default button both for wxWidgets ...
94aff5ff 702 wxWindow *winOldDefault = wxButtonBase::SetDefault();
036da5e3 703
7fb1b2b4 704 // ... and Windows
fcf90ee1
WS
705 SetDefaultStyle(wxDynamicCast(winOldDefault, wxButton), false);
706 SetDefaultStyle(this, true);
ebc0b155
VZ
707
708 return winOldDefault;
036da5e3
VZ
709}
710
627c8d89
VZ
711// return the top level parent window if it's not being deleted yet, otherwise
712// return NULL
05c5f281
VZ
713static wxTopLevelWindow *GetTLWParentIfNotBeingDeleted(wxWindow *win)
714{
627c8d89 715 for ( ;; )
05c5f281 716 {
627c8d89
VZ
717 // IsTopLevel() will return false for a wxTLW being deleted, so we also
718 // need the parent test for this case
719 wxWindow * const parent = win->GetParent();
720 if ( !parent || win->IsTopLevel() )
9617f65b
VZ
721 {
722 if ( win->IsBeingDeleted() )
723 return NULL;
627c8d89 724
05c5f281 725 break;
9617f65b 726 }
627c8d89
VZ
727
728 win = parent;
05c5f281
VZ
729 }
730
9a83f860 731 wxASSERT_MSG( win, wxT("button without top level parent?") );
05c5f281 732
627c8d89 733 wxTopLevelWindow * const tlw = wxDynamicCast(win, wxTopLevelWindow);
9a83f860 734 wxASSERT_MSG( tlw, wxT("logic error in GetTLWParentIfNotBeingDeleted()") );
627c8d89
VZ
735
736 return tlw;
05c5f281
VZ
737}
738
7fb1b2b4 739// set this button as being currently default
036da5e3
VZ
740void wxButton::SetTmpDefault()
741{
05c5f281
VZ
742 wxTopLevelWindow * const tlw = GetTLWParentIfNotBeingDeleted(GetParent());
743 if ( !tlw )
744 return;
036da5e3 745
6c20e8f8
VZ
746 wxWindow *winOldDefault = tlw->GetDefaultItem();
747 tlw->SetTmpDefaultItem(this);
7fb1b2b4 748
fcf90ee1
WS
749 SetDefaultStyle(wxDynamicCast(winOldDefault, wxButton), false);
750 SetDefaultStyle(this, true);
036da5e3
VZ
751}
752
7fb1b2b4 753// unset this button as currently default, it may still stay permanent default
036da5e3
VZ
754void wxButton::UnsetTmpDefault()
755{
05c5f281
VZ
756 wxTopLevelWindow * const tlw = GetTLWParentIfNotBeingDeleted(GetParent());
757 if ( !tlw )
758 return;
036da5e3 759
6c20e8f8 760 tlw->SetTmpDefaultItem(NULL);
036da5e3 761
6c20e8f8 762 wxWindow *winOldDefault = tlw->GetDefaultItem();
7fb1b2b4 763
fcf90ee1
WS
764 SetDefaultStyle(this, false);
765 SetDefaultStyle(wxDynamicCast(winOldDefault, wxButton), true);
036da5e3 766}
8ed57d93 767
036da5e3
VZ
768/* static */
769void
7fb1b2b4 770wxButton::SetDefaultStyle(wxButton *btn, bool on)
036da5e3 771{
7fb1b2b4
VZ
772 // we may be called with NULL pointer -- simpler to do the check here than
773 // in the caller which does wxDynamicCast()
774 if ( !btn )
775 return;
776
777 // first, let DefDlgProc() know about the new default button
778 if ( on )
5d1d2d46 779 {
7fb1b2b4
VZ
780 // we shouldn't set BS_DEFPUSHBUTTON for any button if we don't have
781 // focus at all any more
782 if ( !wxTheApp->IsActive() )
783 return;
cd0b1709 784
6c20e8f8 785 wxWindow * const tlw = wxGetTopLevelParent(btn);
9a83f860 786 wxCHECK_RET( tlw, wxT("button without top level window?") );
cef55d64 787
6c20e8f8 788 ::SendMessage(GetHwndOf(tlw), DM_SETDEFID, btn->GetId(), 0L);
cef55d64
VZ
789
790 // sending DM_SETDEFID also changes the button style to
791 // BS_DEFPUSHBUTTON so there is nothing more to do
5d1d2d46
VZ
792 }
793
7fb1b2b4
VZ
794 // then also change the style as needed
795 long style = ::GetWindowLong(GetHwndOf(btn), GWL_STYLE);
796 if ( !(style & BS_DEFPUSHBUTTON) == on )
be4017f8 797 {
7fb1b2b4
VZ
798 // don't do it with the owner drawn buttons because it will
799 // reset BS_OWNERDRAW style bit too (as BS_OWNERDRAW &
800 // BS_DEFPUSHBUTTON != 0)!
036da5e3
VZ
801 if ( (style & BS_OWNERDRAW) != BS_OWNERDRAW )
802 {
7fb1b2b4
VZ
803 ::SendMessage(GetHwndOf(btn), BM_SETSTYLE,
804 on ? style | BS_DEFPUSHBUTTON
805 : style & ~BS_DEFPUSHBUTTON,
806 1L /* redraw */);
036da5e3 807 }
7fb1b2b4 808 else // owner drawn
036da5e3 809 {
7fb1b2b4
VZ
810 // redraw the button - it will notice itself that it's
811 // [not] the default one [any longer]
812 btn->Refresh();
036da5e3 813 }
be4017f8 814 }
7fb1b2b4 815 //else: already has correct style
2bda0e17
KB
816}
817
edccf428
VZ
818// ----------------------------------------------------------------------------
819// helpers
820// ----------------------------------------------------------------------------
821
822bool wxButton::SendClickEvent()
2bda0e17 823{
edccf428
VZ
824 wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED, GetId());
825 event.SetEventObject(this);
826
827 return ProcessCommand(event);
2bda0e17
KB
828}
829
edccf428 830void wxButton::Command(wxCommandEvent & event)
2bda0e17 831{
edccf428 832 ProcessCommand(event);
2bda0e17
KB
833}
834
edccf428
VZ
835// ----------------------------------------------------------------------------
836// event/message handlers
837// ----------------------------------------------------------------------------
2bda0e17 838
33ac7e6f 839bool wxButton::MSWCommand(WXUINT param, WXWORD WXUNUSED(id))
edccf428 840{
fcf90ee1 841 bool processed = false;
57c0af52 842 switch ( param )
edccf428 843 {
5aab763c
RD
844 // NOTE: Apparently older versions (NT 4?) of the common controls send
845 // BN_DOUBLECLICKED but not a second BN_CLICKED for owner-drawn
87b6002d 846 // buttons, so in order to send two EVT_BUTTON events we should
5aab763c
RD
847 // catch both types. Currently (Feb 2003) up-to-date versions of
848 // win98, win2k and winXP all send two BN_CLICKED messages for
849 // all button types, so we don't catch BN_DOUBLECLICKED anymore
850 // in order to not get 3 EVT_BUTTON events. If this is a problem
851 // then we need to figure out which version of the comctl32 changed
852 // this behaviour and test for it.
853
a95e38c0
VZ
854 case 1: // message came from an accelerator
855 case BN_CLICKED: // normal buttons send this
57c0af52
VZ
856 processed = SendClickEvent();
857 break;
edccf428 858 }
2bda0e17 859
edccf428 860 return processed;
2bda0e17
KB
861}
862
c140b7e7 863WXLRESULT wxButton::MSWWindowProc(WXUINT nMsg, WXWPARAM wParam, WXLPARAM lParam)
678cd6de 864{
87b6002d 865 // when we receive focus, we want to temporarily become the default button in
036da5e3
VZ
866 // our parent panel so that pressing "Enter" would activate us -- and when
867 // losing it we should restore the previous default button as well
be4017f8 868 if ( nMsg == WM_SETFOCUS )
678cd6de 869 {
036da5e3 870 SetTmpDefault();
be4017f8 871
036da5e3
VZ
872 // let the default processing take place too
873 }
874 else if ( nMsg == WM_KILLFOCUS )
875 {
876 UnsetTmpDefault();
678cd6de 877 }
a95e38c0
VZ
878 else if ( nMsg == WM_LBUTTONDBLCLK )
879 {
f6bcfd97
BP
880 // emulate a click event to force an owner-drawn button to change its
881 // appearance - without this, it won't do it
882 (void)wxControl::MSWWindowProc(WM_LBUTTONDOWN, wParam, lParam);
883
036da5e3 884 // and continue with processing the message normally as well
a95e38c0 885 }
4e9da8b7
RD
886#if wxUSE_UXTHEME
887 else if ( nMsg == WM_THEMECHANGED )
888 {
889 // need to recalculate the best size here
890 // as the theme size might have changed
891 InvalidateBestSize();
892 }
d5b98eb9
VZ
893#endif // wxUSE_UXTHEME
894 // must use m_mouseInWindow here instead of IsMouseInWindow()
895 // since we need to know the first time the mouse enters the window
896 // and IsMouseInWindow() would return true in this case
897 else if ( (nMsg == WM_MOUSEMOVE && !m_mouseInWindow) ||
898 nMsg == WM_MOUSELEAVE )
4e9da8b7 899 {
d5b98eb9 900 if (
a6fd73d3
VZ
901 IsEnabled() &&
902 (
d5b98eb9
VZ
903#if wxUSE_UXTHEME
904 wxUxThemeEngine::GetIfActive() ||
905#endif // wxUSE_UXTHEME
6d994163 906 (m_imageData && m_imageData->GetBitmap(State_Current).IsOk())
a6fd73d3 907 )
d5b98eb9 908 )
4e9da8b7
RD
909 {
910 Refresh();
911 }
912 }
678cd6de
VZ
913
914 // let the base class do all real processing
915 return wxControl::MSWWindowProc(nMsg, wParam, lParam);
916}
cd0b1709 917
f2d7fdf7
VZ
918// ----------------------------------------------------------------------------
919// authentication needed handling
920// ----------------------------------------------------------------------------
921
922bool wxButton::DoGetAuthNeeded() const
923{
924 return m_authNeeded;
925}
926
927void wxButton::DoSetAuthNeeded(bool show)
928{
929 // show/hide UAC symbol on Windows Vista and later
930 if ( wxGetWinVersion() >= wxWinVersion_6 )
931 {
932 m_authNeeded = show;
933 ::SendMessage(GetHwnd(), BCM_SETSHIELD, 0, show);
934 InvalidateBestSize();
935 }
936}
937
233f10bf
VZ
938// ----------------------------------------------------------------------------
939// button images
940// ----------------------------------------------------------------------------
941
942wxBitmap wxButton::DoGetBitmap(State which) const
943{
944 return m_imageData ? m_imageData->GetBitmap(which) : wxBitmap();
945}
946
947void wxButton::DoSetBitmap(const wxBitmap& bitmap, State which)
948{
949 // allocate the image data when the first bitmap is set
950 if ( !m_imageData )
951 {
952#if wxUSE_UXTHEME
a6fd73d3
VZ
953 // using image list doesn't work correctly if we don't have any label
954 // (even if we use BUTTON_IMAGELIST_ALIGN_CENTER alignment and
955 // BS_BITMAP style), at least under Windows 2003 so use owner drawn
956 // strategy for bitmap-only buttons
a2117591 957 if ( ShowsLabel() && wxUxThemeEngine::GetIfActive() )
8f4745fe 958 {
d5b98eb9 959 m_imageData = new wxXPButtonImageData(this, bitmap);
8f4745fe 960 }
233f10bf
VZ
961 else
962#endif // wxUSE_UXTHEME
8f4745fe 963 {
d5b98eb9 964 m_imageData = new wxODButtonImageData(this, bitmap);
8f4745fe
VZ
965 MakeOwnerDrawn();
966 }
233f10bf 967 }
d5b98eb9
VZ
968 else
969 {
970 m_imageData->SetBitmap(bitmap, which);
971 }
a6fd73d3 972
e431c7df
VZ
973 // it should be enough to only invalidate the best size when the normal
974 // bitmap changes as all bitmaps assigned to the button should be of the
975 // same size anyhow
976 if ( which == State_Normal )
977 InvalidateBestSize();
978
a6fd73d3
VZ
979 Refresh();
980}
981
982wxSize wxButton::DoGetBitmapMargins() const
983{
984 return m_imageData ? m_imageData->GetBitmapMargins() : wxSize(0, 0);
233f10bf
VZ
985}
986
987void wxButton::DoSetBitmapMargins(wxCoord x, wxCoord y)
988{
989 wxCHECK_RET( m_imageData, "SetBitmap() must be called first" );
990
991 m_imageData->SetBitmapMargins(x, y);
06bb8d92 992 InvalidateBestSize();
233f10bf
VZ
993}
994
995void wxButton::DoSetBitmapPosition(wxDirection dir)
996{
997 wxCHECK_RET( m_imageData, "SetBitmap() must be called first" );
998
999 m_imageData->SetBitmapPosition(dir);
06bb8d92 1000 InvalidateBestSize();
233f10bf
VZ
1001}
1002
cd0b1709
VZ
1003// ----------------------------------------------------------------------------
1004// owner-drawn buttons support
1005// ----------------------------------------------------------------------------
1006
cd0b1709 1007// drawing helpers
d8c89c48
VZ
1008namespace
1009{
cd0b1709 1010
d5b98eb9
VZ
1011// return the button state using both the ODS_XXX flags specified in state
1012// parameter and the current button state
1013wxButton::State GetButtonState(wxButton *btn, UINT state)
1014{
1015 if ( state & ODS_DISABLED )
1016 return wxButton::State_Disabled;
1017
1018 if ( state & ODS_SELECTED )
1019 return wxButton::State_Pressed;
1020
1021 if ( btn->HasCapture() || btn->IsMouseInWindow() )
1022 return wxButton::State_Current;
1023
1024 if ( state & ODS_FOCUS )
1025 return wxButton::State_Focused;
1026
1027 return wxButton::State_Normal;
1028}
1029
d8c89c48
VZ
1030void DrawButtonText(HDC hdc,
1031 RECT *pRect,
1032 const wxString& text,
1033 COLORREF col,
1034 int flags)
cd0b1709 1035{
d8c89c48
VZ
1036 wxTextColoursChanger changeFg(hdc, col, CLR_INVALID);
1037 wxBkModeChanger changeBkMode(hdc, wxBRUSHSTYLE_TRANSPARENT);
cd0b1709 1038
f915b92e
VZ
1039 // center text horizontally in any case
1040 flags |= DT_CENTER;
1041
9a83f860 1042 if ( text.find(wxT('\n')) != wxString::npos )
cbbb6724
VZ
1043 {
1044 // draw multiline label
1045
1046 // first we need to compute its bounding rect
1047 RECT rc;
1048 ::CopyRect(&rc, pRect);
e0a050e3
VS
1049 ::DrawText(hdc, text.wx_str(), text.length(), &rc,
1050 DT_CENTER | DT_CALCRECT);
cbbb6724
VZ
1051
1052 // now center this rect inside the entire button area
1053 const LONG w = rc.right - rc.left;
1054 const LONG h = rc.bottom - rc.top;
1055 rc.left = (pRect->right - pRect->left)/2 - w/2;
1056 rc.right = rc.left+w;
1057 rc.top = (pRect->bottom - pRect->top)/2 - h/2;
1058 rc.bottom = rc.top+h;
1059
f915b92e 1060 ::DrawText(hdc, text.wx_str(), text.length(), &rc, flags);
cbbb6724
VZ
1061 }
1062 else // single line label
1063 {
f915b92e
VZ
1064 // centre text vertically too (notice that we must have DT_SINGLELINE
1065 // for DT_VCENTER to work)
e0a050e3 1066 ::DrawText(hdc, text.wx_str(), text.length(), pRect,
f915b92e 1067 flags | DT_SINGLELINE | DT_VCENTER);
cbbb6724 1068 }
cd0b1709
VZ
1069}
1070
d8c89c48 1071void DrawRect(HDC hdc, const RECT& r)
cd0b1709 1072{
4676948b
JS
1073 wxDrawLine(hdc, r.left, r.top, r.right, r.top);
1074 wxDrawLine(hdc, r.right, r.top, r.right, r.bottom);
1075 wxDrawLine(hdc, r.right, r.bottom, r.left, r.bottom);
1076 wxDrawLine(hdc, r.left, r.bottom, r.left, r.top);
cd0b1709
VZ
1077}
1078
cd0b1709
VZ
1079/*
1080 The button frame looks like this normally:
1081
1082 WWWWWWWWWWWWWWWWWWB
16162a64
GRG
1083 WHHHHHHHHHHHHHHHHGB W = white (HILIGHT)
1084 WH GB H = light grey (LIGHT)
1085 WH GB G = dark grey (SHADOW)
1086 WH GB B = black (DKSHADOW)
1087 WH GB
cd0b1709
VZ
1088 WGGGGGGGGGGGGGGGGGB
1089 BBBBBBBBBBBBBBBBBBB
1090
1091 When the button is selected, the button becomes like this (the total button
1092 size doesn't change):
1093
1094 BBBBBBBBBBBBBBBBBBB
1095 BWWWWWWWWWWWWWWWWBB
16162a64
GRG
1096 BWHHHHHHHHHHHHHHGBB
1097 BWH GBB
1098 BWH GBB
cd0b1709
VZ
1099 BWGGGGGGGGGGGGGGGBB
1100 BBBBBBBBBBBBBBBBBBB
1101 BBBBBBBBBBBBBBBBBBB
1102
1103 When the button is pushed (while selected) it is like:
1104
1105 BBBBBBBBBBBBBBBBBBB
1106 BGGGGGGGGGGGGGGGGGB
1107 BG GB
1108 BG GB
1109 BG GB
16162a64 1110 BG GB
cd0b1709
VZ
1111 BGGGGGGGGGGGGGGGGGB
1112 BBBBBBBBBBBBBBBBBBB
1113*/
8f4745fe 1114void DrawButtonFrame(HDC hdc, RECT& rectBtn,
d8c89c48 1115 bool selected, bool pushed)
cd0b1709
VZ
1116{
1117 RECT r;
1118 CopyRect(&r, &rectBtn);
1119
9c956c0a
VZ
1120 AutoHPEN hpenBlack(GetSysColor(COLOR_3DDKSHADOW)),
1121 hpenGrey(GetSysColor(COLOR_3DSHADOW)),
1122 hpenLightGr(GetSysColor(COLOR_3DLIGHT)),
1123 hpenWhite(GetSysColor(COLOR_3DHILIGHT));
cd0b1709 1124
9c956c0a 1125 SelectInHDC selectPen(hdc, hpenBlack);
cd0b1709
VZ
1126
1127 r.right--;
1128 r.bottom--;
1129
1130 if ( pushed )
1131 {
1132 DrawRect(hdc, r);
1133
1134 (void)SelectObject(hdc, hpenGrey);
fcf90ee1 1135 ::InflateRect(&r, -1, -1);
cd0b1709
VZ
1136
1137 DrawRect(hdc, r);
1138 }
1139 else // !pushed
1140 {
1141 if ( selected )
1142 {
1143 DrawRect(hdc, r);
1144
fcf90ee1 1145 ::InflateRect(&r, -1, -1);
cd0b1709
VZ
1146 }
1147
4676948b
JS
1148 wxDrawLine(hdc, r.left, r.bottom, r.right, r.bottom);
1149 wxDrawLine(hdc, r.right, r.bottom, r.right, r.top - 1);
cd0b1709
VZ
1150
1151 (void)SelectObject(hdc, hpenWhite);
4676948b
JS
1152 wxDrawLine(hdc, r.left, r.bottom - 1, r.left, r.top);
1153 wxDrawLine(hdc, r.left, r.top, r.right, r.top);
cd0b1709 1154
16162a64 1155 (void)SelectObject(hdc, hpenLightGr);
4676948b
JS
1156 wxDrawLine(hdc, r.left + 1, r.bottom - 2, r.left + 1, r.top + 1);
1157 wxDrawLine(hdc, r.left + 1, r.top + 1, r.right - 1, r.top + 1);
16162a64 1158
cd0b1709 1159 (void)SelectObject(hdc, hpenGrey);
4676948b
JS
1160 wxDrawLine(hdc, r.left + 1, r.bottom - 1, r.right - 1, r.bottom - 1);
1161 wxDrawLine(hdc, r.right - 1, r.bottom - 1, r.right - 1, r.top);
cd0b1709 1162 }
8f4745fe
VZ
1163
1164 InflateRect(&rectBtn, -OD_BUTTON_MARGIN, -OD_BUTTON_MARGIN);
cd0b1709
VZ
1165}
1166
4e9da8b7 1167#if wxUSE_UXTHEME
d5b98eb9 1168void DrawXPBackground(wxButton *button, HDC hdc, RECT& rectBtn, UINT state)
cd0b1709 1169{
4e9da8b7 1170 wxUxThemeHandle theme(button, L"BUTTON");
4e9da8b7 1171
d5b98eb9
VZ
1172 // this array is indexed by wxButton::State values and so must be kept in
1173 // sync with it
958e4b4b 1174 static const int uxStates[] =
4e9da8b7 1175 {
d5b98eb9
VZ
1176 PBS_NORMAL, PBS_HOT, PBS_PRESSED, PBS_DISABLED, PBS_DEFAULTED
1177 };
1178
1179 int iState = uxStates[GetButtonState(button, state)];
1180
1181 wxUxThemeEngine * const engine = wxUxThemeEngine::Get();
cd0b1709 1182
4e9da8b7 1183 // draw parent background if needed
d5b98eb9
VZ
1184 if ( engine->IsThemeBackgroundPartiallyTransparent
1185 (
1186 theme,
1187 BP_PUSHBUTTON,
1188 iState
1189 ) )
4e9da8b7 1190 {
d5b98eb9 1191 engine->DrawThemeParentBackground(GetHwndOf(button), hdc, &rectBtn);
4e9da8b7
RD
1192 }
1193
1194 // draw background
d5b98eb9
VZ
1195 engine->DrawThemeBackground(theme, hdc, BP_PUSHBUTTON, iState,
1196 &rectBtn, NULL);
4e9da8b7
RD
1197
1198 // calculate content area margins
1199 MARGINS margins;
d5b98eb9
VZ
1200 engine->GetThemeMargins(theme, hdc, BP_PUSHBUTTON, iState,
1201 TMT_CONTENTMARGINS, &rectBtn, &margins);
8f4745fe 1202 ::InflateRect(&rectBtn, -margins.cxLeftWidth, -margins.cyTopHeight);
a6fd73d3 1203 ::InflateRect(&rectBtn, -XP_BUTTON_EXTRA_MARGIN, -XP_BUTTON_EXTRA_MARGIN);
4e9da8b7
RD
1204
1205 if ( button->UseBgCol() )
1206 {
1207 COLORREF colBg = wxColourToRGB(button->GetBackgroundColour());
9c956c0a 1208 AutoHBRUSH hbrushBackground(colBg);
4e9da8b7
RD
1209
1210 // don't overwrite the focus rect
8f4745fe
VZ
1211 RECT rectClient;
1212 ::CopyRect(&rectClient, &rectBtn);
4e9da8b7
RD
1213 ::InflateRect(&rectClient, -1, -1);
1214 FillRect(hdc, &rectClient, hbrushBackground);
4e9da8b7
RD
1215 }
1216}
1217#endif // wxUSE_UXTHEME
1218
d8c89c48
VZ
1219} // anonymous namespace
1220
1221// ----------------------------------------------------------------------------
1222// owner drawn buttons support
1223// ----------------------------------------------------------------------------
1224
1225void wxButton::MakeOwnerDrawn()
1226{
1227 long style = GetWindowLong(GetHwnd(), GWL_STYLE);
1228 if ( (style & BS_OWNERDRAW) != BS_OWNERDRAW )
1229 {
1230 // make it so
1231 style |= BS_OWNERDRAW;
1232 SetWindowLong(GetHwnd(), GWL_STYLE, style);
1233 }
1234}
1235
1236bool wxButton::SetBackgroundColour(const wxColour &colour)
1237{
1238 if ( !wxControl::SetBackgroundColour(colour) )
1239 {
1240 // nothing to do
1241 return false;
1242 }
1243
1244 MakeOwnerDrawn();
1245
1246 Refresh();
1247
1248 return true;
1249}
1250
1251bool wxButton::SetForegroundColour(const wxColour &colour)
1252{
1253 if ( !wxControl::SetForegroundColour(colour) )
1254 {
1255 // nothing to do
1256 return false;
1257 }
1258
1259 MakeOwnerDrawn();
1260
1261 Refresh();
1262
1263 return true;
1264}
1265
4e9da8b7
RD
1266bool wxButton::MSWOnDraw(WXDRAWITEMSTRUCT *wxdis)
1267{
1268 LPDRAWITEMSTRUCT lpDIS = (LPDRAWITEMSTRUCT)wxdis;
cd0b1709 1269 HDC hdc = lpDIS->hDC;
8f4745fe 1270
cd0b1709 1271 UINT state = lpDIS->itemState;
8f4745fe
VZ
1272 bool pushed = (SendMessage(GetHwnd(), BM_GETSTATE, 0, 0) & BST_PUSHED) != 0;
1273
4e9da8b7
RD
1274 RECT rectBtn;
1275 CopyRect(&rectBtn, &lpDIS->rcItem);
cd0b1709 1276
8f4745fe 1277 // draw the button background
86ac84b8 1278 if ( !HasFlag(wxBORDER_NONE) )
4e9da8b7 1279 {
86ac84b8
VZ
1280#if wxUSE_UXTHEME
1281 if ( wxUxThemeEngine::GetIfActive() )
1282 {
1283 DrawXPBackground(this, hdc, rectBtn, state);
1284 }
1285 else
4e9da8b7 1286#endif // wxUSE_UXTHEME
86ac84b8
VZ
1287 {
1288 COLORREF colBg = wxColourToRGB(GetBackgroundColour());
cd0b1709 1289
86ac84b8
VZ
1290 // first, draw the background
1291 AutoHBRUSH hbrushBackground(colBg);
1292 FillRect(hdc, &rectBtn, hbrushBackground);
cd0b1709 1293
86ac84b8
VZ
1294 // draw the border for the current state
1295 bool selected = (state & ODS_SELECTED) != 0;
1296 if ( !selected )
4e9da8b7 1297 {
86ac84b8
VZ
1298 wxTopLevelWindow *
1299 tlw = wxDynamicCast(wxGetTopLevelParent(this), wxTopLevelWindow);
1300 if ( tlw )
1301 {
1302 selected = tlw->GetDefaultItem() == this;
1303 }
4e9da8b7 1304 }
cd0b1709 1305
86ac84b8
VZ
1306 DrawButtonFrame(hdc, rectBtn, selected, pushed);
1307 }
cd0b1709 1308
86ac84b8
VZ
1309 // draw the focus rectangle if we need it
1310 if ( (state & ODS_FOCUS) && !(state & ODS_NOFOCUSRECT) )
1311 {
1312 DrawFocusRect(hdc, &rectBtn);
cd0b1709 1313
8f4745fe 1314#if wxUSE_UXTHEME
86ac84b8 1315 if ( !wxUxThemeEngine::GetIfActive() )
8f4745fe 1316#endif // wxUSE_UXTHEME
8f4745fe 1317 {
86ac84b8
VZ
1318 if ( pushed )
1319 {
1320 // the label is shifted by 1 pixel to create "pushed" effect
1321 OffsetRect(&rectBtn, 1, 1);
1322 }
8f4745fe 1323 }
4e9da8b7 1324 }
8f4745fe
VZ
1325 }
1326
cd0b1709 1327
8f4745fe
VZ
1328 // draw the image, if any
1329 if ( m_imageData )
1330 {
d5b98eb9
VZ
1331 wxBitmap bmp = m_imageData->GetBitmap(GetButtonState(this, state));
1332 if ( !bmp.IsOk() )
1333 bmp = m_imageData->GetBitmap(State_Normal);
1334
8f4745fe
VZ
1335 const wxSize sizeBmp = bmp.GetSize();
1336 const wxSize margin = m_imageData->GetBitmapMargins();
1337 const wxSize sizeBmpWithMargins(sizeBmp + 2*margin);
1338 wxRect rectButton(wxRectFromRECT(rectBtn));
1339
1340 // for simplicity, we start with centred rectangle and then move it to
1341 // the appropriate edge
1342 wxRect rectBitmap = wxRect(sizeBmp).CentreIn(rectButton);
8f4745fe 1343
1cd44b9c
VZ
1344 // move bitmap only if we have a label, otherwise keep it centered
1345 if ( ShowsLabel() )
1346 {
1347 switch ( m_imageData->GetBitmapPosition() )
1348 {
1349 default:
1350 wxFAIL_MSG( "invalid direction" );
1351 // fall through
1352
1353 case wxLEFT:
1354 rectBitmap.x = rectButton.x + margin.x;
1355 rectButton.x += sizeBmpWithMargins.x;
1356 rectButton.width -= sizeBmpWithMargins.x;
1357 break;
1358
1359 case wxRIGHT:
1360 rectBitmap.x = rectButton.GetRight() - sizeBmp.x - margin.x;
1361 rectButton.width -= sizeBmpWithMargins.x;
1362 break;
1363
1364 case wxTOP:
1365 rectBitmap.y = rectButton.y + margin.y;
1366 rectButton.y += sizeBmpWithMargins.y;
1367 rectButton.height -= sizeBmpWithMargins.y;
1368 break;
1369
1370 case wxBOTTOM:
1371 rectBitmap.y = rectButton.GetBottom() - sizeBmp.y - margin.y;
1372 rectButton.height -= sizeBmpWithMargins.y;
1373 break;
1374 }
4e9da8b7 1375 }
8f4745fe
VZ
1376
1377 wxDCTemp dst((WXHDC)hdc);
1378 dst.DrawBitmap(bmp, rectBitmap.GetPosition(), true);
1379
1380 wxCopyRectToRECT(rectButton, rectBtn);
9750fc42
VZ
1381 }
1382
8f4745fe
VZ
1383
1384 // finally draw the label
a2117591 1385 if ( ShowsLabel() )
a6fd73d3
VZ
1386 {
1387 COLORREF colFg = state & ODS_DISABLED
1388 ? ::GetSysColor(COLOR_GRAYTEXT)
1389 : wxColourToRGB(GetForegroundColour());
1390
1391 // notice that DT_HIDEPREFIX doesn't work on old (pre-Windows 2000)
1392 // systems but by happy coincidence ODS_NOACCEL is not used under them
1393 // neither so DT_HIDEPREFIX should never be used there
a2117591 1394 DrawButtonText(hdc, &rectBtn, GetLabel(), colFg,
a6fd73d3
VZ
1395 state & ODS_NOACCEL ? DT_HIDEPREFIX : 0);
1396 }
cd0b1709 1397
fcf90ee1 1398 return true;
cd0b1709
VZ
1399}
1400
1e6feb95 1401#endif // wxUSE_BUTTON
4af4dec6 1402