Commit 0453702f authored by Jean Felder's avatar Jean Felder

gtk overrides: Make GTK4 widgets iterable

GTK4 removed the Gtk.Container interface, and added API on Gtk.Widget
to iterate over children instead.

Making Widgets iterable allows to easily loop over the children and
check is a widget is a child of an other one.

A new test is also added.
parent d1221d0a
Pipeline #232507 passed with stages
in 15 minutes and 9 seconds
......@@ -135,6 +135,16 @@ class Widget(Gtk.Widget):
translate_coordinates = strip_boolean_result(Gtk.Widget.translate_coordinates)
if GTK4:
def __contains__(self, child):
return child in list(self)
def __iter__(self):
child = self.get_first_child()
while child:
yield child
child = child.get_next_sibling()
if GTK2 or GTK3:
def freeze_child_notify(self):
super(Widget, self).freeze_child_notify()
......
......@@ -902,6 +902,37 @@ class TestGtk(unittest.TestCase):
pixbuf = button.render_icon(Gtk.STOCK_OK, Gtk.IconSize.BUTTON)
self.assertTrue(pixbuf is not None)
@unittest.skipUnless(
Gtk_version == "4.0", "GtkWidget prior to gtk4 is not iterable")
def test_widget_iterable(self):
widget = Gtk.Box()
children = [child for child in widget]
self.assertEqual(len(children), 0)
nr_children = 7
for i in range(nr_children):
widget.append(Gtk.Label())
children = [child for child in widget]
self.assertEqual(len(children), nr_children)
first_child = children[0]
last_child = children[-1]
self.assertEqual(first_child, widget.get_first_child())
self.assertEqual(last_child, widget.get_last_child())
self.assertTrue(first_child in widget)
self.assertTrue(last_child in widget)
widget.remove(first_child)
self.assertTrue(first_child not in widget)
self.assertTrue(last_child in widget)
widget.remove(last_child)
self.assertTrue(first_child not in widget)
self.assertTrue(last_child not in widget)
@unittest.skipUnless(Gtk, 'Gtk not available')
class TestWidget(unittest.TestCase):
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment