]> git.lyx.org Git - features.git/blob - src/insets/insettext.C
d313c0daa2c550b057c81da5cf97768ddff222f2
[features.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);
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         if (!the_locking_inset)
839                 return false;
840         if (the_locking_inset != inset) {
841                 getLyXText(bv)->updateInset(bv, the_locking_inset);
842                 setUpdateStatus(bv, CURSOR_PAR);
843                 return the_locking_inset->updateInsetInInset(bv, inset);
844         }
845         if (getLyXText(bv)->updateInset(bv, inset))
846                 updateLocal(bv, CURSOR_PAR, false);
847         if (cpar(bv) == inset_par && cpos(bv) == inset_pos) {
848                 inset_x = cx(bv) - top_x + drawTextXOffset;
849                 inset_y = cy(bv) + drawTextYOffset;
850         }
851         return true;
852 }
853
854
855 void InsetText::insetButtonPress(BufferView * bv, int x, int y, int button)
856 {
857         no_selection = true;
858
859         int tmp_x = x - drawTextXOffset;
860         int tmp_y = y + insetAscent - getLyXText(bv)->first;
861         Inset * inset = bv->checkInsetHit(getLyXText(bv), tmp_x, tmp_y, button);
862
863         hideInsetCursor(bv);
864         if (the_locking_inset) {
865                 if (the_locking_inset == inset) {
866                         the_locking_inset->insetButtonPress(bv,x-inset_x,y-inset_y,button);
867                         no_selection = false;
868                         return;
869                 } else if (inset) {
870                         // otherwise unlock the_locking_inset and lock the new inset
871                         the_locking_inset->insetUnlock(bv);
872                         inset_x = cx(bv) - top_x + drawTextXOffset;
873                         inset_y = cy(bv) + drawTextYOffset;
874                         the_locking_inset = static_cast<UpdatableInset*>(inset);
875                         inset->insetButtonPress(bv, x - inset_x, y - inset_y, button);
876                         inset->edit(bv, x - inset_x, y - inset_y, button);
877                         if (the_locking_inset)
878                                 updateLocal(bv, CURSOR, false);
879                         no_selection = false;
880                         return;
881                 }
882                 // otherwise only unlock the_locking_inset
883                 the_locking_inset->insetUnlock(bv);
884                 the_locking_inset = 0;
885         }
886         if (bv->theLockingInset()) {
887                 if (isHighlyEditableInset(inset)) {
888                         UpdatableInset * uinset = static_cast<UpdatableInset*>(inset);
889                         inset_x = cx(bv) - top_x + drawTextXOffset;
890                         inset_y = cy(bv) + drawTextYOffset;
891                         inset_pos = cpos(bv);
892                         inset_par = cpar(bv);
893                         inset_boundary = cboundary(bv);
894                         the_locking_inset = uinset;
895                         uinset->insetButtonPress(bv, x - inset_x, y - inset_y,
896                                                  button);
897                         uinset->edit(bv, x - inset_x, y - inset_y, 0);
898                         if (the_locking_inset)
899                                 updateLocal(bv, CURSOR, false);
900                         no_selection = false;
901                         return;
902                 }
903         }
904         if (!inset) { // && (button == 2)) {
905                 bool paste_internally = false;
906                 if ((button == 2) && getLyXText(bv)->selection.set()) {
907                         localDispatch(bv, LFUN_COPY, "");
908                         paste_internally = true;
909                 }
910                 bool clear = false;
911                 if (!lt) {
912                         lt = getLyXText(bv);
913                         clear = true;
914                 }
915                 lt->setCursorFromCoordinates(bv, x-drawTextXOffset, y + insetAscent);
916                 if (lt->selection.set()) {
917                         lt->clearSelection();
918                         updateLocal(bv, FULL, false);
919                 } else {
920                         lt->clearSelection();
921                 }
922                 bv->owner()->setLayout(cpar(bv)->getLayout());
923                 old_par = cpar(bv);
924                 // Insert primary selection with middle mouse
925                 // if there is a local selection in the current buffer,
926                 // insert this
927                 if (button == 2) {
928                         if (paste_internally)
929                                 localDispatch(bv, LFUN_PASTE, "");
930                         else
931                                 localDispatch(bv, LFUN_PASTESELECTION,
932                                               "paragraph");
933                 }
934                 if (clear)
935                         lt = 0;
936         }
937         showInsetCursor(bv);
938         no_selection = false;
939 }
940
941
942 void InsetText::insetButtonRelease(BufferView * bv, int x, int y, int button)
943 {
944         UpdatableInset * inset = 0;
945
946         if (the_locking_inset) {
947                 the_locking_inset->insetButtonRelease(bv,
948                                                       x - inset_x, y - inset_y,
949                                                       button);
950         } else {
951                 if (cpar(bv)->isInset(cpos(bv))) {
952                         inset = static_cast<UpdatableInset*>(cpar(bv)->getInset(cpos(bv)));
953                         if (isHighlyEditableInset(inset)) {
954                                 inset->insetButtonRelease(bv,
955                                                           x - inset_x,
956                                                           y - inset_y, button);
957                         } else {
958                                 inset_x = cx(bv) - top_x + drawTextXOffset;
959                                 inset_y = cy(bv) + drawTextYOffset;
960                                 inset->insetButtonRelease(bv,
961                                                           x - inset_x,
962                                                           y - inset_y, button);
963                                 inset->edit(bv,
964                                             x - inset_x, y - inset_y, button);
965                         }
966                         updateLocal(bv, CURSOR_PAR, false);
967                 }
968         }
969         no_selection = false;
970 }
971
972
973 void InsetText::insetMotionNotify(BufferView * bv, int x, int y, int state)
974 {
975         if (no_selection)
976                 return;
977         if (the_locking_inset) {
978                 the_locking_inset->insetMotionNotify(bv, x - inset_x,
979                                                      y - inset_y,state);
980                 return;
981         }
982         LyXText * t = getLyXText(bv);
983         hideInsetCursor(bv);
984         t->setCursorFromCoordinates(bv, x - drawTextXOffset, y + insetAscent);
985         t->setSelection(bv);
986         if (t->toggle_cursor.par() != t->toggle_end_cursor.par() ||
987                 t->toggle_cursor.pos() != t->toggle_end_cursor.pos())
988                 updateLocal(bv, SELECTION, false);
989         showInsetCursor(bv);
990 }
991
992
993 void InsetText::insetKeyPress(XKeyEvent * xke)
994 {
995         if (the_locking_inset) {
996                 the_locking_inset->insetKeyPress(xke);
997                 return;
998         }
999 }
1000
1001
1002 UpdatableInset::RESULT
1003 InsetText::localDispatch(BufferView * bv,
1004                          kb_action action, string const & arg)
1005 {
1006         bool was_empty = par->size() == 0 && !par->next();
1007         no_selection = false;
1008         UpdatableInset::RESULT
1009                 result= UpdatableInset::localDispatch(bv, action, arg);
1010         if (result != UNDISPATCHED) {
1011                 return DISPATCHED;
1012         }
1013
1014         result = DISPATCHED;
1015         if ((action < 0) && arg.empty())
1016                 return FINISHED;
1017
1018         if (the_locking_inset) {
1019                 result = the_locking_inset->localDispatch(bv, action, arg);
1020                 if (result == DISPATCHED_NOUPDATE)
1021                         return result;
1022                 else if (result == DISPATCHED) {
1023                         updateLocal(bv, CURSOR_PAR, false);
1024                         return result;
1025                 } else if (result >= FINISHED) {
1026                         switch(result) {
1027                         case FINISHED_RIGHT:
1028                                 moveRightIntern(bv, false, false);
1029                                 result = DISPATCHED;
1030                                 break;
1031                         case FINISHED_UP:
1032                                 if ((result = moveUp(bv)) >= FINISHED) {
1033                                         updateLocal(bv, CURSOR, false);
1034                                         bv->unlockInset(this);
1035                                 }
1036                                 break;
1037                         case FINISHED_DOWN:
1038                                 if ((result = moveDown(bv)) >= FINISHED) {
1039                                         updateLocal(bv, CURSOR, false);
1040                                         bv->unlockInset(this);
1041                                 }
1042                                 break;
1043                         default:
1044                                 result = DISPATCHED;
1045                                 break;
1046                         }
1047                         the_locking_inset = 0;
1048 #ifdef WITH_WARNINGS
1049 #warning I changed this to always return Dispatched maybe it is wrong (20011001 Jug)
1050 #endif
1051                         return result;
1052                 }
1053         }
1054         hideInsetCursor(bv);
1055         bool clear = false;
1056         if (!lt) {
1057                 lt = getLyXText(bv);
1058                 clear = true;
1059         }
1060         switch (action) {
1061         // Normal chars
1062         case LFUN_SELFINSERT:
1063                 if (bv->buffer()->isReadonly()) {
1064 //          setErrorMessage(N_("Document is read only"));
1065                         break;
1066                 }
1067                 if (!arg.empty()) {
1068                         /* Automatically delete the currently selected
1069                          * text and replace it with what is being
1070                          * typed in now. Depends on lyxrc settings
1071                          * "auto_region_delete", which defaults to
1072                          * true (on). */
1073
1074                         setUndo(bv, Undo::INSERT,
1075                                 lt->cursor.par(), lt->cursor.par()->next());
1076                         bv->setState();
1077                         if (lyxrc.auto_region_delete) {
1078                                 if (lt->selection.set()) {
1079                                         lt->cutSelection(bv, false);
1080                                 }
1081                         }
1082                         lt->clearSelection();
1083                         for (string::size_type i = 0; i < arg.length(); ++i) {
1084                                 bv->owner()->getIntl()->getTrans().TranslateAndInsert(arg[i], lt);
1085                         }
1086                 }
1087                 lt->selection.cursor = lt->cursor;
1088                 updateLocal(bv, CURSOR_PAR, true);
1089                 result = DISPATCHED_NOUPDATE;
1090                 break;
1091                 // --- Cursor Movements -----------------------------------
1092         case LFUN_RIGHTSEL:
1093                 finishUndo();
1094                 moveRight(bv, false, true);
1095                 lt->setSelection(bv);
1096                 updateLocal(bv, SELECTION, false);
1097                 break;
1098         case LFUN_RIGHT:
1099                 result = moveRight(bv);
1100                 finishUndo();
1101                 updateLocal(bv, CURSOR, false);
1102                 break;
1103         case LFUN_LEFTSEL:
1104                 finishUndo();
1105                 moveLeft(bv, false, true);
1106                 lt->setSelection(bv);
1107                 updateLocal(bv, SELECTION, false);
1108                 break;
1109         case LFUN_LEFT:
1110                 finishUndo();
1111                 result = moveLeft(bv);
1112                 updateLocal(bv, CURSOR, false);
1113                 break;
1114         case LFUN_DOWNSEL:
1115                 finishUndo();
1116                 moveDown(bv);
1117                 lt->setSelection(bv);
1118                 updateLocal(bv, SELECTION, false);
1119                 break;
1120         case LFUN_DOWN:
1121                 finishUndo();
1122                 result = moveDown(bv);
1123                 updateLocal(bv, CURSOR, false);
1124                 break;
1125         case LFUN_UPSEL:
1126                 finishUndo();
1127                 moveUp(bv);
1128                 lt->setSelection(bv);
1129                 updateLocal(bv, SELECTION, false);
1130                 break;
1131         case LFUN_UP:
1132                 finishUndo();
1133                 result = moveUp(bv);
1134                 updateLocal(bv, CURSOR, false);
1135                 break;
1136         case LFUN_HOME:
1137                 finishUndo();
1138                 lt->cursorHome(bv);
1139                 updateLocal(bv, CURSOR, false);
1140                 break;
1141         case LFUN_END:
1142                 lt->cursorEnd(bv);
1143                 updateLocal(bv, CURSOR, false);
1144                 break;
1145         case LFUN_BACKSPACE: {
1146                 setUndo(bv, Undo::DELETE,
1147                         lt->cursor.par(), lt->cursor.par()->next());
1148                 if (lt->selection.set())
1149                         lt->cutSelection(bv);
1150                 else
1151                         lt->backspace(bv);
1152                 updateLocal(bv, CURSOR_PAR, true);
1153         }
1154         break;
1155         
1156         case LFUN_DELETE: {
1157                 setUndo(bv, Undo::DELETE,
1158                         lt->cursor.par(), lt->cursor.par()->next());
1159                 if (lt->selection.set()) {
1160                         lt->cutSelection(bv);
1161                 } else {
1162                         lt->Delete(bv);
1163                 }
1164                 updateLocal(bv, CURSOR_PAR, true);
1165         }
1166         break;
1167         
1168         case LFUN_CUT: {
1169                 setUndo(bv, Undo::DELETE,
1170                         lt->cursor.par(), lt->cursor.par()->next());
1171                 lt->cutSelection(bv);
1172                 updateLocal(bv, CURSOR_PAR, true);
1173         }
1174         break;
1175
1176         case LFUN_COPY:
1177                 finishUndo();
1178                 lt->copySelection(bv);
1179                 updateLocal(bv, CURSOR_PAR, false);
1180                 break;
1181         case LFUN_PASTESELECTION:
1182         {
1183                 string const clip(bv->getClipboard());
1184         
1185                 if (clip.empty())
1186                         break;
1187                 if (arg == "paragraph") {
1188                         lt->insertStringAsParagraphs(bv, clip);
1189                 } else {
1190                         lt->insertStringAsLines(bv, clip);
1191                 }
1192                 updateLocal(bv, CURSOR_PAR, true);
1193                 break;
1194         }
1195         case LFUN_PASTE: {
1196                 if (!autoBreakRows) {
1197
1198                         if (CutAndPaste::nrOfParagraphs() > 1) {
1199                                 Alert::alert(_("Impossible operation"),
1200                                                    _("Cannot include more than one paragraph!"),
1201                                                    _("Sorry."));
1202                                 break;
1203                         }
1204                 }
1205                 setUndo(bv, Undo::INSERT,
1206                         lt->cursor.par(), lt->cursor.par()->next());
1207                 lt->pasteSelection(bv);
1208                 updateLocal(bv, CURSOR_PAR, true);
1209         }
1210         break;
1211
1212         case LFUN_BREAKPARAGRAPH:
1213                 if (!autoBreakRows) {
1214                         result = DISPATCHED;
1215                         break;
1216                 }
1217                 lt->breakParagraph(bv, 0);
1218                 updateLocal(bv, FULL, true);
1219                 break;
1220         case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
1221                 if (!autoBreakRows) {
1222                         result = DISPATCHED;
1223                         break;
1224                 }
1225                 lt->breakParagraph(bv, 1);
1226                 updateLocal(bv, FULL, true);
1227                 break;
1228
1229         case LFUN_BREAKLINE: {
1230                 if (!autoBreakRows) {
1231                         result = DISPATCHED;
1232                         break;
1233                 }
1234                 setUndo(bv, Undo::INSERT,
1235                         lt->cursor.par(), lt->cursor.par()->next());
1236                 lt->insertChar(bv, Paragraph::META_NEWLINE);
1237                 updateLocal(bv, CURSOR_PAR, true);
1238         }
1239         break;
1240
1241         case LFUN_LAYOUT:
1242                 // do not set layouts on non breakable textinsets
1243                 if (autoBreakRows) {
1244                         layout_type cur_layout = cpar(bv)->layout;
1245           
1246                         // Derive layout number from given argument (string)
1247                         // and current buffer's textclass (number). */    
1248                         textclass_type tclass = bv->buffer()->params.textclass;
1249                         std::pair <bool, layout_type> layout = 
1250                                 textclasslist.NumberOfLayout(tclass, arg);
1251
1252                         // If the entry is obsolete, use the new one instead.
1253                         if (layout.first) {
1254                                 string obs = textclasslist.Style(tclass,layout.second).
1255                                         obsoleted_by();
1256                                 if (!obs.empty()) 
1257                                         layout = textclasslist.NumberOfLayout(tclass, obs);
1258                         }
1259
1260                         // see if we found the layout number:
1261                         if (!layout.first) {
1262                                 string const msg = string(N_("Layout ")) + arg + N_(" not known");
1263                                 bv->owner()->getLyXFunc()->dispatch(LFUN_MESSAGE, msg);
1264                                 break;
1265                         }
1266
1267                         if (cur_layout != layout.second) {
1268                                 cur_layout = layout.second;
1269                                 lt->setLayout(bv, layout.second);
1270                                 bv->owner()->setLayout(cpar(bv)->getLayout());
1271                                 updateLocal(bv, CURSOR_PAR, true);
1272                         }
1273                 } else {
1274                         // reset the layout box
1275                         bv->owner()->setLayout(cpar(bv)->getLayout());
1276                 }
1277                 break;
1278         case LFUN_PARAGRAPH_SPACING:
1279                 // This one is absolutely not working. When fiddling with this
1280                 // it also seems to me that the paragraphs inside the insettext
1281                 // inherit bufferparams/paragraphparams in a strange way. (Lgb)
1282         {
1283                 Paragraph * par = lt->cursor.par();
1284                 Spacing::Space cur_spacing = par->params().spacing().getSpace();
1285                 float cur_value = 1.0;
1286                 if (cur_spacing == Spacing::Other) {
1287                         cur_value = par->params().spacing().getValue();
1288                 }
1289                                 
1290                 istringstream istr(arg.c_str());
1291                 string tmp;
1292                 istr >> tmp;
1293                 Spacing::Space new_spacing = cur_spacing;
1294                 float new_value = cur_value;
1295                 if (tmp.empty()) {
1296                         lyxerr << "Missing argument to `paragraph-spacing'"
1297                                    << endl;
1298                 } else if (tmp == "single") {
1299                         new_spacing = Spacing::Single;
1300                 } else if (tmp == "onehalf") {
1301                         new_spacing = Spacing::Onehalf;
1302                 } else if (tmp == "double") {
1303                         new_spacing = Spacing::Double;
1304                 } else if (tmp == "other") {
1305                         new_spacing = Spacing::Other;
1306                         float tmpval = 0.0;
1307                         istr >> tmpval;
1308                         lyxerr << "new_value = " << tmpval << endl;
1309                         if (tmpval != 0.0)
1310                                 new_value = tmpval;
1311                 } else if (tmp == "default") {
1312                         new_spacing = Spacing::Default;
1313                 } else {
1314                         lyxerr << _("Unknown spacing argument: ")
1315                                    << arg << endl;
1316                 }
1317                 if (cur_spacing != new_spacing || cur_value != new_value) {
1318                         par->params().spacing(Spacing(new_spacing, new_value));
1319                         updateLocal(bv, CURSOR_PAR, true);
1320                 }
1321         }
1322         break;
1323         
1324         default:
1325                 if (!bv->Dispatch(action, arg))
1326                         result = UNDISPATCHED;
1327                 break;
1328         }
1329
1330         /// If the action has deleted all text in the inset, we need to change the
1331         // language to the language of the surronding text.
1332         if (!was_empty && par->size() == 0 && !par->next()) {
1333                 LyXFont font(LyXFont::ALL_IGNORE);
1334                 font.setLanguage(bv->getParentLanguage(this));
1335                 setFont(bv, font, false);
1336         }
1337
1338         if (result < FINISHED) {
1339                 showInsetCursor(bv);
1340         } else
1341                 bv->unlockInset(this);
1342         if (clear)
1343                 lt = 0;
1344         return result;
1345 }
1346
1347
1348 int InsetText::latex(Buffer const * buf, ostream & os, bool, bool) const
1349 {
1350         TexRow texrow;
1351         buf->latexParagraphs(os, par, 0, texrow);
1352         return texrow.rows();
1353 }
1354
1355
1356 int InsetText::ascii(Buffer const * buf, ostream & os, int linelen) const
1357 {
1358         Paragraph * p = par;
1359         unsigned int lines = 0;
1360         
1361         while (p) {
1362                 string const tmp = buf->asciiParagraph(p, linelen);
1363                 lines += countChar(tmp, '\n');
1364                 os << tmp;
1365                 p = p->next();
1366         }
1367         return lines;
1368 }
1369
1370
1371 int InsetText::docbook(Buffer const * buf, ostream & os) const
1372 {
1373         Paragraph * p = par;
1374         unsigned int lines = 0;
1375
1376         vector<string> environment_stack(10);
1377         vector<string> environment_inner(10);
1378         
1379         int const command_depth = 0;
1380         string item_name;
1381         
1382         Paragraph::depth_type depth = 0; // paragraph depth
1383
1384         while (p) {
1385                 string sgmlparam;
1386                 int desc_on = 0; // description mode
1387
1388                 LyXLayout const & style =
1389                         textclasslist.Style(buf->params.textclass,
1390                                             p->layout);
1391
1392                 // environment tag closing
1393                 for (; depth > p->params().depth(); --depth) {
1394                         if (environment_inner[depth] != "!-- --") {
1395                                 item_name = "listitem";
1396                                 buf->sgmlCloseTag(os, command_depth + depth,
1397                                              item_name);
1398                                 if (environment_inner[depth] == "varlistentry")
1399                                         buf->sgmlCloseTag(os, depth+command_depth,
1400                                                      environment_inner[depth]);
1401                         }
1402                         buf->sgmlCloseTag(os, depth + command_depth,
1403                                      environment_stack[depth]);
1404                         environment_stack[depth].erase();
1405                         environment_inner[depth].erase();
1406                 }
1407
1408                 if (depth == p->params().depth()
1409                    && environment_stack[depth] != style.latexname()
1410                    && !environment_stack[depth].empty()) {
1411                         if (environment_inner[depth] != "!-- --") {
1412                                 item_name= "listitem";
1413                                 buf->sgmlCloseTag(os, command_depth+depth,
1414                                                   item_name);
1415                                 if (environment_inner[depth] == "varlistentry")
1416                                         buf->sgmlCloseTag(os,
1417                                                           depth + command_depth,
1418                                                           environment_inner[depth]);
1419                         }
1420                         
1421                         buf->sgmlCloseTag(os, depth + command_depth,
1422                                           environment_stack[depth]);
1423                         
1424                         environment_stack[depth].erase();
1425                         environment_inner[depth].erase();
1426                 }
1427
1428                 // Write opening SGML tags.
1429                 switch (style.latextype) {
1430                 case LATEX_PARAGRAPH:
1431                         buf->sgmlOpenTag(os, depth + command_depth,
1432                                          style.latexname());
1433                         break;
1434
1435                 case LATEX_COMMAND:
1436                         buf->sgmlError(p, 0,
1437                                        _("Error : LatexType Command not allowed here.\n"));
1438                         return -1;
1439                         break;
1440
1441                 case LATEX_ENVIRONMENT:
1442                 case LATEX_ITEM_ENVIRONMENT:
1443                         if (depth < p->params().depth()) {
1444                                 depth = p->params().depth();
1445                                 environment_stack[depth].erase();
1446                         }
1447
1448                         if (environment_stack[depth] != style.latexname()) {
1449                                 if(environment_stack.size() == depth + 1) {
1450                                         environment_stack.push_back("!-- --");
1451                                         environment_inner.push_back("!-- --");
1452                                 }
1453                                 environment_stack[depth] = style.latexname();
1454                                 environment_inner[depth] = "!-- --";
1455                                 buf->sgmlOpenTag(os, depth + command_depth,
1456                                                  environment_stack[depth]);
1457                         } else {
1458                                 if (environment_inner[depth] != "!-- --") {
1459                                         item_name= "listitem";
1460                                         buf->sgmlCloseTag(os,
1461                                                           command_depth + depth,
1462                                                           item_name);
1463                                         if (environment_inner[depth] == "varlistentry")
1464                                                 buf->sgmlCloseTag(os,
1465                                                                   depth + command_depth,
1466                                                                   environment_inner[depth]);
1467                                 }
1468                         }
1469                         
1470                         if (style.latextype == LATEX_ENVIRONMENT) {
1471                                 if (!style.latexparam().empty()) {
1472                                         if(style.latexparam() == "CDATA")
1473                                                 os << "<![CDATA[";
1474                                         else
1475                                                 buf->sgmlOpenTag(os, depth + command_depth,
1476                                                                  style.latexparam());
1477                                 }
1478                                 break;
1479                         }
1480
1481                         desc_on = (style.labeltype == LABEL_MANUAL);
1482
1483                         if (desc_on)
1484                                 environment_inner[depth]= "varlistentry";
1485                         else
1486                                 environment_inner[depth]= "listitem";
1487
1488                         buf->sgmlOpenTag(os, depth + 1 + command_depth,
1489                                          environment_inner[depth]);
1490
1491                         if (desc_on) {
1492                                 item_name= "term";
1493                                 buf->sgmlOpenTag(os, depth + 1 + command_depth,
1494                                                  item_name);
1495                         } else {
1496                                 item_name= "para";
1497                                 buf->sgmlOpenTag(os, depth + 1 + command_depth,
1498                                                  item_name);
1499                         }
1500                         break;
1501                 default:
1502                         buf->sgmlOpenTag(os, depth + command_depth,
1503                                          style.latexname());
1504                         break;
1505                 }
1506
1507                 buf->simpleDocBookOnePar(os, p, desc_on,
1508                                          depth + 1 + command_depth);
1509                 p = p->next();
1510
1511                 string end_tag;
1512                 // write closing SGML tags
1513                 switch (style.latextype) {
1514                 case LATEX_ENVIRONMENT:
1515                         if (!style.latexparam().empty()) {
1516                                 if(style.latexparam() == "CDATA")
1517                                         os << "]]>";
1518                                 else
1519                                         buf->sgmlCloseTag(os, depth + command_depth,
1520                                                           style.latexparam());
1521                         }
1522                         break;
1523                 case LATEX_ITEM_ENVIRONMENT:
1524                         if (desc_on == 1) break;
1525                         end_tag= "para";
1526                         buf->sgmlCloseTag(os, depth + 1 + command_depth, end_tag);
1527                         break;
1528                 case LATEX_PARAGRAPH:
1529                         buf->sgmlCloseTag(os, depth + command_depth, style.latexname());
1530                         break;
1531                 default:
1532                         buf->sgmlCloseTag(os, depth + command_depth, style.latexname());
1533                         break;
1534                 }
1535         }
1536
1537         // Close open tags
1538         for (int d = depth; d >= 0; --d) {
1539                 if (!environment_stack[depth].empty()) {
1540                         if (environment_inner[depth] != "!-- --") {
1541                                 item_name = "listitem";
1542                                 buf->sgmlCloseTag(os, command_depth + depth,
1543                                                   item_name);
1544                                if (environment_inner[depth] == "varlistentry")
1545                                        buf->sgmlCloseTag(os, depth + command_depth,
1546                                                          environment_inner[depth]);
1547                         }
1548                         
1549                         buf->sgmlCloseTag(os, depth + command_depth,
1550                                           environment_stack[depth]);
1551                 }
1552         }
1553         
1554         return lines;
1555 }
1556
1557
1558 void InsetText::validate(LaTeXFeatures & features) const
1559 {
1560         Paragraph * p = par;
1561         while (p) {
1562                 p->validate(features);
1563                 p = p->next();
1564         }
1565 }
1566
1567
1568 int InsetText::beginningOfMainBody(Buffer const * buf, Paragraph * p) const
1569 {
1570         if (textclasslist.Style(buf->params.textclass,
1571                                 p->getLayout()).labeltype != LABEL_MANUAL)
1572                 return 0;
1573         else
1574                 return p->beginningOfMainBody();
1575 }
1576
1577
1578 void InsetText::getCursorPos(BufferView * bv,
1579                              int & x, int & y) const
1580 {
1581         if (the_locking_inset) {
1582                 the_locking_inset->getCursorPos(bv, x, y);
1583                 return;
1584         }
1585         x = cx(bv);
1586         y = cy(bv);
1587 }
1588
1589
1590 unsigned int InsetText::insetInInsetY()
1591 {
1592         if (!the_locking_inset)
1593                 return 0;
1594
1595         return (inset_y + the_locking_inset->insetInInsetY());
1596 }
1597
1598
1599 void InsetText::toggleInsetCursor(BufferView * bv)
1600 {
1601         if (the_locking_inset) {
1602                 the_locking_inset->toggleInsetCursor(bv);
1603                 return;
1604         }
1605
1606         LyXFont const font(getLyXText(bv)->getFont(bv->buffer(), cpar(bv), cpos(bv)));
1607
1608         int const asc = lyxfont::maxAscent(font);
1609         int const desc = lyxfont::maxDescent(font);
1610   
1611         if (isCursorVisible())
1612                 bv->hideLockedInsetCursor();
1613         else
1614                 bv->showLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1615         toggleCursorVisible();
1616 }
1617
1618
1619 void InsetText::showInsetCursor(BufferView * bv, bool show)
1620 {
1621         if (the_locking_inset) {
1622                 the_locking_inset->showInsetCursor(bv, show);
1623                 return;
1624         }
1625         if (!isCursorVisible()) {
1626                 LyXFont const font =
1627                         getLyXText(bv)->getFont(bv->buffer(), cpar(bv), cpos(bv));
1628         
1629                 int const asc = lyxfont::maxAscent(font);
1630                 int const desc = lyxfont::maxDescent(font);
1631
1632                 bv->fitLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1633                 if (show)
1634                         bv->showLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1635                 setCursorVisible(true);
1636         }
1637 }
1638
1639
1640 void InsetText::hideInsetCursor(BufferView * bv)
1641 {
1642         if (isCursorVisible()) {
1643                 bv->hideLockedInsetCursor();
1644                 setCursorVisible(false);
1645         }
1646         if (the_locking_inset)
1647                 the_locking_inset->hideInsetCursor(bv);
1648 }
1649
1650
1651 void InsetText::fitInsetCursor(BufferView * bv) const
1652 {
1653         if (the_locking_inset) {
1654                 the_locking_inset->fitInsetCursor(bv);
1655                 return;
1656         }
1657         LyXFont const font =
1658                 getLyXText(bv)->getFont(bv->buffer(), cpar(bv), cpos(bv));
1659         
1660         int const asc = lyxfont::maxAscent(font);
1661         int const desc = lyxfont::maxDescent(font);
1662
1663         bv->fitLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1664 }
1665
1666
1667 UpdatableInset::RESULT
1668 InsetText::moveRight(BufferView * bv, bool activate_inset, bool selecting)
1669 {
1670         if (getLyXText(bv)->cursor.par()->isRightToLeftPar(bv->buffer()->params))
1671                 return moveLeftIntern(bv, false, activate_inset, selecting);
1672         else
1673                 return moveRightIntern(bv, false, activate_inset, selecting);
1674 }
1675
1676
1677 UpdatableInset::RESULT
1678 InsetText::moveLeft(BufferView * bv, bool activate_inset, bool selecting)
1679 {
1680         if (getLyXText(bv)->cursor.par()->isRightToLeftPar(bv->buffer()->params))
1681                 return moveRightIntern(bv, true, activate_inset, selecting);
1682         else
1683                 return moveLeftIntern(bv, true, activate_inset, selecting);
1684 }
1685
1686
1687 UpdatableInset::RESULT
1688 InsetText::moveRightIntern(BufferView * bv, bool behind, 
1689                            bool activate_inset, bool selecting)
1690 {
1691         if (!cpar(bv)->next() && (cpos(bv) >= cpar(bv)->size()))
1692                 return FINISHED_RIGHT;
1693         if (activate_inset && checkAndActivateInset(bv, behind))
1694                 return DISPATCHED;
1695         getLyXText(bv)->cursorRight(bv);
1696         if (!selecting)
1697                 getLyXText(bv)->selection.cursor = getLyXText(bv)->cursor;
1698         return DISPATCHED_NOUPDATE;
1699 }
1700
1701
1702 UpdatableInset::RESULT
1703 InsetText::moveLeftIntern(BufferView * bv, bool behind,
1704                           bool activate_inset, bool selecting)
1705 {
1706         if (!cpar(bv)->previous() && (cpos(bv) <= 0))
1707                 return FINISHED;
1708         getLyXText(bv)->cursorLeft(bv);
1709         if (!selecting)
1710                 getLyXText(bv)->selection.cursor = getLyXText(bv)->cursor;
1711         if (activate_inset && checkAndActivateInset(bv, behind))
1712                 return DISPATCHED;
1713         return DISPATCHED_NOUPDATE;
1714 }
1715
1716
1717 UpdatableInset::RESULT
1718 InsetText::moveUp(BufferView * bv)
1719 {
1720         if (!crow(bv)->previous())
1721                 return FINISHED_UP;
1722         getLyXText(bv)->cursorUp(bv);
1723         return DISPATCHED_NOUPDATE;
1724 }
1725
1726
1727 UpdatableInset::RESULT
1728 InsetText::moveDown(BufferView * bv)
1729 {
1730         if (!crow(bv)->next())
1731                 return FINISHED_DOWN;
1732         getLyXText(bv)->cursorDown(bv);
1733         return DISPATCHED_NOUPDATE;
1734 }
1735
1736
1737 bool InsetText::insertInset(BufferView * bv, Inset * inset)
1738 {
1739         if (the_locking_inset) {
1740                 if (the_locking_inset->insetAllowed(inset))
1741                         return the_locking_inset->insertInset(bv, inset);
1742                 return false;
1743         }
1744         inset->setOwner(this);
1745         hideInsetCursor(bv);
1746
1747         bool clear = false;
1748         if (!lt) {
1749                 lt = getLyXText(bv);
1750                 clear = true;
1751         }
1752         lt->insertInset(bv, inset);
1753 #if 0
1754         if ((!cpar(bv)->isInset(cpos(bv))) ||
1755                 (cpar(bv)->getInset(cpos(bv)) != inset))
1756                 lt->cursorLeft(bv);
1757 #endif
1758         bv->fitCursor();
1759         updateLocal(bv, CURSOR_PAR|CURSOR, true);
1760         showInsetCursor(bv);
1761         if (clear)
1762                 lt = 0;
1763         return true;
1764 }
1765
1766
1767 bool InsetText::insetAllowed(Inset::Code code) const
1768 {
1769         if (the_locking_inset)
1770                 return the_locking_inset->insetAllowed(code);
1771         return true;
1772 }
1773
1774
1775 UpdatableInset * InsetText::getLockingInset() const
1776 {
1777         return the_locking_inset ? the_locking_inset->getLockingInset() :
1778                 const_cast<InsetText *>(this);
1779 }
1780
1781
1782 UpdatableInset * InsetText::getFirstLockingInsetOfType(Inset::Code c)
1783 {
1784         if (c == lyxCode())
1785                 return this;
1786         if (the_locking_inset)
1787                 return the_locking_inset->getFirstLockingInsetOfType(c);
1788         return 0;
1789 }
1790
1791
1792 bool InsetText::showInsetDialog(BufferView * bv) const
1793 {
1794         if (the_locking_inset)
1795                 return the_locking_inset->showInsetDialog(bv);
1796         return false;
1797 }
1798
1799
1800 std::vector<string> const InsetText::getLabelList() const 
1801 {
1802         std::vector<string> label_list;
1803
1804         Paragraph * tpar = par;
1805         while (tpar) {
1806                 Paragraph::inset_iterator beg = tpar->inset_iterator_begin();
1807                 Paragraph::inset_iterator end = tpar->inset_iterator_end();
1808                 for (; beg != end; ++beg) {
1809                         std::vector<string> const l = (*beg)->getLabelList();
1810                         label_list.insert(label_list.end(), l.begin(), l.end());
1811                 }
1812                 tpar = tpar->next();
1813         }
1814         return label_list;
1815 }
1816
1817
1818 void InsetText::setFont(BufferView * bv, LyXFont const & font, bool toggleall,
1819                         bool selectall)
1820 {
1821         if (the_locking_inset) {
1822                 the_locking_inset->setFont(bv, font, toggleall, selectall);
1823                 return;
1824         }
1825         if ((!par->next() && !par->size()) || !cpar(bv)->size()) {
1826                 getLyXText(bv)->setFont(bv, font, toggleall);
1827                 return;
1828         }
1829         bool clear = false;
1830         if (!lt) {
1831                 lt = getLyXText(bv);
1832                 clear = true;
1833         }
1834         if (lt->selection.set()) {
1835                 setUndo(bv, Undo::EDIT, lt->cursor.par(), lt->cursor.par()->next());
1836         }
1837         if (selectall)
1838                 selectAll(bv);
1839         lt->toggleFree(bv, font, toggleall);
1840         if (selectall)
1841                 lt->clearSelection();
1842         bv->fitCursor();
1843         if (selectall || lt->selection.set())
1844                 updateLocal(bv, FULL, true);
1845         else
1846                 updateLocal(bv, CURSOR_PAR, true);
1847         if (clear)
1848                 lt = 0;
1849 }
1850
1851
1852 bool InsetText::checkAndActivateInset(BufferView * bv, bool behind)
1853 {
1854         if (cpar(bv)->isInset(cpos(bv))) {
1855                 unsigned int x;
1856                 unsigned int y;
1857                 Inset * inset =
1858                         static_cast<UpdatableInset*>(cpar(bv)->getInset(cpos(bv)));
1859                 if (!isHighlyEditableInset(inset))
1860                         return false;
1861                 LyXFont const font =
1862                         getLyXText(bv)->getFont(bv->buffer(), cpar(bv), cpos(bv));
1863                 if (behind) {
1864                         x = inset->width(bv, font);
1865                         y = font.isRightToLeft() ? 0 : inset->descent(bv, font);
1866                 } else {
1867                         x = 0;
1868                         y = font.isRightToLeft() ? inset->descent(bv, font) : 0;
1869                 }
1870                 //inset_x = cx(bv) - top_x + drawTextXOffset;
1871                 //inset_y = cy(bv) + drawTextYOffset;
1872                 inset->edit(bv, x, y, 0);
1873                 if (!the_locking_inset)
1874                         return false;
1875                 updateLocal(bv, CURSOR, false);
1876                 return true;
1877         }
1878         return false;
1879 }
1880
1881
1882 bool InsetText::checkAndActivateInset(BufferView * bv, int x, int y,
1883                                       int button)
1884 {
1885         x -= drawTextXOffset;
1886         int dummyx = x;
1887         int dummyy = y + insetAscent;
1888         Inset * inset = bv->checkInsetHit(getLyXText(bv), dummyx, dummyy, button);
1889
1890         if (inset) {
1891                 if (x < 0)
1892                         x = insetWidth;
1893                 if (y < 0)
1894                         y = insetDescent;
1895                 inset_x = cx(bv) - top_x + drawTextXOffset;
1896                 inset_y = cy(bv) + drawTextYOffset;
1897                 inset->edit(bv, x - inset_x, y - inset_y, button);
1898                 if (!the_locking_inset)
1899                         return false;
1900                 updateLocal(bv, CURSOR, false);
1901                 return true;
1902         }
1903         return false;
1904 }
1905
1906
1907 int InsetText::getMaxWidth(BufferView * bv, UpdatableInset const * inset) const
1908 {
1909 #if 0
1910         int w = UpdatableInset::getMaxWidth(bv, inset);
1911         if (w < 0) {
1912                 return -1;
1913         }
1914         if (owner()) {
1915                 w = w - top_x + owner()->x();
1916                 return w;
1917         }
1918         w -= (2 * TEXT_TO_INSET_OFFSET);
1919         return w - top_x;
1920 #else
1921         return UpdatableInset::getMaxWidth(bv, inset);
1922 #endif
1923 }
1924
1925
1926 void InsetText::setParagraphData(Paragraph * p)
1927 {
1928         // we have to unlock any locked inset otherwise we're in troubles
1929         the_locking_inset = 0;
1930         while (par) {
1931                 Paragraph * tmp = par->next();
1932                 delete par;
1933                 par = tmp;
1934         }
1935
1936         par = new Paragraph(*p, false);
1937         par->setInsetOwner(this);
1938         Paragraph * np = par;
1939         while (p->next()) {
1940                 p = p->next();
1941                 np->next(new Paragraph(*p, false));
1942                 np->next()->previous(np);
1943                 np = np->next();
1944                 np->setInsetOwner(this);
1945         }
1946         reinitLyXText();
1947         need_update = INIT;
1948 }
1949
1950
1951 void InsetText::setText(string const & data)
1952 {
1953         clear();
1954         LyXFont font(LyXFont::ALL_SANE);
1955         for (unsigned int i=0; i < data.length(); ++i)
1956                 par->insertChar(i, data[i], font);
1957 }
1958
1959
1960 void InsetText::setAutoBreakRows(bool flag)
1961 {
1962         if (flag != autoBreakRows) {
1963                 autoBreakRows = flag;
1964                 if (!flag)
1965                         removeNewlines();
1966                 need_update = INIT;
1967         }
1968 }
1969
1970
1971 void InsetText::setDrawFrame(BufferView * bv, DrawFrame how)
1972 {
1973         if (how != drawFrame_) {
1974                 drawFrame_ = how;
1975                 if (bv)
1976                         updateLocal(bv, DRAW_FRAME, false);
1977         }
1978 }
1979
1980
1981 void InsetText::setFrameColor(BufferView * bv, LColor::color col)
1982 {
1983         if (frame_color != col) {
1984                 frame_color = col;
1985                 if (bv)
1986                         updateLocal(bv, DRAW_FRAME, false);
1987         }
1988 }
1989
1990
1991 int InsetText::cx(BufferView * bv) const
1992 {
1993         bool clear = false;
1994         if (!lt) {
1995                 lt = getLyXText(bv);
1996                 clear = true;
1997         }
1998         int x = lt->cursor.x() + top_x + TEXT_TO_INSET_OFFSET;
1999         if (the_locking_inset) {
2000                 LyXFont font = lt->getFont(bv->buffer(),
2001                                              lt->cursor.par(),
2002                                              lt->cursor.pos());
2003                 if (font.isVisibleRightToLeft())
2004                         x -= the_locking_inset->width(bv, font);
2005         }
2006         if (clear)
2007                 lt = 0;
2008         return x;
2009 }
2010
2011
2012 int InsetText::cy(BufferView * bv) const
2013 {
2014         LyXFont font;
2015         return getLyXText(bv)->cursor.y() - ascent(bv, font) + TEXT_TO_INSET_OFFSET;
2016 }
2017
2018
2019 pos_type InsetText::cpos(BufferView * bv) const
2020 {
2021         return getLyXText(bv)->cursor.pos();
2022 }
2023
2024
2025 Paragraph * InsetText::cpar(BufferView * bv) const
2026 {
2027         return getLyXText(bv)->cursor.par();
2028 }
2029
2030
2031 bool InsetText::cboundary(BufferView * bv) const
2032 {
2033         return getLyXText(bv)->cursor.boundary();
2034 }
2035
2036
2037 Row * InsetText::crow(BufferView * bv) const
2038 {
2039         return getLyXText(bv)->cursor.row();
2040 }
2041
2042
2043 LyXText * InsetText::getLyXText(BufferView const * lbv,
2044                                 bool const recursive) const
2045 {
2046         if (!recursive && (cached_bview == lbv)) {
2047                 LyXText * lt = cached_text.get();
2048                 lyx::Assert(lt && lt->firstRow()->par() == par);
2049                 return cached_text.get();
2050         }
2051         
2052         // Super UGLY! (Lgb)
2053         BufferView * bv = const_cast<BufferView *>(lbv);
2054         
2055         cached_bview = bv;
2056         Cache::iterator it = cache.find(bv);
2057
2058         if (it != cache.end()) {
2059                 if (do_reinit)
2060                         reinitLyXText();
2061                 else if (do_resize)
2062                         resizeLyXText(do_resize);
2063                 if (lt || !it->second.remove) {
2064                         lyx::Assert(it->second.text.get());
2065                         cached_text = it->second.text;
2066                         if (recursive && the_locking_inset) {
2067                                 return the_locking_inset->getLyXText(bv, true);
2068                         }
2069                         return cached_text.get();
2070                 } else if (it->second.remove) {
2071                         if (locked) {
2072                                 saveLyXTextState(it->second.text.get());
2073                         } else {
2074                                 sstate.lpar = 0;
2075                         }
2076                 }
2077         }
2078         
2079         cached_text.reset(new LyXText(const_cast<InsetText *>(this)));
2080         cached_text->init(bv);
2081         restoreLyXTextState(bv, cached_text.get());
2082
2083         cache.insert(make_pair(bv, cached_text));
2084         
2085         if (the_locking_inset && recursive) {
2086                 return the_locking_inset->getLyXText(bv);
2087         }
2088         return cached_text.get();
2089 }
2090
2091
2092 void InsetText::deleteLyXText(BufferView * bv, bool recursive) const
2093 {
2094         cached_bview = 0;
2095
2096         Cache::iterator it = cache.find(bv);
2097         
2098         if (it == cache.end()) {
2099                 return;
2100         }
2101
2102         lyx::Assert(it->second.text.get());
2103
2104         it->second.remove = true;
2105         if (recursive) {
2106                 /// then remove all LyXText in text-insets
2107                 Paragraph * p = par;
2108                 for (; p; p = p->next()) {
2109                         p->deleteInsetsLyXText(bv);
2110                 }
2111         }
2112 }
2113
2114
2115 void InsetText::resizeLyXText(BufferView * bv, bool force) const
2116 {
2117         if (lt) {
2118                 // we cannot resize this because we are in use!
2119                 // so do this on the next possible getLyXText()
2120                 do_resize = bv;
2121                 return;
2122         }
2123         do_resize = 0;
2124 //      lyxerr << "InsetText::resizeLyXText\n";
2125         if (!par->next() && !par->size()) // no data, resize not neccessary!
2126                 return;
2127         // one endless line, resize normally not necessary
2128         if (!force && getMaxWidth(bv, this) < 0)
2129                 return;
2130
2131         Cache::iterator it = cache.find(bv);
2132         if (it == cache.end()) {
2133                 return;
2134         }
2135         lyx::Assert(it->second.text.get());
2136
2137         LyXText * t = it->second.text.get();
2138         saveLyXTextState(t);
2139         for (Paragraph * p = par; p; p = p->next()) {
2140                 p->resizeInsetsLyXText(bv);
2141         }
2142         t->init(bv, true);
2143         restoreLyXTextState(bv, t);
2144         if (the_locking_inset) {
2145                 inset_x = cx(bv) - top_x + drawTextXOffset;
2146                 inset_y = cy(bv) + drawTextYOffset;
2147         }
2148
2149         if (bv->screen()) {
2150                 t->first = bv->screen()->topCursorVisible(t);
2151         }
2152         if (!owner()) {
2153                 updateLocal(bv, FULL, false);
2154                 // this will scroll the screen such that the cursor becomes visible 
2155                 bv->updateScrollbar();
2156         } else {
2157                 need_update |= FULL;
2158         }
2159 }
2160
2161
2162 void InsetText::reinitLyXText() const
2163 {
2164         if (lt) {
2165                 // we cannot resize this because we are in use!
2166                 // so do this on the next possible getLyXText()
2167                 do_reinit = true;
2168                 return;
2169         }
2170         do_reinit = false;
2171         do_resize = 0;
2172 //      lyxerr << "InsetText::reinitLyXText\n";
2173         for(Cache::iterator it = cache.begin(); it != cache.end(); ++it) {
2174                 lyx::Assert(it->second.text.get());
2175
2176                 LyXText * t = it->second.text.get();
2177                 BufferView * bv = it->first;
2178
2179                 saveLyXTextState(t);
2180                 for (Paragraph * p = par; p; p = p->next()) {
2181                         p->resizeInsetsLyXText(bv);
2182                 }
2183                 t->init(bv, true);
2184                 restoreLyXTextState(bv, t);
2185                 if (the_locking_inset) {
2186                         inset_x = cx(bv) - top_x + drawTextXOffset;
2187                         inset_y = cy(bv) + drawTextYOffset;
2188                 }
2189                 if (bv->screen()) {
2190                         t->first = bv->screen()->topCursorVisible(t);
2191                 }
2192                 if (!owner()) {
2193                         updateLocal(bv, FULL, false);
2194                         // this will scroll the screen such that the cursor becomes visible 
2195                         bv->updateScrollbar();
2196                 } else {
2197                         need_update = FULL;
2198                 }
2199         }
2200 }
2201
2202
2203 void InsetText::removeNewlines()
2204 {
2205         for (Paragraph * p = par; p; p = p->next()) {
2206                 for (int i = 0; i < p->size(); ++i) {
2207                         if (p->getChar(i) == Paragraph::META_NEWLINE)
2208                                 p->erase(i);
2209                 }
2210         }
2211 }
2212
2213
2214 bool InsetText::nodraw() const
2215 {
2216         if (the_locking_inset)
2217                 return the_locking_inset->nodraw();
2218         return UpdatableInset::nodraw();
2219 }
2220
2221
2222 int InsetText::scroll(bool recursive) const
2223 {
2224         int sx = UpdatableInset::scroll(false);
2225
2226         if (recursive && the_locking_inset)
2227                 sx += the_locking_inset->scroll(recursive);
2228
2229         return sx;
2230 }
2231
2232
2233 bool InsetText::doClearArea() const
2234 {
2235         return !locked || (need_update & (FULL|INIT));
2236 }
2237
2238
2239 void InsetText::selectAll(BufferView * bv)
2240 {
2241         getLyXText(bv)->cursorTop(bv);
2242         getLyXText(bv)->selection.cursor = getLyXText(bv)->cursor;
2243         getLyXText(bv)->cursorBottom(bv);
2244         getLyXText(bv)->setSelection(bv);
2245 }
2246
2247
2248 void InsetText::clearSelection(BufferView * bv)
2249 {
2250         getLyXText(bv)->clearSelection();
2251 }
2252
2253
2254 void InsetText::clearInset(Painter & pain, int baseline, bool & cleared) const
2255 {
2256         int w = insetWidth;
2257         int h = insetAscent + insetDescent;
2258         int ty = baseline - insetAscent;
2259         
2260         if (ty < 0) {
2261                 h += ty;
2262                 ty = 0;
2263         }
2264         if ((ty + h) > pain.paperHeight())
2265                 h = pain.paperHeight();
2266         if ((top_x + drawTextXOffset + w) > pain.paperWidth())
2267                 w = pain.paperWidth();
2268 //      w -= TEXT_TO_INSET_OFFSET;
2269         pain.fillRectangle(top_x, ty, w+1, h+1, backgroundColor());
2270         cleared = true;
2271         need_update = FULL;
2272         frame_is_visible = false;
2273 }
2274
2275
2276 Paragraph * InsetText::getParFromID(int id) const
2277 {
2278 #if 0
2279         Paragraph * result = par;
2280         Paragraph * ires = 0;
2281         while (result && result->id() != id) {
2282                 if ((ires = result->getParFromID(id)))
2283                         return ires;
2284                 result = result->next();
2285         }
2286         return result;
2287 #else
2288         Paragraph * tmp = par;
2289         while (tmp) {
2290                 int tmp_id = tmp->id();
2291                 lyxerr << "Looking at paragraph: " << tmp_id << endl;
2292                 if (tmp->id() == id) {
2293                         return tmp;
2294                 }
2295                 Paragraph * tmp2 = tmp->getParFromID(id);
2296                 if (tmp2 != 0) {
2297                         return tmp2;
2298                 }
2299                 tmp = tmp->next();
2300         }
2301         return 0;
2302 #endif
2303 }
2304
2305
2306 Paragraph * InsetText::firstParagraph() const
2307 {
2308         Paragraph * result;
2309         if (the_locking_inset)
2310                 if ((result = the_locking_inset->firstParagraph()))
2311                         return result;
2312         return par;
2313 }
2314
2315
2316 Paragraph * InsetText::getFirstParagraph(int i) const
2317 {
2318         return (i == 0) ? par : 0;
2319 }
2320
2321
2322 LyXCursor const & InsetText::cursor(BufferView * bv) const
2323 {
2324                 if (the_locking_inset)
2325                                 return the_locking_inset->cursor(bv);
2326                 return getLyXText(bv)->cursor;
2327 }
2328
2329
2330 Paragraph * InsetText::paragraph() const
2331 {
2332         return par;
2333 }
2334
2335
2336 void InsetText::paragraph(Paragraph * p)
2337 {
2338         par = p;
2339         // set ourself as owner for all the paragraphs inserted!
2340         Paragraph * np = par;
2341         while (np) {
2342                 np->setInsetOwner(this);
2343                 np = np->next();
2344         }
2345         reinitLyXText();
2346         // redraw myself when asked for
2347         need_update = INIT;
2348 }
2349
2350
2351 Inset * InsetText::getInsetFromID(int id_arg) const
2352 {
2353         if (id_arg == id())
2354                 return const_cast<InsetText *>(this);
2355
2356         Paragraph * lp = par;
2357
2358         while(lp) {
2359                 for (Paragraph::inset_iterator it = lp->inset_iterator_begin(),
2360                          en = lp->inset_iterator_end();
2361                          it != en; ++it)
2362                 {
2363                         if ((*it)->id() == id_arg)
2364                                 return *it;
2365                         Inset * in = (*it)->getInsetFromID(id_arg);
2366                         if (in)
2367                                 return in;
2368                 }
2369                 lp = lp->next();
2370         }
2371         return 0;
2372 }
2373
2374
2375 string const InsetText::selectNextWordToSpellcheck(BufferView * bv, float & value) const
2376 {
2377         bool clear = false;
2378         string str;
2379
2380         if (!lt) {
2381                 lt = getLyXText(bv);
2382                 clear = true;
2383         }
2384         if (the_locking_inset) {
2385                 str = the_locking_inset->selectNextWordToSpellcheck(bv, value);
2386                 if (!str.empty()) {
2387                         value += cy(bv);
2388                         if (clear)
2389                                 lt = 0;
2390                         return str;
2391                 }
2392 #warning Dekel please have a look on this one RTL? (Jug)
2393 #warning DEKEL!
2394                 // we have to go on checking so move cusor to the right
2395                 lt->cursor.pos(lt->cursor.pos() + 1);
2396         }
2397         str = lt->selectNextWordToSpellcheck(bv, value);
2398         if (str.empty())
2399                 bv->unlockInset(const_cast<InsetText *>(this));
2400         else
2401                 value = cy(bv);
2402         if (clear)
2403                 lt = 0;
2404         return str;
2405 }
2406
2407
2408 void InsetText::selectSelectedWord(BufferView * bv)
2409 {
2410         if (the_locking_inset) {
2411                 the_locking_inset->selectSelectedWord(bv);
2412                 return;
2413         }
2414         getLyXText(bv)->selectSelectedWord(bv);
2415         updateLocal(bv, SELECTION, false);
2416 }
2417
2418
2419 void InsetText::toggleSelection(BufferView * bv, bool kill_selection)
2420 {
2421         if (the_locking_inset) {
2422                 the_locking_inset->toggleSelection(bv, kill_selection);
2423         }
2424         bool clear = false;
2425         if (!lt) {
2426                 lt = getLyXText(bv);
2427                 clear = true;
2428         }
2429
2430         int x = top_x + TEXT_TO_INSET_OFFSET;
2431
2432         Row * row = lt->firstRow();
2433         int y_offset = top_baseline - row->ascent_of_text();
2434         int y = y_offset;
2435         while ((row != 0) && ((y+row->height()) <= 0)) {
2436                 y += row->height();
2437                 row = row->next();
2438         }
2439         if (y_offset < 0)
2440                 y_offset = y;
2441         
2442         if (need_update & SELECTION)
2443                 need_update = NONE;
2444         bv->screen()->toggleSelection(lt, bv, kill_selection, y_offset, x);
2445         if (clear)
2446                 lt = 0;
2447 }
2448
2449
2450 bool InsetText::searchForward(BufferView * bv, string const & str,
2451                               bool const & cs, bool const & mw)
2452 {
2453         if (the_locking_inset) {
2454                 if (the_locking_inset->searchForward(bv, str, cs, mw))
2455                         return true;
2456                 bool clear = false;
2457                 if (!lt) {
2458                         lt = getLyXText(bv);
2459                         clear = true;
2460                 }
2461                 Paragraph * lpar = lt->cursor.par();
2462                 pos_type pos = lt->cursor.pos();
2463                 if (pos < lpar->size() - 1)
2464                         ++pos;
2465                 else {
2466                         pos = 0;
2467                         lpar = lpar->next();
2468                 }
2469                 if (!lpar) {
2470                         if (clear)
2471                                 lt = 0;
2472                         // we have to unlock ourself in this function by default!
2473                         bv->unlockInset(const_cast<InsetText *>(this));
2474                         return false;
2475                 }
2476                 lt->setCursor(bv, lpar, pos);
2477                 if (clear)
2478                         lt = 0;
2479         }
2480         if (LyXFind(bv, str, true, true, cs , mw)) {
2481                 return true;
2482         }
2483         // we have to unlock ourself in this function by default!
2484         bv->unlockInset(const_cast<InsetText *>(this));
2485         return false;
2486 }
2487
2488 bool InsetText::searchBackward(BufferView * bv, string const & str,
2489                                bool const & cs, bool const & mw)
2490 {
2491         if (the_locking_inset)
2492                 if (the_locking_inset->searchBackward(bv, str, cs, mw))
2493                         return true;
2494         if (LyXFind(bv, str, false, true, cs, mw)) {
2495                 return true;
2496         }
2497         // we have to unlock ourself in this function by default!
2498         bv->unlockInset(const_cast<InsetText *>(this));
2499         return false;
2500 }
2501
2502
2503 bool InsetText::checkInsertChar(LyXFont & font)
2504 {
2505         if (owner())
2506                 return owner()->checkInsertChar(font);
2507         return true;
2508 }