[goocanvas/introspection] Introspection: Add GooCanvas{Rect, Image, Widget}



commit 7a3fa28343b98ee6fa1f382194851f69d4a53860
Author: John Stowers <john stowers gmail com>
Date:   Thu May 19 20:45:58 2011 +1200

    Introspection: Add GooCanvas{Rect,Image,Widget}

 src/GooCanvas.py      |   61 ++++++++++-------
 src/demo.py           |  179 +++++++++++++++++++++++++++++++++---------------
 src/goocanvasimage.c  |   12 ++--
 src/goocanvaswidget.c |    4 +-
 4 files changed, 167 insertions(+), 89 deletions(-)
---
diff --git a/src/GooCanvas.py b/src/GooCanvas.py
index 322847a..26249fb 100644
--- a/src/GooCanvas.py
+++ b/src/GooCanvas.py
@@ -5,37 +5,30 @@ GooCanvas = modules['GooCanvas']._introspection_module
 
 __all__ = []
 
-class CanvasPath(GooCanvas.CanvasPath):
+class _GooCanvasItem:
+    def __init__(self, parent, **props):
+        if parent:
+            parent.add_child(self, -1)
+        for k,v in props.iteritems():
+            self.set_property(k,v)
+
+class CanvasPath(GooCanvas.CanvasPath, _GooCanvasItem):
 
     def __init__(self, parent, path_data, **props):
         GooCanvas.CanvasPath.__init__(self)
-        if parent:
-            parent.add_child(self, -1)
+        _GooCanvasItem.__init__(self, parent, **props)
         if path_data:
             self.props.data = path_data
-        for k,v in props.iteritems():
-            self.set_property(k,v)
 
 CanvasPath = override(CanvasPath)
 __all__.append('CanvasPath')
 
-class CanvasRect(GooCanvas.CanvasRect):
+class CanvasRect(GooCanvas.CanvasRect, _GooCanvasItem):
 
     def __init__(self, parent, x, y, width, height, **props):
         GooCanvas.CanvasPath.__init__(self)
-        if parent:
-            parent.add_child(self, -1)
         props.update(x=x,y=y,width=width,height=height)
-        for k,v in props.iteritems():
-            self.set_property(k,v)
-
-    def __new__(cls, *args, **kwds):
-        arg_len = len(args)
-        kwd_len = len(kwds)
-        total_len = arg_len + kwd_len
-
-        def _new(cursor_type):
-            return cls.new(cursor_type)
+        _GooCanvasItem.__init__(self, parent, **props)
 
 CanvasRect = override(CanvasRect)
 __all__.append('CanvasRect')
@@ -46,8 +39,6 @@ class CanvasPoints(GooCanvas.CanvasPoints):
 
     def __new__(cls, *points):
 
-        print points
-
         assert len(points)
         assert len(points[0])
 
@@ -61,17 +52,14 @@ class CanvasPoints(GooCanvas.CanvasPoints):
 CanvasPoints = override(CanvasPoints)
 __all__.append('CanvasPoints')
 
-class CanvasPolyline(GooCanvas.CanvasPolyline):
+class CanvasPolyline(GooCanvas.CanvasPolyline, _GooCanvasItem):
 
     def __init__(self, parent, close_path, *points, **props):
         GooCanvas.CanvasPolyline.__init__(self)
-        if parent:
-            parent.add_child(self, -1)
         props.update(close_path=close_path)
         if points:
             props.update(points=CanvasPoints(*points))
-        for k,v in props.iteritems():
-            self.set_property(k,v)
+        _GooCanvasItem.__init__(self, parent, **props)
 
     @classmethod
     def new_line(cls, parent, x1, y1, x2, y2, **props):
@@ -79,3 +67,26 @@ class CanvasPolyline(GooCanvas.CanvasPolyline):
 
 CanvasPolyline = override(CanvasPolyline)
 __all__.append('CanvasPolyline')
