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