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