]> git.lyx.org Git - features.git/blob - src/mathed/formulamacro.C
change to use ostreams instead of string when writing files. fiddling with insettext...
[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: (c) 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);
70 }
71
72
73 int InsetFormulaMacro::Latex(ostream & os, signed char /*fragile*/) const
74 {
75     int ret = 1;
76     tmacro->WriteDef(os);
77     return ret;
78 }
79
80
81 #ifndef USE_OSTREAM_ONLY
82 int InsetFormulaMacro::Latex(string &file, signed char /*fragile*/) const
83 {
84     int ret = 1;
85     tmacro->WriteDef(file);
86     return ret;
87 }
88
89
90 int InsetFormulaMacro::Linuxdoc(string &/*file*/) const
91 {
92     return 0;
93 }
94
95
96 int InsetFormulaMacro::DocBook(string &/*file*/) const
97 {
98     return 0;
99 }
100
101 #else
102
103 int InsetFormulaMacro::Linuxdoc(ostream &) const
104 {
105     return 0;
106 }
107
108
109 int InsetFormulaMacro::DocBook(ostream &) const
110 {
111     return 0;
112 }
113 #endif
114
115
116 void InsetFormulaMacro::Read(LyXLex & lex)
117 {
118         istream & is = lex.getStream();
119         mathed_parser_file(is, lex.GetLineNo());   
120         mathed_parse(0, 0, reinterpret_cast<MathParInset **>(&tmacro));
121     
122         // Update line number
123         lex.setLineNo(mathed_parser_lineno());
124         
125         MathMacroTable::mathMTable.addTemplate(tmacro);
126         name = tmacro->GetName();
127         par = tmacro;
128 }
129
130
131 int InsetFormulaMacro::ascent(Painter & pain, LyXFont const & f) const
132 {
133     if (opened) {
134         tmacro->update();
135         return InsetFormula::ascent(pain, f);
136     }
137     return f.maxAscent()+3;
138 }
139
140
141 int InsetFormulaMacro::descent(Painter & pain, LyXFont const & f) const
142 {
143     if (opened) {
144         tmacro->update();
145         return InsetFormula::descent(pain, f);
146     }
147     return f.maxDescent()+1;
148 }
149
150
151 int InsetFormulaMacro::width(Painter & pain, LyXFont const & f) const
152 {
153     if (opened) {
154         tmacro->update();
155         return InsetFormula::width(pain, f);
156     }
157     string ilabel(_("Macro: "));
158     ilabel += name;
159     return 6 + f.stringWidth(ilabel);
160 }
161
162
163 void InsetFormulaMacro::draw(Painter & pain, LyXFont const & f,
164                              int baseline, float & x) const
165 {
166         LyXFont font(f);
167         tmacro->update();
168         if (opened) {
169                 tmacro->setEditMode(true);
170                 InsetFormula::draw(pain, font, baseline, x);
171                 tmacro->setEditMode(false);     
172         } else {
173                 font.setColor(LColor::math);
174                 
175                 int y = baseline - ascent(pain, font) + 1;
176                 int w = width(pain, font) - 2;
177                 int h = (ascent(pain, font) + descent(pain, font) - 2);
178
179         
180                 pain.fillRectangle(int(x), y, w, h, LColor::mathbg);
181                 pain.rectangle(int(x), y, w, h, LColor::mathframe);
182                 
183                 string s(_("Macro: "));
184                 s += name;
185                 pain.text(int(x + 2), baseline, s, font);
186                 x +=  width(pain, font) - 1;
187         }
188 }
189
190
191 void InsetFormulaMacro::Edit(BufferView * bv, int x, int y,unsigned int button)
192 {
193     opened = true;
194     par = static_cast<MathParInset*>(tmacro->Clone());
195     InsetFormula::Edit(bv, x, y, button);
196 }
197
198                
199 void InsetFormulaMacro::InsetUnlock(BufferView * bv)
200 {
201     opened = false;
202     LyxArrayBase * tarray = tmacro->GetData();
203     MathedIter it(tarray);
204     it.Clear();
205     tmacro->SetData(par->GetData());
206     tmacro->setEditMode(false);
207     InsetFormula::InsetUnlock(bv);
208 }
209
210
211 UpdatableInset::RESULT
212 InsetFormulaMacro::LocalDispatch(BufferView * bv,
213                                  int action, string const & arg)
214 {
215     if (action == LFUN_MATH_MACROARG) {
216         int i = atoi(arg.c_str()) - 1;
217         if (i >= 0 && i < tmacro->getNoArgs()) {
218             mathcursor->Insert(tmacro->getMacroPar(i), LM_TC_INSET);
219             InsetFormula::UpdateLocal(bv);
220         }
221         
222         return DISPATCHED;
223     }
224     tmacro->setEditMode(true);
225     tmacro->Metrics();
226     RESULT result = InsetFormula::LocalDispatch(bv, action, arg);
227     tmacro->setEditMode(false);
228     
229     return result;
230 }