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