Removed a bunch more DEBUGPOINT()s.
[synfig.git] / synfig-studio / trunk / src / gtkmm / instance.cpp
index 58ef103..bb37c17 100644 (file)
@@ -6,6 +6,7 @@
 **
 **     \legal
 **     Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
+**     Copyright (c) 2007 Chris Moore
 **
 **     This package is free software; you can redistribute it and/or
 **     modify it under the terms of the GNU General Public License as
 #include <sigc++/retype.h>
 //#include <sigc++/hide.h>
 #include <synfig/valuenode_composite.h>
+#include <synfig/valuenode_duplicate.h>
 #include "widget_waypointmodel.h"
 #include <gtkmm/actiongroup.h>
-#include "iconcontroler.h"
+#include "iconcontroller.h"
 #include <sys/stat.h>
 #include <errno.h>
 
+#include "general.h"
+
 #endif
 
 using namespace std;
 using namespace etl;
 using namespace synfig;
 using namespace studio;
-using namespace SigC;
+using namespace sigc;
 
 /* === M A C R O S ========================================================= */
 
@@ -71,7 +75,7 @@ int studio::Instance::instance_count_=0;
 
 /* === M E T H O D S ======================================================= */
 
-Instance::Instance(Canvas::Handle canvas):
+Instance::Instance(synfig::Canvas::Handle canvas):
        synfigapp::Instance             (canvas),
 //     canvas_tree_store_              (Gtk::TreeStore::create(CanvasTreeModel())),
 //     canvas_tree_store_              (Gtk::TreeStore::create()),
@@ -119,7 +123,7 @@ Instance::get_visible_canvases()const
 }
 
 handle<Instance>
-Instance::create(Canvas::Handle canvas)
+Instance::create(synfig::Canvas::Handle canvas)
 {
        // Construct a new instance
        handle<Instance> instance(new Instance(canvas));
@@ -143,7 +147,7 @@ Instance::create(Canvas::Handle canvas)
 }
 
 handle<CanvasView>
-Instance::find_canvas_view(Canvas::Handle canvas)
+Instance::find_canvas_view(etl::handle<synfig::Canvas> canvas)
 {
        if(!canvas)
                return 0;
@@ -161,7 +165,7 @@ Instance::find_canvas_view(Canvas::Handle canvas)
 }
 
 void
-Instance::focus(Canvas::Handle canvas)
+Instance::focus(etl::handle<synfig::Canvas> canvas)
 {
        handle<CanvasView> canvas_view=find_canvas_view(canvas);
        assert(canvas_view);
@@ -185,45 +189,43 @@ Instance::set_redo_status(bool x)
 }
 
 bool
-studio::Instance::save_as(const synfig::String &file_name)const
-{
-       if(synfigapp::Instance::save_as(file_name))
-       {
-               App::add_recent_file(file_name);
-               return true;
-       }
-       return false;
-}
-
-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
+               list<handle<CanvasView> >::iterator iter;
+               for(iter=canvas_view_list().begin();iter!=canvas_view_list().end();iter++)
+                       (*iter)->render_settings.set_entry_filename();
                App::add_recent_file(file_name);
                return true;
        }
        return false;
 }
 
-bool
+Instance::Status
 studio::Instance::save()
 {
-       if(basename(get_file_name()).find("untitled")==0)
-       {
-               dialog_save_as();
-               return true;
-       }
+       // 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 need to ask where to save it
+       if(get_file_name().find(DEFAULT_FILENAME_PREFIX)==0)
+               if (dialog_save_as())
+                       return STATUS_OK;
+               else
+                       return STATUS_CANCEL;
 
-       return synfigapp::Instance::save();
+       if (synfigapp::Instance::save())
+               return STATUS_OK;
 
+       App::dialog_error_blocking("Save - Error","Unable to save to '" + get_file_name() + "'");
+       return STATUS_ERROR;
 }
 
