Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/tutorius/creator.py
blob: d8f3d98d575bf896739b83ff0b2fa88ab8c9f6bc (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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
"""
This package contains UI classes related to tutorial authoring.
This includes visual display of tools to edit and create tutorials from within
the activity itself.
"""
# Copyright (C) 2009, Tutorius.org
# Copyright (C) 2009, Simon Poirier <simpoir@gmail.com>
#
#
# 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 1 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

import gtk.gdk
import gtk.glade
import gobject
from gettext import gettext as T

import os
from sugar.graphics import icon
import copy

from sugar.tutorius import overlayer, gtkutils, actions, bundler, properties, addon
from sugar.tutorius import filters, __path__
from sugar.tutorius.services import ObjectStore
from sugar.tutorius.linear_creator import LinearCreator
from sugar.tutorius.core import Tutorial
from sugar.tutorius import viewer

class Creator(object):
    """
    Class acting as a bridge between the creator, serialization and core
    classes. This contains most of the UI part of the editor.
    """
    def __init__(self, activity, tutorial=None):
        """
        Instanciate a tutorial creator for the activity.

        @param activity to bind the creator to
        @param tutorial an existing tutorial to edit, or None to create one
        """
        self._activity = activity
        if not tutorial:
            self._tutorial = FiniteStateMachine('Untitled')
            self._state = State(name='INIT')
            self._tutorial.add_state(self._state)
            self._state_counter = 1
        else:
            self._tutorial = tutorial
            # TODO load existing tutorial; unused yet

        self._action_panel = None
        self._current_filter = None
        self._intro_mask = None
        self._intro_handle = None
        allocation = self._activity.get_allocation()
        self._width = allocation.width
        self._height = allocation.height
        self._selected_widget = None
        self._eventmenu = None
        self.tuto = None
        self._guid = None

        self._hlmask = overlayer.Rectangle(None, (1.0, 0.0, 0.0, 0.5))
        self._activity._overlayer.put(self._hlmask, 0, 0)

        dlg_width = 300
        dlg_height = 70
        sw = gtk.gdk.screen_width()
        sh = gtk.gdk.screen_height()

        self._propedit = ToolBox(self._activity)
        self._propedit.tree.signal_autoconnect({
            'on_quit_clicked': self._cleanup_cb,
            'on_save_clicked': self.save,
            'on_action_activate': self._add_action_cb,
            'on_event_activate': self._add_event_cb,
        })
        self._propedit.window.move(
            gtk.gdk.screen_width()-self._propedit.window.get_allocation().width,
            100)


        self._overview = viewer.Viewer(self._tutorial, self)
        self._overview.win.set_transient_for(self._activity)

        self._overview.win.move(0, gtk.gdk.screen_height()- \
                                self._overview.win.get_allocation().height)

    def delete_action(self, action):
        """
        Removes the first instance of specified action from the tutorial.

        @param action: the action object to remove from the tutorial
        @returns: True if successful, otherwise False.
        """
        state = self._tutorial.get_state_by_name("INIT")

        while True:
            state_actions = state.get_action_list()
            for fsm_action in state_actions:
                if fsm_action is action:
                    state.clear_actions()
                    if state is self._state:
                        fsm_action.exit_editmode()
                    state_actions.remove(fsm_action)
                    self.set_insertion_point(state.name)
                    for keep_action in state_actions:
                        state.add_action(keep_action)
                    return True

            ev_list = state.get_event_filter_list()
            if ev_list:
                state = self._tutorial.get_state_by_name(
                    ev_list[0].get_next_state())
                continue

            return False

    def delete_state(self):
        """
        Remove current state.
        Limitation: The last state cannot be removed, as it doesn't have
                    any transitions to remove anyway.

        @returns: True if successful, otherwise False.
        """
        if not self._state.get_event_filter_list():
            # last state cannot be removed
            return False

        state = self._tutorial.get_state_by_name("INIT")
        ev_list = state.get_event_filter_list()
        if state == self._state:
            next_state = self._tutorial.get_state_by_name(
                ev_list[0].get_next_state())
            self.set_insertion_point(next_state.name)
            self._tutorial.remove_state(state.name)
            self._tutorial.remove_state(next_state.name)
            next_state.name = "INIT"
            self._tutorial.add_state(next_state)
            return True

        # loop to repair links from deleted state
        while ev_list:
            next_state = self._tutorial.get_state_by_name(
                ev_list[0].get_next_state())
            if next_state is self._state:
                # the tutorial will flush the event filters. We'll need to
                # clear and re-add them.
                ev_list = copy.copy(ev_list)
                self._tutorial.remove_state(self._state.name)
                state.clear_event_filters()
                ev_list[0].set_next_state(
                    next_state.get_event_filter_list()[0].get_next_state())
                for ev in ev_list:
                    state.add_event_filter(ev)

                self.set_insertion_point(ev_list[0].get_next_state())
                return True

            state = next_state
            ev_list = state.get_event_filter_list()
        return False

    def get_insertion_point(self):
        return self._state.name

    def set_insertion_point(self, state_name):
        for action in self._state.get_action_list():
            action.exit_editmode()
        self._state = self._tutorial.get_state_by_name(state_name)
        self._overview.win.queue_draw()
        state_actions = self._state.get_action_list()
        for action in state_actions:
            action.enter_editmode()
            action._drag._eventbox.connect_after(
                "button-release-event", self._action_refresh_cb, action)

        if state_actions:
            self._propedit.action = state_actions[0]
        else:
            self._propedit.action = None


    def _evfilt_cb(self, menuitem, event):
        """
        This will get called once the user has selected a menu item from the
        event filter popup menu. This should add the correct event filter
        to the FSM and increment states.
        """
        # undo actions so they don't persist through step editing
        for action in self._state.get_action_list():
            action.exit_editmode()
        self._hlmask.covered = None
        self._propedit.action = None
        self._activity.queue_draw()

    def _intro_cb(self, widget, evt):
        """
        Callback for capture of widget events, when in introspect mode.
        """
        if evt.type == gtk.gdk.BUTTON_PRESS:
            # widget has focus, let's hilight it
            win = gtk.gdk.display_get_default().get_window_at_pointer()
            click_wdg = win[0].get_user_data()
            if not click_wdg.is_ancestor(self._activity._overlayer):
                # as popups are not (yet) supported, it would break
                # badly if we were to play with a widget not in the
                # hierarchy.
                return
            for hole in self._intro_mask.pass_thru:
                self._intro_mask.mask(hole)
            self._intro_mask.unmask(click_wdg)
            self._selected_widget = gtkutils.raddr_lookup(click_wdg)

            if self._eventmenu:
                self._eventmenu.destroy()
            self._eventmenu = gtk.Menu()
            menuitem = gtk.MenuItem(label=type(click_wdg).__name__)
            menuitem.set_sensitive(False)
            self._eventmenu.append(menuitem)
            self._eventmenu.append(gtk.MenuItem())

            for item in gobject.signal_list_names(click_wdg):
                menuitem = gtk.MenuItem(label=item)
                menuitem.connect("activate", self._evfilt_cb, item)
                self._eventmenu.append(menuitem)
            self._eventmenu.show_all()
            self._eventmenu.popup(None, None, None, evt.button, evt.time)
            self._activity.queue_draw()

    def _add_action_cb(self, widget, path):
        """Callback for the action creation toolbar tool"""
        action_type = self._propedit._actions_icons[path][2]
        action = addon.create(action_type)
        action.enter_editmode()
        self._state.add_action(action)
        # FIXME: replace following with event catching
        action._drag._eventbox.connect_after(
            "button-release-event", self._action_refresh_cb, action)
        self._overview.win.queue_draw()

    def _add_event_cb(self, widget, path):
        """Callback for the event creation toolbar tool"""
        event_type = self._propedit._events_icons[path][2]
        event = addon.create(event_type)
        addonname = type(event).__name__
        meta = addon.get_addon_meta(addonname)
        for propname in meta['mandatory_props']:
            prop = getattr(type(event), propname)
            if isinstance(prop, properties.TUAMProperty):
                selector = WidgetSelector(self._activity)
                setattr(event, propname, selector.select())
            elif isinstance(prop, properties.TGtkSignal):
                try:
                    dlg = SignalInputDialog(self._activity,
                                            text="Mandatory property",
                                            field=propname,
                                            addr=event.object_id)
                    setattr(event, propname, dlg.pop())
                except AttributeError:
                    pass
            elif isinstance(prop, properties.TStringProperty):
                dlg = TextInputDialog(self._activity,
                                      text="Mandatory property",
                                      field=propname)
                setattr(event, propname, dlg.pop())
            else:
                raise NotImplementedError()

        event_filters = self._state.get_event_filter_list()
        if event_filters:
            # linearize tutorial by inserting state
            new_state = State(name=str(self._state_counter))
            self._state_counter += 1
            self._state.clear_event_filters()
            for evt_filt in event_filters:
                new_state.add_event_filter(evt_filt)
            event.set_next_state(new_state.name)
            # blocks are shifted, full redraw is necessary
            self._overview.win.queue_draw()
        else:
            # append empty event only if edit not inserting between events
            event.set_next_state(str(self._state_counter))
            new_state = State(name=str(self._state_counter))
            self._state_counter += 1

        self._state.add_event_filter(event)
        self._tutorial.add_state(new_state)
        self._overview.win.queue_draw()

        self.set_insertion_point(new_state.name)

    def _action_refresh_cb(self, widget, evt, action):
        """
        Callback for refreshing properties values and notifying the 
        property dialog of the new values.
        """
        action.exit_editmode()
        action.enter_editmode()
        self._activity.queue_draw()
        # TODO: replace following with event catching
        action._drag._eventbox.connect_after(
            "button-release-event", self._action_refresh_cb, action)
        self._propedit.action = action

        self._overview.win.queue_draw()

    def _cleanup_cb(self, *args):
        """
        Quit editing and cleanup interface artifacts.
        """
        # undo actions so they don't persist through step editing
        for action in self._state.get_action_list():
            action.exit_editmode()

        dialog = gtk.MessageDialog(
            parent=self._activity,
            flags=gtk.DIALOG_MODAL,
            type=gtk.MESSAGE_QUESTION,
            buttons=gtk.BUTTONS_YES_NO,
            message_format=T('Do you want to save before stopping edition?'))
        do_save = dialog.run()
        dialog.destroy()
        if do_save == gtk.RESPONSE_YES:
            self.save()

        # remove UI remains
        self._hlmask.covered = None
        self._activity._overlayer.remove(self._hlmask)
        self._hlmask.destroy()
        self._hlmask = None
        self._propedit.destroy()
        self._overview.destroy()
        self._activity.queue_draw()
        del self._activity._creator

    def save(self, widget=None):
        if not self.tuto:
            dlg = TextInputDialog(self._activity,
                                  text=T("Enter a tutorial title."),
                                  field=T("Title"))
            tutorialName = ""
            while not tutorialName: tutorialName = dlg.pop()
            dlg.destroy()

            # prepare tutorial for serialization
            self.tuto = Tutorial(tutorialName, self._tutorial)
        bundle = bundler.TutorialBundler(self._guid)
        self._guid = bundle.Guid
        bundle.write_metadata_file(self.tuto)
        bundle.write_fsm(self._tutorial)


    def launch(*args, **kwargs):
        """
        Launch and attach a creator to the currently running activity.
        """
        activity = ObjectStore().activity
        if not hasattr(activity, "_creator"):
            activity._creator = Creator(activity)
    launch = staticmethod(launch)

class ToolBox(object):
    def __init__(self, parent):
        super(ToolBox, self).__init__()
        self.__parent = parent
        glade_file = os.path.join(__path__[0], 'ui', 'creator.glade')
        self.tree = gtk.glade.XML(glade_file)
        self.window = self.tree.get_widget('mainwindow')
        self._propbox = self.tree.get_widget('propbox')

        self.window.set_transient_for(parent)

        self._action = None
        self._actions_icons = gtk.ListStore(str, gtk.gdk.Pixbuf, str, str)
        self._actions_icons.set_sort_column_id(0, gtk.SORT_ASCENDING)
        self._events_icons = gtk.ListStore(str, gtk.gdk.Pixbuf, str, str)
        self._events_icons.set_sort_column_id(0, gtk.SORT_ASCENDING)

        for toolname in addon.list_addons():
            meta = addon.get_addon_meta(toolname)
            iconfile = gtk.Image()
            iconfile.set_from_file(icon.get_icon_file_name(meta['icon']))
            img = iconfile.get_pixbuf()
            label = format_multiline(meta['display_name'])

            if meta['type'] == addon.TYPE_ACTION:
                self._actions_icons.append((label, img, toolname, meta['display_name']))
            else:
                self._events_icons.append((label, img, toolname, meta['display_name']))

        iconview1 = self.tree.get_widget('iconview1')
        iconview1.set_model(self._actions_icons)
        iconview1.set_text_column(0)
        iconview1.set_pixbuf_column(1)
        iconview1.set_tooltip_column(3)
        iconview2 = self.tree.get_widget('iconview2')
        iconview2.set_model(self._events_icons)
        iconview2.set_text_column(0)
        iconview2.set_pixbuf_column(1)
        iconview2.set_tooltip_column(3)

        self.window.show()

    def destroy(self):
        """ clean and free the toolbox """
        self.window.destroy()

    def refresh_properties(self):
        """Refresh property values from the selected action."""
        if self._action is None:
            return
        props = self._action._props.keys()
        for propnum in xrange(len(props)):
            row = self._propbox.get_children()[propnum]
            propname = props[propnum]
            prop = getattr(type(self._action), propname)
            propval = getattr(self._action, propname)
            if isinstance(prop, properties.TStringProperty):
                propwdg = row.get_children()[1]
                propwdg.get_buffer().set_text(propval)
            elif isinstance(prop, properties.TIntProperty):
                propwdg = row.get_children()[1]
                propwdg.set_value(propval)
            elif isinstance(prop, properties.TArrayProperty):
                propwdg = row.get_children()[1]
                for i in xrange(len(propval)):
                    entry = propwdg.get_children()[i]
                    entry.set_text(str(propval[i]))
            else:
                propwdg = row.get_children()[1]
                propwdg.set_text(str(propval))

    def set_action(self, action):
        """Setter for the action property."""
        if self._action is action:
            self.refresh_properties()
            return
        for old_prop in self._propbox.get_children():
            self._propbox.remove(old_prop)

        self._action = action
        if action is None:
            return
        for propname in action._props.keys():
            row = gtk.HBox()
            row.pack_start(gtk.Label(T(propname)), False, False, 10)
            prop = getattr(type(action), propname)
            propval = getattr(action, propname)
            if isinstance(prop, properties.TStringProperty):
                propwdg = gtk.TextView()
                propwdg.get_buffer().set_text(propval)
                propwdg.connect_after("focus-out-event", \
                    self._str_prop_changed, action, propname)
            elif isinstance(prop, properties.TIntProperty):
                adjustment = gtk.Adjustment(value=propval,
                    lower=prop.lower_limit.limit,
                    upper=prop.upper_limit.limit,
                    step_incr=1)
                propwdg = gtk.SpinButton(adjustment=adjustment)
                propwdg.connect_after("focus-out-event", \
                    self._int_prop_changed, action, prop)
            elif isinstance(prop, properties.TArrayProperty):
                propwdg = gtk.HBox()
                for i in xrange(len(propval)):
                    entry = gtk.Entry()
                    propwdg.pack_start(entry)
                    entry.connect_after("focus-out-event", \
                        self._list_prop_changed, action, propname, i)
            else:
                propwdg = gtk.Entry()
                propwdg.set_text(str(propval))
            row.pack_end(propwdg)
            self._propbox.pack_start(row, expand=False)
        self._propbox.show_all()
        self.refresh_properties()

    def get_action(self):
        """Getter for the action property"""
        return self._action
    action = property(fset=set_action, fget=get_action, doc=\
        "Action to be edited through introspection.")

    def _list_prop_changed(self, widget, evt, action, propname, idx):
        try:
            #Save props as tuples so that they can be hashed
            attr = list(getattr(action, propname))
            attr[idx] = int(widget.get_text())
            setattr(action, propname, tuple(attr))
        except ValueError:
            widget.set_text(str(getattr(action, propname)[idx]))
        self.__parent._creator._action_refresh_cb(None, None, action)
    def _str_prop_changed(self, widget, evt, action, propname):
        buf = widget.get_buffer()
        setattr(action, propname, buf.get_text(buf.get_start_iter(), buf.get_end_iter()))
        self.__parent._creator._action_refresh_cb(None, None, action)
    def _int_prop_changed(self, widget, evt, action, prop):
        setattr(action, propname, widget.get_value_as_int())
        self.__parent._creator._action_refresh_cb(None, None, action)


class WidgetSelector(object):
    """
    Allow selecting a widget from within a window without interrupting the
    flow of the current call.

    The selector will run on the specified window until either a widget
    is selected or abort() gets called.
    """
    def __init__(self, window):
        super(WidgetSelector, self).__init__()
        self.window = window
        self._intro_mask = None
        self._intro_handle = None
        self._select_handle = None
        self._prelight = None

    def select(self):
        """
        Starts selecting a widget, by grabbing control of the mouse and
        highlighting hovered widgets until one is clicked.
        @returns: a widget address or None
        """
        if not self._intro_mask:
            self._prelight = None
            self._intro_mask = overlayer.Mask(catch_events=True)
            self._select_handle = self._intro_mask.connect_after(
                "button-press-event", self._end_introspect)
            self._intro_handle = self._intro_mask.connect_after(
                "motion-notify-event", self._intro_cb)
            self.window._overlayer.put(self._intro_mask, 0, 0)
            self.window._overlayer.queue_draw()

            while bool(self._intro_mask) and not gtk.main_iteration():
                pass

            return gtkutils.raddr_lookup(self._prelight)

    def _end_introspect(self, widget, evt):
        if evt.type == gtk.gdk.BUTTON_PRESS and self._prelight:
            self._intro_mask.catch_events = False
            self._intro_mask.disconnect(self._intro_handle)
            self._intro_handle = None
            self._intro_mask.disconnect(self._select_handle)
            self._select_handle = None
            self.window._overlayer.remove(self._intro_mask)
            self._intro_mask = None
            # for some reason, gtk may not redraw after this unless told to.
            self.window.queue_draw()

    def _intro_cb(self, widget, evt):
        """
        Callback for capture of widget events, when in introspect mode.
        """
        # widget has focus, let's hilight it
        win = gtk.gdk.display_get_default().get_window_at_pointer()
        if not win:
            return
        click_wdg = win[0].get_user_data()
        if not click_wdg.is_ancestor(self.window._overlayer):
            # as popups are not (yet) supported, it would break
            # badly if we were to play with a widget not in the
            # hierarchy.
            return
        for hole in self._intro_mask.pass_thru:
            self._intro_mask.mask(hole)
        self._intro_mask.unmask(click_wdg)
        self._prelight = click_wdg

        self.window.queue_draw()

    def abort(self):
        """
        Ends the selection. The control will return to the select() caller
        with a return value of None, as selection was aborted.
        """
        self._intro_mask.catch_events = False
        self._intro_mask.disconnect(self._intro_handle)
        self._intro_handle = None
        self._intro_mask.disconnect(self._select_handle)
        self._select_handle = None
        self.window._overlayer.remove(self._intro_mask)
        self._intro_mask = None
        self._prelight = None

class SignalInputDialog(gtk.MessageDialog):
    def __init__(self, parent, text, field, addr):
        """
        Create a gtk signal selection dialog.

        @param parent: the parent window this dialog should stay over.
        @param text: the title of the dialog.
        @param field: the field description of the dialog.
        @param addr: the widget address from which to fetch signal list.
        """
        gtk.MessageDialog.__init__(self, parent,
            gtk.DIALOG_MODAL | gtk.DIALOG_DESTROY_WITH_PARENT,
            gtk.MESSAGE_QUESTION,
            gtk.BUTTONS_OK,
            None)
        self.set_markup(text)
        self.model = gtk.ListStore(str)
        widget = gtkutils.find_widget(parent, addr)
        for signal_name in gobject.signal_list_names(widget):
            self.model.append(row=(signal_name,))
        self.entry = gtk.ComboBox(self.model)
        cell = gtk.CellRendererText()
        self.entry.pack_start(cell)
        self.entry.add_attribute(cell, 'text', 0)
        hbox = gtk.HBox()
        lbl = gtk.Label(field)
        hbox.pack_start(lbl, False)
        hbox.pack_end(self.entry)
        self.vbox.pack_end(hbox, True, True)
        self.show_all()

    def pop(self):
        """
        Show the dialog. It will run in it's own loop and return control 
        to the caller when a signal has been selected.

        @returns: a signal name or None if no signal was selected
        """
        self.run()
        self.hide()
        iter = self.entry.get_active_iter()
        if iter:
            text = self.model.get_value(iter, 0)
            return text
        return None

    def _dialog_done_cb(self, entry, response):
        self.response(response)

class TextInputDialog(gtk.MessageDialog):
    def __init__(self, parent, text, field):
        gtk.MessageDialog.__init__(self, parent,
            gtk.DIALOG_MODAL | gtk.DIALOG_DESTROY_WITH_PARENT,
            gtk.MESSAGE_QUESTION,
            gtk.BUTTONS_OK,
            None)
        self.set_markup(text)
        self.entry = gtk.Entry()
        self.entry.connect("activate", self._dialog_done_cb, gtk.RESPONSE_OK)
        hbox = gtk.HBox()
        lbl = gtk.Label(field)
        hbox.pack_start(lbl, False)
        hbox.pack_end(self.entry)
        self.vbox.pack_end(hbox, True, True)
        self.show_all()

    def pop(self):
        self.run()
        self.hide()
        text = self.entry.get_text()
        return text

    def _dialog_done_cb(self, entry, response):
        self.response(response)

def format_multiline(text, length=10, lines=3, line_separator='\n'):
    """
    Reformat a text to fit in a small space.

    @param length: maximum char per line
    @param lines: maximum number of lines
    """
    words = text.split(' ')
    line = list()
    return_val = []
    linelen = 0

    for word in words:
        t_len = linelen+len(word)
        if t_len < length:
            line.append(word)
            linelen = t_len+1 # count space
        else:
            if len(return_val)+1 < lines:
                return_val.append(' '.join(line))
                line = list()
                linelen = 0
                line.append(word)
            else:
                return_val.append(' '.join(line+['...']))
                return line_separator.join(return_val)

    return_val.append(' '.join(line))
    return line_separator.join(return_val)


# vim:set ts=4 sts=4 sw=4 et: