~sir-rainbow/+junk/scribes-on-win

« back to all changes in this revision

Viewing changes to SCRIBES/TextView.py

  • Committer: goldenmyst
  • Date: 2007-09-25 17:15:52 UTC
  • Revision ID: goldenmyst@goldenmyst-desktop-20070925171552-mvrhxdd39iibs0sr
New branch. New Trigger Management System. New Trigger API. New Plugin Management System. Fix for bug triggered by PyGTK+ version 2.11 or better.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# -*- coding: utf-8 -*-
 
2
# Copyright © 2005 Lateef Alabi-Oki
 
3
#
 
4
# This file is part of Scribes.
 
5
#
 
6
# Scribes is free software; you can redistribute it and/or modify
 
7
# it under the terms of the GNU General Public License as published by
 
8
# the Free Software Foundation; either version 2 of the License, or
 
9
# (at your option) any later version.
 
10
#
 
11
# Scribes is distributed in the hope that it will be useful,
 
12
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
14
# GNU General Public License for more details.
 
15
#
 
16
# You should have received a copy of the GNU General Public License
 
17
# along with Scribes; if not, write to the Free Software
 
18
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 
19
 
 
20
"""
 
21
This module implements a class that creates a container for the text editor's
 
22
buffer.
 
23
 
 
24
@author: Lateef Alabi-Oki
 
25
@organization: The Scribes Project
 
26
@copyright: Copyright © 2005 Lateef Alabi-Oki
 
27
@license: GNU GPLv2 or Later
 
28
@contact: mystilleef@gmail.com
 
29
"""
 
30
 
 
31
from gtksourceview import SourceView
 
32
 
 
33
class ScribesTextView(SourceView):
 
34
        """
 
35
        This class creates the container object that holds the text editor's buffer.
 
36
        It defines the state, properties and behavior of the container object,
 
37
        popular called the text editor's view. The view also defines the look and
 
38
        feel of the buffer. The color of text in the buffer, the size of fonts, the
 
39
        width of tabs are all defined by the view. As such the view is the only
 
40
        user customizable part of the text editor. The class inherits from
 
41
        gtksourceview.SourceView. See the PyGTK, or GTK+, manual for details on the
 
42
        gtksourceview.SourceView class.
 
43
        """
 
44
 
 
45
        def __init__(self, editor):
 
46
                """
 
47
                Initialize object.
 
48
 
 
49
                @param self: Reference to the ScribesTextView instance.
 
50
                @type self: A ScribesTextView object.
 
51
 
 
52
                @param editor: Reference to the text editor instance.
 
53
                @type editor: An Editor object.
 
54
                """
 
55
                SourceView.__init__(self)
 
56
                self.__init_attributes(editor)
 
57
                self.__set_properties()
 
58
                # Monitor signals and events.
 
59
                self.__signal_id_1 = self.connect("map-event", self.__map_event_cb)
 
60
                self.__signal_id_2 = self.connect("focus-in-event", self.__focus_in_event_cb)
 
61
                self.__signal_id_3 = self.connect("drag-motion", self.__drag_motion_cb)
 
62
                self.__signal_id_4 = self.connect("drag-drop", self.__drag_drop_cb)
 
63
                self.__signal_id_5 = self.connect("drag-data-received", self.__drag_data_received_cb)
 
64
                self.__signal_id_6 = self.connect_after("populate-popup", self.__populate_popup_cb)
 
65
                self.__signal_id_7 = self.connect("copy-clipboard", self.__copy_clipboard_cb)
 
66
                self.__signal_id_8 = self.connect("cut-clipboard", self.__cut_clipboard_cb)
 
67
                self.__signal_id_9 = self.connect("paste-clipboard", self.__paste_clipboard_cb)
 
68
                self.__signal_id_10 = self.connect_after("paste-clipboard", self.__paste_clipboard_after_cb)
 
69
                self.__signal_id_11 = self.connect("button-press-event", self.__button_press_event_cb)
 
70
                self.__signal_id_12 = editor.connect("checking-document", self.__checking_document_cb)
 
71
                self.__signal_id_13 = editor.connect("loaded-document", self.__loaded_document_cb)
 
72
                self.__signal_id_14 = editor.connect("load-error", self.__load_error_cb)
 
73
                self.__signal_id_15 = editor.connect("enable-readonly", self.__enable_readonly_cb)
 
74
                self.__signal_id_16 = editor.connect("disable-readonly", self.__disable_readonly_cb)
 
75
                self.__signal_id_17 = editor.connect("renamed-document", self.__renamed_document_cb)
 
76
                self.__signal_id_18 = editor.connect("hide-dialog", self.__hide_dialog_cb)
 
77
                self.__signal_id_19 = editor.connect("show-bar", self.__show_bar_cb)
 
78
                self.__signal_id_20 = editor.connect("hide-bar", self.__hide_bar_cb)
 
79
                self.__signal_id_21 = editor.connect("close-document", self.__close_document_cb)
 
80
                self.__signal_id_22 = editor.connect("close-document-no-save", self.__close_document_cb)
 
81
                self.__signal_id_23 = self.connect("scroll-event", self.__scroll_event_cb)
 
82
                self.__signal_id_24 = self.connect("move-cursor", self.__move_cursor_cb)
 
83
                self.__signal_id_25 = editor.connect("reload-document", self.__reload_document_cb)
 
84
                # GConf notification monitors.
 
85
                self.__client.notify_add("/apps/scribes/font", self.__font_changed_cb)
 
86
                self.__client.notify_add("/apps/scribes/tab", self.__tab_width_cb)
 
87
                self.__client.notify_add("/apps/scribes/text_wrapping", self.__text_wrapping_cb)
 
88
                self.__client.notify_add("/apps/scribes/margin", self.__show_margin_cb)
 
89
                self.__client.notify_add("/apps/scribes/margin_position", self.__margin_position_cb)
 
