sugar-toolkit-gtk3/sugar/graphics/entry.py

129 lines
4.8 KiB
Python
Raw Normal View History

2007-02-09 14:47:11 +01:00
# Copyright (C) 2007, One Laptop Per Child
#
# 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 math
import logging
2007-02-09 17:34:08 +01:00
import gobject
2007-02-09 14:47:11 +01:00
import gtk
2007-02-09 17:34:08 +01:00
import hippo
import pango
2007-02-09 14:47:11 +01:00
from sugar.graphics import style
2007-02-09 14:47:11 +01:00
from sugar.graphics.color import Color
from sugar.graphics.button import Button
from sugar.graphics.roundbox import RoundBox
2007-02-09 14:47:11 +01:00
class Entry(hippo.CanvasBox, hippo.CanvasItem):
__gtype_name__ = 'SugarEntry'
2007-02-09 17:34:08 +01:00
__gproperties__ = {
'text' : (str, None, None, None,
gobject.PARAM_READWRITE)
}
2007-02-10 17:42:11 +01:00
__gsignals__ = {
'button-activated': (gobject.SIGNAL_RUN_FIRST, gobject.TYPE_NONE, ([int]))
}
2007-02-09 14:47:11 +01:00
def __init__(self):
hippo.CanvasBox.__init__(self, orientation=hippo.ORIENTATION_HORIZONTAL)
self.props.yalign = hippo.ALIGNMENT_CENTER
self._buttons = {}
2007-02-09 14:47:11 +01:00
self._round_box = RoundBox()
self._round_box.props.border_color = Color.FRAME_BORDER.get_int()
self.append(self._round_box, hippo.PACK_EXPAND)
2007-02-09 14:47:11 +01:00
self._entry = self.create_entry()
2007-02-09 14:47:11 +01:00
self._entry.props.has_frame = False
2007-02-09 17:34:08 +01:00
self._update_colors(focused=False)
self._entry.modify_text(gtk.STATE_SELECTED,
Color.BLACK.get_gdk_color())
2007-02-09 14:47:11 +01:00
self._entry.connect('focus-in-event', self._entry_focus_in_event_cb)
self._entry.connect('focus-out-event', self._entry_focus_out_event_cb)
self._entry.connect('activate', self._entry_activate_cb)
fd = pango.FontDescription()
fd.set_size(int(round(style.default_font_size * pango.SCALE)))
self._entry.modify_font(fd)
2007-02-09 17:34:08 +01:00
2007-02-09 14:47:11 +01:00
self._canvas_widget = hippo.CanvasWidget()
self._canvas_widget.props.widget = self._entry
self._round_box.append(self._canvas_widget, hippo.PACK_EXPAND)
2007-02-13 17:08:27 +01:00
def create_entry(self):
"""
Subclasses can override this method in order to provide a different
entry widget.
"""
return gtk.Entry()
def add_button(self, icon_name, action_id):
button = Button(icon_name=icon_name)
button.props.scale = style.small_icon_scale
button.props.yalign = hippo.ALIGNMENT_CENTER
button.props.xalign = hippo.ALIGNMENT_START
button.connect('activated', self._button_activated_cb)
self._round_box.append(button)
self._buttons[button] = action_id
2007-02-09 14:47:11 +01:00
2007-02-09 17:34:08 +01:00
def do_set_property(self, pspec, value):
self._entry.set_property(pspec.name, value)
2007-02-09 14:47:11 +01:00
def do_get_property(self, pspec):
return self._entry.get_property(pspec.name)
2007-02-09 17:34:08 +01:00
2007-02-09 14:47:11 +01:00
def _entry_focus_in_event_cb(self, widget, event):
2007-02-09 17:34:08 +01:00
self._update_colors(focused=True)
2007-02-09 14:47:11 +01:00
self.emit_paint_needed(0, 0, -1, -1)
def _entry_focus_out_event_cb(self, widget, event):
2007-02-09 17:34:08 +01:00
self._update_colors(focused=False)
2007-02-09 14:47:11 +01:00
self.emit_paint_needed(0, 0, -1, -1)
2007-02-09 17:34:08 +01:00
def _entry_activate_cb(self, entry):
self.emit_activated()
def _button_activated_cb(self, button):
self.emit('button-activated', self._buttons[button])
2007-02-09 17:34:08 +01:00
def _update_colors(self, focused):
if focused:
self._round_box.props.background_color = \
Color.ENTRY_BACKGROUND_FOCUSED.get_int()
2007-02-09 17:34:08 +01:00
self._entry.modify_base(gtk.STATE_NORMAL,
Color.ENTRY_BACKGROUND_FOCUSED.get_gdk_color())
self._entry.modify_base(gtk.STATE_SELECTED,
Color.ENTRY_SELECTION_FOCUSED.get_gdk_color())
2007-02-10 17:42:11 +01:00
self._entry.modify_text(gtk.STATE_NORMAL,
Color.ENTRY_TEXT_FOCUSED.get_gdk_color())
2007-02-09 17:34:08 +01:00
else:
self._round_box.props.background_color = \
Color.ENTRY_BACKGROUND_UNFOCUSED.get_int()
2007-02-09 17:34:08 +01:00
self._entry.modify_base(gtk.STATE_NORMAL,
Color.ENTRY_BACKGROUND_UNFOCUSED.get_gdk_color())
self._entry.modify_base(gtk.STATE_SELECTED,
Color.ENTRY_SELECTION_UNFOCUSED.get_gdk_color())
2007-02-10 17:42:11 +01:00
self._entry.modify_text(gtk.STATE_NORMAL,
Color.ENTRY_TEXT_UNFOCUSED.get_gdk_color())