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