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 // Define this to 0 to use the help controller as the help
48 // provider, or to 1 to use the 'simple help provider'
49 // (the one implemented with wxTipWindow).
50 #define USE_SIMPLE_HELP_PROVIDER 0
54 #define USE_HTML_HELP 0
58 #include "wx/filesys.h"
59 #include "wx/fs_zip.h"
61 #include "wx/html/helpctrl.h"
64 #if wxUSE_MS_HTML_HELP
65 #include "wx/msw/helpchm.h"
68 #if wxUSE_MS_HTML_HELP && wxUSE_WXHTML_HELP
69 #include "wx/msw/helpbest.h"
72 // ----------------------------------------------------------------------------
74 // ----------------------------------------------------------------------------
75 // the application icon
76 #if defined(__WXGTK__) || defined(__WXX11__) || defined(__WXMOTIF__) || defined(__WXMAC__) || defined(__WXMGL__)
77 #include "mondrian.xpm"
80 // ----------------------------------------------------------------------------
82 // ----------------------------------------------------------------------------
84 // Define a new application type, each program should derive a class from wxApp
85 class MyApp
: public wxApp
88 // override base class virtuals
89 // ----------------------------
91 // this one is called on application startup and is a good place for the app
92 // initialization (doing it here and not in the ctor allows to have an error
93 // return: if OnInit() returns false, the application terminates)
94 virtual bool OnInit();
96 // do some clean up here
100 // Define a new frame type: this is going to be our main frame
101 class MyFrame
: public wxFrame
105 MyFrame(const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
);
107 wxHelpController
& GetHelpController() { return m_help
; }
110 wxHtmlHelpController
& GetAdvancedHtmlHelpController() { return m_advancedHtmlHelp
; }
112 #if wxUSE_MS_HTML_HELP
113 wxCHMHelpController
& GetMSHtmlHelpController() { return m_msHtmlHelp
; }
115 #if wxUSE_MS_HTML_HELP && wxUSE_WXHTML_HELP
116 wxBestHelpController
& GetBestHelpController() { return m_bestHelp
; }
119 // event handlers (these functions should _not_ be virtual)
120 void OnQuit(wxCommandEvent
& event
);
121 void OnHelp(wxCommandEvent
& event
);
122 void OnAdvancedHtmlHelp(wxCommandEvent
& event
);
123 void OnMSHtmlHelp(wxCommandEvent
& event
);
124 void OnBestHelp(wxCommandEvent
& event
);
126 void OnShowContextHelp(wxCommandEvent
& event
);
127 void OnShowDialogContextHelp(wxCommandEvent
& event
);
129 void ShowHelp(int commandId
, wxHelpControllerBase
& helpController
);
132 wxHelpController m_help
;
135 wxHtmlHelpController m_advancedHtmlHelp
;
138 #if wxUSE_MS_HTML_HELP
139 wxCHMHelpController m_msHtmlHelp
;
142 #if wxUSE_MS_HTML_HELP && wxUSE_WXHTML_HELP
143 wxBestHelpController m_bestHelp
;
146 // any class wishing to process wxWindows events must use this macro
147 DECLARE_EVENT_TABLE()
150 // A custom modal dialog
151 class MyModalDialog
: public wxDialog
154 MyModalDialog(wxWindow
*parent
);
158 DECLARE_EVENT_TABLE()
161 // ----------------------------------------------------------------------------
163 // ----------------------------------------------------------------------------
165 // IDs for the controls and the menu commands
171 HelpDemo_Help_Classes
,
172 HelpDemo_Help_Functions
,
174 HelpDemo_Help_Search
,
175 HelpDemo_Help_ContextHelp
,
176 HelpDemo_Help_DialogContextHelp
,
178 HelpDemo_Html_Help_Index
,
179 HelpDemo_Html_Help_Classes
,
180 HelpDemo_Html_Help_Functions
,
181 HelpDemo_Html_Help_Help
,
182 HelpDemo_Html_Help_Search
,
184 HelpDemo_Advanced_Html_Help_Index
,
185 HelpDemo_Advanced_Html_Help_Classes
,
186 HelpDemo_Advanced_Html_Help_Functions
,
187 HelpDemo_Advanced_Html_Help_Help
,
188 HelpDemo_Advanced_Html_Help_Search
,
190 HelpDemo_MS_Html_Help_Index
,
191 HelpDemo_MS_Html_Help_Classes
,
192 HelpDemo_MS_Html_Help_Functions
,
193 HelpDemo_MS_Html_Help_Help
,
194 HelpDemo_MS_Html_Help_Search
,
196 HelpDemo_Best_Help_Index
,
197 HelpDemo_Best_Help_Classes
,
198 HelpDemo_Best_Help_Functions
,
199 HelpDemo_Best_Help_Help
,
200 HelpDemo_Best_Help_Search
,
204 HelpDemo_Help_Netscape
,
205 // controls start here (the numbers are, of course, arbitrary)
206 HelpDemo_Text
= 1000,
209 // ----------------------------------------------------------------------------
210 // event tables and other macros for wxWindows
211 // ----------------------------------------------------------------------------
213 // the event tables connect the wxWindows events with the functions (event
214 // handlers) which process them. It can be also done at run-time, but for the
215 // simple menu events like this the static method is much simpler.
216 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
217 EVT_MENU(HelpDemo_Quit
, MyFrame::OnQuit
)
218 EVT_MENU(HelpDemo_Help_Index
, MyFrame::OnHelp
)
219 EVT_MENU(HelpDemo_Help_Classes
, MyFrame::OnHelp
)
220 EVT_MENU(HelpDemo_Help_Functions
, MyFrame::OnHelp
)
221 EVT_MENU(HelpDemo_Help_Help
, MyFrame::OnHelp
)
222 EVT_MENU(HelpDemo_Help_Search
, MyFrame::OnHelp
)
223 EVT_MENU(HelpDemo_Help_ContextHelp
, MyFrame::OnShowContextHelp
)
224 EVT_MENU(HelpDemo_Help_DialogContextHelp
, MyFrame::OnShowDialogContextHelp
)
226 EVT_MENU(HelpDemo_Advanced_Html_Help_Index
, MyFrame::OnAdvancedHtmlHelp
)
227 EVT_MENU(HelpDemo_Advanced_Html_Help_Classes
, MyFrame::OnAdvancedHtmlHelp
)
228 EVT_MENU(HelpDemo_Advanced_Html_Help_Functions
, MyFrame::OnAdvancedHtmlHelp
)
229 EVT_MENU(HelpDemo_Advanced_Html_Help_Help
, MyFrame::OnAdvancedHtmlHelp
)
230 EVT_MENU(HelpDemo_Advanced_Html_Help_Search
, MyFrame::OnAdvancedHtmlHelp
)
232 EVT_MENU(HelpDemo_MS_Html_Help_Index
, MyFrame::OnMSHtmlHelp
)
233 EVT_MENU(HelpDemo_MS_Html_Help_Classes
, MyFrame::OnMSHtmlHelp
)
234 EVT_MENU(HelpDemo_MS_Html_Help_Functions
, MyFrame::OnMSHtmlHelp
)
235 EVT_MENU(HelpDemo_MS_Html_Help_Help
, MyFrame::OnMSHtmlHelp
)
236 EVT_MENU(HelpDemo_MS_Html_Help_Search
, MyFrame::OnMSHtmlHelp
)
238 EVT_MENU(HelpDemo_Best_Help_Index
, MyFrame::OnBestHelp
)
240 EVT_MENU(HelpDemo_Help_KDE
, MyFrame::OnHelp
)
241 EVT_MENU(HelpDemo_Help_GNOME
, MyFrame::OnHelp
)
242 EVT_MENU(HelpDemo_Help_Netscape
, MyFrame::OnHelp
)
245 // Create a new application object: this macro will allow wxWindows to create
246 // the application object during program execution (it's better than using a
247 // static object for many reasons) and also declares the accessor function
248 // wxGetApp() which will return the reference of the right type (i.e. MyApp and
252 // ============================================================================
254 // ============================================================================
256 // ----------------------------------------------------------------------------
257 // the application class
258 // ----------------------------------------------------------------------------
260 // `Main program' equivalent: the program execution "starts" here
263 // Create a simple help provider to make SetHelpText() do something.
264 // Note that this must be set before any SetHelpText() calls are made.
265 #if USE_SIMPLE_HELP_PROVIDER
266 wxSimpleHelpProvider
* provider
= new wxSimpleHelpProvider
;
268 wxHelpControllerHelpProvider
* provider
= new wxHelpControllerHelpProvider
;
270 wxHelpProvider::Set(provider
);
274 // Required for images in the online documentation
275 wxImage::AddHandler(new wxGIFHandler
);
277 // Required for advanced HTML help
278 #if wxUSE_STREAMS && wxUSE_ZIPSTREAM && wxUSE_ZLIB
279 wxFileSystem::AddHandler(new wxZipFSHandler
);
285 // Create the main application window
286 MyFrame
*frame
= new MyFrame("HelpDemo wxWindows App",
287 wxPoint(50, 50), wxSize(450, 340));
289 #if !USE_SIMPLE_HELP_PROVIDER
290 #if wxUSE_MS_HTML_HELP
291 provider
->SetHelpController(& frame
->GetMSHtmlHelpController());
293 provider
->SetHelpController(& frame
->GetHelpController());
300 // initialise the help system: this means that we'll use doc.hlp file under
301 // Windows and that the HTML docs are in the subdirectory doc for platforms
303 if ( !frame
->GetHelpController().Initialize("doc") )
305 wxLogError(wxT("Cannot initialize the help system, aborting."));
310 #if wxUSE_MS_HTML_HELP
311 if( !frame
->GetMSHtmlHelpController().Initialize("doc") )
313 wxLogError(wxT("Cannot initialize the MS HTML Help system."));
317 #if wxUSE_MS_HTML_HELP && wxUSE_WXHTML_HELP
318 // you need to call Initialize in order to use wxBestHelpController
319 if( !frame
->GetBestHelpController().Initialize("doc") )
321 wxLogError(wxT("Cannot initialize the best help system, aborting."));
326 // initialise the advanced HTML help system: this means that the HTML docs are in .htb
328 if ( !frame
->GetAdvancedHtmlHelpController().Initialize("doc") )
330 wxLogError(wxT("Cannot initialize the advanced HTML help system, aborting."));
337 // defined(__WXMSW__) && wxUSE_MS_HTML_HELP
338 wxString
path(wxGetCwd());
339 if ( !frame
->GetMSHtmlHelpController().Initialize(path
+ "\\doc.chm") )
341 wxLogError("Cannot initialize the MS HTML help system, aborting.");
353 delete wxHelpProvider::Set(NULL
);
358 // ----------------------------------------------------------------------------
360 // ----------------------------------------------------------------------------
363 MyFrame::MyFrame(const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
)
364 : wxFrame((wxFrame
*)NULL
, 300, title
, pos
, size
)
366 // set the frame icon
367 SetIcon(wxICON(mondrian
));
370 wxMenu
*menuFile
= new wxMenu
;
372 menuFile
->Append(HelpDemo_Help_Index
, "&Help Index...");
373 menuFile
->Append(HelpDemo_Help_Classes
, "&Help on Classes...");
374 menuFile
->Append(HelpDemo_Help_Functions
, "&Help on Functions...");
375 menuFile
->Append(HelpDemo_Help_ContextHelp
, "&Context Help...");
376 menuFile
->Append(HelpDemo_Help_DialogContextHelp
, "&Dialog Context Help...\tCtrl-H");
377 menuFile
->Append(HelpDemo_Help_Help
, "&About Help Demo...");
378 menuFile
->Append(HelpDemo_Help_Search
, "&Search help...");
380 menuFile
->AppendSeparator();
381 menuFile
->Append(HelpDemo_Advanced_Html_Help_Index
, "Advanced HTML &Help Index...");
382 menuFile
->Append(HelpDemo_Advanced_Html_Help_Classes
, "Advanced HTML &Help on Classes...");
383 menuFile
->Append(HelpDemo_Advanced_Html_Help_Functions
, "Advanced HTML &Help on Functions...");
384 menuFile
->Append(HelpDemo_Advanced_Html_Help_Help
, "Advanced HTML &About Help Demo...");
385 menuFile
->Append(HelpDemo_Advanced_Html_Help_Search
, "Advanced HTML &Search help...");
388 #if wxUSE_MS_HTML_HELP
389 menuFile
->AppendSeparator();
390 menuFile
->Append(HelpDemo_MS_Html_Help_Index
, "MS HTML &Help Index...");
391 menuFile
->Append(HelpDemo_MS_Html_Help_Classes
, "MS HTML &Help on Classes...");
392 menuFile
->Append(HelpDemo_MS_Html_Help_Functions
, "MS HTML &Help on Functions...");
393 menuFile
->Append(HelpDemo_MS_Html_Help_Help
, "MS HTML &About Help Demo...");
394 menuFile
->Append(HelpDemo_MS_Html_Help_Search
, "MS HTML &Search help...");
397 #if wxUSE_MS_HTML_HELP && wxUSE_WXHTML_HELP
398 menuFile
->AppendSeparator();
399 menuFile
->Append(HelpDemo_Best_Help_Index
, "Best &Help Index...");
404 menuFile
->AppendSeparator();
405 menuFile
->Append(HelpDemo_Help_KDE
, "Use &KDE");
406 menuFile
->Append(HelpDemo_Help_GNOME
, "Use &GNOME");
407 menuFile
->Append(HelpDemo_Help_Netscape
, "Use &Netscape");
410 menuFile
->AppendSeparator();
411 menuFile
->Append(HelpDemo_Quit
, "E&xit");
413 // now append the freshly created menu to the menu bar...
414 wxMenuBar
*menuBar
= new wxMenuBar
;
415 menuBar
->Append(menuFile
, "&File");
417 // ... and attach this menu bar to the frame
420 // create a status bar just for fun (by default with 1 pane only)
422 SetStatusText("Welcome to wxWindows!");
424 // now create some controls
426 // a panel first - if there were several controls, it would allow us to
427 // navigate between them from the keyboard
428 wxPanel
*panel
= new wxPanel(this, 301, wxPoint(0, 0), wxSize(400, 200));
429 panel
->SetHelpText(_("This panel just holds a static text control."));
430 //panel->SetHelpText(wxContextId(300));
432 // and a static control whose parent is the panel
433 wxStaticText
* staticText
= new wxStaticText(panel
, 302, "Hello, world!", wxPoint(10, 10));
434 staticText
->SetHelpText(_("This static text control isn't doing a lot right now."));
440 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
442 // TRUE is to force the frame to close
446 void MyFrame::OnHelp(wxCommandEvent
& event
)
448 ShowHelp(event
.GetId(), m_help
);
451 void MyFrame::OnShowContextHelp(wxCommandEvent
& event
)
453 // This starts context help mode, then the user
454 // clicks on a window to send a help message
455 wxContextHelp
contextHelp(this);
458 void MyFrame::OnShowDialogContextHelp(wxCommandEvent
& event
)
460 MyModalDialog
dialog(this);
464 void MyFrame::OnAdvancedHtmlHelp(wxCommandEvent
& event
)
467 ShowHelp(event
.GetId(), m_advancedHtmlHelp
);
471 void MyFrame::OnMSHtmlHelp(wxCommandEvent
& event
)
473 #if wxUSE_MS_HTML_HELP
474 ShowHelp(event
.GetId(), m_msHtmlHelp
);
478 void MyFrame::OnBestHelp(wxCommandEvent
& event
)
480 #if wxUSE_MS_HTML_HELP && wxUSE_WXHTML_HELP
481 ShowHelp(event
.GetId(), m_bestHelp
);
486 Notes: ShowHelp uses section ids for displaying particular topics,
487 but you might want to use a unique keyword to display a topic, instead.
489 Section ids are specified as follows for the different formats.
493 The [MAP] section specifies the topic to integer id mapping, e.g.
501 The identifier name corresponds to the label used for that topic.
502 You could also put these in a .h file and #include it in both the MAP
503 section and your C++ source.
505 Note that Tex2RTF doesn't currently generate the MAP section automatically.
509 The [MAP] section specifies the HTML filename root to integer id mapping, e.g.
517 The identifier name corresponds to the HTML filename used for that topic.
518 You could also put these in a .h file and #include it in both the MAP
519 section and your C++ source.
521 Note that Tex2RTF doesn't currently generate the MAP section automatically.
523 Simple wxHTML Help and External HTML Help
525 A wxhelp.map file is used, for example:
527 0 wx.htm ; wxWindows: Help index; additional keywords like overview
528 1 wx204.htm ; wxWindows Function Reference
529 2 wx34.htm ; wxWindows Class Reference
531 Note that Tex2RTF doesn't currently generate the MAP section automatically.
535 An extension to the .hhc file format is used, specifying a new parameter
538 <OBJECT type="text/sitemap">
539 <param name="Local" value="doc2.htm#classes">
540 <param name="Name" value="Classes">
541 <param name="ID" value=2>
544 Again, this is not generated automatically by Tex2RTF, though it could
545 be added quite easily.
547 Unfortunately adding the ID parameters appears to interfere with MS HTML Help,
548 so you should not try to compile a .chm file from a .hhc file with
549 this extension, or the contents will be messed up.
552 void MyFrame::ShowHelp(int commandId
, wxHelpControllerBase
& helpController
)
556 case HelpDemo_Help_Classes
:
557 case HelpDemo_Html_Help_Classes
:
558 case HelpDemo_Advanced_Html_Help_Classes
:
559 case HelpDemo_MS_Html_Help_Classes
:
560 case HelpDemo_Best_Help_Classes
:
561 helpController
.DisplaySection(2);
562 //helpController.DisplaySection("Classes"); // An alternative form for most controllers
565 case HelpDemo_Help_Functions
:
566 case HelpDemo_Html_Help_Functions
:
567 case HelpDemo_Advanced_Html_Help_Functions
:
568 case HelpDemo_MS_Html_Help_Functions
:
569 helpController
.DisplaySection(1);
570 //helpController.DisplaySection("Functions"); // An alternative form for most controllers
573 case HelpDemo_Help_Help
:
574 case HelpDemo_Html_Help_Help
:
575 case HelpDemo_Advanced_Html_Help_Help
:
576 case HelpDemo_MS_Html_Help_Help
:
577 case HelpDemo_Best_Help_Help
:
578 helpController
.DisplaySection(3);
579 //helpController.DisplaySection("About"); // An alternative form for most controllers
582 case HelpDemo_Help_Search
:
583 case HelpDemo_Html_Help_Search
:
584 case HelpDemo_Advanced_Html_Help_Search
:
585 case HelpDemo_MS_Html_Help_Search
:
586 case HelpDemo_Best_Help_Search
:
588 wxString key
= wxGetTextFromUser("Search for?",
589 "Search help for keyword",
593 helpController
.KeywordSearch(key
);
597 case HelpDemo_Help_Index
:
598 case HelpDemo_Html_Help_Index
:
599 case HelpDemo_Advanced_Html_Help_Index
:
600 case HelpDemo_MS_Html_Help_Index
:
601 case HelpDemo_Best_Help_Index
:
602 helpController
.DisplayContents();
605 // These three calls are only used by wxExtHelpController
607 case HelpDemo_Help_KDE
:
608 helpController
.SetViewer("kdehelp");
610 case HelpDemo_Help_GNOME
:
611 helpController
.SetViewer("gnome-help-browser");
613 case HelpDemo_Help_Netscape
:
614 helpController
.SetViewer("netscape", wxHELP_NETSCAPE
);
619 // ----------------------------------------------------------------------------
621 // Demonstrates context-sensitive help
622 // ----------------------------------------------------------------------------
624 BEGIN_EVENT_TABLE(MyModalDialog
, wxDialog
)
627 MyModalDialog::MyModalDialog(wxWindow
*parent
)
630 // Add the context-sensitive help button on the caption for MSW
632 SetExtraStyle(wxDIALOG_EX_CONTEXTHELP
);
635 wxDialog::Create(parent
, -1, wxString("Modal dialog"));
637 wxBoxSizer
*sizerTop
= new wxBoxSizer(wxVERTICAL
);
638 wxBoxSizer
*sizerRow
= new wxBoxSizer(wxHORIZONTAL
);
640 wxButton
* btnOK
= new wxButton(this, wxID_OK
, "&OK");
641 btnOK
->SetHelpText(_("The OK button confirms the dialog choices."));
643 wxButton
* btnCancel
= new wxButton(this, wxID_CANCEL
, "&Cancel");
644 btnCancel
->SetHelpText(_("The Cancel button cancels the dialog."));
646 sizerRow
->Add(btnOK
, 0, wxALIGN_CENTER
| wxALL
, 5);
647 sizerRow
->Add(btnCancel
, 0, wxALIGN_CENTER
| wxALL
, 5);
649 // Add explicit context-sensitive help button for non-MSW
651 sizerRow
->Add(new wxContextHelpButton(this), 0, wxALIGN_CENTER
| wxALL
, 5);
654 wxTextCtrl
*text
= new wxTextCtrl(this, -1, wxT("A demo text control"),
655 wxDefaultPosition
, wxSize(300, 100),
657 text
->SetHelpText(_("Type text here if you have got nothing more interesting to do"));
658 sizerTop
->Add(text
, 0, wxEXPAND
|wxALIGN_CENTER_VERTICAL
|wxALL
, 5 );
659 sizerTop
->Add(sizerRow
, 0, wxALIGN_RIGHT
|wxALIGN_CENTER_VERTICAL
|wxALL
, 5 );
664 sizerTop
->SetSizeHints(this);