Skip to content

Commit

Permalink
Update palette file save and open dialog. Ane improve canvas window s…
Browse files Browse the repository at this point in the history
…ave file dialog
  • Loading branch information
jcome authored and morevnaproject committed Nov 29, 2014
1 parent 2dd234a commit 68bab03
Show file tree
Hide file tree
Showing 4 changed files with 152 additions and 36 deletions.
151 changes: 139 additions & 12 deletions synfig-studio/src/gui/app.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2281,6 +2281,50 @@ App::dialog_open_file(const std::string &title, std::string &filename, std::stri
#endif // not USE_WIN32_FILE_DIALOGS
}

bool
App::dialog_open_file_spal(const std::string &title, std::string &filename, std::string preference)
{
synfig::String prev_path;

if(!_preferences.get_value(preference, prev_path))
prev_path = ".";

prev_path = absolute_path(prev_path);

Gtk::FileChooserDialog *dialog = new Gtk::FileChooserDialog(*App::main_window,
title, Gtk::FILE_CHOOSER_ACTION_OPEN);

dialog->set_transient_for(*App::main_window);
dialog->set_current_folder(prev_path);
dialog->add_button(Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL);
dialog->add_button(Gtk::StockID(_("Open")), Gtk::RESPONSE_ACCEPT);

// show only Synfig color palette file (*.spal)
Glib::RefPtr<Gtk::FileFilter> filter_spal = Gtk::FileFilter::create();
filter_spal->set_name("Synfig palette file");
filter_spal->add_pattern("*.spal");
dialog->add_filter(filter_spal);

if (filename.empty())
dialog->set_filename(prev_path);
else if (is_absolute_path(filename))
dialog->set_filename(filename);
else
dialog->set_filename(prev_path + ETL_DIRECTORY_SEPARATOR + filename);

if(dialog->run() == GTK_RESPONSE_ACCEPT) {
filename = dialog->get_filename();
_preferences.set_value(preference, dirname(filename));
delete dialog;
return true;
}

delete dialog;
return false;
}



