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