summaryrefslogtreecommitdiff
path: root/demos/gtk-demo/demos/colorselector.py
blob: 514718b7f77d90f9a7b89045d1587605b8ce275e (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
#!/usr/bin/env python
# -*- Mode: Python; py-indent-offset: 4 -*-
# vim: tabstop=4 shiftwidth=4 expandtab
#
# Copyright (C) 2010 Red Hat, Inc., John (J5) Palmieri <johnp@redhat.com>
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This library 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
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301
# USA

title = "Color Selector"
description = """
 GtkColorSelection lets the user choose a color. GtkColorSelectionDialog is
 a prebuilt dialog containing a GtkColorSelection.
 """

# See FIXME's
is_fully_bound = False

from gi.repository import Gtk, Gdk

class ColorSelectorApp:
    def __init__(self):
        # FIXME: we should allow Gdk.Color to be allocated without parameters
        #        Also color doesn't seem to work
        self.color = Gdk.Color(0, 65535, 0)

        self.window = Gtk.Window()
        self.window.set_title('Color Selection')
        self.window.set_border_width(8)
        self.window.connect('destroy', lambda w: Gtk.main_quit())

        vbox = Gtk.VBox(homogeneous = False,
                        spacing = 8)
        vbox.set_border_width(8)
        self.window.add(vbox)

        # create color swatch area
        frame = Gtk.Frame()
        frame.set_shadow_type(Gtk.ShadowType.IN)
        vbox.pack_start(frame, True, True, 0)

        self.da = Gtk.DrawingArea()
        self.da.connect('expose_event', self.expose_event_cb)

        # set a minimum size
        self.da.set_size_request(200, 200)
        # set the color
        self.da.modify_bg(Gtk.StateType.NORMAL, self.color)
        frame.add(self.da)

        alignment = Gtk.Alignment(xalign=1.0,
                                 yalign=0.5,
                                 xscale=0.0,
                                 yscale=0.0)

        button = Gtk.Button(label='_Change the above color',
                            use_underline=True)
        alignment.add(button)
        vbox.pack_start(alignment, False, False, 0)

        button.connect('clicked', self.change_color_cb)

        self.window.show_all()

    def expose_event_cb(self, widget, event):
        window = widget.get_window()

        if window:

            style = widget.get_style()
            x = event.expose.area.x
            y = event.expose.area.y
            width = event.expose.area.width
            height = event.expose.area.height

            """
            Gdk.draw_rectangle(window,
                                # FIXME: accessing style attribute segfaults
                               style.bg_gc[Gtk.StateType.NORMAL],
                               True,
                               event.expose.area.x, event.expose.area.y,
                               event.expose.area.width, event.expose.area.height)
            """
        return True


    def change_color_cb(self, button):
        dialog = Gtk.ColorSelectionDialog(title='Changing color')
        dialog.set_transient_for(self.window)

        colorsel = dialog.get_color_selection()
        colorsel.set_previous_color(self.color)
        colorsel.set_current_color(self.color)
        colorsel.set_has_palette(True)

        response = dialog.run()

        if response == Gtk.ResponseType.OK:
            self.color = colorsel.get_current_color()
            self.da.modify_bg(Gtk.StateType.NORMAL, self.color)

        dialog.destroy()

def main(demoapp=None):
    app = ColorSelectorApp()
    Gtk.main()

if __name__ == '__main__':
    main()