]> git.lyx.org Git - lyx.git/blob - src/mathed/formulamacro.C
split LyXText::rowlist_ into individual Paragraph::rows_ chunks
[lyx.git] / src / mathed / formulamacro.C
1 /*
2  *  File:        formulamacro.C
3  *  Purpose:     Implementation of the formula macro LyX inset
4  *  Author:      André Pönitz, based on ideas of Alejandro Aguilar Sierra
5  *  Created:     March 2001
6  *  Description: Allows the edition of math macros inside Lyx.
7  *
8  *  Copyright: 2001  The LyX Project
9  *
10  *   You are free to use and modify this code under the terms of
11  *   the GNU General Public Licence version 2 or later.
12  */
13
14 #include <config.h>
15
16 #include "formulamacro.h"
17 #include "lfuns.h"
18 #include "math_cursor.h"
19 #include "math_parser.h"
20 #include "math_macro.h"
21 #include "math_macrotable.h"
22 #include "math_macrotemplate.h"
23 #include "metricsinfo.h"
24 #include "math_support.h"
25 #include "math_mathmlstream.h"
26 #include "BufferView.h"
27 #include "gettext.h"
28 #include "latexrunparams.h"
29 #include "frontends/Painter.h"
30 #include "frontends/font_metrics.h"
31 #include "support/lyxlib.h"
32 #include "support/lstrings.h"
33 #include "support/LOstream.h"
34 #include "debug.h"
35 #include "lyxlex.h"
36 #include "lyxtext.h"
37 #include "Lsstream.h"
38
39 using namespace lyx::support;
40
41 using std::ostream;
42 using std::auto_ptr;
43
44 extern MathCursor * mathcursor;
45
46
47 InsetFormulaMacro::InsetFormulaMacro()
48 {
49         // inset name is inherited from Inset
50         setInsetName("unknown");
51 }
52
53
54 InsetFormulaMacro::InsetFormulaMacro
55         (string const & name, int nargs, string const & type)
56 {
57         setInsetName(name);
58         MathMacroTable::create(MathAtom(new MathMacroTemplate(name, nargs, type)));
59 }
60
61
62 InsetFormulaMacro::InsetFormulaMacro(string const & s)
63 {
64         std::istringstream is(STRCONV(s));
65         read(is);
66 }
67
68
69 auto_ptr<InsetBase> InsetFormulaMacro::clone() const
70 {
71         return auto_ptr<InsetBase>(new InsetFormulaMacro(*this));
72 }
73
74
75 void InsetFormulaMacro::write(Buffer const *, ostream & os) const
76 {
77         os << "FormulaMacro ";
78         WriteStream wi(os, false, false);
79         par()->write(wi);
80 }
81
82
83 int InsetFormulaMacro::latex(Buffer const *, ostream & os,
84                              LatexRunParams const & runparams) const
85 {
86         WriteStream wi(os, runparams.moving_arg, true);
87         par()->write(wi);
88         return 2;
89 }
90
91
92 int InsetFormulaMacro::ascii(Buffer const *, ostream & os, int) const
93 {
94         WriteStream wi(os, false, true);
95         par()->write(wi);
96         return 0;
97 }
98
99
100 int InsetFormulaMacro::linuxdoc(Buffer const * buf, ostream & os) const
101 {
102         return ascii(buf, os, 0);
103 }
104
105
106 int InsetFormulaMacro::docbook(Buffer const * buf, ostream & os, bool) const
107 {
108         return ascii(buf, os, 0);
109 }
110
111
112 void InsetFormulaMacro::read(Buffer const *, LyXLex & lex)
113 {
114         read(lex.getStream());
115 }
116
117
118 void InsetFormulaMacro::read(std::istream & is)
119 {
120         MathMacroTemplate * p = new MathMacroTemplate(is);
121         setInsetName(p->name());
122         MathMacroTable::create(MathAtom(p));
123         //metrics();
124 }
125
126
127 string InsetFormulaMacro::prefix() const
128 {
129         return bformat(_(" Macro: %s: "), getInsetName());
130 }
131
132
133 void InsetFormulaMacro::metrics(MetricsInfo & mi, Dimension & dim) const
134 {
135         par()->metrics(mi, dim_);
136         dim_.asc += 5;
137         dim_.des += 5;
138         dim_.wid += 10 + font_metrics::width(prefix(), mi.base.font);
139         dim = dim_;
140 }
141
142
143 MathAtom const & InsetFormulaMacro::par() const
144 {
145         return MathMacroTable::provide(getInsetName());
146 }
147
148
149 MathAtom & InsetFormulaMacro::par()
150 {
151         return MathMacroTable::provide(getInsetName());
152 }
153
154
155 InsetOld::Code InsetFormulaMacro::lyxCode() const
156 {
157         return InsetOld::MATHMACRO_CODE;
158 }
159
160
161 void InsetFormulaMacro::draw(PainterInfo & p, int x, int y) const
162 {
163         // label
164         LyXFont font = p.base.font;
165         font.setColor(LColor::math);
166
167         PainterInfo pi(p.base.bv);
168         pi.base.style = LM_ST_TEXT;
169         pi.base.font  = font;
170
171         int const a = y - dim_.asc + 1;
172         int const w = dim_.wid - 2;
173         int const h = dim_.height() - 2;
174
175         // LColor::mathbg used to be "AntiqueWhite" but is "linen" now, too
176         pi.pain.fillRectangle(x, a, w, h, LColor::mathmacrobg);
177         pi.pain.rectangle(x, a, w, h, LColor::mathframe);
178
179         if (mathcursor &&
180                         const_cast<InsetFormulaBase const *>(mathcursor->formula()) == this)
181                 mathcursor->drawSelection(pi);
182
183         pi.pain.text(x + 2, y, prefix(), font);
184
185         // formula
186         par()->draw(pi, x + font_metrics::width(prefix(), p.base.font) + 5, y);
187         xo_ = x;
188         yo_ = y;
189 }