// /// Displays FileSave dialog prompting the user to select a file name for savinng the document. /// Filters out read-only files. // bool TDocManager::SelectSave(TDocument& doc) { TDocTemplate* tpl = doc.GetTemplate(); if (!tpl || !tpl->GetFileFilter()) return false; tchar filepath[_MAX_PATH]; if (doc.GetDocPath()) ::_tcscpy(filepath, doc.GetDocPath()); else filepath[0] = 0; // no initial file path int index = SelectDocPath(&tpl, 1, filepath, COUNTOF(filepath), 0, true, &doc); return index ? doc.SetDocPath(filepath) : false; }
// /// Given a list of templates, prompts the user to select one of the templates to use for the file to be /// opened. Returns the template index used for the selection, or 0 if unsuccessful. /// For a file open operation, save is false. For a file save operation, save is /// true. This function can be overridden to provide a customized user interface. /// /// \note This is Windows-specific, using the system-provided file open dialog box // int TDocManager::SelectDocPath(TDocTemplate** tpllist, int tplcount, LPTSTR path, int buflen, long flags, bool save, TDocument* doc) { // Compute length of description(s) and filter(s) // int len = GetTemplateDescription(tpllist, tplcount); // Put together a string of description and filters // TAPointer<tchar> filtbuf(new tchar[++len]); GetTemplateDescription(tpllist, tplcount, filtbuf, len); // Find the (default) template to select // int index, count; TDocument* curDoc = save ? (doc ? doc : GetCurrentDoc()) : 0; CHECK(!save || curDoc); if (save) { for (index = count = 0; count < tplcount; count++) { if (tpllist[count] == curDoc->GetTemplate()) { index = count; break; } } } else { for (index = count = 0; count < tplcount; count++) { if (tpllist[count]->IsFlagSet(dtSelected)) { index = count; break; } } } // Initialize data structure used for launching Common Dialog // flags = (tpllist[index]->GetFlags() | flags); flags &= 0x000FFFFF; // Clear Doc/View related flags. flags |= AdditionalFileDialogFlags; // Add extended flags, e.g. OFN_ENABLESIZING. flags &= ~dtProhibited; // Clear unsupported flags, e.g. OFN_ENABLETEMPLATE. TDvOpenSaveData data(flags, // flags filtbuf, // filter CONST_CAST(LPTSTR, tpllist[index]->GetDirectory()), // initDir. CONST_CAST(LPTSTR, tpllist[index]->GetDefaultExt()), // defExt. index ? index+1 : 0, // filterIndex tpllist, // template list tplcount); // template count //--- Sirma (Krasi) { LPCTSTR fName = path && *path ? path : (doc ? doc->GetTitle() : 0); if (fName && *fName) _tcsncpy(data.FileName, fName, buflen); } //--- Sirma (Krasi) --- // Execute dialog // int result; TWindow* parent = GetApplication()->GetMainWindow(); if (save) result = TDvFileSaveDialog(parent, data).Execute(); else result = TDvFileOpenDialog(parent, data).Execute(); // Dialog was cancelled! // if (result != IDOK) { WARNX(OwlDocView, data.Error != 0, 0, _T("Common dialog error: ") << \ data.Error << _T(" in SelectDocPath()") ); return 0; } // !BB // Here, there's a major dilemma! How, do we know the user did not // mislead us? For example, the user may have selected the *wrong* // template for saving a particular document... This is crucial when // saving documents!!! // // Update templates to 'remember' the template last used // for (count = 0; count < tplcount; count++) { if (count == index-1) tpllist[count]->SetFlag(dtSelected); else tpllist[count]->ClearFlag(dtSelected); } // Update selected template with directory // // // !BB Yeah! Where's the meat? // Copy file name over // CHECK(path); if (data.FileName && data.FileName[0]) { WARNX(OwlDocView, ::_tcslen(data.FileName) < (size_t)buflen, 0, _T("SelectDocPath: Specified buffer is too small")); _tcsncpy(path, data.FileName, buflen); } else *path = 0; // Return index [1-based] of selected template // CHECK(data.FilterIndex >= 1); CHECK(data.FilterIndex <= tplcount); return data.FilterIndex; }