]> git.saurik.com Git - wxWidgets.git/blame - src/common/docview.cpp
In SetProxy, add 1 to pos when parsing port since otherwise we include the colon.
[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{
bb28b477 597 if (GetDocument() && GetDocumentManager())
0fb67cd1
VZ
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()) );
2599ee02
JS
977 if (doc && doc->GetCommandProcessor())
978 doc->GetCommandProcessor()->SetMenuStrings();
f2506310
JS
979}
980
981void wxDocManager::OnUpdateRedo(wxUpdateUIEvent& event)
982{
983 wxDocument *doc = GetCurrentDocument();
984 event.Enable( (doc && doc->GetCommandProcessor() && doc->GetCommandProcessor()->CanRedo()) );
2599ee02
JS
985 if (doc && doc->GetCommandProcessor())
986 doc->GetCommandProcessor()->SetMenuStrings();
f2506310
JS
987}
988
989void wxDocManager::OnUpdatePrint(wxUpdateUIEvent& event)
990{
991 wxDocument *doc = GetCurrentDocument();
992 event.Enable( (doc != (wxDocument*) NULL) );
993}
994
995void wxDocManager::OnUpdatePrintSetup(wxUpdateUIEvent& event)
996{
997 event.Enable( TRUE );
998}
999
1000void wxDocManager::OnUpdatePreview(wxUpdateUIEvent& event)
1001{
1002 wxDocument *doc = GetCurrentDocument();
1003 event.Enable( (doc != (wxDocument*) NULL) );
1004}
1005
6de2f8b9 1006wxView *wxDocManager::GetCurrentView() const
637f467a
JS
1007{
1008 if (m_currentView)
1009 return m_currentView;
1010 if (m_docs.Number() == 1)
1011 {
1012 wxDocument* doc = (wxDocument*) m_docs.First()->Data();
1013 return doc->GetFirstView();
1014 }
c67daf87 1015 return (wxView *) NULL;
637f467a
JS
1016}
1017
1018// Extend event processing to search the view's event table
1019bool wxDocManager::ProcessEvent(wxEvent& event)
1020{
1021 wxView* view = GetCurrentView();
1022 if (view)
1023 {
1024 if (view->ProcessEvent(event))
1025 return TRUE;
1026 }
1027 return wxEvtHandler::ProcessEvent(event);
1028}
1029
c801d85f
KB
1030wxDocument *wxDocManager::CreateDocument(const wxString& path, long flags)
1031{
0fb67cd1
VZ
1032 wxDocTemplate **templates = new wxDocTemplate *[m_templates.Number()];
1033 int i;
1034 int n = 0;
1035 for (i = 0; i < m_templates.Number(); i++)
c801d85f 1036 {
0fb67cd1
VZ
1037 wxDocTemplate *temp = (wxDocTemplate *)(m_templates.Nth(i)->Data());
1038 if (temp->IsVisible())
1039 {
1040 templates[n] = temp;
1041 n ++;
1042 }
c801d85f 1043 }
0fb67cd1 1044 if (n == 0)
c801d85f 1045 {
0fb67cd1
VZ
1046 delete[] templates;
1047 return (wxDocument *) NULL;
c801d85f 1048 }
0fb67cd1
VZ
1049
1050 // If we've reached the max number of docs, close the
1051 // first one.
1052 if (GetDocuments().Number() >= m_maxDocsOpen)
c801d85f 1053 {
0fb67cd1
VZ
1054 wxDocument *doc = (wxDocument *)GetDocuments().First()->Data();
1055 if (doc->Close())
1056 {
1057 // Implicitly deletes the document when
1058 // the last view is deleted
1059 doc->DeleteAllViews();
1060
1061 // Check we're really deleted
1062 if (m_docs.Member(doc))
1063 delete doc;
1064 }
1065 else
51fad246
VZ
1066 {
1067 delete[] templates;
0fb67cd1 1068 return (wxDocument *) NULL;
51fad246 1069 }
c801d85f
KB
1070 }
1071
0fb67cd1
VZ
1072 // New document: user chooses a template, unless there's only one.
1073 if (flags & wxDOC_NEW)
c801d85f 1074 {
0fb67cd1
VZ
1075 if (n == 1)
1076 {
1077 wxDocTemplate *temp = templates[0];
1078 delete[] templates;
1079 wxDocument *newDoc = temp->CreateDocument(path, flags);
1080 if (newDoc)
1081 {
1082 newDoc->SetDocumentName(temp->GetDocumentName());
1083 newDoc->SetDocumentTemplate(temp);
1084 newDoc->OnNewDocument();
1085 }
1086 return newDoc;
1087 }
1088
1089 wxDocTemplate *temp = SelectDocumentType(templates, n);
1090 delete[] templates;
1091 if (temp)
1092 {
1093 wxDocument *newDoc = temp->CreateDocument(path, flags);
1094 if (newDoc)
1095 {
1096 newDoc->SetDocumentName(temp->GetDocumentName());
1097 newDoc->SetDocumentTemplate(temp);
1098 newDoc->OnNewDocument();
1099 }
1100 return newDoc;
1101 }
1102 else
1103 return (wxDocument *) NULL;
c801d85f 1104 }
c801d85f 1105
0fb67cd1
VZ
1106 // Existing document
1107 wxDocTemplate *temp = (wxDocTemplate *) NULL;
c801d85f 1108
223d09f6
KB
1109 wxString path2(wxT(""));
1110 if (path != wxT(""))
0fb67cd1 1111 path2 = path;
c801d85f 1112
0fb67cd1
VZ
1113 if (flags & wxDOC_SILENT)
1114 temp = FindTemplateForPath(path2);
1115 else
1116 temp = SelectDocumentPath(templates, n, path2, flags);
c801d85f 1117
0fb67cd1 1118 delete[] templates;
c801d85f 1119
0fb67cd1 1120 if (temp)
c801d85f 1121 {
0fb67cd1
VZ
1122 wxDocument *newDoc = temp->CreateDocument(path2, flags);
1123 if (newDoc)
1124 {
1125 newDoc->SetDocumentName(temp->GetDocumentName());
1126 newDoc->SetDocumentTemplate(temp);
1127 if (!newDoc->OnOpenDocument(path2))
1128 {
f6bcfd97
BP
1129 newDoc->DeleteAllViews();
1130 // delete newDoc; // Implicitly deleted by DeleteAllViews
0fb67cd1
VZ
1131 return (wxDocument *) NULL;
1132 }
1133 AddFileToHistory(path2);
1134 }
1135 return newDoc;
c801d85f 1136 }
3ca6a5f0
BP
1137
1138 return (wxDocument *) NULL;
c801d85f
KB
1139}
1140
1141wxView *wxDocManager::CreateView(wxDocument *doc, long flags)
1142{
0fb67cd1
VZ
1143 wxDocTemplate **templates = new wxDocTemplate *[m_templates.Number()];
1144 int n =0;
1145 int i;
1146 for (i = 0; i < m_templates.Number(); i++)
1147 {
1148 wxDocTemplate *temp = (wxDocTemplate *)(m_templates.Nth(i)->Data());
1149 if (temp->IsVisible())
1150 {
1151 if (temp->GetDocumentName() == doc->GetDocumentName())
1152 {
1153 templates[n] = temp;
1154 n ++;
1155 }
1156 }
1157 }
1158 if (n == 0)
1159 {
1160 delete[] templates;
1161 return (wxView *) NULL;
1162 }
1163 if (n == 1)
1164 {
1165 wxDocTemplate *temp = templates[0];
1166 delete[] templates;
1167 wxView *view = temp->CreateView(doc, flags);
1168 if (view)
1169 view->SetViewName(temp->GetViewName());
1170 return view;
1171 }
1172
1173 wxDocTemplate *temp = SelectViewType(templates, n);
c801d85f 1174 delete[] templates;
0fb67cd1
VZ
1175 if (temp)
1176 {
1177 wxView *view = temp->CreateView(doc, flags);
1178 if (view)
1179 view->SetViewName(temp->GetViewName());
1180 return view;
1181 }
1182 else
1183 return (wxView *) NULL;
c801d85f
KB
1184}
1185
1186// Not yet implemented
1187void wxDocManager::DeleteTemplate(wxDocTemplate *WXUNUSED(temp), long WXUNUSED(flags))
1188{
1189}
1190
1191// Not yet implemented
1192bool wxDocManager::FlushDoc(wxDocument *WXUNUSED(doc))
1193{
0fb67cd1 1194 return FALSE;
c801d85f
KB
1195}
1196
6de2f8b9 1197wxDocument *wxDocManager::GetCurrentDocument() const
c801d85f 1198{
9cb0033c
VZ
1199 wxView *view = GetCurrentView();
1200 if (view)
1201 return view->GetDocument();
0fb67cd1
VZ
1202 else
1203 return (wxDocument *) NULL;
c801d85f
KB
1204}
1205
1206// Make a default document name
1207bool wxDocManager::MakeDefaultName(wxString& name)
1208{
0fb67cd1
VZ
1209 name.Printf(_("unnamed%d"), m_defaultDocumentNameCounter);
1210 m_defaultDocumentNameCounter++;
53c6e7cc 1211
0fb67cd1 1212 return TRUE;
c801d85f
KB
1213}
1214
f2506310
JS
1215// Make a frame title (override this to do something different)
1216// If docName is empty, a document is not currently active.
1217wxString wxDocManager::MakeFrameTitle(wxDocument* doc)
1218{
1219 wxString appName = wxTheApp->GetAppName();
1220 wxString title;
1221 if (!doc)
1222 title = appName;
1223 else
1224 {
1225 wxString docName;
1226 doc->GetPrintableName(docName);
1227 title = docName + wxString(_(" - ")) + appName;
1228 }
1229 return title;
1230}
1231
1232
c801d85f
KB
1233// Not yet implemented
1234wxDocTemplate *wxDocManager::MatchTemplate(const wxString& WXUNUSED(path))
1235{
0fb67cd1 1236 return (wxDocTemplate *) NULL;
c801d85f
KB
1237}
1238
1239// File history management
1240void wxDocManager::AddFileToHistory(const wxString& file)
1241{
0fb67cd1
VZ
1242 if (m_fileHistory)
1243 m_fileHistory->AddFileToHistory(file);
c801d85f
KB
1244}
1245
0c5d3e1c
VZ
1246void wxDocManager::RemoveFileFromHistory(int i)
1247{
1248 if (m_fileHistory)
1249 m_fileHistory->RemoveFileFromHistory(i);
1250}
1251
c801d85f
KB
1252wxString wxDocManager::GetHistoryFile(int i) const
1253{
0fb67cd1
VZ
1254 wxString histFile;
1255
1256 if (m_fileHistory)
1257 histFile = m_fileHistory->GetHistoryFile(i);
1258
1259 return histFile;
c801d85f
KB
1260}
1261
1262void wxDocManager::FileHistoryUseMenu(wxMenu *menu)
1263{
0fb67cd1
VZ
1264 if (m_fileHistory)
1265 m_fileHistory->UseMenu(menu);
c801d85f
KB
1266}
1267
7f555861 1268void wxDocManager::FileHistoryRemoveMenu(wxMenu *menu)
c801d85f 1269{
0fb67cd1
VZ
1270 if (m_fileHistory)
1271 m_fileHistory->RemoveMenu(menu);
c801d85f
KB
1272}
1273
702ca7c0 1274#if wxUSE_CONFIG
7f555861 1275void wxDocManager::FileHistoryLoad(wxConfigBase& config)
c801d85f 1276{
0fb67cd1
VZ
1277 if (m_fileHistory)
1278 m_fileHistory->Load(config);
7f555861
JS
1279}
1280
1281void wxDocManager::FileHistorySave(wxConfigBase& config)
1282{
0fb67cd1
VZ
1283 if (m_fileHistory)
1284 m_fileHistory->Save(config);
7f555861 1285}
ac57418f 1286#endif
7f555861
JS
1287
1288void wxDocManager::FileHistoryAddFilesToMenu(wxMenu* menu)
1289{
0fb67cd1
VZ
1290 if (m_fileHistory)
1291 m_fileHistory->AddFilesToMenu(menu);
7f555861
JS
1292}
1293
1294void wxDocManager::FileHistoryAddFilesToMenu()
1295{
0fb67cd1
VZ
1296 if (m_fileHistory)
1297 m_fileHistory->AddFilesToMenu();
c801d85f
KB
1298}
1299
6de2f8b9 1300int wxDocManager::GetNoHistoryFiles() const
c801d85f 1301{
0fb67cd1
VZ
1302 if (m_fileHistory)
1303 return m_fileHistory->GetNoHistoryFiles();
c801d85f 1304 else
0fb67cd1 1305 return 0;
c801d85f
KB
1306}
1307
1308
6de2f8b9
VZ
1309// Find out the document template via matching in the document file format
1310// against that of the template
c801d85f
KB
1311wxDocTemplate *wxDocManager::FindTemplateForPath(const wxString& path)
1312{
0fb67cd1 1313 wxDocTemplate *theTemplate = (wxDocTemplate *) NULL;
c801d85f 1314
0fb67cd1
VZ
1315 // Find the template which this extension corresponds to
1316 int i;
1317 for (i = 0; i < m_templates.Number(); i++)
c801d85f 1318 {
0fb67cd1 1319 wxDocTemplate *temp = (wxDocTemplate *)m_templates.Nth(i)->Data();
6de2f8b9 1320 if ( temp->FileMatchesTemplate(path) )
0fb67cd1
VZ
1321 {
1322 theTemplate = temp;
1323 break;
1324 }
c801d85f 1325 }
0fb67cd1 1326 return theTemplate;
c801d85f
KB
1327}
1328
f6bcfd97
BP
1329// Try to get a more suitable parent frame than the top window,
1330// for selection dialogs. Otherwise you may get an unexpected
1331// window being activated when a dialog is shown.
1332static wxWindow* wxFindSuitableParent()
1333{
1334 wxWindow* parent = wxTheApp->GetTopWindow();
1335
1336 wxWindow* focusWindow = wxWindow::FindFocus();
1337 if (focusWindow)
1338 {
1339 while (focusWindow &&
1340 !focusWindow->IsKindOf(CLASSINFO(wxDialog)) &&
1341 !focusWindow->IsKindOf(CLASSINFO(wxFrame)))
1342
1343 focusWindow = focusWindow->GetParent();
1344
1345 if (focusWindow)
1346 parent = focusWindow;
1347 }
1348 return parent;
1349}
1350
c801d85f
KB
1351// Prompts user to open a file, using file specs in templates.
1352// How to implement in wxWindows? Must extend the file selector
1353// dialog or implement own; OR match the extension to the
1354// template extension.
df875e59 1355
c801d85f 1356wxDocTemplate *wxDocManager::SelectDocumentPath(wxDocTemplate **templates,
bf9e3e73 1357#if defined(__WXMSW__) || defined(__WXGTK__)
0fb67cd1 1358 int noTemplates,
f6147cfc
VZ
1359#else
1360 int WXUNUSED(noTemplates),
1361#endif
0fb67cd1
VZ
1362 wxString& path,
1363 long WXUNUSED(flags),
1364 bool WXUNUSED(save))
c801d85f 1365{
bf9e3e73
RR
1366 // We can only have multiple filters in Windows and GTK
1367#if defined(__WXMSW__) || defined(__WXGTK__)
ba681060
VZ
1368 wxString descrBuf;
1369
1370 int i;
1371 for (i = 0; i < noTemplates; i++)
c801d85f 1372 {
ba681060
VZ
1373 if (templates[i]->IsVisible())
1374 {
1375 // add a '|' to separate this filter from the previous one
1376 if ( !descrBuf.IsEmpty() )
223d09f6 1377 descrBuf << wxT('|');
ba681060
VZ
1378
1379 descrBuf << templates[i]->GetDescription()
223d09f6 1380 << wxT(" (") << templates[i]->GetFileFilter() << wxT(") |")
0fb67cd1 1381 << templates[i]->GetFileFilter();
ba681060 1382 }
c801d85f 1383 }
a4294b78 1384#else
223d09f6 1385 wxString descrBuf = wxT("*.*");
a4294b78 1386#endif
c801d85f 1387
3ca6a5f0 1388 int FilterIndex = -1;
f6bcfd97
BP
1389
1390 wxWindow* parent = wxFindSuitableParent();
1391
6de2f8b9 1392 wxString pathTmp = wxFileSelectorEx(_("Select a file"),
ac0ac824 1393 m_lastDirectory,
223d09f6 1394 wxT(""),
6de2f8b9 1395 &FilterIndex,
caf0debf
VZ
1396 descrBuf,
1397 0,
f6bcfd97 1398 parent);
ba681060 1399
3ca6a5f0 1400 wxDocTemplate *theTemplate = (wxDocTemplate *)NULL;
0fb67cd1
VZ
1401 if (!pathTmp.IsEmpty())
1402 {
f6bcfd97
BP
1403 if (!wxFileExists(pathTmp))
1404 {
1405 wxString msgTitle;
1406 if (!wxTheApp->GetAppName().IsEmpty())
1407 msgTitle = wxTheApp->GetAppName();
1408 else
1409 msgTitle = wxString(_("File error"));
02718e6a 1410
f6bcfd97
BP
1411 (void)wxMessageBox(_("Sorry, could not open this file."), msgTitle, wxOK | wxICON_EXCLAMATION,
1412 parent);
1413
1414 path = wxT("");
1415 return (wxDocTemplate *) NULL;
1416 }
ac0ac824
JS
1417 m_lastDirectory = wxPathOnly(pathTmp);
1418
0fb67cd1 1419 path = pathTmp;
0fb67cd1 1420
3ca6a5f0
BP
1421 // first choose the template using the extension, if this fails (i.e.
1422 // wxFileSelectorEx() didn't fill it), then use the path
1423 if ( FilterIndex != -1 )
6de2f8b9 1424 theTemplate = templates[FilterIndex];
3ca6a5f0
BP
1425 if ( !theTemplate )
1426 theTemplate = FindTemplateForPath(path);
0fb67cd1
VZ
1427 }
1428 else
1429 {
223d09f6 1430 path = wxT("");
0fb67cd1 1431 }
3ca6a5f0
BP
1432
1433 return theTemplate;
1434
a4294b78 1435#if 0
0fb67cd1
VZ
1436 // In all other windowing systems, until we have more advanced
1437 // file selectors, we must select the document type (template) first, and
1438 // _then_ pop up the file selector.
1439 wxDocTemplate *temp = SelectDocumentType(templates, noTemplates);
1440 if (!temp)
1441 return (wxDocTemplate *) NULL;
1442
223d09f6 1443 wxChar *pathTmp = wxFileSelector(_("Select a file"), wxT(""), wxT(""),
0fb67cd1
VZ
1444 temp->GetDefaultExtension(),
1445 temp->GetFileFilter(),
1446 0, wxTheApp->GetTopWindow());
1447
1448 if (pathTmp)
1449 {
1450 path = pathTmp;
1451 return temp;
1452 }
1453 else
1454 return (wxDocTemplate *) NULL;
1455#endif // 0
c801d85f
KB
1456}
1457
1458wxDocTemplate *wxDocManager::SelectDocumentType(wxDocTemplate **templates,
52b9ca21 1459 int noTemplates, bool sort)
0fb67cd1 1460{
52b9ca21 1461 wxArrayString strings(sort);
17260efd 1462 wxDocTemplate **data = new wxDocTemplate *[noTemplates];
0fb67cd1
VZ
1463 int i;
1464 int n = 0;
bb28b477 1465
8658ef93
GT
1466 for (i = 0; i < noTemplates; i++)
1467 {
1468 if (templates[i]->IsVisible())
1469 {
bb28b477
JS
1470 int j;
1471 bool want = TRUE;
1472 for (j = 0; j < n; j++)
1473 {
1474 //filter out NOT unique documents + view combinations
1475 if ( templates[i]->m_docTypeName == data[j]->m_docTypeName &&
1476 templates[i]->m_viewTypeName == data[j]->m_viewTypeName
1477 )
1478 want = FALSE;
1479 }
1480
1481 if ( want )
8658ef93 1482 {
bb28b477
JS
1483 strings.Add(templates[i]->m_description);
1484
8658ef93
GT
1485 data[n] = templates[i];
1486 n ++;
1487 }
1488 }
1489 } // for
1490
1491 if (sort)
1492 {
1493 // Yes, this will be slow, but template lists
1494 // are typically short.
1495 int j;
1496 n = strings.Count();
1497 for (i = 0; i < n; i++)
1498 {
1499 for (j = 0; j < noTemplates; j++)
1500 {
1501 if (strings[i] == templates[j]->m_description)
1502 data[i] = templates[j];
1503 }
1504 }
1505 }
17260efd
VZ
1506
1507 wxDocTemplate *theTemplate;
1508
1509 switch ( n )
0fb67cd1 1510 {
17260efd
VZ
1511 case 0:
1512 // no visible templates, hence nothing to choose from
1513 theTemplate = NULL;
1514 break;
0fb67cd1 1515
17260efd
VZ
1516 case 1:
1517 // don't propose the user to choose if he heas no choice
1518 theTemplate = data[0];
1519 break;
1520
1521 default:
1522 // propose the user to choose one of several
1523 theTemplate = (wxDocTemplate *)wxGetSingleChoiceData
1524 (
1525 _("Select a document template"),
1526 _("Templates"),
1527 strings,
1528 (void **)data,
1529 wxFindSuitableParent()
1530 );
1531 }
f6bcfd97 1532
c801d85f 1533 delete[] data;
17260efd 1534
0fb67cd1 1535 return theTemplate;
c801d85f
KB
1536}
1537
1538wxDocTemplate *wxDocManager::SelectViewType(wxDocTemplate **templates,
52b9ca21 1539 int noTemplates, bool sort)
c801d85f 1540{
52b9ca21 1541 wxArrayString strings(sort);
17260efd 1542 wxDocTemplate **data = new wxDocTemplate *[noTemplates];
0fb67cd1
VZ
1543 int i;
1544 int n = 0;
bb28b477 1545
0fb67cd1 1546 for (i = 0; i < noTemplates; i++)
c801d85f 1547 {
17260efd
VZ
1548 wxDocTemplate *templ = templates[i];
1549 if ( templ->IsVisible() && !templ->GetViewName().empty() )
0fb67cd1 1550 {
bb28b477
JS
1551 int j;
1552 bool want = TRUE;
1553 for (j = 0; j < n; j++)
8658ef93 1554 {
bb28b477
JS
1555 //filter out NOT unique views
1556 if ( templates[i]->m_viewTypeName == data[j]->m_viewTypeName )
1557 want = FALSE;
1558 }
1559
1560 if ( want )
1561 {
1562 strings.Add(templ->m_viewTypeName);
8658ef93
GT
1563 data[n] = templ;
1564 n ++;
1565 }
0fb67cd1 1566 }
c801d85f 1567 }
f6bcfd97 1568
8658ef93
GT
1569 if (sort)
1570 {
1571 // Yes, this will be slow, but template lists
1572 // are typically short.
1573 int j;
1574 n = strings.Count();
1575 for (i = 0; i < n; i++)
1576 {
1577 for (j = 0; j < noTemplates; j++)
1578 {
1579 if (strings[i] == templates[j]->m_viewTypeName)
1580 data[i] = templates[j];
1581 }
1582 }
1583 }
1584
17260efd
VZ
1585 wxDocTemplate *theTemplate;
1586
1587 // the same logic as above
1588 switch ( n )
1589 {
1590 case 0:
1591 theTemplate = (wxDocTemplate *)NULL;
1592 break;
1593
1594 case 1:
1595 theTemplate = data[0];
1596 break;
1597
1598 default:
1599 theTemplate = (wxDocTemplate *)wxGetSingleChoiceData
1600 (
1601 _("Select a document view"),
1602 _("Views"),
1603 strings,
1604 (void **)data,
1605 wxFindSuitableParent()
1606 );
1607
1608 }
1609
0fb67cd1
VZ
1610 delete[] data;
1611 return theTemplate;
c801d85f
KB
1612}
1613
1614void wxDocManager::AssociateTemplate(wxDocTemplate *temp)
1615{
0fb67cd1
VZ
1616 if (!m_templates.Member(temp))
1617 m_templates.Append(temp);
c801d85f
KB
1618}
1619
1620void wxDocManager::DisassociateTemplate(wxDocTemplate *temp)
1621{
0fb67cd1 1622 m_templates.DeleteObject(temp);
c801d85f
KB
1623}
1624
1625// Add and remove a document from the manager's list
1626void wxDocManager::AddDocument(wxDocument *doc)
1627{
0fb67cd1
VZ
1628 if (!m_docs.Member(doc))
1629 m_docs.Append(doc);
c801d85f
KB
1630}
1631
1632void wxDocManager::RemoveDocument(wxDocument *doc)
1633{
0fb67cd1 1634 m_docs.DeleteObject(doc);
c801d85f
KB
1635}
1636
1637// Views or windows should inform the document manager
1638// when a view is going in or out of focus
1639void wxDocManager::ActivateView(wxView *view, bool activate, bool WXUNUSED(deleting))
1640{
0fb67cd1
VZ
1641 // If we're deactiving, and if we're not actually deleting the view, then
1642 // don't reset the current view because we may be going to
1643 // a window without a view.
1644 // WHAT DID I MEAN BY THAT EXACTLY?
1645 /*
1646 if (deleting)
1647 {
1648 if (m_currentView == view)
1649 m_currentView = NULL;
1650 }
1651 else
1652 */
1653 {
1654 if (activate)
1655 m_currentView = view;
1656 else
1657 m_currentView = (wxView *) NULL;
1658 }
c801d85f
KB
1659}
1660
0fb67cd1
VZ
1661// ----------------------------------------------------------------------------
1662// Default document child frame
1663// ----------------------------------------------------------------------------
c801d85f
KB
1664
1665BEGIN_EVENT_TABLE(wxDocChildFrame, wxFrame)
1666 EVT_ACTIVATE(wxDocChildFrame::OnActivate)
387a3b02 1667 EVT_CLOSE(wxDocChildFrame::OnCloseWindow)
c801d85f
KB
1668END_EVENT_TABLE()
1669
0fb67cd1
VZ
1670wxDocChildFrame::wxDocChildFrame(wxDocument *doc,
1671 wxView *view,
1672 wxFrame *frame,
1673 wxWindowID id,
1674 const wxString& title,
1675 const wxPoint& pos,
1676 const wxSize& size,
1677 long style,
1678 const wxString& name)
1679 : wxFrame(frame, id, title, pos, size, style, name)
1680{
1681 m_childDocument = doc;
1682 m_childView = view;
1683 if (view)
1684 view->SetFrame(this);
c801d85f
KB
1685}
1686
0fb67cd1 1687wxDocChildFrame::~wxDocChildFrame()
c801d85f
KB
1688{
1689}
1690
1691// Extend event processing to search the view's event table
1692bool wxDocChildFrame::ProcessEvent(wxEvent& event)
1693{
1694 if (m_childView)
1695 m_childView->Activate(TRUE);
1696
0fb67cd1 1697 if ( !m_childView || ! m_childView->ProcessEvent(event) )
c801d85f
KB
1698 {
1699 // Only hand up to the parent if it's a menu command
1700 if (!event.IsKindOf(CLASSINFO(wxCommandEvent)) || !GetParent() || !GetParent()->ProcessEvent(event))
0fb67cd1 1701 return wxEvtHandler::ProcessEvent(event);
c801d85f
KB
1702 else
1703 return TRUE;
1704 }
0fb67cd1
VZ
1705 else
1706 return TRUE;
c801d85f
KB
1707}
1708
c801d85f
KB
1709void wxDocChildFrame::OnActivate(wxActivateEvent& event)
1710{
0fb67cd1 1711 wxFrame::OnActivate(event);
c801d85f 1712
0fb67cd1
VZ
1713 if (m_childView)
1714 m_childView->Activate(event.GetActive());
c801d85f
KB
1715}
1716
387a3b02 1717void wxDocChildFrame::OnCloseWindow(wxCloseEvent& event)
c801d85f 1718{
0fb67cd1 1719 if (m_childView)
c801d85f 1720 {
0fb67cd1
VZ
1721 bool ans = FALSE;
1722 if (!event.CanVeto())
1723 ans = TRUE; // Must delete.
1724 else
1725 ans = m_childView->Close(FALSE); // FALSE means don't delete associated window
387a3b02 1726
0fb67cd1
VZ
1727 if (ans)
1728 {
1729 m_childView->Activate(FALSE);
1730 delete m_childView;
1731 m_childView = (wxView *) NULL;
1732 m_childDocument = (wxDocument *) NULL;
1733
1734 this->Destroy();
1735 }
1736 else
1737 event.Veto();
c801d85f 1738 }
387a3b02 1739 else
0fb67cd1 1740 event.Veto();
c801d85f
KB
1741}
1742
0fb67cd1
VZ
1743// ----------------------------------------------------------------------------
1744// Default parent frame
1745// ----------------------------------------------------------------------------
c801d85f
KB
1746
1747BEGIN_EVENT_TABLE(wxDocParentFrame, wxFrame)
1748 EVT_MENU(wxID_EXIT, wxDocParentFrame::OnExit)
f7bd2698 1749 EVT_MENU_RANGE(wxID_FILE1, wxID_FILE9, wxDocParentFrame::OnMRUFile)
387a3b02 1750 EVT_CLOSE(wxDocParentFrame::OnCloseWindow)
c801d85f
KB
1751END_EVENT_TABLE()
1752
0fb67cd1
VZ
1753wxDocParentFrame::wxDocParentFrame(wxDocManager *manager,
1754 wxFrame *frame,
1755 wxWindowID id,
1756 const wxString& title,
1757 const wxPoint& pos,
1758 const wxSize& size,
1759 long style,
1760 const wxString& name)
1761 : wxFrame(frame, id, title, pos, size, style, name)
c801d85f 1762{
0fb67cd1 1763 m_docManager = manager;
c801d85f
KB
1764}
1765
1766void wxDocParentFrame::OnExit(wxCommandEvent& WXUNUSED(event))
1767{
1768 Close();
1769}
1770
1771void wxDocParentFrame::OnMRUFile(wxCommandEvent& event)
1772{
1fc88785 1773 int n = event.GetId() - wxID_FILE1; // the index in MRU list
0c5d3e1c
VZ
1774 wxString filename(m_docManager->GetHistoryFile(n));
1775 if ( !filename.IsEmpty() )
1776 {
1777 // verify that the file exists before doing anything else
1778 if ( wxFile::Exists(filename) )
1779 {
1780 // try to open it
1781 (void)m_docManager->CreateDocument(filename, wxDOC_SILENT);
1782 }
1783 else
1784 {
1785 // remove the bogus filename from the MRU list and notify the user
1786 // about it
1787 m_docManager->RemoveFileFromHistory(n);
1788
9806a0e7 1789 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
1790 filename.c_str());
1791 }
1792 }
c801d85f
KB
1793}
1794
1795// Extend event processing to search the view's event table
1796bool wxDocParentFrame::ProcessEvent(wxEvent& event)
1797{
1798 // Try the document manager, then do default processing
1799 if (!m_docManager || !m_docManager->ProcessEvent(event))
1800 return wxEvtHandler::ProcessEvent(event);
1801 else
1802 return TRUE;
1803}
1804
c801d85f
KB
1805// Define the behaviour for the frame closing
1806// - must delete all frames except for the main one.
387a3b02 1807void wxDocParentFrame::OnCloseWindow(wxCloseEvent& event)
c801d85f 1808{
0fb67cd1
VZ
1809 if (m_docManager->Clear(!event.CanVeto()))
1810 {
1811 this->Destroy();
1812 }
1813 else
1814 event.Veto();
c801d85f
KB
1815}
1816
47d67540 1817#if wxUSE_PRINTING_ARCHITECTURE
c801d85f 1818
0fb67cd1 1819wxDocPrintout::wxDocPrintout(wxView *view, const wxString& title)
e90c1d2a 1820 : wxPrintout(title)
c801d85f 1821{
0fb67cd1 1822 m_printoutView = view;
c801d85f
KB
1823}
1824
1825bool wxDocPrintout::OnPrintPage(int WXUNUSED(page))
1826{
0fb67cd1
VZ
1827 wxDC *dc = GetDC();
1828
1829 // Get the logical pixels per inch of screen and printer
1830 int ppiScreenX, ppiScreenY;
1831 GetPPIScreen(&ppiScreenX, &ppiScreenY);
1832 int ppiPrinterX, ppiPrinterY;
1833 GetPPIPrinter(&ppiPrinterX, &ppiPrinterY);
1834
1835 // This scales the DC so that the printout roughly represents the
1836 // the screen scaling. The text point size _should_ be the right size
1837 // but in fact is too small for some reason. This is a detail that will
1838 // need to be addressed at some point but can be fudged for the
1839 // moment.
1840 float scale = (float)((float)ppiPrinterX/(float)ppiScreenX);
1841
1842 // Now we have to check in case our real page size is reduced
1843 // (e.g. because we're drawing to a print preview memory DC)
1844 int pageWidth, pageHeight;
1845 int w, h;
1846 dc->GetSize(&w, &h);
1847 GetPageSizePixels(&pageWidth, &pageHeight);
1848
1849 // If printer pageWidth == current DC width, then this doesn't
1850 // change. But w might be the preview bitmap width, so scale down.
1851 float overallScale = scale * (float)(w/(float)pageWidth);
1852 dc->SetUserScale(overallScale, overallScale);
1853
1854 if (m_printoutView)
1855 {
1856 m_printoutView->OnDraw(dc);
1857 }
1858 return TRUE;
c801d85f
KB
1859}
1860
1861bool wxDocPrintout::HasPage(int pageNum)
1862{
0fb67cd1 1863 return (pageNum == 1);
c801d85f
KB
1864}
1865
1866bool wxDocPrintout::OnBeginDocument(int startPage, int endPage)
1867{
0fb67cd1
VZ
1868 if (!wxPrintout::OnBeginDocument(startPage, endPage))
1869 return FALSE;
c801d85f 1870
0fb67cd1 1871 return TRUE;
c801d85f
KB
1872}
1873
1874void wxDocPrintout::GetPageInfo(int *minPage, int *maxPage, int *selPageFrom, int *selPageTo)
1875{
0fb67cd1
VZ
1876 *minPage = 1;
1877 *maxPage = 1;
1878 *selPageFrom = 1;
1879 *selPageTo = 1;
c801d85f
KB
1880}
1881
0fb67cd1 1882#endif // wxUSE_PRINTING_ARCHITECTURE
c801d85f 1883
0fb67cd1
VZ
1884// ----------------------------------------------------------------------------
1885// File history processor
1886// ----------------------------------------------------------------------------
c801d85f
KB
1887
1888wxFileHistory::wxFileHistory(int maxFiles)
1889{
0fb67cd1
VZ
1890 m_fileMaxFiles = maxFiles;
1891 m_fileHistoryN = 0;
50920146 1892 m_fileHistory = new wxChar *[m_fileMaxFiles];
c801d85f
KB
1893}
1894
0fb67cd1 1895wxFileHistory::~wxFileHistory()
c801d85f 1896{
0fb67cd1
VZ
1897 int i;
1898 for (i = 0; i < m_fileHistoryN; i++)
1899 delete[] m_fileHistory[i];
1900 delete[] m_fileHistory;
c801d85f
KB
1901}
1902
1903// File history management
1904void wxFileHistory::AddFileToHistory(const wxString& file)
1905{
0fb67cd1 1906 int i;
02718e6a 1907
0fb67cd1
VZ
1908 // Check we don't already have this file
1909 for (i = 0; i < m_fileHistoryN; i++)
7f555861 1910 {
02718e6a
VZ
1911 if ( m_fileHistory[i] && (file == m_fileHistory[i]) )
1912 {
1913 // we do have it, move it to the top of the history
1914 RemoveFileFromHistory (i);
1915 AddFileToHistory (file);
0fb67cd1 1916 return;
02718e6a 1917 }
7f555861 1918 }
0fb67cd1 1919
02718e6a
VZ
1920 // if we already have a full history, delete the one at the end
1921 if ( m_fileMaxFiles == m_fileHistoryN )
0fb67cd1 1922 {
02718e6a
VZ
1923 RemoveFileFromHistory (m_fileHistoryN - 1);
1924 AddFileToHistory (file);
1925 return;
c801d85f 1926 }
02718e6a
VZ
1927
1928 // Add to the project file history:
1929 // Move existing files (if any) down so we can insert file at beginning.
0fb67cd1
VZ
1930 if (m_fileHistoryN < m_fileMaxFiles)
1931 {
1932 wxNode* node = m_fileMenus.First();
1933 while (node)
1934 {
1935 wxMenu* menu = (wxMenu*) node->Data();
1936 if (m_fileHistoryN == 0)
1937 menu->AppendSeparator();
1938 menu->Append(wxID_FILE1+m_fileHistoryN, _("[EMPTY]"));
1939 node = node->Next();
1940 }
1941 m_fileHistoryN ++;
1942 }
1943 // Shuffle filenames down
1944 for (i = (m_fileHistoryN-1); i > 0; i--)
1945 {
1946 m_fileHistory[i] = m_fileHistory[i-1];
1947 }
1948 m_fileHistory[0] = copystring(file);
1949
02718e6a
VZ
1950 // this is the directory of the last opened file
1951 wxString pathCurrent;
1952 wxSplitPath( m_fileHistory[0], &pathCurrent, NULL, NULL );
0fb67cd1 1953 for (i = 0; i < m_fileHistoryN; i++)
02718e6a
VZ
1954 {
1955 if ( m_fileHistory[i] )
0fb67cd1 1956 {
02718e6a
VZ
1957 // if in same directory just show the filename; otherwise the full
1958 // path
1959 wxString pathInMenu, path, filename, ext;
1960 wxSplitPath( m_fileHistory[i], &path, &filename, &ext );
1961 if ( path == pathCurrent )
1962 {
1963 pathInMenu = filename;
1964 if ( !ext.empty() )
1965 pathInMenu = pathInMenu + wxFILE_SEP_EXT + ext;
1966 }
1967 else
1968 {
1969 // absolute path; could also set relative path
1970 pathInMenu = m_fileHistory[i];
1971 }
1972
0fb67cd1 1973 wxString buf;
02718e6a 1974 buf.Printf(s_MRUEntryFormat, i + 1, pathInMenu.c_str());
0fb67cd1
VZ
1975 wxNode* node = m_fileMenus.First();
1976 while (node)
1977 {
1978 wxMenu* menu = (wxMenu*) node->Data();
02718e6a 1979 menu->SetLabel(wxID_FILE1 + i, buf);
0fb67cd1
VZ
1980 node = node->Next();
1981 }
1982 }
02718e6a 1983 }
c801d85f
KB
1984}
1985
0c5d3e1c
VZ
1986void wxFileHistory::RemoveFileFromHistory(int i)
1987{
1988 wxCHECK_RET( i < m_fileHistoryN,
223d09f6 1989 wxT("invalid index in wxFileHistory::RemoveFileFromHistory") );
0c5d3e1c 1990
0c5d3e1c
VZ
1991 // delete the element from the array (could use memmove() too...)
1992 delete [] m_fileHistory[i];
1993
1994 int j;
1995 for ( j = i; j < m_fileHistoryN - 1; j++ )
1996 {
1997 m_fileHistory[j] = m_fileHistory[j + 1];
1998 }
1999
00e6d8ab
VZ
2000 wxNode* node = m_fileMenus.First();
2001 while ( node )
2002 {
2003 wxMenu* menu = (wxMenu*) node->Data();
2004
2005
0c5d3e1c
VZ
2006 // shuffle filenames up
2007 wxString buf;
2008 for ( j = i; j < m_fileHistoryN - 1; j++ )
2009 {
2010 buf.Printf(s_MRUEntryFormat, j + 1, m_fileHistory[j]);
2011 menu->SetLabel(wxID_FILE1 + j, buf);
2012 }
2013
0c5d3e1c 2014 node = node->Next();
2b273975
VZ
2015
2016 // delete the last menu item which is unused now
00e6d8ab 2017 if (menu->FindItem(wxID_FILE1 + m_fileHistoryN - 1))
2b273975
VZ
2018 menu->Delete(wxID_FILE1 + m_fileHistoryN - 1);
2019
717a57c2
VZ
2020 // delete the last separator too if no more files are left
2021 if ( m_fileHistoryN == 1 )
2022 {
2023 wxMenuItemList::Node *node = menu->GetMenuItems().GetLast();
2024 if ( node )
2025 {
2026 wxMenuItem *menuItem = node->GetData();
2027 if ( menuItem->IsSeparator() )
2028 {
2029 menu->Delete(menuItem);
2030 }
2031 //else: should we search backwards for the last separator?
2032 }
2033 //else: menu is empty somehow
2034 }
0c5d3e1c 2035 }
2b273975 2036
0c5d3e1c
VZ
2037 m_fileHistoryN--;
2038}
2039
c801d85f
KB
2040wxString wxFileHistory::GetHistoryFile(int i) const
2041{
8ee9d618
VZ
2042 wxString s;
2043 if ( i < m_fileHistoryN )
2044 {
2045 s = m_fileHistory[i];
2046 }
0fb67cd1 2047 else
8ee9d618
VZ
2048 {
2049 wxFAIL_MSG( wxT("bad index in wxFileHistory::GetHistoryFile") );
2050 }
2051
2052 return s;
c801d85f
KB
2053}
2054
7f555861 2055void wxFileHistory::UseMenu(wxMenu *menu)
c801d85f 2056{
0fb67cd1
VZ
2057 if (!m_fileMenus.Member(menu))
2058 m_fileMenus.Append(menu);
c801d85f
KB
2059}
2060
7f555861
JS
2061void wxFileHistory::RemoveMenu(wxMenu *menu)
2062{
0fb67cd1 2063 m_fileMenus.DeleteObject(menu);
7f555861
JS
2064}
2065
702ca7c0 2066#if wxUSE_CONFIG
7f555861 2067void wxFileHistory::Load(wxConfigBase& config)
c801d85f 2068{
0fb67cd1
VZ
2069 m_fileHistoryN = 0;
2070 wxString buf;
223d09f6 2071 buf.Printf(wxT("file%d"), m_fileHistoryN+1);
0fb67cd1 2072 wxString historyFile;
223d09f6 2073 while ((m_fileHistoryN <= m_fileMaxFiles) && config.Read(buf, &historyFile) && (historyFile != wxT("")))
0fb67cd1 2074 {
50920146 2075 m_fileHistory[m_fileHistoryN] = copystring((const wxChar*) historyFile);
0fb67cd1 2076 m_fileHistoryN ++;
223d09f6 2077 buf.Printf(wxT("file%d"), m_fileHistoryN+1);
58c837a4 2078 historyFile = wxT("");
0fb67cd1
VZ
2079 }
2080 AddFilesToMenu();
c801d85f
KB
2081}
2082
7f555861 2083void wxFileHistory::Save(wxConfigBase& config)
c801d85f 2084{
0fb67cd1
VZ
2085 int i;
2086 for (i = 0; i < m_fileHistoryN; i++)
2087 {
2088 wxString buf;
223d09f6 2089 buf.Printf(wxT("file%d"), i+1);
0fb67cd1
VZ
2090 config.Write(buf, wxString(m_fileHistory[i]));
2091 }
7f555861 2092}
0fb67cd1 2093#endif // wxUSE_CONFIG
7f555861
JS
2094
2095void wxFileHistory::AddFilesToMenu()
2096{
2097 if (m_fileHistoryN > 0)
2098 {
2099 wxNode* node = m_fileMenus.First();
2100 while (node)
2101 {
2102 wxMenu* menu = (wxMenu*) node->Data();
2103 menu->AppendSeparator();
2104 int i;
2105 for (i = 0; i < m_fileHistoryN; i++)
2106 {
2107 if (m_fileHistory[i])
2108 {
2109 wxString buf;
0c5d3e1c 2110 buf.Printf(s_MRUEntryFormat, i+1, m_fileHistory[i]);
7f555861
JS
2111 menu->Append(wxID_FILE1+i, buf);
2112 }
2113 }
2114 node = node->Next();
2115 }
2116 }
2117}
2118
2119void wxFileHistory::AddFilesToMenu(wxMenu* menu)
2120{
2121 if (m_fileHistoryN > 0)
2122 {
2123 menu->AppendSeparator();
2124 int i;
2125 for (i = 0; i < m_fileHistoryN; i++)
2126 {
2127 if (m_fileHistory[i])
2128 {
2129 wxString buf;
0c5d3e1c 2130 buf.Printf(s_MRUEntryFormat, i+1, m_fileHistory[i]);
7f555861
JS
2131 menu->Append(wxID_FILE1+i, buf);
2132 }
2133 }
2134 }
c801d85f
KB
2135}
2136
0fb67cd1
VZ
2137// ----------------------------------------------------------------------------
2138// Permits compatibility with existing file formats and functions that
2139// manipulate files directly
2140// ----------------------------------------------------------------------------
c801d85f 2141
a533f5c1 2142#if wxUSE_STD_IOSTREAM
dd107c50 2143bool wxTransferFileToStream(const wxString& filename, wxSTD ostream& stream)
c801d85f 2144{
0fb67cd1
VZ
2145 FILE *fd1;
2146 int ch;
c801d85f 2147
f6bcfd97 2148 if ((fd1 = wxFopen (filename.fn_str(), _T("rb"))) == NULL)
0fb67cd1 2149 return FALSE;
c801d85f 2150
0fb67cd1
VZ
2151 while ((ch = getc (fd1)) != EOF)
2152 stream << (unsigned char)ch;
c801d85f 2153
0fb67cd1
VZ
2154 fclose (fd1);
2155 return TRUE;
c801d85f
KB
2156}
2157
dd107c50 2158bool wxTransferStreamToFile(wxSTD istream& stream, const wxString& filename)
c801d85f 2159{
0fb67cd1
VZ
2160 FILE *fd1;
2161 int ch;
c801d85f 2162
f6bcfd97 2163 if ((fd1 = wxFopen (filename.fn_str(), _T("wb"))) == NULL)
c801d85f 2164 {
0fb67cd1 2165 return FALSE;
c801d85f
KB
2166 }
2167
0fb67cd1
VZ
2168 while (!stream.eof())
2169 {
2170 ch = stream.get();
2171 if (!stream.eof())
2172 putc (ch, fd1);
2173 }
2174 fclose (fd1);
2175 return TRUE;
c801d85f 2176}
dc1efb1d
JS
2177#else
2178bool wxTransferFileToStream(const wxString& filename, wxOutputStream& stream)
2179{
2180 FILE *fd1;
2181 int ch;
2182
888b0f22 2183 if ((fd1 = wxFopen (filename, wxT("rb"))) == NULL)
dc1efb1d
JS
2184 return FALSE;
2185
2186 while ((ch = getc (fd1)) != EOF)
2187 stream.PutC((char) ch);
2188
2189 fclose (fd1);
2190 return TRUE;
2191}
2192
2193bool wxTransferStreamToFile(wxInputStream& stream, const wxString& filename)
2194{
2195 FILE *fd1;
2196 char ch;
2197
888b0f22 2198 if ((fd1 = wxFopen (filename, wxT("wb"))) == NULL)
dc1efb1d
JS
2199 {
2200 return FALSE;
2201 }
2202
2203 int len = stream.StreamSize();
2204 // TODO: is this the correct test for EOF?
2205 while (stream.TellI() < (len - 1))
2206 {
2207 ch = stream.GetC();
2208 putc (ch, fd1);
2209 }
2210 fclose (fd1);
2211 return TRUE;
2212}
a533f5c1 2213#endif
c801d85f 2214
0fb67cd1
VZ
2215#endif // wxUSE_DOC_VIEW_ARCHITECTURE
2216