]>
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 | ||
39 | // the application icon (under Windows and OS/2 it is in resources and even | |
40 | // though we could still include the XPM here it would be unused) | |
41 | #if !defined(__WXMSW__) && !defined(__WXPM__) | |
42 | #include "../../sample.xpm" | |
43 | #endif | |
44 | ||
45 | enum { | |
46 | ID_BACK = wxID_HIGHEST + 1, | |
47 | ID_NEXT = wxID_HIGHEST + 2, | |
48 | ID_RELOAD = wxID_HIGHEST + 3, | |
49 | ID_URLLIST = wxID_HIGHEST + 4, | |
50 | ID_STOP = wxID_HIGHEST + 5, | |
51 | ID_WEBKIT = wxID_HIGHEST + 6, | |
52 | ID_VIEW_SOURCE = wxID_HIGHEST + 7, | |
53 | ID_OPEN = wxID_HIGHEST + 8, | |
54 | ID_SAVE = wxID_HIGHEST + 9, | |
55 | ID_SET_SOURCE = wxID_HIGHEST + 10 | |
56 | }; | |
57 | ||
58 | // ---------------------------------------------------------------------------- | |
59 | // private classes | |
60 | // ---------------------------------------------------------------------------- | |
61 | ||
62 | // Define a new application type, each program should derive a class from wxApp | |
63 | class MyApp : public wxApp | |
64 | { | |
65 | public: | |
66 | // override base class virtuals | |
67 | // ---------------------------- | |
68 | ||
69 | // this one is called on application startup and is a good place for the app | |
70 | // initialization (doing it here and not in the ctor allows to have an error | |
71 | // return: if OnInit() returns false, the application terminates) | |
72 | virtual bool OnInit(); | |
73 | }; | |
74 | ||
75 | // Define a new frame type: this is going to be our main frame | |
76 | class MyFrame : public wxFrame | |
77 | { | |
78 | public: | |
79 | // ctor(s) | |
80 | MyFrame(const wxString& title); | |
81 | void OnBackButton(wxCommandEvent& myEvent); | |
82 | void OnNextButton(wxCommandEvent& myEvent); | |
83 | void OnURLEnter(wxCommandEvent& myEvent); | |
84 | void OnStopButton(wxCommandEvent& myEvent); | |
85 | void OnReloadButton(wxCommandEvent& myEvent); | |
86 | void OnViewSource(wxCommandEvent& myEvent); | |
87 | void OnSetSource(wxCommandEvent& myEvent); | |
88 | void OnStateChanged(wxWebKitStateChangedEvent& myEvent); | |
bd1018b9 | 89 | wxWebKitCtrl* mySafari; |
2c990ba6 KO |
90 | wxTextCtrl* urlText; |
91 | private: | |
be5a51fb | 92 | // any class wishing to process wxWidgets events must use this macro |
2c990ba6 KO |
93 | DECLARE_EVENT_TABLE() |
94 | }; | |
95 | ||
96 | // ---------------------------------------------------------------------------- | |
be5a51fb | 97 | // event tables and other macros for wxWidgets |
2c990ba6 KO |
98 | // ---------------------------------------------------------------------------- |
99 | ||
be5a51fb | 100 | // the event tables connect the wxWidgets events with the functions (event |
2c990ba6 KO |
101 | // handlers) which process them. It can be also done at run-time, but for the |
102 | // simple menu events like this the static method is much simpler. | |
103 | BEGIN_EVENT_TABLE(MyFrame, wxFrame) | |
104 | EVT_BUTTON(ID_BACK, MyFrame::OnBackButton) | |
105 | EVT_BUTTON(ID_NEXT, MyFrame::OnNextButton) | |
106 | EVT_BUTTON(ID_STOP, MyFrame::OnStopButton) | |
107 | EVT_BUTTON(ID_RELOAD, MyFrame::OnReloadButton) | |
108 | EVT_MENU(ID_VIEW_SOURCE, MyFrame::OnViewSource) | |
109 | EVT_MENU(ID_SET_SOURCE, MyFrame::OnSetSource) | |
110 | EVT_TEXT_ENTER(ID_URLLIST, MyFrame::OnURLEnter) | |
111 | EVT_WEBKIT_STATE_CHANGED(MyFrame::OnStateChanged) | |
112 | //EVT_MENU(Minimal_Quit, MyFrame::OnQuit) | |
113 | //EVT_MENU(Minimal_About, MyFrame::OnAbout) | |
114 | END_EVENT_TABLE() | |
115 | ||
be5a51fb | 116 | // Create a new application object: this macro will allow wxWidgets to create |
2c990ba6 KO |
117 | // the application object during program execution (it's better than using a |
118 | // static object for many reasons) and also implements the accessor function | |
119 | // wxGetApp() which will return the reference of the right type (i.e. MyApp and | |
120 | // not wxApp) | |
121 | IMPLEMENT_APP(MyApp) | |
122 | ||
123 | // ============================================================================ | |
124 | // implementation | |
125 | // ============================================================================ | |
126 | ||
127 | // ---------------------------------------------------------------------------- | |
128 | // the application class | |
129 | // ---------------------------------------------------------------------------- | |
130 | ||
131 | // 'Main program' equivalent: the program execution "starts" here | |
132 | bool MyApp::OnInit() | |
133 | { | |
134 | // create the main application window | |
135 | MyFrame *frame = new MyFrame(_T("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 | wxMenuBar* myBar = new wxMenuBar(); | |
1b0f5e58 JS |
156 | wxMenu* fileMenu = new wxMenu; |
157 | fileMenu->Append(ID_OPEN, _("&Open")); | |
158 | fileMenu->Append(ID_SAVE, _("&Save")); | |
159 | myBar->Append(fileMenu, _("&File")); | |
2c990ba6 | 160 | |
1b0f5e58 | 161 | wxMenu* editMenu = new wxMenu; |
2c990ba6 | 162 | editMenu->Append(ID_SET_SOURCE, _("Set Page Source")); |
1b0f5e58 | 163 | myBar->Append(editMenu, _("&Edit")); |
2c990ba6 KO |
164 | |
165 | //wxMenu* viewMenu = new wxMenu(_("View")); | |
166 | //viewMenu->Append(ID_VIEW_SOURCE, _("View Source")); | |
167 | //myBar->Append(viewMenu, _("View")); | |
168 | ||
169 | SetMenuBar(myBar); | |
170 | ||
171 | wxToolBar* myToolbar = CreateToolBar(); | |
172 | wxButton* btnBack = new wxButton(myToolbar, ID_BACK, _("Back")); | |
173 | myToolbar->AddControl(btnBack); | |
174 | myToolbar->AddSeparator(); | |
175 | wxButton* btnNext = new wxButton(myToolbar, ID_NEXT, _("Next")); | |
176 | myToolbar->AddControl(btnNext); | |
177 | myToolbar->AddSeparator(); | |
178 | wxButton* btnStop = new wxButton(myToolbar, ID_STOP, _("Stop")); | |
179 | myToolbar->AddControl(btnStop); | |
180 | myToolbar->AddSeparator(); | |
181 | wxButton* btnReload = new wxButton(myToolbar, ID_RELOAD, _("Reload")); | |
182 | myToolbar->AddControl(btnReload); | |
183 | myToolbar->AddSeparator(); | |
184 | urlText = new wxTextCtrl(myToolbar, ID_URLLIST, _T("http://www.wxwidgets.org"), wxDefaultPosition, wxSize(220, -1), wxTE_PROCESS_ENTER); | |
185 | myToolbar->AddControl(urlText); | |
186 | myToolbar->AddSeparator(); | |
187 | myToolbar->Realize(); | |
188 | ||
bd1018b9 | 189 | mySafari = new wxWebKitCtrl(this, ID_WEBKIT, _T("http://www.wxwidgets.org"), wxDefaultPosition, wxSize(200, 200)); |
2c990ba6 KO |
190 | |
191 | #if wxUSE_STATUSBAR | |
192 | CreateStatusBar(2); | |
193 | #endif // wxUSE_STATUSBAR | |
194 | } | |
195 | ||
196 | void MyFrame::OnBackButton(wxCommandEvent& myEvent){ | |
197 | if (mySafari->CanGoBack()) | |
198 | mySafari->GoBack(); | |
199 | } | |
200 | ||
201 | void MyFrame::OnNextButton(wxCommandEvent& myEvent){ | |
202 | if (mySafari->CanGoForward()) | |
203 | mySafari->GoForward(); | |
204 | } | |
205 | ||
206 | void MyFrame::OnStopButton(wxCommandEvent& myEvent){ | |
207 | mySafari->Stop(); | |
208 | } | |
209 | ||
210 | void MyFrame::OnReloadButton(wxCommandEvent& myEvent){ | |
211 | mySafari->Reload(); | |
212 | } | |
213 | ||
214 | void MyFrame::OnURLEnter(wxCommandEvent& myEvent){ | |
215 | mySafari->LoadURL(urlText->GetValue()); | |
216 | } | |
217 | ||
218 | void MyFrame::OnStateChanged(wxWebKitStateChangedEvent& myEvent){ | |
219 | if (GetStatusBar() != NULL){ | |
220 | if (myEvent.GetState() == wxWEBKIT_STATE_NEGOTIATING){ | |
221 | GetStatusBar()->SetStatusText(_("Contacting ") + myEvent.GetURL()); | |
222 | } | |
223 | else if (myEvent.GetState() == wxWEBKIT_STATE_TRANSFERRING){ | |
224 | GetStatusBar()->SetStatusText(_("Loading ") + myEvent.GetURL()); | |
225 | } | |
226 | else if (myEvent.GetState() == wxWEBKIT_STATE_STOP){ | |
227 | GetStatusBar()->SetStatusText(_("Load complete.")); | |
228 | SetTitle(mySafari->GetTitle()); | |
229 | } | |
230 | else if (myEvent.GetState() == wxWEBKIT_STATE_FAILED){ | |
231 | GetStatusBar()->SetStatusText(_("Failed to load ") + myEvent.GetURL()); | |
232 | } | |
233 | } | |
234 | ||
235 | } | |
236 | ||
237 | void MyFrame::OnViewSource(wxCommandEvent& myEvent){ | |
238 | if (mySafari->CanGetPageSource()) | |
239 | wxMessageBox(mySafari->GetPageSource()); | |
240 | } | |
241 | ||
242 | void MyFrame::OnSetSource(wxCommandEvent& myEvent){ | |
243 | if (mySafari){ | |
bd1018b9 | 244 | wxString myText = wxT("<HTML><HEAD></HEAD><BODY><P>Hello world!</P></BODY></HTML>"); |
2c990ba6 KO |
245 | mySafari->SetPageSource(myText); |
246 | } | |
247 | } |