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 void MathedIter::SetData(MathedArray * a)
52 MathedArray * MathedIter::GetData() const
58 int MathedIter::Empty() const
60 return array->last() <= 1;
64 int MathedIter::OK() const
66 return array && (pos < array->last());
70 void MathedIter::Reset()
72 if (array->last() > 0 && MathIsFont((*array)[0])) {
84 byte MathedIter::GetChar() const
87 fcode = (*array)[pos];
94 string const MathedIter::GetString() const
97 fcode = (*array)[++pos];
102 for (; (*array)[pos] >= ' ' && pos < array->last(); ++pos)
109 MathedInset * MathedIter::GetInset() const
113 array->raw_pointer_copy(&p, pos + 1);
116 lyxerr << "Math Error: This is not an inset["
117 << (*array)[pos] << "]" << endl;
123 // An active math inset MUST be derived from MathParInset because it
124 // must have at least one paragraph to edit
125 MathParInset * MathedIter::GetActiveInset() const
128 return reinterpret_cast<MathParInset*>(GetInset());
130 lyxerr << "Math Error: This is not an active inset" << endl;
135 bool MathedIter::Next()
140 if ((*array)[pos] < ' ') {
151 pos += sizeof(char*) + 2;
156 fcode = (*array)[pos++];
162 bool MathedIter::goNextCode(MathedTextCodes code)
165 if ((*array)[pos] == code)
173 void MathedIter::goPosAbs(int p)
176 while (pos < p && Next())
181 void MathedIter::Insert(byte c, MathedTextCodes t)
186 if (t == LM_TC_TAB && col >= ncols - 1)
189 // Never more than one space // array->bf[pos-1] gives error from purify:
190 // Reading 1 byte from 0x47b857 in the heap.
192 // Address 0x47b857 is 1 byte before start of malloc'd block at
193 // 0x47b858 of 16 bytes.
195 if (c == ' ' && ((*array)[pos] == ' ' || (*array)[pos - 1] == ' '))
198 if (IsFont() && (*array)[pos] == t) {
202 if (t != fcode && pos > 0 && MathIsFont((*array)[pos - 1])) {
205 for (; k >= 0 && (*array)[k] >= ' '; --k)
207 fcode = (k >= 0 && MathIsFont((*array)[k])) ? (*array)[k] : -1;
211 short const f = ((*array)[pos] < ' ') ? 0 : fcode;
212 int shift = (t == fcode) ? 1 : ((f) ? 3 : 2);
214 if (t == LM_TC_TAB || t == LM_TC_CR) {
224 if (pos < array->last())
225 array->move(pos, shift);
227 array->need_size(array->last() + shift);
228 array->last(array->last() + shift);
229 (*array)[array->last()] = '\0';
234 (*array)[pos + shift - 1] = fcode;
247 // Prepare to insert a non-char object
248 void MathedIter::split(int shift)
250 if (pos < array->last()) {
253 if ((*array)[pos] >= ' ') {
254 if (pos> 0 && MathIsFont((*array)[pos - 1]))
262 array->move(pos, shift);
265 (*array)[pos + shift - 1] = fcode;
269 array->need_size(array->last() + shift);
270 array->last(array->last() + shift);
273 (*array)[array->last()] = '\0';
277 // I assume that both pos and pos2 are legal positions
278 void MathedIter::join(int pos2)
280 if (!OK() || pos2 <= pos)
284 if (pos > 0 && (*array)[pos] >= ' ' && MathIsFont((*array)[pos - 1]))
287 if (MathIsFont((*array)[pos2 - 1]))
290 if ((*array)[pos2] >= ' ') {
291 for (int p = pos2; p > 0; --p) {
292 if (MathIsFont((*array)[p])) {
300 array->move(pos2, pos - pos2);
304 void MathedIter::Insert(MathedInset * p, int type)
306 int const shift = SizeInset;
308 if (!MathIsInset(type))
313 (*array)[pos] = type;
314 array->raw_pointer_insert(p, pos + 1, sizeof(p));
316 (*array)[pos - 1] = type;
317 (*array)[array->last()] = '\0';
322 bool MathedIter::Delete()
331 if (MathIsFont((*array)[pos - 1]) && (*array)[pos + 1] < ' ') {
335 for (; i > 0 && !MathIsFont((*array)[i]); --i)
337 if (i > 0 && MathIsFont((*array)[i]))
344 if (MathIsInset((*array)[pos]))
345 shift = sizeof(char*) + 2;
346 else if (c == LM_TC_TAB || c == LM_TC_CR) {
348 // lyxerr <<"Es un tab.";
350 lyxerr << "Math Warning: expected inset." << endl;
356 array->move(pos + shift, -shift);
357 if (pos >= array->last())
358 pos = (array->last() > 0) ? array->last() : 0;
365 MathedArray * MathedIter::Copy(int pos1, int pos2)
368 // lyxerr << "Math error: Attempting to copy a void array." << endl;
373 MathedArray * t = array;
376 if (pos1 > 0 || pos2 <= array->last()) {
378 if (pos1 > 0 && (*array)[pos1] > ' ') {
379 for (int p = pos1; p >= 0; --p) {
380 if (MathIsFont((*array)[p])) {
390 if (pos2 > 0 && (*array)[pos2] >= ' '
391 && MathIsFont((*array)[pos2 - 1]))
394 int dx = pos2 - pos1;
395 a = new MathedArray(dx + MathedArray::ARRAY_MIN_SIZE);
396 // lyxerr << "VA " << pos2 << " " << pos2 << " " << dx << endl;
397 array->strange_copy(a, (fc) ? 1 : 0, pos1, dx);
405 a = new MathedArray(*array);
410 MathedInset * inset = GetInset();
411 inset = inset->Clone();
412 array->raw_pointer_insert(inset, pos + 1, sizeof(inset));
422 void MathedIter::Clear()
425 lyxerr << "Math error: Attempting to clean a void array." << endl;
432 MathedInset * inset = GetInset();
433 if (inset->GetType()!= LM_OT_MACRO_ARG)
442 // Check consistency of tabs and crs
443 void MathedIter::checkTabs()
447 // MathedIter:Reset();
449 if ((IsTab() && col >= ncols - 1) || (IsCR() && !(MthIF_CR & flags))) {
454 if (IsCR() && col < ncols - 2)
455 Insert(' ', LM_TC_TAB);
461 Insert(' ', LM_TC_TAB);
467 // Try to adjust tabs in the expected place, as used in eqnarrays
469 // - If there are a relation operator, put tabs around it
470 // - If tehre are not a relation operator, put everything in the
472 void MathedIter::adjustTabs()
476 bool MathedIter::IsInset() const
478 return MathIsInset((*array)[pos]);
482 bool MathedIter::IsActive() const
484 return MathIsActive((*array)[pos]);
488 bool MathedIter::IsFont() const
490 return MathIsFont((*array)[pos]);
494 bool MathedIter::IsScript() const
496 return MathIsScript((*array)[pos]);
500 bool MathedIter::IsTab() const
502 return ((*array)[pos] == LM_TC_TAB);
506 bool MathedIter::IsCR() const
508 return ((*array)[pos] == LM_TC_CR);
512 MathedIter::MathedIter(MathedArray * d)
518 fcode = (array && IsFont()) ? (*array)[0]: 0;
522 void MathedIter::ipush()
531 void MathedIter::ipop()