[gtkmm] How to use Gtk::TreeModelSort
- From: Samuel Abels <spam debain org>
- To: gtkmm-list gnome org
- Subject: [gtkmm] How to use Gtk::TreeModelSort
- Date: Fri, 09 Apr 2004 21:04:43 +0200
Hello,
While trying to create a sorted TreeModel using this:
Glib::RefPtr<Gtk::TreeModelSort> sortstore;
store = Gtk::ListStore::create(columns);
sortstore = Gtk::TreeModelSort::create(store);
(complete code appended below)
the error message is:
------------------
filelist.cc: In constructor `GtkFileList::GtkFileList()':
filelist.cc:31: error: incomplete type 'Gtk::TreeModelSort' cannot be used to
name a scope
/usr/include/gtkmm-2.0/glibmm/refptr.h: In destructor `
Glib::RefPtr<T_CppObject>::~RefPtr() [with T_CppObject = Gtk::TreeModelSort]
':
filelist.cc:27: instantiated from here
/usr/include/gtkmm-2.0/glibmm/refptr.h:176: error: `unreference' undeclared
(first use this function)
/usr/include/gtkmm-2.0/glibmm/refptr.h:176: error: (Each undeclared identifier
is reported only once for each function it appears in.)
/usr/include/gtkmm-2.0/glibmm/refptr.h: In constructor `
Glib::RefPtr<T_CppObject>::RefPtr(const Glib::RefPtr<T_CastFrom>&) [with
T_CastFrom = Gtk::TreeModelSort, T_CppObject = Gtk::TreeModel]':
filelist.cc:36: instantiated from here
/usr/include/gtkmm-2.0/glibmm/refptr.h:206: error: cannot convert `
Gtk::TreeModelSort*' to `Gtk::TreeModel*' in initialization
------------------
I have also searched through the examples and through some gtkmm
programs but none seemed to use this.
I have searched for several hours but I am really lost. Only a link
would also be helpful.
I am using libgtkmm 2.2.10. A complete test program is appended below.
Thanks!
-Samuel
********************************************************
* filelist.h
********************************************************
#include <iostream>
#include <gtkmm.h>
#include <sys/stat.h>
#include <libintl.h>
#include <dirent.h>
#include <grp.h>
#include <pwd.h>
#define _(String) gettext (String)
#define gettext_noop(String) (String)
#define N_(String) gettext_noop (String)
class GtkFileList : public Gtk::TreeView {
public:
GtkFileList(void);
~GtkFileList(void);
/* Jump to the given directory. Returns TRUE on success, otherwise FALSE.
*/
bool set_directory(std::string dir);
/* Specify a list of patterns that represent filenames to be shown in the
* filelist.
*/
void set_filepattern(const gchar **pfilepattern);
/* Specify a comma-sperated list of patterns that represent filenames to be
* shown in the filelist.
*/
void set_filepattern(std::string str);
/* Specify whether or not hidden files and directories should be visible.
*/
void set_showhidden(gboolean show_hidden);
/* Triggers a refresh of the current directory.
*/
void update(void);
protected:
// List model columns.
class ModelColumns : public Gtk::TreeModel::ColumnRecord {
public:
Gtk::TreeModelColumn<Glib::ustring> filename;
Gtk::TreeModelColumn<Glib::ustring> size;
Gtk::TreeModelColumn<Glib::ustring> mode;
Gtk::TreeModelColumn<Glib::ustring> owner;
Gtk::TreeModelColumn<Glib::ustring> fullfilename;
ModelColumns() {
add(filename);
add(size);
add(mode);
add(owner);
add(fullfilename);
}
};
ModelColumns columns; // The column model.
Glib::RefPtr<Gtk::ListStore> store; // The filelist store.
Glib::RefPtr<Gtk::TreeModelSort> sortstore; // The sorting layer.
std::string curdir; // The current directory.
gboolean showhidden; // Show hidden files?
};
********************************************************
* filelist.cc
********************************************************
#include "filelist.h"
GtkFileList::GtkFileList(void)
{
showhidden = TRUE;
store = Gtk::ListStore::create(columns);
//FIXME: This line causes an error.
sortstore = Gtk::TreeModelSort::create(store);
get_selection()->set_mode(Gtk::SELECTION_EXTENDED);
// Attach the liststore to the treeview.
set_model(sortstore);
append_column(_("Filename"), columns.filename);
append_column(_("Size"), columns.size);
append_column(_("Mode"), columns.mode);
append_column(_("Owner"), columns.owner);
set_directory(getenv("HOME"));
}
GtkFileList::~GtkFileList(void)
{
}
/* Jump to the given directory. Returns TRUE on success, otherwise FALSE.
*/
bool GtkFileList::set_directory(std::string dir)
{
struct stat filestat;
if (dir.substr(dir.length(), 1) != "/") // Append a trailing slash.
dir = dir + "/";
stat(dir.c_str(), &filestat); // Make sure it is a directory.
if (!filestat.st_mode & S_IFDIR)
return FALSE;
curdir = dir;
update();
return TRUE;
}
/* Specify whether or not hidden files and directories should be visible.
*/
void GtkFileList::set_showhidden(gboolean show_hidden)
{
if (showhidden == show_hidden)
return;
showhidden = show_hidden;
update();
}
/* Triggers a refresh of the current directory.
* If "hard" is TRUE, the filelist is cleared and re-read (the selection is
* lost).
*/
void GtkFileList::update(void)
{
DIR *stream = NULL;
struct dirent *filestruct = NULL;
struct stat filestatus;
struct passwd *pwd;
struct group *grp;
std::string fullfilename;
char size[20];
char mode[30];
Gtk::TreeModel::Row row;
store->clear();
if (!(stream = opendir(curdir.c_str()))) { // Open the directory.
printf("GtkFileList::update(): Path open failed. %s\n",
curdir.c_str());
return;
}
while ((filestruct = readdir(stream)) != NULL) { // Walk through all files.
// Skip hidden files?
if (!showhidden && strncmp(filestruct->d_name, ".", 1) == 0)
continue;
// Create a full path.
fullfilename = curdir + filestruct->d_name;
// Stat the file.
if (stat(fullfilename.c_str(), &filestatus) == -1)
continue;
// Skip directories.
if (S_ISDIR(filestatus.st_mode) != 0)
continue;
// Create a human readable size and mode.
snprintf(size, 19, "%i kb", (gint)(filestatus.st_size / 1024));
snprintf(mode, 29, "%i%i%i", (gint)(filestatus.st_mode & 0700) >> 6,
(gint)(filestatus.st_mode & 0070) >> 3,
(gint)(filestatus.st_mode & 0007));
// Create a human readable owner.
pwd = getpwuid(filestatus.st_uid);
grp = getgrgid(filestatus.st_gid);
std::string owner;
owner.append((pwd && pwd->pw_name) ? pwd->pw_name : "?");
owner.append(":");
owner.append((grp && grp->gr_name) ? grp->gr_name : "?");
// Add the new row/file.
row = *store->append();
row[columns.filename] = filestruct->d_name;
row[columns.size] = size;
row[columns.mode] = mode;
row[columns.owner] = owner;
row[columns.fullfilename] = fullfilename;
}
closedir(stream);
}
********************************************************
* main.cc
********************************************************
#include <gtkmm.h>
#include <iostream>
#include "filelist.h"
class AppWindow : public Gtk::Window
{
public:
AppWindow();
virtual ~AppWindow() {};
protected:
virtual void on_button_press_event(void);
Gtk::VBox box;
Gtk::Button button;
GtkFileList filelist;
};
AppWindow::AppWindow()
: button(Gtk::Stock::CLOSE)
{
set_title("Sorted TreeView");
set_size_request(300, 300);
set_border_width(10);
box.set_border_width(2);
button.signal_clicked().connect(
SigC::slot(*this, &AppWindow::on_button_press_event));
// Pack and show all our widgets.
box.pack_start(filelist);
box.pack_start(button, Gtk::PACK_SHRINK);
add(box);
show_all();
filelist.update();
}
void AppWindow::on_button_press_event(void)
{
Gtk::Main::quit();
}
int main(int argc, char **argv)
{
Gtk::Main kit(argc, argv);
AppWindow window;
kit.run(window);
return 0;
}
--
------------------------------------------------------
| Samuel Abels | http://www.debain.org |
| spam ad debain dod org | knipknap ad jabber dod org |
------------------------------------------------------
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]