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