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