[pygi] Override Dialog constructor and add_buttons method



commit 19e1bf365544567287dc43565346c3c2aeee2de2
Author: Paolo Borelli <pborelli gnome org>
Date:   Sun Jun 20 13:27:34 2010 +0200

    Override Dialog constructor and add_buttons method

 gi/overrides/Gtk.py     |   46 +++++++++++++++++++++++++++++++++++++++++++++-
 tests/test_overrides.py |   16 ++++++++++++++++
 2 files changed, 61 insertions(+), 1 deletions(-)
---
diff --git a/gi/overrides/Gtk.py b/gi/overrides/Gtk.py
index 38b0dfa..a486612 100644
--- a/gi/overrides/Gtk.py
+++ b/gi/overrides/Gtk.py
@@ -240,7 +240,51 @@ class Builder(Gtk.Builder):
 
 Builder = override(Builder)
 
-__all__ = ['ActionGroup', 'Builder', 'UIManager']
+class Dialog(Gtk.Dialog):
+
+    def __init__(self, title=None, parent=None, flags=0, buttons=None):
+        Gtk.Dialog.__init__(self)
+        if title:
+            self.set_title(title)
+        if parent:
+            self.set_transient_for(parent)
+        if flags & Gtk.DialogFlags.MODAL:
+            self.set_modal(True)
+        if flags & Gtk.DialogFlags.DESTROY_WITH_PARENT:
+            self.set_destroy_with_parent(True)
+        if flags & Gtk.DialogFlags.NO_SEPARATOR:
+            self.set_has_separator(False)
+        if buttons:
+            self.add_buttons(*buttons)
+
+    def add_buttons(self, *args):
+        """
+        The add_buttons() method adds several buttons to the Gtk.Dialog using
+        the button data passed as arguments to the method. This method is the
+        same as calling the Gtk.Dialog.add_button() repeatedly. The button data
+        pairs - button text (or stock ID) and a response ID integer are passed
+        individually. For example:
+
+        >>> dialog.add_buttons(Gtk.STOCK_OPEN, 42, "Close", Gtk.ResponseType.CLOSE)
+
+        will add "Open" and "Close" buttons to dialog.
+        """
+
+        def buttons(b):
+            while b:
+                t, r = b[0:2]
+                b = b[2:]
+                yield t, r
+
+        try:
+            for text, response in buttons(args):
+                self.add_button(text, response)
+        except (IndexError):
+            raise TypeError('Must pass an even number of arguments')
+
+Dialog = override(Dialog)
+
+__all__ = ['ActionGroup', 'Builder', 'Dialog', 'UIManager']
 
 import sys
 
diff --git a/tests/test_overrides.py b/tests/test_overrides.py
index b5af4d1..e0290a1 100644
--- a/tests/test_overrides.py
+++ b/tests/test_overrides.py
@@ -129,4 +129,20 @@ class TestGtk(unittest.TestCase):
 
         self.assertEquals(signal_checker.sentinel, 4)
 
+    def test_dialog(self):
+        self.assertEquals(Gtk.Dialog, overrides.Gtk.Dialog)
+        dialog = Gtk.Dialog (title='Foo',
+                             flags=Gtk.DialogFlags.MODAL,
+                             buttons=('test-button1', 1))
+
+        dialog.add_buttons ('test-button2', 2, Gtk.STOCK_CLOSE, Gtk.ResponseType.CLOSE)
+
+        self.assertEquals('Foo', dialog.get_title())
+        self.assertTrue(dialog.get_modal())
+        button = dialog.get_widget_for_response (1)
+        self.assertEquals('test-button1', button.get_label())
+        button = dialog.get_widget_for_response (2)
+        self.assertEquals('test-button2', button.get_label())
+        button = dialog.get_widget_for_response (Gtk.ResponseType.CLOSE)
+        self.assertEquals(Gtk.STOCK_CLOSE, button.get_label())
 



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