Update year to 2009 in various places
[zeroinstall/zeroinstall-rsl.git] / zeroinstall / gtkui / help_box.py
blob0d6715c5a99625ae7d147134f6e2b7c4f427733c
1 """A dialog box for displaying help text."""
2 # Copyright (C) 2009, Thomas Leonard
3 # See the README file for details, or visit http://0install.net.
5 import gtk
7 class HelpBox:
8 """A dialog for showing longish help texts.
9 The GTK widget is not created until L{display} is called.
10 """
11 box = None
12 title = None
13 sections = None
15 def __init__(self, title, *sections):
16 """Constructor.
17 @param title: window title
18 @param sections: the content, as a list of (section_title, section_body) pairs
19 @type sections: [(str, str)]"""
20 self.title = title
21 self.sections = sections
23 def display(self):
24 """Display this help text. If it is already displayed, close the old window first."""
25 if self.box:
26 self.box.destroy()
27 assert not self.box
29 self.box = box = gtk.Dialog()
30 self.box.set_has_separator(False)
31 self.box.set_position(gtk.WIN_POS_CENTER)
32 box.set_title(self.title)
33 box.set_has_separator(False)
35 swin = gtk.ScrolledWindow(None, None)
36 swin.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_ALWAYS)
37 swin.set_shadow_type(gtk.SHADOW_IN)
38 swin.set_border_width(2)
39 box.vbox.pack_start(swin, True, True)
41 text = gtk.TextView()
42 text.set_left_margin(4)
43 text.set_right_margin(4)
44 text.set_wrap_mode(gtk.WRAP_WORD)
45 text.set_editable(False)
46 text.set_cursor_visible(False)
47 model = text.get_buffer()
48 titer = model.get_start_iter()
49 heading_style = model.create_tag(underline = True, scale = 1.2)
51 first = True
52 for title, body in self.sections:
53 if first:
54 first = False
55 else:
56 model.insert(titer, '\n\n')
57 model.insert_with_tags(titer, title, heading_style)
58 model.insert(titer, '\n' + body.strip())
59 swin.add(text)
61 swin.show_all()
63 box.add_button(gtk.STOCK_CLOSE, gtk.RESPONSE_CANCEL)
64 box.connect('response', lambda box, resp: box.destroy())
66 box.set_default_response(gtk.RESPONSE_CANCEL)
68 def destroyed(box):
69 self.box = None
70 box.connect('destroy', destroyed)
72 box.set_position(gtk.WIN_POS_CENTER)
73 box.set_default_size(gtk.gdk.screen_width() / 4,
74 gtk.gdk.screen_height() / 3)
75 box.show()