]>
git.saurik.com Git - wxWidgets.git/blob - samples/html/test/test.cpp
1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxHtml testing example
4 /////////////////////////////////////////////////////////////////////////////
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/html/htmlwin.h>
26 #include <wx/html/htmlproc.h>
27 #include <wx/fs_inet.h>
29 // ----------------------------------------------------------------------------
31 // ----------------------------------------------------------------------------
33 // Define a new application type, each program should derive a class from wxApp
34 class MyApp
: public wxApp
37 // override base class virtuals
38 // ----------------------------
40 // this one is called on application startup and is a good place for the app
41 // initialization (doing it here and not in the ctor allows to have an error
42 // return: if OnInit() returns false, the application terminates)
43 virtual bool OnInit();
46 // Define a new frame type: this is going to be our main frame
47 class MyFrame
: public wxFrame
51 MyFrame(const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
);
53 // event handlers (these functions should _not_ be virtual)
54 void OnQuit(wxCommandEvent
& event
);
55 void OnPageOpen(wxCommandEvent
& event
);
56 void OnBack(wxCommandEvent
& event
);
57 void OnForward(wxCommandEvent
& event
);
58 void OnProcessor(wxCommandEvent
& event
);
62 wxHtmlProcessor
*m_Processor
;
63 // any class wishing to process wxWindows events must use this macro
68 class BoldProcessor
: public wxHtmlProcessor
71 virtual wxString
Process(const wxString
& s
) const
74 r
.Replace(wxT("<b>"), wxEmptyString
);
75 r
.Replace(wxT("<B>"), wxEmptyString
);
76 r
.Replace(wxT("</b>"), wxEmptyString
);
77 r
.Replace(wxT("</B>"), wxEmptyString
);
83 // ----------------------------------------------------------------------------
85 // ----------------------------------------------------------------------------
87 // IDs for the controls and the menu commands
97 // controls start here (the numbers are, of course, arbitrary)
101 // ----------------------------------------------------------------------------
102 // event tables and other macros for wxWindows
103 // ----------------------------------------------------------------------------
105 // the event tables connect the wxWindows events with the functions (event
106 // handlers) which process them. It can be also done at run-time, but for the
107 // simple menu events like this the static method is much simpler.
108 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
109 EVT_MENU(Minimal_Quit
, MyFrame::OnQuit
)
110 EVT_MENU(Minimal_PageOpen
, MyFrame::OnPageOpen
)
111 EVT_MENU(Minimal_Back
, MyFrame::OnBack
)
112 EVT_MENU(Minimal_Forward
, MyFrame::OnForward
)
113 EVT_MENU(Minimal_Processor
, MyFrame::OnProcessor
)
116 // Create a new application object: this macro will allow wxWindows to create
117 // the application object during program execution (it's better than using a
118 // static object for many reasons) and also declares the accessor function
119 // wxGetApp() which will return the reference of the right type (i.e. MyApp and
123 // ============================================================================
125 // ============================================================================
127 // ----------------------------------------------------------------------------
128 // the application class
129 // ----------------------------------------------------------------------------
130 // `Main program' equivalent: the program execution "starts" here
133 wxInitAllImageHandlers();
134 #if wxUSE_FS_INET && wxUSE_STREAMS && wxUSE_SOCKETS
135 wxFileSystem::AddHandler(new wxInternetFSHandler
);
138 SetVendorName("wxWindows");
139 SetAppName("wxHtmlTest");
140 // the following call to wxConfig::Get will use it to create an object...
142 // Create the main application window
143 MyFrame
*frame
= new MyFrame("wxHtmlWindow testing application",
144 wxPoint(50, 50), wxSize(640, 480));
146 // Show it and tell the application that it's our main window
147 // @@@ what does it do exactly, in fact? is it necessary here?
152 // success: wxApp::OnRun() will be called which will enter the main message
153 // loop and the application will run. If we returned FALSE here, the
154 // application would exit immediately.
158 // ----------------------------------------------------------------------------
160 // ----------------------------------------------------------------------------
164 MyFrame::MyFrame(const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
)
165 : wxFrame((wxFrame
*)NULL
, -1, title
, pos
, size
, wxDEFAULT_FRAME_STYLE
, "html_test_app")
168 wxMenu
*menuFile
= new wxMenu
;
169 wxMenu
*menuNav
= new wxMenu
;
171 menuFile
->Append(Minimal_PageOpen
, "&Open HTML page...");
172 menuFile
->AppendSeparator();
173 menuFile
->Append(Minimal_Processor
, "&Remove bold attribute", "", TRUE
);
175 menuFile
->AppendSeparator();
176 menuFile
->Append(Minimal_Quit
, "&Close frame");
177 menuNav
->Append(Minimal_Back
, "Go &BACK");
178 menuNav
->Append(Minimal_Forward
, "Go &FORWARD");
180 // now append the freshly created menu to the menu bar...
181 wxMenuBar
*menuBar
= new wxMenuBar
;
182 menuBar
->Append(menuFile
, "&File");
183 menuBar
->Append(menuNav
, "&Navigate");
185 // ... and attach this menu bar to the frame
190 m_Processor
= new BoldProcessor
;
191 m_Processor
->Enable(FALSE
);
192 m_Html
= new wxHtmlWindow(this);
193 m_Html
->SetRelatedFrame(this, "HTML : %s");
194 m_Html
->SetRelatedStatusBar(0);
195 m_Html
->ReadCustomization(wxConfig::Get());
196 m_Html
->LoadPage("test.htm");
197 m_Html
->AddProcessor(m_Processor
);
203 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
205 // TRUE is to force the frame to close
206 m_Html
-> WriteCustomization(wxConfig::Get());
207 delete wxConfig::Set(NULL
);
211 void MyFrame::OnPageOpen(wxCommandEvent
& WXUNUSED(event
))
213 wxString p
= wxFileSelector("Open HTML document", "", "", "", "HTML files|*.htm");
214 if (p
!= wxEmptyString
)
215 m_Html
-> LoadPage(p
);
218 void MyFrame::OnBack(wxCommandEvent
& WXUNUSED(event
))
220 if (!m_Html
-> HistoryBack()) wxMessageBox("You reached prehistory era!");
223 void MyFrame::OnForward(wxCommandEvent
& WXUNUSED(event
))
225 if (!m_Html
-> HistoryForward()) wxMessageBox("No more items in history!");
228 void MyFrame::OnProcessor(wxCommandEvent
& WXUNUSED(event
))
230 m_Processor
->Enable(!m_Processor
->IsEnabled());
231 m_Html
->LoadPage(m_Html
->GetOpenedPage());
232 printf("%i\n", m_Processor
->IsEnabled());