2009-08-14 18:30:00 +00:00
|
|
|
#!/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
|
|
|
|
|
2009-09-02 19:43:45 +00:00
|
|
|
""" Editable Label class"""
|
2009-08-14 18:30:00 +00:00
|
|
|
import gtk
|
2009-09-02 23:51:46 +00:00
|
|
|
import gobject
|
2009-08-14 18:30:00 +00:00
|
|
|
|
2009-09-02 19:43:45 +00:00
|
|
|
class EditableLabel(gtk.EventBox):
|
2009-09-06 22:52:58 +00:00
|
|
|
# pylint: disable-msg=W0212
|
|
|
|
# pylint: disable-msg=R0904
|
2009-09-02 19:43:45 +00:00
|
|
|
"""
|
2009-08-14 18:30:00 +00:00
|
|
|
An eventbox that partialy emulate a gtk.Label
|
|
|
|
On double-click, the label is editable, entering an empty will revert back to automatic text
|
2009-09-02 19:43:45 +00:00
|
|
|
"""
|
2009-08-14 18:30:00 +00:00
|
|
|
_label = None
|
|
|
|
_ebox = None
|
|
|
|
_autotext = None
|
|
|
|
_custom = None
|
|
|
|
_entry = None
|
2009-09-03 08:03:37 +00:00
|
|
|
_entry_handler_id = None
|
2009-09-02 19:43:45 +00:00
|
|
|
|
2009-09-02 23:51:46 +00:00
|
|
|
__gsignals__ = {
|
|
|
|
'edit-done': (gobject.SIGNAL_RUN_LAST, gobject.TYPE_NONE, ()),
|
|
|
|
}
|
|
|
|
|
2009-08-14 18:30:00 +00:00
|
|
|
def __init__(self, text = ""):
|
2009-09-02 19:43:45 +00:00
|
|
|
""" Class initialiser"""
|
2009-08-14 18:30:00 +00:00
|
|
|
gtk.EventBox.__init__(self)
|
2009-09-02 23:51:46 +00:00
|
|
|
self.__gobject_init__()
|
|
|
|
|
2009-09-03 08:03:37 +00:00
|
|
|
self._entry_handler_id = []
|
2009-08-14 18:30:00 +00:00
|
|
|
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 ):
|
2009-09-02 19:43:45 +00:00
|
|
|
"""set angle of the label"""
|
2009-08-14 18:30:00 +00:00
|
|
|
self._label.set_angle( angle )
|
2010-01-12 01:05:53 +00:00
|
|
|
|
|
|
|
def editing(self):
|
|
|
|
"""Return if we are currently editing"""
|
|
|
|
return(self._entry != None)
|
|
|
|
|
2009-12-10 13:20:03 +00:00
|
|
|
def set_text(self, text, force=False):
|
2009-09-02 19:43:45 +00:00
|
|
|
"""set the text of the label"""
|
2009-08-14 18:30:00 +00:00
|
|
|
self._autotext = text
|
|
|
|
if not self._custom or force:
|
|
|
|
self._label.set_text(text)
|
|
|
|
|
2009-12-10 13:20:03 +00:00
|
|
|
def get_text(self):
|
2009-09-02 19:43:45 +00:00
|
|
|
"""get the text from the label"""
|
2009-09-06 22:52:58 +00:00
|
|
|
return(self._label.get_text())
|
2009-08-14 18:30:00 +00:00
|
|
|
|
|
|
|
def _on_click_text(self, widget, event):
|
2009-09-06 22:52:58 +00:00
|
|
|
# pylint: disable-msg=W0613
|
2009-09-02 19:43:45 +00:00
|
|
|
"""event handling text edition"""
|
2013-01-30 11:27:01 +00:00
|
|
|
if event.button != 1:
|
|
|
|
return False
|
2009-09-02 23:51:46 +00:00
|
|
|
if event.type == gtk.gdk._2BUTTON_PRESS:
|
2009-08-14 18:30:00 +00:00
|
|
|
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)
|
2013-01-30 11:27:01 +00:00
|
|
|
sig = self._entry.connect("button-press-event",
|
|
|
|
self._on_entry_buttonpress)
|
|
|
|
self._entry_handler_id.append(sig)
|
2009-08-14 18:30:00 +00:00
|
|
|
self._entry.grab_focus ()
|
2009-09-06 22:52:58 +00:00
|
|
|
return(True)
|
|
|
|
return(False)
|
2009-08-14 18:30:00 +00:00
|
|
|
|
|
|
|
def _entry_to_label (self, widget, event):
|
2009-09-06 22:52:58 +00:00
|
|
|
# pylint: disable-msg=W0613
|
2009-09-02 19:43:45 +00:00
|
|
|
"""replace gtk.Entry by the gtk.Label"""
|
2009-08-14 18:30:00 +00:00
|
|
|
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 ()
|
2009-09-02 23:51:46 +00:00
|
|
|
self.emit('edit-done')
|
2015-06-02 01:15:30 +00:00
|
|
|
# Returning False will ensure that focus out event will be sent to
|
|
|
|
# GtkEntry so it will disconnect 'state-changed' handler
|
|
|
|
return(False)
|
2009-09-06 22:52:58 +00:00
|
|
|
return(False)
|
2009-08-14 18:30:00 +00:00
|
|
|
|
|
|
|
def _on_entry_activated (self, widget):
|
2009-09-06 22:52:58 +00:00
|
|
|
# pylint: disable-msg=W0613
|
2009-09-02 19:43:45 +00:00
|
|
|
"""get the text entered in gtk.Entry"""
|
2009-08-14 18:30:00 +00:00
|
|
|
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)
|
|
|
|
|
|
|
|
def _on_entry_keypress (self, widget, event):
|
2009-09-06 22:52:58 +00:00
|
|
|
# pylint: disable-msg=W0613
|
2009-09-02 19:43:45 +00:00
|
|
|
"""handle keypressed in gtk.Entry"""
|
2009-08-14 18:30:00 +00:00
|
|
|
key = gtk.gdk.keyval_name (event.keyval)
|
|
|
|
if key == 'Escape':
|
|
|
|
self._entry_to_label (None, None)
|
|
|
|
|
2013-01-30 11:27:01 +00:00
|
|
|
def _on_entry_buttonpress (self, widget, event):
|
|
|
|
"""handle button events in gtk.Entry."""
|
|
|
|
# Block right clicks to avoid a deadlock.
|
|
|
|
# The correct solution here would be for _entry_to_label to trigger a
|
|
|
|
# deferred execution handler and for that handler to check if focus is
|
|
|
|
# in a GtkMenu. The problem being that we are unable to get a context
|
|
|
|
# menu for the GtkEntry.
|
|
|
|
if event.button == 3:
|
|
|
|
return True
|
|
|
|
|
2010-02-11 22:14:07 +00:00
|
|
|
def modify_fg(self, state, color):
|
2010-03-02 20:37:52 +00:00
|
|
|
"""Set the label foreground"""
|
2010-02-11 22:14:07 +00:00
|
|
|
self._label.modify_fg(state, color)
|
|
|
|
|
2010-04-22 22:14:03 +00:00
|
|
|
def is_custom(self):
|
|
|
|
"""Return whether or not we have a custom string set"""
|
|
|
|
return(self._custom)
|
|
|
|
|
|
|
|
def set_custom(self):
|
|
|
|
"""Set the customness of the string to True"""
|
|
|
|
self._custom = True
|
2015-07-10 19:36:33 +00:00
|
|
|
|
|
|
|
def modify_font(self, fontdesc):
|
|
|
|
"""Set the label font using a pango.FontDescription"""
|
|
|
|
self._label.modify_font(fontdesc)
|
|
|
|
|
2009-09-02 23:51:46 +00:00
|
|
|
gobject.type_register(EditableLabel)
|