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