X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/96bfd05319f9e393843e84ca5274d85ff248bad4..6af638ed35e8f1c414cc209cda8e5059552883c8:/wxPython/tools/img2py.py diff --git a/wxPython/tools/img2py.py b/wxPython/tools/img2py.py index d6e7a0f0af..863de24d42 100644 --- a/wxPython/tools/img2py.py +++ b/wxPython/tools/img2py.py @@ -28,23 +28,34 @@ Options: -u Don't use compression. Leaves the data uncompressed. + -i Also output a function to return the image as a wxIcon. + """ -import sys, os, glob, getopt, tempfile +import sys, os, glob, getopt, tempfile, string import cPickle, cStringIO, zlib import img2xpm def crunch_data(data, compressed): - # convert the lines to a Python list, pickle it and compress the result. + # convert the lines to a Python list, pickle it and optionally compress the result. lines = [] - for line in data[2:]: # skip the first two lines - lines.append(line[1:-3]) # chop one char from the front and three from the end + for line in data: + if line[0] == "\"": + # the line is typically (but not always): + # [quote] [quote][comma][newline] + + # chop one char from the front + line = line[1:] + + # now find the final quote and truncate there + quote = string.rfind(line, "\"") + + # and append the remaining data to our list + lines.append(line[:quote]) - # chop one extra char from the last line - lines[-1] = lines[-1][:-1] # pickle, crunch and convert it to a form suitable for embedding in code data = cPickle.dumps(lines) @@ -59,6 +70,7 @@ def crunch_data(data, compressed): c = i = 0 word = "" octdigits = "01234567" + hexdigits = "0123456789abcdef" while i < len(data): if data[i] != "\\": word = data[i] @@ -70,9 +82,16 @@ def crunch_data(data, compressed): break word = data[i:i+n] i = i + n + elif data[i+1] == 'x': + for n in range(2, 5): + if data[i+n] not in hexdigits: + break + word = data[i:i+n] + i = i + n else: word = data[i:i+2] i = i + 2 + l = len(word) if c + l >= 78-1: fp.write("\\\n") @@ -94,9 +113,10 @@ def main(args): compressed = 1 maskClr = None imgName = "" + icon = 0 try: - opts, fileArgs = getopt.getopt(args, "aun:m:") + opts, fileArgs = getopt.getopt(args, "auin:m:") except getopt.GetoptError: print __doc__ return @@ -110,6 +130,8 @@ def main(args): imgName = val elif opt == "-m": maskClr = val + elif opt == "-i": + icon = 1 if len(fileArgs) != 2: print __doc__ @@ -137,6 +159,8 @@ def main(args): if not append: out.write("# This file was generated by %s\n#\n" % sys.argv[0]) out.write("from wxPython.wx import wxBitmapFromXPMData, wxImageFromBitmap\n") + if icon: + out.write("from wxPython.wx import wxIconFromXPMData\n") if compressed: out.write("import cPickle, zlib\n\n\n") else: @@ -157,6 +181,11 @@ def main(args): "def get%sImage():\n" " return wxImageFromBitmap(get%sBitmap())\n\n" % tuple([imgName] * 4)) + if icon: + out.write("def get%sIcon():\n" + " return wxIconFromXPMData(get%sData())\n\n" + % tuple([imgName] * 2)) + if imgName: n_msg = ' using "%s"' % imgName