Cleanup and layoutification of the InsetInfo class.

* InsetInfo.cpp (InsetInfo): derive from InsetCollapsable instead of 
	InsetText, so that we can use InsetLayout. Should be reverted when the 
	layout stuff is migrated to InsetText or some lower level.
	(initTranslator, nameTranslator): moved outside of the class.
	(name, neverIndent): added.
	(draw, setMouseHover): removed.
	(updateInfo): cleanup and use layout
	(read, setInfo): make sure to reset layout.

	* lib/layouts/stdinsets.inc: add layouts for the various Info insets.
	These are used for both display and output.


git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@26646 a592a061-630c-0410-9148-cb99ea01b6c8
This commit is contained in:
Jean-Marc Lasgouttes 2008-09-30 14:11:40 +00:00
parent 709df05d9f
commit ede2dbfade
3 changed files with 142 additions and 96 deletions

View File

@ -273,3 +273,43 @@ InsetLayout OptArg
EndFont
MultiPar false
End
InsetLayout Info
Decoration conglomerate
End
InsetLayout Info:menu
LatexType command
LatexName menuitem
Preamble
\providecommand{\menuitem}[1]{\textsf{#1}}
EndPreamble
Decoration conglomerate
Font
Family sans
EndFont
End
InsetLayout Info:shortcut
LatexType command
LatexName shortcut
Preamble
\providecommand{\shortcut}[1]{\textsf{#1}}
EndPreamble
Decoration conglomerate
Font
Family sans
EndFont
End
InsetLayout Info:shortcuts
LatexType command
LatexName shortcut
Preamble
\providecommand{\shortcut}[1]{\textsf{#1}}
EndPreamble
Decoration conglomerate
Font
Family sans
EndFont
End

View File

@ -45,51 +45,13 @@ using namespace lyx::support;
namespace lyx {
InsetInfo::InsetInfo(Buffer const & buf, string const & name)
: InsetText(buf), type_(UNKNOWN_INFO), name_(),
mouse_hover_(false)
{
setAutoBreakRows(true);
setDrawFrame(true);
setInfo(name);
}
Inset * InsetInfo::editXY(Cursor & cur, int x, int y)
{
cur.push(*this);
return InsetText::editXY(cur, x, y);
}
void InsetInfo::draw(PainterInfo & pi, int x, int y) const
{
InsetText::draw(pi, x, y);
}
string InsetInfo::infoType() const
{
return nameTranslator().find(type_);
}
docstring InsetInfo::toolTip(BufferView const &, int, int) const
{
odocstringstream os;
os << _("Information regarding ")
<< _(nameTranslator().find(type_))
<< " " << from_utf8(name_);
return os.str();
}
namespace {
Translator<InsetInfo::info_type, string> const initTranslator()
typedef Translator<InsetInfo::info_type, string> NameTranslator;
NameTranslator const initTranslator()
{
Translator<InsetInfo::info_type, string>
translator(InsetInfo::UNKNOWN_INFO, "unknown");
NameTranslator translator(InsetInfo::UNKNOWN_INFO, "unknown");
translator.addPair(InsetInfo::SHORTCUTS_INFO, "shortcuts");
translator.addPair(InsetInfo::SHORTCUT_INFO, "shortcut");
@ -103,15 +65,57 @@ Translator<InsetInfo::info_type, string> const initTranslator()
return translator;
}
} // namespace anon
Translator<InsetInfo::info_type, string>
const & InsetInfo::nameTranslator() const
/// The translator between the information type enum and corresponding string.
NameTranslator const & nameTranslator()
{
static Translator<info_type, string> const translator = initTranslator();
static NameTranslator const translator = initTranslator();
return translator;
}
} // namespace anon
/////////////////////////////////////////////////////////////////////////
//
// InsetInfo
//
/////////////////////////////////////////////////////////////////////////
InsetInfo::InsetInfo(Buffer const & buf, string const & name)
: InsetCollapsable(buf, Collapsed), type_(UNKNOWN_INFO), name_()
{
setAutoBreakRows(true);
setInfo(name);
}
Inset * InsetInfo::editXY(Cursor & cur, int x, int y)
{
cur.push(*this);
return InsetCollapsable::editXY(cur, x, y);
}
string InsetInfo::infoType() const
{
return nameTranslator().find(type_);
}
docstring InsetInfo::name() const
{
return from_ascii("Info:" + infoType());
}
docstring InsetInfo::toolTip(BufferView const &, int, int) const
{
odocstringstream os;
os << _("Information regarding ")
<< _(infoType()) << " " << from_utf8(name_);
return os.str();
}
void InsetInfo::read(Lexer & lex)
@ -136,14 +140,14 @@ void InsetInfo::read(Lexer & lex)
_("Missing \\end_inset at this point."),
from_utf8(token));
}
setLayout(buffer().params());
updateInfo();
}
void InsetInfo::write(ostream & os) const
{
os << "Info\ntype \"" << nameTranslator().find(type_)
<< "\"\narg \"" << name_ << '\"';
os << "Info\ntype \"" << infoType() << "\"\narg \"" << name_ << '\"';
}
@ -194,7 +198,7 @@ bool InsetInfo::getStatus(Cursor & cur, FuncRequest const & cmd,
case LFUN_MOUSE_TRIPLE:
case LFUN_COPY:
case LFUN_INSET_SETTINGS:
return InsetText::getStatus(cur, cmd, flag);
return InsetCollapsable::getStatus(cur, cmd, flag);
case LFUN_INSET_MODIFY:
flag.setEnabled(true);
@ -218,7 +222,7 @@ void InsetInfo::doDispatch(Cursor & cur, FuncRequest & cmd)
case LFUN_MOUSE_TRIPLE:
case LFUN_COPY:
case LFUN_INSET_SETTINGS:
InsetText::doDispatch(cur, cmd);
InsetCollapsable::doDispatch(cur, cmd);
break;
case LFUN_INSET_MODIFY:
@ -240,27 +244,25 @@ void InsetInfo::setInfo(string const & name)
string type;
name_ = trim(split(name, type, ' '));
type_ = nameTranslator().find(type);
setLayout(buffer().params());
updateInfo();
}
void InsetInfo::error(string const & err)
{
InsetText::setText(bformat(_(err), from_utf8(name_)), buffer().params().getFont(),
false);
setText(bformat(_(err), from_utf8(name_)), Font(inherit_font), false);
}
void InsetInfo::setText(docstring const & str)
{
InsetText::setText(str, buffer().params().getFont(), false);
setText(str, Font(inherit_font), false);
}
void InsetInfo::updateInfo()
{
InsetText::clear();
BufferParams const & bp = buffer().params();
switch (type_) {
@ -281,9 +283,11 @@ void InsetInfo::updateInfo()
break;
}
if (type_ == SHORTCUT_INFO)
setText(bindings.rbegin()->print(KeySequence::Portable));
setText(bindings.rbegin()->print(KeySequence::Portable),
Font(getLayout().font()), false);
else
setText(theTopLevelKeymap().printBindings(func));
setText(theTopLevelKeymap().printBindings(func),
Font(getLayout().font()), false);
break;
}
case LYXRC_INFO: {
@ -319,35 +323,48 @@ void InsetInfo::updateInfo()
error("No menu entry for action %1$s");
break;
}
// if find, return its path.
InsetText::clear();
Paragraph & info = paragraphs().front();
unsigned int i = 0;
while (!names.empty()) {
// if found, return its path.
clear();
Paragraph & par = paragraphs().front();
Font const f = Font(getLayout().font());
//Font fu = f;
//fu.fontInfo().setUnderbar(FONT_ON);
docstring_list::const_iterator beg = names.begin();
docstring_list::const_iterator end = names.end();
for (docstring_list::const_iterator it = beg ;
it != end ; ++it) {
// do not insert > for the top level menu item
if (i != 0)
info.insertInset(0, new InsetSpecialChar(InsetSpecialChar::MENU_SEPARATOR),
Change(Change::UNCHANGED));
for (i = 0; i != names.back().length(); ++i)
info.insertChar(i, names.back()[i], bp.getFont(), false);
names.pop_back();
if (it != beg)
par.insertInset(par.size(), new InsetSpecialChar(InsetSpecialChar::MENU_SEPARATOR),
Change(Change::UNCHANGED));
//FIXME: add proper underlines here. This
// involves rewriting searchMenu used above to
// return a vector of menus. If we do not do
// that, we might as well use below
// Paragraph::insert on each string (JMarc)
for (size_type i = 0; i != it->length(); ++i)
par.insertChar(par.size(), (*it)[i],
f, Change(Change::UNCHANGED));
}
break;
}
case ICON_INFO: {
FuncRequest func = lyxaction.lookupFunc(name_);
docstring icon_name = theApp()->iconName(func, true);
//FIXME: We should use the icon directly instead of going through
// FileName. The code below won't work if the icon is embedded in the
// executable through the Qt resource system.
//FIXME: We should use the icon directly instead of
// going through FileName. The code below won't work
// if the icon is embedded in the executable through
// the Qt resource system.
FileName file(to_utf8(icon_name));
if (!file.exists())
break;
InsetGraphics * inset = new InsetGraphics(buffer());
InsetGraphicsParams igp;
igp.filename = file;
InsetGraphics * inset = new InsetGraphics(buffer());
inset->setParams(igp);
paragraphs().front().insertInset(0, inset, Change(Change::UNCHANGED));
clear();
paragraphs().front().insertInset(0, inset,
Change(Change::UNCHANGED));
break;
}
case BUFFER_INFO: {
@ -362,15 +379,6 @@ void InsetInfo::updateInfo()
break;
}
}
// remove indent
paragraphs().begin()->params().noindent(true);
}
bool InsetInfo::setMouseHover(bool mouse_hover)
{
mouse_hover_ = mouse_hover;
return true;
}
@ -379,4 +387,5 @@ docstring InsetInfo::contextMenu(BufferView const &, int, int) const
return from_ascii("context-info");
}
} // namespace lyx

