]> git.lyx.org Git - lyx.git/blobdiff - src/mathed/InsetMathRef.cpp
Assure correct spacing of colored items in mathed
[lyx.git] / src / mathed / InsetMathRef.cpp
index 2cbad4ca56e4d4dd3e000214246d612cfea89194..99058d776f2a2668691e4b2af1388736ee981b7d 100644 (file)
@@ -24,7 +24,7 @@
 #include "MathSupport.h"
 #include "OutputParams.h"
 #include "ParIterator.h"
-#include "sgml.h"
+#include "xml.h"
 
 #include "insets/InsetCommand.h"
 
@@ -38,12 +38,12 @@ using namespace std;
 namespace lyx {
 
 InsetMathRef::InsetMathRef(Buffer * buf)
-       : CommandInset(buf, from_ascii("ref"), false)
+       : InsetMathCommand(buf, from_ascii("ref"), false)
 {}
 
 
 InsetMathRef::InsetMathRef(Buffer * buf, docstring const & data)
-       : CommandInset(buf, data, false)
+       : InsetMathCommand(buf, data, false)
 {}
 
 
@@ -64,8 +64,18 @@ void InsetMathRef::doDispatch(Cursor & cur, FuncRequest & cmd)
        switch (cmd.action()) {
        case LFUN_INSET_MODIFY:
                if (cmd.getArg(0) == "ref") {
+                       if (cmd.getArg(1) == "changetarget") {
+                               string const oldtarget = cmd.getArg(2);
+                               string const newtarget = cmd.getArg(3);
+                               if (!oldtarget.empty() && !newtarget.empty()
+                                   && asString(cell(0)) == from_utf8(oldtarget))
+                                       changeTarget(from_utf8(newtarget));
+                               cur.forceBufferUpdate();
+                               break;
+                       }
                        MathData ar;
                        if (createInsetMath_fromDialogStr(cmd.argument(), ar)) {
+                               cur.recordUndo();
                                *this = *ar[0].nucleus()->asRefInset();
                                break;
                        }
@@ -74,12 +84,16 @@ void InsetMathRef::doDispatch(Cursor & cur, FuncRequest & cmd)
                break;
 
        case LFUN_INSET_DIALOG_UPDATE: {
-               string const data = createDialogStr("ref");
+               string const data = createDialogStr();
                cur.bv().updateDialog("ref", data);
                break;
        }
 
        case LFUN_MOUSE_RELEASE:
+               if (cur.selection()) {
+                       cur.undispatched();
+                       break;
+               }
                if (cmd.button() == mouse_button::button3) {
                        LYXERR0("trying to goto ref '" << to_utf8(asString(cell(0))) << "'");
                        //FIXME: use DispatchResult argument
@@ -88,20 +102,30 @@ void InsetMathRef::doDispatch(Cursor & cur, FuncRequest & cmd)
                }
                if (cmd.button() == mouse_button::button1) {
                        // Eventually trigger dialog with button 3, not 1
-                       string const data = createDialogStr("ref");
+                       string const data = createDialogStr();
                        cur.bv().showDialog("ref", data, this);
                        break;
                }
                cur.undispatched();
                break;
 
-       case LFUN_MOUSE_PRESS:
-       case LFUN_MOUSE_MOTION:
+       case LFUN_MOUSE_PRESS: {
+               bool do_selection = cmd.button() == mouse_button::button1
+                       && cmd.modifier() == ShiftModifier;
+               // For some reason the cursor points inside the first cell, which is not
+               // active.
+               cur.leaveInset(*this);
+               cur.bv().mouseSetCursor(cur, do_selection);
+               break;
+       }
+
+       case LFUN_MOUSE_DOUBLE:
+       case LFUN_MOUSE_TRIPLE:
                // eat other mouse commands
                break;
 
        default:
-               CommandInset::doDispatch(cur, cmd);
+               InsetMathCommand::doDispatch(cur, cmd);
                break;
        }
 }
@@ -116,11 +140,12 @@ bool InsetMathRef::getStatus(Cursor & cur, FuncRequest const & cmd,
        case LFUN_INSET_DIALOG_UPDATE:
        case LFUN_MOUSE_RELEASE:
        case LFUN_MOUSE_PRESS:
-       case LFUN_MOUSE_MOTION:
+       case LFUN_MOUSE_DOUBLE:
+       case LFUN_MOUSE_TRIPLE:
                status.setEnabled(true);
                return true;
        default:
-               return CommandInset::getStatus(cur, cmd, status);
+               return InsetMathCommand::getStatus(cur, cmd, status);
        }
 }
 
