summaryrefslogtreecommitdiffstats
path: root/panel/toolitem.py
blob: 04d2b25ccc568bb252799de6fd80615150673bb0 (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
# vim:set noet ts=4:
#
# ibus - The Input Bus
#
# Copyright (c) 2007-2008 Huang Peng <shawn.p.huang@gmail.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 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 program; if not, write to the
# Free Software Foundation, Inc., 59 Temple Place, Suite 330,
# Boston, MA  02111-1307  USA

import gtk
import gtk.gdk as gdk
import gobject
import ibus
from menu import *

class ToolButton (gtk.ToolButton):
	__gsignals__ = {
		"property-activate" : (
			gobject.SIGNAL_RUN_FIRST,
			gobject.TYPE_NONE,
			(gobject.TYPE_STRING, gobject.TYPE_INT)),
		}

	def __init__ (self, label = None, icon = None, prop = None):
		if prop == None:
			prop = ibus.Property (name= "", type = ibus.PROP_TYPE_NORMAL,
							label = label, icon = icon)
		self._prop = prop

		gtk.ToolButton.__init__ (self, label = prop._label)
		self.set_icon_name (prop._icon)

	def set_icon_name (self, icon_name):
		if icon_name:
			gtk.ToolButton.set_icon_name (self, icon_name)
			self.set_is_important (False)
		else:
			gtk.ToolButton.set_icon_name (self, None)
			self.set_is_important (True)

		self._prop._icon = icon_name

	def do_clicked (self):
		self.emit ("property-activate", self._prop._name, self._prop._state)


class ToggleToolButton (gtk.ToggleToolButton):
	__gsignals__ = {
		"property-activate" : (
			gobject.SIGNAL_RUN_FIRST,
			gobject.TYPE_NONE,
			(gobject.TYPE_STRING, gobject.TYPE_INT)),
		}

	def __init__ (self, label = None, icon = None, state = ibus.PROP_STATE_UNCHECKED, prop = None):
		if prop == None:
			prop = ibus.Property (name = "", type = ibus.PROP_TYPE_TOGGLE,
							label = label, icon = icon, state = state)
		self._prop = prop

		gtk.ToggleToolButton.__init__ (self)
		self.set_label (prop._label)
		self.set_icon_name (prop._icon)
		self.set_state (prop._state)

	def set_icon_name (self, icon_name):
		if icon_name:
			gtk.ToolButton.set_icon_name (self, icon_name)
			self.set_is_important (False)
		else:
			gtk.ToolButton.set_icon_name (self, None)
			self.set_is_important (True)

		self._prop._icon = icon_name

	def set_state (self, state):
		self.set_active (state == ibus.PROP_STATE_CHECKED)
		self._prop._state = state

	def get_state (self):
		return self._prop._state

	def do_toggled (self):
		if self.get_active ():
			self._prop._state = ibus.PROP_STATE_CHECKED
		else:
			self._prop._state = ibus.PROP_STATE_UNCHECKED
		self.emit ("property-activate", self._prop._name, self._prop._state)


class MenuToolButton (ToggleToolButton):
	# __gsignals__ = {
	#		"property-activate" : (
	#			gobject.SIGNAL_RUN_FIRST,
	#			gobject.TYPE_NONE,
	#			(gobject.TYPE_STRING, gobject.TYPE_INT)),
	#		}

	def __init__ (self, label = None, icon = None, prop = None):
		ToggleToolButton.__init__ (self, label = label, icon = icon, prop = prop)
		self._menu = Menu (prop)
		self._menu.connect ("deactivate", lambda m: self.set_active (False))
		self._menu.connect ("property-activate", lambda w,n,s: self.emit ("property-activate", n, s))

	def do_toggled (self):
		if self.get_active ():
			self._menu.popup (0, gtk.get_current_event_time (), self)

gobject.type_register (ToolButton, "ToolButton")
gobject.type_register (ToggleToolButton, "IBusToggleToolButton")
gobject.type_register (MenuToolButton, "MenuToolButton")