[Python-checkins] r65436 - in sandbox/trunk/ttk-gsoc/src/idlelib: EditorWindow.py editorpage.py tabbedpages_new.py tabbedpages_old.py

guilherme.polo python-checkins at python.org
Sun Aug 3 20:48:11 CEST 2008


Author: guilherme.polo
Date: Sun Aug  3 20:48:10 2008
New Revision: 65436

Log:
Added support for closing tabs;
tab name gets updated now;


Modified:
   sandbox/trunk/ttk-gsoc/src/idlelib/EditorWindow.py
   sandbox/trunk/ttk-gsoc/src/idlelib/editorpage.py
   sandbox/trunk/ttk-gsoc/src/idlelib/tabbedpages_new.py
   sandbox/trunk/ttk-gsoc/src/idlelib/tabbedpages_old.py

Modified: sandbox/trunk/ttk-gsoc/src/idlelib/EditorWindow.py
==============================================================================
--- sandbox/trunk/ttk-gsoc/src/idlelib/EditorWindow.py	(original)
+++ sandbox/trunk/ttk-gsoc/src/idlelib/EditorWindow.py	Sun Aug  3 20:48:10 2008
@@ -110,6 +110,7 @@
         self.new_tab(filename=filename)
         self.text = self.current_page.text # XXX
         self.top.focused_widget = self.text
+        self.top.bind('<<tab-closed>>', self._post_tab_close)
         self.text_notebook.pack(fill=BOTH, expand=True)
 
         # The following "width" attribute is used by PyShell, so keep it here
@@ -196,6 +197,9 @@
     def current_page(self):
         """Return the active EditorPage in EditorWindow."""
         curr_tab = self.text_notebook.select()
+        if not curr_tab:
+            return None
+
         if TTK:
             page = self.text_notebook.pages[self.text_notebook.tab(
                 curr_tab)['text']].editpage
@@ -227,8 +231,8 @@
         page = self.text_notebook.add_page(page_title)
 
         vbar = Scrollbar(page.frame, name='vbar')
-        page.editpage = EditorPage(page.frame, self, name='text',
-            padx=5, wrap='none')
+        page.editpage = EditorPage(page.frame, self, title=page_title,
+            name='text', padx=5, wrap='none')
         page.editpage.post_init(filename=filename)
 
         text = page.editpage.text
@@ -257,7 +261,11 @@
 
     def set_line_and_column(self, event=None):
         # Used by PyShell too
-        line, column = self.current_page.text.index(INSERT).split('.')
+        curr_page = self.current_page
+        if not curr_page:
+            return
+
+        line, column = curr_page.text.index(INSERT).split('.')
         self.status_bar.set_label('column', 'Col: %s' % column)
         self.status_bar.set_label('line', 'Ln: %s' % line)
 
@@ -468,31 +476,19 @@
 
     def close(self):
         # XXX need to skip a possible PyShell tab
-        replies = []
         to_check = self.text_notebook.pages.copy()
+
         while to_check:
             curr_tab = self.text_notebook.select()
             if TTK:
                 page_name = self.text_notebook.tab(curr_tab)['text']
             else:
                 page_name = curr_tab
-
             page = to_check.pop(page_name)
             editpage = page.editpage
-            reply = str(editpage.maybesave())
-            replies.append(reply)
-            if reply != "cancel":
-                if editpage.io.filename:
-                    self.update_recent_files_list(new_file=editpage.io.filename)
-                editpage.close()
-                self.text_notebook.remove_page(page_name)
-
-        for reply in replies:
+            reply = editpage.close_tab()
             if reply == "cancel":
                 break
-        else:
-             self._close()
-        return replies
 
     def _close(self):
         WindowList.unregister_callback(self.postwindowsmenu)
@@ -635,11 +631,19 @@
 
     # Private methods
 
+    def _post_tab_close(self, event):
+        if not self.current_page:
+            # no tabs now, close window
+            self._close()
+            return
+
     def _update_controls(self, event):
-        print self.short_title(), "<<"
         curr_page = self.current_page
+        if not curr_page:
+            return
+
         self.text = curr_page.text
