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