Repository navigation
Expand file tree
/
Copy pathinstance.cpp
More file actions
1827 lines (1560 loc) · 57.8 KB
/
Copy pathinstance.cpp
File metadata and controls
1827 lines (1560 loc) · 57.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* === S Y N F I G ========================================================= */
/*! \file gui/instance.cpp
** \brief writeme
**
** \legal
** Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
** Copyright (c) 2007, 2008 Chris Moore
** Copyright (c) 2008, 2011 Carlos López
** Copyright (c) 2009 Nikita Kitaev
** Copyright (c) 2012 Konstantin Dmitriev
**
** This file is part of Synfig.
**
** Synfig is free software: you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation, either version 2 of the License, or
** (at your option) any later version.
**
** Synfig is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with Synfig. If not, see <https://www.gnu.org/licenses/>.
** \endlegal
*/
/* ========================================================================= */
/* === H E A D E R S ======================================================= */
#ifdef USING_PCH
# include "pch.h"
#else
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <gui/instance.h>
#include <cassert>
#include <giomm.h>
#include <gtkmm/actiongroup.h>
#include <gtkmm/button.h>
#include <gtkmm/image.h>
#include <gtkmm/imagemenuitem.h>
#include <gtkmm/menuitem.h>
#include <gtkmm/separatormenuitem.h>
#include <gtkmm/stock.h>
#include <gui/app.h>
#include <gui/canvasview.h>
#include <gui/docks/dock_toolbox.h>
#include <gui/iconcontroller.h>
#include <gui/localization.h>
#include <gui/onemoment.h>
#include <gui/widgets/widget_waypointmodel.h>
#include <gui/workarea.h>
#include <sigc++/sigc++.h>
#include <synfig/canvasfilenaming.h>
#include <synfig/general.h>
#include <synfig/layers/layer_switch.h>
#include <synfig/savecanvas.h>
#include <synfig/synfig_iterations.h>
#include <synfig/valuenode_registry.h>
#include <synfig/valuenodes/valuenode_composite.h>
#include <synfig/valuenodes/valuenode_duplicate.h>
#include <synfig/widthpoint.h>
#include <synfig/zstreambuf.h>
#include <synfigapp/main.h>
#endif
using namespace synfig;
using namespace studio;
/* === M A C R O S ========================================================= */
/* === G L O B A L S ======================================================= */
int studio::Instance::instance_count_=0;
/* === P R O C E D U R E S ================================================= */
static Gtk::Image*
create_image_from_icon(const std::string& icon_name, Gtk::IconSize icon_size)
{
if (icon_name == "image-missing")
return new Gtk::Image();
#if GTK_CHECK_VERSION(3,24,0)
return new Gtk::Image(icon_name, icon_size);
#else
Gtk::Image* image = new Gtk::Image();
image->set_from_icon_name(icon_name, icon_size);
return image;
#endif
}
static void get_layer_path(const Layer::Handle& l, const Canvas::LooseHandle root_canvas, std::string& path);
struct ValueNodePathBuilder {
const Canvas::LooseHandle& canvas;
std::string path;
std::string link_name;
synfig::TraverseCallbackAction
operator() (ValueNode::Handle vn)
{
if (vn->get_type() != type_canvas)
return TRAVERSE_CALLBACK_SKIP;
if ((*vn)(canvas->get_time()).get(Canvas::Handle()) != canvas)
return TRAVERSE_CALLBACK_SKIP;
if (!link_name.empty())
path += "/" + link_name;
if (const auto constvn = ValueNode_Const::Handle::cast_dynamic(vn))
return TRAVERSE_CALLBACK_ABORT;
if (const auto linkable = LinkableValueNode::Handle::cast_dynamic(vn)) {
path += "/" + linkable->get_name();
for (int i=0; i < linkable->link_count(); i++) {
auto ith_link = linkable->get_link(i);
ValueNodePathBuilder builder {canvas};
builder.link_name = linkable->link_name(i);
traverse_valuenodes(ith_link, [&builder](ValueNode::Handle vn) -> synfig::TraverseCallbackAction {
auto action = builder(vn);
return action;
});
if (!builder.path.empty()) {
path += builder.path;
break;
}
}
return TRAVERSE_CALLBACK_ABORT; // hacked RECURSIVE above;
}
return TRAVERSE_CALLBACK_ABORT;
}
};
static void
get_canvas_path(Canvas::LooseHandle canvas, const Canvas::LooseHandle root_canvas, std::string& path)
{
if (canvas->is_inline()) {
if (auto layer = canvas->find_first_parent_of_type<Layer>()) {
for (const auto& param_item : layer->get_param_list()) {
if (param_item.second.get_type() == type_canvas) {
if (param_item.second.get(Canvas::LooseHandle()) == canvas) {
std::string dynamic_str;
auto dynamic_param_iter = layer->dynamic_param_list().find(param_item.first);
if (dynamic_param_iter != layer->dynamic_param_list().end()) {
ValueNodePathBuilder vn_path_builder {canvas};
traverse_valuenodes(dynamic_param_iter->second, [&vn_path_builder](ValueNode::Handle vn) -> TraverseCallbackAction {
return vn_path_builder(vn);
});
dynamic_str = vn_path_builder.path;
}
path = strprintf("/param[@name='%s']%s/canvas%s", param_item.first.c_str(), dynamic_str.c_str(), path.c_str());
}
}
}
get_layer_path(layer, root_canvas, path);
}
} else {
path = canvas->get_relative_id(root_canvas) + path;
}
}
static void
get_layer_path(const Layer::Handle& l, const Canvas::LooseHandle root_canvas, std::string& path)
{
if (l) {
auto canvas = l->get_canvas();
if (!canvas) {
warning("layer without canvas!");
} else {
path = strprintf("/layer[%i]", canvas->size() - canvas->get_depth(l)) + path;
get_canvas_path(canvas, root_canvas, path);
}
}
}
/* === M E T H O D S ======================================================= */
Instance::Instance(synfig::Canvas::Handle canvas, synfig::FileSystem::Handle container):
synfigapp::Instance (canvas, container),
canvas_tree_store_ (Gtk::TreeStore::create(canvas_tree_model)),
history_tree_store_ (HistoryTreeStore::create(this)),
undo_status_(false),
redo_status_(false)
{
id_=instance_count_++;
set_clear_redo_stack_on_new_action(true);
// Connect up all the signals
signal_filename_changed().connect(sigc::mem_fun(*this,&studio::Instance::update_all_titles));
signal_unsaved_status_changed().connect(sigc::hide(sigc::mem_fun(*this,&studio::Instance::update_all_titles)));
signal_undo_status().connect(sigc::mem_fun(*this,&studio::Instance::set_undo_status));
signal_redo_status().connect(sigc::mem_fun(*this,&studio::Instance::set_redo_status));
refresh_canvas_tree();
}
Instance::~Instance()
{
}
// this function returns true if the given extension belongs to image layer type
static bool
is_img(const synfig::String& filename)
{
static const std::set<String> img_ext{".jpg",".jpeg",".png",".bmp",".gif",".tiff",".tif",".dib",".ppm",".pbm",".pgm",".pnm",".webp"};
return img_ext.find(Glib::ustring(filesystem::Path::filename_extension(filename)).lowercase()) != img_ext.end();
}
int
Instance::get_visible_canvases()const
{
int count(0);
CanvasViewList::const_iterator iter;
for(iter=canvas_view_list_.begin();iter!=canvas_view_list_.end();++iter)
if((*iter)->get_visible())
count++;
return count;
}
etl::handle<Instance>
Instance::create(synfig::Canvas::Handle canvas, synfig::FileSystem::Handle container)
{
// Construct a new instance
etl::handle<Instance> instance(new Instance(canvas, container));
// Set the user preference regarding redo-history behavior
{
bool active = synfigapp::Main::settings().get_value("pref.clear_redo_stack_on_new_action", true);
instance->set_clear_redo_stack_on_new_action(active);
}
// Add the new instance to the application's instance list
App::instance_list.push_back(instance);
// Set up the instance with the default UI manager
instance->synfigapp::Instance::set_ui_interface(App::get_ui_interface());
// Signal the new instance
App::signal_instance_created()(instance);
// And then make sure that is has been selected
App::set_selected_instance(instance);
// Create the initial window for the root canvas
instance->focus(canvas);
return instance;
}
CanvasView::Handle
Instance::find_canvas_view(Canvas::Handle canvas)
{
if(!canvas)
return 0;
while(canvas->is_inline())
canvas=canvas->parent();
CanvasViewList::iterator iter;
for (iter = canvas_view_list().begin(); iter != canvas_view_list().end(); ++iter)
if((*iter)->get_canvas()==canvas)
return *iter;
return CanvasView::create(this,canvas);
}
void
Instance::focus(Canvas::Handle canvas)
{
CanvasView::Handle canvas_view = find_canvas_view(canvas);
assert(canvas_view);
canvas_view->present();
}
void
Instance::set_undo_status(bool x)
{
undo_status_=x;
App::dock_toolbox->refresh();
signal_undo_redo_status_changed()();
}
void
Instance::set_redo_status(bool x)
{
redo_status_=x;
App::dock_toolbox->refresh();
signal_undo_redo_status_changed()();
}
void
studio::Instance::run_plugin(std::string plugin_id, bool modify_canvas, std::vector<std::string> extra_args)
{
etl::handle<synfigapp::UIInterface> uim = this->find_canvas_view(this->get_canvas())->get_ui_interface();
std::unordered_map<std::string,std::string> view_state;
const auto required_args = App::plugin_manager.get_script_args(plugin_id);
if (auto canvas_interface = find_canvas_interface(get_canvas())) {
// Current Time
view_state["current_time"] = canvas_interface->get_time().get_string(synfig::Time::FORMAT_VIDEO);
// Selected Layers
if (auto selection = canvas_interface->get_selection_manager()) {
// Layers
if (required_args.selected_layers != PluginScript::ArgNecessity::ARGUMENT_UNUSED) {
for (const auto& layer : selection->get_selected_layers()) {
std::string xpath;
get_layer_path(layer, get_canvas(), xpath);
auto& selected_layers = view_state["selected_layers"];
if (!selected_layers.empty())
selected_layers.append(",");
selected_layers += '"' + JSON::escape_string(xpath) + '"';
}
}
}
}
if (required_args.selected_layers == PluginScript::ArgNecessity::ARGUMENT_MANDATORY && view_state.count("selected_layers") == 0) {
App::dialog_message_1b(
"ERROR",
_("The plugin operation has failed."),
_("This plugin requires layer(s) to be selected.\nSelect one or more layers and re-run the plugin."),
_("Close"));
return;
}
if ( modify_canvas )
{
String message = strprintf(_("Do you really want to run plugin for file \"%s\"?" ),
this->get_canvas()->get_name().c_str());
String details = _("This operation cannot be undone and all undo history will be cleared.");
int answer = uim->confirmation(
message,
details,
_("Proceed"),
_("Cancel"),
synfigapp::UIInterface::RESPONSE_OK);
if(answer != synfigapp::UIInterface::RESPONSE_OK)
return;
}
OneMoment one_moment;
Canvas::Handle canvas(this->get_canvas());
backup(true);
FileSystemTemporary::Handle temporary_filesystem = FileSystemTemporary::Handle::cast_dynamic(get_canvas()->get_file_system());
filesystem::Path tmp_filename = temporary_filesystem->get_temporary_directory() / temporary_filesystem->get_temporary_filename_base();
Time cur_time;
cur_time = canvas->get_time();
// Generate temporary file name
filesystem::Path canvas_file = filesystem::Path(get_canvas()->get_file_name());
filesystem::Path filename_dir = canvas_file.parent_path();
if (!filename_dir.is_absolute())
filename_dir = temporary_filesystem->get_temporary_directory();
auto temp_lock = FileSystemTemporary::reserve_temporary_filename(filename_dir, canvas_file.filename().u8string(), ".sif");
if (!temp_lock.second) {
App::dialog_message_1b("ERROR", _("The plugin operation has failed."), _("Couldn't create a temporary file"), _("Close"));
return;
}
filesystem::Path filename_processed = temp_lock.first;
if ( modify_canvas )
close(false);
if (canvas->use_count() != 1 && modify_canvas) {
one_moment.hide();
App::dialog_message_1b(
"ERROR",
_("The plugin operation has failed."),
_("This can be due to current file "
"being referenced by another composition that is already open, "
"or because of an internal error in Synfig Studio. Try closing "
"any compositions that might reference this file and try again, "
"or restart Synfig Studio."),
_("Close"));
} else {
// Save file copy
String filename_ext = canvas_file.extension().u8string();
if ( filename_ext.empty() || ( filename_ext != ".sif" && filename_ext != ".sifz") )
filename_ext = ".sifz";
FileSystem::ReadStream::Handle stream_in = temporary_filesystem->get_read_stream("#project"+filename_ext);
if (!stream_in)
{
synfig::error(strprintf("run_plugin(): Unable to open file for reading - %s", temporary_filesystem->get_real_uri("#project"+filename_ext).c_str()));
FileSystemTemporary::Identifier identifier(temporary_filesystem, filename_processed.u8string());
if ( !save_canvas(identifier, get_canvas(), true) )
{
App::dialog_message_1b(
"ERROR",
_("The plugin operation has failed."),
_("Could not read composition file."),
_("Close"));
return;
}
temporary_filesystem->save_changes();
}
else
{
if (filename_ext == ".sifz")
stream_in = new ZReadStream(stream_in, synfig::zstreambuf::gzip);
FileSystem::WriteStream::Handle outfile = FileSystemNative::instance()->get_write_stream(filename_processed.u8string());
*outfile << stream_in->rdbuf();
outfile.reset();
stream_in.reset();
}
one_moment.hide();
extra_args.insert(extra_args.begin(), filename_processed.u8string());
bool result = App::plugin_manager.run(plugin_id, extra_args, view_state);
if (result && modify_canvas){
// Restore file copy
FileSystem::WriteStream::Handle stream = temporary_filesystem->get_write_stream("#project"+filename_ext);
if (stream && filename_ext == ".sifz")
stream = FileSystem::WriteStream::Handle(new ZWriteStream(stream));
if (!stream)
{
synfig::error("run_plugin(): Unable to open file for write");
} else {
FileSystem::ReadStream::Handle infile = FileSystemNative::instance()->get_read_stream(filename_processed.u8string());
*stream << infile->rdbuf();
infile.reset();
stream.reset();
}
}
FileSystemNative::instance()->file_remove(filename_processed.u8string());
}
canvas=0;
if ( modify_canvas ) {
one_moment.show();
bool ok = App::open_from_temporary_filesystem(tmp_filename);
if (!ok) {
synfig::error("run_plugin(): Cannot reopen file");
return;
}
etl::handle<Instance> new_instance = App::instance_list.back();
if (!new_instance) {
synfig::error("run_plugin(): Cannot retrieve new instance");
} else {
// Restore time cursor position
canvas = new_instance->get_canvas();
etl::handle<synfigapp::CanvasInterface> new_canvas_interface(new_instance->find_canvas_interface(canvas));
if (!new_canvas_interface) {
synfig::error("run_plugin(): Cannot retrieve canvas interface");
} else {
new_canvas_interface->set_time(cur_time);
}
}
}
}
bool
studio::Instance::save_as(const synfig::String &file_name)
{
if(synfigapp::Instance::save_as(file_name))
{
// after changing the filename, update the render settings with the new filename
for (const auto& canvas_view : canvas_view_list())
canvas_view->render_settings.set_entry_filename();
App::add_recent_file(etl::handle<Instance>(this));
// check for unsaved layers (external bitmaps, etc)
std::vector<Layer::Handle> layers;
find_unsaved_layers(layers);
if (!layers.empty())
{
String message = _("Current canvas saved, but unable to save data for following layers:");
String details;
for(std::vector<Layer::Handle>::const_iterator i = layers.begin(); i != layers.end(); ++i)
{
details.append((*i)->get_description());
if ( Layer_Bitmap::Handle::cast_dynamic(*i)
&& (*i)->get_param_list().count("filename") )
{
ValueBase value = (*i)->get_param("filename");
if (value.same_type_as(String()))
details.append(" (" + value.get(String()) + ")");
}
details.append("\n");
}
App::dialog_message_1b("WARNING", message, "details", _("Close"), details);
}
return true;
}
return false;
}
void
studio::Instance::open()
{
App::dialog_open(get_file_name());
}
Instance::Status
studio::Instance::save()
{
// if we don't have a real filename yet then we need to ask where to save it
if (!has_real_filename())
{
if (dialog_save_as())
return Status::STATUS_OK;
else
return Status::STATUS_CANCEL;
}
if (!save_as(get_canvas()->get_file_name()))
{
std::string msg(strprintf(_("Unable to save to '%s'"), get_file_name().c_str()));
App::dialog_message_1b(
"ERROR",
msg.c_str(),
"details",
_("Close"));
return Status::STATUS_ERROR;
}
return Status::STATUS_OK;
}
// the filename will be set to "Synfig Animation 1" or some such when first created
// and will be changed to an absolute path once it has been saved
// so if it still begins with "Synfig Animation " then we don't have a real filename yet
bool
studio::Instance::has_real_filename()
{
return get_file_name().find(App::custom_filename_prefix.c_str()) != 0;
}
bool
studio::Instance::dialog_save_as()
{
filesystem::Path filename = get_file_name();
Canvas::Handle canvas(get_canvas());
{
OneMoment one_moment;
bool problematic = false;
canvas->foreach_parent([=, &problematic](Node* node)
{
for(;node->parent_count();node=node->get_first_parent())
{
Layer::Handle parent_layer(dynamic_cast<Layer*>(node));
if(parent_layer && parent_layer->get_canvas()->get_root()!=get_canvas())
{
//! \todo Fix big involving "Save As" with referenced compositions
std::string msg(strprintf(_("There is currently a bug when using \"SaveAs\"\n"
"on a composition that is being referenced by other\n"
"files that are currently open. Close these\n"
"other files first before trying to use \"SaveAs\".")));
App::dialog_message_1b(
"ERROR",
msg.c_str(),
"details",
_("Close"));
problematic = true;
return true;
}
if(parent_layer)
break;
}
return false;
});
if (problematic)
return false;
}
if (has_real_filename())
filename = filesystem::absolute(filename);
// show the canvas' name if it has one, else its ID
while (App::dialog_save_file((_("Please choose a file name") +
String(" (") +
(canvas->get_name().empty() ? canvas->get_id() : canvas->get_name()) +
")"),
filename, ANIMATION_DIR_PREFERENCE))
{
// If the filename still has wildcards, then we should
// continue looking for the file we want
std::string base_filename = filename.filename().u8string();
if (base_filename.find('*') != std::string::npos)
continue;
// if file extension is not recognized, then forced to .sifz
if (filename.extension().empty())
filename.concat(".sifz");
canvas->set_name(base_filename);
// forced to .sifz, the below code is not need anymore
try
{
String ext(filename.extension().u8string());
// todo: ".sfg" literal and others
if (ext != ".sif" && ext != ".sifz" && ext != ".sfg" && !App::dialog_message_2b(
_("Unknown extension"),
_("You have given the file name an extension which I do not recognize. "
"Are you sure this is what you want?"),
Gtk::MESSAGE_QUESTION,
_("Cancel"),
_("Sure"))
)
continue;
}
catch(...)
{
continue;
}
bool file_exists = FileSystemNative::instance()->is_file(filename.u8string());
if (!file_exists) {
if (FileSystemNative::instance()->is_exists(filename.u8string())) {
std::string msg(strprintf(_("There is a folder with the same name '%s'.\nPlease choose another name"), filename.u8_str()));
App::dialog_message_1b("ERROR", msg.c_str(), "details", _("Close"));
continue;
}
} else {
// If the file exists and the user doesn't want to overwrite it, keep prompting for a filename
std::string message = strprintf(_("A file named \"%s\" already exists. "
"Do you want to replace it?"),
filename.filename().u8_str());
std::string details = strprintf(_("The file already exists in \"%s\". "
"Replacing it will overwrite its contents."),
filename.parent_path().u8_str());
if (!App::dialog_message_2b(message, details, Gtk::MESSAGE_QUESTION, _("Use Another Name…"), _("Replace")))
continue;
}
if (save_as(filename.u8string()))
{
synfig::set_file_version(synfig::RELEASE_VERSION_CURRENT);
return true;
}
std::string msg(strprintf(_("Unable to save to '%s'"), filename.u8_str()));
App::dialog_message_1b("ERROR", msg.c_str(), "details", _("Close"));
}
return false;
}
bool
studio::Instance::dialog_export()
{
filesystem::Path filename = get_file_name();
Canvas::Handle canvas(get_canvas());
if (has_real_filename())
filename = filesystem::absolute(filename);
// show the canvas' name if it has one, else its ID
std::string plugin_id = App::dialog_export_file(
(_("Please choose a file name") +
String(" (") +
(canvas->get_name().empty() ? canvas->get_id() : canvas->get_name()) +
")"),
filename, ANIMATION_DIR_PREFERENCE
);
if ( !plugin_id.empty() )
{
run_plugin(plugin_id, false, {filename.u8string()});
return true;
}
return false;
}
void
Instance::update_all_titles()
{
for (const auto& canvas_view : canvas_view_list())
canvas_view->update_title();
}
void
Instance::close(bool remove_temporary_files)
{
// This will increase the reference count so we don't get DELETED
// until we are ready
etl::handle<Instance> me(this);
/*
We need to hide some panels when instance is closed.
This is done to avoid the crash when two conditions met:
1) the list is scrolled down
2) user closes file
*/
CanvasView::Handle canvas_view = find_canvas_view(get_canvas());
Gtk::Widget* tree_view_keyframes = canvas_view->get_ext_widget("keyframes");
tree_view_keyframes->hide();
Gtk::Widget* tree_view_params = canvas_view->get_ext_widget("params");
tree_view_params->hide();
Gtk::Widget* tree_view_children = canvas_view->get_ext_widget("children");
tree_view_children->hide();
// Make sure we aren't selected as the current instance
if(studio::App::get_selected_instance()==this)
studio::App::set_selected_instance(0);
// Remove us from the active instance list
std::list<etl::handle<studio::Instance> >::iterator iter;
for (iter = studio::App::instance_list.begin(); iter != studio::App::instance_list.end(); ++iter)
if(*iter==this)
break;
assert(iter!=studio::App::instance_list.end());
if(iter!=studio::App::instance_list.end())
studio::App::instance_list.erase(iter);
// Send out a signal that we are being deleted
studio::App::signal_instance_deleted()(this);
// Hide all of the canvas views
for (const auto& canvas_view : canvas_view_list())
canvas_view->hide();
// Consume pending events before deleting the canvas views
App::process_all_events();
// Delete all of the canvas views
canvas_view_list().clear();
// If there is another open instance to select,
// go ahead and do so. If not, never mind.
if(studio::App::instance_list.empty())
{
studio::App::set_selected_canvas_view(0);
studio::App::set_selected_instance(0);
}
else if(studio::App::instance_list.size() == 1)
{
studio::App::instance_list.front()->canvas_view_list().front()->present();
}
if (!remove_temporary_files) {
FileSystemTemporary::Handle temporary_filesystem = FileSystemTemporary::Handle::cast_dynamic(get_canvas()->get_file_system());
temporary_filesystem->set_keep_files_when_destroyed(true);
}
}
void
Instance::insert_canvas(Gtk::TreeRow row, synfig::Canvas::Handle canvas)
{
CanvasTreeModel canvas_tree_model;
assert(canvas);
row[canvas_tree_model.icon_name] = "canvas_icon";
row[canvas_tree_model.id] = canvas->get_id();
row[canvas_tree_model.name] = canvas->get_name();
if(canvas->is_root())
row[canvas_tree_model.label] = filesystem::Path::basename(canvas->get_file_name());
else
if(!canvas->get_id().empty())
row[canvas_tree_model.label] = canvas->get_id();
else
if(!canvas->get_name().empty())
row[canvas_tree_model.label] = canvas->get_name();
else
row[canvas_tree_model.label] = _("[Unnamed]");
row[canvas_tree_model.canvas] = canvas;
row[canvas_tree_model.is_canvas] = true;
row[canvas_tree_model.is_value_node] = false;
{
synfig::Canvas::Children::iterator iter;
synfig::Canvas::Children &children(canvas->children());
for (iter = children.begin(); iter != children.end(); ++iter)
insert_canvas(*(canvas_tree_store()->append(row.children())),*iter);
}
}
void
Instance::refresh_canvas_tree()
{
canvas_tree_store()->clear();
Gtk::TreeRow row = *(canvas_tree_store()->prepend());
insert_canvas(row,get_canvas());
}
void
Instance::revert()
{
OneMoment one_moment;
filesystem::Path filename(get_file_name());
Canvas::Handle canvas(get_canvas());
close();
if (canvas->use_count() != 1) {
one_moment.hide();
App::dialog_message_1b(
"ERROR",
_("The revert operation has failed."),
_("This can be due to it being referenced by another composition"
" that is already open, or because of an internal error "
"in Synfig Studio. Try closing any compositions that "
"might reference this composition and try again, or "
"restart Synfig Studio."),
_("Close"));
one_moment.show();
}
canvas=0;
App::open(filename);
}
bool
Instance::safe_revert()
{
if(synfigapp::Instance::get_action_count())
{
if (!App::dialog_message_2b(
_("Revert to saved"),
_("You will lose any changes you have made since your last save. "
"Are you sure?"),
Gtk::MESSAGE_QUESTION,
_("Cancel"),
_("Revert"))
)
return false;
}
revert();
return true;
}
bool
Instance::safe_close()
{
CanvasView::Handle canvas_view = find_canvas_view(get_canvas());
etl::handle<synfigapp::UIInterface> uim = canvas_view->get_ui_interface();
// if the animation is currently playing, closing the window will cause a crash,
// so don't allow it
if (canvas_view->is_playing())
{
canvas_view->present();
App::dialog_message_1b(
"ERROR",
_("The animation is currently playing so the window cannot be closed."),
"details",
_("Thanks!"));
return false;
}
if(get_action_count())
do
{
std::string message = strprintf(_("Save changes to document \"%s\" before closing?"),
filesystem::Path::basename(get_file_name()).c_str() );
std::string details = (_("If you don't save, changes from the last time you saved "
"will be permanently lost."));
int answer=uim->yes_no_cancel(
message,
details,
_("Close without Saving"),
_("Cancel"),
_("Save"),
true,
synfigapp::UIInterface::RESPONSE_YES
);
if(answer == synfigapp::UIInterface::RESPONSE_YES){
Status status = save();
if (status == Status::STATUS_OK)
break;
else if (status == Status::STATUS_CANCEL)
return false;
}
if(answer==synfigapp::UIInterface::RESPONSE_NO)
break;
if(answer==synfigapp::UIInterface::RESPONSE_CANCEL)
return false;
} while (true);
close();
return true;
}
void
Instance::add_actions_to_group(const Glib::RefPtr<Gtk::ActionGroup>& action_group, synfig::String& ui_info, const synfigapp::Action::ParamList ¶m_list, synfigapp::Action::Category category)const
{
synfigapp::Action::CandidateList candidate_list;
candidate_list = synfigapp::Action::compile_visible_candidate_list(param_list, category);
candidate_list.sort();
for (const auto& action : candidate_list) {
const std::string icon_name(get_action_icon_name(action));
action_group->add(Gtk::Action::create_with_icon_name(
"action-" + action.name,
icon_name,
action.local_name,
action.local_name
),
sigc::bind(
sigc::bind(
sigc::mem_fun(
*const_cast<studio::Instance*>(this),
&studio::Instance::process_action
),
param_list
),
action.name
)
);
ui_info+=strprintf("<menuitem action='action-%s' />", action.name.c_str());
}
}
void
Instance::add_actions_to_menu(Gtk::Menu *menu, const synfigapp::Action::ParamList ¶m_list,synfigapp::Action::Category category)const
{
synfigapp::Action::CandidateList candidate_list;
candidate_list = synfigapp::Action::compile_visible_candidate_list(param_list, category);
candidate_list.sort();
if(candidate_list.empty())
synfig::warning("%s:%d Action CandidateList is empty!", __FILE__, __LINE__);
for (const auto& action : candidate_list) {
bool is_item_already_in_menu = false;
std::vector<Gtk::Widget*> children = menu->get_children();
for (const Gtk::Widget* child : children) {
if (const Gtk::MenuItem* menu_item = dynamic_cast<const Gtk::MenuItem*>(child)) {
if (menu_item->get_label() == action.local_name) {
is_item_already_in_menu = true;
break;
}
}
}
// Maybe the action supports multiple ducks or layers and is already listed in menu
if (is_item_already_in_menu)
continue;
Gtk::MenuItem* item = Gtk::manage(new Gtk::ImageMenuItem(
*Gtk::manage(create_image_from_icon(get_action_icon_name(action), Gtk::ICON_SIZE_MENU)),
action.local_name ));
item->signal_activate().connect(
sigc::bind(
sigc::bind(
sigc::mem_fun(
*const_cast<studio::Instance*>(this),
&studio::Instance::process_action ),