]>
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
8 // Copyright: (c) 1998 Julian Smart
9 // (c) 2008 Vadim Zeitlin
10 // Licence: wxWindows licence
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"
58 #include "wx/config.h"
61 #include "wx/filename.h"
64 #ifndef wxHAS_IMAGES_IN_RESOURCES
67 #include "notepad.xpm"
70 // ----------------------------------------------------------------------------
71 // MyApp implementation
72 // ----------------------------------------------------------------------------
76 BEGIN_EVENT_TABLE(MyApp
, wxApp
)
77 EVT_MENU(wxID_ABOUT
, MyApp::OnAbout
)
82 #if wxUSE_MDI_ARCHITECTURE
92 // constants for the command line options names
93 namespace CmdLineOption
96 const char * const MDI
= "mdi";
97 const char * const SDI
= "sdi";
98 const char * const SINGLE
= "single";
100 } // namespace CmdLineOption
102 void MyApp::OnInitCmdLine(wxCmdLineParser
& parser
)
104 wxApp::OnInitCmdLine(parser
);
106 parser
.AddSwitch("", CmdLineOption::MDI
,
107 "run in MDI mode: multiple documents, single window");
108 parser
.AddSwitch("", CmdLineOption::SDI
,
109 "run in SDI mode: multiple documents, multiple windows");
110 parser
.AddSwitch("", CmdLineOption::SINGLE
,
111 "run in single document mode");
114 bool MyApp::OnCmdLineParsed(wxCmdLineParser
& parser
)
116 int numModeOptions
= 0;
118 #if wxUSE_MDI_ARCHITECTURE
119 if ( parser
.Found(CmdLineOption::MDI
) )
124 #endif // wxUSE_MDI_ARCHITECTURE
126 if ( parser
.Found(CmdLineOption::SDI
) )
132 if ( parser
.Found(CmdLineOption::SINGLE
) )
134 m_mode
= Mode_Single
;
138 if ( numModeOptions
> 1 )
140 wxLogError("Only a single option choosing the mode can be given.");
144 return wxApp::OnCmdLineParsed(parser
);
149 if ( !wxApp::OnInit() )
152 ::wxInitAllImageHandlers();
154 // Fill in the application information fields before creating wxConfig.
155 SetVendorName("wxWidgets");
156 SetAppName("wx_docview_sample");
157 SetAppDisplayName("wxWidgets DocView Sample");
159 //// Create a document manager
160 wxDocManager
*docManager
= new wxDocManager
;
162 //// Create a template relating drawing documents to their views
163 new wxDocTemplate(docManager
, "Drawing", "*.drw", "", "drw",
164 "Drawing Doc", "Drawing View",
165 CLASSINFO(DrawingDocument
), CLASSINFO(DrawingView
));
166 #if defined( __WXMAC__ ) && wxOSX_USE_CARBON
167 wxFileName::MacRegisterDefaultTypeAndCreator("drw" , 'WXMB' , 'WXMA');
170 if ( m_mode
== Mode_Single
)
172 // If we've only got one window, we only get to edit one document at a
173 // time. Therefore no text editing, just doodling.
174 docManager
->SetMaxDocsOpen(1);
176 else // multiple documents mode: allow documents of different types
178 // Create a template relating text documents to their views
179 new wxDocTemplate(docManager
, "Text", "*.txt;*.text", "", "txt;text",
180 "Text Doc", "Text View",
181 CLASSINFO(TextEditDocument
), CLASSINFO(TextEditView
));
182 #if defined( __WXMAC__ ) && wxOSX_USE_CARBON
183 wxFileName::MacRegisterDefaultTypeAndCreator("txt" , 'TEXT' , 'WXMA');
185 // Create a template relating image documents to their views
186 new wxDocTemplate(docManager
, "Image", "*.png;*.jpg", "", "png;jpg",
187 "Image Doc", "Image View",
188 CLASSINFO(ImageDocument
), CLASSINFO(ImageView
));
191 // create the main frame window
193 #if wxUSE_MDI_ARCHITECTURE
194 if ( m_mode
== Mode_MDI
)
196 frame
= new wxDocMDIParentFrame(docManager
, NULL
, wxID_ANY
,
202 #endif // wxUSE_MDI_ARCHITECTURE
204 frame
= new wxDocParentFrame(docManager
, NULL
, wxID_ANY
,
211 wxMenu
*menuFile
= new wxMenu
;
213 menuFile
->Append(wxID_NEW
);
214 menuFile
->Append(wxID_OPEN
);
216 if ( m_mode
== Mode_Single
)
217 AppendDocumentFileCommands(menuFile
, true);
219 menuFile
->AppendSeparator();
220 menuFile
->Append(wxID_EXIT
);
222 // A nice touch: a history of files visited. Use this menu.
223 docManager
->FileHistoryUseMenu(menuFile
);
225 docManager
->FileHistoryLoad(*wxConfig::Get());
226 #endif // wxUSE_CONFIG
229 if ( m_mode
== Mode_Single
)
231 m_canvas
= new MyCanvas(NULL
, frame
);
232 m_menuEdit
= CreateDrawingEditMenu();
233 docManager
->CreateNewDocument();
236 CreateMenuBarForFrame(frame
, menuFile
, m_menuEdit
);
238 frame
->SetIcon(wxICON(doc
));
247 wxDocManager
* const manager
= wxDocManager::GetDocumentManager();
249 manager
->FileHistorySave(*wxConfig::Get());
250 #endif // wxUSE_CONFIG
253 return wxApp::OnExit();
256 void MyApp::AppendDocumentFileCommands(wxMenu
*menu
, bool supportsPrinting
)
258 menu
->Append(wxID_CLOSE
);
259 menu
->Append(wxID_SAVE
);
260 menu
->Append(wxID_SAVEAS
);
261 menu
->Append(wxID_REVERT
, _("Re&vert..."));
263 if ( supportsPrinting
)
265 menu
->AppendSeparator();
266 menu
->Append(wxID_PRINT
);
267 menu
->Append(wxID_PRINT_SETUP
, "Print &Setup...");
268 menu
->Append(wxID_PREVIEW
);
272 wxMenu
*MyApp::CreateDrawingEditMenu()
274 wxMenu
* const menu
= new wxMenu
;
275 menu
->Append(wxID_UNDO
);
276 menu
->Append(wxID_REDO
);
277 menu
->AppendSeparator();
278 menu
->Append(wxID_CUT
, "&Cut last segment");
283 void MyApp::CreateMenuBarForFrame(wxFrame
*frame
, wxMenu
*file
, wxMenu
*edit
)
285 wxMenuBar
*menubar
= new wxMenuBar
;
287 menubar
->Append(file
, wxGetStockLabel(wxID_FILE
));
290 menubar
->Append(edit
, wxGetStockLabel(wxID_EDIT
));
292 wxMenu
*help
= new wxMenu
;
293 help
->Append(wxID_ABOUT
);
294 menubar
->Append(help
, wxGetStockLabel(wxID_HELP
));
296 frame
->SetMenuBar(menubar
);
299 wxFrame
*MyApp::CreateChildFrame(wxView
*view
, bool isCanvas
)
301 // create a child frame of appropriate class for the current mode
303 wxDocument
*doc
= view
->GetDocument();
304 #if wxUSE_MDI_ARCHITECTURE
305 if ( GetMode() == Mode_MDI
)
307 subframe
= new wxDocMDIChildFrame
311 wxStaticCast(GetTopWindow(), wxDocMDIParentFrame
),
319 #endif // wxUSE_MDI_ARCHITECTURE
321 subframe
= new wxDocChildFrame
325 wxStaticCast(GetTopWindow(), wxDocParentFrame
),
335 wxMenu
*menuFile
= new wxMenu
;
337 menuFile
->Append(wxID_NEW
);
338 menuFile
->Append(wxID_OPEN
);
339 AppendDocumentFileCommands(menuFile
, isCanvas
);
340 menuFile
->AppendSeparator();
341 menuFile
->Append(wxID_EXIT
);
346 menuEdit
= CreateDrawingEditMenu();
348 doc
->GetCommandProcessor()->SetEditMenu(menuEdit
);
349 doc
->GetCommandProcessor()->Initialize();
353 menuEdit
= new wxMenu
;
354 menuEdit
->Append(wxID_COPY
);
355 menuEdit
->Append(wxID_PASTE
);
356 menuEdit
->Append(wxID_SELECTALL
);
359 CreateMenuBarForFrame(subframe
, menuFile
, menuEdit
);
361 subframe
->SetIcon(isCanvas
? wxICON(chrt
) : wxICON(notepad
));
366 void MyApp::OnAbout(wxCommandEvent
& WXUNUSED(event
))
371 #if wxUSE_MDI_ARCHITECTURE
375 #endif // wxUSE_MDI_ARCHITECTURE
382 modeName
= "single document";
386 wxFAIL_MSG( "unknown mode ");
391 "This is the wxWidgets Document/View Sample\n"
392 "running in %s mode.\n"
394 "Authors: Julian Smart, Vadim Zeitlin\n"
396 "Usage: docview [--{mdi,sdi,single}]",