~ubuntu-branches/ubuntu/jaunty/pida/jaunty

« back to all changes in this revision

Viewing changes to pida/pidagtk/contentview.py

  • Committer: Bazaar Package Importer
  • Author(s): Barry deFreese
  • Date: 2006-08-01 13:08:56 UTC
  • mfrom: (0.1.2 etch) (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20060801130856-v92ktopgdxc8rv7q
Tags: 0.3.1-2ubuntu1
* Re-sync with Debian
* Remove bashisms from debian/rules

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# -*- coding: utf-8 -*- 
 
2
 
 
3
# vim:set shiftwidth=4 tabstop=4 expandtab textwidth=79:
 
4
#Copyright (c) 2005 Ali Afshar aafshar@gmail.com
 
5
 
 
6
#Permission is hereby granted, free of charge, to any person obtaining a copy
 
7
#of this software and associated documentation files (the "Software"), to deal
 
8
#in the Software without restriction, including without limitation the rights
 
9
#to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 
10
#copies of the Software, and to permit persons to whom the Software is
 
11
#furnished to do so, subject to the following conditions:
 
12
 
 
13
#The above copyright notice and this permission notice shall be included in
 
14
#all copies or substantial portions of the Software.
 
15
 
 
16
#THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 
17
#IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 
18
#FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 
19
#AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 
20
#LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 
21
#OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 
22
#SOFTWARE.
 
23
 
 
24
# pidagtk import(s)
 
25
import toolbar
 
26
import paned
 
27
 
 
28
# system imports
 
29
import time
 
30
 
 
31
# gtk imports
 
32
import gtk
 
33
import gobject
 
34
 
 
35
 
 
36
class content_view(gtk.VBox):
 
37
    __gsignals__ = {'short-title-changed': (
 
38
                        gobject.SIGNAL_RUN_LAST,
 
39
                        gobject.TYPE_NONE,
 
40
                        ()),
 
41
                    'long-title-changed': (
 
42
                        gobject.SIGNAL_RUN_LAST,
 
43
                        gobject.TYPE_NONE,
 
44
                        ()),
 
45
                    'action' : (
 
46
                        gobject.SIGNAL_RUN_LAST,
 
47
                        gobject.TYPE_NONE,
 
48
                        (gobject.TYPE_PYOBJECT,)),
 
49
                    'removed' : (
 
50
                        gobject.SIGNAL_RUN_LAST,
 
51
                        gobject.TYPE_NONE,
 
52
                        ()),
 
53
                    'raised' : (
 
54
                        gobject.SIGNAL_RUN_LAST,
 
55
                        gobject.TYPE_NONE,
 
56
                        ())}
 
57
    
 
58
    ICON_NAME = None
 
59
 
 
60
    SHORT_TITLE = None
 
61
    LONG_TITLE = None
 
62
 
 
63
    HAS_CONTROL_BOX = True
 
64
    HAS_CLOSE_BUTTON = True
 
65
    HAS_DETACH_BUTTON = True
 
66
 
 
67
    HAS_TITLE = True
 
68
 
 
69
    HAS_TOOLBAR = True
 
70
 
 
71
    WIDGET_TYPE = None
 
72
 
 
73
    BUTTONS = []
 
74
 
 
75
    def __init__(self, service, prefix, widget=None, icon_name=None,
 
76
                 short_title=None, **kw):
 
77
        gtk.VBox.__init__(self)
 
78
        self.__uid = time.time()
 
79
        self.__service = service
 
80
        self.__prefix = prefix
 
81
        self.__init_icon(icon_name)
 
82
        self.__init_short_title(short_title)
 
83
        self.__init_long_title()
 
84
        self.__init_widgets(widget)
 
85
        self.__holder = None
 
86
        self.set_size_request(0,0)
 
87
        self.init(**kw)
 
88
 
 
89
    def __init_icon(self, icon_name):
 
90
        if icon_name is not None:
 
91
            self.icon_name = icon_name
 
92
        elif self.ICON_NAME is not None:
 
93
            self.icon_name = self.ICON_NAME
 
94
        else:
 
95
            self.icon_name = 'terminal'
 
96
 
 
97
    def __init_short_title(self, short_title):
 
98
        if short_title:
 
99
            self.short_title = short_title
 
100
        elif self.SHORT_TITLE:
 
101
            self.short_title = self.SHORT_TITLE
 
102
        else:
 
103
            self.short_title = 'Untitled'
 
104
 
 
105
    def __init_long_title(self):
 
106
        if self.LONG_TITLE is not None:
 
107
            self.long_title = self.LONG_TITLE
 
108
        else:
 
109
            self.long_title = 'Long title'
 
110
 
 
111
    def __init_widgets(self, widget):
 
112
        topbar = gtk.VBox()
 
113
        topbar.show()
 
114
        self.pack_start(topbar, expand=False)
 
115
        
 
116
        titlebar = gtk.HBox()
 
117
        titlebar.show()
 
118
        self.pack_start(titlebar, expand=False)
 
119
        
 
120
        if widget is not None:
 
121
            self.pack_start(widget)
 
122
            self.__widget = widget
 
123
        elif self.WIDGET_TYPE is not None:
 
124
            self.__widget = self.WIDGET_TYPE
 
125
            self.pack_start(self.__widget)
 
126
        else:
 
127
            self.__widget = gtk.VBox()
 
128
            self.pack_start(self.__widget)
 
129
        
 
130
        self.__widget.show()
 
131
        self.__init_topbar(topbar)
 
132
        
 
133
    def __init_topbar(self, topbar):
 
134
        self.__toolbar_area = gtk.HBox()
 
135
        self.__toolbar_area.show()
 
136
        
 
137
        topbar.pack_start(self.__toolbar_area, expand=False)
 
138
        # TODO: check if toolbar.Toolbar needs work too
 
139
        self.__toolbar = toolbar.Toolbar()
 
140
        self.__toolbar.show()
 
141
        
 
142
        self.__toolbar_area.pack_start(self.__toolbar, expand=False)
 
143
        self.__toolbar.connect('clicked', self.cb_toolbar_clicked)
 
144
        for name, icon, tooltip in self.BUTTONS:
 
145
            self.__toolbar.add_button(name, icon, tooltip)
 
146
        if self.HAS_CONTROL_BOX:
 
147
            if self.HAS_DETACH_BUTTON:
 
148
                detbut = paned.sizer('menu', tooltip='Detach this view')
 
149
                #self.__toolbar_area.pack_start(detbut, expand=False)
 
150
                detbut.connect('clicked',
 
151
                            self.cb_controlbar_detach_clicked)
 
152
        self.__long_title_label = gtk.Label(self.__long_title)
 
153
        self.__long_title_label.show()
 
154
        
 
155
        if self.HAS_TITLE:
 
156
            self.__toolbar_area.pack_start(self.__long_title_label, padding=6)
 
157
            self.__long_title_label.set_alignment(0.0, 0.5)
 
158
            self.__long_title_label.set_selectable(True)
 
159
        if self.HAS_CONTROL_BOX:
 
160
            if self.HAS_CLOSE_BUTTON:
 
161
                align = gtk.Alignment()
 
162
                align.show()
 
163
                self.__toolbar_area.pack_start(align)
 
164
                
 
165
                closebut = paned.sizer('close', tooltip='Close this view')
 
166
                closebut.show()
 
167
                
 
168
                self.__toolbar_area.pack_start(closebut, expand=False)
 
169
                closebut.connect('clicked',
 
170
                            self.cb_controlbar_close_clicked)
 
171
                self.__close_button = closebut
 
172
 
 
173
    def init(self):
 
174
        pass
 
175
 
 
176
    def toolbar_add_button(self, name, icon, tooltip):
 
177
        self.__toolbar.add_button(name, icon, tooltip)
 
178
 
 
179
    def toolbar_add_widget(self, widget, **kw):
 
180
        widget.show()
 
181
        self.__toolbar.add_widget(widget, **kw)
 
182
 
 
183
    def toolbar_add_separator(self):
 
184
        self.__toolbar.add_separator()
 
185
 
 
186
    def close(self):
 
187
        self.remove()
 
188
 
 
189
    def remove(self):
 
190
        if self.__holder is not None:
 
191
            self.__holder.remove_page(self)
 
192
    
 
193
    def detach(self):
 
194
        if self.__holder is not None:
 
195
            self.__holder.detach_page(self)
 
196
 
 
197
    def raise_page(self):
 
198
        if self.__holder is not None:
 
199
            self.__holder.set_page(self)
 
200
 
 
201
    def hide_title(self):
 
202
        self.__long_title_label.hide()
 
203
 
 
204
    def hide_controlbox(self):
 
205
        if self.HAS_CLOSE_BUTTON:
 
206
            self.__close_button.hide()
 
207
 
 
208
    def cb_toolbar_clicked(self, toolbar, name):
 
209
        func = 'cb_%s_toolbar_clicked_%s' % (self.__prefix, name)
 
210
        cb = getattr(self.service, func, None)
 
211
        if cb is not None:
 
212
            cb(self, toolbar, name)
 
213
        self.emit('action', name)
 
214
 
 
215
    def cb_controlbar_close_clicked(self, controlbox):
 
216
        self.__controlbar_clicked('close')
 
217
 
 
218
    def cb_controlbar_detach_clicked(self, controlbox):
 
219
        self.__controlbar_clicked('detach')
 
220
 
 
221
    def __controlbar_clicked(self, name):
 
222
        func = 'cb_%s_controlbar_clicked_%s' % (self.__prefix, name)
 
223
        cb = getattr(self.service, func, None)
 
224
        if cb is not None:
 
225
            cb(self, toolbar, name)
 
226
 
 
227
    def get_service(self):
 
228
        return self.__service
 
229
    service = property(get_service)
 
230
    
 
231
    def get_unique_id(self):
 
232
        return self.__uid
 
233
    unique_id = property(get_unique_id)
 
234
 
 
235
    def get_short_title(self):
 
236
        return self.__short_title
 
237
    def set_short_title(self, value):
 
238
        self.__short_title = value
 
239
        self.emit('short-title-changed')
 
240
    short_title = property(get_short_title, set_short_title)
 
241
 
 
242
    def get_long_title(self):
 
243
        return self.__long_title
 
244
    def set_long_title(self, value):
 
245
        self.__long_title = value
 
246
        try:
 
247
            self.__long_title_label.set_label(value)
 
248
        except AttributeError:
 
249
            pass
 
250
        self.emit('long-title-changed')
 
251
    long_title = property(get_long_title, set_long_title)
 
252
 
 
253
    def get_icon_name(self):
 
254
        return self.__icon_name
 
255
    def set_icon_name(self, value):
 
256
        self.__icon_name = value
 
257
    icon_name = property(get_icon_name, set_icon_name)
 
258
    
 
259
    def get_icon(self):
 
260
        return self.create_icon()
 
261
    icon = property(get_icon)
 
262
 
 
263
    def get_holder(self):
 
264
        return self.__holder
 
265
    def set_holder(self, value):
 
266
        self.__holder = value
 
267
    holder = property(get_holder, set_holder)
 
268
 
 
269
    def get_widget(self):
 
270
        return self.__widget
 
271
    widget = property(get_widget)
 
272
 
 
273
    def get_prefix(self):
 
274
        return self.__prefix
 
275
    prefix = property(get_prefix)
 
276
 
 
277
    def create_icon(self):
 
278
        import icons
 
279
        icon = icons.icons.get_image(self.icon_name)
 
280
        eb = self.create_tooltip_box()
 
281
        eb.add(icon)
 
282
        return eb
 
283
 
 
284
    def create_tooltip_box(self):
 
285
        eb = gtk.EventBox()
 
286
        eb.add_events(gtk.gdk.BUTTON_PRESS_MASK)
 
287
        def _click(_eb, event):
 
288
            if event.button == 3:
 
289
                self.create_detach_popup(event)
 
290
                return True
 
291
        eb.connect('button-press-event', _click)
 
292
        return eb
 
293
 
 
294
    def create_detach_popup(self, event):
 
295
        if self.HAS_CONTROL_BOX and (self.HAS_CLOSE_BUTTON or
 
296
                                    self.HAS_DETACH_BUTTON):
 
297
            menu = gtk.Menu()
 
298
            if self.HAS_DETACH_BUTTON:
 
299
                act = gtk.Action(name='detach',
 
300
                                 label='Detach',
 
301
                                 tooltip='Detach this view',
 
302
                                 stock_id='gtk-up')
 
303
                def _det(_act):
 
304
                    self.__controlbar_clicked('detach')
 
305
                act.connect('activate', _det)
 
306
                mi = act.create_menu_item()
 
307
                menu.add(mi)
 
308
            if self.HAS_CLOSE_BUTTON:
 
309
                act = gtk.Action(name='close',
 
310
                                 label='Close',
 
311
                                 tooltip='Close this view',
 
312
                                 stock_id=gtk.STOCK_CLOSE)
 
313
                def _close(_act):
 
314
                    self.__controlbar_clicked('close')
 
315
                act.connect('activate', _close)
 
316
                mi = act.create_menu_item()
 
317
                menu.add(mi)
 
318
            menu.show_all()
 
319
            menu.popup(None, None, None, event.button, event.time)
 
320
        
 
321
 
 
322
gobject.type_register(content_view)