1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
|
// **************************************************************************
// * This file is part of the FreeFileSync project. It is distributed under *
// * GNU General Public License: http://www.gnu.org/licenses/gpl.html *
// * Copyright (C) Zenju (zenju AT gmx DOT de) - All Rights Reserved *
// **************************************************************************
#ifndef BUTTON_HEADER_83415718945878341563415
#define BUTTON_HEADER_83415718945878341563415
#include <wx/bmpbuttn.h>
#include "image_tools.h"
namespace zen
{
//zen::BitmapTextButton is identical to wxBitmapButton, but preserves the label via SetLabel(), which wxFormbuilder would ditch!
class BitmapTextButton : public wxBitmapButton
{
public:
BitmapTextButton(wxWindow* parent,
wxWindowID id,
const wxString& label,
const wxPoint& pos = wxDefaultPosition,
const wxSize& size = wxDefaultSize,
long style = 0,
const wxValidator& validator = wxDefaultValidator,
const wxString& name = wxButtonNameStr) :
wxBitmapButton(parent, id, wxNullBitmap, pos, size, style | wxBU_AUTODRAW, validator, name) { SetLabel(label); }
};
void setBitmapTextLabel(wxBitmapButton& btn, const wxImage& img, const wxString& text, int gap = 5, int border = 5);
//set bitmap label flicker free:
void setImage(wxBitmapButton& button, const wxBitmap& bmp);
//################################### implementation ###################################
inline
void setBitmapTextLabel(wxBitmapButton& btn, const wxImage& img, const wxString& text, int gap, int border)
{
assert(gap >= 0 && border >= 0);
gap = std::max(0, gap);
border = std::max(0, border);
wxImage dynImage = createImageFromText(text, btn.GetFont(), btn.GetForegroundColour());
if (img.IsOk())
{
if (btn.GetLayoutDirection() != wxLayout_RightToLeft)
dynImage = stackImages(img, dynImage, ImageStackLayout::HORIZONTAL, ImageStackAlignment::CENTER, gap);
else
dynImage = stackImages(dynImage, img, ImageStackLayout::HORIZONTAL, ImageStackAlignment::CENTER, gap);
}
//SetMinSize() instead of SetSize() is needed here for wxWindows layout determination to work corretly
wxSize minSize = btn.GetMinSize();
btn.SetMinSize(wxSize(std::max(dynImage.GetWidth () + 2 * border, minSize.GetWidth()),
std::max(dynImage.GetHeight() + 2 * border, minSize.GetHeight())));
btn.SetBitmapLabel(wxBitmap(dynImage));
//SetLabel() calls confuse wxBitmapButton in the disabled state and it won't show the image! workaround:
btn.SetBitmapDisabled(wxBitmap(dynImage.ConvertToDisabled()));
}
inline
void setImage(wxBitmapButton& button, const wxBitmap& bmp)
{
if (!isEqual(button.GetBitmapLabel(), bmp))
{
button.SetBitmapLabel(bmp);
//wxWidgets excels at screwing up consistently once again:
//the first call to SetBitmapLabel() *implicitly* sets the disabled bitmap, too, subsequent calls, DON'T!
button.SetBitmapDisabled(bmp.ConvertToDisabled()); //inefficiency: wxBitmap::ConvertToDisabled() implicitly converts to wxImage!
}
}
}
#endif //BUTTON_HEADER_83415718945878341563415
|