diff options
author | Gian Mario Tagliaretti <gianmt@src.gnome.org> | 2007-07-17 20:57:26 +0000 |
---|---|---|
committer | Gian Mario Tagliaretti <gianmt@src.gnome.org> | 2007-07-17 20:57:26 +0000 |
commit | 5061bfed82fbd94792beacd6f6b13308745dfc7b (patch) | |
tree | 6ce3ae4e4694c83c77dcdf94b3fa8fcea922f16d | |
parent | 4206e5c359a6159bb64a4dcea55936424f10c96e (diff) | |
download | pygtk-5061bfed82fbd94792beacd6f6b13308745dfc7b.tar.gz |
wrap a couple of gtk.Builder methods, new builder example
svn path=/trunk/; revision=2853
-rw-r--r-- | ChangeLog | 11 | ||||
-rw-r--r-- | examples/builder/builder.py | 33 | ||||
-rw-r--r-- | examples/builder/demo.ui | 228 | ||||
-rw-r--r-- | gtk/Makefile.am | 1 | ||||
-rw-r--r-- | gtk/gtk.override | 1 | ||||
-rw-r--r-- | gtk/gtkbuilder.override | 124 |
6 files changed, 398 insertions, 0 deletions
@@ -1,3 +1,14 @@ +2007-07-17 Gian Mario Tagliaretti <gianmt@gnome.org> + + reviewed by: Johan, Fix bug #4457195 + + * gtk/gtkbuilder.override: new file. + + * gtk/Makefile.am, + * gtk/gtk.override: add gtkbuilder.override. + + * examples/builder/*: add new builder example. + 2007-07-15 Gian Mario Tagliaretti <gianmt@gnome.org> * gtk/gtk-2.12.defs: strip gtk_ prefix for new functions. diff --git a/examples/builder/builder.py b/examples/builder/builder.py new file mode 100644 index 00000000..75f32c7d --- /dev/null +++ b/examples/builder/builder.py @@ -0,0 +1,33 @@ +import gtk + +class BuilderExample: + def __init__(self, filename): + # create a new empty Builer object + builder = gtk.Builder() + + # load an XML definition + builder.add_from_file(filename) + + # autoconnect the signals from the instance self + builder.connect_signals(self) + + # get the main window + window = builder.get_object("window1") + window.connect("destroy", gtk.main_quit) + window.show_all() + + def about_activate(self, action, data=None): + about_dlg = gtk.AboutDialog() + about_dlg.set_name("GtkBuilder demo") + about_dlg.run() + about_dlg.destroy() + + def quit_activate(self, action, data=None): + gtk.main_quit() + + def main(self): + gtk.main() + +if __name__ == '__main__': + build = BuilderExample("demo.ui") + build.main() diff --git a/examples/builder/demo.ui b/examples/builder/demo.ui new file mode 100644 index 00000000..1c84db2b --- /dev/null +++ b/examples/builder/demo.ui @@ -0,0 +1,228 @@ +<?xml version="1.0" standalone="no"?> <!--*- mode: xml -*--> +<interface> + <object class="GtkListStore" id="liststore1"> + <columns> + <column type="gchararray"/> + <column type="gchararray"/> + <column type="gint"/> + </columns> + <data> + <row> + <col id="0">John</col> + <col id="1">Doe</col> + <col id="2">25</col> + </row> + <row> + <col id="0">Mary</col> + <col id="1">Dole</col> + <col id="2">50</col> + </row> + </data> + </object> + <object class="GtkUIManager" id="uimanager"> + <child> + <object class="GtkActionGroup" id="DefaultActions"> + <child> + <object class="GtkAction" id="Copy"> + <property name="name">Copy</property> + <property name="tooltip" translatable="yes">Copy selected object into the clipboard</property> + <property name="stock_id">gtk-copy</property> + </object> + </child> + <child> + <object class="GtkAction" id="Cut"> + <property name="name">Cut</property> + <property name="tooltip" translatable="yes">Cut selected object into the clipboard</property> + <property name="stock_id">gtk-cut</property> + </object> + </child> + <child> + <object class="GtkAction" id="EditMenu"> + <property name="name">EditMenu</property> + <property name="label" translatable="yes">_Edit</property> + </object> + </child> + <child> + <object class="GtkAction" id="FileMenu"> + <property name="name">FileMenu</property> + <property name="label" translatable="yes">_File</property> + </object> + </child> + <child> + <object class="GtkAction" id="New"> + <property name="name">New</property> + <property name="tooltip" translatable="yes">Create a new file</property> + <property name="stock_id">gtk-new</property> + </object> + </child> + <child> + <object class="GtkAction" id="Open"> + <property name="name">Open</property> + <property name="tooltip" translatable="yes">Open a file</property> + <property name="stock_id">gtk-open</property> + </object> + </child> + <child> + <object class="GtkAction" id="Paste"> + <property name="name">Paste</property> + <property name="tooltip" translatable="yes">Paste object from the Clipboard</property> + <property name="stock_id">gtk-paste</property> + </object> + </child> + <child> + <object class="GtkAction" id="Quit"> + <property name="name">Quit</property> + <property name="tooltip" translatable="yes">Quit the program</property> + <property name="stock_id">gtk-quit</property> + <signal handler="quit_activate" name="activate"/> + </object> + </child> + <child> + <object class="GtkAction" id="Save"> + <property name="name">Save</property> + <property name="is_important">True</property> + <property name="tooltip" translatable="yes">Save a file</property> + <property name="stock_id">gtk-save</property> + </object> + </child> + <child> + <object class="GtkAction" id="SaveAs"> + <property name="name">SaveAs</property> + <property name="tooltip" translatable="yes">Save with a different name</property> + <property name="stock_id">gtk-save-as</property> + </object> + </child> + <child> + <object class="GtkAction" id="HelpMenu"> + <property name="name">HelpMenu</property> + <property name="label" translatable="yes">_Help</property> + </object> + </child> + <child> + <object class="GtkAction" id="About"> + <property name="name">About</property> + <property name="stock_id">gtk-about</property> + <signal handler="about_activate" name="activate"/> + </object> + <accelerator key="F1"/> + </child> + </object> + </child> + <ui> + <menubar name="menubar1"> + <menu action="FileMenu" name="FileMenu"> + <menuitem action="New" name="New"/> + <menuitem action="Open" name="Open"/> + <menuitem action="Save" name="Save"/> + <menuitem action="SaveAs" name="SaveAs"/> + <separator/> + <menuitem action="Quit" name="Quit"/> + </menu> + <menu action="EditMenu"> + <menuitem action="Copy" name="Copy"/> + <menuitem action="Cut" name="Cut"/> + <menuitem action="Paste" name="Paste"/> + </menu> + <menu action="HelpMenu" name="HelpMenu"> + <menuitem action="About" name="About"/> + </menu> + </menubar> + <toolbar name="toolbar1"> + <toolitem action="New" name="New"/> + <toolitem action="Open" name="Open"/> + <toolitem action="Save" name="Save"/> + <separator/> + <toolitem action="Copy" name="Copy"/> + <toolitem action="Cut" name="Cut"/> + <toolitem action="Paste" name="Paste"/> + </toolbar> + </ui> + </object> + + <object class="GtkWindow" id="window1"> + <property name="default_height">250</property> + <property name="default_width">440</property> + <property name="title">builder</property> + <child> + <object class="GtkVBox" id="vbox1"> + <property name="visible">True</property> + <child> + <object constructor="uimanager" class="GtkMenuBar" id="menubar1"> + <property name="visible">True</property> + </object> + <packing> + <property name="expand">False</property> + </packing> + </child> + <child> + <object constructor="uimanager" class="GtkToolbar" id="toolbar1"> + <property name="visible">True</property> + </object> + <packing> + <property name="expand">False</property> + <property name="position">1</property> + </packing> + </child> + <child> + <object class="GtkScrolledWindow" id="scrolledwindow1"> + <property name="hscrollbar_policy">automatic</property> + <property name="shadow_type">in</property> + <property name="visible">True</property> + <property name="vscrollbar_policy">automatic</property> + <child> + <object class="GtkTreeView" id="treeview1"> + <property name="visible">True</property> + <property name="model">liststore1</property> + <child> + <object class="GtkTreeViewColumn" id="column1"> + <property name="title">Name</property> + <child> + <object class="GtkCellRendererText" id="renderer1"/> + <attributes> + <attribute name="text">0</attribute> + </attributes> + </child> + </object> + </child> + <child> + <object class="GtkTreeViewColumn" id="column2"> + <property name="title">Surname</property> + <child> + <object class="GtkCellRendererText" id="renderer2"/> + <attributes> + <attribute name="text">1</attribute> + </attributes> + </child> + </object> + </child> + <child> + <object class="GtkTreeViewColumn" id="column3"> + <property name="title">Age</property> + <child> + <object class="GtkCellRendererText" id="renderer3"/> + <attributes> + <attribute name="text">2</attribute> + </attributes> + </child> + </object> + </child> + </object> + </child> + </object> + <packing> + <property name="position">2</property> + </packing> + </child> + <child> + <object class="GtkStatusbar" id="statusbar1"> + <property name="visible">True</property> + </object> + <packing> + <property name="expand">False</property> + <property name="position">3</property> + </packing> + </child> + </object> + </child> + </object> +</interface> diff --git a/gtk/Makefile.am b/gtk/Makefile.am index 2862bfe6..a8bb2ac2 100644 --- a/gtk/Makefile.am +++ b/gtk/Makefile.am @@ -76,6 +76,7 @@ GDK_OVERRIDES = \ GTK_OVERRIDES = \ gtk.override \ + gtkbuilder.override \ gtkclist.override \ gtkcontainer.override \ gtkctree.override \ diff --git a/gtk/gtk.override b/gtk/gtk.override index 430fb801..34f43741 100644 --- a/gtk/gtk.override +++ b/gtk/gtk.override @@ -106,6 +106,7 @@ pygtk_util_pyobject_as_double(PyObject *obj, gdouble *value, const char *argumen %% include + gtkbuilder.override gtkclist.override gtkcontainer.override gtkctree.override diff --git a/gtk/gtkbuilder.override b/gtk/gtkbuilder.override new file mode 100644 index 00000000..64ef837d --- /dev/null +++ b/gtk/gtkbuilder.override @@ -0,0 +1,124 @@ +/* -*- Mode: C; c-basic-offset: 4 -*- + * pygtk- Python bindings for the GTK toolkit. + * Copyright (C) 2007 Gian Mario Tagliaretti + * + * gtkbuilder.override: overrides for various builder functions. + * + * 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 + * USA + */ + +%% +headers + +typedef struct{ + PyObject *obj; + PyObject *data; +} PyGCustomSignalNotify; + +%% +override gtk_builder_connect_signals kwargs +static void +connect_many(GtkBuilder *builder, GObject *obj, + const gchar *signal_name, const gchar *handler_name, + GObject *connect_object, GConnectFlags flags, gpointer user_data) +{ + PyGCustomSignalNotify *notify = user_data; + PyObject *handler_dict = notify->obj; + PyObject *tuple, *self; + GClosure *closure = NULL; + + tuple = PyMapping_GetItemString(handler_dict, (gchar *)handler_name); + if (!tuple) { + PyErr_Clear(); + tuple = PyObject_GetAttrString(handler_dict, (gchar *)handler_name); + if (!tuple) { + PyErr_Clear(); + return; + } + } + + if (PyTuple_Check(tuple)) { + PyObject *callback = PyTuple_GetItem(tuple, 0); + PyObject *extra = PySequence_GetSlice(tuple, 1, PyTuple_Size(tuple)); + PyObject *other = NULL; + + if (connect_object) + other = pygobject_new((GObject *)connect_object); + + closure = pyg_closure_new(callback, extra, other); + Py_DECREF(extra); + } else if (PyCallable_Check(tuple)) { + PyObject *other = NULL; + + if (connect_object) + other = pygobject_new((GObject *)connect_object); + + closure = pyg_closure_new(tuple, notify->data, other); + } else { + g_warning("handler for `%s' not callable or a tuple", handler_name); + Py_DECREF(tuple); + return; + } + Py_DECREF(tuple); + self = pygobject_new(obj); + g_signal_connect_closure(obj, signal_name, closure, flags); + pygobject_watch_closure(self, closure); + Py_DECREF(self); +} + +static PyObject * +_wrap_gtk_builder_connect_signals(PyGObject *self, PyObject *args, + PyObject *kwargs) +{ + static char *kwlist[] = { "object", "user_data", NULL }; + PyGCustomSignalNotify notify; + PyObject *object, *user_data = NULL; + + if (!PyArg_ParseTupleAndKeywords(args, kwargs, + "O|O:GtkBuilder.connect_signals", kwlist, + &object, &user_data)) + return NULL; + + notify.obj = object; + notify.data = user_data; + + gtk_builder_connect_signals_full(GTK_BUILDER(self->obj), + connect_many, + ¬ify); + Py_INCREF(Py_None); + return Py_None; +} + +%% +override gtk_builder_get_objects noargs +static PyObject * +_wrap_gtk_builder_get_objects(PyGObject *self) +{ + GSList *l, *objects; + PyObject *pyobjects; + + pyobjects = PyList_New(0); + objects = gtk_builder_get_objects(GTK_BUILDER(self->obj)); + for (l = objects; l; l = l->next) + { + PyObject *item = pygobject_new((GObject *)l->data); + PyList_Append(pyobjects, item); + Py_DECREF(item); + } + g_slist_free(objects); + + return pyobjects; +} |