]> git.saurik.com Git - wxWidgets.git/blob - samples/docview/docview.cpp
Minor corrections to XRC format description.
[wxWidgets.git] / samples / docview / docview.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: samples/docview/docview.cpp
3 // Purpose: Document/view demo
4 // Author: Julian Smart
5 // Modified by: Vadim Zeitlin: merge with the MDI version and general cleanup
6 // Created: 04/01/98
7 // Copyright: (c) 1998 Julian Smart
8 // (c) 2008 Vadim Zeitlin
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 /*
13 This sample show document/view support in wxWidgets.
14
15 It can be run in several ways:
16 * With "--mdi" command line option to use multiple MDI child frames
17 for the multiple documents (this is the default).
18 * With "--sdi" command line option to use multiple top level windows
19 for the multiple documents
20 * With "--single" command line option to support opening a single
21 document only
22
23 Notice that doing it like this somewhat complicates the code, you could
24 make things much simpler in your own programs by using either
25 wxDocParentFrame or wxDocMDIParentFrame unconditionally (and never using
26 the single mode) instead of supporting all of them as this sample does.
27 */
28
29 // ----------------------------------------------------------------------------
30 // headers
31 // ----------------------------------------------------------------------------
32
33 // For compilers that support precompilation, includes "wx/wx.h".
34 #include "wx/wxprec.h"
35
36 #ifdef __BORLANDC__
37 #pragma hdrstop
38 #endif
39
40 #ifndef WX_PRECOMP
41 #include "wx/wx.h"
42 #include "wx/stockitem.h"
43 #endif
44
45 #if !wxUSE_DOC_VIEW_ARCHITECTURE
46 #error You must set wxUSE_DOC_VIEW_ARCHITECTURE to 1 in setup.h!
47 #endif
48
49 #include "wx/docview.h"
50 #include "wx/docmdi.h"
51
52 #include "docview.h"
53 #include "doc.h"
54 #include "view.h"
55
56 #include "wx/cmdline.h"
57 #include "wx/config.h"
58
59 #ifdef __WXMAC__
60 #include "wx/filename.h"
61 #endif
62
63 #ifndef wxHAS_IMAGES_IN_RESOURCES
64 #include "doc.xpm"
65 #include "chart.xpm"
66 #include "notepad.xpm"
67 #endif
68
69 // ----------------------------------------------------------------------------
70 // MyApp implementation
71 // ----------------------------------------------------------------------------
72
73 IMPLEMENT_APP(MyApp)
74
75 BEGIN_EVENT_TABLE(MyApp, wxApp)
76 EVT_MENU(wxID_ABOUT, MyApp::OnAbout)
77 END_EVENT_TABLE()
78
79 MyApp::MyApp()
80 {
81 #if wxUSE_MDI_ARCHITECTURE
82 m_mode = Mode_MDI;
83 #else
84 m_mode = Mode_SDI;
85 #endif
86
87 m_canvas = NULL;
88 m_menuEdit = NULL;
89 }
90
91 // constants for the command line options names
92 namespace CmdLineOption
93 {
94
95 const char * const MDI = "mdi";
96 const char * const SDI = "sdi";
97 const char * const SINGLE = "single";
98
99 } // namespace CmdLineOption
100
101 void MyApp::OnInitCmdLine(wxCmdLineParser& parser)
102 {
103 wxApp::OnInitCmdLine(parser);
104
105 parser.AddSwitch("", CmdLineOption::MDI,
106 "run in MDI mode: multiple documents, single window");
107 parser.AddSwitch("", CmdLineOption::SDI,
108 "run in SDI mode: multiple documents, multiple windows");
109 parser.AddSwitch("", CmdLineOption::SINGLE,
110 "run in single document mode");
111 }
112
113 bool MyApp::OnCmdLineParsed(wxCmdLineParser& parser)
114 {
115 int numModeOptions = 0;
116
117 #if wxUSE_MDI_ARCHITECTURE
118 if ( parser.Found(CmdLineOption::MDI) )
119 {
120 m_mode = Mode_MDI;
121 numModeOptions++;
122 }
123 #endif // wxUSE_MDI_ARCHITECTURE
124
125 if ( parser.Found(CmdLineOption::SDI) )
126 {
127 m_mode = Mode_SDI;
128 numModeOptions++;
129 }
130
131 if ( parser.Found(CmdLineOption::SINGLE) )
132 {
133 m_mode = Mode_Single;
134 numModeOptions++;
135 }
136
137 if ( numModeOptions > 1 )
138 {
139 wxLogError("Only a single option choosing the mode can be given.");
140 return false;
141 }
142
143 return wxApp::OnCmdLineParsed(parser);
144 }
145
146 bool MyApp::OnInit()
147 {
148 if ( !wxApp::OnInit() )
149 return false;
150
151 ::wxInitAllImageHandlers();
152
153 // Fill in the application information fields before creating wxConfig.
154 SetVendorName("wxWidgets");
155 SetAppName("wx_docview_sample");
156 SetAppDisplayName("wxWidgets DocView Sample");
157
158 //// Create a document manager
159 wxDocManager *docManager = new wxDocManager;
160
161 //// Create a template relating drawing documents to their views
162 new wxDocTemplate(docManager, "Drawing", "*.drw", "", "drw",
163 "Drawing Doc", "Drawing View",
164 CLASSINFO(DrawingDocument), CLASSINFO(DrawingView));
165 #if defined( __WXMAC__ ) && wxOSX_USE_CARBON
166 wxFileName::MacRegisterDefaultTypeAndCreator("drw" , 'WXMB' , 'WXMA');
167 #endif
168
169 if ( m_mode == Mode_Single )
170 {
171 // If we've only got one window, we only get to edit one document at a
172 // time. Therefore no text editing, just doodling.
173 docManager->SetMaxDocsOpen(1);
174 }
175 else // multiple documents mode: allow documents of different types
176 {
177 // Create a template relating text documents to their views
178 new wxDocTemplate(docManager, "Text", "*.txt;*.text", "", "txt;text",
179 "Text Doc", "Text View",
180 CLASSINFO(TextEditDocument), CLASSINFO(TextEditView));
181 #if defined( __WXMAC__ ) && wxOSX_USE_CARBON
182 wxFileName::MacRegisterDefaultTypeAndCreator("txt" , 'TEXT' , 'WXMA');
183 #endif
184 // Create a template relating image documents to their views
185 new wxDocTemplate(docManager, "Image", "*.png;*.jpg", "", "png;jpg",
186 "Image Doc", "Image View",
187 CLASSINFO(ImageDocument), CLASSINFO(ImageView));
188 }
189
190 // create the main frame window
191 wxFrame *frame;
192 #if wxUSE_MDI_ARCHITECTURE
193 if ( m_mode == Mode_MDI )
194 {
195 frame = new wxDocMDIParentFrame(docManager, NULL, wxID_ANY,
196 GetAppDisplayName(),
197 wxDefaultPosition,
198 wxSize(500, 400));
199 }
200 else
201 #endif // wxUSE_MDI_ARCHITECTURE
202 {
203 frame = new wxDocParentFrame(docManager, NULL, wxID_ANY,
204 GetAppDisplayName(),
205 wxDefaultPosition,
206 wxSize(500, 400));
207 }
208
209 // and its menu bar
210 wxMenu *menuFile = new wxMenu;
211
212 menuFile->Append(wxID_NEW);
213 menuFile->Append(wxID_OPEN);
214
215 if ( m_mode == Mode_Single )
216 AppendDocumentFileCommands(menuFile, true);
217
218 menuFile->AppendSeparator();
219 menuFile->Append(wxID_EXIT);
220
221 // A nice touch: a history of files visited. Use this menu.
222 docManager->FileHistoryUseMenu(menuFile);
223 #if wxUSE_CONFIG
224 docManager->FileHistoryLoad(*wxConfig::Get());
225 #endif // wxUSE_CONFIG
226
227
228 if ( m_mode == Mode_Single )
229 {
230 m_canvas = new MyCanvas(NULL, frame);
231 m_menuEdit = CreateDrawingEditMenu();
232 docManager->CreateNewDocument();
233 }
234
235 CreateMenuBarForFrame(frame, menuFile, m_menuEdit);
236
237 frame->SetIcon(wxICON(doc));
238 frame->Centre();
239 frame->Show();
240
241 return true;
242 }
243
244 int MyApp::OnExit()
245 {
246 wxDocManager * const manager = wxDocManager::GetDocumentManager();
247 #if wxUSE_CONFIG
248 manager->FileHistorySave(*wxConfig::Get());
249 #endif // wxUSE_CONFIG
250 delete manager;
251
252 return wxApp::OnExit();
253 }
254
255 void MyApp::AppendDocumentFileCommands(wxMenu *menu, bool supportsPrinting)
256 {
257 menu->Append(wxID_CLOSE);
258 menu->Append(wxID_SAVE);
259 menu->Append(wxID_SAVEAS);
260 menu->Append(wxID_REVERT, _("Re&vert..."));
261
262 if ( supportsPrinting )
263 {
264 menu->AppendSeparator();
265 menu->Append(wxID_PRINT);
266 menu->Append(wxID_PRINT_SETUP, "Print &Setup...");
267 menu->Append(wxID_PREVIEW);
268 }
269 }
270
271 wxMenu *MyApp::CreateDrawingEditMenu()
272 {
273 wxMenu * const menu = new wxMenu;
274 menu->Append(wxID_UNDO);
275 menu->Append(wxID_REDO);
276 menu->AppendSeparator();
277 menu->Append(wxID_CUT, "&Cut last segment");
278
279 return menu;
280 }
281
282 void MyApp::CreateMenuBarForFrame(wxFrame *frame, wxMenu *file, wxMenu *edit)
283 {
284 wxMenuBar *menubar = new wxMenuBar;
285
286 menubar->Append(file, wxGetStockLabel(wxID_FILE));
287
288 if ( edit )
289 menubar->Append(edit, wxGetStockLabel(wxID_EDIT));
290
291 wxMenu *help= new wxMenu;
292 help->Append(wxID_ABOUT);
293 menubar->Append(help, wxGetStockLabel(wxID_HELP));
294
295 frame->SetMenuBar(menubar);
296 }
297
298 wxFrame *MyApp::CreateChildFrame(wxView *view, bool isCanvas)
299 {
300 // create a child frame of appropriate class for the current mode
301 wxFrame *subframe;
302 wxDocument *doc = view->GetDocument();
303 #if wxUSE_MDI_ARCHITECTURE
304 if ( GetMode() == Mode_MDI )
305 {
306 subframe = new wxDocMDIChildFrame
307 (
308 doc,
309 view,
310 wxStaticCast(GetTopWindow(), wxDocMDIParentFrame),
311 wxID_ANY,
312 "Child Frame",
313 wxDefaultPosition,
314 wxSize(300, 300)
315 );
316 }
317 else
318 #endif // wxUSE_MDI_ARCHITECTURE
319 {
320 subframe = new wxDocChildFrame
321 (
322 doc,
323 view,
324 wxStaticCast(GetTopWindow(), wxDocParentFrame),
325 wxID_ANY,
326 "Child Frame",
327 wxDefaultPosition,
328 wxSize(300, 300)
329 );
330
331 subframe->Centre();
332 }
333
334 wxMenu *menuFile = new wxMenu;
335
336 menuFile->Append(wxID_NEW);
337 menuFile->Append(wxID_OPEN);
338 AppendDocumentFileCommands(menuFile, isCanvas);
339 menuFile->AppendSeparator();
340 menuFile->Append(wxID_EXIT);
341
342 wxMenu *menuEdit;
343 if ( isCanvas )
344 {
345 menuEdit = CreateDrawingEditMenu();
346
347 doc->GetCommandProcessor()->SetEditMenu(menuEdit);
348 doc->GetCommandProcessor()->Initialize();
349 }
350 else // text frame
351 {
352 menuEdit = new wxMenu;
353 menuEdit->Append(wxID_COPY);
354 menuEdit->Append(wxID_PASTE);
355 menuEdit->Append(wxID_SELECTALL);
356 }
357
358 CreateMenuBarForFrame(subframe, menuFile, menuEdit);
359
360 subframe->SetIcon(isCanvas ? wxICON(chrt) : wxICON(notepad));
361
362 return subframe;
363 }
364
365 void MyApp::OnAbout(wxCommandEvent& WXUNUSED(event))
366 {
367 wxString modeName;
368 switch ( m_mode )
369 {
370 #if wxUSE_MDI_ARCHITECTURE
371 case Mode_MDI:
372 modeName = "MDI";
373 break;
374 #endif // wxUSE_MDI_ARCHITECTURE
375
376 case Mode_SDI:
377 modeName = "SDI";
378 break;
379
380 case Mode_Single:
381 modeName = "single document";
382 break;
383
384 default:
385 wxFAIL_MSG( "unknown mode ");
386 }
387
388 #ifdef __VISUALC6__
389 const int docsCount =
390 wxDocManager::GetDocumentManager()->GetDocuments().GetCount();
391 #else
392 const int docsCount =
393 wxDocManager::GetDocumentManager()->GetDocumentsVector().size();
394 #endif
395
396 wxLogMessage
397 (
398 "This is the wxWidgets Document/View Sample\n"
399 "running in %s mode.\n"
400 "%d open documents.\n"
401 "\n"
402 "Authors: Julian Smart, Vadim Zeitlin\n"
403 "\n"
404 "Usage: docview [--{mdi,sdi,single}]",
405 modeName,
406 docsCount
407 );
408 }