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)
{
if (wxTheClipboard->Open()) {
if (wxTheClipboard->IsSupported(wxDF_TEXT)) {
wxTextDataObject rawdata;
wxTheClipboard->GetData(rawdata);
lua_pushstring(L, rawdata.GetText().utf8_str());
}
else
wxString data = GetClipboard();
if (!data)
lua_pushnil(L);
wxTheClipboard->Close();
}
else
lua_pushnil(L);
lua_pushstring(L, data.utf8_str());
return 1;
}

View file

@ -61,6 +61,7 @@
#include "../subs_edit_ctrl.h"
#include "../subs_grid.h"
#include "../text_selection_controller.h"
#include "../utils.h"
#include "../video_context.h"
namespace {
@ -83,15 +84,7 @@ struct validate_sel_multiple : public Command {
};
void paste_lines(agi::Context *c, bool paste_over) {
wxString data;
if (wxTheClipboard->Open()) {
if (wxTheClipboard->IsSupported(wxDF_TEXT)) {
wxTextDataObject rawdata;
wxTheClipboard->GetData(rawdata);
data = rawdata.GetText();
}
wxTheClipboard->Close();
}
wxString data = GetClipboard();
if (!data) return;
AssDialogue *rel_line = c->selectionController->GetActiveLine();
@ -505,10 +498,7 @@ static void copy_lines(agi::Context *c) {
}
}
if (wxTheClipboard->Open()) {
wxTheClipboard->SetData(new wxTextDataObject(data));
wxTheClipboard->Close();
}
SetClipboard(data);
}
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)
bool Validate(const agi::Context *) {
bool can_paste = false;
if (wxTheClipboard->Open()) {
bool can_paste = wxTheClipboard->IsSupported(wxDF_TEXT);
can_paste = wxTheClipboard->IsSupported(wxDF_TEXT);
wxTheClipboard->Close();
return can_paste;
}
return false;
return can_paste;
}
void operator()(agi::Context *c) {
@ -808,12 +798,12 @@ struct edit_line_paste_over : public Command {
CMD_TYPE(COMMAND_VALIDATE)
bool Validate(const agi::Context *c) {
if (wxTheClipboard->Open()) {
bool can_paste = wxTheClipboard->IsSupported(wxDF_TEXT);
bool can_paste = !c->selectionController->GetSelectedSet().empty();
if (can_paste && wxTheClipboard->Open()) {
can_paste = wxTheClipboard->IsSupported(wxDF_TEXT);
wxTheClipboard->Close();
return can_paste && c->selectionController->GetSelectedSet().size();
}
return false;
return can_paste;
}
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")
void operator()(agi::Context *c) {
if (wxTheClipboard->Open()) {
wxTheClipboard->SetData(new wxTextDataObject(c->videoDisplay->GetMousePosition().Str()));
wxTheClipboard->Close();
}
SetClipboard(c->videoDisplay->GetMousePosition().Str());
}
};
@ -308,10 +305,7 @@ struct video_frame_copy : public validator_video_loaded {
STR_HELP("Copy the currently displayed frame to the clipboard")
void operator()(agi::Context *c) {
if (wxTheClipboard->Open()) {
wxTheClipboard->SetData(new wxBitmapDataObject(wxBitmap(c->videoController->GetFrame(c->videoController->GetFrameN())->GetImage(),24)));
wxTheClipboard->Close();
}
SetClipboard(wxBitmap(c->videoController->GetFrame(c->videoController->GetFrameN())->GetImage(), 24));
}
};
@ -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")
void operator()(agi::Context *c) {
if (wxTheClipboard->Open()) {
wxTheClipboard->SetData(new wxBitmapDataObject(wxBitmap(c->videoController->GetFrame(c->videoController->GetFrameN(), true)->GetImage(),24)));
wxTheClipboard->Close();
}
SetClipboard(wxBitmap(c->videoController->GetFrame(c->videoController->GetFrameN(), true)->GetImage(), 24));
}
};

View file

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

View file

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

View file

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

View file

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

View file

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

View file

@ -311,6 +311,38 @@ bool ForwardMouseWheelEvent(wxWindow *source, wxMouseEvent &evt) {
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 {
class cache_cleaner : public wxThread {
wxString directory;

View file

@ -121,6 +121,12 @@ template <typename T> T tabs(T x) { return x < 0 ? -x : x; }
/// @precondition a <= 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
#ifdef __VISUALC__
#define FORCEINLINE __forceinline