2
# vim: tabstop=4 softtabstop=4 shiftwidth=4 expandtab
4
# Copyright (c) 2009, Emmanuel Bretelle <chantra@debuntu.org>
6
# This program is free software; you can redistribute it and/or modify
7
# it under the terms of the GNU General Public License as published by
8
# the Free Software Foundation, version 2 only.
10
# This program is distributed in the hope that it will be useful,
11
# but WITHOUT ANY WARRANTY; without even the implied warranty of
12
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
# GNU General Public License for more details.
15
# You should have received a copy of the GNU General Public License
16
# along with this program; if not, write to the Free Software
17
# Foundation, Inc., 51 Franklin Street, Fifth Floor
18
# , Boston, MA 02110-1301 USA
20
""" Editable Label class"""
24
class EditableLabel(gtk.EventBox):
25
# pylint: disable-msg=W0212
26
# pylint: disable-msg=R0904
28
An eventbox that partialy emulate a gtk.Label
29
On double-click, the label is editable, entering an empty will revert back to automatic text
36
_entry_handler_id = None
39
'edit-done': (gobject.SIGNAL_RUN_LAST, gobject.TYPE_NONE, ()),
42
def __init__(self, text = ""):
43
""" Class initialiser"""
44
gtk.EventBox.__init__(self)
45
self.__gobject_init__()
47
self._entry_handler_id = []
48
self._label = gtk.Label(text)
50
self.set_visible_window (False)
51
self.add (self._label)
52
self.connect ("button-press-event", self._on_click_text)
54
def set_angle(self, angle ):
55
"""set angle of the label"""
56
self._label.set_angle( angle )
59
"""Return if we are currently editing"""
60
return(self._entry != None)
62
def set_text(self, text, force=False):
63
"""set the text of the label"""
65
if not self._custom or force:
66
self._label.set_text(text)
69
"""get the text from the label"""
70
return(self._label.get_text())
72
def _on_click_text(self, widget, event):
73
# pylint: disable-msg=W0613
74
"""event handling text edition"""
75
if event.type == gtk.gdk._2BUTTON_PRESS:
76
self.remove (self._label)
77
self._entry = gtk.Entry ()
78
self._entry.set_text (self._label.get_text ())
80
self.add (self._entry)
81
sig = self._entry.connect ("focus-out-event", self._entry_to_label)
82
self._entry_handler_id.append(sig)
83
sig = self._entry.connect ("activate", self._on_entry_activated)
84
self._entry_handler_id.append(sig)
85
sig = self._entry.connect ("key-press-event",
86
self._on_entry_keypress)
87
self._entry_handler_id.append(sig)
88
self._entry.grab_focus ()
92
def _entry_to_label (self, widget, event):
93
# pylint: disable-msg=W0613
94
"""replace gtk.Entry by the gtk.Label"""
95
if self._entry and self._entry in self.get_children():
96
#disconnect signals to avoid segfault :s
97
for sig in self._entry_handler_id:
98
if self._entry.handler_is_connected(sig):
99
self._entry.disconnect(sig)
100
self._entry_handler_id = []
101
self.remove (self._entry)
102
self.add (self._label)
105
self.emit('edit-done')
109
def _on_entry_activated (self, widget):
110
# pylint: disable-msg=W0613
111
"""get the text entered in gtk.Entry"""
112
entry = self._entry.get_text ()
113
label = self._label.get_text ()
116
self.set_text (self._autotext)
119
self._label.set_text (entry)
120
self._entry_to_label (None, None)
122
def _on_entry_keypress (self, widget, event):
123
# pylint: disable-msg=W0613
124
"""handle keypressed in gtk.Entry"""
125
key = gtk.gdk.keyval_name (event.keyval)
127
self._entry_to_label (None, None)
129
def modify_fg(self, state, color):
130
"""Set the label foreground"""
131
self._label.modify_fg(state, color)
133
gobject.type_register(EditableLabel)