def get_view (self, compact=False): """Generate a view widget for editing HTML.""" vbox=gtk.VBox() self.editor=HTMLEditor() self.editor.custom_url_loader=self.custom_url_loader self.editor.register_class_parser(self.class_parser) try: self.editor.set_text(self.element.data) except Exception, e: self.controller.log(_("HTML editor: cannot parse content (%s)") % unicode(e))
def get_view(self, compact=False): """Generate a view widget for editing HTML.""" vbox = Gtk.VBox() self.editor = HTMLEditor() self.editor.custom_url_loader = self.custom_url_loader self.editor.register_class_parser(self.class_parser) try: self.editor.set_text(self.element.data) except Exception as e: self.controller.log( _("HTML editor: cannot parse content (%s)") % str(e)) self.editor.connect('drag-data-received', self.editor_drag_received) self.editor.drag_dest_set( Gtk.DestDefaults.MOTION | Gtk.DestDefaults.HIGHLIGHT | Gtk.DestDefaults.ALL, config.data.get_target_types('annotation', 'annotation-type', 'timestamp'), Gdk.DragAction.COPY | Gdk.DragAction.LINK | Gdk.DragAction.ASK) self.editor.connect('button-press-event', self.button_press_cb) self.view = Gtk.VBox() def sel_copy(i): self.editor.get_buffer().copy_clipboard(get_clipboard()) return True def sel_cut(i): self.editor.get_buffer().cut_clipboard(get_clipboard()) return True def sel_paste(i): self.editor.get_buffer().paste_clipboard(get_clipboard()) return True def refresh(i): self.editor.refresh() return True def display_header_menu(i): m = Gtk.Menu() for h in (1, 2, 3): i = Gtk.MenuItem(_("Heading %d") % h) i.connect( 'activate', lambda w, level: self.editor.apply_html_tag('h%d' % level), h) m.append(i) m.show_all() m.popup(None, i, None, 1, Gtk.get_current_event_time()) return True tb = Gtk.Toolbar() vbox.toolbar = tb tb.set_style(Gtk.ToolbarStyle.ICONS) for (icon, tooltip, action) in ( (Gtk.STOCK_BOLD, _("Bold"), lambda i: self.editor.apply_html_tag('b')), (Gtk.STOCK_ITALIC, _("Italic"), lambda i: self.editor.apply_html_tag('i')), ("title_icon.png", _("Header"), display_header_menu), (None, None, None), (Gtk.STOCK_COPY, _("Copy"), sel_copy), (Gtk.STOCK_CUT, _("Cut"), sel_cut), (Gtk.STOCK_PASTE, _("Paste"), sel_paste), (None, None, None), (Gtk.STOCK_REFRESH, _("Refresh"), refresh), ): if not config.data.preferences[ 'expert-mode'] and icon == Gtk.STOCK_REFRESH: continue if not icon: b = Gtk.SeparatorToolItem() else: b = get_pixmap_toolbutton(icon, action) b.set_tooltip_text(tooltip) tb.insert(b, -1) b.show() if self.editor.can_undo(): b = Gtk.ToolButton(Gtk.STOCK_UNDO) b.connect('clicked', lambda i: self.editor.undo()) b.set_tooltip_text(_("Undo")) tb.insert(b, -1) b.show() self.view.pack_start(tb, False, True, 0) sw = Gtk.ScrolledWindow() sw.set_policy(Gtk.PolicyType.AUTOMATIC, Gtk.PolicyType.AUTOMATIC) sw.add(self.editor) context_data = ContextDisplay() def cursor_moved(buf, it, mark): if mark.get_name() == 'insert': context_data.set_context(self.editor.get_current_context(it)) return True self.editor.get_buffer().connect('mark-set', cursor_moved) sw2 = Gtk.ScrolledWindow() sw2.set_policy(Gtk.PolicyType.AUTOMATIC, Gtk.PolicyType.AUTOMATIC) sw2.add(context_data) p = Gtk.HPaned() p.add1(sw2) p.add2(sw) # Hide by default p.set_position(0) p.show_all() self.view.add(p) def edit_wysiwyg(*p): vbox.foreach(vbox.remove) vbox.add(self.view) self.editing_source = False vbox.show_all() return True def edit_source(*p): if self.sourceview is None: self.sourceview = TextContentHandler( element=self.element, controller=self.controller, parent=self.parent) self.sourceview.widget = self.sourceview.get_view() b = get_pixmap_toolbutton('xml.png', edit_wysiwyg) b.set_tooltip_text(_("WYSIWYG editor")) self.sourceview.toolbar.insert(b, 0) vbox.foreach(vbox.remove) vbox.add(self.sourceview.widget) self.editing_source = True vbox.show_all() return True b = get_pixmap_toolbutton('xml.png', edit_source) b.set_tooltip_text(_("Edit HTML source")) tb.insert(b, 0) if config.data.preferences['prefer-wysiwyg']: edit_wysiwyg() self.editor.set_modified(False) else: edit_source() self.sourceview.set_modified(False) return vbox