diff --git a/Gtkmm3/gtk92_fontdialog/src/MyWin.cc b/Gtkmm3/gtk92_fontdialog/src/MyWin.cc index bd670c2..806cffd 100644 --- a/Gtkmm3/gtk92_fontdialog/src/MyWin.cc +++ b/Gtkmm3/gtk92_fontdialog/src/MyWin.cc @@ -1,8 +1,9 @@ #include "MyWin.hh" -#include +#include MyWin::MyWin() -: +:btnsave("Save Config"), +btnload("Load Config from file"), label1("Simple Text"), main_box(Gtk::ORIENTATION_VERTICAL,5) { @@ -18,8 +19,14 @@ main_box(Gtk::ORIENTATION_VERTICAL,5) fontbtn.set_use_font(); fontbtn.signal_font_set().connect(sigc::mem_fun(*this,&MyWin::font_changed)); label1.override_font(descript); + btnsave.signal_clicked().connect(sigc::mem_fun(*this,&MyWin::save_dialog)); + btnload.signal_clicked().connect(sigc::mem_fun(*this,&MyWin::load_dialog)); + + //Pack Buttons main_box.pack_start(label1,Gtk::PACK_SHRINK); main_box.pack_start(fontbtn,Gtk::PACK_SHRINK); + main_box.pack_start(btnload,Gtk::PACK_SHRINK); + main_box.pack_start(btnsave,Gtk::PACK_SHRINK); main_box.set_halign(Gtk::ALIGN_CENTER); main_box.set_valign(Gtk::ALIGN_CENTER); @@ -28,7 +35,75 @@ main_box(Gtk::ORIENTATION_VERTICAL,5) show_all_children(); } +void MyWin::load_dialog(){ + //Create the dialog + dialog=Gtk::FileChooserNative::create("Select a Config File",*this, + Gtk::FILE_CHOOSER_ACTION_OPEN,"OK","Cancel"); + dialog->signal_response().connect(sigc::mem_fun(*this,&MyWin::load_font)); + + //File Filters + auto filter_conf=Gtk::FileFilter::create(); + filter_conf->set_name("Config File"); + filter_conf->add_pattern("*.conf"); + dialog->add_filter(filter_conf); + + auto filter_any=Gtk::FileFilter::create(); + filter_any->set_name("Any Files"); + filter_any->add_pattern("*"); + dialog->add_filter(filter_any); + + dialog->show(); +} + +void MyWin::load_font(int response){ + //Load the config from a file + if(response==Gtk::RESPONSE_ACCEPT){ + Glib::ustring filename=dialog->get_filename(); + char fontname[57]; + std::ifstream infile; + infile.open(filename,std::ios_base::in); + infile.getline(fontname,57); + fontbtn.set_font_name(fontname); + infile.close(); + font_changed(); + } + dialog.reset(); +} + +void MyWin::save_dialog(){ + //Create the dialog + dialog=Gtk::FileChooserNative::create("Select a file to save",*this, + Gtk::FILE_CHOOSER_ACTION_SAVE,"OK","Cancel"); + dialog->signal_response().connect(sigc::mem_fun(*this,&MyWin::save_font)); + + //File Filters + auto filter_conf=Gtk::FileFilter::create(); + filter_conf->set_name("Config File"); + filter_conf->add_pattern("*.conf"); + dialog->add_filter(filter_conf); + + auto filter_any=Gtk::FileFilter::create(); + filter_any->set_name("Any Files"); + filter_any->add_pattern("*"); + dialog->add_filter(filter_any); + + dialog->show(); +} + +void MyWin::save_font(int response){ + //Save Font Config to a file + if(response==Gtk::RESPONSE_ACCEPT){ + Glib::ustring filename=dialog->get_filename(); + std::ofstream outfile; + outfile.open(filename,std::ios_base::out); + outfile< dialog; //Signal Handlers void font_changed(); + void save_dialog(); + void load_dialog(); + void load_font(int response); + void save_font(int response); }; diff --git a/Gtkmm4/gtk93_fontdialog/src/MyWin.cc b/Gtkmm4/gtk93_fontdialog/src/MyWin.cc index 657493c..9c78466 100644 --- a/Gtkmm4/gtk93_fontdialog/src/MyWin.cc +++ b/Gtkmm4/gtk93_fontdialog/src/MyWin.cc @@ -1,8 +1,9 @@ #include "MyWin.hh" -#include +#include MyWin::MyWin() -:fontbtn("Sans 10"), +:btnsave("Save Config"), +btnload("Load Config from file"), label1("Simple Text"), main_box(Gtk::Orientation::VERTICAL,5) { @@ -12,10 +13,19 @@ main_box(Gtk::Orientation::VERTICAL,5) set_title("Font Dialog"); //Add Button + Glib::ustring curr_font=fontbtn.get_font(); + Pango::FontDescription descript(curr_font); fontbtn.set_use_font(); + fontbtn.set_font(curr_font); fontbtn.signal_font_set().connect(sigc::mem_fun(*this,&MyWin::font_changed)); + btnsave.signal_clicked().connect(sigc::mem_fun(*this,&MyWin::save_dialog)); + btnload.signal_clicked().connect(sigc::mem_fun(*this,&MyWin::load_dialog)); + + //Pack Widgets main_box.append(label1); main_box.append(fontbtn); + main_box.append(btnload); + main_box.append(btnsave); main_box.set_halign(Gtk::Align::CENTER); main_box.set_valign(Gtk::Align::CENTER); @@ -24,9 +34,89 @@ main_box(Gtk::Orientation::VERTICAL,5) //show_all_children(); } +void MyWin::load_dialog(){ + //Create the dialog + dialog=Gtk::FileChooserNative::create("Select a Config File",*this, + Gtk::FileChooser::Action::OPEN,"OK","Cancel"); + dialog->signal_response().connect(sigc::mem_fun(*this,&MyWin::load_font)); + + //File Filters + auto filter_conf=Gtk::FileFilter::create(); + filter_conf->set_name("Config File"); + filter_conf->add_pattern("*.conf"); + dialog->add_filter(filter_conf); + + auto filter_any=Gtk::FileFilter::create(); + filter_any->set_name("Any Files"); + filter_any->add_pattern("*"); + dialog->add_filter(filter_any); + + dialog->show(); +} + +void MyWin::load_font(int response){ + //Load the config from a file + if(response==Gtk::ResponseType::ACCEPT){ + //Get Filename + auto file=dialog->get_file(); + Glib::ustring filename=file->get_path(); + //Get The Fontname from a file + char fontname[57]; + std::ifstream infile; + infile.open(filename,std::ios_base::in); + infile.getline(fontname,57); + fontbtn.set_font(fontname); + infile.close(); + //Change the font + font_changed(); + //Free Memory + file.reset(); + } + dialog.reset(); +} + +void MyWin::save_dialog(){ + //Create the dialog + dialog=Gtk::FileChooserNative::create("Select a file to save",*this, + Gtk::FileChooser::Action::SAVE,"OK","Cancel"); + dialog->signal_response().connect(sigc::mem_fun(*this,&MyWin::save_font)); + + //File Filters + auto filter_conf=Gtk::FileFilter::create(); + filter_conf->set_name("Config File"); + filter_conf->add_pattern("*.conf"); + dialog->add_filter(filter_conf); + + auto filter_any=Gtk::FileFilter::create(); + filter_any->set_name("Any Files"); + filter_any->add_pattern("*"); + dialog->add_filter(filter_any); + + dialog->show(); +} + +void MyWin::save_font(int response){ + //Save Font Config to a file + if(response==Gtk::ResponseType::ACCEPT){ + //Get Filename + auto file=dialog->get_file(); + Glib::ustring filename=file->get_path(); + //Save File + std::ofstream outfile; + outfile.open(filename,std::ios_base::out); + outfile< dialog; //Signal Handlers void font_changed(); + void save_dialog(); + void load_dialog(); + void load_font(int response); + void save_font(int response); };