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