Skip to content

Commit

Permalink
Use wxBitmapBundle when possible
Browse files Browse the repository at this point in the history
  • Loading branch information
wangqr committed Aug 21, 2022
1 parent db3cdde commit 81513a5
Show file tree
Hide file tree
Showing 13 changed files with 101 additions and 14 deletions.
5 changes: 4 additions & 1 deletion src/audio_karaoke.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,10 @@ AudioKaraoke::AudioKaraoke(wxWindow *parent, agi::Context *c)
{
using std::bind;

#if defined(__WXMSW__)
#if wxCHECK_VERSION(3, 1, 6)
cancel_button = new wxBitmapButton(this, -1, CMD_BITMAP_BUNDLE_GET(kara_split_cancel, wxLayout_Default, 16));
accept_button = new wxBitmapButton(this, -1, CMD_BITMAP_BUNDLE_GET(kara_split_accept, wxLayout_Default, 16));
#elif defined(__WXMSW__)
cancel_button = new wxBitmapButton(this, -1, CMD_ICON_GET(kara_split_cancel, wxLayout_Default, FromDIP(16)));
accept_button = new wxBitmapButton(this, -1, CMD_ICON_GET(kara_split_accept, wxLayout_Default, FromDIP(16)));
#else
Expand Down
8 changes: 8 additions & 0 deletions src/command/command.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,11 @@ namespace agi { struct Context; }
#define STR_DISP(a) wxString StrDisplay(const agi::Context *) const override { return _(a); }
#define STR_HELP(a) wxString StrHelp() const override { return _(a); }
#define CMD_TYPE(a) int Type() const override { using namespace cmd; return a; }
#if wxCHECK_VERSION(3, 1, 6)
#define CMD_ICON(icon) wxBitmapBundle Icon(wxLayoutDirection dir = wxLayout_LeftToRight) const override { return CMD_BITMAP_BUNDLE_GET(icon, dir, 16); }
#else
#define CMD_ICON(icon) wxBitmap Icon(int size, wxLayoutDirection dir = wxLayout_LeftToRight) const override { return CMD_ICON_GET(icon, dir, size); }
#endif

#define COMMAND_GROUP(cname, cmdname, menu, disp, help) \
struct cname final : public Command { \
Expand Down Expand Up @@ -101,7 +105,11 @@ DEFINE_EXCEPTION(CommandNotFound, CommandError);

/// Request icon.
/// @param size Icon size.
#if wxCHECK_VERSION(3, 1, 6)
virtual wxBitmapBundle Icon(wxLayoutDirection = wxLayout_LeftToRight) const { return wxBitmapBundle{}; }
#else
virtual wxBitmap Icon(int size, wxLayoutDirection = wxLayout_LeftToRight) const { return wxBitmap{}; }
#endif

/// Command function
virtual void operator()(agi::Context *c)=0;
Expand Down
10 changes: 8 additions & 2 deletions src/dialog_colorpicker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -460,7 +460,11 @@ class DialogColorPicker final : public wxDialog {
wxSpinCtrl *alpha_input;

/// The eyedropper is set to a blank icon when it's clicked, so store its normal bitmap
#if wxCHECK_VERSION(3, 1, 6)
wxBitmapBundle eyedropper_bitmap;
#else
wxBitmap eyedropper_bitmap;
#endif

/// The point where the eyedropper was click, used to make it possible to either
/// click the eyedropper or drag the eyedropper
Expand Down Expand Up @@ -600,12 +604,14 @@ DialogColorPicker::DialogColorPicker(wxWindow *parent, agi::Color initial_color,
preview_box = new wxStaticBitmap(this, -1, wxBitmap(40, 40, 24), wxDefaultPosition, wxSize(40, 40), STATIC_BORDER_FLAG);
recent_box = new ColorPickerRecent(this, 8, 4, 16);

#if defined(__WXMSW__)
#if wxCHECK_VERSION(3, 1, 6)
eyedropper_bitmap = CMD_BITMAP_BUNDLE_GET(eyedropper_tool, wxLayout_Default, 24);
#elif defined(__WXMSW__)
eyedropper_bitmap = CMD_ICON_GET(eyedropper_tool, wxLayout_Default, FromDIP(24));
#else
eyedropper_bitmap = GETIMAGE(eyedropper_tool_24);
#endif
eyedropper_bitmap.SetMask(new wxMask(eyedropper_bitmap, wxColour(255, 0, 255)));
// eyedropper_bitmap.SetMask(new wxMask(eyedropper_bitmap, wxColour(255, 0, 255)));
#if wxCHECK_VERSION(3, 1, 0) && defined(__WXMAC__)
screen_dropper_icon = new wxGenericStaticBitmap(this, -1, eyedropper_bitmap, wxDefaultPosition, wxDefaultSize, wxRAISED_BORDER);
#else
Expand Down
12 changes: 11 additions & 1 deletion src/dialog_style_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,11 @@ class DialogStyleManager final : public wxDialog {
DialogStyleManager(agi::Context *context);
};

#if wxCHECK_VERSION(3, 1, 6)
wxBitmapButton* add_bitmap_button(wxWindow* parent, wxSizer* sizer, wxBitmapBundle const& img, wxString const& tooltip) {
#else
wxBitmapButton *add_bitmap_button(wxWindow *parent, wxSizer *sizer, wxBitmap const& img, wxString const& tooltip) {
#endif
wxBitmapButton *btn = new wxBitmapButton(parent, -1, img);
btn->SetToolTip(tooltip);
sizer->Add(btn, wxSizerFlags().Expand());
Expand All @@ -186,7 +190,13 @@ wxSizer *make_move_buttons(wxWindow *parent, wxButton **up, wxButton **down, wxB
wxSizer *sizer = new wxBoxSizer(wxVERTICAL);
sizer->AddStretchSpacer(1);

#if defined(__WXMSW__)
#if wxCHECK_VERSION(3, 1, 6)
*up = add_bitmap_button(parent, sizer, CMD_BITMAP_BUNDLE_GET(arrow_up, wxLayout_Default, 24), _("Move style up"));
*down = add_bitmap_button(parent, sizer, CMD_BITMAP_BUNDLE_GET(arrow_down, wxLayout_Default, 24), _("Move style down"));
*top = add_bitmap_button(parent, sizer, CMD_BITMAP_BUNDLE_GET(arrow_up_stop, wxLayout_Default, 24), _("Move style to top"));
*bottom = add_bitmap_button(parent, sizer, CMD_BITMAP_BUNDLE_GET(arrow_down_stop, wxLayout_Default, 24), _("Move style to bottom"));
*sort = add_bitmap_button(parent, sizer, CMD_BITMAP_BUNDLE_GET(arrow_sort, wxLayout_Default, 24), _("Sort styles alphabetically"));
#elif defined(__WXMSW__)
*up = add_bitmap_button(parent, sizer, CMD_ICON_GET(arrow_up, wxLayout_Default, parent->FromDIP(24)), _("Move style up"));
*down = add_bitmap_button(parent, sizer, CMD_ICON_GET(arrow_down, wxLayout_Default, parent->FromDIP(24)), _("Move style down"));
*top = add_bitmap_button(parent, sizer, CMD_ICON_GET(arrow_up_stop, wxLayout_Default, parent->FromDIP(24)), _("Move style to top"));
Expand Down
4 changes: 4 additions & 0 deletions src/hotkey_data_view_model.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,9 @@ class HotkeyModelCombo final : public HotkeyModelItem {
if (col == 0)
variant = to_wx(combo.Str());
else if (col == 1) {
#if wxCHECK_VERSION(3, 1, 6)
variant << wxDataViewIconText(to_wx(combo.CmdName()), cmd::get(combo.CmdName())->Icon());
#else
wxIcon icon;
try {
auto icon_bmp = cmd::get(combo.CmdName())->Icon(16);
Expand All @@ -95,6 +98,7 @@ class HotkeyModelCombo final : public HotkeyModelItem {
// Just use no icon; error is reported in the description column
}
variant << wxDataViewIconText(to_wx(combo.CmdName()), icon);
#endif
}
else if (col == 2) {
try {
Expand Down
21 changes: 21 additions & 0 deletions src/libresrc/libresrc.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,13 @@
// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
// ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
// OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#pragma once

#include <cstdlib>
#include <utility>

#include <wx/version.h>

#include "bitmap.h"
#include "default_config.h"

Expand All @@ -33,4 +36,22 @@ wxIcon libresrc_geticon(const unsigned char *image, size_t size);
(size) <= 48 ? GETIMAGEDIR(icon##_48, (dir), (size)) : GETIMAGEDIR(icon##_64, (dir), (size)) )
#define GETICON(a) libresrc_geticon(a, sizeof(a))

#if wxCHECK_VERSION(3, 1, 6)
#include <wx/bmpbndl.h>
template<int size>
wxBitmapBundle libresrc_getbitmapbundle(wxBitmap image16, wxBitmap image24, wxBitmap image32, wxBitmap image48, wxBitmap image64) {
wxVector<wxBitmap> bitmaps;
if (size <= 16) bitmaps.push_back(image16);
if (size <= 24) bitmaps.push_back(image24);
if (size <= 32) bitmaps.push_back(image32);
if (size <= 48) bitmaps.push_back(image48);
bitmaps.push_back(image64);
return wxBitmapBundle::FromBitmaps(bitmaps);
}
#define CMD_BITMAP_BUNDLE_GET(icon, dir, size) libresrc_getbitmapbundle<size>( \
GETIMAGEDIR(icon##_16, (dir), 16) , GETIMAGEDIR(icon##_24, (dir), 24) , \
GETIMAGEDIR(icon##_32, (dir), 32) , GETIMAGEDIR(icon##_48, (dir), 48) , \
GETIMAGEDIR(icon##_64, (dir), 64) )
#endif

#define GET_DEFAULT_CONFIG(a) std::make_pair(reinterpret_cast<const char *>(a), sizeof(a))
5 changes: 4 additions & 1 deletion src/menu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,10 @@ class CommandManager {
menu_text += to_wx("\t" + hotkey::get_hotkey_str_first("Default", co->name()));

wxMenuItem *item = new wxMenuItem(parent, id_base + items.size(), menu_text, co->StrHelp(), kind);
#if defined(__WXMSW__)
#if wxCHECK_VERSION(3, 1, 6)
if (kind == wxITEM_NORMAL)
item->SetBitmap(co->Icon());
#elif defined(__WXMSW__)
#if wxCHECK_VERSION(3, 1, 3)
if (kind == wxITEM_NORMAL) {
int size = wxRendererNative::Get().GetCheckMarkSize(context->parent).GetWidth();
Expand Down
4 changes: 3 additions & 1 deletion src/subs_edit_box.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,9 @@ TimeEdit *SubsEditBox::MakeTimeCtrl(wxString const& tooltip, TimeField field) {

void SubsEditBox::MakeButton(const char *cmd_name) {
cmd::Command *command = cmd::get(cmd_name);
#ifdef __WXMSW__
#if wxCHECK_VERSION(3, 1, 6)
wxBitmapButton* btn = new wxBitmapButton(this, -1, command->Icon());
#elif defined(__WXMSW__)
wxBitmapButton* btn = new wxBitmapButton(this, -1, command->Icon(FromDIP(16)));
#else
wxBitmapButton *btn = new wxBitmapButton(this, -1, command->Icon(OPT_GET("App/Toolbar Icon Size")->GetInt()));
Expand Down
15 changes: 15 additions & 0 deletions src/toggle_bitmap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,19 @@ ToggleBitmap::ToggleBitmap(wxWindow *parent, agi::Context *context, const char *
: wxControl(parent, -1, wxDefaultPosition, wxDefaultSize, wxSUNKEN_BORDER)
, context(context)
, command(*cmd::get(cmd_name))
#if wxCHECK_VERSION(3, 1, 6)
, img(command.Icon())
#else
, img(command.Icon(icon_size))
#endif
{
#if wxCHECK_VERSION(3, 1, 6)
int w = size.GetWidth() != -1 ? size.GetWidth() : img.GetPreferredLogicalSizeFor(this).GetWidth();
int h = size.GetHeight() != -1 ? size.GetHeight() : img.GetPreferredLogicalSizeFor(this).GetWidth();
#else
int w = size.GetWidth() != -1 ? size.GetWidth() : img.GetWidth();
int h = size.GetHeight() != -1 ? size.GetHeight() : img.GetHeight();
#endif
SetClientSize(w, h);
GetSize(&w, &h);
SetSizeHints(w, h, w, h);
Expand Down Expand Up @@ -81,6 +90,12 @@ void ToggleBitmap::OnPaint(wxPaintEvent &) {
dc.SetBrush(wxBrush(bgColor));
dc.DrawRectangle(wxPoint(0, 0), GetClientSize());

#if wxCHECK_VERSION(3, 1, 6)
wxBitmap bitmap = img.GetBitmapFor(this);
wxSize excess = (GetClientSize() - FromPhys(bitmap.GetSize())) / 2;
dc.DrawBitmap(bitmap, excess.GetX(), excess.GetY(), true);
#else
wxSize excess = (GetClientSize() - img.GetSize()) / 2;
dc.DrawBitmap(img, excess.GetX(), excess.GetY(), true);
#endif
}
4 changes: 4 additions & 0 deletions src/toggle_bitmap.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,11 @@ namespace cmd { class Command; }
class ToggleBitmap final : public wxControl {
agi::Context *context;
cmd::Command &command;
#if wxCHECK_VERSION(3, 1, 6)
wxBitmapBundle img;
#else
wxBitmap img;
#endif

void OnMouseEvent(wxMouseEvent &evt);
void OnPaint(wxPaintEvent &evt);
Expand Down
12 changes: 10 additions & 2 deletions src/toolbar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,11 @@ namespace {
flags & cmd::COMMAND_TOGGLE ? wxITEM_CHECK :
wxITEM_NORMAL;

#if wxCHECK_VERSION(3, 1, 6)
wxBitmapBundle const& bitmap = command->Icon(GetLayoutDirection());
#else
wxBitmap const& bitmap = command->Icon(icon_size, GetLayoutDirection());
#endif
AddTool(TOOL_ID_BASE + commands.size(), command->StrDisplay(context), bitmap, GetTooltip(command), kind);

commands.push_back(command);
Expand Down Expand Up @@ -171,7 +175,9 @@ namespace {
, context(c)
, ht_context(std::move(ht_context))
, retina_helper(parent)
#ifdef __WXMSW__
#if wxCHECK_VERSION(3, 1, 6)
, icon_size(16)
#elif defined(__WXMSW__)
, icon_size(parent->FromDIP(16))
#else
, icon_size(OPT_GET("App/Toolbar Icon Size")->GetInt())
Expand All @@ -190,7 +196,9 @@ namespace {
, ht_context(std::move(ht_context))
, retina_helper(parent)
#ifndef __WXMAC__
#ifdef __WXMSW__
#if wxCHECK_VERSION(3, 1, 6)
, icon_size(16)
#elif defined(__WXMSW__)
, icon_size(parent->FromDIP(16))
#else
, icon_size(OPT_GET("App/Toolbar Icon Size")->GetInt())
Expand Down
4 changes: 3 additions & 1 deletion src/visual_tool_drag.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,9 @@ static const DraggableFeatureType DRAG_ORIGIN = DRAG_BIG_TRIANGLE;
static const DraggableFeatureType DRAG_START = DRAG_BIG_SQUARE;
static const DraggableFeatureType DRAG_END = DRAG_BIG_CIRCLE;

#ifdef __WXMSW__
#if wxCHECK_VERSION(3, 1, 6)
#define ICON(name) CMD_BITMAP_BUNDLE_GET(name, wxLayout_Default, 16)
#elif defined(__WXMSW__)
#define ICON(name) CMD_ICON_GET(name, wxLayout_Default, toolbar->FromDIP(16))
#else
#define ICON(name) CMD_ICON_GET(name, wxLayout_Default, OPT_GET("App/Toolbar Icon Size")->GetInt())
Expand Down
11 changes: 6 additions & 5 deletions src/visual_tool_vector_clip.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,14 @@ void VisualToolVectorClip::SetToolbar(wxToolBar *toolBar) {
this->toolBar = toolBar;

toolBar->AddSeparator();
#ifdef __WXMSW__
int icon_size = toolBar->FromDIP(16);

#if wxCHECK_VERSION(3, 1, 6)
#define ICON(name) CMD_BITMAP_BUNDLE_GET(name, wxLayout_Default, 16)
#elif defined(__WXMSW__)
#define ICON(name) CMD_ICON_GET(name, wxLayout_Default, toolbar->FromDIP(16))
#else
int icon_size = OPT_GET("App/Toolbar Icon Size")->GetInt();
#define ICON(name) CMD_ICON_GET(name, wxLayout_Default, OPT_GET("App/Toolbar Icon Size")->GetInt())
#endif

#define ICON(name) CMD_ICON_GET(name, wxLayout_Default, icon_size)
toolBar->AddTool(BUTTON_DRAG, _("Drag"), ICON(visual_vector_clip_drag), _("Drag control points"), wxITEM_CHECK);
toolBar->AddTool(BUTTON_LINE, _("Line"), ICON(visual_vector_clip_line), _("Appends a line"), wxITEM_CHECK);
toolBar->AddTool(BUTTON_BICUBIC, _("Bicubic"), ICON(visual_vector_clip_bicubic), _("Appends a bezier bicubic curve"), wxITEM_CHECK);
Expand Down

0 comments on commit 81513a5

Please sign in to comment.