Factor out a bunch of duplicated code for clipboard stuff

This commit is contained in:
Thomas Goyne 2012-10-25 08:13:13 -07:00
parent ff6394c95e
commit df60c2e7a4
10 changed files with 72 additions and 97 deletions

View file

@ -281,18 +281,11 @@ namespace {
int clipboard_get(lua_State *L) int clipboard_get(lua_State *L)
{ {
if (wxTheClipboard->Open()) { wxString data = GetClipboard();
if (wxTheClipboard->IsSupported(wxDF_TEXT)) { if (!data)
wxTextDataObject rawdata;
wxTheClipboard->GetData(rawdata);
lua_pushstring(L, rawdata.GetText().utf8_str());
}
else
lua_pushnil(L);
wxTheClipboard->Close();
}
else
lua_pushnil(L); lua_pushnil(L);
else
lua_pushstring(L, data.utf8_str());
return 1; return 1;
} }

View file

@ -61,6 +61,7 @@
#include "../subs_edit_ctrl.h" #include "../subs_edit_ctrl.h"
#include "../subs_grid.h" #include "../subs_grid.h"
#include "../text_selection_controller.h" #include "../text_selection_controller.h"
#include "../utils.h"
#include "../video_context.h" #include "../video_context.h"
namespace { namespace {
@ -83,15 +84,7 @@ struct validate_sel_multiple : public Command {
}; };
void paste_lines(agi::Context *c, bool paste_over) { void paste_lines(agi::Context *c, bool paste_over) {
wxString data; wxString data = GetClipboard();
if (wxTheClipboard->Open()) {
if (wxTheClipboard->IsSupported(wxDF_TEXT)) {
wxTextDataObject rawdata;
wxTheClipboard->GetData(rawdata);
data = rawdata.GetText();
}
wxTheClipboard->Close();
}
if (!data) return; if (!data) return;
AssDialogue *rel_line = c->selectionController->GetActiveLine(); AssDialogue *rel_line = c->selectionController->GetActiveLine();
@ -505,10 +498,7 @@ static void copy_lines(agi::Context *c) {
} }
} }
if (wxTheClipboard->Open()) { SetClipboard(data);
wxTheClipboard->SetData(new wxTextDataObject(data));
wxTheClipboard->Close();
}
} }
static void delete_lines(agi::Context *c, wxString const& commit_message) { static void delete_lines(agi::Context *c, wxString const& commit_message) {
@ -782,12 +772,12 @@ struct edit_line_paste : public Command {
CMD_TYPE(COMMAND_VALIDATE) CMD_TYPE(COMMAND_VALIDATE)
bool Validate(const agi::Context *) { bool Validate(const agi::Context *) {
bool can_paste = false;
if (wxTheClipboard->Open()) { if (wxTheClipboard->Open()) {
bool can_paste = wxTheClipboard->IsSupported(wxDF_TEXT); can_paste = wxTheClipboard->IsSupported(wxDF_TEXT);
wxTheClipboard->Close(); wxTheClipboard->Close();
return can_paste;
} }
return false; return can_paste;
} }
void operator()(agi::Context *c) { void operator()(agi::Context *c) {
@ -808,12 +798,12 @@ struct edit_line_paste_over : public Command {
CMD_TYPE(COMMAND_VALIDATE) CMD_TYPE(COMMAND_VALIDATE)
bool Validate(const agi::Context *c) { bool Validate(const agi::Context *c) {
if (wxTheClipboard->Open()) { bool can_paste = !c->selectionController->GetSelectedSet().empty();
bool can_paste = wxTheClipboard->IsSupported(wxDF_TEXT); if (can_paste && wxTheClipboard->Open()) {
can_paste = wxTheClipboard->IsSupported(wxDF_TEXT);
wxTheClipboard->Close(); wxTheClipboard->Close();
return can_paste && c->selectionController->GetSelectedSet().size();
} }
return false; return can_paste;
} }
void operator()(agi::Context *c) { void operator()(agi::Context *c) {

View file

@ -241,10 +241,7 @@ struct video_copy_coordinates : public validator_video_loaded {
STR_HELP("Copy the current coordinates of the mouse over the video to the clipboard") STR_HELP("Copy the current coordinates of the mouse over the video to the clipboard")
void operator()(agi::Context *c) { void operator()(agi::Context *c) {
if (wxTheClipboard->Open()) { SetClipboard(c->videoDisplay->GetMousePosition().Str());
wxTheClipboard->SetData(new wxTextDataObject(c->videoDisplay->GetMousePosition().Str()));
wxTheClipboard->Close();
}
} }
}; };
@ -308,10 +305,7 @@ struct video_frame_copy : public validator_video_loaded {
STR_HELP("Copy the currently displayed frame to the clipboard") STR_HELP("Copy the currently displayed frame to the clipboard")
void operator()(agi::Context *c) { void operator()(agi::Context *c) {
if (wxTheClipboard->Open()) { SetClipboard(wxBitmap(c->videoController->GetFrame(c->videoController->GetFrameN())->GetImage(), 24));
wxTheClipboard->SetData(new wxBitmapDataObject(wxBitmap(c->videoController->GetFrame(c->videoController->GetFrameN())->GetImage(),24)));
wxTheClipboard->Close();
}
} }
}; };
@ -323,10 +317,7 @@ struct video_frame_copy_raw : public validator_video_loaded {
STR_HELP("Copy the currently displayed frame to the clipboard, without the subtitles") STR_HELP("Copy the currently displayed frame to the clipboard, without the subtitles")
void operator()(agi::Context *c) { void operator()(agi::Context *c) {
if (wxTheClipboard->Open()) { SetClipboard(wxBitmap(c->videoController->GetFrame(c->videoController->GetFrameN(), true)->GetImage(), 24));
wxTheClipboard->SetData(new wxBitmapDataObject(wxBitmap(c->videoController->GetFrame(c->videoController->GetFrameN(), true)->GetImage(),24)));
wxTheClipboard->Close();
}
} }
}; };

View file

@ -67,6 +67,7 @@
#include "selection_controller.h" #include "selection_controller.h"
#include "standard_paths.h" #include "standard_paths.h"
#include "subtitle_format.h" #include "subtitle_format.h"
#include "utils.h"
using std::tr1::placeholders::_1; using std::tr1::placeholders::_1;
@ -121,22 +122,9 @@ wxString unique_name(Func name_checker, wxString const& source_name) {
return source_name; return source_name;
} }
wxString get_clipboard_text() {
wxString text;
if (wxTheClipboard->Open()) {
if (wxTheClipboard->IsSupported(wxDF_TEXT)) {
wxTextDataObject rawdata;
wxTheClipboard->GetData(rawdata);
text = rawdata.GetText();
}
wxTheClipboard->Close();
}
return text;
}
template<class Func1, class Func2> template<class Func1, class Func2>
void add_styles(Func1 name_checker, Func2 style_adder) { void add_styles(Func1 name_checker, Func2 style_adder) {
wxStringTokenizer st(get_clipboard_text(), '\n'); wxStringTokenizer st(GetClipboard(), '\n');
while (st.HasMoreTokens()) { while (st.HasMoreTokens()) {
try { try {
AssStyle *s = new AssStyle(st.GetNextToken().Trim(true)); AssStyle *s = new AssStyle(st.GetNextToken().Trim(true));
@ -485,10 +473,7 @@ void DialogStyleManager::CopyToClipboard(wxListBox *list, T const& v) {
data += s->GetEntryData(); data += s->GetEntryData();
} }
if (wxTheClipboard->Open()) { SetClipboard(data);
wxTheClipboard->SetData(new wxTextDataObject(data));
wxTheClipboard->Close();
}
} }
void DialogStyleManager::PasteToCurrent() { void DialogStyleManager::PasteToCurrent() {

View file

@ -295,11 +295,6 @@ int AegisubApp::OnExit() {
if (frame) if (frame)
delete frame; delete frame;
if (wxTheClipboard->Open()) {
wxTheClipboard->Flush();
wxTheClipboard->Close();
}
SubtitleFormat::DestroyFormats(); SubtitleFormat::DestroyFormats();
VideoContext::OnExit(); VideoContext::OnExit();
delete plugins; delete plugins;

View file

@ -292,7 +292,7 @@ void SubsEditBox::OnCommit(int type) {
} }
if (type & AssFile::COMMIT_DIAG_TEXT) { if (type & AssFile::COMMIT_DIAG_TEXT) {
TextEdit->SetTextTo(line->Text); TextEdit->SetValue(line->Text);
} }
if (type & AssFile::COMMIT_DIAG_META) { if (type & AssFile::COMMIT_DIAG_META) {
@ -497,7 +497,7 @@ void SubsEditBox::SetControlsState(bool state) {
Enable(state); Enable(state);
if (!state) { if (!state) {
wxEventBlocker blocker(this); wxEventBlocker blocker(this);
TextEdit->SetTextTo(""); TextEdit->SetValue("");
} }
} }

View file

@ -703,15 +703,7 @@ void SubsTextEditCtrl::SetTextTo(wxString text) {
void SubsTextEditCtrl::Paste() { void SubsTextEditCtrl::Paste() {
wxString data; wxString data = GetClipboard();
if (wxTheClipboard->Open()) {
if (wxTheClipboard->IsSupported(wxDF_TEXT)) {
wxTextDataObject rawdata;
wxTheClipboard->GetData(rawdata);
data = rawdata.GetText();
}
wxTheClipboard->Close();
}
data.Replace("\r\n", "\\N"); data.Replace("\r\n", "\\N");
data.Replace("\n", "\\N"); data.Replace("\n", "\\N");

View file

@ -51,6 +51,7 @@
#include "compat.h" #include "compat.h"
#include "include/aegisub/context.h" #include "include/aegisub/context.h"
#include "main.h" #include "main.h"
#include "utils.h"
#include "video_context.h" #include "video_context.h"
#define TimeEditWindowStyle #define TimeEditWindowStyle
@ -224,10 +225,7 @@ void TimeEdit::OnFocusLost(wxFocusEvent &evt) {
} }
void TimeEdit::CopyTime() { void TimeEdit::CopyTime() {
if (wxTheClipboard->Open()) { SetClipboard(GetValue());
wxTheClipboard->SetData(new wxTextDataObject(GetValue()));
wxTheClipboard->Close();
}
} }
void TimeEdit::PasteTime() { void TimeEdit::PasteTime() {
@ -236,23 +234,16 @@ void TimeEdit::PasteTime() {
return; return;
} }
if (wxTheClipboard->Open()) { wxString text = GetClipboard();
wxString text; if (!text) return;
if (wxTheClipboard->IsSupported(wxDF_TEXT)) {
wxTextDataObject data;
wxTheClipboard->GetData(data);
text = data.GetText().Trim(false).Trim(true);
}
wxTheClipboard->Close();
AssTime tempTime(text); AssTime tempTime(text);
if (tempTime.GetAssFormated() == text) { if (tempTime.GetAssFormated() == text) {
SetTime(tempTime); SetTime(tempTime);
SetSelection(0, GetValue().size()); SetSelection(0, GetValue().size());
wxCommandEvent evt(wxEVT_COMMAND_TEXT_UPDATED, GetId()); wxCommandEvent evt(wxEVT_COMMAND_TEXT_UPDATED, GetId());
evt.SetEventObject(this); evt.SetEventObject(this);
HandleWindowEvent(evt); HandleWindowEvent(evt);
}
} }
} }

View file

@ -311,6 +311,38 @@ bool ForwardMouseWheelEvent(wxWindow *source, wxMouseEvent &evt) {
return false; return false;
} }
wxString GetClipboard() {
wxString data;
wxClipboard *cb = wxClipboard::Get();
if (cb->Open()) {
if (cb->IsSupported(wxDF_TEXT)) {
wxTextDataObject raw_data;
cb->GetData(raw_data);
data = raw_data.GetText();
}
cb->Close();
}
return data;
}
void SetClipboard(wxString const& new_data) {
wxClipboard *cb = wxClipboard::Get();
if (cb->Open()) {
cb->SetData(new wxTextDataObject(new_data));
cb->Close();
cb->Flush();
}
}
void SetClipboard(wxBitmap const& new_data) {
wxClipboard *cb = wxClipboard::Get();
if (cb->Open()) {
cb->SetData(new wxBitmapDataObject(new_data));
cb->Close();
cb->Flush();
}
}
namespace { namespace {
class cache_cleaner : public wxThread { class cache_cleaner : public wxThread {
wxString directory; wxString directory;

View file

@ -121,6 +121,12 @@ template <typename T> T tabs(T x) { return x < 0 ? -x : x; }
/// @precondition a <= c /// @precondition a <= c
template<typename T> inline T mid(T a, T b, T c) { return std::max(a, std::min(b, c)); } template<typename T> inline T mid(T a, T b, T c) { return std::max(a, std::min(b, c)); }
/// Get the text contents of the clipboard, or empty string on failure
wxString GetClipboard();
/// Try to set the clipboard to the given string
void SetClipboard(wxString const& new_value);
void SetClipboard(wxBitmap const& new_value);
#ifndef FORCEINLINE #ifndef FORCEINLINE
#ifdef __VISUALC__ #ifdef __VISUALC__
#define FORCEINLINE __forceinline #define FORCEINLINE __forceinline