diff --git a/Gtkmm3/gtk113_file_gtkmm/src/FileWindow.cc b/Gtkmm3/gtk113_file_gtkmm/src/FileWindow.cc index 244663e..998debd 100644 --- a/Gtkmm3/gtk113_file_gtkmm/src/FileWindow.cc +++ b/Gtkmm3/gtk113_file_gtkmm/src/FileWindow.cc @@ -1,7 +1,9 @@ #include "FileWindow.hh" +#include FileWindow::FileWindow() -:vbox(Gtk::ORIENTATION_VERTICAL,5), +:parent_str("/"), +vbox(Gtk::ORIENTATION_VERTICAL,5), btnbox(Gtk::ORIENTATION_HORIZONTAL,5) { //Initalize Window @@ -55,5 +57,106 @@ btnbox(Gtk::ORIENTATION_HORIZONTAL,5) m_viewbar.insert(menu_item,-1); m_viewbar.set_toolbar_style(Gtk::TOOLBAR_ICONS); + //Create Store + m_liststore = Gtk::ListStore::create(columns); + m_liststore->set_default_sort_func(sigc::mem_fun(*this,&FileWindow::sort_func)); + m_liststore->set_sort_column(-1,Gtk::SORT_ASCENDING); + fill_store(); + + create_views(); + vbox.pack_start(m_sw); + m_sw.set_policy(Gtk::POLICY_AUTOMATIC,Gtk::POLICY_AUTOMATIC); + m_sw.add(stack); + show_all_children(); } + +int FileWindow::sort_func(const Gtk::TreeModel::iterator &a,const Gtk::TreeModel::iterator &b){ + bool is_dir_a,is_dir_b; + Glib::ustring name_a,name_b; + + auto row_a=*a,row_b=*b; + is_dir_a = row_a[columns.m_col_is_dir]; + is_dir_b = row_b[columns.m_col_is_dir]; + name_a = row_a[columns.m_col_display_name]; + name_b = row_b[columns.m_col_display_name]; + + if(!is_dir_a && is_dir_b){ + return 1; + } + if(is_dir_a && !is_dir_b){ + return -1; + } + if(name_a[0] != '.' && name_b[0] == '.'){ + return 1; + } + if(name_a[0] == '.' && name_b[0] != '.'){ + return -1; + }else{ + return g_utf8_collate(name_a.c_str(),name_b.c_str()); + } +} + +void FileWindow::fill_store(){ + Gtk::TreeModel::iterator iter; + + //Clear the store + m_liststore->clear(); + + //Go through the directory and get information + try{ + Glib::Dir dir1(parent_str); + bool is_dir; + Glib::ustring display_name; + std::string dir_name; + /* Ignore the files start with '.' when the button is not toggled */ + do{ + dir_name = dir1.read_name(); + //std::cout<append()); + row[columns.m_col_display_name] = display_name; + row[columns.m_col_is_dir] = is_dir; + row[columns.m_col_path] = std::string(path); + row[columns.m_col_pixbuf] = is_dir ? folder_pixbuf : file_pixbuf; + } + + g_free(path); + + }while(dir_name != ""); + } + catch(const Glib::Error &ex){ + std::cout << ex.what() << std::endl; + } +} + +void FileWindow::create_views(){ + //Initalize IconView + m_iconview.set_model(m_liststore); + m_iconview.set_text_column(columns.m_col_display_name); + m_iconview.set_pixbuf_column(columns.m_col_pixbuf); + m_iconview.signal_item_activated().connect(sigc::mem_fun(*this,&FileWindow::item_activated)); + + stack.add(m_iconview); + stack.add(m_treeview); +} + +void FileWindow::item_activated(const Gtk::TreePath &path){ + auto row = *(m_liststore->get_iter(path)); + bool is_dir; + Glib::ustring path_name; + + path_name = row[columns.m_col_path]; + is_dir = row[columns.m_col_is_dir]; + + if(!is_dir){return ;} + + parent_str = path_name; + + fill_store(); +} diff --git a/Gtkmm3/gtk113_file_gtkmm/src/FileWindow.hh b/Gtkmm3/gtk113_file_gtkmm/src/FileWindow.hh index 9aa4f03..1cc4979 100644 --- a/Gtkmm3/gtk113_file_gtkmm/src/FileWindow.hh +++ b/Gtkmm3/gtk113_file_gtkmm/src/FileWindow.hh @@ -23,6 +23,7 @@ private: Gtk::TreeModelColumn m_col_is_dir; }; + ModelColumns columns; Gtk::TreeView m_treeview; Gtk::IconView m_iconview; Glib::RefPtr m_selection; @@ -42,6 +43,13 @@ private: Gtk::ScrolledWindow m_sw; Gtk::Button view_button; Gtk::MenuButton menubtn; + Gtk::Stack stack; + + //Initalize Functions + void fill_store(); + void create_views(); + int sort_func(const Gtk::TreeModel::iterator &a,const Gtk::TreeModel::iterator &b); //Signal Handlers + void item_activated(const Gtk::TreePath &path); }; \ No newline at end of file diff --git a/cpp/xeinit8.cpp b/cpp/xeinit8.cpp new file mode 100644 index 0000000..770d825 --- /dev/null +++ b/cpp/xeinit8.cpp @@ -0,0 +1,12 @@ +#include +#include +#include + +int main(int argc,char *argv[]) +{ + int x; + srand((unsigned)time(NULL)); + x=rand(); + std::cout<