90
                self.__client.notify_add("/apps/scribes/spell_check", self.__spell_check_cb)
 
91
                self.__client.notify_add("/apps/scribes/use_theme_colors", self.__themes_cb)
 
92
                self.__client.notify_add("/apps/scribes/fgcolor", self.__foreground_cb)
 
93
                self.__client.notify_add("/apps/scribes/bgcolor", self.__background_cb)
 
94
                self.__client.notify_add("/apps/scribes/use_tabs", self.__use_tabs_cb)
 
95
                self.__client.notify_add("/apps/scribes/SyntaxHighlight", self.__syntax_cb)
 
96
 
 
97
        def __init_attributes(self, editor):
 
98
                """
 
99
                Initialize the buffer container's attributes.
 
100
 
 
101
                @param self: Reference to the ScribesTextView instance.
 
102
                @type self: A ScribesTextView object.
 
103
 
 
104
                @param editor: Reference to the text editor.
 
105
                @type editor: An Editor object.
 
106
                """
 
107
                self.__client = editor.gconf_client
 
108
                self.__editor = editor
 
109
                self.__registration_id = editor.register_termination_id()
 
110
                self.__spell_checker = None
 
111
                self.__bar_is_visible = False
 
112
                self.__bar = None
 
113
                self.__scroll_id = None
 
114
                self.__move_id = None
 
115
                return
 
116
 
 
117
        def __set_properties(self):
 
118
                """
 
119
                Set default properties.
 
120
 
 
121
                Much of the properties of the view can be defined by users. So the
 
122
                properties of the view are stored in GNOME's configuration database,
 
123
                GConf. During the text editor's initialization process,
 
124
 
 
125
                @param self: Reference to the ScribesTextView instance.
 
126
                @type self: A ScribesTextView object.
 
127
 
 
128
                @param editor: Reference to the text editor instance.
 
129
                @type editor: An Editor object.
 
130
                """
 
131
                # Drag and drop setup.
 
132
                targets = [("text/uri-list", 0, 80)]
 
133
                from gtk import DEST_DEFAULT_ALL
 
134
                from gtk.gdk import ACTION_COPY, BUTTON1_MASK, ACTION_DEFAULT
 
135
                self.drag_dest_set(DEST_DEFAULT_ALL, targets, ACTION_COPY)
 
136
                self.set_property("buffer", self.__editor.textbuffer)
 
137
                from gtk import RESIZE_PARENT
 
138
                self.set_resize_mode(RESIZE_PARENT)
 
139
                self.set_highlight_current_line(True)
 
140
                self.set_show_line_numbers(True)
 
141
                self.set_auto_indent(True)
 
142
                if self.__client.get("/apps/scribes/spell_check"):
 
143
                        spell_check = self.__client.get_bool("/apps/scribes/spell_check")
 
144
                        if spell_check:
 
145
                                try:
 
146
                                        from gobject import GError
 
147
                                        from gtkspell import Spell
 
148
                                        from locale import getdefaultlocale
 
149
                                        self.__spell_checker = Spell(self, getdefaultlocale()[0])
 
150
                                except GError:
 
151
                                        pass
 
152
                margin_position = 72
 
153
                if self.__client.get("/apps/scribes/margin_position"):
 
154
                        margin_position = self.__client.get_int("/apps/scribes/margin_position")
 
155
                self.set_margin(margin_position)
 
156
                show_margin = False
 
157
                if self.__client.get("/apps/scribes/margin"):
 
158
                        show_margin = self.__client.get_bool("/apps/scribes/margin")
 
159
                self.set_show_margin(show_margin)
 
160
                tab_width = 4
 
161
                if self.__client.get("/apps/scribes/tab"):
 
162
                        tab_width = self.__client.get_int("/apps/scribes/tab")
 
163
                self.set_tabs_width(tab_width)
 
164
                use_tabs = True
 
165
                if self.__client.get("/apps/scribes/use_tabs"):
 
166
                        use_tabs = self.__client.get_bool("/apps/scribes/use_tabs")
 
167
                self.set_insert_spaces_instead_of_tabs(not use_tabs)
 
168
                gconf_font = "Monospace 12"
 
169
                if self.__client.get("/apps/scribes/font"):
 
170
                        gconf_font = self.__client.get_string("/apps/scribes/font")
 
171
                from pango import FontDescription
 
172
                font = FontDescription(gconf_font)
 
173
                self.modify_font(font)
 
174
                wrap_mode_bool = True
 
175
                if self.__client.get("/apps/scribes/text_wrapping"):
 
176
                        wrap_mode_bool = self.__client.get_bool("/apps/scribes/text_wrapping")
 
177
                use_theme_colors = True
 
178
                if self.__client.get("/apps/scribes/use_theme_colors"):
 
179
                        use_theme_colors = self.__client.get_bool("/apps/scribes/use_theme_colors")
 
180
                from gtk import WRAP_WORD, WRAP_NONE
 
181
                if wrap_mode_bool:
 
182
                        self.set_wrap_mode(WRAP_WORD)
 
183
                else:
 
184
                        self.set_wrap_mode(WRAP_NONE)
 
185
                if use_theme_colors is False:
 
186
                        # Use foreground and background colors specified by the user stored
 
187
                        # in GConf, the GNOME configuration database.
 
188
                        from gconf import VALUE_STRING
 
189
                        fgcolor = "#000000"
 
190
                        bgcolor = "#ffffff"
 
191
                        if self.__client.get("/apps/scribes/fgcolor"):
 
192
                                fgcolor = self.__client.get_string("/apps/scribes/fgcolor")
 
193
                        if self.__client.get("/apps/scribes/bgcolor"):
 
194
                                bgcolor = self.__client.get_string("/apps/scribes/bgcolor")
 
195
                        try:
 
196
                                from gtk.gdk import color_parse
 
197
                                foreground_color = color_parse(fgcolor)
 
198
                                background_color = color_parse(bgcolor)
 
199
                        except TypeError:
 
