Я работаю в приложении с Gtk + 2, и мне нужно реализовать дерево файлов.
фактический код:
public FileTree() {
store = new TreeStore(2,typeof(string),typeof(string));
this.change_dir( "/dir/path" );
set_model( store );
// File icon
var pixbuf = new Gtk.CellRendererPixbuf();
var column = new Gtk.TreeViewColumn();
column.set_title("");
column.pack_start(pixbuf, false);
column.add_attribute(pixbuf,"stock-id",0);
column.set_alignment(1.0f);
append_column (column);
// File name
Gtk.CellRenderer cell = new Gtk.CellRendererText();
insert_column_with_attributes(-1,"", cell, "text", 1);
// Do some visual configs
this.config();
}
и change_dir()
:
public void change_dir( string path ) {
File repo_dir = File.new_for_path( path );
try {
generate_list( repo_dir, null, new Cancellable());
} catch ( Error e ) {
stderr.printf("Error: %s\n", e.message);
}
}
public void generate_list (
File file,
TreeIter? parent = null,
Cancellable? cancellable = null
) throws Error {
// Enumerator
FileEnumerator enumerator = file.enumerate_children (
"standard::*",
FileQueryInfoFlags.NOFOLLOW_SYMLINKS,
cancellable
);
FileInfo info = null;
TreeIter iter;
while(cancellable.is_cancelled() == false && ((info = enumerator.next_file(cancellable)) != null ))
{
// Check if not it's in the omited files.
if( ! (info.get_name() in IGNORED ) ) {
// Check if is a dir or a file
if( info.get_file_type() == FileType.DIRECTORY ) {
this.store.append( out iter, parent);
this.store.set(iter, 0, STOCK_DIRECTORY, 1, info.get_name());
File subdir = file.resolve_relative_path(info.get_name());
this.generate_list(subdir, iter, cancellable );
} else {
// It's a file
this.store.append( out iter, parent);
this.store.set(iter, 0, STOCK_FILE, 1, info.get_name());
}
}
}
if ( cancellable.is_cancelled()) {
throw new IOError.CANCELLED ("Operation was cancelled");
}
}
Это показывает два столбца (первый со значком папки/файла, а второй - имя папки/файла)
это какой-то способ сделать это в одном столбце ??
РЕДАКТИРОВАТЬ: это может быть какой-то хак, чтобы установить значок рядом с именем, фактический код показывает значок и строку, но когда я расширяю столбец, строки перемещаются немного вправо, и между значком есть пустое пространство и струна.