]>
git.saurik.com Git - wxWidgets.git/blob - samples/html/test/test.cpp
1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxHtml testing example
4 /////////////////////////////////////////////////////////////////////////////
6 #if defined(__GNUG__) && !defined(__APPLE__)
7 #pragma implementation "test.cpp"
8 #pragma interface "test.cpp"
11 // For compilers that support precompilation, includes "wx/wx.h".
12 #include "wx/wxprec.h"
18 // for all others, include the necessary headers (this file is usually all you
19 // need because it includes almost all "standard" wxWindows headers
25 #include "wx/sysopt.h"
26 #include "wx/html/htmlwin.h"
27 #include "wx/html/htmlproc.h"
28 #include "wx/fs_inet.h"
29 #include "wx/filedlg.h"
31 // ----------------------------------------------------------------------------
33 // ----------------------------------------------------------------------------
35 // Define a new application type, each program should derive a class from wxApp
36 class MyApp
: public wxApp
39 // override base class virtuals
40 // ----------------------------
42 // this one is called on application startup and is a good place for the app
43 // initialization (doing it here and not in the ctor allows to have an error
44 // return: if OnInit() returns false, the application terminates)
45 virtual bool OnInit();
48 // Define a new frame type: this is going to be our main frame
49 class MyFrame
: public wxFrame
53 MyFrame(const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
);
55 // event handlers (these functions should _not_ be virtual)
56 void OnQuit(wxCommandEvent
& event
);
57 void OnPageOpen(wxCommandEvent
& event
);
58 void OnBack(wxCommandEvent
& event
);
59 void OnForward(wxCommandEvent
& event
);
60 void OnProcessor(wxCommandEvent
& event
);
64 wxHtmlProcessor
*m_Processor
;
65 // any class wishing to process wxWindows events must use this macro
70 class BoldProcessor
: public wxHtmlProcessor
73 virtual wxString
Process(const wxString
& s
) const
76 r
.Replace(wxT("<b>"), wxEmptyString
);
77 r
.Replace(wxT("<B>"), wxEmptyString
);
78 r
.Replace(wxT("</b>"), wxEmptyString
);
79 r
.Replace(wxT("</B>"), wxEmptyString
);
84 // ----------------------------------------------------------------------------
86 // ----------------------------------------------------------------------------
88 // IDs for the controls and the menu commands
98 // controls start here (the numbers are, of course, arbitrary)
102 // ----------------------------------------------------------------------------
103 // event tables and other macros for wxWindows
104 // ----------------------------------------------------------------------------
106 // the event tables connect the wxWindows events with the functions (event
107 // handlers) which process them. It can be also done at run-time, but for the
108 // simple menu events like this the static method is much simpler.
109 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
110 EVT_MENU(Minimal_Quit
, MyFrame::OnQuit
)
111 EVT_MENU(Minimal_PageOpen
, MyFrame::OnPageOpen
)
112 EVT_MENU(Minimal_Back
, MyFrame::OnBack
)
113 EVT_MENU(Minimal_Forward
, MyFrame::OnForward
)
114 EVT_MENU(Minimal_Processor
, MyFrame::OnProcessor
)
117 // Create a new application object: this macro will allow wxWindows to create
118 // the application object during program execution (it's better than using a
119 // static object for many reasons) and also declares the accessor function
120 // wxGetApp() which will return the reference of the right type (i.e. MyApp and
124 // ============================================================================
126 // ============================================================================
128 // ----------------------------------------------------------------------------
129 // the application class
130 // ----------------------------------------------------------------------------
131 // `Main program' equivalent: the program execution "starts" here
134 #if wxUSE_SYSTEM_OPTIONS
135 wxSystemOptions::SetOption(wxT("no-maskblt"), 1);
138 wxInitAllImageHandlers();
139 #if wxUSE_FS_INET && wxUSE_STREAMS && wxUSE_SOCKETS
140 wxFileSystem::AddHandler(new wxInternetFSHandler
);
143 SetVendorName(wxT("wxWindows"));
144 SetAppName(wxT("wxHtmlTest"));
145 // the following call to wxConfig::Get will use it to create an object...
147 // Create the main application window
148 MyFrame
*frame
= new MyFrame(_("wxHtmlWindow testing application"),
149 wxPoint(50, 50), wxSize(640, 480));
151 // Show it and tell the application that it's our main window
152 // @@@ what does it do exactly, in fact? is it necessary here?
157 // success: wxApp::OnRun() will be called which will enter the main message
158 // loop and the application will run. If we returned FALSE here, the
159 // application would exit immediately.
163 // ----------------------------------------------------------------------------
165 // ----------------------------------------------------------------------------
169 MyFrame::MyFrame(const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
)
170 : wxFrame((wxFrame
*)NULL
, -1, title
, pos
, size
, wxDEFAULT_FRAME_STYLE
,
171 wxT("html_test_app"))
174 wxMenu
*menuFile
= new wxMenu
;
175 wxMenu
*menuNav
= new wxMenu
;
177 menuFile
->Append(Minimal_PageOpen
, _("&Open HTML page..."));
178 menuFile
->AppendSeparator();
179 menuFile
->Append(Minimal_Processor
, _("&Remove bold attribute"), wxT(""), TRUE
);
181 menuFile
->AppendSeparator();
182 menuFile
->Append(Minimal_Quit
, _("&Close frame"));
183 menuNav
->Append(Minimal_Back
, _("Go &BACK"));
184 menuNav
->Append(Minimal_Forward
, _("Go &FORWARD"));
186 // now append the freshly created menu to the menu bar...
187 wxMenuBar
*menuBar
= new wxMenuBar
;
188 menuBar
->Append(menuFile
, _("&File"));
189 menuBar
->Append(menuNav
, _("&Navigate"));
191 // ... and attach this menu bar to the frame
196 m_Processor
= new BoldProcessor
;
197 m_Processor
->Enable(FALSE
);
198 m_Html
= new wxHtmlWindow(this);
199 m_Html
->SetRelatedFrame(this, _("HTML : %s"));
200 m_Html
->SetRelatedStatusBar(0);
201 m_Html
->ReadCustomization(wxConfig::Get());
202 m_Html
->LoadFile(wxFileName(wxT("test.htm")));
203 m_Html
->AddProcessor(m_Processor
);
209 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
211 // TRUE is to force the frame to close
212 m_Html
-> WriteCustomization(wxConfig::Get());
213 delete wxConfig::Set(NULL
);
217 void MyFrame::OnPageOpen(wxCommandEvent
& WXUNUSED(event
))
219 wxString p
= wxFileSelector(_("Open HTML document"), wxT(""), wxT(""), wxT(""), wxT("HTML files|*.htm"));
220 if (p
!= wxEmptyString
)
221 m_Html
-> LoadPage(p
);
224 void MyFrame::OnBack(wxCommandEvent
& WXUNUSED(event
))
226 if (!m_Html
-> HistoryBack()) wxMessageBox(_("You reached prehistory era!"));
229 void MyFrame::OnForward(wxCommandEvent
& WXUNUSED(event
))
231 if (!m_Html
-> HistoryForward()) wxMessageBox(_("No more items in history!"));
234 void MyFrame::OnProcessor(wxCommandEvent
& WXUNUSED(event
))
236 m_Processor
->Enable(!m_Processor
->IsEnabled());
237 m_Html
->LoadPage(m_Html
->GetOpenedPage());