from JascApp import *
from Tkinter import *
import tkMessageBox
import JascUtils

def ScriptProperties():
    return {
        'Author': u'Jasc Software, Inc.',
        'Copyright': u'Copyright (c) 2002-2004 Jasc Software, Inc.  All rights reserved.',
        'Description': "Combine all open images into a single image and export it as a tube",
        'Host': u'Paint Shop Pro',
        'Host Version': u'8.00',
        'ForceTextEditor': App.Constants.Boolean.true
        }

# Begin Translatable Strings
CellArrangementPrompt = "Enter the cell arrangement of the tube:"
CellsAcross = "Cells across:"
CellsDown = "Cells down:"
CellPadding = "Cell padding:"
OKText = "OK"
CancelText = "Cancel"
CellsAcrossInvalid = "Cells across invalid"
CellsAcrossCondition = "Cells across must be between 1 and %d"
CellsDownInvalid = "Cells down invalid"
CellsDownCondition = "Cells down must be between 1 and %d"
NotEnoughCells = "Not enough cells"
CellCountCondition = "Cells across x cells down must be at least %d"
TooManyCells = "Too many cells"
TooManyCellsCondition = "For the cells across value specified, cells down can be no more than %d"
PadAmountInvalid = "Pad amount invalid"
PadAmountCondition = "The per cell pad amount must be between 0 and 25 pixels"
TwoOpenImages = "This script requires at least two open images."
ImageIsEmpty = "Image %s is empty.\nPress OK to skip the image and continue\nPress CANCEL to cancel the script entirely\n"
MoreThanOneLayer = "Image %s has more than one layer.\nPress OK to merge the visible layers.\nPress CANCEL to leave this image out of the tube.\n"
CancelQuery = "Do you wish to cancel the entire script?"
ImageIsVector = "Image %s is vector.\nPress OK to to convert to a raster layer.\nPress CANCEL to leave this image out of the tube.\n"
BackgroundNoTransparency = "Image %s is a background layer without transparency.\nPress OK to to use the image anyway.\nPress CANCEL to leave this image out of the tube.\n"
NotEnoughImages = "Not enough images left to tube."
CellLayout = "Cell Layout"
CancelAborting = "Cancel pressed - aborting"
WorkingOnDocument = "Working on document %s"
# End Translatable Strings


# AutoTubing (as described by a private beta tester):
# 1. Iterate open images and verify that each meets the requirement of being
#    a single raster layer with transparency.  If not, give the user the choice
#    of fixing the image, skipping the image, or canceling the export entirely.
#    During this phase we find the largest source image, since we need to know
#    how big the tube cells are.
# 2. Next compute the best fit for tube layout - best fit is described as closest
#    to square, but the user can override these choices in a dialog.  They number
#    of cells they create must be sufficient for the number of images that will be
#    tubed.
# 3. Create a new image big enough to hold all of the images.
# 4. For possible future editing, define the grid on the image to match the number of cells
# 5. Iterate all the images that will be in the tube, copy them to the clipboard,
#    then paste them into the tube image as a new selection.  We set the offset on the
#    paste operation so that each lands in the right spot.
# 6. Invoke the tube exporter, with cells across, cells down and total cells set properly.
#    Step size is initialized to the cell width.

