]>
Commit | Line | Data |
---|---|---|
1 | ///////////////////////////////////////////////////////////////////////////// | |
2 | // Name: htmlctrl.cpp | |
3 | // Purpose: HtmlCtrl sample | |
4 | // Author: Julian Smart / Kevin Ollivier | |
5 | // Modified by: | |
6 | // Created: 04/16/2004 | |
7 | // Copyright: (c) Julian Smart / Kevin Ollivier | |
8 | // Licence: wxWindows licence | |
9 | ///////////////////////////////////////////////////////////////////////////// | |
10 | ||
11 | // ============================================================================ | |
12 | // declarations | |
13 | // ============================================================================ | |
14 | ||
15 | // ---------------------------------------------------------------------------- | |
16 | // headers | |
17 | // ---------------------------------------------------------------------------- | |
18 | ||
19 | // For compilers that support precompilation, includes "wx/wx.h". | |
20 | #include "wx/wxprec.h" | |
21 | ||
22 | #ifdef __BORLANDC__ | |
23 | #pragma hdrstop | |
24 | #endif | |
25 | ||
26 | // for all others, include the necessary headers (this file is usually all you | |
27 | // need because it includes almost all "standard" wxWidgets headers) | |
28 | #ifndef WX_PRECOMP | |
29 | #include "wx/wx.h" | |
30 | #endif | |
31 | ||
32 | #include "wx/html/webkit.h" | |
33 | ||
34 | #ifndef wxHAS_IMAGES_IN_RESOURCES | |
35 | #include "../../sample.xpm" | |
36 | #endif | |
37 | ||
38 | // ---------------------------------------------------------------------------- | |
39 | // resources | |
40 | // ---------------------------------------------------------------------------- | |
41 | ||
42 | enum { | |
43 | ID_BACK = wxID_HIGHEST + 1, | |
44 | ID_NEXT = wxID_HIGHEST + 2, | |
45 | ID_RELOAD = wxID_HIGHEST + 3, | |
46 | ID_URLLIST = wxID_HIGHEST + 4, | |
47 | ID_STOP = wxID_HIGHEST + 5, | |
48 | ID_WEBKIT = wxID_HIGHEST + 6, | |
49 | ID_VIEW_SOURCE = wxID_HIGHEST + 7, | |
50 | ID_OPEN = wxID_HIGHEST + 8, | |
51 | ID_SAVE = wxID_HIGHEST + 9, | |
52 | ID_SET_SOURCE = wxID_HIGHEST + 10 | |
53 | }; | |
54 | ||
55 | // ---------------------------------------------------------------------------- | |
56 | // private classes | |
57 | // ---------------------------------------------------------------------------- | |
58 | ||
59 | // Define a new application type, each program should derive a class from wxApp | |
60 | class MyApp : public wxApp | |
61 | { | |
62 | public: | |
63 | // override base class virtuals | |
64 | // ---------------------------- | |
65 | ||
66 | // this one is called on application startup and is a good place for the app | |
67 | // initialization (doing it here and not in the ctor allows to have an error | |
68 | // return: if OnInit() returns false, the application terminates) | |
69 | virtual bool OnInit(); | |
70 | }; | |
71 | ||
72 | // Define a new frame type: this is going to be our main frame | |
73 | class MyFrame : public wxFrame | |
74 | { | |
75 | public: | |
76 | // ctor(s) | |
77 | MyFrame(const wxString& title); | |
78 | void OnBackButton(wxCommandEvent& myEvent); | |
79 | void OnNextButton(wxCommandEvent& myEvent); | |
80 | void OnURLEnter(wxCommandEvent& myEvent); | |
81 | void OnStopButton(wxCommandEvent& myEvent); | |
82 | void OnReloadButton(wxCommandEvent& myEvent); | |
83 | void OnViewSource(wxCommandEvent& myEvent); | |
84 | void OnSetSource(wxCommandEvent& myEvent); | |
85 | void OnStateChanged(wxWebKitStateChangedEvent& myEvent); | |
86 | wxWebKitCtrl* mySafari; | |
87 | wxTextCtrl* urlText; | |
88 | private: | |
89 | // any class wishing to process wxWidgets events must use this macro | |
90 | DECLARE_EVENT_TABLE() | |
91 | }; | |
92 | ||
93 | // ---------------------------------------------------------------------------- | |
94 | // event tables and other macros for wxWidgets | |
95 | // ---------------------------------------------------------------------------- | |
96 | ||
97 | // the event tables connect the wxWidgets events with the functions (event | |
98 | // handlers) which process them. It can be also done at run-time, but for the | |
99 | // simple menu events like this the static method is much simpler. | |
100 | BEGIN_EVENT_TABLE(MyFrame, wxFrame) | |
101 | EVT_BUTTON(ID_BACK, MyFrame::OnBackButton) | |
102 | EVT_BUTTON(ID_NEXT, MyFrame::OnNextButton) | |
103 | EVT_BUTTON(ID_STOP, MyFrame::OnStopButton) | |
104 | EVT_BUTTON(ID_RELOAD, MyFrame::OnReloadButton) | |
105 | EVT_MENU(ID_VIEW_SOURCE, MyFrame::OnViewSource) | |
106 | EVT_MENU(ID_SET_SOURCE, MyFrame::OnSetSource) | |
107 | EVT_TEXT_ENTER(ID_URLLIST, MyFrame::OnURLEnter) | |
108 | EVT_WEBKIT_STATE_CHANGED(MyFrame::OnStateChanged) | |
109 | //EVT_MENU(Minimal_Quit, MyFrame::OnQuit) | |
110 | //EVT_MENU(Minimal_About, MyFrame::OnAbout) | |
111 | END_EVENT_TABLE() | |
112 | ||
113 | // Create a new application object: this macro will allow wxWidgets to create | |
114 | // the application object during program execution (it's better than using a | |
115 | // static object for many reasons) and also implements the accessor function | |
116 | // wxGetApp() which will return the reference of the right type (i.e. MyApp and | |
117 | // not wxApp) | |
118 | IMPLEMENT_APP(MyApp) | |
119 | ||
120 | // ============================================================================ | |
121 | // implementation | |
122 | // ============================================================================ | |
123 | ||
124 | // ---------------------------------------------------------------------------- | |
125 | // the application class | |
126 | // ---------------------------------------------------------------------------- | |
127 | ||
128 | // 'Main program' equivalent: the program execution "starts" here | |
129 | bool MyApp::OnInit() | |
130 | { | |
131 | if ( !wxApp::OnInit() ) | |
132 | return false; | |
133 | ||
134 | // create the main application window | |
135 | MyFrame *frame = new MyFrame(wxT("wxWebKit Sample")); | |
136 | ||
137 | // and show it (the frames, unlike simple controls, are not shown when | |
138 | // created initially) | |
139 | frame->Show(true); | |
140 | ||
141 | // success: wxApp::OnRun() will be called which will enter the main message | |
142 | // loop and the application will run. If we returned false here, the | |
143 | // application would exit immediately. | |
144 | return true; | |
145 | } | |
146 | ||
147 | // ---------------------------------------------------------------------------- | |
148 | // main frame | |
149 | // ---------------------------------------------------------------------------- | |
150 | ||
151 | // frame constructor | |
152 | MyFrame::MyFrame(const wxString& title) | |
153 | : wxFrame(NULL, wxID_ANY, title, wxDefaultPosition, wxSize(500,500)) | |
154 | { | |
155 | SetIcon(wxICON(sample)); | |
156 | ||
157 | wxMenuBar* myBar = new wxMenuBar(); | |
158 | wxMenu* fileMenu = new wxMenu; | |
159 | fileMenu->Append(ID_OPEN, _("&Open")); | |
160 | fileMenu->Append(ID_SAVE, _("&Save")); | |
161 | myBar->Append(fileMenu, _("&File")); | |
162 | ||
163 | wxMenu* editMenu = new wxMenu; | |
164 | editMenu->Append(ID_SET_SOURCE, _("Set Page Source")); | |
165 | myBar->Append(editMenu, _("&Edit")); | |
166 | ||
167 | //wxMenu* viewMenu = new wxMenu(_("View")); | |
168 | //viewMenu->Append(ID_VIEW_SOURCE, _("View Source")); | |
169 | //myBar->Append(viewMenu, _("View")); | |
170 | ||
171 | SetMenuBar(myBar); | |
172 | ||
173 | wxToolBar* myToolbar = CreateToolBar(); | |
174 | wxButton* btnBack = new wxButton(myToolbar, ID_BACK, _("Back")); | |
175 | myToolbar->AddControl(btnBack); | |
176 | myToolbar->AddSeparator(); | |
177 | wxButton* btnNext = new wxButton(myToolbar, ID_NEXT, _("Next")); | |
178 | myToolbar->AddControl(btnNext); | |
179 | myToolbar->AddSeparator(); | |
180 | wxButton* btnStop = new wxButton(myToolbar, ID_STOP, _("Stop")); | |
181 | myToolbar->AddControl(btnStop); | |
182 | myToolbar->AddSeparator(); | |
183 | wxButton* btnReload = new wxButton(myToolbar, ID_RELOAD, _("Reload")); | |
184 | myToolbar->AddControl(btnReload); | |
185 | myToolbar->AddSeparator(); | |
186 | urlText = new wxTextCtrl(myToolbar, ID_URLLIST, wxT("http://www.wxwidgets.org"), wxDefaultPosition, wxSize(220, -1), wxTE_PROCESS_ENTER); | |
187 | myToolbar->AddControl(urlText); | |
188 | myToolbar->AddSeparator(); | |
189 | myToolbar->Realize(); | |
190 | ||
191 | // Testing wxWebKitCtrl inside a panel | |
192 | #if 1 | |
193 | wxPanel* panel = new wxPanel(this, wxID_ANY); | |
194 | ||
195 | wxBoxSizer* boxSizer = new wxBoxSizer(wxVERTICAL); | |
196 | panel->SetSizer(boxSizer); | |
197 | ||
198 | mySafari = new wxWebKitCtrl(panel, ID_WEBKIT, wxT("http://www.wxwidgets.org"), wxDefaultPosition, wxSize(200, 200)); | |
199 | ||
200 | boxSizer->Add(mySafari, 1, wxEXPAND); | |
201 | ||
202 | wxBoxSizer* frameSizer = new wxBoxSizer(wxVERTICAL); | |
203 | SetSizer(frameSizer); | |
204 | frameSizer->Add(panel, 1, wxEXPAND); | |
205 | #else | |
206 | mySafari = new wxWebKitCtrl(this, ID_WEBKIT, wxT("http://www.wxwidgets.org"), wxDefaultPosition, wxSize(200, 200)); | |
207 | #endif | |
208 | ||
209 | #if wxUSE_STATUSBAR | |
210 | CreateStatusBar(2); | |
211 | #endif // wxUSE_STATUSBAR | |
212 | } | |
213 | ||
214 | void MyFrame::OnBackButton(wxCommandEvent& WXUNUSED(myEvent)) | |
215 | { | |
216 | if (mySafari->CanGoBack()) | |
217 | mySafari->GoBack(); | |
218 | } | |
219 | ||
220 | void MyFrame::OnNextButton(wxCommandEvent& WXUNUSED(myEvent)) | |
221 | { | |
222 | if (mySafari->CanGoForward()) | |
223 | mySafari->GoForward(); | |
224 | } | |
225 | ||
226 | void MyFrame::OnStopButton(wxCommandEvent& WXUNUSED(myEvent)) | |
227 | { | |
228 | mySafari->Stop(); | |
229 | } | |
230 | ||
231 | void MyFrame::OnReloadButton(wxCommandEvent& WXUNUSED(myEvent)) | |
232 | { | |
233 | mySafari->Reload(); | |
234 | } | |
235 | ||
236 | void MyFrame::OnURLEnter(wxCommandEvent& WXUNUSED(myEvent)) | |
237 | { | |
238 | mySafari->LoadURL(urlText->GetValue()); | |
239 | } | |
240 | ||
241 | void MyFrame::OnStateChanged(wxWebKitStateChangedEvent& myEvent) | |
242 | { | |
243 | if (GetStatusBar() != NULL) | |
244 | { | |
245 | if (myEvent.GetState() == wxWEBKIT_STATE_NEGOTIATING) | |
246 | { | |
247 | GetStatusBar()->SetStatusText(_("Contacting ") + myEvent.GetURL()); | |
248 | urlText->SetValue(myEvent.GetURL()); | |
249 | } | |
250 | else if (myEvent.GetState() == wxWEBKIT_STATE_TRANSFERRING) | |
251 | { | |
252 | GetStatusBar()->SetStatusText(_("Loading ") + myEvent.GetURL()); | |
253 | } | |
254 | else if (myEvent.GetState() == wxWEBKIT_STATE_STOP) | |
255 | { | |
256 | GetStatusBar()->SetStatusText(_("Load complete.")); | |
257 | SetTitle(mySafari->GetTitle()); | |
258 | } | |
259 | else if (myEvent.GetState() == wxWEBKIT_STATE_FAILED) | |
260 | { | |
261 | GetStatusBar()->SetStatusText(_("Failed to load ") + myEvent.GetURL()); | |
262 | } | |
263 | } | |
264 | ||
265 | } | |
266 | ||
267 | void MyFrame::OnViewSource(wxCommandEvent& WXUNUSED(myEvent)) | |
268 | { | |
269 | if (mySafari->CanGetPageSource()) | |
270 | wxMessageBox(mySafari->GetPageSource()); | |
271 | } | |
272 | ||
273 | void MyFrame::OnSetSource(wxCommandEvent& WXUNUSED(myEvent)) | |
274 | { | |
275 | if (mySafari) | |
276 | { | |
277 | wxString myText = wxT("<HTML><HEAD></HEAD><BODY><P>Hello world!</P></BODY></HTML>"); | |
278 | mySafari->SetPageSource(myText); | |
279 | } | |
280 | } |