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

« back to all changes in this revision

Viewing changes to plugins/Indent/Trigger.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 © 2006 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
 
19
# USA
 
20
 
 
21
"""
 
22
This module documents a class that creates a trigger that indents lines.
 
23
 
 
24
@author: Lateef Alabi-Oki
 
25
@organization: The Scribes Project
 
26
@copyright: Copyright © 2006 Lateef Alabi-Oki
 
27
@license: GNU GPLv2 or Later
 
28
@contact: mystilleef@gmail.com
 
29
"""
 
30
 
 
31
from gobject import GObject, SIGNAL_RUN_LAST, TYPE_NONE
 
32
 
 
33
class IndentTrigger(GObject):
 
34
        """
 
35
        This class creates an object, a trigger, that indents or unindents
 
36
        lines in the text editor's buffer.
 
37
        """
 
38
 
 
39
        __gsignals__ = {
 
40
                "destroy": (SIGNAL_RUN_LAST, TYPE_NONE, ()),
 
41
        }
 
42
 
 
43
        def __init__(self, editor):
 
44
                """
 
45
                Initialize the trigger.
 
46
 
 
47
                @param self: Reference to the IndentTrigger instance.
 
48
                @type self: A IndentTrigger object.
 
49
 
 
50
                @param editor: Reference to the text editor.
 
51
                @type editor: An Editor object.
 
52
                """
 
53
                GObject.__init__(self)
 
54
                self.__init_attributes(editor)
 
55
                self.__create_trigger()
 
56
                self.__signal_id_1 = self.__trigger.connect("activate", self.__indent_cb)
 
57
                self.__signal_id_3 = self.__unindent_trigger.connect("activate", self.__unindent_cb)
 
58
                self.__signal_id_2 = self.connect("destroy", self.__destroy_cb)
 
59
                self.__signal_id_4 = self.__editor.textview.connect_after("populate-popup", self.__popup_cb)
 
60
 
 
61
        def __init_attributes(self, editor):
 
62
                """
 
63
                Initialize the trigger's attributes.
 
64
 
 
65
                @param self: Reference to the IndentTrigger instance.
 
66
                @type self: A IndentTrigger object.
 
67
 
 
68
                @param editor: Reference to the text editor.
 
69
                @type editor: An Editor object.
 
70
                """
 
71
                self.__editor = editor
 
72
                self.__trigger = None
 
73
                self.__unindent_trigger = None
 
74
                self.__signal_id_2 = None
 
75
                self.__signal_id_1 = None
 
76
                self.__signal_id_3 = None
 
77
                self.__signal_id_4 = None
 
78
                return
 
79
 
 
80
        def __create_trigger(self):
 
81
                """
 
82
                Create the trigger.
 
83
 
 
84
                @param self: Reference to the IndentTrigger instance.
 
85
                @type self: A IndentTrigger object.
 
86
                """
 
87
                # Trigger to indent lines.
 
88
                from SCRIBES.Trigger import Trigger
 
89
                self.__trigger = Trigger("indent_line", "ctrl - t")
 
90
                self.__editor.add_trigger(self.__trigger)
 
91
 
 
92
                # Trigger to unindent lines.
 
93
                self.__unindent_trigger = Trigger("unindent_line", "ctrl - T")
 
94
                self.__editor.add_trigger(self.__unindent_trigger)
 
95
                return
 
96
 
 
97
        def __indent_cb(self, trigger):
 
98
                """
 
99
                Handles callback when the "activate" signal is emitted.
 
100
 
 
101
                @param self: Reference to the IndentTrigger instance.
 
102
                @type self: A IndentTrigger object.
 
103
 
 
104
                @param trigger: An object to show the document browser.
 
105
                @type trigger: A Trigger object.
 
106
                """
 
107
                if self.__editor.is_readonly:
 
108
                        # Prevent indentation operations when the text editor is in
 
109
                        # readonly mode.
 
110
                        from i18n import msg0001
 
111
                        self.__editor.feedback.update_status_message(msg0001, "fail")
 
112
                        return
 
113
                from i18n import msg0002
 
114
                status_id = self.__editor.feedback.set_modal_message(msg0002, "run")
 
115
                from SCRIBES.cursor import show_busy_textview_cursor, show_textview_cursor
 
116
                show_busy_textview_cursor(self.__editor.textview)
 
117
                from indent import indent
 
118
                lines_indented = indent(self.__editor.textview)
 
119
                show_textview_cursor(self.__editor.textview)
 
120
                self.__editor.feedback.unset_modal_message(status_id, False)
 
