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