]> git.saurik.com Git - wxWidgets.git/blame - samples/docview/docview.cpp
Derive wxStatusBar from wxControl and not wxWindow.
[wxWidgets.git] / samples / docview / docview.cpp
CommitLineData
457814b5 1/////////////////////////////////////////////////////////////////////////////
2d1df0fc 2// Name: samples/docview/docview.cpp
457814b5
JS
3// Purpose: Document/view demo
4// Author: Julian Smart
2d1df0fc 5// Modified by: Vadim Zeitlin: merge with the MDI version and general cleanup
457814b5
JS
6// Created: 04/01/98
7// RCS-ID: $Id$
2d1df0fc
VZ
8// Copyright: (c) 1998 Julian Smart
9// (c) 2008 Vadim Zeitlin
526954c5 10// Licence: wxWindows licence
457814b5
JS
11/////////////////////////////////////////////////////////////////////////////
12
457814b5 13/*
2d1df0fc
VZ
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// ----------------------------------------------------------------------------
457814b5
JS
33
34// For compilers that support precompilation, includes "wx/wx.h".
35#include "wx/wxprec.h"
36
37#ifdef __BORLANDC__
2d1df0fc 38 #pragma hdrstop
457814b5
JS
39#endif
40
41#ifndef WX_PRECOMP
2d1df0fc
VZ
42 #include "wx/wx.h"
43 #include "wx/stockitem.h"
457814b5
JS
44#endif
45
e4b19d9b 46#if !wxUSE_DOC_VIEW_ARCHITECTURE
2d1df0fc 47 #error You must set wxUSE_DOC_VIEW_ARCHITECTURE to 1 in setup.h!
457814b5
JS
48#endif
49
50#include "wx/docview.h"
2d1df0fc 51#include "wx/docmdi.h"
457814b5
JS
52
53#include "docview.h"
54#include "doc.h"
55#include "view.h"
2d1df0fc
VZ
56
57#include "wx/cmdline.h"
58
eb017efe 59#ifdef __WXMAC__
2d1df0fc 60 #include "wx/filename.h"
eb017efe 61#endif
457814b5 62
2d1df0fc
VZ
63#if !defined(__WXMSW__) && !defined(__WXPM__)
64 #include "doc.xpm"
65 #include "chart.xpm"
66 #include "notepad.xpm"
67#endif
457814b5 68
2d1df0fc
VZ
69// ----------------------------------------------------------------------------
70// MyApp implementation
71// ----------------------------------------------------------------------------
457814b5
JS
72
73IMPLEMENT_APP(MyApp)
74
2d1df0fc
VZ
75BEGIN_EVENT_TABLE(MyApp, wxApp)
76 EVT_MENU(wxID_ABOUT, MyApp::OnAbout)
77END_EVENT_TABLE()
78
79MyApp::MyApp()
457814b5 80{
2d1df0fc
VZ
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;
457814b5
JS
89}
90
2d1df0fc
VZ
91// constants for the command line options names
92namespace CmdLineOption
457814b5 93{
6bdf5153 94
2d1df0fc
VZ
95const char * const MDI = "mdi";
96const char * const SDI = "sdi";
97const char * const SINGLE = "single";
98
99} // namespace CmdLineOption
100
101void 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
113bool 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 }
45e6e6f8 130
2d1df0fc 131 if ( parser.Found(CmdLineOption::SINGLE) )
457814b5 132 {
2d1df0fc
VZ
133 m_mode = Mode_Single;
134 numModeOptions++;
457814b5 135 }
6bdf5153 136
2d1df0fc
VZ
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
146bool MyApp::OnInit()
147{
148 if ( !wxApp::OnInit() )
149 return false;
150
f37f49b6
JS
151 ::wxInitAllImageHandlers();
152
2d1df0fc
VZ
153 SetAppName("DocView Sample");
154
f6bcfd97 155 //// Create a document manager
2d1df0fc 156 wxDocManager *docManager = new wxDocManager;
3d0145a5 157
f6bcfd97 158 //// Create a template relating drawing documents to their views
2d1df0fc
VZ
159 new wxDocTemplate(docManager, "Drawing", "*.drw", "", "drw",
160 "Drawing Doc", "Drawing View",
161 CLASSINFO(DrawingDocument), CLASSINFO(DrawingView));
fa9b55df 162#if defined( __WXMAC__ ) && wxOSX_USE_CARBON
2d1df0fc 163 wxFileName::MacRegisterDefaultTypeAndCreator("drw" , 'WXMB' , 'WXMA');
eb017efe 164#endif
6bdf5153 165
2d1df0fc 166 if ( m_mode == Mode_Single )
f6bcfd97 167 {
2d1df0fc
VZ
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);
f6bcfd97 171 }
2d1df0fc 172 else // multiple documents mode: allow documents of different types
eb017efe 173 {
2d1df0fc
VZ
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));
fa9b55df 178#if defined( __WXMAC__ ) && wxOSX_USE_CARBON
2d1df0fc 179 wxFileName::MacRegisterDefaultTypeAndCreator("txt" , 'TEXT' , 'WXMA');
eb017efe 180#endif
f37f49b6
JS
181 // Create a template relating image documents to their views
182 new wxDocTemplate(docManager, "Image", "*.png;*.jpg", "", "png;jpg",
183 "Image Doc", "Image View",
2d4a03f8 184 CLASSINFO(ImageDocument), CLASSINFO(ImageView));
eb017efe 185 }
6bdf5153 186
2d1df0fc
VZ
187 // create the main frame window
188 wxFrame *frame;
189#if wxUSE_MDI_ARCHITECTURE
190 if ( m_mode == Mode_MDI )
f6bcfd97 191 {
2d1df0fc
VZ
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));
f6bcfd97 204 }
6bdf5153 205
2d1df0fc
VZ
206 // and its menu bar
207 wxMenu *menuFile = new wxMenu;
6bdf5153 208
2d1df0fc
VZ
209 menuFile->Append(wxID_NEW);
210 menuFile->Append(wxID_OPEN);
6bdf5153 211
2d1df0fc
VZ
212 if ( m_mode == Mode_Single )
213 AppendDocumentFileCommands(menuFile, true);
6bdf5153 214
2d1df0fc
VZ
215 menuFile->AppendSeparator();
216 menuFile->Append(wxID_EXIT);
6bdf5153 217
2d1df0fc
VZ
218 // A nice touch: a history of files visited. Use this menu.
219 docManager->FileHistoryUseMenu(menuFile);
6bdf5153 220
2d1df0fc
VZ
221 if ( m_mode == Mode_Single )
222 {
223 m_canvas = new MyCanvas(NULL, frame);
224 m_menuEdit = CreateDrawingEditMenu();
abee7f47 225 docManager->CreateNewDocument();
2d1df0fc 226 }
6bdf5153 227
2d1df0fc 228 CreateMenuBarForFrame(frame, menuFile, m_menuEdit);
6bdf5153 229
2d1df0fc 230 frame->SetIcon(wxICON(doc));
9b341e6f
VZ
231 frame->Centre();
232 frame->Show();
6bdf5153 233
f6bcfd97 234 SetTopWindow(frame);
f2aea0d1 235 return true;
457814b5
JS
236}
237
2d1df0fc 238int MyApp::OnExit()
457814b5 239{
2d1df0fc
VZ
240 delete wxDocManager::GetDocumentManager();
241
242 return wxApp::OnExit();
457814b5
JS
243}
244
2d1df0fc
VZ
245void MyApp::AppendDocumentFileCommands(wxMenu *menu, bool supportsPrinting)
246{
247 menu->Append(wxID_CLOSE);
248 menu->Append(wxID_SAVE);
249 menu->Append(wxID_SAVEAS);
4311588b 250 menu->Append(wxID_REVERT, _("Re&vert..."));
f6bcfd97 251
2d1df0fc
VZ
252 if ( supportsPrinting )
253 {
254 menu->AppendSeparator();
255 menu->Append(wxID_PRINT);
256 menu->Append(wxID_PRINT_SETUP, "Print &Setup...");
257 menu->Append(wxID_PREVIEW);
258 }
259}
260
261wxMenu *MyApp::CreateDrawingEditMenu()
457814b5 262{
2d1df0fc
VZ
263 wxMenu * const menu = new wxMenu;
264 menu->Append(wxID_UNDO);
265 menu->Append(wxID_REDO);
266 menu->AppendSeparator();
267 menu->Append(wxID_CUT, "&Cut last segment");
6bdf5153 268
2d1df0fc
VZ
269 return menu;
270}
6bdf5153 271
2d1df0fc
VZ
272void MyApp::CreateMenuBarForFrame(wxFrame *frame, wxMenu *file, wxMenu *edit)
273{
274 wxMenuBar *menubar = new wxMenuBar;
6bdf5153 275
2d1df0fc 276 menubar->Append(file, wxGetStockLabel(wxID_FILE));
6bdf5153 277
2d1df0fc
VZ
278 if ( edit )
279 menubar->Append(edit, wxGetStockLabel(wxID_EDIT));
6bdf5153 280
2d1df0fc
VZ
281 wxMenu *help= new wxMenu;
282 help->Append(wxID_ABOUT);
283 menubar->Append(help, wxGetStockLabel(wxID_HELP));
6bdf5153 284
2d1df0fc
VZ
285 frame->SetMenuBar(menubar);
286}
6bdf5153 287
9b341e6f 288wxFrame *MyApp::CreateChildFrame(wxView *view, bool isCanvas)
2d1df0fc
VZ
289{
290 // create a child frame of appropriate class for the current mode
291 wxFrame *subframe;
9b341e6f 292 wxDocument *doc = view->GetDocument();
2d1df0fc
VZ
293#if wxUSE_MDI_ARCHITECTURE
294 if ( GetMode() == Mode_MDI )
295 {
296 subframe = new wxDocMDIChildFrame
297 (
298 doc,
299 view,
300 wxStaticCast(GetTopWindow(), wxDocMDIParentFrame),
301 wxID_ANY,
302 "Child Frame",
303 wxDefaultPosition,
304 wxSize(300, 300)
305 );
f6bcfd97 306 }
6bdf5153 307 else
2d1df0fc 308#endif // wxUSE_MDI_ARCHITECTURE
6bdf5153 309 {
2d1df0fc
VZ
310 subframe = new wxDocChildFrame
311 (
312 doc,
313 view,
314 wxStaticCast(GetTopWindow(), wxDocParentFrame),
315 wxID_ANY,
316 "Child Frame",
317 wxDefaultPosition,
318 wxSize(300, 300)
319 );
320
9b341e6f 321 subframe->Centre();
6bdf5153
VZ
322 }
323
2d1df0fc 324 wxMenu *menuFile = new wxMenu;
6bdf5153 325
2d1df0fc
VZ
326 menuFile->Append(wxID_NEW);
327 menuFile->Append(wxID_OPEN);
328 AppendDocumentFileCommands(menuFile, isCanvas);
329 menuFile->AppendSeparator();
330 menuFile->Append(wxID_EXIT);
6bdf5153 331
2d1df0fc
VZ
332 wxMenu *menuEdit;
333 if ( isCanvas )
334 {
335 menuEdit = CreateDrawingEditMenu();
457814b5 336
2d1df0fc
VZ
337 doc->GetCommandProcessor()->SetEditMenu(menuEdit);
338 doc->GetCommandProcessor()->Initialize();
339 }
340 else // text frame
341 {
342 menuEdit = new wxMenu;
343 menuEdit->Append(wxID_COPY);
344 menuEdit->Append(wxID_PASTE);
345 menuEdit->Append(wxID_SELECTALL);
346 }
f6bcfd97 347
2d1df0fc 348 CreateMenuBarForFrame(subframe, menuFile, menuEdit);
457814b5 349
2d1df0fc 350 subframe->SetIcon(isCanvas ? wxICON(chrt) : wxICON(notepad));
457814b5 351
2d1df0fc 352 return subframe;
457814b5
JS
353}
354
2d1df0fc 355void MyApp::OnAbout(wxCommandEvent& WXUNUSED(event))
457814b5 356{
2d1df0fc
VZ
357 wxString modeName;
358 switch ( m_mode )
359 {
360#if wxUSE_MDI_ARCHITECTURE
361 case Mode_MDI:
362 modeName = "MDI";
363 break;
364#endif // wxUSE_MDI_ARCHITECTURE
365
366 case Mode_SDI:
367 modeName = "SDI";
368 break;
369
370 case Mode_Single:
371 modeName = "single document";
372 break;
373
374 default:
375 wxFAIL_MSG( "unknown mode ");
376 }
457814b5 377
2d1df0fc
VZ
378 wxLogMessage
379 (
380 "This is the wxWidgets Document/View Sample\n"
381 "running in %s mode.\n"
382 "\n"
383 "Authors: Julian Smart, Vadim Zeitlin\n"
384 "\n"
385 "Usage: docview [--{mdi,sdi,single}]",
386 modeName
387 );
457814b5
JS
388}
389