1# Gimp-Python - allows the writing of Gimp plugins in Python. 2# Copyright (C) 1997 James Henstridge <james@daa.com.au> 3# 4# This program is free software: you can redistribute it and/or modify 5# it under the terms of the GNU General Public License as published by 6# the Free Software Foundation; either version 3 of the License, or 7# (at your option) any later version. 8# 9# This program is distributed in the hope that it will be useful, 10# but WITHOUT ANY WARRANTY; without even the implied warranty of 11# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12# GNU General Public License for more details. 13# 14# You should have received a copy of the GNU General Public License 15# along with this program. If not, see <https://www.gnu.org/licenses/>. 16 17'''This module implements the UI items found in the libgimpui library. 18It requires pygtk to work. These functions take use to callbacks -- one 19is a constraint function, and the other is the callback object. The 20constraint function takes an image object as its first argument, and 21a drawable object as its second if appropriate. The callback functions 22get the selected object as their first argument, and the user data as 23the second. 24 25It also implements a number of selector widgets, which can be used to select 26various gimp data types. Each of these selectors takes default as an argument 27to the constructor, and has a get_value() method for retrieving the result. 28''' 29 30import pygtk 31pygtk.require('2.0') 32 33import gtk, gobject, gimp, gimpcolor 34 35from _gimpui import * 36 37import gettext 38t = gettext.translation('gimp20-python', gimp.locale_directory, fallback=True) 39_ = t.ugettext 40 41def _callbackWrapper(menu_item, callback, data): 42 callback(menu_item.get_data("Gimp-ID"), data) 43 44def _createMenu(items, callback, data): 45 menu = gtk.Menu() 46 if not items: 47 items = [("(none)", None)] 48 for label, id in items: 49 menu_item = gtk.MenuItem(label) 50 menu_item.set_data("Gimp-ID", id) 51 menu.add(menu_item) 52 if callback: 53 menu_item.connect("activate", _callbackWrapper, 54 callback, data) 55 menu_item.show() 56 return menu 57 58 59def ImageMenu(constraint=None, callback=None, data=None): 60 items = [] 61 for img in gimp.image_list(): 62 if constraint and not constraint(img): 63 continue 64 if not img.filename: 65 filename = img.name 66 else: 67 filename = img.filename 68 items.append((filename, img)) 69 items.sort() 70 return _createMenu(items, callback, data) 71 72def LayerMenu(constraint=None, callback=None, data=None): 73 items = [] 74 for img in gimp.image_list(): 75 filename = img.filename 76 if not filename: 77 filename = img.name 78 for layer in img.layers: 79 if constraint and not constraint(img, layer): 80 continue 81 name = filename + "/" + layer.name 82 items.append((name, layer)) 83 items.sort() 84 return _createMenu(items, callback, data) 85 86def ChannelMenu(constraint=None, callback=None, data=None): 87 items = [] 88 for img in gimp.image_list(): 89 filename = img.filename 90 if not filename: 91 filename = img.name 92 for channel in img.channels: 93 if constraint and not constraint(img, channel): 94 continue 95 name = filename + "/" + channel.name 96 items.append((name, channel)) 97 items.sort() 98 return _createMenu(items, callback, data) 99 100def DrawableMenu(constraint=None, callback=None, data=None): 101 items = [] 102 for img in gimp.image_list(): 103 filename = img.filename 104 if not filename: 105 filename = img.name 106 for drawable in img.layers + img.channels: 107 if constraint and not constraint(img, drawable): 108 continue 109 name = filename + "/" + drawable.name 110 items.append((name, drawable)) 111 items.sort() 112 return _createMenu(items, callback, data) 113 114def VectorsMenu(constraint=None, callback=None, data=None): 115 items = [] 116 for img in gimp.image_list(): 117 filename = img.filename 118 if not filename: 119 filename = img.name 120 for vectors in img.vectors: 121 if constraint and not constraint(img, vectors): 122 continue 123 name = filename + "/" + vectors.name 124 items.append((name, vectors)) 125 items.sort() 126 return _createMenu(items, callback, data) 127 128class ImageSelector(ImageComboBox): 129 def __init__(self, default=None): 130 ImageComboBox.__init__(self) 131 if default is not None: 132 self.set_active_image(default) 133 def get_value(self): 134 return self.get_active_image() 135 136class LayerSelector(LayerComboBox): 137 def __init__(self, default=None): 138 LayerComboBox.__init__(self) 139 if default is not None: 140 self.set_active_layer(default) 141 def get_value(self): 142 return self.get_active_layer() 143 144class ChannelSelector(ChannelComboBox): 145 def __init__(self, default=None): 146 ChannelComboBox.__init__(self) 147 if default is not None: 148 self.set_active_channel(default) 149 def get_value(self): 150 return self.get_active_channel() 151 152class DrawableSelector(DrawableComboBox): 153 def __init__(self, default=None): 154 DrawableComboBox.__init__(self) 155 if default is not None: 156 self.set_active_drawable(default) 157 def get_value(self): 158 return self.get_active_drawable() 159 160class VectorsSelector(VectorsComboBox): 161 def __init__(self, default=None): 162 VectorsComboBox.__init__(self) 163 if default is not None: 164 self.set_active_vectors(default) 165 def get_value(self): 166 return self.get_active_vectors() 167 168class ColorSelector(ColorButton): 169 def __init__(self, default=gimpcolor.RGB(1.0, 0, 0)): 170 if isinstance(default, gimpcolor.RGB): 171 color = default 172 elif isinstance(default, tuple): 173 color = apply(gimpcolor.RGB, default) 174 elif isinstance(default, str): 175 color = gimpcolor.rgb_parse_css(default) 176 ColorButton.__init__(self, _("Python-Fu Color Selection"), 100, 20, 177 color, COLOR_AREA_FLAT) 178 def get_value(self): 179 return self.get_color(); 180 181class PatternSelector(PatternSelectButton): 182 def __init__(self, default=""): 183 PatternSelectButton.__init__(self) 184 if default: 185 self.set_pattern(default) 186 def get_value(self): 187 return self.get_pattern() 188 189class BrushSelector(BrushSelectButton): 190 def __init__(self, default=""): 191 BrushSelectButton.__init__(self) 192 if default: 193 self.set_brush(default, -1.0, -1, -1) 194 def get_value(self): 195 return self.get_brush()[0] 196 197class GradientSelector(GradientSelectButton): 198 def __init__(self, default=""): 199 GradientSelectButton.__init__(self) 200 if default: 201 self.set_gradient(default) 202 def get_value(self): 203 return self.get_gradient() 204 205class PaletteSelector(PaletteSelectButton): 206 def __init__(self, default=""): 207 PaletteSelectButton.__init__(self) 208 if default: 209 self.set_palette(default) 210 def get_value(self): 211 return self.get_palette() 212 213class FontSelector(FontSelectButton): 214 def __init__(self, default="Sans"): 215 FontSelectButton.__init__(self) 216 if default: 217 self.set_font(default) 218 def get_value(self): 219 return self.get_font() 220 221class FileSelector(gtk.FileChooserButton): 222 def __init__(self, default=""): 223 gtk.FileChooserButton.__init__(self, _("Python-Fu File Selection")) 224 if default: 225 self.set_filename(default) 226 def get_value(self): 227 return self.get_filename() 228