]> git.saurik.com Git - wxWidgets.git/blame - samples/html/htmlctrl/htmlctrl.cpp
wxMessageBox off the main thread lost result code.
[wxWidgets.git] / samples / html / htmlctrl / htmlctrl.cpp
CommitLineData
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
2c990ba6
KO
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
be5a51fb 27// need because it includes almost all "standard" wxWidgets headers)
2c990ba6
KO
28#ifndef WX_PRECOMP
29 #include "wx/wx.h"
30#endif
31
bc4b8943 32#include "wx/html/webkit.h"
2c990ba6 33
e7092398 34#ifndef wxHAS_IMAGES_IN_RESOURCES
197ab43d
FM
35 #include "../../sample.xpm"
36#endif
37
2c990ba6
KO
38// ----------------------------------------------------------------------------
39// resources
40// ----------------------------------------------------------------------------
41
2c990ba6
KO
42enum {
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
197ab43d 53};
2c990ba6
KO
54
55// ----------------------------------------------------------------------------
56// private classes
57// ----------------------------------------------------------------------------
58
59// Define a new application type, each program should derive a class from wxApp
60class MyApp : public wxApp
61{
62public:
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
73class MyFrame : public wxFrame
74{
75public:
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);
bd1018b9 86 wxWebKitCtrl* mySafari;
2c990ba6
KO
87 wxTextCtrl* urlText;
88private:
be5a51fb 89 // any class wishing to process wxWidgets events must use this macro
2c990ba6
KO
90 DECLARE_EVENT_TABLE()
91};
92
93// ----------------------------------------------------------------------------
be5a51fb 94// event tables and other macros for wxWidgets
2c990ba6
KO
95// ----------------------------------------------------------------------------
96
be5a51fb 97// the event tables connect the wxWidgets events with the functions (event
2c990ba6
KO
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.
100BEGIN_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)
111END_EVENT_TABLE()
112
be5a51fb 113// Create a new application object: this macro will allow wxWidgets to create
2c990ba6
KO
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)
118IMPLEMENT_APP(MyApp)
119
120// ============================================================================
121// implementation
122// ============================================================================
123
124// ----------------------------------------------------------------------------
125// the application class
126// ----------------------------------------------------------------------------
127
128// 'Main program' equivalent: the program execution "starts" here
129bool MyApp::OnInit()
130{
45e6e6f8
VZ
131 if ( !wxApp::OnInit() )
132 return false;
133
2c990ba6 134 // create the main application window
9a83f860 135 MyFrame *frame = new MyFrame(wxT("wxWebKit Sample"));
2c990ba6
KO
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
152MyFrame::MyFrame(const wxString& title)
153 : wxFrame(NULL, wxID_ANY, title, wxDefaultPosition, wxSize(500,500))
154{
197ab43d
FM
155 SetIcon(wxICON(sample));
156
2c990ba6 157 wxMenuBar* myBar = new wxMenuBar();
1b0f5e58
JS
158 wxMenu* fileMenu = new wxMenu;
159 fileMenu->Append(ID_OPEN, _("&Open"));
160 fileMenu->Append(ID_SAVE, _("&Save"));
161 myBar->Append(fileMenu, _("&File"));
197ab43d 162
1b0f5e58 163 wxMenu* editMenu = new wxMenu;
2c990ba6 164 editMenu->Append(ID_SET_SOURCE, _("Set Page Source"));
1b0f5e58 165 myBar->Append(editMenu, _("&Edit"));
197ab43d 166
2c990ba6
KO
167 //wxMenu* viewMenu = new wxMenu(_("View"));
168 //viewMenu->Append(ID_VIEW_SOURCE, _("View Source"));
169 //myBar->Append(viewMenu, _("View"));
197ab43d 170
2c990ba6
KO
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();
9a83f860 186 urlText = new wxTextCtrl(myToolbar, ID_URLLIST, wxT("http://www.wxwidgets.org"), wxDefaultPosition, wxSize(220, -1), wxTE_PROCESS_ENTER);
2c990ba6
KO
187 myToolbar->AddControl(urlText);
188 myToolbar->AddSeparator();
189 myToolbar->Realize();
190
19ff0b2c
JS
191 // Testing wxWebKitCtrl inside a panel
192#if 1
0afdfcac 193 wxPanel* panel = new wxPanel(this, wxID_ANY);
19ff0b2c
JS
194
195 wxBoxSizer* boxSizer = new wxBoxSizer(wxVERTICAL);
196 panel->SetSizer(boxSizer);
197
9a83f860 198 mySafari = new wxWebKitCtrl(panel, ID_WEBKIT, wxT("http://www.wxwidgets.org"), wxDefaultPosition, wxSize(200, 200));
19ff0b2c
JS
199
200 boxSizer->Add(mySafari, 1, wxEXPAND);
2c990ba6 201
19ff0b2c
JS
202 wxBoxSizer* frameSizer = new wxBoxSizer(wxVERTICAL);
203 SetSizer(frameSizer);
204 frameSizer->Add(panel, 1, wxEXPAND);
205#else
9a83f860 206 mySafari = new wxWebKitCtrl(this, ID_WEBKIT, wxT("http://www.wxwidgets.org"), wxDefaultPosition, wxSize(200, 200));
19ff0b2c 207#endif
197ab43d 208
2c990ba6
KO
209#if wxUSE_STATUSBAR
210 CreateStatusBar(2);
211#endif // wxUSE_STATUSBAR
212}
213
8a28bf76
VZ
214void MyFrame::OnBackButton(wxCommandEvent& WXUNUSED(myEvent))
215{
2c990ba6
KO
216 if (mySafari->CanGoBack())
217 mySafari->GoBack();
218}
219
8a28bf76
VZ
220void MyFrame::OnNextButton(wxCommandEvent& WXUNUSED(myEvent))
221{
2c990ba6
KO
222 if (mySafari->CanGoForward())
223 mySafari->GoForward();
224}
225
8a28bf76
VZ
226void MyFrame::OnStopButton(wxCommandEvent& WXUNUSED(myEvent))
227{
2c990ba6
KO
228 mySafari->Stop();
229}
230
8a28bf76
VZ
231void MyFrame::OnReloadButton(wxCommandEvent& WXUNUSED(myEvent))
232{
0afdfcac 233 mySafari->Reload();
2c990ba6
KO
234}
235
8a28bf76
VZ
236void MyFrame::OnURLEnter(wxCommandEvent& WXUNUSED(myEvent))
237{
2c990ba6
KO
238 mySafari->LoadURL(urlText->GetValue());
239}
240
8a28bf76
VZ
241void MyFrame::OnStateChanged(wxWebKitStateChangedEvent& myEvent)
242{
243 if (GetStatusBar() != NULL)
244 {
245 if (myEvent.GetState() == wxWEBKIT_STATE_NEGOTIATING)
246 {
2c990ba6 247 GetStatusBar()->SetStatusText(_("Contacting ") + myEvent.GetURL());
fba8e95e 248 urlText->SetValue(myEvent.GetURL());
2c990ba6 249 }
8a28bf76
VZ
250 else if (myEvent.GetState() == wxWEBKIT_STATE_TRANSFERRING)
251 {
2c990ba6
KO
252 GetStatusBar()->SetStatusText(_("Loading ") + myEvent.GetURL());
253 }
8a28bf76
VZ
254 else if (myEvent.GetState() == wxWEBKIT_STATE_STOP)
255 {
2c990ba6
KO
256 GetStatusBar()->SetStatusText(_("Load complete."));
257 SetTitle(mySafari->GetTitle());
258 }
8a28bf76
VZ
259 else if (myEvent.GetState() == wxWEBKIT_STATE_FAILED)
260 {
2c990ba6
KO
261 GetStatusBar()->SetStatusText(_("Failed to load ") + myEvent.GetURL());
262 }
263 }
264
265}
266
8a28bf76
VZ
267void MyFrame::OnViewSource(wxCommandEvent& WXUNUSED(myEvent))
268{
2c990ba6
KO
269 if (mySafari->CanGetPageSource())
270 wxMessageBox(mySafari->GetPageSource());
271}
272
8a28bf76
VZ
273void MyFrame::OnSetSource(wxCommandEvent& WXUNUSED(myEvent))
274{
275 if (mySafari)
276 {
bd1018b9 277 wxString myText = wxT("<HTML><HEAD></HEAD><BODY><P>Hello world!</P></BODY></HTML>");
2c990ba6
KO
278 mySafari->SetPageSource(myText);
279 }
280}