Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/readdialog.py
blob: 65981ec6afdf569d4d0b87d942b4449c7f73e3a4 (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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
#!/usr/bin/env python

# Stolen from the PyGTK demo module by Maik Hertha <maik.hertha@berlin.de>

import gtk
import gobject 

from sugar.graphics import style
from sugar.graphics.toolbutton import ToolButton

from gettext import gettext as _
import cjson

class BaseReadDialog(gtk.Window):
    def __init__(self, parent_xid, dialog_title):
        gtk.Window.__init__(self)

        self.connect('realize', self.__realize_cb)

        self.set_decorated(False)
        self.set_position(gtk.WIN_POS_CENTER_ALWAYS)
        self.set_border_width(style.LINE_WIDTH)

        width = gtk.gdk.screen_width() - style.GRID_CELL_SIZE * 4
        height = gtk.gdk.screen_height() - style.GRID_CELL_SIZE * 4
        self.set_size_request(width, height)

        self._parent_window_xid = parent_xid

        _vbox = gtk.VBox(spacing=2)
        self.add(_vbox)

        self.toolbar = gtk.Toolbar()
        label = gtk.Label()
        label.set_markup('<b>  %s</b>' % dialog_title)
        label.set_alignment(0, 0.5)
        tool_item = gtk.ToolItem()
        tool_item.add(label)
        label.show()
        self.toolbar.insert(tool_item, -1)
        tool_item.show()

        separator = gtk.SeparatorToolItem()
        separator.props.draw = False
        separator.set_expand(True)
        self.toolbar.insert(separator, -1)
        separator.show()
        stop = ToolButton(icon_name='dialog-cancel')
        stop.set_tooltip(_('Cancel'))
        stop.connect('clicked', lambda *w: self.destroy())
        self.toolbar.insert(stop, -1)
        stop.show()

        delete = ToolButton(icon_name='edit-delete')
        delete.set_tooltip(_('Delete'))
        delete.connect('clicked', self.accept_clicked_delete_cb)
        self.toolbar.insert(delete, -1)
        delete.show()


        accept = ToolButton(icon_name='dialog-ok')
        accept.set_tooltip(_('Ok'))
        accept.connect('clicked', self.accept_clicked_cb)
        accept.show()
        self.toolbar.insert(accept, -1)
        
        _vbox.pack_start(self.toolbar, expand=False)
        self.toolbar.show()

        self._event_box = gtk.EventBox()
        _vbox.pack_start(self._event_box, expand=True, fill = True)
        self._canvas = None

    def set_canvas(self, canvas):
        if self._canvas is not None:
            self._event_box.remove(self._canvas)
        self._event_box.add(canvas)
        self._canvas = canvas

    def __realize_cb(self, widget):
        self.window.set_type_hint(gtk.gdk.WINDOW_TYPE_HINT_DIALOG)
        self.window.set_accept_focus(True)

        parent = gtk.gdk.window_foreign_new(self._parent_window_xid)
        self.window.set_transient_for(parent)

        self.modify_bg(gtk.STATE_NORMAL,
                            style.COLOR_WHITE.get_gdk_color())

        if self._canvas is not None:
            self._canvas.modify_bg(gtk.STATE_NORMAL,
                style.COLOR_WHITE.get_gdk_color())
            self._canvas.grab_focus()

        self._event_box.modify_bg(gtk.STATE_NORMAL,
                style.COLOR_WHITE.get_gdk_color())

    def accept_clicked_cb(self, widget):
        raise NotImplementedError

    
    def accept_clicked_delete_cb(self, widget):
        raise NotImplementedError

class BookmarkDialog(BaseReadDialog):
    def __init__(self, parent_xid, dialog_title, bookmark_title, bookmark_content, page, sidebarinstance):
        BaseReadDialog.__init__(self, parent_xid, dialog_title)

        self._sidebarinstance = sidebarinstance
        self._page = page

        vbox = gtk.VBox()
        thbox = gtk.HBox()
        vbox.pack_start(thbox, expand = False, fill = False)
        thbox.set_border_width(style.DEFAULT_SPACING * 2)
        thbox.set_spacing(style.DEFAULT_SPACING)
        thbox.show()

        label_title = gtk.Label(_('<b>Title</b>:'))
        label_title.set_use_markup(True)
        label_title.set_alignment(1, 0.5)
        label_title.modify_fg(gtk.STATE_NORMAL,
                            style.COLOR_SELECTION_GREY.get_gdk_color())
        thbox.pack_start(label_title, expand=False, fill = False)
        label_title.show()

        self._title_entry = gtk.Entry()
        self._title_entry.modify_bg(gtk.STATE_INSENSITIVE,
                            style.COLOR_WHITE.get_gdk_color())
        self._title_entry.modify_base(gtk.STATE_INSENSITIVE,
                            style.COLOR_WHITE.get_gdk_color())
        self._title_entry.set_size_request(int(gtk.gdk.screen_width() / 3), -1)

        thbox.pack_start(self._title_entry, expand = False, fill = False)
        self._title_entry.show()
        if bookmark_title is not None:
            self._title_entry.set_text(bookmark_title)


        cvbox = gtk.VBox()
        vbox.pack_start(cvbox, expand = True, fill = True)
        cvbox.set_border_width(style.DEFAULT_SPACING * 2)
        cvbox.set_spacing(style.DEFAULT_SPACING/2)
        cvbox.show()

        label_content = gtk.Label(_('<b>Details</b>:'))
        label_content.set_use_markup(True)
        label_content.set_alignment(0, 0)
        label_content.modify_fg(gtk.STATE_NORMAL,
                            style.COLOR_SELECTION_GREY.get_gdk_color())
        cvbox.pack_start(label_content, expand=False, fill = False)
        label_content.show()

        sw = gtk.ScrolledWindow()
        sw.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC)

        self._content_entry = gtk.TextView()
        self._content_entry.set_wrap_mode(gtk.WRAP_WORD)

        sw.add(self._content_entry)
        sw.set_shadow_type(gtk.SHADOW_ETCHED_IN)

        cvbox.pack_start(sw, expand = True, fill = True)
        self._content_entry.show()
        if bookmark_content is not None:
            buffer = self._content_entry.get_buffer()
            buffer.set_text(bookmark_content)

        self.set_canvas(vbox)

