~ubuntu-branches/ubuntu/saucy/pida/saucy

« back to all changes in this revision

Viewing changes to src/plugin.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
 
# vim:set shiftwidth=4 tabstop=4 expandtab textwidth=79:
3
 
# $Id: plugin.py 431 2005-07-21 21:42:14Z aafshar $
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
 
"""This module provides the base plugin superclass"""
25
 
 
26
 
# GTK import
27
 
import gtk
28
 
# Pida import
29
 
import gtkextra
30
 
import base
31
 
 
32
 
class Plugin(base.pidaobject):
33
 
    """ The base plugin class. 
34
 
    
35
 
        This class is to be overriden for any object that wishes to have a
36
 
        reference to the main Application object, or wishes to receive events
37
 
        generated by the main Application object.
38
 
    """
39
 
    # Class attributes for overriding.
40
 
    # The name of the plugin.
41
 
    NAME = 'Plugin'
42
 
    # The icon in the top left
43
 
    ICON = 'fullscreen'
44
 
    # The alternative icon for tabs and tooltip
45
 
    DICON = 'fullscreen', 'Detach window.'
46
 
    # Whether the plugin is detachable.
47
 
    DETACHABLE = False
48
 
    VISIBLE = True
49
 
 
50
 
    #def __init__(self, cb):
51
 
    #    """ 
52
 
    #    Constructor
53
 
    #    
54
 
    #    @param cb: An instance of the main application class.
55
 
    #    @type cb: C{pida.main.Application}
56
 
#
57
 
#        @note: It is recommended that to add additional widgets to the plugin,
58
 
#        that the populate_widgets method is overriden instead of this
59
 
#        constructor.
60
 
#        """
61
 
        # Instance of the Application class.
62
 
#        self.cb = cb
63
 
 
64
 
    def do_init(self):
65
 
        self.detach_window = None
66
 
        pass
67
 
 
68
 
    def configure(self, reg):
69
 
        pass
70
 
 
71
 
    def __populate__(self):
72
 
        # The main box.
73
 
        self.win = gtk.VBox()
74
 
        self.win.show()
75
 
        # The tool bar.        
76
 
        self.bar = gtk.HBox()
77
 
        self.win.pack_start(self.bar, expand=False)
78
 
        self.bar.show()
79
 
        ## The control bar.
80
 
        self.ctlbar = gtk.HBox()
81
 
        self.bar.pack_start(self.ctlbar)
82
 
        self.ctlbar.show()
83
 
        # detach button
84
 
        eb = gtk.EventBox()
85
 
        self.dtbut = gtk.ToggleToolButton(stock_id=None)
86
 
        eb.add(self.dtbut)
87
 
        self.ctlbar.pack_start(eb, expand=False)
88
 
        ic = self.do_get_image(self.DICON[0])
89
 
        self.dtbut.set_icon_widget(ic)
90
 
        self.dtbut.connect('toggled', self.cb_toggledetatch)
91
 
        self.do_set_tooltip(eb, self.DICON[1])
92
 
        # The main title label.
93
 
        self.label = gtk.Label(self.NAME)
94
 
        self.ctlbar.pack_start(self.label, expand=False)
95
 
        # The horizontal expander.
96
 
        self.sepbar = gtkextra.Sepbar()
97
 
        self.sepbar.connect(self.cb_sep_rclick, self.cb_sep_dclick)
98
 
        self.ctlbar.pack_start(self.sepbar.win, padding=6)
99
 
        # The shortcut bar.
100
 
        self.shortbar = gtk.HBox()
101
 
        self.bar.pack_start(self.shortbar, expand=False)
102
 
        # The custom tool bar.
103
 
        self.cusbar = gtkextra.Toolbar()
104
 
        self.bar.pack_start(self.cusbar.win, expand=False)
105
 
        # The holder for transient windows.
106
 
        self.transwin = gtk.VBox()
107
 
#        self.transwin.show()
108
 
        self.win.pack_start(self.transwin, expand=False)
109
 
        #message dialog
110
 
        self.msgbox = gtkextra.Messagebox()
111
 
        self.transwin.pack_start(self.msgbox.win, expand=False)
112
 
        #question dialog
113
 
        self.qstbox = gtkextra.Questionbox()
114
 
        self.transwin.pack_start(self.qstbox.win, expand=False)
115
 
        # The content area.
116
 
        self.frame = gtk.VBox()
117
 
        self.win.pack_start(self.frame)
118
 
        # The toolbar popup menu.
119
 
        self.toolbar_popup = gtkextra.Popup()
