2009-01-13 16:48:06 +01:00
|
|
|
# Copyright (C) 2007, Red Hat, Inc.
|
|
|
|
# Copyright (C) 2008, Benjamin Berg <benjamin@sipsolutions.net>
|
|
|
|
#
|
|
|
|
# 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.
|
|
|
|
|
|
|
|
import gettext
|
2011-12-15 02:23:18 +01:00
|
|
|
from gi.repository import Gdk
|
|
|
|
from gi.repository import GdkPixbuf
|
2011-11-15 19:29:07 +01:00
|
|
|
from gi.repository import Gtk
|
|
|
|
from gi.repository import GObject
|
2009-01-13 16:48:06 +01:00
|
|
|
import struct
|
2009-01-16 23:46:20 +01:00
|
|
|
import logging
|
2009-01-13 16:48:06 +01:00
|
|
|
|
2011-10-29 10:44:18 +02:00
|
|
|
from sugar3.graphics import style
|
|
|
|
from sugar3.graphics.icon import Icon
|
|
|
|
from sugar3.graphics.palette import Palette, ToolInvoker, WidgetInvoker
|
2009-01-13 16:48:06 +01:00
|
|
|
|
2009-08-25 21:12:40 +02:00
|
|
|
|
2009-01-13 16:48:06 +01:00
|
|
|
_ = lambda msg: gettext.dgettext('sugar-toolkit', msg)
|
|
|
|
|
2009-08-25 21:12:40 +02:00
|
|
|
|
2009-01-13 16:48:06 +01:00
|
|
|
def get_svg_color_string(color):
|
2009-08-25 19:55:48 +02:00
|
|
|
return '#%.2X%.2X%.2X' % (color.red / 257, color.green / 257,
|
2009-01-16 23:46:20 +01:00
|
|
|
color.blue / 257)
|
2009-01-13 16:48:06 +01:00
|
|
|
|
2009-08-25 21:12:40 +02:00
|
|
|
|
2011-11-15 19:29:07 +01:00
|
|
|
class _ColorButton(Gtk.Button):
|
|
|
|
"""This is a ColorButton for Sugar. It is similar to the Gtk.ColorButton,
|
2009-01-13 16:48:06 +01:00
|
|
|
but does not have any alpha support.
|
|
|
|
Instead of a color selector dialog it will pop up a Sugar palette.
|
|
|
|
|
2011-10-29 10:44:18 +02:00
|
|
|
As a preview an sugar3.graphics.Icon is used. The fill color will be set to
|
2009-01-13 16:48:06 +01:00
|
|
|
the current color, and the stroke color is set to the font color.
|
|
|
|
"""
|
2009-08-25 19:55:48 +02:00
|
|
|
|
2009-01-13 16:48:06 +01:00
|
|
|
__gtype_name__ = 'SugarColorButton'
|
2011-11-15 19:29:07 +01:00
|
|
|
__gsignals__ = {'color-set': (GObject.SignalFlags.RUN_FIRST, None,
|
2009-08-25 21:12:40 +02:00
|
|
|
tuple())}
|
2009-08-25 19:55:48 +02:00
|
|
|
|
2009-01-13 16:48:06 +01:00
|
|
|
def __init__(self, **kwargs):
|
|
|
|
self._title = _('Choose a color')
|
2011-11-15 19:29:07 +01:00
|
|
|
self._color = Gdk.Color(0, 0, 0)
|
2009-01-13 16:48:06 +01:00
|
|
|
self._has_palette = True
|
|
|
|
self._has_invoker = True
|
|
|
|
self._palette = None
|
|
|
|
self._accept_drag = True
|
|
|
|
|
|
|
|
self._preview = Icon(icon_name='color-preview',
|
2011-11-15 19:29:07 +01:00
|
|
|
icon_size=Gtk.IconSize.BUTTON)
|
2009-01-13 16:48:06 +01:00
|
|
|
|
2011-11-15 19:29:07 +01:00
|
|
|
GObject.GObject.__init__(self, **kwargs)
|
2009-01-13 16:48:06 +01:00
|
|
|
|
2012-08-08 15:32:31 +02:00
|
|
|
# FIXME Drag and drop is not working, SL #3796
|
2009-01-13 16:48:06 +01:00
|
|
|
if self._accept_drag:
|
2012-08-08 15:32:31 +02:00
|
|
|
self.drag_dest_set(Gtk.DestDefaults.MOTION |
|
|
|
|
Gtk.DestDefaults.HIGHLIGHT | Gtk.DestDefaults.DROP,
|
|
|
|
[Gtk.TargetEntry.new('application/x-color', 0, 0)],
|
|
|
|
Gdk.DragAction.COPY)
|
|
|
|
self.drag_source_set(Gdk.ModifierType.BUTTON1_MASK |
|
|
|
|
Gdk.ModifierType.BUTTON3_MASK,
|
|
|
|
[Gtk.TargetEntry.new('application/x-color', 0, 0)],
|
|
|
|
Gdk.DragAction.COPY)
|
2009-01-13 16:48:06 +01:00
|
|
|
self.connect('drag_data_received', self.__drag_data_received_cb)
|
|
|
|
self.connect('drag_data_get', self.__drag_data_get_cb)
|
|
|
|
|
|
|
|
self._preview.fill_color = get_svg_color_string(self._color)
|
2012-08-08 15:32:31 +02:00
|
|
|
self._preview.stroke_color = self._get_fg_style_color_str()
|
2009-01-13 16:48:06 +01:00
|
|
|
self.set_image(self._preview)
|
|
|
|
|
|
|
|
if self._has_palette and self._has_invoker:
|
|
|
|
self._invoker = WidgetInvoker(self)
|
|
|
|
# FIXME: This is a hack.
|
2009-08-25 21:12:40 +02:00
|
|
|
self._invoker.has_rectangle_gap = lambda: False
|
2009-01-13 16:48:06 +01:00
|
|
|
self._invoker.palette = self._palette
|
|
|
|
|
|
|
|
def create_palette(self):
|
|
|
|
if self._has_palette:
|
|
|
|
self._palette = _ColorPalette(color=self._color,
|
|
|
|
primary_text=self._title)
|
|
|
|
self._palette.connect('color-set', self.__palette_color_set_cb)
|
2009-08-25 21:12:40 +02:00
|
|
|
self._palette.connect('notify::color', self.
|
|
|
|
__palette_color_changed)
|
2009-01-13 16:48:06 +01:00
|
|
|
|
|
|
|
return self._palette
|
|
|
|
|
|
|
|
def __palette_color_set_cb(self, palette):
|
|
|
|
self.emit('color-set')
|
2009-08-25 19:55:48 +02:00
|
|
|
|
2009-01-13 16:48:06 +01:00
|
|
|
def __palette_color_changed(self, palette, pspec):
|
|
|
|
self.color = self._palette.color
|
2009-08-25 19:55:48 +02:00
|
|
|
|
2009-01-13 16:48:06 +01:00
|
|
|
def do_style_set(self, previous_style):
|
2012-08-08 15:32:31 +02:00
|
|
|
self._preview.stroke_color = self._get_fg_style_color_str()
|
|
|
|
|
|
|
|
def _get_fg_style_color_str(self):
|
|
|
|
context = self.get_style_context()
|
|
|
|
fg_color = context.get_color(Gtk.StateType.NORMAL)
|
|
|
|
# the color components are stored as float values between 0.0 and 1.0
|
|
|
|
return '#%.2X%.2X%.2X' % (fg_color.red * 255, fg_color.green * 255,
|
|
|
|
fg_color.blue * 255)
|
2009-01-13 16:48:06 +01:00
|
|
|
|
|
|
|
def set_color(self, color):
|
2011-11-15 19:29:07 +01:00
|
|
|
assert isinstance(color, Gdk.Color)
|
2009-08-25 19:55:48 +02:00
|
|
|
|
2009-01-13 16:48:06 +01:00
|
|
|
if self._color.red == color.red and \
|
|
|
|
self._color.green == color.green and \
|
|
|
|
self._color.blue == color.blue:
|
|
|
|
return
|
2009-08-25 19:55:48 +02:00
|
|
|
|
2011-11-15 19:29:07 +01:00
|
|
|
self._color = Gdk.Color(color.red, color.green, color.blue)
|
2009-01-13 16:48:06 +01:00
|
|
|
self._preview.fill_color = get_svg_color_string(self._color)
|
|
|
|
if self._palette:
|
|
|
|
self._palette.props.color = self._color
|
|
|
|
self.notify('color')
|
|
|
|
|
|
|
|
def get_color(self):
|
|
|
|
return self._color
|
|
|
|
|
2011-11-15 19:29:07 +01:00
|
|
|
color = GObject.property(type=object, getter=get_color, setter=set_color)
|
2009-01-13 16:48:06 +01:00
|
|
|
|
|
|
|
def set_icon_name(self, icon_name):
|
|
|
|
self._preview.props.icon_name = icon_name
|
|
|
|
|
|
|
|
def get_icon_name(self):
|
|
|
|
return self._preview.props.icon_name
|
|
|
|
|
2011-11-15 19:29:07 +01:00
|
|
|
icon_name = GObject.property(type=str,
|
2009-01-13 16:48:06 +01:00
|
|
|
getter=get_icon_name, setter=set_icon_name)
|
|
|
|
|
|
|
|
def set_icon_size(self, icon_size):
|
|
|
|
self._preview.props.icon_size = icon_size
|
|
|
|
|
|
|
|
def get_icon_size(self):
|
|
|
|
return self._preview.props.icon_size
|
|
|
|
|
2011-11-15 19:29:07 +01:00
|
|
|
icon_size = GObject.property(type=int,
|
2009-01-13 16:48:06 +01:00
|
|
|
getter=get_icon_size, setter=set_icon_size)
|
|
|
|
|
|
|
|
def set_title(self, title):
|
|
|
|
self._title = title
|
|
|
|
if self._palette:
|
|
|
|
self._palette.primary_text = self._title
|
|
|
|
|
|
|
|
def get_title(self):
|
|
|
|
return self._title
|
|
|
|
|
2011-11-15 19:29:07 +01:00
|
|
|
title = GObject.property(type=str, getter=get_title, setter=set_title)
|
2009-01-13 16:48:06 +01:00
|
|
|
|
|
|
|
def _set_has_invoker(self, has_invoker):
|
|
|
|
self._has_invoker = has_invoker
|
|
|
|
|
|
|
|
def _get_has_invoker(self):
|
|
|
|
return self._has_invoker
|
|
|
|
|
2011-11-15 19:29:07 +01:00
|
|
|
has_invoker = GObject.property(type=bool, default=True,
|
|
|
|
flags=GObject.PARAM_READWRITE |
|
|
|
|
GObject.PARAM_CONSTRUCT_ONLY,
|
2009-01-13 16:48:06 +01:00
|
|
|
getter=_get_has_invoker,
|
|
|
|
setter=_set_has_invoker)
|
|
|
|
|
|
|
|
def _set_has_palette(self, has_palette):
|
|
|
|
self._has_palette = has_palette
|
|
|
|
|
|
|
|
def _get_has_palette(self):
|
|
|
|
return self._has_palette
|
|
|
|
|
2011-11-15 19:29:07 +01:00
|
|
|
has_palette = GObject.property(type=bool, default=True,
|
|
|
|
flags=GObject.PARAM_READWRITE |
|
|
|
|
GObject.PARAM_CONSTRUCT_ONLY,
|
2009-01-13 16:48:06 +01:00
|
|
|
getter=_get_has_palette,
|
|
|
|
setter=_set_has_palette)
|
|
|
|
|
|
|
|
def _set_accept_drag(self, accept_drag):
|
|
|
|
self._accept_drag = accept_drag
|
|
|
|
|
|
|
|
def _get_accept_drag(self):
|
|
|
|
return self._accept_drag
|
|
|
|
|
2011-11-15 19:29:07 +01:00
|
|
|
accept_drag = GObject.property(type=bool, default=True,
|
|
|
|
flags=GObject.PARAM_READWRITE |
|
|
|
|
GObject.PARAM_CONSTRUCT_ONLY,
|
2009-01-13 16:48:06 +01:00
|
|
|
getter=_get_accept_drag,
|
|
|
|
setter=_set_accept_drag)
|
|
|
|
|
|
|
|
def __drag_begin_cb(self, widget, context):
|
2009-08-25 21:12:40 +02:00
|
|
|
# Drag and Drop
|
2011-11-15 19:29:07 +01:00
|
|
|
pixbuf = GdkPixbuf.Pixbuf(GdkPixbuf.Colorspace.RGB, True, 8,
|
2009-01-13 16:48:06 +01:00
|
|
|
style.SMALL_ICON_SIZE,
|
|
|
|
style.SMALL_ICON_SIZE)
|
|
|
|
|
|
|
|
red = self._color.red / 257
|
|
|
|
green = self._color.green / 257
|
|
|
|
blue = self._color.blue / 257
|
2009-08-25 19:55:48 +02:00
|
|
|
|
2009-01-13 16:48:06 +01:00
|
|
|
pixbuf.fill(red << 24 + green << 16 + blue << 8 + 0xff)
|
2009-08-25 19:55:48 +02:00
|
|
|
|
2009-01-13 16:48:06 +01:00
|
|
|
context.set_icon_pixbuf(pixbuf)
|
|
|
|
|
|
|
|
def __drag_data_get_cb(self, widget, context, selection_data, info, time):
|
|
|
|
data = struct.pack('=HHHH', self._color.red, self._color.green,
|
|
|
|
self._color.blue, 65535)
|
|
|
|
selection_data.set(selection_data.target, 16, data)
|
|
|
|
|
|
|
|
def __drag_data_received_cb(self, widget, context, x, y, selection_data, \
|
|
|
|
info, time):
|
|
|
|
if len(selection_data.data) != 8:
|
2009-01-16 23:46:20 +01:00
|
|
|
return
|
2009-01-13 16:48:06 +01:00
|
|
|
|
|
|
|
dropped = selection_data.data
|
|
|
|
red = struct.unpack_from('=H', dropped, 0)[0]
|
|
|
|
green = struct.unpack_from('=H', dropped, 2)[0]
|
|
|
|
blue = struct.unpack_from('=H', dropped, 4)[0]
|
|
|
|
# dropped[6] and dropped[7] is alpha, but we ignore the alpha channel
|
|
|
|
|
2011-11-15 19:29:07 +01:00
|
|
|
color = Gdk.Color(red, green, blue)
|
2009-01-13 16:48:06 +01:00
|
|
|
self.set_color(color)
|
|
|
|
|
|
|
|
|
|
|
|
class _ColorPalette(Palette):
|
|
|
|
"""This is a color picker palette. It will usually be used indirectly
|
2011-10-29 10:44:18 +02:00
|
|
|
trough a sugar3.graphics.ColorButton.
|
2009-01-13 16:48:06 +01:00
|
|
|
"""
|
|
|
|
_RED = 0
|
|
|
|
_GREEN = 1
|
|
|
|
_BLUE = 2
|
|
|
|
|
|
|
|
__gtype_name__ = 'SugarColorPalette'
|
2009-08-25 21:12:40 +02:00
|
|
|
|
2009-01-13 16:48:06 +01:00
|
|
|
# The color-set signal is emitted when the user is finished selecting
|
|
|
|
# a color.
|
2011-11-15 19:29:07 +01:00
|
|
|
__gsignals__ = {'color-set': (GObject.SignalFlags.RUN_FIRST, None,
|
2009-08-25 21:12:40 +02:00
|
|
|
tuple())}
|
2009-01-13 16:48:06 +01:00
|
|
|
|
|
|
|
def __init__(self, **kwargs):
|
2011-11-15 19:29:07 +01:00
|
|
|
self._color = Gdk.Color(0, 0, 0)
|
2009-01-13 16:48:06 +01:00
|
|
|
self._previous_color = self._color.copy()
|
|
|
|
self._scales = None
|
|
|
|
|
|
|
|
Palette.__init__(self, **kwargs)
|
|
|
|
|
|
|
|
self.connect('popup', self.__popup_cb)
|
|
|
|
self.connect('popdown', self.__popdown_cb)
|
|
|
|
|
2011-11-15 19:29:07 +01:00
|
|
|
self._picker_hbox = Gtk.HBox()
|
2009-01-13 16:48:06 +01:00
|
|
|
self.set_content(self._picker_hbox)
|
|
|
|
|
2011-11-15 19:29:07 +01:00
|
|
|
self._swatch_tray = Gtk.Table()
|
2009-01-13 16:48:06 +01:00
|
|
|
|
2011-11-15 19:29:07 +01:00
|
|
|
self._picker_hbox.pack_start(self._swatch_tray, True, True, 0)
|
2011-12-15 02:23:18 +01:00
|
|
|
self._picker_hbox.pack_start(Gtk.VSeparator(), True, True,
|
2009-01-13 16:48:06 +01:00
|
|
|
padding=style.DEFAULT_SPACING)
|
2009-08-25 19:55:48 +02:00
|
|
|
|
2011-11-15 19:29:07 +01:00
|
|
|
self._chooser_table = Gtk.Table(3, 2)
|
2009-01-13 16:48:06 +01:00
|
|
|
self._chooser_table.set_col_spacing(0, style.DEFAULT_PADDING)
|
|
|
|
|
|
|
|
self._scales = []
|
|
|
|
self._scales.append(
|
|
|
|
self._create_color_scale(_('Red'), self._RED, 0))
|
|
|
|
self._scales.append(
|
|
|
|
self._create_color_scale(_('Green'), self._GREEN, 1))
|
|
|
|
self._scales.append(
|
|
|
|
self._create_color_scale(_('Blue'), self._BLUE, 2))
|
|
|
|
|
|
|
|
self._picker_hbox.add(self._chooser_table)
|
2009-08-25 19:55:48 +02:00
|
|
|
|
2009-01-13 16:48:06 +01:00
|
|
|
self._picker_hbox.show_all()
|
|
|
|
|
|
|
|
self._build_swatches()
|
|
|
|
|
|
|
|
def _create_color_scale(self, text, color, row):
|
2011-11-15 19:29:07 +01:00
|
|
|
label = Gtk.Label(label=text)
|
2009-01-13 16:48:06 +01:00
|
|
|
label.props.xalign = 1.0
|
2011-11-15 19:29:07 +01:00
|
|
|
scale = Gtk.HScale()
|
2009-01-13 16:48:06 +01:00
|
|
|
scale.set_size_request(style.zoom(250), -1)
|
|
|
|
scale.set_draw_value(False)
|
|
|
|
scale.set_range(0, 1.0)
|
|
|
|
scale.set_increments(0.1, 0.2)
|
|
|
|
|
|
|
|
if color == self._RED:
|
|
|
|
scale.set_value(self._color.red / 65535.0)
|
|
|
|
elif color == self._GREEN:
|
|
|
|
scale.set_value(self._color.green / 65535.0)
|
|
|
|
elif color == self._BLUE:
|
|
|
|
scale.set_value(self._color.blue / 65535.0)
|
|
|
|
|
|
|
|
scale.connect('value-changed',
|
|
|
|
self.__scale_value_changed_cb,
|
|
|
|
color)
|
|
|
|
self._chooser_table.attach(label, 0, 1, row, row + 1)
|
|
|
|
self._chooser_table.attach(scale, 1, 2, row, row + 1)
|
|
|
|
|
|
|
|
return scale
|
|
|
|
|
|
|
|
def _build_swatches(self):
|
|
|
|
for child in self._swatch_tray.get_children():
|
|
|
|
child.destroy()
|
|
|
|
|
|
|
|
# Use a hardcoded list of colors for now.
|
|
|
|
colors = ['#ed2529', '#69bc47', '#3c54a3',
|
|
|
|
'#f57f25', '#0b6b3a', '#00a0c6',
|
|
|
|
'#f6eb1a', '#b93f94', '#5b4a9c',
|
|
|
|
'#000000', '#919496', '#ffffff']
|
|
|
|
|
|
|
|
# We want 3 rows of colors.
|
|
|
|
rows = 3
|
|
|
|
i = 0
|
|
|
|
self._swatch_tray.props.n_rows = rows
|
|
|
|
self._swatch_tray.props.n_columns = (len(colors) + rows - 1) / rows
|
|
|
|
for color in colors:
|
|
|
|
button = _ColorButton(has_palette=False,
|
2011-11-15 19:29:07 +01:00
|
|
|
color=Gdk.color_parse(color),
|
2009-01-13 16:48:06 +01:00
|
|
|
accept_drag=False,
|
2011-11-15 19:29:07 +01:00
|
|
|
icon_size=Gtk.IconSize.LARGE_TOOLBAR)
|
|
|
|
button.set_relief(Gtk.ReliefStyle.NONE)
|
2009-01-13 16:48:06 +01:00
|
|
|
self._swatch_tray.attach(button,
|
|
|
|
i % rows, i % rows + 1,
|
|
|
|
i / rows, i / rows + 1,
|
|
|
|
yoptions=0, xoptions=0)
|
|
|
|
button.connect('clicked', self.__swatch_button_clicked_cb)
|
|
|
|
i += 1
|
|
|
|
|
|
|
|
self._swatch_tray.show_all()
|
|
|
|
|
|
|
|
def __popup_cb(self, palette):
|
|
|
|
self._previous_color = self._color.copy()
|
|
|
|
|
|
|
|
def __popdown_cb(self, palette):
|
|
|
|
self.emit('color-set')
|
|
|
|
|
|
|
|
def __scale_value_changed_cb(self, widget, color):
|
|
|
|
new_color = self._color.copy()
|
|
|
|
if color == self._RED:
|
|
|
|
new_color.red = int(65535 * widget.get_value())
|
|
|
|
elif color == self._GREEN:
|
|
|
|
new_color.green = int(65535 * widget.get_value())
|
|
|
|
elif color == self._BLUE:
|
|
|
|
new_color.blue = int(65535 * widget.get_value())
|
|
|
|
self.color = new_color
|
|
|
|
|
|
|
|
def do_key_press_event(self, event):
|
2011-11-15 19:29:07 +01:00
|
|
|
if event.keyval == Gdk.KEY_Escape:
|
2009-01-13 16:48:06 +01:00
|
|
|
self.props.color = self._previous_color
|
|
|
|
self.popdown(immediate=True)
|
|
|
|
return True
|
2011-11-15 19:29:07 +01:00
|
|
|
elif event.keyval == Gdk.KEY_Return:
|
2009-01-13 16:48:06 +01:00
|
|
|
self.popdown(immediate=True)
|
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
|
|
|
def __swatch_button_clicked_cb(self, button):
|
|
|
|
self.props.color = button.get_color()
|
|
|
|
|
|
|
|
def set_color(self, color):
|
2011-11-15 19:29:07 +01:00
|
|
|
assert isinstance(color, Gdk.Color)
|
2009-01-13 16:48:06 +01:00
|
|
|
|
|
|
|
if self._color.red == color.red and \
|
|
|
|
self._color.green == color.green and \
|
|
|
|
self._color.blue == color.blue:
|
|
|
|
return
|
|
|
|
|
|
|
|
self._color = color.copy()
|
2009-08-25 19:55:48 +02:00
|
|
|
|
2009-01-13 16:48:06 +01:00
|
|
|
if self._scales:
|
|
|
|
self._scales[self._RED].set_value(self._color.red / 65535.0)
|
|
|
|
self._scales[self._GREEN].set_value(self._color.green / 65535.0)
|
|
|
|
self._scales[self._BLUE].set_value(self._color.blue / 65535.0)
|
2009-08-25 19:55:48 +02:00
|
|
|
|
2009-01-13 16:48:06 +01:00
|
|
|
self.notify('color')
|
|
|
|
|
|
|
|
def get_color(self):
|
|
|
|
return self._color
|
|
|
|
|
2011-11-15 19:29:07 +01:00
|
|
|
color = GObject.property(type=object, getter=get_color, setter=set_color)
|
2009-01-13 16:48:06 +01:00
|
|
|
|
|
|
|
|
|
|
|
def _add_accelerator(tool_button):
|
|
|
|
if not tool_button.props.accelerator or not tool_button.get_toplevel() or \
|
2011-11-15 19:29:07 +01:00
|
|
|
not tool_button.get_child():
|
2009-01-13 16:48:06 +01:00
|
|
|
return
|
|
|
|
|
|
|
|
# TODO: should we remove the accelerator from the prev top level?
|
|
|
|
|
2012-06-06 12:57:00 +02:00
|
|
|
if not hasattr(tool_button.get_toplevel(), 'sugar_accel_group'):
|
2011-11-15 19:29:07 +01:00
|
|
|
logging.warning('No Gtk.AccelGroup in the top level window.')
|
2009-01-13 16:48:06 +01:00
|
|
|
return
|
|
|
|
|
2012-06-06 12:57:00 +02:00
|
|
|
accel_group = tool_button.get_toplevel().sugar_accel_group
|
2011-11-15 19:29:07 +01:00
|
|
|
keyval, mask = Gtk.accelerator_parse(tool_button.props.accelerator)
|
|
|
|
# the accelerator needs to be set at the child, so the Gtk.AccelLabel
|
2009-01-13 16:48:06 +01:00
|
|
|
# in the palette can pick it up.
|
2011-11-15 19:29:07 +01:00
|
|
|
tool_button.get_child().add_accelerator('clicked', accel_group, keyval, mask,
|
|
|
|
Gtk.AccelFlags.LOCKED | Gtk.AccelFlags.VISIBLE)
|
2009-01-13 16:48:06 +01:00
|
|
|
|
2009-08-25 21:12:40 +02:00
|
|
|
|
2009-01-13 16:48:06 +01:00
|
|
|
def _hierarchy_changed_cb(tool_button, previous_toplevel):
|
|
|
|
_add_accelerator(tool_button)
|
|
|
|
|
2009-08-25 21:12:40 +02:00
|
|
|
|
2009-01-13 16:48:06 +01:00
|
|
|
def setup_accelerator(tool_button):
|
|
|
|
_add_accelerator(tool_button)
|
|
|
|
tool_button.connect('hierarchy-changed', _hierarchy_changed_cb)
|
|
|
|
|
2009-08-25 21:12:40 +02:00
|
|
|
|
2011-11-15 19:29:07 +01:00
|
|
|
class ColorToolButton(Gtk.ToolItem):
|
|
|
|
# This not ideal. It would be better to subclass Gtk.ToolButton, however
|
2009-08-25 21:12:40 +02:00
|
|
|
# the python bindings do not seem to be powerfull enough for that.
|
|
|
|
# (As we need to change a variable in the class structure.)
|
|
|
|
|
2009-01-13 16:48:06 +01:00
|
|
|
__gtype_name__ = 'SugarColorToolButton'
|
2011-11-15 19:29:07 +01:00
|
|
|
__gsignals__ = {'color-set': (GObject.SignalFlags.RUN_FIRST, None,
|
2009-08-25 21:12:40 +02:00
|
|
|
tuple())}
|
2009-01-13 16:48:06 +01:00
|
|
|
|
|
|
|
def __init__(self, icon_name='color-preview', **kwargs):
|
|
|
|
self._accelerator = None
|
|
|
|
self._tooltip = None
|
|
|
|
self._palette_invoker = ToolInvoker()
|
|
|
|
self._palette = None
|
|
|
|
|
2011-11-15 19:29:07 +01:00
|
|
|
GObject.GObject.__init__(self, **kwargs)
|
2009-01-13 16:48:06 +01:00
|
|
|
|
2011-11-15 19:29:07 +01:00
|
|
|
# The Gtk.ToolButton has already added a normal button.
|
2009-01-13 16:48:06 +01:00
|
|
|
# Replace it with a ColorButton
|
|
|
|
color_button = _ColorButton(icon_name=icon_name, has_invoker=False)
|
|
|
|
self.add(color_button)
|
2012-08-08 15:32:31 +02:00
|
|
|
color_button.show()
|
2009-01-13 16:48:06 +01:00
|
|
|
|
|
|
|
# The following is so that the behaviour on the toolbar is correct.
|
2011-11-15 19:29:07 +01:00
|
|
|
color_button.set_relief(Gtk.ReliefStyle.NONE)
|
|
|
|
color_button.icon_size = Gtk.IconSize.LARGE_TOOLBAR
|
2009-01-13 16:48:06 +01:00
|
|
|
|
|
|
|
self._palette_invoker.attach_tool(self)
|
2012-11-15 18:09:00 +01:00
|
|
|
self._palette_invoker.props.toggle_palette = True
|
|
|
|
self._palette_invoker.props.lock_palette = True
|
2009-01-13 16:48:06 +01:00
|
|
|
|
|
|
|
# This widget just proxies the following properties to the colorbutton
|
|
|
|
color_button.connect('notify::color', self.__notify_change)
|
|
|
|
color_button.connect('notify::icon-name', self.__notify_change)
|
|
|
|
color_button.connect('notify::icon-size', self.__notify_change)
|
|
|
|
color_button.connect('notify::title', self.__notify_change)
|
|
|
|
color_button.connect('color-set', self.__color_set_cb)
|
|
|
|
color_button.connect('can-activate-accel',
|
|
|
|
self.__button_can_activate_accel_cb)
|
|
|
|
|
|
|
|
def __button_can_activate_accel_cb(self, button, signal_id):
|
|
|
|
# Accept activation via accelerators regardless of this widget's state
|
|
|
|
return True
|
|
|
|
|
|
|
|
def set_accelerator(self, accelerator):
|
|
|
|
self._accelerator = accelerator
|
|
|
|
setup_accelerator(self)
|
|
|
|
|
|
|
|
def get_accelerator(self):
|
|
|
|
return self._accelerator
|
|
|
|
|
2011-11-15 19:29:07 +01:00
|
|
|
accelerator = GObject.property(type=str, setter=set_accelerator,
|
2009-01-13 16:48:06 +01:00
|
|
|
getter=get_accelerator)
|
|
|
|
|
|
|
|
def create_palette(self):
|
|
|
|
self._palette = self.get_child().create_palette()
|
|
|
|
return self._palette
|
|
|
|
|
|
|
|
def get_palette_invoker(self):
|
|
|
|
return self._palette_invoker
|
|
|
|
|
|
|
|
def set_palette_invoker(self, palette_invoker):
|
|
|
|
self._palette_invoker.detach()
|
|
|
|
self._palette_invoker = palette_invoker
|
|
|
|
|
2011-11-15 19:29:07 +01:00
|
|
|
palette_invoker = GObject.property(
|
2009-01-13 16:48:06 +01:00
|
|
|
type=object, setter=set_palette_invoker, getter=get_palette_invoker)
|
|
|
|
|
2012-11-15 18:09:00 +01:00
|
|
|
def set_expanded(self, expanded):
|
|
|
|
box = self.toolbar_box
|
|
|
|
if not box:
|
|
|
|
return
|
|
|
|
|
|
|
|
if not expanded:
|
|
|
|
self._palette_invoker.notify_popdown()
|
|
|
|
return
|
|
|
|
|
|
|
|
if box.expanded_button is not None:
|
|
|
|
box.expanded_button.queue_draw()
|
|
|
|
if box.expanded_button != self:
|
|
|
|
box.expanded_button.set_expanded(False)
|
|
|
|
box.expanded_button = self
|
|
|
|
|
|
|
|
def get_toolbar_box(self):
|
|
|
|
parent = self.get_parent()
|
|
|
|
if not hasattr(parent, 'owner'):
|
|
|
|
return None
|
|
|
|
return parent.owner
|
|
|
|
|
|
|
|
toolbar_box = property(get_toolbar_box)
|
|
|
|
|
2009-01-13 16:48:06 +01:00
|
|
|
def set_color(self, color):
|
|
|
|
self.get_child().props.color = color
|
|
|
|
|
|
|
|
def get_color(self):
|
|
|
|
return self.get_child().props.color
|
|
|
|
|
2011-11-15 19:29:07 +01:00
|
|
|
color = GObject.property(type=object, getter=get_color, setter=set_color)
|
2009-01-13 16:48:06 +01:00
|
|
|
|
|
|
|
def set_icon_name(self, icon_name):
|
|
|
|
self.get_child().props.icon_name = icon_name
|
|
|
|
|
|
|
|
def get_icon_name(self):
|
|
|
|
return self.get_child().props.icon_name
|
|
|
|
|
2011-11-15 19:29:07 +01:00
|
|
|
icon_name = GObject.property(type=str,
|
2009-01-13 16:48:06 +01:00
|
|
|
getter=get_icon_name, setter=set_icon_name)
|
|
|
|
|
|
|
|
def set_icon_size(self, icon_size):
|
|
|
|
self.get_child().props.icon_size = icon_size
|
|
|
|
|
|
|
|
def get_icon_size(self):
|
|
|
|
return self.get_child().props.icon_size
|
|
|
|
|
2011-11-15 19:29:07 +01:00
|
|
|
icon_size = GObject.property(type=int,
|
2009-01-13 16:48:06 +01:00
|
|
|
getter=get_icon_size, setter=set_icon_size)
|
|
|
|
|
|
|
|
def set_title(self, title):
|
|
|
|
self.get_child().props.title = title
|
|
|
|
|
|
|
|
def get_title(self):
|
|
|
|
return self.get_child().props.title
|
|
|
|
|
2011-11-15 19:29:07 +01:00
|
|
|
title = GObject.property(type=str, getter=get_title, setter=set_title)
|
2009-01-13 16:48:06 +01:00
|
|
|
|
2012-08-24 02:08:24 +02:00
|
|
|
def do_draw(self, cr):
|
2009-01-13 16:48:06 +01:00
|
|
|
child = self.get_child()
|
|
|
|
if self._palette and self._palette.is_up():
|
2012-08-24 02:08:24 +02:00
|
|
|
allocation = self.get_allocation()
|
|
|
|
# draw a black background, has been done by the engine before
|
|
|
|
cr.set_source_rgb(0, 0, 0)
|
|
|
|
cr.rectangle(0, 0, allocation.width, allocation.height)
|
|
|
|
cr.paint()
|
|
|
|
|
2012-12-11 05:19:12 +01:00
|
|
|
Gtk.ToolItem.do_draw(self, cr)
|
|
|
|
|
|
|
|
if self._palette and self._palette.is_up():
|
|
|
|
invoker = self._palette.props.invoker
|
|
|
|
invoker.draw_rectangle(cr, self._palette)
|
|
|
|
|
|
|
|
return False
|
2009-01-13 16:48:06 +01:00
|
|
|
|
|
|
|
def __notify_change(self, widget, pspec):
|
|
|
|
self.notify(pspec.name)
|
|
|
|
|
|
|
|
def __color_set_cb(self, widget):
|
|
|
|
self.emit('color-set')
|