class BookmarkAddDialog(BookmarkDialog):
    def __init__(self, parent_xid, dialog_title, bookmark_title, bookmark_content, page, sidebarinstance):
        BookmarkDialog.__init__(self, parent_xid, dialog_title, bookmark_title, bookmark_content, page, sidebarinstance)

    def accept_clicked_cb(self, widget):
        title = self._title_entry.get_text()
        details = self._content_entry.get_buffer().props.text
        content = {'title' : unicode(title), 'body' : unicode(details)} 
        self._sidebarinstance._real_add_bookmark(self._page, cjson.encode(content))
        self.destroy()

class BookmarkEditDialog(BookmarkDialog):
    def __init__(self, parent_xid, dialog_title, bookmark_title, bookmark_content, page, sidebarinstance):
        BookmarkDialog.__init__(self, parent_xid, dialog_title, bookmark_title, bookmark_content, page, sidebarinstance)

    def accept_clicked_cb(self, widget):
        title = self._title_entry.get_text()
        details = self._content_entry.get_buffer().props.text
        content = {'title' : unicode(title), 'body' : unicode(details)} 
        self._sidebarinstance.del_bookmark(self._page)
        self._sidebarinstance._real_add_bookmark(self._page, cjson.encode(content))
        self.destroy()


class AnnotationDialog(BaseReadDialog):
    def __init__(self, parent_xid, dialog_title, annotation_id, annotation_title, annotation_content, page, sidebarinstance):
        BaseReadDialog.__init__(self, parent_xid, dialog_title)

        self._sidebarinstance = sidebarinstance
        self._page = page
        self._annotation_id = annotation_id

        vbox = gtk.VBox()
        thbox = gtk.HBox()
        vbox.pack_start(thbox, expand = False, fill = False)
        thbox.set_border_width(style.DEFAULT_SPACING * 2)
        thbox.set_spacing(style.DEFAULT_SPACING)
        thbox.show()

        label_title = gtk.Label(_('<b>Title</b>:'))
        label_title.set_use_markup(True)
        label_title.set_alignment(1, 0.5)
        label_title.modify_fg(gtk.STATE_NORMAL,
                            style.COLOR_SELECTION_GREY.get_gdk_color())
        thbox.pack_start(label_title, expand=False, fill = False)
        label_title.show()

        self._title_entry = gtk.Entry()
        self._title_entry.modify_bg(gtk.STATE_INSENSITIVE,
                            style.COLOR_WHITE.get_gdk_color())
        self._title_entry.modify_base(gtk.STATE_INSENSITIVE,
                            style.COLOR_WHITE.get_gdk_color())
        self._title_entry.set_size_request(int(gtk.gdk.screen_width() / 3), -1)

        thbox.pack_start(self._title_entry, expand = False, fill = False)
        self._title_entry.show()
        if annotation_title is not None:
            self._title_entry.set_text(annotation_title)


        cvbox = gtk.VBox()
        vbox.pack_start(cvbox, expand = True, fill = True)
        cvbox.set_border_width(style.DEFAULT_SPACING * 2)
        cvbox.set_spacing(style.DEFAULT_SPACING/2)
        cvbox.show()

        label_content = gtk.Label(_('<b>Details</b>:'))
        label_content.set_use_markup(True)
        label_content.set_alignment(0, 0)
        label_content.modify_fg(gtk.STATE_NORMAL,
                            style.COLOR_SELECTION_GREY.get_gdk_color())
        cvbox.pack_start(label_content, expand=False, fill = False)
        label_content.show()

        sw = gtk.ScrolledWindow()
        sw.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC)

        self._content_entry = gtk.TextView()
        self._content_entry.set_wrap_mode(gtk.WRAP_WORD)

        sw.add(self._content_entry)
        sw.set_shadow_type(gtk.SHADOW_ETCHED_IN)

        cvbox.pack_start(sw, expand = True, fill = True)
        self._content_entry.show()
        if annotation_content is not None:
            buffer = self._content_entry.get_buffer()
            buffer.set_text(annotation_content)

        self.set_canvas(vbox)

