~ubuntu-branches/ubuntu/trusty/pitivi/trusty

« back to all changes in this revision

Viewing changes to pitivi/ui/glade.py

  • Committer: Bazaar Package Importer
  • Author(s): Daniel Holbach
  • Date: 2006-02-04 14:42:30 UTC
  • Revision ID: james.westby@ubuntu.com-20060204144230-9ihvyas6lhgn81k1
Tags: upstream-0.9.9.2
ImportĀ upstreamĀ versionĀ 0.9.9.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# PiTiVi , Non-linear video editor
 
2
#
 
3
#       ui/glade.py
 
4
#
 
5
# Copyright (C) 2004,2005 Fluendo, S.L. (www.fluendo.com). All rights reserved.
 
6
# Copyright (c) 2005, Edward Hervey <bilboed@bilboed.com>
 
7
#
 
8
# This program is free software; you can redistribute it and/or
 
9
# modify it under the terms of the GNU Lesser General Public
 
10
# License as published by the Free Software Foundation; either
 
11
# version 2.1 of the License, or (at your option) any later version.
 
12
#
 
13
# This program is distributed in the hope that it will be useful,
 
14
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
15
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
16
# Lesser General Public License for more details.
 
17
#
 
18
# You should have received a copy of the GNU Lesser General Public
 
19
# License along with this program; if not, write to the
 
20
# Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 
21
# Boston, MA 02111-1307, USA.
 
22
#
 
23
 
 
24
import os
 
25
import sys
 
26
 
 
27
import gtk
 
28
import gtk.glade
 
29
import gobject
 
30
 
 
31
# proc := module1.module2.moduleN.proc1().maybe_another_proc()
 
32
#  -> eval proc1().maybe_another_proc() in module1.module2.moduleN
 
33
def flumotion_glade_custom_handler(xml, proc, name, *args):
 
34
    def takewhile(proc, l):
 
35
        ret = []
 
36
        while l and proc(l[0]):
 
37
            ret.append(l[0])
 
38
            l.remove(l[0])
 
39
        return ret
 
40
 
 
41
    def parse_proc(proc):
 
42
        parts = proc.split('.')
 
43
        assert len(parts) > 1
 
44
        modparts = takewhile(str.isalnum, parts)
 
45
        assert modparts and parts
 
46
        return '.'.join(modparts), '.'.join(parts)
 
47
 
 
48
    module, code = parse_proc(proc)
 
49
    try:
 
50
        __import__(module)
 
51
    except Exception, e:
 
52
        raise RuntimeError('Failed to load module %s: %s' % (module, e))
 
53
 
 
54
    try:
 
55
        w = eval(code, sys.modules[module].__dict__)
 
56
    except Exception, e:
 
57
        raise RuntimeError('Failed call %s in module %s: %s'
 
58
                           % (code, module, e))
 
59
    w.set_name(name)
 
60
    w.show()
 
61
    return w
 
62
gtk.glade.set_custom_handler(flumotion_glade_custom_handler)
 
63
 
 
64
 
 
65
class GladeWidget(gtk.VBox):
 
66
    '''
 
67
    Base class for composite widgets backed by glade interface definitions.
 
68
 
 
69
    Example:
 
70
    class MyWidget(GladeWidget):
 
71
        glade_file = 'my_glade_file.glade'
 
72
        ...
 
73
 
 
74
    Remember to chain up if you customize __init__().
 
75
    '''
 
76
        
 
77
    glade_dir = os.path.dirname(os.path.abspath(__file__))
 
78
    glade_file = None
 
79
    glade_typedict = None
 
80
 
 
81
    def __init__(self):
 
82
        gtk.VBox.__init__(self)
 
83
        try:
 
84
            assert self.glade_file
 
85
            filepath = os.path.join(self.glade_dir, self.glade_file)
 
86
            if self.glade_typedict:
 
87
                wtree = gtk.glade.XML(filepath, typedict=self.glade_typedict)
 
88
            else:
 
89
                # pygtk 2.4 doesn't like typedict={} ?
 
90
                wtree = gtk.glade.XML(filepath)
 
91
        except RuntimeError, e:
 
92
            raise RuntimeError('Failed to load file %s from directory %s: %s'
 
93
                               % (self.glade_file, self.glade_dir, e))
 
94
 
 
95
        win = None
 
96
        for widget in wtree.get_widget_prefix(''):
 
97
            wname = widget.get_name()
 
98
            if isinstance(widget, gtk.Window):
 
99
                assert win == None
 
100
                win = widget
 
101
                continue
 
102
            
 
103
            if hasattr(self, wname) and getattr(self, wname):
 
104
                raise AssertionError (
 
105
                    "There is already an attribute called %s in %r" %
 
106
                    (wname, self))
 
107
            setattr(self, wname, widget)
 
108
 
 
109
        assert win != None
 
110
        w = win.get_child()
 
111
        win.remove(w)
 
112
        self.add(w)
 
113
        win.destroy()
 
114
        wtree.signal_autoconnect(self)
 
115
 
 
116
 
 
117
class GladeWindow(gobject.GObject):
 
118
    """
 
119
    Base class for dialogs or windows backed by glade interface definitions.
 
120
 
 
121
    Example:
 
122
    class MyWindow(GladeWindow):
 
123
        glade_file = 'my_glade_file.glade'
 
124
        ...
 
125
 
 
126
    Remember to chain up if you customize __init__(). Also note that GladeWindow
 
127
    does *not* descend from GtkWindow, so you can't treat the resulting object
 
128
    as a GtkWindow. The show, hide, destroy, and present methods are provided as
 
129
    convenience wrappers.
 
130
    """
 
131
 
 
132
    glade_dir = os.path.dirname(os.path.abspath(__file__))
 
133
    glade_file = None
 
134
    glade_typedict = None
 
135
 
 
136
    window = None
 
137
 
 
138
    def __init__(self, parent=None):
 
139
        gobject.GObject.__init__(self)
 
140
        try:
 
141
            assert self.glade_file
 
142
            filepath = os.path.join(self.glade_dir, self.glade_file)
 
143
            if self.glade_typedict:
 
144
                wtree = gtk.glade.XML(filepath, typedict=self.glade_typedict)
 
145
            else:
 
146
                # pygtk 2.4 doesn't like typedict={} ?
 
147
                wtree = gtk.glade.XML(filepath)
 
148
        except RuntimeError, e:
 
149
            raise RuntimeError('Failed to load file %s from directory %s: %s'
 
150
                               % (self.glade_file, self.glade_dir, e))
 
151
 
 
152
        self.widgets = {}
 
153
        for widget in wtree.get_widget_prefix(''):
 
154
            wname = widget.get_name()
 
155
            if isinstance(widget, gtk.Window):
 
156
                assert self.window == None
 
157
                self.window = widget
 
158
                continue
 
159
            
 
160
            if wname in self.widgets:
 
161
                raise AssertionError("Two objects with same name (%s): %r %r"
 
162
                                     % (wname, self.widgets[wname], widget))
 
163
            self.widgets[wname] = widget
 
164
 
 
165
        if parent:
 
166
            self.window.set_transient_for(parent)
 
167
 
 
168
        wtree.signal_autoconnect(self)
 
169
 
 
170
        self.show = self.window.show
 
171
        self.hide = self.window.hide
 
172
        self.present = self.window.present
 
173
        self.run = self.window.run
 
174
 
 
175
    def destroy(self):
 
176
        self.window.destroy()
 
177
        del self.window
 
178