]>
git.saurik.com Git - wxWidgets.git/blob - samples/docview/docview.cpp
5e0c865bae8e2cf244ef9df950261ecadc3a723a
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
8 // Copyright: (c) 1998 Julian Smart
9 // (c) 2008 Vadim Zeitlin
10 // Licence: wxWindows license
11 /////////////////////////////////////////////////////////////////////////////
14 This sample show document/view support in wxWidgets.
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
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.
30 // ----------------------------------------------------------------------------
32 // ----------------------------------------------------------------------------
34 // For compilers that support precompilation, includes "wx/wx.h".
35 #include "wx/wxprec.h"
43 #include "wx/stockitem.h"
46 #if !wxUSE_DOC_VIEW_ARCHITECTURE
47 #error You must set wxUSE_DOC_VIEW_ARCHITECTURE to 1 in setup.h!
50 #include "wx/docview.h"
51 #include "wx/docmdi.h"
57 #include "wx/cmdline.h"
60 #include "wx/filename.h"
63 #if !defined(__WXMSW__) && !defined(__WXPM__)
66 #include "notepad.xpm"
69 // ----------------------------------------------------------------------------
70 // MyApp implementation
71 // ----------------------------------------------------------------------------
75 BEGIN_EVENT_TABLE(MyApp
, wxApp
)
76 EVT_MENU(wxID_ABOUT
, MyApp::OnAbout
)
81 #if wxUSE_MDI_ARCHITECTURE
91 // constants for the command line options names
92 namespace CmdLineOption
95 const char * const MDI
= "mdi";
96 const char * const SDI
= "sdi";
97 const char * const SINGLE
= "single";
99 } // namespace CmdLineOption
101 void MyApp::OnInitCmdLine(wxCmdLineParser
& parser
)
103 wxApp::OnInitCmdLine(parser
);
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");
113 bool MyApp::OnCmdLineParsed(wxCmdLineParser
& parser
)
115 int numModeOptions
= 0;
117 #if wxUSE_MDI_ARCHITECTURE
118 if ( parser
.Found(CmdLineOption::MDI
) )
123 #endif // wxUSE_MDI_ARCHITECTURE
125 if ( parser
.Found(CmdLineOption::SDI
) )
131 if ( parser
.Found(CmdLineOption::SINGLE
) )
133 m_mode
= Mode_Single
;
137 if ( numModeOptions
> 1 )
139 wxLogError("Only a single option choosing the mode can be given.");
143 return wxApp::OnCmdLineParsed(parser
);
148 if ( !wxApp::OnInit() )
151 SetAppName("DocView Sample");
153 //// Create a document manager
154 wxDocManager
*docManager
= new wxDocManager
;
156 //// Create a template relating drawing documents to their views
157 new wxDocTemplate(docManager
, "Drawing", "*.drw", "", "drw",
158 "Drawing Doc", "Drawing View",
159 CLASSINFO(DrawingDocument
), CLASSINFO(DrawingView
));
160 #if defined( __WXMAC__ ) && wxOSX_USE_CARBON
161 wxFileName::MacRegisterDefaultTypeAndCreator("drw" , 'WXMB' , 'WXMA');
164 if ( m_mode
== Mode_Single
)
166 // If we've only got one window, we only get to edit one document at a
167 // time. Therefore no text editing, just doodling.
168 docManager
->SetMaxDocsOpen(1);
170 else // multiple documents mode: allow documents of different types
172 // Create a template relating text documents to their views
173 new wxDocTemplate(docManager
, "Text", "*.txt;*.text", "", "txt;text",
174 "Text Doc", "Text View",
175 CLASSINFO(TextEditDocument
), CLASSINFO(TextEditView
));
176 #if defined( __WXMAC__ ) && wxOSX_USE_CARBON
177 wxFileName::MacRegisterDefaultTypeAndCreator("txt" , 'TEXT' , 'WXMA');
181 // create the main frame window
183 #if wxUSE_MDI_ARCHITECTURE
184 if ( m_mode
== Mode_MDI
)
186 frame
= new wxDocMDIParentFrame(docManager
, NULL
, wxID_ANY
,
192 #endif // wxUSE_MDI_ARCHITECTURE
194 frame
= new wxDocParentFrame(docManager
, NULL
, wxID_ANY
,
201 wxMenu
*menuFile
= new wxMenu
;
203 menuFile
->Append(wxID_NEW
);
204 menuFile
->Append(wxID_OPEN
);
206 if ( m_mode
== Mode_Single
)
207 AppendDocumentFileCommands(menuFile
, true);
209 menuFile
->AppendSeparator();
210 menuFile
->Append(wxID_EXIT
);
212 // A nice touch: a history of files visited. Use this menu.
213 docManager
->FileHistoryUseMenu(menuFile
);
215 if ( m_mode
== Mode_Single
)
217 m_canvas
= new MyCanvas(NULL
, frame
);
218 m_menuEdit
= CreateDrawingEditMenu();
219 docManager
->CreateNewDocument();
222 CreateMenuBarForFrame(frame
, menuFile
, m_menuEdit
);
224 frame
->SetIcon(wxICON(doc
));
225 frame
->Centre(wxBOTH
);
234 delete wxDocManager::GetDocumentManager();
236 return wxApp::OnExit();
239 void MyApp::AppendDocumentFileCommands(wxMenu
*menu
, bool supportsPrinting
)
241 menu
->Append(wxID_CLOSE
);
242 menu
->Append(wxID_SAVE
);
243 menu
->Append(wxID_SAVEAS
);
245 if ( supportsPrinting
)
247 menu
->AppendSeparator();
248 menu
->Append(wxID_PRINT
);
249 menu
->Append(wxID_PRINT_SETUP
, "Print &Setup...");
250 menu
->Append(wxID_PREVIEW
);
254 wxMenu
*MyApp::CreateDrawingEditMenu()
256 wxMenu
* const menu
= new wxMenu
;
257 menu
->Append(wxID_UNDO
);
258 menu
->Append(wxID_REDO
);
259 menu
->AppendSeparator();
260 menu
->Append(wxID_CUT
, "&Cut last segment");
265 void MyApp::CreateMenuBarForFrame(wxFrame
*frame
, wxMenu
*file
, wxMenu
*edit
)
267 wxMenuBar
*menubar
= new wxMenuBar
;
269 menubar
->Append(file
, wxGetStockLabel(wxID_FILE
));
272 menubar
->Append(edit
, wxGetStockLabel(wxID_EDIT
));
274 wxMenu
*help
= new wxMenu
;
275 help
->Append(wxID_ABOUT
);
276 menubar
->Append(help
, wxGetStockLabel(wxID_HELP
));
278 frame
->SetMenuBar(menubar
);
281 wxFrame
*MyApp::CreateChildFrame(wxDocument
*doc
, wxView
*view
, bool isCanvas
)
283 // create a child frame of appropriate class for the current mode
285 #if wxUSE_MDI_ARCHITECTURE
286 if ( GetMode() == Mode_MDI
)
288 subframe
= new wxDocMDIChildFrame
292 wxStaticCast(GetTopWindow(), wxDocMDIParentFrame
),
300 #endif // wxUSE_MDI_ARCHITECTURE
302 subframe
= new wxDocChildFrame
306 wxStaticCast(GetTopWindow(), wxDocParentFrame
),
313 subframe
->Centre(wxBOTH
);
316 wxMenu
*menuFile
= new wxMenu
;
318 menuFile
->Append(wxID_NEW
);
319 menuFile
->Append(wxID_OPEN
);
320 AppendDocumentFileCommands(menuFile
, isCanvas
);
321 menuFile
->AppendSeparator();
322 menuFile
->Append(wxID_EXIT
);
327 menuEdit
= CreateDrawingEditMenu();
329 doc
->GetCommandProcessor()->SetEditMenu(menuEdit
);
330 doc
->GetCommandProcessor()->Initialize();
334 menuEdit
= new wxMenu
;
335 menuEdit
->Append(wxID_COPY
);
336 menuEdit
->Append(wxID_PASTE
);
337 menuEdit
->Append(wxID_SELECTALL
);
340 CreateMenuBarForFrame(subframe
, menuFile
, menuEdit
);
342 subframe
->SetIcon(isCanvas
? wxICON(chrt
) : wxICON(notepad
));
347 void MyApp::OnAbout(wxCommandEvent
& WXUNUSED(event
))
352 #if wxUSE_MDI_ARCHITECTURE
356 #endif // wxUSE_MDI_ARCHITECTURE
363 modeName
= "single document";
367 wxFAIL_MSG( "unknown mode ");
372 "This is the wxWidgets Document/View Sample\n"
373 "running in %s mode.\n"
375 "Authors: Julian Smart, Vadim Zeitlin\n"
377 "Usage: docview [--{mdi,sdi,single}]",