]> git.saurik.com Git - wxWidgets.git/blame - samples/help/demo.cpp
Had accidentally added gridwork.tex to the docs
[wxWidgets.git] / samples / help / demo.cpp
CommitLineData
de5c0ba7 1/////////////////////////////////////////////////////////////////////////////
33b64e6f
JS
2// Name: demo.cpp
3// Purpose: wxHelpController demo
4// Author: Karsten Ballueder
de5c0ba7
KB
5// Modified by:
6// Created: 04/01/98
7// RCS-ID: $Id$
33b64e6f
JS
8// Copyright: (c) Karsten Ballueder, Julian Smart
9// Licence: wxWindows licence
de5c0ba7
KB
10/////////////////////////////////////////////////////////////////////////////
11
12// ============================================================================
13// declarations
14// ============================================================================
15
16// ----------------------------------------------------------------------------
17// headers
18// ----------------------------------------------------------------------------
f96b60aa 19
de5c0ba7
KB
20// For compilers that support precompilation, includes "wx/wx.h".
21#include "wx/wxprec.h"
22
23#ifdef __BORLANDC__
29ea4a29 24# pragma hdrstop
de5c0ba7
KB
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
29ea4a29 30# include "wx/wx.h"
de5c0ba7
KB
31#endif
32
7c4a59a8
JS
33# include "wx/image.h"
34# include "wx/help.h"
35
29ea4a29 36// define this to 1 to use HTML help even under Windows (by default, Windows
7c4a59a8 37// version will use WinHelp).
5da69e38 38// Please also see samples/html/helpview.
7c4a59a8 39
d22699b5 40#define USE_HTML_HELP 1
7c4a59a8 41
f6bcfd97
BP
42// Use old-style HTML help if 1
43#define USE_OLD_HTML_HELP 0
44
7c4a59a8
JS
45#if !wxUSE_HTML
46#undef USE_HTML_HELP
47#define USE_HTML_HELP 0
48#endif
49
f96b60aa 50#if USE_HTML_HELP
7b28757f
JS
51#include <wx/filesys.h>
52#include <wx/fs_zip.h>
53
f6bcfd97 54#if USE_OLD_HTML_HELP
7b28757f 55#include "wx/generic/helpwxht.h"
f6bcfd97
BP
56#endif
57
7b28757f 58#include "wx/html/helpctrl.h"
f96b60aa 59#endif
de5c0ba7 60
f6bcfd97
BP
61#if wxUSE_MS_HTML_HELP
62#include "wx/msw/helpchm.h"
63#endif
64
de5c0ba7
KB
65// ----------------------------------------------------------------------------
66// ressources
67// ----------------------------------------------------------------------------
68// the application icon
55acd85e 69#if defined(__WXGTK__) || defined(__WXMOTIF__)
de5c0ba7
KB
70 #include "mondrian.xpm"
71#endif
72
73// ----------------------------------------------------------------------------
74// private classes
75// ----------------------------------------------------------------------------
76
77// Define a new application type, each program should derive a class from wxApp
78class MyApp : public wxApp
79{
80public:
81 // override base class virtuals
82 // ----------------------------
83
84 // this one is called on application startup and is a good place for the app
85 // initialization (doing it here and not in the ctor allows to have an error
86 // return: if OnInit() returns false, the application terminates)
87 virtual bool OnInit();
88};
89
90// Define a new frame type: this is going to be our main frame
91class MyFrame : public wxFrame
92{
93public:
94 // ctor(s)
95 MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size);
96
e66ad5c6
VZ
97 wxHelpController& GetHelpController() { return m_help; }
98
7c4a59a8 99#if USE_HTML_HELP
f6bcfd97 100#if USE_OLD_HTML_HELP
7c4a59a8 101 wxHelpControllerHtml& GetHtmlHelpController() { return m_htmlHelp; }
f6bcfd97 102#endif
7b28757f 103 wxHtmlHelpController& GetAdvancedHtmlHelpController() { return m_advancedHtmlHelp; }
7c4a59a8 104#endif
f6bcfd97
BP
105#if wxUSE_MS_HTML_HELP
106 wxCHMHelpController& GetMSHtmlHelpController() { return m_msHtmlHelp; }
107#endif
7c4a59a8 108
de5c0ba7
KB
109 // event handlers (these functions should _not_ be virtual)
110 void OnQuit(wxCommandEvent& event);
111 void OnHelp(wxCommandEvent& event);
7c4a59a8 112 void OnHtmlHelp(wxCommandEvent& event);
7b28757f 113 void OnAdvancedHtmlHelp(wxCommandEvent& event);
f6bcfd97 114 void OnMSHtmlHelp(wxCommandEvent& event);
de5c0ba7 115
5da69e38
JS
116 void ShowHelp(int commandId, wxHelpControllerBase& helpController);
117
de5c0ba7 118private:
7c4a59a8
JS
119 wxHelpController m_help;
120
121#if USE_HTML_HELP
f6bcfd97 122#if USE_OLD_HTML_HELP
7c4a59a8 123 wxHelpControllerHtml m_htmlHelp;
f6bcfd97 124#endif
7b28757f 125 wxHtmlHelpController m_advancedHtmlHelp;
7c4a59a8 126#endif
f96b60aa 127
f6bcfd97
BP
128#if wxUSE_MS_HTML_HELP
129 wxCHMHelpController m_msHtmlHelp;
130#endif
131
de5c0ba7
KB
132 // any class wishing to process wxWindows events must use this macro
133 DECLARE_EVENT_TABLE()
134};
135
136// ----------------------------------------------------------------------------
137// constants
138// ----------------------------------------------------------------------------
139
140// IDs for the controls and the menu commands
141enum
142{
143 // menu items
33b64e6f
JS
144 HelpDemo_Quit = 1,
145 HelpDemo_Help_Index,
146 HelpDemo_Help_Classes,
147 HelpDemo_Help_Functions,
148 HelpDemo_Help_Help,
7c4a59a8
JS
149 HelpDemo_Help_Search,
150
151 HelpDemo_Html_Help_Index,
152 HelpDemo_Html_Help_Classes,
153 HelpDemo_Html_Help_Functions,
154 HelpDemo_Html_Help_Help,
155 HelpDemo_Html_Help_Search,
156
7b28757f
JS
157 HelpDemo_Advanced_Html_Help_Index,
158 HelpDemo_Advanced_Html_Help_Classes,
159 HelpDemo_Advanced_Html_Help_Functions,
160 HelpDemo_Advanced_Html_Help_Help,
161 HelpDemo_Advanced_Html_Help_Search,
162
f6bcfd97
BP
163 HelpDemo_MS_Html_Help_Index,
164 HelpDemo_MS_Html_Help_Classes,
165 HelpDemo_MS_Html_Help_Functions,
166 HelpDemo_MS_Html_Help_Help,
167 HelpDemo_MS_Html_Help_Search,
168
33b64e6f
JS
169 HelpDemo_Help_KDE,
170 HelpDemo_Help_GNOME,
171 HelpDemo_Help_Netscape,
de5c0ba7 172 // controls start here (the numbers are, of course, arbitrary)
7b28757f 173 HelpDemo_Text = 1000,
de5c0ba7
KB
174};
175
176// ----------------------------------------------------------------------------
177// event tables and other macros for wxWindows
178// ----------------------------------------------------------------------------
179
180// the event tables connect the wxWindows events with the functions (event
181// handlers) which process them. It can be also done at run-time, but for the
182// simple menu events like this the static method is much simpler.
183BEGIN_EVENT_TABLE(MyFrame, wxFrame)
33b64e6f
JS
184 EVT_MENU(HelpDemo_Quit, MyFrame::OnQuit)
185 EVT_MENU(HelpDemo_Help_Index, MyFrame::OnHelp)
186 EVT_MENU(HelpDemo_Help_Classes, MyFrame::OnHelp)
187 EVT_MENU(HelpDemo_Help_Functions, MyFrame::OnHelp)
188 EVT_MENU(HelpDemo_Help_Help, MyFrame::OnHelp)
7c4a59a8
JS
189 EVT_MENU(HelpDemo_Help_Search, MyFrame::OnHelp)
190
191 EVT_MENU(HelpDemo_Html_Help_Index, MyFrame::OnHtmlHelp)
192 EVT_MENU(HelpDemo_Html_Help_Classes, MyFrame::OnHtmlHelp)
193 EVT_MENU(HelpDemo_Html_Help_Functions, MyFrame::OnHtmlHelp)
194 EVT_MENU(HelpDemo_Html_Help_Help, MyFrame::OnHtmlHelp)
195 EVT_MENU(HelpDemo_Html_Help_Search, MyFrame::OnHtmlHelp)
196
7b28757f
JS
197 EVT_MENU(HelpDemo_Advanced_Html_Help_Index, MyFrame::OnAdvancedHtmlHelp)
198 EVT_MENU(HelpDemo_Advanced_Html_Help_Classes, MyFrame::OnAdvancedHtmlHelp)
199 EVT_MENU(HelpDemo_Advanced_Html_Help_Functions, MyFrame::OnAdvancedHtmlHelp)
200 EVT_MENU(HelpDemo_Advanced_Html_Help_Help, MyFrame::OnAdvancedHtmlHelp)
201 EVT_MENU(HelpDemo_Advanced_Html_Help_Search, MyFrame::OnAdvancedHtmlHelp)
202
f6bcfd97
BP
203 EVT_MENU(HelpDemo_MS_Html_Help_Index, MyFrame::OnMSHtmlHelp)
204 EVT_MENU(HelpDemo_MS_Html_Help_Classes, MyFrame::OnMSHtmlHelp)
205 EVT_MENU(HelpDemo_MS_Html_Help_Functions, MyFrame::OnMSHtmlHelp)
206 EVT_MENU(HelpDemo_MS_Html_Help_Help, MyFrame::OnMSHtmlHelp)
207 EVT_MENU(HelpDemo_MS_Html_Help_Search, MyFrame::OnMSHtmlHelp)
208
33b64e6f
JS
209 EVT_MENU(HelpDemo_Help_KDE, MyFrame::OnHelp)
210 EVT_MENU(HelpDemo_Help_GNOME, MyFrame::OnHelp)
211 EVT_MENU(HelpDemo_Help_Netscape, MyFrame::OnHelp)
de5c0ba7
KB
212END_EVENT_TABLE()
213
214// Create a new application object: this macro will allow wxWindows to create
215// the application object during program execution (it's better than using a
216// static object for many reasons) and also declares the accessor function
217// wxGetApp() which will return the reference of the right type (i.e. MyApp and
218// not wxApp)
219IMPLEMENT_APP(MyApp)
220
221// ============================================================================
222// implementation
223// ============================================================================
224
225// ----------------------------------------------------------------------------
226// the application class
227// ----------------------------------------------------------------------------
228
229// `Main program' equivalent: the program execution "starts" here
230bool MyApp::OnInit()
231{
7c4a59a8
JS
232#if wxUSE_HTML
233#if wxUSE_GIF
234 // Required for images in the online documentation
235 wxImage::AddHandler(new wxGIFHandler);
7b28757f
JS
236
237 // Required for advanced HTML help
238#if wxUSE_STREAMS && wxUSE_ZIPSTREAM && wxUSE_ZLIB
239 wxFileSystem::AddHandler(new wxZipFSHandler);
240#endif
241
7c4a59a8
JS
242#endif
243#endif
244
de5c0ba7 245 // Create the main application window
33b64e6f 246 MyFrame *frame = new MyFrame("HelpDemo wxWindows App",
de5c0ba7
KB
247 wxPoint(50, 50), wxSize(450, 340));
248
de5c0ba7
KB
249 frame->Show(TRUE);
250 SetTopWindow(frame);
251
e66ad5c6
VZ
252 // initialise the help system: this means that we'll use doc.hlp file under
253 // Windows and that the HTML docs are in the subdirectory doc for platforms
254 // using HTML help
255 if ( !frame->GetHelpController().Initialize("doc") )
256 {
257 wxLogError("Cannot initialize the help system, aborting.");
258
259 return FALSE;
260 }
261
7c4a59a8 262#if USE_HTML_HELP
7b28757f 263 // initialise the standard HTML help system: this means that the HTML docs are in the
7c4a59a8 264 // subdirectory doc for platforms using HTML help
f6bcfd97 265#if USE_OLD_HTML_HELP
7c4a59a8
JS
266 if ( !frame->GetHtmlHelpController().Initialize("doc") )
267 {
268 wxLogError("Cannot initialize the HTML help system, aborting.");
269
270 return FALSE;
271 }
f6bcfd97 272#endif
7b28757f
JS
273
274 // initialise the advanced HTML help system: this means that the HTML docs are in .htb
275 // (zipped) form
276 if ( !frame->GetAdvancedHtmlHelpController().Initialize("doc") )
277 {
278 wxLogError("Cannot initialize the advanced HTML help system, aborting.");
279
280 return FALSE;
281 }
7c4a59a8
JS
282#endif
283
f6bcfd97
BP
284#if wxUSE_MS_HTML_HELP
285 if ( !frame->GetMSHtmlHelpController().Initialize("doc") )
286 {
287 wxLogError("Cannot initialize the MS HTML help system, aborting.");
288
289 return FALSE;
290 }
291#endif
292
de5c0ba7
KB
293 return TRUE;
294}
295
296// ----------------------------------------------------------------------------
297// main frame
298// ----------------------------------------------------------------------------
299
300// frame constructor
301MyFrame::MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size)
302 : wxFrame((wxFrame *)NULL, -1, title, pos, size)
303{
304 // set the frame icon
305 SetIcon(wxICON(mondrian));
306
307 // create a menu bar
308 wxMenu *menuFile = new wxMenu;
309
33b64e6f
JS
310 menuFile->Append(HelpDemo_Help_Index, "&Help Index...");
311 menuFile->Append(HelpDemo_Help_Classes, "&Help on Classes...");
312 menuFile->Append(HelpDemo_Help_Functions, "&Help on Functions...");
313 menuFile->Append(HelpDemo_Help_Help, "&About Help Demo...");
33b64e6f 314 menuFile->Append(HelpDemo_Help_Search, "&Search help...");
7c4a59a8 315#if USE_HTML_HELP
f6bcfd97 316#if USE_OLD_HTML_HELP
7c4a59a8
JS
317 menuFile->AppendSeparator();
318 menuFile->Append(HelpDemo_Html_Help_Index, "HTML &Help Index...");
319 menuFile->Append(HelpDemo_Html_Help_Classes, "HTML &Help on Classes...");
320 menuFile->Append(HelpDemo_Html_Help_Functions, "HTML &Help on Functions...");
321 menuFile->Append(HelpDemo_Html_Help_Help, "HTML &About Help Demo...");
7c4a59a8 322 menuFile->Append(HelpDemo_Html_Help_Search, "HTML &Search help...");
f6bcfd97 323#endif
7b28757f
JS
324 menuFile->AppendSeparator();
325 menuFile->Append(HelpDemo_Advanced_Html_Help_Index, "Advanced HTML &Help Index...");
326 menuFile->Append(HelpDemo_Advanced_Html_Help_Classes, "Advanced HTML &Help on Classes...");
327 menuFile->Append(HelpDemo_Advanced_Html_Help_Functions, "Advanced HTML &Help on Functions...");
328 menuFile->Append(HelpDemo_Advanced_Html_Help_Help, "Advanced HTML &About Help Demo...");
329 menuFile->Append(HelpDemo_Advanced_Html_Help_Search, "Advanced HTML &Search help...");
7c4a59a8
JS
330#endif
331
f6bcfd97
BP
332#if wxUSE_MS_HTML_HELP
333 menuFile->AppendSeparator();
334 menuFile->Append(HelpDemo_MS_Html_Help_Index, "MS HTML &Help Index...");
335 menuFile->Append(HelpDemo_MS_Html_Help_Classes, "MS HTML &Help on Classes...");
336 menuFile->Append(HelpDemo_MS_Html_Help_Functions, "MS HTML &Help on Functions...");
337 menuFile->Append(HelpDemo_MS_Html_Help_Help, "MS HTML &About Help Demo...");
338 menuFile->Append(HelpDemo_MS_Html_Help_Search, "MS HTML &Search help...");
339#endif
340
29ea4a29 341#ifndef __WXMSW__
e66ad5c6 342#if !wxUSE_HTML
de5c0ba7 343 menuFile->AppendSeparator();
33b64e6f
JS
344 menuFile->Append(HelpDemo_Help_KDE, "Use &KDE");
345 menuFile->Append(HelpDemo_Help_GNOME, "Use &GNOME");
346 menuFile->Append(HelpDemo_Help_Netscape, "Use &Netscape");
29ea4a29 347#endif
33b64e6f
JS
348#endif
349 menuFile->AppendSeparator();
350 menuFile->Append(HelpDemo_Quit, "E&xit");
de5c0ba7
KB
351
352 // now append the freshly created menu to the menu bar...
353 wxMenuBar *menuBar = new wxMenuBar;
354 menuBar->Append(menuFile, "&File");
355
356 // ... and attach this menu bar to the frame
357 SetMenuBar(menuBar);
358
359 // create a status bar just for fun (by default with 1 pane only)
360 CreateStatusBar();
361 SetStatusText("Welcome to wxWindows!");
362
363 // now create some controls
364
365 // a panel first - if there were several controls, it would allow us to
366 // navigate between them from the keyboard
367 wxPanel *panel = new wxPanel(this, -1, wxPoint(0, 0), wxSize(400, 200));
368
369 // and a static control whose parent is the panel
370 (void)new wxStaticText(panel, -1, "Hello, world!", wxPoint(10, 10));
de5c0ba7
KB
371}
372
373
374// event handlers
375
376void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
377{
378 // TRUE is to force the frame to close
379 Close(TRUE);
380}
381
382void MyFrame::OnHelp(wxCommandEvent& event)
383{
5da69e38
JS
384 ShowHelp(event.GetId(), m_help);
385}
386
387void MyFrame::OnHtmlHelp(wxCommandEvent& event)
388{
f6bcfd97 389#if USE_HTML_HELP && USE_OLD_HTML_HELP
5da69e38
JS
390 ShowHelp(event.GetId(), m_htmlHelp);
391#endif
392}
393
394void MyFrame::OnAdvancedHtmlHelp(wxCommandEvent& event)
395{
396#if USE_HTML_HELP
397 ShowHelp(event.GetId(), m_advancedHtmlHelp);
398#endif
399}
400
f6bcfd97
BP
401void MyFrame::OnMSHtmlHelp(wxCommandEvent& event)
402{
403#if wxUSE_MS_HTML_HELP
404 ShowHelp(event.GetId(), m_msHtmlHelp);
405#endif
406}
407
408/*
409 Notes: ShowHelp uses section ids for displaying particular topics,
410 but you might want to use a unique keyword to display a topic, instead.
411
412 Section ids are specified as follows for the different formats.
413
414 WinHelp
415
416 The [MAP] section specifies the topic to integer id mapping, e.g.
417
418 [MAP]
419 #define intro 100
420 #define functions 1
421 #define classes 2
422 #define about 3
423
424 The identifier name corresponds to the label used for that topic.
425 You could also put these in a .h file and #include it in both the MAP
426 section and your C++ source.
427
428 Note that Tex2RTF doesn't currently generate the MAP section automatically.
429
430 MS HTML Help
431
432 The [MAP] section specifies the HTML filename root to integer id mapping, e.g.
433
434 [MAP]
435 #define doc1 100
436 #define doc3 1
437 #define doc2 2
438 #define doc4 3
439
440 The identifier name corresponds to the HTML filename used for that topic.
441 You could also put these in a .h file and #include it in both the MAP
442 section and your C++ source.
443
444 Note that Tex2RTF doesn't currently generate the MAP section automatically.
445
446 Simple wxHTML Help and External HTML Help
447
448 A wxhelp.map file is used, for example:
449
450 0 wx.htm ; wxWindows: Help index; additional keywords like overview
451 1 wx204.htm ; wxWindows Function Reference
452 2 wx34.htm ; wxWindows Class Reference
453
454 Note that Tex2RTF doesn't currently generate the MAP section automatically.
455
456 Advanced HTML Help
457
458 An extension to the .hhc file format is used, specifying a new parameter
459 with name="ID":
460
461 <OBJECT type="text/sitemap">
462 <param name="Local" value="doc2.htm#classes">
463 <param name="Name" value="Classes">
464 <param name="ID" value=2>
465 </OBJECT>
466
467 Again, this is not generated automatically by Tex2RTF, though it could
468 be added quite easily.
469
470 Unfortunately adding the ID parameters appears to interfere with MS HTML Help,
471 so you should not try to compile a .chm file from a .hhc file with
472 this extension, or the contents will be messed up.
473 */
474
5da69e38
JS
475void MyFrame::ShowHelp(int commandId, wxHelpControllerBase& helpController)
476{
477 switch(commandId)
de5c0ba7 478 {
33b64e6f 479 case HelpDemo_Help_Classes:
5da69e38
JS
480 case HelpDemo_Html_Help_Classes:
481 case HelpDemo_Advanced_Html_Help_Classes:
f6bcfd97 482 case HelpDemo_MS_Html_Help_Classes:
5da69e38 483 helpController.DisplaySection(2);
f6bcfd97 484 //helpController.DisplaySection("Classes"); // An alternative form for most controllers
7c4a59a8 485
7c4a59a8 486 break;
5da69e38 487 case HelpDemo_Help_Functions:
7c4a59a8 488 case HelpDemo_Html_Help_Functions:
5da69e38 489 case HelpDemo_Advanced_Html_Help_Functions:
f6bcfd97 490 case HelpDemo_MS_Html_Help_Functions:
5da69e38 491 helpController.DisplaySection(1);
f6bcfd97 492 //helpController.DisplaySection("Functions"); // An alternative form for most controllers
7c4a59a8 493 break;
5da69e38 494 case HelpDemo_Help_Help:
7c4a59a8 495 case HelpDemo_Html_Help_Help:
5da69e38 496 case HelpDemo_Advanced_Html_Help_Help:
f6bcfd97 497 case HelpDemo_MS_Html_Help_Help:
5da69e38 498 helpController.DisplaySection(3);
f6bcfd97 499 //helpController.DisplaySection("About"); // An alternative form for most controllers
7c4a59a8
JS
500 break;
501
5da69e38 502 case HelpDemo_Help_Search:
7c4a59a8 503 case HelpDemo_Html_Help_Search:
5da69e38 504 case HelpDemo_Advanced_Html_Help_Search:
f6bcfd97 505 case HelpDemo_MS_Html_Help_Search:
7c4a59a8
JS
506 {
507 wxString key = wxGetTextFromUser("Search for?",
508 "Search help for keyword",
509 "",
510 this);
511 if(! key.IsEmpty())
5da69e38 512 helpController.KeywordSearch(key);
7c4a59a8
JS
513 }
514 break;
5da69e38
JS
515
516 case HelpDemo_Help_Index:
7c4a59a8 517 case HelpDemo_Html_Help_Index:
5da69e38 518 case HelpDemo_Advanced_Html_Help_Index:
f6bcfd97 519 case HelpDemo_MS_Html_Help_Index:
5da69e38 520 helpController.DisplayContents();
7c4a59a8 521 break;
7c4a59a8 522
5da69e38 523 // These three calls are only used by wxExtHelpController
7b28757f 524
5da69e38
JS
525 case HelpDemo_Help_KDE:
526 helpController.SetViewer("kdehelp");
7b28757f 527 break;
5da69e38
JS
528 case HelpDemo_Help_GNOME:
529 helpController.SetViewer("gnome-help-browser");
7b28757f 530 break;
5da69e38
JS
531 case HelpDemo_Help_Netscape:
532 helpController.SetViewer("netscape", wxHELP_NETSCAPE);
7b28757f
JS
533 break;
534
7b28757f 535 default:
7b28757f
JS
536 break;
537 }
7b28757f
JS
538}
539