]> git.lyx.org Git - lyx.git/blob - src/insets/insettext.C
the spellcheck cleanup
[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         top_y = 0;
112
113         locked = false;
114         inset_par = -1;
115         inset_pos = 0;
116         inset_x = 0;
117         inset_y = 0;
118         no_selection = true;
119         the_locking_inset = 0;
120         old_par = -1;
121         in_insetAllowed = false;
122         mouse_x = 0;
123         mouse_y = 0;
124 }
125
126
127 void InsetText::clear(bool just_mark_erased)
128 {
129         if (just_mark_erased) {
130                 ParagraphList::iterator it = paragraphs.begin();
131                 ParagraphList::iterator end = paragraphs.end();
132                 for (; it != end; ++it)
133                         it->markErased();
134                 return;
135         }
136
137         // This is a gross hack...
138         LyXLayout_ptr old_layout = paragraphs.begin()->layout();
139
140         paragraphs.clear();
141         paragraphs.push_back(Paragraph());
142         paragraphs.begin()->setInsetOwner(this);
143         paragraphs.begin()->layout(old_layout);
144 }
145
146
147 auto_ptr<InsetBase> InsetText::clone() const
148 {
149         return auto_ptr<InsetBase>(new InsetText(*this));
150 }
151
152
153 void InsetText::write(Buffer const & buf, ostream & os) const
154 {
155         os << "Text\n";
156         writeParagraphData(buf, os);
157 }
158
159
160 void InsetText::writeParagraphData(Buffer const & buf, ostream & os) const
161 {
162         ParagraphList::const_iterator it = paragraphs.begin();
163         ParagraphList::const_iterator end = paragraphs.end();
164         Paragraph::depth_type dth = 0;
165         for (; it != end; ++it) {
166                 it->write(buf, os, buf.params(), dth);
167         }
168 }
169
170
171 void InsetText::read(Buffer const & buf, LyXLex & lex)
172 {
173         string token;
174         Paragraph::depth_type depth = 0;
175
176         clear(false);
177
178 #warning John, look here. Doesnt make much sense.
179         if (buf.params().tracking_changes)
180                 paragraphs.begin()->trackChanges();
181
182         // delete the initial paragraph
183         Paragraph oldpar = *paragraphs.begin();
184         paragraphs.clear();
185         ParagraphList::iterator pit = paragraphs.begin();
186
187         while (lex.isOK()) {
188                 lex.nextToken();
189                 token = lex.getString();
190                 if (token.empty())
191                         continue;
192                 if (token == "\\end_inset") {
193                         break;
194                 }
195
196                 if (token == "\\end_document") {
197                         lex.printError("\\end_document read in inset! Error in document!");
198                         return;
199                 }
200
201                 // FIXME: ugly.
202                 const_cast<Buffer&>(buf).readParagraph(lex, token, paragraphs, pit, depth);
203         }
204
205         pit = paragraphs.begin();
206         ParagraphList::iterator const end = paragraphs.end();
207         for (; pit != end; ++pit)
208                 pit->setInsetOwner(this);
209
210         if (token != "\\end_inset") {
211                 lex.printError("Missing \\end_inset at this point. "
212                                            "Read: `$$Token'");
213         }
214
215         // sanity check
216         // ensure we have at least one par.
217         if (paragraphs.empty())
218                 paragraphs.push_back(oldpar);
219 }
220
221
222 void InsetText::metrics(MetricsInfo & mi, Dimension & dim) const
223 {
224         //lyxerr << "InsetText::metrics: width: " << mi.base.textwidth << endl;
225         textwidth_ = mi.base.textwidth - 30;
226         BufferView * bv = mi.base.bv;
227         setViewCache(bv);
228         text_.metrics(mi, dim);
229         dim.asc += TEXT_TO_INSET_OFFSET;
230         dim.des += TEXT_TO_INSET_OFFSET;
231         dim.wid += 2 * TEXT_TO_INSET_OFFSET;
232         dim.wid = max(dim.wid, 10);
233         dim_ = dim;
234 }
235
236
237 int InsetText::textWidth() const
238 {
239         return textwidth_;
240 }
241
242
243 void InsetText::draw(PainterInfo & pi, int x, int y) const
244 {
245         // update our idea of where we are. Clearly, we should
246         // not have to know this information.
247         top_x = x;
248
249         int const start_x = x;
250
251         BufferView * bv = pi.base.bv;
252         Painter & pain = pi.pain;
253
254         // repaint the background if needed
255         if (backgroundColor() != LColor::background)
256                 clearInset(bv, start_x + TEXT_TO_INSET_OFFSET, y);
257
258         // no draw is necessary !!!
259         if (drawFrame_ == LOCKED && !locked && paragraphs.begin()->empty()) {
260                 top_baseline = y;
261                 return;
262         }
263
264         bv->hideCursor();
265
266         if (!owner())
267                 x += scroll();
268
269         top_baseline = y;
270         top_y = y - dim_.asc;
271
272         if (the_locking_inset
273                   && text_.cursor.par() == inset_par && cpos() == inset_pos) {
274                 inset_x = cx() - x;
275                 inset_y = cy();
276         }
277
278         x += TEXT_TO_INSET_OFFSET;
279
280         paintTextInset(*bv, text_, x, y);
281
282         if (drawFrame_ == ALWAYS || (drawFrame_ == LOCKED && locked))
283                 drawFrame(pain, start_x);
284 }
285
286
287 void InsetText::drawFrame(Painter & pain, int x) const
288 {
289         int const ttoD2 = TEXT_TO_INSET_OFFSET / 2;
290         int const frame_x = x + ttoD2;
291         int const frame_y = top_baseline - dim_.asc + ttoD2;
292         int const frame_w = dim_.wid - TEXT_TO_INSET_OFFSET;
293         int const frame_h = dim_.asc + dim_.des - TEXT_TO_INSET_OFFSET;
294         pain.rectangle(frame_x, frame_y, frame_w, frame_h, frameColor());
295 }
296
297
298 void InsetText::updateLocal(BufferView * bv, bool /*mark_dirty*/)
299 {
300         if (!bv)
301                 return;
302
303         if (!autoBreakRows_ && paragraphs.size() > 1)
304                 collapseParagraphs(bv);
305
306         if (!text_.selection.set())
307                 text_.selection.cursor = text_.cursor;
308
309         bv->fitCursor();
310         bv->updateInset(this);
311         bv->owner()->view_state_changed();
312         bv->owner()->updateMenubar();
313         bv->owner()->updateToolbar();
314         if (old_par != text_.cursor.par()) {
315                 bv->owner()->setLayout(cpar()->layout()->name());
316                 old_par = text_.cursor.par();
317         }
318 }
319
320
321 string const InsetText::editMessage() const
322 {
323         return _("Opened Text Inset");
324 }
325
326
327 void InsetText::insetUnlock(BufferView * bv)
328 {
329         if (the_locking_inset) {
330                 the_locking_inset->insetUnlock(bv);
331                 the_locking_inset = 0;
332                 updateLocal(bv, false);
333         }
334         no_selection = true;
335         locked = false;
336
337         if (text_.selection.set())
338                 text_.clearSelection();
339         else if (owner())
340                 bv->owner()->setLayout(owner()->getLyXText(bv)
341                                        ->cursorPar()->layout()->name());
342         else
343                 bv->owner()->setLayout(bv->text->cursorPar()->layout()->name());
344         // hack for deleteEmptyParMech
345         if (!paragraphs.begin()->empty())
346                 text_.setCursor(0, 0);
347         else if (paragraphs.size() > 1)
348                 text_.setCursor(1, 0);
349 }
350
351
352 void InsetText::lockInset(BufferView * bv)
353 {
354         locked = true;
355         the_locking_inset = 0;
356         inset_pos = inset_x = inset_y = 0;
357         inset_boundary = false;
358         inset_par =  -1;
359         old_par = -1;
360         text_.setCursorIntern(0, 0);
361         text_.clearSelection();
362         finishUndo();
363         // If the inset is empty set the language of the current font to the
364         // language to the surronding text (if different).
365         if (paragraphs.begin()->empty() && paragraphs.size() == 1 &&
366                 bv->getParentLanguage(this) != text_.current_font.language()) {
367                 LyXFont font(LyXFont::ALL_IGNORE);
368                 font.setLanguage(bv->getParentLanguage(this));
369                 setFont(bv, font, false);
370         }
371 }
372
373
374 void InsetText::lockInset(BufferView * /*bv*/, UpdatableInset * inset)
375 {
376         the_locking_inset = inset;
377         inset_x = cx() - top_x;
378         inset_y = cy();
379         inset_pos = cpos();
380         inset_par = text_.cursor.par();
381         inset_boundary = cboundary();
382 }
383
384
385 bool InsetText::lockInsetInInset(BufferView * bv, UpdatableInset * inset)
386 {
387         lyxerr[Debug::INSETS] << "InsetText::LockInsetInInset("
388                               << inset << "): " << endl;
389         if (!inset)
390                 return false;
391         if (!the_locking_inset) {
392                 ParagraphList::iterator pit = paragraphs.begin();
393                 ParagraphList::iterator pend = paragraphs.end();
394
395                 for (; pit != pend; ++pit) {
396                         InsetList::iterator it = pit->insetlist.begin();
397                         InsetList::iterator const end = pit->insetlist.end();
398                         for (; it != end; ++it) {
399                                 if (it->inset == inset) {
400                                         lyxerr << "InsetText::lockInsetInInset: 1 a" << endl;
401                                         text_.setCursorIntern(
402                                                 std::distance(paragraphs.begin(), pit), it->pos);
403                                         lyxerr << "InsetText::lockInsetInInset: 1 b" << endl;
404                                         lyxerr << "bv: " << bv << " inset: " << inset << endl;
405                                         lockInset(bv, inset);
406                                         lyxerr << "InsetText::lockInsetInInset: 1 c" << endl;
407                                         return true;
408                                 }
409                         }
410                 }
411                 lyxerr << "InsetText::lockInsetInInset: 3" << endl;
412                 return false;
413         }
414         if (inset == cpar()->getInset(cpos())) {
415                 lyxerr[Debug::INSETS] << "OK" << endl;
416                 lockInset(bv, inset);
417                 return true;
418         }
419
420         if (the_locking_inset && the_locking_inset == inset) {
421                 if (text_.cursor.par() == inset_par && cpos() == inset_pos) {
422                         lyxerr[Debug::INSETS] << "OK" << endl;
423                         inset_x = cx() - top_x;
424                         inset_y = cy();
425                 } else {
426                         lyxerr[Debug::INSETS] << "cursor.pos != inset_pos" << endl;
427                 }
428         } else if (the_locking_inset) {
429                 lyxerr[Debug::INSETS] << "MAYBE" << endl;
430                 return the_locking_inset->lockInsetInInset(bv, inset);
431         }
432         lyxerr[Debug::INSETS] << "NOT OK" << endl;
433         return false;
434 }
435
436
437 bool InsetText::unlockInsetInInset(BufferView * bv, UpdatableInset * inset,
438                                    bool lr)
439 {
440         if (!the_locking_inset)
441                 return false;
442         if (the_locking_inset == inset) {
443                 the_locking_inset->insetUnlock(bv);
444                 the_locking_inset = 0;
445                 if (lr)
446                         moveRightIntern(bv, true, false);
447                 old_par = -1; // force layout setting
448                 if (scroll())
449                         scroll(bv, 0.0F);
450                 else
451                         updateLocal(bv, false);
452                 return true;
453         }
454         return the_locking_inset->unlockInsetInInset(bv, inset, lr);
455 }
456
457
458 void InsetText::lfunMousePress(FuncRequest const & cmd)
459 {
460         no_selection = true;
461
462         // use this to check mouse motion for selection!
463         mouse_x = cmd.x;
464         mouse_y = cmd.y;
465
466         BufferView * bv = cmd.view();
467         FuncRequest cmd1 = cmd;
468         cmd1.x -= inset_x;
469         cmd1.y -= inset_y;
470         if (!locked)
471                 lockInset(bv);
472
473         int tmp_x = cmd.x;
474         int tmp_y = cmd.y + dim_.asc - bv->top_y();
475         InsetOld * inset = getLyXText(bv)->checkInsetHit(tmp_x, tmp_y);
476
477         if (the_locking_inset) {
478                 if (the_locking_inset == inset) {
479                         the_locking_inset->dispatch(cmd1);
480                         return;
481                 }
482                 // otherwise only unlock the_locking_inset
483                 the_locking_inset->insetUnlock(bv);
484                 the_locking_inset = 0;
485         }
486         if (!inset)
487                 no_selection = false;
488
489         if (bv->theLockingInset()) {
490                 if (isHighlyEditableInset(inset)) {
491                         // We just have to lock the inset before calling a
492                         // PressEvent on it!
493                         UpdatableInset * uinset = static_cast<UpdatableInset*>(inset);
494                         if (!bv->lockInset(uinset)) {
495                                 lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
496                         }
497                         inset->dispatch(cmd1);
498                         if (the_locking_inset)
499                                 updateLocal(bv, false);
500                         return;
501                 }
502         }
503         if (!inset) {
504                 bool paste_internally = false;
505                 if (cmd.button() == mouse_button::button2 && getLyXText(bv)->selection.set()) {
506                         dispatch(FuncRequest(bv, LFUN_COPY));
507                         paste_internally = true;
508                 }
509                 int old_top_y = bv->top_y();
510
511                 text_.setCursorFromCoordinates(cmd.x, cmd.y + dim_.asc);
512                 // set the selection cursor!
513                 text_.selection.cursor = text_.cursor;
514                 bv->x_target(text_.cursor.x());
515
516                 text_.clearSelection();
517                 updateLocal(bv, false);
518
519                 bv->owner()->setLayout(cpar()->layout()->name());
520
521                 // we moved the view we cannot do mouse selection in this case!
522                 if (bv->top_y() != old_top_y)
523                         no_selection = true;
524                 old_par = text_.cursor.par();
525                 // Insert primary selection with middle mouse
526                 // if there is a local selection in the current buffer,
527                 // insert this
528                 if (cmd.button() == mouse_button::button2) {
529                         if (paste_internally)
530                                 dispatch(FuncRequest(bv, LFUN_PASTE));
531                         else
532                                 dispatch(FuncRequest(bv, LFUN_PASTESELECTION, "paragraph"));
533                 }
534         } else {
535                 getLyXText(bv)->clearSelection();
536         }
537 }
538
539
540 bool InsetText::lfunMouseRelease(FuncRequest const & cmd)
541 {
542         BufferView * bv = cmd.view();
543         FuncRequest cmd1 = cmd;
544         cmd1.x -= inset_x;
545         cmd1.y -= inset_y;
546
547         no_selection = true;
548         if (the_locking_inset) {
549                 DispatchResult const res = the_locking_inset->dispatch(cmd1);
550
551                 return res.dispatched();
552         }
553
554         int tmp_x = cmd.x;
555         int tmp_y = cmd.y + dim_.asc - bv->top_y();
556         InsetOld * inset = getLyXText(bv)->checkInsetHit(tmp_x, tmp_y);
557         if (!inset)
558                 return false;
559
560         // We still need to deal properly with the whole relative vs.
561         // absolute mouse co-ords thing in a realiable, sensible way
562         DispatchResult const res = inset->dispatch(cmd1);
563         bool const ret = res.dispatched();
564         updateLocal(bv, false);
565         return ret;
566 }
567
568
569 void InsetText::lfunMouseMotion(FuncRequest const & cmd)
570 {
571         FuncRequest cmd1 = cmd;
572         cmd1.x -= inset_x;
573         cmd1.y -= inset_y;
574
575         if (the_locking_inset) {
576                 the_locking_inset->dispatch(cmd1);
577                 return;
578         }
579
580         if (no_selection || (mouse_x == cmd.x && mouse_y == cmd.y))
581                 return;
582
583         BufferView * bv = cmd.view();
584         LyXCursor cur = text_.cursor;
585         text_.setCursorFromCoordinates (cmd.x, cmd.y + dim_.asc);
586         bv->x_target(text_.cursor.x());
587         if (cur == text_.cursor)
588                 return;
589         text_.setSelection();
590         updateLocal(bv, false);
591 }
592
593
594 DispatchResult
595 InsetText::priv_dispatch(FuncRequest const & cmd,
596                          idx_type & idx, pos_type & pos)
597 {
598         BufferView * bv = cmd.view();
599         setViewCache(bv);
600
601         switch (cmd.action) {
602         case LFUN_INSET_EDIT: {
603                 UpdatableInset::priv_dispatch(cmd, idx, pos);
604
605                 if (!bv->lockInset(this)) {
606                         lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
607                         return DispatchResult(true, true);
608                 }
609
610                 locked = true;
611                 the_locking_inset = 0;
612                 inset_pos = 0;
613                 inset_x = 0;
614                 inset_y = 0;
615                 inset_boundary = false;
616                 inset_par = -1;
617                 old_par = -1;
618
619
620                 if (cmd.argument.size()) {
621                         if (cmd.argument == "left")
622                                 text_.setCursorIntern(0, 0);
623                         else
624                                 text_.setCursor(paragraphs.size() - 1, paragraphs.back().size());
625                 } else {
626                         int tmp_y = (cmd.y < 0) ? 0 : cmd.y;
627                         // we put here -1 and not button as now the button in the
628                         // edit call should not be needed we will fix this in 1.3.x
629                         // cycle hopefully (Jug 20020509)
630                         // FIXME: GUII I've changed this to none: probably WRONG
631                         if (!checkAndActivateInset(bv, cmd.x, tmp_y, mouse_button::none)) {
632                                 text_.setCursorFromCoordinates(cmd.x, cmd.y + dim_.asc);
633                                 text_.cursor.x(text_.cursor.x());
634                                 bv->x_target(text_.cursor.x());
635                         }
636                 }
637
638                 text_.clearSelection();
639                 finishUndo();
640
641                 // If the inset is empty set the language of the current font to the
642                 // language to the surronding text (if different).
643                 if (paragraphs.begin()->empty() &&
644                     paragraphs.size() == 1 &&
645                     bv->getParentLanguage(this) != text_.current_font.language())
646                 {
647                         LyXFont font(LyXFont::ALL_IGNORE);
648                         font.setLanguage(bv->getParentLanguage(this));
649                         setFont(bv, font, false);
650                 }
651
652                 updateLocal(bv, false);
653                 // Tell the paragraph dialog that we've entered an insettext.
654                 bv->dispatch(FuncRequest(LFUN_PARAGRAPH_UPDATE));
655                 return DispatchResult(true, true);
656         }
657
658         case LFUN_MOUSE_PRESS:
659                 lfunMousePress(cmd);
660                 return DispatchResult(true, true);
661
662         case LFUN_MOUSE_MOTION:
663                 lfunMouseMotion(cmd);
664                 return DispatchResult(true, true);
665
666         case LFUN_MOUSE_RELEASE:
667                 return DispatchResult(lfunMouseRelease(cmd));
668
669         default:
670                 break;
671         }
672
673         bool was_empty = paragraphs.begin()->empty() && paragraphs.size() == 1;
674         no_selection = false;
675
676         DispatchResult result = UpdatableInset::priv_dispatch(cmd, idx, pos);
677         if (result.dispatched())
678                 return result;
679
680 #if 0
681         // This looks utterly strange. (Lgb)
682         if (cmd.action == LFUN_UNKNOWN_ACTION && cmd.argument.empty())
683                 return DispatchResult(false, FINISHED);
684 #endif
685
686         if (the_locking_inset) {
687                 DispatchResult result = the_locking_inset->dispatch(cmd);
688
689                 if (result.dispatched()) {
690                         if (result.update()) {
691                                 result.update(false);
692                                 updateLocal(bv, false);
693                         }
694                         return result;
695                 }
696
697                 switch (result.val()) {
698                 case FINISHED_RIGHT:
699                         moveRightIntern(bv, false, false);
700                         result.dispatched(true);
701                         result.update(true);
702                         break;
703                 case FINISHED_UP:
704                         result = moveUp(bv);
705                         if (result.val() >= FINISHED) {
706                                 updateLocal(bv, false);
707                                 bv->unlockInset(this);
708                         }
709                         break;
710                 case FINISHED_DOWN:
711                         result = moveDown(bv);
712                         if (result.val() >= FINISHED) {
713                                 updateLocal(bv, false);
714                                 bv->unlockInset(this);
715                         }
716                         break;
717                 default:
718                         result.dispatched(true);
719                         result.update(true);
720                         break;
721                 }
722                 the_locking_inset = 0;
723                 updateLocal(bv, false);
724                 // make sure status gets reset immediately
725                 bv->owner()->clearMessage();
726                 return result;
727         }
728
729         switch (cmd.action) {
730         // Normal chars
731         case LFUN_SELFINSERT:
732                 if (bv->buffer()->isReadonly()) {
733 //          setErrorMessage(N_("Document is read only"));
734                         break;
735                 }
736                 if (!cmd.argument.empty()) {
737                         /* Automatically delete the currently selected
738                          * text and replace it with what is being
739                          * typed in now. Depends on lyxrc settings
740                          * "auto_region_delete", which defaults to
741                          * true (on). */
742 #if 0
743                         // This should not be needed here and is also WRONG!
744                         recordUndo(bv, Undo::INSERT, text_.cursorPar());
745 #endif
746                         bv->switchKeyMap();
747
748                         if (lyxrc.auto_region_delete && text_.selection.set())
749                                 text_.cutSelection(false, false);
750                         text_.clearSelection();
751
752                         for (string::size_type i = 0; i < cmd.argument.length(); ++i)
753                                 bv->owner()->getIntl().getTransManager().
754                                         TranslateAndInsert(cmd.argument[i], &text_);
755                 }
756                 text_.selection.cursor = text_.cursor;
757                 result.dispatched(true);
758                 result.update(true);
759                 break;
760
761         // cursor movements that need special handling
762
763         case LFUN_RIGHT:
764                 result = moveRight(bv);
765                 finishUndo();
766                 break;
767         case LFUN_LEFT:
768                 finishUndo();
769                 result = moveLeft(bv);
770                 break;
771         case LFUN_DOWN:
772                 finishUndo();
773                 result = moveDown(bv);
774                 break;
775         case LFUN_UP:
776                 finishUndo();
777                 result = moveUp(bv);
778                 break;
779
780         case LFUN_PRIOR:
781                 if (crow() == text_.firstRow())
782                         result.val(FINISHED_UP);
783                 else {
784                         text_.cursorPrevious();
785                         text_.clearSelection();
786                         result.dispatched(true);
787                 }
788                 break;
789
790         case LFUN_NEXT:
791                 if (crow() == text_.lastRow())
792                         result.val(FINISHED_DOWN);
793                 else {
794                         text_.cursorNext();
795                         text_.clearSelection();
796                         result.dispatched(true);
797                 }
798                 break;
799
800         case LFUN_BACKSPACE:
801                 if (text_.selection.set())
802                         text_.cutSelection(true, false);
803                 else
804                         text_.backspace();
805 #warning should be also set dispatched here?
806                 result.update(true);
807                 break;
808
809         case LFUN_DELETE:
810                 if (text_.selection.set())
811                         text_.cutSelection(true, false);
812                 else
813                         text_.Delete();
814 #warning should be also set dispatched here?
815                 result.update(true);
816                 break;
817
818         case LFUN_PASTE:
819                 if (!autoBreakRows_) {
820                         if (CutAndPaste::nrOfParagraphs() > 1) {
821 #ifdef WITH_WARNINGS
822 #warning FIXME horrendously bad UI
823 #endif
824                                 Alert::error(_("Paste failed"), _("Cannot include more than one paragraph."));
825                         }
826                 } else {
827                         replaceSelection(bv->getLyXText());
828                         size_t sel_index = 0;
829                         string const & arg = cmd.argument;
830                         if (isStrUnsignedInt(arg)) {
831 #warning FIXME Check if the arg is in the domain of available selections.
832                                 sel_index = strToUnsignedInt(arg);
833                         }
834                         text_.pasteSelection(sel_index);
835                         // bug 393
836                         text_.clearSelection();
837 #warning should be also set dispatched here?
838                         result.update(true);
839                 }
840                 break;
841
842         case LFUN_BREAKPARAGRAPH:
843                 if (!autoBreakRows_) {
844                         result.dispatched(true);
845                         result.update(true);
846                 } else {
847                         replaceSelection(bv->getLyXText());
848                         text_.breakParagraph(paragraphs, 0);
849 #warning should be also set dispatched here?
850                         result.update(true);
851                 }
852                 break;
853
854         case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
855                 if (!autoBreakRows_) {
856                         result.dispatched(true);
857                         result.update(true);
858                 } else {
859                         replaceSelection(bv->getLyXText());
860                         text_.breakParagraph(paragraphs, 1);
861 #warning should be also set dispatched here?
862                         result.update(true);
863                 }
864                 break;
865
866         case LFUN_BREAKLINE: {
867                 if (!autoBreakRows_) {
868                         result.dispatched(true);
869                         result.update(true);
870                 } else {
871                         replaceSelection(bv->getLyXText());
872                         auto_ptr<InsetNewline> ins(new InsetNewline);
873                         text_.insertInset(ins.release());
874 #warning should be also set dispatched here?
875                         result.update(true);
876                 }
877                 break;
878         }
879
880         case LFUN_LAYOUT:
881                 // do not set layouts on non breakable textinsets
882                 if (autoBreakRows_) {
883                         string cur_layout = cpar()->layout()->name();
884
885                         // Derive layout number from given argument (string)
886                         // and current buffer's textclass (number).
887                         LyXTextClass const & tclass =
888                                 bv->buffer()->params().getLyXTextClass();
889                         string layout = cmd.argument;
890                         bool hasLayout = tclass.hasLayout(layout);
891
892                         // If the entry is obsolete, use the new one instead.
893                         if (hasLayout) {
894                                 string const & obs = tclass[layout]->obsoleted_by();
895                                 if (!obs.empty())
896                                         layout = obs;
897                         }
898
899                         // see if we found the layout number:
900                         if (!hasLayout) {
901                                 FuncRequest lf(LFUN_MESSAGE, N_("Layout ") + cmd.argument + N_(" not known"));
902                                 bv->owner()->dispatch(lf);
903                                 break;
904                         }
905
906                         if (cur_layout != layout) {
907                                 cur_layout = layout;
908                                 text_.setLayout(layout);
909                                 bv->owner()->setLayout(cpar()->layout()->name());
910 #warning should be also set dispatched here?
911                                 result.update(true);
912                         }
913                 } else {
914                         // reset the layout box
915                         bv->owner()->setLayout(cpar()->layout()->name());
916                 }
917                 break;
918
919         default:
920                 break;
921         }
922
923         if (result.update()) {
924                 result.update(false);
925                 updateLocal(bv, true);
926         }
927
928         /// If the action has deleted all text in the inset, we need to change the
929         // language to the language of the surronding text.
930         if (!was_empty && paragraphs.begin()->empty() &&
931             paragraphs.size() == 1) {
932                 LyXFont font(LyXFont::ALL_IGNORE);
933                 font.setLanguage(bv->getParentLanguage(this));
934                 setFont(bv, font, false);
935         }
936
937         if (result.val() >= FINISHED) {
938                 result.val(NONE);
939                 bv->unlockInset(this);
940         }
941
942         return result;
943 }
944
945
946 int InsetText::latex(Buffer const & buf, ostream & os,
947                      LatexRunParams const & runparams) const
948 {
949         TexRow texrow;
950         latexParagraphs(buf, paragraphs, os, texrow, runparams);
951         return texrow.rows();
952 }
953
954
955 int InsetText::ascii(Buffer const & buf, ostream & os,
956                      LatexRunParams const & runparams) const
957 {
958         ParagraphList::const_iterator beg = paragraphs.begin();
959         ParagraphList::const_iterator end = paragraphs.end();
960         ParagraphList::const_iterator it = beg;
961         for (; it != end; ++it)
962                 asciiParagraph(buf, *it, os, runparams, it == beg);
963
964         //FIXME: Give the total numbers of lines
965         return 0;
966 }
967
968
969 int InsetText::linuxdoc(Buffer const & buf, ostream & os,
970                         LatexRunParams const & runparams) const
971 {
972         linuxdocParagraphs(buf, paragraphs, os, runparams);
973         return 0;
974 }
975
976
977 int InsetText::docbook(Buffer const & buf, ostream & os,
978                        LatexRunParams const & runparams) const
979 {
980         docbookParagraphs(buf, paragraphs, os, runparams);
981         return 0;
982 }
983
984
985 void InsetText::validate(LaTeXFeatures & features) const
986 {
987         for_each(paragraphs.begin(), paragraphs.end(),
988                  boost::bind(&Paragraph::validate, _1, boost::ref(features)));
989 }
990
991
992 void InsetText::getCursor(BufferView & bv, int & x, int & y) const
993 {
994         if (the_locking_inset) {
995                 the_locking_inset->getCursor(bv, x, y);
996                 return;
997         }
998         x = cx();
999         y = cy() + InsetText::y();
1000 }
1001
1002
1003 void InsetText::getCursorPos(BufferView * bv, int & x, int & y) const
1004 {
1005         if (the_locking_inset) {
1006                 the_locking_inset->getCursorPos(bv, x, y);
1007                 return;
1008         }
1009         x = cx() - top_x - TEXT_TO_INSET_OFFSET;
1010         y = cy() - TEXT_TO_INSET_OFFSET;
1011 }
1012
1013
1014 int InsetText::insetInInsetY() const
1015 {
1016         if (!the_locking_inset)
1017                 return 0;
1018
1019         return inset_y + the_locking_inset->insetInInsetY();
1020 }
1021
1022
1023 void InsetText::fitInsetCursor(BufferView * bv) const
1024 {
1025         if (the_locking_inset) {
1026                 the_locking_inset->fitInsetCursor(bv);
1027                 return;
1028         }
1029
1030         LyXFont const font = text_.getFont(cpar(), cpos());
1031
1032         int const asc = font_metrics::maxAscent(font);
1033         int const desc = font_metrics::maxDescent(font);
1034
1035         bv->fitLockedInsetCursor(cx(), cy(), asc, desc);
1036 }
1037
1038
1039 DispatchResult InsetText::moveRight(BufferView * bv)
1040 {
1041         if (text_.cursorPar()->isRightToLeftPar(bv->buffer()->params()))
1042                 return moveLeftIntern(bv, false, true, false);
1043         else
1044                 return moveRightIntern(bv, true, true, false);
1045 }
1046
1047
1048 DispatchResult InsetText::moveLeft(BufferView * bv)
1049 {
1050         if (text_.cursorPar()->isRightToLeftPar(bv->buffer()->params()))
1051                 return moveRightIntern(bv, true, true, false);
1052         else
1053                 return moveLeftIntern(bv, false, true, false);
1054 }
1055
1056
1057 DispatchResult
1058 InsetText::moveRightIntern(BufferView * bv, bool front,
1059                            bool activate_inset, bool selecting)
1060 {
1061         ParagraphList::iterator c_par = cpar();
1062
1063         if (boost::next(c_par) == paragraphs.end() && cpos() >= c_par->size())
1064                 return DispatchResult(false, FINISHED_RIGHT);
1065         if (activate_inset && checkAndActivateInset(bv, front))
1066                 return DispatchResult(true, true);
1067         text_.cursorRight(bv);
1068         if (!selecting)
1069                 text_.clearSelection();
1070         return DispatchResult(true);
1071 }
1072
1073
1074 DispatchResult
1075 InsetText::moveLeftIntern(BufferView * bv, bool front,
1076                           bool activate_inset, bool selecting)
1077 {
1078         if (cpar() == paragraphs.begin() && cpos() <= 0)
1079                 return DispatchResult(false, FINISHED);
1080         text_.cursorLeft(bv);
1081         if (!selecting)
1082                 text_.clearSelection();
1083         if (activate_inset && checkAndActivateInset(bv, front))
1084                 return DispatchResult(true, true);
1085         return DispatchResult(true);
1086 }
1087
1088
1089 DispatchResult InsetText::moveUp(BufferView * bv)
1090 {
1091         if (crow() == text_.firstRow())
1092                 return DispatchResult(false, FINISHED_UP);
1093         text_.cursorUp(bv);
1094         text_.clearSelection();
1095         return DispatchResult(true);
1096 }
1097
1098
1099 DispatchResult InsetText::moveDown(BufferView * bv)
1100 {
1101         if (crow() == text_.lastRow())
1102                 return DispatchResult(false, FINISHED_DOWN);
1103         text_.cursorDown(bv);
1104         text_.clearSelection();
1105         return DispatchResult(true);
1106 }
1107
1108
1109 bool InsetText::insertInset(BufferView * bv, InsetOld * inset)
1110 {
1111         if (the_locking_inset) {
1112                 if (the_locking_inset->insetAllowed(inset))
1113                         return the_locking_inset->insertInset(bv, inset);
1114                 return false;
1115         }
1116         inset->setOwner(this);
1117         text_.insertInset(inset);
1118         bv->fitCursor();
1119         updateLocal(bv, true);
1120         return true;
1121 }
1122
1123
1124 bool InsetText::insetAllowed(InsetOld::Code code) const
1125 {
1126         // in_insetAllowed is a really gross hack,
1127         // to allow us to call the owner's insetAllowed
1128         // without stack overflow, which can happen
1129         // when the owner uses InsetCollapsable::insetAllowed()
1130         bool ret = true;
1131         if (in_insetAllowed)
1132                 return ret;
1133         in_insetAllowed = true;
1134         if (the_locking_inset)
1135                 ret = the_locking_inset->insetAllowed(code);
1136         else if (owner())
1137                 ret = owner()->insetAllowed(code);
1138         in_insetAllowed = false;
1139         return ret;
1140 }
1141
1142
1143 UpdatableInset * InsetText::getLockingInset() const
1144 {
1145         return the_locking_inset ? the_locking_inset->getLockingInset() :
1146                 const_cast<InsetText *>(this);
1147 }
1148
1149
1150 UpdatableInset * InsetText::getFirstLockingInsetOfType(InsetOld::Code c)
1151 {
1152         if (c == lyxCode())
1153                 return this;
1154         if (the_locking_inset)
1155                 return the_locking_inset->getFirstLockingInsetOfType(c);
1156         return 0;
1157 }
1158
1159
1160 bool InsetText::showInsetDialog(BufferView * bv) const
1161 {
1162         if (the_locking_inset)
1163                 return the_locking_inset->showInsetDialog(bv);
1164         return false;
1165 }
1166
1167
1168 void InsetText::getLabelList(Buffer const & buffer,
1169                              std::vector<string> & list) const
1170 {
1171         ParagraphList::const_iterator pit = paragraphs.begin();
1172         ParagraphList::const_iterator pend = paragraphs.end();
1173         for (; pit != pend; ++pit) {
1174                 InsetList::const_iterator beg = pit->insetlist.begin();
1175                 InsetList::const_iterator end = pit->insetlist.end();
1176                 for (; beg != end; ++beg)
1177                         beg->inset->getLabelList(buffer, list);
1178         }
1179 }
1180
1181
1182 void InsetText::setFont(BufferView * bv, LyXFont const & font, bool toggleall,
1183                         bool selectall)
1184 {
1185         if (the_locking_inset) {
1186                 the_locking_inset->setFont(bv, font, toggleall, selectall);
1187                 return;
1188         }
1189
1190         if ((paragraphs.size() == 1 && paragraphs.begin()->empty())
1191             || cpar()->empty()) {
1192                 text_.setFont(font, toggleall);
1193                 return;
1194         }
1195
1196
1197         if (text_.selection.set())
1198                 text_.recUndo(text_.cursor.par());
1199
1200         if (selectall) {
1201                 text_.cursorTop();
1202                 text_.selection.cursor = text_.cursor;
1203                 text_.cursorBottom();
1204                 text_.setSelection();
1205         }
1206
1207         text_.toggleFree(font, toggleall);
1208
1209         if (selectall)
1210                 text_.clearSelection();
1211
1212         bv->fitCursor();
1213         updateLocal(bv, true);
1214 }
1215
1216
1217 bool InsetText::checkAndActivateInset(BufferView * bv, bool front)
1218 {
1219         if (cpos() == cpar()->size())
1220                 return false;
1221         InsetOld * inset = cpar()->getInset(cpos());
1222         if (!isHighlyEditableInset(inset))
1223                 return false;
1224         FuncRequest cmd(bv, LFUN_INSET_EDIT, front ? "left" : "right");
1225         inset->dispatch(cmd);
1226         if (!the_locking_inset)
1227                 return false;
1228         updateLocal(bv, false);
1229         return true;
1230 }
1231
1232
1233 bool InsetText::checkAndActivateInset(BufferView * bv, int x, int y,
1234                                       mouse_button::state button)
1235 {
1236         int dummyx = x;
1237         int dummyy = y + dim_.asc;
1238         InsetOld * inset = getLyXText(bv)->checkInsetHit(dummyx, dummyy);
1239         // we only do the edit() call if the inset was hit by the mouse
1240         // or if it is a highly editable inset. So we should call this
1241         // function from our own edit with button < 0.
1242         // FIXME: GUII jbl. I've changed this to ::none for now which is probably
1243         // WRONG
1244         if (button == mouse_button::none && !isHighlyEditableInset(inset))
1245                 return false;
1246
1247         if (!inset)
1248                 return false;
1249         if (x < 0)
1250                 x = dim_.wid;
1251         if (y < 0)
1252                 y = dim_.des;
1253         inset_x = cx() - top_x;
1254         inset_y = cy();
1255         FuncRequest cmd(bv, LFUN_INSET_EDIT, x - inset_x, y - inset_y, button);
1256         inset->dispatch(cmd);
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 }