]> git.lyx.org Git - lyx.git/blob - src/mathed/math_macro.C
mathed37.diff
[lyx.git] / src / mathed / math_macro.C
1 // -*- C++ -*-
2 /*
3  *  File:        math_macro.C
4  *  Purpose:     Implementation of macro class for mathed 
5  *  Author:      Alejandro Aguilar Sierra <asierra@servidor.unam.mx> 
6  *  Created:     November 1996
7  *  Description: WYSIWYG math macros
8  *
9  *  Dependencies: Mathed
10  *
11  *  Copyright: 1996, 1997 Alejandro Aguilar Sierra
12  *
13  *  Version: 0.2, Mathed & Lyx project.
14  *
15  *  This code is under the GNU General Public Licence version 2 or later.
16  */
17
18 #include <config.h>
19
20 #ifdef __GNUG__
21 #pragma implementation
22 #endif
23
24 #include "math_macro.h"
25 #include "array.h"
26 #include "math_iter.h"
27 #include "math_inset.h"
28 #include "math_accentinset.h"
29 #include "math_deliminset.h"
30 #include "math_fracinset.h"
31 #include "math_rowst.h"
32 #include "support/lstrings.h"
33 #include "debug.h"
34 #include "mathed/support.h"
35 #include "math_macrotemplate.h"
36 #include "macro_support.h"
37
38 using std::ostream;
39 using std::endl;
40
41 ostream & operator<<(ostream & o, MathedTextCodes mtc)
42 {
43         return o << int(mtc);
44 }
45
46
47 MathMacro::MathMacro(boost::shared_ptr<MathMacroTemplate> const & t)
48         : MathParInset(LM_ST_TEXT, "", LM_OT_MACRO),
49           tmplate_(t)
50 {
51         nargs_ = tmplate_->getNoArgs();
52         tcode_ = tmplate_->getTCode();
53         args_.resize(nargs_);
54         idx_ = 0;
55         SetName(tmplate_->GetName());
56 }
57
58
59 MathedInset * MathMacro::Clone()
60 {
61         return new MathMacro(*this);
62 }
63
64
65 void MathMacro::Metrics()
66 {
67         if (nargs_ > 0)
68                 tmplate_->update(this);
69         tmplate_->SetStyle(size());
70         tmplate_->Metrics();
71         width = tmplate_->Width();
72         ascent = tmplate_->Ascent();
73         descent = tmplate_->Descent();
74 }
75
76
77 void MathMacro::draw(Painter & pain, int x, int y)
78 {
79         xo(x);
80         yo(y);
81         Metrics();
82         tmplate_->SetStyle(size());
83         tmplate_->draw(pain, x, y);
84 }
85
86
87 bool MathMacro::setArgumentIdx(int i)
88 {
89         if (i >= 0 && i < nargs_) {
90                 idx_ = i;
91                 return true;
92         } else
93                 return false;
94 }
95
96
97 int MathMacro::getArgumentIdx() const 
98
99         return idx_;
100 }
101
102
103 int MathMacro::getMaxArgumentIdx() const 
104
105         return nargs_ - 1; 
106
107
108
109 MathedArray & MathMacro::GetData() 
110
111         return args_[idx_].array; 
112
113
114
115 MathedArray const & MathMacro::GetData() const
116
117         return args_[idx_].array; 
118
119
120
121 int MathMacro::GetColumns() const
122 {
123         return tmplate_->getMacroPar(idx_)->GetColumns();
124 }
125
126
127 void MathMacro::GetXY(int & x, int & y) const
128 {
129 #if 0
130         x = args_[idx_].x_;
131         y = args_[idx_].y_;
132 #else
133         const_cast<MathMacro*>(this)->Metrics();
134         tmplate_->GetMacroXY(idx_, x, y);
135 #endif
136 }
137
138
139 bool MathMacro::Permit(short f) const
140 {
141         return (nargs_ > 0) ?
142                 tmplate_->getMacroPar(idx_)->Permit(f) :
143                 MathParInset::Permit(f);
144 }
145
146
147 void MathMacro::SetFocus(int x, int y)
148 {
149         Metrics();
150         tmplate_->SetMacroFocus(idx_, x, y);
151 }
152
153
154 void MathMacro::setData(MathedArray const & a)
155 {
156         args_[idx_].array = a;
157 }
158
159
160 MathedRowSt * MathMacro::getRowSt() const
161 {
162         return args_[idx_].row;
163 }
164
165
166 MathedTextCodes MathMacro::getTCode() const
167 {
168         return tcode_;
169 }
170         
171
172 void MathMacro::Write(ostream & os, bool fragile)
173 {
174         os << '\\' << name;
175
176         if (nargs_ > 0) {
177                 os << '{';
178                 
179                 for (int i = 0; i < nargs_; ++i) {
180                         array = args_[i].array;
181                         MathParInset::Write(os, fragile);
182                         if (i < nargs_ - 1)  
183                                 os << "}{";
184                 }
185                 os << '}';
186         } else
187                 os << ' ';
188 }