From acbb1c9b8ca23e5a8141d80bd32491ec6aac008e Mon Sep 17 00:00:00 2001 From: Abdelrazak Younes Date: Fri, 7 Dec 2007 11:57:13 +0000 Subject: [PATCH] Split LyXFunc::menuNew() into LyXView::newDocument() and buffer_funcs::newUnnamedFile(). git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@22002 a592a061-630c-0410-9148-cb99ea01b6c8 --- src/LyXFunc.cpp | 56 ++--------------------------------- src/LyXFunc.h | 3 -- src/buffer_funcs.cpp | 19 ++++++++++++ src/buffer_funcs.h | 6 ++++ src/frontends/LyXView.h | 3 ++ src/frontends/qt4/GuiView.cpp | 48 ++++++++++++++++++++++++++++++ src/frontends/qt4/GuiView.h | 2 ++ 7 files changed, 80 insertions(+), 57 deletions(-) diff --git a/src/LyXFunc.cpp b/src/LyXFunc.cpp index e59c4ba12d..c8f5457cee 100644 --- a/src/LyXFunc.cpp +++ b/src/LyXFunc.cpp @@ -901,12 +901,12 @@ void LyXFunc::dispatch(FuncRequest const & cmd) // --- Menus ----------------------------------------------- case LFUN_BUFFER_NEW: - menuNew(argument, false); + lyx_view_->newDocument(argument, false); updateFlags = Update::None; break; case LFUN_BUFFER_NEW_TEMPLATE: - menuNew(argument, true); + lyx_view_->newDocument(argument, true); updateFlags = Update::None; break; @@ -1868,58 +1868,6 @@ void LyXFunc::sendDispatchMessage(docstring const & msg, FuncRequest const & cmd } -void LyXFunc::menuNew(string const & name, bool fromTemplate) -{ - // FIXME: initpath is not used. What to do? - string initpath = lyxrc.document_path; - string filename(name); - - if (lyx_view_->buffer()) { - string const trypath = lyx_view_->buffer()->filePath(); - // If directory is writeable, use this as default. - if (FileName(trypath).isDirWritable()) - initpath = trypath; - } - - static int newfile_number; - - if (filename.empty()) { - filename = addName(lyxrc.document_path, - "newfile" + convert(++newfile_number) + ".lyx"); - while (theBufferList().exists(filename) || - FileName(filename).isReadableFile()) { - ++newfile_number; - filename = addName(lyxrc.document_path, - "newfile" + convert(newfile_number) + - ".lyx"); - } - } - - // The template stuff - string templname; - if (fromTemplate) { - FileDialog dlg(_("Select template file")); - dlg.setButton1(_("Documents|#o#O"), from_utf8(lyxrc.document_path)); - dlg.setButton1(_("Templates|#T#t"), from_utf8(lyxrc.template_path)); - - FileDialog::Result result = - dlg.open(from_utf8(lyxrc.template_path), - FileFilterList(_("LyX Documents (*.lyx)")), - docstring()); - - if (result.first == FileDialog::Later) - return; - if (result.second.empty()) - return; - templname = to_utf8(result.second); - } - - Buffer * const b = newFile(filename, templname, !name.empty()); - if (b) - lyx_view_->setBuffer(b); -} - - Buffer * LyXFunc::loadAndViewFile(FileName const & filename, bool tolastfiles) { lyx_view_->setBusy(true); diff --git a/src/LyXFunc.h b/src/LyXFunc.h index bb5cab78ac..5914c53c66 100644 --- a/src/LyXFunc.h +++ b/src/LyXFunc.h @@ -120,9 +120,6 @@ private: void sendDispatchMessage(docstring const & msg, FuncRequest const & ev); - // I think the following should be moved to BufferView. (Asger) - /// - void menuNew(std::string const & argument, bool fromTemplate); /// void open(std::string const &); /// diff --git a/src/buffer_funcs.cpp b/src/buffer_funcs.cpp index 04d0e3a9ac..a76a2a9952 100644 --- a/src/buffer_funcs.cpp +++ b/src/buffer_funcs.cpp @@ -44,6 +44,7 @@ #include "insets/InsetBibitem.h" #include "insets/InsetInclude.h" +#include "support/convert.h" #include "support/debug.h" #include "support/filetools.h" #include "support/gettext.h" @@ -55,6 +56,7 @@ namespace lyx { using namespace std; +using support::addName; using support::bformat; using support::FileName; using support::libFileSearch; @@ -146,6 +148,23 @@ Buffer * newFile(string const & filename, string const & templatename, } +Buffer * newUnnamedFile(string const & templatename, FileName const & path) +{ + static int newfile_number; + + string document_path = path.absFilename(); + string filename = addName(document_path, + "newfile" + convert(++newfile_number) + ".lyx"); + while (theBufferList().exists(filename) + || FileName(filename).isReadableFile()) { + ++newfile_number; + filename = addName(document_path, + "newfile" + convert(newfile_number) + ".lyx"); + } + return newFile(filename, templatename, false); +} + + int countWords(DocIterator const & from, DocIterator const & to) { int count = 0; diff --git a/src/buffer_funcs.h b/src/buffer_funcs.h index 389b6c9f81..061dc80840 100644 --- a/src/buffer_funcs.h +++ b/src/buffer_funcs.h @@ -35,6 +35,12 @@ Buffer * checkAndLoadLyXFile(support::FileName const & filename); Buffer * newFile(std::string const & filename, std::string const & templatename, bool isNamed = false); +/** Make a new unnamed file (buffer) based on a template + * named \c templatename + */ +Buffer * newUnnamedFile(std::string const & templatename, + support::FileName const & path); + /// Count the number of words in the text between these two iterators int countWords(DocIterator const & from, DocIterator const & to); diff --git a/src/frontends/LyXView.h b/src/frontends/LyXView.h index 61f8e4a202..db9f33fb20 100644 --- a/src/frontends/LyXView.h +++ b/src/frontends/LyXView.h @@ -67,6 +67,9 @@ public: virtual void setBuffer(Buffer * b) = 0; ///< \c Buffer to set. /// virtual bool closeBuffer() = 0; + /// + virtual void newDocument(std::string const & filename, + bool fromTemplate) = 0; //@} diff --git a/src/frontends/qt4/GuiView.cpp b/src/frontends/qt4/GuiView.cpp index 0e16d88a56..3353e885c9 100644 --- a/src/frontends/qt4/GuiView.cpp +++ b/src/frontends/qt4/GuiView.cpp @@ -1032,6 +1032,54 @@ FuncStatus GuiView::getStatus(FuncRequest const & cmd) } +static FileName selectTemplateFile() +{ + FileDialog dlg(_("Select template file")); + dlg.setButton1(_("Documents|#o#O"), from_utf8(lyxrc.document_path)); + dlg.setButton1(_("Templates|#T#t"), from_utf8(lyxrc.template_path)); + + FileDialog::Result result = + dlg.open(from_utf8(lyxrc.template_path), + FileFilterList(_("LyX Documents (*.lyx)")), + docstring()); + + if (result.first == FileDialog::Later) + return FileName(); + if (result.second.empty()) + return FileName(); + return FileName(to_utf8(result.second)); +} + + +void GuiView::newDocument(string const & filename, bool from_template) +{ + FileName initpath; + Buffer * buf = buffer(); + if (buf) { + FileName const trypath(buf->filePath()); + // If directory is writeable, use this as default. + if (trypath.isDirWritable()) + initpath = trypath; + } else + initpath.set(lyxrc.document_path); + + // FIXME: Up to now initpath was unconditionally set to the user document + // path. Is it what we want? If yes, erase the code above. + initpath.set(lyxrc.document_path); + + string templatefile = from_template ? + selectTemplateFile().absFilename() : string(); + Buffer * b; + if (filename.empty()) + b = newUnnamedFile(templatefile, initpath); + else + b = newFile(filename, templatefile, true); + + if (b) + setBuffer(b); +} + + void GuiView::insertLyXFile(docstring const & fname) { BufferView * bv = view(); diff --git a/src/frontends/qt4/GuiView.h b/src/frontends/qt4/GuiView.h index e87f66f265..d73acbe8eb 100644 --- a/src/frontends/qt4/GuiView.h +++ b/src/frontends/qt4/GuiView.h @@ -89,6 +89,8 @@ public: void setBuffer(Buffer * b); ///< \c Buffer to set. /// bool closeBuffer(); + /// + void newDocument(std::string const & filename, bool fromTemplate); /// write all buffers, asking the user, returns false if cancelled bool quitWriteAll(); -- 2.39.5