]>
Commit | Line | Data |
---|---|---|
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 | |
2f6c54eb | 10 | // Licence: wxWindows license |
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 | |
73 | IMPLEMENT_APP(MyApp) | |
74 | ||
2d1df0fc VZ |
75 | BEGIN_EVENT_TABLE(MyApp, wxApp) |
76 | EVT_MENU(wxID_ABOUT, MyApp::OnAbout) | |
77 | END_EVENT_TABLE() | |
78 | ||
79 | MyApp::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 |
92 | namespace CmdLineOption | |
457814b5 | 93 | { |
6bdf5153 | 94 | |
2d1df0fc VZ |
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 | } | |
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 | ||
146 | bool MyApp::OnInit() | |
147 | { | |
148 | if ( !wxApp::OnInit() ) | |
149 | return false; | |
150 | ||
151 | SetAppName("DocView Sample"); | |
152 | ||
f6bcfd97 | 153 | //// Create a document manager |
2d1df0fc | 154 | wxDocManager *docManager = new wxDocManager; |
3d0145a5 | 155 | |
f6bcfd97 | 156 | //// Create a template relating drawing documents to their views |
2d1df0fc VZ |
157 | new wxDocTemplate(docManager, "Drawing", "*.drw", "", "drw", |
158 | "Drawing Doc", "Drawing View", | |
159 | CLASSINFO(DrawingDocument), CLASSINFO(DrawingView)); | |
fa9b55df | 160 | #if defined( __WXMAC__ ) && wxOSX_USE_CARBON |
2d1df0fc | 161 | wxFileName::MacRegisterDefaultTypeAndCreator("drw" , 'WXMB' , 'WXMA'); |
eb017efe | 162 | #endif |
6bdf5153 | 163 | |
2d1df0fc | 164 | if ( m_mode == Mode_Single ) |
f6bcfd97 | 165 | { |
2d1df0fc VZ |
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); | |
f6bcfd97 | 169 | } |
2d1df0fc | 170 | else // multiple documents mode: allow documents of different types |
eb017efe | 171 | { |
2d1df0fc VZ |
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)); | |
fa9b55df | 176 | #if defined( __WXMAC__ ) && wxOSX_USE_CARBON |
2d1df0fc | 177 | wxFileName::MacRegisterDefaultTypeAndCreator("txt" , 'TEXT' , 'WXMA'); |
eb017efe SC |
178 | #endif |
179 | } | |
6bdf5153 | 180 | |
2d1df0fc VZ |
181 | // create the main frame window |
182 | wxFrame *frame; | |
183 | #if wxUSE_MDI_ARCHITECTURE | |
184 | if ( m_mode == Mode_MDI ) | |
f6bcfd97 | 185 | { |
2d1df0fc VZ |
186 | frame = new wxDocMDIParentFrame(docManager, NULL, wxID_ANY, |
187 | GetAppDisplayName(), | |
188 | wxDefaultPosition, | |
189 | wxSize(500, 400)); | |
190 | } | |
191 | else | |
192 | #endif // wxUSE_MDI_ARCHITECTURE | |
193 | { | |
194 | frame = new wxDocParentFrame(docManager, NULL, wxID_ANY, | |
195 | GetAppDisplayName(), | |
196 | wxDefaultPosition, | |
197 | wxSize(500, 400)); | |
f6bcfd97 | 198 | } |
6bdf5153 | 199 | |
2d1df0fc VZ |
200 | // and its menu bar |
201 | wxMenu *menuFile = new wxMenu; | |
6bdf5153 | 202 | |
2d1df0fc VZ |
203 | menuFile->Append(wxID_NEW); |
204 | menuFile->Append(wxID_OPEN); | |
6bdf5153 | 205 | |
2d1df0fc VZ |
206 | if ( m_mode == Mode_Single ) |
207 | AppendDocumentFileCommands(menuFile, true); | |
6bdf5153 | 208 | |
2d1df0fc VZ |
209 | menuFile->AppendSeparator(); |
210 | menuFile->Append(wxID_EXIT); | |
6bdf5153 | 211 | |
2d1df0fc VZ |
212 | // A nice touch: a history of files visited. Use this menu. |
213 | docManager->FileHistoryUseMenu(menuFile); | |
6bdf5153 | 214 | |
2d1df0fc VZ |
215 | if ( m_mode == Mode_Single ) |
216 | { | |
217 | m_canvas = new MyCanvas(NULL, frame); | |
218 | m_menuEdit = CreateDrawingEditMenu(); | |
219 | } | |
6bdf5153 | 220 | |
2d1df0fc | 221 | CreateMenuBarForFrame(frame, menuFile, m_menuEdit); |
6bdf5153 | 222 | |
2d1df0fc | 223 | frame->SetIcon(wxICON(doc)); |
f6bcfd97 | 224 | frame->Centre(wxBOTH); |
f2aea0d1 | 225 | frame->Show(true); |
6bdf5153 | 226 | |
f6bcfd97 | 227 | SetTopWindow(frame); |
f2aea0d1 | 228 | return true; |
457814b5 JS |
229 | } |
230 | ||
2d1df0fc | 231 | int MyApp::OnExit() |
457814b5 | 232 | { |
2d1df0fc VZ |
233 | delete wxDocManager::GetDocumentManager(); |
234 | ||
235 | return wxApp::OnExit(); | |
457814b5 JS |
236 | } |
237 | ||
2d1df0fc VZ |
238 | void MyApp::AppendDocumentFileCommands(wxMenu *menu, bool supportsPrinting) |
239 | { | |
240 | menu->Append(wxID_CLOSE); | |
241 | menu->Append(wxID_SAVE); | |
242 | menu->Append(wxID_SAVEAS); | |
f6bcfd97 | 243 | |
2d1df0fc VZ |
244 | if ( supportsPrinting ) |
245 | { | |
246 | menu->AppendSeparator(); | |
247 | menu->Append(wxID_PRINT); | |
248 | menu->Append(wxID_PRINT_SETUP, "Print &Setup..."); | |
249 | menu->Append(wxID_PREVIEW); | |
250 | } | |
251 | } | |
252 | ||
253 | wxMenu *MyApp::CreateDrawingEditMenu() | |
457814b5 | 254 | { |
2d1df0fc VZ |
255 | wxMenu * const menu = new wxMenu; |
256 | menu->Append(wxID_UNDO); | |
257 | menu->Append(wxID_REDO); | |
258 | menu->AppendSeparator(); | |
259 | menu->Append(wxID_CUT, "&Cut last segment"); | |
6bdf5153 | 260 | |
2d1df0fc VZ |
261 | return menu; |
262 | } | |
6bdf5153 | 263 | |
2d1df0fc VZ |
264 | void MyApp::CreateMenuBarForFrame(wxFrame *frame, wxMenu *file, wxMenu *edit) |
265 | { | |
266 | wxMenuBar *menubar = new wxMenuBar; | |
6bdf5153 | 267 | |
2d1df0fc | 268 | menubar->Append(file, wxGetStockLabel(wxID_FILE)); |
6bdf5153 | 269 | |
2d1df0fc VZ |
270 | if ( edit ) |
271 | menubar->Append(edit, wxGetStockLabel(wxID_EDIT)); | |
6bdf5153 | 272 | |
2d1df0fc VZ |
273 | wxMenu *help= new wxMenu; |
274 | help->Append(wxID_ABOUT); | |
275 | menubar->Append(help, wxGetStockLabel(wxID_HELP)); | |
6bdf5153 | 276 | |
2d1df0fc VZ |
277 | frame->SetMenuBar(menubar); |
278 | } | |
6bdf5153 | 279 | |
2d1df0fc VZ |
280 | wxFrame *MyApp::CreateChildFrame(wxDocument *doc, wxView *view, bool isCanvas) |
281 | { | |
282 | // create a child frame of appropriate class for the current mode | |
283 | wxFrame *subframe; | |
284 | #if wxUSE_MDI_ARCHITECTURE | |
285 | if ( GetMode() == Mode_MDI ) | |
286 | { | |
287 | subframe = new wxDocMDIChildFrame | |
288 | ( | |
289 | doc, | |
290 | view, | |
291 | wxStaticCast(GetTopWindow(), wxDocMDIParentFrame), | |
292 | wxID_ANY, | |
293 | "Child Frame", | |
294 | wxDefaultPosition, | |
295 | wxSize(300, 300) | |
296 | ); | |
f6bcfd97 | 297 | } |
6bdf5153 | 298 | else |
2d1df0fc | 299 | #endif // wxUSE_MDI_ARCHITECTURE |
6bdf5153 | 300 | { |
2d1df0fc VZ |
301 | subframe = new wxDocChildFrame |
302 | ( | |
303 | doc, | |
304 | view, | |
305 | wxStaticCast(GetTopWindow(), wxDocParentFrame), | |
306 | wxID_ANY, | |
307 | "Child Frame", | |
308 | wxDefaultPosition, | |
309 | wxSize(300, 300) | |
310 | ); | |
311 | ||
312 | subframe->Centre(wxBOTH); | |
6bdf5153 VZ |
313 | } |
314 | ||
2d1df0fc | 315 | wxMenu *menuFile = new wxMenu; |
6bdf5153 | 316 | |
2d1df0fc VZ |
317 | menuFile->Append(wxID_NEW); |
318 | menuFile->Append(wxID_OPEN); | |
319 | AppendDocumentFileCommands(menuFile, isCanvas); | |
320 | menuFile->AppendSeparator(); | |
321 | menuFile->Append(wxID_EXIT); | |
6bdf5153 | 322 | |
2d1df0fc VZ |
323 | wxMenu *menuEdit; |
324 | if ( isCanvas ) | |
325 | { | |
326 | menuEdit = CreateDrawingEditMenu(); | |
457814b5 | 327 | |
2d1df0fc VZ |
328 | doc->GetCommandProcessor()->SetEditMenu(menuEdit); |
329 | doc->GetCommandProcessor()->Initialize(); | |
330 | } | |
331 | else // text frame | |
332 | { | |
333 | menuEdit = new wxMenu; | |
334 | menuEdit->Append(wxID_COPY); | |
335 | menuEdit->Append(wxID_PASTE); | |
336 | menuEdit->Append(wxID_SELECTALL); | |
337 | } | |
f6bcfd97 | 338 | |
2d1df0fc | 339 | CreateMenuBarForFrame(subframe, menuFile, menuEdit); |
457814b5 | 340 | |
2d1df0fc | 341 | subframe->SetIcon(isCanvas ? wxICON(chrt) : wxICON(notepad)); |
457814b5 | 342 | |
2d1df0fc | 343 | return subframe; |
457814b5 JS |
344 | } |
345 | ||
2d1df0fc | 346 | void MyApp::OnAbout(wxCommandEvent& WXUNUSED(event)) |
457814b5 | 347 | { |
2d1df0fc VZ |
348 | wxString modeName; |
349 | switch ( m_mode ) | |
350 | { | |
351 | #if wxUSE_MDI_ARCHITECTURE | |
352 | case Mode_MDI: | |
353 | modeName = "MDI"; | |
354 | break; | |
355 | #endif // wxUSE_MDI_ARCHITECTURE | |
356 | ||
357 | case Mode_SDI: | |
358 | modeName = "SDI"; | |
359 | break; | |
360 | ||
361 | case Mode_Single: | |
362 | modeName = "single document"; | |
363 | break; | |
364 | ||
365 | default: | |
366 | wxFAIL_MSG( "unknown mode "); | |
367 | } | |
457814b5 | 368 | |
2d1df0fc VZ |
369 | wxLogMessage |
370 | ( | |
371 | "This is the wxWidgets Document/View Sample\n" | |
372 | "running in %s mode.\n" | |
373 | "\n" | |
374 | "Authors: Julian Smart, Vadim Zeitlin\n" | |
375 | "\n" | |
376 | "Usage: docview [--{mdi,sdi,single}]", | |
377 | modeName | |
378 | ); | |
457814b5 JS |
379 | } |
380 |