André, I thought I'd have a go at creating a LabelInset for mathed analogous
to InsetLabel in the outside world. I attach the current work in progress.
Could you cast your beady eye over it and maybe answer a couple of
questions?
* InsetLatex::latex escapes the contents of the label. Should I create a
write method for LabelInset that does the same with the contents of
cell(0)?
* In math_parser.C, why is the new RefInset(t.cs()) not handled by
createMathInset?
else if (t.cs() == "ref" || t.cs() == "prettyref" ||
t.cs() == "pageref" || t.cs() == "vpageref" || t.cs()
== "vref") {
cell->push_back(MathAtom(new RefInset(t.cs())));
parse(cell->back().nucleus()->cell(1), FLAG_OPTION, mode);
parse(cell->back().nucleus()->cell(0), FLAG_ITEM, mode);
}
* The string label_ in math_hullinset.[Ch] could now be thrown away. Should
I replace it with a LabelInset*, or could a real inset survive on its own
without such special casing?
At the moment, I believe that the MathHullInset should store a LabelInset*
as a straight replacement for string label_. Reasons: the label should be
placed always at the end of the row. Moreover, a row can have only one
label. I would value some insight, however ;-)
--
Angus
? tmp.diff
Index: Makefile.am
===================================================================
RCS file: /usr/local/lyx/cvsroot/lyx-devel/src/mathed/Makefile.am,v
retrieving revision 1.111
diff -u -p -r1.111 Makefile.am
--- Makefile.am 3 Mar 2003 16:15:38 -0000 1.111
+++ Makefile.am 4 Mar 2003 19:01:25 -0000
@@ -160,5 +160,7 @@ libmathed_la_SOURCES = \
button_inset.h \
command_inset.h \
command_inset.C \
+ label_inset.h \
+ label_inset.C \
ref_inset.h \
ref_inset.C
Index: label_inset.C
===================================================================
RCS file: label_inset.C
diff -N label_inset.C
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ label_inset.C 4 Mar 2003 19:01:25 -0000
@@ -0,0 +1,104 @@
+
+#include <config.h>
+
+#include "label_inset.h"
+#include "math_factory.h"
+
+#include "BufferView.h"
+#include "debug.h"
+#include "funcrequest.h"
+#include "gettext.h"
+#include "LaTeXFeatures.h"
+
+#include "frontends/LyXView.h"
+#include "frontends/Dialogs.h"
+
+#include "support/LOstream.h"
+
+
+LabelInset::LabelInset()
+ : CommandInset("label")
+{}
+
+
+MathInset * LabelInset::clone() const
+{
+ return new LabelInset(*this);
+}
+
+
+void LabelInset::infoize(std::ostream & os) const
+{
+ os << "Label: " << cell(0);
+}
+
+
+string LabelInset::screenLabel() const
+{
+ return asString(cell(0));
+}
+
+
+int LabelInset::ascii(std::ostream & os, int) const
+{
+ os << '[' << asString(cell(0)) << ']';
+ return 0;
+}
+
+
+int LabelInset::linuxdoc(std::ostream & os) const
+{
+ os << "<label id=\"" << asString(cell(0)) << "\" >";
+ return 0;
+}
+
+
+int LabelInset::docbook(std::ostream & os, bool) const
+{
+ os << "<anchor id=\"" << asString(cell(0)) << "\">";
+ return 0;
+}
+
+
+dispatch_result
+LabelInset::dispatch(FuncRequest const & cmd, idx_type & idx, pos_type & pos)
+{
+ switch (cmd.action) {
+ case LFUN_MOUSE_RELEASE:
+ if (cmd.button() == mouse_button::button1) {
+ // Eventually trigger dialog with button 3
+ // not 1
+ string const data = createDialogStr("label");
+ cmd.view()->owner()->getDialogs().
+ show("label", data, this);
+ return DISPATCHED;
+ }
+ break;
+ case LFUN_MOUSE_PRESS:
+ case LFUN_MOUSE_MOTION:
+ // eat other mouse commands
+ return DISPATCHED;
+ default:
+ return CommandInset::dispatch(cmd, idx, pos);
+ }
+ // not our business
+ return UNDISPATCHED;
+}
+
+
+dispatch_result LabelInset::localDispatch(FuncRequest const & cmd)
+{
+ if (cmd.action != LFUN_INSET_MODIFY || cmd.getArg(0) != "label")
+ return UNDISPATCHED;
+
+ MathArray ar;
+ if (!createMathInset_fromDialogStr(cmd.argument, ar))
+ return UNDISPATCHED;
+
+ *this = *ar[0].nucleus()->asLabelInset();
+// if (cmd.view())
+// // This does not compile because updateInset expects
+// // an Inset* and 'this' isn't.
+// cmd.view()->updateInset(this, true);
+ return DISPATCHED;
+}
Index: label_inset.h
===================================================================
RCS file: label_inset.h
diff -N label_inset.h
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ label_inset.h 4 Mar 2003 19:01:25 -0000
@@ -0,0 +1,36 @@
+#ifndef LABEL_INSET_H
+#define LABEL_INSET_H
+
+
+#include "command_inset.h"
+
+// for \label
+class LabelInset : public CommandInset {
+public:
+ ///
+ LabelInset();
+ ///
+ MathInset * clone() const;
+ ///
+ //void write(WriteStream & os) const;
+ ///
+ void infoize(std::ostream & os) const;
+ ///
+ dispatch_result dispatch(FuncRequest const & cmd, idx_type & idx, pos_type & pos);
+ ///
+ string screenLabel() const;
+ ///
+ virtual LabelInset * asLabelInset() { return this; }
+
+ /// plain ascii output
+ int ascii(std::ostream & os, int) const;
+ /// linuxdoc output
+ int linuxdoc(std::ostream & os) const;
+ /// docbook output
+ int docbook(std::ostream & os, bool) const;
+
+ /// small wrapper for the time being
+ dispatch_result localDispatch(FuncRequest const & cmd);
+};
+
+#endif
Index: math_factory.C
===================================================================
RCS file: /usr/local/lyx/cvsroot/lyx-devel/src/mathed/math_factory.C,v
retrieving revision 1.73
diff -u -p -r1.73 math_factory.C
--- math_factory.C 4 Mar 2003 16:39:13 -0000 1.73
+++ math_factory.C 4 Mar 2003 19:01:26 -0000
@@ -309,7 +309,7 @@ bool createMathInset_fromDialogStr(strin
string name;
string body = split(str, name, ' ');
- if (name != "ref" )
+ if (name != "label" && name != "ref")
return false;
// body comes with a head "LatexCommand " and a
@@ -324,4 +324,3 @@ bool createMathInset_fromDialogStr(strin
return ar[0].nucleus();
}
-
Index: math_hullinset.C
===================================================================
RCS file: /usr/local/lyx/cvsroot/lyx-devel/src/mathed/math_hullinset.C,v
retrieving revision 1.68
diff -u -p -r1.68 math_hullinset.C
--- math_hullinset.C 18 Feb 2003 11:47:15 -0000 1.68
+++ math_hullinset.C 4 Mar 2003 19:01:26 -0000
@@ -17,10 +17,14 @@
#include "frontends/Painter.h"
#include "frontends/Alert.h"
+#include "frontends/Dialogs.h"
+#include "frontends/LyXView.h"
#include "lyxrc.h"
#include "gettext.h"
#include "BufferView.h"
+#include "label_inset.h"
+#include "math_factory.h"
#include <vector>
using std::vector;
@@ -785,33 +789,41 @@ dispatch_result MathHullInset::dispatch
case LFUN_INSERT_LABEL: {
row_type r = (type_ == "multline") ? nrows() - 1 : row(idx);
- string old_label = label(r);
+// string old_label = label(r);
string new_label = cmd.argument;
if (new_label.empty()) {
- string const default_label =
- (lyxrc.label_init_length >= 0) ? "eq:" : "";
- pair<bool, string> const res = old_label.empty()
- ? Alert::askForText(_("Enter new label to insert:"), default_label)
- : Alert::askForText(_("Enter label:"), old_label);
- if (!res.first)
- break;
- new_label = trim(res.second);
+ new_label = "eq:";
}
+ LabelInset tmp;
+ tmp.cell(0) = asArray(new_label);
+ string const data = tmp.createDialogStr("label");
+ cmd.view()->owner()->getDialogs().show("label",
+ data, 0);
+// string const default_label =
+// (lyxrc.label_init_length >= 0) ? "eq:" : "";
+// pair<bool, string> const res = old_label.empty()
+// ? Alert::askForText(_("Enter new label to insert:"), default_label)
+// : Alert::askForText(_("Enter label:"), old_label);
+// if (!res.first)
+// break;
+// new_label = trim(res.second);
+// }
+
//if (new_label == old_label)
// break; // Nothing to do
- if (!new_label.empty())
- numbered(r, true);
+// if (!new_label.empty())
+// numbered(r, true);
-#warning FIXME: please check you really mean repaint() ... is it needed,
-#warning and if so, should it be update() instead ?
- if (!new_label.empty()
- && cmd.view()->ChangeRefsIfUnique(old_label, new_label))
- cmd.view()->repaint();
+// #warning FIXME: please check you really mean repaint() ... is it needed,
+// #warning and if so, should it be update() instead ?
+// if (!new_label.empty()
+// && cmd.view()->ChangeRefsIfUnique(old_label, new_label))
+// cmd.view()->repaint();
- label(r, new_label);
+// label(r, new_label);
return DISPATCHED;
}
Index: math_inset.h
===================================================================
RCS file: /usr/local/lyx/cvsroot/lyx-devel/src/mathed/math_inset.h,v
retrieving revision 1.155
diff -u -p -r1.155 math_inset.h
--- math_inset.h 25 Feb 2003 18:21:48 -0000 1.155
+++ math_inset.h 4 Mar 2003 19:01:27 -0000
@@ -62,6 +62,7 @@ class MathSpaceInset;
class MathSymbolInset;
class MathUnknownInset;
+class LabelInset;
class RefInset;
class NormalStream;
@@ -218,6 +219,7 @@ public:
virtual MathSymbolInset const * asSymbolInset() const { return 0; }
virtual MathUnknownInset * asUnknownInset() { return 0; }
virtual MathUnknownInset const * asUnknownInset() const { return 0; }
+ virtual LabelInset * asLabelInset() { return 0; }
virtual RefInset * asRefInset() { return 0; }
/// identifies things that can get scripts
Index: math_parser.C
===================================================================
RCS file: /usr/local/lyx/cvsroot/lyx-devel/src/mathed/math_parser.C,v
retrieving revision 1.269
diff -u -p -r1.269 math_parser.C
--- math_parser.C 4 Mar 2003 09:06:13 -0000 1.269
+++ math_parser.C 4 Mar 2003 19:01:29 -0000
@@ -60,6 +60,7 @@ following hack as starting point to writ
#include "math_xyarrowinset.h"
//#include "insets/insetref.h"
+#include "label_inset.h"
#include "ref_inset.h"
#include "lyxlex.h"
@@ -1107,13 +1108,8 @@ void Parser::parse1(MathGridInset & grid
}
else if (t.cs() == "label") {
- string label = parse_verbatim_item();
- if (grid.asHullInset()) {
- grid.asHullInset()->label(cellrow, label);
- } else {
- cell->push_back(createMathInset(t.cs()));
- cell->push_back(MathAtom(new MathBraceInset(asArray(label))));
- }
+ cell->push_back(MathAtom(new LabelInset()));
+ parse(cell->back().nucleus()->cell(0), FLAG_ITEM, mode);
}
else if (t.cs() == "choose" || t.cs() == "over" || t.cs() == "atop") {