From 6971f5260eaac8eda22796ee474912cf66ddbfa2 Mon Sep 17 00:00:00 2001 From: mfrasca <> Date: Sat, 19 May 2007 20:42:33 +0000 Subject: [PATCH] 1714519: object oriented library example pywmphoto rewritten. modified initializer of pixmap: you can give the name of the pixmap containing the background/patterns, so you do not need reading it in advance. --- examples/pywmphoto.py | 87 ++++++++-------------------------------- wmdocklib/pywmhelpers.py | 5 +++ 2 files changed, 22 insertions(+), 70 deletions(-) diff --git a/examples/pywmphoto.py b/examples/pywmphoto.py index b09a91e..c14687a 100644 --- a/examples/pywmphoto.py +++ b/examples/pywmphoto.py @@ -4,7 +4,7 @@ WindowMaker dockapp that displays a static xpm -Copyright (C) 2006 Mario Frasca +Copyright (C) 2006-2007 Mario Frasca Licensed under the GNU General Public License. @@ -12,84 +12,31 @@ Licensed under the GNU General Public License. Changes: 2006-10-27 Mario Frasca First workingish version -""" -usage = """pywmphoto.py [options] -Available options are: --h, --help print this help --f, --file set the xpm name ---debug shows the pixmap + +2007-05-19 Mario Frasca +more compact form, based on wmdocklib.wmoo and optparse. """ -import sys, os, time -import getopt - -import wmdocklib - -def parseCommandLine(argv): - """Parse the commandline. Return a dictionary with options and values.""" - shorts = 'hf:' - longs = ['help', 'file=', 'debug', - ] - try: - opts, nonOptArgs = getopt.getopt(argv[1:], shorts, longs) - except getopt.GetoptError, e: - sys.stderr.write('Error when parsing commandline: ' + str(e) + '\n') - sys.stderr.write(usage) - sys.exit(2) - d = {} - - for o, a in opts: - if o in ('-h', '--help'): - sys.stdout.write(usage) - sys.exit(0) - if o in ('-f', '--file'): - d['file'] = a - if o in ('--debug'): - d['debug'] = True - return d - -def checkForEvents(): - event = wmdocklib.getEvent() - while not event is None: - if event['type'] == 'destroynotify': - sys.exit(0) - event = wmdocklib.getEvent() - -def mainLoop(): - while 1: - checkForEvents() - wmdocklib.redraw() - time.sleep(0.5) +from wmdocklib import wmoo def main(): - clConfig = parseCommandLine(sys.argv) - # openXwindow sets the window title to the program name. If we get the - # program name with a path, split it so we only name the window with the - # filename. - try: - programName = sys.argv[0].split(os.sep)[-1] - except IndexError: # Should only happen when using the interpreter. - programName = '' - sys.argv[0] = programName + from optparse import OptionParser - xpmName = clConfig.get('file') + parser = OptionParser() + parser.add_option("-f", "--file", dest="filename", + help="read background from file", metavar="FILE") + parser.add_option("-d", "--debug", dest="debug", + action="store_true", default=False, + help="print the pixmap") - palette, patterns = wmdocklib.readXPM(xpmName) + (options, args) = parser.parse_args() - debug = clConfig.get('debug') - - global char_width, char_height, maxCharsPerLine, antialiased - wmdocklib.initPixmap(palette=palette, - patterns=patterns, - margin=3, - debug=debug) + app = wmoo.Application(background = options.filename, + margin = 3, + debug = options.debug) - wmdocklib.openXwindow(sys.argv, 64, 64) - wmdocklib.copyXPMArea(0, 64, 58, 58, 3, 3) - - mainLoop() + app.run() if __name__ == '__main__': main() - diff --git a/wmdocklib/pywmhelpers.py b/wmdocklib/pywmhelpers.py index 98d9f6b..93adbd2 100644 --- a/wmdocklib/pywmhelpers.py +++ b/wmdocklib/pywmhelpers.py @@ -207,6 +207,11 @@ def initPixmap(background=None, 'gray41', 'blue1', 'green1', 'cyan1', 'red1', 'magenta1', 'yellow1', 'white'] + if isinstance(patterns, str): + palette, patterns = readXPM(patterns) + if isinstance(background, str): + palette, background = readXPM(background) + alter_palette, palette = palette, {} for name, index in zip(basic_colors, range(16)): palette['%x'%index] = getColorCode(name)