merge chantra's editable label class

This commit is contained in:
Chris Jones 2009-08-14 21:39:08 +01:00
commit 5093f4b3d9
3 changed files with 131 additions and 57 deletions

View File

@ -23,7 +23,6 @@ pygtk.require ("2.0")
import gobject, gtk, pango import gobject, gtk, pango
from terminatorlib.version import APP_NAME, APP_VERSION from terminatorlib.version import APP_NAME, APP_VERSION
from terminatorlib import config from terminatorlib import config
from config import dbg, err, debug from config import dbg, err, debug
@ -32,6 +31,8 @@ from terminatorlib.terminatorterm import TerminatorTerm
from terminatorlib.prefs_profile import ProfileEditor from terminatorlib.prefs_profile import ProfileEditor
from terminatorlib import translation from terminatorlib import translation
from terminatorlib.terminatoreditablelabel import TerminatorEditableLabel
try: try:
import deskbar.core.keybinder as bindkey import deskbar.core.keybinder as bindkey
except: except:
@ -72,32 +73,24 @@ class TerminatorWindowTitle:
class TerminatorNotebookTabLabel(gtk.HBox): class TerminatorNotebookTabLabel(gtk.HBox):
_terminator = None _terminator = None
_notebook = None _notebook = None
_label = None
_icon = None _icon = None
_label = None
_button = None _button = None
_ebox = None
_autotitle = None
custom = None
def __init__(self, title, notebook, terminator): def __init__(self, title, notebook, terminator):
gtk.HBox.__init__(self, False) gtk.HBox.__init__(self, False)
self._notebook = notebook self._notebook = notebook
self._terminator = terminator self._terminator = terminator
self.custom = False
self._label = gtk.Label(title) self._label = TerminatorEditableLabel(title)
self.update_angle() self.update_angle()
self._ebox = gtk.EventBox () self.pack_start(self._label, True, True)
self._ebox.set_visible_window (False)
self._ebox.add (self._label)
self.pack_start(self._ebox, True, True)
self._icon = gtk.Image() self._icon = gtk.Image()
self._icon.set_from_stock(gtk.STOCK_CLOSE, gtk.ICON_SIZE_MENU) self._icon.set_from_stock(gtk.STOCK_CLOSE, gtk.ICON_SIZE_MENU)
self.update_closebut() self.update_closebut()
self._ebox.connect ("button-press-event", self.on_click_title)
self.show_all() self.show_all()
@ -154,9 +147,7 @@ class TerminatorNotebookTabLabel(gtk.HBox):
break break
def set_title(self, title, force=False): def set_title(self, title, force=False):
self._autotitle = title self._label.set_text(title, force)
if not self.custom or force:
self._label.set_text(title)
def get_title(self): def get_title(self):
return self._label.get_text() return self._label.get_text()
@ -167,43 +158,6 @@ class TerminatorNotebookTabLabel(gtk.HBox):
def width_request(self): def width_request(self):
return self.size_request()[0] return self.size_request()[0]
def on_click_title(self, widget, event):
if event.type == gtk.gdk._2BUTTON_PRESS and self._ebox in self.get_children ():
self.remove (self._ebox)
self._entry = gtk.Entry ()
self._entry.set_text (self._label.get_text ())
self._entry.show ()
self.pack_start (self._entry)
self.reorder_child (self._entry, 0)
self._notebook.connect ("switch-page", self.entry_to_label)
self._entry.connect ("activate", self.on_entry_activated)
self._entry.connect ("key-press-event", self.on_entry_keypress)
self._entry.grab_focus ()
def entry_to_label (self, widget, page, page_num):
if (self._entry):
self.remove (self._entry)
self.add (self._ebox)
self._entry = None
self.reorder_child (self._ebox, 0)
self._ebox.show_all ()
def on_entry_activated (self, widget):
entry = self._entry.get_text ()
label = self._label.get_text ()
if entry == '':
self.custom = False
self.set_title (self._autotitle)
elif entry != label:
self.custom = True
self.set_title (self._entry.get_text (), True)
self.entry_to_label (None, None, None)
def on_entry_keypress (self, widget, event):
key = gtk.gdk.keyval_name (event.keyval)
if key == 'Escape':
self.entry_to_label (None, None, None)
class Terminator: class Terminator:
options = None options = None

View File

