1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxHelpController demo
4 // Author: Karsten Ballueder
8 // Copyright: (c) Karsten Ballueder, Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 // For compilers that support precompilation, includes "wx/wx.h".
21 #include "wx/wxprec.h"
27 // for all others, include the necessary headers (this file is usually all you
28 // need because it includes almost all "standard" wxWindows headers
33 # include "wx/image.h"
35 # include "wx/cshelp.h"
38 # include "wx/tooltip.h"
41 // define this to 1 to use HTML help even under Windows (by default, Windows
42 // version will use WinHelp).
43 // Please also see samples/html/helpview.
45 #define USE_HTML_HELP 1
47 // Use old-style HTML help if 1
48 #define USE_OLD_HTML_HELP 0
52 #define USE_HTML_HELP 0
56 #include <wx/filesys.h>
57 #include <wx/fs_zip.h>
60 #include "wx/generic/helpwxht.h"
63 #include "wx/html/helpctrl.h"
66 #if wxUSE_MS_HTML_HELP
67 #include "wx/msw/helpchm.h"
70 // ----------------------------------------------------------------------------
72 // ----------------------------------------------------------------------------
73 // the application icon
74 #if defined(__WXGTK__) || defined(__WXMOTIF__)
75 #include "mondrian.xpm"
78 // ----------------------------------------------------------------------------
80 // ----------------------------------------------------------------------------
82 // Define a new application type, each program should derive a class from wxApp
83 class MyApp
: public wxApp
86 // override base class virtuals
87 // ----------------------------
89 // this one is called on application startup and is a good place for the app
90 // initialization (doing it here and not in the ctor allows to have an error
91 // return: if OnInit() returns false, the application terminates)
92 virtual bool OnInit();
95 // Define a new frame type: this is going to be our main frame
96 class MyFrame
: public wxFrame
100 MyFrame(const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
);
102 wxHelpController
& GetHelpController() { return m_help
; }
105 #if USE_OLD_HTML_HELP
106 wxHelpControllerHtml
& GetHtmlHelpController() { return m_htmlHelp
; }
108 wxHtmlHelpController
& GetAdvancedHtmlHelpController() { return m_advancedHtmlHelp
; }
110 #if wxUSE_MS_HTML_HELP
111 wxCHMHelpController
& GetMSHtmlHelpController() { return m_msHtmlHelp
; }
114 // event handlers (these functions should _not_ be virtual)
115 void OnQuit(wxCommandEvent
& event
);
116 void OnHelp(wxCommandEvent
& event
);
117 void OnHtmlHelp(wxCommandEvent
& event
);
118 void OnAdvancedHtmlHelp(wxCommandEvent
& event
);
119 void OnMSHtmlHelp(wxCommandEvent
& event
);
121 void OnContextHelp(wxHelpEvent
& event
);
122 void OnShowContextHelp(wxCommandEvent
& event
);
123 void OnShowDialogContextHelp(wxCommandEvent
& event
);
125 void ShowHelp(int commandId
, wxHelpControllerBase
& helpController
);
128 wxHelpController m_help
;
131 #if USE_OLD_HTML_HELP
132 wxHelpControllerHtml m_htmlHelp
;
134 wxHtmlHelpController m_advancedHtmlHelp
;
137 #if wxUSE_MS_HTML_HELP
138 wxCHMHelpController m_msHtmlHelp
;
141 // any class wishing to process wxWindows events must use this macro
142 DECLARE_EVENT_TABLE()
145 // A custom modal dialog
146 class MyModalDialog
: public wxDialog
149 MyModalDialog(wxWindow
*parent
);
151 void OnContextHelp(wxHelpEvent
& event
);
155 DECLARE_EVENT_TABLE()
158 // ----------------------------------------------------------------------------
160 // ----------------------------------------------------------------------------
162 // IDs for the controls and the menu commands
168 HelpDemo_Help_Classes
,
169 HelpDemo_Help_Functions
,
171 HelpDemo_Help_Search
,
172 HelpDemo_Help_ContextHelp
,
173 HelpDemo_Help_DialogContextHelp
,
175 HelpDemo_Html_Help_Index
,
176 HelpDemo_Html_Help_Classes
,
177 HelpDemo_Html_Help_Functions
,
178 HelpDemo_Html_Help_Help
,
179 HelpDemo_Html_Help_Search
,
181 HelpDemo_Advanced_Html_Help_Index
,
182 HelpDemo_Advanced_Html_Help_Classes
,
183 HelpDemo_Advanced_Html_Help_Functions
,
184 HelpDemo_Advanced_Html_Help_Help
,
185 HelpDemo_Advanced_Html_Help_Search
,
187 HelpDemo_MS_Html_Help_Index
,
188 HelpDemo_MS_Html_Help_Classes
,
189 HelpDemo_MS_Html_Help_Functions
,
190 HelpDemo_MS_Html_Help_Help
,
191 HelpDemo_MS_Html_Help_Search
,
195 HelpDemo_Help_Netscape
,
196 // controls start here (the numbers are, of course, arbitrary)
197 HelpDemo_Text
= 1000,
200 // ----------------------------------------------------------------------------
201 // event tables and other macros for wxWindows
202 // ----------------------------------------------------------------------------
204 // the event tables connect the wxWindows events with the functions (event
205 // handlers) which process them. It can be also done at run-time, but for the
206 // simple menu events like this the static method is much simpler.
207 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
208 EVT_MENU(HelpDemo_Quit
, MyFrame
::OnQuit
)
209 EVT_MENU(HelpDemo_Help_Index
, MyFrame
::OnHelp
)
210 EVT_MENU(HelpDemo_Help_Classes
, MyFrame
::OnHelp
)
211 EVT_MENU(HelpDemo_Help_Functions
, MyFrame
::OnHelp
)
212 EVT_MENU(HelpDemo_Help_Help
, MyFrame
::OnHelp
)
213 EVT_MENU(HelpDemo_Help_Search
, MyFrame
::OnHelp
)
214 EVT_MENU(HelpDemo_Help_ContextHelp
, MyFrame
::OnShowContextHelp
)
215 EVT_MENU(HelpDemo_Help_DialogContextHelp
, MyFrame
::OnShowDialogContextHelp
)
217 EVT_HELP(-1, MyFrame
::OnContextHelp
)
219 EVT_MENU(HelpDemo_Html_Help_Index
, MyFrame
::OnHtmlHelp
)
220 EVT_MENU(HelpDemo_Html_Help_Classes
, MyFrame
::OnHtmlHelp
)
221 EVT_MENU(HelpDemo_Html_Help_Functions
, MyFrame
::OnHtmlHelp
)
222 EVT_MENU(HelpDemo_Html_Help_Help
, MyFrame
::OnHtmlHelp
)
223 EVT_MENU(HelpDemo_Html_Help_Search
, MyFrame
::OnHtmlHelp
)
225 EVT_MENU(HelpDemo_Advanced_Html_Help_Index
, MyFrame
::OnAdvancedHtmlHelp
)
226 EVT_MENU(HelpDemo_Advanced_Html_Help_Classes
, MyFrame
::OnAdvancedHtmlHelp
)
227 EVT_MENU(HelpDemo_Advanced_Html_Help_Functions
, MyFrame
::OnAdvancedHtmlHelp
)
228 EVT_MENU(HelpDemo_Advanced_Html_Help_Help
, MyFrame
::OnAdvancedHtmlHelp
)
229 EVT_MENU(HelpDemo_Advanced_Html_Help_Search
, MyFrame
::OnAdvancedHtmlHelp
)
231 EVT_MENU(HelpDemo_MS_Html_Help_Index
, MyFrame
::OnMSHtmlHelp
)
232 EVT_MENU(HelpDemo_MS_Html_Help_Classes
, MyFrame
::OnMSHtmlHelp
)
233 EVT_MENU(HelpDemo_MS_Html_Help_Functions
, MyFrame
::OnMSHtmlHelp
)
234 EVT_MENU(HelpDemo_MS_Html_Help_Help
, MyFrame
::OnMSHtmlHelp
)
235 EVT_MENU(HelpDemo_MS_Html_Help_Search
, MyFrame
::OnMSHtmlHelp
)
237 EVT_MENU(HelpDemo_Help_KDE
, MyFrame
::OnHelp
)
238 EVT_MENU(HelpDemo_Help_GNOME
, MyFrame
::OnHelp
)
239 EVT_MENU(HelpDemo_Help_Netscape
, MyFrame
::OnHelp
)
242 // Create a new application object: this macro will allow wxWindows to create
243 // the application object during program execution (it's better than using a
244 // static object for many reasons) and also declares the accessor function
245 // wxGetApp() which will return the reference of the right type (i.e. MyApp and
249 // ============================================================================
251 // ============================================================================
253 // ----------------------------------------------------------------------------
254 // the application class
255 // ----------------------------------------------------------------------------
257 // `Main program' equivalent: the program execution "starts" here
262 // Required for images in the online documentation
263 wxImage
::AddHandler(new wxGIFHandler
);
265 // Required for advanced HTML help
266 #if wxUSE_STREAMS && wxUSE_ZIPSTREAM && wxUSE_ZLIB
267 wxFileSystem
::AddHandler(new wxZipFSHandler
);
273 // Create the main application window
274 MyFrame
*frame
= new MyFrame("HelpDemo wxWindows App",
275 wxPoint(50, 50), wxSize(450, 340));
280 // initialise the help system: this means that we'll use doc.hlp file under
281 // Windows and that the HTML docs are in the subdirectory doc for platforms
283 if ( !frame
->GetHelpController().Initialize("doc") )
285 wxLogError("Cannot initialize the help system, aborting.");
291 // initialise the standard HTML help system: this means that the HTML docs are in the
292 // subdirectory doc for platforms using HTML help
293 #if USE_OLD_HTML_HELP
294 if ( !frame
->GetHtmlHelpController().Initialize("doc") )
296 wxLogError("Cannot initialize the HTML help system, aborting.");
302 // initialise the advanced HTML help system: this means that the HTML docs are in .htb
304 if ( !frame
->GetAdvancedHtmlHelpController().Initialize("doc") )
306 wxLogError("Cannot initialize the advanced HTML help system, aborting.");
312 #if wxUSE_MS_HTML_HELP
313 if ( !frame
->GetMSHtmlHelpController().Initialize("doc") )
315 wxLogError("Cannot initialize the MS HTML help system, aborting.");
324 // ----------------------------------------------------------------------------
326 // ----------------------------------------------------------------------------
329 MyFrame
::MyFrame(const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
)
330 : wxFrame((wxFrame
*)NULL
, -1, title
, pos
, size
)
332 // set the frame icon
333 SetIcon(wxICON(mondrian
));
336 wxMenu
*menuFile
= new wxMenu
;
338 menuFile
->Append(HelpDemo_Help_Index
, "&Help Index...");
339 menuFile
->Append(HelpDemo_Help_Classes
, "&Help on Classes...");
340 menuFile
->Append(HelpDemo_Help_Functions
, "&Help on Functions...");
341 menuFile
->Append(HelpDemo_Help_ContextHelp
, "&Context Help...");
342 menuFile
->Append(HelpDemo_Help_DialogContextHelp
, "&Dialog Context Help...");
343 menuFile
->Append(HelpDemo_Help_Help
, "&About Help Demo...");
344 menuFile
->Append(HelpDemo_Help_Search
, "&Search help...");
346 #if USE_OLD_HTML_HELP
347 menuFile
->AppendSeparator();
348 menuFile
->Append(HelpDemo_Html_Help_Index
, "HTML &Help Index...");
349 menuFile
->Append(HelpDemo_Html_Help_Classes
, "HTML &Help on Classes...");
350 menuFile
->Append(HelpDemo_Html_Help_Functions
, "HTML &Help on Functions...");
351 menuFile
->Append(HelpDemo_Html_Help_Help
, "HTML &About Help Demo...");
352 menuFile
->Append(HelpDemo_Html_Help_Search
, "HTML &Search help...");
354 menuFile
->AppendSeparator();
355 menuFile
->Append(HelpDemo_Advanced_Html_Help_Index
, "Advanced HTML &Help Index...");
356 menuFile
->Append(HelpDemo_Advanced_Html_Help_Classes
, "Advanced HTML &Help on Classes...");
357 menuFile
->Append(HelpDemo_Advanced_Html_Help_Functions
, "Advanced HTML &Help on Functions...");
358 menuFile
->Append(HelpDemo_Advanced_Html_Help_Help
, "Advanced HTML &About Help Demo...");
359 menuFile
->Append(HelpDemo_Advanced_Html_Help_Search
, "Advanced HTML &Search help...");
362 #if wxUSE_MS_HTML_HELP
363 menuFile
->AppendSeparator();
364 menuFile
->Append(HelpDemo_MS_Html_Help_Index
, "MS HTML &Help Index...");
365 menuFile
->Append(HelpDemo_MS_Html_Help_Classes
, "MS HTML &Help on Classes...");
366 menuFile
->Append(HelpDemo_MS_Html_Help_Functions
, "MS HTML &Help on Functions...");
367 menuFile
->Append(HelpDemo_MS_Html_Help_Help
, "MS HTML &About Help Demo...");
368 menuFile
->Append(HelpDemo_MS_Html_Help_Search
, "MS HTML &Search help...");
373 menuFile
->AppendSeparator();
374 menuFile
->Append(HelpDemo_Help_KDE
, "Use &KDE");
375 menuFile
->Append(HelpDemo_Help_GNOME
, "Use &GNOME");
376 menuFile
->Append(HelpDemo_Help_Netscape
, "Use &Netscape");
379 menuFile
->AppendSeparator();
380 menuFile
->Append(HelpDemo_Quit
, "E&xit");
382 // now append the freshly created menu to the menu bar...
383 wxMenuBar
*menuBar
= new wxMenuBar
;
384 menuBar
->Append(menuFile
, "&File");
386 // ... and attach this menu bar to the frame
389 // create a status bar just for fun (by default with 1 pane only)
391 SetStatusText("Welcome to wxWindows!");
393 // now create some controls
395 // a panel first - if there were several controls, it would allow us to
396 // navigate between them from the keyboard
397 wxPanel
*panel
= new wxPanel(this, -1, wxPoint(0, 0), wxSize(400, 200));
399 // and a static control whose parent is the panel
400 (void)new wxStaticText(panel
, -1, "Hello, world!", wxPoint(10, 10));
406 void MyFrame
::OnQuit(wxCommandEvent
& WXUNUSED(event
))
408 // TRUE is to force the frame to close
412 void MyFrame
::OnHelp(wxCommandEvent
& event
)
414 ShowHelp(event
.GetId(), m_help
);
417 void MyFrame
::OnShowContextHelp(wxCommandEvent
& event
)
419 // This starts context help mode, then the user
420 // clicks on a window to send a help message
421 wxContextHelp
contextHelp(this);
424 void MyFrame
::OnShowDialogContextHelp(wxCommandEvent
& event
)
426 MyModalDialog
dialog(this);
430 void MyFrame
::OnContextHelp(wxHelpEvent
& event
)
432 // In a real app, if we didn't recognise this ID, we should call event.Skip()
434 msg
.Printf(wxT("We should now display help for window %d"), event
.GetId());
438 void MyFrame
::OnHtmlHelp(wxCommandEvent
& event
)
440 #if USE_HTML_HELP && USE_OLD_HTML_HELP
441 ShowHelp(event
.GetId(), m_htmlHelp
);
445 void MyFrame
::OnAdvancedHtmlHelp(wxCommandEvent
& event
)
448 ShowHelp(event
.GetId(), m_advancedHtmlHelp
);
452 void MyFrame
::OnMSHtmlHelp(wxCommandEvent
& event
)
454 #if wxUSE_MS_HTML_HELP
455 ShowHelp(event
.GetId(), m_msHtmlHelp
);
460 Notes: ShowHelp uses section ids for displaying particular topics,
461 but you might want to use a unique keyword to display a topic, instead.
463 Section ids are specified as follows for the different formats.
467 The [MAP] section specifies the topic to integer id mapping, e.g.
475 The identifier name corresponds to the label used for that topic.
476 You could also put these in a .h file and #include it in both the MAP
477 section and your C++ source.
479 Note that Tex2RTF doesn't currently generate the MAP section automatically.
483 The [MAP] section specifies the HTML filename root to integer id mapping, e.g.
491 The identifier name corresponds to the HTML filename used for that topic.
492 You could also put these in a .h file and #include it in both the MAP
493 section and your C++ source.
495 Note that Tex2RTF doesn't currently generate the MAP section automatically.
497 Simple wxHTML Help and External HTML Help
499 A wxhelp.map file is used, for example:
501 0 wx.htm ; wxWindows: Help index; additional keywords like overview
502 1 wx204.htm ; wxWindows Function Reference
503 2 wx34.htm ; wxWindows Class Reference
505 Note that Tex2RTF doesn't currently generate the MAP section automatically.
509 An extension to the .hhc file format is used, specifying a new parameter
512 <OBJECT type="text/sitemap">
513 <param name="Local" value="doc2.htm#classes">
514 <param name="Name" value="Classes">
515 <param name="ID" value=2>
518 Again, this is not generated automatically by Tex2RTF, though it could
519 be added quite easily.
521 Unfortunately adding the ID parameters appears to interfere with MS HTML Help,
522 so you should not try to compile a .chm file from a .hhc file with
523 this extension, or the contents will be messed up.
526 void MyFrame
::ShowHelp(int commandId
, wxHelpControllerBase
& helpController
)
530 case HelpDemo_Help_Classes
:
531 case HelpDemo_Html_Help_Classes
:
532 case HelpDemo_Advanced_Html_Help_Classes
:
533 case HelpDemo_MS_Html_Help_Classes
:
534 helpController
.DisplaySection(2);
535 //helpController.DisplaySection("Classes"); // An alternative form for most controllers
538 case HelpDemo_Help_Functions
:
539 case HelpDemo_Html_Help_Functions
:
540 case HelpDemo_Advanced_Html_Help_Functions
:
541 case HelpDemo_MS_Html_Help_Functions
:
542 helpController
.DisplaySection(1);
543 //helpController.DisplaySection("Functions"); // An alternative form for most controllers
545 case HelpDemo_Help_Help
:
546 case HelpDemo_Html_Help_Help
:
547 case HelpDemo_Advanced_Html_Help_Help
:
548 case HelpDemo_MS_Html_Help_Help
:
549 helpController
.DisplaySection(3);
550 //helpController.DisplaySection("About"); // An alternative form for most controllers
553 case HelpDemo_Help_Search
:
554 case HelpDemo_Html_Help_Search
:
555 case HelpDemo_Advanced_Html_Help_Search
:
556 case HelpDemo_MS_Html_Help_Search
:
558 wxString key
= wxGetTextFromUser("Search for?",
559 "Search help for keyword",
563 helpController
.KeywordSearch(key
);
567 case HelpDemo_Help_Index
:
568 case HelpDemo_Html_Help_Index
:
569 case HelpDemo_Advanced_Html_Help_Index
:
570 case HelpDemo_MS_Html_Help_Index
:
571 helpController
.DisplayContents();
574 // These three calls are only used by wxExtHelpController
576 case HelpDemo_Help_KDE
:
577 helpController
.SetViewer("kdehelp");
579 case HelpDemo_Help_GNOME
:
580 helpController
.SetViewer("gnome-help-browser");
582 case HelpDemo_Help_Netscape
:
583 helpController
.SetViewer("netscape", wxHELP_NETSCAPE
);
590 // ----------------------------------------------------------------------------
592 // Demonstrates context-sensitive help
593 // ----------------------------------------------------------------------------
595 BEGIN_EVENT_TABLE(MyModalDialog
, wxDialog
)
596 EVT_HELP(-1, MyModalDialog
::OnContextHelp
)
599 MyModalDialog
::MyModalDialog(wxWindow
*parent
)
602 // Add the context-sensitive help button on the caption, MSW only
603 SetExtraStyle(wxDIALOG_EX_CONTEXTHELP
);
605 wxDialog
::Create(parent
, -1, wxString("Modal dialog"));
607 wxBoxSizer
*sizerTop
= new wxBoxSizer(wxVERTICAL
);
608 wxBoxSizer
*sizerRow
= new wxBoxSizer(wxHORIZONTAL
);
610 wxButton
* btnOK
= new wxButton(this, wxID_OK
, "&OK");
611 wxButton
* btnCancel
= new wxButton(this, wxID_CANCEL
, "&Cancel");
612 sizerRow
->Add(btnOK
, 0, wxALIGN_CENTER
| wxALL
, 5);
613 sizerRow
->Add(btnCancel
, 0, wxALIGN_CENTER
| wxALL
, 5);
615 // Add the explicit context-sensitive help button, non-MSW
617 sizerRow
->Add(new wxContextHelpButton(this), 0, wxALIGN_CENTER
| wxALL
, 5);
620 sizerTop
->Add(new wxTextCtrl(this, wxID_APPLY
, wxT("A demo text control"), wxDefaultPosition
, wxSize(300, 100), wxTE_MULTILINE
),
621 0, wxEXPAND
|wxALIGN_CENTER_VERTICAL
|wxALL
, 5 );
622 sizerTop
->Add(sizerRow
, 0, wxALIGN_RIGHT
|wxALIGN_CENTER_VERTICAL
|wxALL
, 5 );
627 sizerTop
->SetSizeHints(this);
634 void MyModalDialog
::OnContextHelp(wxHelpEvent
& event
)
637 switch (event
.GetId())
641 msg
= _("The OK button confirms the dialog choices.");
646 msg
= _("The Cancel button cancels the dialog.");
651 msg
= _("This is a text control that does nothing in particular.");
654 case wxID_CONTEXT_HELP
:
656 msg
= _("If you didn't know what this button is for, why did you press it? :-)");
661 wxMessageBox(msg
, _("Help"), wxICON_INFORMATION
, this);