changed to use 2 html windows
[wxWidgets.git] / samples / html / test / test.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: test.cpp
3 // Purpose: wxHtml testing example
4 /////////////////////////////////////////////////////////////////////////////
5
6 #ifdef __GNUG__
7 #pragma implementation "test.cpp"
8 #pragma interface "test.cpp"
9 #endif
10
11 // For compilers that support precompilation, includes "wx/wx.h".
12 #include <wx/wxprec.h>
13
14 #ifdef __BORLANDC__
15 #pragma hdrstop
16 #endif
17
18 // for all others, include the necessary headers (this file is usually all you
19 // need because it includes almost all "standard" wxWindows headers
20 #ifndef WX_PRECOMP
21 #include <wx/wx.h>
22 #endif
23
24 #include <wx/image.h>
25 #include <wx/html/htmlwin.h>
26
27 // ----------------------------------------------------------------------------
28 // private classes
29 // ----------------------------------------------------------------------------
30
31 // Define a new application type, each program should derive a class from wxApp
32 class MyApp : public wxApp
33 {
34 public:
35 // override base class virtuals
36 // ----------------------------
37
38 // this one is called on application startup and is a good place for the app
39 // initialization (doing it here and not in the ctor allows to have an error
40 // return: if OnInit() returns false, the application terminates)
41 virtual bool OnInit();
42 };
43
44 // Define a new frame type: this is going to be our main frame
45 class MyFrame : public wxFrame
46 {
47 public:
48 // ctor(s)
49 MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size);
50
51 // event handlers (these functions should _not_ be virtual)
52 void OnQuit(wxCommandEvent& event);
53 void OnAbout(wxCommandEvent& event);
54 void OnBack(wxCommandEvent& event);
55 void OnForward(wxCommandEvent& event);
56
57 private:
58 // any class wishing to process wxWindows events must use this macro
59 DECLARE_EVENT_TABLE()
60 };
61
62 // ----------------------------------------------------------------------------
63 // constants
64 // ----------------------------------------------------------------------------
65
66 // IDs for the controls and the menu commands
67 enum
68 {
69 // menu items
70 Minimal_Quit = 1,
71 Minimal_About,
72 Minimal_Back,
73 Minimal_Forward,
74
75 // controls start here (the numbers are, of course, arbitrary)
76 Minimal_Text = 1000,
77 };
78
79 // ----------------------------------------------------------------------------
80 // event tables and other macros for wxWindows
81 // ----------------------------------------------------------------------------
82
83 // the event tables connect the wxWindows events with the functions (event
84 // handlers) which process them. It can be also done at run-time, but for the
85 // simple menu events like this the static method is much simpler.
86 BEGIN_EVENT_TABLE(MyFrame, wxFrame)
87 EVT_MENU(Minimal_Quit, MyFrame::OnQuit)
88 EVT_MENU(Minimal_About, MyFrame::OnAbout)
89 EVT_MENU(Minimal_Back, MyFrame::OnBack)
90 EVT_MENU(Minimal_Forward, MyFrame::OnForward)
91 END_EVENT_TABLE()
92
93 // Create a new application object: this macro will allow wxWindows to create
94 // the application object during program execution (it's better than using a
95 // static object for many reasons) and also declares the accessor function
96 // wxGetApp() which will return the reference of the right type (i.e. MyApp and
97 // not wxApp)
98 IMPLEMENT_APP(MyApp)
99
100 // ============================================================================
101 // implementation
102 // ============================================================================
103
104 // ----------------------------------------------------------------------------
105 // the application class
106 // ----------------------------------------------------------------------------
107 // `Main program' equivalent: the program execution "starts" here
108 bool MyApp::OnInit()
109 {
110 wxLogDebug("[starting testing app]");
111 #if wxUSE_LIBPNG
112 wxImage::AddHandler(new wxPNGHandler);
113 #endif
114 #if wxUSE_LIBJPEG
115 wxImage::AddHandler(new wxJPEGHandler);
116 #endif
117 #if wxUSE_GIF
118 wxImage::AddHandler(new wxGIFHandler);
119 #endif
120 // Create the main application window
121 MyFrame *frame = new MyFrame("wxHtmlWindow testing application",
122 wxPoint(50, 50), wxSize(640, 480));
123 MyFrame *frame2 = new MyFrame("wxHtmlWindow testing application",
124 wxPoint(150, 50), wxSize(640, 480));
125
126 // Show it and tell the application that it's our main window
127 // @@@ what does it do exactly, in fact? is it necessary here?
128 frame->Show(TRUE);
129 frame2->Show(TRUE);
130 SetTopWindow(frame);
131
132
133 // success: wxApp::OnRun() will be called which will enter the main message
134 // loop and the application will run. If we returned FALSE here, the
135 // application would exit immediately.
136 return TRUE;
137 }
138
139 // ----------------------------------------------------------------------------
140 // main frame
141 // ----------------------------------------------------------------------------
142
143 wxHtmlWindow *html;
144
145 // frame constructor
146 MyFrame::MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size)
147 : wxFrame((wxFrame *)NULL, -1, title, pos, size, wxDEFAULT_FRAME_STYLE, "html_test_app")
148 {
149 // create a menu bar
150 wxMenu *menuFile = new wxMenu;
151 wxMenu *menuNav = new wxMenu;
152
153 menuFile->Append(Minimal_About, "&Load wxWindows manual page");
154 menuFile->AppendSeparator();
155 menuFile->Append(Minimal_Quit, "E&xit");
156 menuNav->Append(Minimal_Back, "Go &BACK");
157 menuNav->Append(Minimal_Forward, "Go &FORWARD");
158
159 // now append the freshly created menu to the menu bar...
160 wxMenuBar *menuBar = new wxMenuBar;
161 menuBar->Append(menuFile, "&File");
162 menuBar->Append(menuNav, "&Navigate");
163
164 // ... and attach this menu bar to the frame
165 SetMenuBar(menuBar);
166
167 CreateStatusBar(1);
168
169 {
170 wxConfig *cfg = new wxConfig("wxHtmlTest");
171 html = new wxHtmlWindow(this);
172 html -> SetRelatedFrame(this, "HTML : %s");
173 html -> SetRelatedStatusBar(0);
174 html -> ReadCustomization(cfg);
175 delete cfg;
176 html -> LoadPage("test.htm");
177 }
178 }
179
180
181 // event handlers
182
183 void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
184 {
185 // TRUE is to force the frame to close
186 wxLogDebug("about to save config...");
187 wxConfig *cfg = new wxConfig("wxHtmlTest");
188 html -> WriteCustomization(cfg);
189 delete cfg;
190 Close(TRUE);
191 }
192
193 void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
194 {
195 html -> LoadPage("fft.html");
196 }
197
198
199
200 void MyFrame::OnBack(wxCommandEvent& WXUNUSED(event))
201 {
202 if (!html -> HistoryBack()) wxMessageBox("You reached prehistory era!");
203 }
204
205
206 void MyFrame::OnForward(wxCommandEvent& WXUNUSED(event))
207 {
208 if (!html -> HistoryForward()) wxMessageBox("No more items in history!");
209 }