3 * Purpose: Implementation of insets for mathed
4 * Author: Alejandro Aguilar Sierra <asierra@servidor.unam.mx>
5 * Created: January 1996
8 * Dependencies: Xlib, XForms
10 * Copyright: 1996, Alejandro Aguilar Sierra
14 * You are free to use and modify this code under the terms of
15 * the GNU General Public Licence version 2 or later.
21 #pragma implementation
24 #include "math_iter.h"
26 #include "math_inset.h"
27 #include "symbol_def.h"
28 #include "support/lstrings.h"
30 #include "mathed/support.h"
34 const int SizeInset = sizeof(char*) + 2;
36 //extern int mathed_char_width(short type, int style, byte c);
37 //extern int mathed_string_width(short type, int style, string const & s);
38 //extern int mathed_char_height(short, int, byte, int &, int &);
41 MathedIter::MathedIter()
42 : flags(0), fcode_(0), pos(0), row(0), col(0), ncols(0), array(0)
46 MathedArray * MathedIter::GetData() const
52 short MathedIter::fcode() const
58 void MathedIter::fcode(short c) const
63 byte MathedIter::at(int p) const
68 byte & MathedIter::at(int p)
73 int MathedIter::Empty() const
75 return array->last() <= 1;
79 int MathedIter::OK() const
81 return array && (pos < array->last());
85 void MathedIter::Reset()
87 if (array->last() > 0 && MathIsFont(at(0))) {
99 byte MathedIter::GetChar() const
109 string const MathedIter::GetString() const
117 for (; at(pos) >= ' ' && pos < array->last(); ++pos)
124 MathedInset * MathedIter::GetInset() const
128 array->raw_pointer_copy(&p, pos + 1);
131 lyxerr << "Math Error: This is not an inset["
132 << at(pos) << "]" << endl;
138 // An active math inset MUST be derived from MathParInset because it
139 // must have at least one paragraph to edit
140 MathParInset * MathedIter::GetActiveInset() const
143 return reinterpret_cast<MathParInset*>(GetInset());
145 lyxerr << "Math Error: This is not an active inset" << endl;
150 bool MathedIter::Next()
166 pos += sizeof(char*) + 2;
177 bool MathedIter::goNextCode(MathedTextCodes code)
188 void MathedIter::goPosAbs(int p)
191 while (pos < p && Next())
196 void MathedIter::insert(byte c, MathedTextCodes t)
201 if (t == LM_TC_TAB && col >= ncols - 1)
204 // Never more than one space // array->bf[pos-1] gives error from purify:
205 // Reading 1 byte from 0x47b857 in the heap.
207 // Address 0x47b857 is 1 byte before start of malloc'd block at
208 // 0x47b858 of 16 bytes.
210 if (c == ' ' && (at(pos) == ' ' || at(pos - 1) == ' '))
213 if (IsFont() && at(pos) == t) {
217 if (t != fcode() && pos > 0 && MathIsFont(at(pos - 1))) {
220 for (; k >= 0 && at(k) >= ' '; --k)
222 fcode( (k >= 0 && MathIsFont(at(k))) ? at(k) : -1 );
226 short const f = (at(pos) < ' ') ? 0 : fcode();
227 int shift = (t == fcode()) ? 1 : ((f) ? 3 : 2);
229 if (t == LM_TC_TAB || t == LM_TC_CR) {
239 if (pos < array->last())
240 array->move(pos, shift);
242 array->need_size(array->last() + shift);
243 array->last(array->last() + shift);
244 at(array->last()) = '\0';
249 at(pos + shift - 1) = fcode();
262 // Prepare to insert a non-char object
263 void MathedIter::split(int shift)
265 if (pos < array->last()) {
268 if (at(pos) >= ' ') {
269 if (pos> 0 && MathIsFont(at(pos - 1)))
277 array->move(pos, shift);
280 at(pos + shift - 1) = fcode();
284 array->need_size(array->last() + shift);
285 array->last(array->last() + shift);
288 at(array->last()) = '\0';
292 // I assume that both pos and pos2 are legal positions
293 void MathedIter::join(int pos2)
295 if (!OK() || pos2 <= pos)
299 if (pos > 0 && at(pos) >= ' ' && MathIsFont(at(pos - 1)))
302 if (MathIsFont(at(pos2 - 1)))
305 if (at(pos2) >= ' ') {
306 for (int p = pos2; p > 0; --p) {
307 if (MathIsFont(at(p))) {
315 array->move(pos2, pos - pos2);
319 void MathedIter::insertInset(MathedInset * p, int type)
322 if (!MathIsInset(type))
325 array->insertInset(pos, p, type);
329 int const shift = SizeInset;
331 if (!MathIsInset(type))
337 array->raw_pointer_insert(p, pos + 1);
340 at(array->last()) = '\0';
346 bool MathedIter::Delete()
352 byte const c = GetChar();
355 if (MathIsFont(at(pos - 1)) && at(pos + 1) < ' ') {
359 for (; i > 0 && !MathIsFont(at(i)); --i)
361 if (i > 0 && MathIsFont(at(i)))
368 if (MathIsInset(at(pos)))
369 shift = sizeof(char*) + 2;
370 else if (c == LM_TC_TAB || c == LM_TC_CR) {
372 // lyxerr <<"Es un tab.";
374 lyxerr << "Math Warning: expected inset." << endl;
380 array->move(pos + shift, -shift);
381 if (pos >= array->last())
382 pos = (array->last() > 0) ? array->last() : 0;
388 // Check consistency of tabs and crs
389 void MathedIter::checkTabs()
393 // MathedIter:Reset();
395 if ((IsTab() && col >= ncols - 1) || (IsCR() && !(MthIF_CR & flags))) {
400 if (IsCR() && col < ncols - 2)
401 insert(' ', LM_TC_TAB);
407 insert(' ', LM_TC_TAB);
413 // Try to adjust tabs in the expected place, as used in eqnarrays
415 // - If there are a relation operator, put tabs around it
416 // - If tehre are not a relation operator, put everything in the
418 void MathedIter::adjustTabs()
422 bool MathedIter::IsInset() const
424 return MathIsInset(at(pos));
428 bool MathedIter::IsActive() const
430 return MathIsActive(at(pos));
434 bool MathedIter::IsFont() const
436 return MathIsFont(at(pos));
440 bool MathedIter::IsScript() const
442 return MathIsScript(at(pos));
446 bool MathedIter::IsTab() const
448 return (at(pos) == LM_TC_TAB);
452 bool MathedIter::IsCR() const
454 return (at(pos) == LM_TC_CR);
458 MathedIter::MathedIter(MathedArray * d)
464 fcode( (array && IsFont()) ? at(0) : 0 );
468 void MathedIter::ipush()
470 stck.fcode = fcode();
477 void MathedIter::ipop()