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