Debian Codegraph @

demos/gtk-demo/example_iconview.cc

raw
1 /* Icon View
2 *
3 * The Gtk::IconView widget is used to display and manipulate icons. It
4 * uses a Gtk::TreeModel for data storage, so the list store example
5 * might be helpful.
6 *
7 */
8
9 #include <gtkmm.h>
10 #include <iostream> //For std::cout.
11
12 class Example_IconView : public Gtk::Window
13 {
14 public:
15 Example_IconView();
16 ~Example_IconView() override;
17
18 protected:
19
20 void fill_store();
21
22 //Signal handlers:
23 virtual void on_button_up();
24 virtual void on_button_home();
25 virtual void on_iconview_item_activated(const Gtk::TreeModel::Path& path);
26
27 virtual int on_model_sort(const Gtk::TreeModel::iterator& a, const Gtk::TreeModel::iterator& b);
28
29 class ModelColumns : public Gtk::TreeModelColumnRecord
30 {
31 public:
32
33 Gtk::TreeModelColumn<std::string> path;
34 Gtk::TreeModelColumn<Glib::ustring> display_name;
35 Gtk::TreeModelColumn< Glib::RefPtr<Gdk::Pixbuf> > pixbuf;
36 Gtk::TreeModelColumn<bool> is_directory;
37
38 ModelColumns() { add(path); add(display_name); add(pixbuf); add(is_directory); }
39 };
40
41 const ModelColumns m_columns;
42
43 Glib::RefPtr<Gtk::ListStore> m_model;
44 Glib::RefPtr<Gdk::Pixbuf> m_refPixbufFile, m_refPixbufFolder;
45
46 std::string m_parent;
47
48 //Member widgets:
49 Gtk::Box m_VBox;
50 Gtk::Toolbar m_Toolbar;
51 Gtk::ToolButton m_ButtonUp, m_ButtonHome;
52 Gtk::ScrolledWindow m_ScrolledWindow;
53 Gtk::IconView m_IconView;
54 };
55
56 //Called by DemoWindow;
57 Gtk::Window* do_iconview()
58 {
59 return new Example_IconView();
60 }
61
62 Example_IconView::Example_IconView()
63 : m_VBox(Gtk::ORIENTATION_VERTICAL),
64 m_ButtonUp(),
65 m_ButtonHome()
66 {
67 m_ButtonUp.set_icon_name("go-up");
68 m_ButtonHome.set_icon_name("go-home");
69
70 set_title("Icon View");
71 set_default_size(650, 400);
72
73 try
74 {
75 m_refPixbufFile = Gdk::Pixbuf::create_from_resource("/iconview/gnome-fs-regular.png");
76 m_refPixbufFolder = Gdk::Pixbuf::create_from_resource("/iconview/gnome-fs-directory.png");
77 }
78 catch(const Glib::Error& error)
79 {
80 std::cout << error.what() << std::endl;
81 }
82
83 m_VBox.pack_start(m_Toolbar, Gtk::PACK_SHRINK);
84
85 m_ButtonUp.set_is_important();
86 m_ButtonUp.set_sensitive();
87 m_Toolbar.append(m_ButtonUp);
88
89 m_ButtonHome.set_is_important();
90 m_ButtonHome.set_sensitive();
91 m_Toolbar.append(m_ButtonHome);
92
93 m_ScrolledWindow.set_shadow_type(Gtk::SHADOW_ETCHED_IN);
94 m_ScrolledWindow.set_policy(Gtk::POLICY_AUTOMATIC, Gtk::POLICY_AUTOMATIC);
95
96 m_VBox.pack_start(m_ScrolledWindow, Gtk::PACK_EXPAND_WIDGET);
97
98 //Create the data model:
99 m_model = Gtk::ListStore::create(m_columns);
100 m_model->set_default_sort_func( sigc::mem_fun(*this, &Example_IconView::on_model_sort) );
101 m_model->set_sort_column(Gtk::TreeSortable::DEFAULT_SORT_COLUMN_ID, Gtk::SORT_ASCENDING);
102
103 /* and fill it with the contents of '/' */
104 m_parent = "/";
105 fill_store();
106
107 m_IconView.set_model(m_model);
108 m_IconView.set_selection_mode(Gtk::SELECTION_MULTIPLE);
109
110 //Connect signals:
111 m_ButtonUp.signal_clicked().connect( sigc::mem_fun(*this, &Example_IconView::on_button_up) );
112 m_ButtonHome.signal_clicked().connect( sigc::mem_fun(*this, &Example_IconView::on_button_home) );
113
114 /* We now set which model columns that correspont to the text
115 * and pixbuf of each item
116 */
117 m_IconView.set_text_column(m_columns.display_name);
118 m_IconView.set_pixbuf_column(m_columns.pixbuf);
119
120 m_IconView.signal_item_activated().connect( sigc::mem_fun(*this, &Example_IconView::on_iconview_item_activated) );
121
122 m_ScrolledWindow.add(m_IconView);
123
124 m_IconView.grab_focus();
125
126 add(m_VBox);
127
128 show_all();
129 }
130
131 int Example_IconView::on_model_sort(const Gtk::TreeModel::iterator& a, const Gtk::TreeModel::iterator& b)
132 {
133 /* We need this function because we want to sort
134 * folders before files.
135 */
136
137 Gtk::TreeModel::Row row_a = *a;
138 Gtk::TreeModel::Row row_b = *b;
139
140 const bool a_is_dir = row_a[m_columns.is_directory];
141 const bool b_is_dir = row_b[m_columns.is_directory];
142
143 if(!a_is_dir && b_is_dir)
144 return 1;
145 else if (a_is_dir && !b_is_dir)
146 return -1;
147 else
148 {
149 Glib::ustring name_a = row_a[m_columns.display_name];
150 return name_a.compare( row_b[m_columns.display_name] );
151 }
152 }
153
154
155 Example_IconView::~Example_IconView()
156 {
157 }
158
159 void Example_IconView::on_button_up()
160 {
161 m_parent = Glib::path_get_dirname(m_parent);
162 fill_store();
163
164 /* Maybe de-sensitize the up button */
165 m_ButtonUp.set_sensitive( m_parent == "/" );
166 }
167
168 void Example_IconView::on_button_home()
169 {
170 m_parent = Glib::get_home_dir();
171 fill_store();
172
173 /* De-sensitize the up button */
174 m_ButtonUp.set_sensitive(true);
175 }
176
177
178 void Example_IconView::fill_store()
179 {
180 /* First clear the store */
181 m_model->clear();
182
183 /* Now go through the directory and extract all the file
184 * information */
185 try
186 {
187 Glib::Dir dir(m_parent); //throws an exception if it fails.
188
189 std::string name = dir.read_name();
190 while(!name.empty())
191 {
192 /* We ignore hidden files that start with a '.' */
193 if (name[0] != '.')
194 {
195 std::string path = Glib::build_filename(m_parent, name);
196 bool is_dir = Glib::file_test(path, Glib::FILE_TEST_IS_DIR);
197
198 Glib::ustring display_name = Glib::filename_to_utf8(name);
199
200 Gtk::TreeModel::iterator iter = m_model->append();
201 Gtk::TreeModel::Row row = *iter;
202 row[m_columns.path] = path;
203 row[m_columns.display_name] = display_name;
204 row[m_columns.is_directory] = is_dir;
205 row[m_columns.pixbuf] = (is_dir ? m_refPixbufFolder : m_refPixbufFile);
206 }
207
208 name = dir.read_name();
209 }
210 }
211 catch(const Glib::FileError& ex)
212 {
213 std::cout << ex.what() << std::endl;
214 }
215 }
216
217
218 void Example_IconView::on_iconview_item_activated(const Gtk::TreeModel::Path& path)
219 {
220 Gtk::TreeModel::iterator iter = m_model->get_iter(path);
221 if(iter)
222 {
223 Gtk::TreeModel::Row row = *iter;
224 const bool is_dir = row[m_columns.is_directory];
225 const std::string filepath = row[m_columns.path];
226 if(is_dir)
227 {
228 m_parent = filepath;
229 fill_store();
230
231 /* Sensitize the up button */
232 m_ButtonUp.set_sensitive();
233 }
234 }
235 }
236