1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: HtmlCtrl sample
4 // Author: Julian Smart / Kevin Ollivier
7 // Copyright: (c) Julian Smart / Kevin Ollivier
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
11 // ============================================================================
13 // ============================================================================
15 // ----------------------------------------------------------------------------
17 // ----------------------------------------------------------------------------
19 // For compilers that support precompilation, includes "wx/wx.h".
20 #include "wx/wxprec.h"
26 // for all others, include the necessary headers (this file is usually all you
27 // need because it includes almost all "standard" wxWidgets headers)
32 #include "wx/html/webkit.h"
34 #ifndef wxHAS_IMAGES_IN_RESOURCES
35 #include "../../sample.xpm"
38 // ----------------------------------------------------------------------------
40 // ----------------------------------------------------------------------------
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
55 // ----------------------------------------------------------------------------
57 // ----------------------------------------------------------------------------
59 // Define a new application type, each program should derive a class from wxApp
60 class MyApp
: public wxApp
63 // override base class virtuals
64 // ----------------------------
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();
72 // Define a new frame type: this is going to be our main frame
73 class MyFrame
: public wxFrame
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
);
86 wxWebKitCtrl
* mySafari
;
89 // any class wishing to process wxWidgets events must use this macro
93 // ----------------------------------------------------------------------------
94 // event tables and other macros for wxWidgets
95 // ----------------------------------------------------------------------------
97 // the event tables connect the wxWidgets events with the functions (event
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.
100 BEGIN_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)
113 // Create a new application object: this macro will allow wxWidgets to create
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
120 // ============================================================================
122 // ============================================================================
124 // ----------------------------------------------------------------------------
125 // the application class
126 // ----------------------------------------------------------------------------
128 // 'Main program' equivalent: the program execution "starts" here
131 if ( !wxApp::OnInit() )
134 // create the main application window
135 MyFrame
*frame
= new MyFrame(wxT("wxWebKit Sample"));
137 // and show it (the frames, unlike simple controls, are not shown when
138 // created initially)
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.
147 // ----------------------------------------------------------------------------
149 // ----------------------------------------------------------------------------
152 MyFrame::MyFrame(const wxString
& title
)
153 : wxFrame(NULL
, wxID_ANY
, title
, wxDefaultPosition
, wxSize(500,500))
155 SetIcon(wxICON(sample
));
157 wxMenuBar
* myBar
= new wxMenuBar();
158 wxMenu
* fileMenu
= new wxMenu
;
159 fileMenu
->Append(ID_OPEN
, _("&Open"));
160 fileMenu
->Append(ID_SAVE
, _("&Save"));
161 myBar
->Append(fileMenu
, _("&File"));
163 wxMenu
* editMenu
= new wxMenu
;
164 editMenu
->Append(ID_SET_SOURCE
, _("Set Page Source"));
165 myBar
->Append(editMenu
, _("&Edit"));
167 //wxMenu* viewMenu = new wxMenu(_("View"));
168 //viewMenu->Append(ID_VIEW_SOURCE, _("View Source"));
169 //myBar->Append(viewMenu, _("View"));
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();
186 urlText
= new wxTextCtrl(myToolbar
, ID_URLLIST
, wxT("http://www.wxwidgets.org"), wxDefaultPosition
, wxSize(220, -1), wxTE_PROCESS_ENTER
);
187 myToolbar
->AddControl(urlText
);
188 myToolbar
->AddSeparator();
189 myToolbar
->Realize();
191 // Testing wxWebKitCtrl inside a panel
193 wxPanel
* panel
= new wxPanel(this, wxID_ANY
);
195 wxBoxSizer
* boxSizer
= new wxBoxSizer(wxVERTICAL
);
196 panel
->SetSizer(boxSizer
);
198 mySafari
= new wxWebKitCtrl(panel
, ID_WEBKIT
, wxT("http://www.wxwidgets.org"), wxDefaultPosition
, wxSize(200, 200));
200 boxSizer
->Add(mySafari
, 1, wxEXPAND
);
202 wxBoxSizer
* frameSizer
= new wxBoxSizer(wxVERTICAL
);
203 SetSizer(frameSizer
);
204 frameSizer
->Add(panel
, 1, wxEXPAND
);
206 mySafari
= new wxWebKitCtrl(this, ID_WEBKIT
, wxT("http://www.wxwidgets.org"), wxDefaultPosition
, wxSize(200, 200));
211 #endif // wxUSE_STATUSBAR
214 void MyFrame::OnBackButton(wxCommandEvent
& WXUNUSED(myEvent
))
216 if (mySafari
->CanGoBack())
220 void MyFrame::OnNextButton(wxCommandEvent
& WXUNUSED(myEvent
))
222 if (mySafari
->CanGoForward())
223 mySafari
->GoForward();
226 void MyFrame::OnStopButton(wxCommandEvent
& WXUNUSED(myEvent
))
231 void MyFrame::OnReloadButton(wxCommandEvent
& WXUNUSED(myEvent
))
236 void MyFrame::OnURLEnter(wxCommandEvent
& WXUNUSED(myEvent
))
238 mySafari
->LoadURL(urlText
->GetValue());
241 void MyFrame::OnStateChanged(wxWebKitStateChangedEvent
& myEvent
)
243 if (GetStatusBar() != NULL
)
245 if (myEvent
.GetState() == wxWEBKIT_STATE_NEGOTIATING
)
247 GetStatusBar()->SetStatusText(_("Contacting ") + myEvent
.GetURL());
248 urlText
->SetValue(myEvent
.GetURL());
250 else if (myEvent
.GetState() == wxWEBKIT_STATE_TRANSFERRING
)
252 GetStatusBar()->SetStatusText(_("Loading ") + myEvent
.GetURL());
254 else if (myEvent
.GetState() == wxWEBKIT_STATE_STOP
)
256 GetStatusBar()->SetStatusText(_("Load complete."));
257 SetTitle(mySafari
->GetTitle());
259 else if (myEvent
.GetState() == wxWEBKIT_STATE_FAILED
)
261 GetStatusBar()->SetStatusText(_("Failed to load ") + myEvent
.GetURL());
267 void MyFrame::OnViewSource(wxCommandEvent
& WXUNUSED(myEvent
))
269 if (mySafari
->CanGetPageSource())
270 wxMessageBox(mySafari
->GetPageSource());
273 void MyFrame::OnSetSource(wxCommandEvent
& WXUNUSED(myEvent
))
277 wxString myText
= wxT("<HTML><HEAD></HEAD><BODY><P>Hello world!</P></BODY></HTML>");
278 mySafari
->SetPageSource(myText
);