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