]> git.lyx.org Git - lyx.git/blob - src/insets/insettext.C
Move depth_type to support/types.h.
[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 "BufferView.h"
18 #include "CutAndPaste.h"
19 #include "debug.h"
20 #include "funcrequest.h"
21 #include "gettext.h"
22 #include "intl.h"
23 #include "lyxfind.h"
24 #include "lyxlex.h"
25 #include "lyxrc.h"
26 #include "metricsinfo.h"
27 #include "paragraph.h"
28 #include "paragraph_funcs.h"
29 #include "ParagraphParameters.h"
30 #include "rowpainter.h"
31 #include "sgml.h"
32 #include "undo_funcs.h"
33 #include "WordLangTuple.h"
34
35 #include "frontends/Alert.h"
36 #include "frontends/font_metrics.h"
37 #include "frontends/LyXView.h"
38 #include "frontends/Painter.h"
39
40 #include "support/lyxalgo.h" // lyx::count
41
42 #include <boost/bind.hpp>
43
44 using std::endl;
45 using std::for_each;
46 using std::min;
47 using std::max;
48 using std::make_pair;
49 using std::auto_ptr;
50 using std::ostream;
51 using std::ifstream;
52 using std::pair;
53 using std::vector;
54
55 using namespace lyx::support;
56 using namespace lyx::graphics;
57 using namespace bv_funcs;
58
59 using lyx::pos_type;
60 using lyx::textclass_type;
61
62
63 InsetText::InsetText(BufferParams const & bp)
64         : UpdatableInset(), text_(0, this, true, paragraphs)
65 {
66         paragraphs.push_back(Paragraph());
67         paragraphs.begin()->layout(bp.getLyXTextClass().defaultLayout());
68         if (bp.tracking_changes)
69                 paragraphs.begin()->trackChanges();
70         init(0);
71 }
72
73
74 InsetText::InsetText(InsetText const & in)
75         : UpdatableInset(in), text_(0, this, true, paragraphs)
76 {
77         init(&in);
78 }
79
80
81 InsetText & InsetText::operator=(InsetText const & it)
82 {
83         init(&it);
84         return *this;
85 }
86
87
88 void InsetText::init(InsetText const * ins)
89 {
90         if (ins) {
91                 textwidth_ = ins->textwidth_;
92                 text_.bv_owner = ins->text_.bv_owner;
93
94                 paragraphs = ins->paragraphs;
95
96                 ParagraphList::iterator pit = paragraphs.begin();
97                 ParagraphList::iterator end = paragraphs.end();
98                 for (; pit != end; ++pit)
99                         pit->setInsetOwner(this);
100
101                 autoBreakRows = ins->autoBreakRows;
102                 drawFrame_ = ins->drawFrame_;
103                 frame_color = ins->frame_color;
104         } else {
105                 textwidth_ = 0; // broken
106                 drawFrame_ = NEVER;
107                 frame_color = LColor::insetframe;
108                 autoBreakRows = false;
109         }
110         the_locking_inset = 0;
111         for_each(paragraphs.begin(), paragraphs.end(),
112                  boost::bind(&Paragraph::setInsetOwner, _1, this));
113         top_y = 0;
114         no_selection = true;
115         drawTextXOffset = 0;
116         drawTextYOffset = 0;
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 + drawTextXOffset;
264                 inset_y = cy() + drawTextYOffset;
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 + drawTextXOffset;
369         inset_y = cy() + drawTextYOffset;
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                 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                                 if (it->inset->getInsetFromID(id)) {
401                                         lyxerr << "InsetText::lockInsetInInset: 2" << endl;
402                                         text_.setCursorIntern(pit, it->pos);
403                                         it->inset->localDispatch(FuncRequest(bv, LFUN_INSET_EDIT));
404                                         return the_locking_inset->lockInsetInInset(bv, inset);
405                                 }
406                         }
407                 }
408                 lyxerr << "InsetText::lockInsetInInset: 3" << endl;
409                 return false;
410         }
411         if (inset == cpar()->getInset(cpos())) {
412                 lyxerr[Debug::INSETS] << "OK" << endl;
413                 lockInset(bv, inset);
414                 return true;
415         }
416
417         if (the_locking_inset && the_locking_inset == inset) {
418                 if (cpar() == inset_par && cpos() == inset_pos) {
419                         lyxerr[Debug::INSETS] << "OK" << endl;
420                         inset_x = cx() - top_x + drawTextXOffset;
421                         inset_y = cy() + drawTextYOffset;
422                 } else {
423                         lyxerr[Debug::INSETS] << "cursor.pos != inset_pos" << endl;
424                 }
425         } else if (the_locking_inset) {
426                 lyxerr[Debug::INSETS] << "MAYBE" << endl;
427                 return the_locking_inset->lockInsetInInset(bv, inset);
428         }
429         lyxerr[Debug::INSETS] << "NOT OK" << endl;
430         return false;
431 }
432
433
434 bool InsetText::unlockInsetInInset(BufferView * bv, UpdatableInset * inset,
435                                    bool lr)
436 {
437         if (!the_locking_inset)
438                 return false;
439         if (the_locking_inset == inset) {
440                 the_locking_inset->insetUnlock(bv);
441                 the_locking_inset = 0;
442                 if (lr)
443                         moveRightIntern(bv, true, false);
444                 old_par = paragraphs.end(); // force layout setting
445                 if (scroll())
446                         scroll(bv, 0.0F);
447                 else
448                         updateLocal(bv, false);
449                 return true;
450         }
451         return the_locking_inset->unlockInsetInInset(bv, inset, lr);
452 }
453
454
455 void InsetText::lfunMousePress(FuncRequest const & cmd)
456 {
457         no_selection = true;
458
459         // use this to check mouse motion for selection!
460         mouse_x = cmd.x;
461         mouse_y = cmd.y;
462
463         BufferView * bv = cmd.view();
464         FuncRequest cmd1 = cmd;
465         cmd1.x -= inset_x;
466         cmd1.y -= inset_y;
467         if (!locked)
468                 lockInset(bv);
469
470         int tmp_x = cmd.x - drawTextXOffset;
471         int tmp_y = cmd.y + dim_.asc - bv->top_y();
472         InsetOld * inset = getLyXText(bv)->checkInsetHit(tmp_x, tmp_y);
473
474         if (the_locking_inset) {
475                 if (the_locking_inset == inset) {
476                         the_locking_inset->localDispatch(cmd1);
477                         return;
478                 }
479                 // otherwise only unlock the_locking_inset
480                 the_locking_inset->insetUnlock(bv);
481                 the_locking_inset = 0;
482         }
483         if (!inset)
484                 no_selection = false;
485
486         if (bv->theLockingInset()) {
487                 if (isHighlyEditableInset(inset)) {
488                         // We just have to lock the inset before calling a
489                         // PressEvent on it!
490                         UpdatableInset * uinset = static_cast<UpdatableInset*>(inset);
491                         if (!bv->lockInset(uinset)) {
492                                 lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
493                         }
494                         inset->localDispatch(cmd1);
495                         if (the_locking_inset)
496                                 updateLocal(bv, false);
497                         return;
498                 }
499         }
500         if (!inset) {
501                 bool paste_internally = false;
502                 if (cmd.button() == mouse_button::button2 && getLyXText(bv)->selection.set()) {
503                         localDispatch(FuncRequest(bv, LFUN_COPY));
504                         paste_internally = true;
505                 }
506                 int old_top_y = bv->top_y();
507
508                 text_.setCursorFromCoordinates(cmd.x - drawTextXOffset,
509                                              cmd.y + dim_.asc);
510                 // set the selection cursor!
511                 text_.selection.cursor = text_.cursor;
512                 text_.cursor.x_fix(text_.cursor.x());
513
514                 text_.clearSelection();
515                 updateLocal(bv, false);
516
517                 bv->owner()->setLayout(cpar()->layout()->name());
518
519                 // we moved the view we cannot do mouse selection in this case!
520                 if (bv->top_y() != old_top_y)
521                         no_selection = true;
522                 old_par = cpar();
523                 // Insert primary selection with middle mouse
524                 // if there is a local selection in the current buffer,
525                 // insert this
526                 if (cmd.button() == mouse_button::button2) {
527                         if (paste_internally)
528                                 localDispatch(FuncRequest(bv, LFUN_PASTE));
529                         else
530                                 localDispatch(FuncRequest(bv, LFUN_PASTESELECTION, "paragraph"));
531                 }
532         } else {
533                 getLyXText(bv)->clearSelection();
534         }
535 }
536
537
538 bool InsetText::lfunMouseRelease(FuncRequest const & cmd)
539 {
540         BufferView * bv = cmd.view();
541         FuncRequest cmd1 = cmd;
542         cmd1.x -= inset_x;
543         cmd1.y -= inset_y;
544
545         no_selection = true;
546         if (the_locking_inset)
547                 return the_locking_inset->localDispatch(cmd1);
548
549         int tmp_x = cmd.x - drawTextXOffset;
550         int tmp_y = cmd.y + dim_.asc - bv->top_y();
551         InsetOld * inset = getLyXText(bv)->checkInsetHit(tmp_x, tmp_y);
552         bool ret = false;
553         if (inset) {
554 // This code should probably be removed now. Simple insets
555 // (!highlyEditable) can actually take the localDispatch,
556 // and turn it into edit() if necessary. But we still
557 // need to deal properly with the whole relative vs.
558 // absolute mouse co-ords thing in a realiable, sensible way
559 #if 0
560                 if (isHighlyEditableInset(inset))
561                         ret = inset->localDispatch(cmd1);
562                 else {
563                         inset_x = cx(bv) - top_x + drawTextXOffset;
564                         inset_y = cy() + drawTextYOffset;
565                         cmd1.x = cmd.x - inset_x;
566                         cmd1.y = cmd.x - inset_y;
567                         inset->edit(bv, cmd1.x, cmd1.y, cmd.button());
568                         ret = true;
569                 }
570 #endif
571                 ret = inset->localDispatch(cmd1);
572                 updateLocal(bv, false);
573
574         }
575         return ret;
576 }
577
578
579 void InsetText::lfunMouseMotion(FuncRequest const & cmd)
580 {
581         FuncRequest cmd1 = cmd;
582         cmd1.x -= inset_x;
583         cmd1.y -= inset_y;
584
585         if (the_locking_inset) {
586                 the_locking_inset->localDispatch(cmd1);
587                 return;
588         }
589
590         if (no_selection || (mouse_x == cmd.x && mouse_y == cmd.y))
591                 return;
592
593         BufferView * bv = cmd.view();
594         LyXCursor cur = text_.cursor;
595         text_.setCursorFromCoordinates
596                 (cmd.x - drawTextXOffset, cmd.y + dim_.asc);
597         text_.cursor.x_fix(text_.cursor.x());
598         if (cur == text_.cursor)
599                 return;
600         text_.setSelection();
601         updateLocal(bv, false);
602 }
603
604
605 InsetOld::RESULT InsetText::localDispatch(FuncRequest const & cmd)
606 {
607         BufferView * bv = cmd.view();
608         setViewCache(bv);
609
610         switch (cmd.action) {
611         case LFUN_INSET_EDIT: {
612                 UpdatableInset::localDispatch(cmd);
613
614                 if (!bv->lockInset(this)) {
615                         lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
616                         return DISPATCHED;
617                 }
618
619                 locked = true;
620                 the_locking_inset = 0;
621                 inset_pos = 0;
622                 inset_x = 0;
623                 inset_y = 0;
624                 inset_boundary = false;
625                 inset_par = paragraphs.end();
626                 old_par = paragraphs.end();
627
628
629                 if (cmd.argument.size()) {
630                         if (cmd.argument == "left")
631                                 text_.setCursorIntern(paragraphs.begin(), 0);
632                         else {
633                                 ParagraphList::iterator it = boost::prior(paragraphs.end());
634                                 text_.setCursor(it, it->size());
635                         }
636                 } else {
637                         int tmp_y = (cmd.y < 0) ? 0 : cmd.y;
638                         // we put here -1 and not button as now the button in the
639                         // edit call should not be needed we will fix this in 1.3.x
640                         // cycle hopefully (Jug 20020509)
641                         // FIXME: GUII I've changed this to none: probably WRONG
642                         if (!checkAndActivateInset(bv, cmd.x, tmp_y, mouse_button::none)) {
643                                 text_.setCursorFromCoordinates(cmd.x - drawTextXOffset,
644                                                                         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         x -= drawTextXOffset;
1398         int dummyx = x;
1399         int dummyy = y + dim_.asc;
1400         InsetOld * inset = getLyXText(bv)->checkInsetHit(dummyx, dummyy);
1401         // we only do the edit() call if the inset was hit by the mouse
1402         // or if it is a highly editable inset. So we should call this
1403         // function from our own edit with button < 0.
1404         // FIXME: GUII jbl. I've changed this to ::none for now which is probably
1405         // WRONG
1406         if (button == mouse_button::none && !isHighlyEditableInset(inset))
1407                 return false;
1408
1409         if (!inset)
1410                 return false;
1411         if (x < 0)
1412                 x = dim_.wid;
1413         if (y < 0)
1414                 y = dim_.des;
1415         inset_x = cx() - top_x + drawTextXOffset;
1416         inset_y = cy() + drawTextYOffset;
1417         FuncRequest cmd(bv, LFUN_INSET_EDIT, x - inset_x, y - inset_y, button);
1418         inset->localDispatch(cmd);
1419         if (!the_locking_inset)
1420                 return false;
1421         updateLocal(bv, false);
1422         return true;
1423 }
1424
1425
1426 void InsetText::markNew(bool track_changes)
1427 {
1428         ParagraphList::iterator pit = paragraphs.begin();
1429         ParagraphList::iterator end = paragraphs.end();
1430         for (; pit != end; ++pit) {
1431                 if (track_changes) {
1432                         pit->trackChanges();
1433                 } else {
1434                         // no-op when not tracking
1435                         pit->cleanChanges();
1436                 }
1437         }
1438 }
1439
1440
1441 void InsetText::setText(string const & data, LyXFont const & font)
1442 {
1443         clear(false);
1444         for (unsigned int i = 0; i < data.length(); ++i)
1445                 paragraphs.begin()->insertChar(i, data[i], font);
1446 }
1447
1448
1449 void InsetText::setAutoBreakRows(bool flag)
1450 {
1451         if (flag != autoBreakRows) {
1452                 autoBreakRows = flag;
1453                 if (!flag)
1454                         removeNewlines();
1455         }
1456 }
1457
1458
1459 void InsetText::setDrawFrame(DrawFrame how)
1460 {
1461         drawFrame_ = how;
1462 }
1463
1464
1465 void InsetText::setFrameColor(LColor::color col)
1466 {
1467         frame_color = col;
1468 }
1469
1470
1471 int InsetText::cx() const
1472 {
1473         int x = text_.cursor.x() + top_x + TEXT_TO_INSET_OFFSET;
1474         if (the_locking_inset) {
1475                 LyXFont font = text_.getFont(text_.cursor.par(), text_.cursor.pos());
1476                 if (font.isVisibleRightToLeft())
1477                         x -= the_locking_inset->width();
1478         }
1479         return x;
1480 }
1481
1482
1483 int InsetText::cy() const
1484 {
1485         return text_.cursor.y() - dim_.asc + TEXT_TO_INSET_OFFSET;
1486 }
1487
1488
1489 pos_type InsetText::cpos() const
1490 {
1491         return text_.cursor.pos();
1492 }
1493
1494
1495 ParagraphList::iterator InsetText::cpar() const
1496 {
1497         return text_.cursor.par();
1498 }
1499
1500
1501 bool InsetText::cboundary() const
1502 {
1503         return text_.cursor.boundary();
1504 }
1505
1506
1507 RowList::iterator InsetText::crow() const
1508 {
1509         return text_.cursorRow();
1510 }
1511
1512
1513 LyXText * InsetText::getLyXText(BufferView const * bv,
1514                                 bool const recursive) const
1515 {
1516         setViewCache(bv);
1517         if (recursive && the_locking_inset)
1518                 return the_locking_inset->getLyXText(bv, true);
1519         return &text_;
1520 }
1521
1522
1523 void InsetText::setViewCache(BufferView const * bv) const
1524 {
1525         if (bv) {
1526                 if (bv != text_.bv_owner) {
1527                         //lyxerr << "setting view cache from "
1528                         //      << text_.bv_owner << " to " << bv << "\n";
1529                         text_.init(const_cast<BufferView *>(bv));
1530                 }
1531                 text_.bv_owner = const_cast<BufferView *>(bv);
1532         }
1533 }
1534
1535
1536 void InsetText::deleteLyXText(BufferView * bv, bool recursive) const
1537 {
1538         if (recursive) {
1539                 /// then remove all LyXText in text-insets
1540                 for_each(const_cast<ParagraphList&>(paragraphs).begin(),
1541                          const_cast<ParagraphList&>(paragraphs).end(),
1542                          boost::bind(&Paragraph::deleteInsetsLyXText, _1, bv));
1543         }
1544 }
1545
1546
1547 void InsetText::removeNewlines()
1548 {
1549         ParagraphList::iterator it = paragraphs.begin();
1550         ParagraphList::iterator end = paragraphs.end();
1551         for (; it != end; ++it)
1552                 for (int i = 0; i < it->size(); ++i)
1553                         if (it->isNewline(i))
1554                                 it->erase(i);
1555 }
1556
1557
1558 int InsetText::scroll(bool recursive) const
1559 {
1560         int sx = UpdatableInset::scroll(false);
1561
1562         if (recursive && the_locking_inset)
1563                 sx += the_locking_inset->scroll(recursive);
1564
1565         return sx;
1566 }
1567
1568
1569 void InsetText::clearSelection(BufferView *)
1570 {
1571         text_.clearSelection();
1572 }
1573
1574
1575 void InsetText::clearInset(BufferView * bv, int start_x, int baseline) const
1576 {
1577         Painter & pain = bv->painter();
1578         int w = dim_.wid;
1579         int h = dim_.asc + dim_.des;
1580         int ty = baseline - dim_.asc;
1581
1582         if (ty < 0) {
1583                 h += ty;
1584                 ty = 0;
1585         }
1586         if (ty + h > pain.paperHeight())
1587                 h = pain.paperHeight();
1588         if (top_x + drawTextXOffset + w > pain.paperWidth())
1589                 w = pain.paperWidth();
1590         pain.fillRectangle(start_x + 1, ty + 1, w - 3, h - 1, backgroundColor());
1591 }
1592
1593
1594 ParagraphList * InsetText::getParagraphs(int i) const
1595 {
1596         return (i == 0) ? const_cast<ParagraphList*>(&paragraphs) : 0;
1597 }
1598
1599
1600 LyXCursor const & InsetText::cursor(BufferView * bv) const
1601 {
1602         if (the_locking_inset)
1603                 return the_locking_inset->cursor(bv);
1604         return getLyXText(bv)->cursor;
1605 }
1606
1607
1608 InsetOld * InsetText::getInsetFromID(int id_arg) const
1609 {
1610         if (id_arg == id())
1611                 return const_cast<InsetText *>(this);
1612
1613         ParagraphList::const_iterator pit = paragraphs.begin();
1614         ParagraphList::const_iterator pend = paragraphs.end();
1615         for (; pit != pend; ++pit) {
1616                 InsetList::const_iterator it = pit->insetlist.begin();
1617                 InsetList::const_iterator end = pit->insetlist.end();
1618                 for (; it != end; ++it) {
1619                         if (it->inset->id() == id_arg)
1620                                 return it->inset;
1621                         InsetOld * in = it->inset->getInsetFromID(id_arg);
1622                         if (in)
1623                                 return in;
1624                 }
1625         }
1626         return 0;
1627 }
1628
1629
1630 WordLangTuple const
1631 InsetText::selectNextWordToSpellcheck(BufferView * bv, float & value) const
1632 {
1633         WordLangTuple word;
1634         if (the_locking_inset) {
1635                 word = the_locking_inset->selectNextWordToSpellcheck(bv, value);
1636                 if (!word.word().empty()) {
1637                         value += cy();
1638                         return word;
1639                 }
1640                 // we have to go on checking so move cursor to the next char
1641                 text_.cursor.pos(text_.cursor.pos() + 1);
1642         }
1643         word = text_.selectNextWordToSpellcheck(value);
1644         if (word.word().empty())
1645                 bv->unlockInset(const_cast<InsetText *>(this));
1646         else
1647                 value = cy();
1648         return word;
1649 }
1650
1651
1652 void InsetText::selectSelectedWord(BufferView * bv)
1653 {
1654         if (the_locking_inset) {
1655                 the_locking_inset->selectSelectedWord(bv);
1656                 return;
1657         }
1658         getLyXText(bv)->selectSelectedWord();
1659         updateLocal(bv, false);
1660 }
1661
1662
1663 bool InsetText::nextChange(BufferView * bv, lyx::pos_type & length)
1664 {
1665         if (the_locking_inset) {
1666                 if (the_locking_inset->nextChange(bv, length))
1667                         return true;
1668                 text_.cursorRight(true);
1669         }
1670         lyx::find::SearchResult result =
1671                 lyx::find::findNextChange(bv, &text_, length);
1672
1673         if (result == lyx::find::SR_FOUND) {
1674                 LyXCursor cur = text_.cursor;
1675                 bv->unlockInset(bv->theLockingInset());
1676                 if (bv->lockInset(this))
1677                         locked = true;
1678                 text_.cursor = cur;
1679                 text_.setSelectionRange(length);
1680                 updateLocal(bv, false);
1681         }
1682         return result != lyx::find::SR_NOT_FOUND;
1683 }
1684
1685
1686 bool InsetText::searchForward(BufferView * bv, string const & str,
1687                               bool cs, bool mw)
1688 {
1689         if (the_locking_inset) {
1690                 if (the_locking_inset->searchForward(bv, str, cs, mw))
1691                         return true;
1692                 text_.cursorRight(true);
1693         }
1694         lyx::find::SearchResult result =
1695                 lyx::find::find(bv, &text_, str, true, cs, mw);
1696
1697         if (result == lyx::find::SR_FOUND) {
1698                 LyXCursor cur = text_.cursor;
1699                 bv->unlockInset(bv->theLockingInset());
1700                 if (bv->lockInset(this))
1701                         locked = true;
1702                 text_.cursor = cur;
1703                 text_.setSelectionRange(str.length());
1704                 updateLocal(bv, false);
1705         }
1706         return result != lyx::find::SR_NOT_FOUND;
1707 }
1708
1709
1710 bool InsetText::searchBackward(BufferView * bv, string const & str,
1711                                bool cs, bool mw)
1712 {
1713         if (the_locking_inset) {
1714                 if (the_locking_inset->searchBackward(bv, str, cs, mw))
1715                         return true;
1716         }
1717         if (!locked) {
1718                 ParagraphList::iterator pit = boost::prior(paragraphs.end());
1719                 text_.setCursor(pit, pit->size());
1720         }
1721         lyx::find::SearchResult result =
1722                 lyx::find::find(bv, &text_, str, false, cs, mw);
1723
1724         if (result == lyx::find::SR_FOUND) {
1725                 LyXCursor cur = text_.cursor;
1726                 bv->unlockInset(bv->theLockingInset());
1727                 if (bv->lockInset(this))
1728                         locked = true;
1729                 text_.cursor = cur;
1730                 text_.setSelectionRange(str.length());
1731                 updateLocal(bv, false);
1732         }
1733         return result != lyx::find::SR_NOT_FOUND;
1734 }
1735
1736
1737 bool InsetText::checkInsertChar(LyXFont & font)
1738 {
1739         return owner() ? owner()->checkInsertChar(font) : true;
1740 }
1741
1742
1743 void InsetText::collapseParagraphs(BufferView * bv)
1744 {
1745         while (paragraphs.size() > 1) {
1746                 ParagraphList::iterator first_par = paragraphs.begin();
1747                 ParagraphList::iterator next_par = boost::next(first_par);
1748                 size_t const first_par_size = first_par->size();
1749
1750                 if (!first_par->empty() &&
1751                     !next_par->empty() &&
1752                     !first_par->isSeparator(first_par_size - 1)) {
1753                         first_par->insertChar(first_par_size, ' ');
1754                 }
1755
1756                 if (text_.selection.set()) {
1757                         if (text_.selection.start.par() == next_par) {
1758                                 text_.selection.start.par(first_par);
1759                                 text_.selection.start.pos(
1760                                         text_.selection.start.pos() + first_par_size);
1761                         }
1762                         if (text_.selection.end.par() == next_par) {
1763                                 text_.selection.end.par(first_par);
1764                                 text_.selection.end.pos(
1765                                         text_.selection.end.pos() + first_par_size);
1766                         }
1767                 }
1768
1769                 mergeParagraph(bv->buffer()->params, paragraphs, first_par);
1770         }
1771 }
1772
1773
1774 void InsetText::getDrawFont(LyXFont & font) const
1775 {
1776         if (!owner())
1777                 return;
1778         owner()->getDrawFont(font);
1779 }
1780
1781
1782 void InsetText::appendParagraphs(Buffer * buffer, ParagraphList & plist)
1783 {
1784 #warning FIXME Check if Changes stuff needs changing here. (Lgb)
1785 // And it probably does. You have to take a look at this John. (Lgb)
1786 #warning John, have a look here. (Lgb)
1787         ParagraphList::iterator pit = plist.begin();
1788         ParagraphList::iterator ins = paragraphs.insert(paragraphs.end(), *pit);
1789         ++pit;
1790         mergeParagraph(buffer->params, paragraphs, boost::prior(ins));
1791
1792         ParagraphList::iterator pend = plist.end();
1793         for (; pit != pend; ++pit)
1794                 paragraphs.push_back(*pit);
1795 }
1796
1797
1798 void InsetText::addPreview(PreviewLoader & loader) const
1799 {
1800         ParagraphList::const_iterator pit = paragraphs.begin();
1801         ParagraphList::const_iterator pend = paragraphs.end();
1802
1803         for (; pit != pend; ++pit) {
1804                 InsetList::const_iterator it  = pit->insetlist.begin();
1805                 InsetList::const_iterator end = pit->insetlist.end();
1806                 for (; it != end; ++it)
1807                         it->inset->addPreview(loader);
1808         }
1809 }