~ubuntu-branches/ubuntu/oneiric/emesene/oneiric-proposed

« back to all changes in this revision

Viewing changes to Menu.py

  • Committer: Bazaar Package Importer
  • Author(s): Devid Antonio Filoni
  • Date: 2011-03-03 14:49:13 UTC
  • mfrom: (1.1.9 upstream)
  • Revision ID: james.westby@ubuntu.com-20110303144913-0adl9cmw2s35lvzo
Tags: 2.0~git20110303-0ubuntu1
* New upstream git revision (LP: #728469).
* Remove debian/watch, debian/emesene.xpm, debian/install and
  debian/README.source files.
* Remove 21_svn2451_fix_avatar and 20_dont_build_own_libmimic patches.
* debian/control: modify python to python (>= 2.5) in Build-Depends field.
* debian/control: remove python-libmimic from Recommends field.
* debian/control: modify python-gtk2 (>= 2.10) to python-gtk2 (>= 2.12) in
  Depends field.
* debian/control: add python-appindicator and python-xmpp to Recommends
  field.
* debian/control: add python-papyon (>= 0.5.4) and python-webkit to Depends
  field.
* debian/control: update Description field.
* debian/control: add python-setuptools to Build-Depends field.
* debian/control: move python-dbus and python-notify to Depends field.
* Update debian/copyright file.
* Update debian/links file.
* debian/menu: update description field.
* Bump Standards-Version to 3.9.1.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# -*- coding: utf-8 -*-
2
 
 
3
 
#   This file is part of emesene.
4
 
#
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.
9
 
#
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.
14
 
#
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'''
19
 
import gtk
20
 
 
21
 
import stock
22
 
from abstract.Menu import *
23
 
 
24
 
accel_group = gtk.AccelGroup()
25
 
 
26
 
def build(element):
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)
36
 
 
37
 
    raise ValueError('Not valid type', type(element), Menu)
38
 
 
39
 
def build_menu_bar(menu):
40
 
    '''build a menu with the form of a menu bar'''
41
 
    gtk_menubar = gtk.MenuBar()
42
 
 
43
 
    for child in menu:
44
 
        gtk_menubar.add(build(child))
45
 
    
46
 
    menu.signal_connect("enabled", _set_sensitive, gtk_menubar, True)
47
 
    menu.signal_connect("disabled", _set_sensitive, gtk_menubar, False)
48
 
 
49
 
    return gtk_menubar
50
 
 
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'''
54
 
    gtk_menu = gtk.Menu()
55
 
 
56
 
    for child in menu:
57
 
        item = build(child)
58
 
        item.show()
59
 
        gtk_menu.add(item)
60
 
    
61
 
    menu.signal_connect("enabled", _set_sensitive, gtk_menu, True)
62
 
    menu.signal_connect("disabled", _set_sensitive, gtk_menu, False)
63
 
 
64
 
    return gtk_menu
65
 
 
66
 
def _set_sensitive(item, widget, enabled):
67
 
    '''set sensitive on widget to enabled'''
68
 
    widget.set_sensitive(enabled)
69
 
 
70
 
def _build_menu(menu):
71
 
    '''build a gtk.Menu from a abstract.Menu.Menu'''
72
 
 
73
 
    gtk_menu = gtk.Menu()
74
 
    gtk_menu_item = gtk.MenuItem(menu.text)
75
 
    gtk_menu_item.set_submenu(gtk_menu)
76
 
 
77
 
    for item in menu:
78
 
        if type(item) is Item:
79
 
            if item.text == '-':
80
 
                gtk_menu.add(gtk.SeparatorMenuItem())
81
 
            else:
82
 
                gtk_menu.add(_build_menuitem(item))
83
 
        elif type(item) is Option:
84
 
            for option in _build_option(item):
85
 
                gtk_menu.add(option)
86
 
        else:
87
 
            gtk_menu.add(build(item))
88
 
 
89
 
    menu.signal_connect("enabled", _set_sensitive, gtk_menu_item, True)
90
 
    menu.signal_connect("disabled", _set_sensitive, gtk_menu_item, False)
91
 
            
92
 
    return gtk_menu_item
93
 
 
94
 
def _build_option(option):
95
 
    '''build an option object, return a list of gtk.RadioMenuItems'''
96
 
    option_list = []
97
 
    gtk_option = gtk.RadioMenuItem(None, option[0].text)
98
 
 
99
 
    if option.selected_index <= 0 or option.selected_index >= len(option):
100
 
        gtk_option.set_active(True)
101
 
 
102
 
    gtk_option.connect('activate', _option_cb, option[0], option)
103
 
    option_list.append(gtk_option)
104
 
 
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)
109
 
 
110
 
        if option.selected_index == count + 1:
