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