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