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
49 #define USE_HTML_HELP 0
53 #include "wx/filesys.h"
54 #include "wx/fs_zip.h"
56 #include "wx/html/helpctrl.h"
59 #if wxUSE_MS_HTML_HELP
60 #include "wx/msw/helpchm.h"
63 #if wxUSE_MS_HTML_HELP && wxUSE_WXHTML_HELP
64 #include "wx/msw/helpbest.h"
67 // ----------------------------------------------------------------------------
69 // ----------------------------------------------------------------------------
70 // the application icon
71 #if defined(__WXGTK__) || defined(__WXX11__) || defined(__WXMOTIF__) || defined(__WXMAC__) || defined(__WXMGL__)
72 #include "mondrian.xpm"
75 // ----------------------------------------------------------------------------
77 // ----------------------------------------------------------------------------
79 // Define a new application type, each program should derive a class from wxApp
80 class MyApp
: public wxApp
83 // override base class virtuals
84 // ----------------------------
86 // this one is called on application startup and is a good place for the app
87 // initialization (doing it here and not in the ctor allows to have an error
88 // return: if OnInit() returns false, the application terminates)
89 virtual bool OnInit();
91 // do some clean up here
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 wxHtmlHelpController
& GetAdvancedHtmlHelpController() { return m_advancedHtmlHelp
; }
107 #if wxUSE_MS_HTML_HELP
108 wxCHMHelpController
& GetMSHtmlHelpController() { return m_msHtmlHelp
; }
110 #if wxUSE_MS_HTML_HELP && wxUSE_WXHTML_HELP
111 wxBestHelpController
& GetBestHelpController() { return m_bestHelp
; }
114 // event handlers (these functions should _not_ be virtual)
115 void OnQuit(wxCommandEvent
& event
);
116 void OnHelp(wxCommandEvent
& event
);
117 void OnAdvancedHtmlHelp(wxCommandEvent
& event
);
118 void OnMSHtmlHelp(wxCommandEvent
& event
);
119 void OnBestHelp(wxCommandEvent
& event
);
121 void OnShowContextHelp(wxCommandEvent
& event
);
122 void OnShowDialogContextHelp(wxCommandEvent
& event
);
124 void ShowHelp(int commandId
, wxHelpControllerBase
& helpController
);
127 wxHelpController m_help
;
130 wxHtmlHelpController m_advancedHtmlHelp
;
133 #if wxUSE_MS_HTML_HELP
134 wxCHMHelpController m_msHtmlHelp
;
137 #if wxUSE_MS_HTML_HELP && wxUSE_WXHTML_HELP
138 wxBestHelpController m_bestHelp
;
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
);
153 DECLARE_EVENT_TABLE()
156 // ----------------------------------------------------------------------------
158 // ----------------------------------------------------------------------------
160 // IDs for the controls and the menu commands
166 HelpDemo_Help_Classes
,
167 HelpDemo_Help_Functions
,
169 HelpDemo_Help_Search
,
170 HelpDemo_Help_ContextHelp
,
171 HelpDemo_Help_DialogContextHelp
,
173 HelpDemo_Html_Help_Index
,
174 HelpDemo_Html_Help_Classes
,
175 HelpDemo_Html_Help_Functions
,
176 HelpDemo_Html_Help_Help
,
177 HelpDemo_Html_Help_Search
,
179 HelpDemo_Advanced_Html_Help_Index
,
180 HelpDemo_Advanced_Html_Help_Classes
,
181 HelpDemo_Advanced_Html_Help_Functions
,
182 HelpDemo_Advanced_Html_Help_Help
,
183 HelpDemo_Advanced_Html_Help_Search
,
185 HelpDemo_MS_Html_Help_Index
,
186 HelpDemo_MS_Html_Help_Classes
,
187 HelpDemo_MS_Html_Help_Functions
,
188 HelpDemo_MS_Html_Help_Help
,
189 HelpDemo_MS_Html_Help_Search
,
191 HelpDemo_Best_Help_Index
,
192 HelpDemo_Best_Help_Classes
,
193 HelpDemo_Best_Help_Functions
,
194 HelpDemo_Best_Help_Help
,
195 HelpDemo_Best_Help_Search
,
199 HelpDemo_Help_Netscape
,
200 // controls start here (the numbers are, of course, arbitrary)
201 HelpDemo_Text
= 1000,
204 // ----------------------------------------------------------------------------
205 // event tables and other macros for wxWindows
206 // ----------------------------------------------------------------------------
208 // the event tables connect the wxWindows events with the functions (event
209 // handlers) which process them. It can be also done at run-time, but for the
210 // simple menu events like this the static method is much simpler.
211 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
212 EVT_MENU(HelpDemo_Quit
, MyFrame::OnQuit
)
213 EVT_MENU(HelpDemo_Help_Index
, MyFrame::OnHelp
)
214 EVT_MENU(HelpDemo_Help_Classes
, MyFrame::OnHelp
)
215 EVT_MENU(HelpDemo_Help_Functions
, MyFrame::OnHelp
)
216 EVT_MENU(HelpDemo_Help_Help
, MyFrame::OnHelp
)
217 EVT_MENU(HelpDemo_Help_Search
, MyFrame::OnHelp
)
218 EVT_MENU(HelpDemo_Help_ContextHelp
, MyFrame::OnShowContextHelp
)
219 EVT_MENU(HelpDemo_Help_DialogContextHelp
, MyFrame::OnShowDialogContextHelp
)
221 EVT_MENU(HelpDemo_Advanced_Html_Help_Index
, MyFrame::OnAdvancedHtmlHelp
)
222 EVT_MENU(HelpDemo_Advanced_Html_Help_Classes
, MyFrame::OnAdvancedHtmlHelp
)
223 EVT_MENU(HelpDemo_Advanced_Html_Help_Functions
, MyFrame::OnAdvancedHtmlHelp
)
224 EVT_MENU(HelpDemo_Advanced_Html_Help_Help
, MyFrame::OnAdvancedHtmlHelp
)
225 EVT_MENU(HelpDemo_Advanced_Html_Help_Search
, MyFrame::OnAdvancedHtmlHelp
)
227 EVT_MENU(HelpDemo_MS_Html_Help_Index
, MyFrame::OnMSHtmlHelp
)
228 EVT_MENU(HelpDemo_MS_Html_Help_Classes
, MyFrame::OnMSHtmlHelp
)
229 EVT_MENU(HelpDemo_MS_Html_Help_Functions
, MyFrame::OnMSHtmlHelp
)
230 EVT_MENU(HelpDemo_MS_Html_Help_Help
, MyFrame::OnMSHtmlHelp
)
231 EVT_MENU(HelpDemo_MS_Html_Help_Search
, MyFrame::OnMSHtmlHelp
)
233 EVT_MENU(HelpDemo_Best_Help_Index
, MyFrame::OnBestHelp
)
235 EVT_MENU(HelpDemo_Help_KDE
, MyFrame::OnHelp
)
236 EVT_MENU(HelpDemo_Help_GNOME
, MyFrame::OnHelp
)
237 EVT_MENU(HelpDemo_Help_Netscape
, MyFrame::OnHelp
)
240 // Create a new application object: this macro will allow wxWindows to create
241 // the application object during program execution (it's better than using a
242 // static object for many reasons) and also declares the accessor function
243 // wxGetApp() which will return the reference of the right type (i.e. MyApp and
247 // ============================================================================
249 // ============================================================================
251 // ----------------------------------------------------------------------------
252 // the application class
253 // ----------------------------------------------------------------------------
255 // `Main program' equivalent: the program execution "starts" here
258 // Create a simple help provider to make SetHelpText() do something.
259 // Note that this must be set before any SetHelpText() calls are made.
260 //wxHelpProvider::Set(new wxSimpleHelpProvider);
261 wxHelpControllerHelpProvider
* provider
= new wxHelpControllerHelpProvider
;
262 wxHelpProvider::Set(provider
);
266 // Required for images in the online documentation
267 wxImage::AddHandler(new wxGIFHandler
);
269 // Required for advanced HTML help
270 #if wxUSE_STREAMS && wxUSE_ZIPSTREAM && wxUSE_ZLIB
271 wxFileSystem::AddHandler(new wxZipFSHandler
);
277 // Create the main application window
278 MyFrame
*frame
= new MyFrame("HelpDemo wxWindows App",
279 wxPoint(50, 50), wxSize(450, 340));
281 #if wxUSE_MS_HTML_HELP
282 provider
->SetHelpController(& frame
->GetMSHtmlHelpController());
284 provider
->SetHelpController(& frame
->GetHelpController());
290 // initialise the help system: this means that we'll use doc.hlp file under
291 // Windows and that the HTML docs are in the subdirectory doc for platforms
293 if ( !frame
->GetHelpController().Initialize("doc") )
295 wxLogError(wxT("Cannot initialize the help system, aborting."));
300 #if wxUSE_MS_HTML_HELP
301 if( !frame
->GetMSHtmlHelpController().Initialize("doc") )
303 wxLogError(wxT("Cannot initialize the MS HTML Help system."));
307 #if wxUSE_MS_HTML_HELP && wxUSE_WXHTML_HELP
308 // you need to call Initialize in order to use wxBestHelpController
309 if( !frame
->GetBestHelpController().Initialize("doc") )
311 wxLogError(wxT("Cannot initialize the best help system, aborting."));
316 // initialise the advanced HTML help system: this means that the HTML docs are in .htb
318 if ( !frame
->GetAdvancedHtmlHelpController().Initialize("doc") )
320 wxLogError(wxT("Cannot initialize the advanced HTML help system, aborting."));
327 // defined(__WXMSW__) && wxUSE_MS_HTML_HELP
328 wxString
path(wxGetCwd());
329 if ( !frame
->GetMSHtmlHelpController().Initialize(path
+ "\\doc.chm") )
331 wxLogError("Cannot initialize the MS HTML help system, aborting.");
343 delete wxHelpProvider::Set(NULL
);
348 // ----------------------------------------------------------------------------
350 // ----------------------------------------------------------------------------
353 MyFrame::MyFrame(const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
)
354 : wxFrame((wxFrame
*)NULL
, 300, title
, pos
, size
)
356 // set the frame icon
357 SetIcon(wxICON(mondrian
));
360 wxMenu
*menuFile
= new wxMenu
;
362 menuFile
->Append(HelpDemo_Help_Index
, "&Help Index...");
363 menuFile
->Append(HelpDemo_Help_Classes
, "&Help on Classes...");
364 menuFile
->Append(HelpDemo_Help_Functions
, "&Help on Functions...");
365 menuFile
->Append(HelpDemo_Help_ContextHelp
, "&Context Help...");
366 menuFile
->Append(HelpDemo_Help_DialogContextHelp
, "&Dialog Context Help...\tCtrl-H");
367 menuFile
->Append(HelpDemo_Help_Help
, "&About Help Demo...");
368 menuFile
->Append(HelpDemo_Help_Search
, "&Search help...");
370 menuFile
->AppendSeparator();
371 menuFile
->Append(HelpDemo_Advanced_Html_Help_Index
, "Advanced HTML &Help Index...");
372 menuFile
->Append(HelpDemo_Advanced_Html_Help_Classes
, "Advanced HTML &Help on Classes...");
373 menuFile
->Append(HelpDemo_Advanced_Html_Help_Functions
, "Advanced HTML &Help on Functions...");
374 menuFile
->Append(HelpDemo_Advanced_Html_Help_Help
, "Advanced HTML &About Help Demo...");
375 menuFile
->Append(HelpDemo_Advanced_Html_Help_Search
, "Advanced HTML &Search help...");
378 #if wxUSE_MS_HTML_HELP
379 menuFile
->AppendSeparator();
380 menuFile
->Append(HelpDemo_MS_Html_Help_Index
, "MS HTML &Help Index...");
381 menuFile
->Append(HelpDemo_MS_Html_Help_Classes
, "MS HTML &Help on Classes...");
382 menuFile
->Append(HelpDemo_MS_Html_Help_Functions
, "MS HTML &Help on Functions...");
383 menuFile
->Append(HelpDemo_MS_Html_Help_Help
, "MS HTML &About Help Demo...");
384 menuFile
->Append(HelpDemo_MS_Html_Help_Search
, "MS HTML &Search help...");
387 #if wxUSE_MS_HTML_HELP && wxUSE_WXHTML_HELP
388 menuFile
->AppendSeparator();
389 menuFile
->Append(HelpDemo_Best_Help_Index
, "Best &Help Index...");
394 menuFile
->AppendSeparator();
395 menuFile
->Append(HelpDemo_Help_KDE
, "Use &KDE");
396 menuFile
->Append(HelpDemo_Help_GNOME
, "Use &GNOME");
397 menuFile
->Append(HelpDemo_Help_Netscape
, "Use &Netscape");
400 menuFile
->AppendSeparator();
401 menuFile
->Append(HelpDemo_Quit
, "E&xit");
403 // now append the freshly created menu to the menu bar...
404 wxMenuBar
*menuBar
= new wxMenuBar
;
405 menuBar
->Append(menuFile
, "&File");
407 // ... and attach this menu bar to the frame
410 // create a status bar just for fun (by default with 1 pane only)
412 SetStatusText("Welcome to wxWindows!");
414 // now create some controls
416 // a panel first - if there were several controls, it would allow us to
417 // navigate between them from the keyboard
418 wxPanel
*panel
= new wxPanel(this, 301, wxPoint(0, 0), wxSize(400, 200));
419 //panel->SetHelpText(_("This panel just holds a static text control."));
420 panel
->SetHelpText(wxContextId(300));
422 // and a static control whose parent is the panel
423 wxStaticText
* staticText
= new wxStaticText(panel
, 302, "Hello, world!", wxPoint(10, 10));
424 staticText
->SetHelpText(_("This static text control isn't doing a lot right now."));
430 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
432 // TRUE is to force the frame to close
436 void MyFrame::OnHelp(wxCommandEvent
& event
)
438 ShowHelp(event
.GetId(), m_help
);
441 void MyFrame::OnShowContextHelp(wxCommandEvent
& event
)
443 // This starts context help mode, then the user
444 // clicks on a window to send a help message
445 wxContextHelp
contextHelp(this);
448 void MyFrame::OnShowDialogContextHelp(wxCommandEvent
& event
)
450 MyModalDialog
dialog(this);
454 void MyFrame::OnAdvancedHtmlHelp(wxCommandEvent
& event
)
457 ShowHelp(event
.GetId(), m_advancedHtmlHelp
);
461 void MyFrame::OnMSHtmlHelp(wxCommandEvent
& event
)
463 #if wxUSE_MS_HTML_HELP
464 ShowHelp(event
.GetId(), m_msHtmlHelp
);
468 void MyFrame::OnBestHelp(wxCommandEvent
& event
)
470 #if wxUSE_MS_HTML_HELP && wxUSE_WXHTML_HELP
471 ShowHelp(event
.GetId(), m_bestHelp
);
476 Notes: ShowHelp uses section ids for displaying particular topics,
477 but you might want to use a unique keyword to display a topic, instead.
479 Section ids are specified as follows for the different formats.
483 The [MAP] section specifies the topic to integer id mapping, e.g.
491 The identifier name corresponds to the label 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.
499 The [MAP] section specifies the HTML filename root to integer id mapping, e.g.
507 The identifier name corresponds to the HTML filename used for that topic.
508 You could also put these in a .h file and #include it in both the MAP
509 section and your C++ source.
511 Note that Tex2RTF doesn't currently generate the MAP section automatically.
513 Simple wxHTML Help and External HTML Help
515 A wxhelp.map file is used, for example:
517 0 wx.htm ; wxWindows: Help index; additional keywords like overview
518 1 wx204.htm ; wxWindows Function Reference
519 2 wx34.htm ; wxWindows Class Reference
521 Note that Tex2RTF doesn't currently generate the MAP section automatically.
525 An extension to the .hhc file format is used, specifying a new parameter
528 <OBJECT type="text/sitemap">
529 <param name="Local" value="doc2.htm#classes">
530 <param name="Name" value="Classes">
531 <param name="ID" value=2>
534 Again, this is not generated automatically by Tex2RTF, though it could
535 be added quite easily.
537 Unfortunately adding the ID parameters appears to interfere with MS HTML Help,
538 so you should not try to compile a .chm file from a .hhc file with
539 this extension, or the contents will be messed up.
542 void MyFrame::ShowHelp(int commandId
, wxHelpControllerBase
& helpController
)
546 case HelpDemo_Help_Classes
:
547 case HelpDemo_Html_Help_Classes
:
548 case HelpDemo_Advanced_Html_Help_Classes
:
549 case HelpDemo_MS_Html_Help_Classes
:
550 case HelpDemo_Best_Help_Classes
:
551 helpController
.DisplaySection(2);
552 //helpController.DisplaySection("Classes"); // An alternative form for most controllers
555 case HelpDemo_Help_Functions
:
556 case HelpDemo_Html_Help_Functions
:
557 case HelpDemo_Advanced_Html_Help_Functions
:
558 case HelpDemo_MS_Html_Help_Functions
:
559 helpController
.DisplaySection(1);
560 //helpController.DisplaySection("Functions"); // An alternative form for most controllers
563 case HelpDemo_Help_Help
:
564 case HelpDemo_Html_Help_Help
:
565 case HelpDemo_Advanced_Html_Help_Help
:
566 case HelpDemo_MS_Html_Help_Help
:
567 case HelpDemo_Best_Help_Help
:
568 helpController
.DisplaySection(3);
569 //helpController.DisplaySection("About"); // An alternative form for most controllers
572 case HelpDemo_Help_Search
:
573 case HelpDemo_Html_Help_Search
:
574 case HelpDemo_Advanced_Html_Help_Search
:
575 case HelpDemo_MS_Html_Help_Search
:
576 case HelpDemo_Best_Help_Search
:
578 wxString key
= wxGetTextFromUser("Search for?",
579 "Search help for keyword",
583 helpController
.KeywordSearch(key
);
587 case HelpDemo_Help_Index
:
588 case HelpDemo_Html_Help_Index
:
589 case HelpDemo_Advanced_Html_Help_Index
:
590 case HelpDemo_MS_Html_Help_Index
:
591 case HelpDemo_Best_Help_Index
:
592 helpController
.DisplayContents();
595 // These three calls are only used by wxExtHelpController
597 case HelpDemo_Help_KDE
:
598 helpController
.SetViewer("kdehelp");
600 case HelpDemo_Help_GNOME
:
601 helpController
.SetViewer("gnome-help-browser");
603 case HelpDemo_Help_Netscape
:
604 helpController
.SetViewer("netscape", wxHELP_NETSCAPE
);
609 // ----------------------------------------------------------------------------
611 // Demonstrates context-sensitive help
612 // ----------------------------------------------------------------------------
614 BEGIN_EVENT_TABLE(MyModalDialog
, wxDialog
)
617 MyModalDialog::MyModalDialog(wxWindow
*parent
)
620 // Add the context-sensitive help button on the caption for MSW
622 SetExtraStyle(wxDIALOG_EX_CONTEXTHELP
);
625 wxDialog::Create(parent
, -1, wxString("Modal dialog"));
627 wxBoxSizer
*sizerTop
= new wxBoxSizer(wxVERTICAL
);
628 wxBoxSizer
*sizerRow
= new wxBoxSizer(wxHORIZONTAL
);
630 wxButton
* btnOK
= new wxButton(this, wxID_OK
, "&OK");
631 btnOK
->SetHelpText(_("The OK button confirms the dialog choices."));
633 wxButton
* btnCancel
= new wxButton(this, wxID_CANCEL
, "&Cancel");
634 btnCancel
->SetHelpText(_("The Cancel button cancels the dialog."));
636 sizerRow
->Add(btnOK
, 0, wxALIGN_CENTER
| wxALL
, 5);
637 sizerRow
->Add(btnCancel
, 0, wxALIGN_CENTER
| wxALL
, 5);
639 // Add explicit context-sensitive help button for non-MSW
641 sizerRow
->Add(new wxContextHelpButton(this), 0, wxALIGN_CENTER
| wxALL
, 5);
644 wxTextCtrl
*text
= new wxTextCtrl(this, -1, wxT("A demo text control"),
645 wxDefaultPosition
, wxSize(300, 100),
647 text
->SetHelpText(_("Type text here if you have got nothing more interesting to do"));
648 sizerTop
->Add(text
, 0, wxEXPAND
|wxALIGN_CENTER_VERTICAL
|wxALL
, 5 );
649 sizerTop
->Add(sizerRow
, 0, wxALIGN_RIGHT
|wxALIGN_CENTER_VERTICAL
|wxALL
, 5 );
654 sizerTop
->SetSizeHints(this);