]> git.lyx.org Git - lyx.git/blob - src/mathed/formulamacro.C
cleanup indent
[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_macrotable.h"
28 #include "math_macrotemplate.h"
29 #include "lyx_main.h"
30 #include "BufferView.h"
31 #include "gettext.h"
32 #include "Painter.h"
33 #include "font.h"
34 #include "support/lyxlib.h"
35 #include "mathed/support.h"
36 #include "support/LOstream.h"
37 #include "debug.h"
38
39 using std::ostream;
40 using std::istream;
41
42 InsetFormulaMacro::InsetFormulaMacro()
43         : InsetFormula(true)
44 {
45         opened_ = false;
46 }
47
48
49 InsetFormulaMacro::InsetFormulaMacro(string nm, int na)
50         : InsetFormula(true), name_(nm)
51 {
52         tmacro_ = MathMacroTable::mathMTable.getTemplate(name_);
53         if (!tmacro_.get()) {
54                 tmacro_.reset(new MathMacroTemplate(name_, na));
55                 MathMacroTable::mathMTable.addTemplate(tmacro_);
56         }
57         opened_ = false;
58 }
59
60
61 InsetFormulaMacro::~InsetFormulaMacro()
62 {
63         // We do not want the InsetFormula destructor to
64         // delete this. That is taken care of elsewhere (Lgb)
65         par = 0;
66 }
67
68
69 Inset * InsetFormulaMacro::Clone(Buffer const &) const
70 {
71         // This should really use a proper copy constructor
72         return new InsetFormulaMacro(name_, 0);
73 }
74
75
76 void InsetFormulaMacro::Write(Buffer const *, ostream & os) const
77 {
78         os << "FormulaMacro ";
79         tmacro_->WriteDef(os, false);
80 }
81
82
83 int InsetFormulaMacro::Latex(Buffer const *, ostream & os, bool /*fragile*/, 
84                              bool /*free_spacing*/) const
85 {
86         tmacro_->WriteDef(os, true); // or false?
87         return 2;
88 }
89
90
91 int InsetFormulaMacro::Linuxdoc(Buffer const * buf, ostream & os) const
92 {
93         return Ascii(buf, os, 0);
94 }
95
96
97 int InsetFormulaMacro::DocBook(Buffer const * buf, ostream & os) const
98 {
99         return Ascii(buf, os, 0);
100 }
101
102
103 void InsetFormulaMacro::Read(Buffer const *, LyXLex & lex)
104 {
105         istream & is = lex.getStream();
106         mathed_parser_file(is, lex.GetLineNo());
107         MathedArray ar;
108         
109         mathed_parse(ar, 0, reinterpret_cast<MathParInset **>(&tmacro_));
110         // Since tmacro_ == 0 when mathed_parse is called we need to set
111         // its contents explicitly afterwards (Lgb)
112         tmacro_->setData(ar);
113         
114         // Update line number
115         lex.setLineNo(mathed_parser_lineno());
116         
117         MathMacroTable::mathMTable.addTemplate(tmacro_);
118         name_ = tmacro_->GetName();
119         par = tmacro_.get();
120
121         // reading of end_inset in the inset!!!
122         while (lex.IsOK()) {
123                 lex.nextToken();
124                 if (lex.GetString() == "\\end_inset")
125                         break;
126         }
127 }
128
129
130 int InsetFormulaMacro::ascent(BufferView * pain, LyXFont const & f) const
131 {
132         if (opened_) {
133                 return InsetFormula::ascent(pain, f);
134         }
135         return lyxfont::maxAscent(f) + 3;
136 }
137
138
139 int InsetFormulaMacro::descent(BufferView * pain, LyXFont const & f) const
140 {
141         if (opened_) {
142                 return InsetFormula::descent(pain, f);
143         }
144         return lyxfont::maxDescent(f) + 1;
145 }
146
147
148 int InsetFormulaMacro::width(BufferView * bv, LyXFont const & f) const
149 {
150         if (opened_) {
151                 return InsetFormula::width(bv, f);
152         }
153         string ilabel(_("Macro: "));
154         ilabel += name_;
155         return 6 + lyxfont::width(ilabel, f);
156 }
157
158
159 void InsetFormulaMacro::draw(BufferView * bv, LyXFont const & f,
160                              int baseline, float & x, bool cleared) const
161 {
162         Painter & pain = bv->painter();
163         LyXFont font(f);
164         if (opened_) {
165                 tmacro_->setEditMode(true);
166                 InsetFormula::draw(bv, font, baseline, x, cleared);
167                 tmacro_->setEditMode(false);    
168         } else {
169                 font.setColor(LColor::math);
170                 
171                 int const y = baseline - ascent(bv, font) + 1;
172                 int const w = width(bv, font) - 2;
173                 int const h = (ascent(bv, font) + descent(bv, font) - 2);
174         
175                 pain.fillRectangle(int(x), y, w, h, LColor::mathbg);
176                 pain.rectangle(int(x), y, w, h, LColor::mathframe);
177                 
178                 string s(_("Macro: "));
179                 s += name_;
180                 pain.text(int(x + 2), baseline, s, font);
181                 x +=  width(bv, font) - 1;
182         }
183 }
184
185
186 string const InsetFormulaMacro::EditMessage() const 
187 {
188         return _("Math macro editor mode");
189 }
190
191
192 void InsetFormulaMacro::Edit(BufferView * bv, int x, int y,unsigned int button)
193 {
194         opened_ = true;
195         par = static_cast<MathParInset*>(tmacro_->Clone());
196         InsetFormula::Edit(bv, x, y, button);
197 }
198
199                
200 void InsetFormulaMacro::InsetUnlock(BufferView * bv)
201 {
202         opened_ = false;
203         tmacro_->setData(par->GetData());
204         tmacro_->setEditMode(false);
205         InsetFormula::InsetUnlock(bv);
206 }
207
208
209 UpdatableInset::RESULT
210 InsetFormulaMacro::LocalDispatch(BufferView * bv,
211                                  kb_action action, string const & arg)
212 {
213         if (action == LFUN_MATH_MACROARG) {
214                 int const i = lyx::atoi(arg) - 1;
215                 if (i >= 0 && i < tmacro_->getNoArgs()) {
216                         mathcursor->insertInset(tmacro_->getMacroPar(i),
217                                                  LM_TC_INSET);
218                         InsetFormula::UpdateLocal(bv);
219                 }
220         
221                 return DISPATCHED;
222         }
223         tmacro_->setEditMode(true);
224         tmacro_->Metrics();
225         RESULT result = InsetFormula::LocalDispatch(bv, action, arg);
226         tmacro_->setEditMode(false);
227     
228         return result;
229 }