111
 
            gtk_opt.set_active(True)
112
 
 
113
 
        gtk_opt.connect('activate', _option_cb, opt, option)
114
 
        option_list.append(gtk_opt)
115
 
    
116
 
    option.signal_connect("enabled", _set_sensitive, gtk_option, True)
117
 
    option.signal_connect("disabled", _set_sensitive, gtk_option, False)
118
 
 
119
 
    return option_list
120
 
 
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)
126
 
    
127
 
    checkbox.signal_connect("enabled", _set_sensitive, gtk_checkbox, True)
128
 
    checkbox.signal_connect("disabled", _set_sensitive, gtk_checkbox, False)
129
 
 
130
 
    return gtk_checkbox
131
 
 
132
 
def _build_menuitem(menuitem):
133
 
    '''build a menu item from a abstract.Menu.Item'''
134
 
    gtk_menuitem = gtk.ImageMenuItem(menuitem.text) 
135
 
 
136
 
    if menuitem.is_stock():
137
 
        image = gtk.image_new_from_stock(
138
 
            stock.map_stock(menuitem.image, gtk.STOCK_MISSING_IMAGE),
139
 
            gtk.ICON_SIZE_MENU)
140
 
        gtk_menuitem.set_image(image)
141
 
    elif menuitem.is_image_path():
142
 
        image = gtk.image_new_from_file(menuite.get_image_path(), 
143
 
            gtk.ICON_SIZE_MENU)
144
 
        gtk_menuitem.set_image(image)
145
 
 
146
 
    if menuitem.accel:
147
 
        special_keys = 0
148
 
 
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
153
 
 
154
 
        gtk_menuitem.add_accelerator('activate', accel_group, 
155
 
            ord(menuitem.accel.key), special_keys, gtk.ACCEL_VISIBLE)
156
 
 
157
 
    gtk_menuitem.connect('activate', _menu_item_cb, menuitem)
158
 
 
159
 
    menuitem.signal_connect("enabled", _set_sensitive, 
160
 
        gtk_menuitem, True)
161
 
    menuitem.signal_connect("disabled", _set_sensitive, 
162
 
        gtk_menuitem, False)
163
 
 
164
 
    return gtk_menuitem
165
 
 
166
 
def _menu_item_cb(gtk_mitem, mitem):
167
 
    '''callback called when a Item is selected, emit the selected signal
168
 
    on Item'''
169
 
    mitem.signal_emit('selected')
170
 
 
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)
175
 
 
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)
180
 
 
181
 
def test():
182
 
    '''test the implementation'''
183
 
    import dialog
184
 
    import abstract.MainMenu as MainMenu
185
 
 
186
 
    def quit_cb(item):
187
 
        '''method called when the quit item is selected'''
188
 
        gtk.main_quit()
189
 
 
190
 
    def status_cb(item, stat):
191
 
        '''method called when a status is selected'''
192
 
        print 'status', status.STATUS[stat], 'selected'
193
 
 
194
 
    window = gtk.Window()
195
 
    window.set_default_size(200, 200)
196
 
    window.connect('delete-event', gtk.main_quit)
197
 
    vbox = gtk.VBox()
198
 
 
199
 
    logged_in = True
200
 
 
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)
205
 
    window.add(vbox)
206
 
    window.show_all()
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
212
 
    gtk.main()
213
 
 
214
 
if __name__ == '__main__':
215
 
    test()