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"
36 #include "../../sample.xpm"
39 // ----------------------------------------------------------------------------
41 // ----------------------------------------------------------------------------
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
56 // ----------------------------------------------------------------------------
58 // ----------------------------------------------------------------------------
60 // Define a new application type, each program should derive a class from wxApp
61 class MyApp
: public wxApp
64 // override base class virtuals
65 // ----------------------------
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();
73 // Define a new frame type: this is going to be our main frame
74 class MyFrame
: public wxFrame
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
);
87 wxWebKitCtrl
* mySafari
;
90 // any class wishing to process wxWidgets events must use this macro
94 // ----------------------------------------------------------------------------
95 // event tables and other macros for wxWidgets
96 // ----------------------------------------------------------------------------
98 // the event tables connect the wxWidgets events with the functions (event
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.
101 BEGIN_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)
114 // Create a new application object: this macro will allow wxWidgets to create
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
121 // ============================================================================
123 // ============================================================================
125 // ----------------------------------------------------------------------------
126 // the application class
127 // ----------------------------------------------------------------------------
129 // 'Main program' equivalent: the program execution "starts" here
132 if ( !wxApp::OnInit() )
135 // create the main application window
136 MyFrame
*frame
= new MyFrame(_T("wxWebKit Sample"));
138 // and show it (the frames, unlike simple controls, are not shown when
139 // created initially)
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.
148 // ----------------------------------------------------------------------------
150 // ----------------------------------------------------------------------------
153 MyFrame::MyFrame(const wxString
& title
)
154 : wxFrame(NULL
, wxID_ANY
, title
, wxDefaultPosition
, wxSize(500,500))
156 SetIcon(wxICON(sample
));
158 wxMenuBar
* myBar
= new wxMenuBar();
159 wxMenu
* fileMenu
= new wxMenu
;
160 fileMenu
->Append(ID_OPEN
, _("&Open"));
161 fileMenu
->Append(ID_SAVE
, _("&Save"));
162 myBar
->Append(fileMenu
, _("&File"));
164 wxMenu
* editMenu
= new wxMenu
;
165 editMenu
->Append(ID_SET_SOURCE
, _("Set Page Source"));
166 myBar
->Append(editMenu
, _("&Edit"));
168 //wxMenu* viewMenu = new wxMenu(_("View"));
169 //viewMenu->Append(ID_VIEW_SOURCE, _("View Source"));
170 //myBar->Append(viewMenu, _("View"));
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();
187 urlText
= new wxTextCtrl(myToolbar
, ID_URLLIST
, _T("http://www.wxwidgets.org"), wxDefaultPosition
, wxSize(220, -1), wxTE_PROCESS_ENTER
);
188 myToolbar
->AddControl(urlText
);
189 myToolbar
->AddSeparator();
190 myToolbar
->Realize();
192 // Testing wxWebKitCtrl inside a panel
194 wxPanel
* panel
= new wxPanel(this, wxID_ANY
);
196 wxBoxSizer
* boxSizer
= new wxBoxSizer(wxVERTICAL
);
197 panel
->SetSizer(boxSizer
);
199 mySafari
= new wxWebKitCtrl(panel
, ID_WEBKIT
, _T("http://www.wxwidgets.org"), wxDefaultPosition
, wxSize(200, 200));
201 boxSizer
->Add(mySafari
, 1, wxEXPAND
);
203 wxBoxSizer
* frameSizer
= new wxBoxSizer(wxVERTICAL
);
204 SetSizer(frameSizer
);
205 frameSizer
->Add(panel
, 1, wxEXPAND
);
207 mySafari
= new wxWebKitCtrl(this, ID_WEBKIT
, _T("http://www.wxwidgets.org"), wxDefaultPosition
, wxSize(200, 200));
212 #endif // wxUSE_STATUSBAR
215 void MyFrame::OnBackButton(wxCommandEvent
& WXUNUSED(myEvent
))
217 if (mySafari
->CanGoBack())
221 void MyFrame::OnNextButton(wxCommandEvent
& WXUNUSED(myEvent
))
223 if (mySafari
->CanGoForward())
224 mySafari
->GoForward();
227 void MyFrame::OnStopButton(wxCommandEvent
& WXUNUSED(myEvent
))
232 void MyFrame::OnReloadButton(wxCommandEvent
& WXUNUSED(myEvent
))
237 void MyFrame::OnURLEnter(wxCommandEvent
& WXUNUSED(myEvent
))
239 mySafari
->LoadURL(urlText
->GetValue());
242 void MyFrame::OnStateChanged(wxWebKitStateChangedEvent
& myEvent
)
244 if (GetStatusBar() != NULL
)
246 if (myEvent
.GetState() == wxWEBKIT_STATE_NEGOTIATING
)
248 GetStatusBar()->SetStatusText(_("Contacting ") + myEvent
.GetURL());
249 urlText
->SetValue(myEvent
.GetURL());
251 else if (myEvent
.GetState() == wxWEBKIT_STATE_TRANSFERRING
)
253 GetStatusBar()->SetStatusText(_("Loading ") + myEvent
.GetURL());
255 else if (myEvent
.GetState() == wxWEBKIT_STATE_STOP
)
257 GetStatusBar()->SetStatusText(_("Load complete."));
258 SetTitle(mySafari
->GetTitle());
260 else if (myEvent
.GetState() == wxWEBKIT_STATE_FAILED
)
262 GetStatusBar()->SetStatusText(_("Failed to load ") + myEvent
.GetURL());
268 void MyFrame::OnViewSource(wxCommandEvent
& WXUNUSED(myEvent
))
270 if (mySafari
->CanGetPageSource())
271 wxMessageBox(mySafari
->GetPageSource());
274 void MyFrame::OnSetSource(wxCommandEvent
& WXUNUSED(myEvent
))
278 wxString myText
= wxT("<HTML><HEAD></HEAD><BODY><P>Hello world!</P></BODY></HTML>");
279 mySafari
->SetPageSource(myText
);