]> git.lyx.org Git - features.git/blobdiff - src/frontends/qt4/GuiView.cpp
s/updateLabels/updateBuffer/g, per a suggestion of Abdel's.
[features.git] / src / frontends / qt4 / GuiView.cpp
index eca9dcd0630187dc4e66161783f8e470a11f99d8..a59284d2565a08dc8cb169613ee37e46dfa83d59 100644 (file)
 #include "GuiView.h"
 
 #include "Dialog.h"
+#include "DispatchResult.h"
 #include "FileDialog.h"
 #include "FontLoader.h"
 #include "GuiApplication.h"
 #include "GuiCommandBuffer.h"
 #include "GuiCompleter.h"
-#include "GuiWorkArea.h"
 #include "GuiKeySymbol.h"
 #include "GuiToc.h"
 #include "GuiToolbar.h"
+#include "GuiWorkArea.h"
 #include "LayoutBox.h"
 #include "Menus.h"
 #include "TocModel.h"
@@ -38,6 +39,7 @@
 #include "BufferList.h"
 #include "BufferParams.h"
 #include "BufferView.h"
+#include "Compare.h"
 #include "Converter.h"
 #include "Cursor.h"
 #include "CutAndPaste.h"
@@ -49,7 +51,7 @@
 #include "Intl.h"
 #include "Layout.h"
 #include "Lexer.h"
-#include "LyXFunc.h"
+#include "LyXAction.h"
 #include "LyX.h"
 #include "LyXRC.h"
 #include "LyXVC.h"
@@ -66,6 +68,7 @@
 #include "support/FileName.h"
 #include "support/filetools.h"
 #include "support/gettext.h"
+#include "support/filetools.h"
 #include "support/ForkedCalls.h"
 #include "support/lassert.h"
 #include "support/lstrings.h"
@@ -74,6 +77,8 @@
 #include "support/Path.h"
 #include "support/Systemcall.h"
 #include "support/Timeout.h"
+#include "support/ProgressInterface.h"
+#include "GuiProgress.h"
 
 #include <QAction>
 #include <QApplication>
 #include <QSplitter>
 #include <QStackedWidget>
 #include <QStatusBar>
+#include <QTime>
 #include <QTimer>
 #include <QToolBar>
 #include <QUrl>
 #include <QScrollBar>
 
+#define EXPORT_in_THREAD 1
+
+// QtConcurrent was introduced in Qt 4.4
+#if (QT_VERSION >= 0x040400)
+#include <QFuture>
+#include <QFutureWatcher>
+#include <QtConcurrentRun>
+#endif
+
 #include <boost/bind.hpp>
 
+#include <sstream>
+
 #ifdef HAVE_SYS_TIME_H
 # include <sys/time.h>
 #endif
@@ -137,6 +154,7 @@ public:
                font.setPointSize(int(toqstr(lyxrc.font_sizes[FONT_SIZE_LARGE]).toDouble()));
                pain.setFont(font);
                pain.drawText(260, 15, text);
+               setFocusPolicy(Qt::StrongFocus);
        }
 
        void paintEvent(QPaintEvent *)
@@ -147,13 +165,25 @@ public:
                pain.drawPixmap(x, y, splash_);
        }
 
+       void keyPressEvent(QKeyEvent * ev)
+       {
+               KeySymbol sym;
+               setKeySymbol(&sym, ev);
+               if (sym.isOK()) {
+                       guiApp->processKeySym(sym, q_key_state(ev->modifiers()));
+                       ev->accept();
+               } else {
+                       ev->ignore();
+               }
+       }
+
 private:
        QPixmap splash_;
 };
 
 
 /// Toolbar store providing access to individual toolbars by name.
-typedef std::map<std::string, GuiToolbar *> ToolbarMap;
+typedef map<string, GuiToolbar *> ToolbarMap;
 
 typedef boost::shared_ptr<Dialog> DialogPtr;
 
@@ -162,8 +192,8 @@ typedef boost::shared_ptr<Dialog> DialogPtr;
 
 struct GuiView::GuiViewPrivate
 {
-       GuiViewPrivate()
-               : current_work_area_(0), current_main_work_area_(0),
+       GuiViewPrivate(GuiView * gv)
+               : gv_(gv), current_work_area_(0), current_main_work_area_(0),
                layout_(0), autosave_timeout_(5000),
                in_show_(false)
        {
@@ -178,6 +208,7 @@ struct GuiView::GuiViewPrivate
                stack_widget_->addWidget(bg_widget_);
                stack_widget_->addWidget(splitter_);
                setBackground();
+               progress_ = new GuiProgress(gv);
        }
 
        ~GuiViewPrivate()
@@ -185,6 +216,7 @@ struct GuiView::GuiViewPrivate
                delete splitter_;
                delete bg_widget_;
                delete stack_widget_;
+               delete progress_;
        }
 
        QMenu * toolBarPopup(GuiView * parent)
@@ -229,6 +261,7 @@ struct GuiView::GuiViewPrivate
        {
                stack_widget_->setCurrentWidget(bg_widget_);
                bg_widget_->setUpdatesEnabled(true);
+               bg_widget_->setFocus();
        }
 
        TabWorkArea * tabWorkArea(int i)
@@ -252,7 +285,20 @@ struct GuiView::GuiViewPrivate
                return tabWorkArea(0);
        }
 
+#if (QT_VERSION >= 0x040400)
+       void setPreviewFuture(QFuture<docstring> const & f)
+       {
+               if (preview_watcher_.isRunning()) {
+                       // we prefer to cancel this preview in order to keep a snappy
+                       // interface.
+                       return;
+               }
+               preview_watcher_.setFuture(f);
+       }
+#endif
+
 public:
+       GuiView * gv_;
        GuiWorkArea * current_work_area_;
        GuiWorkArea * current_main_work_area_;
        QSplitter * splitter_;
@@ -260,6 +306,7 @@ public:
        BackgroundWidget * bg_widget_;
        /// view's toolbars
        ToolbarMap toolbars_;
+       ProgressInterface* progress_;
        /// The main layout box.
        /** 
         * \warning Don't Delete! The layout box is actually owned by
@@ -271,9 +318,6 @@ public:
         */
        LayoutBox * layout_;
 
-       ///
-       map<string, Inset *> open_insets_;
-
        ///
        map<string, DialogPtr> dialogs_;
 
@@ -289,11 +333,20 @@ public:
 
        ///
        TocModels toc_models_;
+
+#if (QT_VERSION >= 0x040400)
+       ///
+       QFutureWatcher<docstring> autosave_watcher_;
+       QFutureWatcher<docstring> preview_watcher_;
+#else
+       struct DummyWatcher { bool isRunning(){return false;} }; 
+       DummyWatcher preview_watcher_;
+#endif
 };
 
 
 GuiView::GuiView(int id)
-       : d(*new GuiViewPrivate), id_(id), closing_(false)
+       : d(*new GuiViewPrivate(this)), id_(id), closing_(false)
 {
        // GuiToolbars *must* be initialised before the menu bar.
        normalSizedIcons(); // at least on Mac the default is 32 otherwise, which is huge
@@ -302,7 +355,7 @@ GuiView::GuiView(int id)
        // set ourself as the current view. This is needed for the menu bar
        // filling, at least for the static special menu item on Mac. Otherwise
        // they are greyed out.
-       theLyXFunc().setLyXView(this);
+       guiApp->setCurrentView(this);
        
        // Fill up the menu bar.
        guiApp->menus().fillMenuBar(menuBar(), this, true);
@@ -327,10 +380,27 @@ GuiView::GuiView(int id)
        setWindowIcon(getPixmap("images/", "lyx", "png"));
 #endif
 
+#if (QT_VERSION >= 0x040300)
+       // use tabbed dock area for multiple docks
+       // (such as "source" and "messages")
+       setDockOptions(QMainWindow::ForceTabbedDocks);
+#endif
+
        // For Drag&Drop.
        setAcceptDrops(true);
 
        statusBar()->setSizeGripEnabled(true);
+       updateStatusBar();
+
+#if (QT_VERSION >= 0x040400)
+       connect(&d.autosave_watcher_, SIGNAL(finished()), this,
+               SLOT(threadFinished()));
+       connect(&d.preview_watcher_, SIGNAL(finished()), this,
+               SLOT(threadFinished()));
+#endif
+
+       connect(this, SIGNAL(triggerShowDialog(QString const &, QString const &, Inset *)),
+               SLOT(doShowDialog(QString const &, QString const &, Inset *)));
 
        // Forbid too small unresizable window because it can happen
        // with some window manager under X11.
@@ -358,6 +428,16 @@ GuiView::~GuiView()
 }
 
 
