# -*- coding utf-8 -*-
##KaaShopping - a shopping list application
##
##Copyright (C) 2010 Rodi
##
##This program is free software; you can redistribute it and/or modify it
##under the terms of the GNU General Public License as published by
##the Free Software Foundation; either version 2 of the License,
##or (at your option) any later version.
##
##This program is distributed in the hope that it will be useful,
##but WITHOUT ANY WARRANTY; without even the implied warranty of
##MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
##GNU General Public License for more details.
##
##You should have received a copy of the GNU General Public License along with
##this program; if not, write to the
##Free Software Foundation, Inc., 51 Franklin St,
##Fifth Floor, Boston, MA 02110, USA
##
import ppygui as gui
#from ctypes import *
from ppygui.w32api import *
from ppygui.font import LOGFONT
#Font enumeration
#Font families are enumerated through a call to EnumFontFamilies. This function receives a callback function pointer that will fill up the font combo box. When the dialog is initialized, it tries to match the font specified in m_logFont with the contents of the combo boxes. If it cannot match the font name and size, the dialog will set both name and size in the proper combo boxes. The dialog is not prepared to receive a clean m_logFont, you must fill it in before using the dialog.
#ClearType
#ClearType support depends on your system supporting it. Please check with your vendor if your device supports ClearType.
#Creating a font with the ClearType property is as simple as specifying 5 as the lfQuality member of the LOGFONT structure.
#
FW_BOLD = 700
LF_FACESIZE = 32
class ENUMLOGFONTEX(Structure):
_fields_ = [("elfLogFont", LOGFONT),
("elfFullName", TCHAR * LF_FACESIZE),
("elfStyle", TCHAR * LF_FACESIZE),
("elfScript", TCHAR * LF_FACESIZE)]
ENUMFONTNAMEXPROC = WINFUNCTYPE(c_int, POINTER(ENUMLOGFONTEX), POINTER(DWORD), DWORD, LPARAM)
have_EnumFontFamilies=True
try:
EnumFontFamilies = windll.coredll.EnumFontFamilies
except:
have_EnumFontFamilies=False
class ChooseFont(gui.Dialog):
RASTER_FONTTYPE = 1
DEVICE_FONTTYPE = 2
TRUETYPE_FONTTYPE = 4
def __init__(self, title='Choose Font'):
gui.Dialog.__init__(self, title, action=("Cancel", self.on_cancel))
self.FONTNAMES = ['Tahoma', 'Curier']
self.fontNames = []
self.FONTSIZES = ['8', '9', '10', '11', '12', '14', '16', '20', '24', '28', '36']
self.label_1 = gui.Label(self, title)
self.label_font = gui.Label(self, 'Font:')
self.combo_font_name = gui.Combo(self, style='list', choices=[])
self.combo_font_name.bind(selchanged=self.on_font_name)
self.label_size = gui.Label(self, 'Size:')
self.combo_font_size = gui.Combo(self, style='list', choices=self.FONTSIZES)
self.combo_font_size.bind(selchanged=self.on_font_size)
self.label_style = gui.Label(self, 'Style') #font=bold_font
self.check_bold = gui.Button(self, 'Bold', style="check", action=self.on_bold)
self.check_italic = gui.Button(self, 'Italic', style="check", action=self.on_italic)
self.check_underline = gui.Button(self, 'Underline', style="check", action=self.on_underline)
self.check_cleartype = gui.Button(self, 'ClearType', style="check", action=self.on_cleartype)
self.check_cleartype.disable()
bold_font = gui.Font(bold=True)
self.label_preview = gui.Label(self, 'Preview') #font=bold_font
self.label_preview2 = gui.Label(self, 'dolorypse')
self.butt_ok = gui.Button(self, "&Ok", action=self.on_ok)
self.butt_cancel = gui.Button(self, "&Cancel", action=self.on_cancel)
self.sippref = gui.SIPPref(self)
sizer = gui.VBox( border=(2,2,2,2), spacing=2 )
sizer.add( self.label_1)
sizer.add( gui.HLine(self) )
table = gui.TBox(2,2, border=(5,5,5,5), spacing_x=2, spacing_y=2, cols_expanded=[1])
table.add( self.label_font )
table.add( self.combo_font_name )
table.add( self.label_size )
table.add( self.combo_font_size )
sizer.add( table )
sizer.add( self.label_style )
sizer.add( gui.HLine(self) )
table2 = gui.TBox(2,2, border=(5,5,5,5), spacing_x=2, spacing_y=2)
table2.add( self.check_bold )
table2.add( self.check_italic )
table2.add( self.check_underline )
table2.add( self.check_cleartype )
sizer.add( table2 )
sizer.add( self.label_preview )
sizer.add( gui.HLine(self) )
sizer2 = gui.VBox(spacing=5)
sizer.add( sizer2 )
sizer2.add( self.label_preview2,1 )
sizer3 = gui.VBox(border=(5,5,5,5), spacing=5 )
sizer3.add(gui.Spacer(0, 0), 1)
sizer.add( sizer3 )
sizer4 = gui.HBox(spacing=5)
sizer4.add(self.butt_cancel,1)
sizer4.add(self.butt_ok,1)
sizer.addfixed(sizer4, 30)
self.set_sizer(sizer)
self.logFont = {'name':"Tahoma", 'size':9, 'bold':True, 'italic':False, 'underline':False}
# Enumerate all the fonts
hDC = gui.GetDC(0)
if have_EnumFontFamilies:
EnumFontFamilies(hDC, None, ENUMFONTNAMEXPROC(self._EnumFontsFamProc), 0)
else:
self.fontNames = self.FONTNAMES
gui.ReleaseDC(0, hDC)
self.fontNames.sort()
for i in self.fontNames :
self.combo_font_name.append(i)
# Now, find the prespecified font, if any
self.set_data()
def set_data(self):
# Now, find the prespecified font, if any
try:
self.combo_font_name.selection = self.fontNames.index(self.logFont['name'])
except ValueError:
pass
# Find the given point size
try:
self.combo_font_size.selection = self.FONTSIZES.index(str(self.logFont['size']))
except ValueError:
pass
# Check for other font properties
self.check_bold.checked = self.logFont['bold']
self.check_italic.checked = self.logFont['italic']
self.check_underline.checked = self.logFont['underline']
#self.check_cleartype.checked = self.logFont['cleartype']
# Render the sample
self.render()
def render(self):
font = gui.Font(**self.logFont)
self.label_preview2.set_font(font)
def onok(self):
self.end('ok')
def on_ok(self, ev):
self.onok()
def on_cancel(self, ev):
self.end('cancel')
def on_bold(self, ev):
self.logFont['bold'] = self.check_bold.checked
self.render()
def on_italic(self, ev):
self.logFont['italic'] = self.check_italic.checked
self.render()
def on_underline(self, ev):
self.logFont['underline'] = self.check_underline.checked
self.render()
def on_cleartype(self, ev):
#self.logFont['cleartype'] = self.check_cleartype.checked
#self.render()
pass
def on_font_name(self, ev):
self.logFont['name'] = self.FONTNAMES[self.combo_font_name.selection]
self.render()
def on_font_size(self, ev):
#nLogPixY=96
#self.logFont.lfHeight = int( - self.FONTSIZES[self.combo_font_size.selection] /72.0)
self.logFont['size'] = int(self.FONTSIZES[self.combo_font_size.selection])
self.render()
def _EnumFontsFamProc(self, pLfe, pNtme, FontType, lparam):
self.fontNames.append(pLfe[0].lfLogFont.lfFaceName)
return 1
def popup(self, parent=None, name="Tahoma", size=9, bold=False, italic=False, color=(0,0,0), underline=False):
self.logFont = {'name':name, 'size':size, 'bold':bold, 'italic':italic, 'underline':underline}
self.set_data()
return gui.Dialog.popup(self) =='ok' and self.logFont or None
class MainFrame(gui.CeFrame):
def __init__(self):
gui.CeFrame.__init__(self, title="test")
dlg = ChooseFont('choose font')
sizer0 = gui.VSizer()
sizer0.add(dlg)
self.set_sizer(sizer0)
lf = {'name':"Tahoma", 'size':10, 'bold':False, 'italic':False, 'underline':False}
dlg.popup(self, **lf )
dlg.destroy()
#self.destroy()
class App(gui.Application):
def __init__(self):
gui.Application.__init__(self)
self.mainframe = MainFrame()
def main():
app = App()
app.run()
app.quit()
if __name__ == '__main__' : main()
# vi:set ts=2 sw=2 ai si: