]> git.lyx.org Git - lyx.git/blob - src/mathed/formulamacro.C
6469b547bdfb6e398adebb4c5002428015676818
[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 "lyx_main.h"
31 #include "BufferView.h"
32 #include "gettext.h"
33 #include "Painter.h"
34 #include "font.h"
35 #include "support/lyxlib.h"
36 #include "mathed/support.h"
37 #include "support/LOstream.h"
38 #include "debug.h"
39
40 using std::ostream;
41 using std::istream;
42
43 extern MathedCursor * mathcursor;
44
45 InsetFormulaMacro::InsetFormulaMacro()
46         : InsetFormula(true)
47 {
48         par = &MathMacroTable::provideTemplate("unknown", 0);
49 }
50
51
52 InsetFormulaMacro::InsetFormulaMacro(string nm, int na)
53   : InsetFormula(true)
54 {
55         par = &MathMacroTable::provideTemplate(nm, na);
56 }
57
58
59 Inset * InsetFormulaMacro::Clone(Buffer const &) const
60 {
61         return new InsetFormulaMacro(*this);
62 }
63
64
65 void InsetFormulaMacro::Write(Buffer const *, ostream & os) const
66 {
67         os << "FormulaMacro ";
68         tmacro()->WriteDef(os, false);
69 }
70
71
72 int InsetFormulaMacro::Latex(Buffer const *, ostream & os, bool /*fragile*/, 
73                              bool /*free_spacing*/) const
74 {
75         tmacro()->WriteDef(os, true); // or false?
76         return 2;
77 }
78
79
80 int InsetFormulaMacro::Linuxdoc(Buffer const * buf, ostream & os) const
81 {
82         return Ascii(buf, os, 0);
83 }
84
85
86 int InsetFormulaMacro::DocBook(Buffer const * buf, ostream & os) const
87 {
88         return Ascii(buf, os, 0);
89 }
90
91
92 void InsetFormulaMacro::Read(Buffer const *, LyXLex & lex)
93 {
94         istream & is = lex.getStream();
95         mathed_parser_file(is, lex.GetLineNo());
96
97         MathParInset * tmp = new MathParInset;
98         MathedArray ar;
99         mathed_parse(ar, tmp, 0);
100         par = &MathMacroTable::provideTemplate(tmp->GetName(), tmp->xo());
101         par->setData(ar);
102         //cerr << "## InsetFormulaMacro::Read name: " << tmp->GetName() << endl;
103         //cerr << "## InsetFormulaMacro::Read nargs: " << tmp->xo() << endl;
104         //cerr << "## InsetFormulaMacro::Read 1: " << ar << endl;
105         
106         // Update line number
107         lex.setLineNo(mathed_parser_lineno());
108         
109         // reading of end_inset in the inset!!!
110         while (lex.IsOK()) {
111                 lex.nextToken();
112                 if (lex.GetString() == "\\end_inset")
113                         break;
114         }
115 }
116
117 int InsetFormulaMacro::ascent(BufferView * pain, LyXFont const & f) const
118 {
119         return InsetFormula::ascent(pain, f);
120 }
121
122
123 int InsetFormulaMacro::descent(BufferView * pain, LyXFont const & f) const
124 {
125         return InsetFormula::descent(pain, f);
126 }
127
128
129 string InsetFormulaMacro::prefix() const
130 {
131         return string(" ") + _("Macro: ") + par->GetName() + ": ";
132 }
133
134 int InsetFormulaMacro::width(BufferView * bv, LyXFont const & f) const
135 {
136         return 10 + lyxfont::width(prefix(), f) + InsetFormula::width(bv, f);
137 }
138
139
140 void InsetFormulaMacro::draw(BufferView * bv, LyXFont const & f,
141                              int baseline, float & x, bool cleared) const
142 {
143         Painter & pain = bv->painter();
144         LyXFont font(f);
145
146         // label
147         font.setColor(LColor::math);
148         
149         int const y = baseline - ascent(bv, font) + 1;
150         int const w = width(bv, font) - 2;
151         int const h = (ascent(bv, font) + descent(bv, font) - 2);
152
153         pain.fillRectangle(int(x), y, w, h, LColor::mathbg);
154         pain.rectangle(int(x), y, w, h, LColor::mathframe);
155         
156         pain.text(int(x + 2), baseline, prefix(), font);
157         x += width(bv, font);
158
159         // formula
160         float t = InsetFormula::width(bv, f) + 5;
161         x -= t;
162         InsetFormula::draw(bv, font, baseline, x, cleared);
163         x += t;
164 }
165
166
167 string const InsetFormulaMacro::EditMessage() const 
168 {
169         return _("Math macro editor mode");
170 }
171
172
173 UpdatableInset::RESULT
174 InsetFormulaMacro::LocalDispatch(BufferView * bv,
175                                  kb_action action, string const & arg)
176 {
177         if (action == LFUN_MATH_MACROARG) {
178                 int const i = lyx::atoi(arg);
179                 if (i > 0 && i <= tmacro()->nargs()) {
180                         mathcursor->insertInset(new MathMacroArgument(i), LM_TC_INSET);
181                         InsetFormula::UpdateLocal(bv);
182                 }
183         
184                 return DISPATCHED;
185         }
186         par->Metrics();
187         return InsetFormula::LocalDispatch(bv, action, arg);
188 }
189
190 MathMacroTemplate * InsetFormulaMacro::tmacro() const
191 {
192         return static_cast<MathMacroTemplate *>(par);
193 }