200
                                foreground_color = color_parse("#000000")
 
201
                                background_color = color_parse("#ffffff")
 
202
                        from gtk import STATE_NORMAL
 
203
                        self.modify_base(STATE_NORMAL, background_color)
 
204
                        self.modify_text(STATE_NORMAL, foreground_color)
 
205
                return
 
206
 
 
207
################################################################################
 
208
#
 
209
#                                               Signal and Event Handlers
 
210
#
 
211
################################################################################
 
212
 
 
213
        def __map_event_cb(self, widget, event):
 
214
                """
 
215
                Handles callback when the textview widget is drawn to screen.
 
216
 
 
217
                @param self: Reference to the ScribesTextView instance.
 
218
                @type self: A ScribesTextView object.
 
219
 
 
220
                @param widget: Reference to the text editor's buffer container.
 
221
                @type widget: A gtk.SourceView object.
 
222
 
 
223
                @param event: An event that occurs when the widgets are displayed on
 
224
                        screen.
 
225
                @type event: A gtk.Event object.
 
226
                """
 
227
                self.__refresh_view()
 
228
                return False
 
229
 
 
230
        def __drag_motion_cb(self, textview, context, x, y, time):
 
231
                """
 
232
                Handles callback when a drag operation begins.
 
233
 
 
234
                @param self: Reference to the ScribesTextView instance.
 
235
                @type self: A ScribesTextView object.
 
236
 
 
237
                @param textview: The text editor's view, or buffer container.
 
238
                @type textview: A gtk.SourceView object.
 
239
 
 
240
                @param context: The type of information being dragged.
 
241
                @type context: A gtk.Context object.
 
242
 
 
243
                @param x: The xcordinate of the cursor.
 
244
                @type x: An Integer object.
 
245
 
 
246
                @param y: The ycordinate of the cursor.
 
247
                @type y: An Integer object.
 
248
 
 
249
                @param time: The time the drag operation began.
 
250
                @type time: An Integer object.
 
251
 
 
252
                @return: False to propagate signals to parent widgets, True otherwise.
 
253
                @rtype: A Boolean object.
 
254
                """
 
255
                from operator import contains
 
256
                if contains(context.targets, "text/uri-list"): return True
 
257
                return False
 
258
 
 
259
        def __drag_drop_cb(self, textview, context, x, y, time):
 
260
                """
 
261
                Handles callback when a drop operation begins.
 
262
 
 
263
                @param self: Reference to the ScribesTextView instance.
 
264
                @type self: A ScribesTextView object.
 
265
 
 
266
                @param textview: The text editor's view, or buffer container.
 
267
                @type textview: A gtk.SourceView object.
 
268
 
 
269
                @param context: The type of information being dragged.
 
270
                @type context: A gtk.Context object.
 
271
 
 
272
                @param x: The xcordinate of the cursor.
 
273
                @type x: An Integer object.
 
274
 
 
275
                @param y: The ycordinate of the cursor.
 
276
                @type y: An Integer object.
 
277
 
 
278
                @param time: The time the drag operation began.
 
279
                @type time: An Integer object.
 
280
 
 
281
                @return: False to propagate signals to parent widgets, True otherwise.
 
282
                @rtype: A Boolean object.
 
283
                """
 
284
                from operator import contains
 
285
                if contains(context.targets, "text/uri-list"): return True
 
286
                return False
 
287
 
 
288
        def __drag_data_received_cb(self, textview, context, xcord, ycord,
 
289
                                                                selection_data, info, timestamp):
 
290
                """
 
291
                Handles callback when a drop operation finishes.
 
292
 
 
293
                @param self: Reference to the ScribesTextView instance.
 
294
                @type self: A ScribesTextView object.
 
295
 
 
296
                @param textview: The text editor's view, or buffer container.
 
297
                @type textview: A gtk.SourceView object.
 
298
 
 
299
                @param context: The type of information being dragged.
 
300
                @type context: A gtk.Context object.
 
301
 
 
302
                @param xcord: The xcordinate of the cursor.
 
303
                @type xcord: An Integer object.
 
304
 
 
305
                @param ycord: The ycordinate of the cursor.
 
306
                @type ycord: An Integer object.
 
307
 
 
308
                @param selection_data: A data selection
 
309
                @type selection_data: A gtk.Selection object.
 
310
 
 
311
                @param info: ?
 
312
                @type info: ? ? object.
 
313
 
 
314
                @param timestamp: The time the drag operation began.
 
315
                @type timestamp: An Integer object.
 
316
 
 
317
                @return: False to propagate signals to parent widgets, True otherwise.
 
318
                @rtype: A Boolean object.
 
319
                """
 
320
                from operator import contains, not_, ne
 
321
                if not_(contains(context.targets, "text/uri-list")): return False
 
322
                if ne(info, 80): return False
 
323
                # Load file
 
324
                uri_list = list(selection_data.get_uris())
 
325
                self.__editor.instance_manager.open_files(uri_list)
 
326
                context.finish(True, False, timestamp)
 
327
                return True
 
328
 
 
329
        def __drag_data_get_cb(self, textview, context, data, info, time):
 
330
                """
 
331
                Handles callback when the "drag-data-get" signal is emitted.
 
332
 
 
333
                @param self: Reference to the TemplateEditorDescriptionView instance.
 
334
                @type self: A TemplateEditorDescriptionView object.
 
335
 
 
336
                @param treeview: Reference to the TemplateEditorDescriptionView
 
337
                @type treeview: A TemplateEditorDescriptionView object.
 
338
 
 
339
                @param context: An object representing context data.
 
340
                @type context: A gtk.DragContext object.
 
341
 
 
342
                @param data: An object representing selection data.
 
343
                @type data: A gtk.SelectionData object.
 
344
 
 
345
                @param info: A unique identification number for the text editor.
 
346
                @type info: An Integer object.
 
347
 
 
348
                @param time: The time of the drag and drop operation.
 
349
                @type time: An Integer object.
 
350
 
 
351
                @return: True to propagate signals to parent widgets.
 
352
                @type: A Boolean Object.
 
353
                """
 
