File: gtk3_chooserbutton.py

package info (click to toggle)
firewalld 0.4.4.2-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 8,212 kB
  • ctags: 3,940
  • sloc: python: 33,758; xml: 14,394; sh: 2,174; makefile: 567
file content (181 lines) | stat: -rwxr-xr-x 5,403 bytes parent folder | download | duplicates (3)
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
#!/usr/bin/python3 -Es
# -*- coding: utf-8 -*-
#
# Copyright (C) 2008,2012 Red Hat, Inc.
#
# Authors:
# Thomas Woerner <twoerner@redhat.com>
# Florian Festi <ffesti@redhat.com>
#
# 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 2 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 <http://www.gnu.org/licenses/>.
#

import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk

class ChooserButton(object):
    def __init__(self, button, default_label=""):
        self.button = button
        self.default_label = default_label

        self.label = None
        self._menu = None
        self._icon = None

        children = self.button.get_children()
        if len(children) == 1 and isinstance(children[0], (Gtk.HBox, Gtk.Box)):
            children = children[0].get_children()
            for child in children:
                if isinstance(child, Gtk.Label):
                    self.label = child
                    break
        else:
            for child in list(button.get_children()):
                button.remove(child)
            hbox = Gtk.HBox()
            self.label = Gtk.Label()
            arrow = Gtk.Arrow(arrow_type=Gtk.ArrowType.DOWN,
                              shadow_type=Gtk.ShadowType.IN)
            hbox.set_spacing(2)
            hbox.pack_start(self.label, True, True, 0)
            hbox.pack_end(arrow, False, False, 0)
            button.add(hbox)
        if not self.label:
            raise ValueError("%s is not a ChooserButton" % button.get_name())
        self.connect("clicked", self._show_menu)
        self.reset()

    def set_sensitive(self, value):
        self.button.set_sensitive(value)

    def get_sensitive(self):
        return self.button.get_sensitive()

    def is_sensitive(self):
        return self.button.is_sensitive()

    def connect(self, _type, *args):
        return self.button.connect(_type, *args)

    def disconnect(self, *args):
        self.button.disconnect(*args)

    def get_text(self):
        return self.text

    def set_text(self, text):
        if not text or len(text) < 1:
            self.reset()
        self.text = text
        self.label.set_text(self.text)

    def set_stock_icon(self, name, size=Gtk.IconSize.MENU):
        if self._icon is None:
            self._icon = Gtk.Image()
            hbox = self.button.get_child()
            hbox.pack_start(self._icon, True, True, 0)
            hbox.reorder_child(self._icon, 0)
            
        self._icon.set_from_stock(name, size)

    def reset(self):
        self.text = None
        self.label.set_text(self.default_label)

    def set_menu(self, menu):
        self._menu = menu
        if menu:
            menu.attach_to_widget(self.button, self._detach_menu)

    def get_menu(self):
        return self._menu

    def _detach_menu(self):
        self._menu = None

    def _show_menu(self, *dummy):
        if not self._menu:
            return
        self._menu.popup(None, None, self._menu_position_func, 0, 0, 0)

    def _menu_position_func(self, menu, dummy):
        allocation = self.button.get_allocation()
        req = menu.size_request()
        menu_width = req.width
        menu_height = req.height
        if menu_width != allocation.width:
            menu.set_size_request(-1, -1)
            req = menu.size_request()
            if req.width > allocation.width:
                menu.set_size_request(req.width, req.height)
            else:
                menu.set_size_request(allocation.width, -1)

        (x, y) = self.button.get_parent_window().get_origin()[1:]
        x += allocation.x
        y += allocation.y + allocation.height

        root = self.button.get_root_window()
        (dummy, dummy, dummy, root_height) = root.get_geometry()

        if y + menu_height > root_height:
            y -= menu_height + allocation.height

        return (x, y, True)


class ToolChooserButton(object):
    
    def __init__(self, button, default_label=''):
        
        self.button = button
        self.default_label = default_label

        self._menu = None
        self._icon = None

        self.reset()

        self.set_sensitive = self.button.set_sensitive

    def get_text(self):
        return self.text

    def set_text(self, text):
        if not text or len(text) < 1:
            self.reset()
        self.text = text
        self.button.set_label(text)

    def set_stock_icon(self, name, size=Gtk.IconSize.BUTTON):
        if self._icon is None:
            self._icon = Gtk.Image()
            self.button.set_icon_widget(self._icon)

        self._icon.set_from_stock(name, size)

    def reset(self):
        self.text = None
        self.button.set_label(self.default_label)

    def set_menu(self, menu):
        self._menu = menu
        self.button.set_menu(menu)

    def get_menu(self):
        return self._menu

    def _detach_menu(self):
        self._menu = None