+void GuiView::threadFinished()
+{
+#if (QT_VERSION >= 0x040400)
+       QFutureWatcher<docstring> const * watcher =
+               static_cast<QFutureWatcher<docstring> const *>(sender());
+       message(watcher->result());
+#endif
+}
+
+
 void GuiView::saveLayout() const
 {
        QSettings settings;
@@ -409,6 +489,8 @@ bool GuiView::restoreLayout()
                dialog->prepareView();
        if ((dialog = findOrBuild("view-source", true)))
                dialog->prepareView();
+       if ((dialog = findOrBuild("progress", true)))
+               dialog->prepareView();
 
        if (!restoreState(settings.value("layout").toByteArray(), 0))
                initToolbars();
@@ -424,7 +506,6 @@ GuiToolbar * GuiView::toolbar(string const & name)
                return it->second;
 
        LYXERR(Debug::GUI, "Toolbar::display: no toolbar named " << name);
-       message(bformat(_("Unknown toolbar \"%1$s\""), from_utf8(name)));
        return 0;
 }
 
@@ -460,7 +541,7 @@ void GuiView::initToolbars()
                if (!tb)
                        continue;
                int const visibility = guiApp->toolbars().defaultVisibility(cit->name);
-               bool newline = true;
+               bool newline = !(visibility & Toolbars::SAMEROW);
                tb->setVisible(false);
                tb->setVisibility(visibility);
 
@@ -473,7 +554,8 @@ void GuiView::initToolbars()
                if (visibility & Toolbars::BOTTOM) {
                        // Qt < 4.2.2 cannot handle ToolBarBreak on non-TOP dock.
 #if (QT_VERSION >= 0x040202)
-                       addToolBarBreak(Qt::BottomToolBarArea);
+                       if (newline)
+                               addToolBarBreak(Qt::BottomToolBarArea);
 #endif
                        addToolBar(Qt::BottomToolBarArea, tb);
                }
@@ -481,7 +563,8 @@ void GuiView::initToolbars()
                if (visibility & Toolbars::LEFT) {
                        // Qt < 4.2.2 cannot handle ToolBarBreak on non-TOP dock.
 #if (QT_VERSION >= 0x040202)
-                       addToolBarBreak(Qt::LeftToolBarArea);
+                       if (newline)
+                               addToolBarBreak(Qt::LeftToolBarArea);
 #endif
                        addToolBar(Qt::LeftToolBarArea, tb);
                }
@@ -489,7 +572,8 @@ void GuiView::initToolbars()
                if (visibility & Toolbars::RIGHT) {
                        // Qt < 4.2.2 cannot handle ToolBarBreak on non-TOP dock.
 #if (QT_VERSION >= 0x040202)
-                       addToolBarBreak(Qt::RightToolBarArea);
+                       if (newline)
+                               addToolBarBreak(Qt::RightToolBarArea);
 #endif
                        addToolBar(Qt::RightToolBarArea, tb);
                }
@@ -509,12 +593,22 @@ TocModels & GuiView::tocModels()
 void GuiView::setFocus()
 {
        LYXERR(Debug::DEBUG, "GuiView::setFocus()" << this);
-       // Make sure LyXFunc points to the correct view.
-       guiApp->setCurrentView(this);
-       theLyXFunc().setLyXView(this);
        QMainWindow::setFocus();
-       if (d.current_work_area_)
-               d.current_work_area_->setFocus();
+}
+
+
+void GuiView::focusInEvent(QFocusEvent * e)
+{
+       LYXERR(Debug::DEBUG, "GuiView::focusInEvent()" << this);
+       QMainWindow::focusInEvent(e);
+       // Make sure guiApp points to the correct view.
+       guiApp->setCurrentView(this);
+       if (currentMainWorkArea())
+               currentMainWorkArea()->setFocus();
+       else if (currentWorkArea())
+               currentWorkArea()->setFocus();
+       else
+               d.bg_widget_->setFocus();
 }
 
 
@@ -608,13 +702,42 @@ void GuiView::dropEvent(QDropEvent * event)
        for (int i = 0; i != files.size(); ++i) {
                string const file = os::internal_path(fromqstr(
                        files.at(i).toLocalFile()));
-               if (!file.empty()) {
-                       // Asynchronously post the event. DropEvent usually come
-                       // from the BufferView. But reloading a file might close
-                       // the BufferView from within its own event handler.
-                       guiApp->dispatchDelayed(FuncRequest(LFUN_FILE_OPEN, file));
-                       event->accept();
+               if (file.empty())
+                       continue;
+
+               string const ext = support::getExtension(file);
+               vector<const Format *> found_formats;
+
+               // Find all formats that have the correct extension.
+               vector<const Format *> const & import_formats 
+                       = theConverters().importableFormats();
+               vector<const Format *>::const_iterator it = import_formats.begin();
+               for (; it != import_formats.end(); ++it)
+                       if ((*it)->extension() == ext)
+                               found_formats.push_back(*it);
+
+               FuncRequest cmd;
+               if (found_formats.size() >= 1) {
+                       if (found_formats.size() > 1) {
+                               //FIXME: show a dialog to choose the correct importable format
+                               LYXERR(Debug::FILES,
+                                       "Multiple importable formats found, selecting first");
+                       }
+                       string const arg = found_formats[0]->name() + " " + file;
+                       cmd = FuncRequest(LFUN_BUFFER_IMPORT, arg);
+               } 
+               else {
+                       //FIXME: do we have to explicitly check whether it's a lyx file?
+                       LYXERR(Debug::FILES,
+                               "No formats found, trying to open it as a lyx file");
+                       cmd = FuncRequest(LFUN_FILE_OPEN, file);
                }
+
+               // Asynchronously post the event. DropEvent usually comes
+               // from the BufferView. But reloading a file might close
+               // the BufferView from within its own event handler.
+               guiApp->dispatchDelayed(cmd);
+               event->accept();
        }
 }
 
@@ -623,8 +746,15 @@ void GuiView::message(docstring const & str)
 {
        if (ForkedProcess::iAmAChild())
                return;
+       
+       // call is moved to GUI-thread by GuiProgress
+       d.progress_->appendMessage(toqstr(str));
+}
 
-       statusBar()->showMessage(toqstr(str));
+
+void GuiView::updateMessage(QString const & str)
+{
+       statusBar()->showMessage(str);
        d.statusbar_timer_.stop();
        d.statusbar_timer_.start(3000);
 }
@@ -650,17 +780,20 @@ void GuiView::bigSizedIcons()
 
 void GuiView::clearMessage()
 {
-       if (!hasFocus())
-               return;
-       theLyXFunc().setLyXView(this);
-       statusBar()->showMessage(toqstr(theLyXFunc().viewStatusMessage()));
+       // FIXME: This code was introduced in r19643 to fix bug #4123. However,
+       // the hasFocus function mostly returns false, even if the focus is on
+       // a workarea in this view.
+       //if (!hasFocus())
+       //      return;
+       showMessage();
        d.statusbar_timer_.stop();
 }
 
 
 void GuiView::updateWindowTitle(GuiWorkArea * wa)
 {
-       if (wa != d.current_work_area_)
+       if (wa != d.current_work_area_
+           || wa->bufferView().buffer().isInternal())
                return;
        setWindowTitle(qt_("LyX: ") + wa->windowTitle());
        setWindowIconText(wa->windowIconText());
@@ -705,6 +838,7 @@ void GuiView::on_lastWorkAreaRemoved()
        updateDialogs();
 
        resetWindowTitleAndIconText();
+       updateStatusBar();
 
        if (lyxrc.open_buffers_in_tabs)
                // Nothing more to do, the window should stay open.
@@ -730,14 +864,21 @@ void GuiView::updateStatusBar()
        if (d.statusbar_timer_.isActive())
                return;
 
-       theLyXFunc().setLyXView(this);
-       statusBar()->showMessage(toqstr(theLyXFunc().viewStatusMessage()));
+       showMessage();
 }
 
 
-bool GuiView::hasFocus() const
+void GuiView::showMessage()
 {
-       return qApp->activeWindow() == this;
+       QString msg = toqstr(theGuiApp()->viewStatusMessage());
+       if (msg.isEmpty()) {
+               BufferView const * bv = currentBufferView();
+               if (bv)
+                       msg = toqstr(bv->cursor().currentState());
+               else
+                       msg = qt_("Welcome to LyX!");
+       }
+       statusBar()->showMessage(msg);
 }
 
 
