summaryrefslogtreecommitdiffstats
path: root/plug-ins/pygimp/gimpui.py
blob: 76ee58472fd503081cd4deef49ec822ccf02e6d6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
#   Gimp-Python - allows the writing of Gimp plugins in Python.
#   Copyright (C) 1997  James Henstridge <james@daa.com.au>
#
#    This program is free software: you can redistribute it and/or modify
#   it under the terms of the GNU General Public License as published by
#   the Free Software Foundation; either version 3 of the License, or
#   (at your option) any later version.
#
#   This program is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#   GNU General Public License for more details.
#
#   You should have received a copy of the GNU General Public License
#   along with this program.  If not, see <https://www.gnu.org/licenses/>.

'''This module implements the UI items found in the libgimpui library.
It requires pygtk to work.  These functions take use to callbacks -- one
is a constraint function, and the other is the callback object.  The
constraint function takes an image object as its first argument, and
a drawable object as its second if appropriate.  The callback functions
get the selected object as their first argument, and the user data as
the second.

It also implements a number of selector widgets, which can be used to select
various gimp data types.  Each of these selectors takes default as an argument
to the constructor, and has a get_value() method for retrieving the result.
'''

import pygtk
pygtk.require('2.0')

import gtk, gobject, gimp, gimpcolor

from _gimpui import *

import gettext
t = gettext.translation('gimp20-python', gimp.locale_directory, fallback=True)
_ = t.ugettext

def _callbackWrapper(menu_item, callback, data):
    callback(menu_item.get_data("Gimp-ID"), data)

def _createMenu(items, callback, data):
    menu = gtk.Menu()
    if not items:
        items = [("(none)", None)]
    for label, id in items:
        menu_item = gtk.MenuItem(label)
        menu_item.set_data("Gimp-ID", id)
        menu.add(menu_item)
        if callback:
            menu_item.connect("activate", _callbackWrapper,
                              callback, data)
        menu_item.show()
    return menu


def ImageMenu(constraint=None, callback=None, data=None):
    items = []
    for img in gimp.image_list():
        if constraint and not constraint(img):
            continue
        if not img.filename:
            filename = img.name
        else:
            filename = img.filename
        items.append((filename, img))
    items.sort()
    return _createMenu(items, callback, data)

def LayerMenu(constraint=None, callback=None, data=None):
    items = []
    for img in gimp.image_list():
        filename = img.filename
        if not filename:
            filename = img.name
        for layer in img.layers:
            if constraint and not constraint(img, layer):
                continue
            name = filename + "/" + layer.name
            items.append((name, layer))
    items.sort()
    return _createMenu(items, callback, data)

def ChannelMenu(constraint=None, callback=None, data=None):
    items = []
    for img in gimp.image_list():
        filename = img.filename
        if not filename:
            filename = img.name
        for channel in img.channels:
            if constraint and not constraint(img, channel):
                continue
            name = filename + "/" + channel.name
            items.append((name, channel))
    items.sort()
    return _createMenu(items, callback, data)

def DrawableMenu(constraint=None, callback=None, data=None):
    items = []
    for img in gimp.image_list():
        filename = img.filename
        if not filename:
            filename = img.name
        for drawable in img.layers + img.channels:
            if constraint and not constraint(img, drawable):
                continue
            name = filename + "/" + drawable.name
            items.append((name, drawable))
    items.sort()
    return _createMenu(items, callback, data)

def VectorsMenu(constraint=None, callback=None, data=None):
    items = []
    for img in gimp.image_list():
        filename = img.filename
        if not filename:
            filename = img.name
        for vectors in img.vectors:
            if constraint and not constraint(img, vectors):
                continue
            name = filename + "/" + vectors.name
            items.append((name, vectors))
    items.sort()
    return _createMenu(items, callback, data)

class ImageSelector(ImageComboBox):
    def __init__(self, default=None):
        ImageComboBox.__init__(self)
        if default is not None:
            self.set_active_image(default)
    def get_value(self):
        return self.get_active_image()

class LayerSelector(LayerComboBox):
    def __init__(self, default=None):
        LayerComboBox.__init__(self)
        if default is not None:
            self.set_active_layer(default)
    def get_value(self):
        return self.get_active_layer()

class ChannelSelector(ChannelComboBox):
    def __init__(self, default=None):
        ChannelComboBox.__init__(self)
        if default is not None:
            self.set_active_channel(default)
    def get_value(self):
        return self.get_active_channel()

class DrawableSelector(DrawableComboBox):
    def __init__(self, default=None):
        DrawableComboBox.__init__(self)
        if default is not None:
            self.set_active_drawable(default)
    def get_value(self):
        return self.get_active_drawable()

class VectorsSelector(VectorsComboBox):
    def __init__(self, default=None):
        VectorsComboBox.__init__(self)
        if default is not None:
            self.set_active_vectors(default)
    def get_value(self):
        return self.get_active_vectors()

class ColorSelector(ColorButton):
    def __init__(self, default=gimpcolor.RGB(1.0, 0, 0)):
        if isinstance(default, gimpcolor.RGB):
            color = default
        elif isinstance(default, tuple):
            color = apply(gimpcolor.RGB, default)
        elif isinstance(default, str):
            color = gimpcolor.rgb_parse_css(default)
        ColorButton.__init__(self, _("Python-Fu Color Selection"), 100, 20,
                             color, COLOR_AREA_FLAT)
    def get_value(self):
        return self.get_color();

class PatternSelector(PatternSelectButton):
    def __init__(self, default=""):
        PatternSelectButton.__init__(self)
        if default:
            self.set_pattern(default)
    def get_value(self):
        return self.get_pattern()

class BrushSelector(BrushSelectButton):
    def __init__(self, default=""):
        BrushSelectButton.__init__(self)
        if default:
            self.set_brush(default, -1.0, -1, -1)
    def get_value(self):
        return self.get_brush()[0]

class GradientSelector(GradientSelectButton):
    def __init__(self, default=""):
        GradientSelectButton.__init__(self)
        if default:
            self.set_gradient(default)
    def get_value(self):
        return self.get_gradient()

class PaletteSelector(PaletteSelectButton):
    def __init__(self, default=""):
        PaletteSelectButton.__init__(self)
        if default:
            self.set_palette(default)
    def get_value(self):
        return self.get_palette()

class FontSelector(FontSelectButton):
    def __init__(self, default="Sans"):
        FontSelectButton.__init__(self)
        if default:
            self.set_font(default)
    def get_value(self):
        return self.get_font()

class FileSelector(gtk.FileChooserButton):
    def __init__(self, default=""):
        gtk.FileChooserButton.__init__(self, _("Python-Fu File Selection"))
        if default:
            self.set_filename(default)
    def get_value(self):
        return self.get_filename()