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
64 int MathedIter::Empty() const
66 return array->last() <= 1;
70 int MathedIter::OK() const
72 return array && (pos < array->last());
76 void MathedIter::Reset()
78 if (array->last() > 0 && MathIsFont((*array)[0])) {
90 byte MathedIter::GetChar() const
100 string const MathedIter::GetString() const
103 fcode((*array)[++pos]);
108 for (; (*array)[pos] >= ' ' && pos < array->last(); ++pos)
115 MathedInset * MathedIter::GetInset() const
119 array->raw_pointer_copy(&p, pos + 1);
122 lyxerr << "Math Error: This is not an inset["
123 << (*array)[pos] << "]" << endl;
129 // An active math inset MUST be derived from MathParInset because it
130 // must have at least one paragraph to edit
131 MathParInset * MathedIter::GetActiveInset() const
134 return reinterpret_cast<MathParInset*>(GetInset());
136 lyxerr << "Math Error: This is not an active inset" << endl;
141 bool MathedIter::Next()
146 if ((*array)[pos] < ' ') {
157 pos += sizeof(char*) + 2;
162 fcode((*array)[pos++]);
168 bool MathedIter::goNextCode(MathedTextCodes code)
171 if ((*array)[pos] == code)
179 void MathedIter::goPosAbs(int p)
182 while (pos < p && Next())
187 void MathedIter::insert(byte c, MathedTextCodes t)
192 if (t == LM_TC_TAB && col >= ncols - 1)
195 // Never more than one space // array->bf[pos-1] gives error from purify:
196 // Reading 1 byte from 0x47b857 in the heap.
198 // Address 0x47b857 is 1 byte before start of malloc'd block at
199 // 0x47b858 of 16 bytes.
201 if (c == ' ' && ((*array)[pos] == ' ' || (*array)[pos - 1] == ' '))
204 if (IsFont() && (*array)[pos] == t) {
208 if (t != fcode() && pos > 0 && MathIsFont((*array)[pos - 1])) {
211 for (; k >= 0 && (*array)[k] >= ' '; --k)
213 fcode( (k >= 0 && MathIsFont((*array)[k])) ? (*array)[k] : -1 );
217 short const f = ((*array)[pos] < ' ') ? 0 : fcode();
218 int shift = (t == fcode()) ? 1 : ((f) ? 3 : 2);
220 if (t == LM_TC_TAB || t == LM_TC_CR) {
230 if (pos < array->last())
231 array->move(pos, shift);
233 array->need_size(array->last() + shift);
234 array->last(array->last() + shift);
235 (*array)[array->last()] = '\0';
240 (*array)[pos + shift - 1] = fcode();
253 // Prepare to insert a non-char object
254 void MathedIter::split(int shift)
256 if (pos < array->last()) {
259 if ((*array)[pos] >= ' ') {
260 if (pos> 0 && MathIsFont((*array)[pos - 1]))
268 array->move(pos, shift);
271 (*array)[pos + shift - 1] = fcode();
275 array->need_size(array->last() + shift);
276 array->last(array->last() + shift);
279 (*array)[array->last()] = '\0';
283 // I assume that both pos and pos2 are legal positions
284 void MathedIter::join(int pos2)
286 if (!OK() || pos2 <= pos)
290 if (pos > 0 && (*array)[pos] >= ' ' && MathIsFont((*array)[pos - 1]))
293 if (MathIsFont((*array)[pos2 - 1]))
296 if ((*array)[pos2] >= ' ') {
297 for (int p = pos2; p > 0; --p) {
298 if (MathIsFont((*array)[p])) {
306 array->move(pos2, pos - pos2);
310 void MathedIter::insertInset(MathedInset * p, int type)
313 if (!MathIsInset(type))
316 array->insertInset(pos, p, type);
320 int const shift = SizeInset;
322 if (!MathIsInset(type))
327 (*array)[pos] = type;
328 array->raw_pointer_insert(p, pos + 1, sizeof(p));
330 (*array)[pos - 1] = type;
331 (*array)[array->last()] = '\0';
337 bool MathedIter::Delete()
346 if (MathIsFont((*array)[pos - 1]) && (*array)[pos + 1] < ' ') {
350 for (; i > 0 && !MathIsFont((*array)[i]); --i)
352 if (i > 0 && MathIsFont((*array)[i]))
359 if (MathIsInset((*array)[pos]))
360 shift = sizeof(char*) + 2;
361 else if (c == LM_TC_TAB || c == LM_TC_CR) {
363 // lyxerr <<"Es un tab.";
365 lyxerr << "Math Warning: expected inset." << endl;
371 array->move(pos + shift, -shift);
372 if (pos >= array->last())
373 pos = (array->last() > 0) ? array->last() : 0;
380 void MathedIter::Clear()
383 lyxerr << "Math error: Attempting to clean a void array." << endl;
390 MathedInset * inset = GetInset();
391 if (inset->GetType()!= LM_OT_MACRO_ARG)
400 // Check consistency of tabs and crs
401 void MathedIter::checkTabs()
405 // MathedIter:Reset();
407 if ((IsTab() && col >= ncols - 1) || (IsCR() && !(MthIF_CR & flags))) {
412 if (IsCR() && col < ncols - 2)
413 insert(' ', LM_TC_TAB);
419 insert(' ', LM_TC_TAB);
425 // Try to adjust tabs in the expected place, as used in eqnarrays
427 // - If there are a relation operator, put tabs around it
428 // - If tehre are not a relation operator, put everything in the
430 void MathedIter::adjustTabs()
434 bool MathedIter::IsInset() const
436 return MathIsInset((*array)[pos]);
440 bool MathedIter::IsActive() const
442 return MathIsActive((*array)[pos]);
446 bool MathedIter::IsFont() const
448 return MathIsFont((*array)[pos]);
452 bool MathedIter::IsScript() const
454 return MathIsScript((*array)[pos]);
458 bool MathedIter::IsTab() const
460 return ((*array)[pos] == LM_TC_TAB);
464 bool MathedIter::IsCR() const
466 return ((*array)[pos] == LM_TC_CR);
470 MathedIter::MathedIter(MathedArray * d)
476 fcode( (array && IsFont()) ? (*array)[0] : 0 );
480 void MathedIter::ipush()
482 stck.fcode = fcode();
489 void MathedIter::ipop()