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