]> git.lyx.org Git - lyx.git/blob - src/mathed/formulamacro.C
macro rework
[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_macrotable.h"
16 #include "math_macrotemplate.h"
17 #include "math_mathmlstream.h"
18
19 #include "BufferView.h"
20 #include "cursor.h"
21 #include "debug.h"
22 #include "gettext.h"
23 #include "LColor.h"
24 #include "lyxlex.h"
25 #include "outputparams.h"
26
27 #include "frontends/Painter.h"
28 #include "frontends/font_metrics.h"
29
30 #include "support/lstrings.h"
31 #include "support/std_sstream.h"
32
33 using lyx::support::bformat;
34
35 using std::string;
36 using std::auto_ptr;
37 using std::ostream;
38 using std::endl;
39
40
41
42 InsetFormulaMacro::InsetFormulaMacro()
43         : MathNestInset(2), name_("unknownA")
44 {}
45
46
47 InsetFormulaMacro::InsetFormulaMacro
48                 (string const & name, int nargs, string const & type)
49         : MathNestInset(2), name_(name)
50 {
51         MathMacroTable::create(MathAtom(new MathMacroTemplate(name, nargs, type)));
52 }
53
54
55 InsetFormulaMacro::InsetFormulaMacro(string const & s)
56         : MathNestInset(2), name_("unknownB")
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         tmpl()->write(wi);
74 }
75
76
77 int InsetFormulaMacro::latex(Buffer const &, ostream & os,
78                              OutputParams const & runparams) const
79 {
80         //lyxerr << "InsetFormulaMacro::latex" << endl;
81         WriteStream wi(os, runparams.moving_arg, true);
82         tmpl()->write(wi);
83         return 2;
84 }
85
86
87 int InsetFormulaMacro::plaintext(Buffer const &, ostream & os,
88                              OutputParams const &) const
89 {
90         WriteStream wi(os, false, true);
91         tmpl()->write(wi);
92         return 0;
93 }
94
95
96 int InsetFormulaMacro::linuxdoc(Buffer const & buf, ostream & os,
97                                 OutputParams const & runparams) const
98 {
99         return plaintext(buf, os, runparams);
100 }
101
102
103 int InsetFormulaMacro::docbook(Buffer const & buf, ostream & os,
104                                OutputParams const & runparams) const
105 {
106         return plaintext(buf, os, runparams);
107 }
108
109
110 void InsetFormulaMacro::read(Buffer const &, LyXLex & lex)
111 {
112         read(lex.getStream());
113 }
114
115
116 void InsetFormulaMacro::read(std::istream & is)
117 {
118         auto_ptr<MathMacroTemplate> p(new MathMacroTemplate(is));
119         name_ = p->name();
120         MathMacroTable::create(MathAtom(p.release()));
121 }
122
123
124 string InsetFormulaMacro::prefix() const
125 {
126         return bformat(_(" Macro: %1$s: "), name_);
127 }
128
129
130 void InsetFormulaMacro::metrics(MetricsInfo & mi, Dimension & dim) const
131 {
132         //lyxerr << "InsetFormulaMacro: " << this << " -- " << &tmpl() << endl;
133         tmpl()->metrics(mi, dim);
134         dim.asc += 5;
135         dim.des += 5;
136         dim.wid += 10 + font_metrics::width(prefix(), mi.base.font);
137         dim_ = dim;
138 }
139
140
141 void InsetFormulaMacro::draw(PainterInfo & p, int x, int y) const
142 {
143         // label
144         LyXFont font = p.base.font;
145         font.setColor(LColor::math);
146
147         PainterInfo pi(p.base.bv, p.pain);
148         pi.base.style = LM_ST_TEXT;
149         pi.base.font  = font;
150
151         int const a = y - dim_.asc + 1;
152         int const w = dim_.wid - 2;
153         int const h = dim_.height() - 2;
154
155         // LColor::mathbg used to be "AntiqueWhite" but is "linen" now, too
156         pi.pain.fillRectangle(x, a, w, h, LColor::mathmacrobg);
157         pi.pain.rectangle(x, a, w, h, LColor::mathframe);
158
159 #ifdef WITH_WARNINGS
160 #warning FIXME
161 #endif
162 #if 0
163         LCursor & cur = p.base.bv->cursor();
164         if (cur.isInside(this))
165                 cur.drawSelection(pi);
166 #endif
167
168         pi.pain.text(x + 2, y, prefix(), font);
169
170         // body
171         tmpl()->draw(pi, x + font_metrics::width(prefix(), p.base.font) + 5, y);
172
173         setPosCache(pi, x, y);
174 }
175
176
177 MathAtom & InsetFormulaMacro::tmpl() const
178 {
179         return MathMacroTable::provide(name_);
180 }