def __init__(self): Gtk.Plug.__init__(self) Gtk.Plug.construct(self, wid) lang_manager = GtkSource.LanguageManager() lang = lang_manager.guess_language(path) style_manager = GtkSource.StyleSchemeManager.get_default() style = style_manager.get_scheme("classic") f = open(path, "r", encoding=ENCODING, errors="replace") txt = f.read() f.close() self.srcbuf = GtkSource.Buffer() self.srcbuf.set_style_scheme(style) self.srcbuf.set_language(lang) self.srcbuf.set_text(txt) scroll = Gtk.ScrolledWindow() view = GtkSource.View.new_with_buffer(self.srcbuf) view.set_show_line_numbers(True) view.set_highlight_current_line(True) view.get_space_drawer().set_enable_matrix(True) view.set_editable(False) view.set_tab_width(8) #view.override_font(Pango.FontDescription('mono 12')) view.set_name("sourceview") cssprovider = Gtk.CssProvider() cssprovider.load_from_path( os.path.dirname(sys.argv[0]) + "/gtksourceview.css") Gtk.StyleContext.add_provider_for_screen( Gdk.Screen.get_default(), cssprovider, Gtk.STYLE_PROVIDER_PRIORITY_APPLICATION) scroll.add(view) hbox = Gtk.Box(orientation=Gtk.Orientation.HORIZONTAL) self.lbl_search = Gtk.Label() button = Gtk.Button.new_with_label("Set text") button.connect("pressed", self.search_dialog) self.btn_case = Gtk.ToggleButton.new_with_label("Case sensitive") self.btn_case.connect("pressed", self.search_case) self.btn_regex = Gtk.ToggleButton.new_with_label("RegEx") self.btn_regex.connect("pressed", self.search_regex) hbox.pack_start(self.btn_case, False, True, 2) hbox.pack_start(self.btn_regex, False, True, 2) hbox.pack_start(button, False, True, 2) hbox.pack_end(self.lbl_search, True, True, 1) hbox.set_border_width(5) win = Gtk.Box(orientation=Gtk.Orientation.VERTICAL) win.pack_start(scroll, True, True, 1) win.pack_end(hbox, False, True, 1) self.sopts = GtkSource.SearchSettings() self.scontext = GtkSource.SearchContext(buffer=self.srcbuf, settings=self.sopts) self.add(win)
def __init__(self): super().__init__() self.document = None self.buffer = GtkSource.Buffer() self.manager = GtkSource.LanguageManager() self.language = self.manager.get_language("markdown") self.buffer.set_language(self.language) self.buffer.create_tag('match', background="#66ff00") self.view = GtkSource.View() self.view.set_buffer(self.buffer) self.view.set_wrap_mode(Gtk.WrapMode.WORD) self.view.set_auto_indent(True) self.view.set_smart_home_end(True) self.view.set_insert_spaces_instead_of_tabs(True) self.view.set_tab_width(4) # self.view.set_pixels_above_lines(4) # self.view.set_pixels_below_lines(4) self.view.set_left_margin(8) self.view.set_right_margin(8) self.view.get_style_context().add_class('norka-editor') self.view.connect('key-release-event', self.on_key_release_event) self.scrolled = Gtk.ScrolledWindow(hexpand=True, vexpand=True) self.scrolled.add(self.view) # SearchBar self.search_bar = SearchBar() self.search_revealer = Gtk.Revealer() self.search_revealer.add(self.search_bar) self.search_bar.connect('find-changed', self.do_next_match) self.search_bar.connect('find-next', self.do_next_match) self.search_bar.connect('find-prev', self.do_previous_match) self.search_bar.connect('stop-search', self.do_stop_search) content_grid = Gtk.Box(orientation=Gtk.Orientation.VERTICAL, spacing=0) content_grid.pack_start(self.search_revealer, False, True, 0) content_grid.pack_start(self.scrolled, True, True, 0) content_grid.show_all() self.overlay = Gtk.Overlay() self.overlay.add(content_grid) self.stats_overlay = Granite.WidgetsOverlayBar.new(self.overlay) self.stats_handler = StatsHandler(overlay_bar=self.stats_overlay, buffer=self.buffer) self.stats_handler.update_default_stat() self.add(self.overlay) self.font_desc = Pango.FontDescription() self.spellchecker = GtkSpell.Checker() self.search_settings = GtkSource.SearchSettings(wrap_around=True) self.search_context = GtkSource.SearchContext(buffer=self.buffer, settings=self.search_settings) self.search_iter = None
def __init__(self): GtkSource.Buffer.__init__(self) self.set_highlight_matching_brackets(False) self._search_mark = Gtk.TextMark() self.add_mark(self._search_mark, self.get_start_iter()) self._search_context = GtkSource.SearchContext(buffer=self) self._search_context.set_highlight(False) self._search_settings = self._search_context.get_settings() self._search_settings.set_regex_enabled(True) self._search_settings.set_wrap_around(False) if not self.get_tag_table().lookup(self._invis_tag_name): self.create_tag(self._invis_tag_name, size=0, editable=False)
def __init__(self): super().__init__() self.document = None self.view = Editor() self.scrolled = Gtk.ScrolledWindow(hexpand=True, vexpand=True) self.scrolled.get_style_context().add_class('scrolled-editor') self.scrolled.add(self.view) # SearchBar self.search_bar = SearchBar() self.search_revealer = Gtk.Revealer() self.search_revealer.add(self.search_bar) self.search_bar.connect('find-changed', self.do_next_match) self.search_bar.connect('find-next', self.do_next_match) self.search_bar.connect('find-prev', self.do_previous_match) self.search_bar.connect('stop-search', self.do_stop_search) content_grid = Gtk.Box(orientation=Gtk.Orientation.VERTICAL, spacing=0) content_grid.pack_start(self.search_revealer, False, True, 0) content_grid.pack_start(self.scrolled, True, True, 0) content_grid.show_all() self.overlay = Gtk.Overlay() self.overlay.add(content_grid) self.stats_overlay = Granite.WidgetsOverlayBar.new(self.overlay) self.stats_handler = StatsHandler(overlay_bar=self.stats_overlay, buffer=self.buffer) self.stats_handler.update_default_stat() self.add(self.overlay) self.font_desc = Pango.FontDescription() self.spellchecker = GtkSpell.Checker() self.search_settings = GtkSource.SearchSettings(wrap_around=True) self.search_context = GtkSource.SearchContext( buffer=self.buffer, settings=self.search_settings) self.search_iter = None
def on_highlight_updated(buffer, start, end): global color_tags for tag in color_tags: buffer.remove_tag(tag, buffer.get_start_iter(), buffer.get_end_iter()) color_tags = [] searcher = GtkSource.SearchContext(buffer=buffer, settings=SEARCH_SETTINGS) current_iter = buffer.get_start_iter() while True: found, start, end, _ = searcher.forward2(current_iter) if not found: break current_iter = end while True: tex = buffer.get_text(start, end, True) if tex.count('(') != tex.count(')'): end.forward_char() else: break if end.ends_line(): break tex = buffer.get_text(start, end, True) rgb = try_parse_color(tex) if rgb != None: start2 = start.copy() start2.forward_char() for i in rgb: fg, bg = get_fg_and_bg(*i) new_tag = buffer.create_tag(foreground_rgba=fg, background_rgba=bg, weight=700) buffer.apply_tag(new_tag, start, start2) start.forward_char() start2.forward_char() color_tags.append(new_tag) print('HIGHLIGHT UPDATED')
def on_search_clicked(self, widget): dialog = SearchDialog(self) dialog.set_transient_for(self) dialog.set_modal(True) dialog.show_all() # ==== This is important part for *dialog ===== # Clicking button placed in the particular *dialog makes dialog to # exit the event loop while emitting a response signal if the # Response ID entry in Glade is activated. response = dialog.run() search_settings = GtkSource.SearchSettings() search_settings.set_case_sensitive(dialog.case_sensitive) search_settings.set_regex_enabled(dialog.regex_enabled) search_settings.set_search_text(dialog.searchbox.get_text()) # buffer (GtkSource.Buffer) – a GtkSource.Buffer. # settings (GtkSource.SearchSettings or None) – a GtkSource.SearchSettings, or None. self.search_context = GtkSource.SearchContext(buffer=self.textbuffer, settings=search_settings) if response == Gtk.ResponseType.OK: cursor_mark = self.textbuffer.get_insert() start = self.textbuffer.get_iter_at_mark(cursor_mark) if start.get_offset() == self.textbuffer.get_char_count(): start = self.textbuffer.get_start_iter() self.search_and_mark(start) dialog.destroy()
def __init__(self): super().__init__() self.document = None self.buffer = GtkSource.Buffer() self.manager = GtkSource.LanguageManager() self.language = self.manager.get_language("markdown") self.buffer.set_language(self.language) self.buffer.create_tag('match', background="#66ff00") self.view = GtkSource.View() self.view.set_buffer(self.buffer) self.view.set_wrap_mode(Gtk.WrapMode.WORD) self.view.set_auto_indent(True) self.view.set_smart_home_end(True) self.view.set_insert_spaces_instead_of_tabs(True) self.view.set_tab_width(4) self.view.props.width_request = 800 self.view.set_halign(Gtk.Align.CENTER) # self.view.set_pixels_above_lines(2) # self.view.set_pixels_below_lines(2) # self.view.set_pixels_inside_wrap(4) self.view.set_top_margin(8) self.view.set_left_margin(8) self.view.set_right_margin(8) self.view.set_bottom_margin(8) self.view.set_monospace(True) self.view.get_style_context().add_class('norka-editor') self.view.connect('key-release-event', self.on_key_release_event) # Connect markup handler self.markup_formatter = MarkupFormatter(self.buffer) self.get_style_context().add_class('norka-editor-view') self.connect('insert-bold', self.on_insert_bold) self.connect('insert-italic', self.on_insert_italic) self.connect('insert-code', self.on_insert_code) self.connect('insert-code-block', self.on_insert_code_block) self.connect('insert-h1', self.on_toggle_header1) self.connect('insert-h2', self.on_toggle_header2) self.connect('insert-h3', self.on_toggle_header3) self.connect('insert-list', self.on_toggle_list) self.connect('insert-ordered-list', self.on_toggle_ordered_list) self.connect('insert-quote', self.on_toggle_quote) self.connect('insert-link', self.on_insert_link) self.connect('insert-image', self.on_insert_image) self.scrolled = Gtk.ScrolledWindow(hexpand=True, vexpand=True) self.scrolled.get_style_context().add_class('scrolled-editor') self.scrolled.add(self.view) # SearchBar self.search_bar = SearchBar() self.search_revealer = Gtk.Revealer() self.search_revealer.add(self.search_bar) self.search_bar.connect('find-changed', self.do_next_match) self.search_bar.connect('find-next', self.do_next_match) self.search_bar.connect('find-prev', self.do_previous_match) self.search_bar.connect('stop-search', self.do_stop_search) content_grid = Gtk.Box(orientation=Gtk.Orientation.VERTICAL, spacing=0) content_grid.pack_start(self.search_revealer, False, True, 0) content_grid.pack_start(self.scrolled, True, True, 0) content_grid.show_all() self.overlay = Gtk.Overlay() self.overlay.add(content_grid) self.stats_overlay = Granite.WidgetsOverlayBar.new(self.overlay) self.stats_handler = StatsHandler(overlay_bar=self.stats_overlay, buffer=self.buffer) self.stats_handler.update_default_stat() self.add(self.overlay) self.font_desc = Pango.FontDescription() self.spellchecker = GtkSpell.Checker() self.search_settings = GtkSource.SearchSettings(wrap_around=True) self.search_context = GtkSource.SearchContext( buffer=self.buffer, settings=self.search_settings) self.search_iter = None