Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/sugar/graphics/entry.py
blob: 68970b78451c2069ea0c85672c3585bdb5ec09ed (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# 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

import hippo
import gtk

from sugar.graphics.frame import Frame
from sugar.graphics.color import Color

class Entry(hippo.CanvasBox, hippo.CanvasItem):
    __gtype_name__ = 'SugarEntry'
    
    def __init__(self):
        hippo.CanvasBox.__init__(self)

        self._radius = -1
        self._background_color = Color.ENTRY_BACKGROUND_UNFOCUSED

        self._entry = gtk.Entry()
        self._entry.props.has_frame = False
        self._entry.modify_base(gtk.STATE_NORMAL,
                                self._background_color.get_gdk_color())
        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._canvas_widget = hippo.CanvasWidget()
        self._canvas_widget.props.widget = self._entry
        self.append(self._canvas_widget, hippo.PACK_EXPAND)

    def do_paint_below_children(self, cr, damaged_box):
        logging.debug('do_paint_below_children: %s', str(self._background_color))

        [width, height] = self._canvas_widget.get_allocation()

        x = 0
        y = 0

        cr.move_to(x + self._radius, y);
        cr.arc(x + width - self._radius, y + self._radius,
               self._radius, math.pi * 1.5, math.pi * 2);
        cr.arc(x + width - self._radius, x + height - self._radius,
               self._radius, 0, math.pi * 0.5);
        cr.arc(x + self._radius, y + height - self._radius,
               self._radius, math.pi * 0.5, math.pi);
        cr.arc(x + self._radius, y + self._radius, self._radius,
               math.pi, math.pi * 1.5);

        cr.set_source_rgba(*self._background_color.get_rgba())
        cr.fill_preserve();

    def do_allocate(self, width, height, origin_changed):
        hippo.CanvasBox.do_allocate(self, width, height, origin_changed)

        [width, height] = self._canvas_widget.get_request()
        radius = min(width, height) / 2
        if radius != self._radius:
            self._radius = radius
            self._canvas_widget.props.padding_left = self._radius
            self._canvas_widget.props.padding_right = self._radius

        self._canvas_widget.do_allocate(self._canvas_widget, width, height,
                                       origin_changed)

    def _entry_focus_in_event_cb(self, widget, event):
        self._background_color = Color.ENTRY_BACKGROUND_FOCUSED
        self._entry.modify_base(gtk.STATE_NORMAL,
                                self._background_color.get_gdk_color())
        self.emit_paint_needed(0, 0, -1, -1)
        logging.debug('_entry_focus_in_event_cb')

    def _entry_focus_out_event_cb(self, widget, event):
        self._background_color = Color.ENTRY_BACKGROUND_UNFOCUSED
        self._entry.modify_base(gtk.STATE_NORMAL,
                                self._background_color.get_gdk_color())
        self.emit_paint_needed(0, 0, -1, -1)
        logging.debug('_entry_focus_out_event_cb')