From 5a10add52eeb1a3134bd81d53d3fecdf5554b036 Mon Sep 17 00:00:00 2001 From: Stefan Schimanski Date: Sun, 16 Nov 2008 19:19:07 +0000 Subject: [PATCH] * "Copy as Reference" in the context menu of a label git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@27575 a592a061-630c-0410-9148-cb99ea01b6c8 --- lib/ui/stdcontext.inc | 2 ++ src/BufferView.cpp | 23 ++++++++++++++++++++++- src/CutAndPaste.cpp | 14 ++++++++++++++ src/CutAndPaste.h | 2 ++ src/FuncCode.h | 1 + src/LyXAction.cpp | 9 +++++++++ src/insets/InsetLabel.cpp | 27 +++++++++++++++++++++++++++ src/insets/InsetLabel.h | 2 ++ 8 files changed, 79 insertions(+), 1 deletion(-) diff --git a/lib/ui/stdcontext.inc b/lib/ui/stdcontext.inc index fe682bfb12..f9fb09c499 100644 --- a/lib/ui/stdcontext.inc +++ b/lib/ui/stdcontext.inc @@ -90,6 +90,8 @@ Menuset Item "Next Cross-Reference|N" "reference-next" Item "Go back to Reference|G" "bookmark-goto 0" Separator + Item "Copy as Reference|C" "copy-label-as-reference" + Separator Item "Settings...|S" "next-inset-toggle" End diff --git a/src/BufferView.cpp b/src/BufferView.cpp index b463778ef3..9af4bb71f0 100644 --- a/src/BufferView.cpp +++ b/src/BufferView.cpp @@ -897,6 +897,14 @@ FuncStatus BufferView::getStatus(FuncRequest const & cmd) flag.setEnabled(! this->cursor().inRegexped()); break; + case LFUN_COPY_LABEL_AS_REF: + // if there is an inset at cursor, see whether it + // handles the lfun, other start from scratch + Inset * inset = cur.nextInset(); + if (!inset || !inset->getStatus(cur, cmd, flag)) + flag = lyx::getStatus(cmd); + break; + case LFUN_NEXT_INSET_TOGGLE: case LFUN_NEXT_INSET_MODIFY: { // this is the real function we want to invoke @@ -1338,7 +1346,20 @@ bool BufferView::dispatch(FuncRequest const & cmd) // turn compression on/off buffer_.params().compressed = !buffer_.params().compressed; break; - + case LFUN_COPY_LABEL_AS_REF: { + // if there is an inset at cursor, try to copy it + Inset * inset = cur.nextInset(); + if (inset) { + FuncRequest tmpcmd = cmd; + inset->dispatch(cur, tmpcmd); + } + if (!cur.result().dispatched()) + // It did not work too; no action needed. + break; + cur.clearSelection(); + processUpdateFlags(Update::SinglePar | Update::FitCursor); + break; + } case LFUN_NEXT_INSET_TOGGLE: { // create the the real function we want to invoke FuncRequest tmpcmd = cmd; diff --git a/src/CutAndPaste.cpp b/src/CutAndPaste.cpp index f367dde1bd..22bc931e0a 100644 --- a/src/CutAndPaste.cpp +++ b/src/CutAndPaste.cpp @@ -687,6 +687,20 @@ void copySelection(Cursor const & cur) } +void copyInset(Cursor const & cur, Inset * inset, docstring const & plaintext) +{ + ParagraphList pars; + Paragraph par; + BufferParams const & bp = cur.buffer().params(); + par.setLayout(bp.documentClass().plainLayout()); + par.insertInset(0, inset, Change(Change::UNCHANGED)); + pars.push_back(par); + theCuts.push(make_pair(pars, bp.documentClassPtr())); + + // stuff the selection onto the X clipboard, from an explicit copy request + putClipboard(theCuts[0].first, theCuts[0].second, plaintext); +} + namespace { void copySelectionToStack(Cursor const & cur, CutStack & cutstack) diff --git a/src/CutAndPaste.h b/src/CutAndPaste.h index a46eeb12f4..a4e975b8a1 100644 --- a/src/CutAndPaste.h +++ b/src/CutAndPaste.h @@ -64,6 +64,8 @@ void replaceSelection(Cursor & cur); void cutSelection(Cursor & cur, bool doclear = true, bool realcut = true); /// Push the current selection to the cut buffer and the system clipboard. void copySelection(Cursor const & cur); +/// +void copyInset(Cursor const & cur, Inset * inset, docstring const & plaintext); /** * Push the current selection to the cut buffer and the system clipboard. * \param plaintext plain text version of the selection for the system diff --git a/src/FuncCode.h b/src/FuncCode.h index 456ea2d37b..b9c88c8b78 100644 --- a/src/FuncCode.h +++ b/src/FuncCode.h @@ -412,6 +412,7 @@ enum FuncCode LFUN_TAB_DELETE, LFUN_WORD_FINDADV, // Tommaso, 20081003 LFUN_REGEXP_MODE, // Tommaso, 20081003 + LFUN_COPY_LABEL_AS_REF, // sts, 20081116 LFUN_LASTACTION // end of the table }; diff --git a/src/LyXAction.cpp b/src/LyXAction.cpp index 4a309c1fed..4b7e690691 100644 --- a/src/LyXAction.cpp +++ b/src/LyXAction.cpp @@ -3089,6 +3089,15 @@ void LyXAction::init() */ { LFUN_BRANCH_DEACTIVATE, "branch-deactivate", Argument, Buffer }, +/*! + * \var lyx::FuncCode lyx::LFUN_COPY_LABEL_AS_REF + * \li Action: Copies the label at the cursor as a cross-reference to be paster elsewhere. + * \li Syntax: copy-label-as-reference + * \li Origin: sts, 16 Nov 2008 + * \endvar + */ + { LFUN_COPY_LABEL_AS_REF, "copy-label-as-reference", ReadOnly | NoUpdate, Edit }, + { LFUN_NOACTION, "", Noop, Hidden } #ifndef DOXYGEN_SHOULD_SKIP_THIS }; diff --git a/src/insets/InsetLabel.cpp b/src/insets/InsetLabel.cpp index 0b671b2c0f..88d2f4f6c5 100644 --- a/src/insets/InsetLabel.cpp +++ b/src/insets/InsetLabel.cpp @@ -17,8 +17,10 @@ #include "buffer_funcs.h" #include "Buffer.h" #include "BufferView.h" +#include "CutAndPaste.h" #include "DispatchResult.h" #include "FuncRequest.h" +#include "FuncStatus.h" #include "InsetIterator.h" #include "ParIterator.h" #include "sgml.h" @@ -133,6 +135,23 @@ void InsetLabel::addToToc(DocIterator const & cpit) } +bool InsetLabel::getStatus(Cursor & cur, FuncRequest const & cmd, + FuncStatus & status) const +{ + bool enabled; + switch (cmd.action) { + case LFUN_COPY_LABEL_AS_REF: + enabled = true; + break; + default: + return InsetCommand::getStatus(cur, cmd, status); + } + + status.setEnabled(enabled); + return true; +} + + void InsetLabel::doDispatch(Cursor & cur, FuncRequest & cmd) { switch (cmd.action) { @@ -150,6 +169,14 @@ void InsetLabel::doDispatch(Cursor & cur, FuncRequest & cmd) break; } + case LFUN_COPY_LABEL_AS_REF: { + InsetCommandParams p(REF_CODE, "ref"); + p["reference"] = getParam("name"); + cap::clearSelection(); + cap::copyInset(cur, new InsetRef(cur.buffer(), p), getParam("name")); + break; + } + default: InsetCommand::doDispatch(cur, cmd); break; diff --git a/src/insets/InsetLabel.h b/src/insets/InsetLabel.h index 75a5ed51d4..92f0560579 100644 --- a/src/insets/InsetLabel.h +++ b/src/insets/InsetLabel.h @@ -56,6 +56,8 @@ public: void addToToc(DocIterator const &); /// void updateCommand(docstring const & new_label, bool updaterefs = true); + /// + bool getStatus(Cursor & cur, FuncRequest const & cmd, FuncStatus & status) const; protected: /// void doDispatch(Cursor & cur, FuncRequest & cmd); -- 2.39.2