#!/usr/bin/env python
#
# $Id: geometrystring.py,v 1.4 2001/11/03 11:05:22 doughellmann Exp $
#
# Copyright 2001 Doug Hellmann.
#
#
# All Rights Reserved
#
# Permission to use, copy, modify, and distribute this software and
# its documentation for any purpose and without fee is hereby
# granted, provided that the above copyright notice appear in all
# copies and that both that copyright notice and this permission
# notice appear in supporting documentation, and that the name of Doug
# Hellmann not be used in advertising or publicity pertaining to
# distribution of the software without specific, written prior
# permission.
#
# DOUG HELLMANN DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
# INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN
# NO EVENT SHALL DOUG HELLMANN BE LIABLE FOR ANY SPECIAL, INDIRECT OR
# CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
# OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
# NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
# CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#
"""Geometry string parser for Tkinter.
"""
__rcs_info__ = {
#
# Creation Information
#
'module_name' : '$RCSfile: geometrystring.py,v $',
'rcs_id' : '$Id: geometrystring.py,v 1.4 2001/11/03 11:05:22 doughellmann Exp $',
'creator' : 'Doug Hellmann <doug@hellfly.net>',
'project' : 'PmwContribD',
'created' : 'Sun, 01-Apr-2001 13:17:03 EDT',
#
# Current Information
#
'author' : '$Author: doughellmann $',
'version' : '$Revision: 1.4 $',
'date' : '$Date: 2001/11/03 11:05:22 $',
}
#
# Import system modules
#
from string import find,atoi
#
# Import Local modules
#
#
# Module
#
def geoStringToTuple( geoString ):
"""Convert a geometry string to a tuple of numerical values.
A geometry string takes the form WxH+X+Y or WxH
"""
w,h,x,y = None,None,None,None
mark = find( geoString,'x' )
if ( mark == -1 ):
geoString = geoString[1:]
mark = find( geoString,'+' )
try:
x = atoi(geoString[ :mark ])
except ValueError:
x = None
geoString = geoString[ mark+1: ]
try:
y = atoi(geoString)
except ValueError:
y = None
return w,h,x,y
try:
w = atoi(geoString[ :mark ])
except ValueError:
w = None
geoString = geoString[ mark+1: ]
mark = find( geoString,'+' )
if (mark == -1 ):
try:
h = atoi(geoString)
except ValueError:
h = None
return w,h,x,y
try:
h = atoi(geoString[ :mark ])
except ValueError:
h = None
geoString = geoString[ mark+1: ]
mark = find( geoString,'+' )
try:
x = atoi(geoString[ :mark ])
except ValueError:
x = None
geoString = geoString[ mark+1: ]
try:
y = atoi(geoString)
except ValueError:
y = None
return w,h,x,y
InvalidGeoTuple = 'Invalid Geometry Tuple'
def geoTupleToString( w=None, h=None, x=None, y=None ):
"""Format a set of values as a geometry string.
"""
if x <> None and y <> None and w <> None and h <> None:
return '%dx%d+%d+%d'%(w,h,x,y)
elif x == None and w <> None:
return '%dx%d'%(w,h)
elif x <> None and w == None:
return '+%d+%d'%(x,y)
else:
raise InvalidGeoTuple, (w, h, x, y)
|