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