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
This commit is contained in:
Abdelrazak Younes 2007-12-07 11:57:13 +00:00
parent 863aebb177
commit acbb1c9b8c
7 changed files with 80 additions and 57 deletions

View File

@ -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<string>(++newfile_number) + ".lyx");
while (theBufferList().exists(filename) ||
FileName(filename).isReadableFile()) {
++newfile_number;
filename = addName(lyxrc.document_path,
"newfile" + convert<string>(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);

View File

@ -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 &);
///

View File

@ -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<string>(++newfile_number) + ".lyx");
while (theBufferList().exists(filename)
|| FileName(filename).isReadableFile()) {
++newfile_number;
filename = addName(document_path,
"newfile" + convert<string>(newfile_number) + ".lyx");
}
return newFile(filename, templatename, false);
}
int countWords(DocIterator const & from, DocIterator const & to)
{
int count = 0;

View File

@ -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);

View File

@ -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;
//@}

View File

@ -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();

View File

@ -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();