@@ -127,7 +127,8 @@ namespace {
127127
128128 const char *clipboard_get ()
129129 {
130- std::string data = GetClipboard ();
130+ std::string data;
131+ agi::dispatch::Main ().Sync ([&] { data = GetClipboard (); });
131132 if (data.empty ())
132133 return nullptr ;
133134 return strndup (data);
@@ -137,18 +138,14 @@ namespace {
137138 {
138139 bool succeeded = false ;
139140
140- #if wxUSE_OLE
141- // OLE needs to be initialized on each thread that wants to write to
142- // the clipboard, which wx does not handle automatically
143- wxClipboard cb;
144- #else
145- wxClipboard &cb = *wxTheClipboard;
146- #endif
147- if (cb.Open ()) {
148- succeeded = cb.SetData (new wxTextDataObject (wxString::FromUTF8 (str)));
149- cb.Close ();
150- cb.Flush ();
151- }
141+ agi::dispatch::Main ().Sync ([&] {
142+ wxClipboard &cb = *wxTheClipboard;
143+ if (cb.Open ()) {
144+ succeeded = cb.SetData (new wxTextDataObject (wxString::FromUTF8 (str)));
145+ cb.Close ();
146+ cb.Flush ();
147+ }
148+ });
152149
153150 return succeeded;
154151 }
@@ -785,27 +782,33 @@ namespace {
785782 {
786783 bool failed = false ;
787784 BackgroundScriptRunner bsr (parent, title);
788- bsr.Run ([&](ProgressSink *ps) {
789- LuaProgressSink lps (L, ps, can_open_config);
790-
791- // Insert our error handler under the function to call
792- lua_pushcclosure (L, add_stack_trace, 0 );
793- lua_insert (L, -nargs - 2 );
794-
795- if (lua_pcall (L, nargs, nresults, -nargs - 2 )) {
796- if (!lua_isnil (L, -1 )) {
797- // if the call failed, log the error here
798- ps->Log (" \n\n Lua reported a runtime error:\n " );
799- ps->Log (get_string_or_default (L, -1 ));
785+ try {
786+ bsr.Run ([&](ProgressSink *ps) {
787+ LuaProgressSink lps (L, ps, can_open_config);
788+
789+ // Insert our error handler under the function to call
790+ lua_pushcclosure (L, add_stack_trace, 0 );
791+ lua_insert (L, -nargs - 2 );
792+
793+ if (lua_pcall (L, nargs, nresults, -nargs - 2 )) {
794+ if (!lua_isnil (L, -1 )) {
795+ // if the call failed, log the error here
796+ ps->Log (" \n\n Lua reported a runtime error:\n " );
797+ ps->Log (get_string_or_default (L, -1 ));
798+ }
799+ lua_pop (L, 2 );
800+ failed = true ;
800801 }
801- lua_pop (L, 2 );
802- failed = true ;
803- }
804- else
805- lua_remove (L, -nresults - 1 );
802+ else
803+ lua_remove (L, -nresults - 1 );
806804
807- lua_gc (L, LUA_GCCOLLECT , 0 );
808- });
805+ lua_gc (L, LUA_GCCOLLECT , 0 );
806+ });
807+ } catch (agi::UserCancelException const &) {
808+ if (!failed)
809+ lua_pop (L, 2 );
810+ throw ;
811+ }
809812 if (failed)
810813 throw agi::UserCancelException (" Script threw an error" );
811814 }
0 commit comments