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