354
                selection = self.get_buffer().get_selection_bounds()
 
355
                from operator import not_
 
356
                if not_(selection): return False
 
357
                string = self.get_buffer().get_text(selection[0], selection[1])
 
358
                data.set(data.target, 8, string)
 
359
                return False
 
360
 
 
361
        def __checking_document_cb(self, editor, uri):
 
362
                """
 
363
                Handles callback when the text editor is loading a document into the
 
364
                text editor's buffer.
 
365
 
 
366
                @param self: Reference to the ScribesTextView instance.
 
367
                @type self: A ScribesTextView object.
 
368
 
 
369
                @param editor: An instance of the text editor.
 
370
                @type editor: An Editor object.
 
371
                """
 
372
                self.handler_block(self.__signal_id_23)
 
373
                self.handler_block(self.__signal_id_24)
 
374
                self.set_property("editable", False)
 
375
                self.set_property("highlight-current-line", False)
 
376
                self.set_property("cursor-visible", False)
 
377
                self.set_property("sensitive", False)
 
378
                self.__refresh_view()
 
379
                return
 
380
 
 
381
        def __loaded_document_cb(self, editor, uri):
 
382
                """
 
383
                Handles callback when the text editor has finished loading a document
 
384
                into the text editor's buffer.
 
385
 
 
386
                @param self: Reference to the ScribesTextView instance.
 
387
                @type self: A ScribesTextView object.
 
388
 
 
389
                @param editor: An instance of the text editor.
 
390
                @type editor: An Editor object.
 
391
                """
 
392
                self.handler_unblock(self.__signal_id_23)
 
393
                self.handler_unblock(self.__signal_id_24)
 
394
                self.set_property("sensitive", True)
 
395
                self.set_property("highlight-current-line", True)
 
396
                self.set_property("cursor-visible", True)
 
397
                self.set_property("editable", True)
 
398
                self.__refresh_view()
 
399
                return
 
400
 
 
401
        def __reload_document_cb(self, *args):
 
402
                self.set_property("editable", False)
 
403
                return
 
404
 
 
405
        def __load_error_cb(self, editor, uri):
 
406
                """
 
407
                Handles callback when the text editor has finished loading a document
 
408
                into the text editor's buffer.
 
409
 
 
410
                @param self: Reference to the ScribesTextView instance.
 
411
                @type self: A ScribesTextView object.
 
412
 
 
413
                @param editor: An instance of the text editor.
 
414
                @type editor: An Editor object.
 
415
                """
 
416
                self.handler_unblock(self.__signal_id_23)
 
417
                self.handler_unblock(self.__signal_id_24)
 
418
                self.set_property("sensitive", True)
 
419
                self.set_property("cursor-visible", True)
 
420
                self.set_property("highlight-current-line", True)
 
421
                self.set_property("editable", True)
 
422
                from cursor import move_view_to_cursor
 
423
                move_view_to_cursor(self)
 
424
                return
 
425
 
 
426
        def __enable_readonly_cb(self, editor):
 
427
                """
 
428
                Handles callback when the text editor is switched to readonly mode.
 
429
 
 
430
                @param self: Reference to the ScribesTextView instance.
 
431
                @type self: A ScribesTextView object.
 
432
 
 
433
                @param editor: An instance of the text editor.
 
434
                @type editor: An Editor object.
 
435
                """
 
436
                self.set_property("editable", False)
 
437
                self.set_property("highlight-current-line", False)
 
438
                self.set_property("cursor-visible", False)
 
439
                self.set_property("show-line-numbers", False)
 
440
                self.__refresh()
 
441
                return
 
442
 
 
443
        def __disable_readonly_cb(self, editor):
 
444
                """
 
445
                Handles callback when the text editor is switched from readonly to
 
446
                readwrite mode.
 
447
 
 
448
                @param self: Reference to the ScribesTextView instance.
 
449
                @type self: A ScribesTextView object.
 
450
 
 
451
                @param editor: An instance of the text editor.
 
452
                @type editor: An Editor object.
 
453
                """
 
454
                self.set_property("editable", True)
 
455
                self.set_property("highlight-current-line", True)
 
456
                self.set_property("cursor-visible", True)
 
457
                self.set_property("show-line-numbers", True)
 
458
                self.__refresh_view()
 
459
                return
 
460
 
 
461
        def __renamed_document_cb(self, editor, uri):
 
462
                """
 
463
                Handles callback when the name of the document is renamed.
 
464
 
 
465
                @param self: Reference to the ScribesTextBuffer instance.
 
466
                @type self: A ScribesTextBuffer object.
 
467
 
 
468
                @param editor: Reference to the text editor.
 
469
                @type editor: An Editor object.
 
470
                """
 
471
                self.set_property("cursor-visible", True)
 
472
                self.set_property("editable", True)
 
473
                self.set_property("highlight-current-line", True)
 
474
                self.set_property("cursor-visible", True)
 
475
                self.set_property("show-line-numbers", True)
 
476
                self.__refresh_view()
 
477
                return
 
478
 
 
479
        def __show_bar_cb(self, editor, bar):
 
480
                """
 
481
                Handles callback when the "hide-bar" signal is emitted.
 
482
 
 
483
                @param self: Reference to the ScribesTextView instance.
 
484
                @type self: A ScribesTextView object.
 
485
 
 
486
                @param editor: Reference to the text editor.
 
487
                @type editor: An Editor object.
 
488
 
 
489
                @param bar: The text editor's bar.
 
490
                @type bar: A ScribesBar object.
 
491
                """
 
492
                self.__bar = bar
 
493
                self.__bar_is_visible = True
 
494
                self.set_property("editable", False)
 
495
                self.set_property("cursor-visible", False)
 
