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