#include <gtkmm.h>
#include <string>
#include <vector>
#include <algorithm>
#include <boost/lexical_cast.hpp>
#include <utility>
#include <functional>
class title_table:public Gtk::TreeModel::ColumnRecord
{
public:
title_table()
{
add(auth);
add(age);
add(sex);
}
Gtk::TreeModelColumn<std::string> auth;
Gtk::TreeModelColumn<int> age;
Gtk::TreeModelColumn<bool> sex;
};
class main_window:public Gtk::Window
{
public:
main_window()
{
add(_vbox);
fill_menu_bar();
fill_tree();
maximize();
show_all();
}
protected:
void fill_tree(void)
{
_ptr_list_model = Gtk::ListStore::create(_my_table);
_tree_view.set_model(_ptr_list_model);
std::vector<int> int_vector;
std::generate_n(back_inserter(int_vector), 10, rand);
std::for_each(int_vector.begin(), int_vector.end(), std::bind1st(std::mem_fun(&main_window::fill),this));
_tree_view.append_column("author", _my_table.auth);
_tree_view.append_column("age", _my_table.age);
_tree_view.append_column("sex", _my_table.sex);
_vbox.add(_tree_view);
}
void fill_menu_bar(void)
{
gen_sub_file(gen_menu_item("File"));
gen_sub_edit(gen_menu_item("Edit"));
gen_sub_view(gen_menu_item("View"));
gen_sub_help(gen_menu_item("Help"));
_vbox.add(_menu_bar);
}
Gtk::MenuItem* gen_menu_item(Glib::ustring name)
{
Gtk::MenuItem* ptr_menu_item = Gtk::manage(new Gtk::MenuItem());
ptr_menu_item->add_label(name);
_menu_bar.add(*ptr_menu_item);
return ptr_menu_item;
}
void on_image(void)
{
Gtk::MessageDialog* p_md = new Gtk::MessageDialog("this is image");
p_md->run();
p_md->hide_all();
}
void on_list(void)
{
Gtk::MessageDialog* p_md = new Gtk::MessageDialog("this is list");
p_md->run();
p_md->hide_all();
}
void on_text(void)
{
Gtk::MessageDialog* p_md = new Gtk::MessageDialog("this is text");
p_md->run();
p_md->hide_all();
}
template <typename T>
void gen_sub_view(T t)
{
Gtk::Menu* ptr_sub_menu = Gtk::manage(new Gtk::Menu());
t->set_submenu(*ptr_sub_menu);
std::vector<std::pair<Glib::ustring, T > > pv;
pv.push_back(std::make_pair("image", &main_window::on_image));
pv.push_back(std::make_pair("list", &main_window::on_list));
pv.push_back(std::make_pair("text", &main_window::on_text));
gen_sub_menu_item(ptr_sub_menu, pv);
}
template<typename parent, typename contain>
void gen_sub_menu_item(parent p, const contain& pair_vector)
{
Gtk::MenuItem* ptr_item;
typename contain::const_iterator it;
for (it = pair_vector.begin(); it != pair_vector.end(); ++it)
{
ptr_item = Gtk::manage(new Gtk::MenuItem());
ptr_item->add_label(it->first);
ptr_item->signal_activate().connect(sigc::mem_fun(*this, it->second));
p->add(*ptr_item);
}
}
void on_undo(void)
{
}
void on_copy(void)
{
}
void on_paste(void)
{
}
template <typename T>
void gen_sub_edit(T t)
{
Gtk::Menu* ptr_sub_menu = Gtk::manage(new Gtk::Menu());
t->set_submenu(*ptr_sub_menu);
std::vector< std::pair<Glib::ustring, T> > pv;
pv.push_back(std::make_pair("undo", &main_window::on_undo));
pv.push_back(std::make_pair("copy", &main_window::on_copy));
pv.push_back(std::make_pair("paste",&main_window::on_paste));
gen_sub_menu_item(ptr_sub_menu, pv);
}
void on_about(void)
{
}
void on_online(void)
{
}
void on_context(void)
{
}
template <typename T>
void gen_sub_help(T t)
{
Gtk::Menu* ptr_sub_menu = Gtk::manage(new Gtk::Menu());
t->set_submenu(*ptr_sub_menu);
std::vector< std::pair<Glib::ustring, T> > pv;
pv.push_back(std::make_pair("about", &main_window::on_about));
pv.push_back(std::make_pair("online", &main_window::on_online));
pv.push_back(std::make_pair("context",&main_window::on_context));
gen_sub_menu_item(ptr_sub_menu, pv);
}
void on_open(void)
{
}
void on_close(void)
{
}
void on_save(void)
{
}
template <typename T>
void gen_sub_file(T t)
{
Gtk::Menu* ptr_sub_menu = Gtk::manage(new Gtk::Menu());
t->set_submenu(*ptr_sub_menu);
std::vector< std::pair<Glib::ustring, T> > pv;
pv.push_back(std::make_pair("open", &main_window::on_open));
pv.push_back(std::make_pair("close", &main_window::on_close));
pv.push_back(std::make_pair("save",&main_window::on_save));
gen_sub_menu_item(ptr_sub_menu, pv);
}
void fill(int value)
{
Gtk::ListStore::iterator it = _ptr_list_model->append();
it->set_value(_my_table.auth, boost::lexical_cast<std::string>(value));
it->set_value(_my_table.age, value);
it->set_value(_my_table.sex,(value % 2 == 0));
}
private:
Gtk::TreeView _tree_view;
title_table _my_table;
Glib::RefPtr<Gtk::ListStore> _ptr_list_model;
Gtk::MenuBar _menu_bar;
Gtk::VBox _vbox;
typedef void (main_window::*T)();
};
int main(int argc, char* argv[])
{
Gtk::Main app(argc, argv);
main_window window;
app.run(window);
return 0;
}
#include <string>
#include <vector>
#include <algorithm>
#include <boost/lexical_cast.hpp>
#include <utility>
#include <functional>
class title_table:public Gtk::TreeModel::ColumnRecord
{
public:
title_table()
{
add(auth);
add(age);
add(sex);
}
Gtk::TreeModelColumn<std::string> auth;
Gtk::TreeModelColumn<int> age;
Gtk::TreeModelColumn<bool> sex;
};
class main_window:public Gtk::Window
{
public:
main_window()
{
add(_vbox);
fill_menu_bar();
fill_tree();
maximize();
show_all();
}
protected:
void fill_tree(void)
{
_ptr_list_model = Gtk::ListStore::create(_my_table);
_tree_view.set_model(_ptr_list_model);
std::vector<int> int_vector;
std::generate_n(back_inserter(int_vector), 10, rand);
std::for_each(int_vector.begin(), int_vector.end(), std::bind1st(std::mem_fun(&main_window::fill),this));
_tree_view.append_column("author", _my_table.auth);
_tree_view.append_column("age", _my_table.age);
_tree_view.append_column("sex", _my_table.sex);
_vbox.add(_tree_view);
}
void fill_menu_bar(void)
{
gen_sub_file(gen_menu_item("File"));
gen_sub_edit(gen_menu_item("Edit"));
gen_sub_view(gen_menu_item("View"));
gen_sub_help(gen_menu_item("Help"));
_vbox.add(_menu_bar);
}
Gtk::MenuItem* gen_menu_item(Glib::ustring name)
{
Gtk::MenuItem* ptr_menu_item = Gtk::manage(new Gtk::MenuItem());
ptr_menu_item->add_label(name);
_menu_bar.add(*ptr_menu_item);
return ptr_menu_item;
}
void on_image(void)
{
Gtk::MessageDialog* p_md = new Gtk::MessageDialog("this is image");
p_md->run();
p_md->hide_all();
}
void on_list(void)
{
Gtk::MessageDialog* p_md = new Gtk::MessageDialog("this is list");
p_md->run();
p_md->hide_all();
}
void on_text(void)
{
Gtk::MessageDialog* p_md = new Gtk::MessageDialog("this is text");
p_md->run();
p_md->hide_all();
}
template <typename T>
void gen_sub_view(T t)
{
Gtk::Menu* ptr_sub_menu = Gtk::manage(new Gtk::Menu());
t->set_submenu(*ptr_sub_menu);
std::vector<std::pair<Glib::ustring, T > > pv;
pv.push_back(std::make_pair("image", &main_window::on_image));
pv.push_back(std::make_pair("list", &main_window::on_list));
pv.push_back(std::make_pair("text", &main_window::on_text));
gen_sub_menu_item(ptr_sub_menu, pv);
}
template<typename parent, typename contain>
void gen_sub_menu_item(parent p, const contain& pair_vector)
{
Gtk::MenuItem* ptr_item;
typename contain::const_iterator it;
for (it = pair_vector.begin(); it != pair_vector.end(); ++it)
{
ptr_item = Gtk::manage(new Gtk::MenuItem());
ptr_item->add_label(it->first);
ptr_item->signal_activate().connect(sigc::mem_fun(*this, it->second));
p->add(*ptr_item);
}
}
void on_undo(void)
{
}
void on_copy(void)
{
}
void on_paste(void)
{
}
template <typename T>
void gen_sub_edit(T t)
{
Gtk::Menu* ptr_sub_menu = Gtk::manage(new Gtk::Menu());
t->set_submenu(*ptr_sub_menu);
std::vector< std::pair<Glib::ustring, T> > pv;
pv.push_back(std::make_pair("undo", &main_window::on_undo));
pv.push_back(std::make_pair("copy", &main_window::on_copy));
pv.push_back(std::make_pair("paste",&main_window::on_paste));
gen_sub_menu_item(ptr_sub_menu, pv);
}
void on_about(void)
{
}
void on_online(void)
{
}
void on_context(void)
{
}
template <typename T>
void gen_sub_help(T t)
{
Gtk::Menu* ptr_sub_menu = Gtk::manage(new Gtk::Menu());
t->set_submenu(*ptr_sub_menu);
std::vector< std::pair<Glib::ustring, T> > pv;
pv.push_back(std::make_pair("about", &main_window::on_about));
pv.push_back(std::make_pair("online", &main_window::on_online));
pv.push_back(std::make_pair("context",&main_window::on_context));
gen_sub_menu_item(ptr_sub_menu, pv);
}
void on_open(void)
{
}
void on_close(void)
{
}
void on_save(void)
{
}
template <typename T>
void gen_sub_file(T t)
{
Gtk::Menu* ptr_sub_menu = Gtk::manage(new Gtk::Menu());
t->set_submenu(*ptr_sub_menu);
std::vector< std::pair<Glib::ustring, T> > pv;
pv.push_back(std::make_pair("open", &main_window::on_open));
pv.push_back(std::make_pair("close", &main_window::on_close));
pv.push_back(std::make_pair("save",&main_window::on_save));
gen_sub_menu_item(ptr_sub_menu, pv);
}
void fill(int value)
{
Gtk::ListStore::iterator it = _ptr_list_model->append();
it->set_value(_my_table.auth, boost::lexical_cast<std::string>(value));
it->set_value(_my_table.age, value);
it->set_value(_my_table.sex,(value % 2 == 0));
}
private:
Gtk::TreeView _tree_view;
title_table _my_table;
Glib::RefPtr<Gtk::ListStore> _ptr_list_model;
Gtk::MenuBar _menu_bar;
Gtk::VBox _vbox;
typedef void (main_window::*T)();
};
int main(int argc, char* argv[])
{
Gtk::Main app(argc, argv);
main_window window;
app.run(window);
return 0;
}