Web · Wiki · Activities · Blog · Lists · Chat · Meeting · Bugs · Git · Translate · Archive · People · Donate
1
# Copyright (C) 2008, One Laptop Per Child
2
# Copyright (C) 2009 Simon Schampijer
3
#
4
# This program is free software; you can redistribute it and/or modify
5
# it under the terms of the GNU General Public License as published by
6
# the Free Software Foundation; either version 2 of the License, or
7
# (at your option) any later version.
8
#
9
# This program is distributed in the hope that it will be useful,
10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
# GNU General Public License for more details.
13
#
14
# You should have received a copy of the GNU General Public License
15
# along with this program; if not, write to the Free Software
16
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
17
18
from gi.repository import GObject
19
from gi.repository import Gtk
20
from gi.repository import Gdk
21
from gettext import gettext as _
22
23
from sugar3.activity.widgets import EditToolbar as BaseEditToolbar
24
from sugar3.graphics import iconentry
25
from sugar3.graphics.toolbutton import ToolButton
26
from sugar3.graphics import style
27
28
29
class EditToolbar(BaseEditToolbar):
30
    def __init__(self, act):
31
        BaseEditToolbar.__init__(self)
32
33
        self._activity = act
34
        self._browser = None
35
36
        self.undo.connect('clicked', self.__undo_cb)
37
        self.redo.connect('clicked', self.__redo_cb)
38
        self.copy.connect('clicked', self.__copy_cb)
39
        self.paste.connect('clicked', self.__paste_cb)
40
41
        separator = Gtk.SeparatorToolItem()
42
        separator.set_draw(False)
43
        separator.set_expand(True)
44
        self.insert(separator, -1)
45
        separator.show()
46
47
        search_item = Gtk.ToolItem()
48
        self.search_entry = iconentry.IconEntry()
49
        self.search_entry.set_icon_from_name(iconentry.ICON_ENTRY_PRIMARY,
50
                                             'entry-search')
51
        self.search_entry.add_clear_button()
52
        self.search_entry.connect('activate', self.__search_entry_activate_cb)
53
        self.search_entry.connect('changed', self.__search_entry_changed_cb)
54
55
        width = int(Gdk.Screen.width() / 3)
56
        self.search_entry.set_size_request(width, -1)
57
58
        search_item.add(self.search_entry)
59
        self.search_entry.show()
60
61
        self.insert(search_item, -1)
62
        search_item.show()
63
64
        self._prev = ToolButton('go-previous-paired')
65
        self._prev.set_tooltip(_('Previous'))
66
        self._prev.props.sensitive = False
67
        self._prev.connect('clicked', self.__find_previous_cb)
68
        self.insert(self._prev, -1)
69
        self._prev.show()
70
71
        self._next = ToolButton('go-next-paired')
72
        self._next.set_tooltip(_('Next'))
73
        self._next.props.sensitive = False
74
        self._next.connect('clicked', self.__find_next_cb)
75
        self.insert(self._next, -1)
76
        self._next.show()
77
78
        tabbed_view = self._activity.get_canvas()
79
80
        GObject.idle_add(lambda:
81
                self._connect_to_browser(tabbed_view.props.current_browser))
82
83
        tabbed_view.connect_after('switch-page', self.__switch_page_cb)
84
85
    def __switch_page_cb(self, tabbed_view, page, page_num):
86
        self._connect_to_browser(tabbed_view.props.current_browser)
87
88
    def _connect_to_browser(self, browser):
89
        if self._browser is not None:
90
            self._browser.disconnect(self._selection_changed_hid)
91
92
        self._browser = browser
93
94
        self._update_undoredo_buttons()
95
        self._update_copypaste_buttons()
96
97
        self._selection_changed_hid = self._browser.connect(
98
                'selection-changed', self._selection_changed_cb)
99
100
    def _selection_changed_cb(self, widget):
101
        self._update_undoredo_buttons()
102
        self._update_copypaste_buttons()
103
104
    def _update_undoredo_buttons(self):
105
        self.undo.set_sensitive(self._browser.can_undo())
106
        self.redo.set_sensitive(self._browser.can_redo())
107
108
    def _update_copypaste_buttons(self):
109
        self.copy.set_sensitive(self._browser.can_copy_clipboard())
110
        self.paste.set_sensitive(self._browser.can_paste_clipboard())
111
112
    def __undo_cb(self, button):
113
        self._browser.undo()
114
        self._update_undoredo_buttons()
115
116
    def __redo_cb(self, button):
117
        self._browser.redo()
118
        self._update_undoredo_buttons()
119
120
    def __copy_cb(self, button):
121
        self._browser.copy_clipboard()
122
123
    def __paste_cb(self, button):
124
        self._browser.paste_clipboard()
125
126
    def _find_and_mark_text(self, entry):
127
        search_text = entry.get_text()
128
        self._browser.unmark_text_matches()
129
        self._browser.mark_text_matches(search_text, case_sensitive=False,
130
                                        limit=0)
131
        self._browser.set_highlight_text_matches(True)
132
        found = self._browser.search_text(search_text, case_sensitive=False,
133
                                          forward=True, wrap=True)
134
        return found
135
136
    def __search_entry_activate_cb(self, entry):
137
        self._find_and_mark_text(entry)
138
139
    def __search_entry_changed_cb(self, entry):
140
        found = self._find_and_mark_text(entry)
141
        if not found:
142
            self._prev.props.sensitive = False
143
            self._next.props.sensitive = False
144
            entry.modify_text(Gtk.StateType.NORMAL,
145
                              style.COLOR_BUTTON_GREY.get_gdk_color())
146
        else:
147
            self._prev.props.sensitive = True
148
            self._next.props.sensitive = True
149
            entry.modify_text(Gtk.StateType.NORMAL,
150
                              style.COLOR_BLACK.get_gdk_color())
151
152
    def __find_previous_cb(self, button):
153
        search_text = self.search_entry.get_text()
154
        self._browser.search_text(search_text, case_sensitive=False,
155
                                  forward=False, wrap=True)
156
157
    def __find_next_cb(self, button):
158
        search_text = self.search_entry.get_text()
159
        self._browser.search_text(search_text, case_sensitive=False,
160
                                  forward=True, wrap=True)