class AnnotationAddDialog(AnnotationDialog):
    def __init__(self, parent_xid, dialog_title, annotation_id, annotation_title, annotation_content, page, sidebarinstance):
        AnnotationDialog.__init__(self, parent_xid, dialog_title, annotation_id, annotation_title, annotation_content, page, sidebarinstance)

    def accept_clicked_cb(self, widget):
        title = self._title_entry.get_text()
        details = self._content_entry.get_buffer().props.text
        content = {'title' : unicode(title), 'body' : unicode(details)} 
        self._sidebarinstance._real_add_annotation(self._page, cjson.encode(content))
        self.destroy()
    
    def accept_clicked_delete_cb(self, widget):
        pass

class AnnotationEditDialog(AnnotationDialog):
    def __init__(self, parent_xid, dialog_title, annotation_id, annotation_title, annotation_content, page, sidebarinstance):
        AnnotationDialog.__init__(self, parent_xid, dialog_title, annotation_id, annotation_title, annotation_content, page, sidebarinstance)

    def accept_clicked_cb(self, widget):
        title = self._title_entry.get_text()
        details = self._content_entry.get_buffer().props.text
        content = {'title' : unicode(title), 'body' : unicode(details)} 
        self._sidebarinstance._real_edit_annotation(self._page, cjson.encode(content), self._annotation_id)
        self.destroy()

    def accept_clicked_delete_cb(self, widget):
        self._sidebarinstance._real_delete_annotation(self._page, self._annotation_id)
        self.destroy()