Author: Steve Lee
(2010/01/23 17:27) Over 2 years ago
Added print opetion. re #15
8
9
def config_file(self):
10
#was os.path.expanduser("~/.dkeyrc") which is good for *nix
11
cfgfile = ".dkeyrc"
cfgfile = ".dkey"
12
from win32com.shell import shell, shellcon
13
dir = shell.SHGetFolderPath(0, # according to MSDN we should now use SHGetKnownFolderPath
14
shellcon.CSIDL_APPDATA,
...
24
def __init__(self, main, defaults={}):
25
26
object.__init__(self)
27
self.config = configobj.ConfigObj(self.config_file(),
self.config = configobj.ConfigObj(self.config_file()+'rc',
28
configspec="dkeyrc.val")
29
print "validation:", self.config.validate(validate.Validator())
30
self.main = main
47
print "calling set_" + attrname
48
setter(value)
49
50
def callgetter(self, attrname):
51
try:
52
method = getattr(self.main, "get_" + attrname)
53
except AttributeError:
54
return self.config[attrname]
55
else:
56
return method()
57
def __setattr__(self, attrname, value):
58
if "constructed" not in self.__dict__:
59
print "setting real attribute '%s'" % attrname
60
self.callsetter(attrname, value)
63
self.config[attrname] = value # always, or at least when the
64
# getter is not defined
65
def __getattr__(self, attrname):
66
67
return self.callgetter(attrname)
61
68
62
def loadattr(self, attrname):
69
setattr(self, attrname, self.config[attrname])
70
71
def load(self):
72
for attr in self.config:
73
self.loadattr(attr)
74
75
76
77
print_settings = gtk.print_settings_new_from_file(self.config_file()+'ps')
78
except:
79
print_settings = gtk.PrintSettings()
80
setattr(self, 'print_settings', print_settings)
81
# for attr in self.defaults:
82
# if attr not in self.config:
83
# self.callsetter(attr, self.defaults[attr])
84
self.config[attr] = getattr(self, attr)
89
self.config.write()
90
91
92
print_settings = getattr(self, 'print_settings')
93
print_settings.to_file(self.config_file()+'ps');
94
95
class DKeyConfig(Config):
96
85
posprefix = "_position"
97
86
coloursuffix = "_colour"
98
self.iterhistory = []
self.pronounce = False
self.placetext = False
self.print_page_setup = None
# load layout data
gtk.rc_parse("dkeyrc.gtk")
87
486
self.setmarks(enditer, enditer, enditer)
488
487
489
def clearWindow(self):
490
print 'clear'
startitr = self.textbuf.get_start_iter();
491
self.textbuf.delete(startitr, self.textbuf.get_end_iter())
492
self.setmarks(startitr, startitr, startitr)
493
494
495
def pageSetup(self):
496
self.print_page_setup = gtk.print_run_page_setup_dialog(self.mainwindow, self.print_page_setup, self.print_settings)
497
498
def printWindow(self):
499
print_op = gtk.PrintOperation()
500
print_op.set_n_pages(1)
501
502
if self.print_settings != None:
503
print_op.set_print_settings( self.print_settings)
504
505
if self.print_page_setup != None:
506
print_op.set_default_page_setup(self.print_page_setup)
507
508
print_op.connect("draw_page", self.printText)
509
res = print_op.run(gtk.PRINT_OPERATION_ACTION_PRINT_DIALOG, self.mainwindow)
510
511
if True or res == gtkprint_OPERATION_RESULT_ERROR :
512
error_dialog = gtk.MessageDialog(self.mainwindow,
513
gtk.DIALOG_DESTROY_WITH_PARENT,
514
gtk.MESSAGE_ERROR,
515
gtk.BUTTONS_CLOSE,
516
"Error printing text:\n")
517
error_dialog.connect("response", lambda w,id: w.destroy())
518
error_dialog.show()
519
520
elif res == gtk.PRINT_OPERATION_RESULT_APPLY:
521
self.print_settings = print_op.get_print_settings()
522
523
def printText(self, operation=None, context=None, page_nr=None):
524
layout = context.create_pango_layout()
525
layout.set_width(int(context.get_width() * pango.SCALE))
526
layout.set_justify(False)
527
layout.set_alignment(pango.ALIGN_LEFT)
528
layout.set_wrap(pango.WRAP_WORD_CHAR)
529
layout.set_font_description(pango.FontDescription(self.config.printfontname))
530
layout.set_text(self.getText())
531
cr = context.get_cairo_context()
532
cr.show_layout(layout)
533
534
def getText(self):
535
start, end = self.textbuf.get_start_iter(), self.textbuf.get_end_iter()
536
text = self.textbuf.get_text(start, end)
537
return text
538
def settext(self, text):
539
startiter, cursoriter, enditer = self.getiters()
540
self.textbuf.delete(startiter, enditer)
541
676
self.buttontable.set_row_spacings(spacing)
721
677
self.buttontable.set_col_spacings(spacing)
722
678
723
724
def set_print_settings(self, settings):
725
self.print_settings = settings
726
727
def get_print_settings(self):
728
return self.print_settings
729
679
# reactions to user actions
730
680
731
681
def on_BackspaceButton_pressed(self, button):
732
755
self.on_BackspaceButton_pressed(None)
806
756
elif action == 'clear':
807
757
self.clearWindow()
808
809
elif action == 'print':
810
self.printWindow()
811
elif action == 'print_setup':
812
self.pageSetup()
758
813
759
pass
814
760
815
788
self.acceptword(self.getcurrentword())
843
789
844
790
def on_FontButton_pressed(self, button):
845
791
dialog = gtk.FontSelectionDialog("Select Font")
846
fontname = self.askFont("Select Font", self.config.fontname)
847
if fontname:
848
self.config.fontname = fontname
849
850
def on_PrintFontButton_pressed(self, button):
851
fontname = self.askFont("Select Print Font", self.config.printfontname)
852
853
self.config.printfontname = fontname
854
855
def askFont(self, title, fontname):
856
dialog = gtk.FontSelectionDialog(title)
792
self.mainwindow.set_keep_above(False) # must be after show on Windows
857
793
dialog.set_transient_for(self.mainwindow)
858
794
859
795
if self.config.fontname:
860
796
dialog.set_font_name(self.config.fontname)
861
dialog.set_font_name(fontname)
797
862
798
if dialog.run() == gtk.RESPONSE_OK:
863
799
self.config.fontname = dialog.get_font_name()
864
fontname = dialog.get_font_name()
865
866
fontname = None
800
867
801
dialog.destroy()
868
802
self.mainwindow.set_keep_above(True) # must be after show on Windows
869
870
871
return fontname
803
872
804
def on_ColourButton_pressed(self, which):
873
805
dialog = gtk.ColorSelectionDialog("Select %s colour" % which)
874
1
fontname = string(default="Sans 14")
2
printfontname = string(default="Sans 14")
text_colour = string(default="GTK")
3
bg_colour = string(default="GTK")
4
sel_text_colour = string(default="GTK")
5
DefaultDirName={pf}\DKey
15
PrivilegesRequired=admin
16
SetupIconFile=dkey.ico
17
UninstallIconFile=dkey.ico
18
19
; max compression - slower, not recommended over around 100 MB
20
;SolidCompression=true
# for now we use windows specific hooks until we see how to do in pygtk (pyxhook)
keymap = { 'Numpad7': 'code2',
keymap = {
'Divide': 'print_setup',
6
'Multiply': 'print',
7
'Subtract': 'clear',
'Numpad7': 'code2',
'Numpad8': 'code3',
'Numpad9': 'code4',
'Numpad4': 'code5',
'Numpad5': 'code6',
'Numpad6': 'code7',
'Numpad1': 'code8',
'Numpad2': 'code9',
'Numpad3': 'code1',
'Numpad0': 'code0',
21
22
'Decimal': 'del',
'Add': 'next',
23
#'Return': '',
'Return': '',
}
import pyHook
<widget id="button14" title="Inject Text"
action="self.config.placetext = 'focus'" group="placetext" />
<widget id="button22" title=""
<widget id="button22" title="Print Font..."
action="" />
action="self.on_PrintFontButton_pressed(None)"/>
<widget id="button23" title=""
<widget id="button24" title=""
#was os.path.expanduser("~/.dkeyrc") which is good for *nix#was os.path.expanduser("~/.dkeyrc") which is good for *nixcfgfile = ".dkeyrc"def __init__(self, main, defaults={}):def __init__(self, main, defaults={}):self.config = configobj.ConfigObj(self.config_file()+'rc',try:return self.callgetter(attrname)method = getattr(self.main, "get_" + attrname)except AttributeError:return self.config[attrname]else:return method()gtk.rc_parse("dkeyrc.gtk")gtk.rc_parse("dkeyrc.gtk")print 'clear'print_op.connect("draw_page", self.printText)error_dialog.connect("response", lambda w,id: w.destroy())dialog = gtk.FontSelectionDialog("Select Font")fontname = self.askFont("Select Font", self.config.fontname)if fontname:self.config.fontname = fontnamedef on_PrintFontButton_pressed(self, button):fontname = self.askFont("Select Print Font", self.config.printfontname)if fontname:self.config.printfontname = fontnamedef askFont(self, title, fontname):dialog = gtk.FontSelectionDialog(title)dialog.set_font_name(self.config.fontname)self.config.fontname = dialog.get_font_name()else:fontname = Nonedialog = gtk.ColorSelectionDialog("Select %s colour" % which)dialog = gtk.ColorSelectionDialog("Select %s colour" % which)DefaultDirName={pf}\DKeyDefaultDirName={pf}\DKeyUninstallIconFile=dkey.icokeymap = { 'Numpad7': 'code2',keymap = {'Divide': 'print_setup','Multiply': 'print','Subtract': 'clear','Numpad7': 'code2','Decimal': 'del','Subtract': 'clear',#'Return': '','Return': '',<widget id="button22" title="Print Font..."action="" />action="self.on_PrintFontButton_pressed(None)"/>