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