Redesign ToggleBitmap to operate on a command

Originally committed to SVN as r5565.
This commit is contained in:
Thomas Goyne 2011-08-27 06:30:49 +00:00
parent 3ecfd34bd3
commit a45b971b3a
5 changed files with 37 additions and 147 deletions

View file

@ -66,7 +66,6 @@
#include "main.h" #include "main.h"
#include "toggle_bitmap.h" #include "toggle_bitmap.h"
#include "selection_controller.h" #include "selection_controller.h"
#include "tooltip_manager.h"
#include "utils.h" #include "utils.h"
enum AudioBoxControlIDs { enum AudioBoxControlIDs {
@ -110,11 +109,7 @@ AudioBox::AudioBox(wxWindow *parent, agi::Context *context)
wxSizer *VertVolArea = new wxBoxSizer(wxVERTICAL); wxSizer *VertVolArea = new wxBoxSizer(wxVERTICAL);
VertVolArea->Add(VertVol,1,wxEXPAND,0); VertVolArea->Add(VertVol,1,wxEXPAND,0);
cmd::Command *link_command = cmd::get("audio/opt/vertical_link"); ToggleBitmap *link_btn = new ToggleBitmap(this, context, "audio/opt/vertical_link", 16, "Audio", wxSize(20, -1));
ToggleBitmap *link_btn = new ToggleBitmap(this, cmd::id("audio/opt/vertical_link"), link_command->Icon(16), wxSize(20, -1));
ToolTipManager::Bind(link_btn, link_command->StrHelp(), "Audio", "audio/opt/vertical_link");
link_btn->SetValue(OPT_GET("Audio/Link")->GetBool());
link_btn->Bind(wxEVT_COMMAND_TOGGLEBUTTON_CLICKED, &AudioBox::OnVerticalLinkButton, this);
VertVolArea->Add(link_btn, 0, wxRIGHT | wxALIGN_CENTER | wxEXPAND, 0); VertVolArea->Add(link_btn, 0, wxRIGHT | wxALIGN_CENTER | wxEXPAND, 0);
OPT_SUB("Audio/Link", bind(&AudioBox::OnVerticalLink, this, std::tr1::placeholders::_1)); OPT_SUB("Audio/Link", bind(&AudioBox::OnVerticalLink, this, std::tr1::placeholders::_1));
@ -186,10 +181,6 @@ BEGIN_EVENT_TABLE(AudioBox,wxPanel)
EVT_TOGGLEBUTTON(Audio_Button_Karaoke, AudioBox::OnKaraoke) EVT_TOGGLEBUTTON(Audio_Button_Karaoke, AudioBox::OnKaraoke)
END_EVENT_TABLE() END_EVENT_TABLE()
void AudioBox::OnVerticalLinkButton(wxCommandEvent &) {
cmd::call("audio/opt/vertical_link", context);
}
void AudioBox::OnHorizontalZoom(wxScrollEvent &event) { void AudioBox::OnHorizontalZoom(wxScrollEvent &event) {
// Negate the value, we want zoom out to be on bottom and zoom in on top, // Negate the value, we want zoom out to be on bottom and zoom in on top,
// but the control doesn't want negative on bottom and positive on top. // but the control doesn't want negative on bottom and positive on top.

View file

@ -103,7 +103,6 @@ class AudioBox : public wxPanel {
void OnVerticalZoom(wxScrollEvent &event); void OnVerticalZoom(wxScrollEvent &event);
void OnVolume(wxScrollEvent &event); void OnVolume(wxScrollEvent &event);
void OnVerticalLink(agi::OptionValue const& opt); void OnVerticalLink(agi::OptionValue const& opt);
void OnVerticalLinkButton(wxCommandEvent&);
void OnKaraoke(wxCommandEvent &); void OnKaraoke(wxCommandEvent &);
void OnJoin(wxCommandEvent &); void OnJoin(wxCommandEvent &);

View file

@ -34,9 +34,6 @@
/// @ingroup custom_control /// @ingroup custom_control
/// ///
///////////
// Headers
#include "config.h" #include "config.h"
#ifndef AGI_PRE #ifndef AGI_PRE
@ -47,125 +44,48 @@
#include "toggle_bitmap.h" #include "toggle_bitmap.h"
#include "command/command.h"
#include "include/aegisub/context.h"
#include "tooltip_manager.h"
/// @brief Constructor ToggleBitmap::ToggleBitmap(wxWindow *parent, agi::Context *context, const char *cmd_name, int icon_size, const char *ht_ctx, wxSize const& size)
/// @param parent : wxControl(parent, -1, wxDefaultPosition, wxDefaultSize, wxSUNKEN_BORDER)
/// @param id , context(context)
/// @param image , command(cmd::get(cmd_name))
/// @param size , img(command->Icon(icon_size))
///
ToggleBitmap::ToggleBitmap(wxWindow *parent,wxWindowID id,const wxBitmap &image,const wxSize &size)
: wxControl (parent,id,wxDefaultPosition,wxDefaultSize,wxSUNKEN_BORDER)
{ {
// Set variables int w = size.GetWidth() != -1 ? size.GetWidth() : img.GetWidth();
img = image; int h = size.GetHeight() != -1 ? size.GetHeight() : img.GetHeight();
state = false;
// Set size
int w,h;
if (size.GetWidth() != -1) w = size.GetWidth();
else w = img.GetWidth();
if (size.GetHeight() != -1) h = size.GetHeight();
else h = img.GetHeight();
SetClientSize(w, h); SetClientSize(w, h);
GetSize(&w, &h); GetSize(&w, &h);
SetSizeHints(w, h, w, h); SetSizeHints(w, h, w, h);
ToolTipManager::Bind(this, command->StrHelp(), ht_ctx, cmd_name);
Bind(wxEVT_PAINT, &ToggleBitmap::OnPaint, this);
Bind(wxEVT_LEFT_DOWN, &ToggleBitmap::OnMouseEvent, this);
} }
void ToggleBitmap::OnMouseEvent(wxMouseEvent &) {
(*command)(context);
/// @brief Get state
/// @return
///
bool ToggleBitmap::GetValue() {
return state;
}
/// @brief Set state
/// @param _state
///
void ToggleBitmap::SetValue(bool _state) {
// Set flag
state = _state;
// Refresh
Refresh(false); Refresh(false);
} }
void ToggleBitmap::OnPaint(wxPaintEvent &) {
wxPaintDC dc(this);
/// @brief Draw image
/// @param dc
///
void ToggleBitmap::DrawImage(wxDC &dc) {
// Get size
int w,h; int w,h;
GetClientSize(&w,&h); GetClientSize(&w,&h);
// Get background color // Get background color
wxColour bgColor; wxColour bgColor = command->IsActive(context) ? wxColour(0,255,0) : wxColour(255,0,0);
if (state) bgColor = wxColour(0,255,0);
else bgColor = wxColour(255,0,0);
wxColor sysCol = wxSystemSettings::GetColour(wxSYS_COLOUR_BTNHIGHLIGHT); wxColor sysCol = wxSystemSettings::GetColour(wxSYS_COLOUR_BTNHIGHLIGHT);
int r,g,b; bgColor.Set(
r = (sysCol.Red() + bgColor.Red()) / 2; (sysCol.Red() + bgColor.Red()) / 2,
g = (sysCol.Green() + bgColor.Green()) / 2; (sysCol.Green() + bgColor.Green()) / 2,
b = (sysCol.Blue() + bgColor.Blue()) / 2; (sysCol.Blue() + bgColor.Blue()) / 2);
bgColor.Set(r,g,b);
// Draw background
dc.SetPen(*wxTRANSPARENT_PEN); dc.SetPen(*wxTRANSPARENT_PEN);
dc.SetBrush(wxBrush(bgColor)); dc.SetBrush(wxBrush(bgColor));
dc.DrawRectangle(0, 0, w, h); dc.DrawRectangle(0, 0, w, h);
// Draw bitmap
dc.DrawBitmap(img, (w - img.GetWidth()) / 2, (h - img.GetHeight()) / 2, true); dc.DrawBitmap(img, (w - img.GetWidth()) / 2, (h - img.GetHeight()) / 2, true);
} }
///////////////
// Event table
BEGIN_EVENT_TABLE(ToggleBitmap,wxControl)
EVT_MOUSE_EVENTS(ToggleBitmap::OnMouseEvent)
EVT_PAINT(ToggleBitmap::OnPaint)
END_EVENT_TABLE()
/// @brief Mouse events
/// @param event
///
void ToggleBitmap::OnMouseEvent(wxMouseEvent &event) {
// Get mouse position
int x = event.GetX();
int y = event.GetY();
int w,h;
GetClientSize(&w,&h);
bool inside = true;
if (x < 0 || y < y || x >= w || y >= h) inside = false;
// Left click to toggle state
if (inside && event.ButtonDown(wxMOUSE_BTN_LEFT)) {
// Update
state = !state;
Refresh(false);
// Send event
wxCommandEvent sendEvent(wxEVT_COMMAND_TOGGLEBUTTON_CLICKED,GetId());
AddPendingEvent(sendEvent);
}
}
/// @brief Paint event
/// @param event
///
void ToggleBitmap::OnPaint(wxPaintEvent &event) {
wxPaintDC dc(this);
DrawImage(dc);
}

View file

@ -34,16 +34,13 @@
/// @ingroup custom_control /// @ingroup custom_control
/// ///
///////////
// Headers
#ifndef AGI_PRE #ifndef AGI_PRE
#include <wx/bitmap.h> #include <wx/bitmap.h>
#include <wx/control.h> #include <wx/control.h>
#endif #endif
namespace agi { struct Context; }
namespace cmd { class Command; }
/// DOCME /// DOCME
/// @class ToggleBitmap /// @class ToggleBitmap
@ -51,22 +48,13 @@
/// ///
/// DOCME /// DOCME
class ToggleBitmap : public wxControl { class ToggleBitmap : public wxControl {
private: agi::Context *context;
cmd::Command *command;
/// DOCME
wxBitmap img; wxBitmap img;
/// DOCME void OnMouseEvent(wxMouseEvent &evt);
bool state; void OnPaint(wxPaintEvent &evt);
void OnMouseEvent(wxMouseEvent &event);
void OnPaint(wxPaintEvent &event);
void DrawImage(wxDC &dc);
public: public:
ToggleBitmap(wxWindow *parent,wxWindowID id,const wxBitmap &image,const wxSize &size=wxDefaultSize); ToggleBitmap(wxWindow *parent, agi::Context *context, const char *command, int icon_size, const char *ht_ctx, wxSize const& size = wxDefaultSize);
bool GetValue();
void SetValue(bool state);
DECLARE_EVENT_TABLE()
}; };

View file

@ -70,14 +70,6 @@ static void add_button(wxWindow *parent, wxSizer *sizer, const char *command) {
sizer->Add(btn, 0, wxTOP | wxLEFT | wxBOTTOM | wxALIGN_CENTER, 2);; sizer->Add(btn, 0, wxTOP | wxLEFT | wxBOTTOM | wxALIGN_CENTER, 2);;
} }
static void add_option(wxWindow *parent, wxSizer *sizer, const char *command, const char *option) {
cmd::Command *c = cmd::get(command);
ToggleBitmap *btn = new ToggleBitmap(parent, cmd::id(command), c->Icon(24));
ToolTipManager::Bind(btn, c->StrHelp(), "Video", command);
btn->SetValue(OPT_GET(option)->GetBool());
sizer->Add(btn, 0, wxTOP | wxLEFT | wxBOTTOM | wxALIGN_CENTER, 2);
}
VideoBox::VideoBox(wxWindow *parent, bool isDetached, agi::Context *context) VideoBox::VideoBox(wxWindow *parent, bool isDetached, agi::Context *context)
: wxPanel (parent,-1) : wxPanel (parent,-1)
, context(context) , context(context)
@ -90,7 +82,7 @@ VideoBox::VideoBox(wxWindow *parent, bool isDetached, agi::Context *context)
add_button(this, videoBottomSizer, "video/play"); add_button(this, videoBottomSizer, "video/play");
add_button(this, videoBottomSizer, "video/play/line"); add_button(this, videoBottomSizer, "video/play/line");
add_button(this, videoBottomSizer, "video/stop"); add_button(this, videoBottomSizer, "video/stop");
add_option(this, videoBottomSizer, "video/opt/autoscroll", "Video/Subtitle Sync"); videoBottomSizer->Add(new ToggleBitmap(this, context, "video/opt/autoscroll", 24, "Video"), 0, wxTOP | wxLEFT | wxBOTTOM | wxALIGN_CENTER, 2);
// Seek // Seek
videoSlider = new VideoSlider(this, context); videoSlider = new VideoSlider(this, context);