@@ -761,12 +902,17 @@ bool GuiView::event(QEvent * e)
        //      break;
 
        case QEvent::WindowActivate: {
-               if (this == guiApp->currentView()) {
+               GuiView * old_view = guiApp->currentView();
+               if (this == old_view) {
                        setFocus();
                        return QMainWindow::event(e);
                }
+               if (old_view && old_view->currentBufferView()) {
+                       // save current selection to the selection buffer to allow
+                       // middle-button paste in this window.
+                       cap::saveSelection(old_view->currentBufferView()->cursor());
+               }
                guiApp->setCurrentView(this);
-               theLyXFunc().setLyXView(this);
                if (d.current_work_area_) {
                        BufferView & bv = d.current_work_area_->bufferView();
                        connectBufferView(bv);
@@ -800,26 +946,7 @@ bool GuiView::event(QEvent * e)
                        }
                }
 #endif
-
-               if (d.current_work_area_)
-                       // Nothing special to do.
-                       return QMainWindow::event(e);
-
-               QKeyEvent * ke = static_cast<QKeyEvent*>(e);
-               // Let Qt handle menu access and the Tab keys to navigate keys to navigate
-               // between controls.
-               if (ke->modifiers() & Qt::AltModifier || ke->key() == Qt::Key_Tab 
-                       || ke->key() == Qt::Key_Backtab)
-                       return QMainWindow::event(e);
-
-               // Allow processing of shortcuts that are allowed even when no Buffer
-               // is viewed.
-               theLyXFunc().setLyXView(this);
-               KeySymbol sym;
-               setKeySymbol(&sym, ke);
-               theLyXFunc().processKeySym(sym, q_key_state(ke->modifiers()));
-               e->accept();
-               return true;
+               return QMainWindow::event(e);
        }
 
        default:
@@ -905,16 +1032,16 @@ GuiWorkArea * GuiView::currentWorkArea()
 
 GuiWorkArea const * GuiView::currentMainWorkArea() const
 {
-       if (d.currentTabWorkArea() == NULL)
-               return NULL;
+       if (!d.currentTabWorkArea())
+               return 0;
        return d.currentTabWorkArea()->currentWorkArea();
 }
 
 
 GuiWorkArea * GuiView::currentMainWorkArea()
 {
-       if (d.currentTabWorkArea() == NULL)
-               return NULL;
+       if (!d.currentTabWorkArea())
+               return 0;
        return d.currentTabWorkArea()->currentWorkArea();
 }
 
