]> git.lyx.org Git - lyx.git/blob - src/mathed/formulamacro.C
Rename ascii to plaintext and LatexRunParams to OutputParams.
[lyx.git] / src / mathed / formulamacro.C
1 /**
2  * \file formulamacro.C
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 "formulamacro.h"
15 #include "math_cursor.h"
16 #include "math_macrotable.h"
17 #include "math_macrotemplate.h"
18 #include "math_mathmlstream.h"
19
20 #include "gettext.h"
21 #include "LColor.h"
22 #include "lyxlex.h"
23 #include "outputparams.h"
24
25 #include "frontends/Painter.h"
26 #include "frontends/font_metrics.h"
27
28 #include "support/lstrings.h"
29 #include "support/std_sstream.h"
30
31 using lyx::support::bformat;
32
33 using std::string;
34 using std::auto_ptr;
35 using std::ostream;
36
37
38 extern MathCursor * mathcursor;
39
40
41 InsetFormulaMacro::InsetFormulaMacro()
42 {
43         // inset name is inherited from Inset
44         setInsetName("unknown");
45 }
46
47
48 InsetFormulaMacro::InsetFormulaMacro
49         (string const & name, int nargs, string const & type)
50 {
51         setInsetName(name);
52         MathMacroTable::create(MathAtom(new MathMacroTemplate(name, nargs, type)));
53 }
54
55
56 InsetFormulaMacro::InsetFormulaMacro(string const & s)
57 {
58         std::istringstream is(s);
59         read(is);
60 }
61
62
63 auto_ptr<InsetBase> InsetFormulaMacro::clone() const
64 {
65         return auto_ptr<InsetBase>(new InsetFormulaMacro(*this));
66 }
67
68
69 void InsetFormulaMacro::write(Buffer const &, ostream & os) const
70 {
71         os << "FormulaMacro ";
72         WriteStream wi(os, false, false);
73         par()->write(wi);
74 }
75
76
77 int InsetFormulaMacro::latex(Buffer const &, ostream & os,
78                              OutputParams const & runparams) const
79 {
80         WriteStream wi(os, runparams.moving_arg, true);
81         par()->write(wi);
82         return 2;
83 }
84
85
86 int InsetFormulaMacro::plaintext(Buffer const &, ostream & os,
87                              OutputParams const &) const
88 {
89         WriteStream wi(os, false, true);
90         par()->write(wi);
91         return 0;
92 }
93
94
95 int InsetFormulaMacro::linuxdoc(Buffer const & buf, ostream & os,
96                                 OutputParams const & runparams) const
97 {
98         return plaintext(buf, os, runparams);
99 }
100
101
102 int InsetFormulaMacro::docbook(Buffer const & buf, ostream & os,
103                                OutputParams const & runparams) const
104 {
105         return plaintext(buf, os, runparams);
106 }
107
108
109 void InsetFormulaMacro::read(Buffer const &, LyXLex & lex)
110 {
111         read(lex.getStream());
112 }
113
114
115 void InsetFormulaMacro::read(std::istream & is)
116 {
117         auto_ptr<MathMacroTemplate> p(new MathMacroTemplate(is));
118         setInsetName(p->name());
119         MathMacroTable::create(MathAtom(p.release()));
120         //metrics();
121 }
122
123
124 string InsetFormulaMacro::prefix() const
125 {
126         return bformat(_(" Macro: %1$s: "), getInsetName());
127 }
128
129
130 void InsetFormulaMacro::metrics(MetricsInfo & mi, Dimension & dim) const
131 {
132         par()->metrics(mi, dim_);
133         dim_.asc += 5;
134         dim_.des += 5;
135         dim_.wid += 10 + font_metrics::width(prefix(), mi.base.font);
136         dim = dim_;
137 }
138
139
140 MathAtom const & InsetFormulaMacro::par() const
141 {
142         return MathMacroTable::provide(getInsetName());
143 }
144
145
146 MathAtom & InsetFormulaMacro::par()
147 {
148         return MathMacroTable::provide(getInsetName());
149 }
150
151
152 InsetOld::Code InsetFormulaMacro::lyxCode() const
153 {
154         return InsetOld::MATHMACRO_CODE;
155 }
156
157
158 void InsetFormulaMacro::draw(PainterInfo & p, int x, int y) const
159 {
160         // label
161         LyXFont font = p.base.font;
162         font.setColor(LColor::math);
163
164         PainterInfo pi(p.base.bv);
165         pi.base.style = LM_ST_TEXT;
166         pi.base.font  = font;
167
168         int const a = y - dim_.asc + 1;
169         int const w = dim_.wid - 2;
170         int const h = dim_.height() - 2;
171
172         // LColor::mathbg used to be "AntiqueWhite" but is "linen" now, too
173         pi.pain.fillRectangle(x, a, w, h, LColor::mathmacrobg);
174         pi.pain.rectangle(x, a, w, h, LColor::mathframe);
175
176         if (mathcursor &&
177                         const_cast<InsetFormulaBase const *>(mathcursor->formula()) == this)
178                 mathcursor->drawSelection(pi);
179
180         pi.pain.text(x + 2, y, prefix(), font);
181
182         // formula
183         par()->draw(pi, x + font_metrics::width(prefix(), p.base.font) + 5, y);
184         xo_ = x;
185         yo_ = y;
186 }