@@ -152,48 +177,44 @@ void InsetMathRef::validate(LaTeXFeatures & features) const
                features.require("prettyref");
        else if (commandname() == "eqref")
                features.require("amsmath");
+       else if (commandname() == "nameref")
+               features.require("nameref");
 }
 
 
-int InsetMathRef::docbook(odocstream & os, OutputParams const & runparams) const
+void InsetMathRef::docbook(XMLStream & xs, OutputParams const &) const
 {
        if (cell(1).empty()) {
-               os << "<xref linkend=\""
-                  << sgml::cleanID(buffer(), runparams, asString(cell(0)));
-               if (runparams.flavor == OutputParams::XML)
-                       os << "\"/>";
-               else
-                       os << "\">";
+               docstring attr = from_utf8("linkend=\"") + xml::cleanID(asString(cell(0))) + from_utf8("\"");
+               xs << xml::CompTag("xref", to_utf8(attr));
        } else {
-               os << "<link linkend=\""
-                  << sgml::cleanID(buffer(), runparams, asString(cell(0)))
-                  << "\">"
+               // Link with linkend, as is it within the document (not outside, in which case xlink:href is better suited).
+               docstring attr = from_utf8("linkend=\"") + xml::cleanID(asString(cell(0))) + from_utf8("\"");
+               xs << xml::StartTag("link", to_utf8(attr))
                   << asString(cell(1))
-                  << "</link>";
+                  << xml::EndTag("link");
        }
-
-       return 0;
 }
 
 
-void InsetMathRef::updateBuffer(ParIterator const & it, UpdateType /*utype*/)
+void InsetMathRef::updateBuffer(ParIterator const & it, UpdateType /*utype*/, bool const /*deleted*/)
 {
        if (!buffer_) {
                LYXERR0("InsetMathRef::updateBuffer: no buffer_!");
                return;
        }
        // register this inset into the buffer reference cache.
-       buffer().references(getTarget()).push_back(make_pair(this, it));
+       buffer().addReference(getTarget(), this, it);
 }
 
 
-string const InsetMathRef::createDialogStr(string const & name) const
+string const InsetMathRef::createDialogStr() const
 {
        InsetCommandParams icp(REF_CODE, to_ascii(commandname()));
        icp["reference"] = asString(cell(0));
        if (!cell(1).empty())
                icp["name"] = asString(cell(1));
-       return InsetCommand::params2string(name, icp);
+       return InsetCommand::params2string(icp);
 }
 
 
@@ -212,7 +233,7 @@ void InsetMathRef::changeTarget(docstring const & target)
        MathData ar;
        Buffer & buf = buffer();
        if (createInsetMath_fromDialogStr(
-           from_utf8(InsetCommand::params2string("ref", icp)), ar)) {
+           from_utf8(InsetCommand::params2string(icp)), ar)) {
                *this = *ar[0].nucleus()->asRefInset();
                // FIXME audit setBuffer calls
                setBuffer(buf);
@@ -227,6 +248,7 @@ InsetMathRef::ref_type_info InsetMathRef::types[] = {
        { from_ascii("vpageref"),  from_ascii(N_("Textual Page Number")),   from_ascii(N_("TextPage: "))},
        { from_ascii("vref"),      from_ascii(N_("Standard+Textual Page")), from_ascii(N_("Ref+Text: "))},
        { from_ascii("prettyref"), from_ascii(N_("PrettyRef")),             from_ascii(N_("FormatRef: "))},
+       { from_ascii("nameref"),   from_ascii(N_("Reference to Name")),     from_ascii(N_("NameRef: "))},
        { from_ascii(""), from_ascii(""), from_ascii("") }
 };