Added a sample to test wxExtHelpController. Should work with other
[wxWidgets.git] / samples / help / demo.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: minimal.cpp
3 // Purpose: Minimal wxWindows sample
4 // Author: Julian Smart
5 // Modified by:
6 // Created: 04/01/98
7 // RCS-ID: $Id$
8 // Copyright: (c) Julian Smart and Markus Holzem
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 // ----------------------------------------------------------------------------
17 // headers
18 // ----------------------------------------------------------------------------
19 #ifdef __GNUG__
20 #pragma implementation "minimal.cpp"
21 #pragma interface "minimal.cpp"
22 #endif
23
24 // For compilers that support precompilation, includes "wx/wx.h".
25 #include "wx/wxprec.h"
26
27 #ifdef __BORLANDC__
28 #pragma hdrstop
29 #endif
30
31 // for all others, include the necessary headers (this file is usually all you
32 // need because it includes almost all "standard" wxWindows headers
33 #ifndef WX_PRECOMP
34 #include "wx/wx.h"
35 #endif
36
37 #include "wx/helpbase.h"
38 #include "wx/help.h"
39
40 // ----------------------------------------------------------------------------
41 // ressources
42 // ----------------------------------------------------------------------------
43 // the application icon
44 #ifdef __WXGTK__
45 #include "mondrian.xpm"
46 #endif
47
48 // ----------------------------------------------------------------------------
49 // private classes
50 // ----------------------------------------------------------------------------
51
52 // Define a new application type, each program should derive a class from wxApp
53 class MyApp : public wxApp
54 {
55 public:
56 // override base class virtuals
57 // ----------------------------
58
59 // this one is called on application startup and is a good place for the app
60 // initialization (doing it here and not in the ctor allows to have an error
61 // return: if OnInit() returns false, the application terminates)
62 virtual bool OnInit();
63 };
64
65 // Define a new frame type: this is going to be our main frame
66 class MyFrame : public wxFrame
67 {
68 public:
69 // ctor(s)
70 MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size);
71
72 // event handlers (these functions should _not_ be virtual)
73 void OnQuit(wxCommandEvent& event);
74 void OnHelp(wxCommandEvent& event);
75
76 private:
77 wxHelpController help;
78 // any class wishing to process wxWindows events must use this macro
79 DECLARE_EVENT_TABLE()
80 };
81
82 // ----------------------------------------------------------------------------
83 // constants
84 // ----------------------------------------------------------------------------
85
86 // IDs for the controls and the menu commands
87 enum
88 {
89 // menu items
90 Minimal_Quit = 1,
91 Minimal_Help_Index,
92 Minimal_Help_Classes,
93 Minimal_Help_Functions,
94 Minimal_Help_Help,
95 Minimal_Help_KDE,
96 Minimal_Help_GNOME,
97 Minimal_Help_Netscape,
98 Minimal_Help_Search,
99 // controls start here (the numbers are, of course, arbitrary)
100 Minimal_Text = 1000,
101 };
102
103 // ----------------------------------------------------------------------------
104 // event tables and other macros for wxWindows
105 // ----------------------------------------------------------------------------
106
107 // the event tables connect the wxWindows events with the functions (event
108 // handlers) which process them. It can be also done at run-time, but for the
109 // simple menu events like this the static method is much simpler.
110 BEGIN_EVENT_TABLE(MyFrame, wxFrame)
111 EVT_MENU(Minimal_Quit, MyFrame::OnQuit)
112 EVT_MENU(Minimal_Help_Index, MyFrame::OnHelp)
113 EVT_MENU(Minimal_Help_Classes, MyFrame::OnHelp)
114 EVT_MENU(Minimal_Help_Functions, MyFrame::OnHelp)
115 EVT_MENU(Minimal_Help_Help, MyFrame::OnHelp)
116 EVT_MENU(Minimal_Help_KDE, MyFrame::OnHelp)
117 EVT_MENU(Minimal_Help_GNOME, MyFrame::OnHelp)
118 EVT_MENU(Minimal_Help_Netscape, MyFrame::OnHelp)
119 EVT_MENU(Minimal_Help_Search, MyFrame::OnHelp)
120 END_EVENT_TABLE()
121
122 // Create a new application object: this macro will allow wxWindows to create
123 // the application object during program execution (it's better than using a
124 // static object for many reasons) and also declares the accessor function
125 // wxGetApp() which will return the reference of the right type (i.e. MyApp and
126 // not wxApp)
127 IMPLEMENT_APP(MyApp)
128
129 // ============================================================================
130 // implementation
131 // ============================================================================
132
133 // ----------------------------------------------------------------------------
134 // the application class
135 // ----------------------------------------------------------------------------
136
137 // `Main program' equivalent: the program execution "starts" here
138 bool MyApp::OnInit()
139 {
140 // Create the main application window
141 MyFrame *frame = new MyFrame("Minimal wxWindows App",
142 wxPoint(50, 50), wxSize(450, 340));
143
144 // Show it and tell the application that it's our main window
145 // @@@ what does it do exactly, in fact? is it necessary here?
146 frame->Show(TRUE);
147 SetTopWindow(frame);
148
149 // success: wxApp::OnRun() will be called which will enter the main message
150 // loop and the application will run. If we returned FALSE here, the
151 // application would exit immediately.
152 return TRUE;
153 }
154
155 // ----------------------------------------------------------------------------
156 // main frame
157 // ----------------------------------------------------------------------------
158
159 // frame constructor
160 MyFrame::MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size)
161 : wxFrame((wxFrame *)NULL, -1, title, pos, size)
162 {
163 // set the frame icon
164 SetIcon(wxICON(mondrian));
165
166 // create a menu bar
167 wxMenu *menuFile = new wxMenu;
168
169 menuFile->Append(Minimal_Help_Index, "&Help Index...");
170 menuFile->Append(Minimal_Help_Classes, "&Help on Classes...");
171 menuFile->Append(Minimal_Help_Functions, "&Help on Functions...");
172 menuFile->Append(Minimal_Help_Help, "&About wxExtHelpController...");
173 menuFile->AppendSeparator();
174 menuFile->Append(Minimal_Help_Search, "&Search help...");
175 if(help.IsKindOf(CLASSINFO(wxExtHelpController)))
176 {
177 menuFile->AppendSeparator();
178 menuFile->Append(Minimal_Help_KDE, "Use &KDE");
179 menuFile->Append(Minimal_Help_GNOME, "Use &GNOME");
180 menuFile->Append(Minimal_Help_Netscape, "Use &Netscape");
181 }
182 menuFile->AppendSeparator();
183 menuFile->Append(Minimal_Quit, "E&xit");
184
185 // now append the freshly created menu to the menu bar...
186 wxMenuBar *menuBar = new wxMenuBar;
187 menuBar->Append(menuFile, "&File");
188
189 // ... and attach this menu bar to the frame
190 SetMenuBar(menuBar);
191
192 // create a status bar just for fun (by default with 1 pane only)
193 CreateStatusBar();
194 SetStatusText("Welcome to wxWindows!");
195
196 // now create some controls
197
198 // a panel first - if there were several controls, it would allow us to
199 // navigate between them from the keyboard
200 wxPanel *panel = new wxPanel(this, -1, wxPoint(0, 0), wxSize(400, 200));
201
202 // and a static control whose parent is the panel
203 (void)new wxStaticText(panel, -1, "Hello, world!", wxPoint(10, 10));
204
205 // initialise the help system
206 help.Initialize("doc");
207
208 }
209
210
211 // event handlers
212
213 void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
214 {
215 // TRUE is to force the frame to close
216 Close(TRUE);
217 }
218
219 void MyFrame::OnHelp(wxCommandEvent& event)
220 {
221 switch(event.GetId())
222 {
223 case Minimal_Help_Classes:
224 help.DisplaySection(1);
225 break;
226 case Minimal_Help_Functions:
227 help.DisplaySection(2);
228 break;
229 case Minimal_Help_Help:
230 help.DisplaySection(5);
231 break;
232 case Minimal_Help_KDE:
233 if(help.IsKindOf(CLASSINFO(wxExtHelpController)))
234 ((wxExtHelpController *)&help)->SetBrowser("kdehelp");
235 break;
236 case Minimal_Help_GNOME:
237 if(help.IsKindOf(CLASSINFO(wxExtHelpController)))
238 ((wxExtHelpController *)&help)->SetBrowser("gnome-help-browser");
239 break;
240 case Minimal_Help_Netscape:
241 if(help.IsKindOf(CLASSINFO(wxExtHelpController)))
242 ((wxExtHelpController *)&help)->SetBrowser("netscape",TRUE);
243 break;
244 case Minimal_Help_Search:
245 {
246 wxString key = wxGetTextFromUser("Search for?",
247 "Search help for keyword",
248 "",
249 this);
250 if(! key.IsEmpty())
251 help.KeywordSearch(key);
252 }
253 break;
254 case Minimal_Help_Index:
255 default:
256 help.DisplayContents();
257 break;
258 }
259 }