1/* -*- Mode: C; c-basic-offset: 4 -*-
2 * pygtk- Python bindings for the GTK toolkit.
3 * Copyright (C) 2005  John Finlay, Johan Dahlin
4 *
5 *   gtkiconview.override: overrides for the gtk.IconView object.
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
20 * USA
21 */
22%%
23ignore
24  gtk_icon_view_new_with_model
25%%
26new-constructor GTK_TYPE_ICON_VIEW
27%%
28override gtk_icon_view_new kwargs
29static int
30_wrap_gtk_icon_view_new(PyGObject *self, PyObject *args, PyObject *kwargs)
31{
32    static char *kwlist[] = { "model", NULL };
33    PyGObject *pymodel = NULL;
34
35    if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|O:GtkIconView.__init__",
36				     kwlist, &pymodel))
37        return -1;
38    if (pymodel == NULL || (PyObject *)pymodel == Py_None)
39        pygobject_construct(self, NULL);
40    else if (pygobject_check(pymodel, &PyGtkTreeModel_Type))
41        pygobject_construct(self, "model", GTK_TREE_MODEL(pymodel->obj), NULL);
42    else {
43	PyErr_SetString(PyExc_TypeError,
44			"model must be a gtk.TreeModel or None");
45	return -1;
46    }
47
48    if (!self->obj) {
49        PyErr_SetString(PyExc_RuntimeError,
50			"could not create GtkIconView object");
51        return -1;
52    }
53    return 0;
54}
55%%
56override gtk_icon_view_get_selected_items noargs
57static PyObject *
58_wrap_gtk_icon_view_get_selected_items(PyGObject *self)
59{
60    GList *paths;
61    PyObject *py_paths;
62    int plen, i;
63
64    paths = gtk_icon_view_get_selected_items(GTK_ICON_VIEW(self->obj));
65
66    plen = g_list_length(paths);
67    if ((py_paths = PyList_New(plen)) == NULL)
68        return NULL;
69    for (i = 0; i < plen; i++) {
70        GtkTreePath *path = (GtkTreePath *)g_list_nth_data(paths, i);
71        PyObject *pypath = pygtk_tree_path_to_pyobject(path);
72        PyList_SET_ITEM(py_paths, i, pypath);
73    }
74    g_list_foreach(paths, (GFunc)gtk_tree_path_free, NULL);
75    g_list_free(paths);
76    return py_paths;
77}
78%%
79override gtk_icon_view_selected_foreach kwargs
80static void
81pygtk_icon_view_selected_foreach_cb(GtkIconView *iconview,
82                                    GtkTreePath *path,
83                                    gpointer user_data)
84{
85    PyGILState_STATE state;
86    PyObject *callback, *args, *ret;
87
88    state = pyg_gil_state_ensure();
89    callback = PyTuple_GetItem((PyObject *)user_data, 0);
90    args = Py_BuildValue("(NNO)",
91                         pygobject_new((GObject *)iconview),
92                         pygtk_tree_path_to_pyobject(path),
93                         PyTuple_GetItem((PyObject *)user_data, 1));
94    ret = PyObject_CallObject(callback, args);
95    if (!ret)
96        PyErr_Print();
97    Py_XDECREF(ret);
98    Py_DECREF(args);
99    pyg_gil_state_release(state);
100}
101static PyObject *
102_wrap_gtk_icon_view_selected_foreach(PyGObject *self, PyObject *args,
103                                     PyObject *kwargs)
104{
105    static char *kwlist[] = { "func", "data",  NULL };
106    PyObject *func, *data, *py_data = Py_None;
107
108    if (!PyArg_ParseTupleAndKeywords(args, kwargs,
109                                     "O|O:GtkIconView.foreach",
110                                     kwlist, &func, &py_data)) {
111        return NULL;
112    }
113    if (!PyCallable_Check(func)) {
114        PyErr_SetString(PyExc_TypeError, "func must be callable");
115        return NULL;
116    }
117
118    data = Py_BuildValue("(OO)", func, py_data);
119
120    gtk_icon_view_selected_foreach(GTK_ICON_VIEW(self->obj),
121                                   pygtk_icon_view_selected_foreach_cb,
122                                   (gpointer)data);
123    Py_DECREF(data);
124    Py_INCREF(Py_None);
125    return Py_None;
126}
127%%
128override gtk_icon_view_get_item_at_pos kwargs
129static PyObject *
130_wrap_gtk_icon_view_get_item_at_pos(PyGObject *self, PyObject *args,
131				    PyObject *kwargs)
132{
133    static char *kwlist[] = { "x", "y", NULL };
134    GtkTreePath *path;
135    GtkCellRenderer *renderer;
136    gint x,y;
137    gboolean r;
138
139    if (!PyArg_ParseTupleAndKeywords(args, kwargs,
140                                     "ii:GtkIconView.get_item_at_pos",
141                                     kwlist,
142                                     &x, &y))
143        return NULL;
144
145
146    r = gtk_icon_view_get_item_at_pos(GTK_ICON_VIEW(self->obj), x, y, &path, &renderer);
147    if (r && path) {
148        PyObject *py_path = pygtk_tree_path_to_pyobject(path);
149        gtk_tree_path_free(path);
150        return Py_BuildValue("(NN)", py_path, pygobject_new((GObject*)renderer));
151    }
152
153    Py_INCREF(Py_None);
154    return Py_None;
155}
156%%
157override gtk_icon_view_get_visible_range noargs
158static PyObject *
159_wrap_gtk_icon_view_get_visible_range(PyGObject *self)
160{
161    GtkTreePath *start_path, *end_path;
162    gboolean r;
163
164    r = gtk_icon_view_get_visible_range(GTK_ICON_VIEW(self->obj),
165					&start_path, &end_path);
166    if (r) {
167        PyObject *py_start_path = pygtk_tree_path_to_pyobject(start_path);
168        PyObject *py_end_path = pygtk_tree_path_to_pyobject(end_path);
169        gtk_tree_path_free(start_path);
170        gtk_tree_path_free(end_path);
171       return Py_BuildValue("(NN)", py_start_path, py_end_path);
172    }
173
174    Py_INCREF(Py_None);
175    return Py_None;
176}
177%%
178override gtk_icon_view_get_cursor noargs
179static PyObject *
180_wrap_gtk_icon_view_get_cursor(PyGObject *self)
181{
182    GtkTreePath *path;
183    GtkCellRenderer *renderer;
184    gboolean r;
185
186    r = gtk_icon_view_get_cursor(GTK_ICON_VIEW(self->obj), &path, &renderer);
187    if (r && path) {
188        PyObject *py_path = pygtk_tree_path_to_pyobject(path);
189        gtk_tree_path_free(path);
190        return Py_BuildValue("(NN)", py_path, pygobject_new((GObject*)renderer));
191    }
192
193    Py_INCREF(Py_None);
194    return Py_None;
195}
196%%
197override gtk_icon_view_get_drag_dest_item noargs
198static PyObject *
199_wrap_gtk_icon_view_get_drag_dest_item(PyGObject *self)
200{
201    GtkTreePath *path;
202    GtkIconViewDropPosition pos;
203
204    gtk_icon_view_get_drag_dest_item(GTK_ICON_VIEW(self->obj), &path, &pos);
205    if (path) {
206        PyObject *py_path = pygtk_tree_path_to_pyobject(path);
207        gtk_tree_path_free(path);
208        return Py_BuildValue("(NN)", py_path,
209			     pyg_enum_from_gtype(GTK_TYPE_ICON_VIEW_DROP_POSITION, (gint)pos));
210    }
211
212    Py_INCREF(Py_None);
213    return Py_None;
214}
215%%
216override gtk_icon_view_get_dest_item_at_pos kwargs
217static PyObject *
218_wrap_gtk_icon_view_get_dest_item_at_pos(PyGObject *self, PyObject *args,
219					 PyObject *kwargs)
220{
221    static char *kwlist[] = { "x", "y", NULL };
222    GtkTreePath *path;
223    GtkIconViewDropPosition pos;
224    gint drag_x, drag_y;
225    gboolean r;
226
227    if (!PyArg_ParseTupleAndKeywords(args, kwargs,
228                                     "ii:GtkIconView.get_dest_item_at_pos",
229                                     kwlist, &drag_x, &drag_y))
230        return NULL;
231
232
233    r = gtk_icon_view_get_dest_item_at_pos(GTK_ICON_VIEW(self->obj),
234					   drag_x, drag_y, &path, &pos);
235    if (r && path) {
236        PyObject *py_path = pygtk_tree_path_to_pyobject(path);
237        gtk_tree_path_free(path);
238        return Py_BuildValue("(NN)", py_path,
239			     pyg_enum_from_gtype(GTK_TYPE_ICON_VIEW_DROP_POSITION, (gint) pos));
240    }
241
242    Py_INCREF(Py_None);
243    return Py_None;
244}
245%%
246override gtk_icon_view_enable_model_drag_source kwargs
247static PyObject *
248_wrap_gtk_icon_view_enable_model_drag_source(PyGObject *self, PyObject *args,
249                                             PyObject *kwargs)
250{
251    static char *kwlist[] = { "start_button_mask", "targets", "actions", NULL };
252    PyObject *py_sbmask, *py_targets, *py_actions;
253    GdkModifierType sbmask;
254    GtkTargetEntry *targets;
255    GdkDragAction actions;
256    gint n_targets, i;
257
258    if (!PyArg_ParseTupleAndKeywords(args, kwargs,
259                                     "OOO:GtkIconView.enable_model_drag_source",
260                                     kwlist,
261                                     &py_sbmask, &py_targets, &py_actions))
262        return NULL;
263    if (pyg_flags_get_value(GDK_TYPE_MODIFIER_TYPE,
264                             py_sbmask, (gint *)&sbmask))
265        return NULL;
266    if (pyg_flags_get_value(GDK_TYPE_DRAG_ACTION,
267                             py_actions, (gint *)&actions))
268        return NULL;
269    if (!PySequence_Check(py_targets)) {
270        PyErr_SetString(PyExc_TypeError, "targets must be a sequence");
271        return NULL;
272    }
273    n_targets = PySequence_Length(py_targets);
274    targets = g_new(GtkTargetEntry, n_targets);
275    for (i = 0; i < n_targets; i++) {
276        PyObject *item = PySequence_GetItem(py_targets, i);
277        Py_DECREF(item);
278        if (!PyArg_ParseTuple(item, "zii", &targets[i].target,
279                              &targets[i].flags, &targets[i].info)) {
280            PyErr_Clear();
281            PyErr_SetString(PyExc_TypeError,
282                            "list items should be of form (string,int,int)");
283            g_free(targets);
284            return NULL;
285        }
286    }
287    gtk_icon_view_enable_model_drag_source(GTK_ICON_VIEW(self->obj),
288                                           sbmask, targets, n_targets, actions);
289    g_free(targets);
290    Py_INCREF(Py_None);
291    return Py_None;
292}
293%%
294override gtk_icon_view_enable_model_drag_dest kwargs
295static PyObject *
296_wrap_gtk_icon_view_enable_model_drag_dest(PyGObject *self, PyObject *args,
297                                           PyObject *kwargs)
298{
299    static char *kwlist[] = { "targets", "actions", NULL };
300    PyObject *py_targets, *py_actions;
301    GtkTargetEntry *targets;
302    GdkDragAction actions;
303    gint n_targets, i;
304
305    if (!PyArg_ParseTupleAndKeywords(args, kwargs,
306                                     "OO:GtkIconView.enable_model_drag_dest",
307                                     kwlist,
308                                     &py_targets, &py_actions))
309        return NULL;
310    if (pyg_flags_get_value(GDK_TYPE_DRAG_ACTION,
311                             py_actions, (gint *)&actions))
312        return NULL;
313    if (!PySequence_Check(py_targets)) {
314        PyErr_SetString(PyExc_TypeError, "targets must be a sequence");
315        return NULL;
316    }
317    n_targets = PySequence_Length(py_targets);
318    targets = g_new(GtkTargetEntry, n_targets);
319    for (i = 0; i < n_targets; i++) {
320        PyObject *item = PySequence_GetItem(py_targets, i);
321        Py_DECREF(item);
322        if (!PyArg_ParseTuple(item, "zii", &targets[i].target,
323                              &targets[i].flags, &targets[i].info)) {
324            PyErr_Clear();
325            PyErr_SetString(PyExc_TypeError,
326                            "list items should be of form (string,int,int)");
327            g_free(targets);
328            return NULL;
329        }
330    }
331    gtk_icon_view_enable_model_drag_dest(GTK_ICON_VIEW(self->obj),
332                                         targets, n_targets, actions);
333    g_free(targets);
334    Py_INCREF(Py_None);
335    return Py_None;
336}
337%%
338override gtk_icon_view_get_tooltip_context kwargs
339static PyObject *
340_wrap_gtk_icon_view_get_tooltip_context(PyGObject *self,
341                                        PyObject *args,
342                                        PyObject *kwargs)
343{
344    static char *kwlist[] = { "x", "y", "keyboard_tip", NULL };
345
346    gboolean        ret;
347    PyObject        *py_ret = Py_None, *py_keyboard_tip = Py_True;
348    gint            x, y;
349    GtkTreeModel    *tree_model;
350    GtkTreePath     *path;
351    GtkTreeIter     iter;
352
353    if (!PyArg_ParseTupleAndKeywords(args, kwargs,
354                        "iiO:GtkIconView.get_tooltip_context",
355                        kwlist, &x, &y, &py_keyboard_tip))
356        return NULL;
357
358    ret = gtk_icon_view_get_tooltip_context(GTK_ICON_VIEW(self->obj), &x, &y,
359                                            PyObject_IsTrue(py_keyboard_tip),
360                                            &tree_model,
361                                            &path, &iter);
362    if (ret) {
363        py_ret = Py_BuildValue("(NNN)",
364                               pygobject_new((GObject *)tree_model),
365                               pygtk_tree_path_to_pyobject(path),
366                               pyg_boxed_new(GTK_TYPE_TREE_ITER, &iter,
367                                             TRUE, TRUE));
368
369        gtk_tree_path_free(path);
370        return py_ret;
371    }
372    Py_INCREF(py_ret);
373    return py_ret;
374}
375%%
376override gtk_icon_view_convert_widget_to_bin_window_coords kwargs
377static PyObject*
378_wrap_gtk_icon_view_convert_widget_to_bin_window_coords(PyGObject *self,
379                                                        PyObject *args,
380                                                        PyObject *kwargs)
381{
382    static char *kwlist[] = { "widget_x", "widget_y", NULL };
383    gint widget_x, widget_y, window_x = 0, window_y = 0;
384
385    if (!PyArg_ParseTupleAndKeywords(args, kwargs,
386                        "ii:GtkIconView.convert_widget_to_bin_window_coords",
387                        kwlist, &widget_x, &widget_y))
388        return NULL;
389
390    gtk_icon_view_convert_widget_to_bin_window_coords(GTK_ICON_VIEW(self->obj),
391                                          widget_x, widget_y,
392                                          &window_x, &window_y);
393
394    return Py_BuildValue("(ii)", window_x, window_y);
395}
396