home *** CD-ROM | disk | FTP | other *** search
/ PC Extra 07 & 08 / pca1507.iso / Software / psp8 / Data1.cab / GridByCells.PspScript < prev    next >
Encoding:
Text File  |  2003-04-22  |  6.0 KB  |  156 lines

  1. from JascApp import *
  2. import JascUtils
  3. from Tkinter import *
  4. import tkMessageBox
  5.  
  6. def ScriptProperties():
  7.     return {
  8.         'Author': 'Joe Fromm (idea by Diana Todd)',
  9.         'Copyright': 'Copyright (C) 2002-2003, Jasc Software Inc., All Rights Reserved. Permission to create derivate works of this script is granted provided this copyright notice is included',
  10.         'Description': 'Prompt for the number of cells in an image, and set the grid to match',
  11.         'Host': 'Paint Shop Pro',
  12.         'Host Version': '8.00'
  13.         }
  14.  
  15. class CellCountDlg(Frame):
  16.     ''' define the dialog used to prompt the user for the number of cells'''
  17.     def __init__( self, parent, title ):
  18.         Frame.__init__(self, parent)    # init our parent
  19.  
  20.         # if we exit with OK this will be set to 1.  A zero means we pressed cancel
  21.         self.OKPressed = 0
  22.         
  23.         # define all the variables attached to the controls
  24.         self.GridLinesX = IntVar()
  25.         self.GridLinesX.set( 3 )
  26.  
  27.         self.GridLinesY = IntVar()
  28.         self.GridLinesY.set( 3 )
  29.         
  30.         # define the basics of the window
  31.         self.pack(expand=YES, fill=BOTH)
  32.         self.master.title('Define Cells')
  33.        
  34.         # put some explanatory text on the window
  35.         Label( self, text = 'Enter the number of cells to have in the grid.\n'
  36.                             'The default gridline color will be used.',
  37.                justify=LEFT ).pack(expand=YES, fill=BOTH, side=TOP)
  38.  
  39.         # make a subframe to hold the cells across controls
  40.         XFrame = Frame( self )
  41.         XLabel = Label( XFrame, text='Cells across:', width=30 )
  42.         XLabel.pack( expand=YES, fill=BOTH, side=LEFT )
  43.         self.XEntry = Entry( XFrame, textvariable=self.GridLinesX )
  44.         self.XEntry.pack( expand=YES, fill=BOTH, side=RIGHT )
  45.         XFrame.pack(side=TOP)
  46.         
  47.         # do the same thing for the cells down controls
  48.         YFrame = Frame( self )
  49.         YLabel = Label( YFrame, text='Cells down:', width=30 )
  50.         YLabel.pack( expand=YES, fill=BOTH, side=LEFT )
  51.         self.YEntry = Entry( YFrame, textvariable=self.GridLinesY )
  52.         self.YEntry.pack( expand=YES, fill=BOTH, side=RIGHT )
  53.         YFrame.pack(side=TOP)
  54.         
  55.         # put OK/Cancel buttons on the dialog - parts of this lifted from TkSimpleDialog
  56.         ButtonFrame = Frame(self)
  57.         OKButton = Button( ButtonFrame, text="OK", width=10,
  58.                            command=self.OnOK, default=ACTIVE )
  59.         OKButton.pack(side=LEFT, padx=5, pady=5)
  60.         CancelButton = Button( ButtonFrame, text="Cancel", width=10,
  61.                                command=self.OnCancel )
  62.         CancelButton.pack(side=LEFT, padx=5, pady=5)
  63.         ButtonFrame.pack()
  64.         
  65.         self.bind("<Return>", self.OnOK)
  66.         self.bind("<Escape>", self.OnCancel)
  67.        
  68.  
  69.     def OnOK(self, event=None):
  70.         ''' called by pressing the OK button - validates data, and if no error
  71.             sets a good return code and dismisses the dialog by calling OnCancel
  72.         '''
  73.         try:
  74.             X = self.GridLinesX.get()
  75.         except ValueError:
  76.             X = 0
  77.  
  78.         try:            
  79.             Y = self.GridLinesY.get()
  80.         except ValueError:
  81.             Y = 0
  82.         
  83.         if X < 1 or X > App.TargetDocument.Width - 1:
  84.             tkMessageBox.showerror( 'Cells across invalid',
  85.                                     'Cells across must be between 1 and %d' % (App.TargetDocument.Width - 1) )
  86.             return 
  87.         if Y < 1 or Y > App.TargetDocument.Height - 1:
  88.             tkMessageBox.showerror( 'Cells down invalid',
  89.                                     'Cells down must be between 1 and %d' % (App.TargetDocument.Height - 1) )
  90.             return 
  91.  
  92.         # if we got here we passed validation
  93.         self.OKPressed = 1
  94.         
  95.         # finish by pressing the Cancel button
  96.         self.OnCancel()
  97.  
  98.     def OnCancel(self, event=None):
  99.         # on cancel we simply terminate the message loop
  100.         self.quit()
  101.  
  102.  
  103. def Do(Environment):
  104.     if JascUtils.RequireADoc( Environment ) == App.Constants.Boolean.false:
  105.         return
  106.     
  107.     # create the root TK window    
  108.     root = Tk()
  109.     
  110.     # create the dialog and show the dialog
  111.     Dlg = CellCountDlg( root, 'Enter Cells')
  112.     
  113.     # tell PSP that a foreign dialog is running.  This causes PSP to do some additional
  114.     # work to keep the UI updating properly and to prevent the script window from going
  115.     # behind PSP.
  116.     App.Do( Environment, 'StartForeignWindow', { 'WindowHandle': int(root.winfo_id()) } )
  117.    
  118.     root.mainloop()
  119.     root.destroy()
  120.     
  121.     App.Do( Environment, 'StartForeignWindow', { 'WindowHandle': 0 } )
  122.     
  123.     # if the user pressed cancel in the dialog just return
  124.     if not Dlg.OKPressed:
  125.         print 'Cancel pressed - aborting'
  126.         return
  127.  
  128.     # get the number of cells to use    
  129.     CellsAcross = Dlg.GridLinesX.get()
  130.     CellsDown = Dlg.GridLinesY.get()
  131.  
  132.     # now divide those into the height and width to get the of the grid -
  133.     # any errors will accumulate on the right and bottom sides
  134.     GridSpacingX = App.TargetDocument.Width / CellsAcross
  135.     GridSpacingY = App.TargetDocument.Height / CellsDown
  136.     
  137.     print 'Setting grid for %d horizontally, %d vertically' % ( GridSpacingX, GridSpacingY)
  138.     App.Do( Environment, 'GridGuideSnapProperties', {
  139.             'CurrentHorzGridSpacing': GridSpacingX, 
  140.             'CurrentVertGridSpacing': GridSpacingY, 
  141.             'GeneralSettings': {
  142.                 'ExecutionMode': App.Constants.ExecutionMode.Silent, 
  143.                 'AutoActionMode': App.Constants.AutoActionMode.Match
  144.                 }
  145.             })
  146.  
  147.     # turn on the grid
  148.     App.Do( Environment, 'ShowGrid', {
  149.             'ShowGrid': App.Constants.ShowCommands.Show, 
  150.             'GeneralSettings': {
  151.                 'ExecutionMode': App.Constants.ExecutionMode.Default, 
  152.                 'AutoActionMode': App.Constants.AutoActionMode.Match
  153.                 }
  154.             })
  155.  
  156.