]> git.saurik.com Git - wxWidgets.git/blob - samples/docview/docview.cpp
correct access for virtuals
[wxWidgets.git] / samples / docview / docview.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: docview.cpp
3 // Purpose: Document/view demo
4 // Author: Julian Smart
5 // Modified by:
6 // Created: 04/01/98
7 // RCS-ID: $Id$
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
11
12 /*
13 * Purpose: Document/view architecture demo for wxWidgets class library
14 * Run with no arguments for multiple top-level windows, -single
15 * for a single window.
16 */
17
18
19 // For compilers that support precompilation, includes "wx/wx.h".
20 #include "wx/wxprec.h"
21
22 #ifdef __BORLANDC__
23 #pragma hdrstop
24 #endif
25
26 #ifndef WX_PRECOMP
27 #include "wx/wx.h"
28 #endif
29
30 #if !wxUSE_DOC_VIEW_ARCHITECTURE
31 #error You must set wxUSE_DOC_VIEW_ARCHITECTURE to 1 in setup.h!
32 #endif
33
34 #include "wx/docview.h"
35
36 #include "docview.h"
37 #include "doc.h"
38 #include "view.h"
39 #ifdef __WXMAC__
40 #include "wx/filename.h"
41 #endif
42
43 MyFrame *frame = (MyFrame *) NULL;
44
45 // In single window mode, don't have any child windows; use
46 // main window.
47 bool singleWindowMode = false;
48
49 IMPLEMENT_APP(MyApp)
50
51 MyApp::MyApp(void)
52 {
53 m_docManager = (wxDocManager *) NULL;
54 }
55
56 bool MyApp::OnInit(void)
57 {
58 //// Find out if we're:
59 //// multiple window: multiple windows, each view in a separate frame
60 //// single window: one view (within the main frame) and one document at a time, as in Windows Write.
61 //// In single window mode, we only allow one document type
62 if (argc > 1)
63 {
64 if (wxStrcmp(argv[1], _T("-single")) == 0)
65 {
66 singleWindowMode = true;
67 }
68 }
69
70 //// Create a document manager
71 m_docManager = new wxDocManager;
72
73 //// Create a template relating drawing documents to their views
74 (void) new wxDocTemplate(m_docManager, _T("Drawing"), _T("*.drw"), _T(""), _T("drw"), _T("Drawing Doc"), _T("Drawing View"),
75 CLASSINFO(DrawingDocument), CLASSINFO(DrawingView));
76 #ifdef __WXMAC__
77 wxFileName::MacRegisterDefaultTypeAndCreator( wxT("drw") , 'WXMB' , 'WXMA' ) ;
78 #endif
79
80 if (singleWindowMode)
81 {
82 // If we've only got one window, we only get to edit
83 // one document at a time. Therefore no text editing, just
84 // doodling.
85 m_docManager->SetMaxDocsOpen(1);
86 }
87 else
88 {
89 //// Create a template relating text documents to their views
90 (void) new wxDocTemplate(m_docManager, _T("Text"), _T("*.txt;*.text"), _T(""), _T("txt;text"), _T("Text Doc"), _T("Text View"),
91 CLASSINFO(TextEditDocument), CLASSINFO(TextEditView));
92 #ifdef __WXMAC__
93 wxFileName::MacRegisterDefaultTypeAndCreator( wxT("txt") , 'TEXT' , 'WXMA' ) ;
94 #endif
95 }
96
97 //// Create the main frame window
98 frame = new MyFrame(m_docManager, (wxFrame *) NULL, wxID_ANY, _T("DocView Demo"), wxPoint(0, 0), wxSize(500, 400), wxDEFAULT_FRAME_STYLE);
99
100 //// Give it an icon (this is ignored in MDI mode: uses resources)
101 #ifdef __WXMSW__
102 frame->SetIcon(wxIcon(_T("doc_icn")));
103 #endif
104
105 //// Make a menubar
106 wxMenu *file_menu = new wxMenu;
107 wxMenu *edit_menu = (wxMenu *) NULL;
108
109 file_menu->Append(wxID_NEW, _T("&New..."));
110 file_menu->Append(wxID_OPEN, _T("&Open..."));
111
112 if (singleWindowMode)
113 {
114 file_menu->Append(wxID_CLOSE, _T("&Close"));
115 file_menu->Append(wxID_SAVE, _T("&Save"));
116 file_menu->Append(wxID_SAVEAS, _T("Save &As..."));
117 file_menu->AppendSeparator();
118 file_menu->Append(wxID_PRINT, _T("&Print..."));
119 file_menu->Append(wxID_PRINT_SETUP, _T("Print &Setup..."));
120 file_menu->Append(wxID_PREVIEW, _T("Print Pre&view"));
121
122 edit_menu = new wxMenu;
123 edit_menu->Append(wxID_UNDO, _T("&Undo"));
124 edit_menu->Append(wxID_REDO, _T("&Redo"));
125 edit_menu->AppendSeparator();
126 edit_menu->Append(DOCVIEW_CUT, _T("&Cut last segment"));
127
128 frame->editMenu = edit_menu;
129 }
130
131 file_menu->AppendSeparator();
132 file_menu->Append(wxID_EXIT, _T("E&xit"));
133
134 // A nice touch: a history of files visited. Use this menu.
135 m_docManager->FileHistoryUseMenu(file_menu);
136
137 wxMenu *help_menu = new wxMenu;
138 help_menu->Append(DOCVIEW_ABOUT, _T("&About"));
139
140 wxMenuBar *menu_bar = new wxMenuBar;
141
142 menu_bar->Append(file_menu, _T("&File"));
143 if (edit_menu)
144 menu_bar->Append(edit_menu, _T("&Edit"));
145 menu_bar->Append(help_menu, _T("&Help"));
146
147 if (singleWindowMode)
148 frame->canvas = frame->CreateCanvas((wxView *) NULL, frame);
149
150 //// Associate the menu bar with the frame
151 frame->SetMenuBar(menu_bar);
152
153 frame->Centre(wxBOTH);
154 frame->Show(true);
155
156 SetTopWindow(frame);
157 return true;
158 }
159
160 int MyApp::OnExit(void)
161 {
162 delete m_docManager;
163 return 0;
164 }
165
166 /*
167 * Centralised code for creating a document frame.
168 * Called from view.cpp, when a view is created, but not used at all
169 * in 'single window' mode.
170 */
171
172 wxFrame *MyApp::CreateChildFrame(wxDocument *doc, wxView *view, bool isCanvas)
173 {
174 //// Make a child frame
175 wxDocChildFrame *subframe = new wxDocChildFrame(doc, view, GetMainFrame(), wxID_ANY, _T("Child Frame"),
176 wxPoint(10, 10), wxSize(300, 300), wxDEFAULT_FRAME_STYLE);
177
178 #ifdef __WXMSW__
179 subframe->SetIcon(wxString(isCanvas ? _T("chrt_icn") : _T("notepad_icn")));
180 #endif
181
182 //// Make a menubar
183 wxMenu *file_menu = new wxMenu;
184
185 file_menu->Append(wxID_NEW, _T("&New..."));
186 file_menu->Append(wxID_OPEN, _T("&Open..."));
187 file_menu->Append(wxID_CLOSE, _T("&Close"));
188 file_menu->Append(wxID_SAVE, _T("&Save"));
189 file_menu->Append(wxID_SAVEAS, _T("Save &As..."));
190
191 if (isCanvas)
192 {
193 file_menu->AppendSeparator();
194 file_menu->Append(wxID_PRINT, _T("&Print..."));
195 file_menu->Append(wxID_PRINT_SETUP, _T("Print &Setup..."));
196 file_menu->Append(wxID_PREVIEW, _T("Print Pre&view"));
197 }
198
199 wxMenu *edit_menu = (wxMenu *) NULL;
200
201 if (isCanvas)
202 {
203 edit_menu = new wxMenu;
204 edit_menu->Append(wxID_UNDO, _T("&Undo"));
205 edit_menu->Append(wxID_REDO, _T("&Redo"));
206 edit_menu->AppendSeparator();
207 edit_menu->Append(DOCVIEW_CUT, _T("&Cut last segment"));
208
209 doc->GetCommandProcessor()->SetEditMenu(edit_menu);
210 }
211
212 wxMenu *help_menu = new wxMenu;
213 help_menu->Append(DOCVIEW_ABOUT, _T("&About"));
214
215 wxMenuBar *menu_bar = new wxMenuBar;
216
217 menu_bar->Append(file_menu, _T("&File"));
218 if (isCanvas)
219 menu_bar->Append(edit_menu, _T("&Edit"));
220 menu_bar->Append(help_menu, _T("&Help"));
221
222 //// Associate the menu bar with the frame
223 subframe->SetMenuBar(menu_bar);
224
225 subframe->Centre(wxBOTH);
226
227 return subframe;
228 }
229
230 /*
231 * This is the top-level window of the application.
232 */
233
234 IMPLEMENT_CLASS(MyFrame, wxDocParentFrame)
235 BEGIN_EVENT_TABLE(MyFrame, wxDocParentFrame)
236 EVT_MENU(DOCVIEW_ABOUT, MyFrame::OnAbout)
237 END_EVENT_TABLE()
238
239 MyFrame::MyFrame(wxDocManager *manager, wxFrame *frame, wxWindowID id, const wxString& title,
240 const wxPoint& pos, const wxSize& size, const long type):
241 wxDocParentFrame(manager, frame, id, title, pos, size, type)
242 {
243 // This pointer only needed if in single window mode
244 canvas = (MyCanvas *) NULL;
245 editMenu = (wxMenu *) NULL;
246 }
247
248 void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event) )
249 {
250 (void)wxMessageBox(_T("DocView Demo\nAuthor: Julian Smart\nUsage: docview.exe [-single]"), _T("About DocView"));
251 }
252
253 // Creates a canvas. Called either from view.cc when a new drawing
254 // view is created, or in OnInit as a child of the main window,
255 // if in 'single window' mode.
256 MyCanvas *MyFrame::CreateCanvas(wxView *view, wxFrame *parent)
257 {
258 int width, height;
259 parent->GetClientSize(&width, &height);
260
261 // Non-retained canvas
262 MyCanvas *canvas = new MyCanvas(view, parent, wxPoint(0, 0), wxSize(width, height), 0);
263 canvas->SetCursor(wxCursor(wxCURSOR_PENCIL));
264
265 // Give it scrollbars
266 canvas->SetScrollbars(20, 20, 50, 50);
267 canvas->SetBackgroundColour(*wxWHITE);
268 canvas->ClearBackground();
269
270 return canvas;
271 }
272
273 MyFrame *GetMainFrame(void)
274 {
275 return frame;
276 }
277