1
# -*- coding: utf-8 -*-
2
# Copyright © 2006 Lateef Alabi-Oki
4
# This file is part of Scribes.
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.
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.
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
22
This module documents a class that creates a trigger that indents lines.
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
31
from gobject import GObject, SIGNAL_RUN_LAST, TYPE_NONE
33
class IndentTrigger(GObject):
35
This class creates an object, a trigger, that indents or unindents
36
lines in the text editor's buffer.
40
"destroy": (SIGNAL_RUN_LAST, TYPE_NONE, ()),
43
def __init__(self, editor):
45
Initialize the trigger.
47
@param self: Reference to the IndentTrigger instance.
48
@type self: A IndentTrigger object.
50
@param editor: Reference to the text editor.
51
@type editor: An Editor object.
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)
61
def __init_attributes(self, editor):
63
Initialize the trigger's attributes.
65
@param self: Reference to the IndentTrigger instance.
66
@type self: A IndentTrigger object.
68
@param editor: Reference to the text editor.
69
@type editor: An Editor object.
71
self.__editor = editor
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
80
def __create_trigger(self):
84
@param self: Reference to the IndentTrigger instance.
85
@type self: A IndentTrigger object.
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)
92
# Trigger to unindent lines.
93
self.__unindent_trigger = Trigger("unindent_line", "ctrl - T")
94
self.__editor.add_trigger(self.__unindent_trigger)
97
def __indent_cb(self, trigger):
99
Handles callback when the "activate" signal is emitted.
101
@param self: Reference to the IndentTrigger instance.
102
@type self: A IndentTrigger object.
104
@param trigger: An object to show the document browser.
105
@type trigger: A Trigger object.
107
if self.__editor.is_readonly:
108
# Prevent indentation operations when the text editor is in
110
from i18n import msg0001
111
self.__editor.feedback.update_status_message(msg0001, "fail")
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")
125
from i18n import msg0004
126
value = lines_indented[0] + 1
127
message = msg0004 % (value)
128
self.__editor.feedback.update_status_message(message, "succeed")
131
def __unindent_cb(self, trigger):
133
Handles callback when the "activate" signal is emitted.
135
@param self: Reference to the UnindentTrigger instance.
136
@type self: A UnindentTrigger object.
138
@param trigger: An object to show the document browser.
139
@type trigger: A Trigger object.
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")
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")
161
from i18n import msg0007
162
value = lines_unindented[0] + 1
163
message = msg0007 % (value)
164
self.__editor.feedback.update_status_message(message, "succeed")
167
def __popup_cb(self, textview, menu):
169
Handles callback when the "populate-popup" signal is emitted.
171
@param self: Reference to the IndentTrigger instance.
172
@type self: An IndentTrigger object.
174
@param textview: Reference to the editor's textview.
175
@type textview: A ScribesTextView object.
177
@param menu: Reference to the editor's popup menu.
178
@type menu: A gtk.Menu object.
180
from PopupMenuItem import IndentPopupMenuItem
181
menu.prepend(IndentPopupMenuItem(self.__editor))
185
def __destroy_cb(self, trigger):
187
Handles callback when the "activate" signal is emitted.
189
@param self: Reference to the IndentTrigger instance.
190
@type self: An IndentTrigger object.
192
@param trigger: Reference to the IndentTrigger instance.
193
@type trigger: A IndentTrigger object.
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