summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--ChangeLog6
-rw-r--r--Makefile.am1
-rw-r--r--examples/gtk/widget.py84
-rw-r--r--gobject/pygobject.c11
-rw-r--r--pygobject.c11
5 files changed, 109 insertions, 4 deletions
diff --git a/ChangeLog b/ChangeLog
index 21a5e7e7..41ebbbea 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,11 @@
2004-06-04 Johan Dahlin <johan@gnome.org>
+ * examples/gtk/widget.py: Add a new example of subclassing a GtkWidget.
+
+ * Makefile.am (EXTRA_DIST): Add widget.py
+
+ * pygobject.c (pygobject_new): guard object_ref call
+
* examples/gtk/uimanager.py: Some more polishing, it'll never be finished!
* gtk/gtkwidget.override (_wrap_gtk_widget__set_allocation): Impl.
diff --git a/Makefile.am b/Makefile.am
index 97de99e8..c996dd30 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -108,6 +108,7 @@ EXTRA_DIST += \
examples/gobject/signal.py \
examples/gobject/properties.py \
examples/gtk/filechooser.py \
+ examples/gtk/widget.py \
examples/gtk/uimanager.py \
examples/pango/utf8-demo.py \
examples/pygtk-demo/pygtk-demo.py \
diff --git a/examples/gtk/widget.py b/examples/gtk/widget.py
new file mode 100644
index 00000000..d685d52e
--- /dev/null
+++ b/examples/gtk/widget.py
@@ -0,0 +1,84 @@
+import ltihooks
+import gobject
+print gobject
+import pango
+import gtk
+from gtk import gdk
+
+TEXT = 'A GtkWidget implemented in PyGTK'
+
+class Widget(gtk.Widget):
+ __gsignals__ = { 'realize': 'override',
+ 'expose-event' : 'override',
+ 'size-allocate': 'override'}
+ def __init__(self):
+ self.__gobject_init__()
+
+ self.draw_gc = None
+ self.layout = self.create_pango_layout(TEXT)
+ self.layout.set_font_description(pango.FontDescription("sans serif 16"))
+
+ def do_realize(self):
+ self.set_flags(self.flags() | gtk.REALIZED)
+
+ self.window = gdk.Window(self.get_parent_window(),
+ width=self.allocation.width,
+ height=self.allocation.height,
+ window_type=gtk.gdk.WINDOW_CHILD,
+ wclass=gtk.gdk.INPUT_OUTPUT,
+ event_mask=self.get_events() | gdk.EXPOSURE_MASK)
+ self.draw_gc = gdk.GC(self.window,
+ line_width=5,
+ line_style=gdk.SOLID,
+ join_style=gdk.JOIN_ROUND)
+ self.window.set_user_data(self)
+ self.style.attach(self.window)
+ self.style.set_background(self.window, gtk.STATE_NORMAL)
+
+ self.send_configure()
+
+ def do_size_allocate(self, allocation):
+ self.allocation = allocation
+
+ if self.flags() & gtk.REALIZED:
+ self.window.move_resize(*allocation)
+ self.send_configure()
+
+ def do_expose_event(self, event):
+ self.chain(event)
+
+ x, y, w, h = self.allocation
+ width = 10
+ self.window.draw_rectangle(self.draw_gc, False,
+ x + width, y + width,
+ w - width * 2, h - width * 2)
+ fontw, fonth = self.layout.get_pixel_size()
+ self.style.paint_layout(self.window, self.state, False,
+ event.area, self, "label",
+ (w - fontw) / 2, (h - fonth) / 2,
+ self.layout)
+
+ def send_configure(self):
+ allocation = self.allocation
+
+ event = gdk.Event(gdk.CONFIGURE)
+ event.send_event = True
+ event.window = self.window
+ event.x = allocation.x
+ event.y = allocation.y
+ event.width = allocation.width
+ event.height = allocation.height
+ self.event(event)
+
+gobject.type_register(Widget)
+
+win = gtk.Window()
+win.set_title(TEXT)
+win.connect('delete-event', gtk.main_quit)
+w = Widget()
+w.set_size_request(400, 200)
+win.add(w)
+
+win.show_all()
+
+gtk.main()
diff --git a/gobject/pygobject.c b/gobject/pygobject.c
index 7e278b3a..fa8ef90d 100644
--- a/gobject/pygobject.c
+++ b/gobject/pygobject.c
@@ -254,6 +254,13 @@ pygobject_new_with_interfaces(GType gtype)
PyErr_Print();
return NULL;
}
+
+#if 0
+ type->tp_dealloc = (destructor)pygobject_dealloc;
+ type->tp_traverse = (traverseproc)pygobject_traverse;
+ type->tp_clear = (inquiry)pygobject_clear;
+ type->tp_flags |= Py_TPFLAGS_HAVE_GC;
+#endif
if (PyType_Ready(type) < 0) {
g_warning ("couldn't make the type `%s' ready", type->tp_name);
@@ -344,12 +351,12 @@ pygobject_new(GObject *obj)
pygobject_new_with_interfaces(). fixes bug #141042 */
if (tp->tp_flags & Py_TPFLAGS_HEAPTYPE)
Py_INCREF(tp);
-
self = PyObject_GC_New(PyGObject, tp);
-
if (self == NULL)
return NULL;
+ pyg_unblock_threads();
self->obj = g_object_ref(obj);
+ pyg_block_threads();
sink_object(self->obj);
self->inst_dict = NULL;
diff --git a/pygobject.c b/pygobject.c
index 7e278b3a..fa8ef90d 100644
--- a/pygobject.c
+++ b/pygobject.c
@@ -254,6 +254,13 @@ pygobject_new_with_interfaces(GType gtype)
PyErr_Print();
return NULL;
}
+
+#if 0
+ type->tp_dealloc = (destructor)pygobject_dealloc;
+ type->tp_traverse = (traverseproc)pygobject_traverse;
+ type->tp_clear = (inquiry)pygobject_clear;
+ type->tp_flags |= Py_TPFLAGS_HAVE_GC;
+#endif
if (PyType_Ready(type) < 0) {
g_warning ("couldn't make the type `%s' ready", type->tp_name);
@@ -344,12 +351,12 @@ pygobject_new(GObject *obj)
pygobject_new_with_interfaces(). fixes bug #141042 */
if (tp->tp_flags & Py_TPFLAGS_HEAPTYPE)
Py_INCREF(tp);
-
self = PyObject_GC_New(PyGObject, tp);
-
if (self == NULL)
return NULL;
+ pyg_unblock_threads();
self->obj = g_object_ref(obj);
+ pyg_block_threads();
sink_object(self->obj);
self->inst_dict = NULL;