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