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