]> git.lyx.org Git - lyx.git/blob - src/insets/insettext.C
more dispatchresult changes
[lyx.git] / src / insets / insettext.C
1 /**
2  * \file insettext.C
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Jürgen Vigna
7  *
8  * Full author contact details are available in file CREDITS.
9  */
10
11 #include <config.h>
12
13 #include "insettext.h"
14 #include "insetnewline.h"
15
16 #include "buffer.h"
17 #include "bufferparams.h"
18 #include "BufferView.h"
19 #include "CutAndPaste.h"
20 #include "debug.h"
21 #include "dispatchresult.h"
22 #include "errorlist.h"
23 #include "funcrequest.h"
24 #include "gettext.h"
25 #include "intl.h"
26 #include "LColor.h"
27 #include "lyxfind.h"
28 #include "lyxlex.h"
29 #include "lyxrc.h"
30 #include "metricsinfo.h"
31 #include "paragraph.h"
32 #include "paragraph_funcs.h"
33 #include "ParagraphParameters.h"
34 #include "rowpainter.h"
35 #include "sgml.h"
36 #include "texrow.h"
37 #include "undo.h"
38 #include "WordLangTuple.h"
39
40 #include "frontends/Alert.h"
41 #include "frontends/font_metrics.h"
42 #include "frontends/LyXView.h"
43 #include "frontends/Painter.h"
44
45 #include "support/lyxalgo.h" // lyx::count
46
47 #include <boost/bind.hpp>
48
49 using bv_funcs::replaceSelection;
50
51 using lyx::pos_type;
52
53 using lyx::graphics::PreviewLoader;
54
55 using lyx::support::isStrUnsignedInt;
56 using lyx::support::strToUnsignedInt;
57
58 using std::endl;
59 using std::for_each;
60 using std::max;
61 using std::string;
62 using std::auto_ptr;
63 using std::ostream;
64 using std::vector;
65
66
67 InsetText::InsetText(BufferParams const & bp)
68         : UpdatableInset(),
69           paragraphs(1),
70           autoBreakRows_(false),
71           drawFrame_(NEVER),
72           frame_color_(LColor::insetframe),
73           text_(0, this, true, paragraphs)
74 {
75         textwidth_ = 0; // broken
76         paragraphs.begin()->layout(bp.getLyXTextClass().defaultLayout());
77         if (bp.tracking_changes)
78                 paragraphs.begin()->trackChanges();
79         init();
80 }
81
82
83 InsetText::InsetText(InsetText const & in)
84         : UpdatableInset(in),
85           text_(in.text_.bv_owner, this, true, paragraphs)
86 {
87         // this is ugly...
88         operator=(in);
89 }
90
91
92 void InsetText::operator=(InsetText const & in)
93 {
94         UpdatableInset::operator=(in);
95         paragraphs = in.paragraphs;
96         autoBreakRows_ = in.autoBreakRows_;
97         drawFrame_ = in.drawFrame_;
98         frame_color_ = in.frame_color_;
99         textwidth_ = in.textwidth_;
100         text_ = LyXText(in.text_.bv_owner, this, true, paragraphs);
101         init();
102 }
103
104
105 void InsetText::init()
106 {
107         ParagraphList::iterator pit = paragraphs.begin();
108         ParagraphList::iterator end = paragraphs.end();
109         for (; pit != end; ++pit)
110                 pit->setInsetOwner(this);
111         text_.paragraphs_ = &paragraphs;
112         top_y = 0;
113
114         locked = false;
115         inset_par = -1;
116         inset_pos = 0;
117         inset_x = 0;
118         inset_y = 0;
119         no_selection = true;
120         the_locking_inset = 0;
121         old_par = -1;
122         in_insetAllowed = false;
123         mouse_x = 0;
124         mouse_y = 0;
125 }
126
127
128 void InsetText::clear(bool just_mark_erased)
129 {
130         if (just_mark_erased) {
131                 ParagraphList::iterator it = paragraphs.begin();
132                 ParagraphList::iterator end = paragraphs.end();
133                 for (; it != end; ++it)
134                         it->markErased();
135                 return;
136         }
137
138         // This is a gross hack...
139         LyXLayout_ptr old_layout = paragraphs.begin()->layout();
140
141         paragraphs.clear();
142         paragraphs.push_back(Paragraph());
143         paragraphs.begin()->setInsetOwner(this);
144         paragraphs.begin()->layout(old_layout);
145 }
146
147
148 auto_ptr<InsetBase> InsetText::clone() const
149 {
150         return auto_ptr<InsetBase>(new InsetText(*this));
151 }
152
153
154 void InsetText::write(Buffer const & buf, ostream & os) const
155 {
156         os << "Text\n";
157         writeParagraphData(buf, os);
158 }
159
160
161 void InsetText::writeParagraphData(Buffer const & buf, ostream & os) const
162 {
163         ParagraphList::const_iterator it = paragraphs.begin();
164         ParagraphList::const_iterator end = paragraphs.end();
165         Paragraph::depth_type dth = 0;
166         for (; it != end; ++it) {
167                 it->write(buf, os, buf.params(), dth);
168         }
169 }
170
171
172 void InsetText::read(Buffer const & buf, LyXLex & lex)
173 {
174         string token;
175         Paragraph::depth_type depth = 0;
176
177         clear(false);
178
179 #warning John, look here. Doesnt make much sense.
180         if (buf.params().tracking_changes)
181                 paragraphs.begin()->trackChanges();
182
183         // delete the initial paragraph
184         Paragraph oldpar = *paragraphs.begin();
185         paragraphs.clear();
186         ParagraphList::iterator pit = paragraphs.begin();
187
188         while (lex.isOK()) {
189                 lex.nextToken();
190                 token = lex.getString();
191                 if (token.empty())
192                         continue;
193                 if (token == "\\end_inset") {
194                         break;
195                 }
196
197                 if (token == "\\end_document") {
198                         lex.printError("\\end_document read in inset! Error in document!");
199                         return;
200                 }
201
202                 // FIXME: ugly.
203                 const_cast<Buffer&>(buf).readParagraph(lex, token, paragraphs, pit, depth);
204         }
205
206         pit = paragraphs.begin();
207         ParagraphList::iterator const end = paragraphs.end();
208         for (; pit != end; ++pit)
209                 pit->setInsetOwner(this);
210
211         if (token != "\\end_inset") {
212                 lex.printError("Missing \\end_inset at this point. "
213                                            "Read: `$$Token'");
214         }
215
216         // sanity check
217         // ensure we have at least one par.
218         if (paragraphs.empty())
219                 paragraphs.push_back(oldpar);
220 }
221
222
223 void InsetText::metrics(MetricsInfo & mi, Dimension & dim) const
224 {
225         //lyxerr << "InsetText::metrics: width: " << mi.base.textwidth << endl;
226         textwidth_ = mi.base.textwidth - 30;
227         BufferView * bv = mi.base.bv;
228         setViewCache(bv);
229         text_.metrics(mi, dim);
230         dim.asc += TEXT_TO_INSET_OFFSET;
231         dim.des += TEXT_TO_INSET_OFFSET;
232         dim.wid += 2 * TEXT_TO_INSET_OFFSET;
233         dim.wid = max(dim.wid, 10);
234         dim_ = dim;
235 }
236
237
238 int InsetText::textWidth() const
239 {
240         return textwidth_;
241 }
242
243
244 void InsetText::draw(PainterInfo & pi, int x, int y) const
245 {
246         // update our idea of where we are. Clearly, we should
247         // not have to know this information.
248         top_x = x;
249
250         int const start_x = x;
251
252         BufferView * bv = pi.base.bv;
253         Painter & pain = pi.pain;
254
255         // repaint the background if needed
256         if (backgroundColor() != LColor::background)
257                 clearInset(bv, start_x + TEXT_TO_INSET_OFFSET, y);
258
259         // no draw is necessary !!!
260         if (drawFrame_ == LOCKED && !locked && paragraphs.begin()->empty()) {
261                 top_baseline = y;
262                 return;
263         }
264
265         bv->hideCursor();
266
267         if (!owner())
268                 x += scroll();
269
270         top_baseline = y;
271         top_y = y - dim_.asc;
272
273         if (the_locking_inset
274                   && text_.cursor.par() == inset_par && cpos() == inset_pos) {
275                 inset_x = cx() - x;
276                 inset_y = cy();
277         }
278
279         x += TEXT_TO_INSET_OFFSET;
280
281         paintTextInset(*bv, text_, x, y);
282
283         if (drawFrame_ == ALWAYS || (drawFrame_ == LOCKED && locked))
284                 drawFrame(pain, start_x);
285 }
286
287
288 void InsetText::drawFrame(Painter & pain, int x) const
289 {
290         int const ttoD2 = TEXT_TO_INSET_OFFSET / 2;
291         int const frame_x = x + ttoD2;
292         int const frame_y = top_baseline - dim_.asc + ttoD2;
293         int const frame_w = dim_.wid - TEXT_TO_INSET_OFFSET;
294         int const frame_h = dim_.asc + dim_.des - TEXT_TO_INSET_OFFSET;
295         pain.rectangle(frame_x, frame_y, frame_w, frame_h, frameColor());
296 }
297
298
299 void InsetText::updateLocal(BufferView * bv, bool /*mark_dirty*/)
300 {
301         if (!bv)
302                 return;
303
304         if (!autoBreakRows_ && paragraphs.size() > 1)
305                 collapseParagraphs(bv);
306
307         if (!text_.selection.set())
308                 text_.selection.cursor = text_.cursor;
309
310         bv->fitCursor();
311         bv->updateInset(this);
312         bv->owner()->view_state_changed();
313         bv->owner()->updateMenubar();
314         bv->owner()->updateToolbar();
315         if (old_par != text_.cursor.par()) {
316                 bv->owner()->setLayout(cpar()->layout()->name());
317                 old_par = text_.cursor.par();
318         }
319 }
320
321
322 string const InsetText::editMessage() const
323 {
324         return _("Opened Text Inset");
325 }
326
327
328 void InsetText::insetUnlock(BufferView * bv)
329 {
330         if (the_locking_inset) {
331                 the_locking_inset->insetUnlock(bv);
332                 the_locking_inset = 0;
333                 updateLocal(bv, false);
334         }
335         no_selection = true;
336         locked = false;
337
338         if (text_.selection.set())
339                 text_.clearSelection();
340         else if (owner())
341                 bv->owner()->setLayout(owner()->getLyXText(bv)
342                                        ->cursorPar()->layout()->name());
343         else
344                 bv->owner()->setLayout(bv->text->cursorPar()->layout()->name());
345         // hack for deleteEmptyParMech
346         if (!paragraphs.begin()->empty())
347                 text_.setCursor(0, 0);
348         else if (paragraphs.size() > 1)
349                 text_.setCursor(1, 0);
350 }
351
352
353 void InsetText::lockInset(BufferView * bv)
354 {
355         locked = true;
356         the_locking_inset = 0;
357         inset_pos = inset_x = inset_y = 0;
358         inset_boundary = false;
359         inset_par =  -1;
360         old_par = -1;
361         text_.setCursorIntern(0, 0);
362         text_.clearSelection();
363         finishUndo();
364         // If the inset is empty set the language of the current font to the
365         // language to the surronding text (if different).
366         if (paragraphs.begin()->empty() && paragraphs.size() == 1 &&
367                 bv->getParentLanguage(this) != text_.current_font.language()) {
368                 LyXFont font(LyXFont::ALL_IGNORE);
369                 font.setLanguage(bv->getParentLanguage(this));
370                 setFont(bv, font, false);
371         }
372 }
373
374
375 void InsetText::lockInset(BufferView * /*bv*/, UpdatableInset * inset)
376 {
377         the_locking_inset = inset;
378         inset_x = cx() - top_x;
379         inset_y = cy();
380         inset_pos = cpos();
381         inset_par = text_.cursor.par();
382         inset_boundary = cboundary();
383 }
384
385
386 bool InsetText::lockInsetInInset(BufferView * bv, UpdatableInset * inset)
387 {
388         lyxerr[Debug::INSETS] << "InsetText::LockInsetInInset("
389                               << inset << "): " << endl;
390         if (!inset)
391                 return false;
392         if (!the_locking_inset) {
393                 ParagraphList::iterator pit = paragraphs.begin();
394                 ParagraphList::iterator pend = paragraphs.end();
395
396                 for (; pit != pend; ++pit) {
397                         InsetList::iterator it = pit->insetlist.begin();
398                         InsetList::iterator const end = pit->insetlist.end();
399                         for (; it != end; ++it) {
400                                 if (it->inset == inset) {
401                                         lyxerr << "InsetText::lockInsetInInset: 1 a" << endl;
402                                         text_.setCursorIntern(
403                                                 std::distance(paragraphs.begin(), pit), it->pos);
404                                         lyxerr << "InsetText::lockInsetInInset: 1 b" << endl;
405                                         lyxerr << "bv: " << bv << " inset: " << inset << endl;
406                                         lockInset(bv, inset);
407                                         lyxerr << "InsetText::lockInsetInInset: 1 c" << endl;
408                                         return true;
409                                 }
410                         }
411                 }
412                 lyxerr << "InsetText::lockInsetInInset: 3" << endl;
413                 return false;
414         }
415         if (inset == cpar()->getInset(cpos())) {
416                 lyxerr[Debug::INSETS] << "OK" << endl;
417                 lockInset(bv, inset);
418                 return true;
419         }
420
421         if (the_locking_inset && the_locking_inset == inset) {
422                 if (text_.cursor.par() == inset_par && cpos() == inset_pos) {
423                         lyxerr[Debug::INSETS] << "OK" << endl;
424                         inset_x = cx() - top_x;
425                         inset_y = cy();
426                 } else {
427                         lyxerr[Debug::INSETS] << "cursor.pos != inset_pos" << endl;
428                 }
429         } else if (the_locking_inset) {
430                 lyxerr[Debug::INSETS] << "MAYBE" << endl;
431                 return the_locking_inset->lockInsetInInset(bv, inset);
432         }
433         lyxerr[Debug::INSETS] << "NOT OK" << endl;
434         return false;
435 }
436
437
438 bool InsetText::unlockInsetInInset(BufferView * bv, UpdatableInset * inset,
439                                    bool lr)
440 {
441         if (!the_locking_inset)
442                 return false;
443         if (the_locking_inset == inset) {
444                 the_locking_inset->insetUnlock(bv);
445                 the_locking_inset = 0;
446                 if (lr)
447                         moveRightIntern(bv, true, false);
448                 old_par = -1; // force layout setting
449                 if (scroll())
450                         scroll(bv, 0.0F);
451                 else
452                         updateLocal(bv, false);
453                 return true;
454         }
455         return the_locking_inset->unlockInsetInInset(bv, inset, lr);
456 }
457
458
459 void InsetText::lfunMousePress(FuncRequest const & cmd)
460 {
461         no_selection = true;
462
463         // use this to check mouse motion for selection!
464         mouse_x = cmd.x;
465         mouse_y = cmd.y;
466
467         BufferView * bv = cmd.view();
468         FuncRequest cmd1 = cmd;
469         cmd1.x -= inset_x;
470         cmd1.y -= inset_y;
471         if (!locked)
472                 lockInset(bv);
473
474         int tmp_x = cmd.x;
475         int tmp_y = cmd.y + dim_.asc - bv->top_y();
476         InsetOld * inset = getLyXText(bv)->checkInsetHit(tmp_x, tmp_y);
477
478         if (the_locking_inset) {
479                 if (the_locking_inset == inset) {
480                         the_locking_inset->dispatch(cmd1);
481                         return;
482                 }
483                 // otherwise only unlock the_locking_inset
484                 the_locking_inset->insetUnlock(bv);
485                 the_locking_inset = 0;
486         }
487         if (!inset)
488                 no_selection = false;
489
490         if (bv->theLockingInset()) {
491                 if (isHighlyEditableInset(inset)) {
492                         // We just have to lock the inset before calling a
493                         // PressEvent on it!
494                         UpdatableInset * uinset = static_cast<UpdatableInset*>(inset);
495                         if (!bv->lockInset(uinset)) {
496                                 lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
497                         }
498                         inset->dispatch(cmd1);
499                         if (the_locking_inset)
500                                 updateLocal(bv, false);
501                         return;
502                 }
503         }
504         if (!inset) {
505                 bool paste_internally = false;
506                 if (cmd.button() == mouse_button::button2 && getLyXText(bv)->selection.set()) {
507                         dispatch(FuncRequest(bv, LFUN_COPY));
508                         paste_internally = true;
509                 }
510                 int old_top_y = bv->top_y();
511
512                 text_.setCursorFromCoordinates(cmd.x, cmd.y + dim_.asc);
513                 // set the selection cursor!
514                 text_.selection.cursor = text_.cursor;
515                 text_.cursor.x_fix(text_.cursor.x());
516
517                 text_.clearSelection();
518                 updateLocal(bv, false);
519
520                 bv->owner()->setLayout(cpar()->layout()->name());
521
522                 // we moved the view we cannot do mouse selection in this case!
523                 if (bv->top_y() != old_top_y)
524                         no_selection = true;
525                 old_par = text_.cursor.par();
526                 // Insert primary selection with middle mouse
527                 // if there is a local selection in the current buffer,
528                 // insert this
529                 if (cmd.button() == mouse_button::button2) {
530                         if (paste_internally)
531                                 dispatch(FuncRequest(bv, LFUN_PASTE));
532                         else
533                                 dispatch(FuncRequest(bv, LFUN_PASTESELECTION, "paragraph"));
534                 }
535         } else {
536                 getLyXText(bv)->clearSelection();
537         }
538 }
539
540
541 bool InsetText::lfunMouseRelease(FuncRequest const & cmd)
542 {
543         BufferView * bv = cmd.view();
544         FuncRequest cmd1 = cmd;
545         cmd1.x -= inset_x;
546         cmd1.y -= inset_y;
547
548         no_selection = true;
549         if (the_locking_inset) {
550                 DispatchResult const res = the_locking_inset->dispatch(cmd1);
551
552                 return res.dispatched() || res.val() >= NOUPDATE;
553         }
554
555         int tmp_x = cmd.x;
556         int tmp_y = cmd.y + dim_.asc - bv->top_y();
557         InsetOld * inset = getLyXText(bv)->checkInsetHit(tmp_x, tmp_y);
558         if (!inset)
559                 return false;
560
561         // We still need to deal properly with the whole relative vs.
562         // absolute mouse co-ords thing in a realiable, sensible way
563         DispatchResult const res = inset->dispatch(cmd1);
564         bool const ret = res.dispatched() || res.val() >= NOUPDATE;
565         updateLocal(bv, false);
566         return ret;
567 }
568
569
570 void InsetText::lfunMouseMotion(FuncRequest const & cmd)
571 {
572         FuncRequest cmd1 = cmd;
573         cmd1.x -= inset_x;
574         cmd1.y -= inset_y;
575
576         if (the_locking_inset) {
577                 the_locking_inset->dispatch(cmd1);
578                 return;
579         }
580
581         if (no_selection || (mouse_x == cmd.x && mouse_y == cmd.y))
582                 return;
583
584         BufferView * bv = cmd.view();
585         LyXCursor cur = text_.cursor;
586         text_.setCursorFromCoordinates (cmd.x, cmd.y + dim_.asc);
587         text_.cursor.x_fix(text_.cursor.x());
588         if (cur == text_.cursor)
589                 return;
590         text_.setSelection();
591         updateLocal(bv, false);
592 }
593
594
595 DispatchResult
596 InsetText::priv_dispatch(FuncRequest const & cmd,
597                          idx_type & idx, pos_type & pos)
598 {
599         BufferView * bv = cmd.view();
600         setViewCache(bv);
601
602         switch (cmd.action) {
603         case LFUN_INSET_EDIT: {
604                 UpdatableInset::priv_dispatch(cmd, idx, pos);
605
606                 if (!bv->lockInset(this)) {
607                         lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
608                         return DispatchResult(true);
609                 }
610
611                 locked = true;
612                 the_locking_inset = 0;
613                 inset_pos = 0;
614                 inset_x = 0;
615                 inset_y = 0;
616                 inset_boundary = false;
617                 inset_par = -1;
618                 old_par = -1;
619
620
621                 if (cmd.argument.size()) {
622                         if (cmd.argument == "left")
623                                 text_.setCursorIntern(0, 0);
624                         else
625                                 text_.setCursor(paragraphs.size() - 1, paragraphs.back().size());
626                 } else {
627                         int tmp_y = (cmd.y < 0) ? 0 : cmd.y;
628                         // we put here -1 and not button as now the button in the
629                         // edit call should not be needed we will fix this in 1.3.x
630                         // cycle hopefully (Jug 20020509)
631                         // FIXME: GUII I've changed this to none: probably WRONG
632                         if (!checkAndActivateInset(bv, cmd.x, tmp_y, mouse_button::none)) {
633                                 text_.setCursorFromCoordinates(cmd.x, cmd.y + dim_.asc);
634                                 text_.cursor.x(text_.cursor.x());
635                                 text_.cursor.x_fix(text_.cursor.x());
636                         }
637                 }
638
639                 text_.clearSelection();
640                 finishUndo();
641
642                 // If the inset is empty set the language of the current font to the
643                 // language to the surronding text (if different).
644                 if (paragraphs.begin()->empty() &&
645                     paragraphs.size() == 1 &&
646                     bv->getParentLanguage(this) != text_.current_font.language())
647                 {
648                         LyXFont font(LyXFont::ALL_IGNORE);
649                         font.setLanguage(bv->getParentLanguage(this));
650                         setFont(bv, font, false);
651                 }
652
653                 updateLocal(bv, false);
654                 // Tell the paragraph dialog that we've entered an insettext.
655                 bv->dispatch(FuncRequest(LFUN_PARAGRAPH_UPDATE));
656                 return DispatchResult(true);
657         }
658
659         case LFUN_MOUSE_PRESS:
660                 lfunMousePress(cmd);
661                 return DispatchResult(true);
662
663         case LFUN_MOUSE_MOTION:
664                 lfunMouseMotion(cmd);
665                 return DispatchResult(true);
666
667         case LFUN_MOUSE_RELEASE:
668                 return DispatchResult(lfunMouseRelease(cmd));
669
670         default:
671                 break;
672         }
673
674         bool was_empty = paragraphs.begin()->empty() && paragraphs.size() == 1;
675         no_selection = false;
676
677         DispatchResult result = UpdatableInset::priv_dispatch(cmd, idx, pos);
678         if (result.dispatched())
679                 return DispatchResult(true);
680
681         result = DispatchResult(true);
682         if (cmd.action < 0 && cmd.argument.empty())
683                 return DispatchResult(false, FINISHED);
684
685         if (the_locking_inset) {
686                 result = the_locking_inset->dispatch(cmd);
687
688                 if (result.dispatched()) {
689                         if (result.val() != NOUPDATE)
690                                 updateLocal(bv, false);
691                         return result;
692                 }
693
694                 switch (result.val()) {
695                 case FINISHED_RIGHT:
696                         moveRightIntern(bv, false, false);
697                         result = DispatchResult(true);
698                         break;
699                 case FINISHED_UP:
700                         result = moveUp(bv);
701                         if (result.val() >= FINISHED) {
702                                 updateLocal(bv, false);
703                                 bv->unlockInset(this);
704                         }
705                         break;
706                 case FINISHED_DOWN:
707                         result = moveDown(bv);
708                         if (result.val() >= FINISHED) {
709                                 updateLocal(bv, false);
710                                 bv->unlockInset(this);
711                         }
712                         break;
713                 default:
714                         result = DispatchResult(true);
715                         break;
716                 }
717                 the_locking_inset = 0;
718                 updateLocal(bv, false);
719                 // make sure status gets reset immediately
720                 bv->owner()->clearMessage();
721                 return result;
722         }
723         bool updflag = false;
724
725         switch (cmd.action) {
726
727         // Normal chars
728         case LFUN_SELFINSERT:
729                 if (bv->buffer()->isReadonly()) {
730 //          setErrorMessage(N_("Document is read only"));
731                         break;
732                 }
733                 if (!cmd.argument.empty()) {
734                         /* Automatically delete the currently selected
735                          * text and replace it with what is being
736                          * typed in now. Depends on lyxrc settings
737                          * "auto_region_delete", which defaults to
738                          * true (on). */
739 #if 0
740                         // This should not be needed here and is also WRONG!
741                         recordUndo(bv, Undo::INSERT, text_.cursorPar());
742 #endif
743                         bv->switchKeyMap();
744
745                         if (lyxrc.auto_region_delete && text_.selection.set())
746                                 text_.cutSelection(false, false);
747                         text_.clearSelection();
748
749                         for (string::size_type i = 0; i < cmd.argument.length(); ++i)
750                                 bv->owner()->getIntl().getTransManager().
751                                         TranslateAndInsert(cmd.argument[i], &text_);
752                 }
753                 text_.selection.cursor = text_.cursor;
754                 updflag = true;
755                 result = DispatchResult(true, NOUPDATE);
756                 break;
757
758         // cursor movements that need special handling
759
760         case LFUN_RIGHT:
761                 result = moveRight(bv);
762                 finishUndo();
763                 break;
764         case LFUN_LEFT:
765                 finishUndo();
766                 result = moveLeft(bv);
767                 break;
768         case LFUN_DOWN:
769                 finishUndo();
770                 result = moveDown(bv);
771                 break;
772         case LFUN_UP:
773                 finishUndo();
774                 result = moveUp(bv);
775                 break;
776
777         case LFUN_PRIOR:
778                 if (crow() == text_.firstRow())
779                         result = DispatchResult(false, FINISHED_UP);
780                 else {
781                         text_.cursorPrevious();
782                         text_.clearSelection();
783                         result = DispatchResult(true, NOUPDATE);
784                 }
785                 break;
786
787         case LFUN_NEXT:
788                 if (crow() == text_.lastRow())
789                         result = DispatchResult(false, FINISHED_DOWN);
790                 else {
791                         text_.cursorNext();
792                         text_.clearSelection();
793                         result = DispatchResult(true, NOUPDATE);
794                 }
795                 break;
796
797         case LFUN_BACKSPACE:
798                 if (text_.selection.set())
799                         text_.cutSelection(true, false);
800                 else
801                         text_.backspace();
802                 updflag = true;
803                 break;
804
805         case LFUN_DELETE:
806                 if (text_.selection.set())
807                         text_.cutSelection(true, false);
808                 else
809                         text_.Delete();
810                 updflag = true;
811                 break;
812
813         case LFUN_PASTE:
814                 if (!autoBreakRows_) {
815                         if (CutAndPaste::nrOfParagraphs() > 1) {
816 #ifdef WITH_WARNINGS
817 #warning FIXME horrendously bad UI
818 #endif
819                                 Alert::error(_("Paste failed"), _("Cannot include more than one paragraph."));
820                         }
821                 } else {
822                         replaceSelection(bv->getLyXText());
823                         size_t sel_index = 0;
824                         string const & arg = cmd.argument;
825                         if (isStrUnsignedInt(arg)) {
826 #warning FIXME Check if the arg is in the domain of available selections.
827                                 sel_index = strToUnsignedInt(arg);
828                         }
829                         text_.pasteSelection(sel_index);
830                         // bug 393
831                         text_.clearSelection();
832                         updflag = true;
833                 }
834                 break;
835
836         case LFUN_BREAKPARAGRAPH:
837                 if (!autoBreakRows_) {
838                         result = DispatchResult(true);
839                 } else {
840                         replaceSelection(bv->getLyXText());
841                         text_.breakParagraph(paragraphs, 0);
842                         updflag = true;
843                 }
844                 break;
845
846         case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
847                 if (!autoBreakRows_) {
848                         result = DispatchResult(true);
849                 } else {
850                         replaceSelection(bv->getLyXText());
851                         text_.breakParagraph(paragraphs, 1);
852                         updflag = true;
853                 }
854                 break;
855
856         case LFUN_BREAKLINE: {
857                 if (!autoBreakRows_) {
858                         result = DispatchResult(true);
859                 } else {
860                         replaceSelection(bv->getLyXText());
861                         text_.insertInset(new InsetNewline);
862                         updflag = true;
863                 }
864                 break;
865         }
866
867         case LFUN_LAYOUT:
868                 // do not set layouts on non breakable textinsets
869                 if (autoBreakRows_) {
870                         string cur_layout = cpar()->layout()->name();
871
872                         // Derive layout number from given argument (string)
873                         // and current buffer's textclass (number).
874                         LyXTextClass const & tclass =
875                                 bv->buffer()->params().getLyXTextClass();
876                         string layout = cmd.argument;
877                         bool hasLayout = tclass.hasLayout(layout);
878
879                         // If the entry is obsolete, use the new one instead.
880                         if (hasLayout) {
881                                 string const & obs = tclass[layout]->obsoleted_by();
882                                 if (!obs.empty())
883                                         layout = obs;
884                         }
885
886                         // see if we found the layout number:
887                         if (!hasLayout) {
888                                 FuncRequest lf(LFUN_MESSAGE, N_("Layout ") + cmd.argument + N_(" not known"));
889                                 bv->owner()->dispatch(lf);
890                                 break;
891                         }
892
893                         if (cur_layout != layout) {
894                                 cur_layout = layout;
895                                 text_.setLayout(layout);
896                                 bv->owner()->setLayout(cpar()->layout()->name());
897                                 updflag = true;
898                         }
899                 } else {
900                         // reset the layout box
901                         bv->owner()->setLayout(cpar()->layout()->name());
902                 }
903                 break;
904
905         default:
906                 if (!bv->dispatch(cmd))
907                         result = DispatchResult(false);
908                 break;
909         }
910
911         updateLocal(bv, updflag);
912
913         /// If the action has deleted all text in the inset, we need to change the
914         // language to the language of the surronding text.
915         if (!was_empty && paragraphs.begin()->empty() &&
916             paragraphs.size() == 1) {
917                 LyXFont font(LyXFont::ALL_IGNORE);
918                 font.setLanguage(bv->getParentLanguage(this));
919                 setFont(bv, font, false);
920         }
921
922         if (result.val() >= FINISHED)
923                 bv->unlockInset(this);
924
925         if (result.val() == NOUPDATE)
926                 result = DispatchResult(true);
927         return result;
928 }
929
930
931 int InsetText::latex(Buffer const & buf, ostream & os,
932                      LatexRunParams const & runparams) const
933 {
934         TexRow texrow;
935         latexParagraphs(buf, paragraphs, os, texrow, runparams);
936         return texrow.rows();
937 }
938
939
940 int InsetText::ascii(Buffer const & buf, ostream & os,
941                      LatexRunParams const & runparams) const
942 {
943         ParagraphList::const_iterator beg = paragraphs.begin();
944         ParagraphList::const_iterator end = paragraphs.end();
945         ParagraphList::const_iterator it = beg;
946         for (; it != end; ++it)
947                 asciiParagraph(buf, *it, os, runparams, it == beg);
948
949         //FIXME: Give the total numbers of lines
950         return 0;
951 }
952
953
954 int InsetText::linuxdoc(Buffer const & buf, ostream & os,
955                         LatexRunParams const & runparams) const
956 {
957         linuxdocParagraphs(buf, paragraphs, os, runparams);
958         return 0;
959 }
960
961
962 int InsetText::docbook(Buffer const & buf, ostream & os,
963                        LatexRunParams const & runparams) const
964 {
965         docbookParagraphs(buf, paragraphs, os, runparams);
966         return 0;
967 }
968
969
970 void InsetText::validate(LaTeXFeatures & features) const
971 {
972         for_each(paragraphs.begin(), paragraphs.end(),
973                  boost::bind(&Paragraph::validate, _1, boost::ref(features)));
974 }
975
976
977 void InsetText::getCursor(BufferView & bv, int & x, int & y) const
978 {
979         if (the_locking_inset) {
980                 the_locking_inset->getCursor(bv, x, y);
981                 return;
982         }
983         x = cx();
984         y = cy() + InsetText::y();
985 }
986
987
988 void InsetText::getCursorPos(BufferView * bv, int & x, int & y) const
989 {
990         if (the_locking_inset) {
991                 the_locking_inset->getCursorPos(bv, x, y);
992                 return;
993         }
994         x = cx() - top_x - TEXT_TO_INSET_OFFSET;
995         y = cy() - TEXT_TO_INSET_OFFSET;
996 }
997
998
999 int InsetText::insetInInsetY() const
1000 {
1001         if (!the_locking_inset)
1002                 return 0;
1003
1004         return inset_y + the_locking_inset->insetInInsetY();
1005 }
1006
1007
1008 void InsetText::fitInsetCursor(BufferView * bv) const
1009 {
1010         if (the_locking_inset) {
1011                 the_locking_inset->fitInsetCursor(bv);
1012                 return;
1013         }
1014
1015         LyXFont const font = text_.getFont(cpar(), cpos());
1016
1017         int const asc = font_metrics::maxAscent(font);
1018         int const desc = font_metrics::maxDescent(font);
1019
1020         bv->fitLockedInsetCursor(cx(), cy(), asc, desc);
1021 }
1022
1023
1024 DispatchResult InsetText::moveRight(BufferView * bv)
1025 {
1026         if (text_.cursorPar()->isRightToLeftPar(bv->buffer()->params()))
1027                 return moveLeftIntern(bv, false, true, false);
1028         else
1029                 return moveRightIntern(bv, true, true, false);
1030 }
1031
1032
1033 DispatchResult InsetText::moveLeft(BufferView * bv)
1034 {
1035         if (text_.cursorPar()->isRightToLeftPar(bv->buffer()->params()))
1036                 return moveRightIntern(bv, true, true, false);
1037         else
1038                 return moveLeftIntern(bv, false, true, false);
1039 }
1040
1041
1042 DispatchResult
1043 InsetText::moveRightIntern(BufferView * bv, bool front,
1044                            bool activate_inset, bool selecting)
1045 {
1046         ParagraphList::iterator c_par = cpar();
1047
1048         if (boost::next(c_par) == paragraphs.end() && cpos() >= c_par->size())
1049                 return DispatchResult(false, FINISHED_RIGHT);
1050         if (activate_inset && checkAndActivateInset(bv, front))
1051                 return DispatchResult(true);
1052         text_.cursorRight(bv);
1053         if (!selecting)
1054                 text_.clearSelection();
1055         return DispatchResult(true, NOUPDATE);
1056 }
1057
1058
1059 DispatchResult
1060 InsetText::moveLeftIntern(BufferView * bv, bool front,
1061                           bool activate_inset, bool selecting)
1062 {
1063         if (cpar() == paragraphs.begin() && cpos() <= 0)
1064                 return DispatchResult(false, FINISHED);
1065         text_.cursorLeft(bv);
1066         if (!selecting)
1067                 text_.clearSelection();
1068         if (activate_inset && checkAndActivateInset(bv, front))
1069                 return DispatchResult(true);
1070         return DispatchResult(true, NOUPDATE);
1071 }
1072
1073
1074 DispatchResult InsetText::moveUp(BufferView * bv)
1075 {
1076         if (crow() == text_.firstRow())
1077                 return DispatchResult(false, FINISHED_UP);
1078         text_.cursorUp(bv);
1079         text_.clearSelection();
1080         return DispatchResult(true, NOUPDATE);
1081 }
1082
1083
1084 DispatchResult InsetText::moveDown(BufferView * bv)
1085 {
1086         if (crow() == text_.lastRow())
1087                 return DispatchResult(false, FINISHED_DOWN);
1088         text_.cursorDown(bv);
1089         text_.clearSelection();
1090         return DispatchResult(true, NOUPDATE);
1091 }
1092
1093
1094 bool InsetText::insertInset(BufferView * bv, InsetOld * inset)
1095 {
1096         if (the_locking_inset) {
1097                 if (the_locking_inset->insetAllowed(inset))
1098                         return the_locking_inset->insertInset(bv, inset);
1099                 return false;
1100         }
1101         inset->setOwner(this);
1102         text_.insertInset(inset);
1103         bv->fitCursor();
1104         updateLocal(bv, true);
1105         return true;
1106 }
1107
1108
1109 bool InsetText::insetAllowed(InsetOld::Code code) const
1110 {
1111         // in_insetAllowed is a really gross hack,
1112         // to allow us to call the owner's insetAllowed
1113         // without stack overflow, which can happen
1114         // when the owner uses InsetCollapsable::insetAllowed()
1115         bool ret = true;
1116         if (in_insetAllowed)
1117                 return ret;
1118         in_insetAllowed = true;
1119         if (the_locking_inset)
1120                 ret = the_locking_inset->insetAllowed(code);
1121         else if (owner())
1122                 ret = owner()->insetAllowed(code);
1123         in_insetAllowed = false;
1124         return ret;
1125 }
1126
1127
1128 UpdatableInset * InsetText::getLockingInset() const
1129 {
1130         return the_locking_inset ? the_locking_inset->getLockingInset() :
1131                 const_cast<InsetText *>(this);
1132 }
1133
1134
1135 UpdatableInset * InsetText::getFirstLockingInsetOfType(InsetOld::Code c)
1136 {
1137         if (c == lyxCode())
1138                 return this;
1139         if (the_locking_inset)
1140                 return the_locking_inset->getFirstLockingInsetOfType(c);
1141         return 0;
1142 }
1143
1144
1145 bool InsetText::showInsetDialog(BufferView * bv) const
1146 {
1147         if (the_locking_inset)
1148                 return the_locking_inset->showInsetDialog(bv);
1149         return false;
1150 }
1151
1152
1153 void InsetText::getLabelList(Buffer const & buffer,
1154                              std::vector<string> & list) const
1155 {
1156         ParagraphList::const_iterator pit = paragraphs.begin();
1157         ParagraphList::const_iterator pend = paragraphs.end();
1158         for (; pit != pend; ++pit) {
1159                 InsetList::const_iterator beg = pit->insetlist.begin();
1160                 InsetList::const_iterator end = pit->insetlist.end();
1161                 for (; beg != end; ++beg)
1162                         beg->inset->getLabelList(buffer, list);
1163         }
1164 }
1165
1166
1167 void InsetText::setFont(BufferView * bv, LyXFont const & font, bool toggleall,
1168                         bool selectall)
1169 {
1170         if (the_locking_inset) {
1171                 the_locking_inset->setFont(bv, font, toggleall, selectall);
1172                 return;
1173         }
1174
1175         if ((paragraphs.size() == 1 && paragraphs.begin()->empty())
1176             || cpar()->empty()) {
1177                 text_.setFont(font, toggleall);
1178                 return;
1179         }
1180
1181
1182         if (text_.selection.set())
1183                 text_.recUndo(text_.cursor.par());
1184
1185         if (selectall) {
1186                 text_.cursorTop();
1187                 text_.selection.cursor = text_.cursor;
1188                 text_.cursorBottom();
1189                 text_.setSelection();
1190         }
1191
1192         text_.toggleFree(font, toggleall);
1193
1194         if (selectall)
1195                 text_.clearSelection();
1196
1197         bv->fitCursor();
1198         updateLocal(bv, true);
1199 }
1200
1201
1202 bool InsetText::checkAndActivateInset(BufferView * bv, bool front)
1203 {
1204         if (cpos() == cpar()->size())
1205                 return false;
1206         InsetOld * inset = cpar()->getInset(cpos());
1207         if (!isHighlyEditableInset(inset))
1208                 return false;
1209         FuncRequest cmd(bv, LFUN_INSET_EDIT, front ? "left" : "right");
1210         inset->dispatch(cmd);
1211         if (!the_locking_inset)
1212                 return false;
1213         updateLocal(bv, false);
1214         return true;
1215 }
1216
1217
1218 bool InsetText::checkAndActivateInset(BufferView * bv, int x, int y,
1219                                       mouse_button::state button)
1220 {
1221         int dummyx = x;
1222         int dummyy = y + dim_.asc;
1223         InsetOld * inset = getLyXText(bv)->checkInsetHit(dummyx, dummyy);
1224         // we only do the edit() call if the inset was hit by the mouse
1225         // or if it is a highly editable inset. So we should call this
1226         // function from our own edit with button < 0.
1227         // FIXME: GUII jbl. I've changed this to ::none for now which is probably
1228         // WRONG
1229         if (button == mouse_button::none && !isHighlyEditableInset(inset))
1230                 return false;
1231
1232         if (!inset)
1233                 return false;
1234         if (x < 0)
1235                 x = dim_.wid;
1236         if (y < 0)
1237                 y = dim_.des;
1238         inset_x = cx() - top_x;
1239         inset_y = cy();
1240         FuncRequest cmd(bv, LFUN_INSET_EDIT, x - inset_x, y - inset_y, button);
1241         inset->dispatch(cmd);
1242         if (!the_locking_inset)
1243                 return false;
1244         updateLocal(bv, false);
1245         return true;
1246 }
1247
1248
1249 void InsetText::markNew(bool track_changes)
1250 {
1251         ParagraphList::iterator pit = paragraphs.begin();
1252         ParagraphList::iterator end = paragraphs.end();
1253         for (; pit != end; ++pit) {
1254                 if (track_changes) {
1255                         pit->trackChanges();
1256                 } else {
1257                         // no-op when not tracking
1258                         pit->cleanChanges();
1259                 }
1260         }
1261 }
1262
1263
1264 void InsetText::setText(string const & data, LyXFont const & font)
1265 {
1266         clear(false);
1267         for (unsigned int i = 0; i < data.length(); ++i)
1268                 paragraphs.begin()->insertChar(i, data[i], font);
1269 }
1270
1271
1272 void InsetText::setAutoBreakRows(bool flag)
1273 {
1274         if (flag != autoBreakRows_) {
1275                 autoBreakRows_ = flag;
1276                 if (!flag)
1277                         removeNewlines();
1278         }
1279 }
1280
1281
1282 void InsetText::setDrawFrame(DrawFrame how)
1283 {
1284         drawFrame_ = how;
1285 }
1286
1287
1288 LColor_color InsetText::frameColor() const
1289 {
1290         return LColor::color(frame_color_);
1291 }
1292
1293
1294 void InsetText::setFrameColor(LColor_color col)
1295 {
1296         frame_color_ = col;
1297 }
1298
1299
1300 int InsetText::cx() const
1301 {
1302         int x = text_.cursor.x() + top_x + TEXT_TO_INSET_OFFSET;
1303         if (the_locking_inset) {
1304                 LyXFont font = text_.getFont(text_.cursorPar(), text_.cursor.pos());
1305                 if (font.isVisibleRightToLeft())
1306                         x -= the_locking_inset->width();
1307         }
1308         return x;
1309 }
1310
1311
1312 int InsetText::cy() const
1313 {
1314         return text_.cursor.y() - dim_.asc + TEXT_TO_INSET_OFFSET;
1315 }
1316
1317
1318 pos_type InsetText::cpos() const
1319 {
1320         return text_.cursor.pos();
1321 }
1322
1323
1324 ParagraphList::iterator InsetText::cpar() const
1325 {
1326         return text_.cursorPar();
1327 }
1328
1329
1330 bool InsetText::cboundary() const
1331 {
1332         return text_.cursor.boundary();
1333 }
1334
1335
1336 RowList::iterator InsetText::crow() const
1337 {
1338         return cpar()->getRow(cpos());
1339 }
1340
1341
1342 LyXText * InsetText::getLyXText(BufferView const * bv,
1343                                 bool const recursive) const
1344 {
1345         setViewCache(bv);
1346         if (recursive && the_locking_inset)
1347                 return the_locking_inset->getLyXText(bv, true);
1348         return &text_;
1349 }
1350
1351
1352 void InsetText::setViewCache(BufferView const * bv) const
1353 {
1354         if (bv) {
1355                 if (bv != text_.bv_owner) {
1356                         //lyxerr << "setting view cache from "
1357                         //      << text_.bv_owner << " to " << bv << "\n";
1358                         text_.init(const_cast<BufferView *>(bv));
1359                 }
1360                 text_.bv_owner = const_cast<BufferView *>(bv);
1361         }
1362 }
1363
1364
1365 void InsetText::deleteLyXText(BufferView * bv, bool recursive) const
1366 {
1367         if (recursive) {
1368                 /// then remove all LyXText in text-insets
1369                 for_each(const_cast<ParagraphList&>(paragraphs).begin(),
1370                          const_cast<ParagraphList&>(paragraphs).end(),
1371                          boost::bind(&Paragraph::deleteInsetsLyXText, _1, bv));
1372         }
1373 }
1374
1375
1376 void InsetText::removeNewlines()
1377 {
1378         ParagraphList::iterator it = paragraphs.begin();
1379         ParagraphList::iterator end = paragraphs.end();
1380         for (; it != end; ++it)
1381                 for (int i = 0; i < it->size(); ++i)
1382                         if (it->isNewline(i))
1383                                 it->erase(i);
1384 }
1385
1386
1387 int InsetText::scroll(bool recursive) const
1388 {
1389         int sx = UpdatableInset::scroll(false);
1390
1391         if (recursive && the_locking_inset)
1392                 sx += the_locking_inset->scroll(recursive);
1393
1394         return sx;
1395 }
1396
1397
1398 void InsetText::clearSelection(BufferView *)
1399 {
1400         text_.clearSelection();
1401 }
1402
1403
1404 void InsetText::clearInset(BufferView * bv, int start_x, int baseline) const
1405 {
1406         Painter & pain = bv->painter();
1407         int w = dim_.wid;
1408         int h = dim_.asc + dim_.des;
1409         int ty = baseline - dim_.asc;
1410
1411         if (ty < 0) {
1412                 h += ty;
1413                 ty = 0;
1414         }
1415         if (ty + h > pain.paperHeight())
1416                 h = pain.paperHeight();
1417         if (top_x + w > pain.paperWidth())
1418                 w = pain.paperWidth();
1419         pain.fillRectangle(start_x + 1, ty + 1, w - 3, h - 1, backgroundColor());
1420 }
1421
1422
1423 ParagraphList * InsetText::getParagraphs(int i) const
1424 {
1425         return (i == 0) ? const_cast<ParagraphList*>(&paragraphs) : 0;
1426 }
1427
1428
1429 LyXText * InsetText::getText(int i) const
1430 {
1431         return (i == 0) ? const_cast<LyXText*>(&text_) : 0;
1432 }
1433
1434
1435 LyXCursor const & InsetText::cursor(BufferView * bv) const
1436 {
1437         if (the_locking_inset)
1438                 return the_locking_inset->cursor(bv);
1439         return getLyXText(bv)->cursor;
1440 }
1441
1442
1443 InsetOld * InsetText::getInsetFromID(int id_arg) const
1444 {
1445         if (id_arg == id())
1446                 return const_cast<InsetText *>(this);
1447
1448         ParagraphList::const_iterator pit = paragraphs.begin();
1449         ParagraphList::const_iterator pend = paragraphs.end();
1450         for (; pit != pend; ++pit) {
1451                 InsetList::const_iterator it = pit->insetlist.begin();
1452                 InsetList::const_iterator end = pit->insetlist.end();
1453                 for (; it != end; ++it) {
1454                         if (it->inset->id() == id_arg)
1455                                 return it->inset;
1456                         InsetOld * in = it->inset->getInsetFromID(id_arg);
1457                         if (in)
1458                                 return in;
1459                 }
1460         }
1461         return 0;
1462 }
1463
1464
1465 WordLangTuple const
1466 InsetText::selectNextWordToSpellcheck(BufferView * bv, float & value) const
1467 {
1468         WordLangTuple word;
1469         if (the_locking_inset) {
1470                 word = the_locking_inset->selectNextWordToSpellcheck(bv, value);
1471                 if (!word.word().empty()) {
1472                         value += cy();
1473                         return word;
1474                 }
1475                 // we have to go on checking so move cursor to the next char
1476                 text_.cursor.pos(text_.cursor.pos() + 1);
1477         }
1478         word = text_.selectNextWordToSpellcheck(value);
1479         if (word.word().empty())
1480                 bv->unlockInset(const_cast<InsetText *>(this));
1481         else
1482                 value = cy();
1483         return word;
1484 }
1485
1486
1487 void InsetText::selectSelectedWord(BufferView * bv)
1488 {
1489         if (the_locking_inset) {
1490                 the_locking_inset->selectSelectedWord(bv);
1491                 return;
1492         }
1493         getLyXText(bv)->selectSelectedWord();
1494         updateLocal(bv, false);
1495 }
1496
1497
1498 bool InsetText::nextChange(BufferView * bv, lyx::pos_type & length)
1499 {
1500         if (the_locking_inset) {
1501                 if (the_locking_inset->nextChange(bv, length))
1502                         return true;
1503                 text_.cursorRight(true);
1504         }
1505         lyx::find::SearchResult result =
1506                 lyx::find::findNextChange(bv, &text_, length);
1507
1508         if (result == lyx::find::SR_FOUND) {
1509                 LyXCursor cur = text_.cursor;
1510                 bv->unlockInset(bv->theLockingInset());
1511                 if (bv->lockInset(this))
1512                         locked = true;
1513                 text_.cursor = cur;
1514                 text_.setSelectionRange(length);
1515                 updateLocal(bv, false);
1516         }
1517         return result != lyx::find::SR_NOT_FOUND;
1518 }
1519
1520
1521 bool InsetText::searchForward(BufferView * bv, string const & str,
1522                               bool cs, bool mw)
1523 {
1524         if (the_locking_inset) {
1525                 if (the_locking_inset->searchForward(bv, str, cs, mw))
1526                         return true;
1527                 text_.cursorRight(true);
1528         }
1529         lyx::find::SearchResult result =
1530                 lyx::find::find(bv, &text_, str, true, cs, mw);
1531
1532         if (result == lyx::find::SR_FOUND) {
1533                 LyXCursor cur = text_.cursor;
1534                 bv->unlockInset(bv->theLockingInset());
1535                 if (bv->lockInset(this))
1536                         locked = true;
1537                 text_.cursor = cur;
1538                 text_.setSelectionRange(str.length());
1539                 updateLocal(bv, false);
1540         }
1541         return result != lyx::find::SR_NOT_FOUND;
1542 }
1543
1544
1545 bool InsetText::searchBackward(BufferView * bv, string const & str,
1546                                bool cs, bool mw)
1547 {
1548         if (the_locking_inset) {
1549                 if (the_locking_inset->searchBackward(bv, str, cs, mw))
1550                         return true;
1551         }
1552         if (!locked) {
1553                 text_.setCursor(paragraphs.size() - 1, paragraphs.back().size());
1554         }
1555         lyx::find::SearchResult result =
1556                 lyx::find::find(bv, &text_, str, false, cs, mw);
1557
1558         if (result == lyx::find::SR_FOUND) {
1559                 LyXCursor cur = text_.cursor;
1560                 bv->unlockInset(bv->theLockingInset());
1561                 if (bv->lockInset(this))
1562                         locked = true;
1563                 text_.cursor = cur;
1564                 text_.setSelectionRange(str.length());
1565                 updateLocal(bv, false);
1566         }
1567         return result != lyx::find::SR_NOT_FOUND;
1568 }
1569
1570
1571 bool InsetText::checkInsertChar(LyXFont & font)
1572 {
1573         return owner() ? owner()->checkInsertChar(font) : true;
1574 }
1575
1576
1577 void InsetText::collapseParagraphs(BufferView * bv)
1578 {
1579         while (paragraphs.size() > 1) {
1580                 ParagraphList::iterator const first = paragraphs.begin();
1581                 ParagraphList::iterator second = first;
1582                 advance(second, 1);
1583                 size_t const first_par_size = first->size();
1584
1585                 if (!first->empty() &&
1586                     !second->empty() &&
1587                     !first->isSeparator(first_par_size - 1)) {
1588                         first->insertChar(first_par_size, ' ');
1589                 }
1590
1591 #warning probably broken
1592                 if (text_.selection.set()) {
1593                         if (text_.selection.start.par() == 1) {
1594                                 text_.selection.start.par(1);
1595                                 text_.selection.start.pos(text_.selection.start.pos() + first_par_size);
1596                         }
1597                         if (text_.selection.end.par() == 2) {
1598                                 text_.selection.end.par(1);
1599                                 text_.selection.end.pos(text_.selection.end.pos() + first_par_size);
1600                         }
1601                 }
1602
1603                 mergeParagraph(bv->buffer()->params(), paragraphs, first);
1604         }
1605 }
1606
1607
1608 void InsetText::getDrawFont(LyXFont & font) const
1609 {
1610         if (!owner())
1611                 return;
1612         owner()->getDrawFont(font);
1613 }
1614
1615
1616 void InsetText::appendParagraphs(Buffer * buffer, ParagraphList & plist)
1617 {
1618 #warning FIXME Check if Changes stuff needs changing here. (Lgb)
1619 // And it probably does. You have to take a look at this John. (Lgb)
1620 #warning John, have a look here. (Lgb)
1621         ParagraphList::iterator pit = plist.begin();
1622         ParagraphList::iterator ins = paragraphs.insert(paragraphs.end(), *pit);
1623         ++pit;
1624         mergeParagraph(buffer->params(), paragraphs, boost::prior(ins));
1625
1626         ParagraphList::iterator pend = plist.end();
1627         for (; pit != pend; ++pit)
1628                 paragraphs.push_back(*pit);
1629 }
1630
1631
1632 void InsetText::addPreview(PreviewLoader & loader) const
1633 {
1634         ParagraphList::const_iterator pit = paragraphs.begin();
1635         ParagraphList::const_iterator pend = paragraphs.end();
1636
1637         for (; pit != pend; ++pit) {
1638                 InsetList::const_iterator it  = pit->insetlist.begin();
1639                 InsetList::const_iterator end = pit->insetlist.end();
1640                 for (; it != end; ++it)
1641                         it->inset->addPreview(loader);
1642         }
1643 }