From 144e721b65069a8449ee32a87c7e0514e4e0a725 Mon Sep 17 00:00:00 2001 From: Abdelrazak Younes Date: Tue, 19 Jun 2007 16:03:47 +0000 Subject: [PATCH] Polish revision 18825 and fix some remaining issues with child documents. The diff is bigger than it should because of white space issues. * buffer_funcs.{h,cpp}: new checkAndLoadLyXFile() transferred from BufferView::loadLyXFile(). * BufferView::loadLyXFile(): deleted. * LyXView::loadLyXFile(): simplify logic and fix some issues: - buggy child document leads to crash due to error list dialog. - ensure that we switch to buffer if there's some errors. - use LyXView::setBuffer() instead of repeating code. git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@18828 a592a061-630c-0410-9148-cb99ea01b6c8 --- src/BufferView.cpp | 55 -------------------- src/BufferView.h | 4 -- src/buffer_funcs.cpp | 41 +++++++++++++++ src/buffer_funcs.h | 7 +++ src/frontends/LyXView.cpp | 105 ++++++++++++++++---------------------- 5 files changed, 91 insertions(+), 121 deletions(-) diff --git a/src/BufferView.cpp b/src/BufferView.cpp index 4f2fe0a334..61b196fbb5 100644 --- a/src/BufferView.cpp +++ b/src/BufferView.cpp @@ -233,61 +233,6 @@ void BufferView::setBuffer(Buffer * b) graphics::Previews::get().generateBufferPreviews(*buffer_); } -// FIXME There is now no need for this to be in BufferView. It should all -// be moved to buffer_func.cpp. (Abdel) -Buffer * BufferView::loadLyXFile(FileName const & filename, bool auto_open) -{ - // File already open? - if (theBufferList().exists(filename.absFilename())) { - docstring const file = makeDisplayPath(filename.absFilename(), 20); - docstring text = bformat(_("The document %1$s is already " - "loaded.\n\nDo you want to revert " - "to the saved version?"), file); - int const ret = Alert::prompt(_("Revert to saved document?"), - text, 0, 1, _("&Revert"), _("&Switch to document")); - - if (ret != 0) { - Buffer * buf = theBufferList().getBuffer(filename.absFilename()); - setBuffer(buf); - return buf; - } - // FIXME: should be LFUN_REVERT - if (!theBufferList().close(theBufferList().getBuffer(filename.absFilename()), false)) - return 0; - // Fall through to new load. (Asger) - buffer_ = 0; - } - - Buffer * b = 0; - - if (isFileReadable(filename)) { - b = theBufferList().newBuffer(filename.absFilename()); - if (!lyx::loadLyXFile(b, filename)) { - theBufferList().release(b); - return 0; - } - } else { - docstring text = bformat(_("The document %1$s does not yet " - "exist.\n\nDo you want to create " - "a new document?"), from_utf8(filename.absFilename())); - int const ret = Alert::prompt(_("Create new document?"), - text, 0, 1, _("&Create"), _("Cancel")); - - if (ret == 0) { - b = newFile(filename.absFilename(), string(), true); - if (!b) - return 0; - } else - return 0; - } - - if (!auto_open) - setBuffer(b); - - return b; -} - - void BufferView::resize() { if (!buffer_) diff --git a/src/BufferView.h b/src/BufferView.h index d4861f0ce5..a27d010f78 100644 --- a/src/BufferView.h +++ b/src/BufferView.h @@ -93,10 +93,6 @@ public: /// resize the BufferView. void resize(); - /// load a buffer into the view. - /// returns the buffer or 0 if not loaded - Buffer * loadLyXFile(support::FileName const & name, bool auto_open = false); - /// perform pending metrics updates. /** \c Update::FitCursor means first to do a FitCursor, and to * force an update if screen position changes. diff --git a/src/buffer_funcs.cpp b/src/buffer_funcs.cpp index 0080874610..3ed3249fbb 100644 --- a/src/buffer_funcs.cpp +++ b/src/buffer_funcs.cpp @@ -180,6 +180,47 @@ bool loadLyXFile(Buffer * b, FileName const & s) return false; } + +Buffer * checkAndLoadLyXFile(FileName const & filename) +{ + // File already open? + if (theBufferList().exists(filename.absFilename())) { + docstring const file = makeDisplayPath(filename.absFilename(), 20); + docstring text = bformat(_("The document %1$s is already " + "loaded.\n\nDo you want to revert " + "to the saved version?"), file); + if (Alert::prompt(_("Revert to saved document?"), + text, 0, 1, _("&Revert"), _("&Switch to document"))) + return theBufferList().getBuffer(filename.absFilename()); + + // FIXME: should be LFUN_REVERT + if (theBufferList().close(theBufferList().getBuffer(filename.absFilename()), false)) + // Load it again. + return checkAndLoadLyXFile(filename); + else + // The file could not be closed. + return 0; + } + + if (isFileReadable(filename)) { + Buffer * b = theBufferList().newBuffer(filename.absFilename()); + if (!lyx::loadLyXFile(b, filename)) { + theBufferList().release(b); + return 0; + } + return b; + } + + docstring text = bformat(_("The document %1$s does not yet " + "exist.\n\nDo you want to create a new document?"), + from_utf8(filename.absFilename())); + if (Alert::prompt(_("Create new document?"), + text, 0, 1, _("&Create"), _("Cancel"))) + return newFile(filename.absFilename(), string(), true); + + return 0; +} + // FIXME newFile() should probably be a member method of Application... Buffer * newFile(string const & filename, string const & templatename, bool const isNamed) diff --git a/src/buffer_funcs.h b/src/buffer_funcs.h index b5a8ac49c1..531fb39f27 100644 --- a/src/buffer_funcs.h +++ b/src/buffer_funcs.h @@ -34,6 +34,13 @@ class ParIterator; */ bool loadLyXFile(Buffer *, support::FileName const & filename); +/** + * Checks and loads a LyX file \param filename. + * \retval the newly created \c Buffer pointer if successful or 0. + * \retval 0 if the \c Buffer could not be created. + */ +Buffer * checkAndLoadLyXFile(support::FileName const & filename); + /* Make a new file (buffer) with name \c filename based on a template * named \c templatename */ diff --git a/src/frontends/LyXView.cpp b/src/frontends/LyXView.cpp index 46623fec01..cdbcc66e16 100644 --- a/src/frontends/LyXView.cpp +++ b/src/frontends/LyXView.cpp @@ -190,71 +190,52 @@ bool LyXView::loadLyXFile(FileName const & filename, bool tolastfiles, if (oldBuffer) parentfilename = oldBuffer->fileName(); - Buffer * newBuffer = - work_area_->bufferView().loadLyXFile(filename, auto_open); + Buffer * newBuffer = checkAndLoadLyXFile(filename); - if (!newBuffer) { + if (!newBuffer) { message(_("Document not loaded.")); - updateStatusBar(); - busy(false); - work_area_->redraw(); - return false; - } - - if (!auto_open) { - disconnectBuffer(); - connectBuffer(*newBuffer); - } - - showErrorList("Parse"); - - if (child_document && newBuffer != oldBuffer) { - // Set the parent name of the child document. - // This makes insertion of citations and references in the child work, - // when the target is in the parent or another child document. - newBuffer->setParentName(parentfilename); - message(bformat(_("Opening child document %1$s..."), - makeDisplayPath(filename.absFilename()))); - } - - // Update the labels and section numbering. - updateLabels(*newBuffer->getMasterBuffer()); - - // scroll to the position when the file was last closed - if (!auto_open && lyxrc.use_lastfilepos) { - pit_type pit; - pos_type pos; - boost::tie(pit, pos) = LyX::ref().session().lastFilePos().load(filename); - // if successfully move to pit (returned par_id is not zero), - // update metrics and reset font - if (work_area_->bufferView().moveToPosition(pit, pos, 0, 0).get<1>()) { - if (work_area_->bufferView().fitCursor()) - work_area_->bufferView().updateMetrics(false); - newBuffer->text().setCurrentFont(work_area_->bufferView().cursor()); - } - } - - if (tolastfiles) - LyX::ref().session().lastFiles().add(filename); - - // FIXME We definitely don't have to do all of this if auto_open... - // The question is: What do we have to do here if we've loaded a new - // file but haven't switched buffers? I'm guessing we can skip the - // layout bit and the title. - // and if we have already switched buffers...won't all of this have been - // done already in setBuffer()? So why does it also need doing here? - // In fact, won't a lot of what's above already have been done? E.g., - // the connecting and disconnecting of buffers will have been done - // already in setBuffer(). - // This bit of cleanup is post-1.5.0.... - updateMenubar(); - updateToolbars(); - updateLayoutChoice(); - updateWindowTitle(); - updateTab(); - updateStatusBar(); + updateStatusBar(); + busy(false); + work_area_->redraw(); + return false; + } + + if (child_document && newBuffer != oldBuffer) { + // Set the parent name of the child document. + // This makes insertion of citations and references in the child work, + // when the target is in the parent or another child document. + newBuffer->setParentName(parentfilename); + message(bformat(_("Opening child document %1$s..."), + makeDisplayPath(filename.absFilename()))); + } + + bool const parse_error = !newBuffer->errorList("Parse").empty(); + if (parse_error || !auto_open) { + setBuffer(newBuffer, child_document); + showErrorList("Parse"); + } + + // Update the labels and section numbering. + updateLabels(*newBuffer->getMasterBuffer()); + + // scroll to the position when the file was last closed + if (!auto_open && lyxrc.use_lastfilepos) { + pit_type pit; + pos_type pos; + boost::tie(pit, pos) = LyX::ref().session().lastFilePos().load(filename); + // if successfully move to pit (returned par_id is not zero), + // update metrics and reset font + if (work_area_->bufferView().moveToPosition(pit, pos, 0, 0).get<1>()) { + if (work_area_->bufferView().fitCursor()) + work_area_->bufferView().updateMetrics(false); + newBuffer->text().setCurrentFont(work_area_->bufferView().cursor()); + } + } + + if (tolastfiles) + LyX::ref().session().lastFiles().add(filename); + busy(false); - work_area_->redraw(); return true; } -- 2.39.2