]>
git.saurik.com Git - wxWidgets.git/blob - 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
7 // Copyright: (c) 1998 Julian Smart
8 // (c) 2008 Vadim Zeitlin
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
13 This sample show document/view support in wxWidgets.
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
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.
29 // ----------------------------------------------------------------------------
31 // ----------------------------------------------------------------------------
33 // For compilers that support precompilation, includes "wx/wx.h".
34 #include "wx/wxprec.h"
42 #include "wx/stockitem.h"
45 #if !wxUSE_DOC_VIEW_ARCHITECTURE
46 #error You must set wxUSE_DOC_VIEW_ARCHITECTURE to 1 in setup.h!
49 #include "wx/docview.h"
50 #include "wx/docmdi.h"
56 #include "wx/cmdline.h"
57 #include "wx/config.h"
60 #include "wx/filename.h"
63 #ifndef wxHAS_IMAGES_IN_RESOURCES
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 ::wxInitAllImageHandlers();
153 // Fill in the application information fields before creating wxConfig.
154 SetVendorName("wxWidgets");
155 SetAppName("wx_docview_sample");
156 SetAppDisplayName("wxWidgets DocView Sample");
158 //// Create a document manager
159 wxDocManager
*docManager
= new wxDocManager
;
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');
169 if ( m_mode
== Mode_Single
)
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);
175 else // multiple documents mode: allow documents of different types
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');
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
));
190 // create the main frame window
192 #if wxUSE_MDI_ARCHITECTURE
193 if ( m_mode
== Mode_MDI
)
195 frame
= new wxDocMDIParentFrame(docManager
, NULL
, wxID_ANY
,
201 #endif // wxUSE_MDI_ARCHITECTURE
203 frame
= new wxDocParentFrame(docManager
, NULL
, wxID_ANY
,
210 wxMenu
*menuFile
= new wxMenu
;
212 menuFile
->Append(wxID_NEW
);
213 menuFile
->Append(wxID_OPEN
);
215 if ( m_mode
== Mode_Single
)
216 AppendDocumentFileCommands(menuFile
, true);
218 menuFile
->AppendSeparator();
219 menuFile
->Append(wxID_EXIT
);
221 // A nice touch: a history of files visited. Use this menu.
222 docManager
->FileHistoryUseMenu(menuFile
);
224 docManager
->FileHistoryLoad(*wxConfig::Get());
225 #endif // wxUSE_CONFIG
228 if ( m_mode
== Mode_Single
)
230 m_canvas
= new MyCanvas(NULL
, frame
);
231 m_menuEdit
= CreateDrawingEditMenu();
232 docManager
->CreateNewDocument();
235 CreateMenuBarForFrame(frame
, menuFile
, m_menuEdit
);
237 frame
->SetIcon(wxICON(doc
));
246 wxDocManager
* const manager
= wxDocManager::GetDocumentManager();
248 manager
->FileHistorySave(*wxConfig::Get());
249 #endif // wxUSE_CONFIG
252 return wxApp::OnExit();
255 void MyApp::AppendDocumentFileCommands(wxMenu
*menu
, bool supportsPrinting
)
257 menu
->Append(wxID_CLOSE
);
258 menu
->Append(wxID_SAVE
);
259 menu
->Append(wxID_SAVEAS
);
260 menu
->Append(wxID_REVERT
, _("Re&vert..."));
262 if ( supportsPrinting
)
264 menu
->AppendSeparator();
265 menu
->Append(wxID_PRINT
);
266 menu
->Append(wxID_PRINT_SETUP
, "Print &Setup...");
267 menu
->Append(wxID_PREVIEW
);
271 wxMenu
*MyApp::CreateDrawingEditMenu()
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");
282 void MyApp::CreateMenuBarForFrame(wxFrame
*frame
, wxMenu
*file
, wxMenu
*edit
)
284 wxMenuBar
*menubar
= new wxMenuBar
;
286 menubar
->Append(file
, wxGetStockLabel(wxID_FILE
));
289 menubar
->Append(edit
, wxGetStockLabel(wxID_EDIT
));
291 wxMenu
*help
= new wxMenu
;
292 help
->Append(wxID_ABOUT
);
293 menubar
->Append(help
, wxGetStockLabel(wxID_HELP
));
295 frame
->SetMenuBar(menubar
);
298 wxFrame
*MyApp::CreateChildFrame(wxView
*view
, bool isCanvas
)
300 // create a child frame of appropriate class for the current mode
302 wxDocument
*doc
= view
->GetDocument();
303 #if wxUSE_MDI_ARCHITECTURE
304 if ( GetMode() == Mode_MDI
)
306 subframe
= new wxDocMDIChildFrame
310 wxStaticCast(GetTopWindow(), wxDocMDIParentFrame
),
318 #endif // wxUSE_MDI_ARCHITECTURE
320 subframe
= new wxDocChildFrame
324 wxStaticCast(GetTopWindow(), wxDocParentFrame
),
334 wxMenu
*menuFile
= new wxMenu
;
336 menuFile
->Append(wxID_NEW
);
337 menuFile
->Append(wxID_OPEN
);
338 AppendDocumentFileCommands(menuFile
, isCanvas
);
339 menuFile
->AppendSeparator();
340 menuFile
->Append(wxID_EXIT
);
345 menuEdit
= CreateDrawingEditMenu();
347 doc
->GetCommandProcessor()->SetEditMenu(menuEdit
);
348 doc
->GetCommandProcessor()->Initialize();
352 menuEdit
= new wxMenu
;
353 menuEdit
->Append(wxID_COPY
);
354 menuEdit
->Append(wxID_PASTE
);
355 menuEdit
->Append(wxID_SELECTALL
);
358 CreateMenuBarForFrame(subframe
, menuFile
, menuEdit
);
360 subframe
->SetIcon(isCanvas
? wxICON(chrt
) : wxICON(notepad
));
365 void MyApp::OnAbout(wxCommandEvent
& WXUNUSED(event
))
370 #if wxUSE_MDI_ARCHITECTURE
374 #endif // wxUSE_MDI_ARCHITECTURE
381 modeName
= "single document";
385 wxFAIL_MSG( "unknown mode ");
389 const int docsCount
=
390 wxDocManager::GetDocumentManager()->GetDocuments().GetCount();
392 const int docsCount
=
393 wxDocManager::GetDocumentManager()->GetDocumentsVector().size();
398 "This is the wxWidgets Document/View Sample\n"
399 "running in %s mode.\n"
400 "%d open documents.\n"
402 "Authors: Julian Smart, Vadim Zeitlin\n"
404 "Usage: docview [--{mdi,sdi,single}]",