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