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