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