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