]> git.lyx.org Git - lyx.git/blobdiff - src/insets/InsetNewline.cpp
DocBook: support bookauthor in bibliographies.
[lyx.git] / src / insets / InsetNewline.cpp
index 103fbe866a590ee12d8bdc6dc2618a8891caf7f5..ecfacf47cef19f8e8546454980fea97055021f85 100644 (file)
@@ -4,6 +4,7 @@
  * Licence details can be found in the file COPYING.
  *
  * \author John Levon
+ * \author Jürgen Spitzmüller
  *
  * Full author contact details are available in file CREDITS.
  */
 
 #include "InsetNewline.h"
 
-#include "support/debug.h"
+#include "Cursor.h"
 #include "Dimension.h"
+#include "FuncRequest.h"
+#include "FuncStatus.h"
+#include "Lexer.h"
 #include "MetricsInfo.h"
-#include "OutputParams.h"
+#include "output_docbook.h"
+#include "output_xhtml.h"
+#include "texstream.h"
 
+#include "frontends/Application.h"
 #include "frontends/FontMetrics.h"
 #include "frontends/Painter.h"
 
-#include "support/docstring.h"
+#include "support/debug.h"
 #include "support/docstream.h"
+#include "support/docstring.h"
 
+using namespace std;
 
 namespace lyx {
 
-using std::endl;
-using std::ostream;
+InsetNewline::InsetNewline() : Inset(nullptr)
+{}
+
+
+int InsetNewline::rowFlags() const
+{
+       if (params_.kind == InsetNewlineParams::LINEBREAK)
+               return AlwaysBreakAfter;
+       else
+           return AlwaysBreakAfter | Flush;
+}
+
+
+void InsetNewlineParams::write(ostream & os) const
+{
+       switch (kind) {
+       case InsetNewlineParams::NEWLINE:
+               os << "newline";
+               break;
+       case InsetNewlineParams::LINEBREAK:
+               os <<  "linebreak";
+               break;
+       }
+}
 
 
-void InsetNewline::read(Buffer const &, Lexer &)
+void InsetNewlineParams::read(Lexer & lex)
 {
-       /* Nothing to read */
+       string token;
+       lex.setContext("InsetNewlineParams::read");
+       lex >> token;
+       if (token == "newline")
+               kind = InsetNewlineParams::NEWLINE;
+       else if (token == "linebreak")
+               kind = InsetNewlineParams::LINEBREAK;
+       else
+               lex.printError("Unknown kind: `$$Token'");
 }
 
 
-void InsetNewline::write(Buffer const &, ostream & os) const
+void InsetNewline::write(ostream & os) const
 {
-       os << "\n" << getLyXName() << '\n';
+       os << "Newline ";
+       params_.write(os);
+}
+
+
+void InsetNewline::read(Lexer & lex)
+{
+       params_.read(lex);
+       lex >> "\\end_inset";
 }
 
 
@@ -51,27 +98,98 @@ void InsetNewline::metrics(MetricsInfo & mi, Dimension & dim) const
 }
 
 
-int InsetNewline::latex(Buffer const &, odocstream & os,
-                       OutputParams const &) const
+void InsetNewline::doDispatch(Cursor & cur, FuncRequest & cmd)
+{
+       switch (cmd.action()) {
+
+       case LFUN_INSET_MODIFY: {
+               InsetNewlineParams params;
+               cur.recordUndo();
+               string2params(to_utf8(cmd.argument()), params);
+               params_.kind = params.kind;
+               break;
+       }
+
+       default:
+               Inset::doDispatch(cur, cmd);
+               break;
+       }
+}
+
+
+bool InsetNewline::getStatus(Cursor & cur, FuncRequest const & cmd,
+       FuncStatus & status) const
 {
-       os << from_ascii(getCmdName()) << '\n';
-       return 0;
+       switch (cmd.action()) {
+       // we handle these
+       case LFUN_INSET_MODIFY:
+               if (cmd.getArg(0) == "newline") {
+                       InsetNewlineParams params;
+                       string2params(to_utf8(cmd.argument()), params);
+                       status.setOnOff(params_.kind == params.kind);
+               }
+               status.setEnabled(true);
+               return true;
+       default:
+               return Inset::getStatus(cur, cmd, status);
+       }
 }
 
 