496
                self.set_property("accepts-tab", False)
 
497
                self.__editor.response()
 
498
                return
 
499
 
 
500
        def __hide_bar_cb(self, editor, bar):
 
501
                """
 
502
                Handles callback when the "hide-bar" signal is emitted.
 
503
 
 
504
                @param self: Reference to the ScribesTextView instance.
 
505
                @type self: A ScribesTextView object.
 
506
 
 
507
                @param editor: Reference to the text editor.
 
508
                @type editor: An Editor object.
 
509
 
 
510
                @param bar: The text editor's bar.
 
511
                @type bar: A ScribesBar object.
 
512
                """
 
513
                self.__bar_is_visible = False
 
514
                self.set_property("editable", True)
 
515
                self.set_property("cursor-visible", True)
 
516
                self.set_property("accepts-tab", True)
 
517
                self.__refresh_view()
 
518
                return
 
519
 
 
520
        def __focus_in_event_cb(self, textview, event):
 
521
                """
 
522
                Handles callback when the text editor's windows "focus-in-event" signal
 
523
                is emitted.
 
524
 
 
525
                @param self: Reference to the ScribesTextView instance.
 
526
                @type self: A ScribesTextView object.
 
527
 
 
528
                @param window: The text editor's buffer container, or view.
 
529
                @type window: A ScribesTextView object.
 
530
 
 
531
                @param event: An event triggered when the text editor's window is focused.
 
532
                @type event: A gtk.Event object.
 
533
 
 
534
                @return: True to propagate signals to parent widgets.
 
535
                @type: A Boolean Object.
 
536
                """
 
537
                if self.__bar_is_visible is False:
 
538
                        self.__refresh_view()
 
539
                return False
 
540
 
 
541
        def __button_press_event_cb(self, textview, event):
 
542
                """
 
543
                Handles callback when the "button-press-event" signal is emitted.
 
544
 
 
545
                @param self: Reference to the ScribesTextView instance.
 
546
                @type self: A ScribesTextView object.
 
547
 
 
548
                @param textview: The text editor's view.
 
549
                @type textview: A ScribesTextView object.
 
550
 
 
551
                @param event: An event that occurs when the mouse buttons are pressed.
 
552
                @type event: A gtk.Event object.
 
553
 
 
554
                @param bar: The text editor's bar.
 
555
                @type bar: A ScribesBar object.
 
556
 
 
557
                @return: True to propagate signals to parent widgets.
 
558
                @type: A Boolean Object.
 
559
                """
 
560
                if self.__bar_is_visible:
 
561
                        self.__bar.hide_bar()
 
562
                        return True
 
563
                return False
 
564
 
 
565
        def __populate_popup_cb(self, textview, menu):
 
566
                """
 
567
                Handles callback when the "populate-popup" signal is emitted.
 
568
 
 
569
                @param self: Reference to the ScribesTextView instance.
 
570
                @type self: A ScribesTextView object.
 
571
 
 
572
                @param textview: The text editor's buffer container.
 
573
                @type textview: A ScribesTextView object.
 
574
 
 
575
                @param menu: The text editor's popup menu.
 
576
                @type menu: A gtk.Menu object.
 
577
 
 
578
                @return: True to propagate signals to parent widgets.
 
579
                @type: A Boolean Object.
 
580
                """
 
581
                from gtk import MenuItem, SeparatorMenuItem
 
582
                menu.insert(SeparatorMenuItem(), 0)
 
583
                return False
 
584
 
 
585
        def __hide_dialog_cb(self, *args):
 
586
                self.__refresh_view()
 
587
                return
 
588
 
 
589
        def __copy_clipboard_cb(self, textview):
 
590
                textbuffer = self.get_buffer()
 
591
                feedback = self.__editor.feedback
 
592
                # Get selection bounds
 
593
                selection = textbuffer.get_selection_bounds()
 
594
 
 
595
                # Depending on whether or not text is selected and copied, send appropriate
 
596
                # feedback back to the user via the statusbar.
 
597
                if selection:
 
598
                        from internationalization import msg0089
 
599
                        feedback.update_status_message(msg0089, "copy")
 
600
                else:
 
601
                        from internationalization import msg0090
 
602
                        feedback.update_status_message(msg0090, "fail")
 
603
                return False
 
604
 
 
605
        def __cut_clipboard_cb(self, textview):
 
606
                feedback = self.__editor.feedback
 
607
                if self.__editor.is_readonly:
 
608
                        from internationalization import msg0145
 
609
                        feedback.update_status_message(msg0145, "fail")
 
610
                        return False
 
611
                textbuffer = self.get_buffer()
 
612
 
 
613
                # Get selection bounds
 
614
                selection = textbuffer.get_selection_bounds()
 
615
                self.__refresh_view()
 
616
                # Depending on whether or not text is selected and copied, send appropriate
 
617
                # feedback back to the user via the statusbar.
 
618
                if selection:
 
619
                        from internationalization import msg0091
 
620
                        feedback.update_status_message(msg0091, "cut")
 
621
                else:
 
622
                        from internationalization import msg0092
 
623
                        feedback.update_status_message(msg0092, "fail")
 
624
                return False
 
625
 
 
626
        def __paste_clipboard_cb(self, textview):
 
627
 
 
628
                feedback = self.__editor.feedback
 
629
                if self.__editor.is_readonly:
 
630
                        from internationalization import msg0145
 
631
                        feedback.update_status_message(msg0145, "fail")
 
632
                        return False
 
633
                # Check to see if there is text in the clipboard manager.
 
634
                from gtk import clipboard_get
 
635
                clipboard = clipboard_get()
 
636
                selection = clipboard.wait_for_text()
 
637
 
 
638
                # Depending on whether or not there is text in the clipboard manager, send
 
639
                # appropriate feedback back to the user via the statusbar.
 
640
                if selection:
 
641
                        from internationalization import msg0093
 
642
                        feedback.update_status_message(msg0093, "paste")
 
