Context menu for InsetArgument

This commit is contained in:
Juergen Spitzmueller 2012-11-23 14:44:45 +01:00
parent 7a05aeb4ec
commit 9914f21bce
4 changed files with 125 additions and 10 deletions

View File

@ -155,6 +155,13 @@ Menuset
OptItem "Settings...|S" "inset-settings"
End
#
# InsetArgument context menu
#
Menu "context-argument"
SwitchArguments
End
#
# InsetBox context menu
#

View File

@ -176,7 +176,10 @@ public:
LanguageSelector,
/** This is the list of arguments available
for insertion into the current layout. */
Arguments
Arguments,
/** This is the list of arguments available
for in the InsetArgument context menu. */
SwitchArguments
};
explicit MenuItem(Kind kind) : kind_(kind), optional_(false) {}
@ -352,7 +355,7 @@ public:
void expandGraphicsGroups(BufferView const *);
void expandSpellingSuggestions(BufferView const *);
void expandLanguageSelector(Buffer const * buf);
void expandArguments(BufferView const *);
void expandArguments(BufferView const *, bool switcharg = false);
///
ItemList items_;
///
@ -461,7 +464,8 @@ void MenuDefinition::read(Lexer & lex)
md_graphicsgroups,
md_spellingsuggestions,
md_languageselector,
md_arguments
md_arguments,
md_switcharguments
};
LexerKeyword menutags[] = {
@ -492,6 +496,7 @@ void MenuDefinition::read(Lexer & lex)
{ "separator", md_separator },
{ "spellingsuggestions", md_spellingsuggestions },
{ "submenu", md_submenu },
{ "switcharguments", md_switcharguments },
{ "toc", md_toc },
{ "toolbars", md_toolbars },
{ "updateformats", md_updateformats },
@ -627,6 +632,10 @@ void MenuDefinition::read(Lexer & lex)
add(MenuItem(MenuItem::Arguments));
break;
case md_switcharguments:
add(MenuItem(MenuItem::SwitchArguments));
break;
case md_optsubmenu:
optional = true;
// fallback to md_submenu
@ -1539,7 +1548,7 @@ void MenuDefinition::expandCiteStyles(BufferView const * bv)
}
void MenuDefinition::expandArguments(BufferView const * bv)
void MenuDefinition::expandArguments(BufferView const * bv, bool switcharg)
{
if (!bv)
return;
@ -1550,7 +1559,7 @@ void MenuDefinition::expandArguments(BufferView const * bv)
args = inset->getLayout().latexargs();
else
args = bv->cursor().paragraph().layout().latexargs();
if (args.empty())
if (args.empty() || (switcharg && args.size() == 1))
return;
Layout::LaTeXArgMap::const_iterator lait = args.begin();
Layout::LaTeXArgMap::const_iterator const laend = args.end();
@ -1559,9 +1568,15 @@ void MenuDefinition::expandArguments(BufferView const * bv)
QString item = toqstr(translateIfPossible(arg.labelstring));
if (!arg.shortcut.empty())
item += "|" + toqstr(arg.shortcut);
add(MenuItem(MenuItem::Command, item,
FuncRequest(LFUN_ARGUMENT_INSERT,
convert<docstring>((*lait).first))));
if (switcharg)
add(MenuItem(MenuItem::Command, item,
FuncRequest(LFUN_INSET_MODIFY,
from_ascii("changetype ")
+ convert<docstring>((*lait).first))));
else
add(MenuItem(MenuItem::Command, item,
FuncRequest(LFUN_ARGUMENT_INSERT,
convert<docstring>((*lait).first))));
}
}
@ -1709,7 +1724,7 @@ struct Menus::Impl {
/** The entries with the following kind are expanded to a
sequence of Command MenuItems: Lastfiles, Documents,
ViewFormats, ExportFormats, UpdateFormats, Branches,
Indices, Arguments
Indices, Arguments, SwitchArguments
*/
void expand(MenuDefinition const & frommenu, MenuDefinition & tomenu,
BufferView const *) const;
@ -1931,7 +1946,11 @@ void Menus::Impl::expand(MenuDefinition const & frommenu,
break;
case MenuItem::Arguments:
tomenu.expandArguments(bv);
tomenu.expandArguments(bv, false);
break;
case MenuItem::SwitchArguments:
tomenu.expandArguments(bv, true);
break;
case MenuItem::Submenu: {

View File

@ -12,6 +12,9 @@
#include "InsetArgument.h"
#include "Cursor.h"
#include "FuncStatus.h"
#include "FuncRequest.h"
#include "InsetList.h"
#include "Layout.h"
#include "Lexer.h"
@ -114,6 +117,86 @@ docstring InsetArgument::toolTip(BufferView const & bv, int, int) const
return toolTipText(tooltip_ + from_ascii(":\n"));
}
void InsetArgument::doDispatch(Cursor & cur, FuncRequest & cmd)
{
switch (cmd.action()) {
case LFUN_INSET_MODIFY: {
string const first_arg = cmd.getArg(0);
bool const change_type = first_arg == "changetype";
if (!change_type) {
// not for us
// this will not be handled higher up
cur.undispatched();
return;
}
cur.recordUndoInset(ATOMIC_UNDO, this);
name_ = cmd.getArg(1);
cur.forceBufferUpdate();
break;
}
default:
InsetCollapsable::doDispatch(cur, cmd);
break;
}
}
bool InsetArgument::getStatus(Cursor & cur, FuncRequest const & cmd,
FuncStatus & flag) const
{
switch (cmd.action()) {
case LFUN_INSET_MODIFY: {
string const first_arg = cmd.getArg(0);
if (first_arg == "changetype") {
string const type = cmd.getArg(1);
flag.setOnOff(type == name_);
if (type == name_) {
flag.setEnabled(true);
return true;
}
Layout::LaTeXArgMap args;
bool const insetlayout = &cur.inset() && cur.paragraph().layout().latexargs().empty();
if (insetlayout)
args = cur.inset().getLayout().latexargs();
else
args = cur.paragraph().layout().latexargs();
Layout::LaTeXArgMap::const_iterator const lait =
args.find(convert<unsigned int>(type));
if (lait != args.end()) {
flag.setEnabled(true);
InsetList::const_iterator it = cur.paragraph().insetList().begin();
InsetList::const_iterator end = cur.paragraph().insetList().end();
for (; it != end; ++it) {
if (it->inset->lyxCode() == ARG_CODE) {
InsetArgument const * ins =
static_cast<InsetArgument const *>(it->inset);
if (ins->name() == type) {
// we have this already
flag.setEnabled(false);
return true;
}
}
}
} else
flag.setEnabled(false);
return true;
}
return InsetCollapsable::getStatus(cur, cmd, flag);
}
default:
return InsetCollapsable::getStatus(cur, cmd, flag);
}
}
string InsetArgument::contextMenuName() const
{
return "context-argument";
}
void InsetArgument::latexArgument(otexstream & os,
OutputParams const & runparams, docstring const & ldelim,
docstring const & rdelim) const

View File

@ -60,6 +60,8 @@ public:
void read(Lexer & lex);
///
bool neverIndent() const { return true; }
///
std::string contextMenuName() const;
//@}
/// \name Public functions inherited from InsetCollapsable class
//@{
@ -81,6 +83,10 @@ protected:
/// \name Protected functions inherited from Inset class
//@{
///
bool getStatus(Cursor &, FuncRequest const &, FuncStatus &) const;
///
void doDispatch(Cursor & cur, FuncRequest & cmd);
///
Inset * clone() const { return new InsetArgument(*this); }
//@}
};