Transfer some GUI oriented code from core to frontend.

* Application/GuiApplication:
- restoreGuiSession(): new method for session restoration.

* LyXView/GuiView
- loadDocument(): new pure virtual interface

* lyxFunc:
- loadAndViewFile(): transfered to GuiView::loadDocument()
- openDocument(): transfered to GuiView.

* LyX.h
- Impl::files_to_load_ : now a simple vector<string>.
- restoreGuiSession(): removed.
- execBatchCommands(): simplified and make use of Application::restoreGuiSession().



git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@22816 a592a061-630c-0410-9148-cb99ea01b6c8
This commit is contained in:
Abdelrazak Younes 2008-02-06 23:02:57 +00:00
parent 5627a35790
commit 8d99df0c93
10 changed files with 192 additions and 183 deletions

View File

@ -147,7 +147,7 @@ struct LyX::Impl
boost::scoped_ptr<Session> session_; boost::scoped_ptr<Session> session_;
/// Files to load at start. /// Files to load at start.
vector<FileName> files_to_load_; vector<string> files_to_load_;
/// The messages translators. /// The messages translators.
map<string, Messages> messages_; map<string, Messages> messages_;
@ -525,24 +525,22 @@ int LyX::init(int & argc, char * argv[])
if (!success) if (!success)
return EXIT_FAILURE; return EXIT_FAILURE;
for (int argi = argc - 1; argi >= 1; --argi) { // Remaining arguments are assumed to be files to load.
// get absolute path of file and add ".lyx" to for (int argi = argc - 1; argi >= 1; --argi)
// the filename if necessary pimpl_->files_to_load_.push_back(to_utf8(from_local8bit(argv[argi])));
pimpl_->files_to_load_.push_back(fileSearch(string(),
os::internal_path(to_utf8(from_local8bit(argv[argi]))),
"lyx", may_not_exist));
}
if (first_start) if (first_start) {
pimpl_->files_to_load_.push_back(i18nLibFileSearch("examples", "splash.lyx")); pimpl_->files_to_load_.push_back(
i18nLibFileSearch("examples", "splash.lyx").absFilename());
}
return EXIT_SUCCESS; return EXIT_SUCCESS;
} }
void LyX::addFileToLoad(FileName const & fname) void LyX::addFileToLoad(string const & fname)
{ {
vector<FileName>::const_iterator cit = find( vector<string>::const_iterator cit = find(
pimpl_->files_to_load_.begin(), pimpl_->files_to_load_.end(), pimpl_->files_to_load_.begin(), pimpl_->files_to_load_.end(),
fname); fname);
@ -553,15 +551,20 @@ void LyX::addFileToLoad(FileName const & fname)
void LyX::loadFiles() void LyX::loadFiles()
{ {
vector<FileName>::const_iterator it = pimpl_->files_to_load_.begin(); vector<string>::const_iterator it = pimpl_->files_to_load_.begin();
vector<FileName>::const_iterator end = pimpl_->files_to_load_.end(); vector<string>::const_iterator end = pimpl_->files_to_load_.end();
for (; it != end; ++it) { for (; it != end; ++it) {
if (it->empty()) // get absolute path of file and add ".lyx" to
// the filename if necessary
FileName fname = fileSearch(string(), os::internal_path(*it), "lyx",
may_not_exist);
if (fname.empty())
continue; continue;
Buffer * buf = pimpl_->buffer_list_.newBuffer(it->absFilename(), false); Buffer * buf = pimpl_->buffer_list_.newBuffer(fname.absFilename(), false);
if (buf->loadLyXFile(*it)) { if (buf->loadLyXFile(fname)) {
ErrorList const & el = buf->errorList("Parse"); ErrorList const & el = buf->errorList("Parse");
if (!el.empty()) if (!el.empty())
for_each(el.begin(), el.end(), for_each(el.begin(), el.end(),
@ -578,10 +581,9 @@ void LyX::execBatchCommands()
// The advantage of doing this here is that the event loop // The advantage of doing this here is that the event loop
// is already started. So any need for interaction will be // is already started. So any need for interaction will be
// aknowledged. // aknowledged.
restoreGuiSession();
// if reconfiguration is needed. // if reconfiguration is needed.
if (textclasslist.empty()) { while (textclasslist.empty()) {
switch (Alert::prompt( switch (Alert::prompt(
_("No textclass is found"), _("No textclass is found"),
_("LyX cannot continue because no textclass is found. " _("LyX cannot continue because no textclass is found. "
@ -601,10 +603,36 @@ void LyX::execBatchCommands()
pimpl_->lyxfunc_.dispatch(FuncRequest(LFUN_RECONFIGURE, pimpl_->lyxfunc_.dispatch(FuncRequest(LFUN_RECONFIGURE,
" --without-latex-config")); " --without-latex-config"));
break; break;
} default:
pimpl_->lyxfunc_.dispatch(FuncRequest(LFUN_LYX_QUIT)); pimpl_->lyxfunc_.dispatch(FuncRequest(LFUN_LYX_QUIT));
return; return;
} }
}
// create the first main window
pimpl_->lyxfunc_.dispatch(FuncRequest(LFUN_WINDOW_NEW, geometryArg));
if (!pimpl_->files_to_load_.empty()) {
// if some files were specified at command-line we assume that the
// user wants to edit *these* files and not to restore the session.
for (size_t i = 0; i != pimpl_->files_to_load_.size(); ++i) {
pimpl_->lyxfunc_.dispatch(
FuncRequest(LFUN_FILE_OPEN, pimpl_->files_to_load_[i]));
}
// clear this list to save a few bytes of RAM
pimpl_->files_to_load_.clear();
}
else
pimpl_->application_->restoreGuiSession();
BufferList::iterator I = theBufferList().begin();
BufferList::iterator end = theBufferList().end();
for (; I != end; ++I) {
Buffer * buf = *I;
if (buf != buf->masterBuffer())
continue;
updateLabels(*buf);
}
// Execute batch commands if available // Execute batch commands if available
if (pimpl_->batch_command.empty()) if (pimpl_->batch_command.empty())
@ -616,47 +644,6 @@ void LyX::execBatchCommands()
} }
void LyX::restoreGuiSession()
{
// create the main window
pimpl_->lyxfunc_.dispatch(FuncRequest(LFUN_WINDOW_NEW, geometryArg));
// if there is no valid class list, do not load any file.
if (textclasslist.empty())
return;
// if some files were specified at command-line we assume that the
// user wants to edit *these* files and not to restore the session.
if (!pimpl_->files_to_load_.empty()) {
for_each(pimpl_->files_to_load_.begin(),
pimpl_->files_to_load_.end(),
bind(&LyXFunc::loadAndViewFile, pimpl_->lyxfunc_, _1, true));
// clear this list to save a few bytes of RAM
pimpl_->files_to_load_.clear();
pimpl_->session_->lastOpened().clear();
} else if (lyxrc.load_session) {
vector<FileName> const & lastopened = pimpl_->session_->lastOpened().getfiles();
// do not add to the lastfile list since these files are restored from
// last session, and should be already there (regular files), or should
// not be added at all (help files).
for_each(lastopened.begin(), lastopened.end(),
bind(&LyXFunc::loadAndViewFile, pimpl_->lyxfunc_, _1, false));
// clear this list to save a few bytes of RAM
pimpl_->session_->lastOpened().clear();
}
BufferList::iterator I = pimpl_->buffer_list_.begin();
BufferList::iterator end = pimpl_->buffer_list_.end();
for (; I != end; ++I) {
Buffer * buf = *I;
if (buf != buf->masterBuffer())
continue;
updateLabels(*buf);
}
}
/* /*
Signals and Windows Signals and Windows
=================== ===================

View File

@ -110,7 +110,7 @@ public:
void execBatchCommands(); void execBatchCommands();
/// ///
void addFileToLoad(support::FileName const &); void addFileToLoad(std::string const &);
private: private:
/// noncopyable /// noncopyable
@ -135,9 +135,6 @@ private:
*/ */
void loadFiles(); void loadFiles();
/// Create a View, load files and restore GUI Session.
void restoreGuiSession();
/// initial LyX set up /// initial LyX set up
bool init(); bool init();
/// set up the default dead key bindings if requested /// set up the default dead key bindings if requested

View File

@ -145,7 +145,7 @@ bool import(LyXView * lv, FileName const & filename,
if (loader_format == "lyx") { if (loader_format == "lyx") {
Buffer * buf = theLyXFunc().loadAndViewFile(lyxfile); Buffer * buf = lv->loadDocument(lyxfile);
if (!buf) { if (!buf) {
// we are done // we are done
lv->message(_("file not imported!")); lv->message(_("file not imported!"));
@ -1125,7 +1125,7 @@ void LyXFunc::dispatch(FuncRequest const & cmd)
} }
lyx_view_->message(bformat(_("Opening help file %1$s..."), lyx_view_->message(bformat(_("Opening help file %1$s..."),
makeDisplayPath(fname.absFilename()))); makeDisplayPath(fname.absFilename())));
Buffer * buf = loadAndViewFile(fname, false); Buffer * buf = lyx_view_->loadDocument(fname, false);
if (buf) { if (buf) {
updateLabels(*buf); updateLabels(*buf);
lyx_view_->setBuffer(buf); lyx_view_->setBuffer(buf);
@ -1194,12 +1194,6 @@ void LyXFunc::dispatch(FuncRequest const & cmd)
break; break;
} }
case LFUN_FILE_OPEN:
BOOST_ASSERT(lyx_view_);
open(argument);
updateFlags = Update::None;
break;
// --- lyxserver commands ---------------------------- // --- lyxserver commands ----------------------------
case LFUN_SERVER_GET_NAME: case LFUN_SERVER_GET_NAME:
BOOST_ASSERT(lyx_view_ && lyx_view_->buffer()); BOOST_ASSERT(lyx_view_ && lyx_view_->buffer());
@ -1233,7 +1227,7 @@ void LyXFunc::dispatch(FuncRequest const & cmd)
if (theBufferList().exists(s.absFilename())) if (theBufferList().exists(s.absFilename()))
buf = theBufferList().getBuffer(s.absFilename()); buf = theBufferList().getBuffer(s.absFilename());
else { else {
buf = loadAndViewFile(s); buf = lyx_view_->loadDocument(s);
loaded = true; loaded = true;
} }
} }
@ -1387,7 +1381,7 @@ void LyXFunc::dispatch(FuncRequest const & cmd)
} else { } else {
setMessage(bformat(_("Opening child document %1$s..."), setMessage(bformat(_("Opening child document %1$s..."),
makeDisplayPath(filename.absFilename()))); makeDisplayPath(filename.absFilename())));
child = loadAndViewFile(filename, true); child = lyx_view_->loadDocument(filename, false);
parsed = true; parsed = true;
} }
if (child) { if (child) {
@ -1864,105 +1858,6 @@ void LyXFunc::sendDispatchMessage(docstring const & msg, FuncRequest const & cmd
} }
Buffer * LyXFunc::loadAndViewFile(FileName const & filename, bool tolastfiles)
{
lyx_view_->setBusy(true);
Buffer * newBuffer = checkAndLoadLyXFile(filename);
if (!newBuffer) {
lyx_view_->message(_("Document not loaded."));
lyx_view_->setBusy(false);
return 0;
}
lyx_view_->setBuffer(newBuffer);
// scroll to the position when the file was last closed
if (lyxrc.use_lastfilepos) {
LastFilePosSection::FilePos filepos =
LyX::ref().session().lastFilePos().load(filename);
lyx_view_->view()->moveToPosition(filepos.pit, filepos.pos, 0, 0);
}
if (tolastfiles)
LyX::ref().session().lastFiles().add(filename);
lyx_view_->setBusy(false);
return newBuffer;
}
void LyXFunc::open(string const & fname)
{
string initpath = lyxrc.document_path;
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;
}
string filename;
if (fname.empty()) {
FileDialog dlg(_("Select document to open"), LFUN_FILE_OPEN);
dlg.setButton1(_("Documents|#o#O"), from_utf8(lyxrc.document_path));
dlg.setButton2(_("Examples|#E#e"),
from_utf8(addPath(package().system_support().absFilename(), "examples")));
FileDialog::Result result =
dlg.open(from_utf8(initpath),
FileFilterList(_("LyX Documents (*.lyx)")),
docstring());
if (result.first == FileDialog::Later)
return;
filename = to_utf8(result.second);
// check selected filename
if (filename.empty()) {
lyx_view_->message(_("Canceled."));
return;
}
} else
filename = fname;
// get absolute path of file and add ".lyx" to the filename if
// necessary.
FileName const fullname =
fileSearch(string(), filename, "lyx", support::may_not_exist);
if (!fullname.empty())
filename = fullname.absFilename();
// if the file doesn't exist, let the user create one
if (!fullname.exists()) {
// the user specifically chose this name. Believe him.
Buffer * const b = newFile(filename, string(), true);
if (b)
lyx_view_->setBuffer(b);
return;
}
docstring const disp_fn = makeDisplayPath(filename);
lyx_view_->message(bformat(_("Opening document %1$s..."), disp_fn));
docstring str2;
Buffer * buf = loadAndViewFile(fullname);
if (buf) {
updateLabels(*buf);
lyx_view_->setBuffer(buf);
buf->errors("Parse");
str2 = bformat(_("Document %1$s opened."), disp_fn);
} else {
str2 = bformat(_("Could not open document %1$s"), disp_fn);
}
lyx_view_->message(str2);
}
void LyXFunc::doImport(string const & argument) void LyXFunc::doImport(string const & argument)
{ {
string format; string format;
@ -2067,7 +1962,7 @@ void LyXFunc::reloadBuffer()
// The user has already confirmed that the changes, if any, should // The user has already confirmed that the changes, if any, should
// be discarded. So we just release the Buffer and don't call closeBuffer(); // be discarded. So we just release the Buffer and don't call closeBuffer();
theBufferList().release(lyx_view_->buffer()); theBufferList().release(lyx_view_->buffer());
Buffer * buf = loadAndViewFile(filename); Buffer * buf = lyx_view_->loadDocument(filename);
docstring const disp_fn = makeDisplayPath(filename.absFilename()); docstring const disp_fn = makeDisplayPath(filename.absFilename());
docstring str; docstring str;
if (buf) { if (buf) {

View File

@ -87,10 +87,6 @@ public:
/// not the current buffer /// not the current buffer
void gotoBookmark(unsigned int idx, bool openFile, bool switchToBuffer); void gotoBookmark(unsigned int idx, bool openFile, bool switchToBuffer);
/// load a buffer into the current workarea.
Buffer * loadAndViewFile(support::FileName const & name, ///< File to load.
bool tolastfiles = true); ///< append to the "Open recent" menu?
/// cursor x position before dispatch started /// cursor x position before dispatch started
int cursorBeforeDispatchX() const { int cursorBeforeDispatchX() const {
return cursorPosBeforeDispatchX_; return cursorPosBeforeDispatchX_;
@ -133,8 +129,6 @@ private:
void sendDispatchMessage(docstring const & msg, void sendDispatchMessage(docstring const & msg,
FuncRequest const & ev); FuncRequest const & ev);
///
void open(std::string const &);
/// ///
void doImport(std::string const &); void doImport(std::string const &);
/// ///

View File

@ -163,6 +163,9 @@ public:
/// ///
virtual void resetGui() = 0; virtual void resetGui() = 0;
/// Load files and restore GUI Session.
virtual void restoreGuiSession() = 0;
/// ///
virtual void hideDialogs(std::string const & name, Inset * inset) const = 0; virtual void hideDialogs(std::string const & name, Inset * inset) const = 0;
/// ///

View File

@ -67,6 +67,12 @@ public:
virtual void setBuffer(Buffer * b) = 0; ///< \c Buffer to set. virtual void setBuffer(Buffer * b) = 0; ///< \c Buffer to set.
/// ///
virtual bool closeBuffer() = 0; virtual bool closeBuffer() = 0;
/// load a document into the current workarea.
virtual Buffer * loadDocument(
support::FileName const & name, ///< File to load.
bool tolastfiles = true ///< append to the "Open recent" menu?
) = 0;
/// ///
virtual void newDocument(std::string const & filename, virtual void newDocument(std::string const & filename,
bool fromTemplate) = 0; bool fromTemplate) = 0;

View File

@ -378,6 +378,24 @@ void GuiApplication::execBatchCommands()
} }
void GuiApplication::restoreGuiSession()
{
if (!lyxrc.load_session)
return;
Session & session = LyX::ref().session();
vector<FileName> const & lastopened = session.lastOpened().getfiles();
// do not add to the lastfile list since these files are restored from
// last session, and should be already there (regular files), or should
// not be added at all (help files).
for_each(lastopened.begin(), lastopened.end(),
bind(&GuiView::loadDocument, current_view_, _1, false));
// clear this list to save a few bytes of RAM
session.lastOpened().clear();
}
QString const GuiApplication::romanFontName() QString const GuiApplication::romanFontName()
{ {
QFont font; QFont font;
@ -429,7 +447,7 @@ bool GuiApplication::event(QEvent * e)
// So we acknowledge the event and delay the file opening // So we acknowledge the event and delay the file opening
// until LyX is ready. // until LyX is ready.
// FIXME UNICODE: FileName accept an utf8 encoded string. // FIXME UNICODE: FileName accept an utf8 encoded string.
LyX::ref().addFileToLoad(FileName(fromqstr(foe->file()))); LyX::ref().addFileToLoad(fromqstr(foe->file()));
else else
lyx::dispatch(FuncRequest(LFUN_FILE_OPEN, lyx::dispatch(FuncRequest(LFUN_FILE_OPEN,
qstring_to_ucs4(foe->file()))); qstring_to_ucs4(foe->file())));

View File

@ -61,6 +61,7 @@ public:
virtual FuncStatus getStatus(FuncRequest const &); virtual FuncStatus getStatus(FuncRequest const &);
virtual bool dispatch(FuncRequest const &); virtual bool dispatch(FuncRequest const &);
virtual void resetGui(); virtual void resetGui();
void restoreGuiSession();
virtual Clipboard & clipboard(); virtual Clipboard & clipboard();
virtual Selection & selection(); virtual Selection & selection();
virtual FontLoader & fontLoader() { return font_loader_; } virtual FontLoader & fontLoader() { return font_loader_; }

View File

@ -1055,6 +1055,105 @@ static FileName selectTemplateFile()
} }
Buffer * GuiView::loadDocument(FileName const & filename, bool tolastfiles)
{
setBusy(true);
Buffer * newBuffer = checkAndLoadLyXFile(filename);
if (!newBuffer) {
message(_("Document not loaded."));
setBusy(false);
return 0;
}
setBuffer(newBuffer);
// scroll to the position when the file was last closed
if (lyxrc.use_lastfilepos) {
LastFilePosSection::FilePos filepos =
LyX::ref().session().lastFilePos().load(filename);
view()->moveToPosition(filepos.pit, filepos.pos, 0, 0);
}
if (tolastfiles)
LyX::ref().session().lastFiles().add(filename);
setBusy(false);
return newBuffer;
}
void GuiView::openDocument(string const & fname)
{
string initpath = lyxrc.document_path;
if (buffer()) {
string const trypath = buffer()->filePath();
// If directory is writeable, use this as default.
if (FileName(trypath).isDirWritable())
initpath = trypath;
}
string filename;
if (fname.empty()) {
FileDialog dlg(_("Select document to open"), LFUN_FILE_OPEN);
dlg.setButton1(_("Documents|#o#O"), from_utf8(lyxrc.document_path));
dlg.setButton2(_("Examples|#E#e"),
from_utf8(addPath(package().system_support().absFilename(), "examples")));
FileDialog::Result result =
dlg.open(from_utf8(initpath),
FileFilterList(_("LyX Documents (*.lyx)")),
docstring());
if (result.first == FileDialog::Later)
return;
filename = to_utf8(result.second);
// check selected filename
if (filename.empty()) {
message(_("Canceled."));
return;
}
} else
filename = fname;
// get absolute path of file and add ".lyx" to the filename if
// necessary.
FileName const fullname =
fileSearch(string(), filename, "lyx", support::may_not_exist);
if (!fullname.empty())
filename = fullname.absFilename();
// if the file doesn't exist, let the user create one
if (!fullname.exists()) {
// the user specifically chose this name. Believe him.
Buffer * const b = newFile(filename, string(), true);
if (b)
setBuffer(b);
return;
}
docstring const disp_fn = makeDisplayPath(filename);
message(bformat(_("Opening document %1$s..."), disp_fn));
docstring str2;
Buffer * buf = loadDocument(fullname);
if (buf) {
updateLabels(*buf);
setBuffer(buf);
buf->errors("Parse");
str2 = bformat(_("Document %1$s opened."), disp_fn);
} else {
str2 = bformat(_("Could not open document %1$s"), disp_fn);
}
message(str2);
}
void GuiView::newDocument(string const & filename, bool from_template) void GuiView::newDocument(string const & filename, bool from_template)
{ {
FileName initpath(lyxrc.document_path); FileName initpath(lyxrc.document_path);
@ -1352,6 +1451,10 @@ bool GuiView::dispatch(FuncRequest const & cmd)
bv->cursor().updateFlags(Update::None); bv->cursor().updateFlags(Update::None);
switch(cmd.action) { switch(cmd.action) {
case LFUN_FILE_OPEN:
openDocument(to_utf8(cmd.argument()));
break;
case LFUN_BUFFER_SWITCH: case LFUN_BUFFER_SWITCH:
setBuffer(theBufferList().getBuffer(to_utf8(cmd.argument()))); setBuffer(theBufferList().getBuffer(to_utf8(cmd.argument())));
break; break;

View File

@ -89,6 +89,11 @@ public:
void setBuffer(Buffer * b); ///< \c Buffer to set. void setBuffer(Buffer * b); ///< \c Buffer to set.
/// ///
bool closeBuffer(); bool closeBuffer();
/// load a document into the current workarea.
Buffer * loadDocument(support::FileName const & name, ///< File to load.
bool tolastfiles = true); ///< append to the "Open recent" menu?
///
void openDocument(std::string const & filename);
/// ///
void newDocument(std::string const & filename, bool fromTemplate); void newDocument(std::string const & filename, bool fromTemplate);
/// write all buffers, asking the user, returns false if cancelled /// write all buffers, asking the user, returns false if cancelled