643
                else:
 
644
                        from internationalization import msg0094
 
645
                        feedback.update_status_message(msg0094, "fail")
 
646
                return False
 
647
 
 
648
        def __paste_clipboard_after_cb(self, textview):
 
649
                self.__refresh_view()
 
650
                return False
 
651
 
 
652
        def __scroll_event_cb(self, *args):
 
653
                from gobject import idle_add, PRIORITY_LOW, source_remove
 
654
                try:
 
655
                        source_remove(self.__scroll_id)
 
656
                except:
 
657
                        pass
 
658
                self.__scroll_id = idle_add(self.__test_response, priority=PRIORITY_LOW)
 
659
                return False
 
660
 
 
661
        def __move_cursor_cb(self, *args):
 
662
                from gobject import idle_add, PRIORITY_LOW, source_remove
 
663
                try:
 
664
                        source_remove(self.__move_id)
 
665
                except:
 
666
                        pass
 
667
                self.__move_id = idle_add(self.__test_response, priority=PRIORITY_LOW)
 
668
                return False
 
669
 
 
670
        def __test_response(self):
 
671
                self.__editor.response()
 
672
                return False
 
673
 
 
674
        def __close_document_cb(self, editor):
 
675
                """
 
676
                Handles callback when the "close-document" signal is emitted.
 
677
 
 
678
                @param self: Reference to the Store instance.
 
679
                @type self: A Store object.
 
680
 
 
681
                @param editor: Reference to the text editor.
 
682
                @type editor: An Editor object.
 
683
                """
 
684
                self.__destroy()
 
685
                return
 
686
 
 
687
################################################################################
 
688
#
 
689
#                                               GConf Notification Handlers
 
690
#
 
691
################################################################################
 
692
 
 
693
        def __font_changed_cb(self, client, cnxn_id, entry, data):
 
694
                """
 
695
                Handles callback when the font of the text editor changes.
 
696
 
 
697
                @param self: Reference to the ScribesTextView instance.
 
698
                @type self: A ScribesTextView object.
 
699
 
 
700
                @param client: A client used to query the GConf daemon and database
 
701
                @type client: A gconf.Client object.
 
702
 
 
703
                @param cnxn_id: The identification number for the GConf client.
 
704
                @type cnxn_id: An Integer object.
 
705
 
 
706
                @param entry: An entry from the GConf database.
 
707
                @type entry: A gconf.Entry object.
 
708
 
 
709
                @param data: Optional data
 
710
                @type data: Any type object.
 
711
                """
 
712
                from pango import FontDescription
 
713
                new_font = FontDescription(entry.value.to_string())
 
714
                self.modify_font(new_font)
 
715
                self.__refresh_view()
 
716
                return
 
717
 
 
718
        def __tab_width_cb(self, client, cnxn_id, entry, data):
 
719
                """
 
720
                Handles callback when the tab width of the text editor changes.
 
721
 
 
722
                @param self: Reference to the ScribesTextView instance.
 
723
                @type self: A ScribesTextView object.
 
724
 
 
725
                @param client: A client used to query the GConf daemon and database
 
726
                @type client: A gconf.Client object.
 
727
 
 
728
                @param cnxn_id: The identification number for the GConf client.
 
729
                @type cnxn_id: An Integer object.
 
730
 
 
731
                @param entry: An entry from the GConf database.
 
732
                @type entry: A gconf.Entry object.
 
733
 
 
734
                @param data: Optional data
 
735
                @type data: Any type object.
 
736
                """
 
737
                self.set_tabs_width(int(entry.value.to_string()))
 
738
                self.__refresh_view()
 
739
                return
 
740
 
 
741
        def __text_wrapping_cb(self, client, cnxn_id, entry, data):
 
742
                """
 
743
                Handles callback when the text wrapping mode of the text editor changes.
 
744
 
 
745
                @param self: Reference to the ScribesTextView instance.
 
746
                @type self: A ScribesTextView object.
 
747
 
 
748
                @param client: A client used to query the GConf daemon and database
 
749
                @type client: A gconf.Client object.
 
750
 
 
751
                @param cnxn_id: The identification number for the GConf client.
 
752
                @type cnxn_id: An Integer object.
 
753
 
 
754
                @param entry: An entry from the GConf database.
 
755
                @type entry: A gconf.Entry object.
 
756
 
 
757
                @param data: Optional data
 
758
                @type data: Any type object.
 
759
                """
 
760
                from gtk import WRAP_NONE, WRAP_WORD
 
761
                if entry.value.get_bool():
 
762
                        self.set_wrap_mode(WRAP_WORD)
 
763
                else:
 
764
                        self.set_wrap_mode(WRAP_NONE)
 
765
                self.__refresh_view()
 
766
                return
 
767
 
 
768
        def __margin_position_cb(self, client, cnxn_id, entry, data):
 
769
                """
 
770
                Handles callback when the right margin position of the text editor
 
771
                changes.
 
772
 
 
773
                @param self: Reference to the ScribesTextView instance.
 
774
                @type self: A ScribesTextView object.
 
775
 
 
776
                @param client: A client used to query the GConf daemon and database
 
777
                @type client: A gconf.Client object.
 
778
 
 
779
                @param cnxn_id: The identification number for the GConf client.
 
780
                @type cnxn_id: An Integer object.
 
781
 
 
782
                @param entry: An entry from the GConf database.
 
783
                @type entry: A gconf.Entry object.
 
784
 
 
785
                @param data: Optional data
 
786
                @type data: Any type object.
 
787
                """
 
788
                self.set_margin(int(entry.value.to_string()))
 
789
                self.__refresh_view()
 
790
                return
 
791
 
 
792
        def __show_margin_cb(self, client, cnxn_id, entry, data):
 
