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