Rebake everything using bakefile 0.2.7.
[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 // RCS-ID: $Id$
8 // Copyright: (c) 1998 Julian Smart
9 // (c) 2008 Vadim Zeitlin
10 // Licence: wxWindows license
11 /////////////////////////////////////////////////////////////////////////////
12
13 /*
14 This sample show document/view support in wxWidgets.
15
16 It can be run in several ways:
17 * With "--mdi" command line option to use multiple MDI child frames
18 for the multiple documents (this is the default).
19 * With "--sdi" command line option to use multiple top level windows
20 for the multiple documents
21 * With "--single" command line option to support opening a single
22 document only
23
24 Notice that doing it like this somewhat complicates the code, you could
25 make things much simpler in your own programs by using either
26 wxDocParentFrame or wxDocMDIParentFrame unconditionally (and never using
27 the single mode) instead of supporting all of them as this sample does.
28 */
29
30 // ----------------------------------------------------------------------------
31 // headers
32 // ----------------------------------------------------------------------------
33
34 // For compilers that support precompilation, includes "wx/wx.h".
35 #include "wx/wxprec.h"
36
37 #ifdef __BORLANDC__
38 #pragma hdrstop
39 #endif
40
41 #ifndef WX_PRECOMP
42 #include "wx/wx.h"
43 #include "wx/stockitem.h"
44 #endif
45
46 #if !wxUSE_DOC_VIEW_ARCHITECTURE
47 #error You must set wxUSE_DOC_VIEW_ARCHITECTURE to 1 in setup.h!
48 #endif
49
50 #include "wx/docview.h"
51 #include "wx/docmdi.h"
52
53 #include "docview.h"
54 #include "doc.h"
55 #include "view.h"
56
57 #include "wx/cmdline.h"
58
59 #ifdef __WXMAC__
60 #include "wx/filename.h"
61 #endif
62
63 #if !defined(__WXMSW__) && !defined(__WXPM__)
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 SetAppName("DocView Sample");
154
155 //// Create a document manager
156 wxDocManager *docManager = new wxDocManager;
157
158 //// Create a template relating drawing documents to their views
159 new wxDocTemplate(docManager, "Drawing", "*.drw", "", "drw",
160 "Drawing Doc", "Drawing View",
161 CLASSINFO(DrawingDocument), CLASSINFO(DrawingView));
162 #if defined( __WXMAC__ ) && wxOSX_USE_CARBON
163 wxFileName::MacRegisterDefaultTypeAndCreator("drw" , 'WXMB' , 'WXMA');
164 #endif
165
166 if ( m_mode == Mode_Single )
167 {
168 // If we've only got one window, we only get to edit one document at a
169 // time. Therefore no text editing, just doodling.
170 docManager->SetMaxDocsOpen(1);
171 }
172 else // multiple documents mode: allow documents of different types
173 {
174 // Create a template relating text documents to their views
175 new wxDocTemplate(docManager, "Text", "*.txt;*.text", "", "txt;text",
176 "Text Doc", "Text View",
177 CLASSINFO(TextEditDocument), CLASSINFO(TextEditView));
178 #if defined( __WXMAC__ ) && wxOSX_USE_CARBON
179 wxFileName::MacRegisterDefaultTypeAndCreator("txt" , 'TEXT' , 'WXMA');
180 #endif
181 // Create a template relating image documents to their views
182 new wxDocTemplate(docManager, "Image", "*.png;*.jpg", "", "png;jpg",
183 "Image Doc", "Image View",
184 CLASSINFO(wxImageDocument), CLASSINFO(wxImageView));
185 }
186
187 // create the main frame window
188 wxFrame *frame;
189 #if wxUSE_MDI_ARCHITECTURE
190 if ( m_mode == Mode_MDI )
191 {
192 frame = new wxDocMDIParentFrame(docManager, NULL, wxID_ANY,
193 GetAppDisplayName(),
194 wxDefaultPosition,
195 wxSize(500, 400));
196 }
197 else
198 #endif // wxUSE_MDI_ARCHITECTURE
199 {
200 frame = new wxDocParentFrame(docManager, NULL, wxID_ANY,
201 GetAppDisplayName(),
202 wxDefaultPosition,
203 wxSize(500, 400));
204 }
205
206 // and its menu bar
207 wxMenu *menuFile = new wxMenu;
208
209 menuFile->Append(wxID_NEW);
210 menuFile->Append(wxID_OPEN);
211
212 if ( m_mode == Mode_Single )
213 AppendDocumentFileCommands(menuFile, true);
214
215 menuFile->AppendSeparator();
216 menuFile->Append(wxID_EXIT);
217
218 // A nice touch: a history of files visited. Use this menu.
219 docManager->FileHistoryUseMenu(menuFile);
220
221 if ( m_mode == Mode_Single )
222 {
223 m_canvas = new MyCanvas(NULL, frame);
224 m_menuEdit = CreateDrawingEditMenu();
225 docManager->CreateNewDocument();
226 }
227
228 CreateMenuBarForFrame(frame, menuFile, m_menuEdit);
229
230 frame->SetIcon(wxICON(doc));
231 frame->Centre(wxBOTH);
232 frame->Show(true);
233
234 SetTopWindow(frame);
235 return true;
236 }
237
238 int MyApp::OnExit()
239 {
240 delete wxDocManager::GetDocumentManager();
241
242 return wxApp::OnExit();
243 }
244
245 void MyApp::AppendDocumentFileCommands(wxMenu *menu, bool supportsPrinting)
246 {
247 menu->Append(wxID_CLOSE);
248 menu->Append(wxID_SAVE);
249 menu->Append(wxID_SAVEAS);
250
251 if ( supportsPrinting )
252 {
253 menu->AppendSeparator();
254 menu->Append(wxID_PRINT);
255 menu->Append(wxID_PRINT_SETUP, "Print &Setup...");
256 menu->Append(wxID_PREVIEW);
257 }
258 }
259
260 wxMenu *MyApp::CreateDrawingEditMenu()
261 {
262 wxMenu * const menu = new wxMenu;
263 menu->Append(wxID_UNDO);
264 menu->Append(wxID_REDO);
265 menu->AppendSeparator();
266 menu->Append(wxID_CUT, "&Cut last segment");
267
268 return menu;
269 }
270
271 void MyApp::CreateMenuBarForFrame(wxFrame *frame, wxMenu *file, wxMenu *edit)
272 {
273 wxMenuBar *menubar = new wxMenuBar;
274
275 menubar->Append(file, wxGetStockLabel(wxID_FILE));
276
277 if ( edit )
278 menubar->Append(edit, wxGetStockLabel(wxID_EDIT));
279
280 wxMenu *help= new wxMenu;
281 help->Append(wxID_ABOUT);
282 menubar->Append(help, wxGetStockLabel(wxID_HELP));
283
284 frame->SetMenuBar(menubar);
285 }
286
287 wxFrame *MyApp::CreateChildFrame(wxDocument *doc, wxView *view, bool isCanvas)
288 {
289 // create a child frame of appropriate class for the current mode
290 wxFrame *subframe;
291 #if wxUSE_MDI_ARCHITECTURE
292 if ( GetMode() == Mode_MDI )
293 {
294 subframe = new wxDocMDIChildFrame
295 (
296 doc,
297 view,
298 wxStaticCast(GetTopWindow(), wxDocMDIParentFrame),
299 wxID_ANY,
300 "Child Frame",
301 wxDefaultPosition,
302 wxSize(300, 300)
303 );
304 }
305 else
306 #endif // wxUSE_MDI_ARCHITECTURE
307 {
308 subframe = new wxDocChildFrame
309 (
310 doc,
311 view,
312 wxStaticCast(GetTopWindow(), wxDocParentFrame),
313 wxID_ANY,
314 "Child Frame",
315 wxDefaultPosition,
316 wxSize(300, 300)
317 );
318
319 subframe->Centre(wxBOTH);
320 }
321
322 wxMenu *menuFile = new wxMenu;
323
324 menuFile->Append(wxID_NEW);
325 menuFile->Append(wxID_OPEN);
326 AppendDocumentFileCommands(menuFile, isCanvas);
327 menuFile->AppendSeparator();
328 menuFile->Append(wxID_EXIT);
329
330 wxMenu *menuEdit;
331 if ( isCanvas )
332 {
333 menuEdit = CreateDrawingEditMenu();
334
335 doc->GetCommandProcessor()->SetEditMenu(menuEdit);
336 doc->GetCommandProcessor()->Initialize();
337 }
338 else // text frame
339 {
340 menuEdit = new wxMenu;
341 menuEdit->Append(wxID_COPY);
342 menuEdit->Append(wxID_PASTE);
343 menuEdit->Append(wxID_SELECTALL);
344 }
345
346 CreateMenuBarForFrame(subframe, menuFile, menuEdit);
347
348 subframe->SetIcon(isCanvas ? wxICON(chrt) : wxICON(notepad));
349
350 return subframe;
351 }
352
353 void MyApp::OnAbout(wxCommandEvent& WXUNUSED(event))
354 {
355 wxString modeName;
356 switch ( m_mode )
357 {
358 #if wxUSE_MDI_ARCHITECTURE
359 case Mode_MDI:
360 modeName = "MDI";
361 break;
362 #endif // wxUSE_MDI_ARCHITECTURE
363
364 case Mode_SDI:
365 modeName = "SDI";
366 break;
367
368 case Mode_Single:
369 modeName = "single document";
370 break;
371
372 default:
373 wxFAIL_MSG( "unknown mode ");
374 }
375
376 wxLogMessage
377 (
378 "This is the wxWidgets Document/View Sample\n"
379 "running in %s mode.\n"
380 "\n"
381 "Authors: Julian Smart, Vadim Zeitlin\n"
382 "\n"
383 "Usage: docview [--{mdi,sdi,single}]",
384 modeName
385 );
386 }
387