bool
App::dialog_open_file_with_history_button(const std::string &title, std::string &filename, bool &show_history, std::string preference)
{
Expand Down Expand Up @@ -2447,7 +2491,10 @@ App::dialog_save_file(const std::string &title, std::string &filename, std::stri
// file type filters
Glib::RefPtr<Gtk::FileFilter> filter_sif = Gtk::FileFilter::create();
filter_sif->set_name("Uncompressed Synfig file(*.sif)");
filter_sif->add_mime_type("application/x-sif");

// sif share same mime type "application/x-sif" with sifz, so it will mixed .sif and .sifz files. Use only
// pattern ("*.sif") for sif file format should be oK.
//filter_sif->add_mime_type("application/x-sif");
filter_sif->add_pattern("*.sif");

Glib::RefPtr<Gtk::FileFilter> filter_sifz = Gtk::FileFilter::create();
Expand Down Expand Up @@ -2497,9 +2544,10 @@ App::dialog_save_file(const std::string &title, std::string &filename, std::stri
dialog->set_extra_widget(*hbox);
}

if (filename.empty()) dialog->set_filename(prev_path);
else
{
if (filename.empty()) {
dialog->set_filename(prev_path);

}else{
std::string full_path;
if (is_absolute_path(filename))
full_path = filename;
Expand All @@ -2513,20 +2561,35 @@ App::dialog_save_file(const std::string &title, std::string &filename, std::stri
struct stat s;
if(stat(full_path.c_str(),&s) == -1 && errno == ENOENT)
dialog->set_current_name(basename(filename));

}
// set file filter according to previous file format
if (filename_extension(filename) == ".sif" ) dialog->set_filter(filter_sif);
if (filename_extension(filename)== ".sifz" ) dialog->set_filter(filter_sifz);
if (filename_extension(filename) == ".sfg" ) dialog->set_filter(filter_sfg);

// set focus to the file name entry(box) of dialog instead to avoid the name
// we are going to save changes while changing file filter each time.
dialog->set_current_name(basename(filename));

if(dialog->run() == GTK_RESPONSE_ACCEPT) {

if (preference == ANIMATION_DIR_PREFERENCE)
set_file_version(synfig::ReleaseVersion(file_type_enum->get_value()));
{
// add file extension according to file filter selected by user
if (dialog->get_filter() == filter_sif)
filename = dialog->get_filename() + ".sif";
else if (dialog->get_filter() == filter_sifz)
filename = dialog->get_filename() + ".sifz";
else if (dialog->get_filter() == filter_sfg)
filename = dialog->get_filename() + ".sfg";
filename = dialog->get_filename();
if (filename_extension(filename) != ".sif" &&
filename_extension(filename) != ".sifz" &&
filename_extension(filename) != ".sfg")
{
if (dialog->get_filter() == filter_sif)
filename = dialog->get_filename() + ".sif";
else if (dialog->get_filter() == filter_sifz)
filename = dialog->get_filename() + ".sifz";
else if (dialog->get_filter() == filter_sfg)
filename = dialog->get_filename() + ".sfg";
}
else filename = dialog->get_filename();
}

Expand All @@ -2541,6 +2604,70 @@ App::dialog_save_file(const std::string &title, std::string &filename, std::stri
#endif
}


bool
App::dialog_save_file_spal(const std::string &title, std::string &filename, std::string preference)
{
synfig::String prev_path;
if(!_preferences.get_value(preference, prev_path))
prev_path=".";
prev_path = absolute_path(prev_path);

Gtk::FileChooserDialog *dialog = new Gtk::FileChooserDialog(*App::main_window, title, Gtk::FILE_CHOOSER_ACTION_SAVE);

// file type filters
Glib::RefPtr<Gtk::FileFilter> filter_spal = Gtk::FileFilter::create();
filter_spal->set_name("Synfig palette file(*.spal)");
filter_spal->add_pattern("*.spal");

dialog->set_current_folder(prev_path);
dialog->add_button(Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL);
dialog->add_button(Gtk::Stock::SAVE, Gtk::RESPONSE_ACCEPT);

dialog->add_filter(filter_spal);

if (filename.empty()) {
dialog->set_filename(prev_path);

}else{
std::string full_path;
if (is_absolute_path(filename))
full_path = filename;
else
full_path = prev_path + ETL_DIRECTORY_SEPARATOR + filename;

// select the file if it exists
dialog->set_filename(full_path);

// if the file doesn't exist, put its name into the filename box
struct stat s;
if(stat(full_path.c_str(),&s) == -1 && errno == ENOENT)
dialog->set_current_name(basename(filename));

}

dialog->set_filter(filter_spal);

// set focus to the file name entry(box) of dialog instead to avoid the name
// we are going to save changes while changing file filter each time.
dialog->set_current_name(basename(filename));

if(dialog->run() == GTK_RESPONSE_ACCEPT) {

// add file extension according to file filter selected by user
filename = dialog->get_filename();
if (filename_extension(filename) != ".spal")
filename = dialog->get_filename() + ".spal";

delete dialog;
return true;
}

delete dialog;
return false;
}


bool
App::dialog_select_list_item(const std::string &title, const std::string &message, const std::list<std::string> &list, int &item_index)
{
Expand Down Expand Up @@ -3053,7 +3180,7 @@ App::dialog_open(string filename)
filename="*.sif";

bool show_history = false;
while(dialog_open_file_with_history_button(_("Open"), filename, show_history, ANIMATION_DIR_PREFERENCE))
while(dialog_open_file_with_history_button(_("Please select a file"), filename, show_history, ANIMATION_DIR_PREFERENCE))
{
// If the filename still has wildcards, then we should
// continue looking for the file we want
Expand All @@ -3077,7 +3204,7 @@ App::dialog_open(string filename)

// show dialog
index=0;
if (!dialog_select_list_item(_("Open"), _("Select one of previous versions of file"), list, index))
if (!dialog_select_list_item(_("Please select a file"), _("Select one of previous versions of file"), list, index))
continue;

// find selected entry in list (descending)
Expand Down
2 changes: 2 additions & 0 deletions synfig-studio/src/gui/app.h
Original file line number Diff line number Diff line change
Expand Up @@ -362,8 +362,10 @@ class App : public Gtk::Main, private IconController
// static bool dialog_file(const std::string &title, std::string &filename);

static bool dialog_open_file(const std::string &title, std::string &filename, std::string preference);
static bool dialog_open_file_spal(const std::string &title, std::string &filename, std::string preference);
static bool dialog_open_file_with_history_button(const std::string &title, std::string &filename, bool &show_history, std::string preference);
static bool dialog_save_file(const std::string &title, std::string &filename, std::string preference);
static bool dialog_save_file_spal(const std::string &title, std::string &filename, std::string preference);

static bool dialog_select_list_item(const std::string &title, const std::string &message, const std::list<std::string> &list, int &item_index);

Expand Down
33 changes: 10 additions & 23 deletions synfig-studio/src/gui/modules/mod_palette/dock_paledit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -151,12 +151,12 @@ Dock_PalEdit::Dock_PalEdit():
action_group->add(Gtk::Action::create(
"palette-load",
Gtk::StockID("gtk-open"),
_("Load a palette"),
_("Load a saved palette")
_("Open a palette"),
_("Open a saved palette")
),
sigc::mem_fun(
*this,
&Dock_PalEdit::on_load_pressed
&Dock_PalEdit::on_open_pressed
)
);
action_group->add(Gtk::Action::create(
Expand Down Expand Up @@ -230,31 +230,18 @@ Dock_PalEdit::on_add_pressed()
void
Dock_PalEdit::on_save_pressed()
{
synfig::String filename = "";
while (App::dialog_save_file(_("Choose a Filename to Save As"),
filename, ANIMATION_DIR_PREFERENCE))
// it would be nice to have initial spal file name same as current canvas name,
// use "My Palette" as temporary spal file name as a hack.
//synfig::String filename = selected_instance->get_file_name();
synfig::String filename = "My Palette";
while (App::dialog_save_file_spal(_("Please choose a file name"), filename, ANIMATION_DIR_PREFERENCE))
{
// If the filename still has wildcards, then we should
// continue looking for the file we want
string base_filename = basename(filename);
if (find(base_filename.begin(),base_filename.end(),'*')!=base_filename.end())
continue;

if (filename_extension(filename) == "")
filename+=".spal";

try
{
String ext(filename_extension(filename));
if(ext!=".spal" && !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(...)
{
continue;
}

{
struct stat s;
int stat_return = stat(filename.c_str(), &s);
Expand Down Expand Up @@ -282,10 +269,10 @@ Dock_PalEdit::on_save_pressed()
}

void
Dock_PalEdit::on_load_pressed()
Dock_PalEdit::on_open_pressed()
{
synfig::String filename = "*.spal";
while(App::dialog_open_file(_("Choose a Palette to load"), filename, ANIMATION_DIR_PREFERENCE))
while(App::dialog_open_file_spal(_("Please select a palette file"), filename, ANIMATION_DIR_PREFERENCE))
{
// If the filename still has wildcards, then we should
// continue looking for the file we want
Expand Down
2 changes: 1 addition & 1 deletion synfig-studio/src/gui/modules/mod_palette/dock_paledit.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ class Dock_PalEdit : public Dockable

void on_save_pressed();

void on_load_pressed();
void on_open_pressed();

void show_menu(int i);

Expand Down

0 comments on commit 68bab03

Please sign in to comment.