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