我正在使用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");
}
}
它显示两列(首先是文件夹/文件图标,第二列是文件夹/文件的名称)
这是在一个单独的列中执行此操作的一些方法吗?
编辑:在名称的侧面设置图标可能有点骇人听闻,实际代码显示图标和字符串但是当我展开一列时,字符串会向右移动一点并且有一个空白区域图标和字符串之间。答案 0 :(得分:6)
使用TreeViewColumn的方法pack_start()
,我只需将任何单元格渲染器附加到列中。
(在C中,这就像http://developer.gnome.org/gtk/unstable/gtk-question-index.html(见5.3))
所以,刚刚修改过:
// 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);
使用:
// File icon
var pixbuf = new Gtk.CellRendererPixbuf();
column.set_title("");
column.pack_start(pixbuf, false);
column.add_attribute(pixbuf,"stock-id",0);
// The name of the file.
var cell = new Gtk.CellRendererText();
column.pack_start(cell, false);
column.add_attribute(cell,"text",1);
append_column (column);
它是:)