]> git.saurik.com Git - wxWidgets.git/blame - src/common/docview.cpp
Added detection of vsscanf and whether or not a prototype exists in headers.
[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
5// Modified by:
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
4fedd384
JS
47#include "wx/ffile.h"
48
b8afeb43
SC
49#ifdef __WXMAC__
50 #include "wx/filename.h"
51#endif
c2ff79b1 52
ce4169a4 53#if wxUSE_PRINTING_ARCHITECTURE
8ecff181
WS
54 #include "wx/prntbase.h"
55 #include "wx/printdlg.h"
ce4169a4
RR
56#endif
57
7f555861 58#include "wx/confbase.h"
74b31181 59#include "wx/file.h"
1e6feb95 60#include "wx/cmdproc.h"
0f30d8e3 61#include "wx/tokenzr.h"
b4a980f4 62#include "wx/filename.h"
c801d85f 63
c801d85f
KB
64#include <stdio.h>
65#include <string.h>
66
a533f5c1 67#if wxUSE_STD_IOSTREAM
8ecff181 68 #include "wx/ioswrap.h"
968454db 69 #include "wx/beforestd.h"
8ecff181
WS
70 #if wxUSE_IOSTREAMH
71 #include <fstream.h>
72 #else
73 #include <fstream>
74 #endif
968454db 75 #include "wx/afterstd.h"
a533f5c1 76#else
8ecff181 77 #include "wx/wfstream.h"
c801d85f
KB
78#endif
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
1e6feb95 95IMPLEMENT_DYNAMIC_CLASS(wxFileHistory, wxObject)
c801d85f 96
0fb67cd1
VZ
97// ----------------------------------------------------------------------------
98// function prototypes
99// ----------------------------------------------------------------------------
100
9d4a05be 101static wxWindow* wxFindSuitableParent(void);
0fb67cd1 102
0c5d3e1c
VZ
103// ----------------------------------------------------------------------------
104// local constants
105// ----------------------------------------------------------------------------
106
2695a14e 107static const wxChar *s_MRUEntryFormat = wxT("&%d %s");
0c5d3e1c 108
0fb67cd1
VZ
109// ============================================================================
110// implementation
111// ============================================================================
112
113// ----------------------------------------------------------------------------
114// local functions
115// ----------------------------------------------------------------------------
116
86501081 117static wxString FindExtension(const wxString& path)
0fb67cd1
VZ
118{
119 wxString ext;
120 wxSplitPath(path, NULL, NULL, &ext);
121
122 // VZ: extensions are considered not case sensitive - is this really a good
123 // idea?
124 return ext.MakeLower();
125}
126
127// ----------------------------------------------------------------------------
128// Definition of wxDocument
129// ----------------------------------------------------------------------------
c801d85f
KB
130
131wxDocument::wxDocument(wxDocument *parent)
132{
68379eaf 133 m_documentModified = false;
0fb67cd1
VZ
134 m_documentParent = parent;
135 m_documentTemplate = (wxDocTemplate *) NULL;
f6bcfd97 136 m_commandProcessor = (wxCommandProcessor*) NULL;
68379eaf 137 m_savedYet = false;
c801d85f
KB
138}
139
0fb67cd1 140bool wxDocument::DeleteContents()
c801d85f 141{
68379eaf 142 return true;
c801d85f
KB
143}
144
0fb67cd1 145wxDocument::~wxDocument()
c801d85f 146{
0fb67cd1 147 DeleteContents();
c801d85f 148
0fb67cd1
VZ
149 if (m_commandProcessor)
150 delete m_commandProcessor;
c801d85f 151
f6bcfd97
BP
152 if (GetDocumentManager())
153 GetDocumentManager()->RemoveDocument(this);
c801d85f 154
0fb67cd1
VZ
155 // Not safe to do here, since it'll invoke virtual view functions
156 // expecting to see valid derived objects: and by the time we get here,
157 // we've called destructors higher up.
158 //DeleteAllViews();
c801d85f 159}
0fb67cd1
VZ
160
161bool wxDocument::Close()
c801d85f 162{
0fb67cd1
VZ
163 if (OnSaveModified())
164 return OnCloseDocument();
165 else
68379eaf 166 return false;
c801d85f 167}
0fb67cd1
VZ
168
169bool wxDocument::OnCloseDocument()
c801d85f 170{
b23e843b 171 // Tell all views that we're about to close
42771621 172 NotifyClosing();
0fb67cd1 173 DeleteContents();
68379eaf
WS
174 Modify(false);
175 return true;
c801d85f
KB
176}
177
0fb67cd1
VZ
178// Note that this implicitly deletes the document when the last view is
179// deleted.
180bool wxDocument::DeleteAllViews()
c801d85f 181{
f6bcfd97
BP
182 wxDocManager* manager = GetDocumentManager();
183
40702099
VZ
184 // first check if all views agree to be closed
185 const wxList::iterator end = m_documentViews.end();
186 for ( wxList::iterator i = m_documentViews.begin(); i != end; ++i )
0fb67cd1 187 {
40702099
VZ
188 wxView *view = (wxView *)*i;
189 if ( !view->Close() )
68379eaf 190 return false;
0fb67cd1 191 }
d5a93fda 192
40702099
VZ
193 // all views agreed to close, now do close them
194 if ( m_documentViews.empty() )
195 {
196 // normally the document would be implicitly deleted when the last view
197 // is, but if don't have any views, do it here instead
198 if ( manager && manager->GetDocuments().Member(this) )
199 delete this;
200 }
201 else // have views
202 {
203 // as we delete elements we iterate over, don't use the usual "from
204 // begin to end" loop
205 for ( ;; )
206 {
207 wxView *view = (wxView *)*m_documentViews.begin();
208
209 bool isLastOne = m_documentViews.size() == 1;
210
211 // this always deletes the node implicitly and if this is the last
212 // view also deletes this object itself (also implicitly, great),
213 // so we can't test for m_documentViews.empty() after calling this!
214 delete view;
215
216 if ( isLastOne )
217 break;
218 }
219 }
f6bcfd97 220
68379eaf 221 return true;
c801d85f
KB
222}
223
6de2f8b9 224wxView *wxDocument::GetFirstView() const
c801d85f 225{
b1d4dd7a 226 if (m_documentViews.GetCount() == 0)
0fb67cd1 227 return (wxView *) NULL;
b1d4dd7a 228 return (wxView *)m_documentViews.GetFirst()->GetData();
c801d85f
KB
229}
230
6de2f8b9 231wxDocManager *wxDocument::GetDocumentManager() const
c801d85f 232{
f6bcfd97 233 return (m_documentTemplate ? m_documentTemplate->GetDocumentManager() : (wxDocManager*) NULL);
c801d85f
KB
234}
235
0fb67cd1 236bool wxDocument::OnNewDocument()
c801d85f 237{
0fb67cd1 238 if (!OnSaveModified())
68379eaf 239 return false;
c801d85f 240
68379eaf 241 if (OnCloseDocument()==false) return false;
0fb67cd1 242 DeleteContents();
68379eaf
WS
243 Modify(false);
244 SetDocumentSaved(false);
c801d85f 245
724b119a 246 const wxString name = GetDocumentManager()->MakeNewDocumentName();
0fb67cd1 247 SetTitle(name);
68379eaf 248 SetFilename(name, true);
0fb67cd1 249
68379eaf 250 return true;
c801d85f
KB
251}
252
0fb67cd1 253bool wxDocument::Save()
c801d85f 254{
d06a66f5 255 if (!IsModified() && m_savedYet)
68379eaf 256 return true;
c801d85f 257
d06a66f5
VZ
258 if ( m_documentFile.empty() || !m_savedYet )
259 return SaveAs();
260
261 return OnSaveDocument(m_documentFile);
c801d85f 262}
0fb67cd1
VZ
263
264bool wxDocument::SaveAs()
c801d85f 265{
ba681060
VZ
266 wxDocTemplate *docTemplate = GetDocumentTemplate();
267 if (!docTemplate)
68379eaf 268 return false;
0fb67cd1 269
04129514
JS
270#if defined(__WXMSW__) || defined(__WXGTK__) || defined(__WXMAC__)
271 wxString filter = docTemplate->GetDescription() + wxT(" (") + docTemplate->GetFileFilter() + wxT(")|") + docTemplate->GetFileFilter();
272
273 // Now see if there are some other template with identical view and document
274 // classes, whose filters may also be used.
275
276 if (docTemplate->GetViewClassInfo() && docTemplate->GetDocClassInfo())
277 {
e7b21b01 278 wxList::compatibility_iterator node = docTemplate->GetDocumentManager()->GetTemplates().GetFirst();
04129514
JS
279 while (node)
280 {
281 wxDocTemplate *t = (wxDocTemplate*) node->GetData();
68379eaf 282
04129514
JS
283 if (t->IsVisible() && t != docTemplate &&
284 t->GetViewClassInfo() == docTemplate->GetViewClassInfo() &&
285 t->GetDocClassInfo() == docTemplate->GetDocClassInfo())
286 {
287 // add a '|' to separate this filter from the previous one
b494c48b 288 if ( !filter.empty() )
04129514 289 filter << wxT('|');
68379eaf 290
04129514
JS
291 filter << t->GetDescription() << wxT(" (") << t->GetFileFilter() << wxT(") |")
292 << t->GetFileFilter();
293 }
294
295 node = node->GetNext();
296 }
297 }
298#else
299 wxString filter = docTemplate->GetFileFilter() ;
300#endif
e0214f1f
JS
301 wxString defaultDir = docTemplate->GetDirectory();
302 if (defaultDir.IsEmpty())
303 defaultDir = wxPathOnly(GetFilename());
304
f6fe9f9c 305 wxString tmp = wxFileSelector(_("Save As"),
e0214f1f 306 defaultDir,
015e69f3 307 wxFileNameFromPath(GetFilename()),
0fb67cd1 308 docTemplate->GetDefaultExtension(),
04129514 309 filter,
ff3e84ff 310 wxFD_SAVE | wxFD_OVERWRITE_PROMPT,
0fb67cd1
VZ
311 GetDocumentWindow());
312
b494c48b 313 if (tmp.empty())
68379eaf 314 return false;
ba681060 315
2bb0cd28 316 wxString fileName(tmp);
88ac883a 317 wxString path, name, ext;
2bb0cd28
JS
318 wxSplitPath(fileName, & path, & name, & ext);
319
b494c48b 320 if (ext.empty())
2bb0cd28 321 {
2b5f62a0 322 fileName += wxT(".");
2bb0cd28
JS
323 fileName += docTemplate->GetDefaultExtension();
324 }
325
326 SetFilename(fileName);
327 SetTitle(wxFileNameFromPath(fileName));
0fb67cd1 328
c801d85f 329 // Notify the views that the filename has changed
222ed1d6 330 wxList::compatibility_iterator node = m_documentViews.GetFirst();
c801d85f
KB
331 while (node)
332 {
b1d4dd7a 333 wxView *view = (wxView *)node->GetData();
0fb67cd1 334 view->OnChangeFilename();
b1d4dd7a 335 node = node->GetNext();
c801d85f 336 }
ba681060 337
9d4a05be
JS
338 // Files that were not saved correctly are not added to the FileHistory.
339 if (!OnSaveDocument(m_documentFile))
68379eaf 340 return false;
9d4a05be 341
42771621 342 // A file that doesn't use the default extension of its document template cannot be opened
9d4a05be
JS
343 // via the FileHistory, so we do not add it.
344 if (docTemplate->FileMatchesTemplate(fileName))
345 {
346 GetDocumentManager()->AddFileToHistory(fileName);
347 }
348 else
349 {
350 // The user will probably not be able to open the file again, so
351 // we could warn about the wrong file-extension here.
352 }
68379eaf 353 return true;
c801d85f 354}
0fb67cd1 355
c801d85f
KB
356bool wxDocument::OnSaveDocument(const wxString& file)
357{
0fb67cd1 358 if ( !file )
68379eaf 359 return false;
c801d85f 360
69936aea 361 if ( !DoSaveDocument(file) )
68379eaf 362 return false;
0fb67cd1 363
68379eaf 364 Modify(false);
0fb67cd1 365 SetFilename(file);
68379eaf 366 SetDocumentSaved(true);
b8afeb43
SC
367#ifdef __WXMAC__
368 wxFileName fn(file) ;
369 fn.MacSetDefaultTypeAndCreator() ;
370#endif
68379eaf 371 return true;
c801d85f 372}
0fb67cd1 373
c801d85f
KB
374bool wxDocument::OnOpenDocument(const wxString& file)
375{
0fb67cd1 376 if (!OnSaveModified())
68379eaf 377 return false;
c801d85f 378
69936aea 379 if ( !DoOpenDocument(file) )
68379eaf 380 return false;
0fb67cd1 381
68379eaf
WS
382 SetFilename(file, true);
383 Modify(false);
384 m_savedYet = true;
0fb67cd1
VZ
385
386 UpdateAllViews();
c801d85f 387
68379eaf 388 return true;
c801d85f 389}
0fb67cd1 390
a533f5c1 391#if wxUSE_STD_IOSTREAM
dd107c50 392wxSTD istream& wxDocument::LoadObject(wxSTD istream& stream)
23a54e14
RR
393#else
394wxInputStream& wxDocument::LoadObject(wxInputStream& stream)
395#endif
c801d85f 396{
0fb67cd1 397 return stream;
c801d85f
KB
398}
399
23a54e14 400#if wxUSE_STD_IOSTREAM
dd107c50 401wxSTD ostream& wxDocument::SaveObject(wxSTD ostream& stream)
a533f5c1 402#else
23a54e14
RR
403wxOutputStream& wxDocument::SaveObject(wxOutputStream& stream)
404#endif
a533f5c1 405{
23a54e14 406 return stream;
a533f5c1 407}
c801d85f 408
0fb67cd1 409bool wxDocument::Revert()
c801d85f 410{
68379eaf 411 return false;
c801d85f
KB
412}
413
414
415// Get title, or filename if no title, else unnamed
724b119a 416#if WXWIN_COMPATIBILITY_2_8
c801d85f
KB
417bool wxDocument::GetPrintableName(wxString& buf) const
418{
724b119a
VZ
419 // this function can not only be overridden by the user code but also
420 // called by it so we need to ensure that we return the same thing as
421 // GetUserReadableName() but we can't call it because this would result in
422 // an infinite recursion, hence we use the helper DoGetUserReadableName()
423 buf = DoGetUserReadableName();
424
425 return true;
426}
427#endif // WXWIN_COMPATIBILITY_2_8
428
429wxString wxDocument::GetUserReadableName() const
430{
431#if WXWIN_COMPATIBILITY_2_8
432 // we need to call the old virtual function to ensure that the overridden
433 // version of it is still called
434 wxString name;
435 if ( GetPrintableName(name) )
436 return name;
437#endif // WXWIN_COMPATIBILITY_2_8
438
439 return DoGetUserReadableName();
440}
441
442wxString wxDocument::DoGetUserReadableName() const
443{
444 if ( !m_documentTitle.empty() )
445 return m_documentTitle;
446
447 if ( !m_documentFile.empty() )
448 return wxFileNameFromPath(m_documentFile);
449
450 return _("unnamed");
c801d85f
KB
451}
452
6de2f8b9 453wxWindow *wxDocument::GetDocumentWindow() const
c801d85f 454{
0fb67cd1
VZ
455 wxView *view = GetFirstView();
456 if (view)
457 return view->GetFrame();
458 else
459 return wxTheApp->GetTopWindow();
c801d85f
KB
460}
461
0fb67cd1 462wxCommandProcessor *wxDocument::OnCreateCommandProcessor()
c801d85f 463{
0fb67cd1 464 return new wxCommandProcessor;
c801d85f
KB
465}
466
68379eaf 467// true if safe to close
0fb67cd1 468bool wxDocument::OnSaveModified()
c801d85f 469{
0fb67cd1 470 if (IsModified())
c801d85f 471 {
724b119a 472 wxString title = GetUserReadableName();
0fb67cd1
VZ
473
474 wxString msgTitle;
9cf3d218
VZ
475 if (!wxTheApp->GetAppDisplayName().empty())
476 msgTitle = wxTheApp->GetAppDisplayName();
0fb67cd1
VZ
477 else
478 msgTitle = wxString(_("Warning"));
479
480 wxString prompt;
86501081 481 prompt.Printf(_("Do you want to save changes to document %s?"), title);
0fb67cd1
VZ
482 int res = wxMessageBox(prompt, msgTitle,
483 wxYES_NO|wxCANCEL|wxICON_QUESTION,
484 GetDocumentWindow());
485 if (res == wxNO)
486 {
68379eaf
WS
487 Modify(false);
488 return true;
0fb67cd1
VZ
489 }
490 else if (res == wxYES)
491 return Save();
492 else if (res == wxCANCEL)
68379eaf 493 return false;
c801d85f 494 }
68379eaf 495 return true;
c801d85f
KB
496}
497
498bool wxDocument::Draw(wxDC& WXUNUSED(context))
499{
68379eaf 500 return true;
c801d85f
KB
501}
502
503bool wxDocument::AddView(wxView *view)
504{
0fb67cd1
VZ
505 if (!m_documentViews.Member(view))
506 {
507 m_documentViews.Append(view);
508 OnChangedViewList();
509 }
68379eaf 510 return true;
c801d85f
KB
511}
512
513bool wxDocument::RemoveView(wxView *view)
514{
0fb67cd1
VZ
515 (void)m_documentViews.DeleteObject(view);
516 OnChangedViewList();
68379eaf 517 return true;
c801d85f
KB
518}
519
520bool wxDocument::OnCreate(const wxString& WXUNUSED(path), long flags)
521{
0fb67cd1 522 if (GetDocumentTemplate()->CreateView(this, flags))
68379eaf 523 return true;
0fb67cd1 524 else
68379eaf 525 return false;
c801d85f
KB
526}
527
528// Called after a view is added or removed.
529// The default implementation deletes the document if
530// there are no more views.
0fb67cd1 531void wxDocument::OnChangedViewList()
c801d85f 532{
b1d4dd7a 533 if (m_documentViews.GetCount() == 0)
c801d85f 534 {
0fb67cd1
VZ
535 if (OnSaveModified())
536 {
537 delete this;
538 }
c801d85f 539 }
c801d85f
KB
540}
541
542void wxDocument::UpdateAllViews(wxView *sender, wxObject *hint)
543{
222ed1d6 544 wxList::compatibility_iterator node = m_documentViews.GetFirst();
0fb67cd1
VZ
545 while (node)
546 {
b1d4dd7a 547 wxView *view = (wxView *)node->GetData();
42771621 548 if (view != sender)
2b5f62a0 549 view->OnUpdate(sender, hint);
b1d4dd7a 550 node = node->GetNext();
0fb67cd1 551 }
c801d85f
KB
552}
553
b23e843b
JS
554void wxDocument::NotifyClosing()
555{
222ed1d6 556 wxList::compatibility_iterator node = m_documentViews.GetFirst();
b23e843b
JS
557 while (node)
558 {
b1d4dd7a 559 wxView *view = (wxView *)node->GetData();
b23e843b 560 view->OnClosingDocument();
b1d4dd7a 561 node = node->GetNext();
b23e843b
JS
562 }
563}
564
c801d85f
KB
565void wxDocument::SetFilename(const wxString& filename, bool notifyViews)
566{
0fb67cd1
VZ
567 m_documentFile = filename;
568 if ( notifyViews )
c801d85f 569 {
0fb67cd1 570 // Notify the views that the filename has changed
222ed1d6 571 wxList::compatibility_iterator node = m_documentViews.GetFirst();
0fb67cd1
VZ
572 while (node)
573 {
b1d4dd7a 574 wxView *view = (wxView *)node->GetData();
0fb67cd1 575 view->OnChangeFilename();
b1d4dd7a 576 node = node->GetNext();
0fb67cd1 577 }
c801d85f 578 }
c801d85f
KB
579}
580
69936aea
VZ
581bool wxDocument::DoSaveDocument(const wxString& file)
582{
583 wxString msgTitle;
9cf3d218
VZ
584 if (!wxTheApp->GetAppDisplayName().empty())
585 msgTitle = wxTheApp->GetAppDisplayName();
69936aea
VZ
586 else
587 msgTitle = wxString(_("File error"));
588
589#if wxUSE_STD_IOSTREAM
53e2e87c 590 wxSTD ofstream store(file.mb_str(), wxSTD ios::binary);
69936aea
VZ
591 if (store.fail() || store.bad())
592#else
593 wxFileOutputStream store(file);
594 if (store.GetLastError() != wxSTREAM_NO_ERROR)
595#endif
596 {
597 (void)wxMessageBox(_("Sorry, could not open this file for saving."), msgTitle, wxOK | wxICON_EXCLAMATION,
598 GetDocumentWindow());
599 // Saving error
68379eaf 600 return false;
69936aea
VZ
601 }
602 if (!SaveObject(store))
603 {
604 (void)wxMessageBox(_("Sorry, could not save this file."), msgTitle, wxOK | wxICON_EXCLAMATION,
605 GetDocumentWindow());
606 // Saving error
68379eaf 607 return false;
69936aea
VZ
608 }
609
68379eaf 610 return true;
69936aea
VZ
611}
612
613bool wxDocument::DoOpenDocument(const wxString& file)
614{
69936aea 615#if wxUSE_STD_IOSTREAM
53e2e87c 616 wxSTD ifstream store(file.mb_str(), wxSTD ios::binary);
64c12a6d 617 if (!store.fail() && !store.bad())
69936aea
VZ
618#else
619 wxFileInputStream store(file);
64c12a6d 620 if (store.GetLastError() == wxSTREAM_NO_ERROR)
69936aea
VZ
621#endif
622 {
69936aea 623#if wxUSE_STD_IOSTREAM
64c12a6d
VZ
624 LoadObject(store);
625 if ( !!store || store.eof() )
69936aea 626#else
64c12a6d
VZ
627 int res = LoadObject(store).GetLastError();
628 if ( res == wxSTREAM_NO_ERROR || res == wxSTREAM_EOF )
69936aea 629#endif
64c12a6d 630 return true;
69936aea
VZ
631 }
632
64c12a6d
VZ
633 wxLogError(_("Sorry, could not open this file."));
634 return false;
69936aea
VZ
635}
636
637
0fb67cd1
VZ
638// ----------------------------------------------------------------------------
639// Document view
640// ----------------------------------------------------------------------------
c801d85f 641
dbdb39b2 642wxView::wxView()
c801d85f 643{
0fb67cd1
VZ
644 m_viewDocument = (wxDocument*) NULL;
645
0fb67cd1 646 m_viewFrame = (wxFrame *) NULL;
c801d85f
KB
647}
648
0fb67cd1 649wxView::~wxView()
c801d85f 650{
68379eaf 651 GetDocumentManager()->ActivateView(this, false);
0fb67cd1 652 m_viewDocument->RemoveView(this);
c801d85f
KB
653}
654
655// Extend event processing to search the document's event table
656bool wxView::ProcessEvent(wxEvent& event)
657{
0fb67cd1
VZ
658 if ( !GetDocument() || !GetDocument()->ProcessEvent(event) )
659 return wxEvtHandler::ProcessEvent(event);
18759f78 660
68379eaf 661 return true;
c801d85f
KB
662}
663
664void wxView::OnActivateView(bool WXUNUSED(activate), wxView *WXUNUSED(activeView), wxView *WXUNUSED(deactiveView))
665{
666}
667
668void wxView::OnPrint(wxDC *dc, wxObject *WXUNUSED(info))
669{
0fb67cd1 670 OnDraw(dc);
c801d85f
KB
671}
672
673void wxView::OnUpdate(wxView *WXUNUSED(sender), wxObject *WXUNUSED(hint))
674{
675}
676
0fb67cd1 677void wxView::OnChangeFilename()
c801d85f 678{
faa49bfd
WS
679 // GetFrame can return wxWindow rather than wxTopLevelWindow due to
680 // generic MDI implementation so use SetLabel rather than SetTitle.
681 // It should cause SetTitle() for top level windows.
682 wxWindow *win = GetFrame();
683 if (!win) return;
f2506310 684
faa49bfd
WS
685 wxDocument *doc = GetDocument();
686 if (!doc) return;
c801d85f 687
724b119a 688 win->SetLabel(doc->GetUserReadableName());
c801d85f
KB
689}
690
691void wxView::SetDocument(wxDocument *doc)
692{
0fb67cd1
VZ
693 m_viewDocument = doc;
694 if (doc)
695 doc->AddView(this);
c801d85f
KB
696}
697
698bool wxView::Close(bool deleteWindow)
699{
0fb67cd1 700 if (OnClose(deleteWindow))
68379eaf 701 return true;
0fb67cd1 702 else
68379eaf 703 return false;
c801d85f
KB
704}
705
706void wxView::Activate(bool activate)
707{
bb28b477 708 if (GetDocument() && GetDocumentManager())
0fb67cd1
VZ
709 {
710 OnActivateView(activate, this, GetDocumentManager()->GetCurrentView());
711 GetDocumentManager()->ActivateView(this, activate);
712 }
c801d85f
KB
713}
714
715bool wxView::OnClose(bool WXUNUSED(deleteWindow))
716{
68379eaf 717 return GetDocument() ? GetDocument()->Close() : true;
c801d85f
KB
718}
719
47d67540 720#if wxUSE_PRINTING_ARCHITECTURE
0fb67cd1 721wxPrintout *wxView::OnCreatePrintout()
c801d85f 722{
0fb67cd1 723 return new wxDocPrintout(this);
c801d85f 724}
6de2f8b9 725#endif // wxUSE_PRINTING_ARCHITECTURE
c801d85f 726
0fb67cd1
VZ
727// ----------------------------------------------------------------------------
728// wxDocTemplate
729// ----------------------------------------------------------------------------
c801d85f 730
0fb67cd1
VZ
731wxDocTemplate::wxDocTemplate(wxDocManager *manager,
732 const wxString& descr,
733 const wxString& filter,
734 const wxString& dir,
735 const wxString& ext,
736 const wxString& docTypeName,
737 const wxString& viewTypeName,
738 wxClassInfo *docClassInfo,
739 wxClassInfo *viewClassInfo,
740 long flags)
c801d85f 741{
0fb67cd1 742 m_documentManager = manager;
0fb67cd1
VZ
743 m_description = descr;
744 m_directory = dir;
745 m_defaultExt = ext;
746 m_fileFilter = filter;
747 m_flags = flags;
748 m_docTypeName = docTypeName;
749 m_viewTypeName = viewTypeName;
750 m_documentManager->AssociateTemplate(this);
c801d85f 751
0fb67cd1
VZ
752 m_docClassInfo = docClassInfo;
753 m_viewClassInfo = viewClassInfo;
c801d85f
KB
754}
755
0fb67cd1 756wxDocTemplate::~wxDocTemplate()
c801d85f 757{
0fb67cd1 758 m_documentManager->DisassociateTemplate(this);
c801d85f 759}
0fb67cd1
VZ
760
761// Tries to dynamically construct an object of the right class.
c801d85f
KB
762wxDocument *wxDocTemplate::CreateDocument(const wxString& path, long flags)
763{
69936aea
VZ
764 wxDocument *doc = DoCreateDocument();
765 if ( doc == NULL )
0fb67cd1 766 return (wxDocument *) NULL;
68379eaf 767
217b7140
JS
768 if (InitDocument(doc, path, flags))
769 {
770 return doc;
771 }
772 else
773 {
774 return (wxDocument *) NULL;
775 }
776}
777
778bool wxDocTemplate::InitDocument(wxDocument* doc, const wxString& path, long flags)
779{
0fb67cd1
VZ
780 doc->SetFilename(path);
781 doc->SetDocumentTemplate(this);
782 GetDocumentManager()->AddDocument(doc);
783 doc->SetCommandProcessor(doc->OnCreateCommandProcessor());
784
785 if (doc->OnCreate(path, flags))
217b7140 786 return true;
0fb67cd1
VZ
787 else
788 {
f6bcfd97
BP
789 if (GetDocumentManager()->GetDocuments().Member(doc))
790 doc->DeleteAllViews();
217b7140 791 return false;
0fb67cd1 792 }
c801d85f
KB
793}
794
795wxView *wxDocTemplate::CreateView(wxDocument *doc, long flags)
796{
69936aea
VZ
797 wxView *view = DoCreateView();
798 if ( view == NULL )
0fb67cd1 799 return (wxView *) NULL;
69936aea 800
0fb67cd1
VZ
801 view->SetDocument(doc);
802 if (view->OnCreate(doc, flags))
803 {
804 return view;
805 }
806 else
807 {
808 delete view;
809 return (wxView *) NULL;
810 }
c801d85f
KB
811}
812
6de2f8b9
VZ
813// The default (very primitive) format detection: check is the extension is
814// that of the template
815bool wxDocTemplate::FileMatchesTemplate(const wxString& path)
816{
0f30d8e3
WS
817 wxStringTokenizer parser (GetFileFilter(), wxT(";"));
818 wxString anything = wxT ("*");
819 while (parser.HasMoreTokens())
820 {
821 wxString filter = parser.GetNextToken();
822 wxString filterExt = FindExtension (filter);
823 if ( filter.IsSameAs (anything) ||
824 filterExt.IsSameAs (anything) ||
825 filterExt.IsSameAs (FindExtension (path)) )
826 return true;
827 }
6de2f8b9
VZ
828 return GetDefaultExtension().IsSameAs(FindExtension(path));
829}
830
69936aea
VZ
831wxDocument *wxDocTemplate::DoCreateDocument()
832{
833 if (!m_docClassInfo)
834 return (wxDocument *) NULL;
835
836 return (wxDocument *)m_docClassInfo->CreateObject();
837}
838
839wxView *wxDocTemplate::DoCreateView()
840{
841 if (!m_viewClassInfo)
842 return (wxView *) NULL;
843
844 return (wxView *)m_viewClassInfo->CreateObject();
845}
846
0fb67cd1
VZ
847// ----------------------------------------------------------------------------
848// wxDocManager
849// ----------------------------------------------------------------------------
850
c801d85f
KB
851BEGIN_EVENT_TABLE(wxDocManager, wxEvtHandler)
852 EVT_MENU(wxID_OPEN, wxDocManager::OnFileOpen)
853 EVT_MENU(wxID_CLOSE, wxDocManager::OnFileClose)
33a20136 854 EVT_MENU(wxID_CLOSE_ALL, wxDocManager::OnFileCloseAll)
c801d85f
KB
855 EVT_MENU(wxID_REVERT, wxDocManager::OnFileRevert)
856 EVT_MENU(wxID_NEW, wxDocManager::OnFileNew)
857 EVT_MENU(wxID_SAVE, wxDocManager::OnFileSave)
858 EVT_MENU(wxID_SAVEAS, wxDocManager::OnFileSaveAs)
859 EVT_MENU(wxID_UNDO, wxDocManager::OnUndo)
860 EVT_MENU(wxID_REDO, wxDocManager::OnRedo)
f2506310
JS
861
862 EVT_UPDATE_UI(wxID_OPEN, wxDocManager::OnUpdateFileOpen)
863 EVT_UPDATE_UI(wxID_CLOSE, wxDocManager::OnUpdateFileClose)
33a20136 864 EVT_UPDATE_UI(wxID_CLOSE_ALL, wxDocManager::OnUpdateFileClose)
f2506310
JS
865 EVT_UPDATE_UI(wxID_REVERT, wxDocManager::OnUpdateFileRevert)
866 EVT_UPDATE_UI(wxID_NEW, wxDocManager::OnUpdateFileNew)
867 EVT_UPDATE_UI(wxID_SAVE, wxDocManager::OnUpdateFileSave)
868 EVT_UPDATE_UI(wxID_SAVEAS, wxDocManager::OnUpdateFileSaveAs)
869 EVT_UPDATE_UI(wxID_UNDO, wxDocManager::OnUpdateUndo)
870 EVT_UPDATE_UI(wxID_REDO, wxDocManager::OnUpdateRedo)
871
ce4169a4 872#if wxUSE_PRINTING_ARCHITECTURE
c801d85f 873 EVT_MENU(wxID_PRINT, wxDocManager::OnPrint)
c801d85f 874 EVT_MENU(wxID_PREVIEW, wxDocManager::OnPreview)
f2506310
JS
875
876 EVT_UPDATE_UI(wxID_PRINT, wxDocManager::OnUpdatePrint)
f2506310 877 EVT_UPDATE_UI(wxID_PREVIEW, wxDocManager::OnUpdatePreview)
ce4169a4 878#endif
c801d85f
KB
879END_EVENT_TABLE()
880
f2506310
JS
881wxDocManager* wxDocManager::sm_docManager = (wxDocManager*) NULL;
882
c801d85f
KB
883wxDocManager::wxDocManager(long flags, bool initialize)
884{
0fb67cd1
VZ
885 m_defaultDocumentNameCounter = 1;
886 m_flags = flags;
887 m_currentView = (wxView *) NULL;
888 m_maxDocsOpen = 10000;
889 m_fileHistory = (wxFileHistory *) NULL;
890 if (initialize)
891 Initialize();
f2506310 892 sm_docManager = this;
c801d85f
KB
893}
894
0fb67cd1 895wxDocManager::~wxDocManager()
c801d85f 896{
0fb67cd1
VZ
897 Clear();
898 if (m_fileHistory)
899 delete m_fileHistory;
f2506310 900 sm_docManager = (wxDocManager*) NULL;
c801d85f
KB
901}
902
b72b1920
JS
903// closes the specified document
904bool wxDocManager::CloseDocument(wxDocument* doc, bool force)
905{
906 if (doc->Close() || force)
907 {
908 // Implicitly deletes the document when
909 // the last view is deleted
910 doc->DeleteAllViews();
911
912 // Check we're really deleted
913 if (m_docs.Member(doc))
914 delete doc;
42771621 915
68379eaf 916 return true;
b72b1920 917 }
68379eaf 918 return false;
b72b1920
JS
919}
920
33a20136 921bool wxDocManager::CloseDocuments(bool force)
c801d85f 922{
222ed1d6 923 wxList::compatibility_iterator node = m_docs.GetFirst();
0fb67cd1
VZ
924 while (node)
925 {
b1d4dd7a 926 wxDocument *doc = (wxDocument *)node->GetData();
222ed1d6 927 wxList::compatibility_iterator next = node->GetNext();
68379eaf 928
b72b1920 929 if (!CloseDocument(doc, force))
68379eaf 930 return false;
0fb67cd1 931
0fb67cd1
VZ
932 // This assumes that documents are not connected in
933 // any way, i.e. deleting one document does NOT
934 // delete another.
935 node = next;
936 }
68379eaf 937 return true;
33a20136
VZ
938}
939
940bool wxDocManager::Clear(bool force)
941{
942 if (!CloseDocuments(force))
68379eaf 943 return false;
33a20136 944
8af94d3b
VZ
945 m_currentView = NULL;
946
222ed1d6 947 wxList::compatibility_iterator node = m_templates.GetFirst();
0fb67cd1
VZ
948 while (node)
949 {
b1d4dd7a 950 wxDocTemplate *templ = (wxDocTemplate*) node->GetData();
222ed1d6 951 wxList::compatibility_iterator next = node->GetNext();
c801d85f
KB
952 delete templ;
953 node = next;
0fb67cd1 954 }
68379eaf 955 return true;
c801d85f
KB
956}
957
0fb67cd1 958bool wxDocManager::Initialize()
c801d85f 959{
0fb67cd1 960 m_fileHistory = OnCreateFileHistory();
68379eaf 961 return true;
c801d85f
KB
962}
963
0fb67cd1 964wxFileHistory *wxDocManager::OnCreateFileHistory()
c801d85f 965{
0fb67cd1 966 return new wxFileHistory;
c801d85f
KB
967}
968
969void wxDocManager::OnFileClose(wxCommandEvent& WXUNUSED(event))
970{
0fb67cd1
VZ
971 wxDocument *doc = GetCurrentDocument();
972 if (!doc)
973 return;
974 if (doc->Close())
975 {
976 doc->DeleteAllViews();
977 if (m_docs.Member(doc))
978 delete doc;
979 }
c801d85f
KB
980}
981
33a20136
VZ
982void wxDocManager::OnFileCloseAll(wxCommandEvent& WXUNUSED(event))
983{
68379eaf 984 CloseDocuments(false);
33a20136
VZ
985}
986
c801d85f
KB
987void wxDocManager::OnFileNew(wxCommandEvent& WXUNUSED(event))
988{
b494c48b 989 CreateDocument( wxEmptyString, wxDOC_NEW );
c801d85f
KB
990}
991
992void wxDocManager::OnFileOpen(wxCommandEvent& WXUNUSED(event))
993{
b494c48b 994 if ( !CreateDocument( wxEmptyString, 0) )
5f170f33
VZ
995 {
996 OnOpenFileFailure();
997 }
c801d85f
KB
998}
999
1000void wxDocManager::OnFileRevert(wxCommandEvent& WXUNUSED(event))
1001{
0fb67cd1
VZ
1002 wxDocument *doc = GetCurrentDocument();
1003 if (!doc)
1004 return;
1005 doc->Revert();
c801d85f
KB
1006}
1007
1008void wxDocManager::OnFileSave(wxCommandEvent& WXUNUSED(event))
1009{
0fb67cd1
VZ
1010 wxDocument *doc = GetCurrentDocument();
1011 if (!doc)
1012 return;
1013 doc->Save();
c801d85f
KB
1014}
1015
1016void wxDocManager::OnFileSaveAs(wxCommandEvent& WXUNUSED(event))
1017{
0fb67cd1
VZ
1018 wxDocument *doc = GetCurrentDocument();
1019 if (!doc)
1020 return;
1021 doc->SaveAs();
c801d85f
KB
1022}
1023
1024void wxDocManager::OnPrint(wxCommandEvent& WXUNUSED(event))
1025{
88ac883a 1026#if wxUSE_PRINTING_ARCHITECTURE
0fb67cd1
VZ
1027 wxView *view = GetCurrentView();
1028 if (!view)
1029 return;
c801d85f 1030
0fb67cd1
VZ
1031 wxPrintout *printout = view->OnCreatePrintout();
1032 if (printout)
1033 {
1034 wxPrinter printer;
68379eaf 1035 printer.Print(view->GetFrame(), printout, true);
c801d85f 1036
0fb67cd1
VZ
1037 delete printout;
1038 }
88ac883a 1039#endif // wxUSE_PRINTING_ARCHITECTURE
c801d85f
KB
1040}
1041
c801d85f
KB
1042void wxDocManager::OnPreview(wxCommandEvent& WXUNUSED(event))
1043{
88ac883a 1044#if wxUSE_PRINTING_ARCHITECTURE
0fb67cd1
VZ
1045 wxView *view = GetCurrentView();
1046 if (!view)
1047 return;
c801d85f 1048
0fb67cd1
VZ
1049 wxPrintout *printout = view->OnCreatePrintout();
1050 if (printout)
1051 {
1052 // Pass two printout objects: for preview, and possible printing.
999836aa 1053 wxPrintPreviewBase *preview = new wxPrintPreview(printout, view->OnCreatePrintout());
3f8b4a3f
JS
1054 if ( !preview->Ok() )
1055 {
1056 delete preview;
1057 wxMessageBox( _("Sorry, print preview needs a printer to be installed.") );
1058 return;
1059 }
0fb67cd1
VZ
1060
1061 wxPreviewFrame *frame = new wxPreviewFrame(preview, (wxFrame *)wxTheApp->GetTopWindow(), _("Print Preview"),
1062 wxPoint(100, 100), wxSize(600, 650));
1063 frame->Centre(wxBOTH);
1064 frame->Initialize();
68379eaf 1065 frame->Show(true);
0fb67cd1 1066 }
88ac883a 1067#endif // wxUSE_PRINTING_ARCHITECTURE
c801d85f
KB
1068}
1069
34aa6140 1070void wxDocManager::OnUndo(wxCommandEvent& event)
c801d85f 1071{
0fb67cd1
VZ
1072 wxDocument *doc = GetCurrentDocument();
1073 if (!doc)
1074 return;
1075 if (doc->GetCommandProcessor())
1076 doc->GetCommandProcessor()->Undo();
34aa6140
JS
1077 else
1078 event.Skip();
c801d85f
KB
1079}
1080
34aa6140 1081void wxDocManager::OnRedo(wxCommandEvent& event)
c801d85f 1082{
0fb67cd1
VZ
1083 wxDocument *doc = GetCurrentDocument();
1084 if (!doc)
1085 return;
1086 if (doc->GetCommandProcessor())
1087 doc->GetCommandProcessor()->Redo();
34aa6140
JS
1088 else
1089 event.Skip();
c801d85f
KB
1090}
1091
f2506310
JS
1092// Handlers for UI update commands
1093
1094void wxDocManager::OnUpdateFileOpen(wxUpdateUIEvent& event)
1095{
68379eaf 1096 event.Enable( true );
f2506310
JS
1097}
1098
1099void wxDocManager::OnUpdateFileClose(wxUpdateUIEvent& event)
1100{
1101 wxDocument *doc = GetCurrentDocument();
1102 event.Enable( (doc != (wxDocument*) NULL) );
1103}
1104
1105void wxDocManager::OnUpdateFileRevert(wxUpdateUIEvent& event)
1106{
1107 wxDocument *doc = GetCurrentDocument();
1108 event.Enable( (doc != (wxDocument*) NULL) );
1109}
1110
1111void wxDocManager::OnUpdateFileNew(wxUpdateUIEvent& event)
1112{
68379eaf 1113 event.Enable( true );
f2506310
JS
1114}
1115
1116void wxDocManager::OnUpdateFileSave(wxUpdateUIEvent& event)
1117{
1118 wxDocument *doc = GetCurrentDocument();
9cb0033c 1119 event.Enable( doc && doc->IsModified() );
f2506310
JS
1120}
1121
1122void wxDocManager::OnUpdateFileSaveAs(wxUpdateUIEvent& event)
1123{
1124 wxDocument *doc = GetCurrentDocument();
1125 event.Enable( (doc != (wxDocument*) NULL) );
1126}
1127
1128void wxDocManager::OnUpdateUndo(wxUpdateUIEvent& event)
1129{
1130 wxDocument *doc = GetCurrentDocument();
34aa6140 1131 if (!doc)
68379eaf 1132 event.Enable(false);
34aa6140
JS
1133 else if (!doc->GetCommandProcessor())
1134 event.Skip();
1135 else
1136 {
1137 event.Enable( doc->GetCommandProcessor()->CanUndo() );
2599ee02 1138 doc->GetCommandProcessor()->SetMenuStrings();
34aa6140 1139 }
f2506310
JS
1140}
1141
1142void wxDocManager::OnUpdateRedo(wxUpdateUIEvent& event)
1143{
1144 wxDocument *doc = GetCurrentDocument();
34aa6140 1145 if (!doc)
68379eaf 1146 event.Enable(false);
34aa6140
JS
1147 else if (!doc->GetCommandProcessor())
1148 event.Skip();
1149 else
1150 {
1151 event.Enable( doc->GetCommandProcessor()->CanRedo() );
2599ee02 1152 doc->GetCommandProcessor()->SetMenuStrings();
34aa6140 1153 }
f2506310
JS
1154}
1155
1156void wxDocManager::OnUpdatePrint(wxUpdateUIEvent& event)
1157{
1158 wxDocument *doc = GetCurrentDocument();
1159 event.Enable( (doc != (wxDocument*) NULL) );
1160}
1161
f2506310
JS
1162void wxDocManager::OnUpdatePreview(wxUpdateUIEvent& event)
1163{
1164 wxDocument *doc = GetCurrentDocument();
1165 event.Enable( (doc != (wxDocument*) NULL) );
1166}
1167
6de2f8b9 1168wxView *wxDocManager::GetCurrentView() const
637f467a
JS
1169{
1170 if (m_currentView)
1171 return m_currentView;
b1d4dd7a 1172 if (m_docs.GetCount() == 1)
637f467a 1173 {
b1d4dd7a 1174 wxDocument* doc = (wxDocument*) m_docs.GetFirst()->GetData();
637f467a
JS
1175 return doc->GetFirstView();
1176 }
c67daf87 1177 return (wxView *) NULL;
637f467a
JS
1178}
1179
1180// Extend event processing to search the view's event table
1181bool wxDocManager::ProcessEvent(wxEvent& event)
1182{
1183 wxView* view = GetCurrentView();
1184 if (view)
1185 {
1186 if (view->ProcessEvent(event))
68379eaf 1187 return true;
637f467a
JS
1188 }
1189 return wxEvtHandler::ProcessEvent(event);
1190}
1191
c801d85f
KB
1192wxDocument *wxDocManager::CreateDocument(const wxString& path, long flags)
1193{
b1d4dd7a
RL
1194 wxDocTemplate **templates = new wxDocTemplate *[m_templates.GetCount()];
1195 int n = 0;
1196
1197 for (size_t i = 0; i < m_templates.GetCount(); i++)
c801d85f 1198 {
b1d4dd7a 1199 wxDocTemplate *temp = (wxDocTemplate *)(m_templates.Item(i)->GetData());
0fb67cd1
VZ
1200 if (temp->IsVisible())
1201 {
1202 templates[n] = temp;
1203 n ++;
1204 }
c801d85f 1205 }
0fb67cd1 1206 if (n == 0)
c801d85f 1207 {
0fb67cd1
VZ
1208 delete[] templates;
1209 return (wxDocument *) NULL;
c801d85f 1210 }
42771621 1211
b72b1920 1212 wxDocument* docToClose = NULL;
0fb67cd1
VZ
1213
1214 // If we've reached the max number of docs, close the
1215 // first one.
b1d4dd7a 1216 if ( (int)GetDocuments().GetCount() >= m_maxDocsOpen )
c801d85f 1217 {
b1d4dd7a 1218 wxDocument *doc = (wxDocument *)GetDocuments().GetFirst()->GetData();
b72b1920 1219 docToClose = doc;
c801d85f
KB
1220 }
1221
0fb67cd1
VZ
1222 // New document: user chooses a template, unless there's only one.
1223 if (flags & wxDOC_NEW)
c801d85f 1224 {
0fb67cd1
VZ
1225 if (n == 1)
1226 {
b72b1920
JS
1227 if (docToClose)
1228 {
68379eaf 1229 if (!CloseDocument(docToClose, false))
b72b1920 1230 {
7a311019 1231 delete[] templates;
b72b1920
JS
1232 return NULL;
1233 }
1234 }
42771621 1235
0fb67cd1
VZ
1236 wxDocTemplate *temp = templates[0];
1237 delete[] templates;
1238 wxDocument *newDoc = temp->CreateDocument(path, flags);
42771621 1239
0fb67cd1
VZ
1240 if (newDoc)
1241 {
1242 newDoc->SetDocumentName(temp->GetDocumentName());
1243 newDoc->SetDocumentTemplate(temp);
f5729a6b
JS
1244 if (!newDoc->OnNewDocument() )
1245 {
1246 // Document is implicitly deleted by DeleteAllViews
1247 newDoc->DeleteAllViews();
1248 return NULL;
1249 }
0fb67cd1
VZ
1250 }
1251 return newDoc;
1252 }
1253
1254 wxDocTemplate *temp = SelectDocumentType(templates, n);
1255 delete[] templates;
1256 if (temp)
1257 {
b72b1920
JS
1258 if (docToClose)
1259 {
68379eaf 1260 if (!CloseDocument(docToClose, false))
b72b1920
JS
1261 {
1262 return NULL;
1263 }
1264 }
42771621 1265
0fb67cd1 1266 wxDocument *newDoc = temp->CreateDocument(path, flags);
b72b1920 1267
0fb67cd1
VZ
1268 if (newDoc)
1269 {
1270 newDoc->SetDocumentName(temp->GetDocumentName());
1271 newDoc->SetDocumentTemplate(temp);
f5729a6b
JS
1272 if (!newDoc->OnNewDocument() )
1273 {
1274 // Document is implicitly deleted by DeleteAllViews
1275 newDoc->DeleteAllViews();
1276 return NULL;
1277 }
0fb67cd1
VZ
1278 }
1279 return newDoc;
1280 }
1281 else
1282 return (wxDocument *) NULL;
c801d85f 1283 }
c801d85f 1284
0fb67cd1 1285 // Existing document
999836aa 1286 wxDocTemplate *temp;
c801d85f 1287
b494c48b 1288 wxString path2 = path;
c801d85f 1289
0fb67cd1 1290 if (flags & wxDOC_SILENT)
9d4a05be 1291 {
0fb67cd1 1292 temp = FindTemplateForPath(path2);
9d4a05be
JS
1293 if (!temp)
1294 {
42771621 1295 // Since we do not add files with non-default extensions to the FileHistory this
9d4a05be
JS
1296 // can only happen if the application changes the allowed templates in runtime.
1297 (void)wxMessageBox(_("Sorry, the format for this file is unknown."),
42771621 1298 _("Open File"),
9d4a05be
JS
1299 wxOK | wxICON_EXCLAMATION, wxFindSuitableParent());
1300 }
1301 }
0fb67cd1
VZ
1302 else
1303 temp = SelectDocumentPath(templates, n, path2, flags);
c801d85f 1304
0fb67cd1 1305 delete[] templates;
c801d85f 1306
0fb67cd1 1307 if (temp)
c801d85f 1308 {
b72b1920
JS
1309 if (docToClose)
1310 {
68379eaf 1311 if (!CloseDocument(docToClose, false))
b72b1920
JS
1312 {
1313 return NULL;
1314 }
1315 }
42771621 1316
6489e7ee
JS
1317 //see if this file is already open
1318 for (size_t i = 0; i < GetDocuments().GetCount(); ++i)
1319 {
1320 wxDocument* currentDoc = (wxDocument*)(GetDocuments().Item(i)->GetData());
1321#ifdef __WXMSW__
1322 //file paths are case-insensitive on Windows
1323 if (path2.CmpNoCase(currentDoc->GetFilename()) == 0)
1324#else
1325 if (path2.Cmp(currentDoc->GetFilename()) == 0)
1326#endif
1327 {
1328 //file already open. Just activate it and return
1329 if (currentDoc->GetFirstView())
1330 {
1331 ActivateView(currentDoc->GetFirstView(), true);
1332 if (currentDoc->GetDocumentWindow())
1333 currentDoc->GetDocumentWindow()->SetFocus();
1334 return currentDoc;
1335 }
1336 }
1337 }
1338
0fb67cd1
VZ
1339 wxDocument *newDoc = temp->CreateDocument(path2, flags);
1340 if (newDoc)
1341 {
1342 newDoc->SetDocumentName(temp->GetDocumentName());
1343 newDoc->SetDocumentTemplate(temp);
1344 if (!newDoc->OnOpenDocument(path2))
1345 {
f6bcfd97
BP
1346 newDoc->DeleteAllViews();
1347 // delete newDoc; // Implicitly deleted by DeleteAllViews
0fb67cd1
VZ
1348 return (wxDocument *) NULL;
1349 }
9d4a05be
JS
1350 // A file that doesn't use the default extension of its document
1351 // template cannot be opened via the FileHistory, so we do not
1352 // add it.
1353 if (temp->FileMatchesTemplate(path2))
1354 AddFileToHistory(path2);
0fb67cd1
VZ
1355 }
1356 return newDoc;
c801d85f 1357 }
3ca6a5f0
BP
1358
1359 return (wxDocument *) NULL;
c801d85f
KB
1360}
1361
1362wxView *wxDocManager::CreateView(wxDocument *doc, long flags)
1363{
b1d4dd7a
RL
1364 wxDocTemplate **templates = new wxDocTemplate *[m_templates.GetCount()];
1365 int n =0;
1366
1367 for (size_t i = 0; i < m_templates.GetCount(); i++)
0fb67cd1 1368 {
b1d4dd7a 1369 wxDocTemplate *temp = (wxDocTemplate *)(m_templates.Item(i)->GetData());
0fb67cd1
VZ
1370 if (temp->IsVisible())
1371 {
1372 if (temp->GetDocumentName() == doc->GetDocumentName())
1373 {
1374 templates[n] = temp;
1375 n ++;
1376 }
1377 }
1378 }
1379 if (n == 0)
1380 {
1381 delete[] templates;
1382 return (wxView *) NULL;
1383 }
1384 if (n == 1)
1385 {
1386 wxDocTemplate *temp = templates[0];
1387 delete[] templates;
1388 wxView *view = temp->CreateView(doc, flags);
1389 if (view)
1390 view->SetViewName(temp->GetViewName());
1391 return view;
1392 }
1393
1394 wxDocTemplate *temp = SelectViewType(templates, n);
c801d85f 1395 delete[] templates;
0fb67cd1
VZ
1396 if (temp)
1397 {
1398 wxView *view = temp->CreateView(doc, flags);
1399 if (view)
1400 view->SetViewName(temp->GetViewName());
1401 return view;
1402 }
1403 else
1404 return (wxView *) NULL;
c801d85f
KB
1405}
1406
1407// Not yet implemented
1408void wxDocManager::DeleteTemplate(wxDocTemplate *WXUNUSED(temp), long WXUNUSED(flags))
1409{
1410}
1411
1412// Not yet implemented
1413bool wxDocManager::FlushDoc(wxDocument *WXUNUSED(doc))
1414{
68379eaf 1415 return false;
c801d85f
KB
1416}
1417
6de2f8b9 1418wxDocument *wxDocManager::GetCurrentDocument() const
c801d85f 1419{
9cb0033c
VZ
1420 wxView *view = GetCurrentView();
1421 if (view)
1422 return view->GetDocument();
0fb67cd1
VZ
1423 else
1424 return (wxDocument *) NULL;
c801d85f
KB
1425}
1426
724b119a
VZ
1427// Make a default name for a new document
1428#if WXWIN_COMPATIBILITY_2_8
1429bool wxDocManager::MakeDefaultName(wxString& WXUNUSED(name))
1430{
1431 // we consider that this function can only be overridden by the user code,
1432 // not called by it as it only makes sense to call it internally, so we
1433 // don't bother to return anything from here
1434 return false;
1435}
1436#endif // WXWIN_COMPATIBILITY_2_8
1437
1438wxString wxDocManager::MakeNewDocumentName()
c801d85f 1439{
724b119a 1440 wxString name;
53c6e7cc 1441
724b119a
VZ
1442#if WXWIN_COMPATIBILITY_2_8
1443 if ( !MakeDefaultName(name) )
1444#endif // WXWIN_COMPATIBILITY_2_8
1445 {
1446 name.Printf(_("unnamed%d"), m_defaultDocumentNameCounter);
1447 m_defaultDocumentNameCounter++;
1448 }
1449
1450 return name;
c801d85f
KB
1451}
1452
f2506310
JS
1453// Make a frame title (override this to do something different)
1454// If docName is empty, a document is not currently active.
1455wxString wxDocManager::MakeFrameTitle(wxDocument* doc)
1456{
9cf3d218 1457 wxString appName = wxTheApp->GetAppDisplayName();
f2506310
JS
1458 wxString title;
1459 if (!doc)
1460 title = appName;
1461 else
1462 {
724b119a 1463 wxString docName = doc->GetUserReadableName();
f2506310
JS
1464 title = docName + wxString(_(" - ")) + appName;
1465 }
1466 return title;
1467}
1468
1469
c801d85f
KB
1470// Not yet implemented
1471wxDocTemplate *wxDocManager::MatchTemplate(const wxString& WXUNUSED(path))
1472{
0fb67cd1 1473 return (wxDocTemplate *) NULL;
c801d85f
KB
1474}
1475
1476// File history management
1477void wxDocManager::AddFileToHistory(const wxString& file)
1478{
0fb67cd1
VZ
1479 if (m_fileHistory)
1480 m_fileHistory->AddFileToHistory(file);
c801d85f
KB
1481}
1482
e49c85af 1483void wxDocManager::RemoveFileFromHistory(size_t i)
0c5d3e1c
VZ
1484{
1485 if (m_fileHistory)
1486 m_fileHistory->RemoveFileFromHistory(i);
1487}
1488
e49c85af 1489wxString wxDocManager::GetHistoryFile(size_t i) const
c801d85f 1490{
0fb67cd1
VZ
1491 wxString histFile;
1492
1493 if (m_fileHistory)
1494 histFile = m_fileHistory->GetHistoryFile(i);
1495
1496 return histFile;
c801d85f
KB
1497}
1498
1499void wxDocManager::FileHistoryUseMenu(wxMenu *menu)
1500{
0fb67cd1
VZ
1501 if (m_fileHistory)
1502 m_fileHistory->UseMenu(menu);
c801d85f
KB
1503}
1504
7f555861 1505void wxDocManager::FileHistoryRemoveMenu(wxMenu *menu)
c801d85f 1506{
0fb67cd1
VZ
1507 if (m_fileHistory)
1508 m_fileHistory->RemoveMenu(menu);
c801d85f
KB
1509}
1510
702ca7c0 1511#if wxUSE_CONFIG
7f555861 1512void wxDocManager::FileHistoryLoad(wxConfigBase& config)
c801d85f 1513{
0fb67cd1
VZ
1514 if (m_fileHistory)
1515 m_fileHistory->Load(config);
7f555861
JS
1516}
1517
1518void wxDocManager::FileHistorySave(wxConfigBase& config)
1519{
0fb67cd1
VZ
1520 if (m_fileHistory)
1521 m_fileHistory->Save(config);
7f555861 1522}
ac57418f 1523#endif
7f555861
JS
1524
1525void wxDocManager::FileHistoryAddFilesToMenu(wxMenu* menu)
1526{
0fb67cd1
VZ
1527 if (m_fileHistory)
1528 m_fileHistory->AddFilesToMenu(menu);
7f555861
JS
1529}
1530
1531void wxDocManager::FileHistoryAddFilesToMenu()
1532{
0fb67cd1
VZ
1533 if (m_fileHistory)
1534 m_fileHistory->AddFilesToMenu();
c801d85f
KB
1535}
1536
7af6b69e 1537size_t wxDocManager::GetHistoryFilesCount() const
c801d85f 1538{
7af6b69e 1539 return m_fileHistory ? m_fileHistory->GetCount() : 0;
c801d85f
KB
1540}
1541
1542
6de2f8b9
VZ
1543// Find out the document template via matching in the document file format
1544// against that of the template
c801d85f
KB
1545wxDocTemplate *wxDocManager::FindTemplateForPath(const wxString& path)
1546{
0fb67cd1 1547 wxDocTemplate *theTemplate = (wxDocTemplate *) NULL;
c801d85f 1548
0fb67cd1 1549 // Find the template which this extension corresponds to
b1d4dd7a 1550 for (size_t i = 0; i < m_templates.GetCount(); i++)
c801d85f 1551 {
b1d4dd7a 1552 wxDocTemplate *temp = (wxDocTemplate *)m_templates.Item(i)->GetData();
6de2f8b9 1553 if ( temp->FileMatchesTemplate(path) )
0fb67cd1
VZ
1554 {
1555 theTemplate = temp;
1556 break;
1557 }
c801d85f 1558 }
0fb67cd1 1559 return theTemplate;
c801d85f
KB
1560}
1561
f6bcfd97
BP
1562// Try to get a more suitable parent frame than the top window,
1563// for selection dialogs. Otherwise you may get an unexpected
1564// window being activated when a dialog is shown.
1565static wxWindow* wxFindSuitableParent()
1566{
1567 wxWindow* parent = wxTheApp->GetTopWindow();
1568
1569 wxWindow* focusWindow = wxWindow::FindFocus();
1570 if (focusWindow)
1571 {
1572 while (focusWindow &&
1573 !focusWindow->IsKindOf(CLASSINFO(wxDialog)) &&
1574 !focusWindow->IsKindOf(CLASSINFO(wxFrame)))
1575
1576 focusWindow = focusWindow->GetParent();
1577
1578 if (focusWindow)
1579 parent = focusWindow;
1580 }
1581 return parent;
1582}
1583
c801d85f 1584// Prompts user to open a file, using file specs in templates.
9d4a05be
JS
1585// Must extend the file selector dialog or implement own; OR
1586// match the extension to the template extension.
df875e59 1587
c801d85f 1588wxDocTemplate *wxDocManager::SelectDocumentPath(wxDocTemplate **templates,
ce68ad25 1589#if defined(__WXMSW__) || defined(__WXGTK__) || defined(__WXMAC__)
0fb67cd1 1590 int noTemplates,
f6147cfc
VZ
1591#else
1592 int WXUNUSED(noTemplates),
1593#endif
0fb67cd1
VZ
1594 wxString& path,
1595 long WXUNUSED(flags),
1596 bool WXUNUSED(save))
c801d85f 1597{
bf9e3e73 1598 // We can only have multiple filters in Windows and GTK
ce68ad25 1599#if defined(__WXMSW__) || defined(__WXGTK__) || defined(__WXMAC__)
ba681060
VZ
1600 wxString descrBuf;
1601
1602 int i;
1603 for (i = 0; i < noTemplates; i++)
c801d85f 1604 {
ba681060
VZ
1605 if (templates[i]->IsVisible())
1606 {
1607 // add a '|' to separate this filter from the previous one
b494c48b 1608 if ( !descrBuf.empty() )
223d09f6 1609 descrBuf << wxT('|');
ba681060
VZ
1610
1611 descrBuf << templates[i]->GetDescription()
223d09f6 1612 << wxT(" (") << templates[i]->GetFileFilter() << wxT(") |")
0fb67cd1 1613 << templates[i]->GetFileFilter();
ba681060 1614 }
c801d85f 1615 }
a4294b78 1616#else
223d09f6 1617 wxString descrBuf = wxT("*.*");
a4294b78 1618#endif
c801d85f 1619
3ca6a5f0 1620 int FilterIndex = -1;
f6bcfd97
BP
1621
1622 wxWindow* parent = wxFindSuitableParent();
1623
f6fe9f9c 1624 wxString pathTmp = wxFileSelectorEx(_("Open File"),
ac0ac824 1625 m_lastDirectory,
b494c48b 1626 wxEmptyString,
6de2f8b9 1627 &FilterIndex,
caf0debf
VZ
1628 descrBuf,
1629 0,
f6bcfd97 1630 parent);
ba681060 1631
3ca6a5f0 1632 wxDocTemplate *theTemplate = (wxDocTemplate *)NULL;
b494c48b 1633 if (!pathTmp.empty())
0fb67cd1 1634 {
f6bcfd97
BP
1635 if (!wxFileExists(pathTmp))
1636 {
1637 wxString msgTitle;
9cf3d218
VZ
1638 if (!wxTheApp->GetAppDisplayName().empty())
1639 msgTitle = wxTheApp->GetAppDisplayName();
f6bcfd97
BP
1640 else
1641 msgTitle = wxString(_("File error"));
02718e6a 1642
f6bcfd97
BP
1643 (void)wxMessageBox(_("Sorry, could not open this file."), msgTitle, wxOK | wxICON_EXCLAMATION,
1644 parent);
1645
b494c48b 1646 path = wxEmptyString;
f6bcfd97
BP
1647 return (wxDocTemplate *) NULL;
1648 }
ac0ac824
JS
1649 m_lastDirectory = wxPathOnly(pathTmp);
1650
0fb67cd1 1651 path = pathTmp;
0fb67cd1 1652
3ca6a5f0
BP
1653 // first choose the template using the extension, if this fails (i.e.
1654 // wxFileSelectorEx() didn't fill it), then use the path
1655 if ( FilterIndex != -1 )
6de2f8b9 1656 theTemplate = templates[FilterIndex];
3ca6a5f0
BP
1657 if ( !theTemplate )
1658 theTemplate = FindTemplateForPath(path);
9d4a05be
JS
1659 if ( !theTemplate )
1660 {
42771621 1661 // Since we do not add files with non-default extensions to the FileHistory this
9d4a05be
JS
1662 // can only happen if the application changes the allowed templates in runtime.
1663 (void)wxMessageBox(_("Sorry, the format for this file is unknown."),
42771621 1664 _("Open File"),
9d4a05be
JS
1665 wxOK | wxICON_EXCLAMATION, wxFindSuitableParent());
1666 }
0fb67cd1
VZ
1667 }
1668 else
1669 {
b494c48b 1670 path = wxEmptyString;
0fb67cd1 1671 }
3ca6a5f0
BP
1672
1673 return theTemplate;
c801d85f
KB
1674}
1675
1676wxDocTemplate *wxDocManager::SelectDocumentType(wxDocTemplate **templates,
52b9ca21 1677 int noTemplates, bool sort)
0fb67cd1 1678{
222ed1d6 1679 wxArrayString strings;
17260efd 1680 wxDocTemplate **data = new wxDocTemplate *[noTemplates];
0fb67cd1
VZ
1681 int i;
1682 int n = 0;
42771621
VZ
1683
1684 for (i = 0; i < noTemplates; i++)
1685 {
1686 if (templates[i]->IsVisible())
1687 {
1688 int j;
68379eaf 1689 bool want = true;
42771621
VZ
1690 for (j = 0; j < n; j++)
1691 {
bb28b477 1692 //filter out NOT unique documents + view combinations
42771621 1693 if ( templates[i]->m_docTypeName == data[j]->m_docTypeName &&
bb28b477
JS
1694 templates[i]->m_viewTypeName == data[j]->m_viewTypeName
1695 )
68379eaf 1696 want = false;
42771621 1697 }
bb28b477
JS
1698
1699 if ( want )
42771621
VZ
1700 {
1701 strings.Add(templates[i]->m_description);
1702
1703 data[n] = templates[i];
1704 n ++;
1705 }
1706 }
1707 } // for
1708
1709 if (sort)
1710 {
a2a03d78 1711 strings.Sort(); // ascending sort
42771621
VZ
1712 // Yes, this will be slow, but template lists
1713 // are typically short.
1714 int j;
1715 n = strings.Count();
1716 for (i = 0; i < n; i++)
1717 {
1718 for (j = 0; j < noTemplates; j++)
1719 {
1720 if (strings[i] == templates[j]->m_description)
1721 data[i] = templates[j];
1722 }
1723 }
1724 }
17260efd
VZ
1725
1726 wxDocTemplate *theTemplate;
1727
1728 switch ( n )
0fb67cd1 1729 {
17260efd
VZ
1730 case 0:
1731 // no visible templates, hence nothing to choose from
1732 theTemplate = NULL;
1733 break;
0fb67cd1 1734
17260efd
VZ
1735 case 1:
1736 // don't propose the user to choose if he heas no choice
1737 theTemplate = data[0];
1738 break;
1739
1740 default:
1741 // propose the user to choose one of several
1742 theTemplate = (wxDocTemplate *)wxGetSingleChoiceData
1743 (
1744 _("Select a document template"),
1745 _("Templates"),
1746 strings,
1747 (void **)data,
1748 wxFindSuitableParent()
1749 );
1750 }
f6bcfd97 1751
c801d85f 1752 delete[] data;
17260efd 1753
0fb67cd1 1754 return theTemplate;
c801d85f
KB
1755}
1756
1757wxDocTemplate *wxDocManager::SelectViewType(wxDocTemplate **templates,
52b9ca21 1758 int noTemplates, bool sort)
c801d85f 1759{
222ed1d6 1760 wxArrayString strings;
17260efd 1761 wxDocTemplate **data = new wxDocTemplate *[noTemplates];
0fb67cd1
VZ
1762 int i;
1763 int n = 0;
42771621 1764
0fb67cd1 1765 for (i = 0; i < noTemplates; i++)
c801d85f 1766 {
17260efd
VZ
1767 wxDocTemplate *templ = templates[i];
1768 if ( templ->IsVisible() && !templ->GetViewName().empty() )
0fb67cd1 1769 {
42771621 1770 int j;
68379eaf 1771 bool want = true;
42771621
VZ
1772 for (j = 0; j < n; j++)
1773 {
bb28b477 1774 //filter out NOT unique views
42771621 1775 if ( templates[i]->m_viewTypeName == data[j]->m_viewTypeName )
68379eaf 1776 want = false;
42771621 1777 }
bb28b477
JS
1778
1779 if ( want )
1780 {
42771621
VZ
1781 strings.Add(templ->m_viewTypeName);
1782 data[n] = templ;
1783 n ++;
1784 }
0fb67cd1 1785 }
c801d85f 1786 }
f6bcfd97 1787
42771621
VZ
1788 if (sort)
1789 {
a2a03d78 1790 strings.Sort(); // ascending sort
42771621
VZ
1791 // Yes, this will be slow, but template lists
1792 // are typically short.
1793 int j;
1794 n = strings.Count();
1795 for (i = 0; i < n; i++)
1796 {
1797 for (j = 0; j < noTemplates; j++)
1798 {
1799 if (strings[i] == templates[j]->m_viewTypeName)
1800 data[i] = templates[j];
1801 }
1802 }
1803 }
8658ef93 1804
17260efd
VZ
1805 wxDocTemplate *theTemplate;
1806
1807 // the same logic as above
1808 switch ( n )
1809 {
1810 case 0:
1811 theTemplate = (wxDocTemplate *)NULL;
1812 break;
1813
1814 case 1:
1815 theTemplate = data[0];
1816 break;
1817
1818 default:
1819 theTemplate = (wxDocTemplate *)wxGetSingleChoiceData
1820 (
1821 _("Select a document view"),
1822 _("Views"),
1823 strings,
1824 (void **)data,
1825 wxFindSuitableParent()
1826 );
1827
1828 }
1829
0fb67cd1
VZ
1830 delete[] data;
1831 return theTemplate;
c801d85f
KB
1832}
1833
1834void wxDocManager::AssociateTemplate(wxDocTemplate *temp)
1835{
0fb67cd1
VZ
1836 if (!m_templates.Member(temp))
1837 m_templates.Append(temp);
c801d85f
KB
1838}
1839
1840void wxDocManager::DisassociateTemplate(wxDocTemplate *temp)
1841{
0fb67cd1 1842 m_templates.DeleteObject(temp);
c801d85f
KB
1843}
1844
1845// Add and remove a document from the manager's list
1846void wxDocManager::AddDocument(wxDocument *doc)
1847{
0fb67cd1
VZ
1848 if (!m_docs.Member(doc))
1849 m_docs.Append(doc);
c801d85f
KB
1850}
1851
1852void wxDocManager::RemoveDocument(wxDocument *doc)
1853{
0fb67cd1 1854 m_docs.DeleteObject(doc);
c801d85f
KB
1855}
1856
1857// Views or windows should inform the document manager
1858// when a view is going in or out of focus
18759f78
VZ
1859void wxDocManager::ActivateView(wxView *view, bool activate)
1860{
1861 if ( activate )
0fb67cd1 1862 {
18759f78
VZ
1863 m_currentView = view;
1864 }
1865 else // deactivate
1866 {
1867 if ( m_currentView == view )
1868 {
1869 // don't keep stale pointer
0fb67cd1 1870 m_currentView = (wxView *) NULL;
18759f78 1871 }
0fb67cd1 1872 }
c801d85f
KB
1873}
1874
0fb67cd1
VZ
1875// ----------------------------------------------------------------------------
1876// Default document child frame
1877// ----------------------------------------------------------------------------
c801d85f
KB
1878
1879BEGIN_EVENT_TABLE(wxDocChildFrame, wxFrame)
1880 EVT_ACTIVATE(wxDocChildFrame::OnActivate)
387a3b02 1881 EVT_CLOSE(wxDocChildFrame::OnCloseWindow)
c801d85f
KB
1882END_EVENT_TABLE()
1883
0fb67cd1
VZ
1884wxDocChildFrame::wxDocChildFrame(wxDocument *doc,
1885 wxView *view,
1886 wxFrame *frame,
1887 wxWindowID id,
1888 const wxString& title,
1889 const wxPoint& pos,
1890 const wxSize& size,
1891 long style,
1892 const wxString& name)
1893 : wxFrame(frame, id, title, pos, size, style, name)
1894{
1895 m_childDocument = doc;
1896 m_childView = view;
1897 if (view)
1898 view->SetFrame(this);
c801d85f
KB
1899}
1900
c801d85f
KB
1901// Extend event processing to search the view's event table
1902bool wxDocChildFrame::ProcessEvent(wxEvent& event)
1903{
1904 if (m_childView)
68379eaf 1905 m_childView->Activate(true);
c801d85f 1906
0fb67cd1 1907 if ( !m_childView || ! m_childView->ProcessEvent(event) )
c801d85f
KB
1908 {
1909 // Only hand up to the parent if it's a menu command
1910 if (!event.IsKindOf(CLASSINFO(wxCommandEvent)) || !GetParent() || !GetParent()->ProcessEvent(event))
0fb67cd1 1911 return wxEvtHandler::ProcessEvent(event);
c801d85f 1912 else
68379eaf 1913 return true;
c801d85f 1914 }
0fb67cd1 1915 else
68379eaf 1916 return true;
c801d85f
KB
1917}
1918
c801d85f
KB
1919void wxDocChildFrame::OnActivate(wxActivateEvent& event)
1920{
0fb67cd1 1921 wxFrame::OnActivate(event);
c801d85f 1922
0fb67cd1
VZ
1923 if (m_childView)
1924 m_childView->Activate(event.GetActive());
c801d85f
KB
1925}
1926
387a3b02 1927void wxDocChildFrame::OnCloseWindow(wxCloseEvent& event)
c801d85f 1928{
0fb67cd1 1929 if (m_childView)
c801d85f 1930 {
999836aa 1931 bool ans = event.CanVeto()
68379eaf
WS
1932 ? m_childView->Close(false) // false means don't delete associated window
1933 : true; // Must delete.
387a3b02 1934
0fb67cd1
VZ
1935 if (ans)
1936 {
68379eaf 1937 m_childView->Activate(false);
0fb67cd1
VZ
1938 delete m_childView;
1939 m_childView = (wxView *) NULL;
1940 m_childDocument = (wxDocument *) NULL;
1941
1942 this->Destroy();
1943 }
1944 else
1945 event.Veto();
c801d85f 1946 }
387a3b02 1947 else
0fb67cd1 1948 event.Veto();
c801d85f
KB
1949}
1950
0fb67cd1
VZ
1951// ----------------------------------------------------------------------------
1952// Default parent frame
1953// ----------------------------------------------------------------------------
c801d85f
KB
1954
1955BEGIN_EVENT_TABLE(wxDocParentFrame, wxFrame)
1956 EVT_MENU(wxID_EXIT, wxDocParentFrame::OnExit)
f7bd2698 1957 EVT_MENU_RANGE(wxID_FILE1, wxID_FILE9, wxDocParentFrame::OnMRUFile)
387a3b02 1958 EVT_CLOSE(wxDocParentFrame::OnCloseWindow)
c801d85f
KB
1959END_EVENT_TABLE()
1960
0c246b3c
PC
1961wxDocParentFrame::wxDocParentFrame()
1962{
1963 m_docManager = NULL;
1964}
1965
0fb67cd1
VZ
1966wxDocParentFrame::wxDocParentFrame(wxDocManager *manager,
1967 wxFrame *frame,
1968 wxWindowID id,
1969 const wxString& title,
1970 const wxPoint& pos,
1971 const wxSize& size,
1972 long style,
1973 const wxString& name)
1974 : wxFrame(frame, id, title, pos, size, style, name)
c801d85f 1975{
0fb67cd1 1976 m_docManager = manager;
c801d85f
KB
1977}
1978
0c246b3c
PC
1979bool wxDocParentFrame::Create(wxDocManager *manager,
1980 wxFrame *frame,
1981 wxWindowID id,
1982 const wxString& title,
1983 const wxPoint& pos,
1984 const wxSize& size,
1985 long style,
1986 const wxString& name)
1987{
1988 m_docManager = manager;
1989 return base_type::Create(frame, id, title, pos, size, style, name);
1990}
1991
c801d85f
KB
1992void wxDocParentFrame::OnExit(wxCommandEvent& WXUNUSED(event))
1993{
1994 Close();
1995}
1996
1997void wxDocParentFrame::OnMRUFile(wxCommandEvent& event)
1998{
1fc88785 1999 int n = event.GetId() - wxID_FILE1; // the index in MRU list
0c5d3e1c 2000 wxString filename(m_docManager->GetHistoryFile(n));
b494c48b 2001 if ( !filename.empty() )
0c5d3e1c
VZ
2002 {
2003 // verify that the file exists before doing anything else
2004 if ( wxFile::Exists(filename) )
2005 {
2006 // try to open it
9d4a05be
JS
2007 if (!m_docManager->CreateDocument(filename, wxDOC_SILENT))
2008 {
2009 // remove the file from the MRU list. The user should already be notified.
2010 m_docManager->RemoveFileFromHistory(n);
42771621 2011
9d4a05be
JS
2012 wxLogError(_("The file '%s' couldn't be opened.\nIt has been removed from the most recently used files list."),
2013 filename.c_str());
2014 }
0c5d3e1c
VZ
2015 }
2016 else
2017 {
2018 // remove the bogus filename from the MRU list and notify the user
2019 // about it
2020 m_docManager->RemoveFileFromHistory(n);
2021
9806a0e7 2022 wxLogError(_("The file '%s' doesn't exist and couldn't be opened.\nIt has been removed from the most recently used files list."),
0c5d3e1c
VZ
2023 filename.c_str());
2024 }
2025 }
c801d85f
KB
2026}
2027
2028// Extend event processing to search the view's event table
2029bool wxDocParentFrame::ProcessEvent(wxEvent& event)
2030{
2031 // Try the document manager, then do default processing
2032 if (!m_docManager || !m_docManager->ProcessEvent(event))
2033 return wxEvtHandler::ProcessEvent(event);
2034 else
68379eaf 2035 return true;
c801d85f
KB
2036}
2037
c801d85f
KB
2038// Define the behaviour for the frame closing
2039// - must delete all frames except for the main one.
387a3b02 2040void wxDocParentFrame::OnCloseWindow(wxCloseEvent& event)
c801d85f 2041{
0fb67cd1
VZ
2042 if (m_docManager->Clear(!event.CanVeto()))
2043 {
2044 this->Destroy();
2045 }
2046 else
2047 event.Veto();
c801d85f
KB
2048}
2049
47d67540 2050#if wxUSE_PRINTING_ARCHITECTURE
c801d85f 2051
0fb67cd1 2052wxDocPrintout::wxDocPrintout(wxView *view, const wxString& title)
e90c1d2a 2053 : wxPrintout(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
2110void wxDocPrintout::GetPageInfo(int *minPage, int *maxPage, int *selPageFrom, int *selPageTo)
2111{
0fb67cd1
VZ
2112 *minPage = 1;
2113 *maxPage = 1;
2114 *selPageFrom = 1;
2115 *selPageTo = 1;
c801d85f
KB
2116}
2117
0fb67cd1 2118#endif // wxUSE_PRINTING_ARCHITECTURE
c801d85f 2119
0fb67cd1
VZ
2120// ----------------------------------------------------------------------------
2121// File history processor
2122// ----------------------------------------------------------------------------
c801d85f 2123
e49c85af 2124wxFileHistory::wxFileHistory(size_t maxFiles, wxWindowID idBase)
c801d85f 2125{
0fb67cd1 2126 m_fileMaxFiles = maxFiles;
e49c85af 2127 m_idBase = idBase;
c801d85f
KB
2128}
2129
0fb67cd1 2130wxFileHistory::~wxFileHistory()
c801d85f 2131{
c801d85f
KB
2132}
2133
c801d85f
KB
2134void wxFileHistory::AddFileToHistory(const wxString& file)
2135{
b4a980f4 2136 wxFileName fn(file);
e49c85af 2137 size_t i;
02718e6a 2138
0fb67cd1 2139 // Check we don't already have this file
b4a980f4 2140 for (i = 0; i < m_fileHistory.GetCount(); i++)
7f555861 2141 {
b4a980f4
VZ
2142 // we need to do a comparison using wxFileNames because it knows
2143 // how to exactly compare files on the different platforms
2144 // (e.g. handle case [in]sensitive filesystems)
2145 if ( fn == wxFileName(m_fileHistory[i]) )
02718e6a
VZ
2146 {
2147 // we do have it, move it to the top of the history
2148 RemoveFileFromHistory (i);
2149 AddFileToHistory (file);
0fb67cd1 2150 return;
02718e6a 2151 }
7f555861 2152 }
0fb67cd1 2153
02718e6a 2154 // if we already have a full history, delete the one at the end
b4a980f4 2155 if ( m_fileMaxFiles == m_fileHistory.GetCount() )
0fb67cd1 2156 {
b4a980f4 2157 RemoveFileFromHistory (m_fileHistory.GetCount() - 1);
02718e6a
VZ
2158 AddFileToHistory (file);
2159 return;
c801d85f 2160 }
02718e6a
VZ
2161
2162 // Add to the project file history:
2163 // Move existing files (if any) down so we can insert file at beginning.
b4a980f4 2164 if (m_fileHistory.GetCount() < m_fileMaxFiles)
0fb67cd1 2165 {
222ed1d6 2166 wxList::compatibility_iterator node = m_fileMenus.GetFirst();
0fb67cd1
VZ
2167 while (node)
2168 {
b1d4dd7a 2169 wxMenu* menu = (wxMenu*) node->GetData();
b4a980f4 2170 if ( m_fileHistory.IsEmpty() && menu->GetMenuItemCount() )
c8c5c7f6 2171 {
0fb67cd1 2172 menu->AppendSeparator();
c8c5c7f6 2173 }
b4a980f4 2174 menu->Append(m_idBase+m_fileHistory.GetCount(), _("[EMPTY]"));
b1d4dd7a 2175 node = node->GetNext();
0fb67cd1 2176 }
0fb67cd1 2177 }
b4a980f4
VZ
2178
2179 m_fileHistory.Insert(file, 0);
0fb67cd1 2180
02718e6a
VZ
2181 // this is the directory of the last opened file
2182 wxString pathCurrent;
2183 wxSplitPath( m_fileHistory[0], &pathCurrent, NULL, NULL );
b4a980f4 2184 for (i = 0; i < m_fileHistory.GetCount(); i++)
0fb67cd1 2185 {
02718e6a
VZ
2186 // if in same directory just show the filename; otherwise the full
2187 // path
2188 wxString pathInMenu, path, filename, ext;
2189 wxSplitPath( m_fileHistory[i], &path, &filename, &ext );
2190 if ( path == pathCurrent )
2191 {
2192 pathInMenu = filename;
2193 if ( !ext.empty() )
2194 pathInMenu = pathInMenu + wxFILE_SEP_EXT + ext;
2195 }
2196 else
2197 {
2198 // absolute path; could also set relative path
2199 pathInMenu = m_fileHistory[i];
2200 }
2201
28ea89b6
VZ
2202 // we need to quote '&' characters which are used for mnemonics
2203 pathInMenu.Replace(_T("&"), _T("&&"));
b4a980f4 2204
0fb67cd1 2205 wxString buf;
02718e6a 2206 buf.Printf(s_MRUEntryFormat, i + 1, pathInMenu.c_str());
b4a980f4 2207
222ed1d6 2208 wxList::compatibility_iterator node = m_fileMenus.GetFirst();
0fb67cd1
VZ
2209 while (node)
2210 {
b1d4dd7a 2211 wxMenu* menu = (wxMenu*) node->GetData();
e49c85af 2212 menu->SetLabel(m_idBase + i, buf);
b1d4dd7a 2213 node = node->GetNext();
0fb67cd1
VZ
2214 }
2215 }
c801d85f
KB
2216}
2217
e49c85af 2218void wxFileHistory::RemoveFileFromHistory(size_t i)
0c5d3e1c 2219{
b4a980f4 2220 wxCHECK_RET( i < m_fileHistory.GetCount(),
223d09f6 2221 wxT("invalid index in wxFileHistory::RemoveFileFromHistory") );
0c5d3e1c 2222
f6fe9f9c 2223 // delete the element from the array
b4a980f4 2224 m_fileHistory.RemoveAt(i);
0c5d3e1c 2225
222ed1d6 2226 wxList::compatibility_iterator node = m_fileMenus.GetFirst();
00e6d8ab
VZ
2227 while ( node )
2228 {
4a10ea8b 2229 wxMenu* menu = (wxMenu*) node->GetData();
00e6d8ab 2230
4a10ea8b
MW
2231 // shuffle filenames up
2232 wxString buf;
b4a980f4 2233 for ( size_t j = i; j < m_fileHistory.GetCount(); j++ )
4a10ea8b 2234 {
b4a980f4 2235 buf.Printf(s_MRUEntryFormat, j + 1, m_fileHistory[j].c_str());
4a10ea8b
MW
2236 menu->SetLabel(m_idBase + j, buf);
2237 }
0c5d3e1c 2238
4a10ea8b 2239 node = node->GetNext();
2b273975
VZ
2240
2241 // delete the last menu item which is unused now
b4a980f4 2242 wxWindowID lastItemId = m_idBase + wx_truncate_cast(wxWindowID, m_fileHistory.GetCount());
e49c85af
VZ
2243 if (menu->FindItem(lastItemId))
2244 {
2245 menu->Delete(lastItemId);
2246 }
2b273975 2247
717a57c2 2248 // delete the last separator too if no more files are left
b4a980f4 2249 if ( m_fileHistory.GetCount() == 0 )
717a57c2 2250 {
f1afd2e0
VZ
2251 wxMenuItemList::compatibility_iterator nodeLast = menu->GetMenuItems().GetLast();
2252 if ( nodeLast )
717a57c2 2253 {
f1afd2e0 2254 wxMenuItem *menuItem = nodeLast->GetData();
717a57c2
VZ
2255 if ( menuItem->IsSeparator() )
2256 {
2257 menu->Delete(menuItem);
2258 }
2259 //else: should we search backwards for the last separator?
2260 }
2261 //else: menu is empty somehow
2262 }
0c5d3e1c 2263 }
c801d85f
KB
2264}
2265
7f555861 2266void wxFileHistory::UseMenu(wxMenu *menu)
c801d85f 2267{
0fb67cd1
VZ
2268 if (!m_fileMenus.Member(menu))
2269 m_fileMenus.Append(menu);
c801d85f
KB
2270}
2271
7f555861
JS
2272void wxFileHistory::RemoveMenu(wxMenu *menu)
2273{
0fb67cd1 2274 m_fileMenus.DeleteObject(menu);
7f555861
JS
2275}
2276
702ca7c0 2277#if wxUSE_CONFIG
7f555861 2278void wxFileHistory::Load(wxConfigBase& config)
c801d85f 2279{
b4a980f4
VZ
2280 m_fileHistory.Clear();
2281
0fb67cd1 2282 wxString buf;
b4a980f4
VZ
2283 buf.Printf(wxT("file%d"), 1);
2284
0fb67cd1 2285 wxString historyFile;
f6fe9f9c 2286 while ((m_fileHistory.GetCount() < m_fileMaxFiles) &&
b4a980f4 2287 config.Read(buf, &historyFile) && !historyFile.empty())
0fb67cd1 2288 {
b4a980f4
VZ
2289 m_fileHistory.Add(historyFile);
2290
2291 buf.Printf(wxT("file%d"), (int)m_fileHistory.GetCount()+1);
b494c48b 2292 historyFile = wxEmptyString;
0fb67cd1 2293 }
b4a980f4 2294
0fb67cd1 2295 AddFilesToMenu();
c801d85f
KB
2296}
2297
7f555861 2298void wxFileHistory::Save(wxConfigBase& config)
c801d85f 2299{
e49c85af 2300 size_t i;
9d4a05be 2301 for (i = 0; i < m_fileMaxFiles; i++)
0fb67cd1
VZ
2302 {
2303 wxString buf;
8c9564b3 2304 buf.Printf(wxT("file%d"), (int)i+1);
b4a980f4 2305 if (i < m_fileHistory.GetCount())
9d4a05be
JS
2306 config.Write(buf, wxString(m_fileHistory[i]));
2307 else
42771621 2308 config.Write(buf, wxEmptyString);
0fb67cd1 2309 }
7f555861 2310}
0fb67cd1 2311#endif // wxUSE_CONFIG
7f555861
JS
2312
2313void wxFileHistory::AddFilesToMenu()
2314{
b4a980f4 2315 if (m_fileHistory.GetCount() > 0)
7f555861 2316 {
222ed1d6 2317 wxList::compatibility_iterator node = m_fileMenus.GetFirst();
7f555861
JS
2318 while (node)
2319 {
b1d4dd7a 2320 wxMenu* menu = (wxMenu*) node->GetData();
c8c5c7f6
JS
2321 if (menu->GetMenuItemCount())
2322 {
2323 menu->AppendSeparator();
2324 }
2325
e49c85af 2326 size_t i;
b4a980f4 2327 for (i = 0; i < m_fileHistory.GetCount(); i++)
7f555861
JS
2328 {
2329 wxString buf;
b4a980f4 2330 buf.Printf(s_MRUEntryFormat, i+1, m_fileHistory[i].c_str());
e49c85af 2331 menu->Append(m_idBase+i, buf);
7f555861 2332 }
b1d4dd7a 2333 node = node->GetNext();
7f555861
JS
2334 }
2335 }
2336}
2337
2338void wxFileHistory::AddFilesToMenu(wxMenu* menu)
2339{
b4a980f4 2340 if (m_fileHistory.GetCount() > 0)
7f555861 2341 {
c8c5c7f6
JS
2342 if (menu->GetMenuItemCount())
2343 {
2344 menu->AppendSeparator();
2345 }
2346
e49c85af 2347 size_t i;
b4a980f4 2348 for (i = 0; i < m_fileHistory.GetCount(); i++)
7f555861
JS
2349 {
2350 wxString buf;
b4a980f4 2351 buf.Printf(s_MRUEntryFormat, i+1, m_fileHistory[i].c_str());
e49c85af 2352 menu->Append(m_idBase+i, buf);
7f555861
JS
2353 }
2354 }
c801d85f
KB
2355}
2356
0fb67cd1
VZ
2357// ----------------------------------------------------------------------------
2358// Permits compatibility with existing file formats and functions that
2359// manipulate files directly
2360// ----------------------------------------------------------------------------
c801d85f 2361
a533f5c1 2362#if wxUSE_STD_IOSTREAM
fb6e5b17 2363
dd107c50 2364bool wxTransferFileToStream(const wxString& filename, wxSTD ostream& stream)
c801d85f 2365{
fb6e5b17
VZ
2366 wxFFile file(filename, _T("rb"));
2367 if ( !file.IsOpened() )
68379eaf 2368 return false;
c801d85f 2369
fb6e5b17
VZ
2370 char buf[4096];
2371
2372 size_t nRead;
2373 do
2374 {
2375 nRead = file.Read(buf, WXSIZEOF(buf));
2376 if ( file.Error() )
68379eaf 2377 return false;
fb6e5b17
VZ
2378
2379 stream.write(buf, nRead);
2380 if ( !stream )
68379eaf 2381 return false;
fb6e5b17
VZ
2382 }
2383 while ( !file.Eof() );
c801d85f 2384
68379eaf 2385 return true;
c801d85f
KB
2386}
2387
dd107c50 2388bool wxTransferStreamToFile(wxSTD istream& stream, const wxString& filename)
c801d85f 2389{
fb6e5b17
VZ
2390 wxFFile file(filename, _T("wb"));
2391 if ( !file.IsOpened() )
68379eaf 2392 return false;
c801d85f 2393
fb6e5b17
VZ
2394 char buf[4096];
2395 do
0fb67cd1 2396 {
fb6e5b17
VZ
2397 stream.read(buf, WXSIZEOF(buf));
2398 if ( !stream.bad() ) // fail may be set on EOF, don't use operator!()
2399 {
2400 if ( !file.Write(buf, stream.gcount()) )
68379eaf 2401 return false;
fb6e5b17 2402 }
0fb67cd1 2403 }
fb6e5b17
VZ
2404 while ( !stream.eof() );
2405
68379eaf 2406 return true;
c801d85f 2407}
fb6e5b17
VZ
2408
2409#else // !wxUSE_STD_IOSTREAM
2410
dc1efb1d
JS
2411bool wxTransferFileToStream(const wxString& filename, wxOutputStream& stream)
2412{
fb6e5b17
VZ
2413 wxFFile file(filename, _T("rb"));
2414 if ( !file.IsOpened() )
68379eaf 2415 return false;
dc1efb1d 2416
fb6e5b17
VZ
2417 char buf[4096];
2418
2419 size_t nRead;
2420 do
2421 {
2422 nRead = file.Read(buf, WXSIZEOF(buf));
2423 if ( file.Error() )
68379eaf 2424 return false;
fb6e5b17
VZ
2425
2426 stream.Write(buf, nRead);
2427 if ( !stream )
68379eaf 2428 return false;
fb6e5b17
VZ
2429 }
2430 while ( !file.Eof() );
dc1efb1d 2431
68379eaf 2432 return true;
dc1efb1d
JS
2433}
2434
2435bool wxTransferStreamToFile(wxInputStream& stream, const wxString& filename)
2436{
fb6e5b17
VZ
2437 wxFFile file(filename, _T("wb"));
2438 if ( !file.IsOpened() )
68379eaf 2439 return false;
dc1efb1d 2440
fb6e5b17 2441 char buf[4096];
07f87b6b 2442 for ( ;; )
dc1efb1d 2443 {
fb6e5b17
VZ
2444 stream.Read(buf, WXSIZEOF(buf));
2445
2446 const size_t nRead = stream.LastRead();
07f87b6b
VZ
2447 if ( !nRead )
2448 {
2449 if ( stream.Eof() )
2450 break;
2451
2452 return false;
2453 }
2454
2455 if ( !file.Write(buf, nRead) )
68379eaf 2456 return false;
dc1efb1d 2457 }
fb6e5b17 2458
68379eaf 2459 return true;
dc1efb1d 2460}
fb6e5b17
VZ
2461
2462#endif // wxUSE_STD_IOSTREAM/!wxUSE_STD_IOSTREAM
c801d85f 2463
0fb67cd1 2464#endif // wxUSE_DOC_VIEW_ARCHITECTURE