]> git.saurik.com Git - wxWidgets.git/blame - src/common/docview.cpp
fixing osx_carbon
[wxWidgets.git] / src / common / docview.cpp
CommitLineData
5d9ea849 1/////////////////////////////////////////////////////////////////////////////
1e6feb95 2// Name: src/common/docview.cpp
c801d85f
KB
3// Purpose: Document/view classes
4// Author: Julian Smart
c77049a0 5// Modified by: Vadim Zeitlin
c801d85f
KB
6// Created: 01/02/97
7// RCS-ID: $Id$
55d99c7a 8// Copyright: (c) Julian Smart
65571936 9// Licence: wxWindows licence
c801d85f
KB
10/////////////////////////////////////////////////////////////////////////////
11
0fb67cd1
VZ
12// ============================================================================
13// declarations
14// ============================================================================
15
16// ----------------------------------------------------------------------------
17// headers
18// ----------------------------------------------------------------------------
19
c801d85f
KB
20// For compilers that support precompilation, includes "wx.h".
21#include "wx/wxprec.h"
22
23#ifdef __BORLANDC__
8ecff181 24 #pragma hdrstop
c801d85f
KB
25#endif
26
47d67540 27#if wxUSE_DOC_VIEW_ARCHITECTURE
c801d85f 28
246c5004
WS
29#include "wx/docview.h"
30
c801d85f 31#ifndef WX_PRECOMP
8ecff181 32 #include "wx/list.h"
0fb67cd1
VZ
33 #include "wx/string.h"
34 #include "wx/utils.h"
35 #include "wx/app.h"
36 #include "wx/dc.h"
d0bdc3ca 37 #include "wx/dialog.h"
0fb67cd1 38 #include "wx/menu.h"
0fb67cd1 39 #include "wx/filedlg.h"
6de2f8b9 40 #include "wx/intl.h"
c0369005 41 #include "wx/log.h"
246c5004 42 #include "wx/msgdlg.h"
59cf2e49 43 #include "wx/mdi.h"
949c9f74 44 #include "wx/choicdlg.h"
f7bd2698
JS
45#endif
46
ce4169a4 47#if wxUSE_PRINTING_ARCHITECTURE
8ecff181
WS
48 #include "wx/prntbase.h"
49 #include "wx/printdlg.h"
ce4169a4
RR
50#endif
51
7f555861 52#include "wx/confbase.h"
c77049a0 53#include "wx/filename.h"
74b31181 54#include "wx/file.h"
c77049a0 55#include "wx/ffile.h"
1e6feb95 56#include "wx/cmdproc.h"
0f30d8e3 57#include "wx/tokenzr.h"
b4a980f4 58#include "wx/filename.h"
d8efd219 59#include "wx/stdpaths.h"
c77049a0 60#include "wx/vector.h"
664e1314
VZ
61#include "wx/scopedarray.h"
62#include "wx/scopedptr.h"
3a393f8a 63#include "wx/except.h"
c801d85f 64
a533f5c1 65#if wxUSE_STD_IOSTREAM
8ecff181 66 #include "wx/ioswrap.h"
968454db 67 #include "wx/beforestd.h"
8ecff181
WS
68 #if wxUSE_IOSTREAMH
69 #include <fstream.h>
70 #else
71 #include <fstream>
72 #endif
968454db 73 #include "wx/afterstd.h"
a533f5c1 74#else
8ecff181 75 #include "wx/wfstream.h"
c801d85f
KB
76#endif
77
c77049a0
VZ
78typedef wxVector<wxDocTemplate *> wxDocTemplates;
79
0fb67cd1 80// ----------------------------------------------------------------------------
77ffb593 81// wxWidgets macros
0fb67cd1
VZ
82// ----------------------------------------------------------------------------
83
1e6feb95
VZ
84IMPLEMENT_ABSTRACT_CLASS(wxDocument, wxEvtHandler)
85IMPLEMENT_ABSTRACT_CLASS(wxView, wxEvtHandler)
86IMPLEMENT_ABSTRACT_CLASS(wxDocTemplate, wxObject)
87IMPLEMENT_DYNAMIC_CLASS(wxDocManager, wxEvtHandler)
88IMPLEMENT_CLASS(wxDocChildFrame, wxFrame)
89IMPLEMENT_CLASS(wxDocParentFrame, wxFrame)
0fb67cd1 90
1e6feb95
VZ
91#if wxUSE_PRINTING_ARCHITECTURE
92 IMPLEMENT_DYNAMIC_CLASS(wxDocPrintout, wxPrintout)
93#endif
0fb67cd1 94
0fb67cd1
VZ
95// ============================================================================
96// implementation
97// ============================================================================
98
99// ----------------------------------------------------------------------------
c77049a0 100// private helpers
0fb67cd1
VZ
101// ----------------------------------------------------------------------------
102
c77049a0
VZ
103namespace
104{
105
c77049a0 106wxString FindExtension(const wxString& path)
0fb67cd1
VZ
107{
108 wxString ext;
47d281e6 109 wxFileName::SplitPath(path, NULL, NULL, &ext);
0fb67cd1
VZ
110
111 // VZ: extensions are considered not case sensitive - is this really a good
112 // idea?
113 return ext.MakeLower();
114}
115
c77049a0
VZ
116} // anonymous namespace
117
0fb67cd1
VZ
118// ----------------------------------------------------------------------------
119// Definition of wxDocument
120// ----------------------------------------------------------------------------
c801d85f
KB
121
122wxDocument::wxDocument(wxDocument *parent)
123{
68379eaf 124 m_documentModified = false;
c77049a0 125 m_documentTemplate = NULL;
4db97e24
VZ
126
127 m_documentParent = parent;
128 if ( parent )
129 parent->m_childDocuments.push_back(this);
130
c77049a0 131 m_commandProcessor = NULL;
68379eaf 132 m_savedYet = false;
c801d85f
KB
133}
134
0fb67cd1 135bool wxDocument::DeleteContents()
c801d85f 136{
68379eaf 137 return true;
c801d85f
KB
138}
139
0fb67cd1 140wxDocument::~wxDocument()
c801d85f 141{
89d94e04 142 delete m_commandProcessor;
c801d85f 143
f6bcfd97
BP
144 if (GetDocumentManager())
145 GetDocumentManager()->RemoveDocument(this);
c801d85f 146
4db97e24
VZ
147 if ( m_documentParent )
148 m_documentParent->m_childDocuments.remove(this);
149
0fb67cd1
VZ
150 // Not safe to do here, since it'll invoke virtual view functions
151 // expecting to see valid derived objects: and by the time we get here,
152 // we've called destructors higher up.
153 //DeleteAllViews();
c801d85f 154}
0fb67cd1
VZ
155
156bool wxDocument::Close()
c801d85f 157{
89d94e04 158 if ( !OnSaveModified() )
68379eaf 159 return false;
89d94e04 160
4db97e24
VZ
161 // When the parent document closes, its children must be closed as well as
162 // they can't exist without the parent.
163
164 // As usual, first check if all children can be closed.
165 DocsList::const_iterator it = m_childDocuments.begin();
166 for ( DocsList::const_iterator end = m_childDocuments.end(); it != end; ++it )
167 {
168 if ( !(*it)->OnSaveModified() )
169 {
170 // Leave the parent document opened if a child can't close.
171 return false;
172 }
173 }
174
175 // Now that they all did, do close them: as m_childDocuments is modified as
176 // we iterate over it, don't use the usual for-style iteration here.
177 while ( !m_childDocuments.empty() )
178 {
179 wxDocument * const childDoc = m_childDocuments.front();
180
181 // This will call OnSaveModified() once again but it shouldn't do
182 // anything as the document was just saved or marked as not needing to
183 // be saved by the call to OnSaveModified() that returned true above.
184 if ( !childDoc->Close() )
185 {
186 wxFAIL_MSG( "Closing the child document unexpectedly failed "
187 "after its OnSaveModified() returned true" );
188 }
189
190 // Delete the child document by deleting all its views.
191 childDoc->DeleteAllViews();
192 }
193
194
89d94e04 195 return OnCloseDocument();
c801d85f 196}
0fb67cd1
VZ
197
198bool wxDocument::OnCloseDocument()
c801d85f 199{
b23e843b 200 // Tell all views that we're about to close
42771621 201 NotifyClosing();
0fb67cd1 202 DeleteContents();
68379eaf
WS
203 Modify(false);
204 return true;
c801d85f
KB
205}
206
0fb67cd1
VZ
207// Note that this implicitly deletes the document when the last view is
208// deleted.
209bool wxDocument::DeleteAllViews()
c801d85f 210{
f6bcfd97
BP
211 wxDocManager* manager = GetDocumentManager();
212
40702099
VZ
213 // first check if all views agree to be closed
214 const wxList::iterator end = m_documentViews.end();
215 for ( wxList::iterator i = m_documentViews.begin(); i != end; ++i )
0fb67cd1 216 {
40702099
VZ
217 wxView *view = (wxView *)*i;
218 if ( !view->Close() )
68379eaf 219 return false;
0fb67cd1 220 }
d5a93fda 221
40702099
VZ
222 // all views agreed to close, now do close them
223 if ( m_documentViews.empty() )
224 {
225 // normally the document would be implicitly deleted when the last view
226 // is, but if don't have any views, do it here instead
227 if ( manager && manager->GetDocuments().Member(this) )
228 delete this;
229 }
230 else // have views
231 {
232 // as we delete elements we iterate over, don't use the usual "from
233 // begin to end" loop
234 for ( ;; )
235 {
236 wxView *view = (wxView *)*m_documentViews.begin();
237
238 bool isLastOne = m_documentViews.size() == 1;
239
240 // this always deletes the node implicitly and if this is the last
241 // view also deletes this object itself (also implicitly, great),
242 // so we can't test for m_documentViews.empty() after calling this!
243 delete view;
244
245 if ( isLastOne )
246 break;
247 }
248 }
f6bcfd97 249
68379eaf 250 return true;
c801d85f
KB
251}
252
6de2f8b9 253wxView *wxDocument::GetFirstView() const
c801d85f 254{
89d94e04 255 if ( m_documentViews.empty() )
c77049a0 256 return NULL;
89d94e04
VZ
257
258 return static_cast<wxView *>(m_documentViews.GetFirst()->GetData());
c801d85f
KB
259}
260
250ab35a
JS
261void wxDocument::Modify(bool mod)
262{
263 if (mod != m_documentModified)
264 {
265 m_documentModified = mod;
266
267 // Allow views to append asterix to the title
268 wxView* view = GetFirstView();
269 if (view) view->OnChangeFilename();
270 }
271}
272
6de2f8b9 273wxDocManager *wxDocument::GetDocumentManager() const
c801d85f 274{
4db97e24
VZ
275 // For child documents we use the same document manager as the parent, even
276 // though we don't have our own template (as children are not opened/saved
277 // directly).
278 if ( m_documentParent )
279 return m_documentParent->GetDocumentManager();
280
c77049a0 281 return m_documentTemplate ? m_documentTemplate->GetDocumentManager() : NULL;
c801d85f
KB
282}
283
0fb67cd1 284bool wxDocument::OnNewDocument()
c801d85f 285{
0b108d10
VZ
286 // notice that there is no need to neither reset nor even check the
287 // modified flag here as the document itself is a new object (this is only
288 // called from CreateDocument()) and so it shouldn't be saved anyhow even
289 // if it is modified -- this could happen if the user code creates
290 // documents pre-filled with some user-entered (and which hence must not be
291 // lost) information
c801d85f 292
68379eaf 293 SetDocumentSaved(false);
c801d85f 294
724b119a 295 const wxString name = GetDocumentManager()->MakeNewDocumentName();
0fb67cd1 296 SetTitle(name);
68379eaf 297 SetFilename(name, true);
0fb67cd1 298
68379eaf 299 return true;
c801d85f
KB
300}
301
0fb67cd1 302bool wxDocument::Save()
c801d85f 303{
de56f240 304 if ( AlreadySaved() )
68379eaf 305 return true;
c801d85f 306
d06a66f5
VZ
307 if ( m_documentFile.empty() || !m_savedYet )
308 return SaveAs();
309
310 return OnSaveDocument(m_documentFile);
c801d85f 311}
0fb67cd1
VZ
312
313bool wxDocument::SaveAs()
c801d85f 314{
ba681060
VZ
315 wxDocTemplate *docTemplate = GetDocumentTemplate();
316 if (!docTemplate)
68379eaf 317 return false;
0fb67cd1 318
89d94e04
VZ
319#ifdef wxHAS_MULTIPLE_FILEDLG_FILTERS
320 wxString filter = docTemplate->GetDescription() + wxT(" (") +
321 docTemplate->GetFileFilter() + wxT(")|") +
322 docTemplate->GetFileFilter();
04129514
JS
323
324 // Now see if there are some other template with identical view and document
325 // classes, whose filters may also be used.
04129514
JS
326 if (docTemplate->GetViewClassInfo() && docTemplate->GetDocClassInfo())
327 {
89d94e04
VZ
328 wxList::compatibility_iterator
329 node = docTemplate->GetDocumentManager()->GetTemplates().GetFirst();
04129514
JS
330 while (node)
331 {
332 wxDocTemplate *t = (wxDocTemplate*) node->GetData();
68379eaf 333
04129514
JS
334 if (t->IsVisible() && t != docTemplate &&
335 t->GetViewClassInfo() == docTemplate->GetViewClassInfo() &&
336 t->GetDocClassInfo() == docTemplate->GetDocClassInfo())
337 {
338 // add a '|' to separate this filter from the previous one
b494c48b 339 if ( !filter.empty() )
04129514 340 filter << wxT('|');
68379eaf 341
89d94e04
VZ
342 filter << t->GetDescription()
343 << wxT(" (") << t->GetFileFilter() << wxT(") |")
04129514
JS
344 << t->GetFileFilter();
345 }
346
347 node = node->GetNext();
348 }
349 }
350#else
351 wxString filter = docTemplate->GetFileFilter() ;
352#endif
89d94e04 353
e0214f1f 354 wxString defaultDir = docTemplate->GetDirectory();
d8efd219
VZ
355 if ( defaultDir.empty() )
356 {
e0214f1f 357 defaultDir = wxPathOnly(GetFilename());
d8efd219
VZ
358 if ( defaultDir.empty() )
359 defaultDir = GetDocumentManager()->GetLastDirectory();
360 }
e0214f1f 361
00e3ea1c 362 wxString fileName = wxFileSelector(_("Save As"),
e0214f1f 363 defaultDir,
015e69f3 364 wxFileNameFromPath(GetFilename()),
0fb67cd1 365 docTemplate->GetDefaultExtension(),
04129514 366 filter,
ff3e84ff 367 wxFD_SAVE | wxFD_OVERWRITE_PROMPT,
0fb67cd1
VZ
368 GetDocumentWindow());
369
00e3ea1c 370 if (fileName.empty())
89d94e04 371 return false; // cancelled by user
ba681060 372
9d4a05be 373 // Files that were not saved correctly are not added to the FileHistory.
00e3ea1c 374 if (!OnSaveDocument(fileName))
68379eaf 375 return false;
9d4a05be 376
00e3ea1c
FM
377 SetTitle(wxFileNameFromPath(fileName));
378 SetFilename(fileName, true); // will call OnChangeFileName automatically
379
fd461854
VZ
380 // A file that doesn't use the default extension of its document template
381 // cannot be opened via the FileHistory, so we do not add it.
382 if (docTemplate->FileMatchesTemplate(fileName))
383 {
384 GetDocumentManager()->AddFileToHistory(fileName);
385 }
386 //else: the user will probably not be able to open the file again, so we
387 // could warn about the wrong file-extension here
89d94e04 388
fd461854 389 return true;
c801d85f 390}
0fb67cd1 391
c801d85f
KB
392bool wxDocument::OnSaveDocument(const wxString& file)
393{
0fb67cd1 394 if ( !file )
68379eaf 395 return false;
c801d85f 396
69936aea 397 if ( !DoSaveDocument(file) )
68379eaf 398 return false;
0fb67cd1 399
68379eaf 400 Modify(false);
0fb67cd1 401 SetFilename(file);
68379eaf 402 SetDocumentSaved(true);
26eef304 403#if defined( __WXOSX_MAC__ ) && wxOSX_USE_CARBON
b8afeb43
SC
404 wxFileName fn(file) ;
405 fn.MacSetDefaultTypeAndCreator() ;
406#endif
68379eaf 407 return true;
c801d85f 408}
0fb67cd1 409
c801d85f
KB
410bool wxDocument::OnOpenDocument(const wxString& file)
411{
0b108d10
VZ
412 // notice that there is no need to check the modified flag here for the
413 // reasons explained in OnNewDocument()
c801d85f 414
69936aea 415 if ( !DoOpenDocument(file) )
68379eaf 416 return false;
0fb67cd1 417
68379eaf 418 SetFilename(file, true);
0b108d10
VZ
419
420 // stretching the logic a little this does make sense because the document
421 // had been saved into the file we just loaded it from, it just could have
422 // happened during a previous program execution, it's just that the name of
423 // this method is a bit unfortunate, it should probably have been called
424 // HasAssociatedFileName()
425 SetDocumentSaved(true);
0fb67cd1
VZ
426
427 UpdateAllViews();
c801d85f 428
68379eaf 429 return true;
c801d85f 430}
0fb67cd1 431
a533f5c1 432#if wxUSE_STD_IOSTREAM
dd107c50 433wxSTD istream& wxDocument::LoadObject(wxSTD istream& stream)
23a54e14
RR
434#else
435wxInputStream& wxDocument::LoadObject(wxInputStream& stream)
436#endif
c801d85f 437{
0fb67cd1 438 return stream;
c801d85f
KB
439}
440
23a54e14 441#if wxUSE_STD_IOSTREAM
dd107c50 442wxSTD ostream& wxDocument::SaveObject(wxSTD ostream& stream)
a533f5c1 443#else
23a54e14
RR
444wxOutputStream& wxDocument::SaveObject(wxOutputStream& stream)
445#endif
a533f5c1 446{
23a54e14 447 return stream;
a533f5c1 448}
c801d85f 449
0fb67cd1 450bool wxDocument::Revert()
c801d85f 451{
4311588b
VZ
452 if ( wxMessageBox
453 (
454 _("Discard changes and reload the last saved version?"),
455 wxTheApp->GetAppDisplayName(),
456 wxYES_NO | wxCANCEL | wxICON_QUESTION,
457 GetDocumentWindow()
458 ) != wxYES )
459 return false;
460
461 if ( !DoOpenDocument(GetFilename()) )
462 return false;
463
464 Modify(false);
465 UpdateAllViews();
466
467 return true;
c801d85f
KB
468}
469
470
471// Get title, or filename if no title, else unnamed
724b119a 472#if WXWIN_COMPATIBILITY_2_8
c801d85f
KB
473bool wxDocument::GetPrintableName(wxString& buf) const
474{
4c51a665 475 // this function cannot only be overridden by the user code but also
724b119a
VZ
476 // called by it so we need to ensure that we return the same thing as
477 // GetUserReadableName() but we can't call it because this would result in
478 // an infinite recursion, hence we use the helper DoGetUserReadableName()
479 buf = DoGetUserReadableName();
480
481 return true;
482}
483#endif // WXWIN_COMPATIBILITY_2_8
484
485wxString wxDocument::GetUserReadableName() const
486{
487#if WXWIN_COMPATIBILITY_2_8
488 // we need to call the old virtual function to ensure that the overridden
489 // version of it is still called
490 wxString name;
491 if ( GetPrintableName(name) )
492 return name;
493#endif // WXWIN_COMPATIBILITY_2_8
494
495 return DoGetUserReadableName();
496}
497
498wxString wxDocument::DoGetUserReadableName() const
499{
500 if ( !m_documentTitle.empty() )
501 return m_documentTitle;
502
503 if ( !m_documentFile.empty() )
504 return wxFileNameFromPath(m_documentFile);
505
506 return _("unnamed");
c801d85f
KB
507}
508
6de2f8b9 509wxWindow *wxDocument::GetDocumentWindow() const
c801d85f 510{
89d94e04
VZ
511 wxView * const view = GetFirstView();
512
513 return view ? view->GetFrame() : wxTheApp->GetTopWindow();
c801d85f
KB
514}
515
0fb67cd1 516wxCommandProcessor *wxDocument::OnCreateCommandProcessor()
c801d85f 517{
0fb67cd1 518 return new wxCommandProcessor;
c801d85f
KB
519}
520
68379eaf 521// true if safe to close
0fb67cd1 522bool wxDocument::OnSaveModified()
c801d85f 523{
c77049a0
VZ
524 if ( IsModified() )
525 {
526 switch ( wxMessageBox
527 (
528 wxString::Format
529 (
4f57a7bf 530 _("Do you want to save changes to %s?"),
c77049a0
VZ
531 GetUserReadableName()
532 ),
533 wxTheApp->GetAppDisplayName(),
b6e96faf 534 wxYES_NO | wxCANCEL | wxICON_QUESTION | wxCENTRE
c77049a0 535 ) )
0fb67cd1 536 {
c77049a0
VZ
537 case wxNO:
538 Modify(false);
539 break;
540
541 case wxYES:
542 return Save();
543
544 case wxCANCEL:
545 return false;
0fb67cd1 546 }
c801d85f 547 }
c77049a0 548
68379eaf 549 return true;
c801d85f
KB
550}
551
552bool wxDocument::Draw(wxDC& WXUNUSED(context))
553{
68379eaf 554 return true;
c801d85f
KB
555}
556
557bool wxDocument::AddView(wxView *view)
558{
c77049a0 559 if ( !m_documentViews.Member(view) )
0fb67cd1
VZ
560 {
561 m_documentViews.Append(view);
562 OnChangedViewList();
563 }
68379eaf 564 return true;
c801d85f
KB
565}
566
567bool wxDocument::RemoveView(wxView *view)
568{
0fb67cd1
VZ
569 (void)m_documentViews.DeleteObject(view);
570 OnChangedViewList();
68379eaf 571 return true;
c801d85f
KB
572}
573
574bool wxDocument::OnCreate(const wxString& WXUNUSED(path), long flags)
575{
c77049a0 576 return GetDocumentTemplate()->CreateView(this, flags) != NULL;
c801d85f
KB
577}
578
579// Called after a view is added or removed.
580// The default implementation deletes the document if
581// there are no more views.
0fb67cd1 582void wxDocument::OnChangedViewList()
c801d85f 583{
c77049a0
VZ
584 if ( m_documentViews.empty() && OnSaveModified() )
585 delete this;
c801d85f
KB
586}
587
588void wxDocument::UpdateAllViews(wxView *sender, wxObject *hint)
589{
222ed1d6 590 wxList::compatibility_iterator node = m_documentViews.GetFirst();
0fb67cd1
VZ
591 while (node)
592 {
b1d4dd7a 593 wxView *view = (wxView *)node->GetData();
42771621 594 if (view != sender)
2b5f62a0 595 view->OnUpdate(sender, hint);
b1d4dd7a 596 node = node->GetNext();
0fb67cd1 597 }
c801d85f
KB
598}
599
b23e843b
JS
600void wxDocument::NotifyClosing()
601{
222ed1d6 602 wxList::compatibility_iterator node = m_documentViews.GetFirst();
b23e843b
JS
603 while (node)
604 {
b1d4dd7a 605 wxView *view = (wxView *)node->GetData();
b23e843b 606 view->OnClosingDocument();
b1d4dd7a 607 node = node->GetNext();
b23e843b
JS
608 }
609}
610
c801d85f
KB
611void wxDocument::SetFilename(const wxString& filename, bool notifyViews)
612{
0fb67cd1 613 m_documentFile = filename;
00e3ea1c
FM
614 OnChangeFilename(notifyViews);
615}
616
617void wxDocument::OnChangeFilename(bool notifyViews)
618{
0fb67cd1 619 if ( notifyViews )
c801d85f 620 {
0fb67cd1 621 // Notify the views that the filename has changed
222ed1d6 622 wxList::compatibility_iterator node = m_documentViews.GetFirst();
0fb67cd1
VZ
623 while (node)
624 {
b1d4dd7a 625 wxView *view = (wxView *)node->GetData();
0fb67cd1 626 view->OnChangeFilename();
b1d4dd7a 627 node = node->GetNext();
0fb67cd1 628 }
c801d85f 629 }
c801d85f
KB
630}
631
69936aea
VZ
632bool wxDocument::DoSaveDocument(const wxString& file)
633{
69936aea 634#if wxUSE_STD_IOSTREAM
53e2e87c 635 wxSTD ofstream store(file.mb_str(), wxSTD ios::binary);
c09b7217 636 if ( !store )
69936aea
VZ
637#else
638 wxFileOutputStream store(file);
c09b7217 639 if ( store.GetLastError() != wxSTREAM_NO_ERROR )
69936aea
VZ
640#endif
641 {
e58eaff7 642 wxLogError(_("File \"%s\" could not be opened for writing."), file);
68379eaf 643 return false;
69936aea 644 }
e58eaff7 645
69936aea
VZ
646 if (!SaveObject(store))
647 {
e58eaff7 648 wxLogError(_("Failed to save document to the file \"%s\"."), file);
68379eaf 649 return false;
69936aea
VZ
650 }
651
68379eaf 652 return true;
69936aea
VZ
653}
654
655bool wxDocument::DoOpenDocument(const wxString& file)
656{
69936aea 657#if wxUSE_STD_IOSTREAM
53e2e87c 658 wxSTD ifstream store(file.mb_str(), wxSTD ios::binary);
c09b7217 659 if ( !store )
69936aea
VZ
660#else
661 wxFileInputStream store(file);
9690b006 662 if (store.GetLastError() != wxSTREAM_NO_ERROR || !store.IsOk())
69936aea
VZ
663#endif
664 {
e58eaff7
VZ
665 wxLogError(_("File \"%s\" could not be opened for reading."), file);
666 return false;
667 }
668
69936aea 669#if wxUSE_STD_IOSTREAM
e58eaff7 670 LoadObject(store);
c09b7217 671 if ( !store )
69936aea 672#else
e58eaff7
VZ
673 int res = LoadObject(store).GetLastError();
674 if ( res != wxSTREAM_NO_ERROR && res != wxSTREAM_EOF )
69936aea 675#endif
e58eaff7
VZ
676 {
677 wxLogError(_("Failed to read document from the file \"%s\"."), file);
678 return false;
69936aea
VZ
679 }
680
e58eaff7 681 return true;
69936aea
VZ
682}
683
684
0fb67cd1
VZ
685// ----------------------------------------------------------------------------
686// Document view
687// ----------------------------------------------------------------------------
c801d85f 688
dbdb39b2 689wxView::wxView()
c801d85f 690{
c77049a0 691 m_viewDocument = NULL;
0fb67cd1 692
c77049a0 693 m_viewFrame = NULL;
a9e2e6e5
VZ
694
695 m_docChildFrame = NULL;
c801d85f
KB
696}
697
0fb67cd1 698wxView::~wxView()
c801d85f 699{
d3785012 700 if (m_viewDocument && GetDocumentManager())
63c639ec 701 GetDocumentManager()->ActivateView(this, false);
a9e2e6e5
VZ
702
703 // reset our frame view first, before removing it from the document as
704 // SetView(NULL) is a simple call while RemoveView() may result in user
705 // code being executed and this user code can, for example, show a message
706 // box which would result in an activation event for m_docChildFrame and so
707 // could reactivate the view being destroyed -- unless we reset it first
708 if ( m_docChildFrame && m_docChildFrame->GetView() == this )
ada1ff0d
VZ
709 {
710 // prevent it from doing anything with us
a9e2e6e5
VZ
711 m_docChildFrame->SetView(NULL);
712
ada1ff0d
VZ
713 // it doesn't make sense to leave the frame alive if its associated
714 // view doesn't exist any more so unconditionally close it as well
715 //
716 // notice that we only get here if m_docChildFrame is non-NULL in the
717 // first place and it will be always NULL if we're deleted because our
718 // frame was closed, so this only catches the case of directly deleting
719 // the view, as it happens if its creation fails in wxDocTemplate::
720 // CreateView() for example
721 m_docChildFrame->GetWindow()->Destroy();
722 }
723
a9e2e6e5
VZ
724 if ( m_viewDocument )
725 m_viewDocument->RemoveView(this);
c801d85f
KB
726}
727
ada1ff0d
VZ
728void wxView::SetDocChildFrame(wxDocChildFrameAnyBase *docChildFrame)
729{
730 SetFrame(docChildFrame ? docChildFrame->GetWindow() : NULL);
731 m_docChildFrame = docChildFrame;
732}
733
8cc208e3 734bool wxView::TryBefore(wxEvent& event)
c801d85f 735{
bba5e72a 736 wxDocument * const doc = GetDocument();
44070fd3 737 return doc && doc->ProcessEventLocally(event);
c801d85f
KB
738}
739
89d94e04
VZ
740void wxView::OnActivateView(bool WXUNUSED(activate),
741 wxView *WXUNUSED(activeView),
742 wxView *WXUNUSED(deactiveView))
c801d85f
KB
743{
744}
745
746void wxView::OnPrint(wxDC *dc, wxObject *WXUNUSED(info))
747{
0fb67cd1 748 OnDraw(dc);
c801d85f
KB
749}
750
751void wxView::OnUpdate(wxView *WXUNUSED(sender), wxObject *WXUNUSED(hint))
752{
753}
754
0fb67cd1 755void wxView::OnChangeFilename()
c801d85f 756{
faa49bfd
WS
757 // GetFrame can return wxWindow rather than wxTopLevelWindow due to
758 // generic MDI implementation so use SetLabel rather than SetTitle.
759 // It should cause SetTitle() for top level windows.
760 wxWindow *win = GetFrame();
761 if (!win) return;
f2506310 762
faa49bfd
WS
763 wxDocument *doc = GetDocument();
764 if (!doc) return;
c801d85f 765
250ab35a
JS
766 wxString label = doc->GetUserReadableName();
767 if (doc->IsModified())
768 {
769 label += "*";
770 }
771 win->SetLabel(label);
c801d85f
KB
772}
773
774void wxView::SetDocument(wxDocument *doc)
775{
0fb67cd1
VZ
776 m_viewDocument = doc;
777 if (doc)
778 doc->AddView(this);
c801d85f
KB
779}
780
781bool wxView::Close(bool deleteWindow)
782{
c77049a0 783 return OnClose(deleteWindow);
c801d85f
KB
784}
785
786void wxView::Activate(bool activate)
787{
bb28b477 788 if (GetDocument() && GetDocumentManager())
0fb67cd1
VZ
789 {
790 OnActivateView(activate, this, GetDocumentManager()->GetCurrentView());
791 GetDocumentManager()->ActivateView(this, activate);
792 }
c801d85f
KB
793}
794
795bool wxView::OnClose(bool WXUNUSED(deleteWindow))
796{
68379eaf 797 return GetDocument() ? GetDocument()->Close() : true;
c801d85f
KB
798}
799
47d67540 800#if wxUSE_PRINTING_ARCHITECTURE
0fb67cd1 801wxPrintout *wxView::OnCreatePrintout()
c801d85f 802{
0fb67cd1 803 return new wxDocPrintout(this);
c801d85f 804}
6de2f8b9 805#endif // wxUSE_PRINTING_ARCHITECTURE
c801d85f 806
0fb67cd1
VZ
807// ----------------------------------------------------------------------------
808// wxDocTemplate
809// ----------------------------------------------------------------------------
c801d85f 810
0fb67cd1
VZ
811wxDocTemplate::wxDocTemplate(wxDocManager *manager,
812 const wxString& descr,
813 const wxString& filter,
814 const wxString& dir,
815 const wxString& ext,
816 const wxString& docTypeName,
817 const wxString& viewTypeName,
818 wxClassInfo *docClassInfo,
819 wxClassInfo *viewClassInfo,
820 long flags)
c801d85f 821{
0fb67cd1 822 m_documentManager = manager;
0fb67cd1
VZ
823 m_description = descr;
824 m_directory = dir;
825 m_defaultExt = ext;
826 m_fileFilter = filter;
827 m_flags = flags;
828 m_docTypeName = docTypeName;
829 m_viewTypeName = viewTypeName;
830 m_documentManager->AssociateTemplate(this);
c801d85f 831
0fb67cd1
VZ
832 m_docClassInfo = docClassInfo;
833 m_viewClassInfo = viewClassInfo;
c801d85f
KB
834}
835
0fb67cd1 836wxDocTemplate::~wxDocTemplate()
c801d85f 837{
0fb67cd1 838 m_documentManager->DisassociateTemplate(this);
c801d85f 839}
0fb67cd1
VZ
840
841// Tries to dynamically construct an object of the right class.
c801d85f
KB
842wxDocument *wxDocTemplate::CreateDocument(const wxString& path, long flags)
843{
4c7d530a
VZ
844 // InitDocument() is supposed to delete the document object if its
845 // initialization fails so don't use wxScopedPtr<> here: this is fragile
846 // but unavoidable because the default implementation uses CreateView()
847 // which may -- or not -- create a wxView and if it does create it and its
848 // initialization fails then the view destructor will delete the document
849 // (via RemoveView()) and as we can't distinguish between the two cases we
850 // just have to assume that it always deletes it in case of failure
851 wxDocument * const doc = DoCreateDocument();
852
853 return doc && InitDocument(doc, path, flags) ? doc : NULL;
217b7140
JS
854}
855
c77049a0
VZ
856bool
857wxDocTemplate::InitDocument(wxDocument* doc, const wxString& path, long flags)
217b7140 858{
0fb67cd1
VZ
859 doc->SetFilename(path);
860 doc->SetDocumentTemplate(this);
861 GetDocumentManager()->AddDocument(doc);
862 doc->SetCommandProcessor(doc->OnCreateCommandProcessor());
863
864 if (doc->OnCreate(path, flags))
217b7140 865 return true;
89d94e04
VZ
866
867 if (GetDocumentManager()->GetDocuments().Member(doc))
868 doc->DeleteAllViews();
869 return false;
c801d85f
KB
870}
871
872wxView *wxDocTemplate::CreateView(wxDocument *doc, long flags)
873{
7047d798
VZ
874 wxScopedPtr<wxView> view(DoCreateView());
875 if ( !view )
c77049a0 876 return NULL;
69936aea 877
0fb67cd1 878 view->SetDocument(doc);
7047d798 879 if ( !view->OnCreate(doc, flags) )
c77049a0 880 return NULL;
7047d798
VZ
881
882 return view.release();
c801d85f
KB
883}
884
6de2f8b9
VZ
885// The default (very primitive) format detection: check is the extension is
886// that of the template
887bool wxDocTemplate::FileMatchesTemplate(const wxString& path)
888{
0f30d8e3
WS
889 wxStringTokenizer parser (GetFileFilter(), wxT(";"));
890 wxString anything = wxT ("*");
891 while (parser.HasMoreTokens())
892 {
893 wxString filter = parser.GetNextToken();
894 wxString filterExt = FindExtension (filter);
895 if ( filter.IsSameAs (anything) ||
896 filterExt.IsSameAs (anything) ||
897 filterExt.IsSameAs (FindExtension (path)) )
898 return true;
899 }
6de2f8b9
VZ
900 return GetDefaultExtension().IsSameAs(FindExtension(path));
901}
902
69936aea
VZ
903wxDocument *wxDocTemplate::DoCreateDocument()
904{
905 if (!m_docClassInfo)
c77049a0 906 return NULL;
69936aea 907
89d94e04 908 return static_cast<wxDocument *>(m_docClassInfo->CreateObject());
69936aea
VZ
909}
910
911wxView *wxDocTemplate::DoCreateView()
912{
913 if (!m_viewClassInfo)
c77049a0 914 return NULL;
69936aea 915
89d94e04 916 return static_cast<wxView *>(m_viewClassInfo->CreateObject());
69936aea
VZ
917}
918
0fb67cd1
VZ
919// ----------------------------------------------------------------------------
920// wxDocManager
921// ----------------------------------------------------------------------------
922
c801d85f
KB
923BEGIN_EVENT_TABLE(wxDocManager, wxEvtHandler)
924 EVT_MENU(wxID_OPEN, wxDocManager::OnFileOpen)
925 EVT_MENU(wxID_CLOSE, wxDocManager::OnFileClose)
33a20136 926 EVT_MENU(wxID_CLOSE_ALL, wxDocManager::OnFileCloseAll)
c801d85f
KB
927 EVT_MENU(wxID_REVERT, wxDocManager::OnFileRevert)
928 EVT_MENU(wxID_NEW, wxDocManager::OnFileNew)
929 EVT_MENU(wxID_SAVE, wxDocManager::OnFileSave)
930 EVT_MENU(wxID_SAVEAS, wxDocManager::OnFileSaveAs)
931 EVT_MENU(wxID_UNDO, wxDocManager::OnUndo)
932 EVT_MENU(wxID_REDO, wxDocManager::OnRedo)
f2506310 933
c70da1a2
VZ
934 // We don't know in advance how many items can there be in the MRU files
935 // list so set up OnMRUFile() as a handler for all menu events and do the
936 // check for the id of the menu item clicked inside it.
937 EVT_MENU(wxID_ANY, wxDocManager::OnMRUFile)
938
f2506310 939 EVT_UPDATE_UI(wxID_OPEN, wxDocManager::OnUpdateFileOpen)
c77049a0
VZ
940 EVT_UPDATE_UI(wxID_CLOSE, wxDocManager::OnUpdateDisableIfNoDoc)
941 EVT_UPDATE_UI(wxID_CLOSE_ALL, wxDocManager::OnUpdateDisableIfNoDoc)
4311588b 942 EVT_UPDATE_UI(wxID_REVERT, wxDocManager::OnUpdateFileRevert)
f2506310
JS
943 EVT_UPDATE_UI(wxID_NEW, wxDocManager::OnUpdateFileNew)
944 EVT_UPDATE_UI(wxID_SAVE, wxDocManager::OnUpdateFileSave)
4db97e24 945 EVT_UPDATE_UI(wxID_SAVEAS, wxDocManager::OnUpdateFileSaveAs)
f2506310
JS
946 EVT_UPDATE_UI(wxID_UNDO, wxDocManager::OnUpdateUndo)
947 EVT_UPDATE_UI(wxID_REDO, wxDocManager::OnUpdateRedo)
948
ce4169a4 949#if wxUSE_PRINTING_ARCHITECTURE
c801d85f 950 EVT_MENU(wxID_PRINT, wxDocManager::OnPrint)
c801d85f 951 EVT_MENU(wxID_PREVIEW, wxDocManager::OnPreview)
b99017ce 952 EVT_MENU(wxID_PRINT_SETUP, wxDocManager::OnPageSetup)
f2506310 953
c77049a0
VZ
954 EVT_UPDATE_UI(wxID_PRINT, wxDocManager::OnUpdateDisableIfNoDoc)
955 EVT_UPDATE_UI(wxID_PREVIEW, wxDocManager::OnUpdateDisableIfNoDoc)
b27367a5
VZ
956 // NB: we keep "Print setup" menu item always enabled as it can be used
957 // even without an active document
b99017ce 958#endif // wxUSE_PRINTING_ARCHITECTURE
c801d85f
KB
959END_EVENT_TABLE()
960
c77049a0 961wxDocManager* wxDocManager::sm_docManager = NULL;
f2506310 962
c77049a0 963wxDocManager::wxDocManager(long WXUNUSED(flags), bool initialize)
c801d85f 964{
c77049a0
VZ
965 sm_docManager = this;
966
0fb67cd1 967 m_defaultDocumentNameCounter = 1;
c77049a0
VZ
968 m_currentView = NULL;
969 m_maxDocsOpen = INT_MAX;
970 m_fileHistory = NULL;
971 if ( initialize )
0fb67cd1 972 Initialize();
c801d85f
KB
973}
974
0fb67cd1 975wxDocManager::~wxDocManager()
c801d85f 976{
0fb67cd1 977 Clear();
c77049a0
VZ
978 delete m_fileHistory;
979 sm_docManager = NULL;
c801d85f
KB
980}
981
b72b1920
JS
982// closes the specified document
983bool wxDocManager::CloseDocument(wxDocument* doc, bool force)
984{
89d94e04
VZ
985 if ( !doc->Close() && !force )
986 return false;
b72b1920 987
89d94e04
VZ
988 // Implicitly deletes the document when
989 // the last view is deleted
990 doc->DeleteAllViews();
42771621 991
89d94e04
VZ
992 // Check we're really deleted
993 if (m_docs.Member(doc))
994 delete doc;
995
996 return true;
b72b1920
JS
997}
998
33a20136 999bool wxDocManager::CloseDocuments(bool force)
c801d85f 1000{
222ed1d6 1001 wxList::compatibility_iterator node = m_docs.GetFirst();
0fb67cd1
VZ
1002 while (node)
1003 {
b1d4dd7a 1004 wxDocument *doc = (wxDocument *)node->GetData();
222ed1d6 1005 wxList::compatibility_iterator next = node->GetNext();
68379eaf 1006
b72b1920 1007 if (!CloseDocument(doc, force))
68379eaf 1008 return false;
0fb67cd1 1009
0fb67cd1
VZ
1010 // This assumes that documents are not connected in
1011 // any way, i.e. deleting one document does NOT
1012 // delete another.
1013 node = next;
1014 }
68379eaf 1015 return true;
33a20136
VZ
1016}
1017
1018bool wxDocManager::Clear(bool force)
1019{
1020 if (!CloseDocuments(force))
68379eaf 1021 return false;
33a20136 1022
8af94d3b
VZ
1023 m_currentView = NULL;
1024
222ed1d6 1025 wxList::compatibility_iterator node = m_templates.GetFirst();
0fb67cd1
VZ
1026 while (node)
1027 {
b1d4dd7a 1028 wxDocTemplate *templ = (wxDocTemplate*) node->GetData();
222ed1d6 1029 wxList::compatibility_iterator next = node->GetNext();
c801d85f
KB
1030 delete templ;
1031 node = next;
0fb67cd1 1032 }
68379eaf 1033 return true;
c801d85f
KB
1034}
1035
0fb67cd1 1036bool wxDocManager::Initialize()
c801d85f 1037{
0fb67cd1 1038 m_fileHistory = OnCreateFileHistory();
68379eaf 1039 return true;
c801d85f
KB
1040}
1041
d8efd219
VZ
1042wxString wxDocManager::GetLastDirectory() const
1043{
bba6891c 1044 // if we haven't determined the last used directory yet, do it now
d8efd219
VZ
1045 if ( m_lastDirectory.empty() )
1046 {
bba6891c
VZ
1047 // we're going to modify m_lastDirectory in this const method, so do it
1048 // via non-const self pointer instead of const this one
d8efd219 1049 wxDocManager * const self = const_cast<wxDocManager *>(this);
bba6891c
VZ
1050
1051 // first try to reuse the directory of the most recently opened file:
1052 // this ensures that if the user opens a file, closes the program and
1053 // runs it again the "Open file" dialog will open in the directory of
1054 // the last file he used
0571b62b 1055 if ( m_fileHistory && m_fileHistory->GetCount() )
bba6891c 1056 {
0571b62b 1057 const wxString lastOpened = m_fileHistory->GetHistoryFile(0);
bba6891c
VZ
1058 const wxFileName fn(lastOpened);
1059 if ( fn.DirExists() )
1060 {
1061 self->m_lastDirectory = fn.GetPath();
1062 }
1063 //else: should we try the next one?
1064 }
0571b62b 1065 //else: no history yet
bba6891c
VZ
1066
1067 // if we don't have any files in the history (yet?), use the
1068 // system-dependent default location for the document files
1069 if ( m_lastDirectory.empty() )
1070 {
1071 self->m_lastDirectory = wxStandardPaths::Get().GetAppDocumentsDir();
1072 }
d8efd219
VZ
1073 }
1074
1075 return m_lastDirectory;
1076}
1077
0fb67cd1 1078wxFileHistory *wxDocManager::OnCreateFileHistory()
c801d85f 1079{
0fb67cd1 1080 return new wxFileHistory;
c801d85f
KB
1081}
1082
1083void wxDocManager::OnFileClose(wxCommandEvent& WXUNUSED(event))
1084{
0fb67cd1 1085 wxDocument *doc = GetCurrentDocument();
568b12fa
VZ
1086 if (doc)
1087 CloseDocument(doc);
c801d85f
KB
1088}
1089
33a20136
VZ
1090void wxDocManager::OnFileCloseAll(wxCommandEvent& WXUNUSED(event))
1091{
68379eaf 1092 CloseDocuments(false);
33a20136
VZ
1093}
1094
c801d85f
KB
1095void wxDocManager::OnFileNew(wxCommandEvent& WXUNUSED(event))
1096{
b5412983 1097 CreateNewDocument();
c801d85f
KB
1098}
1099
1100void wxDocManager::OnFileOpen(wxCommandEvent& WXUNUSED(event))
1101{
89d94e04 1102 if ( !CreateDocument("") )
5f170f33
VZ
1103 {
1104 OnOpenFileFailure();
1105 }
c801d85f
KB
1106}
1107
1108void wxDocManager::OnFileRevert(wxCommandEvent& WXUNUSED(event))
1109{
0fb67cd1
VZ
1110 wxDocument *doc = GetCurrentDocument();
1111 if (!doc)
1112 return;
1113 doc->Revert();
c801d85f
KB
1114}
1115
1116void wxDocManager::OnFileSave(wxCommandEvent& WXUNUSED(event))
1117{
0fb67cd1
VZ
1118 wxDocument *doc = GetCurrentDocument();
1119 if (!doc)
1120 return;
1121 doc->Save();
c801d85f
KB
1122}
1123
1124void wxDocManager::OnFileSaveAs(wxCommandEvent& WXUNUSED(event))
1125{
0fb67cd1
VZ
1126 wxDocument *doc = GetCurrentDocument();
1127 if (!doc)
1128 return;
1129 doc->SaveAs();
c801d85f
KB
1130}
1131
c70da1a2
VZ
1132void wxDocManager::OnMRUFile(wxCommandEvent& event)
1133{
1134 // Check if the id is in the range assigned to MRU list entries.
1135 const int id = event.GetId();
1136 if ( id >= wxID_FILE1 &&
f50d8458 1137 id < wxID_FILE1 + static_cast<int>(m_fileHistory->GetCount()) )
c70da1a2
VZ
1138 {
1139 DoOpenMRUFile(id - wxID_FILE1);
1140 }
1141 else
1142 {
1143 event.Skip();
1144 }
1145}
1146
1147void wxDocManager::DoOpenMRUFile(unsigned n)
1148{
1149 wxString filename(GetHistoryFile(n));
1150 if ( filename.empty() )
1151 return;
1152
1153 wxString errMsg; // must contain exactly one "%s" if non-empty
1154 if ( wxFile::Exists(filename) )
1155 {
d38eb01c
VZ
1156 // Try to open it but don't give an error if it failed: this could be
1157 // normal, e.g. because the user cancelled opening it, and we don't
1158 // have any useful information to put in the error message anyhow, so
1159 // we assume that in case of an error the appropriate message had been
1160 // already logged.
1161 (void)CreateDocument(filename, wxDOC_SILENT);
c70da1a2
VZ
1162 }
1163 else // file doesn't exist
1164 {
d38eb01c 1165 OnMRUFileNotExist(n, filename);
c70da1a2 1166 }
d38eb01c 1167}
c70da1a2 1168
d38eb01c
VZ
1169void wxDocManager::OnMRUFileNotExist(unsigned n, const wxString& filename)
1170{
c70da1a2
VZ
1171 // remove the file which we can't open from the MRU list
1172 RemoveFileFromHistory(n);
1173
1174 // and tell the user about it
d38eb01c
VZ
1175 wxLogError(_("The file '%s' doesn't exist and couldn't be opened.\n"
1176 "It has been removed from the most recently used files list."),
c70da1a2
VZ
1177 filename);
1178}
1179
b99017ce
VZ
1180#if wxUSE_PRINTING_ARCHITECTURE
1181
c801d85f
KB
1182void wxDocManager::OnPrint(wxCommandEvent& WXUNUSED(event))
1183{
575e4cd6 1184 wxView *view = GetActiveView();
0fb67cd1
VZ
1185 if (!view)
1186 return;
c801d85f 1187
0fb67cd1
VZ
1188 wxPrintout *printout = view->OnCreatePrintout();
1189 if (printout)
1190 {
b99017ce
VZ
1191 wxPrintDialogData printDialogData(m_pageSetupDialogData.GetPrintData());
1192 wxPrinter printer(&printDialogData);
68379eaf 1193 printer.Print(view->GetFrame(), printout, true);
c801d85f 1194
0fb67cd1
VZ
1195 delete printout;
1196 }
c801d85f
KB
1197}
1198
b99017ce
VZ
1199void wxDocManager::OnPageSetup(wxCommandEvent& WXUNUSED(event))
1200{
1201 wxPageSetupDialog dlg(wxTheApp->GetTopWindow(), &m_pageSetupDialogData);
1202 if ( dlg.ShowModal() == wxID_OK )
1203 {
1204 m_pageSetupDialogData = dlg.GetPageSetupData();
1205 }
1206}
1207
94dc70d1
VZ
1208wxPreviewFrame* wxDocManager::CreatePreviewFrame(wxPrintPreviewBase* preview,
1209 wxWindow *parent,
1210 const wxString& title)
1211{
1212 return new wxPreviewFrame(preview, parent, title);
1213}
94dc70d1 1214
c801d85f
KB
1215void wxDocManager::OnPreview(wxCommandEvent& WXUNUSED(event))
1216{
84108812 1217 wxBusyCursor busy;
575e4cd6 1218 wxView *view = GetActiveView();
0fb67cd1
VZ
1219 if (!view)
1220 return;
c801d85f 1221
0fb67cd1
VZ
1222 wxPrintout *printout = view->OnCreatePrintout();
1223 if (printout)
1224 {
b99017ce
VZ
1225 wxPrintDialogData printDialogData(m_pageSetupDialogData.GetPrintData());
1226
0fb67cd1 1227 // Pass two printout objects: for preview, and possible printing.
89d94e04 1228 wxPrintPreviewBase *
b99017ce
VZ
1229 preview = new wxPrintPreview(printout,
1230 view->OnCreatePrintout(),
1231 &printDialogData);
a1b806b9 1232 if ( !preview->IsOk() )
3f8b4a3f
JS
1233 {
1234 delete preview;
89d94e04 1235 wxLogError(_("Print preview creation failed."));
3f8b4a3f
JS
1236 return;
1237 }
0fb67cd1 1238
94dc70d1
VZ
1239 wxPreviewFrame* frame = CreatePreviewFrame(preview,
1240 wxTheApp->GetTopWindow(),
1241 _("Print Preview"));
1242 wxCHECK_RET( frame, "should create a print preview frame" );
1243
0fb67cd1
VZ
1244 frame->Centre(wxBOTH);
1245 frame->Initialize();
68379eaf 1246 frame->Show(true);
0fb67cd1 1247 }
c801d85f 1248}
b99017ce 1249#endif // wxUSE_PRINTING_ARCHITECTURE
c801d85f 1250
34aa6140 1251void wxDocManager::OnUndo(wxCommandEvent& event)
c801d85f 1252{
89d94e04
VZ
1253 wxCommandProcessor * const cmdproc = GetCurrentCommandProcessor();
1254 if ( !cmdproc )
1255 {
34aa6140 1256 event.Skip();
89d94e04
VZ
1257 return;
1258 }
1259
1260 cmdproc->Undo();
c801d85f
KB
1261}
1262
34aa6140 1263void wxDocManager::OnRedo(wxCommandEvent& event)
c801d85f 1264{
89d94e04
VZ
1265 wxCommandProcessor * const cmdproc = GetCurrentCommandProcessor();
1266 if ( !cmdproc )
1267 {
34aa6140 1268 event.Skip();
89d94e04
VZ
1269 return;
1270 }
1271
1272 cmdproc->Redo();
c801d85f
KB
1273}
1274
f2506310
JS
1275// Handlers for UI update commands
1276
1277void wxDocManager::OnUpdateFileOpen(wxUpdateUIEvent& event)
1278{
84cae69a
FM
1279 // CreateDocument() (which is called from OnFileOpen) may succeed
1280 // only when there is at least a template:
1281 event.Enable( GetTemplates().GetCount()>0 );
f2506310
JS
1282}
1283
c77049a0 1284void wxDocManager::OnUpdateDisableIfNoDoc(wxUpdateUIEvent& event)
f2506310 1285{
c77049a0 1286 event.Enable( GetCurrentDocument() != NULL );
f2506310
JS
1287}
1288
4311588b
VZ
1289void wxDocManager::OnUpdateFileRevert(wxUpdateUIEvent& event)
1290{
1291 wxDocument* doc = GetCurrentDocument();
1292 event.Enable(doc && doc->IsModified() && doc->GetDocumentSaved());
1293}
1294
f2506310
JS
1295void wxDocManager::OnUpdateFileNew(wxUpdateUIEvent& event)
1296{
84cae69a
FM
1297 // CreateDocument() (which is called from OnFileNew) may succeed
1298 // only when there is at least a template:
1299 event.Enable( GetTemplates().GetCount()>0 );
f2506310
JS
1300}
1301
1302void wxDocManager::OnUpdateFileSave(wxUpdateUIEvent& event)
1303{
de56f240 1304 wxDocument * const doc = GetCurrentDocument();
4db97e24
VZ
1305 event.Enable( doc && !doc->IsChildDocument() && !doc->AlreadySaved() );
1306}
1307
1308void wxDocManager::OnUpdateFileSaveAs(wxUpdateUIEvent& event)
1309{
1310 wxDocument * const doc = GetCurrentDocument();
1311 event.Enable( doc && !doc->IsChildDocument() );
f2506310
JS
1312}
1313
f2506310
JS
1314void wxDocManager::OnUpdateUndo(wxUpdateUIEvent& event)
1315{
89d94e04
VZ
1316 wxCommandProcessor * const cmdproc = GetCurrentCommandProcessor();
1317 if ( !cmdproc )
34aa6140 1318 {
89d94e04
VZ
1319 event.Enable(false);
1320 return;
34aa6140 1321 }
89d94e04
VZ
1322
1323 event.Enable(cmdproc->CanUndo());
1324 cmdproc->SetMenuStrings();
f2506310
JS
1325}
1326
1327void wxDocManager::OnUpdateRedo(wxUpdateUIEvent& event)
1328{
89d94e04
VZ
1329 wxCommandProcessor * const cmdproc = GetCurrentCommandProcessor();
1330 if ( !cmdproc )
34aa6140 1331 {
89d94e04
VZ
1332 event.Enable(false);
1333 return;
34aa6140 1334 }
89d94e04
VZ
1335
1336 event.Enable(cmdproc->CanRedo());
1337 cmdproc->SetMenuStrings();
f2506310
JS
1338}
1339
575e4cd6 1340wxView *wxDocManager::GetActiveView() const
637f467a 1341{
575e4cd6
VZ
1342 wxView *view = GetCurrentView();
1343
1344 if ( !view && !m_docs.empty() )
637f467a 1345 {
575e4cd6
VZ
1346 // if we have exactly one document, consider its view to be the current
1347 // one
1348 //
1349 // VZ: I'm not exactly sure why is this needed but this is how this
1350 // code used to behave before the bug #9518 was fixed and it seems
1351 // safer to preserve the old logic
1352 wxList::compatibility_iterator node = m_docs.GetFirst();
1353 if ( !node->GetNext() )
1354 {
1355 wxDocument *doc = static_cast<wxDocument *>(node->GetData());
1356 view = doc->GetFirstView();
1357 }
1358 //else: we have more than one document
637f467a 1359 }
575e4cd6
VZ
1360
1361 return view;
637f467a
JS
1362}
1363
8cc208e3 1364bool wxDocManager::TryBefore(wxEvent& event)
637f467a 1365{
575e4cd6 1366 wxView * const view = GetActiveView();
44070fd3 1367 return view && view->ProcessEventLocally(event);
637f467a
JS
1368}
1369
c77049a0 1370namespace
c801d85f 1371{
b1d4dd7a 1372
c77049a0
VZ
1373// helper function: return only the visible templates
1374wxDocTemplates GetVisibleTemplates(const wxList& allTemplates)
1375{
1376 // select only the visible templates
1377 const size_t totalNumTemplates = allTemplates.GetCount();
1378 wxDocTemplates templates;
1379 if ( totalNumTemplates )
c801d85f 1380 {
c77049a0
VZ
1381 templates.reserve(totalNumTemplates);
1382
1383 for ( wxList::const_iterator i = allTemplates.begin(),
1384 end = allTemplates.end();
1385 i != end;
1386 ++i )
0fb67cd1 1387 {
c77049a0
VZ
1388 wxDocTemplate * const temp = (wxDocTemplate *)*i;
1389 if ( temp->IsVisible() )
1390 templates.push_back(temp);
0fb67cd1 1391 }
c801d85f 1392 }
42771621 1393
c77049a0
VZ
1394 return templates;
1395}
0fb67cd1 1396
c77049a0
VZ
1397} // anonymous namespace
1398
8325504f
VZ
1399void wxDocManager::ActivateDocument(wxDocument *doc)
1400{
1401 wxView * const view = doc->GetFirstView();
1402 if ( !view )
1403 return;
1404
1405 view->Activate(true);
1406 if ( wxWindow *win = view->GetFrame() )
1407 win->SetFocus();
1408}
1409
c77049a0
VZ
1410wxDocument *wxDocManager::CreateDocument(const wxString& pathOrig, long flags)
1411{
1412 // this ought to be const but SelectDocumentType/Path() are not
1413 // const-correct and can't be changed as, being virtual, this risks
1414 // breaking user code overriding them
1415 wxDocTemplates templates(GetVisibleTemplates(m_templates));
1416 const size_t numTemplates = templates.size();
1417 if ( !numTemplates )
c801d85f 1418 {
c77049a0
VZ
1419 // no templates can be used, can't create document
1420 return NULL;
c801d85f
KB
1421 }
1422
42771621 1423
c77049a0
VZ
1424 // normally user should select the template to use but wxDOC_SILENT flag we
1425 // choose one ourselves
1426 wxString path = pathOrig; // may be modified below
1427 wxDocTemplate *temp;
1428 if ( flags & wxDOC_SILENT )
1429 {
1430 wxASSERT_MSG( !path.empty(),
1431 "using empty path with wxDOC_SILENT doesn't make sense" );
42771621 1432
c77049a0
VZ
1433 temp = FindTemplateForPath(path);
1434 if ( !temp )
1435 {
1436 wxLogWarning(_("The format of file '%s' couldn't be determined."),
1437 path);
0fb67cd1 1438 }
c77049a0
VZ
1439 }
1440 else // not silent, ask the user
1441 {
1442 // for the new file we need just the template, for an existing one we
1443 // need the template and the path, unless it's already specified
1444 if ( (flags & wxDOC_NEW) || !path.empty() )
1445 temp = SelectDocumentType(&templates[0], numTemplates);
1446 else
1447 temp = SelectDocumentPath(&templates[0], numTemplates, path, flags);
1448 }
0fb67cd1 1449
c77049a0
VZ
1450 if ( !temp )
1451 return NULL;
42771621 1452
c77049a0
VZ
1453 // check whether the document with this path is already opened
1454 if ( !path.empty() )
1455 {
1456 const wxFileName fn(path);
1457 for ( wxList::const_iterator i = m_docs.begin(); i != m_docs.end(); ++i )
1458 {
1459 wxDocument * const doc = (wxDocument*)*i;
b72b1920 1460
c77049a0 1461 if ( fn == doc->GetFilename() )
0fb67cd1 1462 {
c77049a0 1463 // file already open, just activate it and return
8325504f 1464 ActivateDocument(doc);
54cbdc64 1465 return doc;
0fb67cd1 1466 }
0fb67cd1 1467 }
c801d85f 1468 }
c801d85f 1469
c801d85f 1470
c77049a0 1471 // no, we need to create a new document
c801d85f 1472
c77049a0
VZ
1473
1474 // if we've reached the max number of docs, close the first one.
1475 if ( (int)GetDocuments().GetCount() >= m_maxDocsOpen )
9d4a05be 1476 {
c77049a0 1477 if ( !CloseDocument((wxDocument *)GetDocuments().GetFirst()->GetData()) )
9d4a05be 1478 {
c77049a0
VZ
1479 // can't open the new document if closing the old one failed
1480 return NULL;
9d4a05be
JS
1481 }
1482 }
c801d85f 1483
c801d85f 1484
c77049a0
VZ
1485 // do create and initialize the new document finally
1486 wxDocument * const docNew = temp->CreateDocument(path, flags);
1487 if ( !docNew )
1488 return NULL;
42771621 1489
c77049a0
VZ
1490 docNew->SetDocumentName(temp->GetDocumentName());
1491 docNew->SetDocumentTemplate(temp);
6489e7ee 1492
7339d7bd 1493 wxTRY
c77049a0 1494 {
7339d7bd
VZ
1495 // call the appropriate function depending on whether we're creating a
1496 // new file or opening an existing one
1497 if ( !(flags & wxDOC_NEW ? docNew->OnNewDocument()
1498 : docNew->OnOpenDocument(path)) )
1499 {
1500 docNew->DeleteAllViews();
1501 return NULL;
1502 }
c801d85f 1503 }
7339d7bd 1504 wxCATCH_ALL( docNew->DeleteAllViews(); throw; )
3ca6a5f0 1505
c77049a0
VZ
1506 // add the successfully opened file to MRU, but only if we're going to be
1507 // able to reopen it successfully later which requires the template for
1508 // this document to be retrievable from the file extension
1509 if ( !(flags & wxDOC_NEW) && temp->FileMatchesTemplate(path) )
1510 AddFileToHistory(path);
1511
8325504f
VZ
1512 // at least under Mac (where views are top level windows) it seems to be
1513 // necessary to manually activate the new document to bring it to the
1514 // forefront -- and it shouldn't hurt doing this under the other platforms
1515 ActivateDocument(docNew);
1516
c77049a0 1517 return docNew;
c801d85f
KB
1518}
1519
1520wxView *wxDocManager::CreateView(wxDocument *doc, long flags)
1521{
c77049a0
VZ
1522 wxDocTemplates templates(GetVisibleTemplates(m_templates));
1523 const size_t numTemplates = templates.size();
b1d4dd7a 1524
c77049a0
VZ
1525 if ( numTemplates == 0 )
1526 return NULL;
0fb67cd1 1527
c77049a0
VZ
1528 wxDocTemplate * const
1529 temp = numTemplates == 1 ? templates[0]
1530 : SelectViewType(&templates[0], numTemplates);
1531
1532 if ( !temp )
1533 return NULL;
1534
1535 wxView *view = temp->CreateView(doc, flags);
1536 if ( view )
1537 view->SetViewName(temp->GetViewName());
1538 return view;
c801d85f
KB
1539}
1540
1541// Not yet implemented
c77049a0
VZ
1542void
1543wxDocManager::DeleteTemplate(wxDocTemplate *WXUNUSED(temp), long WXUNUSED(flags))
c801d85f
KB
1544{
1545}
1546
1547// Not yet implemented
1548bool wxDocManager::FlushDoc(wxDocument *WXUNUSED(doc))
1549{
68379eaf 1550 return false;
c801d85f
KB
1551}
1552
6de2f8b9 1553wxDocument *wxDocManager::GetCurrentDocument() const
c801d85f 1554{
575e4cd6 1555 wxView * const view = GetActiveView();
89d94e04
VZ
1556 return view ? view->GetDocument() : NULL;
1557}
1558
1559wxCommandProcessor *wxDocManager::GetCurrentCommandProcessor() const
1560{
1561 wxDocument * const doc = GetCurrentDocument();
1562 return doc ? doc->GetCommandProcessor() : NULL;
c801d85f
KB
1563}
1564
724b119a
VZ
1565// Make a default name for a new document
1566#if WXWIN_COMPATIBILITY_2_8
1567bool wxDocManager::MakeDefaultName(wxString& WXUNUSED(name))
1568{
1569 // we consider that this function can only be overridden by the user code,
1570 // not called by it as it only makes sense to call it internally, so we
1571 // don't bother to return anything from here
1572 return false;
1573}
1574#endif // WXWIN_COMPATIBILITY_2_8
1575
1576wxString wxDocManager::MakeNewDocumentName()
c801d85f 1577{
724b119a 1578 wxString name;
53c6e7cc 1579
724b119a
VZ
1580#if WXWIN_COMPATIBILITY_2_8
1581 if ( !MakeDefaultName(name) )
1582#endif // WXWIN_COMPATIBILITY_2_8
1583 {
1584 name.Printf(_("unnamed%d"), m_defaultDocumentNameCounter);
1585 m_defaultDocumentNameCounter++;
1586 }
1587
1588 return name;
c801d85f
KB
1589}
1590
f2506310
JS
1591// Make a frame title (override this to do something different)
1592// If docName is empty, a document is not currently active.
1593wxString wxDocManager::MakeFrameTitle(wxDocument* doc)
1594{
9cf3d218 1595 wxString appName = wxTheApp->GetAppDisplayName();
f2506310
JS
1596 wxString title;
1597 if (!doc)
1598 title = appName;
1599 else
1600 {
724b119a 1601 wxString docName = doc->GetUserReadableName();
f2506310
JS
1602 title = docName + wxString(_(" - ")) + appName;
1603 }
1604 return title;
1605}
1606
1607
c801d85f
KB
1608// Not yet implemented
1609wxDocTemplate *wxDocManager::MatchTemplate(const wxString& WXUNUSED(path))
1610{
c77049a0 1611 return NULL;
c801d85f
KB
1612}
1613
1614// File history management
1615void wxDocManager::AddFileToHistory(const wxString& file)
1616{
0fb67cd1
VZ
1617 if (m_fileHistory)
1618 m_fileHistory->AddFileToHistory(file);
c801d85f
KB
1619}
1620
e49c85af 1621void wxDocManager::RemoveFileFromHistory(size_t i)
0c5d3e1c
VZ
1622{
1623 if (m_fileHistory)
1624 m_fileHistory->RemoveFileFromHistory(i);
1625}
1626
e49c85af 1627wxString wxDocManager::GetHistoryFile(size_t i) const
c801d85f 1628{
0fb67cd1
VZ
1629 wxString histFile;
1630
1631 if (m_fileHistory)
1632 histFile = m_fileHistory->GetHistoryFile(i);
1633
1634 return histFile;
c801d85f
KB
1635}
1636
1637void wxDocManager::FileHistoryUseMenu(wxMenu *menu)
1638{
0fb67cd1
VZ
1639 if (m_fileHistory)
1640 m_fileHistory->UseMenu(menu);
c801d85f
KB
1641}
1642
7f555861 1643void wxDocManager::FileHistoryRemoveMenu(wxMenu *menu)
c801d85f 1644{
0fb67cd1
VZ
1645 if (m_fileHistory)
1646 m_fileHistory->RemoveMenu(menu);
c801d85f
KB
1647}
1648
702ca7c0 1649#if wxUSE_CONFIG
9c8116f8 1650void wxDocManager::FileHistoryLoad(const wxConfigBase& config)
c801d85f 1651{
0fb67cd1
VZ
1652 if (m_fileHistory)
1653 m_fileHistory->Load(config);
7f555861
JS
1654}
1655
1656void wxDocManager::FileHistorySave(wxConfigBase& config)
1657{
0fb67cd1
VZ
1658 if (m_fileHistory)
1659 m_fileHistory->Save(config);
7f555861 1660}
ac57418f 1661#endif
7f555861
JS
1662
1663void wxDocManager::FileHistoryAddFilesToMenu(wxMenu* menu)
1664{
0fb67cd1
VZ
1665 if (m_fileHistory)
1666 m_fileHistory->AddFilesToMenu(menu);
7f555861
JS
1667}
1668
1669void wxDocManager::FileHistoryAddFilesToMenu()
1670{
0fb67cd1
VZ
1671 if (m_fileHistory)
1672 m_fileHistory->AddFilesToMenu();
c801d85f
KB
1673}
1674
7af6b69e 1675size_t wxDocManager::GetHistoryFilesCount() const
c801d85f 1676{
7af6b69e 1677 return m_fileHistory ? m_fileHistory->GetCount() : 0;
c801d85f
KB
1678}
1679
1680
6de2f8b9
VZ
1681// Find out the document template via matching in the document file format
1682// against that of the template
c801d85f
KB
1683wxDocTemplate *wxDocManager::FindTemplateForPath(const wxString& path)
1684{
c77049a0 1685 wxDocTemplate *theTemplate = NULL;
c801d85f 1686
0fb67cd1 1687 // Find the template which this extension corresponds to
b1d4dd7a 1688 for (size_t i = 0; i < m_templates.GetCount(); i++)
c801d85f 1689 {
b1d4dd7a 1690 wxDocTemplate *temp = (wxDocTemplate *)m_templates.Item(i)->GetData();
6de2f8b9 1691 if ( temp->FileMatchesTemplate(path) )
0fb67cd1
VZ
1692 {
1693 theTemplate = temp;
1694 break;
1695 }
c801d85f 1696 }
0fb67cd1 1697 return theTemplate;
c801d85f
KB
1698}
1699
1700// Prompts user to open a file, using file specs in templates.
9d4a05be
JS
1701// Must extend the file selector dialog or implement own; OR
1702// match the extension to the template extension.
df875e59 1703
c801d85f 1704wxDocTemplate *wxDocManager::SelectDocumentPath(wxDocTemplate **templates,
0fb67cd1
VZ
1705 int noTemplates,
1706 wxString& path,
1707 long WXUNUSED(flags),
1708 bool WXUNUSED(save))
c801d85f 1709{
89d94e04 1710#ifdef wxHAS_MULTIPLE_FILEDLG_FILTERS
ba681060
VZ
1711 wxString descrBuf;
1712
89d94e04 1713 for (int i = 0; i < noTemplates; i++)
c801d85f 1714 {
ba681060
VZ
1715 if (templates[i]->IsVisible())
1716 {
1717 // add a '|' to separate this filter from the previous one
b494c48b 1718 if ( !descrBuf.empty() )
223d09f6 1719 descrBuf << wxT('|');
ba681060
VZ
1720
1721 descrBuf << templates[i]->GetDescription()
223d09f6 1722 << wxT(" (") << templates[i]->GetFileFilter() << wxT(") |")
0fb67cd1 1723 << templates[i]->GetFileFilter();
ba681060 1724 }
c801d85f 1725 }
a4294b78 1726#else
223d09f6 1727 wxString descrBuf = wxT("*.*");
c77049a0 1728 wxUnusedVar(noTemplates);
a4294b78 1729#endif
c801d85f 1730
3ca6a5f0 1731 int FilterIndex = -1;
f6bcfd97 1732
f6fe9f9c 1733 wxString pathTmp = wxFileSelectorEx(_("Open File"),
d8efd219 1734 GetLastDirectory(),
b494c48b 1735 wxEmptyString,
6de2f8b9 1736 &FilterIndex,
97aa1a74
VZ
1737 descrBuf,
1738 wxFD_OPEN | wxFD_FILE_MUST_EXIST);
ba681060 1739
c77049a0 1740 wxDocTemplate *theTemplate = NULL;
b494c48b 1741 if (!pathTmp.empty())
0fb67cd1 1742 {
f6bcfd97
BP
1743 if (!wxFileExists(pathTmp))
1744 {
1745 wxString msgTitle;
9cf3d218
VZ
1746 if (!wxTheApp->GetAppDisplayName().empty())
1747 msgTitle = wxTheApp->GetAppDisplayName();
f6bcfd97
BP
1748 else
1749 msgTitle = wxString(_("File error"));
02718e6a 1750
89d94e04
VZ
1751 wxMessageBox(_("Sorry, could not open this file."),
1752 msgTitle,
b6e96faf 1753 wxOK | wxICON_EXCLAMATION | wxCENTRE);
f6bcfd97 1754
b494c48b 1755 path = wxEmptyString;
c77049a0 1756 return NULL;
f6bcfd97 1757 }
d8efd219
VZ
1758
1759 SetLastDirectory(wxPathOnly(pathTmp));
ac0ac824 1760
0fb67cd1 1761 path = pathTmp;
0fb67cd1 1762
3ca6a5f0
BP
1763 // first choose the template using the extension, if this fails (i.e.
1764 // wxFileSelectorEx() didn't fill it), then use the path
1765 if ( FilterIndex != -1 )
6de2f8b9 1766 theTemplate = templates[FilterIndex];
3ca6a5f0
BP
1767 if ( !theTemplate )
1768 theTemplate = FindTemplateForPath(path);
9d4a05be
JS
1769 if ( !theTemplate )
1770 {
89d94e04
VZ
1771 // Since we do not add files with non-default extensions to the
1772 // file history this can only happen if the application changes the
1773 // allowed templates in runtime.
1774 wxMessageBox(_("Sorry, the format for this file is unknown."),
1775 _("Open File"),
b6e96faf 1776 wxOK | wxICON_EXCLAMATION | wxCENTRE);
9d4a05be 1777 }
0fb67cd1
VZ
1778 }
1779 else
1780 {
89d94e04 1781 path.clear();
0fb67cd1 1782 }
3ca6a5f0
BP
1783
1784 return theTemplate;
c801d85f
KB
1785}
1786
1787wxDocTemplate *wxDocManager::SelectDocumentType(wxDocTemplate **templates,
52b9ca21 1788 int noTemplates, bool sort)
0fb67cd1 1789{
222ed1d6 1790 wxArrayString strings;
89d94e04 1791 wxScopedArray<wxDocTemplate *> data(new wxDocTemplate *[noTemplates]);
0fb67cd1
VZ
1792 int i;
1793 int n = 0;
42771621
VZ
1794
1795 for (i = 0; i < noTemplates; i++)
1796 {
1797 if (templates[i]->IsVisible())
1798 {
1799 int j;
68379eaf 1800 bool want = true;
42771621
VZ
1801 for (j = 0; j < n; j++)
1802 {
bb28b477 1803 //filter out NOT unique documents + view combinations
42771621 1804 if ( templates[i]->m_docTypeName == data[j]->m_docTypeName &&
bb28b477
JS
1805 templates[i]->m_viewTypeName == data[j]->m_viewTypeName
1806 )
68379eaf 1807 want = false;
42771621 1808 }
bb28b477
JS
1809
1810 if ( want )
42771621
VZ
1811 {
1812 strings.Add(templates[i]->m_description);
1813
1814 data[n] = templates[i];
1815 n ++;
1816 }
1817 }
1818 } // for
1819
1820 if (sort)
1821 {
a2a03d78 1822 strings.Sort(); // ascending sort
42771621
VZ
1823 // Yes, this will be slow, but template lists
1824 // are typically short.
1825 int j;
1826 n = strings.Count();
1827 for (i = 0; i < n; i++)
1828 {
1829 for (j = 0; j < noTemplates; j++)
1830 {
1831 if (strings[i] == templates[j]->m_description)
1832 data[i] = templates[j];
1833 }
1834 }
1835 }
17260efd
VZ
1836
1837 wxDocTemplate *theTemplate;
1838
1839 switch ( n )
0fb67cd1 1840 {
17260efd
VZ
1841 case 0:
1842 // no visible templates, hence nothing to choose from
1843 theTemplate = NULL;
1844 break;
0fb67cd1 1845
17260efd 1846 case 1:
c77049a0 1847 // don't propose the user to choose if he has no choice
17260efd
VZ
1848 theTemplate = data[0];
1849 break;
1850
1851 default:
1852 // propose the user to choose one of several
1853 theTemplate = (wxDocTemplate *)wxGetSingleChoiceData
1854 (
1855 _("Select a document template"),
1856 _("Templates"),
1857 strings,
b6e96faf 1858 (void **)data.get()
17260efd
VZ
1859 );
1860 }
f6bcfd97 1861
0fb67cd1 1862 return theTemplate;
c801d85f
KB
1863}
1864
1865wxDocTemplate *wxDocManager::SelectViewType(wxDocTemplate **templates,
52b9ca21 1866 int noTemplates, bool sort)
c801d85f 1867{
222ed1d6 1868 wxArrayString strings;
89d94e04 1869 wxScopedArray<wxDocTemplate *> data(new wxDocTemplate *[noTemplates]);
0fb67cd1
VZ
1870 int i;
1871 int n = 0;
42771621 1872
0fb67cd1 1873 for (i = 0; i < noTemplates; i++)
c801d85f 1874 {
17260efd
VZ
1875 wxDocTemplate *templ = templates[i];
1876 if ( templ->IsVisible() && !templ->GetViewName().empty() )
0fb67cd1 1877 {
42771621 1878 int j;
68379eaf 1879 bool want = true;
42771621
VZ
1880 for (j = 0; j < n; j++)
1881 {
bb28b477 1882 //filter out NOT unique views
42771621 1883 if ( templates[i]->m_viewTypeName == data[j]->m_viewTypeName )
68379eaf 1884 want = false;
42771621 1885 }
bb28b477
JS
1886
1887 if ( want )
1888 {
42771621
VZ
1889 strings.Add(templ->m_viewTypeName);
1890 data[n] = templ;
1891 n ++;
1892 }
0fb67cd1 1893 }
c801d85f 1894 }
f6bcfd97 1895
42771621
VZ
1896 if (sort)
1897 {
a2a03d78 1898 strings.Sort(); // ascending sort
42771621
VZ
1899 // Yes, this will be slow, but template lists
1900 // are typically short.
1901 int j;
1902 n = strings.Count();
1903 for (i = 0; i < n; i++)
1904 {
1905 for (j = 0; j < noTemplates; j++)
1906 {
1907 if (strings[i] == templates[j]->m_viewTypeName)
1908 data[i] = templates[j];
1909 }
1910 }
1911 }
8658ef93 1912
17260efd
VZ
1913 wxDocTemplate *theTemplate;
1914
1915 // the same logic as above
1916 switch ( n )
1917 {
1918 case 0:
c77049a0 1919 theTemplate = NULL;
17260efd
VZ
1920 break;
1921
1922 case 1:
1923 theTemplate = data[0];
1924 break;
1925
1926 default:
1927 theTemplate = (wxDocTemplate *)wxGetSingleChoiceData
1928 (
1929 _("Select a document view"),
1930 _("Views"),
1931 strings,
b6e96faf 1932 (void **)data.get()
17260efd
VZ
1933 );
1934
1935 }
1936
0fb67cd1 1937 return theTemplate;
c801d85f
KB
1938}
1939
1940void wxDocManager::AssociateTemplate(wxDocTemplate *temp)
1941{
0fb67cd1
VZ
1942 if (!m_templates.Member(temp))
1943 m_templates.Append(temp);
c801d85f
KB
1944}
1945
1946void wxDocManager::DisassociateTemplate(wxDocTemplate *temp)
1947{
0fb67cd1 1948 m_templates.DeleteObject(temp);
c801d85f
KB
1949}
1950
a70d268a
VZ
1951wxDocTemplate* wxDocManager::FindTemplate(const wxClassInfo* classinfo)
1952{
1953 for ( wxList::compatibility_iterator node = m_templates.GetFirst();
1954 node;
1955 node = node->GetNext() )
1956 {
1957 wxDocTemplate* t = wxStaticCast(node->GetData(), wxDocTemplate);
1958 if ( t->GetDocClassInfo() == classinfo )
1959 return t;
1960 }
1961
1962 return NULL;
1963}
1964
c801d85f
KB
1965// Add and remove a document from the manager's list
1966void wxDocManager::AddDocument(wxDocument *doc)
1967{
0fb67cd1
VZ
1968 if (!m_docs.Member(doc))
1969 m_docs.Append(doc);
c801d85f
KB
1970}
1971
1972void wxDocManager::RemoveDocument(wxDocument *doc)
1973{
0fb67cd1 1974 m_docs.DeleteObject(doc);
c801d85f
KB
1975}
1976
1977// Views or windows should inform the document manager
1978// when a view is going in or out of focus
18759f78
VZ
1979void wxDocManager::ActivateView(wxView *view, bool activate)
1980{
1981 if ( activate )
0fb67cd1 1982 {
18759f78
VZ
1983 m_currentView = view;
1984 }
1985 else // deactivate
1986 {
1987 if ( m_currentView == view )
1988 {
1989 // don't keep stale pointer
c77049a0 1990 m_currentView = NULL;
18759f78 1991 }
0fb67cd1 1992 }
c801d85f
KB
1993}
1994
0fb67cd1 1995// ----------------------------------------------------------------------------
a9e2e6e5 1996// wxDocChildFrameAnyBase
0fb67cd1 1997// ----------------------------------------------------------------------------
c801d85f 1998
a9e2e6e5 1999bool wxDocChildFrameAnyBase::CloseView(wxCloseEvent& event)
c801d85f 2000{
8cc208e3
VZ
2001 if ( m_childView )
2002 {
cf30eac2
VZ
2003 // notice that we must call wxView::Close() and OnClose() called from
2004 // it in any case, even if we know that we are going to close anyhow
2005 if ( !m_childView->Close(false) && event.CanVeto() )
a9e2e6e5
VZ
2006 {
2007 event.Veto();
2008 return false;
2009 }
89d94e04 2010
a9e2e6e5 2011 m_childView->Activate(false);
ada1ff0d
VZ
2012
2013 // it is important to reset m_childView frame pointer to NULL before
2014 // deleting it because while normally it is the frame which deletes the
2015 // view when it's closed, the view also closes the frame if it is
2016 // deleted directly not by us as indicated by its doc child frame
2017 // pointer still being set
2018 m_childView->SetDocChildFrame(NULL);
5276b0a5 2019 wxDELETE(m_childView);
89d94e04 2020 }
387a3b02 2021
89d94e04 2022 m_childDocument = NULL;
0fb67cd1 2023
a9e2e6e5 2024 return true;
c801d85f
KB
2025}
2026
0fb67cd1 2027// ----------------------------------------------------------------------------
c48bdb0c 2028// wxDocParentFrameAnyBase
0fb67cd1 2029// ----------------------------------------------------------------------------
c801d85f 2030
47d67540 2031#if wxUSE_PRINTING_ARCHITECTURE
c801d85f 2032
302674fe
VZ
2033namespace
2034{
2035
2036wxString GetAppropriateTitle(const wxView *view, const wxString& titleGiven)
2037{
2038 wxString title(titleGiven);
2039 if ( title.empty() )
2040 {
2041 if ( view && view->GetDocument() )
2042 title = view->GetDocument()->GetUserReadableName();
2043 else
2044 title = _("Printout");
2045 }
2046
2047 return title;
2048}
2049
2050} // anonymous namespace
2051
0fb67cd1 2052wxDocPrintout::wxDocPrintout(wxView *view, const wxString& title)
302674fe 2053 : wxPrintout(GetAppropriateTitle(view, title))
c801d85f 2054{
0fb67cd1 2055 m_printoutView = view;
c801d85f
KB
2056}
2057
2058bool wxDocPrintout::OnPrintPage(int WXUNUSED(page))
2059{
0fb67cd1
VZ
2060 wxDC *dc = GetDC();
2061
2062 // Get the logical pixels per inch of screen and printer
2063 int ppiScreenX, ppiScreenY;
2064 GetPPIScreen(&ppiScreenX, &ppiScreenY);
999836aa 2065 wxUnusedVar(ppiScreenY);
0fb67cd1
VZ
2066 int ppiPrinterX, ppiPrinterY;
2067 GetPPIPrinter(&ppiPrinterX, &ppiPrinterY);
999836aa 2068 wxUnusedVar(ppiPrinterY);
0fb67cd1
VZ
2069
2070 // This scales the DC so that the printout roughly represents the
2071 // the screen scaling. The text point size _should_ be the right size
2072 // but in fact is too small for some reason. This is a detail that will
2073 // need to be addressed at some point but can be fudged for the
2074 // moment.
2075 float scale = (float)((float)ppiPrinterX/(float)ppiScreenX);
2076
2077 // Now we have to check in case our real page size is reduced
2078 // (e.g. because we're drawing to a print preview memory DC)
2079 int pageWidth, pageHeight;
2080 int w, h;
2081 dc->GetSize(&w, &h);
2082 GetPageSizePixels(&pageWidth, &pageHeight);
999836aa 2083 wxUnusedVar(pageHeight);
0fb67cd1
VZ
2084
2085 // If printer pageWidth == current DC width, then this doesn't
2086 // change. But w might be the preview bitmap width, so scale down.
2087 float overallScale = scale * (float)(w/(float)pageWidth);
2088 dc->SetUserScale(overallScale, overallScale);
2089
2090 if (m_printoutView)
2091 {
2092 m_printoutView->OnDraw(dc);
2093 }
68379eaf 2094 return true;
c801d85f
KB
2095}
2096
2097bool wxDocPrintout::HasPage(int pageNum)
2098{
0fb67cd1 2099 return (pageNum == 1);
c801d85f
KB
2100}
2101
2102bool wxDocPrintout::OnBeginDocument(int startPage, int endPage)
2103{
0fb67cd1 2104 if (!wxPrintout::OnBeginDocument(startPage, endPage))
68379eaf 2105 return false;
c801d85f 2106
68379eaf 2107 return true;
c801d85f
KB
2108}
2109
89d94e04
VZ
2110void wxDocPrintout::GetPageInfo(int *minPage, int *maxPage,
2111 int *selPageFrom, int *selPageTo)
c801d85f 2112{
0fb67cd1
VZ
2113 *minPage = 1;
2114 *maxPage = 1;
2115 *selPageFrom = 1;
2116 *selPageTo = 1;
c801d85f
KB
2117}
2118
0fb67cd1 2119#endif // wxUSE_PRINTING_ARCHITECTURE
c801d85f 2120
0fb67cd1
VZ
2121// ----------------------------------------------------------------------------
2122// Permits compatibility with existing file formats and functions that
2123// manipulate files directly
2124// ----------------------------------------------------------------------------
c801d85f 2125
a533f5c1 2126#if wxUSE_STD_IOSTREAM
fb6e5b17 2127
dd107c50 2128bool wxTransferFileToStream(const wxString& filename, wxSTD ostream& stream)
c801d85f 2129{
ce0822e1 2130#if wxUSE_FFILE
9a83f860 2131 wxFFile file(filename, wxT("rb"));
ce0822e1
VZ
2132#elif wxUSE_FILE
2133 wxFile file(filename, wxFile::read);
2134#endif
fb6e5b17 2135 if ( !file.IsOpened() )
68379eaf 2136 return false;
c801d85f 2137
fb6e5b17
VZ
2138 char buf[4096];
2139
2140 size_t nRead;
2141 do
2142 {
2143 nRead = file.Read(buf, WXSIZEOF(buf));
2144 if ( file.Error() )
68379eaf 2145 return false;
fb6e5b17
VZ
2146
2147 stream.write(buf, nRead);
2148 if ( !stream )
68379eaf 2149 return false;
fb6e5b17
VZ
2150 }
2151 while ( !file.Eof() );
c801d85f 2152
68379eaf 2153 return true;
c801d85f
KB
2154}
2155
dd107c50 2156bool wxTransferStreamToFile(wxSTD istream& stream, const wxString& filename)
c801d85f 2157{
ce0822e1 2158#if wxUSE_FFILE
9a83f860 2159 wxFFile file(filename, wxT("wb"));
ce0822e1
VZ
2160#elif wxUSE_FILE
2161 wxFile file(filename, wxFile::write);
2162#endif
fb6e5b17 2163 if ( !file.IsOpened() )
68379eaf 2164 return false;
c801d85f 2165
fb6e5b17
VZ
2166 char buf[4096];
2167 do
0fb67cd1 2168 {
fb6e5b17
VZ
2169 stream.read(buf, WXSIZEOF(buf));
2170 if ( !stream.bad() ) // fail may be set on EOF, don't use operator!()
2171 {
2172 if ( !file.Write(buf, stream.gcount()) )
68379eaf 2173 return false;
fb6e5b17 2174 }
0fb67cd1 2175 }
fb6e5b17
VZ
2176 while ( !stream.eof() );
2177
68379eaf 2178 return true;
c801d85f 2179}
fb6e5b17
VZ
2180
2181#else // !wxUSE_STD_IOSTREAM
2182
dc1efb1d
JS
2183bool wxTransferFileToStream(const wxString& filename, wxOutputStream& stream)
2184{
ce0822e1 2185#if wxUSE_FFILE
9a83f860 2186 wxFFile file(filename, wxT("rb"));
ce0822e1
VZ
2187#elif wxUSE_FILE
2188 wxFile file(filename, wxFile::read);
2189#endif
fb6e5b17 2190 if ( !file.IsOpened() )
68379eaf 2191 return false;
dc1efb1d 2192
fb6e5b17
VZ
2193 char buf[4096];
2194
2195 size_t nRead;
2196 do
2197 {
2198 nRead = file.Read(buf, WXSIZEOF(buf));
2199 if ( file.Error() )
68379eaf 2200 return false;
fb6e5b17
VZ
2201
2202 stream.Write(buf, nRead);
2203 if ( !stream )
68379eaf 2204 return false;
fb6e5b17
VZ
2205 }
2206 while ( !file.Eof() );
dc1efb1d 2207
68379eaf 2208 return true;
dc1efb1d
JS
2209}
2210
2211bool wxTransferStreamToFile(wxInputStream& stream, const wxString& filename)
2212{
ce0822e1 2213#if wxUSE_FFILE
9a83f860 2214 wxFFile file(filename, wxT("wb"));
ce0822e1
VZ
2215#elif wxUSE_FILE
2216 wxFile file(filename, wxFile::write);
2217#endif
fb6e5b17 2218 if ( !file.IsOpened() )
68379eaf 2219 return false;
dc1efb1d 2220
fb6e5b17 2221 char buf[4096];
07f87b6b 2222 for ( ;; )
dc1efb1d 2223 {
fb6e5b17
VZ
2224 stream.Read(buf, WXSIZEOF(buf));
2225
2226 const size_t nRead = stream.LastRead();
07f87b6b
VZ
2227 if ( !nRead )
2228 {
2229 if ( stream.Eof() )
2230 break;
2231
2232 return false;
2233 }
2234
2235 if ( !file.Write(buf, nRead) )
68379eaf 2236 return false;
dc1efb1d 2237 }
fb6e5b17 2238
68379eaf 2239 return true;
dc1efb1d 2240}
fb6e5b17
VZ
2241
2242#endif // wxUSE_STD_IOSTREAM/!wxUSE_STD_IOSTREAM
c801d85f 2243
0fb67cd1 2244#endif // wxUSE_DOC_VIEW_ARCHITECTURE