# Copyright (C) 2012-2013 S. Daniel Francis # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 3 of the License, or # (at your option) any later version. # # This program 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 General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, # MA 02110-1301, USA. import logging logger = logging.getLogger('stock') from gi.repository import Gtk from gi.repository import Gdk icon_factory = Gtk.IconFactory() stock_items = {} def register(name, label, accelerator, icon_name): if icon_name: icon_source = Gtk.IconSource() icon_source.set_icon_name(icon_name) icon = Gtk.IconSet() icon.add_source(icon_source) icon_factory.add(name, icon) icon_factory.add_default() iconset = Gtk.IconFactory.lookup_default(name) stock_items[name] = {'label': label, 'accelerator': accelerator, 'has_icon': iconset or None} def overwrite_stock(stock_id, new_accelerator): info = Gtk.stock_lookup(stock_id) keyval, mask = Gtk.accelerator_parse(new_accelerator) info.modifier = mask info.keyval = keyval logger.debug(str(info)) Gtk.stock_add([info]) # Here we overwrite the key accelerators for some stock ids. # Feel free to add here any other stock id if you need it at your activity, # and send us a patch. overwrite_stock(Gtk.STOCK_SAVE_AS, 'S') overwrite_stock(Gtk.STOCK_ZOOM_IN, 'plus') overwrite_stock(Gtk.STOCK_ZOOM_OUT, 'minus') overwrite_stock(Gtk.STOCK_ZOOM_100, '0') # Key accelerator will be F11 on desktops and return on Sugar. overwrite_stock(Gtk.STOCK_FULLSCREEN, 'F11') overwrite_stock(Gtk.STOCK_ADD, 'A') overwrite_stock(Gtk.STOCK_REMOVE, 'Delete') overwrite_stock(Gtk.STOCK_SELECT_COLOR, 'L') def get_label(stock, underline=True): text = stock_items[stock]['label'] if underline: text = text.replace('_', '') return text def get_accelerator(stock): accelerator = stock_items[stock]['accelerator'] return Gtk.accelerator_parse(accelerator) if accelerator is not None \ else (0, Gdk.ModifierType(0)) for i in Gtk.stock_list_ids(): # I am very noisy # logger.debug(i) info = Gtk.stock_lookup(i) iconset = Gtk.IconFactory.lookup_default(i) if info is not None: stock_items[i] = {'label': info.label, 'accelerator': Gtk.accelerator_name(info.keyval, info.modifier), 'has_icon': iconset or None}