home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2010 January / maximum-cd-2010-01.iso / DiscContents / gimp-2.6.7-i686-setup.exe / {app} / lib / gimp / 2.0 / python / gimpfu,1.pyo (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2009-08-14  |  25.8 KB  |  736 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyo (Python 2.6)
  3.  
  4. '''Simple interface to writing GIMP plug-ins in Python.
  5.  
  6. Instead of worrying about all the user interaction, saving last used values
  7. and everything, the gimpfu module can take care of it for you.  It provides
  8. a simple register() function that will register your plug-in if needed, and
  9. cause your plug-in function to be called when needed.
  10.  
  11. Gimpfu will also handle showing a user interface for editing plug-in parameters
  12. if the plug-in is called interactively, and will also save the last used
  13. parameters, so the RUN_WITH_LAST_VALUES run_type will work correctly.  It
  14. will also make sure that the displays are flushed on completion if the plug-in
  15. was run interactively.
  16.  
  17. When registering the plug-in, you do not need to worry about specifying
  18. the run_type parameter.
  19.  
  20. A typical gimpfu plug-in would look like this:
  21.   from gimpfu import *
  22.  
  23.   def plugin_func(image, drawable, args):
  24.               #do what plugins do best
  25.   register(
  26.               "plugin_func",
  27.               "blurb",
  28.               "help message",
  29.               "author",
  30.               "copyright",
  31.               "year",
  32.               "My plug-in",
  33.               "*",
  34.               [
  35.                   (PF_IMAGE, "image", "Input image"),
  36.                   (PF_DRAWABLE, "drawable", "Input drawable"),
  37.                   (PF_STRING, "arg", "The argument", "default-value")
  38.               ],
  39.               [],
  40.               plugin_func, menu="<Image>/Somewhere")
  41.   main()
  42.  
  43. The call to "from gimpfu import *" will import all the gimp constants into
  44. the plug-in namespace, and also import the symbols gimp, pdb, register and
  45. main.  This should be just about all any plug-in needs.
  46.  
  47. You can use any of the PF_* constants below as parameter types, and an
  48. appropriate user interface element will be displayed when the plug-in is
  49. run in interactive mode.  Note that the the PF_SPINNER and PF_SLIDER types
  50. expect a fifth element in their description tuple -- a 3-tuple of the form
  51. (lower,upper,step), which defines the limits for the slider or spinner.
  52.  
  53. If want to localize your plug-in, add an optional domain parameter to the
  54. register call. It can be the name of the translation domain or a tuple that
  55. consists of the translation domain and the directory where the translations
  56. are installed.
  57. '''
  58. import string as _string
  59. import math
  60. import gimp
  61. import gimpcolor
  62. from gimpenums import *
  63. pdb = gimp.pdb
  64. import gettext
  65. t = gettext.translation('gimp20-python', gimp.locale_directory, fallback = True)
  66. _ = t.ugettext
  67.  
  68. class error(RuntimeError):
  69.     pass
  70.  
  71.  
  72. class CancelError(RuntimeError):
  73.     pass
  74.  
  75. PF_INT8 = PDB_INT8
  76. PF_INT16 = PDB_INT16
  77. PF_INT32 = PDB_INT32
  78. PF_INT = PF_INT32
  79. PF_FLOAT = PDB_FLOAT
  80. PF_STRING = PDB_STRING
  81. PF_VALUE = PF_STRING
  82. PF_COLOR = PDB_COLOR
  83. PF_COLOUR = PF_COLOR
  84. PF_REGION = PDB_REGION
  85. PF_DISPLAY = PDB_DISPLAY
  86. PF_IMAGE = PDB_IMAGE
  87. PF_LAYER = PDB_LAYER
  88. PF_CHANNEL = PDB_CHANNEL
  89. PF_DRAWABLE = PDB_DRAWABLE
  90. PF_VECTORS = PDB_VECTORS
  91. PF_TOGGLE = 1000
  92. PF_BOOL = PF_TOGGLE
  93. PF_SLIDER = 1001
  94. PF_SPINNER = 1002
  95. PF_ADJUSTMENT = PF_SPINNER
  96. PF_FONT = 1003
  97. PF_FILE = 1004
  98. PF_BRUSH = 1005
  99. PF_PATTERN = 1006
  100. PF_GRADIENT = 1007
  101. PF_RADIO = 1008
  102. PF_TEXT = 1009
  103. PF_PALETTE = 1010
  104. PF_FILENAME = 1011
  105. PF_DIRNAME = 1012
  106. PF_OPTION = 1013
  107. _type_mapping = {
  108.     PF_INT8: PDB_INT8,
  109.     PF_INT16: PDB_INT16,
  110.     PF_INT32: PDB_INT32,
  111.     PF_FLOAT: PDB_FLOAT,
  112.     PF_STRING: PDB_STRING,
  113.     PF_COLOR: PDB_COLOR,
  114.     PF_REGION: PDB_REGION,
  115.     PF_DISPLAY: PDB_DISPLAY,
  116.     PF_IMAGE: PDB_IMAGE,
  117.     PF_LAYER: PDB_LAYER,
  118.     PF_CHANNEL: PDB_CHANNEL,
  119.     PF_DRAWABLE: PDB_DRAWABLE,
  120.     PF_VECTORS: PDB_VECTORS,
  121.     PF_TOGGLE: PDB_INT32,
  122.     PF_SLIDER: PDB_FLOAT,
  123.     PF_SPINNER: PDB_INT32,
  124.     PF_FONT: PDB_STRING,
  125.     PF_FILE: PDB_STRING,
  126.     PF_BRUSH: PDB_STRING,
  127.     PF_PATTERN: PDB_STRING,
  128.     PF_GRADIENT: PDB_STRING,
  129.     PF_RADIO: PDB_STRING,
  130.     PF_TEXT: PDB_STRING,
  131.     PF_PALETTE: PDB_STRING,
  132.     PF_FILENAME: PDB_STRING,
  133.     PF_DIRNAME: PDB_STRING,
  134.     PF_OPTION: PDB_INT32 }
  135. _obj_mapping = {
  136.     PF_INT8: int,
  137.     PF_INT16: int,
  138.     PF_INT32: int,
  139.     PF_FLOAT: float,
  140.     PF_STRING: str,
  141.     PF_COLOR: gimpcolor.RGB,
  142.     PF_REGION: int,
  143.     PF_DISPLAY: gimp.Display,
  144.     PF_IMAGE: gimp.Image,
  145.     PF_LAYER: gimp.Layer,
  146.     PF_CHANNEL: gimp.Channel,
  147.     PF_DRAWABLE: gimp.Drawable,
  148.     PF_VECTORS: gimp.Vectors,
  149.     PF_TOGGLE: bool,
  150.     PF_SLIDER: float,
  151.     PF_SPINNER: int,
  152.     PF_FONT: str,
  153.     PF_FILE: str,
  154.     PF_BRUSH: str,
  155.     PF_PATTERN: str,
  156.     PF_GRADIENT: str,
  157.     PF_RADIO: str,
  158.     PF_TEXT: str,
  159.     PF_PALETTE: str,
  160.     PF_FILENAME: str,
  161.     PF_DIRNAME: str,
  162.     PF_OPTION: int }
  163. _registered_plugins_ = { }
  164.  
  165. def register(proc_name, blurb, help, author, copyright, date, label, imagetypes, params, results, function, menu = None, domain = None, on_query = None, on_run = None):
  166.     '''This is called to register a new plug-in.'''
  167.     
  168.     def letterCheck(str):
  169.         allowed = _string.letters + _string.digits + '_' + '-'
  170.         for ch in str:
  171.             if ch not in allowed:
  172.                 return 0
  173.         else:
  174.             return 1
  175.         return ch not in allowed
  176.  
  177.     if not letterCheck(proc_name):
  178.         raise error, 'procedure name contains illegal characters'
  179.     letterCheck(proc_name)
  180.     for ent in params:
  181.         if len(ent) < 4:
  182.             raise error, 'parameter definition must contain at least 4 elements (%s given: %s)' % (len(ent), ent)
  183.         len(ent) < 4
  184.         if type(ent[0]) != int:
  185.             raise error, 'parameter types must be integers'
  186.         type(ent[0]) != int
  187.         if not letterCheck(ent[1]):
  188.             raise error, 'parameter name contains illegal characters'
  189.         letterCheck(ent[1])
  190.     
  191.     for ent in results:
  192.         if len(ent) < 3:
  193.             raise error, 'result definition must contain at least 3 elements (%s given: %s)' % (len(ent), ent)
  194.         len(ent) < 3
  195.         if type(ent[0]) != type(42):
  196.             raise error, 'result types must be integers'
  197.         type(ent[0]) != type(42)
  198.         if not letterCheck(ent[1]):
  199.             raise error, 'result name contains illegal characters'
  200.         letterCheck(ent[1])
  201.     
  202.     plugin_type = PLUGIN
  203.     if not (proc_name[:7] == 'python-') and not (proc_name[:7] == 'python_') and not (proc_name[:10] == 'extension-') and not (proc_name[:10] == 'extension_') and not (proc_name[:8] == 'plug-in-') and not (proc_name[:8] == 'plug_in_') and not (proc_name[:5] == 'file-') and not (proc_name[:5] == 'file_'):
  204.         proc_name = 'python-fu-' + proc_name
  205.     
  206.     need_compat_params = False
  207.     if menu is None and label:
  208.         fields = label.split('/')
  209.         if fields:
  210.             label = fields.pop()
  211.             menu = '/'.join(fields)
  212.             need_compat_params = True
  213.         
  214.         if need_compat_params and plugin_type == PLUGIN:
  215.             file_params = [
  216.                 (PDB_STRING, 'filename', 'The name of the file', ''),
  217.                 (PDB_STRING, 'raw-filename', 'The name of the file', '')]
  218.             if menu is None:
  219.                 pass
  220.             elif menu[:6] == '<Load>':
  221.                 params[0:0] = file_params
  222.             elif menu[:7] == '<Image>' or menu[:6] == '<Save>':
  223.                 params.insert(0, (PDB_IMAGE, 'image', 'Input image', None))
  224.                 params.insert(1, (PDB_DRAWABLE, 'drawable', 'Input drawable', None))
  225.                 if menu[:6] == '<Save>':
  226.                     params[2:2] = file_params
  227.                 
  228.             
  229.         
  230.     
  231.     _registered_plugins_[proc_name] = (blurb, help, author, copyright, date, label, imagetypes, plugin_type, params, results, function, menu, domain, on_query, on_run)
  232.  
  233.  
  234. def _query():
  235.     for plugin in _registered_plugins_.keys():
  236.         (blurb, help, author, copyright, date, label, imagetypes, plugin_type, params, results, function, menu, domain, on_query, on_run) = _registered_plugins_[plugin]
  237.         
  238.         def make_params(params):
  239.             return [ (_type_mapping[x[0]], x[1], _string.replace(x[2], '_', '')) for x in params ]
  240.  
  241.         params = make_params(params)
  242.         params.insert(0, (PDB_INT32, 'run-mode', 'Interactive, Non-Interactive'))
  243.         results = make_params(results)
  244.         if domain:
  245.             
  246.             try:
  247.                 (domain, locale_dir) = domain
  248.                 gimp.domain_register(domain, locale_dir)
  249.             except ValueError:
  250.                 gimp.domain_register(domain)
  251.             except:
  252.                 None<EXCEPTION MATCH>ValueError
  253.             
  254.  
  255.         None<EXCEPTION MATCH>ValueError
  256.         gimp.install_procedure(plugin, blurb, help, author, copyright, date, label, imagetypes, plugin_type, params, results)
  257.         if menu:
  258.             gimp.menu_register(plugin, menu)
  259.         
  260.         if on_query:
  261.             on_query()
  262.             continue
  263.     
  264.  
  265.  
  266. def _get_defaults(proc_name):
  267.     import gimpshelf
  268.     (blurb, help, author, copyright, date, label, imagetypes, plugin_type, params, results, function, menu, domain, on_query, on_run) = _registered_plugins_[proc_name]
  269.     key = 'python-fu-save--' + proc_name
  270.     if gimpshelf.shelf.has_key(key):
  271.         return gimpshelf.shelf[key]
  272.     return [ x[3] for x in params ]
  273.  
  274.  
  275. def _set_defaults(proc_name, defaults):
  276.     import gimpshelf
  277.     key = 'python-fu-save--' + proc_name
  278.     gimpshelf.shelf[key] = defaults
  279.  
  280.  
  281. def _interact(proc_name, start_params):
  282.     (blurb, help, author, copyright, date, label, imagetypes, plugin_type, params, results, function, menu, domain, on_query, on_run) = _registered_plugins_[proc_name]
  283.     
  284.     def run_script(run_params):
  285.         params = start_params + tuple(run_params)
  286.         _set_defaults(proc_name, params)
  287.         return apply(function, params)
  288.  
  289.     params = params[len(start_params):]
  290.     if len(params) == 0:
  291.         return run_script([])
  292.     import pygtk
  293.     pygtk.require('2.0')
  294.     import gimpui
  295.     import gtk
  296.     defaults = _get_defaults(proc_name)
  297.     defaults = defaults[len(start_params):]
  298.     
  299.     class EntryValueError(Exception):
  300.         pass
  301.  
  302.     
  303.     def warning_dialog(parent, primary, secondary = (None,)):
  304.         dlg = gtk.MessageDialog(parent, gtk.DIALOG_DESTROY_WITH_PARENT, gtk.MESSAGE_WARNING, gtk.BUTTONS_CLOSE, primary)
  305.         if secondary:
  306.             dlg.format_secondary_text(secondary)
  307.         
  308.         dlg.run()
  309.         dlg.destroy()
  310.  
  311.     
  312.     def error_dialog(parent, proc_name):
  313.         import sys
  314.         import traceback
  315.         exc_str = exc_only_str = _('Missing exception information')
  316.         
  317.         try:
  318.             (etype, value, tb) = sys.exc_info()
  319.             exc_str = ''.join(traceback.format_exception(etype, value, tb))
  320.             exc_only_str = ''.join(traceback.format_exception_only(etype, value))
  321.         finally:
  322.             etype = None
  323.             value = None
  324.             tb = None
  325.  
  326.         title = _('An error occured running %s') % proc_name
  327.         dlg = gtk.MessageDialog(parent, gtk.DIALOG_DESTROY_WITH_PARENT, gtk.MESSAGE_ERROR, gtk.BUTTONS_CLOSE, title)
  328.         dlg.format_secondary_text(exc_only_str)
  329.         alignment = gtk.Alignment(0, 0, 1, 1)
  330.         alignment.set_padding(0, 0, 12, 12)
  331.         dlg.vbox.pack_start(alignment)
  332.         alignment.show()
  333.         expander = gtk.Expander(_('_More Information'))
  334.         expander.set_use_underline(True)
  335.         expander.set_spacing(6)
  336.         alignment.add(expander)
  337.         expander.show()
  338.         scrolled = gtk.ScrolledWindow()
  339.         scrolled.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC)
  340.         scrolled.set_size_request(-1, 200)
  341.         expander.add(scrolled)
  342.         scrolled.show()
  343.         label = gtk.Label(exc_str)
  344.         label.set_alignment(0, 0)
  345.         label.set_padding(6, 6)
  346.         label.set_selectable(True)
  347.         scrolled.add_with_viewport(label)
  348.         label.show()
  349.         
  350.         def response(widget, id):
  351.             widget.destroy()
  352.  
  353.         dlg.connect('response', response)
  354.         dlg.set_resizable(True)
  355.         dlg.show()
  356.  
  357.     
  358.     class StringEntry((gtk.Entry,)):
  359.         
  360.         def __init__(self, default = ('',)):
  361.             gtk.Entry.__init__(self)
  362.             self.set_text(str(default))
  363.  
  364.         
  365.         def get_value(self):
  366.             return self.get_text()
  367.  
  368.  
  369.     
  370.     class TextEntry((gtk.ScrolledWindow,)):
  371.         
  372.         def __init__(self, default = ('',)):
  373.             gtk.ScrolledWindow.__init__(self)
  374.             self.set_shadow_type(gtk.SHADOW_IN)
  375.             self.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC)
  376.             self.set_size_request(100, -1)
  377.             self.view = gtk.TextView()
  378.             self.add(self.view)
  379.             self.view.show()
  380.             self.buffer = self.view.get_buffer()
  381.             self.set_value(str(default))
  382.  
  383.         
  384.         def set_value(self, text):
  385.             self.buffer.set_text(text)
  386.  
  387.         
  388.         def get_value(self):
  389.             return self.buffer.get_text(self.buffer.get_start_iter(), self.buffer.get_end_iter())
  390.  
  391.  
  392.     
  393.     class IntEntry((StringEntry,)):
  394.         
  395.         def get_value(self):
  396.             
  397.             try:
  398.                 return int(self.get_text())
  399.             except ValueError:
  400.                 e = None
  401.                 raise EntryValueError, e.args
  402.  
  403.  
  404.  
  405.     
  406.     class FloatEntry((StringEntry,)):
  407.         
  408.         def get_value(self):
  409.             
  410.             try:
  411.                 return float(self.get_text())
  412.             except ValueError:
  413.                 e = None
  414.                 raise EntryValueError, e.args
  415.  
  416.  
  417.  
  418.     
  419.     def precision(step):
  420.         if math.fabs(step) >= 1 or step == 0:
  421.             digits = 0
  422.         else:
  423.             digits = abs(math.floor(math.log10(math.fabs(step))))
  424.         if digits > 20:
  425.             digits = 20
  426.         
  427.         return int(digits)
  428.  
  429.     
  430.     class SliderEntry('SliderEntry', (gtk.HScale,)):
  431.         
  432.         def __init__(self, default = None, bounds = (0, (0, 100, 5))):
  433.             step = bounds[2]
  434.             self.adj = gtk.Adjustment(default, bounds[0], bounds[1], step, 10 * step, 0)
  435.             gtk.HScale.__init__(self, self.adj)
  436.             self.set_digits(precision(step))
  437.  
  438.         
  439.         def get_value(self):
  440.             return self.adj.value
  441.  
  442.  
  443.     
  444.     class SpinnerEntry('SpinnerEntry', (gtk.SpinButton,)):
  445.         
  446.         def __init__(self, default = None, bounds = (0, (0, 100, 5))):
  447.             step = bounds[2]
  448.             self.adj = gtk.Adjustment(default, bounds[0], bounds[1], step, 10 * step, 0)
  449.             gtk.SpinButton.__init__(self, self.adj, step, precision(step))
  450.  
  451.  
  452.     
  453.     class ToggleEntry((gtk.ToggleButton,)):
  454.         
  455.         def __init__(self, default = (0,)):
  456.             gtk.ToggleButton.__init__(self)
  457.             self.label = gtk.Label(_('No'))
  458.             self.add(self.label)
  459.             self.label.show()
  460.             self.connect('toggled', self.changed)
  461.             self.set_active(default)
  462.  
  463.         
  464.         def changed(self, tog):
  465.             if tog.get_active():
  466.                 self.label.set_text(_('Yes'))
  467.             else:
  468.                 self.label.set_text(_('No'))
  469.  
  470.         
  471.         def get_value(self):
  472.             return self.get_active()
  473.  
  474.  
  475.     
  476.     class RadioEntry((gtk.VBox,)):
  477.         
  478.         def __init__(self, default = 0, items = (((_('Yes'), 1), (_('No'), 0)),)):
  479.             gtk.VBox.__init__(self, homogeneous = False, spacing = 2)
  480.             button = None
  481.             for label, value in items:
  482.                 button = gtk.RadioButton(button, label)
  483.                 self.pack_start(button)
  484.                 button.show()
  485.                 button.connect('toggled', self.changed, value)
  486.                 if value == default:
  487.                     button.set_active(True)
  488.                     self.active_value = value
  489.                     continue
  490.             
  491.  
  492.         
  493.         def changed(self, radio, value):
  494.             if radio.get_active():
  495.                 self.active_value = value
  496.             
  497.  
  498.         
  499.         def get_value(self):
  500.             return self.active_value
  501.  
  502.  
  503.     
  504.     class ComboEntry((gtk.ComboBox,)):
  505.         
  506.         def __init__(self, default = 0, items = ((),)):
  507.             store = gtk.ListStore(str)
  508.             for item in items:
  509.                 store.append([
  510.                     item])
  511.             
  512.             gtk.ComboBox.__init__(self, model = store)
  513.             cell = gtk.CellRendererText()
  514.             self.pack_start(cell)
  515.             self.set_attributes(cell, text = 0)
  516.             self.set_active(default)
  517.  
  518.         
  519.         def get_value(self):
  520.             return self.get_active()
  521.  
  522.  
  523.     
  524.     def FileSelector(default = (None, '')):
  525.         if default and default.endswith('/'):
  526.             selector = DirnameSelector
  527.             if default == '/':
  528.                 default = ''
  529.             
  530.         else:
  531.             selector = FilenameSelector
  532.         return selector(default)
  533.  
  534.     
  535.     class FilenameSelector((gtk.FileChooserButton,)):
  536.         
  537.         def __init__(self, default = '', save_mode = (False,)):
  538.             gtk.FileChooserButton.__init__(self, _('Python-Fu File Selection'))
  539.             self.set_action(gtk.FILE_CHOOSER_ACTION_OPEN)
  540.             if default:
  541.                 self.set_filename(default)
  542.             
  543.  
  544.         
  545.         def get_value(self):
  546.             return self.get_filename()
  547.  
  548.  
  549.     
  550.     class DirnameSelector((gtk.FileChooserButton,)):
  551.         
  552.         def __init__(self, default = ('',)):
  553.             gtk.FileChooserButton.__init__(self, _('Python-Fu Folder Selection'))
  554.             self.set_action(gtk.FILE_CHOOSER_ACTION_SELECT_FOLDER)
  555.             if default:
  556.                 self.set_filename(default)
  557.             
  558.  
  559.         
  560.         def get_value(self):
  561.             return self.get_filename()
  562.  
  563.  
  564.     _edit_mapping = {
  565.         PF_INT8: IntEntry,
  566.         PF_INT16: IntEntry,
  567.         PF_INT32: IntEntry,
  568.         PF_FLOAT: FloatEntry,
  569.         PF_STRING: StringEntry,
  570.         PF_COLOR: gimpui.ColorSelector,
  571.         PF_REGION: IntEntry,
  572.         PF_IMAGE: gimpui.ImageSelector,
  573.         PF_LAYER: gimpui.LayerSelector,
  574.         PF_CHANNEL: gimpui.ChannelSelector,
  575.         PF_DRAWABLE: gimpui.DrawableSelector,
  576.         PF_VECTORS: gimpui.VectorsSelector,
  577.         PF_TOGGLE: ToggleEntry,
  578.         PF_SLIDER: SliderEntry,
  579.         PF_SPINNER: SpinnerEntry,
  580.         PF_RADIO: RadioEntry,
  581.         PF_OPTION: ComboEntry,
  582.         PF_FONT: gimpui.FontSelector,
  583.         PF_FILE: FileSelector,
  584.         PF_FILENAME: FilenameSelector,
  585.         PF_DIRNAME: DirnameSelector,
  586.         PF_BRUSH: gimpui.BrushSelector,
  587.         PF_PATTERN: gimpui.PatternSelector,
  588.         PF_GRADIENT: gimpui.GradientSelector,
  589.         PF_PALETTE: gimpui.PaletteSelector,
  590.         PF_TEXT: TextEntry }
  591.     if on_run:
  592.         on_run()
  593.     
  594.     tooltips = gtk.Tooltips()
  595.     dialog = gimpui.Dialog(proc_name, 'python-fu', None, 0, None, proc_name, (gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL, gtk.STOCK_OK, gtk.RESPONSE_OK))
  596.     dialog.set_alternative_button_order((gtk.RESPONSE_OK, gtk.RESPONSE_CANCEL))
  597.     dialog.set_transient()
  598.     vbox = gtk.VBox(False, 12)
  599.     vbox.set_border_width(12)
  600.     dialog.vbox.pack_start(vbox)
  601.     vbox.show()
  602.     if blurb:
  603.         if domain:
  604.             
  605.             try:
  606.                 (domain, locale_dir) = domain
  607.                 trans = gettext.translation(domain, locale_dir, fallback = True)
  608.             except ValueError:
  609.                 trans = gettext.translation(domain, fallback = True)
  610.  
  611.             blurb = trans.ugettext(blurb)
  612.         
  613.         box = gimpui.HintBox(blurb)
  614.         vbox.pack_start(box, expand = False)
  615.         box.show()
  616.     
  617.     table = gtk.Table(len(params), 2, False)
  618.     table.set_row_spacings(6)
  619.     table.set_col_spacings(6)
  620.     vbox.pack_start(table, expand = False)
  621.     table.show()
  622.     
  623.     def response(dlg, id):
  624.         if id == gtk.RESPONSE_OK:
  625.             dlg.set_response_sensitive(gtk.RESPONSE_OK, False)
  626.             dlg.set_response_sensitive(gtk.RESPONSE_CANCEL, False)
  627.             params = []
  628.             
  629.             try:
  630.                 for wid in edit_wids:
  631.                     params.append(wid.get_value())
  632.             except EntryValueError:
  633.                 warning_dialog(dialog, _("Invalid input for '%s'") % wid.desc)
  634.  
  635.             
  636.             try:
  637.                 dialog.res = run_script(params)
  638.             except Exception:
  639.                 dlg.set_response_sensitive(gtk.RESPONSE_CANCEL, True)
  640.                 error_dialog(dialog, proc_name)
  641.                 raise 
  642.             except:
  643.                 None<EXCEPTION MATCH>Exception
  644.             
  645.  
  646.         None<EXCEPTION MATCH>Exception
  647.         gtk.main_quit()
  648.  
  649.     dialog.connect('response', response)
  650.     edit_wids = []
  651.     for i in range(len(params)):
  652.         pf_type = params[i][0]
  653.         name = params[i][1]
  654.         desc = params[i][2]
  655.         def_val = defaults[i]
  656.         label = gtk.Label(desc)
  657.         label.set_use_underline(True)
  658.         label.set_alignment(0, 0.5)
  659.         table.attach(label, 1, 2, i, i + 1, xoptions = gtk.FILL)
  660.         label.show()
  661.         if pf_type in (PF_SPINNER, PF_SLIDER, PF_RADIO, PF_OPTION):
  662.             wid = _edit_mapping[pf_type](def_val, params[i][4])
  663.         else:
  664.             wid = _edit_mapping[pf_type](def_val)
  665.         label.set_mnemonic_widget(wid)
  666.         table.attach(wid, 2, 3, i, i + 1, yoptions = 0)
  667.         if pf_type != PF_TEXT:
  668.             tooltips.set_tip(wid, desc, None)
  669.         else:
  670.             tooltips.set_tip(wid.view, desc, None)
  671.         wid.show()
  672.         wid.desc = desc
  673.         edit_wids.append(wid)
  674.     
  675.     progress_vbox = gtk.VBox(False, 6)
  676.     vbox.pack_end(progress_vbox, expand = False)
  677.     progress_vbox.show()
  678.     progress = gimpui.ProgressBar()
  679.     progress_vbox.pack_start(progress)
  680.     progress.show()
  681.     tooltips.enable()
  682.     dialog.show()
  683.     gtk.main()
  684.     if hasattr(dialog, 'res'):
  685.         res = dialog.res
  686.         dialog.destroy()
  687.         return res
  688.     dialog.destroy()
  689.     raise CancelError
  690.  
  691.  
  692. def _run(proc_name, params):
  693.     run_mode = params[0]
  694.     func = _registered_plugins_[proc_name][10]
  695.     if run_mode == RUN_NONINTERACTIVE:
  696.         return apply(func, params[1:])
  697.     script_params = _registered_plugins_[proc_name][8]
  698.     min_args = 0
  699.     if len(script_params) > min_args:
  700.         start_params = params[:min_args + 1]
  701.         if run_mode == RUN_WITH_LAST_VALS:
  702.             default_params = _get_defaults(proc_name)
  703.             params = start_params + default_params[min_args:]
  704.         else:
  705.             params = start_params
  706.     else:
  707.         run_mode = RUN_NONINTERACTIVE
  708.     if run_mode == RUN_INTERACTIVE:
  709.         
  710.         try:
  711.             res = _interact(proc_name, params[1:])
  712.         except CancelError:
  713.             return None
  714.         
  715.  
  716.     None<EXCEPTION MATCH>CancelError
  717.     res = apply(func, params[1:])
  718.     gimp.displays_flush()
  719.     return res
  720.  
  721.  
  722. def main():
  723.     '''This should be called after registering the plug-in.'''
  724.     gimp.main(None, None, _query, _run)
  725.  
  726.  
  727. def fail(msg):
  728.     '''Display and error message and quit'''
  729.     gimp.message(msg)
  730.     raise error, msg
  731.  
  732.  
  733. def N_(message):
  734.     return message
  735.  
  736.