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