]> git.lyx.org Git - features.git/commitdiff
InsetInfo: add InsetInfo (core)
authorBo Peng <bpeng@lyx.org>
Thu, 11 Oct 2007 14:52:00 +0000 (14:52 +0000)
committerBo Peng <bpeng@lyx.org>
Thu, 11 Oct 2007 14:52:00 +0000 (14:52 +0000)
* 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

development/scons/scons_manifest.py
src/BufferView.cpp
src/LyXAction.cpp
src/Makefile.am
src/Text3.cpp
src/factory.cpp
src/insets/Inset.cpp
src/insets/Inset.h
src/insets/InsetInfo.cpp [new file with mode: 0644]
src/insets/InsetInfo.h [new file with mode: 0644]
src/lfuns.h

index bbafa230e3a2a50ffa0769f8aac775afeb681d22..a63195a99f8c0e861d66d29c75e4078c20a1e99a 100644 (file)
@@ -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
index 72d6382a7debc33369cde0418cd43820dacb62f6..109fa8eb244d0e6a62cc7126e9b625709217ee5c 100644 (file)
@@ -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:
index 127ff4d4acd1900650c799e9b1eb6654c4f41e43..29874f7798a7ff1406a302c32b5c5a5a596a35d0 100644 (file)
@@ -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 }
        };
index 2a280a107baebfda9f021e95040e64d5d4b297b2..b65888ded4aebabdf8b9ccb541f2b1d7f687834d 100644 (file)
@@ -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 \
index 1dd9b126b85241851595420245488c3bbf28cdb5..6ae28a5d9bfc62e8aba988cb8081fb27095a20ff 100644 (file)
@@ -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<InsetInfo *>(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())
index faa386251c7a4fbf9c34cb1c5f6ddd2ad649a946..278b64fb383826fa9a5172fff863c982e4faf0cb 100644 (file)
@@ -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;
index 0814a5042305557114a8d06bd40ef7b960544b3f..d6e6c4d760bfd5c6577d243b98569cb6f3b74c07 100644 (file)
@@ -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 =
index 2090a7e369ae0600a21f1c6d79a28c4d20f5a5e6..d109b1e9fe6ee33014cb8214dbe04ce320a4c4ec 100644 (file)
@@ -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 (file)
index 0000000..831a7a4
--- /dev/null
@@ -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 <config.h>
+
+#include "InsetInfo.h"
+
+#include <sstream>
+#include <stack>
+
+#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<InsetInfo::info_type, string> const initTranslator()
+{      
+       Translator<InsetInfo::info_type, string> 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<InsetInfo::info_type, std::string> const & InsetInfo::nameTranslator() const
+{
+       static Translator<info_type, string> 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<bool, lyx::textclass_type> 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 (file)
index 0000000..1982165
--- /dev/null
@@ -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<info_type, std::string> 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
index b216e35ecfffcb43e60d4582c8dc5565e8dca849..033a963e663c18d577db1d9c41afb691af1ebbb8 100644 (file)
@@ -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
 };