summaryrefslogtreecommitdiff
path: root/GstInspector/GUI/pages.py
blob: 08f8f985af648c8d13ff3c20aaec9e4a4c2c412a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
# -*- coding: utf-8; mode: python; -*-
#
#  GStreamer Inspector - Multimedia system plugin introspection
#
#  Copyright (C) 2007 René Stadler <mail@renestadler.de>
#
#  This program is free software; you can redistribute it and/or modify it
#  under the terms of the GNU General Public License as published by the Free
#  Software Foundation; either version 3 of the License, or (at your option)
#  any later version.
#
#  This program 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 General Public License for
#  more details.
#
#  You should have received a copy of the GNU General Public License along with
#  this program.  If not, see <http://www.gnu.org/licenses/>.

"""GStreamer Inspector GUI.pages module."""

import logging
from gettext import ngettext

import pango
import gobject
import gtk

from GstInspector.GUI.models import (NameValueModel,
                                     PadsModel,
                                     PropertiesModel,
                                     SignalsModel)
from GstInspector.GUI.utils import (Manager,
                                    iter_container,
                                    size_group_for_tables,
                                    widget_add_popup_menu)

# Pre 2.10 GTK+ compatibility.
_gtk_has_link_buttons = hasattr (gtk, "LinkButton")

def walk_tree_model (model):

    tree_iter = model.get_iter_root ()

    while tree_iter:
        yield tree_iter
        if model.iter_has_child (tree_iter):
            tree_iter = model.iter_children (tree_iter)
        else:
            next = model.iter_next (tree_iter)
            if next:
                tree_iter = next
            else:
                tree_iter = model.iter_parent (tree_iter)
                while tree_iter:
                    next = model.iter_next (tree_iter)
                    if next:
                        tree_iter = next
                        break
                    else:
                        tree_iter = model.iter_parent (tree_iter)

class Page (object):

    name = None
    widget_name = None

    sensitive = True

    def __init__ (self, window):

        self.logger = logging.getLogger ("ui.page.%s" % (self.name,))
        self.widget = getattr (window.widgets, self.widget_name)
        self.element = None

    def update (self, element):

        self.element = element

        self.handle_update (element)

    def handle_update (self, element):

        pass