121
                if len(lines_indented) > 1:
 
122
                        from i18n import msg0003
 
123
                        self.__editor.feedback.update_status_message(msg0003, "succeed")
 
124
                else:
 
125
                        from i18n import msg0004
 
126
                        value = lines_indented[0] + 1
 
127
                        message = msg0004 % (value)
 
128
                        self.__editor.feedback.update_status_message(message, "succeed")
 
129
                return
 
130
 
 
131
        def __unindent_cb(self, trigger):
 
132
                """
 
133
                Handles callback when the "activate" signal is emitted.
 
134
 
 
135
                @param self: Reference to the UnindentTrigger instance.
 
136
                @type self: A UnindentTrigger object.
 
137
 
 
138
                @param trigger: An object to show the document browser.
 
139
                @type trigger: A Trigger object.
 
140
                """
 
141
                if self.__editor.is_readonly:
 
142
                        # Prevent save operations when the text editor is in readonly mode.
 
143
                        from i18n import msg0001
 
144
                        self.__editor.feedback.update_status_message(msg0001, "fail")
 
145
                        return
 
146
                from i18n import msg0002
 
147
                status_id = self.__editor.feedback.set_modal_message(msg0002, "run")
 
148
                from SCRIBES.cursor import show_busy_textview_cursor, show_textview_cursor
 
149
                show_busy_textview_cursor(self.__editor.textview)
 
150
                from unindent import unindent
 
151
                lines_unindented = unindent(self.__editor.textview)
 
152
                show_textview_cursor(self.__editor.textview)
 
153
                self.__editor.feedback.unset_modal_message(status_id, False)
 
154
                if not lines_unindented:
 
155
                        from i18n import msg0005
 
156
                        self.__editor.feedback.update_status_message(msg0005, "fail")
 
157
                elif len(lines_unindented) > 1:
 
158
                        from i18n import msg0006
 
159
                        self.__editor.feedback.update_status_message(msg0006, "succeed")
 
160
                else:
 
161
                        from i18n import msg0007
 
162
                        value = lines_unindented[0] + 1
 
163
                        message = msg0007 % (value)
 
164
                        self.__editor.feedback.update_status_message(message, "succeed")
 
165
                return
 
166
 
 
167
        def __popup_cb(self, textview, menu):
 
168
                """
 
169
                Handles callback when the "populate-popup" signal is emitted.
 
170
 
 
171
                @param self: Reference to the IndentTrigger instance.
 
172
                @type self: An IndentTrigger object.
 
173
 
 
174
                @param textview: Reference to the editor's textview.
 
175
                @type textview: A ScribesTextView object.
 
176
 
 
177
                @param menu: Reference to the editor's popup menu.
 
178
                @type menu: A gtk.Menu object.
 
179
                """
 
180
                from PopupMenuItem import IndentPopupMenuItem
 
181
                menu.prepend(IndentPopupMenuItem(self.__editor))
 
182
                menu.show_all()
 
183
                return False
 
184
 
 
185
        def __destroy_cb(self, trigger):
 
186
                """
 
187
                Handles callback when the "activate" signal is emitted.
 
188
 
 
189
                @param self: Reference to the IndentTrigger instance.
 
190
                @type self: An IndentTrigger object.
 
191
 
 
192
                @param trigger: Reference to the IndentTrigger instance.
 
193
                @type trigger: A IndentTrigger object.
 
194
                """
 
195
                self.__editor.triggermanager.remove_trigger(self.__trigger)
 
196
                self.__editor.triggermanager.remove_trigger(self.__unindent_trigger)
 
197
                if self.__signal_id_1 and self.__trigger.handler_is_connected(self.__signal_id_1):
 
198
                        self.__trigger.disconnect(self.__signal_id_1)
 
199
                if self.__signal_id_2 and self.handler_is_connected(self.__signal_id_2):
 
200
                        self.disconnect(self.__signal_id_2)
 
201
                if self.__signal_id_3 and self.__unindent_trigger.handler_is_connected(self.__signal_id_3):
 
202
                        self.__unindent_trigger.disconnect(self.__signal_id_3)
 
203
                if self.__signal_id_4 and self.__editor.textview.handler_is_connected(self.__signal_id_4):
 
204
                        self.__editor.textview.disconnect(self.__signal_id_4)
 
205
                del self.__editor, self.__trigger, self.__unindent_trigger
 
206
                del self.__signal_id_2, self.__signal_id_1, self.__signal_id_3
 
207
                del self.__signal_id_4
 
208
                del self
 
209
                self = None
 
210
                return