def test_get_recent_files(): handle, name = tempfile.mkstemp(text=True) try: fle = open(name, 'w') fle.write('\n'.join(LINES)) fle.close() filenames = [] for r in get_recent_files(name): filename, scroll_pos, cursor_pos = r filenames.append(filename) assert len(filenames) == len(LINES)-1 # there should be one error finally: os.unlink(name)
def __init__(self): super(GTKEditor, self).__init__() # settings self.settings = load_gtk_settings() self.window = gtk.Window(gtk.WINDOW_TOPLEVEL) self.window.connect("delete_event", self.on_window_delete_event) self.window.connect("destroy", self.on_window_destroy) self.window.connect("configure_event", self.on_window_configure) width = self.settings.win_width height = self.settings.win_height self.window.set_default_size(width, height) x = self.settings.win_x y = self.settings.win_y self.window.move(x, y) self.vbox = gtk.VBox() self.window.add(self.vbox) # Actions and action groups: actions = get_actions(self) self.global_accelgroup = gtk.AccelGroup() self.window.add_accel_group(self.global_accelgroup) self.selection_accelgroup = gtk.AccelGroup() self.window.add_accel_group(self.selection_accelgroup) self.global_actiongroup = gtk.ActionGroup('global') self.selection_actiongroup = gtk.ActionGroup('selection') # global actions self.global_actiongroup.add_actions(actions['actions']) self.global_actiongroup.add_toggle_actions(actions['toggle_actions']) linesep_value = linesep_map[self.settings.linesep] edit_ws_action = self.global_actiongroup.get_action('EditWorkspace') edit_ws_action.set_sensitive(False) clear_wsc_action = self.global_actiongroup.get_action('ClearWorkspaceCache') clear_wsc_action.set_sensitive(False) for action in self.global_actiongroup.list_actions(): action.set_accel_group(self.global_accelgroup) action.connect_accelerator() if action.get_name() == 'Undo': self.undo_action = action if action.get_name() == 'Redo': self.redo_action = action self.global_actiongroup.set_sensitive(True) self.global_actiongroup.set_visible(True) self.undo_action.set_sensitive(False) self.redo_action.set_sensitive(False) # things you can do with selections self.selection_actiongroup.add_actions(actions['selection_actions']) for action in self.selection_actiongroup.list_actions(): action.set_accel_group(self.selection_accelgroup) action.connect_accelerator() self.selection_actiongroup.set_sensitive(False) self.selection_actiongroup.set_visible(True) # uimanager, menu uimanager = gtk.UIManager() uimanager.insert_action_group(self.global_actiongroup, 0) uimanager.insert_action_group(self.selection_actiongroup, -1) uimanager.add_ui_from_string(get_menu_xml()) uimanager.ensure_update() # is this really necessary? toplevels = uimanager.get_toplevels(gtk.UI_MANAGER_MENUBAR) self.menubar = toplevels[0] self.vbox.pack_start(self.menubar, expand=False) self.uimanager = uimanager # workspaces self.workspace_merge_id = None self.workspaces_actiongroup = gtk.ActionGroup('workspaces') self.uimanager.insert_action_group(self.workspaces_actiongroup, -1) #self._sync_workspaces_menu() self.reload_workspaces() # colourschemes self.colourschemes = self.settings.load_colourschemes() # sane default that will be overridden in a moment: self.colourscheme = self.colourschemes[0] self.set_colourscheme(self.settings.colourscheme) uimanager.ensure_update() # statusbar self.status_hbox = gtk.HBox(spacing=0) self.vbox.pack_end(self.status_hbox, expand=False) self.status_message = gtk.Label() self.status_modified = gtk.Label() self.status_position = gtk.Label() self.status_lexer = gtk.Label() self.status_linesep = gtk.Label() self.status_encoding = gtk.Label() self.status_message.set_alignment(0, 0) self.status_modified.set_alignment(0, 0) self.status_position.set_alignment(0, 0) self.status_lexer.set_alignment(1, 0) self.status_position.set_size_request(100, -1) self.status_lexer.set_size_request(150, -1) self.status_message.set_padding(3, 3) self.status_modified.set_padding(3, 3) self.status_position.set_padding(3, 3) self.status_lexer.set_padding(3, 3) self.status_linesep.set_padding(3, 3) self.status_encoding.set_padding(3, 3) self.status_hbox.pack_start(self.status_message, expand=True) self.status_hbox.pack_start(self.status_modified, expand=False) self.status_hbox.pack_start(self.status_position, expand=False) self.status_hbox.pack_start(self.status_lexer, expand=False) self.status_hbox.pack_start(self.status_linesep, expand=False) self.status_hbox.pack_start(self.status_encoding, expand=False) self.vpaned = gtk.VPaned() self.hpaned = gtk.HPaned() self.vbox.pack_end(self.hpaned, expand=True) self.document_tree_model = gtk.TreeStore(str, str, bool) self.document_tree = gtk.TreeView(self.document_tree_model) self.document_tree.unset_flags(gtk.CAN_FOCUS) column_filename = gtk.TreeViewColumn('Documents') self.document_tree.append_column(column_filename) cell_filename = gtk.CellRendererText() column_filename.pack_end(cell_filename) column_filename.add_attribute(cell_filename, 'text', 0) def cb(column, cell_renderer, tree_model, iterrow): doc = self.textarea.view.document filepath = tree_model.get_value(iterrow, 1) if filepath in (doc.location, doc.description): cell_renderer.set_property('weight', pango.WEIGHT_BOLD) else: cell_renderer.set_property('weight', pango.WEIGHT_NORMAL) column_filename.set_cell_data_func(cell_filename, cb) self.column_filename = column_filename self.scrolled_window = gtk.ScrolledWindow() self.scrolled_window.add(self.document_tree) self.scrolled_window.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC) self.hpaned.pack1(self.scrolled_window, resize=False, shrink=False) # textarea self.textarea = GTKTextarea(self) # replace with something to load the session recent_files_path = self.settings.get_recent_files_path() if os.path.exists(recent_files_path): for r in get_recent_files(recent_files_path): path, scroll_pos, cursor_pos = r if os.path.exists(path) and os.path.isfile(path): view = self.new_view(path) view.cursor_pos = cursor_pos view.scroll_pos = scroll_pos view.last_x_pos = cursor_pos[1] self.switch_current_view(view) # to trigger scroll # if the most recent file is set in settings, try and switch to it. # (it will have to be set in the recent files list to be loaded) if self.settings.most_recent_file: for view in self.views: if view.document.location == self.settings.most_recent_file: self.switch_current_view(view) break if not self.views: self.new_view() def add(widget): #self.hpaned.pack2(widget, resize=True, shrink=False) self.vpaned.pack1(widget, resize=False, shrink=True) self.textarea.attach(add) self.search_notebook = gtk.Notebook() self.search_notebook.set_property('tab-vborder', 0) self.vpaned.pack2(self.search_notebook, resize=True, shrink=True) self.search_notebook.connect("switch-page", self.on_search_notebook_switch_page) self.hpaned.pack2(self.vpaned, resize=True, shrink=False) self.window.show_all() self.textarea.show() # hack because some stuff can't be initialised # earlier self.search_notebook.hide() self.clipboard = gtk.clipboard_get(gtk.gdk.SELECTION_CLIPBOARD) self.scrolled_window_visible = True self.document_tree.set_size_request(200, -1) # ?? self.document_tree.connect("cursor-changed", self.on_document_tree_cursor_changed) self.document_tree.expand_all() self._select_current_view_in_tree() if not self.settings.show_statusbar: self.status_hbox.hide() if not self.settings.show_sidebar: self.scrolled_window_visible = False self.scrolled_window.hide() self.open_dialog = GtkOpenDialog(self) self.save_dialog = GtkSaveDialog(self) self.goto_dialog = GtkGotoLineDialog(self) self.switch_dialog = GtkSwitchDocumentDialog(self) self.add_workspace_dialog = GtkAddWorkspaceDialog(self) self.edit_workspace_dialog = GtkEditWorkspaceDialog(self) self.preferences_dialog = GtkPreferencesDialog(self) self.find_dialog = GtkFindDialog(self) # pre-select the workspace for w in self.workspaces: if w.name == self.settings.workspace: self.switch_workspace(w) break