]> git.lyx.org Git - lyx.git/blobdiff - src/frontends/qt4/GuiApplication.cpp
Extend list of accessible menu info
[lyx.git] / src / frontends / qt4 / GuiApplication.cpp
index fc96995968005ed42e4c13bdf771a82278cbf861..d4b1701758ae248bae8345d66164d0d7d1250441 100644 (file)
@@ -14,6 +14,7 @@
 
 #include "GuiApplication.h"
 
+#include "ToolTipFormatter.h"
 #include "ColorCache.h"
 #include "ColorSet.h"
 #include "GuiClipboard.h"
@@ -35,6 +36,7 @@
 #include "BufferView.h"
 #include "CmdDef.h"
 #include "Color.h"
+#include "Converter.h"
 #include "CutAndPaste.h"
 #include "ErrorList.h"
 #include "Font.h"
@@ -63,7 +65,6 @@
 #include "support/ExceptionMessage.h"
 #include "support/FileName.h"
 #include "support/filetools.h"
-#include "support/foreach.h"
 #include "support/ForkedCalls.h"
 #include "support/gettext.h"
 #include "support/lassert.h"
 #include <queue>
 
 #include <QByteArray>
-#include <QClipboard>
 #include <QDateTime>
 #include <QDesktopWidget>
 #include <QDir>
 #include <QEvent>
-#include <QEventLoop>
 #include <QFileOpenEvent>
 #include <QFileInfo>
+#include <QFontDatabase>
 #include <QHash>
 #include <QIcon>
 #include <QImageReader>
 #include <QRegExp>
 #include <QSessionManager>
 #include <QSettings>
-#include <QShowEvent>
 #include <QSocketNotifier>
 #include <QSortFilterProxyModel>
 #include <QStandardItemModel>
-#include <QTextCodec>
 #include <QTimer>
 #include <QTranslator>
 #include <QThreadPool>
 #ifdef Q_WS_X11
 #include <X11/Xatom.h>
 #include <X11/Xlib.h>
+#include <QX11Info>
 #undef CursorShape
 #undef None
 #elif defined(QPA_XCB)
 #include <xcb/xcb.h>
+#ifdef HAVE_QT5_X11_EXTRAS
+#include <QtX11Extras/QX11Info>
+#endif
 #endif
 
 #if (QT_VERSION < 0x050000) || (QT_VERSION >= 0x050400)
 #include <QMacPasteboardMime>
 #endif // Q_OS_MAC
 
-#include "support/bind.h"
 #include <boost/crc.hpp>
 
 #include <exception>
@@ -183,6 +184,12 @@ frontend::Application * createApplication(int & argc, char * argv[])
                }
        }
 #endif
+
+#if defined(Q_OS_WIN) || defined(Q_CYGWIN_WIN)
+       // On Windows, allow bringing the LyX window to top
+       AllowSetForegroundWindow(ASFW_ANY);
+#endif
+
        frontend::GuiApplication * guiApp = new frontend::GuiApplication(argc, argv);
        // I'd rather do that in the constructor, but I do not think that
        // the palette is accessible there.
@@ -226,14 +233,35 @@ vector<string> loadableImageFormats()
        if (qt_formats.empty())
                LYXERR(Debug::GRAPHICS, "\nQt4 Problem: No Format available!");
 
+       bool jpeg_found = false;
+       bool jpg_found = false;
        for (QList<QByteArray>::const_iterator it = qt_formats.begin(); it != qt_formats.end(); ++it) {
 
                LYXERR(Debug::GRAPHICS, (const char *) *it << ", ");
 
                string ext = ascii_lowercase((const char *) *it);
                // special case
-               if (ext == "jpeg")
+               if (ext == "jpeg") {
+                       jpeg_found = true;
+                       if (jpg_found)
+                               continue;
                        ext = "jpg";
+               }
+               else if (ext == "jpg") {
+                       jpg_found = true;
+                       if (jpeg_found)
+                               continue;
+               }
+               else if (lyxrc.use_converter_cache &&
+                        (ext == "svg" || ext == "svgz") &&
+                         theConverters().isReachable("svg", "png"))
+                       // Qt only supports SVG 1.2 tiny. See #9778. We prefer displaying
+                       // the SVG as in the output. However we require that the converter
+                       // cache is enabled since this is expensive. We also require that
+                       // an explicit svg->png converter is defined, since the default
+                       // converter could produce bad quality as well. This assumes that
+                       // png can always be loaded.
+                       continue;
                fmts.push_back(ext);
        }
 
