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