class CellCountDlg(Frame):
    ''' define the dialog used to prompt the user for the number of cells'''
    def __init__( self, parent, title, NumCols, NumRows, TotalCells ):
        Frame.__init__(self)    # init our parent

        # if we exit with OK this will be set to 1.  A zero means we pressed cancel
        self.OKPressed = 0
        self.ImageCount = TotalCells
        
        # define all the variables attached to the controls
        self.GridLinesX = IntVar()
        self.GridLinesX.set( NumCols )

        self.GridLinesY = IntVar()
        self.GridLinesY.set( NumRows )

        self.PadAmount = IntVar()
        self.PadAmount.set( 2 )
        
        # define the basics of the window
        self.pack(expand=YES, fill=BOTH)
        self.master.title('Define Cells')
       
        # put some explanatory text on the window
        Label( self, text = CellArrangementPrompt,
               justify=LEFT ).pack(expand=YES, fill=BOTH, side=TOP)

        # make a subframe to hold the cells across controls
        XFrame = Frame( self )
        XLabel = Label( XFrame, text=CellsAcross, width=30 )
        XLabel.pack( expand=YES, fill=BOTH, side=LEFT )
        self.XEntry = Entry( XFrame, textvariable=self.GridLinesX )
        self.XEntry.pack( expand=YES, fill=BOTH, side=RIGHT )
        XFrame.pack(side=TOP)
        
        # do the same thing for the cells down controls
        YFrame = Frame( self )
        YLabel = Label( YFrame, text=CellsDown, width=30 )
        YLabel.pack( expand=YES, fill=BOTH, side=LEFT )
        self.YEntry = Entry( YFrame, textvariable=self.GridLinesY )
        self.YEntry.pack( expand=YES, fill=BOTH, side=RIGHT )
        YFrame.pack(side=TOP)
        
        # and finally for the cell padding
        PadFrame = Frame( self )
        PadLabel = Label( PadFrame, text=CellPadding, width=30 )
        PadLabel.pack( expand=YES, fill=BOTH, side=LEFT )
        self.PadEntry = Entry( PadFrame, textvariable=self.PadAmount )
        self.PadEntry.pack( expand=YES, fill=BOTH, side=RIGHT )
        PadFrame.pack(side=TOP)
        
        # put OK/Cancel buttons on the dialog - parts of this lifted from TkSimpleDialog
        ButtonFrame = Frame(self)
        OKButton = Button( ButtonFrame, text=OKText, width=10,
                           command=self.OnOK, default=ACTIVE )
        OKButton.pack(side=LEFT, padx=5, pady=5)
        CancelButton = Button( ButtonFrame, text=CancelText, width=10,
                               command=self.OnCancel )
        CancelButton.pack(side=LEFT, padx=5, pady=5)
        ButtonFrame.pack()
        
        self.bind('<Return>', self.OnOK)
        self.bind('<Escape>', self.OnCancel)
       

    def OnOK(self, event=None):
        ''' called by pressing the OK button - validates data, and if no error
            sets a good return code and dismisses the dialog by calling OnCancel
        '''
        try:
            X = self.GridLinesX.get()
        except ValueError:
            X = 0

        try:            
            Y = self.GridLinesY.get()
        except ValueError:
            Y = 0
        
        try:
            Pad = self.PadAmount.get()
        except ValueError:
            Pad = -1
            
        if X < 1 or X > self.ImageCount:
            App.Do( GlobalEnv, 'StartForeignWindow', { 'WindowHandle': 0 } )
            tkMessageBox.showerror( CellsAcrossInvalid,
                                    CellsAcrossCondition % self.ImageCount )
            App.Do( GlobalEnv, 'StartForeignWindow', { 'WindowHandle': int(self.winfo_id()) } )
            return 
        if Y < 1 or Y > self.ImageCount:
            App.Do( GlobalEnv, 'StartForeignWindow', { 'WindowHandle': 0 } )
            tkMessageBox.showerror( CellsDownInvalid,
                                    CellsDownCondition % self.ImageCount )
            App.Do( GlobalEnv, 'StartForeignWindow', { 'WindowHandle': int(self.winfo_id()) } )
            return
        if X * Y < self.ImageCount:
            App.Do( GlobalEnv, 'StartForeignWindow', { 'WindowHandle': 0 } )
            tkMessageBox.showerror( NotEnoughCells,
                                    CellCountCondition % self.ImageCount )
            App.Do( GlobalEnv, 'StartForeignWindow', { 'WindowHandle': int(self.winfo_id()) } )
            return
        if X * Y > self.ImageCount + X:
            MaxDown = self.ImageCount / X
            if self.ImageCount % X != 0:
                MaxDown += 1
            App.Do( GlobalEnv, 'StartForeignWindow', { 'WindowHandle': 0 } )
            tkMessageBox.showerror( TooManyCells,
                                    TooManyCellsCondition % MaxDown )
            App.Do( GlobalEnv, 'StartForeignWindow', { 'WindowHandle': int(self.winfo_id()) } )
            return
        if Pad < 0 or Pad > 25:
            App.Do( GlobalEnv, 'StartForeignWindow', { 'WindowHandle': 0 } )
            tkMessageBox.showerror( PadAmountInvalid,
                                    PadAmountCondition )
            App.Do( GlobalEnv, 'StartForeignWindow', { 'WindowHandle': int(self.winfo_id()) } )
            return
        
        # if we got here we passed validation
        self.OKPressed = 1
        
        # finish by pressing the Cancel button
        self.OnCancel()

    def OnCancel(self, event=None):
        # on cancel we simply terminate the message loop
        self.quit()

