From: Bo Peng Date: Thu, 11 Oct 2007 14:52:00 +0000 (+0000) Subject: InsetInfo: add InsetInfo (core) X-Git-Tag: 1.6.10~7888 X-Git-Url: https://git.lyx.org/gitweb/?a=commitdiff_plain;h=62c98f2ffca915a6d4c91753b9340dc57722b43f;p=features.git InsetInfo: add InsetInfo (core) * src/insets/Inset.cpp: add InsetInfo * src/insets/Inset.h * src/insets/InsetInfo.cpp * src/insets/InsetInfo.h * src/LyXAction.cpp: add LFUN_INFO_INSERT * src/lfuns.h * src/BufferView.cpp: * src/factory.cpp * src/Text3.cpp: insert InsetInfo with selected text * development/scons/scons_manifest.py: build tools update * src/Makefile.am git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@20904 a592a061-630c-0410-9148-cb99ea01b6c8 --- diff --git a/development/scons/scons_manifest.py b/development/scons/scons_manifest.py index bbafa230e3..a63195a99f 100644 --- a/development/scons/scons_manifest.py +++ b/development/scons/scons_manifest.py @@ -1028,6 +1028,7 @@ src_insets_header_files = Split(''' InsetHFill.h InsetInclude.h InsetIndex.h + InsetInfo.h InsetLabel.h InsetLine.h InsetListings.h @@ -1083,6 +1084,7 @@ src_insets_files = Split(''' InsetHFill.cpp InsetInclude.cpp InsetIndex.cpp + InsetInfo.cpp InsetLabel.cpp InsetLine.cpp InsetListings.cpp diff --git a/src/BufferView.cpp b/src/BufferView.cpp index 72d6382a7d..109fa8eb24 100644 --- a/src/BufferView.cpp +++ b/src/BufferView.cpp @@ -867,6 +867,7 @@ FuncStatus BufferView::getStatus(FuncRequest const & cmd) break; case LFUN_FONT_STATE: case LFUN_LABEL_INSERT: + case LFUN_INFO_INSERT: case LFUN_PARAGRAPH_GOTO: // FIXME handle non-trivially case LFUN_OUTLINE_UP: diff --git a/src/LyXAction.cpp b/src/LyXAction.cpp index 127ff4d4ac..29874f7798 100644 --- a/src/LyXAction.cpp +++ b/src/LyXAction.cpp @@ -376,6 +376,7 @@ void LyXAction::init() { LFUN_LAYOUT_MODULES_CLEAR, "layout-modules-clear", Noop }, { LFUN_LAYOUT_MODULE_ADD, "layout-module-add", Noop }, { LFUN_LAYOUT_RELOAD, "layout-reload", Noop }, + { LFUN_INFO_INSERT, "info-insert", Noop }, { LFUN_NOACTION, "", Noop } }; diff --git a/src/Makefile.am b/src/Makefile.am index 2a280a107b..b65888ded4 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -523,6 +523,8 @@ liblyxinsets_la_SOURCES = \ insets/InsetInclude.h \ insets/InsetIndex.cpp \ insets/InsetIndex.h \ + insets/InsetInfo.cpp \ + insets/InsetInfo.h \ insets/InsetLabel.cpp \ insets/InsetLabel.h \ insets/InsetLine.cpp \ diff --git a/src/Text3.cpp b/src/Text3.cpp index 1dd9b126b8..6ae28a5d9b 100644 --- a/src/Text3.cpp +++ b/src/Text3.cpp @@ -58,6 +58,7 @@ #include "insets/InsetQuotes.h" #include "insets/InsetSpecialChar.h" #include "insets/InsetText.h" +#include "insets/InsetInfo.h" #include "support/lstrings.h" #include "support/lyxlib.h" @@ -1113,7 +1114,20 @@ void Text::dispatch(Cursor & cur, FuncRequest & cmd) break; } - + case LFUN_INFO_INSERT: { + if (!cur.selection()) + break; + Inset * inset = createInset(&cur.bv(), cmd); + if (!inset) + break; + // use selected text as info to avoid a separate UI + docstring ds = cur.selectionAsString(false); + cutSelection(cur, true, false); + insertInset(cur, inset); + static_cast(inset)->setInfo(to_utf8(ds)); + cur.posRight(); + break; + } #if 0 case LFUN_LIST_INSERT: case LFUN_THEOREM_INSERT: @@ -1706,6 +1720,9 @@ bool Text::getStatus(Cursor & cur, FuncRequest const & cmd, case LFUN_LABEL_INSERT: code = Inset::LABEL_CODE; break; + case LFUN_INFO_INSERT: + code = Inset::INFO_CODE; + break; case LFUN_OPTIONAL_INSERT: code = Inset::OPTARG_CODE; enable = numberOfOptArgs(cur.paragraph()) diff --git a/src/factory.cpp b/src/factory.cpp index faa386251c..278b64fb38 100644 --- a/src/factory.cpp +++ b/src/factory.cpp @@ -38,6 +38,7 @@ #include "insets/InsetHFill.h" #include "insets/InsetInclude.h" #include "insets/InsetIndex.h" +#include "insets/InsetInfo.h" #include "insets/InsetNomencl.h" #include "insets/InsetLabel.h" #include "insets/InsetLine.h" @@ -221,6 +222,8 @@ Inset * createInset(BufferView * bv, FuncRequest const & cmd) case LFUN_ENVIRONMENT_INSERT: return new InsetEnvironment(params, cmd.argument()); + case LFUN_INFO_INSERT: + return new InsetInfo(params, to_utf8(cmd.argument())); #if 0 case LFUN_LIST_INSERT: return new InsetList; @@ -497,6 +500,8 @@ Inset * readInset(Lexer & lex, Buffer const & buf) inset.reset(new InsetIndex(buf.params())); } else if (tmptok == "FloatList") { inset.reset(new InsetFloatList); + } else if (tmptok == "Info") { + inset.reset(new InsetInfo(buf.params())); } else { lyxerr << "unknown Inset type '" << tmptok << "'" << std::endl; diff --git a/src/insets/Inset.cpp b/src/insets/Inset.cpp index 0814a50423..d6e6c4d760 100644 --- a/src/insets/Inset.cpp +++ b/src/insets/Inset.cpp @@ -103,6 +103,7 @@ static TranslatorMap const build_translator() InsetName("vspace", Inset::VSPACE_CODE), InsetName("mathmacroarg", Inset::MATHMACROARG_CODE), InsetName("listings", Inset::LISTINGS_CODE), + InsetName("info", Inset::INFO_CODE), }; std::size_t const insetnames_size = diff --git a/src/insets/Inset.h b/src/insets/Inset.h index 2090a7e369..d109b1e9fe 100644 --- a/src/insets/Inset.h +++ b/src/insets/Inset.h @@ -367,7 +367,9 @@ public: /// PAGEBREAK_CODE, /// - LISTINGS_CODE + LISTINGS_CODE, + /// + INFO_CODE, }; /** returns the Code corresponding to the \c name. diff --git a/src/insets/InsetInfo.cpp b/src/insets/InsetInfo.cpp new file mode 100644 index 0000000000..831a7a4b1a --- /dev/null +++ b/src/insets/InsetInfo.cpp @@ -0,0 +1,208 @@ +/** + * \file InsetInfo.cpp + * This file is part of LyX, the document processor. + * Licence details can be found in the file COPYING. + * + * \author Bo Peng + * + * Full author contact details are available in file CREDITS. + */ +#include + +#include "InsetInfo.h" + +#include +#include + +#include "BufferParams.h" +#include "BufferView.h" +#include "debug.h" +#include "FuncRequest.h" +#include "gettext.h" +#include "InsetSpecialChar.h" +#include "KeyMap.h" +#include "LaTeXFeatures.h" +#include "LyXAction.h" +#include "Lexer.h" +#include "MetricsInfo.h" +#include "ParagraphParameters.h" +#include "TextClassList.h" +#include "support/lstrings.h" +#include "support/ExceptionMessage.h" + +using std::pair; +using std::string; +using std::ostream; +using std::ostringstream; +using std::stack; + +namespace lyx { + +using support::prefixIs; +using support::trim; +using support::split; +using support::rtrim; +using support::ExceptionMessage; +using support::WarningException; + +InsetInfo::InsetInfo(BufferParams const & bp, string const & name) + : InsetText(bp), bp_(bp), type_(UNKNOWN_INFO), name_(), + mouse_hover_(false) +{ + setAutoBreakRows(true); + setDrawFrame(true); + setInfo(name); +} + + +Inset * InsetInfo::editXY(Cursor & cur, int x, int y) +{ + return this; +} + + +void InsetInfo::draw(PainterInfo & pi, int x, int y) const +{ + InsetText::draw(pi, x, y); + if (mouse_hover_) { + odocstringstream os; + os << _("Information regarding ") + <<_(nameTranslator().find(type_)) + << _(" ") << from_utf8(name_); + pi.base.bv->message(os.str()); + } +} + + +namespace { + +Translator const initTranslator() +{ + Translator translator(InsetInfo::UNKNOWN_INFO, "unknown"); + + translator.addPair(InsetInfo::SHORTCUT_INFO, "shortcut"); + translator.addPair(InsetInfo::PACKAGE_INFO, "package"); + translator.addPair(InsetInfo::TEXTCLASS_INFO, "textclass"); + + return translator; +} + +} // namespace anon + +Translator const & InsetInfo::nameTranslator() const +{ + static Translator const translator = + initTranslator(); + return translator; +} + + + +void InsetInfo::read(Buffer const & buf, Lexer & lex) +{ + string token; + while (lex.isOK()) { + lex.next(); + token = lex.getString(); + if (token == "type") { + lex.next(); + token = lex.getString(); + type_ = nameTranslator().find(token); + } else if (token == "arg") { + lex.next(); + name_ = lex.getString(); + } else if (token == "\\end_inset") + break; + } + if (token != "\\end_inset") { + lex.printError("Missing \\end_inset at this point"); + throw ExceptionMessage(WarningException, + _("Missing \\end_inset at this point."), + from_utf8(token)); + } + updateInfo(); +} + + +void InsetInfo::write(Buffer const & buf, std::ostream & os) const +{ + os << "Info\ntype \"" + << nameTranslator().find(type_) + << "\"\narg \"" << name_ << '\"'; +} + + +void InsetInfo::doDispatch(Cursor & cur, FuncRequest & cmd) +{ + // FIXME: we should allow selection, copy etc... + switch (cmd.action) { + case LFUN_MOUSE_PRESS: + case LFUN_MOUSE_RELEASE: + case LFUN_MOUSE_MOTION: + case LFUN_MOUSE_DOUBLE: + case LFUN_MOUSE_TRIPLE: + // do not dispatch to InsetText + cur.dispatched(); + break; + + default: + InsetText::doDispatch(cur, cmd); + break; + } +} + + +void InsetInfo::setInfo(string const & name) +{ + if (name.empty()) + return; + // info_type name + string type; + name_ = trim(split(name, type, ' ')); + type_ = nameTranslator().find(type); + updateInfo(); +} + + +void InsetInfo::updateInfo() +{ + InsetText::clear(); + + switch (type_) { + case UNKNOWN_INFO: + setText(_("Unknown Info: ") + from_utf8(name_), + bp_.getFont(), false); + break; + case SHORTCUT_INFO: { + FuncRequest func = lyxaction.lookupFunc(name_); + if (func.action != LFUN_UNKNOWN_ACTION) + setText(theTopLevelKeymap().printbindings(func), + bp_.getFont(), false); + break; + } + case PACKAGE_INFO: + // check in packages.lst + setText(LaTeXFeatures::isAvailable(name_) ? _("yes") : _("no"), + bp_.getFont(), false); + break; + case TEXTCLASS_INFO: { + // name_ is the class name + pair pp = + textclasslist.numberOfClass(name_); + setText(pp.first ? _("yes") : _("no"), + bp_.getFont(), false); + break; + } + } + // remove indent + paragraphs().begin()->params().noindent(true); +} + + +bool InsetInfo::setMouseHover(bool mouse_hover) +{ + mouse_hover_ = mouse_hover; + return true; +} + +} // namespace lyx diff --git a/src/insets/InsetInfo.h b/src/insets/InsetInfo.h new file mode 100644 index 0000000000..19821658ae --- /dev/null +++ b/src/insets/InsetInfo.h @@ -0,0 +1,76 @@ +// -*- C++ -*- +/** + * \file InsetInfo.h + * This file is part of LyX, the document processor. + * Licence details can be found in the file COPYING. + * + * \author Bo Peng + * + * Full author contact details are available in file CREDITS. + */ + +#ifndef INSET_INFO_H +#define INSET_INFO_H + +#include "InsetText.h" +#include "RenderButton.h" +#include "gettext.h" +#include "Cursor.h" +#include "support/Translator.h" + +namespace lyx { + +/** Used to insert index labels + */ +class InsetInfo : public InsetText { +public: + enum info_type { + UNKNOWN_INFO, // Invalid type + SHORTCUT_INFO, // Keyboard shortcut + PACKAGE_INFO, // Availability of package + TEXTCLASS_INFO, // Availability of textclass + }; + + /// + InsetInfo(BufferParams const & bp, std::string const & info = std::string()); + /// + 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(Buffer const &, Lexer & lex); + /// + void write(Buffer const & buf, std::ostream & os) const; + /// + void doDispatch(Cursor & cur, FuncRequest & cmd); + /// + Inset::Code lyxCode() const { return Inset::INFO_CODE; } + /// + void setInfo(std::string const & info); + /// + bool setMouseHover(bool mouse_hover); + +private: + /// The translator between the information type enum and corresponding string. + Translator const & nameTranslator() const; + /// update info_ and text + void updateInfo(); + /// + virtual Inset * clone() const { return new InsetInfo(*this); } + /// + info_type type_; + /// + std::string name_; + /// store the buffer parameter + BufferParams const & bp_; + /// + bool mouse_hover_; +}; + + + +} // namespace lyx + +#endif diff --git a/src/lfuns.h b/src/lfuns.h index b216e35ecf..033a963e66 100644 --- a/src/lfuns.h +++ b/src/lfuns.h @@ -402,8 +402,9 @@ enum kb_action { LFUN_LAYOUT_MODULES_CLEAR, // rgh, 20070825 LFUN_LAYOUT_MODULE_ADD, // rgh, 20070825 LFUN_LAYOUT_RELOAD, // rgh, 20070903 - LFUN_MASTER_BUFFER_VIEW, // Tommaso, 20070920 - LFUN_MASTER_BUFFER_UPDATE, // Tommaso, 20070920 + LFUN_MASTER_BUFFER_VIEW, // Tommaso, 20070920 + LFUN_MASTER_BUFFER_UPDATE, // Tommaso, 20070920 + LFUN_INFO_INSERT, // bpeng, 20071007 LFUN_LASTACTION // end of the table };