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