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