# Copyright (C) 2009, Tutorius.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 2 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 St, Fifth Floor, Boston, MA 02110-1301 USA import time import gobject import gtk, gtk.gdk from sugar.tutorius.actions import Action from sugar.tutorius.properties import TUAMProperty from sugar.tutorius.gtkutils import find_widget from sugar import profile # for easy profile access xo_line_color = profile.get_color().get_stroke_color() xo_fill_color = profile.get_color().get_fill_color() class ChangeColor(Action): """ ChangeColorEvent """ # widget address property widaddr = TUAMProperty("0") # set timeout timeout = 500 def __init__(self, widaddr=None): """Constructor - Change a widget color @param widaddr: the widget for which you want to change the color (UAM) """ Action.__init__(self) if widaddr: self.widaddr = widaddr self.init_style = None self._new_color = None self.wid = None self._handler_id = None def do(self, **kwargs): """ do. Change the color of the widaddr widget with the chosen color """ if not "activity" in kwargs: raise TypeError("activity argument is Mandatory") # get widget instance self.wid = find_widget(kwargs["activity"], self.widaddr, ignore_errors=False) if not self.wid: raise NameError("widget not found") # we have to get the initial color in the sugar rc theme self.init_style = self.wid.rc_get_style() # define new color self._new_color = gtk.gdk.color_parse(xo_fill_color) # set change color timeout (flash) self._handler_id = gobject.timeout_add(ChangeColor.timeout, self._timeout_cb) def undo(self): """ Remove timer and go back to the original color """ if self._handler_id: try: #remove the timer gobject.source_remove(self._handler_id) except: pass # modify bg color (go back to original color) self.wid.modify_bg(gtk.STATE_NORMAL, self.init_style.bg[gtk.STATE_NORMAL]) self.wid.modify_bg(gtk.STATE_PRELIGHT, self.init_style.bg[gtk.STATE_PRELIGHT]) self.wid.modify_bg(gtk.STATE_ACTIVE, self.init_style.bg[gtk.STATE_ACTIVE]) self.wid.modify_bg(gtk.STATE_INSENSITIVE, self.init_style.bg[gtk.STATE_INSENSITIVE]) def _timeout_cb(self): """ _timeout_cb triggers the eventfilter callback. """ if self.wid.rc_get_style().bg[gtk.STATE_NORMAL] == self._new_color: # modify bg color (go back to original color) self.wid.modify_bg(gtk.STATE_NORMAL, self.init_style.bg[gtk.STATE_NORMAL]) self.wid.modify_bg(gtk.STATE_PRELIGHT, self.init_style.bg[gtk.STATE_PRELIGHT]) self.wid.modify_bg(gtk.STATE_ACTIVE, self.init_style.bg[gtk.STATE_ACTIVE]) self.wid.modify_bg(gtk.STATE_INSENSITIVE, self.init_style.bg[gtk.STATE_INSENSITIVE]) else: # modify bg color (to new color) self.wid.modify_bg(gtk.STATE_NORMAL, self._new_color) self.wid.modify_bg(gtk.STATE_PRELIGHT, self._new_color) self.wid.modify_bg(gtk.STATE_ACTIVE, self._new_color) self.wid.modify_bg(gtk.STATE_INSENSITIVE, self._new_color) return True __action__ = { "name" : "ChangeColor", "display_name" : "Change widget color", "icon" : "message-bubble", "class" : ChangeColor, "mandatory_props" : ["widaddr"] }