]> git.lyx.org Git - lyx.git/blob - src/mathed/formulamacro.C
doubleClick, trippleClick, new UpdateInsetList some other small stuff
[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 "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 int InsetFormulaMacro::Latex(string &file, signed char /*fragile*/) const
82 {
83     int ret = 1;
84     tmacro->WriteDef(file);
85     return ret;
86 }
87
88
89 int InsetFormulaMacro::Linuxdoc(string &/*file*/) const
90 {
91     return 0;
92 }
93
94
95 int InsetFormulaMacro::DocBook(string &/*file*/) const
96 {
97     return 0;
98 }
99
100
101 void InsetFormulaMacro::Read(LyXLex & lex)
102 {
103         istream & is = lex.getStream();
104         mathed_parser_file(is, lex.GetLineNo());   
105         mathed_parse(0, 0, reinterpret_cast<MathParInset **>(&tmacro));
106     
107         // Update line number
108         lex.setLineNo(mathed_parser_lineno());
109         
110         MathMacroTable::mathMTable.addTemplate(tmacro);
111         name = tmacro->GetName();
112         par = tmacro;
113 }
114
115
116 int InsetFormulaMacro::ascent(Painter & pain, LyXFont const & f) const
117 {
118     if (opened) {
119         tmacro->update();
120         return InsetFormula::ascent(pain, f);
121     }
122     return f.maxAscent()+3;
123 }
124
125
126 int InsetFormulaMacro::descent(Painter & pain, LyXFont const & f) const
127 {
128     if (opened) {
129         tmacro->update();
130         return InsetFormula::descent(pain, f);
131     }
132     return f.maxDescent()+1;
133 }
134
135
136 int InsetFormulaMacro::width(Painter & pain, LyXFont const & f) const
137 {
138     if (opened) {
139         tmacro->update();
140         return InsetFormula::width(pain, f);
141     }
142     string ilabel(_("Macro: "));
143     ilabel += name;
144     return 6 + f.stringWidth(ilabel);
145 }
146
147
148 void InsetFormulaMacro::draw(Painter & pain, LyXFont const & f,
149                              int baseline, float & x) const
150 {
151         LyXFont font(f);
152         tmacro->update();
153         if (opened) {
154                 tmacro->setEditMode(true);
155                 InsetFormula::draw(pain, font, baseline, x);
156                 tmacro->setEditMode(false);     
157         } else {
158                 font.setColor(LColor::math);
159                 
160                 int y = baseline - ascent(pain, font) + 1;
161                 int w = width(pain, font) - 2;
162                 int h = (ascent(pain, font) + descent(pain, font) - 2);
163
164         
165                 pain.fillRectangle(int(x), y, w, h, LColor::mathbg);
166                 pain.rectangle(int(x), y, w, h, LColor::mathframe);
167                 
168                 string s(_("Macro: "));
169                 s += name;
170                 pain.text(int(x + 2), baseline, s, font);
171                 x +=  width(pain, font) - 1;
172         }
173 }
174
175
176 void InsetFormulaMacro::Edit(BufferView * bv, int x, int y)
177 {
178     opened = true;
179     par = static_cast<MathParInset*>(tmacro->Clone());
180     InsetFormula::Edit(bv, x, y);
181 }
182
183                
184 void InsetFormulaMacro::InsetUnlock(BufferView * bv)
185 {
186     opened = false;
187     LyxArrayBase * tarray = tmacro->GetData();
188     MathedIter it(tarray);
189     it.Clear();
190     tmacro->SetData(par->GetData());
191     tmacro->setEditMode(false);
192     InsetFormula::InsetUnlock(bv);
193 }
194
195
196 bool InsetFormulaMacro::LocalDispatch(BufferView * bv,
197                                       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(bv);
204         }
205         
206         return true;
207     }
208     tmacro->setEditMode(true);
209     tmacro->Metrics();
210     bool result = InsetFormula::LocalDispatch(bv, action, arg);
211     tmacro->setEditMode(false);
212     
213     return result;
214 }