def Do(Environment):
    global GlobalEnv
    GlobalEnv = Environment
    
    # we need at least two images for this to work
    if len(App.Documents) < 2:
        App.Do(Environment,  'MsgBox', {
                'Buttons': App.Constants.MsgButtons.OK, 
                'Icon': App.Constants.MsgIcons.Stop, 
                'Text': TwoOpenImages
                })
        return  
    
    # 1. Iterate open images and verify that each meets the requirement of being
    #    a single raster layer with transparency.  If not, give the user the choice
    #    of fixing the image, skipping the image, or canceling the export entirely.
    #    During this phase we find the largest source image, since we need to know
    #    how big the tube cells are.
        
    CellHeight = 0
    CellWidth = 0
    IncludedDocs = []
    
    # all of these formats have palettes, and tubes have to be true color
    PalettedFormats = [ App.Constants.PixelFormat.Index1, App.Constants.PixelFormat.Index4,
                        App.Constants.PixelFormat.Index8, App.Constants.PixelFormat.Grey,
                        App.Constants.PixelFormat.GreyA ]
    
    for Doc in App.Documents:
        DocInfo = App.Do( Environment, 'ReturnImageInfo', {}, Doc )
        LayerInfo = App.Do( Environment, 'ReturnLayerProperties', {}, Doc )
        ColorsInDoc = App.Do( Environment, 'CountImageColors', {
                                'GeneralSettings': {
                                    'ExecutionMode': App.Constants.ExecutionMode.Silent, 
                                    'AutoActionMode': App.Constants.AutoActionMode.Match
                                    }
                                })


        LayerCount = DocInfo[ 'LayerNum' ]
        LayerType = LayerInfo[ 'LayerType' ]
        PixelFormat = DocInfo[ 'PixelFormat' ]
        IncludeDoc = 0
        if ColorsInDoc == 0:    # an empty image will fail on paste
            MsgText = ImageIsEmpty % Doc.Title
            result = App.Do(Environment,  'MsgBox', {
                        'Buttons': App.Constants.MsgButtons.OKCancel, 
                        'Icon': App.Constants.MsgIcons.Question, 
                        'Text': MsgText
                        })
            if result == 0: # cancel script
                return  # give up
        elif LayerCount != 1:
            MsgText = MoreThanOneLayer % Doc.Title
            result = App.Do(Environment,  'MsgBox', {
                        'Buttons': App.Constants.MsgButtons.OKCancel, 
                        'Icon': App.Constants.MsgIcons.Question, 
                        'Text': MsgText
                        })
            if result == 1:  # OK button
                App.Do( Environment, 'LayerMergeVisible', {}, Doc )
                IncludeDoc = 1
            elif result == 0: # Cancel button
                result = App.Do(Environment,  'MsgBox', {
                            'Buttons': App.Constants.MsgButtons.YesNo, 
                            'Icon': App.Constants.MsgIcons.Question, 
                            'Text': CancelQuery
                            })
                if result == 1:
                    return  # give up
                
        elif LayerType == App.Constants.LayerType.Vector:
            MsgText = ImageIsVector % Doc.Title
            result = App.Do(Environment,  'MsgBox', {
                        'Buttons': App.Constants.MsgButtons.OKCancel, 
                        'Icon': App.Constants.MsgIcons.Question, 
                        'Text': MsgText
                        })
            if result == 1:  # OK button
                App.Do( Environment, 'LayerConvertToRaster', {}, Doc )
                IncludeDoc = 1
            elif result == 0: # cancel button
                result = App.Do(Environment,  'MsgBox', {
                            'Buttons': App.Constants.MsgButtons.YesNo, 
                            'Icon': App.Constants.MsgIcons.Question, 
                            'Text': CancelQuery
                            })
                if result == 1:
                    return  # give up
                
        elif PixelFormat != App.Constants.PixelFormat.BGRA and \
             PixelFormat != App.Constants.PixelFormat.GreyA:
            MsgText = BackgroundNoTransparency % Doc.Title
            result = App.Do(Environment,  'MsgBox', {
                        'Buttons': App.Constants.MsgButtons.OKCancel, 
                        'Icon': App.Constants.MsgIcons.Question, 
                        'Text': MsgText
                        })
            if result == 1:  # OK button
                if PixelFormat in PalettedFormats:    # increase color depth if needed
                    App.Do(Environment, 'IncreaseColorsTo16Million', {}, Doc )
                IncludeDoc = 1  # use the doc anyway
            elif result == 0: # no button
                result = App.Do(Environment,  'MsgBox', {
                            'Buttons': App.Constants.MsgButtons.YesNo, 
                            'Icon': App.Constants.MsgIcons.Question, 
                            'Text': CancelQuery
                            })
                if result == 1:
                    return  # give up
        else:
            IncludeDoc = 1  # image was fine just the way it is

        # if we are going to use this document add it to our list and
        # update the needed cell size
        if IncludeDoc:
            IncludedDocs.append( Doc )
            CellHeight = max( CellHeight, Doc.Height )
            CellWidth = max( CellWidth, Doc.Width )

    if len(IncludedDocs) < 2:
        App.Do(Environment,  'MsgBox', {
                'Buttons': App.Constants.MsgButtons.OK, 
                'Icon': App.Constants.MsgIcons.Stop, 
                'Text': NotEnoughImages
                })
        return  # not enough images left

    # 2. Next compute the best fit for tube layout - best fit is described as closest
    #    to square.  We do this by computing the size of various layouts going from
    #    1 x n, 2 x n/2, 3 x n/3, all the way up to n x 1.  We watch the difference between
    #    height and width - when it starts increasing we know we found the best fit.
    OldDifference = 9999999 # init this high our initial test works
    for i in range( 1, len(IncludedDocs)+1 ):
        NumRows = i
        NumCols = (len(IncludedDocs) + i-1) / i

        Width = NumCols * CellWidth
        Height = NumRows * CellHeight
        Difference = abs( Width - Height )

        if Difference >= OldDifference:
            NumRows = i - 1
            NumCols = (len(IncludedDocs) + NumRows - 1) / NumRows
            break
        else:
            OldDifference = Difference

    # 2. Seed a dialog with the best fit choices, but let the user override. The number
    #    of cells they create must be sufficient for the number of images that will be
    #    tubed.
    # create the root TK window    
    root = Tk()
    
    # create the dialog and show the dialog
    Dlg = CellCountDlg( root, CellLayout, NumCols, NumRows, len(IncludedDocs) )
    
    # tell PSP that a foreign dialog is running.  This causes PSP to do some additional
    # work to keep the UI updating properly and to prevent the script window from going
    # behind PSP.
    App.Do( Environment, 'StartForeignWindow', { 'WindowHandle': int(Dlg.winfo_id()) } )
    
    root.mainloop()
    App.Do( Environment, 'StartForeignWindow', { 'WindowHandle': 0 } )
    root.destroy()
    
    # if the user pressed cancel in the dialog just return
    if not Dlg.OKPressed:
        print CancelAborting
        return

    # get the number of cells to use    
    PadFactor = Dlg.PadAmount.get()
    CellWidth += PadFactor
    CellHeight += PadFactor
    
    NumCols = Dlg.GridLinesX.get()
    NumRows = Dlg.GridLinesY.get()
    
    # 3. Create a new image big enough to hold all of the images.
    App.Do( Environment, 'NewFile', {
            'Width': NumCols * CellWidth, 
            'Height': NumRows * CellHeight, 
            'ColorDepth': App.Constants.Colordepth.SixteenMillionColor, 
            'DimensionUnits': App.Constants.DimensionType.Pixels, 
            'Transparent': App.Constants.Boolean.true, 
            'VectorBackground': App.Constants.Boolean.false, 
            'GeneralSettings': {
                'ExecutionMode': App.Constants.ExecutionMode.Silent, 
                }
            })
    TubeDoc = App.ActiveDocument
    
    # 4. For possible future editing, define the grid on the image to match the cell layout
    #    We actually make the grid half the size, so that it we can snap to the center of the cell
    GridSpacingX = TubeDoc.Width / NumCols / 2
    GridSpacingY = TubeDoc.Height / NumRows / 2
        
    App.Do( Environment, 'GridGuideSnapProperties', {
            'CurrentHorzGridSpacing': GridSpacingX, 
            'CurrentVertGridSpacing': GridSpacingY, 
            'GeneralSettings': {
                'ExecutionMode': App.Constants.ExecutionMode.Silent, 
                'AutoActionMode': App.Constants.AutoActionMode.Match
                }
            }, TubeDoc )


    # 5. Iterate all the images that will be in the tube, copy them to the clipboard,
    #    then paste them into the tube image as a new selection.  We then use the mover tool
    #    to center each image in its cell.
    Row = 0
    Col = 0
    for Doc in IncludedDocs:
        if Doc.Title == TubeDoc.Title:
            continue    # skip the one we just created

        print WorkingOnDocument % Doc.Title

        # temporarily save any selection we have
        SelSaver = JascUtils.SaveSelection(Environment, Doc)

        # copy the source image to the clipboard
        App.Do( Environment, 'Copy', {
                'LayerNumber': 0, 
                'CopyToClipboard': App.Constants.Boolean.false, 
                'GeneralSettings': {
                    'ExecutionMode': App.Constants.ExecutionMode.Silent, 
                    'AutoActionMode': App.Constants.AutoActionMode.Match
                    }
                }, Doc)

        # put back any selection we saved        
        SelSaver.RestoreSelection()
        
        # now paste as a new selection, moving it to the proper spot
        # the paste will default to the center of the image, so we just
        # compute the difference between the center of the image and center
        # of the cell
        OffsetX = (Col * CellWidth + CellWidth / 2) - (TubeDoc.Width / 2)
        OffsetY = (Row * CellHeight + CellHeight / 2) - (TubeDoc.Height / 2)
        App.Do( Environment, 'PasteAsNewSelection', {
                'Offset': (OffsetX, OffsetY), 
                'GeneralSettings': {
                    'ExecutionMode': App.Constants.ExecutionMode.Default, 
                    'AutoActionMode': App.Constants.AutoActionMode.Match
                    }
                }, TubeDoc)

        # remove the selection
        App.Do( Environment, 'SelectNone', {}, TubeDoc )        

        # advance to the next column, and possibly the next row
        Col += 1
        if Col == NumCols:
            Col = 0
            Row += 1
            

    # 6. Invoke the tube exporter, with cells across, cells down and total cells set properly.
    #    Step size is initialized to the cell width.
    App.Do( Environment, 'ExportTube', {
            'FileName': 'AutoTube', 
            'NumberOfCellsAcross': NumCols, 
            'NumberOfCellsDown': NumRows, 
            'TotalNumberOfCells': len(IncludedDocs), 
            'PlacementMode': App.Constants.TubePlacementMode.Random, 
            'SelectionMode': App.Constants.TubeSelectionMode.Random, 
            'StepSize': min( CellWidth, 500 ), 
            'OverwriteIfExists': App.Constants.Boolean.true, 
            'GeneralSettings': {
                'ExecutionMode': App.Constants.ExecutionMode.Interactive, 
                'AutoActionMode': App.Constants.AutoActionMode.Match
                }
            })