-        curr_page.saved_change_hook() # update window title
+        curr_page.saved_change_hook(update_tab_title=False) # update window title
         curr_page.text.focus_set()
         self.set_line_and_column()
 

Modified: sandbox/trunk/ttk-gsoc/src/idlelib/editorpage.py
==============================================================================
--- sandbox/trunk/ttk-gsoc/src/idlelib/editorpage.py	(original)
+++ sandbox/trunk/ttk-gsoc/src/idlelib/editorpage.py	Sun Aug  3 20:48:10 2008
@@ -73,8 +73,9 @@
     return file, filename, descr
 
 class EditorPage(object):
-    def __init__(self, parent_frame, editwin, **kwargs):
+    def __init__(self, parent_frame, editwin, title=None, **kwargs):
         self.editwin = editwin
+        self.title = title
         kwargs.setdefault('width', idleConf.GetOption('main', 'EditorPage',
             'width'))
         kwargs.setdefault('height', idleConf.GetOption('main', 'EditorPage',
@@ -100,6 +101,19 @@
                 self.io.loadfile(filename)
             else:
                 self.io.set_filename(filename)
+        self.saved_change_hook(False)
+
+    def close_tab(self, event=None):
+        """Close current tab, if no more tabs present, close the window."""
+        reply = str(self.maybesave())
+        if reply != "cancel":
+            if self.io.filename:
+                self.editwin.update_recent_files_list(new_file=self.io.filename)
+            self.close()
+            self.editwin.text_notebook.remove_page(self.title)
+            self.editwin.top.event_generate('<<tab-closed>>')
+
+        return reply
 
     def close(self):
         """Perform necessary cleanup for this page before closing it."""
@@ -117,25 +131,32 @@
         self.text = None
 
     # XXX (1) mark where these functions are used
-    def saved_change_hook(self):
+    def saved_change_hook(self, update_window_title=True, update_tab_title=True):
         short = self.editwin.short_title()
         long = self.long_title()
 
         if short and long:
             title = short + " - " + long
+            tabtitle = os.path.split(long)[-1]
         elif short:
             title = short
+            tabtitle = short
         elif long:
             title = long
+            tabtitle = os.path.split(long)[-1]
         else:
-            title = "Untitled"
+            title = tabtitle = "Untitled"
         icon = short or long or title
         if not self.get_saved():
             title = "*%s*" % title
+            tabtitle = "*%s*" % tabtitle
             icon = "*%s" % icon
 
-        self.editwin.top.wm_title(title)
-        self.editwin.top.wm_iconname(icon)
+        if update_tab_title:
+            self.editwin.text_notebook.update_tabtitle(self, tabtitle)
+        if update_window_title:
+            self.editwin.top.wm_title(title)
+            self.editwin.top.wm_iconname(icon)
 
     def get_saved(self):
         return self.undo.get_saved()
@@ -144,11 +165,6 @@
         self.undo.set_saved(flag)
 
     def filename_change_hook(self):
-        try:
-            print self, self.editwin, self.editwin.inversedict
-        except AttributeError: # PyShell
-            pass
-
         if self.editwin.flist:
             self.editwin.flist.filename_changed_edit(self, self.editwin)
         self.saved_change_hook()
@@ -341,7 +357,7 @@
                 method_name = tb[prefix_size:-prefix_size].replace('-', '_')
                 text.bind(tb, getattr(self, prefix % method_name.lower()))
             
-        actions = ('<<close-tab>>', '<<help>>', '<<python-docs>>',
+        actions = ('<<help>>', '<<python-docs>>',
             '<<about-idle>>', '<<open-config-dialog>>', '<<open-module>>',
             '<<cut>>', '<<copy>>', '<<paste>>', '<<select-all>>',
             '<<remove-selection>>', '<<del-word-left>>', '<<del-word-right>>',
@@ -361,6 +377,7 @@
             method_name = action[prefix_size:-prefix_size].replace('-', '_')
             text.bind(action, getattr(self.editwin, method_name))
 
+        text.bind('<<close-tab>>', self.close_tab)
         text.bind('<<newline-and-indent>>', self.newline_and_indent_event)
         text.bind("<<do-nothing>>", lambda event: "break")
         text.bind("<Left>", self._move_at_edge_if_selection(0))
@@ -382,10 +399,6 @@
             # Command-W on editorwindows doesn't work without this.
             text.bind('<<close-window>>', self.editwin.close_event)
 
-    def _close_tab(self, event):
-        """Close current tab, if no more tabs present, close the window."""
-        print "I do nothing right now"
-
     def _help(self, event=None):
         fn = os.path.join(os.path.abspath(os.path.dirname(__file__)),
             'help.txt')

Modified: sandbox/trunk/ttk-gsoc/src/idlelib/tabbedpages_new.py
==============================================================================
--- sandbox/trunk/ttk-gsoc/src/idlelib/tabbedpages_new.py	(original)
+++ sandbox/trunk/ttk-gsoc/src/idlelib/tabbedpages_new.py	Sun Aug  3 20:48:10 2008
@@ -36,6 +36,28 @@
         for name in page_names:
             self.add_page(name)
 
+    def update_tabtitle(self, tab, newtitle):
+        """Update tab title to newtitle."""
+        currpage = self.pages[tab.title].frame
+        old = tab.title
+
+        # resolve title duplicate
+        if newtitle in self.pages and currpage != self.pages[newtitle].frame:
+            # newtitle is already present, and the current tab is not the
+            # one who owns it
+            count = 1
+            temptitle = newtitle
+            while temptitle in self.pages:
+                if currpage == self.pages[temptitle].frame:
+                    break
+                temptitle = "%s #%d" % (newtitle, count)
+                count += 1
+            newtitle = temptitle
+
+        tab.title = newtitle
+        self.pages[newtitle] = self.pages.pop(old)
+        self.tab(currpage, text=newtitle)
+
     def add_page(self, page_name):
         """Add a new page with the name given in page_name."""
         if not page_name:

Modified: sandbox/trunk/ttk-gsoc/src/idlelib/tabbedpages_old.py
==============================================================================
--- sandbox/trunk/ttk-gsoc/src/idlelib/tabbedpages_old.py	(original)
+++ sandbox/trunk/ttk-gsoc/src/idlelib/tabbedpages_old.py	Sun Aug  3 20:48:10 2008
@@ -412,6 +412,34 @@
 
         self.change_page(self._default_page)
 
+    def update_tabtitle(self, tab, newtitle):
+        """Update tab title to newtitle."""
+        currpage = self.pages[tab.title]
+        old = tab.title
+
+        # resolve title duplicate
+        if newtitle in self.pages:
+            count = 1
+            temptitle = newtitle
+            while temptitle in self.pages:
+                temptitle = "%s #%d" % (newtitle, count)
+                count += 1
+            newtitle = temptitle
+
+        tab.title = newtitle
+        # now update 1 million places.. yeh..
+        self.pages[newtitle] = self.pages.pop(old)
+        self._pages_order[self._pages_order.index(old)] = newtitle
+        self._tab_set._tab_names[self._tab_set._tab_names.index(old)] = newtitle
+        self._tab_set._tabs[newtitle] = self._tab_set._tabs.pop(old)
+        self._tab_set._tabs[newtitle].button['text'] = newtitle
+        if self._tab_set._selected_tab == old:
+            self._tab_set._selected_tab = newtitle
+        if self._current_page == old:
+            self._current_page = newtitle
+        if self._default_page == old:
+            self._default_page = newtitle
+
     def add_page(self, page_name):
         """Add a new page with the name given in page_name."""
         if not page_name:
@@ -436,7 +464,6 @@
             raise KeyError("No such TabPage: '%s" % page_name)
 
         self._pages_order.remove(page_name)
-
         # handle removing last remaining, default, or currently shown page
         if len(self._pages_order) > 0:
             if page_name == self._default_page:
@@ -468,6 +495,7 @@
             self.pages[page_name]._show()
 
         self._tab_set.set_selected_tab(page_name)
+        self.event_generate('<<NotebookTabChanged>>') # conform to ttk.Notebook
 
     def last_page(self):
         return self.pages[self._pages_order[-1]]


More information about the Python-checkins mailing list