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