+
+class CanvasImage(GooCanvas.CanvasImage, _GooCanvasItem):
+
+    def __init__(self, parent, pixbuf, x, y, **props):
+        GooCanvas.CanvasImage.__init__(self)
+        props.update(pixbuf=pixbuf,x=x,y=y)
+        _GooCanvasItem.__init__(self, parent, **props)
+
+CanvasImage = override(CanvasImage)
+__all__.append('CanvasImage')
+
+class CanvasWidget(GooCanvas.CanvasWidget, _GooCanvasItem):
+
+    def __init__(self, parent, widget, x, y, width, height, **props):
+        GooCanvas.CanvasWidget.__init__(self)
+        #taken from the C constructor
+        #g_object_set_data (G_OBJECT (witem->widget), "goo-canvas-item", witem);
+        widget.show()
+        props.update(widget=widget,x=x,y=y,width=width,height=height)
+        _GooCanvasItem.__init__(self, parent, **props)
+
+CanvasWidget = override(CanvasWidget)
+__all__.append('CanvasWidget')
diff --git a/src/demo.py b/src/demo.py
index 4e3e30f..3e37d12 100644
--- a/src/demo.py
+++ b/src/demo.py
@@ -1,4 +1,46 @@
-from gi.repository import Gtk, GooCanvas
+from gi.repository import Gtk, GdkPixbuf, GooCanvas
+
+def setup_polyline(c):
+    group = c.get_root_item()
+
+    GooCanvas.CanvasRect (group, 0, 0, 600, 450, line_width=4.0)
+
+    GooCanvas.CanvasPolyline.new_line(group, 0, 150, 600, 150, line_width=4.0)
+
+    GooCanvas.CanvasPolyline.new_line(group, 0, 300, 600, 300, line_width=4.0)
+
+    GooCanvas.CanvasPolyline.new_line(group, 200, 0, 200, 450, line_width=4.0)
+
+    GooCanvas.CanvasPolyline.new_line(group, 400, 0, 400, 450, line_width=4.0)
+
+    GooCanvas.CanvasPolyline (group, False,
+                       (340.0, 170.0),
+                       (340.0, 230.0),
+                       (390.0, 230.0),
+                       (390.0, 170.0),
+                       stroke_color="midnightblue",
+                       line_width=3.0,
+                       start_arrow=True,
+                       end_arrow=True,
+                       arrow_tip_length=3.0,
+                       arrow_length=4.0,
+                       arrow_width=3.5)
+
+    GooCanvas.CanvasPolyline (group, False,
+                       (356.0, 180.0),
+                       (374.0, 220.0),
+                       stroke_color="blue",
+                       line_width=1.0,
+                       start_arrow=True,
+                       end_arrow=True,
+                       arrow_tip_length=5.0,
+                       arrow_length=6.0,
+                       arrow_width=6.0)
+
+    GooCanvas.CanvasPolyline (group, False,
+                       (356.0, 220.0),
+                       start_arrow=True,
+                       end_arrow=True)
 
 def setup_canvas(c):
     root = c.get_root_item()
@@ -86,70 +128,95 @@ def setup_canvas(c):
                   line_width=5.0,
                   )
 
