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