View File

@ -12,8 +12,7 @@
#ifndef INSET_INFO_H
#define INSET_INFO_H
#include "InsetText.h"
#include "RenderButton.h"
#include "InsetCollapsable.h"
#include "Cursor.h"
#include "support/gettext.h"
@ -78,7 +77,7 @@ namespace lyx {
/** Used to insert index labels
*/
class InsetInfo : public InsetText {
class InsetInfo : public InsetCollapsable {
public:
enum info_type {
UNKNOWN_INFO, // Invalid type
@ -95,12 +94,14 @@ public:
///
InsetInfo(Buffer const & buf, std::string const & info = std::string());
///
InsetCode lyxCode() const { return INFO_CODE; }
///
docstring name() const;
///
Inset * editXY(Cursor & cur, int x, int y);
///
EDITABLE editable() const { return NOT_EDITABLE; }
///
void draw(PainterInfo & pi, int x, int y) const;
///
void read(Lexer & lex);
///
void write(std::ostream & os) const;
@ -117,33 +118,29 @@ public:
///
void doDispatch(Cursor & cur, FuncRequest & cmd);
///
InsetCode lyxCode() const { return INFO_CODE; }
///
void setInfo(std::string const & info);
/// update info_ and text
void updateInfo();
///
bool setMouseHover(bool mouse_hover);
///
docstring toolTip(BufferView const & bv, int x, int y) const;
///
docstring contextMenu(BufferView const &, int, int) const;
/// should paragraph indendation be ommitted in any case?
bool neverIndent() const { return true; }
private:
/// The translator between the information type enum and corresponding string.
Translator<info_type, std::string> const & nameTranslator() const;
///
virtual Inset * clone() const { return new InsetInfo(*this); }
///
void error(std::string const & err);
///
void setText(docstring const & str);
// make sure that the other version of setText is still available.
using InsetCollapsable::setText;
///
info_type type_;
///
std::string name_;
///
bool mouse_hover_;
};