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)
47 MathedArray * MathedIter::GetData() const
52 short MathedIter::fcode() const
57 void MathedIter::fcode(short c) const
63 int MathedIter::Empty() const
65 return array->last() <= 1;
69 int MathedIter::OK() const
71 return array && (pos < array->last());
75 void MathedIter::Reset()
77 if (array->last() > 0 && MathIsFont((*array)[0])) {
89 byte MathedIter::GetChar() const
99 string const MathedIter::GetString() const
102 fcode((*array)[++pos]);
107 for (; (*array)[pos] >= ' ' && pos < array->last(); ++pos)
114 MathedInset * MathedIter::GetInset() const
118 array->raw_pointer_copy(&p, pos + 1);
121 lyxerr << "Math Error: This is not an inset["
122 << (*array)[pos] << "]" << endl;
128 // An active math inset MUST be derived from MathParInset because it
129 // must have at least one paragraph to edit
130 MathParInset * MathedIter::GetActiveInset() const
133 return reinterpret_cast<MathParInset*>(GetInset());
135 lyxerr << "Math Error: This is not an active inset" << endl;
140 bool MathedIter::Next()
145 if ((*array)[pos] < ' ') {
156 pos += sizeof(char*) + 2;
161 fcode((*array)[pos++]);
167 bool MathedIter::goNextCode(MathedTextCodes code)
170 if ((*array)[pos] == code)
178 void MathedIter::goPosAbs(int p)
181 while (pos < p && Next())
186 void MathedIter::insert(byte c, MathedTextCodes t)
191 if (t == LM_TC_TAB && col >= ncols - 1)
194 // Never more than one space // array->bf[pos-1] gives error from purify:
195 // Reading 1 byte from 0x47b857 in the heap.
197 // Address 0x47b857 is 1 byte before start of malloc'd block at
198 // 0x47b858 of 16 bytes.
200 if (c == ' ' && ((*array)[pos] == ' ' || (*array)[pos - 1] == ' '))
203 if (IsFont() && (*array)[pos] == t) {
207 if (t != fcode() && pos > 0 && MathIsFont((*array)[pos - 1])) {
210 for (; k >= 0 && (*array)[k] >= ' '; --k)
212 fcode( (k >= 0 && MathIsFont((*array)[k])) ? (*array)[k] : -1 );
216 short const f = ((*array)[pos] < ' ') ? 0 : fcode();
217 int shift = (t == fcode()) ? 1 : ((f) ? 3 : 2);
219 if (t == LM_TC_TAB || t == LM_TC_CR) {
229 if (pos < array->last())
230 array->move(pos, shift);
232 array->need_size(array->last() + shift);
233 array->last(array->last() + shift);
234 (*array)[array->last()] = '\0';
239 (*array)[pos + shift - 1] = fcode();
252 // Prepare to insert a non-char object
253 void MathedIter::split(int shift)
255 if (pos < array->last()) {
258 if ((*array)[pos] >= ' ') {
259 if (pos> 0 && MathIsFont((*array)[pos - 1]))
267 array->move(pos, shift);
270 (*array)[pos + shift - 1] = fcode();
274 array->need_size(array->last() + shift);
275 array->last(array->last() + shift);
278 (*array)[array->last()] = '\0';
282 // I assume that both pos and pos2 are legal positions
283 void MathedIter::join(int pos2)
285 if (!OK() || pos2 <= pos)
289 if (pos > 0 && (*array)[pos] >= ' ' && MathIsFont((*array)[pos - 1]))
292 if (MathIsFont((*array)[pos2 - 1]))
295 if ((*array)[pos2] >= ' ') {
296 for (int p = pos2; p > 0; --p) {
297 if (MathIsFont((*array)[p])) {
305 array->move(pos2, pos - pos2);
309 void MathedIter::insertInset(MathedInset * p, int type)
312 if (!MathIsInset(type))
315 array->insertInset(pos, p, type);
319 int const shift = SizeInset;
321 if (!MathIsInset(type))
326 (*array)[pos] = type;
327 array->raw_pointer_insert(p, pos + 1, sizeof(p));
329 (*array)[pos - 1] = type;
330 (*array)[array->last()] = '\0';
336 bool MathedIter::Delete()
345 if (MathIsFont((*array)[pos - 1]) && (*array)[pos + 1] < ' ') {
349 for (; i > 0 && !MathIsFont((*array)[i]); --i)
351 if (i > 0 && MathIsFont((*array)[i]))
358 if (MathIsInset((*array)[pos]))
359 shift = sizeof(char*) + 2;
360 else if (c == LM_TC_TAB || c == LM_TC_CR) {
362 // lyxerr <<"Es un tab.";
364 lyxerr << "Math Warning: expected inset." << endl;
370 array->move(pos + shift, -shift);
371 if (pos >= array->last())
372 pos = (array->last() > 0) ? array->last() : 0;
379 void MathedIter::Clear()
382 lyxerr << "Math error: Attempting to clean a void array." << endl;
389 MathedInset * inset = GetInset();
390 if (inset->GetType()!= LM_OT_MACRO_ARG)
399 // Check consistency of tabs and crs
400 void MathedIter::checkTabs()
404 // MathedIter:Reset();
406 if ((IsTab() && col >= ncols - 1) || (IsCR() && !(MthIF_CR & flags))) {
411 if (IsCR() && col < ncols - 2)
412 insert(' ', LM_TC_TAB);
418 insert(' ', LM_TC_TAB);
424 // Try to adjust tabs in the expected place, as used in eqnarrays
426 // - If there are a relation operator, put tabs around it
427 // - If tehre are not a relation operator, put everything in the
429 void MathedIter::adjustTabs()
433 bool MathedIter::IsInset() const
435 return MathIsInset((*array)[pos]);
439 bool MathedIter::IsActive() const
441 return MathIsActive((*array)[pos]);
445 bool MathedIter::IsFont() const
447 return MathIsFont((*array)[pos]);
451 bool MathedIter::IsScript() const
453 return MathIsScript((*array)[pos]);
457 bool MathedIter::IsTab() const
459 return ((*array)[pos] == LM_TC_TAB);
463 bool MathedIter::IsCR() const
465 return ((*array)[pos] == LM_TC_CR);
469 MathedIter::MathedIter(MathedArray * d)
475 fcode( (array && IsFont()) ? (*array)[0] : 0 );
479 void MathedIter::ipush()
481 stck.fcode = fcode();
488 void MathedIter::ipop()