def __init__(self, script_buffer): gtk.Window.__init__(self) self.set_title(_('Macro preview')) self.set_default_size(480, 720) self.set_border_width(6) self.connect('delete-event', self._onDelete) self.script_buffer = script_buffer text_view = gtksourceview.SourceView(self.script_buffer) text_view.set_editable(True) text_view.set_cursor_visible(True) text_view.modify_font(pango.FontDescription('Mono')) sw = gtk.ScrolledWindow() sw.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC) sw.set_shadow_type(gtk.SHADOW_IN) sw.add(text_view) vbox = gtk.VBox() vbox.set_spacing(3) vbox.pack_start(sw) bbox = gtk.HButtonBox() bbox.set_layout(gtk.BUTTONBOX_START) for label, callback in [(gtk.STOCK_SAVE_AS, self._onSave), (gtk.STOCK_CLEAR, self._onClear), (gtk.STOCK_MEDIA_RECORD, self._onRecord), (gtk.STOCK_MEDIA_PLAY, self._onPlay)]: button = gtk.Button(label) button.set_use_stock(True) button.connect('clicked', callback) bbox.pack_start(button) if label == gtk.STOCK_MEDIA_RECORD: self.script_buffer.connect('notify::recording', self._onRecordChange, button) vbox.pack_start(bbox, False) self.progress_bar = gtk.ProgressBar() vbox.pack_start(self.progress_bar, False) self.add(vbox)
def __init__(self, filename, mimetype=None): self.filename = filename if SV2: self.buffer = gtksourceview2.Buffer() self.buffer.set_highlight_syntax(True) else: self.buffer = gtksourceview.SourceBuffer() self.buffer.set_highlight(True) self.buffer.set_max_undo_levels(1000) if SV2: self.view = gtksourceview2.View(self.buffer) else: self.view = gtksourceview.SourceView(self.buffer) self.view.set_auto_indent(True) self.view.modify_font(pango.FontDescription('Ubuntu Mono 12')) self.view.connect('key_press_event', self.on_key_press_event) self.touch() language = self.get_language(mimetype) if language: self.buffer.set_language(language) self.file_signature = None self.file_text = '' self.sync_from_disk()
def __init__(self, filename): self.filename = filename if SV2: self.buffer = gtksourceview2.Buffer() self.buffer.set_highlight_syntax(True) else: self.buffer = gtksourceview.SourceBuffer() self.buffer.set_highlight(True) self.buffer.set_max_undo_levels(1000) if SV2: self.view = gtksourceview2.View(self.buffer) else: self.view = gtksourceview.SourceView(self.buffer) self.view.set_auto_indent(True) self.view.modify_font(pango.FontDescription('monospace')) mimetype = mimetypes.guess_type(filename)[0] if mimetype: self.buffer.set_language(get_language_from_mime_type(mimetype)) if not os.path.exists(filename): open(filename, 'wb').close() self.file_signature = None self.file_text = '' self.sync_from_disk()
def new_cb(self, b): untitled_number = self.get_untitled_number() scrolled_window = gtk.ScrolledWindow() scrolled_window.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC) if sys.version_info[2] > 0: import gtksourceview sourceview = gtksourceview.SourceView() sourceview.set_show_line_numbers(True) sourceview.set_auto_indent(True) # sourceview.set_highlight_current_line (True) else: sourceview = gtk.TextView() txtbuf = sourceview.get_buffer() txtbuf.connect('changed', self.on_modified) txtbuf.create_tag("python_keyword", foreground="#a52a2a") txtbuf.create_tag("python_string", foreground="#ff00ff") txtbuf.create_tag("python_comment", foreground="#0000ff") txtbuf.create_tag("python_builtin", foreground="#2e8b57") txtbuf.create_tag("python_function", foreground="#1db8ca") scrolled_window.add(sourceview) sourceview.set_editable(True) # Append the scrolled window as a notebook page frame = gtk.Frame() frame.add(scrolled_window) # Construct label for a notebook page hbox = gtk.HBox() default_label = 'Untitled ' + str(untitled_number) label = gtk.Label(default_label) hbox.pack_start(label, False) image = gtk.image_new_from_stock(gtk.STOCK_CLOSE, gtk.ICON_SIZE_MENU) button = gtk.Button() button.add(image) button.set_relief(gtk.RELIEF_NONE) button.set_size_request(18, 18) button.connect('clicked', self.close_cb) hbox.pack_end(button, False) self.notebook.append_page(frame, hbox) self.add_file_name(self.num_pages, default_label) self.window.set_title(default_label + '-LDTP Editor') self.num_pages = self.num_pages + 1 hbox.show_all() self.window.show_all() self.notebook.set_current_page(self.num_pages - 1) sourceview.grab_focus()
def __init__(self, widget=None, value=""): if HAS_GTKSOURCEVIEW: if widget is None: self.view = gtksourceview.SourceView(self.buffer) else: self.view = widget self.buffer = gtksourceview.SourceBuffer() self.buffer.set_text(value) if HAS_GTKSPELL: gtkspell.Spell(self.view) self.view.show() else: TextView.__init__(self, widget, value)
def _init_gtksourceview1(self): try: import gtksourceview srcbuf = gtksourceview.SourceBuffer() langman = gtksourceview.SourceLanguagesManager() lang = langman.get_language_from_mime_type("text/x-csrc") srcbuf.set_language(lang) srcbuf.set_highlight(True) srcview = gtksourceview.SourceView(srcbuf) srcview.set_show_line_numbers(True) return (1, srcview, srcbuf) except: L.warning('Cannot load gtksourceview 1') return None
class Editor: """Wraps a gtrksourceview widget and adds a few abstraction methods.""" def __init__(self,parent,title=''): self.parent = parent self.logger = logging.getLogger("gvr.Editors.Editor") self.logger.debug("Using gtksourceview version %s" % SRCVIEW) # remove any children from previous sessions for child in self.parent.get_children(): self.parent.remove(child) # Look for the locale to which the syntax highlighting should be set # We assume the locale is available, if not there won't be any higlighting. try: loc = utils.get_locale()[:2] except Exception,info: self.logger.exception("Error in checking locale") loc = '' if loc: mime = 'gvr_%s' % loc else: mime = 'gvr_en' if SRCVIEW == 1: srctagtable = gtksourceview.SourceTagTable() self.srcbuffer = gtksourceview.SourceBuffer(table=srctagtable) man = gtksourceview.SourceLanguagesManager() lang = man.get_language_from_mime_type('text/x-'+mime) self.logger.debug("gtksourceview buffer syntax higlight set to %s" % mime) self.srcbuffer.set_language(lang) self.srcbuffer.set_highlight(True) self.srcview = gtksourceview.SourceView(buffer=self.srcbuffer) self.srcview.set_tabs_width(4) else: self.srcbuffer = gtksourceview2.Buffer() self.srcview = gtksourceview2.View(buffer=self.srcbuffer) man = gtksourceview2.LanguageManager() self.logger.debug("set search path to %s" % utils.GTKSOURCEVIEWPATH) man.set_search_path([utils.GTKSOURCEVIEWPATH]) #man.set_search_path(["/tmp/language-specs"]) #print dir(man) # Horrible hacks, gtksourceview2 on XO differs from Ubuntu :-( # Reminder, if XO changes their gtksourceview2 again it will probably # break GvRng here. # And of course the 767 version changes gtksourceview2 again :-( # You should never change existing programs and leave the original name unchanged. # I commented out the XO bit in case we need it when there's another change. ## if utils.platform == 'XO': ## langs = man.list_languages() ## self.srcbuffer.set_highlight(True) ## for lang in langs: ## for m in lang.get_mime_types(): ## if m == 'text/x-'+mime: ## self.logger.debug("XO gtksourceview buffer syntax higlight set to %s" % lang) ## self.srcbuffer.set_language(lang) ## else: langs = man.get_language_ids() self.srcbuffer.set_highlight_syntax(True) self.logger.debug("Found language files:%s" % langs) for id in langs: if id == mime: self.logger.debug("gtksourceview buffer syntax higlight set to %s" % mime) self.srcbuffer.set_language(man.get_language(id)) break self.srcview.set_tab_width(4) # some methods that are the same on version 1 and 2 self.tag_h = self.srcbuffer.create_tag(background='lightblue') self.srcbuffer.set_max_undo_levels(10) self.srcview.set_show_line_numbers(True) self.srcview.set_insert_spaces_instead_of_tabs(True) #self.srcview.set_wrap_mode(gtk.WRAP_CHAR) self.parent.add(self.srcview) self.parent.show_all() self.old_start_iter = None
def __init__(self, buffer=None): menu_items = [ (_("/_File"), None, None, 0, "<Branch>"), (_("/File/_New"), "<control>N", self.do_new, 0, None), (_("/File/_Open"), "<control>O", self.do_open, 0, None), (_("/File/_Save"), "<control>S", self.do_save, 0, None), (_("/File/Save _As..."), None, self.do_save_as, 0, None), (_("/File/sep1"), None, None, 0, "<Separator>"), (_("/File/_Close"), "<control>W", self.do_close, 0, None), (_("/File/E_xit"), "<control>Q", self.do_exit, 0, None), (_("/_Edit"), None, None, 0, "<Branch>"), (_("/Edit/Undo"), "<control>Z", self.do_undo, 0, None), (_("/Edit/Redo"), "<control><shift>Z", self.do_redo, 0, None), (_("/Edit/sep1"), None, None, 0, "<Separator>"), (_("/Edit/Find..."), "<control>F", self.do_search, 0, None), (_("/_Settings"), None, None, 0, "<Branch>"), (_("/Settings/Wrap _Off"), None, self.do_wrap_changed, gtk.WRAP_NONE, "<RadioItem>"), (_("/Settings/Wrap _Words"), None, self.do_wrap_changed, gtk.WRAP_WORD, _("/Settings/Wrap Off")), (_("/Settings/Wrap _Chars"), None, self.do_wrap_changed, gtk.WRAP_CHAR, _("/Settings/Wrap Off")), (_("/_Run"), None, None, 0, "<Branch>"), (_("/Run/_Run script"), "<control>R", self.run_script, False, None), (_("/Run/sep1"), None, None, 0, "<Separator>"), (_("/Run/Run socket server"), None, self.do_socketserver_changed, True, "<ToggleItem>"), (_("/Run/Show variables window"), None, self.do_varwindow_changed, True, "<ToggleItem>"), (_("/Run/Run fullscreen"), None, self.do_fullscreen_changed, True, "<ToggleItem>"), (_("/_Help"), None, None, 0, "<Branch>"), (_("/Help/_About"), None, self.do_about, 0, None) ] if not buffer: buffer = Buffer() gtk.Window.__init__(self) TestText.views.push(self) buffer.ref() if not TestText.colormap: TestText.colormap = self.get_colormap() self.connect("delete_event", self.delete_event_cb) self.accel_group = gtk.AccelGroup() self.item_factory = gtk.ItemFactory(gtk.MenuBar, "<main>", self.accel_group) self.item_factory.set_data("view", self) self.item_factory.create_items(menu_items) self.add_accel_group(self.accel_group) hpaned = gtk.HPaned() vbox = gtk.VBox(False, 0) hpaned.add1(vbox) self.add(hpaned) vbox.pack_start(self.item_factory.get_widget("<main>"), False, False, 0) sw = gtk.ScrolledWindow() sw.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC) self.text_view = gtksourceview.SourceView(buffer) self.text_view.set_wrap_mode(gtk.WRAP_WORD) self.text_view.set_show_line_numbers(True) self.text_view.set_auto_indent(True) self.text_view.set_insert_spaces_instead_of_tabs(True) # FIXME: the following 2 lines don't have any effect :/ self.text_view.tab_width = 4 self.text_view.indent_width = 4 self.text_view.connect("expose_event", self.tab_stops_expose) self.bhid = buffer.connect("mark_set", self.cursor_set_callback) if View.FONT is None: # Get font or fallback context = self.text_view.get_pango_context() fonts = context.list_families() for font in fonts: if font.get_name() == 'Bitstream Vera Sans Mono': View.FONT = 'Bitstream Vera Sans Mono 8' break else: print 'Bitstream Vera Font not found.' print 'Download and install it from here' print 'http://ftp.gnome.org/pub/GNOME/sources/ttf-bitstream-vera/1.10/' View.FONT = 'Mono 8' self.text_view.modify_font(pango.FontDescription(View.FONT)) vbox.pack_start(sw, True, True, 0) sw.add(self.text_view) # this creates a console error and output window besides script window self.console_error = ConsoleWindow() # we create an instance for stdout filter self.stdout_filter = Stdout_Filter(self.console_error) # we redirect stderr sys.stderr = self.console_error # stdout is redirected too, but through the filter in order to get different color for text sys.stdout = self.stdout_filter # error-console window is added to container as second child hpaned.add2(self.console_error.text_window) hpaned.set_position(450) # message displayed in console-error window at start, the double true values passed makes it render with system message tag self.console_error.write( _("This is a console window, error messages and script output are shown here,\n you can clear the window selecting and deleting the content\n\n" ), True, True) self.set_default_size(800, 500) self.text_view.grab_focus() self.set_view_title() self.init_menus() # options toggle self.use_varwindow = False self.use_socketserver = False self.go_fullscreen = False # setup syntax highlighting manager = gtksourceview.SourceLanguagesManager() language = manager.get_language_from_mime_type("text/x-python") buffer.set_language(language) buffer.set_highlight(True) self.shoebot_window = None try: self.set_icon_from_file(ICON_FILE) except gobject.GError: # icon not found = no icon pass self.show_all()
def create_view_window(buffer, sourceview=None): # window window = gtk.Window(gtk.WINDOW_TOPLEVEL) window.set_border_width(0) window.set_title('PyGtkSourceView Demo') windows.append(window) # this list contains all view windows # view view = gtksourceview.SourceView(buffer) buffer.connect('mark_set', move_cursor_cb, view) buffer.connect('changed', update_cursor_position, view) view.connect('button-press-event', button_press_cb) window.connect('delete-event', window_deleted_cb, view) # action group and UI manager action_group = gtk.ActionGroup('ViewActions') action_group.add_actions(view_actions, view) action_group.add_toggle_actions(toggle_actions, view) action_group.add_radio_actions(radio_actions, -1, tabs_toggled_cb, view) ui_manager = gtk.UIManager() ui_manager.insert_action_group(action_group, 0) # save a reference to the ui manager in the window for later use window.set_data('ui_manager', ui_manager) accel_group = ui_manager.get_accel_group() window.add_accel_group(accel_group) ui_manager.add_ui_from_string(view_ui_description) # misc widgets vbox = gtk.VBox(0, False) sw = gtk.ScrolledWindow() sw.set_shadow_type(gtk.SHADOW_IN) pos_label = gtk.Label('Position') view.set_data('pos_label', pos_label) menu = ui_manager.get_widget('/MainMenu') # layout widgets window.add(vbox) vbox.pack_start(menu, False, False, 0) vbox.pack_start(sw, True, True, 0) sw.add(view) vbox.pack_start(pos_label, False, False, 0) # setup view font_desc = pango.FontDescription('monospace 10') if font_desc: view.modify_font(font_desc) # change view attributes to match those of sourceview if sourceview: action = action_group.get_action('ShowNumbers') action.set_active(sourceview.get_show_line_numbers()) action = action_group.get_action('ShowMarkers') action.set_active(sourceview.get_show_line_markers()) action = action_group.get_action('ShowMargin') action.set_active(sourceview.get_margin()) action = action_group.get_action('AutoIndent') action.set_active(sourceview.get_auto_indent()) action = action_group.get_action('InsertSpaces') action.set_active(sourceview.get_insert_spaces_instead_of_tabs()) action = action_group.get_action('TabsWidth%d' % sourceview.get_tabs_width()) if action: action.set_active(True) # add marker pixbufs pixbuf = gtk.gdk.pixbuf_new_from_file( os.path.join(DATADIR, 'pixmaps/apple-green.png')) if pixbuf: view.set_marker_pixbuf(MARKER_TYPE_1, pixbuf) else: print 'could not load marker 1 image. Spurious messages might get triggered' pixbuf = gtk.gdk.pixbuf_new_from_file( os.path.join(DATADIR, 'pixmaps/apple-red.png')) if pixbuf: view.set_marker_pixbuf(MARKER_TYPE_2, pixbuf) else: print 'could not load marker 2 image. Spurious messages might get triggered' vbox.show_all() return window