]> git.lyx.org Git - features.git/blob - src/mathed/formulamacro.C
fix lyxalgo.h, dra pagebreak with text on line, change the math_deco_search a bit
[features.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 #include <cstdlib>
18
19 #ifdef __GNUG__
20 #pragma implementation "formulamacro.h"
21 #endif
22
23 #include "formulamacro.h"
24 #include "commandtags.h"
25 #include "math_cursor.h"
26 #include "math_parser.h"
27 #include "math_macro.h"
28 #include "lyx_main.h"
29 #include "BufferView.h"
30 #include "gettext.h"
31 #include "Painter.h"
32
33
34 InsetFormulaMacro::InsetFormulaMacro()
35        : InsetFormula(true)
36 {
37     tmacro = 0;
38     opened = false;
39 }
40
41
42 InsetFormulaMacro::InsetFormulaMacro(string nm, int na, bool /*e*/)
43         : InsetFormula(true), name(nm)
44 {
45     tmacro = MathMacroTable::mathMTable.getTemplate(name.c_str());
46     if (!tmacro) {
47         tmacro = new MathMacroTemplate(name.c_str(), na);
48         MathMacroTable::mathMTable.addTemplate(tmacro);
49     }
50     opened = false;
51 }
52
53
54 InsetFormulaMacro::~InsetFormulaMacro()
55 {
56     par = 0;
57 }
58
59
60 Inset * InsetFormulaMacro::Clone() const
61 {
62    return new InsetFormulaMacro(name);
63 }
64
65
66 void InsetFormulaMacro::Write(ostream & os) const
67 {
68         os << "FormulaMacro ";
69         Latex(os, 0, false);
70 }
71
72
73 int InsetFormulaMacro::Latex(ostream & os, signed char /*fragile*/, 
74                              bool /*free_spacing*/) const
75 {
76     int ret = 1;
77     tmacro->WriteDef(os);
78     return ret;
79 }
80
81
82 int InsetFormulaMacro::Linuxdoc(ostream &) const
83 {
84     return 0;
85 }
86
87
88 int InsetFormulaMacro::DocBook(ostream &) const
89 {
90     return 0;
91 }
92
93
94 void InsetFormulaMacro::Read(LyXLex & lex)
95 {
96         istream & is = lex.getStream();
97         mathed_parser_file(is, lex.GetLineNo());   
98         mathed_parse(0, 0, reinterpret_cast<MathParInset **>(&tmacro));
99     
100         // Update line number
101         lex.setLineNo(mathed_parser_lineno());
102         
103         MathMacroTable::mathMTable.addTemplate(tmacro);
104         name = tmacro->GetName();
105         par = tmacro;
106 }
107
108
109 int InsetFormulaMacro::ascent(Painter & pain, LyXFont const & f) const
110 {
111     if (opened) {
112         tmacro->update();
113         return InsetFormula::ascent(pain, f);
114     }
115     return f.maxAscent()+3;
116 }
117
118
119 int InsetFormulaMacro::descent(Painter & pain, LyXFont const & f) const
120 {
121     if (opened) {
122         tmacro->update();
123         return InsetFormula::descent(pain, f);
124     }
125     return f.maxDescent()+1;
126 }
127
128
129 int InsetFormulaMacro::width(Painter & pain, LyXFont const & f) const
130 {
131     if (opened) {
132         tmacro->update();
133         return InsetFormula::width(pain, f);
134     }
135     string ilabel(_("Macro: "));
136     ilabel += name;
137     return 6 + f.stringWidth(ilabel);
138 }
139
140
141 void InsetFormulaMacro::draw(Painter & pain, LyXFont const & f,
142                              int baseline, float & x) const
143 {
144         LyXFont font(f);
145         tmacro->update();
146         if (opened) {
147                 tmacro->setEditMode(true);
148                 InsetFormula::draw(pain, font, baseline, x);
149                 tmacro->setEditMode(false);     
150         } else {
151                 font.setColor(LColor::math);
152                 
153                 int y = baseline - ascent(pain, font) + 1;
154                 int w = width(pain, font) - 2;
155                 int h = (ascent(pain, font) + descent(pain, font) - 2);
156
157         
158                 pain.fillRectangle(int(x), y, w, h, LColor::mathbg);
159                 pain.rectangle(int(x), y, w, h, LColor::mathframe);
160                 
161                 string s(_("Macro: "));
162                 s += name;
163                 pain.text(int(x + 2), baseline, s, font);
164                 x +=  width(pain, font) - 1;
165         }
166 }
167
168
169 void InsetFormulaMacro::Edit(BufferView * bv, int x, int y,unsigned int button)
170 {
171     opened = true;
172     par = static_cast<MathParInset*>(tmacro->Clone());
173     InsetFormula::Edit(bv, x, y, button);
174 }
175
176                
177 void InsetFormulaMacro::InsetUnlock(BufferView * bv)
178 {
179     opened = false;
180     LyxArrayBase * tarray = tmacro->GetData();
181     MathedIter it(tarray);
182     it.Clear();
183     tmacro->SetData(par->GetData());
184     tmacro->setEditMode(false);
185     InsetFormula::InsetUnlock(bv);
186 }
187
188
189 UpdatableInset::RESULT
190 InsetFormulaMacro::LocalDispatch(BufferView * bv,
191                                  int action, string const & arg)
192 {
193     if (action == LFUN_MATH_MACROARG) {
194         int i = atoi(arg.c_str()) - 1;
195         if (i >= 0 && i < tmacro->getNoArgs()) {
196             mathcursor->Insert(tmacro->getMacroPar(i), LM_TC_INSET);
197             InsetFormula::UpdateLocal(bv);
198         }
199         
200         return DISPATCHED;
201     }
202     tmacro->setEditMode(true);
203     tmacro->Metrics();
204     RESULT result = InsetFormula::LocalDispatch(bv, action, arg);
205     tmacro->setEditMode(false);
206     
207     return result;
208 }