Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/sugar/sweetener/stock.py
blob: 28f395672d6b922c3a246a49bb8d19ed013c7521 (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
# -*- coding: utf-8 -*-
#
# Copyright (C) 2012 S. Daniel Francis <francis@sugarlabs.org>
#
# 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

icon_factory = Gtk.IconFactory()

# Set the icon name for the stock items, this is used only in Sugar.
icons = {Gtk.STOCK_ADD: 'list-add'}


def register(name, label, accelerator, icon_name):
    if accelerator == None:
        keyval = 0
        mask = 0
    else:
        keyval, mask = Gtk.accelerator_parse(accelerator)
    logger.debug(keyval)
    logger.debug(mask)
    item = Gtk.StockItem.new()
    item.stock_id = name
    item.label = label
    item.modifier = mask
    item.keyval = keyval
    Gtk.stock_add([item])
    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()
        icons[name] = icon_name


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, '<Shift><Ctrl>S')
overwrite_stock(Gtk.STOCK_ZOOM_IN, '<Ctrl>plus')
overwrite_stock(Gtk.STOCK_ZOOM_OUT, '<Ctrl>minus')
overwrite_stock(Gtk.STOCK_ZOOM_100, '<Ctrl>0')
# Key accelerator will be F11 on desktops and <Alt>return on Sugar.
overwrite_stock(Gtk.STOCK_FULLSCREEN, '<Alt>Return')
overwrite_stock(Gtk.STOCK_ADD, '<Ctrl>A')
overwrite_stock(Gtk.STOCK_REMOVE, '<Ctrl>R')
overwrite_stock(Gtk.STOCK_SELECT_COLOR, '<Ctrl>L')


def get_label(stock, underline):
    text = Gtk.stock_lookup(stock)[1]
    if underline:
        text = text.replace('_', '')
    return text


def get_accelerator(stock):
    stock_id = Gtk.stock_lookup(stock)
    return stock_id.modifier, stock_id.keyval