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