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