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