1 /////////////////////////////////////////////////////////////////////////////
2 // Name:        zip.cpp
3 // Purpose:     wxHtml sample
4 // Author:      ?
5 // Modified by:
6 // Created:     ?
7 // Copyright:   (c) wxWidgets team
8 // Licence:     wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
10 
11 // For compilers that support precompilation, includes "wx/wx.h".
12 #include "wx/wxprec.h"
13 
14 #ifdef __BORLANDC__
15     #pragma hdrstop
16 #endif
17 
18 // for all others, include the necessary headers (this file is usually all you
19 // need because it includes almost all "standard" wxWidgets headers
20 #ifndef WX_PRECOMP
21     #include "wx/wx.h"
22 #endif
23 
24 #include "wx/image.h"
25 #include "wx/html/htmlwin.h"
26 #include "wx/fs_zip.h"
27 
28 #ifndef wxHAS_IMAGES_IN_RESOURCES
29     #include "../../sample.xpm"
30 #endif
31 
32 // ----------------------------------------------------------------------------
33 // private classes
34 // ----------------------------------------------------------------------------
35 
36 // Define a new application type, each program should derive a class from wxApp
37 class MyApp : public wxApp
38 {
39 public:
40     // override base class virtuals
41     // ----------------------------
42 
43     // this one is called on application startup and is a good place for the app
44     // initialization (doing it here and not in the ctor allows to have an error
45     // return: if OnInit() returns false, the application terminates)
46     virtual bool OnInit();
47 };
48 
49 // Define a new frame type: this is going to be our main frame
50 class MyFrame : public wxFrame
51 {
52 public:
53     // ctor(s)
54     MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size);
55 
56     // event handlers (these functions should _not_ be virtual)
57     void OnQuit(wxCommandEvent& event);
58     void OnBack(wxCommandEvent& event);
59     void OnForward(wxCommandEvent& event);
60 
61 private:
62     // any class wishing to process wxWidgets events must use this macro
63     wxDECLARE_EVENT_TABLE();
64 };
65 
66 // ----------------------------------------------------------------------------
67 // constants
68 // ----------------------------------------------------------------------------
69 
70 // IDs for the controls and the menu commands
71 enum
72 {
73     // menu items
74     Minimal_Quit = 1,
75     Minimal_Back,
76     Minimal_Forward
77 };
78 
79 // ----------------------------------------------------------------------------
80 // event tables and other macros for wxWidgets
81 // ----------------------------------------------------------------------------
82 
83 // the event tables connect the wxWidgets events with the functions (event
84 // handlers) which process them. It can be also done at run-time, but for the
85 // simple menu events like this the static method is much simpler.
wxBEGIN_EVENT_TABLE(MyFrame,wxFrame)86 wxBEGIN_EVENT_TABLE(MyFrame, wxFrame)
87     EVT_MENU(Minimal_Quit,  MyFrame::OnQuit)
88     EVT_MENU(Minimal_Back, MyFrame::OnBack)
89     EVT_MENU(Minimal_Forward, MyFrame::OnForward)
90 wxEND_EVENT_TABLE()
91 
92 // Create a new application object: this macro will allow wxWidgets to create
93 // the application object during program execution (it's better than using a
94 // static object for many reasons) and also declares the accessor function
95 // wxGetApp() which will return the reference of the right type (i.e. MyApp and
96 // not wxApp)
97 IMPLEMENT_APP(MyApp)
98 
99 // ============================================================================
100 // implementation
101 // ============================================================================
102 
103 // ----------------------------------------------------------------------------
104 // the application class
105 // ----------------------------------------------------------------------------
106 // `Main program' equivalent: the program execution "starts" here
107 bool MyApp::OnInit()
108 {
109     if ( !wxApp::OnInit() )
110         return false;
111 
112 #if wxUSE_LIBPNG
113     wxImage::AddHandler(new wxPNGHandler);
114 #endif
115 #if wxUSE_LIBJPEG
116     wxImage::AddHandler(new wxJPEGHandler);
117 #endif
118 
119     wxFileSystem::AddHandler(new wxZipFSHandler);
120 
121     // Create the main application window
122     MyFrame *frame = new MyFrame(_("wxHtmlWindow testing application"),
123         wxDefaultPosition, wxSize(640, 480) );
124 
125     // Show it
126     frame->Show(true);
127 
128     // success: wxApp::OnRun() will be called which will enter the main message
129     // loop and the application will run. If we returned false here, the
130     // application would exit immediately.
131 
132     return true;
133 }
134 
135 // ----------------------------------------------------------------------------
136 // main frame
137 // ----------------------------------------------------------------------------
138 
139 wxHtmlWindow *html;
140 
141 // frame constructor
MyFrame(const wxString & title,const wxPoint & pos,const wxSize & size)142 MyFrame::MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size)
143     : wxFrame((wxFrame *)NULL, wxID_ANY, title, pos, size)
144 {
145     SetIcon(wxICON(sample));
146 
147     // create a menu bar
148     wxMenu *menuFile = new wxMenu;
149     wxMenu *menuNav = new wxMenu;
150 
151     menuFile->Append(Minimal_Quit, _("E&xit"));
152     menuNav->Append(Minimal_Back, _("Go &BACK"));
153     menuNav->Append(Minimal_Forward, _("Go &FORWARD"));
154 
155     // now append the freshly created menu to the menu bar...
156     wxMenuBar *menuBar = new wxMenuBar;
157     menuBar->Append(menuFile, _("&File"));
158     menuBar->Append(menuNav, _("&Navigate"));
159 
160     // ... and attach this menu bar to the frame
161     SetMenuBar(menuBar);
162 
163 #if wxUSE_STATUSBAR
164     CreateStatusBar(1);
165 #endif // wxUSE_STATUSBAR
166 
167     html = new wxHtmlWindow(this);
168     html -> SetRelatedFrame(this, _("HTML : %s"));
169 #if wxUSE_STATUSBAR
170     html -> SetRelatedStatusBar(0);
171 #endif // wxUSE_STATUSBAR
172     html -> LoadPage(wxT("start.htm"));
173 }
174 
175 
176 // event handlers
177 
OnQuit(wxCommandEvent & WXUNUSED (event))178 void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
179 {
180     // true is to force the frame to close
181     Close(true);
182 }
183 
OnBack(wxCommandEvent & WXUNUSED (event))184 void MyFrame::OnBack(wxCommandEvent& WXUNUSED(event))
185 {
186     if (!html -> HistoryBack()) wxMessageBox(_("You reached prehistory era!"));
187 }
188 
OnForward(wxCommandEvent & WXUNUSED (event))189 void MyFrame::OnForward(wxCommandEvent& WXUNUSED(event))
190 {
191     if (!html -> HistoryForward()) wxMessageBox(_("No more items in history!"));
192 }
193