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