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