071ab08da4b324c76103b1b889dc0ce0bc08ddfd
[platform/upstream/ibus.git] / ui / gtk / toolitem.py
1 # vim:set et sts=4 sw=4:
2 #
3 # ibus - The Input Bus
4 #
5 # Copyright (c) 2007-2010 Peng Huang <shawn.p.huang@gmail.com>
6 # Copyright (c) 2007-2010 Red Hat, Inc.
7 #
8 # This library is free software; you can redistribute it and/or
9 # modify it under the terms of the GNU Lesser General Public
10 # License as published by the Free Software Foundation; either
11 # version 2 of the License, or (at your option) any later version.
12 #
13 # This library is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 # GNU Lesser General Public License for more details.
17 #
18 # You should have received a copy of the GNU Lesser General Public
19 # License along with this program; if not, write to the
20 # Free Software Foundation, Inc., 59 Temple Place, Suite 330,
21 # Boston, MA  02111-1307  USA
22
23 import gtk
24 import gtk.gdk as gdk
25 import gobject
26 import ibus
27 from propitem import PropItem
28 import icon
29 from menu import *
30
31 class ToolButton(gtk.ToolButton, PropItem):
32     __gtype_name__ = "IBusToolButton"
33     __gsignals__ = {
34         "property-activate" : (
35             gobject.SIGNAL_RUN_FIRST,
36             gobject.TYPE_NONE,
37             (gobject.TYPE_STRING, gobject.TYPE_INT)),
38         }
39
40     def __init__(self, prop):
41         gtk.ToolButton.__init__(self, label = prop.label.text)
42         self.set_homogeneous(False)
43         PropItem.__init__(self, prop)
44         self.property_changed()
45
46     def set_icon_name(self, icon_name):
47         if icon_name:
48             widget = icon.IconWidget(icon_name, 18)
49             gtk.ToolButton.set_icon_widget(self, widget)
50             self.set_is_important(False)
51         elif self._prop.label.text:
52             gtk.ToolButton.set_icon_widget(self, None)
53             self.set_is_important(True)
54         else:
55             widget = icon.IconWidget("ibus", 18)
56             gtk.ToolButton.set_icon_widget(self, widget)
57             self.set_is_important(False)
58
59         self._prop.icon = icon_name
60
61     def set_tooltip_text(self, text):
62         if text:
63             gtk.ToolButton.set_tooltip_text(self, text.text)
64         else:
65             gtk.ToolButton.set_tooltip_text(self, None)
66
67         self._prop.tooltip = text
68
69     def property_changed(self):
70         self.set_label(self._prop.label.text)
71         self.set_tooltip_text(self._prop.tooltip)
72         self.set_sensitive(self._prop.sensitive)
73         self.set_icon_name(self._prop.icon)
74
75         if self._prop.visible:
76             self.set_no_show_all(False)
77             self.show_all()
78         else:
79             self.set_no_show_all(True)
80             self.hide_all()
81
82     def do_clicked(self):
83         self.emit("property-activate", self._prop.key, self._prop.state)
84
85
86 class ToggleToolButton(gtk.ToggleToolButton, PropItem):
87     __gtype_name__ = "IBusToggleToolButton"
88     __gsignals__ = {
89         "property-activate" : (
90             gobject.SIGNAL_RUN_FIRST,
91             gobject.TYPE_NONE,
92             (gobject.TYPE_STRING, gobject.TYPE_INT)),
93         }
94
95     def __init__(self, prop):
96         gtk.ToggleToolButton.__init__(self)
97         self.set_homogeneous(False)
98         PropItem.__init__(self, prop)
99         self.property_changed()
100
101     def set_icon_name(self, icon_name):
102         if icon_name:
103             widget = icon.IconWidget(icon_name, 18)
104             gtk.ToggleToolButton.set_icon_widget(self, widget)
105             self.set_is_important(False)
106         elif self._prop.label:
107             gtk.ToggleToolButton.set_icon_widget(self, None)
108             self.set_is_important(True)
109         else:
110             widget = icon.IconWidget("ibus", 18)
111             gtk.ToggleToolButton.set_icon_widget(self, widget)
112             self.set_is_important(False)
113
114         self._prop.icon = icon_name
115
116     def set_tooltip_text(self, text):
117         if text:
118             gtk.ToggleToolButton.set_tooltip_text(self, text.text)
119         else:
120             gtk.ToggleToolButton.set_tooltip_text(self, None)
121
122         self._prop.tooltip = text
123
124     def property_changed(self):
125         self.set_tooltip_text(self._prop.tooltip)
126         self.set_label(self._prop.label.text)
127         self.set_icon_name(self._prop.icon)
128         self.set_active(self._prop.state == ibus.PROP_STATE_CHECKED)
129         self.set_sensitive(self._prop.sensitive)
130         if self._prop.visible:
131             self.set_no_show_all(False)
132             self.show_all()
133         else:
134             self.set_no_show_all(True)
135             self.hide_all()
136
137     def do_toggled(self):
138         if self.get_active():
139             self._prop.state = ibus.PROP_STATE_CHECKED
140         else:
141             self._prop.state = ibus.PROP_STATE_UNCHECKED
142         self.emit("property-activate", self._prop.key, self._prop.state)
143
144 class SeparatorToolItem(gtk.SeparatorToolItem, PropItem):
145     __gtype_name__ = "IBusSeparatorToolItem"
146     def __init__ (self, prop):
147         gtk.SeparatorToolItem.__init__(self)
148         self.set_homogeneous(False)
149         PropItem.__init__(self, prop)
150
151 class MenuToolButton(ToggleToolButton):
152     __gtype_name__ = "IBusMenuToolButton"
153     # __gsignals__ = {
154     #        "property-activate" : (
155     #            gobject.SIGNAL_RUN_FIRST,
156     #            gobject.TYPE_NONE,
157     #            (gobject.TYPE_STRING, gobject.TYPE_INT)),
158     #        }
159
160     def __init__(self, prop):
161         super(MenuToolButton, self).__init__(prop)
162         self._menu = Menu(prop)
163         self._menu.connect("deactivate", lambda m: self.set_active(False))
164         self._menu.connect("property-activate", lambda w,n,s: self.emit("property-activate", n, s))
165
166     def update_property(self, prop):
167         PropItem.update_property(self, prop)
168         self._menu.update_property(prop)
169
170     def do_toggled(self):
171         if self.get_active():
172             self._menu.popup(0, gtk.get_current_event_time(), self)
173
174     def destroy(self):
175         self._menu.destroy()
176         self._menu = None
177         super(MenuToolButton, self).destroy()