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