-def demo_polyline():
-    c = GooCanvas.Canvas()
-    c.set_size_request(600, 450)
-
-    group = c.get_root_item()
-
-    GooCanvas.CanvasRect (group, 0, 0, 600, 450, line_width=4.0)
-
-    GooCanvas.CanvasPolyline.new_line(group, 0, 150, 600, 150, line_width=4.0)
-
-    GooCanvas.CanvasPolyline.new_line(group, 0, 300, 600, 300, line_width=4.0)
-
-    GooCanvas.CanvasPolyline.new_line(group, 200, 0, 200, 450, line_width=4.0)
-
-    GooCanvas.CanvasPolyline.new_line(group, 400, 0, 400, 450, line_width=4.0)
+def setup_scalability(c):
+    N_COLS = 5
+    N_ROWS = 20
+    PADDING = 10
+
+    vbox = Gtk.VBox (homogeneous=False, spacing=4)
+    vbox.set_border_width (4)
+    vbox.show()
+
+    table = Gtk.Table (2, 2, False)
+    table.set_row_spacings (4)
+    table.set_col_spacings (4)
+    vbox.pack_start (table, True, True, 0)
+    table.show ()
+
+    frame = Gtk.Frame ()
+    frame.set_shadow_type (Gtk.ShadowType.IN)
+    table.attach (frame,
+              0, 1, 0, 1,
+              Gtk.AttachOptions.EXPAND | Gtk.AttachOptions.FILL | Gtk.AttachOptions.SHRINK,
+              Gtk.AttachOptions.EXPAND | Gtk.AttachOptions.FILL | Gtk.AttachOptions.SHRINK,
+              0, 0);
+    frame.show()
+
+    if 1:
+        pb = GdkPixbuf.Pixbuf.new_from_file("../demo/toroid.png")
+        width = pb.get_width()
+        height = pb.get_height()
+    else:
+        pb = None
+        width = 37
+        height = 19
+
+    c.set_bounds (
+            0, 0,
+            N_COLS * (width + PADDING),
+            N_ROWS * (height + PADDING))
+    c.show ();
+
+    scrolled_win = Gtk.ScrolledWindow ()
+    scrolled_win.show()
+    frame.add(scrolled_win)
+    scrolled_win.add(c)
 
-    GooCanvas.CanvasPolyline (group, False,
-				       (340.0, 170.0),
-				       (340.0, 230.0),
-				       (390.0, 230.0),
-				       (390.0, 170.0),
-				       stroke_color="midnightblue",
-				       line_width=3.0,
-				       start_arrow=True,
-				       end_arrow=True,
-				       arrow_tip_length=3.0,
-				       arrow_length=4.0,
-				       arrow_width=3.5)
+    root = c.get_root_item()
+    for i in range(N_COLS):
+        for j in range(N_ROWS):
+            if pb:
+                GooCanvas.CanvasImage (root, pb,
+                           i * (width + PADDING),
+                           j * (height + PADDING))
+            else:
+                item = GooCanvas.CanvasRect (root,
+                          i * (width + PADDING),
+                          j * (height + PADDING),
+                          width, height)
+                item.props.fill_color = {True:"mediumseagreen",False:"steelblue"}[j % 2 == 0]
+
+    return vbox
+
+def setup_widget(c):
+    root = c.get_root_item()
 
-    GooCanvas.CanvasPolyline (group, False,
-				       (356.0, 180.0),
-				       (374.0, 220.0),
-				       stroke_color="blue",
-				       line_width=1.0,
-				       start_arrow=True,
-				       end_arrow=True,
-				       arrow_tip_length=5.0,
-				       arrow_length=6.0,
-				       arrow_width=6.0)
+    #Add a few simple items. */
+    GooCanvas.CanvasWidget (root, Gtk.Label("Hello World"), 50, 50, 200, 100)
+    GooCanvas.CanvasWidget (root, Gtk.Entry(), 50, 250, 200, 50)
 
-    GooCanvas.CanvasPolyline (group, False,
-				       (356.0, 220.0),
-				       start_arrow=True,
-				       end_arrow=True)
-				       
-#  setup_item_signals (polyline1);
+    entry = Gtk.Entry ()
+    entry.set_text ("Size: -1 x -1")
+    GooCanvas.CanvasWidget (root, entry, 50, 300, -1, -1)
 
-    w = Gtk.Window()
-    w.add(c)
-    w.show_all()
-    w.connect("destroy", Gtk.main_quit)
+    entry = Gtk.Entry ()
+    entry.set_text ("Size: 100 x -1")
+    GooCanvas.CanvasWidget (root, entry, 50, 350, 100, -1)
 
-def demo_canvas():
+def demo_window(setup_func):
     c = GooCanvas.Canvas()
     c.set_size_request(600, 450)
-
-    setup_canvas(c)
-
+    widget = setup_func(c) or c
     w = Gtk.Window()