class DetailsPage (Page):

    name = "details"
    widget_name = "details_scrolled"

    def __init__ (self, window, *a, **kw):

        Page.__init__ (self, window, *a, **kw)

        widgets = window.widgets

        self.widgets = widgets

        size_group_for_tables ((widgets.plugin_details_table,
                                widgets.element_details_table,),
                               gtk.SIZE_GROUP_HORIZONTAL,
                               left_attach = 0, right_attach = 1)

        if _gtk_has_link_buttons:
            self._prepare_origin ()

    def _prepare_origin (self):

        box = self.widgets.origin_hbox
        label = self.widgets.origin_label
        label.hide ()
        self.link_button = gtk.LinkButton ("")
        self.link_button.props.xalign = 0.0
        self.link_button.connect ("clicked", self.handle_link_button_clicked)
        self.link_button.show ()

        # Little hack to make the label inside the LinkButton align with all
        # the others.  The LinkButton has a margin around the label.  A shadow
        # (border) is drawn there on mouse over only, so it looks totally odd
        # without this little adjustment.
        button_req_width = self.link_button.size_request ()[0]
        child_req_width = self.link_button.child.size_request ()[0]
        extra_pad = (button_req_width - child_req_width) / 2
        children = [label]
        children += iter_container (self.widgets.element_details_table,
                                    left_attach = 1, right_attach = 2)
        children += iter_container (self.widgets.plugin_details_table,
                                    left_attach = 1, right_attach = 2)
        for child in children:
            if child != box:
                child.props.xpad = extra_pad

        box.pack_start (self.link_button, False, False)

    def _update_origin (self, origin):

        if "://" in origin:
            is_uri = True
        else:
            is_uri = False

        if is_uri:
            self.widgets.origin_label.hide ()
            button = self.link_button
            button.props.label = origin
            button.props.uri = origin
            button.show ()
        else:
            self.link_button.hide ()
            label = self.widgets.origin_label
            label.props.label = origin
            label.show ()

    def _fix_text (self, text):

        if text is None:
            return ""
        text = text.replace ("\n", " ").replace ("  ", " ")
        return text

    def handle_link_button_clicked (self, button):

        show_uri (button.props.uri, widget = button)

    def handle_update (self, element):

        widgets = self.widgets
        plugin = element.plugin

        # Element details.

        description = self._fix_text (element.description)
        authors_h = ngettext ("Author:", "Authors:",
                              len (element.authors or ()))
        authors = "\n".join ((str (a) for a in element.authors))
        uri_protocols = ", ".join (("%s://" % (s,)
                                    for s in element.uri_protocols))
        widgets.uri_protocols_label.props.sensitive = uri_protocols and True
        if not uri_protocols:
            uri_protocols = _("Handles no URIs")
        interfaces = "\n".join (element.interfaces)
        widgets.interfaces_label.props.sensitive = interfaces and True
        if not interfaces:
            interfaces = _("Implements no interfaces")
        hierarchy = "\n".join ("  " * i + h
                               for i, h in zip (range (len (element.hierarchy)),
                                                element.hierarchy))

        widgets.element_name_label.props.label = element.name
        widgets.element_longname_label.props.label = element.longname
        widgets.element_description_label.props.label = description
        widgets.authors_h_label.props.label = authors_h
        widgets.authors_label.props.label = authors
        widgets.rank_label.props.label = str (element.rank)
        widgets.klasses_label.props.label = "/".join (element.klasses)
        widgets.uri_protocols_label.props.label = uri_protocols
        widgets.interfaces_label.props.label = interfaces
        widgets.hierarchy_label.props.label = hierarchy

        # Plugin details.

        description = self._fix_text (plugin.description)
        filename = plugin.filename
        if not filename:
            filename = "(static plugin)"

        widgets.plugin_name_label.props.label = plugin.name
        widgets.plugin_description_label.props.label = description
        widgets.filename_label.props.label = filename
        widgets.version_label.props.label = plugin.version
        widgets.license_label.props.label = plugin.license
        widgets.source_label.props.label = plugin.source
        widgets.package_label.props.label = plugin.package
        if not _gtk_has_link_buttons:
            widgets.origin_label.props.label = plugin.origin
        else:
            self._update_origin (plugin.origin)

class SpanningCellRenderer (gtk.GenericCellRenderer):

    DEBUG_DISABLE_DRAW_SPACING = False

    def __init__ (self, *a, **kw):

        gtk.GenericCellRenderer.__init__ (self, *a, **kw)

        self.child_cells = []
        self.spacing = 0
        self.connect ("notify::is-expander", self.handle_notify_is_expander)
        self.connect ("notify::is-expanded", self.handle_notify_is_expanded)

    def add_cell (self, cell):

        if len (self.child_cells) == 2:
            raise NotImplementedError ("can only handle two child cells for now")

        self.child_cells.append (cell)

    def handle_notify_is_expander (self, prop, value):

        for child in self.child_cells:
            child.props.is_expander = value

    def handle_notify_is_expanded (self, prop, value):

        for child in self.child_cells:
            child.props.is_expanded = value

    def on_get_size (self, widget, area = None):

        visible_cells = [c for c in self.child_cells if c.props.visible]
        width, height = 0, 0

        for child in visible_cells:
            cx, cy, cw, ch = child.get_size (widget)
            width += cw
            height = max (height, ch)

        if visible_cells:
            width += self.spacing * (len (visible_cells) - 1)

        return (0, 0, width, height,)

    @staticmethod
    def get_child_width (cell, widget):

        if cell.props.width > 0:
            return cell.props.width
        return cell.get_size (widget)[2]

    def on_render (self, window, widget, background_area, cell_area, expose_area, flags):

        if not self.child_cells:
            return

        if len (self.child_cells) == 2:
            child1, child2 = self.child_cells
        else:
            child1 = self.child_cells[0]
            child2 = None
        if not child1.props.visible and (not child2 or not child2.props.visible):
            return
        if not child2 or not child2.props.visible:
            child1.render (window, widget, background_area, cell_area, expose_area, flags)
        elif not child1.props.visible:
            child2.render (window, widget, background_area, cell_area, expose_area, flags)
        else:
            child1_width = self.get_child_width (child1, widget)

            cell_area1 = cell_area.copy ()
            cell_area2 = cell_area.copy ()

            bg_area1 = background_area.copy ()
            bg_area2 = background_area.copy ()

            cell_area1.width = child1_width
            cell_area2.x += child1_width + self.spacing
            cell_area2.width -= child1_width

            bg_area1.width = cell_area1.x + cell_area1.width - bg_area1.x
            if not self.DEBUG_DISABLE_DRAW_SPACING:
                bg_area1.width += self.spacing // 2

            bg_area2.x = bg_area1.x + bg_area1.width
            bg_area2.width -= bg_area1.width
            if self.DEBUG_DISABLE_DRAW_SPACING:
                bg_area2.x += self.spacing
                bg_area2.width -= self.spacing

            child1.render (window, widget, bg_area1, cell_area1, expose_area, flags)
            child2.render (window, widget, bg_area2, cell_area2, expose_area, flags)

    def on_activate (self, *args, **kwargs):

        raise NotImplementedError ("activation is not implemented")

    def on_start_editing (self, *args, **kwargs):

        raise NotImplementedError ("editing is not implemented")

