Help sample should work for .zip help files now.
[wxWidgets.git] / samples / help / demo.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: demo.cpp
3 // Purpose: wxHelpController demo
4 // Author: Karsten Ballueder
5 // Modified by:
6 // Created: 04/01/98
7 // RCS-ID: $Id$
8 // Copyright: (c) Karsten Ballueder, Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 // ----------------------------------------------------------------------------
17 // headers
18 // ----------------------------------------------------------------------------
19
20 // For compilers that support precompilation, includes "wx/wx.h".
21 #include "wx/wxprec.h"
22
23 #ifdef __BORLANDC__
24 # pragma hdrstop
25 #endif
26
27 // for all others, include the necessary headers (this file is usually all you
28 // need because it includes almost all "standard" wxWindows headers
29 #ifndef WX_PRECOMP
30 # include "wx/wx.h"
31 #endif
32
33 # include "wx/image.h"
34 # include "wx/help.h"
35
36 // define this to 1 to use HTML help even under Windows (by default, Windows
37 // version will use WinHelp).
38 // Please also see samples/html/helpview.
39
40 #define USE_HTML_HELP 1
41
42 #if !wxUSE_HTML
43 #undef USE_HTML_HELP
44 #define USE_HTML_HELP 0
45 #endif
46
47 #if USE_HTML_HELP
48
49 #include <wx/filesys.h>
50 #include <wx/fs_zip.h>
51
52 #include "wx/generic/helpwxht.h"
53 #include "wx/html/helpctrl.h"
54 #endif
55
56 // ----------------------------------------------------------------------------
57 // ressources
58 // ----------------------------------------------------------------------------
59 // the application icon
60 #if defined(__WXGTK__) || defined(__WXMOTIF__)
61 #include "mondrian.xpm"
62 #endif
63
64 // ----------------------------------------------------------------------------
65 // private classes
66 // ----------------------------------------------------------------------------
67
68 // Define a new application type, each program should derive a class from wxApp
69 class MyApp : public wxApp
70 {
71 public:
72 // override base class virtuals
73 // ----------------------------
74
75 // this one is called on application startup and is a good place for the app
76 // initialization (doing it here and not in the ctor allows to have an error
77 // return: if OnInit() returns false, the application terminates)
78 virtual bool OnInit();
79 };
80
81 // Define a new frame type: this is going to be our main frame
82 class MyFrame : public wxFrame
83 {
84 public:
85 // ctor(s)
86 MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size);
87
88 wxHelpController& GetHelpController() { return m_help; }
89
90 #if USE_HTML_HELP
91 wxHelpControllerHtml& GetHtmlHelpController() { return m_htmlHelp; }
92 wxHtmlHelpController& GetAdvancedHtmlHelpController() { return m_advancedHtmlHelp; }
93 #endif
94
95 // event handlers (these functions should _not_ be virtual)
96 void OnQuit(wxCommandEvent& event);
97 void OnHelp(wxCommandEvent& event);
98 void OnHtmlHelp(wxCommandEvent& event);
99 void OnAdvancedHtmlHelp(wxCommandEvent& event);
100
101 void ShowHelp(int commandId, wxHelpControllerBase& helpController);
102
103 private:
104 wxHelpController m_help;
105
106 #if USE_HTML_HELP
107 wxHelpControllerHtml m_htmlHelp;
108 wxHtmlHelpController m_advancedHtmlHelp;
109 #endif
110
111 // any class wishing to process wxWindows events must use this macro
112 DECLARE_EVENT_TABLE()
113 };
114
115 // ----------------------------------------------------------------------------
116 // constants
117 // ----------------------------------------------------------------------------
118
119 // IDs for the controls and the menu commands
120 enum
121 {
122 // menu items
123 HelpDemo_Quit = 1,
124 HelpDemo_Help_Index,
125 HelpDemo_Help_Classes,
126 HelpDemo_Help_Functions,
127 HelpDemo_Help_Help,
128 HelpDemo_Help_Search,
129
130 HelpDemo_Html_Help_Index,
131 HelpDemo_Html_Help_Classes,
132 HelpDemo_Html_Help_Functions,
133 HelpDemo_Html_Help_Help,
134 HelpDemo_Html_Help_Search,
135
136 HelpDemo_Advanced_Html_Help_Index,
137 HelpDemo_Advanced_Html_Help_Classes,
138 HelpDemo_Advanced_Html_Help_Functions,
139 HelpDemo_Advanced_Html_Help_Help,
140 HelpDemo_Advanced_Html_Help_Search,
141
142 HelpDemo_Help_KDE,
143 HelpDemo_Help_GNOME,
144 HelpDemo_Help_Netscape,
145 // controls start here (the numbers are, of course, arbitrary)
146 HelpDemo_Text = 1000,
147 };
148
149 // ----------------------------------------------------------------------------
150 // event tables and other macros for wxWindows
151 // ----------------------------------------------------------------------------
152
153 // the event tables connect the wxWindows events with the functions (event
154 // handlers) which process them. It can be also done at run-time, but for the
155 // simple menu events like this the static method is much simpler.
156 BEGIN_EVENT_TABLE(MyFrame, wxFrame)
157 EVT_MENU(HelpDemo_Quit, MyFrame::OnQuit)
158 EVT_MENU(HelpDemo_Help_Index, MyFrame::OnHelp)
159 EVT_MENU(HelpDemo_Help_Classes, MyFrame::OnHelp)
160 EVT_MENU(HelpDemo_Help_Functions, MyFrame::OnHelp)
161 EVT_MENU(HelpDemo_Help_Help, MyFrame::OnHelp)
162 EVT_MENU(HelpDemo_Help_Search, MyFrame::OnHelp)
163
164 EVT_MENU(HelpDemo_Html_Help_Index, MyFrame::OnHtmlHelp)
165 EVT_MENU(HelpDemo_Html_Help_Classes, MyFrame::OnHtmlHelp)
166 EVT_MENU(HelpDemo_Html_Help_Functions, MyFrame::OnHtmlHelp)
167 EVT_MENU(HelpDemo_Html_Help_Help, MyFrame::OnHtmlHelp)
168 EVT_MENU(HelpDemo_Html_Help_Search, MyFrame::OnHtmlHelp)
169
170 EVT_MENU(HelpDemo_Advanced_Html_Help_Index, MyFrame::OnAdvancedHtmlHelp)
171 EVT_MENU(HelpDemo_Advanced_Html_Help_Classes, MyFrame::OnAdvancedHtmlHelp)
172 EVT_MENU(HelpDemo_Advanced_Html_Help_Functions, MyFrame::OnAdvancedHtmlHelp)
173 EVT_MENU(HelpDemo_Advanced_Html_Help_Help, MyFrame::OnAdvancedHtmlHelp)
174 EVT_MENU(HelpDemo_Advanced_Html_Help_Search, MyFrame::OnAdvancedHtmlHelp)
175
176 EVT_MENU(HelpDemo_Help_KDE, MyFrame::OnHelp)
177 EVT_MENU(HelpDemo_Help_GNOME, MyFrame::OnHelp)
178 EVT_MENU(HelpDemo_Help_Netscape, MyFrame::OnHelp)
179 END_EVENT_TABLE()
180
181 // Create a new application object: this macro will allow wxWindows to create
182 // the application object during program execution (it's better than using a
183 // static object for many reasons) and also declares the accessor function
184 // wxGetApp() which will return the reference of the right type (i.e. MyApp and
185 // not wxApp)
186 IMPLEMENT_APP(MyApp)
187
188 // ============================================================================
189 // implementation
190 // ============================================================================
191
192 // ----------------------------------------------------------------------------
193 // the application class
194 // ----------------------------------------------------------------------------
195
196 // `Main program' equivalent: the program execution "starts" here
197 bool MyApp::OnInit()
198 {
199 #if wxUSE_HTML
200 #if wxUSE_GIF
201 // Required for images in the online documentation
202 wxImage::AddHandler(new wxGIFHandler);
203
204 // Required for advanced HTML help
205 #if wxUSE_STREAMS && wxUSE_ZIPSTREAM && wxUSE_ZLIB
206 wxFileSystem::AddHandler(new wxZipFSHandler);
207 #endif
208
209 #endif
210 #endif
211
212 // Create the main application window
213 MyFrame *frame = new MyFrame("HelpDemo wxWindows App",
214 wxPoint(50, 50), wxSize(450, 340));
215
216 frame->Show(TRUE);
217 SetTopWindow(frame);
218
219 // initialise the help system: this means that we'll use doc.hlp file under
220 // Windows and that the HTML docs are in the subdirectory doc for platforms
221 // using HTML help
222 if ( !frame->GetHelpController().Initialize("doc") )
223 {
224 wxLogError("Cannot initialize the help system, aborting.");
225
226 return FALSE;
227 }
228
229 #if USE_HTML_HELP
230 // initialise the standard HTML help system: this means that the HTML docs are in the
231 // subdirectory doc for platforms using HTML help
232 if ( !frame->GetHtmlHelpController().Initialize("doc") )
233 {
234 wxLogError("Cannot initialize the HTML help system, aborting.");
235
236 return FALSE;
237 }
238
239 // initialise the advanced HTML help system: this means that the HTML docs are in .htb
240 // (zipped) form
241 if ( !frame->GetAdvancedHtmlHelpController().Initialize("doc") )
242 {
243 wxLogError("Cannot initialize the advanced HTML help system, aborting.");
244
245 return FALSE;
246 }
247 #endif
248
249 return TRUE;
250 }
251
252 // ----------------------------------------------------------------------------
253 // main frame
254 // ----------------------------------------------------------------------------
255
256 // frame constructor
257 MyFrame::MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size)
258 : wxFrame((wxFrame *)NULL, -1, title, pos, size)
259 {
260 // set the frame icon
261 SetIcon(wxICON(mondrian));
262
263 // create a menu bar
264 wxMenu *menuFile = new wxMenu;
265
266 menuFile->Append(HelpDemo_Help_Index, "&Help Index...");
267 menuFile->Append(HelpDemo_Help_Classes, "&Help on Classes...");
268 menuFile->Append(HelpDemo_Help_Functions, "&Help on Functions...");
269 menuFile->Append(HelpDemo_Help_Help, "&About Help Demo...");
270 menuFile->Append(HelpDemo_Help_Search, "&Search help...");
271 #if USE_HTML_HELP
272 menuFile->AppendSeparator();
273 menuFile->Append(HelpDemo_Html_Help_Index, "HTML &Help Index...");
274 menuFile->Append(HelpDemo_Html_Help_Classes, "HTML &Help on Classes...");
275 menuFile->Append(HelpDemo_Html_Help_Functions, "HTML &Help on Functions...");
276 menuFile->Append(HelpDemo_Html_Help_Help, "HTML &About Help Demo...");
277 menuFile->Append(HelpDemo_Html_Help_Search, "HTML &Search help...");
278 menuFile->AppendSeparator();
279 menuFile->Append(HelpDemo_Advanced_Html_Help_Index, "Advanced HTML &Help Index...");
280 menuFile->Append(HelpDemo_Advanced_Html_Help_Classes, "Advanced HTML &Help on Classes...");
281 menuFile->Append(HelpDemo_Advanced_Html_Help_Functions, "Advanced HTML &Help on Functions...");
282 menuFile->Append(HelpDemo_Advanced_Html_Help_Help, "Advanced HTML &About Help Demo...");
283 menuFile->Append(HelpDemo_Advanced_Html_Help_Search, "Advanced HTML &Search help...");
284 #endif
285
286 #ifndef __WXMSW__
287 #if !wxUSE_HTML
288 menuFile->AppendSeparator();
289 menuFile->Append(HelpDemo_Help_KDE, "Use &KDE");
290 menuFile->Append(HelpDemo_Help_GNOME, "Use &GNOME");
291 menuFile->Append(HelpDemo_Help_Netscape, "Use &Netscape");
292 #endif
293 #endif
294 menuFile->AppendSeparator();
295 menuFile->Append(HelpDemo_Quit, "E&xit");
296
297 // now append the freshly created menu to the menu bar...
298 wxMenuBar *menuBar = new wxMenuBar;
299 menuBar->Append(menuFile, "&File");
300
301 // ... and attach this menu bar to the frame
302 SetMenuBar(menuBar);
303
304 // create a status bar just for fun (by default with 1 pane only)
305 CreateStatusBar();
306 SetStatusText("Welcome to wxWindows!");
307
308 // now create some controls
309
310 // a panel first - if there were several controls, it would allow us to
311 // navigate between them from the keyboard
312 wxPanel *panel = new wxPanel(this, -1, wxPoint(0, 0), wxSize(400, 200));
313
314 // and a static control whose parent is the panel
315 (void)new wxStaticText(panel, -1, "Hello, world!", wxPoint(10, 10));
316 }
317
318
319 // event handlers
320
321 void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
322 {
323 // TRUE is to force the frame to close
324 Close(TRUE);
325 }
326
327 void MyFrame::OnHelp(wxCommandEvent& event)
328 {
329 ShowHelp(event.GetId(), m_help);
330 }
331
332 void MyFrame::OnHtmlHelp(wxCommandEvent& event)
333 {
334 #if USE_HTML_HELP
335 ShowHelp(event.GetId(), m_htmlHelp);
336 #endif
337 }
338
339 void MyFrame::OnAdvancedHtmlHelp(wxCommandEvent& event)
340 {
341 #if USE_HTML_HELP
342 ShowHelp(event.GetId(), m_advancedHtmlHelp);
343 #endif
344 }
345
346 void MyFrame::ShowHelp(int commandId, wxHelpControllerBase& helpController)
347 {
348 switch(commandId)
349 {
350
351 // Note: For WinHelp, these ids are specified in the map session, mapping
352 // topic names to numbers.
353 // For HTML and external help, a wxhelp.map file is used.
354
355 case HelpDemo_Help_Classes:
356 case HelpDemo_Html_Help_Classes:
357 case HelpDemo_Advanced_Html_Help_Classes:
358 helpController.DisplaySection(2);
359
360 // if (helpController.IsKindOf(CLASSINFO(wxHtmlHelpController)))
361 // ((wxHtmlHelpController&)helpController).Display("Classes"); // An alternative form for this controller
362
363 break;
364 case HelpDemo_Help_Functions:
365 case HelpDemo_Html_Help_Functions:
366 case HelpDemo_Advanced_Html_Help_Functions:
367 helpController.DisplaySection(1);
368 break;
369 case HelpDemo_Help_Help:
370 case HelpDemo_Html_Help_Help:
371 case HelpDemo_Advanced_Html_Help_Help:
372 helpController.DisplaySection(3);
373 break;
374
375 case HelpDemo_Help_Search:
376 case HelpDemo_Html_Help_Search:
377 case HelpDemo_Advanced_Html_Help_Search:
378 {
379 wxString key = wxGetTextFromUser("Search for?",
380 "Search help for keyword",
381 "",
382 this);
383 if(! key.IsEmpty())
384 helpController.KeywordSearch(key);
385 }
386 break;
387
388 case HelpDemo_Help_Index:
389 case HelpDemo_Html_Help_Index:
390 case HelpDemo_Advanced_Html_Help_Index:
391 helpController.DisplayContents();
392 break;
393
394 // These three calls are only used by wxExtHelpController
395
396 case HelpDemo_Help_KDE:
397 helpController.SetViewer("kdehelp");
398 break;
399 case HelpDemo_Help_GNOME:
400 helpController.SetViewer("gnome-help-browser");
401 break;
402 case HelpDemo_Help_Netscape:
403 helpController.SetViewer("netscape", wxHELP_NETSCAPE);
404 break;
405
406 default:
407 break;
408 }
409 }
410