]> git.lyx.org Git - lyx.git/blob - src/mathed/formulamacro.C
0a3d90611a2d2b0dfe7acb50f898c06db29f4532
[lyx.git] / src / mathed / formulamacro.C
1 /*
2  *  File:        formula.h
3  *  Purpose:     Implementation of formula inset
4  *  Author:      Alejandro Aguilar Sierra <asierra@servidor.unam.mx> 
5  *  Created:     January 1996
6  *  Description: Allows the edition of math paragraphs inside Lyx. 
7  *
8  *  Copyright: 1996, 1997 Alejandro Aguilar Sierra
9  *
10  *  Version: 0.4, Lyx project.
11  *
12  *   You are free to use and modify this code under the terms of
13  *   the GNU General Public Licence version 2 or later.
14  */
15
16 #include <config.h>
17
18 #ifdef __GNUG__
19 #pragma implementation
20 #endif
21
22 #include "formulamacro.h"
23 #include "commandtags.h"
24 #include "math_cursor.h"
25 #include "math_parser.h"
26 #include "math_macro.h"
27 #include "math_macroarg.h"
28 #include "math_macrotable.h"
29 #include "math_macrotemplate.h"
30 #include "math_matrixinset.h"
31 #include "lyx_main.h"
32 #include "BufferView.h"
33 #include "gettext.h"
34 #include "Painter.h"
35 #include "font.h"
36 #include "support/lyxlib.h"
37 #include "mathed/support.h"
38 #include "support/LOstream.h"
39 #include "debug.h"
40 #include "lyxlex.h"
41
42 using std::ostream;
43
44 extern MathCursor * mathcursor;
45
46 InsetFormulaMacro::InsetFormulaMacro()
47         : InsetFormulaBase(new MathMacroTemplate("unknown", 0))
48 {}
49
50
51 InsetFormulaMacro::InsetFormulaMacro(string nm, int na)
52         : InsetFormulaBase(new MathMacroTemplate(nm, na))
53 {
54         MathMacroTable::insertTemplate(tmacro());
55 }
56
57
58 Inset * InsetFormulaMacro::clone(Buffer const &) const
59 {
60         return new InsetFormulaMacro(*this);
61 }
62
63
64 void InsetFormulaMacro::write(Buffer const *, ostream & os) const
65 {
66         os << "FormulaMacro ";
67         tmacro()->Write(os, false);
68 }
69
70
71 int InsetFormulaMacro::latex(Buffer const *, ostream & os, bool fragile, 
72                              bool /*free_spacing*/) const
73 {
74         tmacro()->Write(os, fragile);
75         return 2;
76 }
77
78 int InsetFormulaMacro::ascii(Buffer const *, ostream & os, int) const
79 {
80         tmacro()->Write(os, false);
81         return 0;
82 }
83
84
85 int InsetFormulaMacro::linuxdoc(Buffer const * buf, ostream & os) const
86 {
87         return ascii(buf, os, 0);
88 }
89
90
91 int InsetFormulaMacro::docBook(Buffer const * buf, ostream & os) const
92 {
93         return ascii(buf, os, 0);
94 }
95
96
97 void InsetFormulaMacro::read(Buffer const *, LyXLex & lex)
98 {
99         // Awful hack...
100         par_ = mathed_parse(lex);
101         MathMacroTable::insertTemplate(tmacro());
102         par_->Metrics(LM_ST_TEXT);
103 }
104
105
106 string InsetFormulaMacro::prefix() const
107 {
108         return string(" ") + _("Macro: ") + tmacro()->name() + ": ";
109 }
110
111
112 int InsetFormulaMacro::ascent(BufferView *, LyXFont const &) const
113 {
114         return tmacro()->ascent() + 5;
115 }
116
117
118 int InsetFormulaMacro::descent(BufferView *, LyXFont const &) const
119 {
120         return tmacro()->descent() + 5;
121 }
122
123
124 int InsetFormulaMacro::width(BufferView *, LyXFont const & f) const
125 {
126         tmacro()->Metrics(LM_ST_TEXT);
127         return 10 + lyxfont::width(prefix(), f) + tmacro()->width();
128 }
129
130
131 void InsetFormulaMacro::draw(BufferView * bv, LyXFont const & f,
132                              int baseline, float & x, bool /*cleared*/) const
133 {
134         Painter & pain = bv->painter();
135         LyXFont font(f);
136
137         // label
138         font.setColor(LColor::math);
139         
140         int const y = baseline - ascent(bv, font) + 1;
141         int const w = width(bv, font) - 2;
142         int const h = ascent(bv, font) + descent(bv, font) - 2;
143
144         pain.fillRectangle(int(x), y , w, h, LColor::mathbg);
145         pain.rectangle(int(x), y, w, h, LColor::mathframe);
146
147         if (mathcursor && mathcursor->formula() == this && mathcursor->Selection()) {
148                 int xp[10];
149                 int yp[10];
150                 int n;
151                 mathcursor->SelGetArea(xp, yp, n);
152                 pain.fillPolygon(xp, yp, n, LColor::selection);
153         }
154
155         pain.text(int(x + 2), baseline, prefix(), font);
156         x += width(bv, font);
157
158         // formula
159         float t = tmacro()->width() + 5;
160         x -= t;
161         tmacro()->draw(pain, int(x), baseline);
162         x += t;
163 }
164
165
166 UpdatableInset::RESULT
167 InsetFormulaMacro::localDispatch(BufferView * bv,
168                                  kb_action action, string const & arg)
169 {
170         RESULT result = DISPATCHED;
171         switch (action) {
172                 case LFUN_MATH_MACROARG: {
173                         int const i = lyx::atoi(arg);
174                         lyxerr << "inserting macro arg " << i << "\n";
175                         if (i > 0 && i <= tmacro()->nargs()) {
176                                 mathcursor->insert(new MathMacroArgument(i));
177                                 updateLocal(bv);
178                         } else {
179                                 lyxerr << "not in range 0.." << tmacro()->nargs() << "\n";
180                         }
181                         break;
182                 }
183                 
184                 default:
185                         result = InsetFormulaBase::localDispatch(bv, action, arg);
186         }
187         return result;
188 }
189
190
191 MathMacroTemplate * InsetFormulaMacro::tmacro() const
192 {
193         return static_cast<MathMacroTemplate *>(par_);
194 }
195
196
197 Inset::Code InsetFormulaMacro::lyxCode() const
198 {
199         return Inset::MATHMACRO_CODE;
200 }
201