class NameValuePageBase (Page):

    """Base class for pages that display a tree view populated with name/value
    pairs."""

    DEBUG_DRAW_COLORED_AREAS = False

    CELL_SPACING = 12

    view_name = None

    def __init__ (self, window):

        Page.__init__ (self, window)

        self.view = view = getattr (window.widgets, self.view_name)

        self.old_size = None

        view.connect ("row-collapsed", self.handle_view_row_collapsed)
        view.connect ("row-expanded", self.handle_view_row_expanded)
        view.connect ("row-activated", self.handle_view_row_activated)
        view.connect ("notify::style", self.handle_view_notify_style)
        view.connect ("notify::model", self.handle_view_notify_model)

        self.name_cell_width = 0
        self.value_cell_width = 0
        self.cached_name_widths = {}
        self.cached_name_width = None

        self.tree_view_states = {}
        self.collapsed_rows = []
        self.expanded_rows = []

        cell = SpanningCellRenderer ()
        self.cell = cell
        cell.spacing = self.CELL_SPACING

        sub_cell = gtk.CellRendererText ()
        # Storing xpad in instance to speed up the cell data function:
        self.name_xpad = sub_cell.props.xpad
        cell.add_cell (sub_cell)

        sub_cell = gtk.CellRendererText ()
        cell.add_cell (sub_cell)

        self.set_default_cell_data ()

        column = gtk.TreeViewColumn ("")
        self.column = column
        column.connect ("notify::width", self.handle_column_notify_width)
        column.props.sizing = gtk.TREE_VIEW_COLUMN_FIXED
        column.pack_start (cell, True)
        column.set_cell_data_func (cell, self.cell_data_function)
        view.append_column (column)

        self.update_style_values ()

        widget_add_popup_menu (self.view, window.name_value_popup)

    def update_style_values (self):

        self.expander_size = self.view.style_get_property ("expander-size")
        self.h_separator = self.view.style_get_property ("horizontal-separator")
        self.focus_line_width = self.view.style_get_property ("focus-line-width")
        try:
            self.level_indentation = self.view.props.level_indentation
            self.show_expanders = self.view.props.show_expanders
        except AttributeError:
            # These properties were added during the 2.10 series.
            self.level_indentation = 0
            self.show_expanders = True

        EXPANDER_EXTRA_PADDING = 4
        self.expander_size += EXPANDER_EXTRA_PADDING

    def get_indentation (self, depth):

        if self.show_expanders:
            expander = self.expander_size * (depth + 1)
        elif self.show_expanders:
            expander = self.expander_size
        else:
            expander = 0

        if depth > 0:
            level = self.level_indentation * (depth - 1)
        else:
            level = 0

        return expander + level

    def set_default_cell_data (self, intrinsic = False):

        name_cell, value_cell = self.cell.child_cells
        name_props = name_cell.props
        value_props = value_cell.props

        name_props.ellipsize = pango.ELLIPSIZE_END
        if intrinsic:
            name_props.ellipsize = pango.ELLIPSIZE_NONE
            name_props.wrap_width = -1
            name_props.width = -1
        else:
            name_props.ellipsize = pango.ELLIPSIZE_END
        if self.DEBUG_DRAW_COLORED_AREAS:
            name_props.cell_background = "blue"
            name_props.background = "yellow"

        value_props.wrap_mode = pango.WRAP_WORD_CHAR
        if intrinsic:
            value_props.wrap_width = -1
            value_props.width = -1
        else:
            xpad = value_props.xpad
            value_props.width = self.value_cell_width
            value_props.wrap_width = max (0, self.value_cell_width - xpad * 2)

        if self.DEBUG_DRAW_COLORED_AREAS:
            value_props.cell_background = "red"
            value_props.background = "green"

    def cell_data_function (self, column, cell, model, tree_iter, intrinsic = False):

        name, value = model.get (tree_iter,
                                 NameValueModel.COL_NAME,
                                 NameValueModel.COL_VALUE)
        depth = model.iter_depth (tree_iter)

        name_cell, value_cell = cell.child_cells
        name_props = name_cell.props
        value_props = value_cell.props

        name_props.text = name
        if model.iter_has_child (tree_iter):
            # Line up with expander.
            name_props.yalign = 0.5
        else:
            name_props.yalign = 0.0

        value_props.text = value

        if depth != 0 and value is not None:
            name_props.weight = pango.WEIGHT_NORMAL
        else:
            # depth == 0 or value is None
            name_props.weight = pango.WEIGHT_BOLD

        if value:
            value_props.visible = True
            if not intrinsic:
                # The intrinsic width includes the indentation, now we subtract
                # it again:
                indentation = self.get_indentation (depth)
                name_width = max (0, self.name_cell_width - indentation)
        else:
            value_props.visible = False
            if not intrinsic:
                name_width = self.name_cell_width + self.value_cell_width

        if not intrinsic:
            name_props.width = name_width
            name_props.wrap_width = max (-1, name_width - self.name_xpad * 2)

    def compute_intrinsic_name_width (self):

        model = self.view.props.model
        name_cell, value_cell = self.cell.child_cells

        if not model:
            return 0

        self.set_default_cell_data (intrinsic = True)

        name_width = 0
        for tree_iter in walk_tree_model (model):

            name, value = model.get (tree_iter,
                                     NameValueModel.COL_NAME,
                                     NameValueModel.COL_VALUE)
            if value:
                # The name cell does not span.
                depth = model.iter_depth (tree_iter)
                # The computed width depends on the values of just two renderer
                # properties: 'text' and whether 'weight' is set to bold.  We
                # replicate the logic from the data function here to avoid
                # calling it (pushing property values into the cell renderer is
                # surprisingly slow).
                vals = (name, depth == 0 or value is None,)
                if vals in self.cached_name_widths:
                    w = self.cached_name_widths[vals]
                else:
                    self.cell_data_function (self.column, self.cell, model, tree_iter,
                                             intrinsic = True)
                    w = name_cell.get_size (self.view, None)[2]
                    self.cached_name_widths[vals] = w
                name_width = max (name_width, w + self.get_indentation (depth))

        # Restore constant renderer properties for regular rendering.
        self.set_default_cell_data (intrinsic = False)

        return name_width

    def value_width_if_less_than (self, max_width):

        model = self.view.props.model
        value_cell = self.cell.child_cells[1]

        if not model:
            return 0

        self.set_default_cell_data (intrinsic = True)
        try:
            value_width = 0
            for tree_iter in walk_tree_model (model):

                self.cell_data_function (self.column, self.cell, model, tree_iter,
                                         intrinsic = True)
                width = value_cell.get_size (self.view, None)[2]
                if width > max_width:
                    return width
                value_width = max (width, value_width)
        finally:
            self.set_default_cell_data (intrinsic = False)

        return value_width

    def handle_view_row_activated (self, view, tree_path, column):

        expanded = view.row_expanded (tree_path)

        if expanded:
            view.collapse_row (tree_path)
        else:
            view.expand_row (tree_path, True)

    def handle_column_notify_width (self, column, gparam):

        self.update_style_values ()
        self.recompute_sizes (resize = True)

    def handle_view_notify_style (self, view, gparam):

        self.update_style_values ()
        self.cached_name_widths.clear ()
        self.recompute_sizes (force = True)

    def handle_view_notify_model (self, view, gparam):

        model = view.props.model

        if not model:
            return

        self.recompute_sizes (force = True)

        if len (model):
            self.sensitive = True
            view.props.sensitive = True
        else:
            self.sensitive = False
            view.props.sensitive = False

        view.expand_all ()

    def recompute_sizes (self, force = False, resize = False):

        width = self.column.props.width
        if not force and self.old_size == width:
            return
        self.old_size = width

        spacing = self.CELL_SPACING

        if force or self.cached_name_width is None:
            self.cached_name_width = self.compute_intrinsic_name_width ()
        name_intrinsic = self.cached_name_width
        if name_intrinsic:
            width -= spacing + self.h_separator + self.focus_line_width
        if name_intrinsic < width // 2:
            self.name_cell_width = name_intrinsic
            self.value_cell_width = width - name_intrinsic
        else:
            limit = width - name_intrinsic
            value_width = self.value_width_if_less_than (limit)
            if value_width < limit:
                self.name_cell_width = name_intrinsic
                self.value_cell_width = width - name_intrinsic
            else:
                limit = width // 2
                value_width = self.value_width_if_less_than (limit)
                if value_width < limit:
                    self.name_cell_width = width - value_width
                    self.value_cell_width = value_width
                else:
                    self.name_cell_width = width // 2
                    self.value_cell_width = width - self.name_cell_width

        if self.name_cell_width < 0:
            self.name_cell_width = 0
        if self.value_cell_width < 0:
            self.value_cell_width = 0

        self.set_default_cell_data ()

        if resize:
            # Ensure that the rows receive correct heights (needed since we
            # implement wrap widths depending on the column width):
            self.column.queue_resize ()

    def expand_all (self):

        self.view.expand_all ()

    def collapse_all (self):

        self.view.collapse_all ()

    def handle_view_row_collapsed (self, view, tree_iter, path):

        try:
            self.expanded_rows.remove (path)
        except ValueError:
            self.collapsed_rows.append (path)

    def handle_view_row_expanded (self, view, tree_iter, path):

        try:
            self.collapsed_rows.remove (path)
        except ValueError:
            self.expanded_rows.append (path)

    def save_tree_view_state (self):

        model = self.view.props.model
        if model is None:
            return

        selection = self.view.get_selection ()
        selected_rows = selection.get_selected_rows ()[1]

        top_row = None
        if self.view.flags () & gtk.MAPPED:
            vis_range = self.view.get_visible_range ()
            if vis_range:
                top_row = vis_range[0]
                if top_row == (0,):
                    top_row = None

        state = {"selected-rows" : selected_rows,
                 "top-row" : top_row,
                 "expanded-rows" : tuple (self.expanded_rows),
                 "collapsed-rows" : tuple (self.collapsed_rows)}
        self.tree_view_states[self.element.name] = state

        del self.expanded_rows[:]
        del self.collapsed_rows[:]

    def restore_tree_view_state (self):

        try:
            state = self.tree_view_states[self.element.name]
        except KeyError:
            selected_rows = []
            top_row = None
        else:
            selected_rows = state["selected-rows"]
            top_row = state["top-row"]
            self.collapsed_rows[:] = state["collapsed-rows"]
            self.expanded_rows[:] = state["expanded-rows"]

        for path in self.collapsed_rows:
            self.view.collapse_row (path)

        for path in self.expanded_rows:
            self.view.expand_row (path, False)

        selection = self.view.get_selection ()
        selection.unselect_all ()
        try:
            for path in selected_rows:
                selection.select_path (path)
        except ValueError:
            pass

        if top_row is None:
            top_row = (0,)

        model = self.view.props.model
        if model and len (model):
            self.view.scroll_to_cell (top_row, self.column, True, 0., 0.)

    def update (self, element):

        self.view.handler_block_by_func (self.handle_view_row_collapsed)
        self.view.handler_block_by_func (self.handle_view_row_expanded)

        try:
            if self.element:
                self.save_tree_view_state ()

            Page.update (self, element)

            self.restore_tree_view_state ()

        finally:
            self.view.handler_unblock_by_func (self.handle_view_row_collapsed)
            self.view.handler_unblock_by_func (self.handle_view_row_expanded)

