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