120
 
        #self.populate_widgets()
121
 
        #self.connect_widgets()
122
 
        #self.frame.show_all()
123
 
        #self.win.show_all()
124
 
    
125
 
 
126
 
    def populate_widgets(self):
127
 
        """
128
 
        Called after the constructor to populate the plugin.
129
 
        
130
 
        Override this method and add the desired widgets to the plugin.
131
 
        """
132
 
        pass
133
 
 
134
 
    def connect_widgets(self):
135
 
        """ 
136
 
        Called after widget population to connect signals.
137
 
        """
138
 
        pass
139
 
 
140
 
    def add(self, widget, *args, **kwargs):
141
 
        """
142
 
        Add a widget to the plugin.
143
 
        
144
 
        @param widget: The widget to add.
145
 
        @type widget: L{gtk.Widget}
146
 
        
147
 
        @param *args: Additional arguments to pass the pack_start method.
148
 
        @param **kwargs: Additional kwargs to pass the pack_start method.
149
 
        
150
 
        @note: use C{expand=False} in kwargs, to prevent widget expanding.
151
 
        """
152
 
        self.frame.pack_start(widget, *args, **kwargs)
153
 
 
154
 
    def add_button(self, stock, callback, tooltip='None Set!', cbargs=[]):
155
 
        """
156
 
        Add a button to the plugin toolbar and toolbar menu.
157
 
        
158
 
        @param stock: The icon name.
159
 
        @type stock: string
160
 
        
161
 
        @param callback: The function to call back on button activation
162
 
        @type callback: function
163
 
 
164
 
        @param tooltip: The tooltip to display for the button.
165
 
        @type: tooltip: string
166
 
 
167
 
        @param cbargs: A list of arguments to pass the callback function.
168
 
        @type cbargs: list
169
 
 
170
 
        @return: The newly created button.
171
 
        @rtype: L{gtk.ToolButton}
172
 
        """
173
 
        self.toolbar_popup.add_item(stock, tooltip, callback, cbargs)
174
 
        return self.cusbar.add_button(stock, callback, tooltip, cbargs)
175
 
    
176
 
    def add_separator(self):
177
 
        """
178
 
        Add a separator to the toolbar and toolbar menu.
179
 
        """
180
 
        self.toolbar_popup.add_separator()
181
 
        self.cusbar.add_separator()
182
 
        
183
 
    def message(self, message):
184
 
        """ 
185
 
        Give the user a message in a transient window.
186
 
        
187
 
        @param message: The text of the message to give the user.
188
 
        @type message: C{str}
189
 
        """
190
 
        self.msgbox.message(message)
191
 
 
192
 
    def question(self, message, callback):
193
 
        """
194
 
        Ask the user a question in a transient window.
195
 
        
196
 
        The question is popped up to the user inside the plugin, and the
197
 
        callback method is called on successful submission with the answer as
198
 
        argument.
199
 
 
200
 
        @param message: The prompt to display.
201
 
        @type message: string
202
 
 
203
 
        @param callback: The function to callback on submission.
204
 
        @type callback: function
205
 
 
206
 
        @note: the signature of the callback function will be:
207
 
 
208
 
            C{def callback_function(answer):} for functions, and
209
 
 
210
 
            C{def callback_function(self, answer):} for class methods.
211
 
        """
212
 
        self.qstbox.question(message, callback)
213
 
 
214
 
    def attach(self, *a):
215
 
        """
216
 
        Reparent the plugin in the original parent.
217
 
        """
218
 
        self.win.reparent(self.oldparent)
219
 
        self.detach_window.destroy()
220
 
        self.detach_window = None
221
 
    
222
 
    def detatch(self):
223
 
        """
224
 
        Reparent the plugin in a top-level window.
225
 
        """
226
 
        self.oldparent = self.win.get_parent()
227
 
        self.detach_window = gtkextra.Winparent(self)
228
 
 
229
 
    def log(self, message, level):
230
 
        """
231
 
        Log a message.
232
 
        
233
 
        @param message: The message to be logged.
234
 
        @type message: string
235
 
 
236
 
        @param level: The log level.
237
 
        @type level: int
238
 
        """
239
 
        # Add plugin name to message.
240
 
        text = '%s: %s' % (self.NAME, message)
241
 
        # Call the main log event.
242
 
        self.cb.action('log', self.NAME, message, level)
243
 
 
244
 
    def debug(self, message):
245
 
        """
246
 
        Log a debug message. 
247
 
        
248
 
        @param message: The message to be logged.
249
 
        @type message: string
250
 
        """
