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