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