]> git.lyx.org Git - lyx.git/blob - src/insets/insettext.C
79ba5e635a83f3389f83754daa192842e03a2975
[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                         bool dispatched = false;
1018                         switch (action) {
1019                         case LFUN_UNKNOWN_ACTION:
1020                         case LFUN_BREAKPARAGRAPH:
1021                         case LFUN_BREAKLINE:
1022                                 moveRightIntern(bv, false, false);
1023                                 break;
1024                         case LFUN_RIGHT:
1025                                 if (!getLyXText(bv)->cursor.par()->isRightToLeftPar(bv->buffer()->params))
1026                                         moveRightIntern(bv, false, false);
1027                                 dispatched = true;
1028                                 break;
1029                         case LFUN_LEFT:
1030                                 if (getLyXText(bv)->cursor.par()->isRightToLeftPar(bv->buffer()->params))
1031                                         moveRightIntern(bv, false, false);
1032                                 dispatched = true;
1033                                 break;
1034                         default:
1035                                 break;
1036                         }
1037                         the_locking_inset = 0;
1038                         if (dispatched)
1039                                 return DISPATCHED;
1040                 }
1041         }
1042         hideInsetCursor(bv);
1043         bool clear = false;
1044         if (!lt) {
1045                 lt = getLyXText(bv);
1046                 clear = true;
1047         }
1048         switch (action) {
1049         // Normal chars
1050         case LFUN_SELFINSERT:
1051                 if (bv->buffer()->isReadonly()) {
1052 //          setErrorMessage(N_("Document is read only"));
1053                         break;
1054                 }
1055                 if (!arg.empty()) {
1056                         /* Automatically delete the currently selected
1057                          * text and replace it with what is being
1058                          * typed in now. Depends on lyxrc settings
1059                          * "auto_region_delete", which defaults to
1060                          * true (on). */
1061
1062                         setUndo(bv, Undo::INSERT,
1063                                 lt->cursor.par(), lt->cursor.par()->next());
1064                         bv->setState();
1065                         if (lyxrc.auto_region_delete) {
1066                                 if (lt->selection.set()) {
1067                                         lt->cutSelection(bv, false);
1068                                 }
1069                         }
1070                         lt->clearSelection();
1071                         for (string::size_type i = 0; i < arg.length(); ++i) {
1072                                 bv->owner()->getIntl()->getTrans().TranslateAndInsert(arg[i], lt);
1073                         }
1074                 }
1075                 lt->selection.cursor = lt->cursor;
1076                 updateLocal(bv, CURSOR_PAR, true);
1077                 result = DISPATCHED_NOUPDATE;
1078                 break;
1079                 // --- Cursor Movements -----------------------------------
1080         case LFUN_RIGHTSEL:
1081                 finishUndo();
1082                 moveRight(bv, false, true);
1083                 lt->setSelection(bv);
1084                 updateLocal(bv, SELECTION, false);
1085                 break;
1086         case LFUN_RIGHT:
1087                 result = moveRight(bv);
1088                 finishUndo();
1089                 updateLocal(bv, CURSOR, false);
1090                 break;
1091         case LFUN_LEFTSEL:
1092                 finishUndo();
1093                 moveLeft(bv, false, true);
1094                 lt->setSelection(bv);
1095                 updateLocal(bv, SELECTION, false);
1096                 break;
1097         case LFUN_LEFT:
1098                 finishUndo();
1099                 result = moveLeft(bv);
1100                 updateLocal(bv, CURSOR, false);
1101                 break;
1102         case LFUN_DOWNSEL:
1103                 finishUndo();
1104                 moveDown(bv);
1105                 lt->setSelection(bv);
1106                 updateLocal(bv, SELECTION, false);
1107                 break;
1108         case LFUN_DOWN:
1109                 finishUndo();
1110                 result = moveDown(bv);
1111                 updateLocal(bv, CURSOR, false);
1112                 break;
1113         case LFUN_UPSEL:
1114                 finishUndo();
1115                 moveUp(bv);
1116                 lt->setSelection(bv);
1117                 updateLocal(bv, SELECTION, false);
1118                 break;
1119         case LFUN_UP:
1120                 finishUndo();
1121                 result = moveUp(bv);
1122                 updateLocal(bv, CURSOR, false);
1123                 break;
1124         case LFUN_HOME:
1125                 finishUndo();
1126                 lt->cursorHome(bv);
1127                 updateLocal(bv, CURSOR, false);
1128                 break;
1129         case LFUN_END:
1130                 lt->cursorEnd(bv);
1131                 updateLocal(bv, CURSOR, false);
1132                 break;
1133         case LFUN_BACKSPACE: {
1134                 setUndo(bv, Undo::DELETE,
1135                         lt->cursor.par(), lt->cursor.par()->next());
1136                 if (lt->selection.set())
1137                         lt->cutSelection(bv);
1138                 else
1139                         lt->backspace(bv);
1140                 updateLocal(bv, CURSOR_PAR, true);
1141         }
1142         break;
1143         
1144         case LFUN_DELETE: {
1145                 setUndo(bv, Undo::DELETE,
1146                         lt->cursor.par(), lt->cursor.par()->next());
1147                 if (lt->selection.set()) {
1148                         lt->cutSelection(bv);
1149                 } else {
1150                         lt->Delete(bv);
1151                 }
1152                 updateLocal(bv, CURSOR_PAR, true);
1153         }
1154         break;
1155         
1156         case LFUN_CUT: {
1157                 setUndo(bv, Undo::DELETE,
1158                         lt->cursor.par(), lt->cursor.par()->next());
1159                 lt->cutSelection(bv);
1160                 updateLocal(bv, CURSOR_PAR, true);
1161         }
1162         break;
1163
1164         case LFUN_COPY:
1165                 finishUndo();
1166                 lt->copySelection(bv);
1167                 updateLocal(bv, CURSOR_PAR, false);
1168                 break;
1169         case LFUN_PASTESELECTION:
1170         {
1171                 string const clip(bv->getClipboard());
1172         
1173                 if (clip.empty())
1174                         break;
1175                 if (arg == "paragraph") {
1176                         lt->insertStringAsParagraphs(bv, clip);
1177                 } else {
1178                         lt->insertStringAsLines(bv, clip);
1179                 }
1180                 updateLocal(bv, CURSOR_PAR, true);
1181                 break;
1182         }
1183         case LFUN_PASTE: {
1184                 if (!autoBreakRows) {
1185
1186                         if (CutAndPaste::nrOfParagraphs() > 1) {
1187                                 WriteAlert(_("Impossible operation"),
1188                                                    _("Cannot include more than one paragraph!"),
1189                                                    _("Sorry."));
1190                                 break;
1191                         }
1192                 }
1193                 setUndo(bv, Undo::INSERT,
1194                         lt->cursor.par(), lt->cursor.par()->next());
1195                 lt->pasteSelection(bv);
1196                 updateLocal(bv, CURSOR_PAR, true);
1197         }
1198         break;
1199
1200         case LFUN_BREAKPARAGRAPH:
1201                 if (!autoBreakRows) {
1202                         result = DISPATCHED;
1203                         break;
1204                 }
1205                 lt->breakParagraph(bv, 0);
1206                 updateLocal(bv, FULL, true);
1207                 break;
1208         case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
1209                 if (!autoBreakRows) {
1210                         result = DISPATCHED;
1211                         break;
1212                 }
1213                 lt->breakParagraph(bv, 1);
1214                 updateLocal(bv, FULL, true);
1215                 break;
1216
1217         case LFUN_BREAKLINE: {
1218                 if (!autoBreakRows) {
1219                         result = DISPATCHED;
1220                         break;
1221                 }
1222                 setUndo(bv, Undo::INSERT,
1223                         lt->cursor.par(), lt->cursor.par()->next());
1224                 lt->insertChar(bv, Paragraph::META_NEWLINE);
1225                 updateLocal(bv, CURSOR_PAR, true);
1226         }
1227         break;
1228
1229         case LFUN_LAYOUT:
1230                 // do not set layouts on non breakable textinsets
1231                 if (autoBreakRows) {
1232                         LyXTextClass::size_type cur_layout = cpar(bv)->layout;
1233           
1234                         // Derive layout number from given argument (string)
1235                         // and current buffer's textclass (number). */    
1236                         LyXTextClassList::ClassList::size_type tclass =
1237                                 bv->buffer()->params.textclass;
1238                         std::pair <bool, LyXTextClass::size_type> layout = 
1239                                 textclasslist.NumberOfLayout(tclass, arg);
1240
1241                         // If the entry is obsolete, use the new one instead.
1242                         if (layout.first) {
1243                                 string obs = textclasslist.Style(tclass,layout.second).
1244                                         obsoleted_by();
1245                                 if (!obs.empty()) 
1246                                         layout = textclasslist.NumberOfLayout(tclass, obs);
1247                         }
1248
1249                         // see if we found the layout number:
1250                         if (!layout.first) {
1251                                 string const msg = string(N_("Layout ")) + arg + N_(" not known");
1252                                 bv->owner()->getLyXFunc()->dispatch(LFUN_MESSAGE, msg);
1253                                 break;
1254                         }
1255
1256                         if (cur_layout != layout.second) {
1257                                 cur_layout = layout.second;
1258                                 lt->setLayout(bv, layout.second);
1259                                 bv->owner()->setLayout(cpar(bv)->getLayout());
1260                                 updateLocal(bv, CURSOR_PAR, true);
1261                         }
1262                 } else {
1263                         // reset the layout box
1264                         bv->owner()->setLayout(cpar(bv)->getLayout());
1265                 }
1266                 break;
1267         case LFUN_PARAGRAPH_SPACING:
1268                 // This one is absolutely not working. When fiddling with this
1269                 // it also seems to me that the paragraphs inside the insettext
1270                 // inherit bufferparams/paragraphparams in a strange way. (Lgb)
1271         {
1272                 Paragraph * par = lt->cursor.par();
1273                 Spacing::Space cur_spacing = par->params().spacing().getSpace();
1274                 float cur_value = 1.0;
1275                 if (cur_spacing == Spacing::Other) {
1276                         cur_value = par->params().spacing().getValue();
1277                 }
1278                                 
1279                 istringstream istr(arg.c_str());
1280                 string tmp;
1281                 istr >> tmp;
1282                 Spacing::Space new_spacing = cur_spacing;
1283                 float new_value = cur_value;
1284                 if (tmp.empty()) {
1285                         lyxerr << "Missing argument to `paragraph-spacing'"
1286                                    << endl;
1287                 } else if (tmp == "single") {
1288                         new_spacing = Spacing::Single;
1289                 } else if (tmp == "onehalf") {
1290                         new_spacing = Spacing::Onehalf;
1291                 } else if (tmp == "double") {
1292                         new_spacing = Spacing::Double;
1293                 } else if (tmp == "other") {
1294                         new_spacing = Spacing::Other;
1295                         float tmpval = 0.0;
1296                         istr >> tmpval;
1297                         lyxerr << "new_value = " << tmpval << endl;
1298                         if (tmpval != 0.0)
1299                                 new_value = tmpval;
1300                 } else if (tmp == "default") {
1301                         new_spacing = Spacing::Default;
1302                 } else {
1303                         lyxerr << _("Unknown spacing argument: ")
1304                                    << arg << endl;
1305                 }
1306                 if (cur_spacing != new_spacing || cur_value != new_value) {
1307                         par->params().spacing(Spacing(new_spacing, new_value));
1308                         updateLocal(bv, CURSOR_PAR, true);
1309                 }
1310         }
1311         break;
1312         
1313         default:
1314                 if (!bv->Dispatch(action, arg))
1315                         result = UNDISPATCHED;
1316                 break;
1317         }
1318
1319         /// If the action has deleted all text in the inset, we need to change the
1320         // language to the language of the surronding text.
1321         if (!was_empty && par->size() == 0 && !par->next()) {
1322                 LyXFont font(LyXFont::ALL_IGNORE);
1323                 font.setLanguage(bv->getParentLanguage(this));
1324                 setFont(bv, font, false);
1325         }
1326
1327         if (result < FINISHED) {
1328                 showInsetCursor(bv);
1329         } else
1330                 bv->unlockInset(this);
1331         if (clear)
1332                 lt = 0;
1333         return result;
1334 }
1335
1336
1337 int InsetText::latex(Buffer const * buf, ostream & os, bool, bool) const
1338 {
1339         TexRow texrow;
1340         buf->latexParagraphs(os, par, 0, texrow);
1341         return texrow.rows();
1342 }
1343
1344
1345 int InsetText::ascii(Buffer const * buf, ostream & os, int linelen) const
1346 {
1347         Paragraph * p = par;
1348         unsigned int lines = 0;
1349         
1350         while (p) {
1351                 string const tmp = buf->asciiParagraph(p, linelen);
1352                 lines += countChar(tmp, '\n');
1353                 os << tmp;
1354                 p = p->next();
1355         }
1356         return lines;
1357 }
1358
1359
1360 int InsetText::docbook(Buffer const * buf, ostream & os) const
1361 {
1362         Paragraph * p = par;
1363         unsigned int lines = 0;
1364         int desc = 0;
1365         
1366         string tmp;
1367         while (p) {
1368                 buf->simpleDocBookOnePar(os, tmp, p, desc, 0);
1369                 p = p->next();
1370         }
1371         
1372         return lines;
1373 }
1374
1375
1376 void InsetText::validate(LaTeXFeatures & features) const
1377 {
1378         Paragraph * p = par;
1379         while (p) {
1380                 p->validate(features);
1381                 p = p->next();
1382         }
1383 }
1384
1385
1386 int InsetText::beginningOfMainBody(Buffer const * buf, Paragraph * p) const
1387 {
1388         if (textclasslist.Style(buf->params.textclass,
1389                                 p->getLayout()).labeltype != LABEL_MANUAL)
1390                 return 0;
1391         else
1392                 return p->beginningOfMainBody();
1393 }
1394
1395
1396 void InsetText::getCursorPos(BufferView * bv,
1397                              int & x, int & y) const
1398 {
1399         if (the_locking_inset) {
1400                 the_locking_inset->getCursorPos(bv, x, y);
1401                 return;
1402         }
1403         x = cx(bv);
1404         y = cy(bv);
1405 }
1406
1407
1408 unsigned int InsetText::insetInInsetY()
1409 {
1410         if (!the_locking_inset)
1411                 return 0;
1412
1413         return (inset_y + the_locking_inset->insetInInsetY());
1414 }
1415
1416
1417 void InsetText::toggleInsetCursor(BufferView * bv)
1418 {
1419         if (the_locking_inset) {
1420                 the_locking_inset->toggleInsetCursor(bv);
1421                 return;
1422         }
1423
1424         LyXFont const font(getLyXText(bv)->getFont(bv->buffer(), cpar(bv), cpos(bv)));
1425
1426         int const asc = lyxfont::maxAscent(font);
1427         int const desc = lyxfont::maxDescent(font);
1428   
1429         if (isCursorVisible())
1430                 bv->hideLockedInsetCursor();
1431         else
1432                 bv->showLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1433         toggleCursorVisible();
1434 }
1435
1436
1437 void InsetText::showInsetCursor(BufferView * bv, bool show)
1438 {
1439         if (the_locking_inset) {
1440                 the_locking_inset->showInsetCursor(bv, show);
1441                 return;
1442         }
1443         if (!isCursorVisible()) {
1444                 LyXFont const font =
1445                         getLyXText(bv)->getFont(bv->buffer(), cpar(bv), cpos(bv));
1446         
1447                 int const asc = lyxfont::maxAscent(font);
1448                 int const desc = lyxfont::maxDescent(font);
1449
1450                 bv->fitLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1451                 if (show)
1452                         bv->showLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1453                 setCursorVisible(true);
1454         }
1455 }
1456
1457
1458 void InsetText::hideInsetCursor(BufferView * bv)
1459 {
1460         if (isCursorVisible()) {
1461                 bv->hideLockedInsetCursor();
1462                 setCursorVisible(false);
1463         }
1464         if (the_locking_inset)
1465                 the_locking_inset->hideInsetCursor(bv);
1466 }
1467
1468
1469 void InsetText::fitInsetCursor(BufferView * bv) const
1470 {
1471         if (the_locking_inset) {
1472                 the_locking_inset->fitInsetCursor(bv);
1473                 return;
1474         }
1475         LyXFont const font =
1476                 getLyXText(bv)->getFont(bv->buffer(), cpar(bv), cpos(bv));
1477         
1478         int const asc = lyxfont::maxAscent(font);
1479         int const desc = lyxfont::maxDescent(font);
1480
1481         bv->fitLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1482 }
1483
1484
1485 UpdatableInset::RESULT
1486 InsetText::moveRight(BufferView * bv, bool activate_inset, bool selecting)
1487 {
1488         if (getLyXText(bv)->cursor.par()->isRightToLeftPar(bv->buffer()->params))
1489                 return moveLeftIntern(bv, false, activate_inset, selecting);
1490         else
1491                 return moveRightIntern(bv, false, activate_inset, selecting);
1492 }
1493
1494
1495 UpdatableInset::RESULT
1496 InsetText::moveLeft(BufferView * bv, bool activate_inset, bool selecting)
1497 {
1498         if (getLyXText(bv)->cursor.par()->isRightToLeftPar(bv->buffer()->params))
1499                 return moveRightIntern(bv, true, activate_inset, selecting);
1500         else
1501                 return moveLeftIntern(bv, true, activate_inset, selecting);
1502 }
1503
1504
1505 UpdatableInset::RESULT
1506 InsetText::moveRightIntern(BufferView * bv, bool behind, 
1507                            bool activate_inset, bool selecting)
1508 {
1509         if (!cpar(bv)->next() && (cpos(bv) >= cpar(bv)->size()))
1510                 return FINISHED_RIGHT;
1511         if (activate_inset && checkAndActivateInset(bv, behind))
1512                 return DISPATCHED;
1513         getLyXText(bv)->cursorRight(bv);
1514         if (!selecting)
1515                 getLyXText(bv)->selection.cursor = getLyXText(bv)->cursor;
1516         return DISPATCHED_NOUPDATE;
1517 }
1518
1519
1520 UpdatableInset::RESULT
1521 InsetText::moveLeftIntern(BufferView * bv, bool behind,
1522                           bool activate_inset, bool selecting)
1523 {
1524         if (!cpar(bv)->previous() && (cpos(bv) <= 0))
1525                 return FINISHED;
1526         getLyXText(bv)->cursorLeft(bv);
1527         if (!selecting)
1528                 getLyXText(bv)->selection.cursor = getLyXText(bv)->cursor;
1529         if (activate_inset && checkAndActivateInset(bv, behind))
1530                 return DISPATCHED;
1531         return DISPATCHED_NOUPDATE;
1532 }
1533
1534
1535 UpdatableInset::RESULT
1536 InsetText::moveUp(BufferView * bv)
1537 {
1538         if (!crow(bv)->previous())
1539                 return FINISHED_UP;
1540         getLyXText(bv)->cursorUp(bv);
1541         return DISPATCHED_NOUPDATE;
1542 }
1543
1544
1545 UpdatableInset::RESULT
1546 InsetText::moveDown(BufferView * bv)
1547 {
1548         if (!crow(bv)->next())
1549                 return FINISHED_DOWN;
1550         getLyXText(bv)->cursorDown(bv);
1551         return DISPATCHED_NOUPDATE;
1552 }
1553
1554
1555 bool InsetText::insertInset(BufferView * bv, Inset * inset)
1556 {
1557         if (the_locking_inset) {
1558                 if (the_locking_inset->insetAllowed(inset))
1559                         return the_locking_inset->insertInset(bv, inset);
1560                 return false;
1561         }
1562         inset->setOwner(this);
1563         hideInsetCursor(bv);
1564
1565         bool clear = false;
1566         if (!lt) {
1567                 lt = getLyXText(bv);
1568                 clear = true;
1569         }
1570         lt->insertInset(bv, inset);
1571 #if 0
1572         if ((cpar(bv)->getChar(cpos(bv)) != Paragraph::META_INSET) ||
1573                 (cpar(bv)->getInset(cpos(bv)) != inset))
1574                 lt->cursorLeft(bv);
1575 #endif
1576         bv->fitCursor();
1577         updateLocal(bv, CURSOR_PAR|CURSOR, true);
1578         showInsetCursor(bv);
1579         if (clear)
1580                 lt = 0;
1581         return true;
1582 }
1583
1584
1585 bool InsetText::insetAllowed(Inset::Code code) const
1586 {
1587         if (the_locking_inset)
1588                 return the_locking_inset->insetAllowed(code);
1589         return true;
1590 }
1591
1592
1593 UpdatableInset * InsetText::getLockingInset() const
1594 {
1595         return the_locking_inset ? the_locking_inset->getLockingInset() :
1596                 const_cast<InsetText *>(this);
1597 }
1598
1599
1600 UpdatableInset * InsetText::getFirstLockingInsetOfType(Inset::Code c)
1601 {
1602         if (c == lyxCode())
1603                 return this;
1604         if (the_locking_inset)
1605                 return the_locking_inset->getFirstLockingInsetOfType(c);
1606         return 0;
1607 }
1608
1609
1610 bool InsetText::showInsetDialog(BufferView * bv) const
1611 {
1612         if (the_locking_inset)
1613                 return the_locking_inset->showInsetDialog(bv);
1614         return false;
1615 }
1616
1617
1618 std::vector<string> const InsetText::getLabelList() const 
1619 {
1620         std::vector<string> label_list;
1621
1622         Paragraph * tpar = par;
1623         while (tpar) {
1624                 Paragraph::inset_iterator beg = tpar->inset_iterator_begin();
1625                 Paragraph::inset_iterator end = tpar->inset_iterator_end();
1626                 for (; beg != end; ++beg) {
1627                         std::vector<string> const l = (*beg)->getLabelList();
1628                         label_list.insert(label_list.end(), l.begin(), l.end());
1629                 }
1630                 tpar = tpar->next();
1631         }
1632         return label_list;
1633 }
1634
1635
1636 void InsetText::setFont(BufferView * bv, LyXFont const & font, bool toggleall,
1637                         bool selectall)
1638 {
1639         if (the_locking_inset) {
1640                 the_locking_inset->setFont(bv, font, toggleall, selectall);
1641                 return;
1642         }
1643         if ((!par->next() && !par->size()) || !cpar(bv)->size()) {
1644                 getLyXText(bv)->setFont(bv, font, toggleall);
1645                 return;
1646         }
1647         bool clear = false;
1648         if (!lt) {
1649                 lt = getLyXText(bv);
1650                 clear = true;
1651         }
1652         if (lt->selection.set()) {
1653                 setUndo(bv, Undo::EDIT, lt->cursor.par(), lt->cursor.par()->next());
1654         }
1655         if (selectall)
1656                 selectAll(bv);
1657         lt->toggleFree(bv, font, toggleall);
1658         if (selectall)
1659                 lt->clearSelection();
1660         bv->fitCursor();
1661         if (selectall || lt->selection.set())
1662                 updateLocal(bv, FULL, true);
1663         else
1664                 updateLocal(bv, CURSOR_PAR, true);
1665         if (clear)
1666                 lt = 0;
1667 }
1668
1669
1670 bool InsetText::checkAndActivateInset(BufferView * bv, bool behind)
1671 {
1672         if (cpar(bv)->getChar(cpos(bv)) == Paragraph::META_INSET) {
1673                 unsigned int x;
1674                 unsigned int y;
1675                 Inset * inset =
1676                         static_cast<UpdatableInset*>(cpar(bv)->getInset(cpos(bv)));
1677                 if (!inset || inset->editable() != Inset::HIGHLY_EDITABLE)
1678                         return false;
1679                 LyXFont const font =
1680                         getLyXText(bv)->getFont(bv->buffer(), cpar(bv), cpos(bv));
1681                 if (behind) {
1682                         x = inset->width(bv, font);
1683                         y = font.isRightToLeft() ? 0 : inset->descent(bv, font);
1684                 } else {
1685                         x = 0;
1686                         y = font.isRightToLeft() ? inset->descent(bv, font) : 0;
1687                 }
1688                 //inset_x = cx(bv) - top_x + drawTextXOffset;
1689                 //inset_y = cy(bv) + drawTextYOffset;
1690                 inset->edit(bv, x, y, 0);
1691                 if (!the_locking_inset)
1692                         return false;
1693                 updateLocal(bv, CURSOR, false);
1694                 return true;
1695         }
1696         return false;
1697 }
1698
1699
1700 bool InsetText::checkAndActivateInset(BufferView * bv, int x, int y,
1701                                       int button)
1702 {
1703         x -= drawTextXOffset;
1704         int dummyx = x;
1705         int dummyy = y + insetAscent;
1706         Inset * inset = bv->checkInsetHit(getLyXText(bv), dummyx, dummyy, button);
1707
1708         if (inset) {
1709                 if (x < 0)
1710                         x = insetWidth;
1711                 if (y < 0)
1712                         y = insetDescent;
1713                 inset_x = cx(bv) - top_x + drawTextXOffset;
1714                 inset_y = cy(bv) + drawTextYOffset;
1715                 inset->edit(bv, x - inset_x, y - inset_y, button);
1716                 if (!the_locking_inset)
1717                         return false;
1718                 updateLocal(bv, CURSOR, false);
1719                 return true;
1720         }
1721         return false;
1722 }
1723
1724
1725 int InsetText::getMaxWidth(BufferView * bv, UpdatableInset const * inset) const
1726 {
1727 #if 0
1728         int w = UpdatableInset::getMaxWidth(bv, inset);
1729         if (w < 0) {
1730                 return -1;
1731         }
1732         if (owner()) {
1733                 w = w - top_x + owner()->x();
1734                 return w;
1735         }
1736         w -= (2 * TEXT_TO_INSET_OFFSET);
1737         return w - top_x;
1738 #else
1739         return UpdatableInset::getMaxWidth(bv, inset);
1740 #endif
1741 }
1742
1743
1744 void InsetText::setParagraphData(Paragraph * p)
1745 {
1746         // we have to unlock any locked inset otherwise we're in troubles
1747         the_locking_inset = 0;
1748         while (par) {
1749                 Paragraph * tmp = par->next();
1750                 delete par;
1751                 par = tmp;
1752         }
1753
1754         par = new Paragraph(*p, false);
1755         par->setInsetOwner(this);
1756         Paragraph * np = par;
1757         while (p->next()) {
1758                 p = p->next();
1759                 np->next(new Paragraph(*p, false));
1760                 np->next()->previous(np);
1761                 np = np->next();
1762                 np->setInsetOwner(this);
1763         }
1764         need_update = INIT;
1765 }
1766
1767
1768 void InsetText::setText(string const & data)
1769 {
1770         clear();
1771         LyXFont font(LyXFont::ALL_SANE);
1772         for (unsigned int i=0; i < data.length(); ++i)
1773                 par->insertChar(i, data[i], font);
1774 }
1775
1776
1777 void InsetText::setAutoBreakRows(bool flag)
1778 {
1779         if (flag != autoBreakRows) {
1780                 autoBreakRows = flag;
1781                 if (!flag)
1782                         removeNewlines();
1783                 need_update = INIT;
1784         }
1785 }
1786
1787
1788 void InsetText::setDrawFrame(BufferView * bv, DrawFrame how)
1789 {
1790         if (how != drawFrame_) {
1791                 drawFrame_ = how;
1792                 if (bv)
1793                         updateLocal(bv, DRAW_FRAME, false);
1794         }
1795 }
1796
1797
1798 void InsetText::setFrameColor(BufferView * bv, LColor::color col)
1799 {
1800         if (frame_color != col) {
1801                 frame_color = col;
1802                 if (bv)
1803                         updateLocal(bv, DRAW_FRAME, false);
1804         }
1805 }
1806
1807
1808 int InsetText::cx(BufferView * bv) const
1809 {
1810         bool clear = false;
1811         if (!lt) {
1812                 lt = getLyXText(bv);
1813                 clear = true;
1814         }
1815         int x = lt->cursor.x() + top_x + TEXT_TO_INSET_OFFSET;
1816         if (the_locking_inset) {
1817                 LyXFont font = lt->getFont(bv->buffer(),
1818                                              lt->cursor.par(),
1819                                              lt->cursor.pos());
1820                 if (font.isVisibleRightToLeft())
1821                         x -= the_locking_inset->width(bv, font);
1822         }
1823         if (clear)
1824                 lt = 0;
1825         return x;
1826 }
1827
1828
1829 int InsetText::cy(BufferView * bv) const
1830 {
1831         LyXFont font;
1832         return getLyXText(bv)->cursor.y() - ascent(bv, font) + TEXT_TO_INSET_OFFSET;
1833 }
1834
1835
1836 Paragraph::size_type InsetText::cpos(BufferView * bv) const
1837 {
1838         return getLyXText(bv)->cursor.pos();
1839 }
1840
1841
1842 Paragraph * InsetText::cpar(BufferView * bv) const
1843 {
1844         return getLyXText(bv)->cursor.par();
1845 }
1846
1847
1848 bool InsetText::cboundary(BufferView * bv) const
1849 {
1850         return getLyXText(bv)->cursor.boundary();
1851 }
1852
1853
1854 Row * InsetText::crow(BufferView * bv) const
1855 {
1856         return getLyXText(bv)->cursor.row();
1857 }
1858
1859
1860 LyXText * InsetText::getLyXText(BufferView const * lbv,
1861                                 bool const recursive) const
1862 {
1863         if (!recursive && (cached_bview == lbv))
1864                 return cached_text.get();
1865         
1866         // Super UGLY! (Lgb)
1867         BufferView * bv = const_cast<BufferView *>(lbv);
1868         
1869         cached_bview = bv;
1870         Cache::iterator it = cache.find(bv);
1871
1872         if (it != cache.end()) {
1873                 if (do_reinit)
1874                         reinitLyXText();
1875                 else if (do_resize)
1876                         resizeLyXText(do_resize);
1877                 if (lt || !it->second.remove) {
1878                         lyx::Assert(it->second.text.get());
1879                         cached_text = it->second.text;
1880                         if (recursive && the_locking_inset) {
1881                                 return the_locking_inset->getLyXText(bv, true);
1882                         }
1883                         return cached_text.get();
1884                 } else if (it->second.remove) {
1885                         if (locked) {
1886                                 saveLyXTextState(it->second.text.get());
1887                         } else {
1888                                 sstate.lpar = 0;
1889                         }
1890                 }
1891         }
1892         
1893         cached_text.reset(new LyXText(const_cast<InsetText *>(this)));
1894         cached_text->init(bv);
1895         restoreLyXTextState(bv, cached_text.get());
1896
1897         cache.insert(make_pair(bv, cached_text));
1898         
1899         if (the_locking_inset && recursive) {
1900                 return the_locking_inset->getLyXText(bv);
1901         }
1902         return cached_text.get();
1903 }
1904
1905
1906 void InsetText::deleteLyXText(BufferView * bv, bool recursive) const
1907 {
1908         cached_bview = 0;
1909
1910         Cache::iterator it = cache.find(bv);
1911         
1912         if (it == cache.end()) {
1913                 return;
1914         }
1915
1916         lyx::Assert(it->second.text.get());
1917
1918         it->second.remove = true;
1919         if (recursive) {
1920                 /// then remove all LyXText in text-insets
1921                 Paragraph * p = par;
1922                 for (; p; p = p->next()) {
1923                         p->deleteInsetsLyXText(bv);
1924                 }
1925         }
1926 }
1927
1928
1929 void InsetText::resizeLyXText(BufferView * bv, bool force) const
1930 {
1931         if (lt) {
1932                 // we cannot resize this because we are in use!
1933                 // so do this on the next possible getLyXText()
1934                 do_resize = bv;
1935                 return;
1936         }
1937         do_resize = 0;
1938 //      lyxerr << "InsetText::resizeLyXText\n";
1939         if (!par->next() && !par->size()) // no data, resize not neccessary!
1940                 return;
1941         // one endless line, resize normally not necessary
1942         if (!force && getMaxWidth(bv, this) < 0)
1943                 return;
1944
1945         Cache::iterator it = cache.find(bv);
1946         if (it == cache.end()) {
1947                 return;
1948         }
1949         lyx::Assert(it->second.text.get());
1950
1951         LyXText * t = it->second.text.get();
1952         saveLyXTextState(t);
1953         for (Paragraph * p = par; p; p = p->next()) {
1954                 p->resizeInsetsLyXText(bv);
1955         }
1956         t->init(bv, true);
1957         restoreLyXTextState(bv, t);
1958         if (the_locking_inset) {
1959                 inset_x = cx(bv) - top_x + drawTextXOffset;
1960                 inset_y = cy(bv) + drawTextYOffset;
1961         }
1962
1963         if (bv->screen()) {
1964                 t->first = bv->screen()->topCursorVisible(t);
1965         }
1966         if (!owner()) {
1967                 updateLocal(bv, FULL, false);
1968                 // this will scroll the screen such that the cursor becomes visible 
1969                 bv->updateScrollbar();
1970         } else {
1971                 need_update |= FULL;
1972         }
1973 }
1974
1975
1976 void InsetText::reinitLyXText() const
1977 {
1978         if (lt) {
1979                 // we cannot resize this because we are in use!
1980                 // so do this on the next possible getLyXText()
1981                 do_reinit = true;
1982                 return;
1983         }
1984         do_reinit = false;
1985         do_resize = 0;
1986 //      lyxerr << "InsetText::reinitLyXText\n";
1987         for(Cache::iterator it = cache.begin(); it != cache.end(); ++it) {
1988                 lyx::Assert(it->second.text.get());
1989
1990                 LyXText * t = it->second.text.get();
1991                 BufferView * bv = it->first;
1992
1993                 saveLyXTextState(t);
1994                 for (Paragraph * p = par; p; p = p->next()) {
1995                         p->resizeInsetsLyXText(bv);
1996                 }
1997                 t->init(bv, true);
1998                 restoreLyXTextState(bv, t);
1999                 if (the_locking_inset) {
2000                         inset_x = cx(bv) - top_x + drawTextXOffset;
2001                         inset_y = cy(bv) + drawTextYOffset;
2002                 }
2003                 if (bv->screen()) {
2004                         t->first = bv->screen()->topCursorVisible(t);
2005                 }
2006                 if (!owner()) {
2007                         updateLocal(bv, FULL, false);
2008                         // this will scroll the screen such that the cursor becomes visible 
2009                         bv->updateScrollbar();
2010                 } else {
2011                         need_update = FULL;
2012                 }
2013         }
2014 }
2015
2016
2017 void InsetText::removeNewlines()
2018 {
2019         for (Paragraph * p = par; p; p = p->next()) {
2020                 for (int i = 0; i < p->size(); ++i) {
2021                         if (p->getChar(i) == Paragraph::META_NEWLINE)
2022                                 p->erase(i);
2023                 }
2024         }
2025 }
2026
2027
2028 bool InsetText::nodraw() const
2029 {
2030         if (the_locking_inset)
2031                 return the_locking_inset->nodraw();
2032         return UpdatableInset::nodraw();
2033 }
2034
2035
2036 int InsetText::scroll(bool recursive) const
2037 {
2038         int sx = UpdatableInset::scroll(false);
2039
2040         if (recursive && the_locking_inset)
2041                 sx += the_locking_inset->scroll(recursive);
2042
2043         return sx;
2044 }
2045
2046
2047 bool InsetText::doClearArea() const
2048 {
2049         return !locked || (need_update & (FULL|INIT));
2050 }
2051
2052
2053 void InsetText::selectAll(BufferView * bv)
2054 {
2055         getLyXText(bv)->cursorTop(bv);
2056         getLyXText(bv)->selection.cursor = getLyXText(bv)->cursor;
2057         getLyXText(bv)->cursorBottom(bv);
2058         getLyXText(bv)->setSelection(bv);
2059 }
2060
2061
2062 void InsetText::clearSelection(BufferView * bv)
2063 {
2064         getLyXText(bv)->clearSelection();
2065 }
2066
2067
2068 void InsetText::clearInset(Painter & pain, int baseline, bool & cleared) const
2069 {
2070         int w = insetWidth;
2071         int h = insetAscent + insetDescent;
2072         int ty = baseline - insetAscent;
2073         
2074         if (ty < 0) {
2075                 h += ty;
2076                 ty = 0;
2077         }
2078         if ((ty + h) > pain.paperHeight())
2079                 h = pain.paperHeight();
2080         if ((top_x + drawTextXOffset + w) > pain.paperWidth())
2081                 w = pain.paperWidth();
2082 //      w -= TEXT_TO_INSET_OFFSET;
2083         pain.fillRectangle(top_x, ty, w+1, h+1, backgroundColor());
2084         cleared = true;
2085         need_update = FULL;
2086         frame_is_visible = false;
2087 }
2088
2089
2090 Paragraph * InsetText::getParFromID(int id) const
2091 {
2092 #if 0
2093         Paragraph * result = par;
2094         Paragraph * ires = 0;
2095         while (result && result->id() != id) {
2096                 if ((ires = result->getParFromID(id)))
2097                         return ires;
2098                 result = result->next();
2099         }
2100         return result;
2101 #else
2102         Paragraph * tmp = par;
2103         while (tmp) {
2104                 int tmp_id = tmp->id();
2105                 lyxerr << "Looking at paragraph: " << tmp_id << endl;
2106                 if (tmp->id() == id) {
2107                         return tmp;
2108                 }
2109                 Paragraph * tmp2 = tmp->getParFromID(id);
2110                 if (tmp2 != 0) {
2111                         return tmp2;
2112                 }
2113                 tmp = tmp->next();
2114         }
2115         return 0;
2116 #endif
2117 }
2118
2119
2120 Paragraph * InsetText::firstParagraph() const
2121 {
2122         Paragraph * result;
2123         if (the_locking_inset)
2124                 if ((result = the_locking_inset->firstParagraph()))
2125                         return result;
2126         return par;
2127 }
2128
2129
2130 Paragraph * InsetText::getFirstParagraph(int i) const
2131 {
2132         return (i == 0) ? par : 0;
2133 }
2134
2135
2136 LyXCursor const & InsetText::cursor(BufferView * bv) const
2137 {
2138                 if (the_locking_inset)
2139                                 return the_locking_inset->cursor(bv);
2140                 return getLyXText(bv)->cursor;
2141 }
2142
2143
2144 Paragraph * InsetText::paragraph() const
2145 {
2146         return par;
2147 }
2148
2149
2150 void InsetText::paragraph(Paragraph * p)
2151 {
2152         par = p;
2153 #if 0
2154         // we now have to update/redraw all instances
2155         for (Cache::iterator cit = cache.begin(); cit != cache.end(); ++cit) {
2156                 delete cit->second;
2157                 cit->second = 0;
2158         }
2159 #endif
2160         // redraw myself when asked for
2161         need_update = INIT;
2162 }
2163
2164
2165 Inset * InsetText::getInsetFromID(int id_arg) const
2166 {
2167         if (id_arg == id())
2168                 return const_cast<InsetText *>(this);
2169
2170         Paragraph * lp = par;
2171
2172         while(lp) {
2173                 for (Paragraph::inset_iterator it = lp->inset_iterator_begin(),
2174                          en = lp->inset_iterator_end();
2175                          it != en; ++it)
2176                 {
2177                         if ((*it)->id() == id_arg)
2178                                 return *it;
2179                         Inset * in = (*it)->getInsetFromID(id_arg);
2180                         if (in)
2181                                 return in;
2182                 }
2183                 lp = lp->next();
2184         }
2185         return 0;
2186 }
2187
2188
2189 string const InsetText::selectNextWord(BufferView * bv, float & value) const
2190 {
2191         bool clear = false;
2192         string str;
2193
2194         if (!lt) {
2195                 lt = getLyXText(bv);
2196                 clear = true;
2197         }
2198         if (the_locking_inset) {
2199                 str = the_locking_inset->selectNextWord(bv, value);
2200                 if (!str.empty()) {
2201                         value += cy(bv);
2202                         if (clear)
2203                                 lt = 0;
2204                         return str;
2205                 }
2206 #warning Dekel please have a look on this one RTL? (Jug)
2207 #warning DEKEL!
2208                 // we have to go on checking so move cusor to the right
2209                 lt->cursor.pos(lt->cursor.pos() + 1);
2210         }
2211         str = lt->selectNextWord(bv, value);
2212         if (str.empty())
2213                 bv->unlockInset(const_cast<InsetText *>(this));
2214         else
2215                 value = cy(bv);
2216         if (clear)
2217                 lt = 0;
2218         return str;
2219 }
2220
2221
2222 void InsetText::selectSelectedWord(BufferView * bv)
2223 {
2224         if (the_locking_inset) {
2225                 the_locking_inset->selectSelectedWord(bv);
2226                 return;
2227         }
2228         getLyXText(bv)->selectSelectedWord(bv);
2229         updateLocal(bv, SELECTION, false);
2230 }
2231
2232
2233 void InsetText::toggleSelection(BufferView * bv, bool kill_selection)
2234 {
2235         if (the_locking_inset) {
2236                 the_locking_inset->toggleSelection(bv, kill_selection);
2237         }
2238         bool clear = false;
2239         if (!lt) {
2240                 lt = getLyXText(bv);
2241                 clear = true;
2242         }
2243
2244         int x = top_x + TEXT_TO_INSET_OFFSET;
2245
2246         int y = 0;
2247         Row * row = lt->getRowNearY(y);
2248         int y_offset = top_baseline - row->ascent_of_text();
2249         y = y_offset;
2250         while ((row != 0) && ((y+row->height()) <= 0)) {
2251                 y += row->height();
2252                 row = row->next();
2253         }
2254         if (y_offset < 0)
2255                 y_offset = y;
2256         
2257         if (need_update & SELECTION)
2258                 need_update = NONE;
2259         bv->screen()->toggleSelection(lt, bv, kill_selection, y_offset, x);
2260         if (clear)
2261                 lt = 0;
2262 }
2263
2264
2265 bool InsetText::searchForward(BufferView * bv, string const & str,
2266                               bool const & cs, bool const & mw)
2267 {
2268         if (the_locking_inset) {
2269                 if (the_locking_inset->searchForward(bv, str, cs, mw))
2270                         return true;
2271                 bool clear = false;
2272                 if (!lt) {
2273                         lt = getLyXText(bv);
2274                         clear = true;
2275                 }
2276                 Paragraph * lpar = lt->cursor.par();
2277                 Paragraph::size_type pos = lt->cursor.pos();
2278                 if (pos < lpar->size() - 1)
2279                         ++pos;
2280                 else {
2281                         pos = 0;
2282                         lpar = lpar->next();
2283                 }
2284                 if (!lpar) {
2285                         if (clear)
2286                                 lt = 0;
2287                         // we have to unlock ourself in this function by default!
2288                         bv->unlockInset(const_cast<InsetText *>(this));
2289                         return false;
2290                 }
2291                 lt->setCursor(bv, lpar, pos);
2292                 if (clear)
2293                         lt = 0;
2294         }
2295         if (LyXFind(bv, str, true, true, cs , mw)) {
2296                 return true;
2297         }
2298         // we have to unlock ourself in this function by default!
2299         bv->unlockInset(const_cast<InsetText *>(this));
2300         return false;
2301 }
2302
2303 bool InsetText::searchBackward(BufferView * bv, string const & str,
2304                                bool const & cs, bool const & mw)
2305 {
2306         if (the_locking_inset)
2307                 if (the_locking_inset->searchBackward(bv, str, cs, mw))
2308                         return true;
2309         if (LyXFind(bv, str, false, true, cs, mw)) {
2310                 return true;
2311         }
2312         // we have to unlock ourself in this function by default!
2313         bv->unlockInset(const_cast<InsetText *>(this));
2314         return false;
2315 }