]> git.lyx.org Git - features.git/commitdiff
Polish revision 18825 and fix some remaining issues with child documents. The diff...
authorAbdelrazak Younes <younes@lyx.org>
Tue, 19 Jun 2007 16:03:47 +0000 (16:03 +0000)
committerAbdelrazak Younes <younes@lyx.org>
Tue, 19 Jun 2007 16:03:47 +0000 (16:03 +0000)
* 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
src/BufferView.h
src/buffer_funcs.cpp
src/buffer_funcs.h
src/frontends/LyXView.cpp

index 4f2fe0a334747319a2fb86844188b8ad3c4d3074..61b196fbb5657de98ac3ae5c26d2d42880cb5fce 100644 (file)
@@ -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_)
index d4861f0ce569724e7179066f7f18d7146e18273c..a27d010f78c2f8f5f536117cabed059a312589b0 100644 (file)
@@ -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.
index 0080874610693be76d3f3cf7a836b159aa0b3624..3ed3249fbb2c49a54967c65f258ea4c23a0e9d23 100644 (file)
@@ -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)
index b5a8ac49c1bd1b9e4b40e31220e9862f653a2b94..531fb39f27ce603ced4e606723e16a6d19de4ead 100644 (file)
@@ -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
  */
index 46623fec01cf98a9888da170762070430e2b3872..cdbcc66e1661d43a3101fb496620ae609892dda6 100644 (file)
@@ -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;
 }