Как сделать столбец со строкой и pixbuf в GtkTreeview?

Я работаю в приложении с 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");
        }
    }

Это показывает два столбца (первый со значком папки/файла, а второй - имя папки/файла)

это какой-то способ сделать это в одном столбце ??

РЕДАКТИРОВАТЬ: это может быть какой-то хак, чтобы установить значок рядом с именем, фактический код показывает значок и строку, но когда я расширяю столбец, строки перемещаются немного вправо, и между значком есть пустое пространство и струна.


person Matias    schedule 30.11.2012    source источник


Ответы (1)


С помощью метода 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);

И вот оно :)

person Matias    schedule 01.12.2012