From 0c4ef746e91cb8987430b89f22cb4699e06410fe Mon Sep 17 00:00:00 2001 From: Jesse van den Kieboom Date: Mon, 03 May 2010 11:48:21 +0000 Subject: Make multi edit menu item proper toggle item --- diff --git a/plugins/multiedit/multiedit/documenthelper.py b/plugins/multiedit/multiedit/documenthelper.py index 58ccac7..df08c7f 100644 --- a/plugins/multiedit/multiedit/documenthelper.py +++ b/plugins/multiedit/multiedit/documenthelper.py @@ -69,6 +69,16 @@ class DocumentHelper(Signals): self.reset_buffer(self._view.get_buffer()) self.initialize_event_handlers() + self.toggle_callback = None + + def get_view(self): + return self._view + + def set_toggle_callback(self, callback, data): + self.toggle_callback = lambda: callback(data) + + def enabled(self): + return self._in_mode def _update_selection_tag(self): style = self._view.get_style() @@ -147,12 +157,21 @@ class DocumentHelper(Signals): self._view.set_border_window_size(gtk.TEXT_WINDOW_TOP, 0) self.remove_edit_points() + if self.toggle_callback: + self.toggle_callback() + def enable_multi_edit(self): self._view.set_border_window_size(gtk.TEXT_WINDOW_TOP, 20) self._in_mode = True - def toggle_multi_edit(self): - if self._in_mode: + if self.toggle_callback: + self.toggle_callback() + + def toggle_multi_edit(self, enabled): + if self.enabled() == enabled: + return + + if self.enabled(): self.disable_multi_edit() else: self.enable_multi_edit() diff --git a/plugins/multiedit/multiedit/windowhelper.py b/plugins/multiedit/multiedit/windowhelper.py index d9941ae..adae3af 100644 --- a/plugins/multiedit/multiedit/windowhelper.py +++ b/plugins/multiedit/multiedit/windowhelper.py @@ -58,14 +58,15 @@ class WindowHelper(Signals): self.connect_signal(window, 'tab-added', self.on_tab_added) self.connect_signal(window, 'tab-removed', self.on_tab_removed) - + self.connect_signal(window, 'active-tab-changed', self.on_active_tab_changed) + self.install_ui() def install_ui(self): manager = self._window.get_ui_manager() self._action_group = gtk.ActionGroup("GeditMultiEditPluginActions") - self._action_group.add_actions( + self._action_group.add_toggle_actions( [('MultiEditModeAction', None, _('Multi Edit Mode'), 'C', _('Start multi edit mode'), self.on_multi_edit_mode)]) manager.insert_action_group(self._action_group, -1) @@ -95,26 +96,40 @@ class WindowHelper(Signals): def add_document_helper(self, view): if view.get_data(constants.DOCUMENT_HELPER_KEY) != None: return - - DocumentHelper(view) + + helper = DocumentHelper(view) + helper.set_toggle_callback(self.on_multi_edit_toggled, helper) def remove_document_helper(self, view): helper = view.get_data(constants.DOCUMENT_HELPER_KEY) if helper != None: helper.stop() - + + def get_action(self): + return self._action_group.get_action('MultiEditModeAction') + + def on_multi_edit_toggled(self, helper): + if helper.get_view() == self._window.get_active_view(): + self.get_action().set_active(helper.enabled()) + def on_tab_added(self, window, tab): self.add_document_helper(tab.get_view()) def on_tab_removed(self, window, tab): self.remove_document_helper(tab.get_view()) - + + def on_active_tab_changed(self, window, tab): + view = tab.get_view() + helper = view.get_data(constants.DOCUMENT_HELPER_KEY) + + self.get_action().set_active(helper != None and helper.enabled()) + def on_multi_edit_mode(self, action): view = self._window.get_active_view() helper = view.get_data(constants.DOCUMENT_HELPER_KEY) if helper != None: - helper.toggle_multi_edit() + helper.toggle_multi_edit(self.get_action().get_active()) # ex:ts=4:et: -- cgit v0.8.3.1