]> git.lyx.org Git - lyx.git/blob - src/mathed/InsetFormulaMacro.cpp
visual mode for bidi cursor movement
[lyx.git] / src / mathed / InsetFormulaMacro.cpp
1 /**
2  * \file InsetFormulaMacro.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Alejandro Aguilar Sierra
7  * \author André Pönitz
8  *
9  * Full author contact details are available in file CREDITS.
10  */
11
12 #include <config.h>
13
14 #include "InsetFormulaMacro.h"
15 #include "MacroTable.h"
16 #include "MathMacroTemplate.h"
17
18 #include "BufferView.h"
19 #include "Cursor.h"
20 #include "support/debug.h"
21 #include "support/gettext.h"
22 #include "Lexer.h"
23 #include "OutputParams.h"
24
25 #include "frontends/FontMetrics.h"
26 #include "frontends/Painter.h"
27
28 #include "support/lstrings.h"
29
30 #include <sstream>
31
32 using namespace std;
33 using namespace lyx::support;
34
35 namespace lyx {
36
37
38 InsetFormulaMacro::InsetFormulaMacro()
39         : InsetMathNest(2), name_("unknownA")
40 {}
41
42
43 InsetFormulaMacro::InsetFormulaMacro
44                 (docstring const & name, int nargs, docstring const & type)
45         : InsetMathNest(2), name_(name)
46 {
47         MathMacroTable::create(MathAtom(new MathMacroTemplate(name, nargs, type)));
48 }
49
50
51 InsetFormulaMacro::InsetFormulaMacro(string const & s)
52         : InsetMathNest(2), name_("unknownB")
53 {
54         istringstream is(s);
55         read(is);
56 }
57
58
59 Inset * InsetFormulaMacro::clone() const
60 {
61         return new InsetFormulaMacro(*this);
62 }
63
64
65 void InsetFormulaMacro::write(Buffer const &, ostream & os) const
66 {
67         os << "FormulaMacro\n";
68         WriteStream wi(os, false, false);
69         tmpl()->write(wi);
70 }
71
72
73 int InsetFormulaMacro::latex(Buffer const &, odocstream & os,
74                              OutputParams const & runparams) const
75 {
76         //lyxerr << "InsetFormulaMacro::latex" << endl;
77         WriteStream wi(os, runparams.moving_arg, true);
78         tmpl()->write(wi);
79         return 2;
80 }
81
82
83 int InsetFormulaMacro::plaintext(Buffer const &, odocstream & os,
84                                  OutputParams const &) const
85 {
86         odocstringstream oss;
87         WriteStream wi(oss, false, true);
88         tmpl()->write(wi);
89
90         docstring const str = oss.str();
91         os << str;
92         return str.size();
93 }
94
95
96 int InsetFormulaMacro::docbook(Buffer const & buf, ostream & os,
97                                OutputParams const & runparams) const
98 {
99         return plaintext(buf, os, runparams);
100 }
101
102
103 void InsetFormulaMacro::read(Buffer const &, Lexer & lex)
104 {
105         read(lex.getStream());
106 }
107
108
109 void InsetFormulaMacro::read(istream & is)
110 {
111         auto_ptr<MathMacroTemplate> p(new MathMacroTemplate(is));
112         name_ = p->name();
113         MathMacroTable::create(MathAtom(p.release()));
114 }
115
116
117 string InsetFormulaMacro::prefix() const
118 {
119         return to_utf8(bformat(_(" Macro: %1$s: "), lyx::from_utf8(name_)));
120 }
121
122
123 void InsetFormulaMacro::metrics(MetricsInfo & mi, Dimension & dim) const
124 {
125         //lyxerr << "InsetFormulaMacro: " << this << " -- " << &tmpl() << endl;
126         tmpl()->metrics(mi, dim);
127         dim.asc += 5;
128         dim.des += 5;
129         dim.wid += 10 + theFontMetrics(mi.base.font).width(prefix());
130         dim_ = dim;
131 }
132
133
134 void InsetFormulaMacro::draw(PainterInfo & p, int x, int y) const
135 {
136         // label
137         Font font = p.base.font;
138         font.setColor(Color_math);
139
140         PainterInfo pi(p.base.bv, p.pain);
141         pi.base.style = LM_ST_TEXT;
142         pi.base.font  = font;
143
144         int const a = y - dim_.asc + 1;
145         int const w = dim_.wid - 2;
146         int const h = dim_.height() - 2;
147
148         // Color_mathbg used to be "AntiqueWhite" but is "linen" now, too
149         pi.pain.fillRectangle(x, a, w, h, Color_mathmacrobg);
150         pi.pain.rectangle(x, a, w, h, Color_mathframe);
151
152         // FIXME
153 #if 0
154         Cursor & cur = p.base.bv->cursor();
155         if (cur.isInside(this))
156                 cur.drawSelection(pi);
157 #endif
158
159         pi.pain.text(x + 2, y, prefix(), font);
160
161         // body
162         tmpl()->draw(pi,
163                 x + theFontMetrics(p.base.font).width(prefix()) + 5,
164                 y);
165
166         setPosCache(pi, x, y);
167 }
168
169
170 MathAtom & InsetFormulaMacro::tmpl() const
171 {
172         return MathMacroTable::provide(name_);
173 }
174
175
176 } // namespace lyx