def create_menuitem(string, stock_id=None): from gtk import MenuItem, Image, HBox, Label hbox = HBox(spacing=7) hbox.set_property("border-width", 2) if stock_id: image = Image() image.set_property("stock", stock_id) hbox.pack_start(image, False, False, 0) label = Label(string) label.set_property("use-underline", True) hbox.pack_start(label, False, False, 0) menuitem = MenuItem() menuitem.add(hbox) return menuitem
class PopupMenuItem(ImageMenuItem): def __init__(self, editor): ImageMenuItem.__init__(self, _("In_dentation")) self.__init_attributes(editor) self.__set_properties() self.__sigid1 = self.__menuitem1.connect("activate", self.__activate_cb) self.__sigid2 = self.__menuitem2.connect("activate", self.__activate_cb) self.__sigid3 = self.__textview.connect("focus-in-event", self.__focus_cb) def __init_attributes(self, editor): self.__editor = editor from gtk import STOCK_INDENT, STOCK_UNINDENT, Image, Menu self.__image = Image() self.__menu = Menu() self.__textview = editor.textview self.__menuitem1 = editor.create_menuitem(_("Shift _Right (ctrl + t)"), STOCK_INDENT) self.__menuitem2 = editor.create_menuitem( _("Shift _Left (ctrl + shift + t)"), STOCK_UNINDENT) return def __set_properties(self): self.set_property("name", "Indentation Popup MenuItem") from gtk import STOCK_JUSTIFY_CENTER self.__image.set_property("stock", STOCK_JUSTIFY_CENTER) self.set_image(self.__image) self.set_submenu(self.__menu) self.__menu.append(self.__menuitem1) self.__menu.append(self.__menuitem2) if self.__editor.readonly: self.set_property("sensitive", False) return def __activate_cb(self, menuitem): if menuitem == self.__menuitem1: self.__editor.trigger("indent") else: self.__editor.trigger("unindent") return True def __focus_cb(self, *args): self.__editor.disconnect_signal(self.__sigid1, self.__menuitem1) self.__editor.disconnect_signal(self.__sigid2, self.__menuitem1) self.__editor.disconnect_signal(self.__sigid3, self.__textview) self.destroy() del self self = None return False
class PopupMenuItem(ImageMenuItem): def __init__(self, editor): ImageMenuItem.__init__(self, _("Selection")) self.__init_attributes(editor) self.__creates_widgets() self.__set_properties() self.__signal_id_1 = self.select_word_menuitem.connect( "activate", self.__popup_activate_cb) self.__signal_id_2 = self.select_line_menuitem.connect( "activate", self.__popup_activate_cb) self.__signal_id_3 = self.select_sentence_menuitem.connect( "activate", self.__popup_activate_cb) self.__signal_id_4 = self.paragraph_item.connect( "activate", self.__popup_activate_cb) self.__signal_id_5 = self.select_word_menuitem.connect( "map-event", self.__popup_word_map_event_cb) self.__signal_id_6 = self.select_line_menuitem.connect( "map-event", self.__popup_line_map_event_cb) self.__signal_id_7 = self.select_sentence_menuitem.connect( "map-event", self.__popup_sentence_map_event_cb) self.__signal_id_9 = self.scribesview.connect("focus-in-event", self.__focus_in_event_cb) def __init_attributes(self, editor): self.scribesview = editor.textview self.editor = editor self.menu = None self.image = None self.select_word_menuitem = None self.select_line_menuitem = None self.select_sentence_menuitem = None return def __creates_widgets(self): from gtk import Image, STOCK_BOLD, Menu self.image = Image() self.image.set_property("stock", STOCK_BOLD) self.menu = Menu() self.select_word_menuitem = self.editor.create_menuitem( _("Select word (alt + w)")) self.select_line_menuitem = self.editor.create_menuitem( _("Select line (alt + l)")) self.select_sentence_menuitem = self.editor.create_menuitem( _("Select sentence (alt + s)")) self.paragraph_item = self.editor.create_menuitem( _("Select paragraph (alt + p)")) return def __set_properties(self): self.set_image(self.image) self.set_submenu(self.menu) self.menu.append(self.select_line_menuitem) self.menu.append(self.select_word_menuitem) self.menu.append(self.select_sentence_menuitem) self.menu.append(self.paragraph_item) if self.editor.readonly: self.set_property("sensitive", False) return def __popup_activate_cb(self, menuitem): if menuitem == self.select_word_menuitem: self.editor.trigger("select-word") elif menuitem == self.select_line_menuitem: self.editor.trigger("select-line") elif menuitem == self.select_sentence_menuitem: self.editor.trigger("select-sentence") elif menuitem == self.paragraph_item: self.editor.trigger("select-paragraph") return True def __popup_word_map_event_cb(self, menuitem, event): menuitem.set_property("sensitive", False) from word import inside_word, starts_word, ends_word cursor_position = self.editor.get_cursor_iterator() if inside_word(cursor_position) or starts_word( cursor_position) or ends_word(cursor_position): menuitem.set_property("sensitive", True) return True def __popup_line_map_event_cb(self, menuitem, event): menuitem.set_property("sensitive", False) from lines import get_line_bounds begin_position, end_position = get_line_bounds(self.editor.textbuffer) if not begin_position.get_char() in ["\n", "\x00"]: menuitem.set_property("sensitive", True) return True def __popup_sentence_map_event_cb(self, menuitem, event): menuitem.set_property("sensitive", False) cursor_position = self.editor.get_cursor_iterator() if cursor_position.starts_sentence() or cursor_position.ends_sentence( ) or cursor_position.inside_sentence(): menuitem.set_property("sensitive", True) return True def __focus_in_event_cb(self, event, textview): self.editor.disconnect_signal(self.__signal_id_1, self.select_word_menuitem) self.editor.disconnect_signal(self.__signal_id_2, self.select_line_menuitem) self.editor.disconnect_signal(self.__signal_id_3, self.select_sentence_menuitem) self.editor.disconnect_signal(self.__signal_id_4, self.select_sentence_menuitem) self.editor.disconnect_signal(self.__signal_id_5, self.select_word_menuitem) self.editor.disconnect_signal(self.__signal_id_6, self.select_line_menuitem) self.editor.disconnect_signal(self.__signal_id_7, self.select_sentence_menuitem) self.editor.disconnect_signal(self.__signal_id_9, self.scribesview) if self.select_word_menuitem: self.select_word_menuitem.destroy() if self.select_sentence_menuitem: self.select_sentence_menuitem.destroy() if self.select_line_menuitem: self.select_line_menuitem.destroy() if self.image: self.image.destroy() if self.menu: self.menu.destroy() del self self = None return False
class PopupMenuItem(ImageMenuItem): def __init__(self, editor): ImageMenuItem.__init__(self, _("_Lines")) self.__init_attributes(editor) self.__set_properties() self.__sigid1 = self.__menuitem1.connect("activate", self.__activate_cb) self.__sigid2 = self.__menuitem2.connect("activate", self.__activate_cb) self.__sigid3 = self.__menuitem3.connect("activate", self.__activate_cb) self.__sigid4 = self.__menuitem4.connect("activate", self.__activate_cb) self.__sigid5 = self.__menuitem5.connect("activate", self.__activate_cb) self.__sigid6 = self.__menuitem6.connect("activate", self.__activate_cb) self.__sigid7 = self.__menuitem7.connect("activate", self.__activate_cb) self.__sigid8 = self.__view.connect("focus-in-event", self.__destroy_cb) def __init_attributes(self, editor): self.__editor = editor self.__view = editor.textview from gtk import Menu, Image self.__menu = Menu() self.__image = Image() self.__menuitem1 = self.__editor.create_menuitem(_("_Join Line (alt + j)")) self.__menuitem2 = self.__editor.create_menuitem(_("D_uplicate Line (ctrl + u)")) self.__menuitem3 = self.__editor.create_menuitem(_("_Delete Line (alt + d)")) self.__menuitem4 = self.__editor.create_menuitem(_("Free Line _Below (alt + o)")) self.__menuitem5 = self.__editor.create_menuitem(_("Free Line _Above (alt + shift + o)")) self.__menuitem6 = self.__editor.create_menuitem(_("Delete Cursor to Line _End (alt + End)")) self.__menuitem7 = self.__editor.create_menuitem(_("Delete _Cursor to Line Begin (alt + Home)")) return def __set_properties(self): self.set_property("name", "Line Operation Menuitem") from gtk import STOCK_JUSTIFY_CENTER self.__image.set_property("stock", STOCK_JUSTIFY_CENTER) self.set_image(self.__image) self.set_submenu(self.__menu) self.__menu.append(self.__menuitem1) self.__menu.append(self.__menuitem2) self.__menu.append(self.__menuitem3) self.__menu.append(self.__menuitem4) self.__menu.append(self.__menuitem5) self.__menu.append(self.__menuitem6) self.__menu.append(self.__menuitem7) if self.__editor.readonly: self.set_property("sensitive", False) return def __activate_cb(self, menuitem): if menuitem == self.__menuitem1: self.__editor.trigger("join-line") elif menuitem == self.__menuitem2: self.__editor.trigger("duplicate-line") elif menuitem == self.__menuitem3: self.__editor.trigger("delete-line") elif menuitem == self.__menuitem4: self.__editor.trigger("free-line-below") elif menuitem == self.__menuitem5: self.__editor.trigger("free-line-above") elif menuitem == self.__menuitem6: self.__editor.trigger("delete-cursor-to-end") elif menuitem == self.__menuitem7: self.__editor.trigger("delete-cursor-to-start") return True def __destroy_cb(self, *args): self.__editor.disconnect_signal(self.__sigid1, self.__menuitem1) self.__editor.disconnect_signal(self.__sigid2, self.__menuitem2) self.__editor.disconnect_signal(self.__sigid3, self.__menuitem3) self.__editor.disconnect_signal(self.__sigid4, self.__menuitem4) self.__editor.disconnect_signal(self.__sigid5, self.__menuitem5) self.__editor.disconnect_signal(self.__sigid6, self.__menuitem6) self.__editor.disconnect_signal(self.__sigid7, self.__menuitem7) self.__editor.disconnect_signal(self.__sigid8, self.__view) self.destroy() del self self = None return False
class PopupMenuItem(ImageMenuItem): def __init__(self, editor): ImageMenuItem.__init__(self, _("_Case")) self.__init_attributes(editor) self.__set_properties() self.__sigid1 = self.__menuitem1.connect("activate", self.__activate_cb) self.__sigid2 = self.__menuitem2.connect("activate", self.__activate_cb) self.__sigid3 = self.__menuitem3.connect("activate", self.__activate_cb) self.__sigid4 = self.__menuitem1.connect("map-event", self.__map_event_cb) self.__sigid5 = self.__menuitem2.connect("map-event", self.__map_event_cb) self.__sigid6 = self.__menuitem3.connect("map-event", self.__map_event_cb) self.__sigid7 = self.__editor.textview.connect("focus-in-event", self.__focus_cb) def __init_attributes(self, editor): from gtk import Menu, Image, STOCK_SORT_DESCENDING self.__image = Image() self.__image.set_property("stock", STOCK_SORT_DESCENDING) self.__editor = editor self.__menu = Menu() self.__menuitem1 = editor.create_menuitem(_("_Togglecase (alt + u)")) self.__menuitem2 = editor.create_menuitem(_("_Titlecase (alt + shift + u)")) self.__menuitem3 = editor.create_menuitem(_("_Swapcase (alt + shift + l)")) return def __set_properties(self): self.set_property("name", "Case Popup MenuItem") self.set_image(self.__image) self.set_submenu(self.__menu) self.__menu.append(self.__menuitem1) self.__menu.append(self.__menuitem2) self.__menu.append(self.__menuitem3) if self.__editor.readonly: self.set_property("sensitive", False) return def __activate_cb(self, menuitem): if menuitem == self.__menuitem1: self.__editor.trigger("togglecase") elif menuitem == self.__menuitem2: self.__editor.trigger("titlecase") else: self.__editor.trigger("swapcase") return True def __map_event_cb(self, menuitem, event): return False def __focus_cb(self, textview, event): self.__editor.disconnect_signal(self.__sigid1, self.__menuitem1) self.__editor.disconnect_signal(self.__sigid2, self.__menuitem2) self.__editor.disconnect_signal(self.__sigid3, self.__menuitem3) self.__editor.disconnect_signal(self.__sigid4, self.__menuitem1) self.__editor.disconnect_signal(self.__sigid5, self.__menuitem2) self.__editor.disconnect_signal(self.__sigid6, self.__menuitem3) self.__editor.disconnect_signal(self.__sigid7, self.__editor.textview) self.__menu.destroy() self.__image.destroy() self.destroy() del self self = None return False
class PopupMenuItem(ImageMenuItem): def __init__(self, editor): ImageMenuItem.__init__(self, _("_Case")) self.__init_attributes(editor) self.__set_properties() self.__sigid1 = self.__menuitem1.connect("activate", self.__activate_cb) self.__sigid2 = self.__menuitem2.connect("activate", self.__activate_cb) self.__sigid3 = self.__menuitem3.connect("activate", self.__activate_cb) self.__sigid4 = self.__menuitem1.connect("map-event", self.__map_event_cb) self.__sigid5 = self.__menuitem2.connect("map-event", self.__map_event_cb) self.__sigid6 = self.__menuitem3.connect("map-event", self.__map_event_cb) self.__sigid7 = self.__editor.textview.connect("focus-in-event", self.__focus_cb) def __init_attributes(self, editor): from gtk import Menu, Image, STOCK_SORT_DESCENDING self.__image = Image() self.__image.set_property("stock", STOCK_SORT_DESCENDING) self.__editor = editor self.__menu = Menu() self.__menuitem1 = editor.create_menuitem(_("_Togglecase (alt + u)")) self.__menuitem2 = editor.create_menuitem( _("_Titlecase (alt + shift + u)")) self.__menuitem3 = editor.create_menuitem( _("_Swapcase (alt + shift + l)")) return def __set_properties(self): self.set_property("name", "Case Popup MenuItem") self.set_image(self.__image) self.set_submenu(self.__menu) self.__menu.append(self.__menuitem1) self.__menu.append(self.__menuitem2) self.__menu.append(self.__menuitem3) if self.__editor.readonly: self.set_property("sensitive", False) return def __activate_cb(self, menuitem): if menuitem == self.__menuitem1: self.__editor.trigger("togglecase") elif menuitem == self.__menuitem2: self.__editor.trigger("titlecase") else: self.__editor.trigger("swapcase") return True def __map_event_cb(self, menuitem, event): return False def __focus_cb(self, textview, event): self.__editor.disconnect_signal(self.__sigid1, self.__menuitem1) self.__editor.disconnect_signal(self.__sigid2, self.__menuitem2) self.__editor.disconnect_signal(self.__sigid3, self.__menuitem3) self.__editor.disconnect_signal(self.__sigid4, self.__menuitem1) self.__editor.disconnect_signal(self.__sigid5, self.__menuitem2) self.__editor.disconnect_signal(self.__sigid6, self.__menuitem3) self.__editor.disconnect_signal(self.__sigid7, self.__editor.textview) self.__menu.destroy() self.__image.destroy() self.destroy() del self self = None return False
class PopupMenuItem(ImageMenuItem): def __init__(self, editor): ImageMenuItem.__init__(self, _("Paragraph")) self.__init_attributes(editor) self.__set_properties() self.__sig_id_1 = self.__next_item.connect("activate", self.__activate_cb) self.__sig_id_2 = self.__previous_item.connect("activate", self.__activate_cb) self.__sig_id_3 = self.__reflow_item.connect("activate", self.__activate_cb) self.__sig_id_4 = editor.textview.connect("focus-in-event", self.__destroy_cb) self.__sig_id_5 = self.__select_item.connect("activate", self.__activate_cb) def __init_attributes(self, editor): from gtk import Menu, Image self.__editor = editor self.__menu = Menu() self.__image = Image() self.__previous_item = editor.create_menuitem( _("Previous Paragraph (alt + Right)")) self.__next_item = editor.create_menuitem( _("Next Paragraph (alt + Left)")) self.__reflow_item = editor.create_menuitem( _("Reflow Paragraph (alt + q)")) self.__select_item = editor.create_menuitem( _("Select Paragraph (alt + p)")) return def __set_properties(self): from gtk import STOCK_JUMP_TO self.__image.set_property("stock", STOCK_JUMP_TO) self.set_image(self.__image) self.set_submenu(self.__menu) self.__menu.append(self.__previous_item) self.__menu.append(self.__next_item) self.__menu.append(self.__reflow_item) self.__menu.append(self.__select_item) if self.__editor.readonly: self.__reflow_item.set_property("sensitive", False) return def __activate_cb(self, menuitem): if menuitem == self.__previous_item: self.__editor.trigger("previous-paragraph") elif menuitem == self.__next_item: self.__editor.trigger("next-paragraph") elif menuitem == self.__select_item: self.__editor.trigger("select-paragraph") else: self.__editor.trigger("reflow-paragraph") return False def __destroy_cb(self, *args): self.__editor.disconnect_signal(self.__sig_id_1, self.__next_item) self.__editor.disconnect_signal(self.__sig_id_2, self.__previous_item) self.__editor.disconnect_signal(self.__sig_id_3, self.__reflow_item) self.__editor.disconnect_signal(self.__sig_id_4, self.__editor.textview) self.__editor.disconnect_signal(self.__sig_id_5, self.__select_item) self.__next_item.destroy() self.__select_item.destroy() self.__previous_item.destroy() self.__reflow_item.destroy() self.__menu.destroy() self.__image.destroy() self.destroy() del self self = None return False
class PopupMenuItem(ImageMenuItem): def __init__(self, editor): ImageMenuItem.__init__(self, _("Paragraph")) self.__init_attributes(editor) self.__set_properties() self.__sig_id_1 = self.__next_item.connect("activate", self.__activate_cb) self.__sig_id_2 = self.__previous_item.connect("activate", self.__activate_cb) self.__sig_id_3 = self.__reflow_item.connect("activate", self.__activate_cb) self.__sig_id_4 = editor.textview.connect("focus-in-event", self.__destroy_cb) self.__sig_id_5 = self.__select_item.connect("activate", self.__activate_cb) def __init_attributes(self, editor): from gtk import Menu, Image self.__editor = editor self.__menu = Menu() self.__image = Image() self.__previous_item = editor.create_menuitem(_("Previous Paragraph (alt + Right)")) self.__next_item = editor.create_menuitem(_("Next Paragraph (alt + Left)")) self.__reflow_item = editor.create_menuitem(_("Reflow Paragraph (alt + q)")) self.__select_item = editor.create_menuitem(_("Select Paragraph (alt + p)")) return def __set_properties(self): from gtk import STOCK_JUMP_TO self.__image.set_property("stock", STOCK_JUMP_TO) self.set_image(self.__image) self.set_submenu(self.__menu) self.__menu.append(self.__previous_item) self.__menu.append(self.__next_item) self.__menu.append(self.__reflow_item) self.__menu.append(self.__select_item) if self.__editor.readonly: self.__reflow_item.set_property("sensitive", False) return def __activate_cb(self, menuitem): if menuitem == self.__previous_item: self.__editor.trigger("previous-paragraph") elif menuitem == self.__next_item: self.__editor.trigger("next-paragraph") elif menuitem == self.__select_item: self.__editor.trigger("select-paragraph") else: self.__editor.trigger("reflow-paragraph") return False def __destroy_cb(self, *args): self.__editor.disconnect_signal(self.__sig_id_1, self.__next_item) self.__editor.disconnect_signal(self.__sig_id_2, self.__previous_item) self.__editor.disconnect_signal(self.__sig_id_3, self.__reflow_item) self.__editor.disconnect_signal(self.__sig_id_4, self.__editor.textview) self.__editor.disconnect_signal(self.__sig_id_5, self.__select_item) self.__next_item.destroy() self.__select_item.destroy() self.__previous_item.destroy() self.__reflow_item.destroy() self.__menu.destroy() self.__image.destroy() self.destroy() del self self = None return False
class PopupMenuItem(ImageMenuItem): def __init__(self, editor): ImageMenuItem.__init__(self, _("Selection")) self.__init_attributes(editor) self.__creates_widgets() self.__set_properties() self.__signal_id_1 = self.select_word_menuitem.connect("activate", self.__popup_activate_cb) self.__signal_id_2 = self.select_line_menuitem.connect("activate", self.__popup_activate_cb) self.__signal_id_3 = self.select_sentence_menuitem.connect("activate", self.__popup_activate_cb) self.__signal_id_4 = self.paragraph_item.connect("activate", self.__popup_activate_cb) self.__signal_id_5 = self.select_word_menuitem.connect("map-event", self.__popup_word_map_event_cb) self.__signal_id_6 = self.select_line_menuitem.connect("map-event", self.__popup_line_map_event_cb) self.__signal_id_7 = self.select_sentence_menuitem.connect("map-event", self.__popup_sentence_map_event_cb) self.__signal_id_9 = self.scribesview.connect("focus-in-event", self.__focus_in_event_cb) def __init_attributes(self, editor): self.scribesview = editor.textview self.editor = editor self.menu = None self.image = None self.select_word_menuitem = None self.select_line_menuitem = None self.select_sentence_menuitem = None return def __creates_widgets(self): from gtk import Image, STOCK_BOLD, Menu self.image = Image() self.image.set_property("stock", STOCK_BOLD) self.menu = Menu() self.select_word_menuitem = self.editor.create_menuitem(_("Select word (alt + w)")) self.select_line_menuitem = self.editor.create_menuitem(_("Select line (alt + l)")) self.select_sentence_menuitem = self.editor.create_menuitem(_("Select sentence (alt + s)")) self.paragraph_item = self.editor.create_menuitem(_("Select paragraph (alt + p)")) return def __set_properties(self): self.set_image(self.image) self.set_submenu(self.menu) self.menu.append(self.select_line_menuitem) self.menu.append(self.select_word_menuitem) self.menu.append(self.select_sentence_menuitem) self.menu.append(self.paragraph_item) if self.editor.readonly: self.set_property("sensitive", False) return def __popup_activate_cb(self, menuitem): if menuitem == self.select_word_menuitem: self.editor.trigger("select-word") elif menuitem == self.select_line_menuitem: self.editor.trigger("select-line") elif menuitem == self.select_sentence_menuitem: self.editor.trigger("select-sentence") elif menuitem == self.paragraph_item: self.editor.trigger("select-paragraph") return True def __popup_word_map_event_cb(self, menuitem, event): menuitem.set_property("sensitive", False) from word import inside_word, starts_word, ends_word cursor_position = self.editor.get_cursor_iterator() if inside_word(cursor_position) or starts_word(cursor_position) or ends_word(cursor_position): menuitem.set_property("sensitive", True) return True def __popup_line_map_event_cb(self, menuitem, event): menuitem.set_property("sensitive", False) from lines import get_line_bounds begin_position, end_position = get_line_bounds(self.editor.textbuffer) if not begin_position.get_char() in ["\n", "\x00"]: menuitem.set_property("sensitive", True) return True def __popup_sentence_map_event_cb(self, menuitem, event): menuitem.set_property("sensitive", False) cursor_position = self.editor.get_cursor_iterator() if cursor_position.starts_sentence() or cursor_position.ends_sentence() or cursor_position.inside_sentence(): menuitem.set_property("sensitive", True) return True def __focus_in_event_cb(self, event, textview): self.editor.disconnect_signal(self.__signal_id_1, self.select_word_menuitem) self.editor.disconnect_signal(self.__signal_id_2, self.select_line_menuitem) self.editor.disconnect_signal(self.__signal_id_3, self.select_sentence_menuitem) self.editor.disconnect_signal(self.__signal_id_4, self.select_sentence_menuitem) self.editor.disconnect_signal(self.__signal_id_5, self.select_word_menuitem) self.editor.disconnect_signal(self.__signal_id_6, self.select_line_menuitem) self.editor.disconnect_signal(self.__signal_id_7, self.select_sentence_menuitem) self.editor.disconnect_signal(self.__signal_id_9, self.scribesview) if self.select_word_menuitem: self.select_word_menuitem.destroy() if self.select_sentence_menuitem: self.select_sentence_menuitem.destroy() if self.select_line_menuitem: self.select_line_menuitem.destroy() if self.image: self.image.destroy() if self.menu: self.menu.destroy() del self self = None return False