251
 
 
252
 
        self.log(message, 0)
253
 
 
254
 
    def info(self, message):
255
 
        """
256
 
        Log an info message.
257
 
 
258
 
        @param message: The message to be logged.
259
 
        @type message: string
260
 
        """
261
 
        self.log(message, 1)
262
 
 
263
 
    def warn(self, message):
264
 
        """
265
 
        Log a warning. 
266
 
        
267
 
        @param message: The message to be logged.
268
 
        @type message: string
269
 
        """
270
 
        self.log(message, 2)
271
 
 
272
 
    def error(self, message):
273
 
        """
274
 
        Log an error. 
275
 
        
276
 
        @param message: The message to be logged.
277
 
        @type message: string
278
 
        """
279
 
        self.log(message, 3)
280
 
 
281
 
    def cb_sep_rclick(self, event):
282
 
        """
283
 
        Called when the toolbar separator is right clicked. 
284
 
        
285
 
        Default behaviour pops up the toolbar menu. Override this method to
286
 
        change this behaviour.
287
 
 
288
 
        @param event: The gdk event firing the callback.
289
 
        @type event: gtk.gdk.Event
290
 
        """
291
 
        self.toolbar_popup.popup(event.time)
292
 
 
293
 
    def cb_sep_dclick(self, event):
294
 
        """
295
 
        Called when the horizontal separator bar is double clicked. 
296
 
        
297
 
        Override this method to add desired bahaviour
298
 
 
299
 
        @param event: The gdk event firing the callback.
300
 
        @type event: gtk.gdk.Event
301
 
        """
302
 
        pass
303
 
 
304
 
    def cb_alternative(self):
305
 
        """
306
 
        The alternative function called for non detachable plugins.
307
 
        """
308
 
        pass
309
 
    
310
 
    def cb_toggledetatch(self, *a):
311
 
        """
312
 
        Called back when the detach button is clicked.
313
 
 
314
 
        Detach the plugin for a detachable plugin, or call the alternative
315
 
        callback for undetachable plugins.
316
 
        """
317
 
        # Check whether the detach button is active or not.
318
 
        if self.dtbut.get_active():
319
 
            # Detach detachable plugins, or call the alternative callback.
320
 
            if self.DETACHABLE:
321
 
                self.detatch()
322
 
            else:
323
 
                self.cb_alternative()
324
 
                # Ensure the toggle button behaves normally.
325
 
                self.dtbut.set_active(False)
326
 
        else:
327
 
            # Reattach detached plugins.
328
 
            if self.DETACHABLE:
329
 
                self.attach()
330
 
 
331
 
    #def evt_init(self):
332
 
    #    """
333
 
    #    Event: called on initializing the plugin.
334
 
    #    """
335
 
    #    self.init()
336
 
 
337
 
    def evt_populate(self):
338
 
        self.__populate__()
339
 
        self.populate_widgets()
340
 
        self.connect_widgets()
341
 
        self.frame.show_all()
342
 
        self.win.show_all()
343
 
 
344
 
    def evt_shown(self):
345
 
        self.msgbox.hide()
346
 
        self.qstbox.hide()
347
 
 
348
 
    def get_window(self):
349
 
        return self.win
350
 
 
351
 
    def get_main_toolbar(self):
352
 
        return self.cusbar
353
 
 
354
 
    def get_extra_toolbar(self):
355
 
        return self.shortbar
356
 
 
357
 
class CommonEvents(object):
358
 
    """ Just for reference """
359
 
    def evt_started(self, serverlist):
360
 
        """
361
 
        Event: called on starting.
362
 
        
363
 
        @param serverlist: A list of servers registered with the X root
364
 
            window.
365
 
        @type serverlist: C{list} of C{(name, X-window ID)} pairs
366
 
        """
367
 
        pass
368
 
 
369
 
    def evt_die(self):
370
 
        """ 
371
 
        Event: Called before shut-down.
372
 
        """
373
 
        pass
374
 
 
375
 
    def evt_reset(self):
376
 
        """ 
377
 
        Event: called when main configuration has been changed.
378
 
        """
379
 
        pass
380
 
 
381
 
    def evt_shortcuts(self):
382
 
        """
383
 
        Event: called for shortcuts window to be shown.
384
 
        """
385
 
        pass
386
 
 
387
 
    def evt_shortcutschanged(self):
388
 
        """
389
 
        Event: Called when shortcuts have been changed.
390
 
        """
391
 
        pass
392
 
 
393
 
    def evt_newterm(self, command, args, **kwargs):
