Added Rollover control.

This commit is contained in:
Tomeu Vizoso
2007-02-20 16:38:25 +01:00
parent 5f65056d78
commit f3cf928f31
8 changed files with 272 additions and 65 deletions
+85 -15
View File
@@ -23,6 +23,7 @@ import hippo
from canvasicon import CanvasIcon
from iconcolor import IconColor
from sugar.graphics import units
from sugar.graphics.timeline import Timeline
from sugar import profile
STANDARD_SIZE = 0
@@ -41,15 +42,20 @@ class Button(hippo.CanvasBox):
gobject.PARAM_READWRITE)
}
def __init__(self, icon_name):
self._normal_color = IconColor('white')
def __init__(self, icon_name, color=None):
if color:
self._normal_color = color
else:
self._normal_color = IconColor('white')
self._prelight_color = profile.get_color()
self._inactive_color = IconColor('#808080,#424242')
self._active = True
self._rollover = None
self._hover_rollover = False
self._icon = CanvasIcon(icon_name=icon_name, cache=True,
color=self._normal_color)
self._connect_signals(self._icon)
hippo.CanvasBox.__init__(self)
@@ -57,6 +63,81 @@ class Button(hippo.CanvasBox):
self.append(self._icon, hippo.PACK_EXPAND)
self._timeline = Timeline(self)
self._timeline.add_tag('popup', 6, 6)
self._timeline.add_tag('before_popdown', 7, 7)
self._timeline.add_tag('popdown', 8, 8)
self.connect('motion-notify-event', self._motion_notify_event_cb)
self.connect('button-press-event', self._button_press_event_cb)
def get_rollover(self):
return self._rollover
def get_rollover_context(self):
return None
def do_popup(self, current, n_frames):
if self._rollover:
return
rollover = self.get_rollover()
if not rollover:
return
rollover_context = self.get_rollover_context()
if rollover_context:
rollover_context.popped_up(rollover)
rollover.connect('motion-notify-event',
self._rollover_motion_notify_event_cb)
rollover.connect('action-completed',
self._rollover_action_completed_cb)
context = self._icon.get_context()
#[x, y] = context.translate_to_screen(self._icon)
[x, y] = context.translate_to_widget(self._icon)
# TODO: Any better place to do this?
rollover.props.box_width = max(rollover.props.box_width,
self.get_width_request())
[width, height] = self._icon.get_allocation()
rollover.popup(x, y + height)
self._rollover = rollover
def do_popdown(self, current, frame):
if self._rollover:
self._rollover.popdown()
rollover_context = self.get_rollover_context()
if rollover_context:
rollover_context.popped_down(self._rollover)
self._rollover = None
def popdown(self):
self._timeline.play('popdown', 'popdown')
def _motion_notify_event_cb(self, button, event):
if event.detail == hippo.MOTION_DETAIL_ENTER:
self._timeline.play(None, 'popup')
elif event.detail == hippo.MOTION_DETAIL_LEAVE:
if not self._hover_rollover:
self._timeline.play('before_popdown', 'popdown')
def _rollover_motion_notify_event_cb(self, rollover, event):
if event.detail == hippo.MOTION_DETAIL_ENTER:
self._hover_rollover = True
self._timeline.play('popup', 'popup')
elif event.detail == hippo.MOTION_DETAIL_LEAVE:
self._hover_rollover = False
self._timeline.play('popdown', 'popdown')
def _rollover_action_completed_cb(self, rollover):
self.popdown()
def _set_size(self, size):
if size == SMALL_SIZE:
self.props.box_width = -1
@@ -93,17 +174,6 @@ class Button(hippo.CanvasBox):
else:
return hippo.CanvasBox.get_property(self, pspec)
def _connect_signals(self, item):
item.connect('button-release-event', self._button_release_event_cb)
# TODO: Prelighting is disabled by now. Need to figure how we want it to behave.
#item.connect('motion-notify-event', self._motion_notify_event_cb)
def _button_release_event_cb(self, widget, event):
def _button_press_event_cb(self, widget, event):
if self._active:
self.emit_activated()
def _motion_notify_event_cb(self, widget, event):
if self._active and event.detail == hippo.MOTION_DETAIL_ENTER:
self._icon.props.color = self._prelight_color
elif self._active and event.detail == hippo.MOTION_DETAIL_LEAVE:
self._icon.props.color = self._normal_color
+6 -3
View File
@@ -60,7 +60,7 @@ class Menu(hippo.CanvasBox, hippo.CanvasItem):
canvas_text.props.font_desc = font.DEFAULT.get_pango_desc()
box.append(canvas_text)
box.connect('button-press-event', self._button_press_event_cb,
box.connect('button-press-event', self._item_button_press_event_cb,
[action_id, label])
self.append(box)
@@ -82,7 +82,7 @@ class Menu(hippo.CanvasBox, hippo.CanvasItem):
self._window.destroy()
self._window = None
def _button_press_event_cb(self, item, event, data):
def _item_button_press_event_cb(self, item, event, data):
self.emit('action', data)
self.hide()
@@ -152,8 +152,11 @@ class OptionMenu(hippo.CanvasBox, hippo.CanvasItem):
context = self._round_box.get_context()
#[x, y] = context.translate_to_screen(self._round_box)
[x, y] = context.translate_to_widget(self._round_box)
[width, height] = self._round_box.get_allocation()
# TODO: Any better place to do this?
self._menu.props.box_width = self.get_width_request()
[width, height] = self._round_box.get_allocation()
self._menu.show(x, y + height)
def _menu_action_cb(self, menu, data):
+84
View File
@@ -0,0 +1,84 @@
# 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 sys
import logging
import gobject
import gtk
import hippo
from sugar.graphics import style
from sugar.graphics.roundbox import RoundBox
from sugar.graphics import button
from sugar.graphics import color
from sugar.graphics import font
from sugar.graphics.canvasicon import CanvasIcon
class Rollover(hippo.CanvasBox, hippo.CanvasItem):
__gtype_name__ = 'SugarRollover'
__gsignals__ = {
'action-completed': (gobject.SIGNAL_RUN_FIRST, gobject.TYPE_NONE, ([]))
}
def __init__(self, title):
hippo.CanvasBox.__init__(self)
self.props.background_color = color.MENU_BACKGROUND.get_int()
self.props.border_color = color.MENU_BORDER.get_int()
#TODO: how we should specify the border thickness?
self.props.border = style.separator_thickness
self._window = None
def add_item(self, action_id, label, icon_name=None, icon_color=None):
box = hippo.CanvasBox(orientation=hippo.ORIENTATION_HORIZONTAL)
box.props.padding = 5
box.props.spacing = 5
if icon_name:
icon = CanvasIcon(icon_name=icon_name, scale=style.small_icon_scale)
if icon_color:
icon.props.color = icon_color
box.append(icon)
canvas_text = hippo.CanvasText()
canvas_text.props.text = label
canvas_text.props.color = color.LABEL_TEXT.get_int()
canvas_text.props.font_desc = font.DEFAULT.get_pango_desc()
box.append(canvas_text)
box.connect('button-press-event', self._item_button_press_event_cb)
self.append(box)
def add_separator(self):
box = hippo.CanvasBox()
box.props.background_color = color.MENU_SEPARATOR.get_int()
box.props.box_height = style.separator_thickness
self.append(box)
def popup(self, x, y):
if not self._window:
self._window = hippo.CanvasWindow(gtk.WINDOW_POPUP)
self._window.move(x, y)
self._window.set_root(self)
self._window.show()
def popdown(self):
if self._window:
self._window.destroy()
self._window = None
def _item_button_press_event_cb(self, item, event):
self.emit('action-completed')
+44
View File
@@ -0,0 +1,44 @@
# 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 gobject
import hippo
class RolloverContext(gobject.GObject):
__gtype_name__ = 'SugarRolloverContext'
__gsignals__ = {
'activated': (gobject.SIGNAL_RUN_FIRST, gobject.TYPE_NONE, ([])),
'deactivated': (gobject.SIGNAL_RUN_FIRST, gobject.TYPE_NONE, ([]))
}
def __init__(self):
self._active_control = None
gobject.GObject.__init__(self)
def popped_up(self, control):
if self._active_control:
self._active_control.popdown()
self._active_control = control
self.emit('activated')
def popped_down(self, control):
if self._active_control == control:
self._active_control = None
self.emit('deactivated')
def is_active(self):
return self._active_control != None