-    w.add(c)
+    w.set_size_request(600, 450)
+    w.add(widget)
     w.show_all()
     w.connect("destroy", Gtk.main_quit)
 
-demo_canvas()
-demo_polyline()
+if __name__ == "__main__":
+    demo_window(setup_canvas)
+    demo_window(setup_polyline)
+    demo_window(setup_scalability)
+    demo_window(setup_widget)
 
-Gtk.main()
+    Gtk.main()
diff --git a/src/goocanvasimage.c b/src/goocanvasimage.c
index 5ecc7db..6cde02c 100644
--- a/src/goocanvasimage.c
+++ b/src/goocanvasimage.c
@@ -175,10 +175,10 @@ goo_canvas_image_init (GooCanvasImage *image)
 
 /**
  * goo_canvas_image_new:
- * @parent: the parent item, or %NULL. If a parent is specified, it will assume
+ * @parent: (allow-none): the parent item, or %NULL. If a parent is specified, it will assume
  *  ownership of the item, and the item will automatically be freed when it is
  *  removed from the parent. Otherwise call g_object_unref() to free it.
- * @pixbuf: the #GdkPixbuf containing the image data, or %NULL.
+ * @pixbuf: (allow-none): the #GdkPixbuf containing the image data, or %NULL.
  * @x: the x coordinate of the image.
  * @y: the y coordinate of the image.
  * @...: optional pairs of property names and values, and a terminating %NULL.
@@ -195,7 +195,7 @@ goo_canvas_image_init (GooCanvasImage *image)
  *                                               NULL);
  * </programlisting></informalexample>
  *
- * Returns: a new image item.
+ * Returns: (transfer full): a new image item.
  **/
 GooCanvasItem*
 goo_canvas_image_new (GooCanvasItem *parent,
@@ -610,10 +610,10 @@ goo_canvas_image_model_init (GooCanvasImageModel *emodel)
 
 /**
  * goo_canvas_image_model_new:
- * @parent: the parent model, or %NULL. If a parent is specified, it will
+ * @parent: (allow-none): the parent model, or %NULL. If a parent is specified, it will
  *  assume ownership of the item, and the item will automatically be freed when
  *  it is removed from the parent. Otherwise call g_object_unref() to free it.
- * @pixbuf: the #GdkPixbuf containing the image data, or %NULL.
+ * @pixbuf: (allow-none): the #GdkPixbuf containing the image data, or %NULL.
  * @x: the x coordinate of the image.
  * @y: the y coordinate of the image.
  * @...: optional pairs of property names and values, and a terminating %NULL.
@@ -630,7 +630,7 @@ goo_canvas_image_model_init (GooCanvasImageModel *emodel)
  *                                                          NULL);
  * </programlisting></informalexample>
  *
- * Returns: a new image model.
+ * Returns: (transfer full): a new image model.
  **/
 GooCanvasItemModel*
 goo_canvas_image_model_new (GooCanvasItemModel *parent,
diff --git a/src/goocanvaswidget.c b/src/goocanvaswidget.c
index fdf8927..f8b1263 100644
--- a/src/goocanvaswidget.c
+++ b/src/goocanvaswidget.c
@@ -85,7 +85,7 @@ goo_canvas_widget_init (GooCanvasWidget *witem)
 
 /**
  * goo_canvas_widget_new:
- * @parent: the parent item, or %NULL. If a parent is specified, it will assume
+ * @parent: (allow-none): the parent item, or %NULL. If a parent is specified, it will assume
  *  ownership of the item, and the item will automatically be freed when it is
  *  removed from the parent. Otherwise call g_object_unref() to free it.
  * @widget: the widget.
@@ -110,7 +110,7 @@ goo_canvas_widget_init (GooCanvasWidget *witem)
  *                                                NULL);
  * </programlisting></informalexample>
  * 
- * Returns: a new widget item.
+ * Returns: (transfer full): a new widget item.
  **/
 GooCanvasItem*
 goo_canvas_widget_new               (GooCanvasItem    *parent,



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]