]> git.lyx.org Git - lyx.git/blob - src/insets/insettext.C
15afebff957f579f6ad560b496128f32b856a36c
[lyx.git] / src / insets / insettext.C
1 /* This file is part of
2  * ======================================================
3  * 
4  *           LyX, The Document Processor
5  *
6  *           Copyright 1998-2001 The LyX Team.
7  *
8  * ======================================================
9  */
10
11 #include <config.h>
12
13 #include <fstream>
14 #include <algorithm>
15
16 #include <cstdlib>
17 //#include <signal.h>
18
19 #ifdef __GNUG__
20 #pragma implementation
21 #endif
22
23 #include "insettext.h"
24 #include "paragraph.h"
25 #include "lyxlex.h"
26 #include "debug.h"
27 #include "lyxfont.h"
28 #include "commandtags.h"
29 #include "buffer.h"
30 #include "LyXView.h"
31 #include "BufferView.h"
32 #include "layout.h"
33 #include "LaTeXFeatures.h"
34 #include "Painter.h"
35 #include "frontends/Alert.h"
36 #include "lyxtext.h"
37 #include "lyxcursor.h"
38 #include "CutAndPaste.h"
39 #include "font.h"
40 #include "LColor.h"
41 #include "support/textutils.h"
42 #include "support/LAssert.h"
43 #include "support/lstrings.h"
44 #include "lyxrow.h"
45 #include "lyxrc.h"
46 #include "intl.h"
47 #include "trans_mgr.h"
48 #include "lyxscreen.h"
49 #include "WorkArea.h"
50 #include "gettext.h"
51 #include "lyxfunc.h"
52 #include "ParagraphParameters.h"
53 #include "undo_funcs.h"
54 #include "lyxfind.h"
55
56 using std::ostream;
57 using std::ifstream;
58 using std::endl;
59 using std::min;
60 using std::max;
61 using std::make_pair;
62 using std::vector;
63
64 using lyx::pos_type;
65 using lyx::layout_type;
66 using lyx::textclass_type;
67
68 extern unsigned char getCurrentTextClass(Buffer *);
69 extern bool math_insert_greek(BufferView *, char);
70 extern int greek_kb_flag;
71
72
73 // These functions should probably go into bufferview_funcs somehow (Jug)
74
75 void InsetText::saveLyXTextState(LyXText * t) const
76 {
77         // check if my paragraphs are still valid
78         Paragraph * p = par;
79         while (p) {
80                 if (p == t->cursor.par())
81                         break;
82                 p = p->next();
83         }
84         
85         if (p && t->cursor.pos() <= p->size()) {
86                 sstate.lpar = t->cursor.par();
87                 sstate.pos = t->cursor.pos();
88                 sstate.boundary = t->cursor.boundary();
89                 sstate.selstartpar = t->selection.start.par();
90                 sstate.selstartpos = t->selection.start.pos();
91                 sstate.selstartboundary = t->selection.start.boundary();
92                 sstate.selendpar = t->selection.end.par();
93                 sstate.selendpos = t->selection.end.pos();
94                 sstate.selendboundary = t->selection.end.boundary();
95                 sstate.selection = t->selection.set();
96                 sstate.mark_set = t->selection.mark();
97                 sstate.refresh = t->refresh_row != 0;
98         } else {
99                 sstate.lpar = 0;
100         }
101 }
102
103 void InsetText::restoreLyXTextState(BufferView * bv, LyXText * t) const
104 {
105         if (sstate.lpar) {
106                 t->selection.set(true);
107                 /* at this point just to avoid the Delete-Empty-Paragraph
108                  * Mechanism when setting the cursor */
109                 t->selection.mark(sstate.mark_set);
110                 if (sstate.selection) {
111                         t->setCursor(bv, sstate.selstartpar, sstate.selstartpos,
112                                      true, sstate.selstartboundary);
113                         t->selection.cursor = t->cursor;
114                         t->setCursor(bv, sstate.selendpar, sstate.selendpos,
115                                      true, sstate.selendboundary);
116                         t->setSelection(bv);
117                         t->setCursor(bv, sstate.lpar, sstate.pos);
118                 } else {
119                         t->setCursor(bv, sstate.lpar, sstate.pos, true, sstate.boundary);
120                         t->selection.cursor = t->cursor;
121                         t->selection.set(false);
122                 }
123                 if (sstate.refresh) {
124                 }
125         }
126 }
127
128
129 InsetText::InnerCache::InnerCache(boost::shared_ptr<LyXText> t)
130 {
131         text = t;
132         remove = false;
133 }
134
135
136 InsetText::InsetText()
137         : UpdatableInset(), lt(0), in_update(false), do_resize(0),
138           do_reinit(false)
139 {
140         par = new Paragraph;
141         init();
142 }
143
144
145 InsetText::InsetText(InsetText const & in, bool same_id)
146         : UpdatableInset(in, same_id), lt(0), in_update(false), do_resize(0),
147           do_reinit(false)
148 {
149         par = 0;
150         init(&in, same_id);
151 }
152
153
154 InsetText & InsetText::operator=(InsetText const & it)
155 {
156         init(&it);
157         return * this;
158 }
159
160
161 void InsetText::init(InsetText const * ins, bool same_id)
162 {
163         if (ins) {
164                 setParagraphData(ins->par, same_id);
165                 autoBreakRows = ins->autoBreakRows;
166                 drawFrame_ = ins->drawFrame_;
167                 frame_color = ins->frame_color;
168                 if (same_id)
169                         id_ = ins->id_;
170         } else {
171                 Paragraph * p = par;
172                 while (p) {
173                         p->setInsetOwner(this);
174                         p = p->next();
175                 }
176                 the_locking_inset = 0;
177                 drawFrame_ = NEVER;
178                 frame_color = LColor::insetframe;
179                 autoBreakRows = false;
180         }
181         top_y = 0;
182         insetAscent = 0;
183         insetDescent = 0;
184         insetWidth = 0;
185         old_max_width = 0;
186         no_selection = false;
187         need_update = FULL;
188         drawTextXOffset = 0;
189         drawTextYOffset = 0;
190         xpos = 0.0;
191         locked = false;
192         old_par = 0;
193         last_drawn_width = -1;
194         frame_is_visible = false;
195         cached_bview = 0;
196         sstate.lpar = 0;
197         in_insetAllowed = false;
198 }
199
200
201 InsetText::~InsetText()
202 {
203         cached_bview = 0;
204
205         // NOTE
206         
207         while (par) {
208                 Paragraph * tmp = par->next();
209                 delete par;
210                 par = tmp;
211         }
212 }
213
214
215 void InsetText::clear()
216 {
217         while (par) {
218                 Paragraph * tmp = par->next();
219                 delete par;
220                 par = tmp;
221         }
222         par = new Paragraph;
223         reinitLyXText();
224         need_update = INIT;
225 }
226
227
228 Inset * InsetText::clone(Buffer const &, bool same_id) const
229 {
230         return  new InsetText(*this, same_id);
231 }
232
233
234 void InsetText::write(Buffer const * buf, ostream & os) const
235 {
236         os << "Text\n";
237         writeParagraphData(buf, os);
238 }
239
240
241 void InsetText::writeParagraphData(Buffer const * buf, ostream & os) const
242 {
243         par->writeFile(buf, os, buf->params, 0);
244 }
245
246
247 void InsetText::read(Buffer const * buf, LyXLex & lex)
248 {
249         string token;
250         int pos = 0;
251         Paragraph * return_par = 0;
252         Paragraph::depth_type depth = 0; 
253         LyXFont font(LyXFont::ALL_INHERIT);
254
255         clear();
256         
257         while (lex.isOK()) {
258                 lex.nextToken();
259                 token = lex.getString();
260                 if (token.empty())
261                         continue;
262                 if (token == "\\end_inset") {
263 #ifndef NO_COMPABILITY
264                         const_cast<Buffer*>(buf)->insertErtContents(par, pos, font, false);
265 #endif
266                         break;
267                 }
268                 
269                 if (const_cast<Buffer*>(buf)->
270                         parseSingleLyXformat2Token(lex, par, return_par,
271                                                    token, pos, depth, font)) {
272                         // the_end read this should NEVER happen
273                         lex.printError("\\the_end read in inset! Error in document!");
274                         return;
275                 }
276         }
277         if (!return_par)
278                 return_par = par;
279         par = return_par;
280         while (return_par) {
281                 return_par->setInsetOwner(this);
282                 return_par = return_par->next();
283         }
284         
285         if (token != "\\end_inset") {
286                 lex.printError("Missing \\end_inset at this point. "
287                                            "Read: `$$Token'");
288         }
289         need_update = FULL;
290 }
291
292
293 int InsetText::ascent(BufferView * bv, LyXFont const &) const
294 {
295         bool clear = false;
296         if (!lt) {
297                 lt = getLyXText(bv);
298                 clear = true;
299         }
300         insetAscent = lt->firstRow()->ascent_of_text() + TEXT_TO_INSET_OFFSET;
301         if (clear)
302                 lt = 0;
303         return insetAscent;
304 }
305
306
307 int InsetText::descent(BufferView * bv, LyXFont const &) const
308 {
309         bool clear = false;
310         if (!lt) {
311                 lt = getLyXText(bv);
312                 clear = true;
313         }
314         insetDescent = lt->height - lt->firstRow()->ascent_of_text() +
315                 TEXT_TO_INSET_OFFSET;
316         if (clear)
317                 lt = 0;
318         return insetDescent;
319 }
320
321
322 int InsetText::width(BufferView * bv, LyXFont const &) const
323 {
324         insetWidth = max(textWidth(bv), (int)getLyXText(bv)->width) +
325                 (2 * TEXT_TO_INSET_OFFSET);
326         insetWidth = max(insetWidth, 10);
327         return insetWidth;
328 }
329
330
331 int InsetText::textWidth(BufferView * bv, bool fordraw) const
332 {
333         int w;
334         if (!autoBreakRows) {
335                 w = -1;
336         } else {
337                 w = getMaxWidth(bv, this);
338         }
339         if (fordraw) {
340                 return max(w - (2 * TEXT_TO_INSET_OFFSET),
341                            (int)getLyXText(bv)->width);
342         } else if (w < 0) {
343             return -1;
344         }
345         return w - (2 * TEXT_TO_INSET_OFFSET);
346 }
347
348
349 void InsetText::draw(BufferView * bv, LyXFont const & f,
350                      int baseline, float & x, bool cleared) const
351 {
352         if (nodraw())
353                 return;
354
355         Painter & pain = bv->painter();
356         
357         // this is the first thing we have to ask because if the x pos
358         // changed we have to do a complete rebreak of the text as we
359         // may have few space to draw in. Well we should check on this too
360         int old_x = top_x;
361         if (top_x != int(x)) {
362                 top_x = int(x);
363                 int nw = getMaxWidth(bv, this);
364                 if (nw > 0 && old_max_width != nw) {
365                         need_update = INIT;
366                         old_max_width = nw;
367                         bv->text->status(bv, LyXText::CHANGED_IN_DRAW);
368                         topx_set = true;
369                         return;
370                 } else {
371                         top_x = old_x;
372                 }
373         }
374
375         // repaint the background if needed
376         if (cleared && backgroundColor() != LColor::background) {
377                 top_x = int(x);
378                 clearInset(bv, baseline, cleared);
379                 top_x = old_x;
380         }
381
382         // no draw is necessary !!!
383         if ((drawFrame_ == LOCKED) && !locked && !par->size()) {
384                 top_x = int(x);
385                 topx_set = true;
386                 top_baseline = baseline;
387                 x += width(bv, f);
388                 if (need_update & CLEAR_FRAME)
389                         clearFrame(pain, cleared);
390                 need_update = NONE;
391                 return;
392         }
393
394         xpos = x;
395         if (!owner())
396                 x += static_cast<float>(scroll());
397
398         // if top_x differs we did it already
399         if (!cleared && (top_x == int(x))
400             && ((need_update&(INIT|FULL)) || (top_baseline != baseline)
401                 ||(last_drawn_width != insetWidth))) {
402                 clearInset(bv, baseline, cleared);
403         }
404
405         top_x = int(x);
406         topx_set = true;
407         if (cleared)
408                 frame_is_visible = false;
409
410         if (!cleared && (need_update == NONE)) {
411                 if (locked)
412                         drawFrame(pain, cleared);
413                 return;
414         }
415
416         // call these methods so that insetWidth, insetAscent and
417         // insetDescent have the right values. 
418         width(bv, f);
419         ascent(bv, f);
420         descent(bv, f);
421
422         top_baseline = baseline;
423         top_y = baseline - insetAscent;
424
425         if (last_drawn_width != insetWidth) {
426                 if (!cleared) 
427                         clearInset(bv, baseline, cleared);
428                 need_update |= FULL;
429                 last_drawn_width = insetWidth;
430         }
431
432         if (the_locking_inset && (cpar(bv) == inset_par)
433                 && (cpos(bv) == inset_pos)) {
434                 inset_x = cx(bv) - top_x + drawTextXOffset;
435                 inset_y = cy(bv) + drawTextYOffset;
436         }
437         if (!cleared && (need_update == CURSOR)
438             && !getLyXText(bv)->selection.set()) {
439                 drawFrame(pain, cleared);
440                 x += insetWidth; 
441                 need_update = NONE;
442                 return;
443         }
444         bool clear = false;
445         if (!lt) {
446                 lt = getLyXText(bv);
447                 clear = true;
448         }
449         x += TEXT_TO_INSET_OFFSET;
450
451         Row * row = lt->firstRow();
452         int y_offset = baseline - row->ascent_of_text();
453         int ph = pain.paperHeight();
454         int first = 0;
455         int y = y_offset;
456         while ((row != 0) && ((y+row->height()) <= 0)) {
457                 y += row->height();
458                 first += row->height();
459                 row = row->next();
460         }
461         if (y_offset < 0)
462                 y_offset = y;
463         lt->first = first;
464         if (cleared || (need_update&(INIT|FULL))) {
465                 int yf = y_offset;
466                 y = 0;
467                 while ((row != 0) && (yf < ph)) {
468                         lt->getVisibleRow(bv, y+y_offset, int(x), row,
469                                                 y+first, cleared);
470                         y += row->height();
471                         yf += row->height();
472                         row = row->next();
473                 }
474         } else if (!locked) {
475                 if (need_update & CURSOR) {
476                         bv->screen()->toggleSelection(lt, bv, true, y_offset,int(x));
477                         lt->clearSelection();
478                         lt->selection.cursor = lt->cursor;
479                 }
480                 bv->screen()->update(lt, bv, y_offset, int(x));
481         } else {
482                 locked = false;
483                 if (need_update & SELECTION) {
484                         bv->screen()->toggleToggle(lt, bv, y_offset, int(x));
485                 } else if (need_update & CURSOR) {
486                         bv->screen()->toggleSelection(lt, bv, true, y_offset,int(x));
487                         lt->clearSelection();
488                         lt->selection.cursor = lt->cursor;
489                 }
490                 bv->screen()->update(lt, bv, y_offset, int(x));
491                 locked = true;
492         }
493
494         lt->refresh_y = 0;
495         lt->status(bv, LyXText::UNCHANGED);
496         if ((need_update != CURSOR_PAR) &&
497             ((drawFrame_ == ALWAYS) || ((drawFrame_ == LOCKED) && locked))) {
498                 drawFrame(pain, cleared);
499         } else if (need_update & CLEAR_FRAME) {
500                 clearFrame(pain, cleared);
501         }
502         
503         x += insetWidth - TEXT_TO_INSET_OFFSET;
504         
505         if (bv->text->status() == LyXText::CHANGED_IN_DRAW) {
506                 need_update |= FULL;
507         } else if (need_update != INIT) {
508                 need_update = NONE;
509         }
510         
511         if (clear)
512                 lt = 0;
513 }
514
515
516 void InsetText::drawFrame(Painter & pain, bool cleared) const
517 {
518         static int const ttoD2 = TEXT_TO_INSET_OFFSET / 2;
519         if (!frame_is_visible || cleared) {
520                 frame_x = top_x + ttoD2;
521                 frame_y = top_baseline - insetAscent + ttoD2;
522                 frame_w = insetWidth - TEXT_TO_INSET_OFFSET;
523                 frame_h = insetAscent + insetDescent - TEXT_TO_INSET_OFFSET;
524                 pain.rectangle(frame_x, frame_y,
525                                frame_w, frame_h,
526                                frame_color);
527                 frame_is_visible = true;
528         }
529 }
530
531
532 void InsetText::clearFrame(Painter & pain, bool cleared) const
533 {
534         if (frame_is_visible) {
535                 if (!cleared) {
536                         pain.rectangle(frame_x, frame_y, frame_w, frame_h,
537                                        backgroundColor());
538                 }
539                 frame_is_visible = false;
540         }
541 }
542
543
544 void InsetText::update(BufferView * bv, LyXFont const & font, bool reinit)
545 {
546         if (in_update) {
547                 if (reinit && owner()) {
548                         reinitLyXText();
549                         owner()->update(bv, font, true);
550                 }
551                 return;
552         }
553         in_update = true;
554         if (reinit || need_update == INIT) {
555                 need_update = FULL;
556                 // we should put this call where we set need_update to INIT!
557                 reinitLyXText();
558                 if (owner())
559                         owner()->update(bv, font, true);
560                 in_update = false;
561                 return;
562         }
563         if (the_locking_inset) {
564                 inset_x = cx(bv) - top_x + drawTextXOffset;
565                 inset_y = cy(bv) + drawTextYOffset;
566                 the_locking_inset->update(bv, font, reinit);
567         }
568
569         bool clear = false;
570         if (!lt) {
571                 lt = getLyXText(bv);
572                 clear = true;
573         }
574 #if 0
575         int oldw = insetWidth;
576         insetWidth = lt->width + (2 * TEXT_TO_INSET_OFFSET);
577         if (oldw != insetWidth) {
578                 resizeLyXText(bv);
579                 need_update |= FULL;
580                 if (clear)
581                         lt = 0;
582                 in_update = false;
583                 return;
584         }
585 #endif
586         if ((need_update & CURSOR_PAR) && (lt->status() == LyXText::UNCHANGED) &&
587                 the_locking_inset)
588         {
589                 lt->updateInset(bv, the_locking_inset);
590         }
591         if (lt->status() == LyXText::NEED_MORE_REFRESH)
592                 need_update |= FULL;
593         if (clear)
594                 lt = 0;
595         in_update = false;
596 }
597
598
599 void InsetText::setUpdateStatus(BufferView * bv, int what) const
600 {
601         bool clear = false;
602         if (!lt) {
603                 lt = getLyXText(bv);
604                 clear = true;
605         }
606         need_update |= what;
607         // we have to redraw us full if our LyXText NEEDS_MORE_REFRES or
608         // if we don't break row so that we only have one row to update!
609         if ((lt->status() == LyXText::NEED_MORE_REFRESH) ||
610             (!autoBreakRows &&
611              (lt->status() == LyXText::NEED_VERY_LITTLE_REFRESH)))
612         {
613                 need_update |= FULL;
614         } else if (lt->status() == LyXText::NEED_VERY_LITTLE_REFRESH) {
615                 need_update |= CURSOR_PAR;
616         }
617
618         // this to not draw a selection when we redraw all of it!
619         if (need_update & CURSOR && !(need_update & SELECTION)) {
620                 if (lt->selection.set())
621                         need_update = FULL;
622                 lt->clearSelection();
623         }
624         if (clear)
625                 lt = 0;
626 }
627
628
629 void InsetText::updateLocal(BufferView * bv, int what, bool mark_dirty) const
630 {
631         bool clear = false;
632         if (!lt) {
633                 lt = getLyXText(bv);
634                 clear = true;
635         }
636         lt->fullRebreak(bv);
637         setUpdateStatus(bv, what);
638         if (((need_update != CURSOR) && (need_update != NONE)) ||
639             (lt->status() != LyXText::UNCHANGED) || lt->selection.set())
640         {
641                 bv->updateInset(const_cast<InsetText *>(this), mark_dirty);
642         }
643         if (need_update == CURSOR)
644                 need_update = NONE;
645         bv->owner()->showState();
646         if (old_par != cpar(bv)) {
647                 bv->owner()->setLayout(cpar(bv)->getLayout());
648                 old_par = cpar(bv);
649         }
650         if (clear)
651                 lt = 0;
652 }
653
654
655 string const InsetText::editMessage() const
656 {
657         return _("Opened Text Inset");
658 }
659
660
661 void InsetText::edit(BufferView * bv, int x, int y, unsigned int button)
662 {
663         UpdatableInset::edit(bv, x, y, button);
664         
665         if (!bv->lockInset(this)) {
666                 lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
667                 return;
668         }
669         locked = true;
670         the_locking_inset = 0;
671         inset_pos = inset_x = inset_y = 0;
672         inset_boundary = false;
673         inset_par = 0;
674         old_par = 0;
675         int tmp_y = (y < 0) ? 0 : y;
676         bool clear = false;
677         if (!lt) {
678                 lt = getLyXText(bv);
679                 clear = true;
680         }
681
682         if (!checkAndActivateInset(bv, x, tmp_y, button))
683                 lt->setCursorFromCoordinates(bv, x - drawTextXOffset,
684                                             y + insetAscent);
685         lt->clearSelection();
686         finishUndo();
687         showInsetCursor(bv);
688         updateLocal(bv, CURSOR, false);
689
690         // If the inset is empty set the language of the current font to the
691         // language to the surronding text (if different).
692         if (par->size() == 0 && !par->next() &&
693                 bv->getParentLanguage(this) != lt->current_font.language()) {
694                 LyXFont font(LyXFont::ALL_IGNORE);
695                 font.setLanguage(bv->getParentLanguage(this));
696                 setFont(bv, font, false);
697         }
698         if (clear)
699                 lt = 0;
700 }
701
702
703 void InsetText::edit(BufferView * bv, bool front)
704 {
705         UpdatableInset::edit(bv, front);
706         
707         if (!bv->lockInset(this)) {
708                 lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
709                 return;
710         }
711         locked = true;
712         the_locking_inset = 0;
713         inset_pos = inset_x = inset_y = 0;
714         inset_boundary = false;
715         inset_par = 0;
716         old_par = 0;
717         bool clear = false;
718         if (!lt) {
719                 lt = getLyXText(bv);
720                 clear = true;
721         }
722         if (front)
723                 lt->setCursor(bv, par, 0);
724         else {
725                 Paragraph * p = par;
726                 while (p->next())
727                         p = p->next();
728 //              int const pos = (p->size() ? p->size()-1 : p->size());
729                 lt->setCursor(bv, p, p->size());
730         }
731         lt->clearSelection();
732         finishUndo();
733         showInsetCursor(bv);
734         updateLocal(bv, CURSOR, false);
735
736         // If the inset is empty set the language of the current font to the
737         // language to the surronding text (if different).
738         if (par->size() == 0 && !par->next() &&
739                 bv->getParentLanguage(this) != lt->current_font.language()) {
740                 LyXFont font(LyXFont::ALL_IGNORE);
741                 font.setLanguage(bv->getParentLanguage(this));
742                 setFont(bv, font, false);
743         }
744         if (clear)
745                 lt = 0;
746 }
747
748
749 void InsetText::insetUnlock(BufferView * bv)
750 {
751         if (the_locking_inset) {
752                 the_locking_inset->insetUnlock(bv);
753                 the_locking_inset = 0;
754         }
755         hideInsetCursor(bv);
756         no_selection = false;
757         locked = false;
758         int code;
759         if (drawFrame_ == LOCKED)
760                 code = CURSOR|CLEAR_FRAME;
761         else 
762                 code = CURSOR;
763         bool clear = false;
764         if (!lt) {
765                 lt = getLyXText(bv);
766                 clear = true;
767         }
768         if (lt->selection.set()) {
769                 lt->clearSelection();
770                 code = FULL;
771         } else if (owner()) {
772                 bv->owner()->setLayout(owner()->getLyXText(bv)
773                                        ->cursor.par()->getLayout());
774         } else
775                 bv->owner()->setLayout(bv->text->cursor.par()->getLayout());
776         // hack for deleteEmptyParMech
777         lt->setCursor(bv, par, 0);
778         updateLocal(bv, code, false);
779         if (clear)
780                 lt = 0;
781 }
782
783 void InsetText::lockInset(BufferView * bv, UpdatableInset * inset)
784 {
785         the_locking_inset = inset;
786         inset_x = cx(bv) - top_x + drawTextXOffset;
787         inset_y = cy(bv) + drawTextYOffset;
788         inset_pos = cpos(bv);
789         inset_par = cpar(bv);
790         inset_boundary = cboundary(bv);
791         updateLocal(bv, CURSOR, false);
792 }
793
794
795 bool InsetText::lockInsetInInset(BufferView * bv, UpdatableInset * inset)
796 {
797         lyxerr[Debug::INSETS] << "InsetText::LockInsetInInset("
798                               << inset << "): ";
799         if (!inset)
800                 return false;
801         if (!the_locking_inset) {
802                 Paragraph * p = par;
803                 int const id = inset->id();
804                 while(p) {
805                         Paragraph::inset_iterator it =
806                                 p->inset_iterator_begin();
807                         Paragraph::inset_iterator const end =
808                                 p->inset_iterator_end();
809                         for (; it != end; ++it) {
810                                 if ((*it) == inset) {
811                                         getLyXText(bv)->setCursorIntern(bv, p, it.getPos());
812                                         lockInset(bv, inset);
813                                         return true;
814                                 }
815                                 if ((*it)->getInsetFromID(id)) {
816                                         lockInset(bv, static_cast<UpdatableInset *>(*it));
817                                         return the_locking_inset->lockInsetInInset(bv, inset);
818                                 }
819                         }
820                         p = p->next();
821                 }
822                 return false;
823         }
824         if (inset == cpar(bv)->getInset(cpos(bv))) {
825                 lyxerr[Debug::INSETS] << "OK" << endl;
826                 lockInset(bv, inset);
827                 return true;
828         } else if (the_locking_inset && (the_locking_inset == inset)) {
829                 if (cpar(bv) == inset_par && cpos(bv) == inset_pos) {
830                         lyxerr[Debug::INSETS] << "OK" << endl;
831                         inset_x = cx(bv) - top_x + drawTextXOffset;
832                         inset_y = cy(bv) + drawTextYOffset;
833                 } else {
834                         lyxerr[Debug::INSETS] << "cursor.pos != inset_pos" << endl;
835                 }
836         } else if (the_locking_inset) {
837                 lyxerr[Debug::INSETS] << "MAYBE" << endl;
838                 return the_locking_inset->lockInsetInInset(bv, inset);
839         }
840         lyxerr[Debug::INSETS] << "NOT OK" << endl;
841         return false;
842 }
843
844
845 bool InsetText::unlockInsetInInset(BufferView * bv, UpdatableInset * inset,
846                                    bool lr)
847 {
848         if (!the_locking_inset)
849                 return false;
850         if (the_locking_inset == inset) {
851                 the_locking_inset->insetUnlock(bv);
852                 getLyXText(bv)->updateInset(bv, inset);
853                 the_locking_inset = 0;
854                 if (lr)
855                         moveRight(bv, false);
856                 old_par = 0; // force layout setting
857                 if (scroll())
858                         scroll(bv, 0.0F);
859                 else
860                         updateLocal(bv, CURSOR, false);
861                 return true;
862         }
863         return the_locking_inset->unlockInsetInInset(bv, inset, lr);
864 }
865
866
867 bool InsetText::updateInsetInInset(BufferView * bv, Inset * inset)
868 {
869         bool clear = false;
870         if (!lt) {
871                 lt = getLyXText(bv);
872                 clear = true;
873         }
874         if (!the_locking_inset) {
875                 bool found = lt->updateInset(bv, inset);
876                 if (clear)
877                         lt = 0;
878                 if (found)
879                         setUpdateStatus(bv, NONE);
880                 return found;
881         }
882         if (the_locking_inset != inset) {
883 #if 0
884                 lt->updateInset(bv, the_locking_inset);
885                 setUpdateStatus(bv, CURSOR_PAR);
886 #endif
887                 bool found = the_locking_inset->updateInsetInInset(bv, inset);
888                 if (clear)
889                         lt = 0;
890                 if (found)
891                         setUpdateStatus(bv, CURSOR_PAR);
892                 return found;
893         }
894         bool found = lt->updateInset(bv, inset);
895         if (clear)
896                 lt = 0;
897         if (found) {
898                 setUpdateStatus(bv, CURSOR_PAR);
899 #if 0
900                 updateLocal(bv, CURSOR_PAR, false);
901 #endif
902                 if (cpar(bv) == inset_par && cpos(bv) == inset_pos) {
903                         inset_x = cx(bv) - top_x + drawTextXOffset;
904                         inset_y = cy(bv) + drawTextYOffset;
905                 }
906         }
907         return found;
908 }
909
910
911 void InsetText::insetButtonPress(BufferView * bv, int x, int y, int button)
912 {
913         no_selection = true;
914
915         int tmp_x = x - drawTextXOffset;
916         int tmp_y = y + insetAscent - getLyXText(bv)->first;
917         Inset * inset = bv->checkInsetHit(getLyXText(bv), tmp_x, tmp_y, button);
918
919         hideInsetCursor(bv);
920         if (the_locking_inset) {
921                 if (the_locking_inset == inset) {
922                         the_locking_inset->insetButtonPress(bv,x-inset_x,y-inset_y,button);
923                         no_selection = false;
924                         return;
925                 } else if (inset) {
926                         // otherwise unlock the_locking_inset and lock the new inset
927                         the_locking_inset->insetUnlock(bv);
928                         inset_x = cx(bv) - top_x + drawTextXOffset;
929                         inset_y = cy(bv) + drawTextYOffset;
930                         the_locking_inset = static_cast<UpdatableInset*>(inset);
931                         inset->insetButtonPress(bv, x - inset_x, y - inset_y, button);
932                         inset->edit(bv, x - inset_x, y - inset_y, button);
933                         if (the_locking_inset)
934                                 updateLocal(bv, CURSOR, false);
935                         no_selection = false;
936                         return;
937                 }
938                 // otherwise only unlock the_locking_inset
939                 the_locking_inset->insetUnlock(bv);
940                 the_locking_inset = 0;
941         }
942         if (bv->theLockingInset()) {
943                 if (isHighlyEditableInset(inset)) {
944                         UpdatableInset * uinset = static_cast<UpdatableInset*>(inset);
945                         inset_x = cx(bv) - top_x + drawTextXOffset;
946                         inset_y = cy(bv) + drawTextYOffset;
947                         inset_pos = cpos(bv);
948                         inset_par = cpar(bv);
949                         inset_boundary = cboundary(bv);
950                         the_locking_inset = uinset;
951                         uinset->insetButtonPress(bv, x - inset_x, y - inset_y,
952                                                  button);
953                         uinset->edit(bv, x - inset_x, y - inset_y, 0);
954                         if (the_locking_inset)
955                                 updateLocal(bv, CURSOR, false);
956                         no_selection = false;
957                         return;
958                 }
959         }
960         if (!inset) { // && (button == 2)) {
961                 bool paste_internally = false;
962                 if ((button == 2) && getLyXText(bv)->selection.set()) {
963                         localDispatch(bv, LFUN_COPY, "");
964                         paste_internally = true;
965                 }
966                 bool clear = false;
967                 if (!lt) {
968                         lt = getLyXText(bv);
969                         clear = true;
970                 }
971                 lt->setCursorFromCoordinates(bv, x-drawTextXOffset, y + insetAscent);
972                 if (lt->selection.set()) {
973                         lt->clearSelection();
974                         updateLocal(bv, FULL, false);
975                 } else {
976                         lt->clearSelection();
977                 }
978                 bv->owner()->setLayout(cpar(bv)->getLayout());
979                 old_par = cpar(bv);
980                 // Insert primary selection with middle mouse
981                 // if there is a local selection in the current buffer,
982                 // insert this
983                 if (button == 2) {
984                         if (paste_internally)
985                                 localDispatch(bv, LFUN_PASTE, "");
986                         else
987                                 localDispatch(bv, LFUN_PASTESELECTION,
988                                               "paragraph");
989                 }
990                 if (clear)
991                         lt = 0;
992         }
993         showInsetCursor(bv);
994         no_selection = false;
995 }
996
997
998 bool InsetText::insetButtonRelease(BufferView * bv, int x, int y, int button)
999 {
1000         if (the_locking_inset) {
1001                 return the_locking_inset->insetButtonRelease(bv,
1002                                                              x - inset_x, y - inset_y,
1003                                                              button);
1004         }
1005         int tmp_x = x - drawTextXOffset;
1006         int tmp_y = y + insetAscent - getLyXText(bv)->first;
1007         Inset * inset = bv->checkInsetHit(getLyXText(bv), tmp_x, tmp_y, button);
1008         bool ret = false;
1009         if (inset) {
1010                 if (isHighlyEditableInset(inset)) {
1011                         ret = inset->insetButtonRelease(bv, x - inset_x,
1012                                                         y - inset_y, button);
1013                 } else {
1014                         inset_x = cx(bv) - top_x + drawTextXOffset;
1015                         inset_y = cy(bv) + drawTextYOffset;
1016                         ret = inset->insetButtonRelease(bv, x - inset_x,
1017                                                         y - inset_y, button);
1018                         inset->edit(bv, x - inset_x,
1019                                     y - inset_y, button);
1020                 }
1021                 updateLocal(bv, CURSOR_PAR, false);
1022         }
1023         return ret;
1024 }
1025
1026
1027 void InsetText::insetMotionNotify(BufferView * bv, int x, int y, int state)
1028 {
1029         if (no_selection)
1030                 return;
1031         if (the_locking_inset) {
1032                 the_locking_inset->insetMotionNotify(bv, x - inset_x,
1033                                                      y - inset_y,state);
1034                 return;
1035         }
1036         bool clear = false;
1037         if (!lt) {
1038                 lt = getLyXText(bv);
1039                 clear = true;
1040         }
1041         hideInsetCursor(bv);
1042         lt->setCursorFromCoordinates(bv, x - drawTextXOffset, y + insetAscent);
1043         lt->setSelection(bv);
1044         if (lt->toggle_cursor.par() != lt->toggle_end_cursor.par() ||
1045                 lt->toggle_cursor.pos() != lt->toggle_end_cursor.pos())
1046         {
1047                 updateLocal(bv, SELECTION, false);
1048         }
1049         showInsetCursor(bv);
1050         if (clear)
1051                 lt = 0;
1052 }
1053
1054
1055 void InsetText::insetKeyPress(XKeyEvent * xke)
1056 {
1057         if (the_locking_inset) {
1058                 the_locking_inset->insetKeyPress(xke);
1059                 return;
1060         }
1061 }
1062
1063
1064 UpdatableInset::RESULT
1065 InsetText::localDispatch(BufferView * bv,
1066                          kb_action action, string const & arg)
1067 {
1068         bool was_empty = par->size() == 0 && !par->next();
1069         no_selection = false;
1070         UpdatableInset::RESULT
1071                 result= UpdatableInset::localDispatch(bv, action, arg);
1072         if (result != UNDISPATCHED) {
1073                 return DISPATCHED;
1074         }
1075
1076         result = DISPATCHED;
1077         if ((action < 0) && arg.empty())
1078                 return FINISHED;
1079
1080         if (the_locking_inset) {
1081                 result = the_locking_inset->localDispatch(bv, action, arg);
1082                 if (result == DISPATCHED_NOUPDATE)
1083                         return result;
1084                 else if (result == DISPATCHED) {
1085                         updateLocal(bv, CURSOR_PAR, false);
1086                         return result;
1087                 } else if (result >= FINISHED) {
1088                         switch(result) {
1089                         case FINISHED_RIGHT:
1090                                 moveRightIntern(bv, false, false);
1091                                 result = DISPATCHED;
1092                                 break;
1093                         case FINISHED_UP:
1094                                 if ((result = moveUp(bv)) >= FINISHED) {
1095                                         updateLocal(bv, CURSOR, false);
1096                                         bv->unlockInset(this);
1097                                 }
1098                                 break;
1099                         case FINISHED_DOWN:
1100                                 if ((result = moveDown(bv)) >= FINISHED) {
1101                                         updateLocal(bv, CURSOR, false);
1102                                         bv->unlockInset(this);
1103                                 }
1104                                 break;
1105                         default:
1106                                 result = DISPATCHED;
1107                                 break;
1108                         }
1109                         the_locking_inset = 0;
1110 #ifdef WITH_WARNINGS
1111 #warning I changed this to always return Dispatched maybe it is wrong (20011001 Jug)
1112 #endif
1113                         return result;
1114                 }
1115         }
1116         hideInsetCursor(bv);
1117         bool clear = false;
1118         if (!lt) {
1119                 lt = getLyXText(bv);
1120                 clear = true;
1121         }
1122         switch (action) {
1123         // Normal chars
1124         case LFUN_SELFINSERT:
1125                 if (bv->buffer()->isReadonly()) {
1126 //          setErrorMessage(N_("Document is read only"));
1127                         break;
1128                 }
1129                 if (!arg.empty()) {
1130                         /* Automatically delete the currently selected
1131                          * text and replace it with what is being
1132                          * typed in now. Depends on lyxrc settings
1133                          * "auto_region_delete", which defaults to
1134                          * true (on). */
1135
1136                         setUndo(bv, Undo::INSERT,
1137                                 lt->cursor.par(), lt->cursor.par()->next());
1138                         bv->setState();
1139                         if (lyxrc.auto_region_delete) {
1140                                 if (lt->selection.set()) {
1141                                         lt->cutSelection(bv, false);
1142                                 }
1143                         }
1144                         lt->clearSelection();
1145                         for (string::size_type i = 0; i < arg.length(); ++i) {
1146                                 bv->owner()->getIntl()->getTrans().TranslateAndInsert(arg[i], lt);
1147                         }
1148                 }
1149                 lt->selection.cursor = lt->cursor;
1150                 updateLocal(bv, CURSOR_PAR, true);
1151                 result = DISPATCHED_NOUPDATE;
1152                 break;
1153                 // --- Cursor Movements -----------------------------------
1154         case LFUN_RIGHTSEL:
1155                 finishUndo();
1156                 moveRight(bv, false, true);
1157                 lt->setSelection(bv);
1158                 updateLocal(bv, SELECTION, false);
1159                 break;
1160         case LFUN_RIGHT:
1161                 result = moveRight(bv);
1162                 finishUndo();
1163                 updateLocal(bv, CURSOR, false);
1164                 break;
1165         case LFUN_LEFTSEL:
1166                 finishUndo();
1167                 moveLeft(bv, false, true);
1168                 lt->setSelection(bv);
1169                 updateLocal(bv, SELECTION, false);
1170                 break;
1171         case LFUN_LEFT:
1172                 finishUndo();
1173                 result = moveLeft(bv);
1174                 updateLocal(bv, CURSOR, false);
1175                 break;
1176         case LFUN_DOWNSEL:
1177                 finishUndo();
1178                 moveDown(bv);
1179                 lt->setSelection(bv);
1180                 updateLocal(bv, SELECTION, false);
1181                 break;
1182         case LFUN_DOWN:
1183                 finishUndo();
1184                 result = moveDown(bv);
1185                 updateLocal(bv, CURSOR, false);
1186                 break;
1187         case LFUN_UPSEL:
1188                 finishUndo();
1189                 moveUp(bv);
1190                 lt->setSelection(bv);
1191                 updateLocal(bv, SELECTION, false);
1192                 break;
1193         case LFUN_UP:
1194                 finishUndo();
1195                 result = moveUp(bv);
1196                 updateLocal(bv, CURSOR, false);
1197                 break;
1198         case LFUN_HOME:
1199                 finishUndo();
1200                 lt->cursorHome(bv);
1201                 updateLocal(bv, CURSOR, false);
1202                 break;
1203         case LFUN_END:
1204                 lt->cursorEnd(bv);
1205                 updateLocal(bv, CURSOR, false);
1206                 break;
1207         case LFUN_BACKSPACE: {
1208                 setUndo(bv, Undo::DELETE,
1209                         lt->cursor.par(), lt->cursor.par()->next());
1210                 if (lt->selection.set())
1211                         lt->cutSelection(bv);
1212                 else
1213                         lt->backspace(bv);
1214                 updateLocal(bv, CURSOR_PAR, true);
1215         }
1216         break;
1217         
1218         case LFUN_DELETE: {
1219                 setUndo(bv, Undo::DELETE,
1220                         lt->cursor.par(), lt->cursor.par()->next());
1221                 if (lt->selection.set()) {
1222                         lt->cutSelection(bv);
1223                 } else {
1224                         lt->Delete(bv);
1225                 }
1226                 updateLocal(bv, CURSOR_PAR, true);
1227         }
1228         break;
1229         
1230         case LFUN_CUT: {
1231                 setUndo(bv, Undo::DELETE,
1232                         lt->cursor.par(), lt->cursor.par()->next());
1233                 lt->cutSelection(bv);
1234                 updateLocal(bv, CURSOR_PAR, true);
1235         }
1236         break;
1237
1238         case LFUN_COPY:
1239                 finishUndo();
1240                 lt->copySelection(bv);
1241                 updateLocal(bv, CURSOR_PAR, false);
1242                 break;
1243         case LFUN_PASTESELECTION:
1244         {
1245                 string const clip(bv->getClipboard());
1246         
1247                 if (clip.empty())
1248                         break;
1249                 if (arg == "paragraph") {
1250                         lt->insertStringAsParagraphs(bv, clip);
1251                 } else {
1252                         lt->insertStringAsLines(bv, clip);
1253                 }
1254                 updateLocal(bv, CURSOR_PAR, true);
1255                 break;
1256         }
1257         case LFUN_PASTE: {
1258                 if (!autoBreakRows) {
1259
1260                         if (CutAndPaste::nrOfParagraphs() > 1) {
1261                                 Alert::alert(_("Impossible operation"),
1262                                                    _("Cannot include more than one paragraph!"),
1263                                                    _("Sorry."));
1264                                 break;
1265                         }
1266                 }
1267                 setUndo(bv, Undo::INSERT,
1268                         lt->cursor.par(), lt->cursor.par()->next());
1269                 lt->pasteSelection(bv);
1270                 updateLocal(bv, CURSOR_PAR, true);
1271         }
1272         break;
1273
1274         case LFUN_BREAKPARAGRAPH:
1275                 if (!autoBreakRows) {
1276                         result = DISPATCHED;
1277                         break;
1278                 }
1279                 lt->breakParagraph(bv, 0);
1280                 updateLocal(bv, FULL, true);
1281                 break;
1282         case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
1283                 if (!autoBreakRows) {
1284                         result = DISPATCHED;
1285                         break;
1286                 }
1287                 lt->breakParagraph(bv, 1);
1288                 updateLocal(bv, FULL, true);
1289                 break;
1290
1291         case LFUN_BREAKLINE: {
1292                 if (!autoBreakRows) {
1293                         result = DISPATCHED;
1294                         break;
1295                 }
1296                 setUndo(bv, Undo::INSERT,
1297                         lt->cursor.par(), lt->cursor.par()->next());
1298                 lt->insertChar(bv, Paragraph::META_NEWLINE);
1299                 updateLocal(bv, CURSOR_PAR, true);
1300         }
1301         break;
1302
1303         case LFUN_LAYOUT:
1304                 // do not set layouts on non breakable textinsets
1305                 if (autoBreakRows) {
1306                         layout_type cur_layout = cpar(bv)->layout;
1307           
1308                         // Derive layout number from given argument (string)
1309                         // and current buffer's textclass (number). */    
1310                         textclass_type tclass = bv->buffer()->params.textclass;
1311                         std::pair <bool, layout_type> layout = 
1312                                 textclasslist.NumberOfLayout(tclass, arg);
1313
1314                         // If the entry is obsolete, use the new one instead.
1315                         if (layout.first) {
1316                                 string obs = textclasslist.Style(tclass,layout.second).
1317                                         obsoleted_by();
1318                                 if (!obs.empty()) 
1319                                         layout = textclasslist.NumberOfLayout(tclass, obs);
1320                         }
1321
1322                         // see if we found the layout number:
1323                         if (!layout.first) {
1324                                 string const msg = string(N_("Layout ")) + arg + N_(" not known");
1325                                 bv->owner()->getLyXFunc()->dispatch(LFUN_MESSAGE, msg);
1326                                 break;
1327                         }
1328
1329                         if (cur_layout != layout.second) {
1330                                 cur_layout = layout.second;
1331                                 lt->setLayout(bv, layout.second);
1332                                 bv->owner()->setLayout(cpar(bv)->getLayout());
1333                                 updateLocal(bv, CURSOR_PAR, true);
1334                         }
1335                 } else {
1336                         // reset the layout box
1337                         bv->owner()->setLayout(cpar(bv)->getLayout());
1338                 }
1339                 break;
1340         case LFUN_PARAGRAPH_SPACING:
1341                 // This one is absolutely not working. When fiddling with this
1342                 // it also seems to me that the paragraphs inside the insettext
1343                 // inherit bufferparams/paragraphparams in a strange way. (Lgb)
1344         {
1345                 Paragraph * par = lt->cursor.par();
1346                 Spacing::Space cur_spacing = par->params().spacing().getSpace();
1347                 float cur_value = 1.0;
1348                 if (cur_spacing == Spacing::Other) {
1349                         cur_value = par->params().spacing().getValue();
1350                 }
1351                                 
1352                 istringstream istr(arg.c_str());
1353                 string tmp;
1354                 istr >> tmp;
1355                 Spacing::Space new_spacing = cur_spacing;
1356                 float new_value = cur_value;
1357                 if (tmp.empty()) {
1358                         lyxerr << "Missing argument to `paragraph-spacing'"
1359                                    << endl;
1360                 } else if (tmp == "single") {
1361                         new_spacing = Spacing::Single;
1362                 } else if (tmp == "onehalf") {
1363                         new_spacing = Spacing::Onehalf;
1364                 } else if (tmp == "double") {
1365                         new_spacing = Spacing::Double;
1366                 } else if (tmp == "other") {
1367                         new_spacing = Spacing::Other;
1368                         float tmpval = 0.0;
1369                         istr >> tmpval;
1370                         lyxerr << "new_value = " << tmpval << endl;
1371                         if (tmpval != 0.0)
1372                                 new_value = tmpval;
1373                 } else if (tmp == "default") {
1374                         new_spacing = Spacing::Default;
1375                 } else {
1376                         lyxerr << _("Unknown spacing argument: ")
1377                                    << arg << endl;
1378                 }
1379                 if (cur_spacing != new_spacing || cur_value != new_value) {
1380                         par->params().spacing(Spacing(new_spacing, new_value));
1381                         updateLocal(bv, CURSOR_PAR, true);
1382                 }
1383         }
1384         break;
1385         
1386         default:
1387                 if (!bv->Dispatch(action, arg))
1388                         result = UNDISPATCHED;
1389                 break;
1390         }
1391
1392         /// If the action has deleted all text in the inset, we need to change the
1393         // language to the language of the surronding text.
1394         if (!was_empty && par->size() == 0 && !par->next()) {
1395                 LyXFont font(LyXFont::ALL_IGNORE);
1396                 font.setLanguage(bv->getParentLanguage(this));
1397                 setFont(bv, font, false);
1398         }
1399
1400         if (result < FINISHED) {
1401                 showInsetCursor(bv);
1402         } else
1403                 bv->unlockInset(this);
1404         if (clear)
1405                 lt = 0;
1406         return result;
1407 }
1408
1409
1410 int InsetText::latex(Buffer const * buf, ostream & os, bool, bool) const
1411 {
1412         TexRow texrow;
1413         buf->latexParagraphs(os, par, 0, texrow);
1414         return texrow.rows();
1415 }
1416
1417
1418 int InsetText::ascii(Buffer const * buf, ostream & os, int linelen) const
1419 {
1420         Paragraph * p = par;
1421         unsigned int lines = 0;
1422         
1423         while (p) {
1424                 string const tmp = buf->asciiParagraph(p, linelen);
1425                 lines += countChar(tmp, '\n');
1426                 os << tmp;
1427                 p = p->next();
1428         }
1429         return lines;
1430 }
1431
1432
1433 int InsetText::docbook(Buffer const * buf, ostream & os) const
1434 {
1435         Paragraph * p = par;
1436         unsigned int lines = 0;
1437
1438         vector<string> environment_stack(10);
1439         vector<string> environment_inner(10);
1440         
1441         int const command_depth = 0;
1442         string item_name;
1443         
1444         Paragraph::depth_type depth = 0; // paragraph depth
1445
1446         while (p) {
1447                 string sgmlparam;
1448                 int desc_on = 0; // description mode
1449
1450                 LyXLayout const & style =
1451                         textclasslist.Style(buf->params.textclass,
1452                                             p->layout);
1453
1454                 // environment tag closing
1455                 for (; depth > p->params().depth(); --depth) {
1456                         if (environment_inner[depth] != "!-- --") {
1457                                 item_name = "listitem";
1458                                 buf->sgmlCloseTag(os, command_depth + depth,
1459                                              item_name);
1460                                 if (environment_inner[depth] == "varlistentry")
1461                                         buf->sgmlCloseTag(os, depth+command_depth,
1462                                                      environment_inner[depth]);
1463                         }
1464                         buf->sgmlCloseTag(os, depth + command_depth,
1465                                      environment_stack[depth]);
1466                         environment_stack[depth].erase();
1467                         environment_inner[depth].erase();
1468                 }
1469
1470                 if (depth == p->params().depth()
1471                    && environment_stack[depth] != style.latexname()
1472                    && !environment_stack[depth].empty()) {
1473                         if (environment_inner[depth] != "!-- --") {
1474                                 item_name= "listitem";
1475                                 buf->sgmlCloseTag(os, command_depth+depth,
1476                                                   item_name);
1477                                 if (environment_inner[depth] == "varlistentry")
1478                                         buf->sgmlCloseTag(os,
1479                                                           depth + command_depth,
1480                                                           environment_inner[depth]);
1481                         }
1482                         
1483                         buf->sgmlCloseTag(os, depth + command_depth,
1484                                           environment_stack[depth]);
1485                         
1486                         environment_stack[depth].erase();
1487                         environment_inner[depth].erase();
1488                 }
1489
1490                 // Write opening SGML tags.
1491                 switch (style.latextype) {
1492                 case LATEX_PARAGRAPH:
1493                         buf->sgmlOpenTag(os, depth + command_depth,
1494                                          style.latexname());
1495                         break;
1496
1497                 case LATEX_COMMAND:
1498                         buf->sgmlError(p, 0,
1499                                        _("Error : LatexType Command not allowed here.\n"));
1500                         return -1;
1501                         break;
1502
1503                 case LATEX_ENVIRONMENT:
1504                 case LATEX_ITEM_ENVIRONMENT:
1505                         if (depth < p->params().depth()) {
1506                                 depth = p->params().depth();
1507                                 environment_stack[depth].erase();
1508                         }
1509
1510                         if (environment_stack[depth] != style.latexname()) {
1511                                 if(environment_stack.size() == depth + 1) {
1512                                         environment_stack.push_back("!-- --");
1513                                         environment_inner.push_back("!-- --");
1514                                 }
1515                                 environment_stack[depth] = style.latexname();
1516                                 environment_inner[depth] = "!-- --";
1517                                 buf->sgmlOpenTag(os, depth + command_depth,
1518                                                  environment_stack[depth]);
1519                         } else {
1520                                 if (environment_inner[depth] != "!-- --") {
1521                                         item_name= "listitem";
1522                                         buf->sgmlCloseTag(os,
1523                                                           command_depth + depth,
1524                                                           item_name);
1525                                         if (environment_inner[depth] == "varlistentry")
1526                                                 buf->sgmlCloseTag(os,
1527                                                                   depth + command_depth,
1528                                                                   environment_inner[depth]);
1529                                 }
1530                         }
1531                         
1532                         if (style.latextype == LATEX_ENVIRONMENT) {
1533                                 if (!style.latexparam().empty()) {
1534                                         if(style.latexparam() == "CDATA")
1535                                                 os << "<![CDATA[";
1536                                         else
1537                                                 buf->sgmlOpenTag(os, depth + command_depth,
1538                                                                  style.latexparam());
1539                                 }
1540                                 break;
1541                         }
1542
1543                         desc_on = (style.labeltype == LABEL_MANUAL);
1544
1545                         if (desc_on)
1546                                 environment_inner[depth]= "varlistentry";
1547                         else
1548                                 environment_inner[depth]= "listitem";
1549
1550                         buf->sgmlOpenTag(os, depth + 1 + command_depth,
1551                                          environment_inner[depth]);
1552
1553                         if (desc_on) {
1554                                 item_name= "term";
1555                                 buf->sgmlOpenTag(os, depth + 1 + command_depth,
1556                                                  item_name);
1557                         } else {
1558                                 item_name= "para";
1559                                 buf->sgmlOpenTag(os, depth + 1 + command_depth,
1560                                                  item_name);
1561                         }
1562                         break;
1563                 default:
1564                         buf->sgmlOpenTag(os, depth + command_depth,
1565                                          style.latexname());
1566                         break;
1567                 }
1568
1569                 buf->simpleDocBookOnePar(os, p, desc_on,
1570                                          depth + 1 + command_depth);
1571                 p = p->next();
1572
1573                 string end_tag;
1574                 // write closing SGML tags
1575                 switch (style.latextype) {
1576                 case LATEX_ENVIRONMENT:
1577                         if (!style.latexparam().empty()) {
1578                                 if(style.latexparam() == "CDATA")
1579                                         os << "]]>";
1580                                 else
1581                                         buf->sgmlCloseTag(os, depth + command_depth,
1582                                                           style.latexparam());
1583                         }
1584                         break;
1585                 case LATEX_ITEM_ENVIRONMENT:
1586                         if (desc_on == 1) break;
1587                         end_tag= "para";
1588                         buf->sgmlCloseTag(os, depth + 1 + command_depth, end_tag);
1589                         break;
1590                 case LATEX_PARAGRAPH:
1591                         buf->sgmlCloseTag(os, depth + command_depth, style.latexname());
1592                         break;
1593                 default:
1594                         buf->sgmlCloseTag(os, depth + command_depth, style.latexname());
1595                         break;
1596                 }
1597         }
1598
1599         // Close open tags
1600         for (int d = depth; d >= 0; --d) {
1601                 if (!environment_stack[depth].empty()) {
1602                         if (environment_inner[depth] != "!-- --") {
1603                                 item_name = "listitem";
1604                                 buf->sgmlCloseTag(os, command_depth + depth,
1605                                                   item_name);
1606                                if (environment_inner[depth] == "varlistentry")
1607                                        buf->sgmlCloseTag(os, depth + command_depth,
1608                                                          environment_inner[depth]);
1609                         }
1610                         
1611                         buf->sgmlCloseTag(os, depth + command_depth,
1612                                           environment_stack[depth]);
1613                 }
1614         }
1615         
1616         return lines;
1617 }
1618
1619
1620 void InsetText::validate(LaTeXFeatures & features) const
1621 {
1622         Paragraph * p = par;
1623         while (p) {
1624                 p->validate(features);
1625                 p = p->next();
1626         }
1627 }
1628
1629
1630 int InsetText::beginningOfMainBody(Buffer const * buf, Paragraph * p) const
1631 {
1632         if (textclasslist.Style(buf->params.textclass,
1633                                 p->getLayout()).labeltype != LABEL_MANUAL)
1634                 return 0;
1635         else
1636                 return p->beginningOfMainBody();
1637 }
1638
1639
1640 void InsetText::getCursorPos(BufferView * bv,
1641                              int & x, int & y) const
1642 {
1643         if (the_locking_inset) {
1644                 the_locking_inset->getCursorPos(bv, x, y);
1645                 return;
1646         }
1647         x = cx(bv);
1648         y = cy(bv);
1649 }
1650
1651
1652 unsigned int InsetText::insetInInsetY()
1653 {
1654         if (!the_locking_inset)
1655                 return 0;
1656
1657         return (inset_y + the_locking_inset->insetInInsetY());
1658 }
1659
1660
1661 void InsetText::toggleInsetCursor(BufferView * bv)
1662 {
1663         if (the_locking_inset) {
1664                 the_locking_inset->toggleInsetCursor(bv);
1665                 return;
1666         }
1667
1668         LyXFont const font(getLyXText(bv)->getFont(bv->buffer(), cpar(bv), cpos(bv)));
1669
1670         int const asc = lyxfont::maxAscent(font);
1671         int const desc = lyxfont::maxDescent(font);
1672   
1673         if (isCursorVisible())
1674                 bv->hideLockedInsetCursor();
1675         else
1676                 bv->showLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1677         toggleCursorVisible();
1678 }
1679
1680
1681 void InsetText::showInsetCursor(BufferView * bv, bool show)
1682 {
1683         if (the_locking_inset) {
1684                 the_locking_inset->showInsetCursor(bv, show);
1685                 return;
1686         }
1687         if (!isCursorVisible()) {
1688                 LyXFont const font =
1689                         getLyXText(bv)->getFont(bv->buffer(), cpar(bv), cpos(bv));
1690         
1691                 int const asc = lyxfont::maxAscent(font);
1692                 int const desc = lyxfont::maxDescent(font);
1693
1694                 bv->fitLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1695                 if (show)
1696                         bv->showLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1697                 setCursorVisible(true);
1698         }
1699 }
1700
1701
1702 void InsetText::hideInsetCursor(BufferView * bv)
1703 {
1704         if (isCursorVisible()) {
1705                 bv->hideLockedInsetCursor();
1706                 setCursorVisible(false);
1707         }
1708         if (the_locking_inset)
1709                 the_locking_inset->hideInsetCursor(bv);
1710 }
1711
1712
1713 void InsetText::fitInsetCursor(BufferView * bv) const
1714 {
1715         if (the_locking_inset) {
1716                 the_locking_inset->fitInsetCursor(bv);
1717                 return;
1718         }
1719         LyXFont const font =
1720                 getLyXText(bv)->getFont(bv->buffer(), cpar(bv), cpos(bv));
1721         
1722         int const asc = lyxfont::maxAscent(font);
1723         int const desc = lyxfont::maxDescent(font);
1724
1725         bv->fitLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1726 }
1727
1728
1729 UpdatableInset::RESULT
1730 InsetText::moveRight(BufferView * bv, bool activate_inset, bool selecting)
1731 {
1732         if (getLyXText(bv)->cursor.par()->isRightToLeftPar(bv->buffer()->params))
1733                 return moveLeftIntern(bv, false, activate_inset, selecting);
1734         else
1735                 return moveRightIntern(bv, false, activate_inset, selecting);
1736 }
1737
1738
1739 UpdatableInset::RESULT
1740 InsetText::moveLeft(BufferView * bv, bool activate_inset, bool selecting)
1741 {
1742         if (getLyXText(bv)->cursor.par()->isRightToLeftPar(bv->buffer()->params))
1743                 return moveRightIntern(bv, true, activate_inset, selecting);
1744         else
1745                 return moveLeftIntern(bv, true, activate_inset, selecting);
1746 }
1747
1748
1749 UpdatableInset::RESULT
1750 InsetText::moveRightIntern(BufferView * bv, bool behind, 
1751                            bool activate_inset, bool selecting)
1752 {
1753         if (!cpar(bv)->next() && (cpos(bv) >= cpar(bv)->size()))
1754                 return FINISHED_RIGHT;
1755         if (activate_inset && checkAndActivateInset(bv, behind))
1756                 return DISPATCHED;
1757         getLyXText(bv)->cursorRight(bv);
1758         if (!selecting)
1759                 getLyXText(bv)->selection.cursor = getLyXText(bv)->cursor;
1760         return DISPATCHED_NOUPDATE;
1761 }
1762
1763
1764 UpdatableInset::RESULT
1765 InsetText::moveLeftIntern(BufferView * bv, bool behind,
1766                           bool activate_inset, bool selecting)
1767 {
1768         if (!cpar(bv)->previous() && (cpos(bv) <= 0))
1769                 return FINISHED;
1770         getLyXText(bv)->cursorLeft(bv);
1771         if (!selecting)
1772                 getLyXText(bv)->selection.cursor = getLyXText(bv)->cursor;
1773         if (activate_inset && checkAndActivateInset(bv, behind))
1774                 return DISPATCHED;
1775         return DISPATCHED_NOUPDATE;
1776 }
1777
1778
1779 UpdatableInset::RESULT
1780 InsetText::moveUp(BufferView * bv)
1781 {
1782         if (!crow(bv)->previous())
1783                 return FINISHED_UP;
1784         getLyXText(bv)->cursorUp(bv);
1785         return DISPATCHED_NOUPDATE;
1786 }
1787
1788
1789 UpdatableInset::RESULT
1790 InsetText::moveDown(BufferView * bv)
1791 {
1792         if (!crow(bv)->next())
1793                 return FINISHED_DOWN;
1794         getLyXText(bv)->cursorDown(bv);
1795         return DISPATCHED_NOUPDATE;
1796 }
1797
1798
1799 bool InsetText::insertInset(BufferView * bv, Inset * inset)
1800 {
1801         if (the_locking_inset) {
1802                 if (the_locking_inset->insetAllowed(inset))
1803                         return the_locking_inset->insertInset(bv, inset);
1804                 return false;
1805         }
1806         inset->setOwner(this);
1807         hideInsetCursor(bv);
1808
1809         bool clear = false;
1810         if (!lt) {
1811                 lt = getLyXText(bv);
1812                 clear = true;
1813         }
1814         lt->insertInset(bv, inset);
1815 #if 0
1816         if ((!cpar(bv)->isInset(cpos(bv))) ||
1817                 (cpar(bv)->getInset(cpos(bv)) != inset))
1818                 lt->cursorLeft(bv);
1819 #endif
1820         bv->fitCursor();
1821         updateLocal(bv, CURSOR_PAR|CURSOR, true);
1822 //      showInsetCursor(bv);
1823         if (clear)
1824                 lt = 0;
1825         return true;
1826 }
1827
1828
1829 bool InsetText::insetAllowed(Inset::Code code) const
1830 {
1831         // in_insetAllowed is a really gross hack,
1832         // to allow us to call the owner's insetAllowed
1833         // without stack overflow, which can happen
1834         // when the owner uses InsetCollapsable::insetAllowed()
1835         bool ret = true;
1836         if (in_insetAllowed)
1837                 return ret;
1838         in_insetAllowed = true;
1839         if (the_locking_inset)
1840                 ret = the_locking_inset->insetAllowed(code);
1841         else if (owner())
1842                 ret = owner()->insetAllowed(code);
1843         in_insetAllowed = false;
1844         return ret;
1845 }
1846
1847
1848 UpdatableInset * InsetText::getLockingInset() const
1849 {
1850         return the_locking_inset ? the_locking_inset->getLockingInset() :
1851                 const_cast<InsetText *>(this);
1852 }
1853
1854
1855 UpdatableInset * InsetText::getFirstLockingInsetOfType(Inset::Code c)
1856 {
1857         if (c == lyxCode())
1858                 return this;
1859         if (the_locking_inset)
1860                 return the_locking_inset->getFirstLockingInsetOfType(c);
1861         return 0;
1862 }
1863
1864
1865 bool InsetText::showInsetDialog(BufferView * bv) const
1866 {
1867         if (the_locking_inset)
1868                 return the_locking_inset->showInsetDialog(bv);
1869         return false;
1870 }
1871
1872
1873 std::vector<string> const InsetText::getLabelList() const 
1874 {
1875         std::vector<string> label_list;
1876
1877         Paragraph * tpar = par;
1878         while (tpar) {
1879                 Paragraph::inset_iterator beg = tpar->inset_iterator_begin();
1880                 Paragraph::inset_iterator end = tpar->inset_iterator_end();
1881                 for (; beg != end; ++beg) {
1882                         std::vector<string> const l = (*beg)->getLabelList();
1883                         label_list.insert(label_list.end(), l.begin(), l.end());
1884                 }
1885                 tpar = tpar->next();
1886         }
1887         return label_list;
1888 }
1889
1890
1891 void InsetText::setFont(BufferView * bv, LyXFont const & font, bool toggleall,
1892                         bool selectall)
1893 {
1894         if (the_locking_inset) {
1895                 the_locking_inset->setFont(bv, font, toggleall, selectall);
1896                 return;
1897         }
1898         if ((!par->next() && !par->size()) || !cpar(bv)->size()) {
1899                 getLyXText(bv)->setFont(bv, font, toggleall);
1900                 return;
1901         }
1902         bool clear = false;
1903         if (!lt) {
1904                 lt = getLyXText(bv);
1905                 clear = true;
1906         }
1907         if (lt->selection.set()) {
1908                 setUndo(bv, Undo::EDIT, lt->cursor.par(), lt->cursor.par()->next());
1909         }
1910         if (selectall)
1911                 selectAll(bv);
1912         lt->toggleFree(bv, font, toggleall);
1913         if (selectall)
1914                 lt->clearSelection();
1915         bv->fitCursor();
1916         if (selectall || lt->selection.set())
1917                 updateLocal(bv, FULL, true);
1918         else
1919                 updateLocal(bv, CURSOR_PAR, true);
1920         if (clear)
1921                 lt = 0;
1922 }
1923
1924
1925 bool InsetText::checkAndActivateInset(BufferView * bv, bool behind)
1926 {
1927         if (cpar(bv)->isInset(cpos(bv))) {
1928                 unsigned int x;
1929                 unsigned int y;
1930                 Inset * inset =
1931                         static_cast<UpdatableInset*>(cpar(bv)->getInset(cpos(bv)));
1932                 if (!isHighlyEditableInset(inset))
1933                         return false;
1934                 LyXFont const font =
1935                         getLyXText(bv)->getFont(bv->buffer(), cpar(bv), cpos(bv));
1936                 if (behind) {
1937                         x = inset->width(bv, font);
1938                         y = font.isRightToLeft() ? 0 : inset->descent(bv, font);
1939                 } else {
1940                         x = 0;
1941                         y = font.isRightToLeft() ? inset->descent(bv, font) : 0;
1942                 }
1943                 //inset_x = cx(bv) - top_x + drawTextXOffset;
1944                 //inset_y = cy(bv) + drawTextYOffset;
1945                 inset->edit(bv, x, y, 0);
1946                 if (!the_locking_inset)
1947                         return false;
1948                 updateLocal(bv, CURSOR, false);
1949                 return true;
1950         }
1951         return false;
1952 }
1953
1954
1955 bool InsetText::checkAndActivateInset(BufferView * bv, int x, int y,
1956                                       int button)
1957 {
1958         x -= drawTextXOffset;
1959         int dummyx = x;
1960         int dummyy = y + insetAscent;
1961         Inset * inset = bv->checkInsetHit(getLyXText(bv), dummyx, dummyy, button);
1962
1963         if (inset) {
1964                 if (x < 0)
1965                         x = insetWidth;
1966                 if (y < 0)
1967                         y = insetDescent;
1968                 inset_x = cx(bv) - top_x + drawTextXOffset;
1969                 inset_y = cy(bv) + drawTextYOffset;
1970                 inset->edit(bv, x - inset_x, y - inset_y, button);
1971                 if (!the_locking_inset)
1972                         return false;
1973                 updateLocal(bv, CURSOR, false);
1974                 return true;
1975         }
1976         return false;
1977 }
1978
1979
1980 int InsetText::getMaxWidth(BufferView * bv, UpdatableInset const * inset) const
1981 {
1982 #if 0
1983         int w = UpdatableInset::getMaxWidth(bv, inset);
1984         if (w < 0) {
1985                 return -1;
1986         }
1987         if (owner()) {
1988                 w = w - top_x + owner()->x();
1989                 return w;
1990         }
1991         w -= (2 * TEXT_TO_INSET_OFFSET);
1992         return w - top_x;
1993 #else
1994         return UpdatableInset::getMaxWidth(bv, inset);
1995 #endif
1996 }
1997
1998
1999 void InsetText::setParagraphData(Paragraph * p, bool same_id)
2000 {
2001         // we have to unlock any locked inset otherwise we're in troubles
2002         the_locking_inset = 0;
2003         while (par) {
2004                 Paragraph * tmp = par->next();
2005                 delete par;
2006                 par = tmp;
2007         }
2008
2009         par = new Paragraph(*p, same_id);
2010         par->setInsetOwner(this);
2011         Paragraph * np = par;
2012         while (p->next()) {
2013                 p = p->next();
2014                 np->next(new Paragraph(*p, same_id));
2015                 np->next()->previous(np);
2016                 np = np->next();
2017                 np->setInsetOwner(this);
2018         }
2019         reinitLyXText();
2020         need_update = INIT;
2021 }
2022
2023
2024 void InsetText::setText(string const & data)
2025 {
2026         clear();
2027         LyXFont font(LyXFont::ALL_SANE);
2028         for (unsigned int i=0; i < data.length(); ++i)
2029                 par->insertChar(i, data[i], font);
2030 }
2031
2032
2033 void InsetText::setAutoBreakRows(bool flag)
2034 {
2035         if (flag != autoBreakRows) {
2036                 autoBreakRows = flag;
2037                 if (!flag)
2038                         removeNewlines();
2039                 need_update = INIT;
2040         }
2041 }
2042
2043
2044 void InsetText::setDrawFrame(BufferView * bv, DrawFrame how)
2045 {
2046         if (how != drawFrame_) {
2047                 drawFrame_ = how;
2048                 if (bv)
2049                         updateLocal(bv, DRAW_FRAME, false);
2050         }
2051 }
2052
2053
2054 void InsetText::setFrameColor(BufferView * bv, LColor::color col)
2055 {
2056         if (frame_color != col) {
2057                 frame_color = col;
2058                 if (bv)
2059                         updateLocal(bv, DRAW_FRAME, false);
2060         }
2061 }
2062
2063
2064 int InsetText::cx(BufferView * bv) const
2065 {
2066         bool clear = false;
2067         if (!lt) {
2068                 lt = getLyXText(bv);
2069                 clear = true;
2070         }
2071         int x = lt->cursor.x() + top_x + TEXT_TO_INSET_OFFSET;
2072         if (the_locking_inset) {
2073                 LyXFont font = lt->getFont(bv->buffer(),
2074                                              lt->cursor.par(),
2075                                              lt->cursor.pos());
2076                 if (font.isVisibleRightToLeft())
2077                         x -= the_locking_inset->width(bv, font);
2078         }
2079         if (clear)
2080                 lt = 0;
2081         return x;
2082 }
2083
2084
2085 int InsetText::cy(BufferView * bv) const
2086 {
2087         LyXFont font;
2088         return getLyXText(bv)->cursor.y() - ascent(bv, font) + TEXT_TO_INSET_OFFSET;
2089 }
2090
2091
2092 pos_type InsetText::cpos(BufferView * bv) const
2093 {
2094         return getLyXText(bv)->cursor.pos();
2095 }
2096
2097
2098 Paragraph * InsetText::cpar(BufferView * bv) const
2099 {
2100         return getLyXText(bv)->cursor.par();
2101 }
2102
2103
2104 bool InsetText::cboundary(BufferView * bv) const
2105 {
2106         return getLyXText(bv)->cursor.boundary();
2107 }
2108
2109
2110 Row * InsetText::crow(BufferView * bv) const
2111 {
2112         return getLyXText(bv)->cursor.row();
2113 }
2114
2115
2116 LyXText * InsetText::getLyXText(BufferView const * lbv,
2117                                 bool const recursive) const
2118 {
2119         if (!recursive && (cached_bview == lbv)) {
2120                 LyXText * lt = cached_text.get();
2121                 lyx::Assert(lt && lt->firstRow()->par() == par);
2122                 return cached_text.get();
2123         }
2124         
2125         // Super UGLY! (Lgb)
2126         BufferView * bv = const_cast<BufferView *>(lbv);
2127         
2128         cached_bview = bv;
2129         Cache::iterator it = cache.find(bv);
2130
2131         if (it != cache.end()) {
2132                 if (do_reinit)
2133                         reinitLyXText();
2134                 else if (do_resize)
2135                         resizeLyXText(do_resize);
2136                 if (lt || !it->second.remove) {
2137                         lyx::Assert(it->second.text.get());
2138                         cached_text = it->second.text;
2139                         if (recursive && the_locking_inset) {
2140                                 return the_locking_inset->getLyXText(bv, true);
2141                         }
2142                         return cached_text.get();
2143                 } else if (it->second.remove) {
2144                         if (locked) {
2145                                 saveLyXTextState(it->second.text.get());
2146                         } else {
2147                                 sstate.lpar = 0;
2148                         }
2149                 }
2150         }
2151         
2152         cached_text.reset(new LyXText(const_cast<InsetText *>(this)));
2153         cached_text->init(bv);
2154         restoreLyXTextState(bv, cached_text.get());
2155
2156         cache.insert(make_pair(bv, cached_text));
2157         
2158         if (the_locking_inset && recursive) {
2159                 return the_locking_inset->getLyXText(bv);
2160         }
2161         return cached_text.get();
2162 }
2163
2164
2165 void InsetText::deleteLyXText(BufferView * bv, bool recursive) const
2166 {
2167         cached_bview = 0;
2168
2169         Cache::iterator it = cache.find(bv);
2170         
2171         if (it == cache.end()) {
2172                 return;
2173         }
2174
2175         lyx::Assert(it->second.text.get());
2176
2177         it->second.remove = true;
2178         if (recursive) {
2179                 /// then remove all LyXText in text-insets
2180                 Paragraph * p = par;
2181                 for (; p; p = p->next()) {
2182                         p->deleteInsetsLyXText(bv);
2183                 }
2184         }
2185 }
2186
2187
2188 void InsetText::resizeLyXText(BufferView * bv, bool force) const
2189 {
2190         if (lt) {
2191                 // we cannot resize this because we are in use!
2192                 // so do this on the next possible getLyXText()
2193                 do_resize = bv;
2194                 return;
2195         }
2196         do_resize = 0;
2197 //      lyxerr << "InsetText::resizeLyXText\n";
2198         if (!par->next() && !par->size()) // no data, resize not neccessary!
2199                 return;
2200         // one endless line, resize normally not necessary
2201         if (!force && getMaxWidth(bv, this) < 0)
2202                 return;
2203
2204         Cache::iterator it = cache.find(bv);
2205         if (it == cache.end()) {
2206                 return;
2207         }
2208         lyx::Assert(it->second.text.get());
2209
2210         LyXText * t = it->second.text.get();
2211         saveLyXTextState(t);
2212         for (Paragraph * p = par; p; p = p->next()) {
2213                 p->resizeInsetsLyXText(bv);
2214         }
2215         t->init(bv, true);
2216         restoreLyXTextState(bv, t);
2217         if (the_locking_inset) {
2218                 inset_x = cx(bv) - top_x + drawTextXOffset;
2219                 inset_y = cy(bv) + drawTextYOffset;
2220         }
2221
2222         if (bv->screen()) {
2223                 t->first = bv->screen()->topCursorVisible(t);
2224         }
2225         if (!owner()) {
2226                 updateLocal(bv, FULL, false);
2227                 // this will scroll the screen such that the cursor becomes visible 
2228                 bv->updateScrollbar();
2229         } else {
2230                 need_update |= FULL;
2231         }
2232 }
2233
2234
2235 void InsetText::reinitLyXText() const
2236 {
2237         if (lt) {
2238                 // we cannot resize this because we are in use!
2239                 // so do this on the next possible getLyXText()
2240                 do_reinit = true;
2241                 return;
2242         }
2243         do_reinit = false;
2244         do_resize = 0;
2245 //      lyxerr << "InsetText::reinitLyXText\n";
2246         for(Cache::iterator it = cache.begin(); it != cache.end(); ++it) {
2247                 lyx::Assert(it->second.text.get());
2248
2249                 LyXText * t = it->second.text.get();
2250                 BufferView * bv = it->first;
2251
2252                 saveLyXTextState(t);
2253                 for (Paragraph * p = par; p; p = p->next()) {
2254                         p->resizeInsetsLyXText(bv);
2255                 }
2256                 t->init(bv, true);
2257                 restoreLyXTextState(bv, t);
2258                 if (the_locking_inset) {
2259                         inset_x = cx(bv) - top_x + drawTextXOffset;
2260                         inset_y = cy(bv) + drawTextYOffset;
2261                 }
2262                 if (bv->screen()) {
2263                         t->first = bv->screen()->topCursorVisible(t);
2264                 }
2265                 if (!owner()) {
2266                         updateLocal(bv, FULL, false);
2267                         // this will scroll the screen such that the cursor becomes visible 
2268                         bv->updateScrollbar();
2269                 } else {
2270                         need_update = FULL;
2271                 }
2272         }
2273 }
2274
2275
2276 void InsetText::removeNewlines()
2277 {
2278         bool changed = false;
2279         
2280         for (Paragraph * p = par; p; p = p->next()) {
2281                 for (int i = 0; i < p->size(); ++i) {
2282                         if (p->getChar(i) == Paragraph::META_NEWLINE) {
2283                                 changed = true;
2284                                 p->erase(i);
2285                         }
2286                 }
2287         }
2288         if (changed)
2289                 reinitLyXText();
2290 }
2291
2292
2293 bool InsetText::nodraw() const
2294 {
2295         if (the_locking_inset)
2296                 return the_locking_inset->nodraw();
2297         return UpdatableInset::nodraw();
2298 }
2299
2300
2301 int InsetText::scroll(bool recursive) const
2302 {
2303         int sx = UpdatableInset::scroll(false);
2304
2305         if (recursive && the_locking_inset)
2306                 sx += the_locking_inset->scroll(recursive);
2307
2308         return sx;
2309 }
2310
2311
2312 bool InsetText::doClearArea() const
2313 {
2314         return !locked || (need_update & (FULL|INIT));
2315 }
2316
2317
2318 void InsetText::selectAll(BufferView * bv)
2319 {
2320         getLyXText(bv)->cursorTop(bv);
2321         getLyXText(bv)->selection.cursor = getLyXText(bv)->cursor;
2322         getLyXText(bv)->cursorBottom(bv);
2323         getLyXText(bv)->setSelection(bv);
2324 }
2325
2326
2327 void InsetText::clearSelection(BufferView * bv)
2328 {
2329         getLyXText(bv)->clearSelection();
2330 }
2331
2332
2333 void InsetText::clearInset(BufferView * bv, int baseline, bool & cleared) const
2334 {
2335         LyXFont dummy;
2336         Painter & pain = bv->painter();
2337         int w = insetWidth;
2338         int h = ascent(bv, dummy) + descent(bv, dummy);
2339         int ty = baseline - insetAscent;
2340         
2341         if (ty < 0) {
2342                 h += ty;
2343                 ty = 0;
2344         }
2345         if ((ty + h) > pain.paperHeight())
2346                 h = pain.paperHeight();
2347         if ((top_x + drawTextXOffset + w) > pain.paperWidth())
2348                 w = pain.paperWidth();
2349 //      w -= TEXT_TO_INSET_OFFSET;
2350         pain.fillRectangle(top_x, ty, w+1, h+1, backgroundColor());
2351         cleared = true;
2352         need_update = FULL;
2353         frame_is_visible = false;
2354 }
2355
2356
2357 Paragraph * InsetText::getParFromID(int id) const
2358 {
2359 #if 0
2360         Paragraph * result = par;
2361         Paragraph * ires = 0;
2362         while (result && result->id() != id) {
2363                 if ((ires = result->getParFromID(id)))
2364                         return ires;
2365                 result = result->next();
2366         }
2367         return result;
2368 #else
2369         Paragraph * tmp = par;
2370         while (tmp) {
2371                 if (tmp->id() == id) {
2372                         return tmp;
2373                 }
2374                 Paragraph * tmp2 = tmp->getParFromID(id);
2375                 if (tmp2 != 0) {
2376                         return tmp2;
2377                 }
2378                 tmp = tmp->next();
2379         }
2380         return 0;
2381 #endif
2382 }
2383
2384
2385 Paragraph * InsetText::firstParagraph() const
2386 {
2387         Paragraph * result;
2388         if (the_locking_inset)
2389                 if ((result = the_locking_inset->firstParagraph()))
2390                         return result;
2391         return par;
2392 }
2393
2394
2395 Paragraph * InsetText::getFirstParagraph(int i) const
2396 {
2397         return (i == 0) ? par : 0;
2398 }
2399
2400
2401 LyXCursor const & InsetText::cursor(BufferView * bv) const
2402 {
2403                 if (the_locking_inset)
2404                                 return the_locking_inset->cursor(bv);
2405                 return getLyXText(bv)->cursor;
2406 }
2407
2408
2409 Paragraph * InsetText::paragraph() const
2410 {
2411         return par;
2412 }
2413
2414
2415 void InsetText::paragraph(Paragraph * p)
2416 {
2417         // GENERAL COMMENT: We don't have to free the old paragraphs as the
2418         // caller of this function has to take care of it. This IS important
2419         // as we could have to insert a paragraph before this one and just
2420         // link the actual to a new ones next and set it with this function
2421         // and are done!
2422         par = p;
2423         // set ourself as owner for all the paragraphs inserted!
2424         Paragraph * np = par;
2425         while (np) {
2426                 np->setInsetOwner(this);
2427                 np = np->next();
2428         }
2429         reinitLyXText();
2430         // redraw myself when asked for
2431         need_update = INIT;
2432 }
2433
2434
2435 Inset * InsetText::getInsetFromID(int id_arg) const
2436 {
2437         if (id_arg == id())
2438                 return const_cast<InsetText *>(this);
2439
2440         Paragraph * lp = par;
2441
2442         while (lp) {
2443                 for (Paragraph::inset_iterator it = lp->inset_iterator_begin(),
2444                          en = lp->inset_iterator_end();
2445                          it != en; ++it)
2446                 {
2447                         if ((*it)->id() == id_arg)
2448                                 return *it;
2449                         Inset * in = (*it)->getInsetFromID(id_arg);
2450                         if (in)
2451                                 return in;
2452                 }
2453                 lp = lp->next();
2454         }
2455         return 0;
2456 }
2457
2458
2459 string const InsetText::selectNextWordToSpellcheck(BufferView * bv, float & value) const
2460 {
2461         bool clear = false;
2462         string str;
2463
2464         if (!lt) {
2465                 lt = getLyXText(bv);
2466                 clear = true;
2467         }
2468         if (the_locking_inset) {
2469                 str = the_locking_inset->selectNextWordToSpellcheck(bv, value);
2470                 if (!str.empty()) {
2471                         value += cy(bv);
2472                         if (clear)
2473                                 lt = 0;
2474                         return str;
2475                 }
2476 #warning Dekel please have a look on this one RTL? (Jug)
2477 #warning DEKEL!
2478                 // we have to go on checking so move cusor to the right
2479                 lt->cursor.pos(lt->cursor.pos() + 1);
2480         }
2481         str = lt->selectNextWordToSpellcheck(bv, value);
2482         if (str.empty())
2483                 bv->unlockInset(const_cast<InsetText *>(this));
2484         else
2485                 value = cy(bv);
2486         if (clear)
2487                 lt = 0;
2488         return str;
2489 }
2490
2491
2492 void InsetText::selectSelectedWord(BufferView * bv)
2493 {
2494         if (the_locking_inset) {
2495                 the_locking_inset->selectSelectedWord(bv);
2496                 return;
2497         }
2498         getLyXText(bv)->selectSelectedWord(bv);
2499         updateLocal(bv, SELECTION, false);
2500 }
2501
2502
2503 void InsetText::toggleSelection(BufferView * bv, bool kill_selection)
2504 {
2505         if (the_locking_inset) {
2506                 the_locking_inset->toggleSelection(bv, kill_selection);
2507         }
2508         bool clear = false;
2509         if (!lt) {
2510                 lt = getLyXText(bv);
2511                 clear = true;
2512         }
2513
2514         int x = top_x + TEXT_TO_INSET_OFFSET;
2515
2516         Row * row = lt->firstRow();
2517         int y_offset = top_baseline - row->ascent_of_text();
2518         int y = y_offset;
2519         while ((row != 0) && ((y+row->height()) <= 0)) {
2520                 y += row->height();
2521                 row = row->next();
2522         }
2523         if (y_offset < 0)
2524                 y_offset = y;
2525         
2526         if (need_update & SELECTION)
2527                 need_update = NONE;
2528         bv->screen()->toggleSelection(lt, bv, kill_selection, y_offset, x);
2529         if (clear)
2530                 lt = 0;
2531 }
2532
2533
2534 bool InsetText::searchForward(BufferView * bv, string const & str,
2535                               bool const & cs, bool const & mw)
2536 {
2537         if (the_locking_inset) {
2538                 if (the_locking_inset->searchForward(bv, str, cs, mw))
2539                         return true;
2540                 bool clear = false;
2541                 if (!lt) {
2542                         lt = getLyXText(bv);
2543                         clear = true;
2544                 }
2545                 Paragraph * lpar = lt->cursor.par();
2546                 pos_type pos = lt->cursor.pos();
2547                 if (pos < lpar->size() - 1)
2548                         ++pos;
2549                 else {
2550                         pos = 0;
2551                         lpar = lpar->next();
2552                 }
2553                 if (!lpar) {
2554                         if (clear)
2555                                 lt = 0;
2556                         // we have to unlock ourself in this function by default!
2557                         bv->unlockInset(const_cast<InsetText *>(this));
2558                         return false;
2559                 }
2560                 lt->setCursor(bv, lpar, pos);
2561                 if (clear)
2562                         lt = 0;
2563         }
2564         if (LyXFind(bv, str, true, true, cs , mw)) {
2565                 return true;
2566         }
2567         // we have to unlock ourself in this function by default!
2568         bv->unlockInset(const_cast<InsetText *>(this));
2569         return false;
2570 }
2571
2572 bool InsetText::searchBackward(BufferView * bv, string const & str,
2573                                bool const & cs, bool const & mw)
2574 {
2575         if (the_locking_inset)
2576                 if (the_locking_inset->searchBackward(bv, str, cs, mw))
2577                         return true;
2578         if (LyXFind(bv, str, false, true, cs, mw)) {
2579                 return true;
2580         }
2581         // we have to unlock ourself in this function by default!
2582         bv->unlockInset(const_cast<InsetText *>(this));
2583         return false;
2584 }
2585
2586
2587 bool InsetText::checkInsertChar(LyXFont & font)
2588 {
2589         if (owner())
2590                 return owner()->checkInsertChar(font);
2591         return true;
2592 }