Web · Wiki · Activities · Blog · Lists · Chat · Meeting · Bugs · Git · Translate · Archive · People · Donate
1
# -*- coding: utf-8 -*-
2
# Copyright (c) 2011, Walter Bender
3
4
# This program is free software; you can redistribute it and/or modify
5
# it under the terms of the GNU General Public License as published by
6
# the Free Software Foundation; either version 3 of the License, or
7
# (at your option) any later version.
8
#
9
# You should have received a copy of the GNU General Public License
10
# along with this library; if not, write to the Free Software
11
# Foundation, 51 Franklin Street, Suite 500 Boston, MA 02110-1335 USA
12
13
14
from gi.repository import Gtk
15
16
from sugar3.graphics.radiotoolbutton import RadioToolButton
17
from sugar3.graphics.toolbutton import ToolButton
18
from sugar3.graphics.combobox import ComboBox
19
from sugar3.graphics.toolcombobox import ToolComboBox
20
21
22
def combo_factory(combo_array, toolbar, callback, cb_arg=None,
23
                  tooltip=None, default=None):
24
    '''Factory for making a toolbar combo box'''
25
    combo = ComboBox()
26
    if tooltip is not None and hasattr(combo, 'set_tooltip_text'):
27
        combo.set_tooltip_text(tooltip)
28
    if cb_arg is not None:
29
        combo.connect('changed', callback, cb_arg)
30
    else:
31
        combo.connect('changed', callback)
32
    for i, selection in enumerate(combo_array):
33
        combo.append_item(i, selection, None)
34
    combo.show()
35
    toolitem = Gtk.ToolItem()
36
    toolitem.add(combo)
37
    if hasattr(toolbar, 'insert'):  # the main toolbar
38
        toolbar.insert(toolitem, -1)
39
    else:  # or a secondary toolbar
40
        toolbar.props.page.insert(toolitem, -1)
41
    toolitem.show()
42
    if default is not None:
43
        combo.set_active(combo_array.index(default))
44
    return combo
45
46
47
def entry_factory(default_string, toolbar, tooltip=None, max=3):
48
    ''' Factory for adding a text box to a toolbar '''
49
    entry = Gtk.Entry()
50
    entry.set_text(default_string)
51
    if tooltip is not None and hasattr(entry, 'set_tooltip_text'):
52
        entry.set_tooltip_text(tooltip)
53
    entry.set_width_chars(max)
54
    entry.show()
55
    toolitem = Gtk.ToolItem()
56
    toolitem.add(entry)
57
    if hasattr(toolbar, 'insert'):  # the main toolbar
58
        toolbar.insert(toolitem, -1)
59
    else:  # or a secondary toolbar
60
        toolbar.props.page.insert(toolitem, -1)
61
    toolitem.show()
62
    return entry
63
64
65
def button_factory(icon_name, toolbar, callback, cb_arg=None, tooltip=None,
66
                   accelerator=None):
67
    '''Factory for making toolbar buttons'''
68
    button = ToolButton(icon_name)
69
    if tooltip is not None:
70
        button.set_tooltip(tooltip)
71
    button.props.sensitive = True
72
    if accelerator is not None:
73
        button.props.accelerator = accelerator
74
    if cb_arg is not None:
75
        button.connect('clicked', callback, cb_arg)
76
    else:
77
        button.connect('clicked', callback)
78
    if hasattr(toolbar, 'insert'):  # the main toolbar
79
        toolbar.insert(button, -1)
80
    else:  # or a secondary toolbar
81
        toolbar.props.page.insert(button, -1)
82
    button.show()
83
    return button
84
85
86
def radio_factory(button_name, toolbar, callback, cb_arg=None, tooltip=None,
87
                  group=None):
88
    ''' Add a radio button to a toolbar '''
89
    button = RadioToolButton(group=group)
90
    button.set_icon_name(button_name)
91
    if callback is not None:
92
        if cb_arg is None:
93
            button.connect('clicked', callback)
94
        else:
95
            button.connect('clicked', callback, cb_arg)
96
    if hasattr(toolbar, 'insert'):  # Add button to the main toolbar...
97
        toolbar.insert(button, -1)
98
    else:  # ...or a secondary toolbar.
99
        toolbar.props.page.insert(button, -1)
100
    button.show()
101
    if tooltip is not None:
102
        button.set_tooltip(tooltip)
103
    return button
104
105
106
def label_factory(toolbar, label_text, width=None):
107
    ''' Factory for adding a label to a toolbar '''
108
    label = Gtk.Label(label=label_text)
109
    label.set_line_wrap(True)
110
    if width is not None:
111
        label.set_size_request(width, -1)  # doesn't work on XOs
112
    label.show()
113
    toolitem = Gtk.ToolItem()
114
    toolitem.add(label)
115
    if hasattr(toolbar, 'insert'):  # the main toolbar
116
        toolbar.insert(toolitem, -1)
117
    else:  # or a secondary toolbar
118
        toolbar.props.page.insert(toolitem, -1)
119
    toolitem.show()
120
    return label
121
122
123
def separator_factory(toolbar, expand=False, visible=True):
124
    ''' add a separator to a toolbar '''
125
    separator = Gtk.SeparatorToolItem()
126
    separator.props.draw = visible
127
    separator.set_expand(expand)
128
    if hasattr(toolbar, 'insert'):  # the main toolbar
129
        toolbar.insert(separator, -1)
130
    else:  # or a secondary toolbar
131
        toolbar.props.page.insert(separator, -1)
132
    separator.show()
133
134
135
def image_factory(image, toolbar, tooltip=None):
136
    ''' Add an image to the toolbar '''
137
    img = Gtk.Image()
138
    img.set_from_pixbuf(image)
139
    img_tool = Gtk.ToolItem()
140
    img_tool.add(img)
141
    if tooltip is not None:
142
        img.set_tooltip_text(tooltip)
143
    if hasattr(toolbar, 'insert'):  # the main toolbar
144
        toolbar.insert(img_tool, -1)
145
    else:  # or a secondary toolbar
146
        toolbar.props.page.insert(img_tool, -1)
147
    img_tool.show()
148
    return img
149
150
151
def spin_factory(default, min, max, callback, toolbar):
152
    spin_adj = Gtk.Adjustment(default, min, max, 1, 32, 0)
153
    spin = Gtk.SpinButton(spin_adj, 0, 0)
154
    spin_id = spin.connect('value-changed', callback)
155
    spin.set_numeric(True)
156
    spin.show()
157
    toolitem = Gtk.ToolItem()
158
    toolitem.add(spin)
159
    if hasattr(toolbar, 'insert'):  # the main toolbar
160
        toolbar.insert(toolitem, -1)
161
    else:
162
        toolbar.props.page.insert(toolitem, -1)
163
    toolitem.show()
164
    return spin