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