1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 import os.path
20 import webbrowser
21
22 import wx.html
23
24 from timelinelib.config.paths import HELP_RESOURCES_DIR
25 from timelinelib.config.paths import ICONS_DIR
26 from timelinelib.wxgui.utils import display_error_message
27
28
30
31 HOME_ID = 10
32 BACKWARD_ID = 20
33 FORWARD_ID = 30
34
36 wx.Frame.__init__(self, parent, title=_("Help"),
37 size=(600, 650), style=wx.DEFAULT_FRAME_STYLE)
38 self.history = []
39 self.current_pos = -1
40 self._create_help_system()
41 self._create_gui()
42 self._update_buttons()
43
55
57 self.show_page("contents")
58
61
62 - def show_page(self, id, type="page", change_history=True):
63 """
64 Where which is a tuple (type, id):
65
66 * (page, page_id)
67 * (search, search_string)
68 """
69 if self.help_system is None:
70 display_error_message(
71 _("Could not find markdown Python package. It is needed by the help system."),
72 self.GetParent())
73 return
74 if change_history:
75 same_page_as_last = False
76 if self.current_pos != -1:
77 _, current_id = self.history[self.current_pos]
78 if id == current_id:
79 same_page_as_last = True
80 if same_page_as_last is False:
81 self.history = self.history[:self.current_pos + 1]
82 self.history.append((type, id))
83 self.current_pos += 1
84 self._update_buttons()
85 if type == "page":
86 self.html_window.SetPage(self._generate_page(id))
87 elif type == "search":
88 self.html_window.SetPage(self.help_system.get_search_results_page(id))
89 self.Show()
90 self.Raise()
91
93 self.Bind(wx.EVT_CLOSE, self._window_on_close)
94 self.toolbar = self.CreateToolBar()
95 size = (24, 24)
96 if 'wxMSW' in wx.PlatformInfo:
97 home_bmp = wx.Bitmap(os.path.join(ICONS_DIR, "home.png"))
98 back_bmp = wx.Bitmap(os.path.join(ICONS_DIR, "backward.png"))
99 forward_bmp = wx.Bitmap(os.path.join(ICONS_DIR, "forward.png"))
100 else:
101 home_bmp = wx.ArtProvider.GetBitmap(wx.ART_GO_HOME, wx.ART_TOOLBAR,
102 size)
103 back_bmp = wx.ArtProvider.GetBitmap(wx.ART_GO_BACK, wx.ART_TOOLBAR,
104 size)
105 forward_bmp = wx.ArtProvider.GetBitmap(wx.ART_GO_FORWARD,
106 wx.ART_TOOLBAR, size)
107 self.toolbar.SetToolBitmapSize(size)
108
109 home_str = _("Go to home page")
110 self.toolbar.AddTool(HelpBrowserFrame.HOME_ID, home_str,
111 home_bmp, shortHelp=home_str)
112 self.Bind(wx.EVT_TOOL, self._toolbar_on_click, id=HelpBrowserFrame.HOME_ID)
113
114 self.toolbar.AddSeparator()
115
116 backward_str = _("Go back one page")
117 self.toolbar.AddTool(HelpBrowserFrame.BACKWARD_ID, backward_str,
118 back_bmp, shortHelp=backward_str)
119 self.Bind(wx.EVT_TOOL, self._toolbar_on_click,
120 id=HelpBrowserFrame.BACKWARD_ID)
121
122 forward_str = _("Go forward one page")
123 self.toolbar.AddTool(HelpBrowserFrame.FORWARD_ID, forward_str,
124 forward_bmp, shortHelp=forward_str)
125 self.Bind(wx.EVT_TOOL, self._toolbar_on_click,
126 id=HelpBrowserFrame.FORWARD_ID)
127
128 self.toolbar.AddSeparator()
129
130 self.search = wx.SearchCtrl(self.toolbar, size=(150, -1),
131 style=wx.TE_PROCESS_ENTER)
132 self.Bind(wx.EVT_TEXT_ENTER, self._search_on_text_enter, self.search)
133 self.toolbar.AddControl(self.search)
134 self.toolbar.Realize()
135
136 self.html_window = wx.html.HtmlWindow(self)
137 self.Bind(wx.html.EVT_HTML_LINK_CLICKED,
138 self._html_window_on_link_clicked, self.html_window)
139 self.html_window.Connect(wx.ID_ANY, wx.ID_ANY, wx.EVT_KEY_DOWN.typeId,
140 self._window_on_key_down)
141
144
146 """
147 Event handler used when a keyboard key has been pressed.
148
149 The following keys are handled:
150 Key Action
151 -------- ------------------------------------
152 Backspace Go to previous page
153 """
154 keycode = evt.GetKeyCode()
155 if keycode == wx.WXK_BACK:
156 self._go_back()
157 evt.Skip()
158
166
168 self._search(self.search.GetValue())
169
171 url = e.GetLinkInfo().GetHref()
172 if url.startswith("page:"):
173 self.show_page(url[5:])
174 else:
175 webbrowser.open(url)
176
178 self.show_page(self.help_system.home_page)
179
181 if self.current_pos > 0:
182 self.current_pos -= 1
183 current_type, current_id = self.history[self.current_pos]
184 self.show_page(current_id, type=current_type, change_history=False)
185
187 if self.current_pos < len(self.history) - 1:
188 self.current_pos += 1
189 current_type, current_id = self.history[self.current_pos]
190 self.show_page(current_id, type=current_type, change_history=False)
191
194
201
202 - def _generate_page(self, page_id):
203 page = self.help_system.get_page(page_id)
204 if page is None:
205 error_page_html = "<h1>%s</h1><p>%s</p>" % (
206 _("Page not found"),
207 _("Could not find page '%s'.") % page_id)
208 return self._wrap_in_html(error_page_html)
209 else:
210 return self._wrap_in_html(page.render_to_html())
211
213 HTML_SKELETON = """
214 <html>
215 <head>
216 </head>
217 <body>
218 %s
219 </body>
220 </html>
221 """
222 return HTML_SKELETON % content
223