class PadsPage (NameValuePageBase):

    name = "pads"
    widget_name = "pads_scrolled"
    view_name = "pads_view"

    def handle_update (self, element):

        view = self.view
        model = PadsModel (element)

        view.props.model = model

class PropertiesPage (NameValuePageBase):

    name = "properties"
    widget_name = "props_scrolled"
    view_name = "props_view"

    def handle_update (self, element):

        view = self.view
        model = PropertiesModel (element)
        view.props.model = model

        for row in model:
            value = row[model.COL_VALUE]
            if value is not None:
                # Property is inherited from a parent class.
                view.collapse_row (row.path)

class SignalsPage (NameValuePageBase):

    name = "signals"
    widget_name = "signals_scrolled"
    view_name = "signals_view"

    def handle_update (self, element):

        view = self.view
        model = SignalsModel (element)

        view.props.model = model

class PageManager (Manager):

    """GUI manager that handles the element info notebook.  The individual
    notebook pages are handled by specific objects derived from the Page
    class."""

    def _get_active (self):

        notebook = self.notebook

        page_index = notebook.props.page
        widget = notebook.get_nth_page (page_index)
        page = self.pages[widget]

        return page

    def _set_active (self, page):

        notebook = self.notebook

        for i in range (notebook.get_n_pages ()):
            widget = notebook.get_nth_page (i)
            if page.widget == widget:
                self.notebook.props.page = i
                return
        else:
            raise KeyError ("no such page %r" % (page,))

    page_classes = (DetailsPage, PadsPage, PropertiesPage, SignalsPage,)
    active = property (_get_active, _set_active)

    @classmethod
    def iter_item_classes (cls):

        return iter (cls.page_classes)

    def __init__ (self, *a, **kw):

        Manager.__init__ (self, *a, **kw)

        self.action_group = gtk.ActionGroup ("NameValueActions")
        expand_all = ("expand-all", gtk.STOCK_ZOOM_IN, _("_Expand all"),)
        collapse_all = ("collapse-all", gtk.STOCK_ZOOM_OUT, _("_Collapse all"),)
        self.action_group.add_actions ([expand_all, collapse_all])

    def attach (self, inspector_window):

        self.app = inspector_window.app
        widgets = inspector_window.widgets
        actions = inspector_window.actions

        self.notebook = widgets.element_info_book
        pages = [cls (inspector_window) for cls in self.page_classes]

        notebook_widgets = [self.notebook.get_nth_page (i)
                            for i in range (self.notebook.get_n_pages ())]
        for page in pages:
            assert page.widget in notebook_widgets

        self.pages = dict (((page.widget, page,) for page in pages))

        last_page_class = self.app.state.page
        if not last_page_class:
            last_page_class = DetailsPage

        self.active = self.find_item (name = last_page_class.name)
        self.element = None
        self.update_source = None

        action = actions.expand_all
        handler = self.handle_expand_all_action_activate
        action.connect ("activate", handler)

        action = actions.collapse_all
        handler = self.handle_collapse_all_action_activate
        action.connect ("activate", handler)

    def detach (self):

        if self.update_source is not None:
            gobject.source_remove (self.update_source)
            self.update_source = None

        self.app.state.page = self.active

    def iter_items (self):

        return self.pages.itervalues ()

    def update_iter (self):

        while True:
            for page in self.pages.values ():
                if page.element != self.element:
                    page.update (self.element)
                    self.update_label_sensitivity (page)
                    assert page.element == self.element
                    yield True
                    break
            else:
                self.update_source = None
                yield False
                return

    def update (self, element):

        self.element = element

        self.active.update (element)
        self.update_label_sensitivity (self.active)

        # Defer updating the other pages, which are invisible:
        if self.update_source is None:
            callback = self.update_iter ().next
            self.update_source = gobject.idle_add (callback)

    def update_label_sensitivity (self, page):

        tab_label = self.notebook.get_tab_label (page.widget)
        if tab_label:
            tab_label.props.sensitive = page.sensitive

    def handle_expand_all_action_activate (self, action):

        try:
            self.active.expand_all ()
        except AttributeError:
            pass

    def handle_collapse_all_action_activate (self, action):

        try:
            self.active.collapse_all ()
        except AttributeError:
            pass

from gettext import gettext as _