]>
Commit | Line | Data |
---|---|---|
1 | # 11/18/2003 - Jeff Grimmett (grimmtooth@softhome.net) | |
2 | # | |
3 | # o Updated for wx namespace | |
4 | # | |
5 | ||
6 | import wx | |
7 | ||
8 | #--------------------------------------------------------------------------- | |
9 | ||
10 | class TestPanel(wx.Panel): | |
11 | def __init__(self, parent, log): | |
12 | wx.Panel.__init__(self, parent, -1) | |
13 | self.log = log | |
14 | ||
15 | btn = wx.Button(self, -1, "Select Font") | |
16 | self.Bind(wx.EVT_BUTTON, self.OnSelectFont, btn) | |
17 | ||
18 | self.sampleText = wx.TextCtrl(self, -1, "Sample Text") | |
19 | #from wxPython.lib.stattext import wxGenStaticText | |
20 | #self.sampleText = wxGenStaticText(self, -1, "Sample Text") | |
21 | ||
22 | self.curFont = self.sampleText.GetFont() | |
23 | self.curClr = wx.BLACK | |
24 | ||
25 | fgs = wx.FlexGridSizer(cols=2, vgap=5, hgap=5) | |
26 | fgs.AddGrowableCol(1) | |
27 | fgs.AddGrowableRow(0) | |
28 | ||
29 | fgs.Add(btn) | |
30 | fgs.Add(self.sampleText, 0, wx.ADJUST_MINSIZE|wx.GROW) | |
31 | ||
32 | fgs.Add((15,15)); fgs.Add((15,15)) # an empty row | |
33 | ||
34 | fgs.Add(wx.StaticText(self, -1, "PointSize:")) | |
35 | self.ps = wx.StaticText(self, -1, "") | |
36 | font = self.ps.GetFont() | |
37 | font.SetWeight(wx.BOLD) | |
38 | self.ps.SetFont(font) | |
39 | fgs.Add(self.ps, 0, wx.ADJUST_MINSIZE) | |
40 | ||
41 | fgs.Add(wx.StaticText(self, -1, "Family:")) | |
42 | self.family = wx.StaticText(self, -1, "") | |
43 | self.family.SetFont(font) | |
44 | fgs.Add(self.family, 0, wx.ADJUST_MINSIZE) | |
45 | ||
46 | fgs.Add(wx.StaticText(self, -1, "Style:")) | |
47 | self.style = wx.StaticText(self, -1, "") | |
48 | self.style.SetFont(font) | |
49 | fgs.Add(self.style, 0, wx.ADJUST_MINSIZE) | |
50 | ||
51 | fgs.Add(wx.StaticText(self, -1, "Weight:")) | |
52 | self.weight = wx.StaticText(self, -1, "") | |
53 | self.weight.SetFont(font) | |
54 | fgs.Add(self.weight, 0, wx.ADJUST_MINSIZE) | |
55 | ||
56 | fgs.Add(wx.StaticText(self, -1, "Face:")) | |
57 | self.face = wx.StaticText(self, -1, "") | |
58 | self.face.SetFont(font) | |
59 | fgs.Add(self.face, 0, wx.ADJUST_MINSIZE) | |
60 | ||
61 | fgs.Add((15,15)); fgs.Add((15,15)) # an empty row | |
62 | ||
63 | fgs.Add(wx.StaticText(self, -1, "wx.NativeFontInfo:")) | |
64 | self.nfi = wx.StaticText(self, -1, "") | |
65 | self.nfi.SetFont(font) | |
66 | fgs.Add(self.nfi, 0, wx.ADJUST_MINSIZE) | |
67 | ||
68 | # give it some border space | |
69 | sizer = wx.BoxSizer(wx.VERTICAL) | |
70 | sizer.Add(fgs, 0, wx.GROW|wx.ADJUST_MINSIZE|wx.ALL, 25) | |
71 | ||
72 | self.SetSizer(sizer) | |
73 | self.UpdateUI() | |
74 | ||
75 | ||
76 | def UpdateUI(self): | |
77 | self.sampleText.SetFont(self.curFont) | |
78 | self.ps.SetLabel(str(self.curFont.GetPointSize())) | |
79 | self.family.SetLabel(self.curFont.GetFamilyString()) | |
80 | self.style.SetLabel(self.curFont.GetStyleString()) | |
81 | self.weight.SetLabel(self.curFont.GetWeightString()) | |
82 | self.face.SetLabel(self.curFont.GetFaceName()) | |
83 | self.nfi.SetLabel(self.curFont.GetNativeFontInfo().ToString()) | |
84 | self.Layout() | |
85 | ||
86 | ||
87 | def OnSelectFont(self, evt): | |
88 | data = wx.FontData() | |
89 | data.EnableEffects(True) | |
90 | data.SetColour(self.curClr) # set colour | |
91 | data.SetInitialFont(self.curFont) | |
92 | ||
93 | dlg = wx.FontDialog(self, data) | |
94 | ||
95 | if dlg.ShowModal() == wx.ID_OK: | |
96 | data = dlg.GetFontData() | |
97 | font = data.GetChosenFont() | |
98 | colour = data.GetColour() | |
99 | ||
100 | self.log.WriteText('You selected: "%s", %d points, color %s\n' % | |
101 | (font.GetFaceName(), font.GetPointSize(), | |
102 | colour.Get())) | |
103 | ||
104 | self.curFont = font | |
105 | self.curClr = colour | |
106 | self.UpdateUI() | |
107 | ||
108 | # Don't destroy the dialog until you get everything you need from the | |
109 | # dialog! | |
110 | dlg.Destroy() | |
111 | ||
112 | ||
113 | #--------------------------------------------------------------------------- | |
114 | ||
115 | def runTest(frame, nb, log): | |
116 | win = TestPanel(nb, log) | |
117 | return win | |
118 | ||
119 | #--------------------------------------------------------------------------- | |
120 | ||
121 | ||
122 | overview = """\ | |
123 | This class allows you to use the system font selection dialog | |
124 | from within your program. Generally speaking, this allows you | |
125 | to select a font by its name, font size, and weight, and | |
126 | on some systems such things as strikethrough and underline. | |
127 | ||
128 | As with other dialogs used in wxPython, it is important to | |
129 | use the class' methods to extract the information you need | |
130 | about the font <b>before</b> you destroy the dialog. Failure | |
131 | to observe this almost always leads to a program failure of | |
132 | some sort, often ugly. | |
133 | ||
134 | This demo serves two purposes; it shows how to use the dialog | |
135 | to GET font information from the user, but also shows how | |
136 | to APPLY that information once you get it. | |
137 | ||
138 | """ | |
139 | ||
140 | if __name__ == '__main__': | |
141 | import sys,os | |
142 | import run | |
143 | run.main(['', os.path.basename(sys.argv[0])]) | |
144 |