ecpickers.py
5.09 KB
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
###############################################################################
# Name: ecpickers.py #
# Purpose: Custom picker controls #
# Author: Cody Precord <cprecord@editra.org> #
# Copyright: (c) 2008 Cody Precord <staff@editra.org> #
# License: wxWindows License #
###############################################################################
"""
Editra Control Library: Editra Control Pickers
Collection of various custom picker controls
Class: PyFontPicker
Custom font picker control
@summary: Various custom picker controls
"""
__author__ = "Cody Precord <cprecord@editra.org>"
__svnid__ = "$Id: ecpickers.py 70230 2012-01-01 01:47:42Z CJP $"
__revision__ = "$Revision: 70230 $"
__all__ = ['PyFontPicker', 'FontChangeEvent',
'EVT_FONT_CHANGED', 'edEVT_FONT_CHANGED']
#-----------------------------------------------------------------------------#
# Imports
import wx
_ = wx.GetTranslation
#-----------------------------------------------------------------------------#
edEVT_FONT_CHANGED = wx.NewEventType()
EVT_FONT_CHANGED = wx.PyEventBinder(edEVT_FONT_CHANGED, 1)
class FontChangeEvent(wx.PyCommandEvent):
"""General notification event"""
def __init__(self, etype, eid, value=None, obj=None):
super(FontChangeEvent, self).__init__(etype, eid)
# Attributes
self._value = value
# Setup
self.SetEventObject(obj)
def GetValue(self):
"""Returns the value from the event.
@return: the value of this event
"""
return self._value
#-----------------------------------------------------------------------------#
class PyFontPicker(wx.Panel):
"""A slightly enhanced wx.FontPickerCtrl that displays the choosen font in
the label text using the choosen font as well as the font's size using
nicer formatting.
"""
def __init__(self, parent, id_=wx.ID_ANY, default=wx.NullFont):
"""Initializes the PyFontPicker
@param parent: Parent Window
@keyword id_: Control ID
@keyword default: The font to initialize as selected in the control
"""
super(PyFontPicker, self).__init__(parent, id_, style=wx.NO_BORDER)
# Attributes
if default == wx.NullFont:
self._font = wx.SystemSettings_GetFont(wx.SYS_SYSTEM_FONT)
else:
self._font = default
self._text = wx.StaticText(self)
self._text.SetFont(default)
self._text.SetLabel(u"%s - %dpt" % (self._font.GetFaceName(), \
self._font.GetPointSize()))
self._button = wx.Button(self, label=_("Set Font") + u'...')
# Layout
vsizer = wx.BoxSizer(wx.VERTICAL)
sizer = wx.BoxSizer(wx.HORIZONTAL)
sizer.AddStretchSpacer()
sizer.Add(self._text, 0, wx.ALIGN_CENTER_VERTICAL)
sizer.AddStretchSpacer()
sizer.Add(self._button, 0, wx.ALIGN_CENTER_VERTICAL)
vsizer.AddMany([((1, 1), 0), (sizer, 0, wx.EXPAND), ((1, 1), 0)])
self.SetSizer(vsizer)
self.SetAutoLayout(True)
# Event Handlers
self.Bind(wx.EVT_BUTTON, lambda evt: self.ShowFontDlg(), self._button)
self.Bind(wx.EVT_FONTPICKER_CHANGED, self.OnChange)
def GetFontValue(self):
"""Gets the currently choosen font
@return: wx.Font
"""
return self._font
def GetTextCtrl(self):
"""Gets the widgets text control
@return: wx.StaticText
"""
return self._text
def OnChange(self, evt):
"""Updates the text control using our custom stylings after
the font is changed.
@param evt: The event that called this handler
"""
font = evt.GetFont()
if font.IsNull():
return
self._font = font
self._text.SetFont(self._font)
self._text.SetLabel(u"%s - %dpt" % (font.GetFaceName(), \
font.GetPointSize()))
self.Layout()
evt = FontChangeEvent(edEVT_FONT_CHANGED, self.GetId(), self._font, self)
wx.PostEvent(self.GetParent(), evt)
def SetButtonLabel(self, label):
"""Sets the buttons label"""
self._button.SetLabel(label)
self._button.Refresh()
self.Layout()
def SetToolTipString(self, tip):
"""Sets the tooltip of the window
@param tip: string
"""
self._text.SetToolTipString(tip)
self._button.SetToolTipString(tip)
wx.Panel.SetToolTipString(self, tip)
def ShowFontDlg(self):
"""Opens the FontDialog and processes the result"""
fdata = wx.FontData()
fdata.SetInitialFont(self._font)
fdlg = wx.FontDialog(self.GetParent(), fdata)
if fdlg.ShowModal() == wx.ID_OK:
fdata = fdlg.GetFontData()
wx.PostEvent(self, wx.FontPickerEvent(self, self.GetId(),
fdata.GetChosenFont()))
fdlg.Destroy()