]> git.saurik.com Git - wxWidgets.git/blob - samples/html/test/test.cpp
Fixed flat style for toolbars under XP, Windows Classic style
[wxWidgets.git] / samples / html / test / test.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: test.cpp
3 // Purpose: wxHtml testing example
4 // Author: Vaclav Slavik
5 // Created: 1999-07-07
6 // RCS-ID: $Id$
7 // Copyright: (c) Vaclav Slavik
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
10
11 #if defined(__GNUG__) && !defined(__APPLE__)
12 #pragma implementation "test.cpp"
13 #pragma interface "test.cpp"
14 #endif
15
16 // For compilers that support precompilation, includes "wx/wx.h".
17 #include "wx/wxprec.h"
18
19 #ifdef __BORLANDC__
20 #pragma hdrstop
21 #endif
22
23 // For all others, include the necessary headers (this file is usually all you
24 // need because it includes almost all "standard" wxWidgets headers
25 #ifndef WX_PRECOMP
26 #include "wx/wx.h"
27 #endif
28
29 #include "wx/image.h"
30 #include "wx/sysopt.h"
31 #include "wx/html/htmlwin.h"
32 #include "wx/html/htmlproc.h"
33 #include "wx/fs_inet.h"
34 #include "wx/filedlg.h"
35
36 // ----------------------------------------------------------------------------
37 // private classes
38 // ----------------------------------------------------------------------------
39
40 // Define a new application type, each program should derive a class from wxApp
41 class MyApp : public wxApp
42 {
43 public:
44 virtual bool OnInit();
45 };
46
47 // Define a new frame type: this is going to be our main frame
48 class MyFrame : public wxFrame
49 {
50 public:
51 // ctor(s)
52 MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size);
53
54 // event handlers (these functions should _not_ be virtual)
55 void OnQuit(wxCommandEvent& event);
56 void OnPageOpen(wxCommandEvent& event);
57 void OnBack(wxCommandEvent& event);
58 void OnForward(wxCommandEvent& event);
59 void OnProcessor(wxCommandEvent& event);
60
61 private:
62 wxHtmlWindow *m_Html;
63 wxHtmlProcessor *m_Processor;
64
65 // Any class wishing to process wxWidgets events must use this macro
66 DECLARE_EVENT_TABLE()
67 };
68
69
70 class BoldProcessor : public wxHtmlProcessor
71 {
72 public:
73 virtual wxString Process(const wxString& s) const
74 {
75 wxString r(s);
76 r.Replace(wxT("<b>"), wxEmptyString);
77 r.Replace(wxT("<B>"), wxEmptyString);
78 r.Replace(wxT("</b>"), wxEmptyString);
79 r.Replace(wxT("</B>"), wxEmptyString);
80
81 return r;
82 }
83 };
84
85 // ----------------------------------------------------------------------------
86 // constants
87 // ----------------------------------------------------------------------------
88
89 // IDs for the controls and the menu commands
90 enum
91 {
92 // menu items
93 ID_PageOpen = wxID_HIGHEST,
94 ID_Back,
95 ID_Forward,
96 ID_Processor
97 };
98
99 // ----------------------------------------------------------------------------
100 // event tables and other macros for wxWidgets
101 // ----------------------------------------------------------------------------
102
103 BEGIN_EVENT_TABLE(MyFrame, wxFrame)
104 EVT_MENU(wxID_EXIT, MyFrame::OnQuit)
105 EVT_MENU(ID_PageOpen, MyFrame::OnPageOpen)
106 EVT_MENU(ID_Back, MyFrame::OnBack)
107 EVT_MENU(ID_Forward, MyFrame::OnForward)
108 EVT_MENU(ID_Processor, MyFrame::OnProcessor)
109 END_EVENT_TABLE()
110
111 IMPLEMENT_APP(MyApp)
112
113 // ============================================================================
114 // implementation
115 // ============================================================================
116
117 // ----------------------------------------------------------------------------
118 // the application class
119 // ----------------------------------------------------------------------------
120
121 // `Main program' equivalent: the program execution "starts" here
122 bool MyApp::OnInit()
123 {
124 #if wxUSE_SYSTEM_OPTIONS
125 wxSystemOptions::SetOption(wxT("no-maskblt"), 1);
126 #endif
127
128 wxInitAllImageHandlers();
129 #if wxUSE_FS_INET && wxUSE_STREAMS && wxUSE_SOCKETS
130 wxFileSystem::AddHandler(new wxInternetFSHandler);
131 #endif
132
133 SetVendorName(wxT("wxWidgets"));
134 SetAppName(wxT("wxHtmlTest"));
135 // the following call to wxConfig::Get will use it to create an object...
136
137 // Create the main application window
138 MyFrame *frame = new MyFrame(_("wxHtmlWindow testing application"),
139 wxDefaultPosition, wxSize(640, 480));
140
141 frame->Show();
142
143 return true /* continue running */;
144 }
145
146 // ----------------------------------------------------------------------------
147 // main frame
148 // ----------------------------------------------------------------------------
149
150 // frame constructor
151 MyFrame::MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size)
152 : wxFrame((wxFrame *)NULL, wxID_ANY, title, pos, size,
153 wxDEFAULT_FRAME_STYLE, wxT("html_test_app"))
154 {
155 // create a menu bar
156 wxMenu *menuFile = new wxMenu;
157 wxMenu *menuNav = new wxMenu;
158
159 menuFile->Append(ID_PageOpen, _("&Open HTML page..."));
160 menuFile->AppendSeparator();
161 menuFile->Append(ID_Processor, _("&Remove bold attribute"),
162 wxEmptyString, wxITEM_CHECK);
163
164 menuFile->AppendSeparator();
165 menuFile->Append(wxID_EXIT, _("&Close frame"));
166 menuNav->Append(ID_Back, _("Go &BACK"));
167 menuNav->Append(ID_Forward, _("Go &FORWARD"));
168
169 // now append the freshly created menu to the menu bar...
170 wxMenuBar *menuBar = new wxMenuBar;
171 menuBar->Append(menuFile, _("&File"));
172 menuBar->Append(menuNav, _("&Navigate"));
173
174 // ... and attach this menu bar to the frame
175 SetMenuBar(menuBar);
176
177 #if wxUSE_ACCEL
178 // Create convenient accelerators for Back and Forward navigation
179 wxAcceleratorEntry entries[2];
180 entries[0].Set(wxACCEL_ALT, WXK_LEFT, ID_Back);
181 entries[1].Set(wxACCEL_ALT, WXK_RIGHT, ID_Forward);
182
183 wxAcceleratorTable accel(WXSIZEOF(entries), entries);
184 SetAcceleratorTable(accel);
185 #endif // wxUSE_ACCEL
186
187 #if wxUSE_STATUSBAR
188 CreateStatusBar(1);
189 #endif // wxUSE_STATUSBAR
190
191 m_Processor = new BoldProcessor;
192 m_Processor->Enable(false);
193 m_Html = new wxHtmlWindow(this);
194 m_Html->SetRelatedFrame(this, _("HTML : %s"));
195 #if wxUSE_STATUSBAR
196 m_Html->SetRelatedStatusBar(0);
197 #endif // wxUSE_STATUSBAR
198 m_Html->ReadCustomization(wxConfig::Get());
199 m_Html->LoadFile(wxFileName(wxT("test.htm")));
200 m_Html->AddProcessor(m_Processor);
201 }
202
203
204 // event handlers
205
206 void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
207 {
208 m_Html->WriteCustomization(wxConfig::Get());
209 delete wxConfig::Set(NULL);
210
211 // true is to force the frame to close
212 Close(true);
213 }
214
215 void MyFrame::OnPageOpen(wxCommandEvent& WXUNUSED(event))
216 {
217 wxString p = wxFileSelector(_("Open HTML document"), wxEmptyString,
218 wxEmptyString, wxEmptyString, wxT("HTML files|*.htm"));
219
220 if (p != wxEmptyString)
221 m_Html->LoadPage(p);
222 }
223
224 void MyFrame::OnBack(wxCommandEvent& WXUNUSED(event))
225 {
226 if (!m_Html->HistoryBack())
227 {
228 wxMessageBox(_("You reached prehistory era!"));
229 }
230 }
231
232 void MyFrame::OnForward(wxCommandEvent& WXUNUSED(event))
233 {
234 if (!m_Html->HistoryForward())
235 {
236 wxMessageBox(_("No more items in history!"));
237 }
238 }
239
240 void MyFrame::OnProcessor(wxCommandEvent& WXUNUSED(event))
241 {
242 m_Processor->Enable(!m_Processor->IsEnabled());
243 m_Html->LoadPage(m_Html->GetOpenedPage());
244 }