]> git.lyx.org Git - features.git/commitdiff
Deal with a UI bug discussed on the list.
authorRichard Heck <rgheck@comcast.net>
Mon, 6 Aug 2007 04:14:26 +0000 (04:14 +0000)
committerRichard Heck <rgheck@comcast.net>
Mon, 6 Aug 2007 04:14:26 +0000 (04:14 +0000)
The effect is this: If you try to open a file that is already open,
LyX (i) checks if the buffer is dirty, and just switches buffers if
it is not; (ii) if it is, you get this message:
 The document %1$s is already loaded and has unsaved changes.
 Do you want to abandon your changes and reload the document?
    [Reload] [Keep Changes]
If you hit the latter, you just get switched to the document.

git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@19331 a592a061-630c-0410-9148-cb99ea01b6c8

src/buffer_funcs.cpp
src/buffer_funcs.h
src/frontends/LyXView.cpp

index 514374a02c782a01f28c03902a83a435acce0a91..1f1cd7917c1c6c8490b875be1af000d27d1e990f 100644 (file)
@@ -181,20 +181,29 @@ bool loadLyXFile(Buffer * b, FileName const & s)
 }
 
 
+bool checkIfLoaded(FileName const & fn)
+{
+       return theBufferList().getBuffer(fn.absFilename());
+}
+
+
 Buffer * checkAndLoadLyXFile(FileName const & filename)
 {
        // File already open?
-       if (theBufferList().exists(filename.absFilename())) {
+       Buffer * checkBuffer = theBufferList().getBuffer(filename.absFilename());
+       if (checkBuffer) {
+               if (checkBuffer->isClean())
+                       return checkBuffer;
                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());
+               docstring text = bformat(_(
+                               "The document %1$s is already loaded and has unsaved changes.\n"
+                               "Do you want to abandon your changes and reload the version on disk?"), file);
+               if (Alert::prompt(_("Reload saved document?"),
+                               text, 0, 1,  _("&Reload"), _("&Keep Changes")))
+                       return checkBuffer;
 
                // FIXME: should be LFUN_REVERT
-               if (theBufferList().close(theBufferList().getBuffer(filename.absFilename()), false))
+               if (theBufferList().close(checkBuffer, false))
                        // Load it again.
                        return checkAndLoadLyXFile(filename);
                else
index 350f438161a1b0a14a75a357ba9988704590ac70..655d59bf34242a703dc17c5fb37bee55b504eff6 100644 (file)
@@ -28,6 +28,12 @@ class ErrorList;
 class TeXErrors;
 class ParIterator;
 
+
+/** 
+ * Returns true if the file is already loaded into a buffer.
+ */
+bool checkIfLoaded(support::FileName const & fn);
+
 /**
  *  Loads a LyX file \c filename into \c Buffer
  *  and \return success status.
@@ -41,8 +47,8 @@ bool loadLyXFile(Buffer *, support::FileName const & filename);
  */
 Buffer * checkAndLoadLyXFile(support::FileName const & filename);
 
-/* Make a new file (buffer) with name \c filename based on a template
- * named \c templatename
+/** Make a new file (buffer) with name \c filename based on a template
+ *  named \c templatename
  */
 Buffer * newFile(std::string const & filename, std::string const & templatename,
                 bool isNamed = false);
index 773231394d86f743628918c62c07bc359dab5269..f2278c8662a6bd42fb87d99db16c08c6426657fe 100644 (file)
@@ -206,6 +206,7 @@ bool LyXView::loadLyXFile(FileName const & filename, bool tolastfiles,
        if (oldBuffer)
                parentfilename = oldBuffer->fileName();
 
+       bool alreadyLoaded = checkIfLoaded(filename);
        Buffer * newBuffer = checkAndLoadLyXFile(filename);
 
        if (!newBuffer) {
@@ -229,33 +230,35 @@ bool LyXView::loadLyXFile(FileName const & filename, bool tolastfiles,
        updateLabels(*newBuffer->getMasterBuffer());
 
        bool const parse_error = !newBuffer->errorList("Parse").empty();
-       if (parse_error || !auto_open) {
+       bool const need_switch = parse_error || !auto_open;
+       if (need_switch) {
                setBuffer(newBuffer, child_document);
-               showErrorList("Parse");
-       }
-
-       // 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());
-                       updateMenubar();
-                       updateToolbars();
-                       updateLayoutChoice();
-                       updateStatusBar();
-                       work_area_->redraw();
+               if (!alreadyLoaded) {
+                       if (parse_error)
+                               showErrorList("Parse");
+                       // scroll to the position when the file was last closed
+                       if (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());
+                                       updateMenubar();
+                                       updateToolbars();
+                                       updateLayoutChoice();
+                                       updateStatusBar();
+                                       work_area_->redraw();
+                               }
+                       }
+               if (tolastfiles)
+                       LyX::ref().session().lastFiles().add(filename);
                }
        }
 
-       if (tolastfiles)
-               LyX::ref().session().lastFiles().add(filename);
-
        busy(false);
        return true;
 }