PHP-GTK2 Newsletter

PHP-GTK2 Tips & Techniques
FREE Newsletter
by kksou



Sample Code 307: How to remove nodes from GtkTreeView - Part 3 - remove child nodes only?
Written by kksou   
Saturday, 25 August 2007
Problem

This is in response to Fernando's post titled 'Remove the nodes of a tree'.

Instead of remove the parent node+child nodes when the user presses the 'Delete' key, he would like to delete only the child nodes as shown below:

How to remove nodes from GtkTreeView - Part 3 - remove child nodes only?


Solution
  • Use $parent_node->children() to get all the first-level child nodes. Save these in an array.
  • Remove each of the first-level child node with unset($model[$iter]) (Please see Part 1 for more explanations).

Sample Code

Note 1: If you select "C:\", the program will run, but it will take a long time. You will see the processing, though, in the command window.

Note 2: On windows, you may see a warning "Gtk-WARNING: Could not find the icon 'stock_unknown'. The 'hicolor' theme was not found either, perhaps you need to install it." This is from the GtkFileChooserButton. It's just a warning. You may ignore it, or go ahead and install it.

Note 3: The following image files are required by the sample code below. Please save a copy of the image files and put them in the same directory where you store the sample code.

 folder_open.gif
 folder_closed.gif

1   
2   
4   
5   
6   
7   
8   
9   
10   
11   
12   
13   
14   
15   
16   
17   
18   
19   
20   
21   
22   
23   
24   
25   
26   
27   
28   
29   
30   
31   
32   
33   
34   
35   
37   
38   
39   
40   
41   
42   
43   
44   
45   
46   
47   
48   
49   
50   
51   
52   
57   
58   
59   
60   
61   
62   
63   
64   
65   
67   
68   
69   
70   
71   
73   
74   
75   
76   
77   
78   
79   
81   
82   
83   
84   
85   
87   
88   
89   
90   
91   
92   
93   
94   
95   
96   
97   
98   
99   
100   
101   
102   
103   
104   
105   
106   
107   
108   
109   
110   
111   
112   
113   
114   
115   
116   
117   
118   
119   
120   
121   
122   
123   
124   
125   
126   
127   
128   
129   
130   
131   
132   
133   
134   
135   
136   
137   
138   
139   
141   
142   
143   
144   
146   
147   
149   
150   
151   
154   
157   
162   
165   
166   
169   
173   
174   
175   
176   
177   
178   
179   
180   
181   
<?php
$window = new GtkWindow();
$window->connect_simple('destroy', array('Gtk','main_quit'));
$window->add($vbox = new GtkVBox());

// display title
$title = new GtkLabel("Remove nodes from GtkTreeView - Part 3\n".
"             remove child nodes only");
$title->modify_font(new PangoFontDescription("Times New Roman Italic 10"));
$title->modify_fg(Gtk::STATE_NORMAL, GdkColor::parse("#0000ff"));
$title->set_size_request(-1, 40);
$vbox->pack_start($title, 0, 0);

$vbox->pack_start($vbox2 = new GtkVBox(), 0, 0);
$vbox2->set_size_request(-1, 40);
$vbox2->pack_start(new GtkLabel("Highlight a row, and press ".
"'Delete' key to delete a file/folder"), 0);
$vbox2->pack_start(new GtkLabel("This is a demo. It won't ".
"actually delete the file/folder"), 0);

$vbox->pack_start($hbox = new GtkHBox());
$hbox->pack_start(new GtkLabel('Click to select a folder: '), 0, 0);

// set up model
if (defined("GObject::TYPE_STRING")) {
    $model = new GtkTreeStore(GObject::TYPE_STRING);
} else {
    $model = new GtkTreeStore(Gtk::TYPE_STRING);
}

// set up file chooser button
$file_chooser_button = new GtkFileChooserButton('Select the Folder',
    Gtk::FILE_CHOOSER_ACTION_SELECT_FOLDER);
$file_chooser_button->set_size_request(320, -1);
$file_chooser_button->connect('selection-changed', 'on_selection_changed');
on_selection_changed($file_chooser_button);
$hbox->pack_start($file_chooser_button, 0, 0);