793
                """
 
794
                Handles callback when the right margin of the text editor is hidden or
 
795
                displayed.
 
796
 
 
797
                @param self: Reference to the ScribesTextView instance.
 
798
                @type self: A ScribesTextView object.
 
799
 
 
800
                @param client: A client used to query the GConf daemon and database
 
801
                @type client: A gconf.Client object.
 
802
 
 
803
                @param cnxn_id: The identification number for the GConf client.
 
804
                @type cnxn_id: An Integer object.
 
805
 
 
806
                @param entry: An entry from the GConf database.
 
807
                @type entry: A gconf.Entry object.
 
808
 
 
809
                @param data: Optional data
 
810
                @type data: Any type object.
 
811
                """
 
812
                self.set_show_margin(entry.value.get_bool())
 
813
                self.__refresh_view()
 
814
                return
 
815
 
 
816
        def __spell_check_cb(self, client, cnxn_id, entry, data):
 
817
                """
 
818
                Handles callback when spell checking is toggled on or off in the text
 
819
                editor.
 
820
 
 
821
                @param self: Reference to the ScribesTextView instance.
 
822
                @type self: A ScribesTextView object.
 
823
 
 
824
                @param client: A client used to query the GConf daemon and database
 
825
                @type client: A gconf.Client object.
 
826
 
 
827
                @param cnxn_id: The identification number for the GConf client.
 
828
                @type cnxn_id: An Integer object.
 
829
 
 
830
                @param entry: An entry from the GConf database.
 
831
                @type entry: A gconf.Entry object.
 
832
 
 
833
                @param data: Optional data
 
834
                @type data: Any type object.
 
835
 
 
836
                """
 
837
                if entry.value.get_bool():
 
838
                        from gobject import GError
 
839
                        try:
 
840
                                from gtkspell import Spell
 
841
                                from locale import getdefaultlocale
 
842
                                self.__spell_checker = Spell(self, getdefaultlocale()[0])
 
843
                        except GError:
 
844
                                pass
 
845
                else:
 
846
                        self.__spell_checker.detach()
 
847
                self.__refresh_view()
 
848
                return
 
849
 
 
850
        def __themes_cb(self, client, cnxn_id, entry, data):
 
851
                """
 
852
                Handles callback when of the text editor's view theme changes.
 
853
 
 
854
                @param self: Reference to the ScribesTextView instance.
 
855
                @type self: A ScribesTextView object.
 
856
 
 
857
                @param client: A client used to query the GConf daemon and database
 
858
                @type client: A gconf.Client object.
 
859
 
 
860
                @param cnxn_id: The identification number for the GConf client.
 
861
                @type cnxn_id: An Integer object.
 
862
 
 
863
                @param entry: An entry from the GConf database.
 
864
                @type entry: A gconf.Entry object.
 
865
 
 
866
                @param data: Optional data
 
867
                @type data: Any type object.
 
868
                """
 
869
                # Set the foreground and background color of the text editor's buffer.
 
870
                if entry.value.get_bool():
 
871
                        # Use foreground and background colors specified by the GTK+ theme.
 
872
                        from gtk import RcStyle
 
873
                        style = RcStyle()
 
874
                        self.modify_style(style)
 
875
                        #  Reset font to value in GConf
 
876
                        value = client.get_string("/apps/scribes/font")
 
877
                        from pango import FontDescription
 
878
                        font = FontDescription(value)
 
879
                        self.modify_font(font)
 
880
                else:
 
881
                        try:
 
882
                                # Use foreground and background colors specified by the
 
883
                                # user stored in GConf, the GNOME configuration database.
 
884
                                color = client.get_string("/apps/scribes/fgcolor")
 
885
                                from gtk.gdk import color_parse
 
886
                                foreground_color = color_parse(color)
 
887
                                color = client.get_string("/apps/scribes/bgcolor")
 
888
                                background_color = color_parse(color)
 
889
                        except TypeError:
 
890
                                foreground_color = color_parse("#000000")
 
891
                                background_color = color_parse("#ffffff")
 
892
                        from gtk import STATE_NORMAL
 
893
                        self.modify_base(STATE_NORMAL, background_color)
 
894
                        self.modify_text(STATE_NORMAL, foreground_color)
 
895
                self.__refresh_view()
 
896
                return
 
897
 
 
898
        def __foreground_cb(self, client, cnxn_id, entry, data):
 
899
                """
 
900
                Handles callback when the color text within the text editor changes.
 
901
 
 
902
                @param self: Reference to the ScribesTextView instance.
 
903
                @type self: A ScribesTextView object.
 
904
 
 
905
                @param client: A client used to query the GConf daemon and database
 
906
                @type client: A gconf.Client object.
 
907
 
 
908
                @param cnxn_id: The identification number for the GConf client.
 
909
                @type cnxn_id: An Integer object.
 
910
 
 
911
                @param entry: An entry from the GConf database.
 
912
                @type entry: A gconf.Entry object.
 
913
 
 
914
                @param data: Optional data
 
915
                @type data: Any type object.
 
916
                """
 
917
                color = client.get_string("/apps/scribes/fgcolor")
 
918
                from gtk.gdk import color_parse
 
919
                foreground_color = color_parse(color)
 
920
                from gtk import STATE_NORMAL
 
921
                self.modify_text(STATE_NORMAL, foreground_color)
 
922
                self.__refresh_view()
 
923
                return
 
924
 
 
925
        def __background_cb(self, client, cnxn_id, entry, data):
 
926
                """
 
927
                Handles callback when the background color of the text editor changes.
 
928
 
 
929
                @param self: Reference to the ScribesTextView instance.
 
930
                @type self: A ScribesTextView object.
 
931
 
 
932
                @param client: A client used to query the GConf daemon and database
 
933
                @type client: A gconf.Client object.
 
934
 
 
935
                @param cnxn_id: The identification number for the GConf client.
 
936
                @type cnxn_id: An Integer object.
 
937
 
 
938
                @param entry: An entry from the GConf database.
 
939
                @type entry: A gconf.Entry object.
 
940
 
 
941
                @param data: Optional data
 
942
                @type data: Any type object.
 
943
                """
 
