Added WinHelp sample doc to samples/help, and made it possible to show
[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 for a more complex help viewer.
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 # include "wx/generic/helpwxht.h"
49 #endif
50
51 // ----------------------------------------------------------------------------
52 // ressources
53 // ----------------------------------------------------------------------------
54 // the application icon
55 #if defined(__WXGTK__) || defined(__WXMOTIF__)
56 #include "mondrian.xpm"
57 #endif
58
59 // ----------------------------------------------------------------------------
60 // private classes
61 // ----------------------------------------------------------------------------
62
63 // Define a new application type, each program should derive a class from wxApp
64 class MyApp : public wxApp
65 {
66 public:
67 // override base class virtuals
68 // ----------------------------
69
70 // this one is called on application startup and is a good place for the app
71 // initialization (doing it here and not in the ctor allows to have an error
72 // return: if OnInit() returns false, the application terminates)
73 virtual bool OnInit();
74 };
75
76 // Define a new frame type: this is going to be our main frame
77 class MyFrame : public wxFrame
78 {
79 public:
80 // ctor(s)
81 MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size);
82
83 wxHelpController& GetHelpController() { return m_help; }
84
85 #if USE_HTML_HELP
86 wxHelpControllerHtml& GetHtmlHelpController() { return m_htmlHelp; }
87 #endif
88
89 // event handlers (these functions should _not_ be virtual)
90 void OnQuit(wxCommandEvent& event);
91 void OnHelp(wxCommandEvent& event);
92 void OnHtmlHelp(wxCommandEvent& event);
93
94 private:
95 wxHelpController m_help;
96
97 #if USE_HTML_HELP
98 wxHelpControllerHtml m_htmlHelp;
99 #endif
100
101 // any class wishing to process wxWindows events must use this macro
102 DECLARE_EVENT_TABLE()
103 };
104
105 // ----------------------------------------------------------------------------
106 // constants
107 // ----------------------------------------------------------------------------
108
109 // IDs for the controls and the menu commands
110 enum
111 {
112 // menu items
113 HelpDemo_Quit = 1,
114 HelpDemo_Help_Index,
115 HelpDemo_Help_Classes,
116 HelpDemo_Help_Functions,
117 HelpDemo_Help_Help,
118 HelpDemo_Help_Search,
119
120 HelpDemo_Html_Help_Index,
121 HelpDemo_Html_Help_Classes,
122 HelpDemo_Html_Help_Functions,
123 HelpDemo_Html_Help_Help,
124 HelpDemo_Html_Help_Search,
125
126 HelpDemo_Help_KDE,
127 HelpDemo_Help_GNOME,
128 HelpDemo_Help_Netscape,
129 // controls start here (the numbers are, of course, arbitrary)
130 HelpDemo_Text = 1000,
131 };
132
133 // ----------------------------------------------------------------------------
134 // event tables and other macros for wxWindows
135 // ----------------------------------------------------------------------------
136
137 // the event tables connect the wxWindows events with the functions (event
138 // handlers) which process them. It can be also done at run-time, but for the
139 // simple menu events like this the static method is much simpler.
140 BEGIN_EVENT_TABLE(MyFrame, wxFrame)
141 EVT_MENU(HelpDemo_Quit, MyFrame::OnQuit)
142 EVT_MENU(HelpDemo_Help_Index, MyFrame::OnHelp)
143 EVT_MENU(HelpDemo_Help_Classes, MyFrame::OnHelp)
144 EVT_MENU(HelpDemo_Help_Functions, MyFrame::OnHelp)
145 EVT_MENU(HelpDemo_Help_Help, MyFrame::OnHelp)
146 EVT_MENU(HelpDemo_Help_Search, MyFrame::OnHelp)
147
148 EVT_MENU(HelpDemo_Html_Help_Index, MyFrame::OnHtmlHelp)
149 EVT_MENU(HelpDemo_Html_Help_Classes, MyFrame::OnHtmlHelp)
150 EVT_MENU(HelpDemo_Html_Help_Functions, MyFrame::OnHtmlHelp)
151 EVT_MENU(HelpDemo_Html_Help_Help, MyFrame::OnHtmlHelp)
152 EVT_MENU(HelpDemo_Html_Help_Search, MyFrame::OnHtmlHelp)
153
154 EVT_MENU(HelpDemo_Help_KDE, MyFrame::OnHelp)
155 EVT_MENU(HelpDemo_Help_GNOME, MyFrame::OnHelp)
156 EVT_MENU(HelpDemo_Help_Netscape, MyFrame::OnHelp)
157 END_EVENT_TABLE()
158
159 // Create a new application object: this macro will allow wxWindows to create
160 // the application object during program execution (it's better than using a
161 // static object for many reasons) and also declares the accessor function
162 // wxGetApp() which will return the reference of the right type (i.e. MyApp and
163 // not wxApp)
164 IMPLEMENT_APP(MyApp)
165
166 // ============================================================================
167 // implementation
168 // ============================================================================
169
170 // ----------------------------------------------------------------------------
171 // the application class
172 // ----------------------------------------------------------------------------
173
174 // `Main program' equivalent: the program execution "starts" here
175 bool MyApp::OnInit()
176 {
177 #if wxUSE_HTML
178 #if wxUSE_GIF
179 // Required for images in the online documentation
180 wxImage::AddHandler(new wxGIFHandler);
181 #endif
182 #endif
183
184 // Create the main application window
185 MyFrame *frame = new MyFrame("HelpDemo wxWindows App",
186 wxPoint(50, 50), wxSize(450, 340));
187
188 frame->Show(TRUE);
189 SetTopWindow(frame);
190
191 // initialise the help system: this means that we'll use doc.hlp file under
192 // Windows and that the HTML docs are in the subdirectory doc for platforms
193 // using HTML help
194 if ( !frame->GetHelpController().Initialize("doc") )
195 {
196 wxLogError("Cannot initialize the help system, aborting.");
197
198 return FALSE;
199 }
200
201 #if USE_HTML_HELP
202 // initialise the help system: this means that the HTML docs are in the
203 // subdirectory doc for platforms using HTML help
204 if ( !frame->GetHtmlHelpController().Initialize("doc") )
205 {
206 wxLogError("Cannot initialize the HTML help system, aborting.");
207
208 return FALSE;
209 }
210 #endif
211
212 return TRUE;
213 }
214
215 // ----------------------------------------------------------------------------
216 // main frame
217 // ----------------------------------------------------------------------------
218
219 // frame constructor
220 MyFrame::MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size)
221 : wxFrame((wxFrame *)NULL, -1, title, pos, size)
222 {
223 // set the frame icon
224 SetIcon(wxICON(mondrian));
225
226 // create a menu bar
227 wxMenu *menuFile = new wxMenu;
228
229 menuFile->Append(HelpDemo_Help_Index, "&Help Index...");
230 menuFile->Append(HelpDemo_Help_Classes, "&Help on Classes...");
231 menuFile->Append(HelpDemo_Help_Functions, "&Help on Functions...");
232 menuFile->Append(HelpDemo_Help_Help, "&About Help Demo...");
233 menuFile->AppendSeparator();
234 menuFile->Append(HelpDemo_Help_Search, "&Search help...");
235 #if USE_HTML_HELP
236 menuFile->AppendSeparator();
237 menuFile->Append(HelpDemo_Html_Help_Index, "HTML &Help Index...");
238 menuFile->Append(HelpDemo_Html_Help_Classes, "HTML &Help on Classes...");
239 menuFile->Append(HelpDemo_Html_Help_Functions, "HTML &Help on Functions...");
240 menuFile->Append(HelpDemo_Html_Help_Help, "HTML &About Help Demo...");
241 menuFile->AppendSeparator();
242 menuFile->Append(HelpDemo_Html_Help_Search, "HTML &Search help...");
243 #endif
244
245 #ifndef __WXMSW__
246 #if !wxUSE_HTML
247 menuFile->AppendSeparator();
248 menuFile->Append(HelpDemo_Help_KDE, "Use &KDE");
249 menuFile->Append(HelpDemo_Help_GNOME, "Use &GNOME");
250 menuFile->Append(HelpDemo_Help_Netscape, "Use &Netscape");
251 #endif
252 #endif
253 menuFile->AppendSeparator();
254 menuFile->Append(HelpDemo_Quit, "E&xit");
255
256 // now append the freshly created menu to the menu bar...
257 wxMenuBar *menuBar = new wxMenuBar;
258 menuBar->Append(menuFile, "&File");
259
260 // ... and attach this menu bar to the frame
261 SetMenuBar(menuBar);
262
263 // create a status bar just for fun (by default with 1 pane only)
264 CreateStatusBar();
265 SetStatusText("Welcome to wxWindows!");
266
267 // now create some controls
268
269 // a panel first - if there were several controls, it would allow us to
270 // navigate between them from the keyboard
271 wxPanel *panel = new wxPanel(this, -1, wxPoint(0, 0), wxSize(400, 200));
272
273 // and a static control whose parent is the panel
274 (void)new wxStaticText(panel, -1, "Hello, world!", wxPoint(10, 10));
275 }
276
277
278 // event handlers
279
280 void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
281 {
282 // TRUE is to force the frame to close
283 Close(TRUE);
284 }
285
286 void MyFrame::OnHelp(wxCommandEvent& event)
287 {
288 switch(event.GetId())
289 {
290
291 // Note: For WinHelp, these ids are specified in the map session, mapping
292 // topic names to numbers.
293 // For HTML and external help, a wxhelp.map file is used.
294
295 case HelpDemo_Help_Classes:
296 m_help.DisplaySection(2);
297 break;
298 case HelpDemo_Help_Functions:
299 m_help.DisplaySection(1);
300 break;
301 case HelpDemo_Help_Help:
302 m_help.DisplaySection(3);
303 break;
304
305 // These three calls are only used by wxExtHelpController
306
307 case HelpDemo_Help_KDE:
308 m_help.SetViewer("kdehelp");
309 break;
310 case HelpDemo_Help_GNOME:
311 m_help.SetViewer("gnome-help-browser");
312 break;
313 case HelpDemo_Help_Netscape:
314 m_help.SetViewer("netscape", wxHELP_NETSCAPE);
315 break;
316
317 case HelpDemo_Help_Search:
318 {
319 wxString key = wxGetTextFromUser("Search for?",
320 "Search help for keyword",
321 "",
322 this);
323 if(! key.IsEmpty())
324 m_help.KeywordSearch(key);
325 }
326 break;
327 case HelpDemo_Help_Index:
328 default:
329 m_help.DisplayContents();
330 break;
331 }
332 }
333
334 void MyFrame::OnHtmlHelp(wxCommandEvent& event)
335 {
336 #if USE_HTML_HELP
337 switch(event.GetId())
338 {
339
340 case HelpDemo_Html_Help_Classes:
341 m_htmlHelp.DisplaySection(2);
342 break;
343 case HelpDemo_Html_Help_Functions:
344 m_htmlHelp.DisplaySection(1);
345 break;
346 case HelpDemo_Html_Help_Help:
347 m_htmlHelp.DisplaySection(3);
348 break;
349
350 case HelpDemo_Html_Help_Search:
351 {
352 wxString key = wxGetTextFromUser("Search for?",
353 "Search help for keyword",
354 "",
355 this);
356 if(! key.IsEmpty())
357 m_htmlHelp.KeywordSearch(key);
358 }
359 break;
360 case HelpDemo_Html_Help_Index:
361 default:
362 m_htmlHelp.DisplayContents();
363 break;
364 }
365 #endif
366 }
367