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