]>
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 | 6 | // Created: 04/01/98 |
2d1df0fc VZ |
7 | // Copyright: (c) 1998 Julian Smart |
8 | // (c) 2008 Vadim Zeitlin | |
526954c5 | 9 | // Licence: wxWindows licence |
457814b5 JS |
10 | ///////////////////////////////////////////////////////////////////////////// |
11 | ||
457814b5 | 12 | /* |
2d1df0fc VZ |
13 | This sample show document/view support in wxWidgets. |
14 | ||
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 | |
21 | document only | |
22 | ||
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. | |
27 | */ | |
28 | ||
29 | // ---------------------------------------------------------------------------- | |
30 | // headers | |
31 | // ---------------------------------------------------------------------------- | |
457814b5 JS |
32 | |
33 | // For compilers that support precompilation, includes "wx/wx.h". | |
34 | #include "wx/wxprec.h" | |
35 | ||
36 | #ifdef __BORLANDC__ | |
2d1df0fc | 37 | #pragma hdrstop |
457814b5 JS |
38 | #endif |
39 | ||
40 | #ifndef WX_PRECOMP | |
2d1df0fc VZ |
41 | #include "wx/wx.h" |
42 | #include "wx/stockitem.h" | |
457814b5 JS |
43 | #endif |
44 | ||
e4b19d9b | 45 | #if !wxUSE_DOC_VIEW_ARCHITECTURE |
2d1df0fc | 46 | #error You must set wxUSE_DOC_VIEW_ARCHITECTURE to 1 in setup.h! |
457814b5 JS |
47 | #endif |
48 | ||
49 | #include "wx/docview.h" | |
2d1df0fc | 50 | #include "wx/docmdi.h" |
457814b5 JS |
51 | |
52 | #include "docview.h" | |
53 | #include "doc.h" | |
54 | #include "view.h" | |
2d1df0fc VZ |
55 | |
56 | #include "wx/cmdline.h" | |
a6b7e90b | 57 | #include "wx/config.h" |
2d1df0fc | 58 | |
eb017efe | 59 | #ifdef __WXMAC__ |
2d1df0fc | 60 | #include "wx/filename.h" |
eb017efe | 61 | #endif |
457814b5 | 62 | |
e7092398 | 63 | #ifndef wxHAS_IMAGES_IN_RESOURCES |
2d1df0fc VZ |
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 | ||
f37f49b6 JS |
151 | ::wxInitAllImageHandlers(); |
152 | ||
a6b7e90b VZ |
153 | // Fill in the application information fields before creating wxConfig. |
154 | SetVendorName("wxWidgets"); | |
155 | SetAppName("wx_docview_sample"); | |
156 | SetAppDisplayName("wxWidgets DocView Sample"); | |
2d1df0fc | 157 | |
f6bcfd97 | 158 | //// Create a document manager |
2d1df0fc | 159 | wxDocManager *docManager = new wxDocManager; |
3d0145a5 | 160 | |
f6bcfd97 | 161 | //// Create a template relating drawing documents to their views |
2d1df0fc VZ |
162 | new wxDocTemplate(docManager, "Drawing", "*.drw", "", "drw", |
163 | "Drawing Doc", "Drawing View", | |
164 | CLASSINFO(DrawingDocument), CLASSINFO(DrawingView)); | |
fa9b55df | 165 | #if defined( __WXMAC__ ) && wxOSX_USE_CARBON |
2d1df0fc | 166 | wxFileName::MacRegisterDefaultTypeAndCreator("drw" , 'WXMB' , 'WXMA'); |
eb017efe | 167 | #endif |
6bdf5153 | 168 | |
2d1df0fc | 169 | if ( m_mode == Mode_Single ) |
f6bcfd97 | 170 | { |
2d1df0fc VZ |
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); | |
f6bcfd97 | 174 | } |
2d1df0fc | 175 | else // multiple documents mode: allow documents of different types |
eb017efe | 176 | { |
2d1df0fc VZ |
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)); | |
fa9b55df | 181 | #if defined( __WXMAC__ ) && wxOSX_USE_CARBON |
2d1df0fc | 182 | wxFileName::MacRegisterDefaultTypeAndCreator("txt" , 'TEXT' , 'WXMA'); |
eb017efe | 183 | #endif |
f37f49b6 JS |
184 | // Create a template relating image documents to their views |
185 | new wxDocTemplate(docManager, "Image", "*.png;*.jpg", "", "png;jpg", | |
186 | "Image Doc", "Image View", | |
2d4a03f8 | 187 | CLASSINFO(ImageDocument), CLASSINFO(ImageView)); |
eb017efe | 188 | } |
6bdf5153 | 189 | |
2d1df0fc VZ |
190 | // create the main frame window |
191 | wxFrame *frame; | |
192 | #if wxUSE_MDI_ARCHITECTURE | |
193 | if ( m_mode == Mode_MDI ) | |
f6bcfd97 | 194 | { |
2d1df0fc VZ |
195 | frame = new wxDocMDIParentFrame(docManager, NULL, wxID_ANY, |
196 | GetAppDisplayName(), | |
197 | wxDefaultPosition, | |
198 | wxSize(500, 400)); | |
199 | } | |
200 | else | |
201 | #endif // wxUSE_MDI_ARCHITECTURE | |
202 | { | |
203 | frame = new wxDocParentFrame(docManager, NULL, wxID_ANY, | |
204 | GetAppDisplayName(), | |
205 | wxDefaultPosition, | |
206 | wxSize(500, 400)); | |
f6bcfd97 | 207 | } |
6bdf5153 | 208 | |
2d1df0fc VZ |
209 | // and its menu bar |
210 | wxMenu *menuFile = new wxMenu; | |
6bdf5153 | 211 | |
2d1df0fc VZ |
212 | menuFile->Append(wxID_NEW); |
213 | menuFile->Append(wxID_OPEN); | |
6bdf5153 | 214 | |
2d1df0fc VZ |
215 | if ( m_mode == Mode_Single ) |
216 | AppendDocumentFileCommands(menuFile, true); | |
6bdf5153 | 217 | |
2d1df0fc VZ |
218 | menuFile->AppendSeparator(); |
219 | menuFile->Append(wxID_EXIT); | |
6bdf5153 | 220 | |
2d1df0fc VZ |
221 | // A nice touch: a history of files visited. Use this menu. |
222 | docManager->FileHistoryUseMenu(menuFile); | |
a6b7e90b VZ |
223 | #if wxUSE_CONFIG |
224 | docManager->FileHistoryLoad(*wxConfig::Get()); | |
225 | #endif // wxUSE_CONFIG | |
226 | ||
6bdf5153 | 227 | |
2d1df0fc VZ |
228 | if ( m_mode == Mode_Single ) |
229 | { | |
230 | m_canvas = new MyCanvas(NULL, frame); | |
231 | m_menuEdit = CreateDrawingEditMenu(); | |
abee7f47 | 232 | docManager->CreateNewDocument(); |
2d1df0fc | 233 | } |
6bdf5153 | 234 | |
2d1df0fc | 235 | CreateMenuBarForFrame(frame, menuFile, m_menuEdit); |
6bdf5153 | 236 | |
2d1df0fc | 237 | frame->SetIcon(wxICON(doc)); |
9b341e6f VZ |
238 | frame->Centre(); |
239 | frame->Show(); | |
6bdf5153 | 240 | |
f2aea0d1 | 241 | return true; |
457814b5 JS |
242 | } |
243 | ||
2d1df0fc | 244 | int MyApp::OnExit() |
457814b5 | 245 | { |
a6b7e90b VZ |
246 | wxDocManager * const manager = wxDocManager::GetDocumentManager(); |
247 | #if wxUSE_CONFIG | |
248 | manager->FileHistorySave(*wxConfig::Get()); | |
249 | #endif // wxUSE_CONFIG | |
250 | delete manager; | |
2d1df0fc VZ |
251 | |
252 | return wxApp::OnExit(); | |
457814b5 JS |
253 | } |
254 | ||
2d1df0fc VZ |
255 | void MyApp::AppendDocumentFileCommands(wxMenu *menu, bool supportsPrinting) |
256 | { | |
257 | menu->Append(wxID_CLOSE); | |
258 | menu->Append(wxID_SAVE); | |
259 | menu->Append(wxID_SAVEAS); | |
4311588b | 260 | menu->Append(wxID_REVERT, _("Re&vert...")); |
f6bcfd97 | 261 | |
2d1df0fc VZ |
262 | if ( supportsPrinting ) |
263 | { | |
264 | menu->AppendSeparator(); | |
265 | menu->Append(wxID_PRINT); | |
266 | menu->Append(wxID_PRINT_SETUP, "Print &Setup..."); | |
267 | menu->Append(wxID_PREVIEW); | |
268 | } | |
269 | } | |
270 | ||
271 | wxMenu *MyApp::CreateDrawingEditMenu() | |
457814b5 | 272 | { |
2d1df0fc VZ |
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"); | |
6bdf5153 | 278 | |
2d1df0fc VZ |
279 | return menu; |
280 | } | |
6bdf5153 | 281 | |
2d1df0fc VZ |
282 | void MyApp::CreateMenuBarForFrame(wxFrame *frame, wxMenu *file, wxMenu *edit) |
283 | { | |
284 | wxMenuBar *menubar = new wxMenuBar; | |
6bdf5153 | 285 | |
2d1df0fc | 286 | menubar->Append(file, wxGetStockLabel(wxID_FILE)); |
6bdf5153 | 287 | |
2d1df0fc VZ |
288 | if ( edit ) |
289 | menubar->Append(edit, wxGetStockLabel(wxID_EDIT)); | |
6bdf5153 | 290 | |
2d1df0fc VZ |
291 | wxMenu *help= new wxMenu; |
292 | help->Append(wxID_ABOUT); | |
293 | menubar->Append(help, wxGetStockLabel(wxID_HELP)); | |
6bdf5153 | 294 | |
2d1df0fc VZ |
295 | frame->SetMenuBar(menubar); |
296 | } | |
6bdf5153 | 297 | |
9b341e6f | 298 | wxFrame *MyApp::CreateChildFrame(wxView *view, bool isCanvas) |
2d1df0fc VZ |
299 | { |
300 | // create a child frame of appropriate class for the current mode | |
301 | wxFrame *subframe; | |
9b341e6f | 302 | wxDocument *doc = view->GetDocument(); |
2d1df0fc VZ |
303 | #if wxUSE_MDI_ARCHITECTURE |
304 | if ( GetMode() == Mode_MDI ) | |
305 | { | |
306 | subframe = new wxDocMDIChildFrame | |
307 | ( | |
308 | doc, | |
309 | view, | |
310 | wxStaticCast(GetTopWindow(), wxDocMDIParentFrame), | |
311 | wxID_ANY, | |
312 | "Child Frame", | |
313 | wxDefaultPosition, | |
314 | wxSize(300, 300) | |
315 | ); | |
f6bcfd97 | 316 | } |
6bdf5153 | 317 | else |
2d1df0fc | 318 | #endif // wxUSE_MDI_ARCHITECTURE |
6bdf5153 | 319 | { |
2d1df0fc VZ |
320 | subframe = new wxDocChildFrame |
321 | ( | |
322 | doc, | |
323 | view, | |
324 | wxStaticCast(GetTopWindow(), wxDocParentFrame), | |
325 | wxID_ANY, | |
326 | "Child Frame", | |
327 | wxDefaultPosition, | |
328 | wxSize(300, 300) | |
329 | ); | |
330 | ||
9b341e6f | 331 | subframe->Centre(); |
6bdf5153 VZ |
332 | } |
333 | ||
2d1df0fc | 334 | wxMenu *menuFile = new wxMenu; |
6bdf5153 | 335 | |
2d1df0fc VZ |
336 | menuFile->Append(wxID_NEW); |
337 | menuFile->Append(wxID_OPEN); | |
338 | AppendDocumentFileCommands(menuFile, isCanvas); | |
339 | menuFile->AppendSeparator(); | |
340 | menuFile->Append(wxID_EXIT); | |
6bdf5153 | 341 | |
2d1df0fc VZ |
342 | wxMenu *menuEdit; |
343 | if ( isCanvas ) | |
344 | { | |
345 | menuEdit = CreateDrawingEditMenu(); | |
457814b5 | 346 | |
2d1df0fc VZ |
347 | doc->GetCommandProcessor()->SetEditMenu(menuEdit); |
348 | doc->GetCommandProcessor()->Initialize(); | |
349 | } | |
350 | else // text frame | |
351 | { | |
352 | menuEdit = new wxMenu; | |
353 | menuEdit->Append(wxID_COPY); | |
354 | menuEdit->Append(wxID_PASTE); | |
355 | menuEdit->Append(wxID_SELECTALL); | |
356 | } | |
f6bcfd97 | 357 | |
2d1df0fc | 358 | CreateMenuBarForFrame(subframe, menuFile, menuEdit); |
457814b5 | 359 | |
2d1df0fc | 360 | subframe->SetIcon(isCanvas ? wxICON(chrt) : wxICON(notepad)); |
457814b5 | 361 | |
2d1df0fc | 362 | return subframe; |
457814b5 JS |
363 | } |
364 | ||
2d1df0fc | 365 | void MyApp::OnAbout(wxCommandEvent& WXUNUSED(event)) |
457814b5 | 366 | { |
2d1df0fc VZ |
367 | wxString modeName; |
368 | switch ( m_mode ) | |
369 | { | |
370 | #if wxUSE_MDI_ARCHITECTURE | |
371 | case Mode_MDI: | |
372 | modeName = "MDI"; | |
373 | break; | |
374 | #endif // wxUSE_MDI_ARCHITECTURE | |
375 | ||
376 | case Mode_SDI: | |
377 | modeName = "SDI"; | |
378 | break; | |
379 | ||
380 | case Mode_Single: | |
381 | modeName = "single document"; | |
382 | break; | |
383 | ||
384 | default: | |
385 | wxFAIL_MSG( "unknown mode "); | |
386 | } | |
d83af366 VZ |
387 | |
388 | #ifdef __VISUALC6__ | |
389 | const int docsCount = | |
390 | wxDocManager::GetDocumentManager()->GetDocuments().GetCount(); | |
391 | #else | |
392 | const int docsCount = | |
393 | wxDocManager::GetDocumentManager()->GetDocumentsVector().size(); | |
394 | #endif | |
457814b5 | 395 | |
2d1df0fc VZ |
396 | wxLogMessage |
397 | ( | |
398 | "This is the wxWidgets Document/View Sample\n" | |
399 | "running in %s mode.\n" | |
93d0805b | 400 | "%d open documents.\n" |
2d1df0fc VZ |
401 | "\n" |
402 | "Authors: Julian Smart, Vadim Zeitlin\n" | |
403 | "\n" | |
404 | "Usage: docview [--{mdi,sdi,single}]", | |
93d0805b | 405 | modeName, |
d83af366 | 406 | docsCount |
2d1df0fc | 407 | ); |
457814b5 | 408 | } |