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