394
 
        """
395
 
        Event: called to open a command in a new terminal.
396
 
        
397
 
        @param command: The path to the command to be executed.
398
 
        @type command: string
399
 
 
400
 
        @param args: The argument list to pass the command (this usually
401
 
            starts with the command name as the first argument)
402
 
        @type args: list
403
 
 
404
 
        @param kwargs: a list of additional keyword args to pass the terminal.
405
 
            See the VTE reference manual for details.
406
 
        """
407
 
        pass
408
 
 
409
 
    def evt_log(self, title, message, level=0):
410
 
        """
411
 
        Event: called to log a mesage.
412
 
        
413
 
        @param title: The title of the message.
414
 
        @type title: string
415
 
        
416
 
        @param message: The message.
417
 
        @type message: string
418
 
 
419
 
        @param level: The level to be logged.
420
 
        @type level: int
421
 
        """
422
 
        pass
423
 
 
424
 
    def evt_connectserver(self, servername):
425
 
        """
426
 
        Event: called to connect to a server.
427
 
        
428
 
        @param servername: The server to connect to.
429
 
        @type servername: string
430
 
        """
431
 
        pass
432
 
    
433
 
    def evt_serverchange(self, servername):
434
 
        """
435
 
        Event: called when the server is changed
436
 
 
437
 
        @param servername: The server connected to.
438
 
        @type servername: string
439
 
        """
440
 
        pass
441
 
 
442
 
    def evt_badserver(self, servername):
443
 
        """
444
 
        Event: called after attempting to connect to a bad server.
445
 
 
446
 
        @param servername: The bad server name.
447
 
        @type servername: string
448
 
        """
449
 
        pass
450
 
 
451
 
    def evt_bufferlist(self, bufferlist):
452
 
        """
453
 
        Event: called when a new buffer list is received.
454
 
        
455
 
        @param bufferlist: A list of loaded buffers.
456
 
        @type bufferlist a C{list} of C{(number, name)} pairs
457
 
        """
458
 
        pass
459
 
        
460
 
    def evt_bufferchange(self, buffernumber, buffername):
461
 
        """
462
 
        Event: called when the buffer number has changed.
463
 
 
464
 
        @param buffernumber: The number of the buffer changed to.
465
 
        @type buffernumber: string
466
 
 
467
 
        @param buffername: The path of the buffer changed to on disk.
468
 
        @type buffername: string
469
 
        """
470
 
        pass
471
 
 
472
 
    def evt_bufferunload(self, *a):
473
 
        """
474
 
        Event: called when a buffer is unloaded.
475
 
        """
476
 
        pass
477
 
 
478
 
    def evt_filetype (self, buffernumber, filetype):
479
 
        """
480
 
        Event: called when the filetype is detected
481
 
 
482
 
        @param buffernumber: The number of the buffer
483
 
        @type buffernumber: string
484
 
 
485
 
        @param filetype: The fieltype of file
486
 
        @type filetype: string
487
 
        """
488
 
        pass
489
 
 
490
 
    def evt_bufferexecute(self):
491
 
        """
492
 
        Called to execute the contents of a buffer.
493
 
        """
494
 
        pass
495
 
 
496
 
    def evt_breakpointset(self, line, filename=None):
497
 
        """
498
 
        Called to set a breakpoint.
499
 
 
500
 
        If no filename is passed, it is assumed that the current filename is
501
 
        specified.
502
 
 
503
 
        @param line: The line number to place the brakpoint.
504
 
        @type line: int or string
505
 
 
506
 
        @param filename: The filename to place the breakpoint.
507
 
        @type filename: string
508
 
        """
509
 
        pass
510
 
 
511
 
    def evt_breakpointclear(self, line, filename=None):
512
 
        """
513
 
        Event: called to clear a breakpoint.
514
 
        
515
 
        If no filename is passed, it is assumed that the current filename is
516
 
        specified.
517
 
 
518
 
        @param line: The line number to clear the brakpoint.
519
 
        @type line: int or string
520
 
 
521
 
        @param filename: The filename to clear the breakpoint.
522
 
        @type filename: string
523
 
        """
524
 
        pass
525
 
 
526
 
    def evt_projectexecute(self, *a):
527
 
        """
528
 
        Event: called to execute the project.
529
 
        """  
530
 
        pass
531
 
 
532
 
    def evt_doc(self, text):
533
 
        """
534
 
        Called to perform a doc lookup.
535
 
 
536
 
        @param text: The text to lookup in doc.
537
 
        @type text: string
538
 
        """
539
 
        pass
540