// set up scroll window
$scrolled_win = new GtkScrolledWindow();
$scrolled_win->set_policy( Gtk::POLICY_AUTOMATIC,
    Gtk::POLICY_AUTOMATIC);
$vbox->pack_start($scrolled_win);

// set up treeview
$view = new GtkTreeView($model);
$scrolled_win->add($view);
$view->set_size_request(400, 240);

//set up treeview columns
$column = new GtkTreeViewColumn();

// for image
$cell_renderer = new GtkCellRendererPixbuf();
$column->pack_start($cell_renderer, false);
$cell_renderer->set_property('pixbuf-expander-open',
    GdkPixbuf::new_from_file('folder_open.gif'));
$cell_renderer->set_property('pixbuf-expander-closed',
    GdkPixbuf::new_from_file('folder_closed.gif'));

// for filename
$cell_renderer = new GtkCellRendererText();
$column->pack_start($cell_renderer, true);
$column->set_attributes($cell_renderer, 'text', 0);

$view->append_column($column);
$column->set_title('Folders / Files');

$view->connect('key-press-event', 'on_keypress');

//display it
$window->show_all();
Gtk::main();

function on_selection_changed($button) {
    $folder = $button->get_current_folder();
    echo "folder: $folder\n";
    populate_tree($folder);
}

function populate_tree($folder) {
    global $model;
    $model->clear();
    $root = $folder;
    $dir_list = array($root);
    $nodes = array();
    $nodes[$root] = null;
    while(count($dir_list)>0) {
        $dir = array_shift($dir_list);
        echo "folder = $dir\n";

        // add the directories first
        if ($handle = opendir($dir)) {
            while (false !== ($file = readdir($handle))) {
                if ($file != "." && $file != "..") {
                    $fullpath = $dir.'/'.$file;
                    if (is_dir($fullpath)) {
                        $nodes[$fullpath] = $model->append($nodes[$dir],
                            array($file));
                        array_push($dir_list, $fullpath);
                    }
                }
            }
            closedir($handle);
        }

        $num_files = 0;
        // then add the files
        if ($handle = opendir($dir)) {
            while (false !== ($file = readdir($handle))) {
                if ($file != "." && $file != "..") {
                    $fullpath = $dir.'/'.$file;
                    if (!is_dir($fullpath)) {
                        $nodes[$fullpath] = $model->append($nodes[$dir],
                            array($file));
                        ++$num_files;
                    }
                }
            }
            closedir($handle);
        }

        if ($num_files==0) $nodes[$fullpath] = $model->append($nodes[$dir],
            array(''));
    }
}

function on_keypress($widget, $event) {
    if ($event->keyval==Gdk::KEY_Delete) {
        global $view;
        $selection = $view->get_selection();
        list($model, $iter) = $selection->get_selected();
        if ($iter==NULL) return true;

        $path = $model->get_string_from_iter($iter);
        $parent_node = $model[$iter];
        $nodes_to_remove = array();
        foreach ($parent_node->children() as $child_node) { // note 1
            $iter = $child_node->iter; // note 2
            $nodes_to_remove[] = $model->get_string_from_iter($iter); // note 3
        }

        for ($i=count($nodes_to_remove)-1; $i>=0; --$i) { // note 4
            unset($model[$nodes_to_remove[$i]]); // note 5
        }

        $selection->select_path($path);
        return true;
    }
    return false;
}

?>
Explanation

The above code is an extension of the code from How to remove nodes from GtkTreeView - Part 2 - highlight remains? .

What's new here:

  1. Get all the first level of the child nodes.
  2. Get the corresponding iter.
  3. Store the corresponding path in the array $nodes_to_remove
  4. Note that we need to remove the paths in reverse order. Try removing the paths from $i=1 to count(nodes_to_remove) and you will know what I mean.
  5. Remove this node!

Related Links

User reviews

There are no user reviews yet.

Note: You have to be a registered member to leave a comment. Free registration here.

 
< Prev   Next >

Blog - Forum - Privacy Policy - Contact Us
Copyright © 2006-2009. kksou.com. All Rights Reserved