libcrn  3.9.5
A document image processing library
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
GtkCRNFileSelecter.cpp
Go to the documentation of this file.
1 /* Copyright 2010-2014 CoReNum, INSA-Lyon
2  *
3  * This file is part of libgtkcrnmm.
4  *
5  * libgtkcrnmm is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU Lesser General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * libgtkcrnmm is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public License
16  * along with libgtkcrnmm. If not, see <http://www.gnu.org/licenses/>.
17  *
18  * file: GtkCRNFileSelecter.cpp
19  * \author Yann LEYDIER
20  */
21 
22 #include <libgtkcrnmm_config.h>
23 #include <CRNi18n.h>
24 #include <GtkCRNFileSelecter.h>
25 #include <CRNIO/CRNIO.h>
26 #include <CRNData/CRNForeach.h>
27 
28 using namespace GtkCRN;
29 
34  path(p)
35 {
36  pathdisplay.show();
37  pathdisplay.set_sensitive(false);
38  pack_start(pathdisplay, false, true, 0);
39 
40  Gtk::ScrolledWindow *sw = Gtk::manage(new Gtk::ScrolledWindow);
41  sw->show();
42  pack_start(*sw, true, true, 0);
43 
44  path.ToLocal();
45  files = Gtk::ListStore::create(columns);
46  tv.set_model(files);
47  tv.append_column(_("Filename"), columns.name);
48  Gtk::TreeView::Column *col = tv.get_column(0);
49  if (col)
50  col->set_sort_column(columns.name);
51  tv.signal_row_activated().connect(sigc::mem_fun(this, &FileSelecter::on_row_activated));
52  tv.show();
53  sw->add(tv);
54  update_files();
55 }
56 
61 {
62  path = p;
63  path.ToLocal();
64  update_files();
65 }
66 
72 {
73  for (Gtk::TreeIter it : files->children())
74  {
75  if (it->get_value(columns.name).c_str() == p)
76  {
77  tv.get_selection()->select(it);
78  return true;
79  }
80  }
81  return false;
82 }
83 
88 {
89  if (tv.get_selection()->count_selected_rows() == 0)
90  return "";
91  Gtk::TreeIter it(tv.get_selection()->get_selected());
92  return it->get_value(columns.name).c_str();
93 }
94 
96 void FileSelecter::update_files()
97 {
98  pathdisplay.set_text(path.CStr());
99  files->clear();
100  crn::IO::Directory dir(path);
101  std::vector<crn::Path> dirfiles(dir.GetFiles());
102  for (const crn::Path &file : dirfiles)
103  {
104  Gtk::TreeIter it(files->append());
105  it->set_value(columns.name, Glib::ustring(file.GetFilename().CStr()));
106  }
107  tv.get_selection()->select(files->children()[0]);
108 }
109 
111 void FileSelecter::on_selection_changed()
112 {
113  selection_changed.emit(get_selection());
114 }
115 
120 void FileSelecter::on_row_activated(const Gtk::TreeModel::Path& path, Gtk::TreeViewColumn* column)
121 {
122  selection_activated.emit(get_selection());
123 }
#define _(String)
Definition: CRNi18n.h:51
crn::Path get_selection() const
Gets the selected file.
A handler to the content of a directory.
Definition: CRNIO.h:116
bool set_selection(const crn::Path &p)
Sets the selected file.
const char * CStr() const noexcept
Conversion to UTF8 cstring.
A convenience class for file paths.
Definition: CRNPath.h:39
FileSelecter(const crn::Path &p)
Constructor.
void set_path(const crn::Path &p)
Sets the path of the directory to display.
Path & ToLocal()
Converts the path to the local format.
Definition: CRNPath.cpp:534