@@ -249,13 +277,13 @@ vector<string> loadableImageFormats()
 
 namespace {
 
-struct PngMap {
+struct ImgMap {
        QString key;
        QString value;
 };
 
 
-bool operator<(PngMap const & lhs, PngMap const & rhs)
+bool operator<(ImgMap const & lhs, ImgMap const & rhs)
 {
        return lhs.key < rhs.key;
 }
@@ -264,7 +292,7 @@ bool operator<(PngMap const & lhs, PngMap const & rhs)
 class CompareKey {
 public:
        CompareKey(QString const & name) : name_(name) {}
-       bool operator()(PngMap const & other) const { return other.key == name_; }
+       bool operator()(ImgMap const & other) const { return other.key == name_; }
 private:
        QString const name_;
 };
@@ -274,7 +302,7 @@ private:
 // Upper case comes before lower case
 // Please don't change the formatting, this list is parsed by
 // development/tools/generate_symbols_images.py.
-PngMap sorted_png_map[] = {
+ImgMap sorted_img_map[] = {
        { "Arrownot", "arrownot2"},
        { "Arrowvert", "arrowvert2"},
        { "Bowtie", "bowtie2" },
@@ -370,17 +398,16 @@ PngMap sorted_png_map[] = {
 };
 
 
-size_t const nr_sorted_png_map = sizeof(sorted_png_map) / sizeof(PngMap);
+size_t const nr_sorted_img_map = sizeof(sorted_img_map) / sizeof(ImgMap);
 
 // This list specifies which system's theme icon is related to which lyx
 // command. It was based on:
 // http://standards.freedesktop.org/icon-naming-spec/icon-naming-spec-latest.html
 // this must be sorted alphabetically
 // Upper case comes before lower case
-PngMap sorted_theme_icon_map[] = {
+ImgMap sorted_theme_icon_map[] = {
        { "bookmark-goto 0", "go-jump" },
        { "buffer-new", "document-new" },
-       { "buffer-print", "document-print" },
        { "buffer-write", "document-save" },
        { "buffer-write-as", "document-save-as" },
        { "buffer-zoom-in", "zoom-in" },
@@ -389,7 +416,6 @@ PngMap sorted_theme_icon_map[] = {
        { "cut", "edit-cut" },
        { "depth-decrement", "format-indent-less" },
        { "depth-increment", "format-indent-more" },
-       { "dialog-show print", "document-print" },
        { "dialog-show spellchecker", "tools-check-spelling" },
        { "dialog-show-new-inset graphics", "insert-image" },
        { "dialog-toggle findreplaceadv", "edit-find-replace" },
@@ -406,54 +432,54 @@ PngMap sorted_theme_icon_map[] = {
        { "window-new", "window-new" }
 };
 
-size_t const nr_sorted_theme_icon_map = sizeof(sorted_theme_icon_map) / sizeof(PngMap);
+size_t const nr_sorted_theme_icon_map = sizeof(sorted_theme_icon_map) / sizeof(ImgMap);
 
 
-QString findPng(QString const & name)
+QString findImg(QString const & name)
 {
-       PngMap const * const begin = sorted_png_map;
-       PngMap const * const end = begin + nr_sorted_png_map;
+       ImgMap const * const begin = sorted_img_map;
+       ImgMap const * const end = begin + nr_sorted_img_map;
        LATTEST(sorted(begin, end));
 
-       PngMap const * const it = find_if(begin, end, CompareKey(name));
+       ImgMap const * const it = find_if(begin, end, CompareKey(name));
 
-       QString png_name;
+       QString img_name;
        if (it != end) {
-               png_name = it->value;
+               img_name = it->value;
        } else {
-               png_name = name;
-               png_name.replace('_', "underscore");
-               png_name.replace(' ', '_');
+               img_name = name;
+               img_name.replace('_', "underscore");
+               img_name.replace(' ', '_');
 
                // This way we can have "math-delim { }" on the toolbar.
-               png_name.replace('(', "lparen");
-               png_name.replace(')', "rparen");
-               png_name.replace('[', "lbracket");
-               png_name.replace(']', "rbracket");
-               png_name.replace('{', "lbrace");
-               png_name.replace('}', "rbrace");
-               png_name.replace('|', "bars");
-               png_name.replace(',', "thinspace");
-               png_name.replace(':', "mediumspace");
-               png_name.replace(';', "thickspace");
-               png_name.replace('!', "negthinspace");
+               img_name.replace('(', "lparen");
+               img_name.replace(')', "rparen");
+               img_name.replace('[', "lbracket");
+               img_name.replace(']', "rbracket");
+               img_name.replace('{', "lbrace");
+               img_name.replace('}', "rbrace");
+               img_name.replace('|', "bars");
+               img_name.replace(',', "thinspace");
+               img_name.replace(':', "mediumspace");
+               img_name.replace(';', "thickspace");
+               img_name.replace('!', "negthinspace");
        }
 
-       LYXERR(Debug::GUI, "findPng(" << name << ")\n"
-               << "Looking for math PNG called \"" << png_name << '"');
-       return png_name;
+       LYXERR(Debug::GUI, "findImg(" << name << ")\n"
+               << "Looking for math icon called \"" << img_name << '"');
+       return img_name;
 }
 
-} // namespace anon
+} // namespace
 
 
 QString themeIconName(QString const & action)
 {
-       PngMap const * const begin = sorted_theme_icon_map;
-       PngMap const * const end = begin + nr_sorted_theme_icon_map;
+       ImgMap const * const begin = sorted_theme_icon_map;
+       ImgMap const * const end = begin + nr_sorted_theme_icon_map;
        LASSERT(sorted(begin, end), /**/);
 
-       PngMap const * const it = find_if(begin, end, CompareKey(action));
+       ImgMap const * const it = find_if(begin, end, CompareKey(action));
 
        if (it != end)
                return it->value;
@@ -471,13 +497,13 @@ QString iconName(FuncRequest const & f, bool unknown)
        case LFUN_MATH_INSERT:
                if (!f.argument().empty()) {
                        path = "math/";
-                       name1 = findPng(toqstr(f.argument()).mid(1));
+                       name1 = findImg(toqstr(f.argument()).mid(1));
                }
                break;
        case LFUN_MATH_DELIM:
        case LFUN_MATH_BIGDELIM:
                path = "math/";
-               name1 = findPng(toqstr(f.argument()));
+               name1 = findImg(toqstr(f.argument()));
                break;
        case LFUN_CALL:
                path = "commands/";
@@ -488,24 +514,9 @@ QString iconName(FuncRequest const & f, bool unknown)
                docstring firstcom;
                docstring dummy = split(f.argument(), firstcom, ';');
                name1 = toqstr(firstcom);
-               // FIXME: we should rename the icons to tabular-xxx instead of
-               // "tabular-feature-xxx"
-               name1.replace("inset-modify tabular", "tabular-feature");
                name1.replace(' ', '_');
                break;
        }
-       case LFUN_INSET_MODIFY: {
-               // FIXME: we should rename the icons to tabular-xxx instead of
-               // "tabular-feature-xxx" and generalize this naming to all
-               // insets, not to tabular using ones.
-               string inset_name;
-               string const command = split(to_utf8(f.argument()), inset_name, ' ');
-               if (insetCode(inset_name) == TABULAR_CODE) {
-                       name1 = "tabular-feature "+ toqstr(command);
-                       name1.replace(' ', '_');
-                       break;
-               }
-       }
        default:
                name2 = toqstr(lyxaction.getActionName(f.action()));
                name1 = name2;
@@ -560,7 +571,7 @@ QString iconName(FuncRequest const & f, bool unknown)
                FileName fname = imageLibFileSearch(imagedir, "unknown", "svgz,png", mode);
                if (fname.exists())
                        return toqstr(fname.absFileName());
-               return QString(":/images/unknown.png");
+               return QString(":/images/unknown.svgz");
        }
 
        return QString();
@@ -569,18 +580,7 @@ QString iconName(FuncRequest const & f, bool unknown)
 
 bool getPixmap(QPixmap & pixmap, QString const & path)
 {
-       if (pixmap.load(path)) {
-#if QT_VERSION >= 0x050000
-               if (path.endsWith(".svgz") || path.endsWith(".svg") ) {
-                       GuiApplication const * guiApp = theGuiApp();
-                       if (guiApp != 0) {
-                               pixmap.setDevicePixelRatio(guiApp->pixelRatio());
-                       }
-               }
-#endif
-               return true;
-       }
-       return false;
+       return pixmap.load(path);
 }
 
 
@@ -594,7 +594,7 @@ QPixmap getPixmap(QString const & path, QString const & name, QString const & ex
        if (getPixmap(pixmap, fpath)) {
                return pixmap;
        }
-       
+
        QStringList exts = ext.split(",");
        fpath = ":/" + path + name + ".";
        for (int i = 0; i < exts.size(); ++i) {
@@ -616,12 +616,16 @@ QIcon getIcon(FuncRequest const & f, bool unknown)
 {
 #if (QT_VERSION >= 0x040600)
        if (lyxrc.use_system_theme_icons) {
+               // use the icons from system theme that are available
                QString action = toqstr(lyxaction.getActionName(f.action()));
                if (!f.argument().empty())
                        action += " " + toqstr(f.argument());
                QString const theme_icon = themeIconName(action);
-               if (QIcon::hasThemeIcon(theme_icon))
-                       return QIcon::fromTheme(theme_icon);
+               if (QIcon::hasThemeIcon(theme_icon)) {
+                       QIcon const thmicn = QIcon::fromTheme(theme_icon);
+                       if (!thmicn.isNull())
+                               return thmicn;
+               }
        }
 #endif
 
@@ -660,38 +664,48 @@ public:
 };
 
 
-////////////////////////////////////////////////////////////////////////
-//
-// Mac specific stuff goes here...
-//
-////////////////////////////////////////////////////////////////////////
-
-class MenuTranslator : public QTranslator
+class GuiTranslator : public QTranslator
 {
 public:
-       MenuTranslator(QObject * parent)
+       GuiTranslator(QObject * parent = nullptr)
                : QTranslator(parent)
        {}
 
-#if QT_VERSION >= 0x050000
        virtual QString translate(const char * /* context */,
                const char *sourceText,
+#if QT_VERSION >= 0x050000
                const char * /* disambiguation */ = 0, int /* n */ = -1) const
 #else
-       QString translate(const char * /*context*/,
-         const char * sourceText,
-         const char * /*comment*/ = 0) const
+               const char * /*comment*/ = 0) const
 #endif
        {
-               string const s = sourceText;
-               if (s == N_("About %1") || s == N_("Preferences")
-                               || s == N_("Reconfigure") || s == N_("Quit %1"))
-                       return qt_(s);
-               else
-                       return QString();
+               // Here we declare the strings that need to be translated from Qt own GUI
+               // This is needed to include these strings to po files
+               _("About %1");
+               _("Preferences");
+               _("Reconfigure");
+               _("Quit %1");
+               _("&OK");
+               // Already in po: "Cancel", "&Cancel"
+               _("Apply"); // Already in po: "&Apply"
+               _("Reset"); // Already in po: "&Reset" "R&eset" "Rese&t"
+
+               docstring s = getGuiMessages().getIfFound(sourceText);
+               // This test should eventually be removed when translations are updated
+               if (s.empty())
+                       LYXERR(Debug::LOCALE, "Missing translation for `"
+                              << string(sourceText) << "'");
+               return toqstr(s);
        }
 };
 
+
+////////////////////////////////////////////////////////////////////////
+//
+// Mac specific stuff goes here...
+//
+////////////////////////////////////////////////////////////////////////
+
 #ifdef Q_OS_MAC
 // QMacPasteboardMimeGraphics can only be compiled on Mac.
 
@@ -777,8 +791,8 @@ class QWindowsMimeMetafile : public QWINDOWSMIME {
 public:
        QWindowsMimeMetafile() {}
 
-       bool canConvertFromMime(FORMATETC const & formatetc,
-               QMimeData const * mimedata) const
+       bool canConvertFromMime(FORMATETC const & /*formatetc*/,
+               QMimeData const * /*mimedata*/) const
        {
                return false;
        }
@@ -792,14 +806,14 @@ public:
                return pDataObj->QueryGetData(&formatetc) == S_OK;
        }
 
-       bool convertFromMime(FORMATETC const & formatetc,
-               const QMimeData * mimedata, STGMEDIUM * pmedium) const
+       bool convertFromMime(FORMATETC const & /*formatetc*/,
+               const QMimeData * /*mimedata*/, STGMEDIUM * /*pmedium*/) const
        {
                return false;
        }
 
        QVariant convertToMime(QString const & mimetype, IDataObject * pDataObj,
-               QVariant::Type preferredType) const
+               QVariant::Type /*preferredType*/) const
        {
                QByteArray data;
                if (!canConvertToMime(mimetype, pDataObj))
@@ -830,7 +844,7 @@ public:
 
 
        QVector<FORMATETC> formatsForMime(QString const & mimetype,
-               QMimeData const * mimedata) const
+               QMimeData const * /*mimedata*/) const
        {
                QVector<FORMATETC> formats;
                if (mimetype == emfMimeType() || mimetype == wmfMimeType())
@@ -940,8 +954,10 @@ struct GuiApplication::Private
        FontLoader font_loader_;
        ///
        ColorCache color_cache_;
-       ///
+       /// the built-in Qt translation mechanism
        QTranslator qt_trans_;
+       /// LyX gettext-based translation for Qt elements
+       GuiTranslator gui_trans_;
        ///
        QHash<int, SocketNotifier *> socket_notifiers_;
        ///
@@ -965,6 +981,9 @@ struct GuiApplication::Private
        ///
        KeyModifier meta_fake_bit;
 
+       /// The result of last dispatch action
+       DispatchResult dispatch_result_;
+
        /// Multiple views container.
        /**
        * Warning: This must not be a smart pointer as the destruction of the
@@ -1012,10 +1031,15 @@ GuiApplication::GuiApplication(int & argc, char ** argv)
        QCoreApplication::setOrganizationName(app_name);
        QCoreApplication::setOrganizationDomain("lyx.org");
        QCoreApplication::setApplicationName(lyx_package);
+#if QT_VERSION >= 0x050000
+       QCoreApplication::setAttribute(Qt::AA_UseHighDpiPixmaps);
+#endif
 
        qsrand(QDateTime::currentDateTime().toTime_t());
 
-       // Install translator for GUI elements.
+       // Install LyX translator for missing Qt translations
+       installTranslator(&d->gui_trans_);
+       // Install Qt native translator for GUI elements.
        installTranslator(&d->qt_trans_);
 
 #ifdef QPA_XCB
@@ -1034,10 +1058,6 @@ GuiApplication::GuiApplication(int & argc, char ** argv)
        // FIXME: Do we need a lyxrc setting for this on Mac? This behaviour
        // seems to be the default case for applications like LyX.
        setQuitOnLastWindowClosed(false);
-       // This allows to translate the strings that appear in the LyX menu.
-       /// A translator suitable for the entries in the LyX menu.
-       /// Only needed with Qt/Mac.
-       installTranslator(new MenuTranslator(this));
        ///
        setupApplescript();
 #endif
@@ -1081,6 +1101,9 @@ GuiApplication::GuiApplication(int & argc, char ** argv)
        // This is clearly not enough in a time where we use threads for
        // document preview and/or export. 20 should be OK.
        QThreadPool::globalInstance()->setMaxThreadCount(20);
+
+       // make sure tooltips are formatted
+       installEventFilter(new ToolTipFormatter(this));
 }
 
 
@@ -1093,7 +1116,7 @@ GuiApplication * theGuiApp()
 double GuiApplication::pixelRatio() const
 {
 #if QT_VERSION >= 0x050000
-       return devicePixelRatio();
+       return qt_scale_factor * devicePixelRatio();
 #else
        return 1.0;
 #endif
@@ -1115,7 +1138,7 @@ docstring Application::iconName(FuncRequest const & f, bool unknown)
 
 docstring Application::mathIcon(docstring const & c)
 {
-       return qstring_to_ucs4(findPng(toqstr(c)));
+       return qstring_to_ucs4(findImg(toqstr(c)));
 }
 
 
@@ -1332,12 +1355,12 @@ bool GuiApplication::getStatus(FuncRequest const & cmd, FuncStatus & flag) const
 static docstring makeDispatchMessage(docstring const & msg,
                                     FuncRequest const & cmd)
 {
-       const bool verbose = (cmd.origin() == FuncRequest::MENU
+       const bool be_verbose = (cmd.origin() == FuncRequest::MENU
                              || cmd.origin() == FuncRequest::TOOLBAR
                              || cmd.origin() == FuncRequest::COMMANDBUFFER);
 
-       if (cmd.action() == LFUN_SELF_INSERT || !verbose) {
-               LYXERR(Debug::ACTION, "dispatch msg is " << msg);
+       if (cmd.action() == LFUN_SELF_INSERT || !be_verbose) {
+               LYXERR(Debug::ACTION, "dispatch msg is `" << msg << "'");
                return msg;
        }
 
@@ -1372,26 +1395,35 @@ static docstring makeDispatchMessage(docstring const & msg,
 }
 
 
-void GuiApplication::dispatch(FuncRequest const & cmd)
+DispatchResult const & GuiApplication::dispatch(FuncRequest const & cmd)
 {
+       DispatchResult dr;
+
        Buffer * buffer = 0;
+       if (cmd.view_origin() && current_view_ != cmd.view_origin()) {
+               //setCurrentView(cmd.view_origin); //does not work
+               dr.setError(true);
+               dr.setMessage(_("Wrong focus!"));
+               d->dispatch_result_ = dr;
+               return d->dispatch_result_;
+       }
        if (current_view_ && current_view_->currentBufferView()) {
                current_view_->currentBufferView()->cursor().saveBeforeDispatchPosXY();
                buffer = &current_view_->currentBufferView()->buffer();
-               if (buffer)
-                       buffer->undo().beginUndoGroup();
        }
 
-       DispatchResult dr;
+       dr.screenUpdate(Update::FitCursor);
+       {
+               // This handles undo groups automagically
+               UndoGroupHelper ugh(buffer);
+               dispatch(cmd, dr);
+       }
+
        // redraw the screen at the end (first of the two drawing steps).
        // This is done unless explicitly requested otherwise
-       dr.screenUpdate(Update::FitCursor);
-       dispatch(cmd, dr);
        updateCurrentView(cmd, dr);
-
-       // the buffer may have been closed by one action
-       if (theBufferList().isLoaded(buffer))
-               buffer->undo().endUndoGroup();
+       d->dispatch_result_ = dr;
+       return d->dispatch_result_;
 }
 
 
@@ -1405,6 +1437,9 @@ void GuiApplication::updateCurrentView(FuncRequest const & cmd, DispatchResult &
                if (dr.needBufferUpdate()) {
                        bv->cursor().clearBufferUpdate();
                        bv->buffer().updateBuffer();
+               } else if (dr.needChangesUpdate()) {
+                       // updateBuffer() already updates the change-tracking presence flag
+                       bv->buffer().updateChangesPresent();
                }
                // BufferView::update() updates the ViewMetricsInfo and
                // also initializes the position cache for all insets in
@@ -1416,7 +1451,7 @@ void GuiApplication::updateCurrentView(FuncRequest const & cmd, DispatchResult &
                theSelection().haveSelection(bv->cursor().selection());
 
                // update gui
-               current_view_->restartCursor();
+               current_view_->restartCaret();
        }
        if (dr.needMessageUpdate()) {
                // Some messages may already be translated, so we cannot use _()
@@ -1456,7 +1491,7 @@ void GuiApplication::gotoBookmark(unsigned int idx, bool openFile,
        // if the current buffer is not that one, switch to it.
        BufferView * doc_bv = current_view_ ?
                current_view_->documentBufferView() : 0;
-       Cursor const old = doc_bv->cursor();
+       Cursor const * old = doc_bv ? &doc_bv->cursor() : 0;
        if (!doc_bv || doc_bv->buffer().fileName() != tmp.filename) {
                if (switchToBuffer) {
                        dispatch(FuncRequest(LFUN_BUFFER_SWITCH, file));
@@ -1468,13 +1503,13 @@ void GuiApplication::gotoBookmark(unsigned int idx, bool openFile,
        }
 
        // moveToPosition try paragraph id first and then paragraph (pit, pos).
-       if (!doc_bv->moveToPosition(
+       if (!doc_bv || !doc_bv->moveToPosition(
                        tmp.bottom_pit, tmp.bottom_pos, tmp.top_id, tmp.top_pos))
                return;
 
        Cursor & cur = doc_bv->cursor();
-       if (cur != old)
-               notifyCursorLeavesOrEnters(old, cur);
+       if (old && cur != *old)
+               notifyCursorLeavesOrEnters(*old, cur);
 
        // bm changed
        if (idx == 0)
@@ -1610,20 +1645,13 @@ void GuiApplication::dispatch(FuncRequest const & cmd, DispatchResult & dr)
        case LFUN_SCREEN_FONT_UPDATE: {
                // handle the screen font changes.
                d->font_loader_.update();
-               // Backup current_view_
-               GuiView * view = current_view_;
-               // Set current_view_ to zero to forbid GuiWorkArea::redraw()
-               // to skip the refresh.
-               current_view_ = 0;
-               theBufferList().changed(false);
-               // Restore current_view_
-               current_view_ = view;
+               dr.screenUpdate(Update::Force | Update::FitCursor);
                break;
        }
 
        case LFUN_BUFFER_NEW:
                validateCurrentView();
-               if (d->views_.empty()
+               if (!current_view_
                   || (!lyxrc.open_buffers_in_tabs && current_view_->documentBufferView() != 0)) {
                        createView(QString(), false); // keep hidden
                        current_view_->newDocument(to_utf8(cmd.argument()), false);
@@ -1636,7 +1664,7 @@ void GuiApplication::dispatch(FuncRequest const & cmd, DispatchResult & dr)
 
        case LFUN_BUFFER_NEW_TEMPLATE:
                validateCurrentView();
-               if (d->views_.empty()
+               if (!current_view_
                   || (!lyxrc.open_buffers_in_tabs && current_view_->documentBufferView() != 0)) {
                        createView();
                        current_view_->newDocument(to_utf8(cmd.argument()), true);
@@ -1648,12 +1676,14 @@ void GuiApplication::dispatch(FuncRequest const & cmd, DispatchResult & dr)
                break;
 
        case LFUN_FILE_OPEN: {
+               // FIXME: normally the code below is not needed, since getStatus makes sure that
+               //   current_view_ is not null.
                validateCurrentView();
                // FIXME: create a new method shared with LFUN_HELP_OPEN.
                string const fname = to_utf8(cmd.argument());
                bool const is_open = FileName::isAbsolute(fname)
                        && theBufferList().getBuffer(FileName(fname));
-               if (d->views_.empty()
+               if (!current_view_
                    || (!lyxrc.open_buffers_in_tabs
                        && current_view_->documentBufferView() != 0
                        && !is_open)) {
@@ -1663,10 +1693,21 @@ void GuiApplication::dispatch(FuncRequest const & cmd, DispatchResult & dr)
                        crc = for_each(fname.begin(), fname.end(), crc);
                        createView(crc.checksum());
                        current_view_->openDocument(fname);
-                       if (current_view_ && !current_view_->documentBufferView())
+                       if (!current_view_->documentBufferView())
                                current_view_->close();
-               } else
+                       else if (cmd.origin() == FuncRequest::LYXSERVER) {
+                               current_view_->raise();
+                               current_view_->activateWindow();
+                               current_view_->showNormal();
+                       }
+               } else {
                        current_view_->openDocument(fname);
+                       if (cmd.origin() == FuncRequest::LYXSERVER) {
+                               current_view_->raise();
+                               current_view_->activateWindow();
+                               current_view_->showNormal();
+                       }
+               }
                break;
        }
 
@@ -1691,13 +1732,8 @@ void GuiApplication::dispatch(FuncRequest const & cmd, DispatchResult & dr)
                current_view_->message(bformat(_("Opening help file %1$s..."),
                                               makeDisplayPath(fname.absFileName())));
                Buffer * buf = current_view_->loadDocument(fname, false);
-
-#ifndef DEVEL_VERSION
                if (buf)
-                       buf->setReadonly(true);
-#else
-               (void) buf;
-#endif
+                       buf->setReadonly(!current_view_->develMode());
                break;
        }
 
@@ -1731,6 +1767,8 @@ void GuiApplication::dispatch(FuncRequest const & cmd, DispatchResult & dr)
                }
                // Make sure we don't keep old colors in cache.
                d->color_cache_.clear();
+               // Update the current view
+               lyx::dispatch(FuncRequest(LFUN_SCREEN_FONT_UPDATE));
                break;
        }
 
@@ -1764,6 +1802,7 @@ void GuiApplication::dispatch(FuncRequest const & cmd, DispatchResult & dr)
                //   UI, then, nothing would happen. This seems fairly unlikely, but
                //   it definitely is a bug.
 
+               dr.forceBufferUpdate();
                break;
        }
 
@@ -1821,12 +1860,19 @@ void GuiApplication::dispatch(FuncRequest const & cmd, DispatchResult & dr)
                // repeat command
                string countstr;
                string rest = split(argument, countstr, ' ');
-               istringstream is(countstr);
-               int count = 0;
-               is >> count;
-               //lyxerr << "repeat: count: " << count << " cmd: " << rest << endl;
-               for (int i = 0; i < count; ++i)
-                       dispatch(lyxaction.lookupFunc(rest));
+               int const count = convert<int>(countstr);
+               // an arbitrary number to limit number of iterations
+               int const max_iter = 10000;
+               if (count > max_iter) {
+                       dr.setMessage(bformat(_("Cannot iterate more than %1$d times"), max_iter));
+                       dr.setError(true);
+               } else {
+                       for (int i = 0; i < count; ++i) {
+                               FuncRequest lfun = lyxaction.lookupFunc(rest);
+                               lfun.allowAsync(false);
+                               dispatch(lfun);
+                       }
+               }
                break;
        }
 
@@ -1836,34 +1882,34 @@ void GuiApplication::dispatch(FuncRequest const & cmd, DispatchResult & dr)
                // FIXME: this LFUN should also work without any view.
                Buffer * buffer = (current_view_ && current_view_->documentBufferView())
                                  ? &(current_view_->documentBufferView()->buffer()) : 0;
-               if (buffer)
-                       buffer->undo().beginUndoGroup();
+               // This handles undo groups automagically
+               UndoGroupHelper ugh(buffer);
                while (!arg.empty()) {
                        string first;
                        arg = split(arg, first, ';');
                        FuncRequest func(lyxaction.lookupFunc(first));
+                       func.allowAsync(false);
                        func.setOrigin(cmd.origin());
                        dispatch(func);
                }
-               // the buffer may have been closed by one action
-               if (theBufferList().isLoaded(buffer))
-                       buffer->undo().endUndoGroup();
                break;
        }
 
        case LFUN_BUFFER_FORALL: {
-               FuncRequest const funcToRun = lyxaction.lookupFunc(cmd.getLongArg(0));
+               FuncRequest funcToRun = lyxaction.lookupFunc(cmd.getLongArg(0));
+               funcToRun.allowAsync(false);
 
                map<Buffer *, GuiView *> views_lVisible;
                map<GuiView *, Buffer *> activeBuffers;
 
                QList<GuiView *> allViews = d->views_.values();
 
-               // this foreach does not modify any buffer. It just collects info on local visibility of buffers
-               // and on which buffer is active in each view.
+               // this for does not modify any buffer. It just collects info on local
+               // visibility of buffers and on which buffer is active in each view.
                Buffer * const last = theBufferList().last();
-               foreach (GuiView * view, allViews) {
-                       // all of the buffers might be locally hidden. That is, there is no active buffer.
+               for(GuiView * view : allViews) {
+                       // all of the buffers might be locally hidden. That is, there is no
+                       // active buffer.
                        if (!view || !view->currentBufferView())
                                activeBuffers[view] = 0;
                        else
@@ -1910,7 +1956,7 @@ void GuiApplication::dispatch(FuncRequest const & cmd, DispatchResult & dr)
                }
 
                // put things back to how they were (if possible).
-               foreach (GuiView * view, allViews) {
+               for (GuiView * view : allViews) {
                        Buffer * originalBuf = activeBuffers[view];
                        // there might not have been an active buffer in this view or it might have been closed by the LFUN.
                        if (theBufferList().isLoaded(originalBuf))
@@ -2033,7 +2079,7 @@ void GuiApplication::dispatch(FuncRequest const & cmd, DispatchResult & dr)
                                createView();
                }
        }
-
+       // fall through
        default:
                // The LFUN must be for one of GuiView, BufferView, Buffer or Cursor;
                // let's try that:
@@ -2082,21 +2128,45 @@ void GuiApplication::handleKeyFunc(FuncCode action)
 }
 
 
+//Keep this in sync with GuiApplication::processKeySym below
+bool GuiApplication::queryKeySym(KeySymbol const & keysym,
+                                 KeyModifier state) const
+{
+       // Do nothing if we have nothing
+       if (!keysym.isOK() || keysym.isModifier())
+               return false;
+       // Do a one-deep top-level lookup for cancel and meta-fake keys.
+       KeySequence seq;
+       FuncRequest func = seq.addkey(keysym, state);
+       // When not cancel or meta-fake, do the normal lookup.
+       if ((func.action() != LFUN_CANCEL) && (func.action() != LFUN_META_PREFIX)) {
+               seq = d->keyseq;
+               func = seq.addkey(keysym, (state | d->meta_fake_bit));
+       }
+       // Maybe user can only reach the key via holding down shift.
+       // Let's see. But only if shift is the only modifier
+       if (func.action() == LFUN_UNKNOWN_ACTION && state == ShiftModifier)
+               // If addkey looked up a command and did not find further commands then
+               // seq has been reset at this point
+               func = seq.addkey(keysym, NoModifier);
+
+       LYXERR(Debug::KEY, " Key (queried) [action=" << func.action() << "]["
+              << seq.print(KeySequence::Portable) << ']');
+       return func.action() != LFUN_UNKNOWN_ACTION;
+}
+
+
+//Keep this in sync with GuiApplication::queryKeySym above
 void GuiApplication::processKeySym(KeySymbol const & keysym, KeyModifier state)
 {
        LYXERR(Debug::KEY, "KeySym is " << keysym.getSymbolName());
 
        // Do nothing if we have nothing (JMarc)
-       if (!keysym.isOK()) {
-               LYXERR(Debug::KEY, "Empty kbd action (probably composing)");
+       if (!keysym.isOK() || keysym.isModifier()) {
+               if (!keysym.isOK())
+                       LYXERR(Debug::KEY, "Empty kbd action (probably composing)");
                if (current_view_)
-                       current_view_->restartCursor();
-               return;
-       }
-
-       if (keysym.isModifier()) {
-               if (current_view_)
-                       current_view_->restartCursor();
+                       current_view_->restartCaret();
                return;
        }
 
@@ -2140,6 +2210,8 @@ void GuiApplication::processKeySym(KeySymbol const & keysym, KeyModifier state)
        // Let's see. But only if shift is the only modifier
        if (func.action() == LFUN_UNKNOWN_ACTION && state == ShiftModifier) {
                LYXERR(Debug::KEY, "Trying without shift");
+               // If addkey looked up a command and did not find further commands then
+               // seq has been reset at this point
                func = d->keyseq.addkey(keysym, NoModifier);
                LYXERR(Debug::KEY, "Action now " << func.action());
        }
@@ -2153,9 +2225,15 @@ void GuiApplication::processKeySym(KeySymbol const & keysym, KeyModifier state)
                        // must not be inserted (#5704)
                        if (!isPrintable(encoded_last_key)) {
                                LYXERR(Debug::KEY, "Non-printable character! Omitting.");
-                               current_view_->restartCursor();
+                               if (current_view_)
+                                       current_view_->restartCaret();
                                return;
                        }
+                       // The following modifier check is not needed on Mac.
+                       // The keysym is either not text or it is different
+                       // from the non-modifier keysym. See #9875 for the
+                       // broken alt-modifier effect of having this code active.
+#if !defined(Q_OS_MAC)
                        // If a non-Shift Modifier is used we have a non-bound key sequence
                        // (such as Alt+j = j). This should be omitted (#5575).
                        // On Windows, AltModifier and ControlModifier are both
@@ -2166,11 +2244,15 @@ void GuiApplication::processKeySym(KeySymbol const & keysym, KeyModifier state)
 #if defined(Q_OS_WIN) || defined(Q_CYGWIN_WIN)
                            && !(state & AltModifier && state & ControlModifier)
 #endif
-                           ) {
-                               current_view_->message(_("Unknown function."));
-                               current_view_->restartCursor();
+                           )
+                       {
+                               if (current_view_) {
+                                       current_view_->message(_("Unknown function."));
+                                       current_view_->restartCaret();
+                               }
                                return;
                        }
+#endif
                        // Since all checks above were passed, we now really have text that
                        // is to be inserted (e.g., AltGr-bound symbols). Thus change the
                        // func to LFUN_SELF_INSERT and thus cause the text to be inserted
@@ -2181,7 +2263,7 @@ void GuiApplication::processKeySym(KeySymbol const & keysym, KeyModifier state)
                        LYXERR(Debug::KEY, "Unknown Action and not isText() -- giving up");
                        if (current_view_) {
                                current_view_->message(_("Unknown function."));
-                               current_view_->restartCursor();
+                               current_view_->restartCaret();
                        }
                        return;
                }
@@ -2215,8 +2297,12 @@ void GuiApplication::processFuncRequestAsync(FuncRequest const & func)
 void GuiApplication::processFuncRequestQueue()
 {
        while (!d->func_request_queue_.empty()) {
-               processFuncRequest(d->func_request_queue_.front());
+               // take the item from the stack _before_ processing the
+               // request in order to avoid race conditions from nested
+               // or parallel requests (see #10406)
+               FuncRequest const fr(d->func_request_queue_.front());
                d->func_request_queue_.pop();
+               processFuncRequest(fr);
        }
 }
 
@@ -2279,6 +2365,8 @@ void GuiApplication::createView(QString const & geometry_arg, bool autoShow,
 
        LYXERR(Debug::GUI, "About to create new window with ID " << id);
        GuiView * view = new GuiView(id);
+       // `view' is the new current_view_. Tell coverity that is is not 0.
+       LATTEST(current_view_);
        // register view
        d->views_[id] = view;
 
@@ -2328,6 +2416,15 @@ void GuiApplication::createView(QString const & geometry_arg, bool autoShow,
 }
 
 
+bool GuiApplication::unhide(Buffer * buf)
+{
+       if (!currentView())
+               return false;
+       currentView()->setBuffer(buf, false);
+       return true;
+}
+
+
 Clipboard & GuiApplication::clipboard()
 {
        return d->clipboard_;
@@ -2465,13 +2562,12 @@ QAbstractItemModel * GuiApplication::languageModel()
 
        QStandardItemModel * lang_model = new QStandardItemModel(this);
        lang_model->insertColumns(0, 3);
-       int current_row;
        QIcon speller(getPixmap("images/", "dialog-show_spellchecker", "svgz,png"));
        QIcon saurus(getPixmap("images/", "thesaurus-entry", "svgz,png"));
        Languages::const_iterator it = lyx::languages.begin();
        Languages::const_iterator end = lyx::languages.end();
        for (; it != end; ++it) {
-               current_row = lang_model->rowCount();
+               int current_row = lang_model->rowCount();
                lang_model->insertRows(current_row, 1);
                QModelIndex pl_item = lang_model->index(current_row, 0);
                QModelIndex sp_item = lang_model->index(current_row, 1);
@@ -2511,7 +2607,7 @@ void GuiApplication::restoreGuiSession()
        // not be added at all (help files).
        for (size_t i = 0; i < lastopened.size(); ++i) {
                FileName const & file_name = lastopened[i].file_name;
-               if (d->views_.empty() || (!lyxrc.open_buffers_in_tabs
+               if (!current_view_ || (!lyxrc.open_buffers_in_tabs
                          && current_view_->documentBufferView() != 0)) {
                        boost::crc_32_type crc;
                        string const & fname = file_name.absFileName();
@@ -2556,11 +2652,41 @@ QString const GuiApplication::sansFontName()
 
 QString const GuiApplication::typewriterFontName()
 {
-       QFont font;
-       font.setStyleHint(QFont::TypeWriter);
-       font.setFamily("monospace");
+       return QFontInfo(typewriterSystemFont()).family();
+}
 
-       return QFontInfo(font).family();
+
+namespace {
+       // We cannot use QFont::fixedPitch() because it doesn't
+       // return the fact but only if it is requested.
+       static bool isFixedPitch(const QFont & font) {
+               const QFontInfo fi(font);
+               return fi.fixedPitch();
+       }
+} // namespace
+
+
+QFont const GuiApplication::typewriterSystemFont()
+{
+#if QT_VERSION >= 0x050200
+       QFont font = QFontDatabase::systemFont(QFontDatabase::FixedFont);
+#else
+       QFont font("monospace");
+#endif
+       if (!isFixedPitch(font)) {
+               // try to enforce a real monospaced font
+               font.setStyleHint(QFont::Monospace);
+               if (!isFixedPitch(font)) {
+                       font.setStyleHint(QFont::TypeWriter);
+                       if (!isFixedPitch(font)) font.setFamily("courier");
+               }
+       }
+#ifdef Q_OS_MAC
+       // On a Mac the result is too small and it's not practical to
+       // rely on Qtconfig utility to change the system settings of Qt.
+       font.setPointSize(12);
+#endif
+       return font;
 }
 
 
@@ -2609,7 +2735,8 @@ bool GuiApplication::notify(QObject * receiver, QEvent * event)
 #endif
                        // In release mode, try to exit gracefully.
                        this->exit(1);
-
+                       // FIXME: GCC 7 thinks we can fall through here. Can we?
+                       // fall through
                case BufferException: {
                        if (!current_view_ || !current_view_->documentBufferView())
                                return false;
@@ -2753,7 +2880,7 @@ bool GuiApplication::closeAllViews()
        theSession().lastOpened().clear();
 
        QList<GuiView *> const views = d->views_.values();
-       foreach (GuiView * view, views) {
+       for (GuiView * view : views) {
                if (!view->closeScheduled())
                        return false;
        }
@@ -2769,7 +2896,7 @@ bool GuiApplication::prepareAllViewsForLogout()
                return true;
 
        QList<GuiView *> const views = d->views_.values();
-       foreach (GuiView * view, views) {
+       for (GuiView * view : views) {
                if (!view->prepareAllBuffersForLogout())
                        return false;
        }
@@ -2788,7 +2915,7 @@ GuiView & GuiApplication::view(int id) const
 void GuiApplication::hideDialogs(string const & name, Inset * inset) const
 {
        QList<GuiView *> const views = d->views_.values();
-       foreach (GuiView * view, views)
+       for (GuiView * view : views)
                view->hideDialog(name, inset);
 }
 
@@ -2808,7 +2935,16 @@ Buffer const * GuiApplication::updateInset(Inset const * inset) const
 bool GuiApplication::searchMenu(FuncRequest const & func,
        docstring_list & names) const
 {
-       return d->menus_.searchMenu(func, names);
+       BufferView * bv = 0;
+       if (current_view_)
+               bv = current_view_->currentBufferView();
+       return d->menus_.searchMenu(func, names, bv);
+}
+
+
+bool GuiApplication::hasBufferView() const
+{
+       return (current_view_ && current_view_->currentBufferView());
 }
 
 
@@ -2876,7 +3012,7 @@ GuiApplication::ReturnValues GuiApplication::readUIFile(FileName ui_path)
                        QString const file = toqstr(lex.getString());
                        bool const success = readUIFile(file, true);
                        if (!success) {
-                               LYXERR0("Failed to read inlcuded file: " << fromqstr(file));
+                               LYXERR0("Failed to read included file: " << fromqstr(file));
                                return ReadError;
                        }
                        break;
@@ -3058,8 +3194,26 @@ bool GuiApplication::x11EventFilter(XEvent * xev)
                BufferView * bv = current_view_->currentBufferView();
                if (bv) {
                        docstring const sel = bv->requestSelection();
-                       if (!sel.empty())
+                       if (!sel.empty()) {
                                d->selection_.put(sel);
+                               // Refresh the selection request timestamp.
+                               // We have to do this by ourselves as Qt seems
+                               // not doing that, maybe because of our
+                               // "persistent selection" implementation
+                               // (see comments in GuiSelection.cpp).
+                               XSelectionEvent nev;
+                               nev.type = SelectionNotify;
+                               nev.display = xev->xselectionrequest.display;
+                               nev.requestor = xev->xselectionrequest.requestor;
+                               nev.selection = xev->xselectionrequest.selection;
+                               nev.target = xev->xselectionrequest.target;
+                               nev.property = 0L; // None
+                               nev.time = CurrentTime;
+                               XSendEvent(QX11Info::display(),
+                                       nev.requestor, False, 0,
+                                       reinterpret_cast<XEvent *>(&nev));
+                               return true;
+                       }
                }
                break;
        }
@@ -3077,7 +3231,7 @@ bool GuiApplication::x11EventFilter(XEvent * xev)
 }
 #elif defined(QPA_XCB)
 bool GuiApplication::nativeEventFilter(const QByteArray & eventType,
-                                      void * message, long *) Q_DECL_OVERRIDE
+                                      void * message, long *)
 {
        if (!current_view_ || eventType != "xcb_generic_event_t")
                return false;
@@ -3094,8 +3248,29 @@ bool GuiApplication::nativeEventFilter(const QByteArray & eventType,
                BufferView * bv = current_view_->currentBufferView();
                if (bv) {
                        docstring const sel = bv->requestSelection();
-                       if (!sel.empty())
+                       if (!sel.empty()) {
                                d->selection_.put(sel);
+#ifdef HAVE_QT5_X11_EXTRAS
+                               // Refresh the selection request timestamp.
+                               // We have to do this by ourselves as Qt seems
+                               // not doing that, maybe because of our
+                               // "persistent selection" implementation
+                               // (see comments in GuiSelection.cpp).
+                               xcb_selection_notify_event_t nev;
+                               nev.response_type = XCB_SELECTION_NOTIFY;
+                               nev.requestor = srev->requestor;
+                               nev.selection = srev->selection;
+                               nev.target = srev->target;
+                               nev.property = XCB_NONE;
+                               nev.time = XCB_CURRENT_TIME;
+                               xcb_connection_t * con = QX11Info::connection();
+                               xcb_send_event(con, 0, srev->requestor,
+                                       XCB_EVENT_MASK_NO_EVENT,
+                                       reinterpret_cast<char const *>(&nev));
+                               xcb_flush(con);
+#endif
+                               return true;
+                       }
                }
                break;
        }