Skip to content

Commit

Permalink
gtk3: support GtkTreeIterCompareFunc
Browse files Browse the repository at this point in the history
GitHub: fix #596

Reported by Christopher L. Ramsey. Thanks!!!
  • Loading branch information
kou committed Oct 18, 2015
1 parent d1c3d94 commit c77eea2
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 0 deletions.
33 changes: 33 additions & 0 deletions gtk3/ext/gtk3/rb-gtk3.c
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,37 @@ rb_gtk3_tree_cell_data_func_callback(GtkTreeViewColumn *column,
rb_iter);
}

static gint
rb_gtk3_tree_iter_compare_func_callback(GtkTreeModel *model,
GtkTreeIter *iter1,
GtkTreeIter *iter2,
gpointer user_data)
{
RBGICallbackData *callback_data = user_data;
ID id_set_model;
VALUE rb_model;
VALUE rb_iter1;
VALUE rb_iter2;
VALUE rb_result;

CONST_ID(id_set_model, "model=");
rb_model = GOBJ2RVAL(model);

rb_iter1 = BOXED2RVAL(iter1, GTK_TYPE_TREE_ITER);
rb_funcall(rb_iter1, id_set_model, 1, rb_model);

rb_iter2 = BOXED2RVAL(iter2, GTK_TYPE_TREE_ITER);
rb_funcall(rb_iter2, id_set_model, 1, rb_model);

rb_result = rb_funcall(callback_data->rb_callback,
id_call,
3,
rb_model,
rb_iter1,
rb_iter2);
return NUM2INT(rb_result);
}

static void
rb_gtk3_tree_model_filter_modify_func_callback(GtkTreeModel *model,
GtkTreeIter *iter,
Expand Down Expand Up @@ -412,6 +443,8 @@ rb_gtk3_callback_finder(GIArgInfo *info)
return rb_gtk3_translate_func_callback;
} else if (name_equal(info, "TreeCellDataFunc")) {
return rb_gtk3_tree_cell_data_func_callback;
} else if (name_equal(info, "TreeIterCompareFunc")) {
return rb_gtk3_tree_iter_compare_func_callback;
} else if (name_equal(info, "TreeModelFilterModifyFunc")) {
return rb_gtk3_tree_model_filter_modify_func_callback;
} else if (name_equal(info, "TreeModelFilterVisibleFunc")) {
Expand Down
35 changes: 35 additions & 0 deletions gtk3/test/test-gtk-tree-sortable.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# Copyright (C) 2015 Ruby-GNOME2 Project Team
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA

class TestGtkTreeSortable < Test::Unit::TestCase
include GtkTestUtils

def setup
@model = Gtk::ListStore.new(String)
end

test "#set_sort_func" do
@model.append[0] = "abc"
@model.append[0] = "xyz"
@model.append[0] = "efg"
@model.set_sort_column_id(0, :ascending)
@model.set_sort_func(0) do |_model, iter1, iter2|
iter2[0] <=> iter1[0]
end
assert_equal(["xyz", "efg", "abc"],
@model.collect {|_model, _path, iter| iter[0]})
end
end

0 comments on commit c77eea2

Please sign in to comment.