944
                color = client.get_string("/apps/scribes/bgcolor")
 
945
                from gtk.gdk import color_parse
 
946
                background_color = color_parse(color)
 
947
                from gtk import STATE_NORMAL
 
948
                self.modify_base(STATE_NORMAL, background_color)
 
949
                self.__refresh_view()
 
950
                return
 
951
 
 
952
        def __use_tabs_cb(self, client, cnxn_id, entry, data):
 
953
                """
 
954
                Handles callback #FIXME: yeap
 
955
 
 
956
                @param self: Reference to the ScribesTextView instance.
 
957
                @type self: A ScribesTextView object.
 
958
 
 
959
                @param client: A client used to query the GConf daemon and database
 
960
                @type client: A gconf.Client object.
 
961
 
 
962
                @param cnxn_id: The identification number for the GConf client.
 
963
                @type cnxn_id: An Integer object.
 
964
 
 
965
                @param entry: An entry from the GConf database.
 
966
                @type entry: A gconf.Entry object.
 
967
 
 
968
                @param data: Optional data
 
969
                @type data: Any type object.
 
970
                """
 
971
                use_tabs = client.get_bool("/apps/scribes/use_tabs")
 
972
                self.set_insert_spaces_instead_of_tabs(not use_tabs)
 
973
                self.__refresh_view()
 
974
                return
 
975
 
 
976
        def __syntax_cb(self, client, cnxn_id, entry, data):
 
977
                """
 
978
                Handles callback #FIXME: yeap
 
979
 
 
980
                @param self: Reference to the ScribesTextView instance.
 
981
                @type self: A ScribesTextView object.
 
982
 
 
983
                @param client: A client used to query the GConf daemon and database
 
984
                @type client: A gconf.Client object.
 
985
 
 
986
                @param cnxn_id: The identification number for the GConf client.
 
987
                @type cnxn_id: An Integer object.
 
988
 
 
989
                @param entry: An entry from the GConf database.
 
990
                @type entry: A gconf.Entry object.
 
991
 
 
992
                @param data: Optional data
 
993
                @type data: Any type object.
 
994
                """
 
995
                from syntax import activate_syntax_highlight
 
996
                from utils import get_language
 
997
                language = None
 
998
                if self.__editor.uri:
 
999
                        language = get_language(self.__editor.uri)
 
1000
                activate_syntax_highlight(self.get_buffer(), language)
 
1001
                from gobject import idle_add, PRIORITY_LOW
 
1002
                idle_add(self.__refresh, priority=PRIORITY_LOW)
 
1003
                return
 
1004
 
 
1005
        def __refresh_view(self):
 
1006
                """
 
1007
                Redraw elements of the view and its children.
 
1008
 
 
1009
                @param self: Reference to the ScribesTextView instance.
 
1010
                @type self: A ScribesTextView object.
 
1011
                """
 
1012
                from gobject import idle_add, PRIORITY_LOW
 
1013
                idle_add(self.__refresh, priority=PRIORITY_LOW)
 
1014
                self.grab_focus()
 
1015
                return
 
1016
 
 
1017
        def __refresh(self):
 
1018
                self.queue_draw()
 
1019
                self.queue_resize()
 
1020
                self.resize_children()
 
1021
                try:
 
1022
                        self.window.process_updates(True)
 
1023
                except:
 
1024
                        pass
 
1025
                self.__editor.response()
 
1026
                return False
 
1027
 
 
1028
        def __destroy(self):
 
1029
                """
 
1030
                Destroy instance of this class.
 
1031
 
 
1032
                @param self: Reference to the Store instance.
 
1033
                @type self: A Store object.
 
1034
                """
 
1035
                # Disconnect signals.
 
1036
                from utils import disconnect_signal, delete_attributes
 
1037
                disconnect_signal(self.__signal_id_1, self)
 
1038
                disconnect_signal(self.__signal_id_2, self)
 
1039
                disconnect_signal(self.__signal_id_3, self)
 
1040
                disconnect_signal(self.__signal_id_4, self)
 
1041
                disconnect_signal(self.__signal_id_5, self)
 
1042
                disconnect_signal(self.__signal_id_6, self)
 
1043
                disconnect_signal(self.__signal_id_7, self)
 
1044
                disconnect_signal(self.__signal_id_8, self)
 
1045
                disconnect_signal(self.__signal_id_9, self)
 
1046
                disconnect_signal(self.__signal_id_10, self)
 
1047
                disconnect_signal(self.__signal_id_11, self)
 
1048
                disconnect_signal(self.__signal_id_12, self.__editor)
 
1049
                disconnect_signal(self.__signal_id_13, self.__editor)
 
1050
                disconnect_signal(self.__signal_id_14, self.__editor)
 
1051
                disconnect_signal(self.__signal_id_15, self.__editor)
 
1052
                disconnect_signal(self.__signal_id_16, self.__editor)
 
1053
                disconnect_signal(self.__signal_id_17, self.__editor)
 
1054
                disconnect_signal(self.__signal_id_18, self.__editor)
 
1055
                disconnect_signal(self.__signal_id_19, self.__editor)
 
1056
                disconnect_signal(self.__signal_id_20, self.__editor)
 
1057
                disconnect_signal(self.__signal_id_21, self.__editor)
 
1058
                disconnect_signal(self.__signal_id_22, self.__editor)
 
1059
                disconnect_signal(self.__signal_id_23, self)
 
1060
                disconnect_signal(self.__signal_id_24, self)
 
1061
                disconnect_signal(self.__signal_id_25, self.__editor)
 
1062
                #self.destroy()
 
1063
                # Unregister object so that editor can quit.
 
1064
                self.__editor.unregister_termination_id(self.__registration_id)
 
1065
                delete_attributes(self)
 
1066
                # Delete data attributes.
 
1067
                del self
 
1068
                self = None
 
1069
                return