-void
+bool
 studio::Instance::dialog_save_as()
 {
-       string filename="*.sif";
-
+       string filename=basename(get_file_name());
        Canvas::Handle canvas(get_canvas());
 
        {
@@ -244,7 +246,7 @@ studio::Instance::dialog_save_as()
                                                "other files first before trying to use \"SaveAs\"."
                                        );
 
-                                       return;
+                                       return false;
                                }
                                if(parent_layer)
                                        break;
@@ -252,24 +254,28 @@ studio::Instance::dialog_save_as()
                }
        }
 
-       while(App::dialog_saveas_file("SaveAs", filename))
+       // show the canvas' name if it has one, else its ID
+       while(App::dialog_save_file(_("Choose a Filename to Save As") +
+                                                               String(" (") +
+                                                               (canvas->get_name().empty()
+                                                                ? canvas->get_id()
+                                                                : canvas->get_name()) +
+                                                               ") ...", filename))
        {
                // If the filename still has wildcards, then we should
                // continue looking for the file we want
                if(find(filename.begin(),filename.end(),'*')!=filename.end())
                        continue;
 
-               if(find(filename.begin(),filename.end(),'.')==filename.end())
-                       filename+=".sif";
+               if (filename_extension(filename) == "")
+                       filename+=".sifz";
 
                try
                {
-                       String ext(String(filename.begin()+filename.find_last_of('.')+1,filename.end()));
-                       if(ext!="sif" && ext!="sifz" && !App::dialog_yes_no(_("Unknown extension"),
-                               _("You have given the file name an extension\nwhich I do not recognise. Are you sure this is what you want?")))
-                       {
+                       String ext(filename_extension(filename));
+                       if(ext!=".sif" && ext!=".sifz" && !App::dialog_yes_no(_("Unknown extension"),
+                               _("You have given the file name an extension\nwhich I do not recognize. Are you sure this is what you want?")))
                                continue;
-                       }
                }
                catch(...)
                {
@@ -278,9 +284,19 @@ studio::Instance::dialog_save_as()
 
                {
                        struct stat     s;
-                       // if stat() succeeds, or it fails with something other than 'file doesn't exist', the file exists
+                       int stat_return = stat(filename.c_str(), &s);
+
+                       // if stat() fails with something other than 'file doesn't exist', there's been a real
+                       // error of some kind.  let's give up now and ask for a new path.
+                       if (stat_return == -1 && errno != ENOENT)
+                       {
+                               perror(filename.c_str());
+                               App::dialog_error_blocking("SaveAs - Error","Unable to check whether '" + filename + "' exists.");
+                               continue;
+                       }
+
                        // if the file exists and the user doesn't want to overwrite it, keep prompting for a filename
-                       if ((stat(filename.c_str(), &s) != -1 || errno != ENOENT) &&
+                       if ((stat_return == 0) &&
                                !App::dialog_yes_no("File exists",
                                                                        "A file named '" +
                                                                        filename +
@@ -290,10 +306,12 @@ studio::Instance::dialog_save_as()
                }
 
                if(save_as(filename))
-                       break;
+                       return true;
 
-               App::dialog_error_blocking("SaveAs - Error","Unable to save file");
+               App::dialog_error_blocking("SaveAs - Error","Unable to save to '" + filename + "'");
        }
+
+       return false;
 }
 
 void
@@ -348,15 +366,12 @@ Instance::close()
                studio::App::set_selected_instance(0);
        }
        else
-       {
-               studio::App::set_selected_canvas_view(studio::App::instance_list.front()->canvas_view_list().front());
-               //studio::App::set_selected_instance(studio::App::instance_list.front());
-       }
+               studio::App::instance_list.front()->canvas_view_list().front()->present();
 }
 
 
 void
-Instance::insert_canvas(Gtk::TreeRow row,Canvas::Handle canvas)
+Instance::insert_canvas(Gtk::TreeRow row, synfig::Canvas::Handle canvas)
 {
        CanvasTreeModel canvas_tree_model;
        assert(canvas);
@@ -468,7 +483,7 @@ Instance::dialog_cvs_commit()
        }
        catch(...)
        {
-               App::dialog_error_blocking(_("Error"),_("An error has occured when trying to COMMIT"));
+               App::dialog_error_blocking(_("Error"),_("An error has occurred when trying to COMMIT"));
        }
        update_all_titles();
 }
