| 1 |
# Copyright (C) 2008-2010 Adam Olsen |
| 2 |
# |
| 3 |
# This program is free software; you can redistribute it and/or modify |
| 4 |
# it under the terms of the GNU General Public License as published by |
| 5 |
# the Free Software Foundation; either version 2, or (at your option) |
| 6 |
# any later version. |
| 7 |
# |
| 8 |
# This program is distributed in the hope that it will be useful, |
| 9 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 |
# GNU General Public License for more details. |
| 12 |
# |
| 13 |
# You should have received a copy of the GNU General Public License |
| 14 |
# along with this program; if not, write to the Free Software |
| 15 |
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 16 |
# |
| 17 |
# |
| 18 |
# The developers of the Exaile media player hereby grant permission |
| 19 |
# for non-GPL compatible GStreamer and Exaile plugins to be used and |
| 20 |
# distributed together with GStreamer and Exaile. This permission is |
| 21 |
# above and beyond the permissions granted by the GPL license by which |
| 22 |
# Exaile is covered. If you modify this code, you may extend this |
| 23 |
# exception to your version of the code, but you are not obligated to |
| 24 |
# do so. If you do not wish to do so, delete this exception statement |
| 25 |
# from your version. |
| 26 |
|
| 27 |
from typing import Any, Callable, Iterable, List, Optional, Tuple, Union |
| 28 |
|
| 29 |
from gi.repository import Gtk, Gdk |
| 30 |
|
| 31 |
from xl import common, providers |
| 32 |
from xlgui.accelerators import Accelerator |
| 33 |
|
| 34 |
# Fake accel group so that menuitems can trick GTK into |
| 35 |
# showing accelerators in the menus. |
| 36 |
FAKEACCELGROUP = Gtk.AccelGroup() |
| 37 |
|
| 38 |
|
| 39 |
def simple_separator(name, after): |
| 40 |
def factory(menu, parent, context): |
| 41 |
item = Gtk.SeparatorMenuItem() |
| 42 |
return item |
| 43 |
|
| 44 |
item = MenuItem(name, factory, after=after) |
| 45 |
item._pos = 'last' |
| 46 |
return item |
| 47 |
|
| 48 |
|
| 49 |
def _get_accel( |
| 50 |
callback: Union[Accelerator, Callable, None], display_name: Optional[str] |
| 51 |
) -> Tuple[Optional[Accelerator], Optional[Callable], Optional[str]]: |
| 52 |
# utility function to get menu information from an xlgui.Accelerator |
| 53 |
accelerator: Optional[Accelerator] = None |
| 54 |
if isinstance(callback, Accelerator): |
| 55 |
accelerator = callback |
| 56 |
if display_name is None: |
| 57 |
display_name = accelerator.helptext |
| 58 |
callback = accelerator.callback |
| 59 |
return accelerator, callback, display_name |
| 60 |
|
| 61 |
|
| 62 |
def simple_menu_item( |
| 63 |
name: str, |
| 64 |
after: List[str], |
| 65 |
display_name: Optional[str] = None, |
| 66 |
icon_name: Optional[str] = None, |
| 67 |
callback: Union[Accelerator, Callable, None] = None, |
| 68 |
callback_args: Iterable[Any] = [], |
| 69 |
submenu: Optional[Gtk.Menu] = None, |
| 70 |
condition_fn: Optional[Callable[..., bool]] = None, |
| 71 |
sensitive_cb: Optional[Callable[[], bool]] = None, |
| 72 |
) -> 'MenuItem': |
| 73 |
""" |
| 74 |
Factory function that should handle most cases for menus |
| 75 |
|
| 76 |
:param name: Internal name for the item. must be unique within the menu. |
| 77 |
:param after: List of ids which come before this item, this item will |
| 78 |
be placed after the lowest of these. |
| 79 |
:param display_name: Name as is to appear in the menu. |
| 80 |
:param icon_name: Name of the icon to display, or None for no icon. |
| 81 |
:param callback: The function to call when the menu item is activated OR |
| 82 |
an xlgui accelerator object. |
| 83 |
signature: callback(widget, name, parent, context) |
| 84 |
:param submenu: The Gtk.Menu that is to be the submenu of this item |
| 85 |
:param condition_fn: A function to call when the menu is displayed. If |
| 86 |
the function returns False, the menu item is not shown |
| 87 |
signature: condition_fn(name, parent, context) |
| 88 |
:param sensitive_cb: A function that if it returns False, the menu item |
| 89 |
will be disabled |
| 90 |
""" |
| 91 |
accelerator, callback, display_name = _get_accel(callback, display_name) |
| 92 |
|
| 93 |
def factory(menu, parent, context) -> Optional[Gtk.MenuItem]: |
| 94 |
item: Optional[Gtk.MenuItem] = None |
| 95 |
|
| 96 |
if condition_fn is not None and not condition_fn(name, parent, context): |
| 97 |
return None |
| 98 |
|
| 99 |
if display_name is not None: |
| 100 |
if icon_name is not None: |
| 101 |
item = Gtk.ImageMenuItem.new_from_stock(display_name) |
| 102 |
image = Gtk.Image.new_from_icon_name(icon_name, size=Gtk.IconSize.MENU) |
| 103 |
item.set_image(image) |
| 104 |
else: |
| 105 |
item = Gtk.MenuItem.new_with_mnemonic(display_name) |
| 106 |
else: |
| 107 |
assert icon_name is not None |
| 108 |
item = Gtk.ImageMenuItem.new_from_stock(icon_name) |
| 109 |
|
| 110 |
if submenu is not None: |
| 111 |
item.set_submenu(submenu) |
| 112 |
|
| 113 |
if accelerator is not None: |
| 114 |
item.add_accelerator( |
| 115 |
'activate', |
| 116 |
FAKEACCELGROUP, |
| 117 |
accelerator.key, |
| 118 |
accelerator.mods, |
| 119 |
Gtk.AccelFlags.VISIBLE, |
| 120 |
) |
| 121 |
|
| 122 |
if callback is not None: |
| 123 |
item.connect('activate', callback, name, parent, context, *callback_args) |
| 124 |
|
| 125 |
if sensitive_cb is not None and not sensitive_cb(): |
| 126 |
item.set_sensitive(False) |
| 127 |
|
| 128 |
return item |
| 129 |
|
| 130 |
return MenuItem(name, factory, after=after) |
| 131 |
|
| 132 |
|
| 133 |
def check_menu_item( |
| 134 |
name: str, after, display_name: str, checked_func: Callable[..., bool], callback |
| 135 |
) -> 'MenuItem': |
| 136 |
accelerator, callback, display_name = _get_accel(callback, display_name) |
| 137 |
|
| 138 |
def factory(menu, parent, context): |
| 139 |
item = Gtk.CheckMenuItem.new_with_mnemonic(display_name) |
| 140 |
active = checked_func(name, parent, context) |
| 141 |
item.set_active(active) |
| 142 |
if accelerator is not None: |
| 143 |
item.add_accelerator( |
| 144 |
'activate', |
| 145 |
FAKEACCELGROUP, |
| 146 |
accelerator.key, |
| 147 |
accelerator.mods, |
| 148 |
Gtk.AccelFlags.VISIBLE, |
| 149 |
) |
| 150 |
item.connect('activate', callback, name, parent, context) |
| 151 |
return item |
| 152 |
|
| 153 |
return MenuItem(name, factory, after=after) |
| 154 |
|
| 155 |
|
| 156 |
def radio_menu_item( |
| 157 |
name: str, after, display_name: str, groupname, selected_func, callback |
| 158 |
) -> 'RadioMenuItem': |
| 159 |
def factory(menu, parent, context): |
| 160 |
for index, item in enumerate(menu._items): |
| 161 |
if hasattr(item, 'groupname') and item.groupname == groupname: |
| 162 |
break |
| 163 |
else: |
| 164 |
index = None |
| 165 |
|
| 166 |
if index is not None: |
| 167 |
try: |
| 168 |
group_parent = menu.get_children()[index] |
| 169 |
if not isinstance(group_parent, Gtk.RadioMenuItem): |
| 170 |
group_parent = None |
| 171 |
except IndexError: |
| 172 |
group_parent = None |
| 173 |
|
| 174 |
if group_parent: |
| 175 |
group = group_parent.get_group() |
| 176 |
else: |
| 177 |
group = None |
| 178 |
|
| 179 |
item = Gtk.RadioMenuItem.new_with_mnemonic(group, display_name) |
| 180 |
active = selected_func(name, parent, context) |
| 181 |
item.set_active(active) |
| 182 |
|
| 183 |
item.connect('activate', callback, name, parent, context) |
| 184 |
return item |
| 185 |
|
| 186 |
return RadioMenuItem(name, factory, after=after, groupname=groupname) |
| 187 |
|
| 188 |
|
| 189 |
class MenuItem: |
| 190 |
__slots__ = ['name', 'after', '_factory', '_pos', '_provider_data'] |
| 191 |
|
| 192 |
def __init__( |
| 193 |
self, |
| 194 |
name: str, |
| 195 |
factory: Optional[Callable[..., Optional[Gtk.MenuItem]]], |
| 196 |
after: List[str], |
| 197 |
): |
| 198 |
self.name = name |
| 199 |
self.after = after |
| 200 |
self._factory = factory |
| 201 |
# Don't change this unless you have a REALLY good |
| 202 |
# reason to. after= is the 'supported' |
| 203 |
# method of ordering, this property is not |
| 204 |
# considered public api and may change |
| 205 |
# without warning. |
| 206 |
self._pos = 'normal' |
| 207 |
|
| 208 |
def factory(self, menu, parent, context) -> Optional[Gtk.MenuItem]: |
| 209 |
""" |
| 210 |
The factory function is called when the menu is shown, and |
| 211 |
should return a menu item. If it returns None, the item is |
| 212 |
not shown. |
| 213 |
""" |
| 214 |
return self._factory(menu, parent, context) |
| 215 |
|
| 216 |
def register(self, servicename, target=None): |
| 217 |
""" |
| 218 |
Shortcut for providers.register(), allows registration |
| 219 |
for use with a ProviderMenu |
| 220 |
""" |
| 221 |
self._provider_data = (servicename, target) |
| 222 |
return providers.register(servicename, self, target=target) |
| 223 |
|
| 224 |
def unregister(self): |
| 225 |
""" |
| 226 |
Shortcut for providers.unregister() |
| 227 |
""" |
| 228 |
servicename, target = self._provider_data |
| 229 |
return providers.unregister(servicename, self, target) |
| 230 |
|
| 231 |
def __repr__(self): |
| 232 |
return '<xlgui.widgets.MenuItem: %s>' % self.name |
| 233 |
|
| 234 |
|
| 235 |
class RadioMenuItem(MenuItem): |
| 236 |
__slots__ = ['groupname'] |
| 237 |
|
| 238 |
def __init__(self, name, factory, after, groupname): |
| 239 |
MenuItem.__init__(self, name, factory, after) |
| 240 |
self.groupname = groupname |
| 241 |
|
| 242 |
|
| 243 |
class Menu(Gtk.Menu): |
| 244 |
""" |
| 245 |
Generic flexible menu with reliable |
| 246 |
menu item order and context handling |
| 247 |
""" |
| 248 |
|
| 249 |
def __init__(self, parent, context_func=None, inherit_context=False): |
| 250 |
""" |
| 251 |
:param parent: the parent for this menu |
| 252 |
:param context_func: a function for context |
| 253 |
retrieval |
| 254 |
:param inherit_context: If a submenu, inherit context function from |
| 255 |
parent menu |
| 256 |
""" |
| 257 |
Gtk.Menu.__init__(self) |
| 258 |
self._parent = parent |
| 259 |
self._items: List[MenuItem] = [] |
| 260 |
self.context_func = context_func |
| 261 |
self.connect('show', lambda *e: self.regenerate_menu()) |
| 262 |
self.connect('hide', lambda *e: self.clear_menu()) |
| 263 |
# Placeholder exists to make sure unity doesn't get confused (legacy?) |
| 264 |
self.placeholder = Gtk.MenuItem.new_with_mnemonic('') |
| 265 |
self._inherit_context = inherit_context |
| 266 |
|
| 267 |
def get_context(self): |
| 268 |
""" |
| 269 |
Retrieves the menu context which |
| 270 |
can contain various data |
| 271 |
|
| 272 |
:returns: {'key1': 'value1', ...} |
| 273 |
:rtype: dictionary |
| 274 |
""" |
| 275 |
if self._inherit_context: |
| 276 |
return self.get_parent_shell().get_context() |
| 277 |
elif self.context_func is None: |
| 278 |
return {} |
| 279 |
else: |
| 280 |
return self.context_func(self._parent) |
| 281 |
|
| 282 |
def add_item(self, item: MenuItem) -> None: |
| 283 |
""" |
| 284 |
Adds a menu item and triggers reordering |
| 285 |
|
| 286 |
:param item: the menu item |
| 287 |
""" |
| 288 |
self._items.append(item) |
| 289 |
self.reorder_items() |
| 290 |
|
| 291 |
def add_simple(self, label: str, callback, icon_name: Optional[str] = None) -> None: |
| 292 |
""" |
| 293 |
Provide a simple mechanism to add menu items without a lot of hassle |
| 294 |
|
| 295 |
:param label: Label to display |
| 296 |
:param callback: Callback that will be called on click |
| 297 |
:param icon_name: GTK mostly ignores this, and it will go away |
| 298 |
|
| 299 |
.. note:: If you use this API, you should generally only use this |
| 300 |
API to add items to that menu |
| 301 |
""" |
| 302 |
self.add_item( |
| 303 |
simple_menu_item( |
| 304 |
'_i%s' % len(self._items), |
| 305 |
[], |
| 306 |
label, |
| 307 |
icon_name=icon_name, |
| 308 |
callback=callback, |
| 309 |
) |
| 310 |
) |
| 311 |
|
| 312 |
def remove_item(self, item: MenuItem) -> None: |
| 313 |
""" |
| 314 |
Removes a menu item |
| 315 |
|
| 316 |
:param item: the menu item |
| 317 |
""" |
| 318 |
self._items.remove(item) |
| 319 |
|
| 320 |
def clear_menu(self) -> None: |
| 321 |
""" |
| 322 |
Removes all menu items and submenus to prevent |
| 323 |
references sticking around due to saved contexts |
| 324 |
""" |
| 325 |
self.append(self.placeholder) |
| 326 |
children = self.get_children() |
| 327 |
for c in children: |
| 328 |
if c is self.placeholder: |
| 329 |
continue |
| 330 |
c.set_submenu(None) |
| 331 |
self.remove(c) |
| 332 |
|
| 333 |
def reorder_items(self) -> None: |
| 334 |
""" |
| 335 |
Reorders all menu items |
| 336 |
""" |
| 337 |
pmap = {'first': 0, 'normal': 1, 'last': 2} |
| 338 |
items = [ |
| 339 |
common.PosetItem(i.name, i.after, pmap[i._pos], value=i) |
| 340 |
for i in self._items |
| 341 |
] |
| 342 |
items = common.order_poset(items) |
| 343 |
self._items = [i.value for i in items] |
| 344 |
|
| 345 |
def regenerate_menu(self) -> None: |
| 346 |
""" |
| 347 |
Regenerates the menu by retrieving |
| 348 |
the context and calling the factory |
| 349 |
method of all menu items |
| 350 |
""" |
| 351 |
context = self.get_context() |
| 352 |
if self.placeholder in self.get_children(): |
| 353 |
self.remove(self.placeholder) |
| 354 |
for item in self._items: |
| 355 |
subitem = item.factory(self, self._parent, context) |
| 356 |
if subitem is not None: |
| 357 |
self.append(subitem) |
| 358 |
self.show_all() |
| 359 |
|
| 360 |
def popup(self, *args) -> None: |
| 361 |
""" |
| 362 |
Pops out the menu (Only if |
| 363 |
there are items to show) |
| 364 |
""" |
| 365 |
if len(self._items) > 0: |
| 366 |
if len(args) == 1: |
| 367 |
event = args[0] |
| 368 |
Gtk.Menu.popup(self, None, None, None, None, event.button, event.time) |
| 369 |
else: |
| 370 |
Gtk.Menu.popup(self, *args) |
| 371 |
|
| 372 |
def popup_at_widget(self, widget: Gtk.Widget, event: Gdk.Event) -> None: |
| 373 |
Gtk.Menu.popup_at_widget( |
| 374 |
self, widget, Gdk.Gravity.EAST, Gdk.Gravity.EAST, event |
| 375 |
) |
| 376 |
|
| 377 |
|
| 378 |
class ProviderMenu(providers.ProviderHandler, Menu): |
| 379 |
""" |
| 380 |
A menu that can be added to by registering a menu item with |
| 381 |
the providers system. If desired, a menu item can be targeted |
| 382 |
towards a specific parent widget. |
| 383 |
""" |
| 384 |
|
| 385 |
def __init__(self, name, parent): |
| 386 |
providers.ProviderHandler.__init__(self, name, parent) |
| 387 |
Menu.__init__(self, parent) |
| 388 |
for p in self.get_providers(): |
| 389 |
self.on_provider_added(p) |
| 390 |
|
| 391 |
def on_provider_added(self, provider): |
| 392 |
self.add_item(provider) |
| 393 |
|
| 394 |
def on_provider_removed(self, provider): |
| 395 |
self.remove_item(provider) |
| 396 |
|
| 397 |
def set_context_func(self, context_func: callable) -> None: |
| 398 |
self.context_func = context_func |
| 399 |
|
| 400 |
|
| 401 |
class MultiProviderMenu(providers.MultiProviderHandler, Menu): |
| 402 |
""" |
| 403 |
A menu that can be added to by registering a menu item with |
| 404 |
the providers system. If desired, a menu item can be targeted |
| 405 |
towards a specific parent widget. |
| 406 |
|
| 407 |
Supports retrieving menu items from multiple providers |
| 408 |
""" |
| 409 |
|
| 410 |
def __init__(self, names, parent): |
| 411 |
providers.MultiProviderHandler.__init__(self, names, parent) |
| 412 |
Menu.__init__(self, parent) |
| 413 |
for p in self.get_providers(): |
| 414 |
self.on_provider_added(p) |
| 415 |
|
| 416 |
def on_provider_added(self, provider): |
| 417 |
self.add_item(provider) |
| 418 |
|
| 419 |
def on_provider_removed(self, provider): |
| 420 |
self.remove_item(provider) |