-
Notifications
You must be signed in to change notification settings - Fork 1
/
preview.py
52 lines (42 loc) · 1.51 KB
/
preview.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
"""
Slightly modified GtkEntry for previewing text.
"""
import gtk
import pango
class PreviewEntry(gtk.Entry):
def __init__(self, text='Example Preview'):
gtk.Entry.__init__(self)
self.text = text
self.set_text(text)
self.set_inner_border(gtk.Border(15, 15, 15, 15))
self.set_alignment(0.5)
self.connect('focus-out-event', self.fill_if_empty)
self.font = self.get_style().font_desc.copy()
self.font.set_size(16 * pango.SCALE)
self.bg_color = None
self.fg_color = None
self.change_style()
def fill_if_empty(self, me, evt):
if self.get_text().strip() == '':
self.set_text(self.text)
def alloc_color(self, color):
return self.get_colormap().alloc_color(color)
def set_bg_color(self, color):
self.bg_color = color
self.change_style()
def set_fg_color(self, color):
self.fg_color = color
self.change_style()
def change_style(self):
style = gtk.RcStyle()
style.font_desc = self.font
style.xthickness = 0
style.ythickness = 0
for state in [gtk.STATE_NORMAL, gtk.STATE_ACTIVE, gtk.STATE_PRELIGHT, gtk.STATE_INSENSITIVE]:
if self.bg_color is not None:
style.bg[state] = self.bg_color
style.base[state] = self.bg_color
if self.fg_color is not None:
style.fg[state] = self.fg_color
style.text[state] = self.fg_color
self.modify_style(style)