From acd0bec92f85b8c9ecada0c54576a9e5f24145ec Mon Sep 17 00:00:00 2001 From: Reinier Heeres Date: Thu, 22 Nov 2007 17:04:58 +0100 Subject: [PATCH] Proper clear icon in Mesh view and journal, #4311 --- lib/sugar/_sugarext.defs | 2 +- lib/sugar/graphics/iconentry.py | 64 ++++++++++++++++++++++++++++++++- 2 files changed, 64 insertions(+), 2 deletions(-) diff --git a/lib/sugar/_sugarext.defs b/lib/sugar/_sugarext.defs index 151bc92f..1c9812ef 100644 --- a/lib/sugar/_sugarext.defs +++ b/lib/sugar/_sugarext.defs @@ -119,7 +119,7 @@ (return-type "none") (parameters '("SexyIconEntryPosition" "position") - '("GtkImage*" "icon") + '("GtkImage*" "icon" (null-ok)) ) ) diff --git a/lib/sugar/graphics/iconentry.py b/lib/sugar/graphics/iconentry.py index 5bd87140..6990eea8 100644 --- a/lib/sugar/graphics/iconentry.py +++ b/lib/sugar/graphics/iconentry.py @@ -19,16 +19,37 @@ import gtk from sugar import _sugarext +from sugar.graphics.icon import _SVGLoader +import sugar.profile + ICON_ENTRY_PRIMARY = _sugarext.ICON_ENTRY_PRIMARY ICON_ENTRY_SECONDARY = _sugarext.ICON_ENTRY_SECONDARY class IconEntry(_sugarext.IconEntry): + + def __init__(self): + _sugarext.IconEntry.__init__(self) + + self._clear_icon = None + self._clear_shown = False + + self.connect('key_press_event', self._keypress_event_cb) + def set_icon_from_name(self, position, name): icon_theme = gtk.icon_theme_get_default() icon_info = icon_theme.lookup_icon(name, gtk.ICON_SIZE_SMALL_TOOLBAR, 0) - pixbuf = gtk.gdk.pixbuf_new_from_file(icon_info.get_filename()) + + if icon_info.get_filename().endswith('.svg'): + loader = _SVGLoader() + color = sugar.profile.get_color() + entities = {'fill_color': color.get_fill_color(), + 'stroke_color': color.get_stroke_color()} + handle = loader.load(icon_info.get_filename(), entities, None) + pixbuf = handle.get_pixbuf() + else: + pixbuf = gtk.gdk.pixbuf_new_from_file(icon_info.get_filename()) del icon_info image = gtk.Image() @@ -43,3 +64,44 @@ class IconEntry(_sugarext.IconEntry): 'stock, not %r.' % image.get_storage_type()) _sugarext.IconEntry.set_icon(self, position, image) + def remove_icon(self, position): + _sugarext.IconEntry.set_icon(self, position, None) + + def add_clear_button(self): + if self.props.text != "": + self.show_clear_button() + else: + self.hide_clear_button() + + self.connect('icon-pressed', self._icon_pressed_cb) + self.connect('changed', self._changed_cb) + + def show_clear_button(self): + if not self._clear_shown: + self.set_icon_from_name(ICON_ENTRY_SECONDARY, + 'dialog-cancel') + self._clear_shown = True + + def hide_clear_button(self): + if self._clear_shown: + self.remove_icon(ICON_ENTRY_SECONDARY) + self._clear_shown = False + + def _keypress_event_cb(self, widget, event): + keyval = gtk.gdk.keyval_name(event.keyval) + if keyval == 'Escape': + self.props.text = '' + return True + return False + + def _icon_pressed_cb(self, entru, icon_pos, button): + if icon_pos == ICON_ENTRY_SECONDARY: + self.set_text('') + self.hide_clear_button() + + def _changed_cb(self, icon_entry): + if not self.props.text: + self.hide_clear_button() + else: + self.show_clear_button() +