4

我正在使用 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");
        }
    }

这显示了两列(第一列是文件夹/文件图标,第二列是文件夹/文件的名称)

这是在一列中执行此操作的某种方法?

编辑:在名称旁边设置图标可能是一些技巧,实际代码显示图标和字符串,但是当我展开一列时,字符串向右移动一点,图标之间有一个空格和字符串。

4

1 回答 1

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);

它就在那里:)

于 2012-12-01T03:46:50.047 回答