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