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