1
# -*- coding: utf-8 -*-
3
# This file is part of emesene.
5
# Emesene is free software; you can redistribute it and/or modify
6
# it under the terms of the GNU General Public License as published by
7
# the Free Software Foundation; either version 2 of the License, or
8
# (at your option) any later version.
10
# emesene is distributed in the hope that it will be useful,
11
# but WITHOUT ANY WARRANTY; without even the implied warranty of
12
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
# GNU General Public License for more details.
15
# You should have received a copy of the GNU General Public License
16
# along with emesene; if not, write to the Free Software
17
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18
'''a gtk implementation of abstract.Menu'''
22
from abstract.Menu import *
24
accel_group = gtk.AccelGroup()
27
'''build an element with the defined toolkit and return it'''
28
if type(element) is Menu:
29
return _build_menu(element)
30
elif type(element) is Item:
31
return _build_menuitem(element)
32
elif type(element) is Option:
33
return _build_option(element)
34
elif type(element) is CheckBox:
35
return _build_checkbox(element)
37
raise ValueError('Not valid type', type(element), Menu)
39
def build_menu_bar(menu):
40
'''build a menu with the form of a menu bar'''
41
gtk_menubar = gtk.MenuBar()
44
gtk_menubar.add(build(child))
46
menu.signal_connect("enabled", _set_sensitive, gtk_menubar, True)
47
menu.signal_connect("disabled", _set_sensitive, gtk_menubar, False)
51
def build_pop_up(menu, x=0, y=0):
52
'''build a pop up menu, if positions are given and needed by the toolkit
53
will be used to show the pop up on that position'''
61
menu.signal_connect("enabled", _set_sensitive, gtk_menu, True)
62
menu.signal_connect("disabled", _set_sensitive, gtk_menu, False)
66
def _set_sensitive(item, widget, enabled):
67
'''set sensitive on widget to enabled'''
68
widget.set_sensitive(enabled)
70
def _build_menu(menu):
71
'''build a gtk.Menu from a abstract.Menu.Menu'''
74
gtk_menu_item = gtk.MenuItem(menu.text)
75
gtk_menu_item.set_submenu(gtk_menu)
78
if type(item) is Item:
80
gtk_menu.add(gtk.SeparatorMenuItem())
82
gtk_menu.add(_build_menuitem(item))
83
elif type(item) is Option:
84
for option in _build_option(item):
87
gtk_menu.add(build(item))
89
menu.signal_connect("enabled", _set_sensitive, gtk_menu_item, True)
90
menu.signal_connect("disabled", _set_sensitive, gtk_menu_item, False)
94
def _build_option(option):
95
'''build an option object, return a list of gtk.RadioMenuItems'''
97
gtk_option = gtk.RadioMenuItem(None, option[0].text)
99
if option.selected_index <= 0 or option.selected_index >= len(option):
100
gtk_option.set_active(True)
102
gtk_option.connect('activate', _option_cb, option[0], option)
103
option_list.append(gtk_option)
105
for (count, opt) in enumerate(option[1:]):
106
gtk_opt = gtk.RadioMenuItem(gtk_option, opt.text)
107
option.signal_connect("enabled", _set_sensitive, gtk_opt, True)
108
option.signal_connect("disabled", _set_sensitive, gtk_opt, False)
110
if option.selected_index == count + 1:
111
gtk_opt.set_active(True)
113
gtk_opt.connect('activate', _option_cb, opt, option)
114
option_list.append(gtk_opt)
116
option.signal_connect("enabled", _set_sensitive, gtk_option, True)
117
option.signal_connect("disabled", _set_sensitive, gtk_option, False)
121
def _build_checkbox(checkbox):
122
'''build a checkbox and return it'''
123
gtk_checkbox = gtk.CheckMenuItem(checkbox.text)
124
gtk_checkbox.set_active(checkbox.toggled)
125
gtk_checkbox.connect('toggled', _checkbox_cb, checkbox)
127
checkbox.signal_connect("enabled", _set_sensitive, gtk_checkbox, True)
128
checkbox.signal_connect("disabled", _set_sensitive, gtk_checkbox, False)
132
def _build_menuitem(menuitem):
133
'''build a menu item from a abstract.Menu.Item'''
134
gtk_menuitem = gtk.ImageMenuItem(menuitem.text)
136
if menuitem.is_stock():
137
image = gtk.image_new_from_stock(
138
stock.map_stock(menuitem.image, gtk.STOCK_MISSING_IMAGE),
140
gtk_menuitem.set_image(image)
141
elif menuitem.is_image_path():
142
image = gtk.image_new_from_file(menuite.get_image_path(),
144
gtk_menuitem.set_image(image)
149
if menuitem.accel.ctrl:
150
special_keys = special_keys | gtk.gdk.CONTROL_MASK
151
if menuitem.accel.alt:
152
special_keys = special_keys | gtk.gdk.MOD1_MASK
154
gtk_menuitem.add_accelerator('activate', accel_group,
155
ord(menuitem.accel.key), special_keys, gtk.ACCEL_VISIBLE)
157
gtk_menuitem.connect('activate', _menu_item_cb, menuitem)
159
menuitem.signal_connect("enabled", _set_sensitive,
161
menuitem.signal_connect("disabled", _set_sensitive,
166
def _menu_item_cb(gtk_mitem, mitem):
167
'''callback called when a Item is selected, emit the selected signal
169
mitem.signal_emit('selected')
171
def _option_cb(gtk_option, option, options):
172
'''callback selected when an option is toggled'''
173
option.toggled = gtk_option.get_active()
174
option.signal_emit('toggled', option.toggled)
176
def _checkbox_cb(gtk_checkbox, checkbox):
177
'''callback called when the checbox is toggled'''
178
checkbox.toggled = gtk_checkbox.get_active()
179
checkbox.signal_emit('toggled', checkbox.toggled)
182
'''test the implementation'''
184
import abstract.MainMenu as MainMenu
187
'''method called when the quit item is selected'''
190
def status_cb(item, stat):
191
'''method called when a status is selected'''
192
print 'status', status.STATUS[stat], 'selected'
194
window = gtk.Window()
195
window.set_default_size(200, 200)
196
window.connect('delete-event', gtk.main_quit)
201
menu = MainMenu.MainMenu(dialog, None, None, None)
202
vbox.pack_start(build_menu_bar(menu), False)
203
vbox.pack_start(gtk.Label('test'), True, True)
204
vbox.pack_start(gtk.Statusbar(), False)
207
menu.status_item.enabled = False
208
menu.order_option.enabled = False
209
menu.quit_item.enabled = False
210
menu.help_menu.enabled = False
211
menu.show_by_nick_option.enabled = False
214
if __name__ == '__main__':