from PSPApp import *
import PSPUtils

def ScriptProperties():
    return {
        'Author': u'Heinz Schweda',
        'Copyright': u'© www.schweda.net',
        'Description': u'Square Thumbnail',
        'Host': u'Paint Shop Pro X',
        'Host Version': u'10.00'
        }

def Do(Environment):
    # EnableOptimizedScriptUndo
    App.Do( Environment, 'EnableOptimizedScriptUndo', {
            'GeneralSettings': {
                'ExecutionMode': App.Constants.ExecutionMode.Default,
                'AutoActionMode': App.Constants.AutoActionMode.Match,
                'Version': ((10,0,0),1)
                }
            })


    if PSPUtils.RequireADoc( Environment ) == False: return

    if PSPUtils.IsTrueColor( Environment, App.TargetDocument ) == False:

        PSPUtils.PromoteToTrueColor( Environment, App.TargetDocument )

    # compute the width and the height of image, preserving the aspect ratio
    ImageInfo = App.Do( Environment, 'ReturnImageInfo' )
    Width = ImageInfo['Width']
    Height = ImageInfo['Height']
    Title = ImageInfo['Title']

    # Begin Translatable Strings
    DocumentSizeMsg = u"FEHLER: Das Bild %s ist kleiner als die angeforderte Miniaturansicht - es wurde keine Aenderung vorgenommen"
    ResizedMsg = u"Die Groesse des Bildes %s wurde in %d x %d geaendert."
    CroppedMsg = u"Das Bild %s wurde auf eine Groesse von %d x %d beschnitten"

    # Dialog
    Result = App.Do( Environment, 'GetNumber', {
        'DefaultValue': 100,
        'MinValue': 5,
        'MaxValue': 4096,
        'DialogTitle': 'Choose Thumbnail Width',
        'Prompt': 'Width in Pixel',
        'GetInteger': True,
        'GeneralSettings': {
            'ExecutionMode': App.Constants.ExecutionMode.Interactive
            }
        })

    if Result[ 'OKButton' ] == 0: raise SystemExit # Cancel Button

    MaxThumbnailSize = Result[ 'EnteredNumber' ]

    # Umrechnung
    if Width <= MaxThumbnailSize or Height <= MaxThumbnailSize:
	print DocumentSizeMsg % (Title)
	return
    elif Width > Height:
	NewHeight = MaxThumbnailSize
	NewWidth  = Width * float(NewHeight) / float(Height)
	PlaceBottom = 0
	PlaceTop = 0
	PlaceLeft = ( ( NewWidth - NewHeight ) / 2 ) * -1
	PlaceRight = ( NewWidth - PlaceLeft ) * -1
	ratio = NewWidth / NewHeight
    else:   # height > width
	NewWidth  = MaxThumbnailSize
	NewHeight = Height * float(NewWidth) / float(Width)
	PlaceLeft = 0
	PlaceRight = 0
	PlaceTop = ( ( NewWidth - NewHeight ) / 2 ) * -1
	PlaceBottom = ( NewWidth - PlaceLeft ) * -1
	ratio = NewWidth / NewHeight

    print ResizedMsg  % ( Title, NewWidth, NewHeight )

    App.Do( Environment, 'Resize', {
            'CurrentDimensionUnits': App.Constants.UnitsOfMeasure.Pixels, 
            'CurrentResolutionUnits': App.Constants.ResolutionUnits.PixelsPerIn, 
            'MaintainAspectRatio': App.Constants.Boolean.true, 
            'OriginalDimensionUnits': App.Constants.UnitsOfMeasure.Pixels, 
            'OriginalResolutionUnits': App.Constants.ResolutionUnits.PixelsPerIn, 
            'Resample': App.Constants.Boolean.true, 
            'ResampleType': App.Constants.ResampleType.SmartSize, 
            'ResizeAllLayers': App.Constants.Boolean.true, 
            'Resolution': 100.000, 
            'Width': NewWidth, 
            'Height': NewHeight, 
            'GeneralSettings': {
                'ExecutionMode': App.Constants.ExecutionMode.Silent, 
                'AutoActionMode': App.Constants.AutoActionMode.Match
                }
            })

    print ResizedMsg  % ( Title, NewWidth, NewHeight )

    # ResizeCanvas
    App.Do( Environment, 'ResizeCanvas', {
            'AspectRatio': ratio, 
            'FillColor': (255,255,255), 
            'HoriPlace': App.Constants.HorizontalType.Center, 
            'MaintainAspect': False, 
            'NewDimUnits': App.Constants.UnitsOfMeasure.Pixels, 
            'NewHeight': MaxThumbnailSize, 
            'NewWidth': MaxThumbnailSize, 
            'PlaceBottom': PlaceBottom, 
            'PlaceLeft': PlaceLeft, 
            'PlaceRight': PlaceRight, 
            'PlaceTop': PlaceTop, 
            'VertPlace': App.Constants.VerticalType.Center, 
            'GeneralSettings': {
                'ExecutionMode': App.Constants.ExecutionMode.Silent, 
                'AutoActionMode': App.Constants.AutoActionMode.Match, 
                'Version': ((10,0,0),1)
                }
            })

    print CroppedMsg  % ( Title, MaxThumbnailSize, MaxThumbnailSize )