@@ -922,13 +1049,17 @@ GuiWorkArea * GuiView::currentMainWorkArea()
 void GuiView::setCurrentWorkArea(GuiWorkArea * wa)
 {
        LYXERR(Debug::DEBUG, "Setting current wa: " << wa << endl);
-       if (wa == NULL) {
-               d.current_work_area_ = NULL;
+       if (!wa) {
+               d.current_work_area_ = 0;
                d.setBackground();
                return;
        }
-       GuiWorkArea * old_gwa = theGuiApp()->currentView()->currentWorkArea();
-       if (old_gwa == wa)
+
+       // FIXME: I've no clue why this is here and why it accesses
+       //  theGuiApp()->currentView, which might be 0 (bug 6464).
+       //  See also 27525 (vfr).
+       if (theGuiApp()->currentView() == this 
+                 && theGuiApp()->currentView()->currentWorkArea() == wa) 
                return;
 
        if (currentBufferView())
@@ -1039,13 +1170,13 @@ void GuiView::updateToolbars()
 
 void GuiView::setBuffer(Buffer * newBuffer)
 {
-       LYXERR(Debug::DEBUG, "Setting buffer: " << newBuffer << std::endl);
+       LYXERR(Debug::DEBUG, "Setting buffer: " << newBuffer << endl);
        LASSERT(newBuffer, return);
        setBusy(true);
 
        GuiWorkArea * wa = workArea(*newBuffer);
        if (wa == 0) {
-               newBuffer->masterBuffer()->updateLabels();
+               newBuffer->masterBuffer()->updateBuffer();
                wa = addWorkArea(*newBuffer);
        } else {
                //Disconnect the old buffer...there's no new one.
@@ -1098,7 +1229,7 @@ void GuiView::errors(string const & error_type, bool from_master)
 }
 
 
-void GuiView::updateTocItem(std::string const & type, DocIterator const & dit)
+void GuiView::updateTocItem(string const & type, DocIterator const & dit)
 {
        d.toc_models_.updateItem(toqstr(type), dit);
 }
@@ -1156,12 +1287,43 @@ BufferView const * GuiView::currentBufferView() const
 }
 
 
+#if (QT_VERSION >= 0x040400)
+static docstring saveAndDestroyBuffer(Buffer * buffer, FileName const & fname)
+{
+       bool failed = true;
+       FileName const tmp_ret = FileName::tempName("lyxauto");
+       if (!tmp_ret.empty()) {
+               if (buffer->writeFile(tmp_ret))
+                       failed = !tmp_ret.moveTo(fname);
+       }
+       if (failed) {
+               // failed to write/rename tmp_ret so try writing direct
+               failed = buffer->writeFile(fname);
+       }
+       delete buffer;
+       return failed
+               ? _("Automatic save failed!")
+               : _("Automatic save done.");
+}
+#endif
+
+
 void GuiView::autoSave()
 {
        LYXERR(Debug::INFO, "Running autoSave()");
 
-       if (documentBufferView())
-               documentBufferView()->buffer().autoSave();
+       Buffer * buffer = documentBufferView()
+               ? &documentBufferView()->buffer() : 0;
+       if (!buffer)
+               return;
+
+#if (QT_VERSION >= 0x040400)
+       QFuture<docstring> f = QtConcurrent::run(saveAndDestroyBuffer, buffer->clone(),
+               buffer->getAutosaveFilename());
+       d.autosave_watcher_.setFuture(f);
+#else
+       buffer->autoSave();
+#endif
 }
 
 
@@ -1180,6 +1342,15 @@ bool GuiView::getStatus(FuncRequest const & cmd, FuncStatus & flag)
        Buffer * doc_buffer = documentBufferView()
                ? &(documentBufferView()->buffer()) : 0;
 
+       // Check whether we need a buffer
+       if (!lyxaction.funcHasFlag(cmd.action, LyXAction::NoBuffer) && !buf) {
+               // no, exit directly
+               flag.message(from_utf8(N_("Command not allowed with"
+                                   "out any document open")));
+               flag.setEnabled(false);
+               return true;
+       }
+
        if (cmd.origin == FuncRequest::TOC) {
                GuiToc * toc = static_cast<GuiToc*>(findOrBuild("toc", false));
                FuncStatus fs;
@@ -1191,6 +1362,28 @@ bool GuiView::getStatus(FuncRequest const & cmd, FuncStatus & flag)
        }
 
        switch(cmd.action) {
+       case LFUN_BUFFER_IMPORT:
+               break;
+
+       case LFUN_MASTER_BUFFER_UPDATE:
+       case LFUN_MASTER_BUFFER_VIEW: 
+               enable = doc_buffer && doc_buffer->parent() != 0
+                       && !d.preview_watcher_.isRunning();
+               break;
+
+       case LFUN_BUFFER_UPDATE:
+       case LFUN_BUFFER_VIEW: {
+               if (!doc_buffer || d.preview_watcher_.isRunning()) {
+                       enable = false;
+                       break;
+               }
+               string format = to_utf8(cmd.argument());
+               if (cmd.argument().empty())
+                       format = doc_buffer->getDefaultOutputFormat();
+               enable = doc_buffer->isExportableFormat(format);
+               break;
+       }
+
        case LFUN_BUFFER_RELOAD:
                enable = doc_buffer && !doc_buffer->isUnnamed()
                        && doc_buffer->fileName().exists()
@@ -1198,14 +1391,41 @@ bool GuiView::getStatus(FuncRequest const & cmd, FuncStatus & flag)
                           || doc_buffer->isExternallyModified(Buffer::timestamp_method));
                break;
 
+       case LFUN_BUFFER_CHILD_OPEN:
+               enable = doc_buffer;
+               break;
+
        case LFUN_BUFFER_WRITE:
                enable = doc_buffer && (doc_buffer->isUnnamed() || !doc_buffer->isClean());
                break;
 
+       //FIXME: This LFUN should be moved to GuiApplication.
+       case LFUN_BUFFER_WRITE_ALL: {
+               // We enable the command only if there are some modified buffers
+               Buffer * first = theBufferList().first();
+               enable = false;
+               if (!first)
+                       break;
+               Buffer * b = first;
+               // We cannot use a for loop as the buffer list is a cycle.
+               do {
+                       if (!b->isClean()) {
+                               enable = true;
+                               break;
+                       }
+                       b = theBufferList().next(b);
+               } while (b != first); 
+               break;
+       }
+
        case LFUN_BUFFER_WRITE_AS:
                enable = doc_buffer;
                break;
 
+       case LFUN_BUFFER_CLOSE:
+               enable = doc_buffer;
+               break;
+
        case LFUN_BUFFER_CLOSE_ALL:
                enable = theBufferList().last() != theBufferList().first();
                break;
@@ -1223,15 +1443,34 @@ bool GuiView::getStatus(FuncRequest const & cmd, FuncStatus & flag)
                enable = d.currentTabWorkArea();
                break;
 
-       case LFUN_TOOLBAR_TOGGLE:
-               if (GuiToolbar * t = toolbar(cmd.getArg(0)))
+       case LFUN_TOOLBAR_TOGGLE: {
+               string const name = cmd.getArg(0);
+               if (GuiToolbar * t = toolbar(name))
                        flag.setOnOff(t->isVisible());
+               else {
+                       enable = false;
+                       docstring const msg = 
+                               bformat(_("Unknown toolbar \"%1$s\""), from_utf8(name));
+                       flag.message(msg);
+               }
+               break;
+       }
+
+       case LFUN_DROP_LAYOUTS_CHOICE:
+               enable = buf; 
                break;
 
        case LFUN_UI_TOGGLE:
                flag.setOnOff(isFullScreen());
                break;
 
+       case LFUN_DIALOG_DISCONNECT_INSET:
+               break;
+
+       case LFUN_DIALOG_HIDE:
+               // FIXME: should we check if the dialog is shown?
+               break;
+
        case LFUN_DIALOG_TOGGLE:
                flag.setOnOff(isDialogVisible(cmd.getArg(0)));
                // fall through to set "enable"
@@ -1241,7 +1480,9 @@ bool GuiView::getStatus(FuncRequest const & cmd, FuncStatus & flag)
                        enable = name == "aboutlyx"
                                || name == "file" //FIXME: should be removed.
                                || name == "prefs"
-                               || name == "texinfo";
+                               || name == "texinfo"
+                               || name == "progress"
+                               || name == "compare";
                else if (name == "print")
                        enable = doc_buffer->isExportable("dvi")
                                && lyxrc.print_command != "none";
@@ -1272,24 +1513,11 @@ bool GuiView::getStatus(FuncRequest const & cmd, FuncStatus & flag)
                break;
        }
 
-       case LFUN_INSET_APPLY: {
-               string const name = cmd.getArg(0);
-               Inset * inset = getOpenInset(name);
-               if (inset) {
-                       FuncRequest fr(LFUN_INSET_MODIFY, cmd.argument());
-                       FuncStatus fs;
-                       if (!inset->getStatus(currentBufferView()->cursor(), fr, fs)) {
-                               // Every inset is supposed to handle this
-                               LASSERT(false, break);
-                       }
-                       flag |= fs;
-               } else {
-                       FuncRequest fr(LFUN_INSET_INSERT, cmd.argument());
-                       flag |= lyx::getStatus(fr);
-               }
-               enable = flag.enabled();
+       case LFUN_COMMAND_EXECUTE:
+       case LFUN_MESSAGE:
+       case LFUN_MENU_OPEN:
+               // Nothing to check.
                break;
-       }
 
        case LFUN_COMPLETION_INLINE:
                if (!d.current_work_area_
@@ -1335,6 +1563,10 @@ bool GuiView::getStatus(FuncRequest const & cmd, FuncStatus & flag)
                enable = doc_buffer;
                break;
        
+       case LFUN_BUFFER_NEXT:
+       case LFUN_BUFFER_PREVIOUS:
+               // FIXME: should we check is there is an previous or next buffer?
+               break;
        case LFUN_BUFFER_SWITCH:
                // toggle on the current buffer, but do not toggle off
                // the other ones (is that a good idea?)
@@ -1355,7 +1587,7 @@ bool GuiView::getStatus(FuncRequest const & cmd, FuncStatus & flag)
        case LFUN_VC_LOCKING_TOGGLE:
                enable = doc_buffer && !doc_buffer->isReadonly()
                        && doc_buffer->lyxvc().lockingToggleEnabled();
-               flag.setOnOff(enable && !doc_buffer->lyxvc().locker().empty());
+               flag.setOnOff(enable && doc_buffer->lyxvc().locking());
                break;
        case LFUN_VC_REVERT:
                enable = doc_buffer && doc_buffer->lyxvc().inUse();
@@ -1363,6 +1595,9 @@ bool GuiView::getStatus(FuncRequest const & cmd, FuncStatus & flag)
        case LFUN_VC_UNDO_LAST:
                enable = doc_buffer && doc_buffer->lyxvc().undoLastEnabled();
                break;
+       case LFUN_VC_REPO_UPDATE:
+               enable = doc_buffer && doc_buffer->lyxvc().inUse();
+               break;
        case LFUN_VC_COMMAND: {
                if (cmd.argument().empty())
                        enable = false;
@@ -1370,6 +1605,13 @@ bool GuiView::getStatus(FuncRequest const & cmd, FuncStatus & flag)
                        enable = false;
                break;
        }
+       case LFUN_VC_COMPARE:
+               enable = doc_buffer && !cmd.argument().empty()
+                        && doc_buffer->lyxvc().prepareFileRevisionEnabled();
+               break;
+
+       case LFUN_SERVER_GOTO_FILE_ROW:
+               break;
 
        default:
                return false;
@@ -1496,7 +1738,7 @@ void GuiView::openDocument(string const & fname)
        docstring str2;
        Buffer * buf = loadDocument(fullname);
        if (buf) {
-               buf->updateLabels();
+               buf->updateBuffer();
                setBuffer(buf);
                buf->errors("Parse");
                str2 = bformat(_("Document %1$s opened."), disp_fn);
@@ -1545,7 +1787,7 @@ static bool import(GuiView * lv, FileName const & filename,
                Buffer * buf = lv->loadDocument(lyxfile);
                if (!buf)
                        return false;
-               buf->updateLabels();
+               buf->updateBuffer();
                lv->setBuffer(buf);
                buf->errors("Parse");
        } else {
@@ -1559,7 +1801,7 @@ static bool import(GuiView * lv, FileName const & filename,
                                          formats.extension(loader_format));
                lv->currentBufferView()->insertPlaintextFile(FileName(filename2),
                        as_paragraphs);
-               theLyXFunc().setLyXView(lv);
+               guiApp->setCurrentView(lv);
                lyx::dispatch(FuncRequest(LFUN_MARK_OFF));
        }
 
@@ -1676,7 +1918,7 @@ void GuiView::newDocument(string const & filename, bool from_template)
        
        Buffer * b;
        if (filename.empty())
-               b = newUnnamedFile(templatefile, initpath);
+               b = newUnnamedFile(initpath, to_utf8(_("newfile")), templatefile);
        else
                b = newFile(filename, templatefile, true);
 
@@ -1916,6 +2158,7 @@ bool GuiView::closeWorkArea(GuiWorkArea * wa)
 bool GuiView::closeBuffer()
 {
        GuiWorkArea * wa = currentMainWorkArea();
+       setCurrentWorkArea(wa);
        Buffer & buf = wa->bufferView().buffer();
        return wa && closeWorkArea(wa, !buf.parent());
 }
@@ -1983,14 +2226,31 @@ bool GuiView::closeWorkAreaAll()
 
 bool GuiView::closeWorkArea(GuiWorkArea * wa, bool close_buffer)
 {
+       if (!wa)
+               return false;
+
        Buffer & buf = wa->bufferView().buffer();
 
+       if (close_buffer)
+               return closeBuffer(buf);
+       else {
+               if (!inMultiTabs(wa))
+                       if (!saveBufferIfNeeded(buf, true))
+                               return false;
+               removeWorkArea(wa);
+               return true;
+       }
+}
+
+
+bool GuiView::closeBuffer(Buffer & buf)
+{
        // If we are in a close_event all children will be closed in some time,
        // so no need to do it here. This will ensure that the children end up
        // in the session file in the correct order. If we close the master
        // buffer, we can close or release the child buffers here too.
-       if (close_buffer && !closing_) {
-               vector<Buffer *> clist = buf.getChildren();
+       if (!closing_) {
+               vector<Buffer *> clist = buf.getChildren(false);
                for (vector<Buffer *>::const_iterator it = clist.begin();
                         it != clist.end(); ++it) {
                        // If a child is dirty, do not close
@@ -2009,20 +2269,10 @@ bool GuiView::closeWorkArea(GuiWorkArea * wa, bool close_buffer)
        //FIXME: we should update only the bookmarks related to this buffer!
        LYXERR(Debug::DEBUG, "GuiView::closeBuffer()");
        for (size_t i = 0; i < theSession().bookmarks().size(); ++i)
-               theLyXFunc().gotoBookmark(i+1, false, false);
-
-       // if we are only hiding the buffer and there are multiple views
-       // of the buffer, then we do not need to ensure a clean buffer.
-       bool const allow_dirty = inMultiTabs(wa) && !close_buffer;
-
-       if (allow_dirty || saveBufferIfNeeded(buf, !close_buffer)) {
-               // save in sessions if requested
-               // do not save childs if their master
-               // is opened as well
-               if (!close_buffer)
-                       removeWorkArea(wa);
-               else
-                       theBufferList().release(&buf);
+               guiApp->gotoBookmark(i+1, false, false);
+
+       if (saveBufferIfNeeded(buf, false)) {
+               theBufferList().release(&buf);
                return true;
        }
        return false;
@@ -2097,7 +2347,7 @@ bool GuiView::saveBufferIfNeeded(Buffer & buf, bool hiding)
                buf.removeAutosaveFile();
                if (hiding)
                        // revert all changes
-                       buf.loadLyXFile(buf.fileName());
+                       buf.reload();
                buf.markClean();
                break;
        case 2:
@@ -2194,27 +2444,34 @@ static bool ensureBufferClean(Buffer * buffer)
 void GuiView::reloadBuffer()
 {
        Buffer * buf = &documentBufferView()->buffer();
-       FileName filename = buf->fileName();
-       // The user has already confirmed that the changes, if any, should
-       // be discarded. So we just release the Buffer and don't call closeBuffer();
-       theBufferList().release(buf);
-       buf = loadDocument(filename);
-       docstring const disp_fn = makeDisplayPath(filename.absFilename());
-       docstring str;
-       if (buf) {
-               buf->updateLabels();
-               setBuffer(buf);
-               buf->errors("Parse");
-               str = bformat(_("Document %1$s reloaded."), disp_fn);
-       } else {
-               str = bformat(_("Could not reload document %1$s"), disp_fn);
+       buf->reload();
+}
+
+
+void GuiView::checkExternallyModifiedBuffers()
+{
+       BufferList::iterator bit = theBufferList().begin();
+       BufferList::iterator const bend = theBufferList().end();
+       for (; bit != bend; ++bit) {
+               if ((*bit)->fileName().exists()
+                   && (*bit)->isExternallyModified(Buffer::checksum_method)) {
+                       docstring text = bformat(_("Document \n%1$s\n has been externally modified."
+                                       " Reload now? Any local changes will be lost."),
+                                       from_utf8((*bit)->absFileName()));
+                       int const ret = Alert::prompt(_("Reload externally changed document?"),
+                                               text, 0, 1, _("&Reload"), _("&Cancel"));
+                       if (!ret)
+                               (*bit)->reload();
+               }
        }
-       message(str);
 }
 
 
+//FIXME use a DispatchResult object to transmit messages
 void GuiView::dispatchVC(FuncRequest const & cmd)
 {
+       // message for statusbar
+       string msg;
        Buffer * buffer = documentBufferView()
                ? &(documentBufferView()->buffer()) : 0;
 
@@ -2232,8 +2489,9 @@ void GuiView::dispatchVC(FuncRequest const & cmd)
                if (!buffer || !ensureBufferClean(buffer))
                        break;
                if (buffer->lyxvc().inUse() && !buffer->isReadonly()) {
-                       message(from_utf8(buffer->lyxvc().checkIn()));
-                       reloadBuffer();
+                       msg = buffer->lyxvc().checkIn();
+                       if (!msg.empty())
+                               reloadBuffer();
                }
                break;
 
@@ -2241,7 +2499,7 @@ void GuiView::dispatchVC(FuncRequest const & cmd)
                if (!buffer || !ensureBufferClean(buffer))
                        break;
                if (buffer->lyxvc().inUse()) {
-                       message(from_utf8(buffer->lyxvc().checkOut()));
+                       msg = buffer->lyxvc().checkOut();
                        reloadBuffer();
                }
                break;
@@ -2256,7 +2514,7 @@ void GuiView::dispatchVC(FuncRequest const & cmd)
                                frontend::Alert::error(_("Revision control error."),
                                _("Error when setting the locking property."));
                        } else {
-                               message(from_utf8(res));
+                               msg = res;
                                reloadBuffer();
                        }
                }
@@ -2274,6 +2532,14 @@ void GuiView::dispatchVC(FuncRequest const & cmd)
                reloadBuffer();
                break;
 
+       case LFUN_VC_REPO_UPDATE:
+               LASSERT(buffer, return);
+               if (ensureBufferClean(buffer)) {
+                       msg = buffer->lyxvc().repoUpdate();
+                       checkExternallyModifiedBuffers();
+               }
+               break;
+
        case LFUN_VC_COMMAND: {
                string flag = cmd.getArg(0);
                if (buffer && contains(flag, 'R') && !ensureBufferClean(buffer))
@@ -2315,41 +2581,295 @@ void GuiView::dispatchVC(FuncRequest const & cmd)
 
                break;
                }
+
+       case LFUN_VC_COMPARE: {
+
+               string rev1 = cmd.getArg(0);
+               string f1, f2;
+
+               // f1
+               if (!buffer->lyxvc().prepareFileRevision(rev1, f1))
+                       break;
+
+               if (isStrInt(rev1) && convert<int>(rev1) <= 0) {
+                       f2 = buffer->absFileName();
+               } else {
+                       string rev2 = cmd.getArg(1);
+                       if (rev2.empty())
+                               break;
+                       // f2
+                       if (!buffer->lyxvc().prepareFileRevision(rev2, f2))
+                               break;
+               }
+               // FIXME We need to call comparison feature here.
+               // This is quick and dirty code for testing VC.
+               // We need that comparison feature has some LFUN_COMPARE <FLAG> file1 file1
+               // where <FLAG> specifies whether we want GUI dialog or just launch
+               // running with defaults.
+               /*
+               FileName initpath(lyxrc.document_path);
+               Buffer * dest = newUnnamedFile(initpath, to_utf8(_("differences")));
+               CompareOptions options;
+               Compare * compare = new Compare(loadIfNeeded(FileName(f1)), loadIfNeeded(FileName(f2)), dest, options);
+               compare->start(QThread::LowPriority);
+               Sleep::millisec(200);
+               lyx::dispatch(FuncRequest(LFUN_BUFFER_SWITCH, dest->absFileName()));
+               */
+               break;
+       }
+
+       default:
+               break;
+       }
+
+       if (!msg.empty())
+               message(from_utf8(msg));
+}
+
+
+void GuiView::openChildDocument(string const & fname)
+{
+       LASSERT(documentBufferView(), return);
+       Buffer & buffer = documentBufferView()->buffer();
+       FileName const filename = support::makeAbsPath(fname, buffer.filePath());
+       documentBufferView()->saveBookmark(false);
+       Buffer * child = 0;
+       bool parsed = false;
+       if (theBufferList().exists(filename)) {
+               child = theBufferList().getBuffer(filename);
+       } else {
+               message(bformat(_("Opening child document %1$s..."),
+               makeDisplayPath(filename.absFilename())));
+               child = loadDocument(filename, false);
+               parsed = true;
+       }
+       if (!child)
+               return;
+
+       // 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.
+       child->setParent(&buffer);
+       child->masterBuffer()->updateBuffer();
+       setBuffer(child);
+       if (parsed)
+               child->errors("Parse");
+}
+
+
+bool GuiView::goToFileRow(string const & argument)
+{
+       string file_name;
+       int row;
+       size_t i = argument.find_last_of(' ');
+       if (i != string::npos) {
+               file_name = os::internal_path(trim(argument.substr(0, i)));
+               istringstream is(argument.substr(i + 1));
+               is >> row;
+               if (is.fail())
+                       i = string::npos;
+       }
+       if (i == string::npos) {
+               LYXERR0("Wrong argument: " << argument);
+               return false;
+       }
+       Buffer * buf = 0;
+       string const abstmp = package().temp_dir().absFilename();
+       string const realtmp = package().temp_dir().realPath();
+       // We have to use os::path_prefix_is() here, instead of
+       // simply prefixIs(), because the file name comes from
+       // an external application and may need case adjustment.
+       if (os::path_prefix_is(file_name, abstmp, os::CASE_ADJUSTED)
+               || os::path_prefix_is(file_name, realtmp, os::CASE_ADJUSTED)) {
+               // Needed by inverse dvi search. If it is a file
+               // in tmpdir, call the apropriated function.
+               // If tmpdir is a symlink, we may have the real
+               // path passed back, so we correct for that.
+               if (!prefixIs(file_name, abstmp))
+                       file_name = subst(file_name, realtmp, abstmp);
+               buf = theBufferList().getBufferFromTmp(file_name);
+       } else {
+               // Must replace extension of the file to be .lyx
+               // and get full path
+               FileName const s = fileSearch(string(),
+                                             support::changeExtension(file_name, ".lyx"), "lyx");
+               // Either change buffer or load the file
+               if (theBufferList().exists(s))
+                       buf = theBufferList().getBuffer(s);
+               else if (s.exists()) {
+                       buf = loadDocument(s);
+                       buf->updateBuffer();
+                       buf->errors("Parse");
+               } else {
+                       message(bformat(
+                                       _("File does not exist: %1$s"),
+                                       makeDisplayPath(file_name)));
+                       return false;
+               }
        }
+       setBuffer(buf);
+       documentBufferView()->setCursorFromRow(row);
+       return true;
 }
 
 
-bool GuiView::dispatch(FuncRequest const & cmd)
+#if (QT_VERSION >= 0x040400)
+static docstring exportAndDestroy(Buffer * buffer, string const & format)
+{
+       bool const update_unincluded =
+                               buffer->params().maintain_unincluded_children
+                               && !buffer->params().getIncludedChildren().empty();
+       bool const success = buffer->doExport(format, true, update_unincluded);
+       delete buffer;
+       return success
+               ? bformat(_("Successful export to format: %1$s"), from_utf8(format))
+               : bformat(_("Error exporting to format: %1$s"), from_utf8(format));
+}
+
+
+static docstring previewAndDestroy(Buffer * buffer, string const & format)
+{
+       bool const update_unincluded =
+                               buffer->params().maintain_unincluded_children
+                               && !buffer->params().getIncludedChildren().empty();
+       bool const success = buffer->preview(format, update_unincluded);
+       delete buffer;
+       return success
+               ? bformat(_("Successful preview of format: %1$s"), from_utf8(format))
+               : bformat(_("Error previewing format: %1$s"), from_utf8(format));
+}
+#endif
+
+
+void GuiView::dispatch(FuncRequest const & cmd, DispatchResult & dr)
 {
        BufferView * bv = currentBufferView();
        // By default we won't need any update.
-       if (bv)
-               bv->cursor().updateFlags(Update::None);
+       dr.update(Update::None);
+       // assume cmd will be dispatched
+       dr.dispatched(true);
 
        Buffer * doc_buffer = documentBufferView()
                ? &(documentBufferView()->buffer()) : 0;
 
-       bool dispatched = true;
-
        if (cmd.origin == FuncRequest::TOC) {
                GuiToc * toc = static_cast<GuiToc*>(findOrBuild("toc", false));
+               // FIXME: do we need to pass a DispatchResult object here?
                toc->doDispatch(bv->cursor(), cmd);
-               return true;
+               return;
        }
 
+       string const argument = to_utf8(cmd.argument());
+
        switch(cmd.action) {
+               case LFUN_BUFFER_CHILD_OPEN:
+                       openChildDocument(to_utf8(cmd.argument()));
+                       break;
+
                case LFUN_BUFFER_IMPORT:
                        importDocument(to_utf8(cmd.argument()));
                        break;
 
+               case LFUN_BUFFER_EXPORT: {
+                       if (!doc_buffer)
+                               break;
+                       // GCC only sees strfwd.h when building merged
+                       if (::lyx::operator==(cmd.argument(), "custom")) {
+                               dispatch(FuncRequest(LFUN_DIALOG_SHOW, "sendto"), 
+                                        dr);
+                               break;
+                       }
+                       if (!doc_buffer->doExport(argument, false)) {
+                               dr.setError(true);
+                               dr.setMessage(bformat(_("Error exporting to format: %1$s."),
+                                       cmd.argument()));
+                       }
+                       break;
+               }
+
+               case LFUN_BUFFER_UPDATE: {
+                       if (!doc_buffer)
+                               break;
+                       string format = argument;
+                       if (argument.empty())
+                               format = doc_buffer->getDefaultOutputFormat();
+#if EXPORT_in_THREAD && (QT_VERSION >= 0x040400)
+                       d.progress_->clearMessages();
+                       message(_("Exporting ..."));
+                       QFuture<docstring> f = QtConcurrent::run(exportAndDestroy,
+                               doc_buffer->clone(), format);
+                       d.setPreviewFuture(f);
+#else
+                       bool const update_unincluded =
+                               doc_buffer->params().maintain_unincluded_children
+                               && !doc_buffer->params().getIncludedChildren().empty();
+                       doc_buffer->doExport(format, true, update_unincluded);
+#endif
+                       break;
+               }
+               case LFUN_BUFFER_VIEW: {
+                       if (!doc_buffer)
+                               break;
+                       string format = argument;
+                       if (argument.empty())
+                               format = doc_buffer->getDefaultOutputFormat();
+#if EXPORT_in_THREAD && (QT_VERSION >= 0x040400)
+                       d.progress_->clearMessages();
+                       message(_("Previewing ..."));
+                       QFuture<docstring> f = QtConcurrent::run(previewAndDestroy,
+                               doc_buffer->clone(), format);
+                       d.setPreviewFuture(f);
+#else
+                       bool const update_unincluded =
+                               doc_buffer->params().maintain_unincluded_children
+                               && !doc_buffer->params().getIncludedChildren().empty();
+                       doc_buffer->preview(format, update_unincluded);
+#endif
+                       break;
+               }
+               case LFUN_MASTER_BUFFER_UPDATE: {
+                       if (!doc_buffer)
+                               break;
+                       string format = argument;
+                       Buffer const * master = doc_buffer->masterBuffer();
+                       if (argument.empty())
+                               format = master->getDefaultOutputFormat();
+#if EXPORT_in_THREAD && (QT_VERSION >= 0x040400)
+                       QFuture<docstring> f = QtConcurrent::run(exportAndDestroy,
+                               master->clone(), format);
+                       d.setPreviewFuture(f);
+#else
+                       bool const update_unincluded =
+                               master->params().maintain_unincluded_children
+                               && !master->params().getIncludedChildren().empty();
+                       master->doExport(format, true);
+#endif
+                       break;
+               }
+               case LFUN_MASTER_BUFFER_VIEW: {
+                       string format = argument;
+                       Buffer const * master = doc_buffer->masterBuffer();
+                       if (argument.empty())
+                               format = master->getDefaultOutputFormat();
+#if EXPORT_in_THREAD && (QT_VERSION >= 0x040400)
+                       QFuture<docstring> f = QtConcurrent::run(previewAndDestroy,
+                               master->clone(), format);
+                       d.setPreviewFuture(f);
+#else
+                       master->preview(format);
+#endif
+                       break;
+               }
                case LFUN_BUFFER_SWITCH:
                        if (FileName::isAbsolute(to_utf8(cmd.argument()))) {
                                Buffer * buffer = 
                                        theBufferList().getBuffer(FileName(to_utf8(cmd.argument())));
                                if (buffer)
                                        setBuffer(buffer);
-                               else
-                                       bv->cursor().message(_("Document not loaded"));
+                               else {
+                                       dr.setError(true);
+                                       dr.setMessage(_("Document not loaded"));
+                               }
                        }
                        break;
 
@@ -2400,8 +2920,10 @@ bool GuiView::dispatch(FuncRequest const & cmd)
                        int const ret = Alert::prompt(_("Revert to saved document?"),
                                text, 1, 1, _("&Revert"), _("&Cancel"));
 
-                       if (ret == 0)
+                       if (ret == 0) {
+                               doc_buffer->markClean();
                                reloadBuffer();
+                       }
                        break;
                }
 
@@ -2429,10 +2951,18 @@ bool GuiView::dispatch(FuncRequest const & cmd)
                                }
                                b = theBufferList().next(b);
                        } while (b != first); 
-                       message(_("All documents saved."));
+                       dr.setMessage(_("All documents saved."));
                        break;
                }
 
+               case LFUN_BUFFER_CLOSE:
+                       closeBuffer();
+                       break;
+
+               case LFUN_BUFFER_CLOSE_ALL:
+                       closeBufferAll();
+                       break;
+
                case LFUN_TOOLBAR_TOGGLE: {
                        string const name = cmd.getArg(0);
                        if (GuiToolbar * t = toolbar(name))
@@ -2442,10 +2972,15 @@ bool GuiView::dispatch(FuncRequest const & cmd)
 
                case LFUN_DIALOG_UPDATE: {
                        string const name = to_utf8(cmd.argument());
-                       // Can only update a dialog connected to an existing inset
-                       Inset * inset = getOpenInset(name);
-                       if (inset) {
+                       if (currentBufferView()) {
+                               Inset * inset = currentBufferView()->editedInset(name);
+                               // Can only update a dialog connected to an existing inset
+                               if (!inset)
+                                       break;
+                               // FIXME: get rid of this indirection; GuiView ask the inset
+                               // if he is kind enough to update itself...
                                FuncRequest fr(LFUN_INSET_DIALOG_UPDATE, cmd.argument());
+                               //FIXME: pass DispatchResult here?
                                inset->dispatch(currentBufferView()->cursor(), fr);
                        } else if (name == "paragraph") {
                                lyx::dispatch(FuncRequest(LFUN_PARAGRAPH_UPDATE));
@@ -2457,9 +2992,9 @@ bool GuiView::dispatch(FuncRequest const & cmd)
 
                case LFUN_DIALOG_TOGGLE: {
                        if (isDialogVisible(cmd.getArg(0)))
-                               dispatch(FuncRequest(LFUN_DIALOG_HIDE, cmd.argument()));
+                               dispatch(FuncRequest(LFUN_DIALOG_HIDE, cmd.argument()), dr);
                        else
-                               dispatch(FuncRequest(LFUN_DIALOG_SHOW, cmd.argument()));
+                               dispatch(FuncRequest(LFUN_DIALOG_SHOW, cmd.argument()), dr);
                        break;
                }
 
@@ -2503,41 +3038,27 @@ bool GuiView::dispatch(FuncRequest const & cmd)
                                        showDialog("symbols", data);
                        // bug 5274
                        } else if (name == "prefs" && isFullScreen()) {
-                               FuncRequest fr(LFUN_INSET_INSERT, "fullscreen");
-                               lfunUiToggle(fr);
+                               lfunUiToggle("fullscreen");
                                showDialog("prefs", data);
                        } else
                                showDialog(name, data);
                        break;
                }
 
-               case LFUN_INSET_APPLY: {
-                       string const name = cmd.getArg(0);
-                       Inset * inset = getOpenInset(name);
-                       if (inset) {
-                               // put cursor in front of inset.
-                               if (!currentBufferView()->setCursorFromInset(inset)) {
-                                       LASSERT(false, break);
-                               }
-                               BufferView * bv = currentBufferView();
-                               // useful if we are called from a dialog.
-                               bv->cursor().beginUndoGroup();
-                               bv->cursor().recordUndo();
-                               FuncRequest fr(LFUN_INSET_MODIFY, cmd.argument());
-                               inset->dispatch(bv->cursor(), fr);
-                               bv->cursor().endUndoGroup();
-                       } else {
-                               FuncRequest fr(LFUN_INSET_INSERT, cmd.argument());
-                               lyx::dispatch(fr);
-                       }
+               case LFUN_MESSAGE:
+                       dr.setMessage(cmd.argument());
                        break;
-               }
 
-               case LFUN_UI_TOGGLE:
-                       lfunUiToggle(cmd);
+               case LFUN_UI_TOGGLE: {
+                       string arg = cmd.getArg(0);
+                       if (!lfunUiToggle(arg)) {
+                               docstring const msg = "ui-toggle " + _("%1$s unknown command!");
+                               dr.setMessage(bformat(msg, from_utf8(arg)));
+                       }
                        // Make sure the keyboard focus stays in the work area.
                        setFocus();
                        break;
+               }
 
                case LFUN_SPLIT_VIEW: {
                        LASSERT(doc_buffer, break);
@@ -2617,15 +3138,21 @@ bool GuiView::dispatch(FuncRequest const & cmd)
                case LFUN_VC_REGISTER:
                case LFUN_VC_CHECK_IN:
                case LFUN_VC_CHECK_OUT:
+               case LFUN_VC_REPO_UPDATE:
                case LFUN_VC_LOCKING_TOGGLE:
                case LFUN_VC_REVERT:
                case LFUN_VC_UNDO_LAST:
                case LFUN_VC_COMMAND:
+               case LFUN_VC_COMPARE:
                        dispatchVC(cmd);
                        break;
 
+               case LFUN_SERVER_GOTO_FILE_ROW:
+                       goToFileRow(to_utf8(cmd.argument()));
+                       break;
+
                default:
-                       dispatched = false;
+                       dr.dispatched(false);
                        break;
        }
 
@@ -2637,14 +3164,13 @@ bool GuiView::dispatch(FuncRequest const & cmd)
                        statusBar()->hide();
        }
 
-       return dispatched;
+       return;
 }
 
 
-void GuiView::lfunUiToggle(FuncRequest const & cmd)
+bool GuiView::lfunUiToggle(string const & ui_component)
 {
-       string const arg = cmd.getArg(0);
-       if (arg == "scrollbar") {
+       if (ui_component == "scrollbar") {
                // hide() is of no help
                if (d.current_work_area_->verticalScrollBarPolicy() ==
                        Qt::ScrollBarAlwaysOff)
@@ -2654,18 +3180,13 @@ void GuiView::lfunUiToggle(FuncRequest const & cmd)
                else
                        d.current_work_area_->setVerticalScrollBarPolicy(
                                Qt::ScrollBarAlwaysOff);
-               return;
-       }
-       if (arg == "statusbar") {
+       } else if (ui_component == "statusbar") {
                statusBar()->setVisible(!statusBar()->isVisible());
-               return;
-       }
-       if (arg == "menubar") {
+       } else if (ui_component == "menubar") {
                menuBar()->setVisible(!menuBar()->isVisible());
-               return;
-       }
+       } else
 #if QT_VERSION >= 0x040300
-       if (arg == "frame") {
+       if (ui_component == "frame") {
                int l, t, r, b;
                getContentsMargins(&l, &t, &r, &b);
                //are the frames in default state?
@@ -2675,15 +3196,13 @@ void GuiView::lfunUiToggle(FuncRequest const & cmd)
                } else {
                        setContentsMargins(0, 0, 0, 0);
                }
-               return;
-       }
+       } else
 #endif
-       if (arg == "fullscreen") {
+       if (ui_component == "fullscreen") {
                toggleFullScreen();
-               return;
-       }
-
-       message(bformat("LFUN_UI_TOGGLE " + _("%1$s unknown command!"), from_utf8(arg)));
+       } else
+               return false;
+       return true;
 }
 
 
@@ -2766,13 +3285,13 @@ namespace {
 
 char const * const dialognames[] = {
 "aboutlyx", "bibitem", "bibtex", "box", "branch", "changes", "character",
-"citation", "document", "errorlist", "ert", "external", "file", "findreplace",
-"findreplaceadv", "float", "graphics", "href", "include", "index",
-"index_print", "info", "listings", "label", "log", "mathdelimiter",
+"citation", "compare", "document", "errorlist", "ert", "external", "file",
+"findreplace", "findreplaceadv", "float", "graphics", "href", "include",
+"index", "index_print", "info", "listings", "label", "log", "mathdelimiter",
 "mathmatrix", "mathspace", "nomenclature", "nomencl_print", "note",
 "paragraph", "phantom", "prefs", "print", "ref", "sendto", "space",
 "spellchecker", "symbols", "tabular", "tabularcreate", "thesaurus", "texinfo",
-"toc", "view-source", "vspace", "wrap" };
+"toc", "view-source", "vspace", "wrap", "progress"};
 
 char const * const * const end_dialognames =
        dialognames + (sizeof(dialognames) / sizeof(char *));
@@ -2799,15 +3318,15 @@ bool isValidName(string const & name)
 
 void GuiView::resetDialogs()
 {
-       // Make sure that no LFUN uses any LyXView.
-       theLyXFunc().setLyXView(0);
+       // Make sure that no LFUN uses any GuiView.
+       guiApp->setCurrentView(0);
        saveLayout();
        menuBar()->clear();
        constructToolbars();
        guiApp->menus().fillMenuBar(menuBar(), this, false);
        d.layout_->updateContents(true);
        // Now update controls with current buffer.
-       theLyXFunc().setLyXView(this);
+       guiApp->setCurrentView(this);
        restoreLayout();
        restartCursor();
 }
@@ -2838,17 +3357,27 @@ Dialog * GuiView::findOrBuild(string const & name, bool hide_it)
 
 void GuiView::showDialog(string const & name, string const & data,
        Inset * inset)
+{
+       triggerShowDialog(toqstr(name), toqstr(data), inset);
+}
+
+
+void GuiView::doShowDialog(QString const & qname, QString const & qdata,
+       Inset * inset)
 {
        if (d.in_show_)
                return;
 
+       const string name = fromqstr(qname);
+       const string data = fromqstr(qdata);
+
        d.in_show_ = true;
        try {
                Dialog * dialog = findOrBuild(name, false);
                if (dialog) {
                        dialog->showData(data);
-                       if (inset)
-                               d.open_insets_[name] = inset;
+                       if (inset && currentBufferView())
+                               currentBufferView()->editInset(name, inset);
                }
        }
        catch (ExceptionMessage const & ex) {
@@ -2874,13 +3403,15 @@ void GuiView::hideDialog(string const & name, Inset * inset)
        if (it == d.dialogs_.end())
                return;
 
-       if (inset && inset != getOpenInset(name))
+       if (inset && currentBufferView()
+               && inset != currentBufferView()->editedInset(name))
                return;
 
        Dialog * const dialog = it->second.get();
        if (dialog->isVisibleView())
                dialog->hideView();
-       d.open_insets_[name] = 0;
+       if (currentBufferView())
+               currentBufferView()->editInset(name, 0);
 }
 
 
@@ -2888,19 +3419,8 @@ void GuiView::disconnectDialog(string const & name)
 {
        if (!isValidName(name))
                return;
-
-       if (d.open_insets_.find(name) != d.open_insets_.end())
-               d.open_insets_[name] = 0;
-}
-
-
-Inset * GuiView::getOpenInset(string const & name) const
-{
-       if (!isValidName(name))
-               return 0;
-
-       map<string, Inset *>::const_iterator it = d.open_insets_.find(name);
-       return it == d.open_insets_.end() ? 0 : it->second;
+       if (currentBufferView())
+               currentBufferView()->editInset(name, 0);
 }
 
 
@@ -2921,37 +3441,36 @@ void GuiView::updateDialogs()
 
        for(; it != end; ++it) {
                Dialog * dialog = it->second.get();
-               if (dialog && dialog->isVisibleView())
-                       dialog->checkStatus();
+               if (dialog) {
+                       if (dialog->isBufferDependent() && !documentBufferView())
+                               hideDialog(fromqstr(dialog->name()), 0);
+                       else if (dialog->isVisibleView())
+                               dialog->checkStatus();
+               }
        }
        updateToolbars();
        updateLayoutList();
 }
 
+Dialog * createDialog(GuiView & lv, string const & name);
 
 // will be replaced by a proper factory...
 Dialog * createGuiAbout(GuiView & lv);
-Dialog * createGuiBibitem(GuiView & lv);
 Dialog * createGuiBibtex(GuiView & lv);
-Dialog * createGuiBox(GuiView & lv);
-Dialog * createGuiBranch(GuiView & lv);
 Dialog * createGuiChanges(GuiView & lv);
 Dialog * createGuiCharacter(GuiView & lv);
 Dialog * createGuiCitation(GuiView & lv);
+Dialog * createGuiCompare(GuiView & lv);
 Dialog * createGuiDelimiter(GuiView & lv);
 Dialog * createGuiDocument(GuiView & lv);
 Dialog * createGuiErrorList(GuiView & lv);
-Dialog * createGuiERT(GuiView & lv);
 Dialog * createGuiExternal(GuiView & lv);
-Dialog * createGuiFloat(GuiView & lv);
 Dialog * createGuiGraphics(GuiView & lv);
 Dialog * createGuiInclude(GuiView & lv);
 Dialog * createGuiIndex(GuiView & lv);
-Dialog * createGuiInfo(GuiView & lv);
 Dialog * createGuiLabel(GuiView & lv);
 Dialog * createGuiListings(GuiView & lv);
 Dialog * createGuiLog(GuiView & lv);
-Dialog * createGuiMathHSpace(GuiView & lv);
 Dialog * createGuiMathMatrix(GuiView & lv);
 Dialog * createGuiNomenclature(GuiView & lv);
 Dialog * createGuiNote(GuiView & lv);
@@ -2969,43 +3488,40 @@ Dialog * createGuiShowFile(GuiView & lv);
 Dialog * createGuiSpellchecker(GuiView & lv);
 Dialog * createGuiSymbols(GuiView & lv);
 Dialog * createGuiTabularCreate(GuiView & lv);
-Dialog * createGuiTabular(GuiView & lv);
 Dialog * createGuiTexInfo(GuiView & lv);
-Dialog * createGuiTextHSpace(GuiView & lv);
 Dialog * createGuiToc(GuiView & lv);
 Dialog * createGuiThesaurus(GuiView & lv);
 Dialog * createGuiHyperlink(GuiView & lv);
-Dialog * createGuiVSpace(GuiView & lv);
 Dialog * createGuiViewSource(GuiView & lv);
 Dialog * createGuiWrap(GuiView & lv);
+Dialog * createGuiProgressView(GuiView & lv);
+
 
 
 Dialog * GuiView::build(string const & name)
 {
        LASSERT(isValidName(name), return 0);
 
+       Dialog * dialog = createDialog(*this, name);
+       if (dialog)
+               return dialog;
+
        if (name == "aboutlyx")
                return createGuiAbout(*this);
-       if (name == "bibitem")
-               return createGuiBibitem(*this);
        if (name == "bibtex")
                return createGuiBibtex(*this);
-       if (name == "box")
-               return createGuiBox(*this);
-       if (name == "branch")
-               return createGuiBranch(*this);
        if (name == "changes")
                return createGuiChanges(*this);
        if (name == "character")
                return createGuiCharacter(*this);
        if (name == "citation")
                return createGuiCitation(*this);
+       if (name == "compare")
+               return createGuiCompare(*this);
        if (name == "document")
                return createGuiDocument(*this);
        if (name == "errorlist")
                return createGuiErrorList(*this);
-       if (name == "ert")
-               return createGuiERT(*this);
        if (name == "external")
                return createGuiExternal(*this);
        if (name == "file")
@@ -3014,8 +3530,6 @@ Dialog * GuiView::build(string const & name)
                return createGuiSearch(*this);
        if (name == "findreplaceadv")
                return createGuiSearchAdv(*this);
-       if (name == "float")
-               return createGuiFloat(*this);
        if (name == "graphics")
                return createGuiGraphics(*this);
        if (name == "href")
@@ -3026,8 +3540,6 @@ Dialog * GuiView::build(string const & name)
                return createGuiIndex(*this);
        if (name == "index_print")
                return createGuiPrintindex(*this);
-       if (name == "info")
-               return createGuiInfo(*this);
        if (name == "label")
                return createGuiLabel(*this);
        if (name == "listings")
@@ -3036,8 +3548,6 @@ Dialog * GuiView::build(string const & name)
                return createGuiLog(*this);
        if (name == "mathdelimiter")
                return createGuiDelimiter(*this);
-       if (name == "mathspace")
-               return createGuiMathHSpace(*this);
        if (name == "mathmatrix")
                return createGuiMathMatrix(*this);
        if (name == "nomenclature")
@@ -3058,14 +3568,10 @@ Dialog * GuiView::build(string const & name)
                return createGuiRef(*this);
        if (name == "sendto")
                return createGuiSendTo(*this);
-       if (name == "space")
-               return createGuiTextHSpace(*this);
        if (name == "spellchecker")
                return createGuiSpellchecker(*this);
        if (name == "symbols")
                return createGuiSymbols(*this);
-       if (name == "tabular")
-               return createGuiTabular(*this);
        if (name == "tabularcreate")
                return createGuiTabularCreate(*this);
        if (name == "texinfo")
@@ -3076,10 +3582,10 @@ Dialog * GuiView::build(string const & name)
                return createGuiToc(*this);
        if (name == "view-source")
                return createGuiViewSource(*this);
-       if (name == "vspace")
-               return createGuiVSpace(*this);
        if (name == "wrap")
                return createGuiWrap(*this);
+       if (name == "progress")
+               return createGuiProgressView(*this);
 
        return 0;
 }