@@ -493,7 +508,7 @@ Instance::dialog_cvs_add()
        }
        catch(...)
        {
-               App::dialog_error_blocking(_("Error"),_("An error has occured when trying to ADD"));
+               App::dialog_error_blocking(_("Error"),_("An error has occurred when trying to ADD"));
        }
        update_all_titles();
 }
@@ -534,7 +549,7 @@ Instance::dialog_cvs_update()
        }
        catch(...)
        {
-               App::dialog_error_blocking(_("Error"),_("An error has occured when trying to UPDATE"));
+               App::dialog_error_blocking(_("Error"),_("An error has occurred when trying to UPDATE"));
        }
        //update_all_titles();
 }
@@ -570,7 +585,7 @@ Instance::dialog_cvs_revert()
        }
        catch(...)
        {
-               App::dialog_error_blocking(_("Error"),_("An error has occured when trying to UPDATE"));
+               App::dialog_error_blocking(_("Error"),_("An error has occurred when trying to UPDATE"));
        }
        //update_all_titles();
 }
@@ -617,7 +632,7 @@ bool
 Instance::safe_revert()
 {
        if(synfigapp::Instance::get_action_count())
-               if(!App::dialog_yes_no(_("Revert to saved"), _("You will loose any changes you have made since your last save.\nAre you sure?")))
+               if(!App::dialog_yes_no(_("Revert to saved"), _("You will lose any changes you have made since your last save.\nAre you sure?")))
                        return false;
        revert();
        return true;
@@ -626,18 +641,33 @@ Instance::safe_revert()
 bool
 Instance::safe_close()
 {
-       handle<synfigapp::UIInterface> uim;
-       uim=find_canvas_view(get_canvas())->get_ui_interface();
+       handle<CanvasView> canvas_view = find_canvas_view(get_canvas());
+       handle<synfigapp::UIInterface> uim=canvas_view->get_ui_interface();
 
-       if(get_action_count())
+       // if the animation is currently playing, closing the window will cause a crash,
+       // so don't allow it
+       if (canvas_view->is_playing())
        {
-               string str=strprintf(_("Would you like to save your changes to %s?"),basename(get_file_name()).c_str() );
-               int answer=uim->yes_no_cancel(get_canvas()->get_name(),str,synfigapp::UIInterface::RESPONSE_YES);
-               if(answer==synfigapp::UIInterface::RESPONSE_YES)
-                       save();
-               if(answer==synfigapp::UIInterface::RESPONSE_CANCEL)
-                       return false;
+               canvas_view->present();
+               App::dialog_error_blocking("Close Error", "The animation is currently playing so the window cannot be closed.");
+               return false;
        }
+       if(get_action_count())
+               do
+               {
+                       string str=strprintf(_("Would you like to save your changes to %s?"),basename(get_file_name()).c_str() );
+                       int answer=uim->yes_no_cancel(get_canvas()->get_name(),str,synfigapp::UIInterface::RESPONSE_YES);
+                       if(answer==synfigapp::UIInterface::RESPONSE_YES)
+                       {
+                               enum Status status = save();
+                               if (status == STATUS_OK) break;
+                               else if (status == STATUS_CANCEL) return false;
+                       }
+                       if(answer==synfigapp::UIInterface::RESPONSE_NO)
+                               break;
+                       if(answer==synfigapp::UIInterface::RESPONSE_CANCEL)
+                               return false;
+               } while (true);
 
        if(is_modified())
        {
@@ -667,7 +697,7 @@ Instance::add_actions_to_group(const Glib::RefPtr<Gtk::ActionGroup>& action_grou
        candidate_list.sort();
 
        if(candidate_list.empty())
-               synfig::warning("Action CandidateList is empty!");
+               synfig::warning("%s:%d Action CandidateList is empty!", __FILE__, __LINE__);
 
        for(iter=candidate_list.begin();iter!=candidate_list.end();++iter)
        {
@@ -707,7 +737,7 @@ Instance::add_actions_to_menu(Gtk::Menu *menu, const synfigapp::Action::ParamLis
        candidate_list.sort();
 
        if(candidate_list.empty())
-               synfig::warning("Action CandidateList is empty!");
+               synfig::warning("%s:%d Action CandidateList is empty!", __FILE__, __LINE__);
 
        for(iter=candidate_list.begin();iter!=candidate_list.end();++iter)
        {
@@ -776,11 +806,11 @@ Instance::add_actions_to_menu(Gtk::Menu *menu, const synfigapp::Action::ParamLis
        candidate_list.sort();
 
        if(candidate_list.empty())
-               synfig::warning("Action CandidateList is empty!");
+               synfig::warning("%s:%d Action CandidateList is empty!", __FILE__, __LINE__);
        if(candidate_list2.empty())
-               synfig::warning("Action CandidateList2 is empty!");
+               synfig::warning("%s:%d Action CandidateList2 is empty!", __FILE__, __LINE__);
 
-       // Seperate out the candidate lists so that there are no conflicts
+       // Separate out the candidate lists so that there are no conflicts
        for(iter=candidate_list.begin();iter!=candidate_list.end();++iter)
        {
                synfigapp::Action::CandidateList::iterator iter2(candidate_list2.find(iter->name));
@@ -891,7 +921,7 @@ Instance::add_actions_to_menu(Gtk::Menu *menu, const synfigapp::Action::ParamLis
 }
 
 void
-Instance::process_action(String name, synfigapp::Action::ParamList param_list)
+Instance::process_action(synfig::String name, synfigapp::Action::ParamList param_list)
 {
        assert(synfigapp::Action::book().count(name));
 
@@ -977,6 +1007,18 @@ Instance::make_param_menu(Gtk::Menu *menu,synfig::Canvas::Handle canvas, synfiga
        // Populate the convert menu by looping through
        // the ValueNode book and find the ones that are
        // relevant.
+
+       // show the 'Convert' sub-menu if this valuedesc is anything other than either:
+       //   the 'Index' parameter of a Duplicate layer
+       // or
+       //   a Duplicate ValueNode whose parent is not a (layer or ValueNode)
+       if (!((value_desc.parent_is_layer_param() &&
+                  value_desc.get_layer()->get_name() == "duplicate" &&
+                  value_desc.get_param_name() == "index") ||
+                 (value_desc.is_value_node() &&
+                  ValueNode_Duplicate::Handle::cast_dynamic(value_desc.get_value_node()) &&
+                  !(value_desc.parent_is_layer_param() ||
+                        value_desc.parent_is_value_node()))))
        {
                Gtk::Menu *convert_menu=manage(new Gtk::Menu());
                LinkableValueNode::Book::const_iterator iter;
@@ -1058,10 +1100,8 @@ edit_several_waypoints(etl::handle<CanvasView> canvas_view, std::list<synfigapp:
        dialog.add_button(Gtk::StockID("gtk-cancel"),0);
        dialog.show();
 
-       DEBUGPOINT();
        if(dialog.run()==0 || widget_waypoint_model.get_waypoint_model().is_trivial())
                return;
-       DEBUGPOINT();
        synfigapp::Action::PassiveGrouper group(canvas_interface->get_instance().get(),_("Set Waypoints"));
 
        std::list<synfigapp::ValueDesc>::iterator iter;