]> git.lyx.org Git - lyx.git/blobdiff - src/frontends/qt4/GuiView.cpp
If we are in a closeEvent, we don't want to close all buffers, because these may...
[lyx.git] / src / frontends / qt4 / GuiView.cpp
index f403cef31b525e98fd16f8d70daffba6884ec0e5..8986f7dfbccd4b8a6e958403977c6c5f64c30ec8 100644 (file)
@@ -547,7 +547,7 @@ void GuiView::closeEvent(QCloseEvent * close_event)
        // it can happen that this event arrives without selecting the view,
        // e.g. when clicking the close button on a background window.
        setFocus();
-       if (!closeBufferAll(true)) {
+       if (!closeWorkAreaAll(true)) {
                closing_ = false;
                close_event->ignore();
                return;
@@ -584,47 +584,6 @@ void GuiView::closeEvent(QCloseEvent * close_event)
 }
 
 
-bool GuiView::closeBufferAll(bool in_close_event)
-{
-       GuiWorkArea * active_wa = currentMainWorkArea();
-       setCurrentWorkArea(active_wa);
-
-       // We might be in a situation that there is still a tabWorkArea, but
-       // there are no tabs anymore. This can happen when we get here after a 
-       // TabWorkArea::lastWorkAreaRemoved() signal. Therefore we count how
-       // many TabWorkArea's have no documents anymore.
-       int empty_twa = 0;
-
-       // We have to call count() each time, because it can happen that
-       // more than one splitter will disappear in one iteration (bug 5998).
-       for (; d.splitter_->count() > empty_twa; ) {
-               TabWorkArea * twa = d.tabWorkArea(empty_twa);
-                               
-               int twa_count = twa->count();
-               if (twa->count() == 0)
-                       ++empty_twa;
-
-               for (; twa == d.tabWorkArea(empty_twa) && twa_count; --twa_count) {
-                       twa->setCurrentIndex(twa_count-1);
-
-                       GuiWorkArea * wa = twa->currentWorkArea();
-                       bool const is_active_wa = active_wa == wa;
-                       Buffer & b = wa->bufferView().buffer();
-
-                       // We only want to close the buffer if the same buffer is not in
-                       // another view.
-                       bool const close_buffer = !(inMultiViews(wa) || b.parent());
-
-                       // closeBuffer() needs buffer workArea still alive and
-                       // set as currrent one, and destroys it
-                       if (!closeWorkArea(wa, close_buffer, in_close_event, is_active_wa))
-                               return false;
-               }
-       }
-       return true;
-}
-
-
 void GuiView::dragEnterEvent(QDragEnterEvent * event)
 {
        if (event->mimeData()->hasUrls())
@@ -1903,7 +1862,8 @@ bool GuiView::hideWorkArea(GuiWorkArea * wa)
 
 bool GuiView::closeWorkArea(GuiWorkArea * wa)
 {
-       return closeWorkArea(wa, true);
+       Buffer & buf = wa->bufferView().buffer();
+       return closeWorkArea(wa, !buf.parent());
 }
 
 
@@ -1915,6 +1875,48 @@ bool GuiView::closeBuffer()
 }
 
 
+bool GuiView::closeBufferAll(bool in_close_event)
+{
+       // First close all workareas. This will make
+       // sure that dirty buffers are saved.
+       if (!closeWorkAreaAll(in_close_event))
+               return false;
+
+       // Now close the hidden buffers. We prevent hidden 
+       // buffers from being dirty, so we can just close them.
+       theBufferList().closeAll();
+       return true;
+}
+
+
+bool GuiView::closeWorkAreaAll(bool in_close_event)
+{
+       GuiWorkArea * active_wa = currentMainWorkArea();
+       setCurrentWorkArea(active_wa);
+
+       // We might be in a situation that there is still a tabWorkArea, but
+       // there are no tabs anymore. This can happen when we get here after a 
+       // TabWorkArea::lastWorkAreaRemoved() signal. Therefore we count how
+       // many TabWorkArea's have no documents anymore.
+       int empty_twa = 0;
+
+       // We have to call count() each time, because it can happen that
+       // more than one splitter will disappear in one iteration (bug 5998).
+       for (; d.splitter_->count() > empty_twa; ) {
+               TabWorkArea * twa = d.tabWorkArea(empty_twa);
+                               
+               if (twa->count() == 0)
+                       ++empty_twa;
+               else {
+                       setCurrentWorkArea(twa->currentWorkArea());
+                       if (!closeTabWorkArea(twa, true, active_wa))
+                               return false;
+               }
+       }
+       return true;
+}
+
+
 bool GuiView::closeWorkArea(GuiWorkArea * wa, bool close_buffer,
        bool in_close_event, bool mark_active)
 {
@@ -1959,6 +1961,31 @@ bool GuiView::closeWorkArea(GuiWorkArea * wa, bool close_buffer,
 }
 
 
+bool GuiView::closeTabWorkArea(TabWorkArea * twa, bool in_close_event,
+                                                          GuiWorkArea * main_work_area)
+{
+       while (twa == d.currentTabWorkArea()) {
+               twa->setCurrentIndex(twa->count()-1);
+
+               GuiWorkArea * wa = twa->currentWorkArea();
+               bool const is_active_wa = main_work_area == wa;
+               Buffer & b = wa->bufferView().buffer();
+
+               // We only want to close the buffer if the same buffer is not visible
+               // in another view, and if this is not a child and if we are closing
+               // a view (not a tabgroup).
+               bool const close_buffer = 
+                       !inMultiViews(wa) && !b.parent() && in_close_event;
+
+               // closeBuffer() needs buffer workArea still alive and
+               // set as currrent one, and destroys it
+               if (!closeWorkArea(wa, close_buffer, true, is_active_wa))
+                       return false;
+       }
+       return true;
+}
+
+
 bool GuiView::saveBufferIfNeeded(Buffer & buf, bool hiding)
 {
        if (buf.isClean() || buf.paragraphs().empty())
@@ -2284,7 +2311,7 @@ bool GuiView::dispatch(FuncRequest const & cmd)
 
                case LFUN_CLOSE_TAB_GROUP:
                        if (TabWorkArea * twa = d.currentTabWorkArea()) {
-                               delete twa;
+                               closeTabWorkArea(twa, false);
                                d.current_work_area_ = 0;
                                twa = d.currentTabWorkArea();
                                // Switch to the next GuiWorkArea in the found TabWorkArea.