diff --git a/aegisub/src/auto4_lua.cpp b/aegisub/src/auto4_lua.cpp index 27877633b..896abbafc 100644 --- a/aegisub/src/auto4_lua.cpp +++ b/aegisub/src/auto4_lua.cpp @@ -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 - lua_pushnil(L); - wxTheClipboard->Close(); - } - else + wxString data = GetClipboard(); + if (!data) lua_pushnil(L); + else + lua_pushstring(L, data.utf8_str()); return 1; } diff --git a/aegisub/src/command/edit.cpp b/aegisub/src/command/edit.cpp index 7108b668a..e23f6ae15 100644 --- a/aegisub/src/command/edit.cpp +++ b/aegisub/src/command/edit.cpp @@ -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) { diff --git a/aegisub/src/command/video.cpp b/aegisub/src/command/video.cpp index 10c93c212..fd34d8ef1 100644 --- a/aegisub/src/command/video.cpp +++ b/aegisub/src/command/video.cpp @@ -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)); } }; diff --git a/aegisub/src/dialog_style_manager.cpp b/aegisub/src/dialog_style_manager.cpp index 1f564839b..2b5aa39ac 100644 --- a/aegisub/src/dialog_style_manager.cpp +++ b/aegisub/src/dialog_style_manager.cpp @@ -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 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() { diff --git a/aegisub/src/main.cpp b/aegisub/src/main.cpp index 43b42503a..1062f7f28 100644 --- a/aegisub/src/main.cpp +++ b/aegisub/src/main.cpp @@ -295,11 +295,6 @@ int AegisubApp::OnExit() { if (frame) delete frame; - if (wxTheClipboard->Open()) { - wxTheClipboard->Flush(); - wxTheClipboard->Close(); - } - SubtitleFormat::DestroyFormats(); VideoContext::OnExit(); delete plugins; diff --git a/aegisub/src/subs_edit_box.cpp b/aegisub/src/subs_edit_box.cpp index da4d01263..bb9e431cd 100644 --- a/aegisub/src/subs_edit_box.cpp +++ b/aegisub/src/subs_edit_box.cpp @@ -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(""); } } diff --git a/aegisub/src/subs_edit_ctrl.cpp b/aegisub/src/subs_edit_ctrl.cpp index db5ed1882..b43b0d444 100644 --- a/aegisub/src/subs_edit_ctrl.cpp +++ b/aegisub/src/subs_edit_ctrl.cpp @@ -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"); diff --git a/aegisub/src/timeedit_ctrl.cpp b/aegisub/src/timeedit_ctrl.cpp index 78d9d894b..312a5adb3 100644 --- a/aegisub/src/timeedit_ctrl.cpp +++ b/aegisub/src/timeedit_ctrl.cpp @@ -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,23 +234,16 @@ 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) { - SetTime(tempTime); - SetSelection(0, GetValue().size()); + AssTime tempTime(text); + if (tempTime.GetAssFormated() == text) { + SetTime(tempTime); + SetSelection(0, GetValue().size()); - wxCommandEvent evt(wxEVT_COMMAND_TEXT_UPDATED, GetId()); - evt.SetEventObject(this); - HandleWindowEvent(evt); - } + wxCommandEvent evt(wxEVT_COMMAND_TEXT_UPDATED, GetId()); + evt.SetEventObject(this); + HandleWindowEvent(evt); } } diff --git a/aegisub/src/utils.cpp b/aegisub/src/utils.cpp index 466c6ee87..5eb90888b 100644 --- a/aegisub/src/utils.cpp +++ b/aegisub/src/utils.cpp @@ -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; diff --git a/aegisub/src/utils.h b/aegisub/src/utils.h index cb4daf760..593938750 100644 --- a/aegisub/src/utils.h +++ b/aegisub/src/utils.h @@ -121,6 +121,12 @@ template T tabs(T x) { return x < 0 ? -x : x; } /// @precondition a <= c template 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