diff --git a/Gtkmm3/cfgfile2/cfgfile.cc b/Gtkmm3/cfgfile2/cfgfile.cc new file mode 100644 index 0000000..58ba11b --- /dev/null +++ b/Gtkmm3/cfgfile2/cfgfile.cc @@ -0,0 +1,154 @@ +// A Test to read Config files +// This is a modified version +// The Config File Should be a text file +// and the content as follows +// key=value + +#include "cfgfile.hh" + +static void Trim(std::string &str){ + if(str.empty()){//String is empty + return; + } + + size_t i,start_pos,end_pos; + + for(i=0;i=0;i--){//Get End Position + if(!isspace(str[i])){ + break; + } + } + + end_pos=i; + + str=str.substr(start_pos,end_pos+1); +} + +bool readCfgFile(std::string &cfgfilePath,conf_map &configs){ + //Open The Config File and load config to map + std::fstream cfgfile; + cfgfile.open(cfgfilePath); + if(!cfgfile.is_open()){ + std::cout<<"Failed to open the file!"<signal_changed().connect(sigc::mem_fun(*this,&MyWin::selection_changed)); + + show_all_children(); +} + +void MyWin::selection_changed(){ + auto row=*(selection->get_selected()); + m_label.set_label(row[m_columns.m_col_value]); +} + +void MyWin::btnload_clicked(){ + dialog=Gtk::FileChooserNative::create("Open A Config File",*this,Gtk::FILE_CHOOSER_ACTION_OPEN, + "OK","Cancel"); + + dialog->signal_response().connect(sigc::mem_fun(*this,&MyWin::dialog_response)); + + auto filter_any=Gtk::FileFilter::create(); + filter_any->add_pattern("*"); + filter_any->set_name("Any Files"); + + dialog->add_filter(filter_any); + + dialog->show(); +} + +void MyWin::dialog_response(int response_id){ + if(response_id==Gtk::RESPONSE_ACCEPT){ + auto filename=dialog->get_filename(); + + //Clear the liststore + ref_liststore->clear(); + + //If Configs is not empty,clear it + if(!my_configs.empty()){ + my_configs.clear(); + } + + if(readCfgFile(filename,my_configs)){ + auto mite=my_configs.begin(); + for(;mite!=my_configs.end();mite++){ + auto row=*(ref_liststore->append()); + row[m_columns.m_col_key]=mite->first; + row[m_columns.m_col_value]=mite->second; + } + } + } + dialog.reset(); +} + +void MyWin::btnsave_clicked(){ + dialog=Gtk::FileChooserNative::create("Open A Config File",*this,Gtk::FILE_CHOOSER_ACTION_SAVE, + "OK","Cancel"); + + dialog->signal_response().connect(sigc::mem_fun(*this,&MyWin::dialog_save_response)); + + auto filter_cfg=Gtk::FileFilter::create(); + filter_cfg->add_pattern("*.cfg"); + filter_cfg->set_name("Config Files"); + dialog->add_filter(filter_cfg); + + auto filter_any=Gtk::FileFilter::create(); + filter_any->add_pattern("*"); + filter_any->set_name("Any Files"); + dialog->add_filter(filter_any); + + dialog->show(); +} + +void MyWin::dialog_save_response(int response_id){ + if(response_id==Gtk::RESPONSE_ACCEPT){ + + //Get Filename and open(create) a config file + std::string filename=dialog->get_filename(); + std::fstream outfile; + outfile.open(filename,std::ios_base::out); + if(!outfile.is_open()){ + dialog.reset(); + std::cout<<"Error!"; + return ; + } + + //Write Key and Values + auto children=ref_liststore->children(); + for(auto iter = children.begin(); + iter != children.end(); iter++) + { + auto row=*iter; + outfile< +#include "../cfgfile2/cfgfile.hh" + +class MyWin : public Gtk::Window{ +public: + + MyWin(); + +private: + + //Model Columns + class ModelColumns : public Gtk::TreeModelColumnRecord{ + public: + ModelColumns() + {add(m_col_key); add(m_col_value); } + + Gtk::TreeModelColumn m_col_key; + Gtk::TreeModelColumn m_col_value; + }; + + ModelColumns m_columns; + + Glib::RefPtr ref_liststore; + Glib::RefPtr selection; + + //Child Widgets + Gtk::TreeView m_treeview; + Gtk::Box main_box,btn_box; + Gtk::ScrolledWindow scrolled; + Gtk::Label m_label; + Gtk::Button btn_load,btn_save,btn_exit; + + Glib::RefPtr dialog; + + //Keys and values for config + conf_map my_configs; + + //Signal Handlers + void selection_changed(); + void btnload_clicked(); + void btnsave_clicked(); + void dialog_response(int response_id); + void dialog_save_response(int response_id); + +}; \ No newline at end of file diff --git a/Gtkmm3/gtk102_configs/src/main.cc b/Gtkmm3/gtk102_configs/src/main.cc new file mode 100644 index 0000000..3c8b613 --- /dev/null +++ b/Gtkmm3/gtk102_configs/src/main.cc @@ -0,0 +1,7 @@ +#include "MyWin.hh" + +int main(int argc,char **argv){ + auto app=Gtk::Application::create(argc,argv,"org.gtk.daleclack"); + MyWin window; + return app->run(window); +}