From f62aefc18e6bf9228abe24313c365b1820aabdb3 Mon Sep 17 00:00:00 2001 From: Juergen Spitzmueller Date: Thu, 2 Jan 2020 14:09:24 +0100 Subject: [PATCH] New helper lfun ifrelatives This executes a command only if a buffer has either a master or children Helps to disable master-related items in the UI --- lib/RELEASE-NOTES | 2 ++ src/FuncCode.h | 1 + src/LyXAction.cpp | 13 +++++++++++++ src/frontends/qt/GuiApplication.cpp | 26 ++++++++++++++++++++++++++ 4 files changed, 42 insertions(+) diff --git a/lib/RELEASE-NOTES b/lib/RELEASE-NOTES index 9da7d8cec1..30e294840d 100644 --- a/lib/RELEASE-NOTES +++ b/lib/RELEASE-NOTES @@ -69,6 +69,8 @@ * export-cancel: Used to cancel background export processes. +* ifrelatives is a helper function to check whether a buffer has a master or children. + * master-buffer-forall executes an lfun in the master and all children of a document. * paragraph-select is a new convenience function to select the paragraph diff --git a/src/FuncCode.h b/src/FuncCode.h index 10d65b3e59..762bc035ca 100644 --- a/src/FuncCode.h +++ b/src/FuncCode.h @@ -489,6 +489,7 @@ enum FuncCode // 380 LFUN_BUFFER_RESET_EXPORT, // spitz 20191226 LFUN_MASTER_BUFFER_FORALL, // spitz 20191231 + LFUN_IF_RELATIVES, // spitz 20200102 LFUN_LASTACTION // end of the table }; diff --git a/src/LyXAction.cpp b/src/LyXAction.cpp index aa57112776..a9789ad290 100644 --- a/src/LyXAction.cpp +++ b/src/LyXAction.cpp @@ -1923,6 +1923,19 @@ void LyXAction::init() */ { LFUN_ICON_SIZE, "icon-size", NoBuffer, Buffer }, + +/*! + * \var lyx::FuncCode lyx::LFUN_IF_RELATIVES + * \li Action: Helper function for master-related actions + * \li Notion: In a buffer, the function will be disabled if the buffer + * has no master or children. It thus allows to enable + * actions only in documents with master/children + * \li Syntax: ifrelatives + * \li Origin: spitz, 2 January 2020 + * \endvar + */ + { LFUN_IF_RELATIVES, "ifrelatives", Noop, Buffer }, + /*! * \var lyx::FuncCode lyx::LFUN_INDEX_INSERT * \li Action: Inserts Index entry. diff --git a/src/frontends/qt/GuiApplication.cpp b/src/frontends/qt/GuiApplication.cpp index e67a476218..9c15d7a770 100644 --- a/src/frontends/qt/GuiApplication.cpp +++ b/src/frontends/qt/GuiApplication.cpp @@ -1310,6 +1310,20 @@ bool GuiApplication::getStatus(FuncRequest const & cmd, FuncStatus & flag) const break; } + case LFUN_IF_RELATIVES: { + string const lfun = to_utf8(cmd.argument()); + BufferView const * bv = + current_view_ ? current_view_->currentBufferView() : nullptr; + if (!bv || (bv->buffer().parent() == nullptr && !bv->buffer().hasChildren())) { + enable = false; + break; + } + FuncRequest func(lyxaction.lookupFunc(lfun)); + func.setOrigin(cmd.origin()); + flag = getStatus(func); + break; + } + case LFUN_CURSOR_FOLLOWS_SCROLLBAR_TOGGLE: case LFUN_REPEAT: case LFUN_PREFERENCES_SAVE: @@ -2051,6 +2065,18 @@ void GuiApplication::dispatch(FuncRequest const & cmd, DispatchResult & dr) break; } + case LFUN_IF_RELATIVES: { + string const lfun = to_utf8(cmd.argument()); + FuncRequest func(lyxaction.lookupFunc(lfun)); + func.setOrigin(cmd.origin()); + FuncStatus const stat = getStatus(func); + if (stat.enabled()) { + dispatch(func); + break; + } + break; + } + case LFUN_PREFERENCES_SAVE: lyxrc.write(support::makeAbsPath("preferences", package().user_support().absFileName()), false); -- 2.39.5