@ -0,0 +1,120 @@
#!/usr/bin/env python
# vim: tabstop=4 softtabstop=4 shiftwidth=4 expandtab
#
# Copyright (c) 2009, Emmanuel Bretelle <chantra@debuntu.org>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, version 2 only.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor
# , Boston, MA 02110-1301 USA
# pylint: disable-msg=W0212
''' Editable Label class'''
import gtk
class TerminatorEditableLabel( gtk.EventBox ):
'''
An eventbox that partialy emulate a gtk.Label
On double-click, the label is editable, entering an empty will revert back to automatic text
'''
_label = None
_ebox = None
_autotext = None
_custom = None
_entry = None
_entry_handler_id = []
def __init__(self, text = ""):
''' Class initialiser'''
gtk.EventBox.__init__(self)
self._label = gtk.Label(text)
self._custom = False
self.set_visible_window (False)
self.add (self._label)
self.connect ("button-press-event", self._on_click_text)
def set_angle(self, angle ):
'''set angle of the label'''
self._label.set_angle( angle )
def set_text( self, text, force=False):
'''set the text of the label'''
self._autotext = text
if not self._custom or force:
self._label.set_text(text)
def get_text( self ):
'''get the text from the label'''
return self._label.get_text()
def _on_click_text(self, widget, event):
'''event handling text edition'''
if event.type == gtk.gdk._2BUTTON_PRESS :
self.remove (self._label)
self._entry = gtk.Entry ()
self._entry.set_text (self._label.get_text ())
self._entry.show ()
self.add (self._entry)
sig = self._entry.connect ("focus-out-event", self._entry_to_label)
self._entry_handler_id.append(sig)
sig = self._entry.connect ("activate", self._on_entry_activated)
self._entry_handler_id.append(sig)
sig = self._entry.connect ("key-press-event",
self._on_entry_keypress)
self._entry_handler_id.append(sig)
self._entry.grab_focus ()
return True
# make pylint happy
if 1 or widget or event:
return False
def _entry_to_label (self, widget, event):
'''replace gtk.Entry by the gtk.Label'''
if self._entry and self._entry in self.get_children():
#disconnect signals to avoid segfault :s
for sig in self._entry_handler_id:
if self._entry.handler_is_connected(sig):
self._entry.disconnect(sig)
self._entry_handler_id = []
self.remove (self._entry)
self.add (self._label)
self._entry = None
self.show_all ()
return True
#make pylint happy
if 1 or widget or event:
return False
def _on_entry_activated (self, widget):
'''get the text entered in gtk.Entry'''
entry = self._entry.get_text ()
label = self._label.get_text ()
if entry == '':
self._custom = False
self.set_text (self._autotext)
elif entry != label:
self._custom = True
self._label.set_text (entry)
self._entry_to_label (None, None)
# make pylint happy
if 1 or widget:
return
def _on_entry_keypress (self, widget, event):
'''handle keypressed in gtk.Entry'''
key = gtk.gdk.keyval_name (event.keyval)
if key == 'Escape':
self._entry_to_label (None, None)
# make pylint happy
if 1 or widget or event:
return

View File

@ -30,7 +30,7 @@ from terminatorlib.config import dbg, err, debug
#import encoding list #import encoding list
from terminatorlib.encoding import TerminatorEncoding from terminatorlib.encoding import TerminatorEncoding
from terminatorlib.terminatoreditablelabel import TerminatorEditableLabel
# import translation support # import translation support
from terminatorlib import translation from terminatorlib import translation
@ -62,7 +62,7 @@ class TerminatorTermTitle (gtk.EventBox):
def __init__ (self, terminal, terminator, configwanted = False): def __init__ (self, terminal, terminator, configwanted = False):
gtk.EventBox.__init__ (self) gtk.EventBox.__init__ (self)
self._title = gtk.Label () self._title = TerminatorEditableLabel()
self._group = gtk.Label () self._group = gtk.Label ()
self._separator = gtk.VSeparator () self._separator = gtk.VSeparator ()
self._ebox = gtk.EventBox () self._ebox = gtk.EventBox ()
@ -92,12 +92,12 @@ class TerminatorTermTitle (gtk.EventBox):
self._hbox.pack_start (self._title, True, True) self._hbox.pack_start (self._title, True, True)
self.add (self._hbox) self.add (self._hbox)
self._title.show () self._title.show_all ()
self._hbox.show () self._hbox.show ()
self.wanted = configwanted self.wanted = configwanted
self.connect ("button-release-event", self.on_clicked) self.connect ("button-press-event", self.on_clicked)
def connect_icon (self, func): def connect_icon (self, func):
self._ebox.connect ("button-release-event", func) self._ebox.connect ("button-release-event", func)
@ -105,7 +105,7 @@ class TerminatorTermTitle (gtk.EventBox):
def on_clicked (self, widget, event): def on_clicked (self, widget, event):
if self._parent is not None: if self._parent is not None:
self._parent._vte.grab_focus () self._parent._vte.grab_focus ()
def set_group_label (self, name): def set_group_label (self, name):
"""If 'name' is None, hide the group name object, otherwise set it as the group label""" """If 'name' is None, hide the group name object, otherwise set it as the group label"""
if name: if name: