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