Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/src/sugar3/graphics/palette.py
blob: efdc9fd893c4f14001ed595d1ea7d1183524baf2 (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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
# Copyright (C) 2007, Eduardo Silva <edsiper@gmail.com>
# Copyright (C) 2008, One Laptop Per Child
# Copyright (C) 2009, Tomeu Vizoso
# Copyright (C) 2011, Benjamin Berg <benjamin@sipsolutions.net>
# Copyright (C) 2011, Marco Pesenti Gritti <marco@marcopg.org>
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2 of the License, or (at your option) any later version.
#
# This library 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
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the
# Free Software Foundation, Inc., 59 Temple Place - Suite 330,
# Boston, MA 02111-1307, USA.

"""
STABLE.
"""

from gi.repository import Gtk
from gi.repository import GObject
from gi.repository import Pango

from sugar3.graphics import palettegroup
from sugar3.graphics import animator
from sugar3.graphics import style
from sugar3.graphics.icon import Icon
from sugar3.graphics.palettewindow import PaletteWindow, \
    _PaletteWindowWidget, _PaletteMenuWidget

# DEPRECATED
# Import these for backwards compatibility
from sugar3.graphics.palettewindow import MouseSpeedDetector, Invoker, \
        WidgetInvoker, CursorInvoker, ToolInvoker, CellRendererInvoker


class _HeaderItem(Gtk.MenuItem):
    """A MenuItem with a custom child widget that gets all the
    available space.

    """

    __gtype_name__ = 'SugarPaletteHeader'

    def __init__(self, widget):
        Gtk.MenuItem.__init__(self)
        if self.get_child() is not None:
            self.remove(self.get_child())
        self.add(widget)
        # FIXME we have to mark it as insensitive again to make it an
        # informational element, when we realize how to get the icon
        # displayed correctly - SL #3836
        # self.set_sensitive(False)

    def do_size_allocate(self, allocation):
        self.set_allocation(allocation)
        self.get_child().size_allocate(allocation)


class _HeaderSeparator(Gtk.SeparatorMenuItem):
    """A SeparatorMenuItem that can be styled in the theme."""

    __gtype_name__ = 'SugarPaletteHeaderSeparator'

    def __init__(self):
        Gtk.SeparatorMenuItem.__init__(self)


class Palette(PaletteWindow):
    """Floating palette implementation.

    This class dynamically switches between one of two encapsulated child
    widget types: a _PaletteWindowWidget or a _PaletteMenuWidget.

    The window widget, created by default, acts as the container for any
    type of widget the user may wish to add. It can optionally display primary
    text, secondary text, and an icon at the top of the palette.

    If the user attempts to access the 'menu' property, the window widget is
    destroyed and the palette is dynamically switched to use a menu widget.
    This is a GtkMenu that retains the same look and feel as a normal palette,
    allowing submenus and so on. If primary text, secondary text and/or icons
    were provided, an initial menu entry is created containing widgets to
    display such information.
    """

    __gsignals__ = {
        'activate': (GObject.SignalFlags.RUN_FIRST, None, ([])),
    }

    __gtype_name__ = 'SugarPalette'

    def __init__(self, label=None, accel_path=None,
                 text_maxlen=60, **kwargs):
        # DEPRECATED: label is passed with the primary-text property,
        # accel_path is set via the invoker property

        self._primary_text = None
        self._secondary_text = None
        self._icon = None
        self._icon_visible = True
        self._palette_state = self.PRIMARY

        self._primary_box = Gtk.HBox()
        self._primary_box.show()

        self._icon_box = Gtk.HBox()
        self._icon_box.set_size_request(style.GRID_CELL_SIZE, -1)
        self._primary_box.pack_start(self._icon_box, False, True, 0)

        labels_box = Gtk.VBox()
        self._label_alignment = Gtk.Alignment(xalign=0, yalign=0.5, xscale=1,
                                              yscale=0.33)
        self._label_alignment.set_padding(0, 0, style.DEFAULT_SPACING,
                                          style.DEFAULT_SPACING)
        self._label_alignment.add(labels_box)
        self._label_alignment.show()
        self._primary_box.pack_start(self._label_alignment, True, True, 0)
        labels_box.show()

        self._label = Gtk.AccelLabel(label='')
        self._label.set_alignment(0, 0.5)

        if text_maxlen > 0:
            self._label.set_max_width_chars(text_maxlen)
            self._label.set_ellipsize(Pango.EllipsizeMode.MIDDLE)
        labels_box.pack_start(self._label, True, True, 0)

        self._secondary_label = Gtk.Label()
        self._secondary_label.set_alignment(0, 0.5)

        if text_maxlen > 0:
            self._secondary_label.set_max_width_chars(text_maxlen)
            self._secondary_label.set_ellipsize(Pango.EllipsizeMode.END)

        labels_box.pack_start(self._secondary_label, True, True, 0)

        self._secondary_box = Gtk.VBox()

        self._separator = Gtk.HSeparator()
        self._secondary_box.pack_start(self._separator, True, True, 0)

        self._secondary_anim = animator.Animator(2.0, 10)
        self._secondary_anim.add(_SecondaryAnimation(self))

        # we init after initializing all of our containers
        PaletteWindow.__init__(self, **kwargs)

        self._full_request = [0, 0]
        self._content = None

        # we set these for backward compatibility
        if label is not None:
            self.props.primary_text = label

        self._add_content()

        self.action_bar = PaletteActionBar()
        self._secondary_box.pack_start(self.action_bar, True, True, 0)
        self.action_bar.show()

        self.connect('notify::invoker', self.__notify_invoker_cb)
        self.connect('popdown', self.__popdown_cb)

        # Default to a normal window palette
        self._content_widget = None
        self.set_content(None)

    def _setup_widget(self):
        PaletteWindow._setup_widget(self)
        self._widget.connect('destroy', self.__destroy_cb)

    def __destroy_cb(self, palette):
        self._secondary_anim.stop()
        self.popdown(immediate=True)
        # Break the reference cycle. It looks like the gc is not able to free
        # it, possibly because Gtk.Menu memory handling is very special.
        self._widget = None

    def __popdown_cb(self, widget):
        self._secondary_anim.stop()

    def __notify_invoker_cb(self, palette, pspec):
        invoker = self.props.invoker
        if invoker is not None and hasattr(invoker.props, 'widget'):
            self._update_accel_widget()
            self._invoker.connect('notify::widget',
                                  self.__invoker_widget_changed_cb)

    def __invoker_widget_changed_cb(self, invoker, spec):
        self._update_accel_widget()

    def get_full_size_request(self):
        return self._full_request

    def popup(self, immediate=False, state=None):
        if self._invoker is not None:
            self._update_full_request()

        PaletteWindow.popup(self, immediate)

        if state is None:
            state = self.PRIMARY
        self.set_palette_state(state)

        if state == self.PRIMARY:
            self._secondary_anim.start()
        else:
            self._secondary_anim.stop()

    def popdown(self, immediate=False):
        if immediate:
            self._secondary_anim.stop()
            # to suppress glitches while later re-opening
            self.set_palette_state(self.PRIMARY)
            if self._widget:
                self._widget.size_request()
        PaletteWindow.popdown(self, immediate)

    def on_enter(self):
        PaletteWindow.on_enter(self)
        self._secondary_anim.start()

    def _add_content(self):
        # The content is not shown until a widget is added
        self._content = Gtk.VBox()
        self._content.set_border_width(style.DEFAULT_SPACING)
        self._secondary_box.pack_start(self._content, True, True, 0)

    def _update_accel_widget(self):
        assert self.props.invoker is not None
        self._label.props.accel_widget = self.props.invoker.props.widget

    def set_primary_text(self, label, accel_path=None):
        self._primary_text = label

        if label is not None:
            self._label.set_markup('<b>%s</b>' % label)
            self._label.show()

    def get_primary_text(self):
        return self._primary_text

    primary_text = GObject.property(type=str,
                                    getter=get_primary_text,
                                    setter=set_primary_text)

    def set_secondary_text(self, label):
        if label is not None:
            label = label.split('\n', 1)[0]
        self._secondary_text = label

        if label is None:
            self._secondary_label.hide()
        else:
            self._secondary_label.set_text(label)
            self._secondary_label.show()

    def get_secondary_text(self):
        return self._secondary_text

    secondary_text = GObject.property(type=str, getter=get_secondary_text,
        setter=set_secondary_text)

    def _show_icon(self):
        self._label_alignment.set_padding(0, 0, 0, style.DEFAULT_SPACING)
        self._icon_box.show()

    def _hide_icon(self):
        self._icon_box.hide()
        self._label_alignment.set_padding(0, 0, style.DEFAULT_SPACING,
                                          style.DEFAULT_SPACING)

    def set_icon(self, icon):
        if icon is None:
            self._icon = None
            self._hide_icon()
        else:
            if self._icon:
                self._icon_box.remove(self._icon_box.get_children()[0])

            event_box = Gtk.EventBox()
            event_box.connect('button-release-event',
                              self.__icon_button_release_event_cb)
            self._icon_box.pack_start(event_box, True, True, 0)
            event_box.show()

            self._icon = icon
            self._icon.props.icon_size = Gtk.IconSize.LARGE_TOOLBAR
            event_box.add(self._icon)
            self._icon.show()
            self._show_icon()

    def get_icon(self):
        return self._icon

    icon = GObject.property(type=object, getter=get_icon, setter=set_icon)

    def __icon_button_release_event_cb(self, icon, event):
        self.emit('activate')

    def set_icon_visible(self, visible):
        self._icon_visible = visible

        if visible and self._icon is not None:
            self._show_icon()
        else:
            self._hide_icon()

    def get_icon_visible(self):
        return self._icon_visilbe

    icon_visible = GObject.property(type=bool,
                                    default=True,
                                    getter=get_icon_visible,
                                    setter=set_icon_visible)

    def set_content(self, widget):
        assert self._widget is None \
                or isinstance(self._widget, _PaletteWindowWidget)

        if self._widget is None:
            self._widget = _PaletteWindowWidget(self)
            self._setup_widget()

            self._palette_box = Gtk.VBox()
            self._palette_box.pack_start(self._primary_box, False, True, 0)
            self._palette_box.pack_start(self._secondary_box, True, True, 0)

            self._widget.add(self._palette_box)
            self._palette_box.show()
            height = style.GRID_CELL_SIZE - 2 * self._widget.get_border_width()
            self._primary_box.set_size_request(-1, height)

        if self._content.get_children():
            self._content.remove(self._content.get_children()[0])

        if widget is not None:
            self._content.add(widget)
            self._content.show()
        else:
            self._content.hide()

        self._content_widget = widget

        self._update_accept_focus()
        self._update_separators()

    def get_label_width(self):
        # Gtk.AccelLabel request doesn't include the accelerator.
        label_width = self._label_alignment.get_preferred_width()[1] + \
                      self._label.get_accel_width()
        return label_width

    def _update_separators(self):
        visible = self._content.get_children()
        self._separator.props.visible = visible

    def _update_accept_focus(self):
        accept_focus = len(self._content.get_children())
        self._widget.set_accept_focus(accept_focus)

    def _update_full_request(self):
        if self._palette_state == self.PRIMARY:
            self._secondary_box.show()

        self._full_request = self._widget.size_request()

        if self._palette_state == self.PRIMARY:
            self._secondary_box.hide()

    def _set_palette_state(self, state):
        if self._palette_state == state:
            return

        if state == self.PRIMARY:
            self._secondary_box.hide()
        elif state == self.SECONDARY:
            self._secondary_box.show()
            self.update_position()

        self._palette_state = state

    def get_menu(self):
        assert self._content_widget is None

        if self._widget is None \
                or not isinstance(self._widget, _PaletteMenuWidget):
            if self._widget is not None:
                self._palette_box.remove(self._primary_box)
                self._palette_box.remove(self._secondary_box)
                self._teardown_widget()
                self._widget.destroy()

            self._widget = _PaletteMenuWidget()

            self._label_menuitem = _HeaderItem(self._primary_box)
            self._label_menuitem.show()
            self._widget.append(self._label_menuitem)

            separator = _HeaderSeparator()
            self._widget.append(separator)
            separator.show()

            self._setup_widget()

        return self._widget

    menu = GObject.property(type=object, getter=get_menu)


class PaletteActionBar(Gtk.HButtonBox):

    def add_action(self, label, icon_name=None):
        button = Gtk.Button(label)

        if icon_name:
            icon = Icon(icon_name)
            button.set_image(icon)
            icon.show()

        self.pack_start(button, True, True, 0)
        button.show()


class _SecondaryAnimation(animator.Animation):

    def __init__(self, palette):
        animator.Animation.__init__(self, 0.0, 1.0)
        self._palette = palette

    def next_frame(self, current):
        if current == 1.0:
            self._palette.set_palette_state(Palette.SECONDARY)