-int InsetNewline::plaintext(Buffer const &, odocstream & os,
-                           OutputParams const &) const
+ColorCode InsetNewline::ColorName() const
+{
+       switch (params_.kind) {
+               case InsetNewlineParams::NEWLINE:
+                       return Color_eolmarker;
+                       break;
+               case InsetNewlineParams::LINEBREAK:
+                       return Color_pagebreak;
+                       break;
+       }
+       // not really useful, but to avoids gcc complaints
+       return Color_eolmarker;
+}
+
+
+void InsetNewline::latex(otexstream & os, OutputParams const & rp) const
+{
+       switch (params_.kind) {
+               case InsetNewlineParams::NEWLINE:
+                       if (!rp.newlinecmd.empty())
+                               os << "\\" << rp.newlinecmd << "\n";
+                       else if (rp.inTableCell == OutputParams::PLAIN)
+                               os << "\\newline\n";
+                       else
+                               os << "\\\\\n";
+                       break;
+               case InsetNewlineParams::LINEBREAK:
+                       os << "\\linebreak{}\n";
+                       break;
+               default:
+                       os << "\\\\\n";
+                       break;
+       }
+}
+
+
+int InsetNewline::plaintext(odocstringstream & os,
+        OutputParams const &, size_t) const
 {
        os << '\n';
        return PLAINTEXT_NEWLINE;
 }
 
 
-int InsetNewline::docbook(Buffer const &, odocstream & os,
-                         OutputParams const &) const
+void InsetNewline::docbook(XMLStream &, OutputParams const &) const
 {
-       os << '\n';
-       return 0;
+       // New lines are handled by Paragraph::simpleDocBookOnePar.
+}
+
+
+docstring InsetNewline::xhtml(XMLStream & xs, OutputParams const &) const
+{
+       xs << xml::CR() << xml::CompTag("br") << xml::CR();
+       return docstring();
 }
 
 
@@ -119,22 +237,65 @@ void InsetNewline::draw(PainterInfo & pi, int x, int y) const
 
        pi.pain.lines(xp, yp, 3, ColorName());
 
-       // add label text behind the newline marker to divide from \newline
-       int w = 0;
-       int a = 0;
-       int d = 0;
-       theFontMetrics(font).rectText(insetLabel(), w, a, d);
-       
-       int const text_start = int(x + 2 * wid);
-                       
-       pi.pain.rectText(text_start, yp[0] + d, insetLabel(), font,
-               Color_none, Color_none);
+       if (params_.kind == InsetNewlineParams::LINEBREAK) {
+
+               yp[2] = int(y - 0.500 * asc * 0.75);
+
+               if (pi.ltr_pos) {
+                       xp[0] = int(x + 1.3 * wid);
+                       xp[1] = int(x + 2 * wid);
+                       xp[2] = int(x + 2 * wid);
+               } else {
+                       xp[0] = int(x - 0.3 * wid);
+                       xp[1] = int(x - wid);
+                       xp[2] = int(x - wid);
+               }
+               pi.pain.lines(xp, yp, 3, ColorName());
+
+               yp[0] = int(y - 0.875 * asc * 0.75);
+               yp[1] = int(y - 0.500 * asc * 0.75);
+               yp[2] = int(y - 0.125 * asc * 0.75);
+
+               if (pi.ltr_pos) {
+                       xp[0] = int(x + 2 * wid * 0.813);
+                       xp[1] = int(x + 2 * wid);
+                       xp[2] = int(x + 2 * wid * 0.813);
+               } else {
+                       xp[0] = int(x - wid * 0.625);
+                       xp[1] = int(x - wid);
+                       xp[2] = int(x - wid * 0.625);
+               }
+               pi.pain.lines(xp, yp, 3, ColorName());
+       }
+}
+
+
+string InsetNewline::contextMenuName() const
+{
+       return "context-newline";
+}
+
+
+void InsetNewline::string2params(string const & in, InsetNewlineParams & params)
+{
+       params = InsetNewlineParams();
+       if (in.empty())
+               return;
+       istringstream data(in);
+       Lexer lex;
+       lex.setStream(data);
+       lex.setContext("InsetNewline::string2params");
+       lex >> "newline";
+       params.read(lex);
 }
 
 
-bool InsetNewline::isSpace() const
+string InsetNewline::params2string(InsetNewlineParams const & params)
 {
-       return true;
+       ostringstream data;
+       data << "newline" << ' ';
+       params.write(data);
+       return data.str();
 }