1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: HtmlCtrl sample
4 // Author: Julian Smart / Kevin Ollivier
8 // Copyright: (c) Julian Smart / Kevin Ollivier
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 // For compilers that support precompilation, includes "wx/wx.h".
21 #include "wx/wxprec.h"
27 // for all others, include the necessary headers (this file is usually all you
28 // need because it includes almost all "standard" wxWidgets headers)
33 #include "wx/html/webkit.h"
35 // ----------------------------------------------------------------------------
37 // ----------------------------------------------------------------------------
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
52 // ----------------------------------------------------------------------------
54 // ----------------------------------------------------------------------------
56 // Define a new application type, each program should derive a class from wxApp
57 class MyApp
: public wxApp
60 // override base class virtuals
61 // ----------------------------
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();
69 // Define a new frame type: this is going to be our main frame
70 class MyFrame
: public wxFrame
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
);
83 wxWebKitCtrl
* mySafari
;
86 // any class wishing to process wxWidgets events must use this macro
90 // ----------------------------------------------------------------------------
91 // event tables and other macros for wxWidgets
92 // ----------------------------------------------------------------------------
94 // the event tables connect the wxWidgets events with the functions (event
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)
110 // Create a new application object: this macro will allow wxWidgets to create
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
117 // ============================================================================
119 // ============================================================================
121 // ----------------------------------------------------------------------------
122 // the application class
123 // ----------------------------------------------------------------------------
125 // 'Main program' equivalent: the program execution "starts" here
128 // create the main application window
129 MyFrame
*frame
= new MyFrame(_T("wxWebKit Sample"));
131 // and show it (the frames, unlike simple controls, are not shown when
132 // created initially)
135 // success: wxApp::OnRun() will be called which will enter the main message
136 // loop and the application will run. If we returned false here, the
137 // application would exit immediately.
141 // ----------------------------------------------------------------------------
143 // ----------------------------------------------------------------------------
146 MyFrame::MyFrame(const wxString
& title
)
147 : wxFrame(NULL
, wxID_ANY
, title
, wxDefaultPosition
, wxSize(500,500))
149 wxMenuBar
* myBar
= new wxMenuBar();
150 wxMenu
* fileMenu
= new wxMenu
;
151 fileMenu
->Append(ID_OPEN
, _("&Open"));
152 fileMenu
->Append(ID_SAVE
, _("&Save"));
153 myBar
->Append(fileMenu
, _("&File"));
155 wxMenu
* editMenu
= new wxMenu
;
156 editMenu
->Append(ID_SET_SOURCE
, _("Set Page Source"));
157 myBar
->Append(editMenu
, _("&Edit"));
159 //wxMenu* viewMenu = new wxMenu(_("View"));
160 //viewMenu->Append(ID_VIEW_SOURCE, _("View Source"));
161 //myBar->Append(viewMenu, _("View"));
165 wxToolBar
* myToolbar
= CreateToolBar();
166 wxButton
* btnBack
= new wxButton(myToolbar
, ID_BACK
, _("Back"));
167 myToolbar
->AddControl(btnBack
);
168 myToolbar
->AddSeparator();
169 wxButton
* btnNext
= new wxButton(myToolbar
, ID_NEXT
, _("Next"));
170 myToolbar
->AddControl(btnNext
);
171 myToolbar
->AddSeparator();
172 wxButton
* btnStop
= new wxButton(myToolbar
, ID_STOP
, _("Stop"));
173 myToolbar
->AddControl(btnStop
);
174 myToolbar
->AddSeparator();
175 wxButton
* btnReload
= new wxButton(myToolbar
, ID_RELOAD
, _("Reload"));
176 myToolbar
->AddControl(btnReload
);
177 myToolbar
->AddSeparator();
178 urlText
= new wxTextCtrl(myToolbar
, ID_URLLIST
, _T("http://www.wxwidgets.org"), wxDefaultPosition
, wxSize(220, -1), wxTE_PROCESS_ENTER
);
179 myToolbar
->AddControl(urlText
);
180 myToolbar
->AddSeparator();
181 myToolbar
->Realize();
183 // Testing wxWebKitCtrl inside a panel
185 wxPanel
* panel
= new wxPanel(this, wxID_ANY
);
187 wxBoxSizer
* boxSizer
= new wxBoxSizer(wxVERTICAL
);
188 panel
->SetSizer(boxSizer
);
190 mySafari
= new wxWebKitCtrl(panel
, ID_WEBKIT
, _T("http://www.wxwidgets.org"), wxDefaultPosition
, wxSize(200, 200));
192 boxSizer
->Add(mySafari
, 1, wxEXPAND
);
194 wxBoxSizer
* frameSizer
= new wxBoxSizer(wxVERTICAL
);
195 SetSizer(frameSizer
);
196 frameSizer
->Add(panel
, 1, wxEXPAND
);
198 mySafari
= new wxWebKitCtrl(this, ID_WEBKIT
, _T("http://www.wxwidgets.org"), wxDefaultPosition
, wxSize(200, 200));
203 #endif // wxUSE_STATUSBAR
206 void MyFrame::OnBackButton(wxCommandEvent
& myEvent
){
207 if (mySafari
->CanGoBack())
211 void MyFrame::OnNextButton(wxCommandEvent
& myEvent
){
212 if (mySafari
->CanGoForward())
213 mySafari
->GoForward();
216 void MyFrame::OnStopButton(wxCommandEvent
& myEvent
){
220 void MyFrame::OnReloadButton(wxCommandEvent
& myEvent
){
224 void MyFrame::OnURLEnter(wxCommandEvent
& myEvent
){
225 mySafari
->LoadURL(urlText
->GetValue());
228 void MyFrame::OnStateChanged(wxWebKitStateChangedEvent
& myEvent
){
229 if (GetStatusBar() != NULL
){
230 if (myEvent
.GetState() == wxWEBKIT_STATE_NEGOTIATING
){
231 GetStatusBar()->SetStatusText(_("Contacting ") + myEvent
.GetURL());
232 urlText
->SetValue(myEvent
.GetURL());
234 else if (myEvent
.GetState() == wxWEBKIT_STATE_TRANSFERRING
){
235 GetStatusBar()->SetStatusText(_("Loading ") + myEvent
.GetURL());
237 else if (myEvent
.GetState() == wxWEBKIT_STATE_STOP
){
238 GetStatusBar()->SetStatusText(_("Load complete."));
239 SetTitle(mySafari
->GetTitle());
241 else if (myEvent
.GetState() == wxWEBKIT_STATE_FAILED
){
242 GetStatusBar()->SetStatusText(_("Failed to load ") + myEvent
.GetURL());
248 void MyFrame::OnViewSource(wxCommandEvent
& myEvent
){
249 if (mySafari
->CanGetPageSource())
250 wxMessageBox(mySafari
->GetPageSource());
253 void MyFrame::OnSetSource(wxCommandEvent
& myEvent
){
255 wxString myText
= wxT("<HTML><HEAD></HEAD><BODY><P>Hello world!</P></BODY></HTML>");
256 mySafari
->SetPageSource(myText
);