Imported PyGObject's pygtkcompat.py for better compatibility
[zeroinstall.git] / zeroinstall / gtkui / help_box.py
blob6d500e9dd29acadab1d832eb979c07d4afd084a7
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
6 import sys
8 class HelpBox:
9 """A dialog for showing longish help texts.
10 The GTK widget is not created until L{display} is called.
11 """
12 box = None
13 title = None
14 sections = None
16 def __init__(self, title, *sections):
17 """Constructor.
18 @param title: window title
19 @param sections: the content, as a list of (section_title, section_body) pairs
20 @type sections: [(str, str)]"""
21 self.title = title
22 self.sections = sections
24 def display(self):
25 """Display this help text. If it is already displayed, close the old window first."""
26 if self.box:
27 self.box.destroy()
28 assert not self.box
30 self.box = box = gtk.Dialog()
31 if sys.version_info[0] < 3:
32 self.box.set_has_separator(False)
33 self.box.set_position(gtk.WIN_POS_CENTER)
34 box.set_title(self.title)
36 swin = gtk.ScrolledWindow(None, None)
37 swin.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_ALWAYS)
38 swin.set_shadow_type(gtk.SHADOW_IN)
39 swin.set_border_width(2)
40 box.get_content_area().pack_start(swin, True, True, 0)
42 text = gtk.TextView()
43 text.set_left_margin(4)
44 text.set_right_margin(4)
45 text.set_wrap_mode(gtk.WRAP_WORD)
46 text.set_editable(False)
47 text.set_cursor_visible(False)
48 model = text.get_buffer()
49 titer = model.get_start_iter()
50 heading_style = model.create_tag(underline = True, scale = 1.2)
52 first = True
53 for title, body in self.sections:
54 if first:
55 first = False
56 else:
57 model.insert(titer, '\n\n')
58 model.insert_with_tags(titer, title, heading_style)
59 model.insert(titer, '\n' + body.strip())
60 swin.add(text)
62 swin.show_all()
64 box.add_button(gtk.STOCK_CLOSE, gtk.RESPONSE_CANCEL)
65 box.connect('response', lambda box, resp: box.destroy())
67 box.set_default_response(gtk.RESPONSE_CANCEL)
69 def destroyed(box):
70 self.box = None
71 box.connect('destroy', destroyed)
73 box.set_position(gtk.WIN_POS_CENTER)
74 box.set_default_size(gtk.gdk.screen_width() / 4,
75 gtk.gdk.screen_height() / 3)
76 box.show()