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