1#!/usr/bin/env python
2# -*- Mode: Python; py-indent-offset: 4 -*-
3# vim: tabstop=4 shiftwidth=4 expandtab
4#
5# Copyright (C) 2010 Red Hat, Inc., John (J5) Palmieri <johnp@redhat.com>
6#
7# This library is free software; you can redistribute it and/or
8# modify it under the terms of the GNU Lesser General Public
9# License as published by the Free Software Foundation; either
10# version 2.1 of the License, or (at your option) any later version.
11#
12# This library is distributed in the hope that it will be useful,
13# but WITHOUT ANY WARRANTY; without even the implied warranty of
14# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15# Lesser General Public License for more details.
16#
17# You should have received a copy of the GNU Lesser General Public
18# License along with this library; if not, write to the Free Software
19# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301
20# USA
21
22title = "Color Selector"
23description = """
24 GtkColorSelection lets the user choose a color. GtkColorSelectionDialog is
25 a prebuilt dialog containing a GtkColorSelection.
26 """
27
28
29from gi.repository import Gtk, Gdk
30
31
32class ColorSelectorApp:
33    def __init__(self):
34        # FIXME: we should allow Gdk.Color to be allocated without parameters
35        #        Also color doesn't seem to work
36        self.color = Gdk.RGBA()
37        self.color.red = 0
38        self.color.blue = 1
39        self.color.green = 0
40        self.color.alpha = 1
41
42        self.window = Gtk.Window()
43        self.window.set_title('Color Selection')
44        self.window.set_border_width(8)
45        self.window.connect('destroy', lambda w: Gtk.main_quit())
46
47        vbox = Gtk.VBox(homogeneous=False,
48                        spacing=8)
49        vbox.set_border_width(8)
50        self.window.add(vbox)
51
52        # create color swatch area
53        frame = Gtk.Frame()
54        frame.set_shadow_type(Gtk.ShadowType.IN)
55        vbox.pack_start(frame, True, True, 0)
56
57        self.da = Gtk.DrawingArea()
58        self.da.connect('draw', self.draw_cb)
59
60        # set a minimum size
61        self.da.set_size_request(200, 200)
62        # set the color
63        self.da.override_background_color(0, self.color)
64        frame.add(self.da)
65
66        alignment = Gtk.Alignment(xalign=1.0,
67                                  yalign=0.5,
68                                  xscale=0.0,
69                                  yscale=0.0)
70
71        button = Gtk.Button(label='_Change the above color',
72                            use_underline=True)
73        alignment.add(button)
74        vbox.pack_start(alignment, False, False, 0)
75
76        button.connect('clicked', self.change_color_cb)
77
78        self.window.show_all()
79
80    def draw_cb(self, widget, cairo_ctx):
81        style = widget.get_style_context()
82        bg_color = style.get_background_color(0)
83        Gdk.cairo_set_source_rgba(cairo_ctx, bg_color)
84        cairo_ctx.paint()
85
86        return True
87
88    def change_color_cb(self, button):
89        dialog = Gtk.ColorSelectionDialog(title='Changing color')
90        dialog.set_transient_for(self.window)
91
92        colorsel = dialog.get_color_selection()
93        colorsel.set_previous_rgba(self.color)
94        colorsel.set_current_rgba(self.color)
95        colorsel.set_has_palette(True)
96
97        response = dialog.run()
98
99        if response == Gtk.ResponseType.OK:
100            self.color = colorsel.get_current_rgba()
101            self.da.override_background_color(0, self.color)
102
103        dialog.destroy()
104
105
106def main(demoapp=None):
107    ColorSelectorApp()
108    Gtk.main()
109
110
111if __name__ == '__main__':
112    main()
113