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