File: rbgtk-tree-modelfilter.c

package info (click to toggle)
ruby-gnome 3.4.3-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 29,112 kB
  • sloc: ansic: 97,029; ruby: 70,747; xml: 350; sh: 142; cpp: 45; makefile: 29
file content (197 lines) | stat: -rw-r--r-- 5,987 bytes parent folder | download | duplicates (6)
1
2
3
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
/* -*- c-file-style: "ruby"; indent-tabs-mode: nil -*- */
/*
 *  Copyright (C) 2011  Ruby-GNOME2 Project Team
 *  Copyright (C) 2004,2005 Masao Mutoh
 *
 *  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
 */

#include "rbgtk3private.h"

#define RG_TARGET_NAMESPACE cTreeModelFilter
#define _SELF(s) (RVAL2GTKTREEMODELFILTER(s))

static ID id_child_model;
static ID id_root;

static VALUE
rg_initialize(int argc, VALUE *argv, VALUE self)
{
    VALUE child_model, root;
    GtkTreeModel* widget;

    rb_scan_args(argc, argv, "11", &child_model, &root);

    G_CHILD_SET(self, id_child_model, child_model);
    if (NIL_P(root)){
        widget = gtk_tree_model_filter_new(RVAL2GTKTREEMODEL(child_model), 
                                           (GtkTreePath*)NULL);
    } else {
        G_CHILD_SET(self, id_root, root);
        widget = gtk_tree_model_filter_new(RVAL2GTKTREEMODEL(child_model), 
                                           (GtkTreePath*)RVAL2GTKTREEPATH(root));
    }

    G_INITIALIZE(self, widget);
    return Qnil;
}

static gboolean
visible_func(GtkTreeModel *model, GtkTreeIter *iter, gpointer func)
{
    VALUE ret;

    iter->user_data3 = model;
    ret = rb_funcall((VALUE)func, id_call, 2, GOBJ2RVAL(model), GTKTREEITER2RVAL(iter));
    return RVAL2CBOOL(ret);
}

static VALUE
rg_set_visible_func(VALUE self)
{
    VALUE func = rb_block_proc();
    G_RELATIVE(self, func);
    gtk_tree_model_filter_set_visible_func(_SELF(self), 
                                           (GtkTreeModelFilterVisibleFunc)visible_func, 
                                           (gpointer)func, NULL);
    return self;
}

static void
modify_func(GtkTreeModel *model, GtkTreeIter *iter, GValue *value, gint column, gpointer func)
{
    VALUE ret;
    iter->user_data3 = model;
    ret = rb_funcall((VALUE)func, id_call, 3, GOBJ2RVAL(model), GTKTREEITER2RVAL(iter),
                     INT2NUM(column));
    rbgobj_rvalue_to_gvalue(ret, value);
}

/*
 * Should return value
 *
 * e.g. 
 * filter.set_modify_func(String) do |model, iter, column|
 *   "foo"
 * end
 */
static VALUE
rg_set_modify_func(int argc, VALUE *argv, VALUE self)
{
    VALUE func = rb_block_proc();
    gint i;
    GType* types;

    if (argc == 0) rb_raise(rb_eArgError, "need more than 1 class type.");

    types = ALLOCA_N(GType, argc);  

    G_RELATIVE(self, func);

    for (i = 0; i < argc; i++){
        types[i] = CLASS2GTYPE(argv[i]);
    }
    gtk_tree_model_filter_set_modify_func(_SELF(self),
                                          argc, types, 
                                          (GtkTreeModelFilterModifyFunc)modify_func,
                                          (gpointer)func, NULL);
    return self;
}

static VALUE
rg_set_visible_column(VALUE self, VALUE column)
{
    gtk_tree_model_filter_set_visible_column(_SELF(self), NUM2INT(column));
    return self;
}

static VALUE
rg_model(VALUE self)
{
    return GOBJ2RVAL(gtk_tree_model_filter_get_model(_SELF(self)));
}

static VALUE
rg_convert_child_iter_to_iter(VALUE self, VALUE child_iter)
{
    GtkTreeIter filter_iter;
    GtkTreeModelFilter* modelfilter = _SELF(self);
    gtk_tree_model_filter_convert_child_iter_to_iter(modelfilter, &filter_iter,
                                                     RVAL2GTKTREEITER(child_iter));
    filter_iter.user_data3 = gtk_tree_model_filter_get_model(modelfilter);
    return GTKTREEITER2RVAL(&filter_iter);
}

static VALUE
rg_convert_iter_to_child_iter(VALUE self, VALUE filtered_iter)
{
    GtkTreeIter child_iter;
    GtkTreeModelFilter* modelfilter = _SELF(self);
    gtk_tree_model_filter_convert_iter_to_child_iter(modelfilter, &child_iter,
                                                     RVAL2GTKTREEITER(filtered_iter));
    child_iter.user_data3 = gtk_tree_model_filter_get_model(modelfilter);
    return GTKTREEITER2RVAL(&child_iter);
} 

static VALUE
rg_convert_child_path_to_path(VALUE self, VALUE child_path)
{
    return GTKTREEPATH2RVAL(gtk_tree_model_filter_convert_child_path_to_path(
                             _SELF(self),
                             RVAL2GTKTREEPATH(child_path)));
}
static VALUE
rg_convert_path_to_child_path(VALUE self, VALUE filter_path)
{
    return GTKTREEPATH2RVAL(gtk_tree_model_filter_convert_path_to_child_path(
                             _SELF(self),
                             RVAL2GTKTREEPATH(filter_path)));
}

static VALUE
rg_refilter(VALUE self)
{
    gtk_tree_model_filter_refilter(_SELF(self));
    return self;
}

static VALUE
rg_clear_cache(VALUE self)
{
    gtk_tree_model_filter_clear_cache(_SELF(self));
    return self;
}

void
Init_gtk_treemodelfilter(VALUE mGtk)
{
    VALUE RG_TARGET_NAMESPACE = G_DEF_CLASS(GTK_TYPE_TREE_MODEL_FILTER, "TreeModelFilter", mGtk);

    id_child_model = rb_intern("child_model");
    id_root = rb_intern("root");

    RG_DEF_METHOD(initialize, -1);
    RG_DEF_METHOD(set_visible_func, 0);
    RG_DEF_METHOD(set_modify_func, -1);
    RG_DEF_METHOD(set_visible_column, 1);
    RG_DEF_METHOD(model, 0);
    RG_DEF_METHOD(convert_child_iter_to_iter, 1);
    RG_DEF_METHOD(convert_iter_to_child_iter, 1);
    RG_DEF_METHOD(convert_child_path_to_path, 1);
    RG_DEF_METHOD(convert_path_to_child_path, 1);
    RG_DEF_METHOD(refilter, 0);
    RG_DEF_METHOD(clear_cache, 0);
}