Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/activity.py
blob: 09fbba846c5e77b5deee7c6ca4bc86b50c2a1126 (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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# -*- coding: utf-8 -*-
# Copyright 2011 Manuel QuiƱones
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

"""
TiddlyWiki Activity: A reusable non-linear personal web notebook.

This activity displays a TiddlyWiki, which is a wiki in a single html,
via WebKit.

"""

import os
import gtk
import webkit
import logging

from sugar.activity import activity
from sugar.graphics.toolbarbox import ToolbarBox
from sugar.graphics.toolbarbox import ToolbarButton
from sugar.activity.widgets import ActivityToolbarButton
from sugar.activity.widgets import StopButton
from sugar.activity.widgets import EditToolbar

from toolbars import ViewToolbar

_TIDDLYWIKI_PATH = os.path.join(activity.get_bundle_path(),
                                "data/empty.html")


class TiddlyWikiActivity(activity.Activity):
    """TiddlyWikiActivity class as specified in activity.info"""

    def __init__(self, handle):
        """Set up the TiddlyWiki activity."""
        activity.Activity.__init__(self, handle)

        self._web_view = None

        # we do not have collaboration features, make the share option
        # insensitive:
        self.max_participants = 1

        # add the toolbar:
        toolbar_box = self._make_toolbar()
        self.set_toolbar_box(toolbar_box)
        toolbar_box.show()

        # add the webkit view in the canvas:
        canvas_widget = self.make_webkit_view()
        self.set_canvas(canvas_widget)
        canvas_widget.show()

    def _make_toolbar(self):
        logging.info("Creating toolbars")
        toolbar_box = ToolbarBox()

        # activity toolbar:
        activity_button = ActivityToolbarButton(self)
        toolbar_box.toolbar.insert(activity_button, -1)
        activity_button.show()

        # edit toolbar:
        edit_toolbar = EditToolbar()
        edit_toolbar.undo.connect('clicked', self._edit_undo_cb)
        edit_toolbar.redo.connect('clicked', self._edit_redo_cb)
        edit_toolbar.copy.connect('clicked', self._edit_copy_cb)
        edit_toolbar.paste.connect('clicked', self._edit_paste_cb)
        edit_toolbar_button = ToolbarButton(page=edit_toolbar,
                                            icon_name='toolbar-edit')
        toolbar_box.toolbar.insert(edit_toolbar_button, -1)
        edit_toolbar_button.show()

        # view toolbar:
        view_toolbar = ViewToolbar()
        view_toolbar.set_view(self)
        view_toolbar_button = ToolbarButton(page=view_toolbar,
                                            icon_name='toolbar-view')
        toolbar_box.toolbar.insert(view_toolbar_button, -1)
        view_toolbar_button.show()

        # fill with blank space:
        separator = gtk.SeparatorToolItem()
        separator.props.draw = False
        separator.set_expand(True)
        toolbar_box.toolbar.insert(separator, -1)
        separator.show()

        # stop button:
        stop_button = StopButton(self)
        toolbar_box.toolbar.insert(stop_button, -1)
        stop_button.show()

        return toolbar_box

    def make_webkit_view(self):
        logging.info("Creating webkit view")
        web_view = webkit.WebView()
        web_view.load_uri('file://' + _TIDDLYWIKI_PATH)
        self._web_view = web_view
        web_view.show()
        scrolledwindow = gtk.ScrolledWindow()
        scrolledwindow.add(web_view)
        return scrolledwindow

    def _edit_undo_cb(self, button):
        self._web_view.undo()

    def _edit_redo_cb(self, button):
        self._web_view.redo()

    def _edit_copy_cb(self, button):
        self._web_view.copy_clipboard()

    def _edit_paste_cb(self, button):
        self._web_view.paste_clipboard()

    def zoom_in(self):
        self._web_view.zoom_in()

    def zoom_out(self):
        self._web_view.zoom_out()