mirror of
https://github.com/gryf/wmdocklib.git
synced 2025-12-18 20:10:23 +01:00
adding a new program. it's a very simple clone of wmPhoto, with almost none
of its functionality. all this program does is show a picture in the tile. only useful to show how easy it is to use the wmdocklib library.
This commit is contained in:
95
examples/pywmPhoto.py
Normal file
95
examples/pywmPhoto.py
Normal file
@@ -0,0 +1,95 @@
|
|||||||
|
#!/usr/bin/env python
|
||||||
|
|
||||||
|
"""pywmPhoto.py
|
||||||
|
|
||||||
|
WindowMaker dockapp that displays a static xpm
|
||||||
|
|
||||||
|
Copyright (C) 2006 Mario Frasca
|
||||||
|
|
||||||
|
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 <file> set the xpm name
|
||||||
|
--debug shows the pixmap
|
||||||
|
"""
|
||||||
|
|
||||||
|
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)
|
||||||
|
|
||||||
|
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
|
||||||
|
|
||||||
|
xpmName = clConfig.get('file')
|
||||||
|
|
||||||
|
palette, patterns = wmdocklib.readXPM(xpmName)
|
||||||
|
|
||||||
|
debug = clConfig.get('debug')
|
||||||
|
|
||||||
|
global char_width, char_height, maxCharsPerLine, antialiased
|
||||||
|
wmdocklib.initPixmap(palette=palette,
|
||||||
|
patterns=patterns,
|
||||||
|
margin=3,
|
||||||
|
debug=debug)
|
||||||
|
|
||||||
|
wmdocklib.openXwindow(sys.argv, 64, 64)
|
||||||
|
wmdocklib.copyXPMArea(0, 64, 58, 58, 3, 3)
|
||||||
|
|
||||||
|
mainLoop()
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
main()
|
||||||
|
|
||||||
1
setup.py
1
setup.py
@@ -29,6 +29,7 @@ setup(name="pywmdockapps",
|
|||||||
'examples/pywmhdmon.py',
|
'examples/pywmhdmon.py',
|
||||||
'examples/pywmseti.py',
|
'examples/pywmseti.py',
|
||||||
'examples/pywmsysmon.py',
|
'examples/pywmsysmon.py',
|
||||||
|
'examples/pywmPhoto.py',
|
||||||
'examples/pywmgeneric.py'],
|
'examples/pywmgeneric.py'],
|
||||||
package_data={'wmdocklib': ['*.xpm']},
|
package_data={'wmdocklib': ['*.xpm']},
|
||||||
ext_modules = [module1])
|
ext_modules = [module1])
|
||||||
|
|||||||
@@ -127,10 +127,13 @@ def getVertSpacing(numLines, margin, height, yOffset):
|
|||||||
def readXPM(fileName):
|
def readXPM(fileName):
|
||||||
"""Read the xpm in filename.
|
"""Read the xpm in filename.
|
||||||
|
|
||||||
Return a list of strings containing the xpm. Raise IOError if we run
|
Return the pair (palette, pixels).
|
||||||
into trouble when trying to read the file. This function surely
|
|
||||||
doesn't handle all XPMs, but it handles the ones I use, so that'll
|
palette is a dictionary char->color (no translation attempted).
|
||||||
do.
|
pixels is a list of strings.
|
||||||
|
|
||||||
|
Raise IOError if we run into trouble when trying to read the file. This
|
||||||
|
function has not been tested extensively. do not try to use more than
|
||||||
"""
|
"""
|
||||||
f = file(fileName, 'r')
|
f = file(fileName, 'r')
|
||||||
lines = [l.rstrip('\n') for l in f.readlines()]
|
lines = [l.rstrip('\n') for l in f.readlines()]
|
||||||
@@ -145,7 +148,17 @@ def readXPM(fileName):
|
|||||||
s = s[nextStrEnd+1:]
|
s = s[nextStrEnd+1:]
|
||||||
continue
|
continue
|
||||||
break
|
break
|
||||||
return res
|
|
||||||
|
palette = {}
|
||||||
|
colorCount = int(res[0].split(' ')[2])
|
||||||
|
charsPerColor = int(res[0].split(' ')[3])
|
||||||
|
assert(charsPerColor == 1)
|
||||||
|
for i in range(colorCount):
|
||||||
|
colorChar = res[i+1][0]
|
||||||
|
colorName = res[i+1][1:].split()[1]
|
||||||
|
palette[colorChar] = colorName
|
||||||
|
res = res[1 + int(res[0].split(' ')[2]):]
|
||||||
|
return palette, res
|
||||||
|
|
||||||
def initPixmap(background=None,
|
def initPixmap(background=None,
|
||||||
patterns=None,
|
patterns=None,
|
||||||
@@ -165,13 +178,13 @@ def initPixmap(background=None,
|
|||||||
|
|
||||||
the remaining lower area defines the character set. this is initialized
|
the remaining lower area defines the character set. this is initialized
|
||||||
using the corresponding named character set. a file with this name must
|
using the corresponding named character set. a file with this name must
|
||||||
be found somewhere in the path. the colours used in the xpm file for
|
be found somewhere in the path. the colors used in the xpm file for
|
||||||
the charset must be ' ' and '%'. they will be changed here to bg and
|
the charset must be ' ' and '%'. they will be changed here to bg and
|
||||||
fg.
|
fg.
|
||||||
|
|
||||||
palette is a dictionary
|
palette is a dictionary
|
||||||
1: of integers <- [0..15] to colours.
|
1: of integers <- [0..15] to colors.
|
||||||
2: of single chars to colours.
|
2: of single chars to colors.
|
||||||
|
|
||||||
a default palette is provided, and can be silently overwritten with the
|
a default palette is provided, and can be silently overwritten with the
|
||||||
one passed as parameter.
|
one passed as parameter.
|
||||||
@@ -183,15 +196,15 @@ def initPixmap(background=None,
|
|||||||
available = dict([(chr(ch), True) for ch in range(32,127)])
|
available = dict([(chr(ch), True) for ch in range(32,127)])
|
||||||
|
|
||||||
# a palette is a dictionary from one single letter to an hexadecimal
|
# a palette is a dictionary from one single letter to an hexadecimal
|
||||||
# colour. per default we offer a 16 colours palette including what I
|
# color. per default we offer a 16 colors palette including what I
|
||||||
# consider the basic colours:
|
# consider the basic colors:
|
||||||
basic_colours = ['black', 'blue3', 'green3', 'cyan3',
|
basic_colors = ['black', 'blue3', 'green3', 'cyan3',
|
||||||
'red3', 'magenta3', 'yellow3', 'gray',
|
'red3', 'magenta3', 'yellow3', 'gray',
|
||||||
'gray41', 'blue1', 'green1', 'cyan1',
|
'gray41', 'blue1', 'green1', 'cyan1',
|
||||||
'red1', 'magenta1', 'yellow1', 'white']
|
'red1', 'magenta1', 'yellow1', 'white']
|
||||||
|
|
||||||
alter_palette, palette = palette, {}
|
alter_palette, palette = palette, {}
|
||||||
for name, index in zip(basic_colours, range(16)):
|
for name, index in zip(basic_colors, range(16)):
|
||||||
palette['%x'%index] = getColorCode(name)
|
palette['%x'%index] = getColorCode(name)
|
||||||
available['%x'%index] = False
|
available['%x'%index] = False
|
||||||
palette[' '] = 'None'
|
palette[' '] = 'None'
|
||||||
@@ -234,7 +247,7 @@ def initPixmap(background=None,
|
|||||||
] + [
|
] + [
|
||||||
' '*width for item in range(margin)
|
' '*width for item in range(margin)
|
||||||
]
|
]
|
||||||
elif isinstance(background, types.ListType):
|
elif isinstance(background, types.ListType) and not isinstance(background[0], types.StringTypes):
|
||||||
nbackground = [[' ']*width for i in range(height)]
|
nbackground = [[' ']*width for i in range(height)]
|
||||||
for ((left, top),(right, bottom)) in background:
|
for ((left, top),(right, bottom)) in background:
|
||||||
for x in range(left, right+1):
|
for x in range(left, right+1):
|
||||||
@@ -255,14 +268,7 @@ def initPixmap(background=None,
|
|||||||
|
|
||||||
def readFont(font_name):
|
def readFont(font_name):
|
||||||
# read xpm, return cell_size, definition and palette.
|
# read xpm, return cell_size, definition and palette.
|
||||||
font_palette = {}
|
font_palette, fontdef = readXPM(__file__[:__file__.rfind(os.sep) + 1] + font_name + '.xpm')
|
||||||
fontdef = readXPM(__file__[:__file__.rfind(os.sep) + 1] + font_name + '.xpm')
|
|
||||||
colorCount = int(fontdef[0].split(' ')[2])
|
|
||||||
for i in range(colorCount):
|
|
||||||
colorChar = fontdef[i+1][0]
|
|
||||||
colorName = fontdef[i+1][1:].split()[1]
|
|
||||||
font_palette[colorChar] = colorName
|
|
||||||
fontdef = fontdef[1 + int(fontdef[0].split(' ')[2]):]
|
|
||||||
|
|
||||||
import re
|
import re
|
||||||
m = re.match(r'.*([0-9]+)x([0-9]+).*', font_name)
|
m = re.match(r'.*([0-9]+)x([0-9]+).*', font_name)
|
||||||
@@ -290,7 +296,7 @@ def initPixmap(background=None,
|
|||||||
|
|
||||||
fg, bg must be of the form #xxxxxx
|
fg, bg must be of the form #xxxxxx
|
||||||
|
|
||||||
the corresponding calibrated colour lies at a specific percentage of
|
the corresponding calibrated color lies at a specific percentage of
|
||||||
the vector going from background to foreground."""
|
the vector going from background to foreground."""
|
||||||
|
|
||||||
bg_point = [int(bg[i*2+1:i*2+3],16) for i in range(3)]
|
bg_point = [int(bg[i*2+1:i*2+3],16) for i in range(3)]
|
||||||
@@ -299,11 +305,11 @@ def initPixmap(background=None,
|
|||||||
fg_vec = [f-b for (f,b) in zip(fg_point,bg_point)]
|
fg_vec = [f-b for (f,b) in zip(fg_point,bg_point)]
|
||||||
|
|
||||||
new_font_palette = {}
|
new_font_palette = {}
|
||||||
for k, colourName in font_palette.items():
|
for k, colorName in font_palette.items():
|
||||||
if colourName == 'None':
|
if colorName == 'None':
|
||||||
continue
|
continue
|
||||||
origColour = getColorCode(colourName)[1:]
|
origColor = getColorCode(colorName)[1:]
|
||||||
origRgb = [int(origColour[i*2:i*2+2],16)/256. for i in range(3)]
|
origRgb = [int(origColor[i*2:i*2+2],16)/256. for i in range(3)]
|
||||||
intensity = sum(origRgb) / 3
|
intensity = sum(origRgb) / 3
|
||||||
newRgb = [i * intensity + base for i,base in zip(fg_vec, bg_point)]
|
newRgb = [i * intensity + base for i,base in zip(fg_vec, bg_point)]
|
||||||
new_font_palette[k] = '#'+''.join(["%02x"%i for i in newRgb])
|
new_font_palette[k] = '#'+''.join(["%02x"%i for i in newRgb])
|
||||||
|
|||||||
Reference in New Issue
Block a user