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