]> git.lyx.org Git - lyx.git/blob - src/insets/insettext.C
replace ParagraphList::iterator by lyx::paroffset_type to prevent cursor
[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(),
68           paragraphs(1),
69           autoBreakRows_(false),
70           drawFrame_(NEVER),
71           frame_color_(LColor::insetframe),
72           text_(0, this, true, paragraphs)
73 {
74         textwidth_ = 0; // broken
75         paragraphs.begin()->layout(bp.getLyXTextClass().defaultLayout());
76         if (bp.tracking_changes)
77                 paragraphs.begin()->trackChanges();
78         init();
79 }
80
81
82 InsetText::InsetText(InsetText const & in)
83         : UpdatableInset(in),
84           text_(in.text_.bv_owner, this, true, paragraphs)
85 {
86         // this is ugly...
87         operator=(in);
88 }
89
90
91 void InsetText::operator=(InsetText const & in)
92 {
93         UpdatableInset::operator=(in);
94         paragraphs = in.paragraphs;
95         autoBreakRows_ = in.autoBreakRows_;
96         drawFrame_ = in.drawFrame_;
97         frame_color_ = in.frame_color_;
98         textwidth_ = in.textwidth_;
99         text_ = LyXText(in.text_.bv_owner, this, true, paragraphs);
100         init();
101 }
102
103
104 void InsetText::init()
105 {
106         ParagraphList::iterator pit = paragraphs.begin();
107         ParagraphList::iterator end = paragraphs.end();
108         for (; pit != end; ++pit)
109                 pit->setInsetOwner(this);
110         text_.paragraphs_ = &paragraphs;
111         top_y = 0;
112
113         locked = false;
114         inset_par = paragraphs.end();
115         inset_pos = 0;
116         inset_x = 0;
117         inset_y = 0;
118         no_selection = true;
119         the_locking_inset = 0;
120         old_par = paragraphs.end();
121         in_insetAllowed = false;
122         mouse_x = 0;
123         mouse_y = 0;
124 }
125
126
127 void InsetText::clear(bool just_mark_erased)
128 {
129         if (just_mark_erased) {
130                 ParagraphList::iterator it = paragraphs.begin();
131                 ParagraphList::iterator end = paragraphs.end();
132                 for (; it != end; ++it)
133                         it->markErased();
134                 return;
135         }
136
137         // This is a gross hack...
138         LyXLayout_ptr old_layout = paragraphs.begin()->layout();
139
140         paragraphs.clear();
141         paragraphs.push_back(Paragraph());
142         paragraphs.begin()->setInsetOwner(this);
143         paragraphs.begin()->layout(old_layout);
144 }
145
146
147 auto_ptr<InsetBase> InsetText::clone() const
148 {
149         return auto_ptr<InsetBase>(new InsetText(*this));
150 }
151
152
153 void InsetText::write(Buffer const & buf, ostream & os) const
154 {
155         os << "Text\n";
156         writeParagraphData(buf, os);
157 }
158
159
160 void InsetText::writeParagraphData(Buffer const & buf, ostream & os) const
161 {
162         ParagraphList::const_iterator it = paragraphs.begin();
163         ParagraphList::const_iterator end = paragraphs.end();
164         Paragraph::depth_type dth = 0;
165         for (; it != end; ++it) {
166                 it->write(buf, os, buf.params(), dth);
167         }
168 }
169
170
171 void InsetText::read(Buffer const & buf, LyXLex & lex)
172 {
173         string token;
174         Paragraph::depth_type depth = 0;
175
176         clear(false);
177
178 #warning John, look here. Doesnt make much sense.
179         if (buf.params().tracking_changes)
180                 paragraphs.begin()->trackChanges();
181
182         // delete the initial paragraph
183         Paragraph oldpar = *paragraphs.begin();
184         paragraphs.clear();
185         ParagraphList::iterator pit = paragraphs.begin();
186
187         while (lex.isOK()) {
188                 lex.nextToken();
189                 token = lex.getString();
190                 if (token.empty())
191                         continue;
192                 if (token == "\\end_inset") {
193                         break;
194                 }
195
196                 if (token == "\\end_document") {
197                         lex.printError("\\end_document read in inset! Error in document!");
198                         return;
199                 }
200
201                 // FIXME: ugly.
202                 const_cast<Buffer&>(buf).readParagraph(lex, token, paragraphs, pit, depth);
203         }
204
205         pit = paragraphs.begin();
206         ParagraphList::iterator const end = paragraphs.end();
207         for (; pit != end; ++pit)
208                 pit->setInsetOwner(this);
209
210         if (token != "\\end_inset") {
211                 lex.printError("Missing \\end_inset at this point. "
212                                            "Read: `$$Token'");
213         }
214
215         // sanity check
216         // ensure we have at least one par.
217         if (paragraphs.empty())
218                 paragraphs.push_back(oldpar);
219 }
220
221
222 void InsetText::metrics(MetricsInfo & mi, Dimension & dim) const
223 {
224         //lyxerr << "InsetText::metrics: width: " << mi.base.textwidth << endl;
225
226         textwidth_ = mi.base.textwidth;
227         BufferView * bv = mi.base.bv;
228         setViewCache(bv);
229         text_.metrics(mi, dim);
230         dim.asc += TEXT_TO_INSET_OFFSET;
231         dim.des += TEXT_TO_INSET_OFFSET;
232         dim.wid += 2 * TEXT_TO_INSET_OFFSET;
233         dim.wid = max(dim.wid, 10);
234         dim_ = dim;
235 }
236
237
238 int InsetText::textWidth() const
239 {
240         return textwidth_;
241 }
242
243
244 void InsetText::draw(PainterInfo & pi, int x, int y) const
245 {
246         // update our idea of where we are. Clearly, we should
247         // not have to know this information.
248         top_x = x;
249
250         int const start_x = x;
251
252         BufferView * bv = pi.base.bv;
253         Painter & pain = pi.pain;
254
255         // repaint the background if needed
256         if (backgroundColor() != LColor::background)
257                 clearInset(bv, start_x + TEXT_TO_INSET_OFFSET, y);
258
259         // no draw is necessary !!!
260         if (drawFrame_ == LOCKED && !locked && paragraphs.begin()->empty()) {
261                 top_baseline = y;
262                 return;
263         }
264
265         bv->hideCursor();
266
267         if (!owner())
268                 x += scroll();
269
270         top_baseline = y;
271         top_y = y - dim_.asc;
272
273         if (the_locking_inset && cpar() == inset_par && cpos() == inset_pos) {
274                 inset_x = cx() - x;
275                 inset_y = cy();
276         }
277
278         x += TEXT_TO_INSET_OFFSET;
279
280         paintTextInset(*bv, text_, x, y);
281
282         if (drawFrame_ == ALWAYS || (drawFrame_ == LOCKED && locked))
283                 drawFrame(pain, start_x);
284 }
285
286
287 void InsetText::drawFrame(Painter & pain, int x) const
288 {
289         int const ttoD2 = TEXT_TO_INSET_OFFSET / 2;
290         int const frame_x = x + ttoD2;
291         int const frame_y = top_baseline - dim_.asc + ttoD2;
292         int const frame_w = dim_.wid - TEXT_TO_INSET_OFFSET;
293         int const frame_h = dim_.asc + dim_.des - TEXT_TO_INSET_OFFSET;
294         pain.rectangle(frame_x, frame_y, frame_w, frame_h, frameColor());
295 }
296
297
298 void InsetText::updateLocal(BufferView * bv, bool /*mark_dirty*/)
299 {
300         if (!bv)
301                 return;
302
303         if (!autoBreakRows_ && paragraphs.size() > 1)
304                 collapseParagraphs(bv);
305
306         if (!text_.selection.set())
307                 text_.selection.cursor = text_.cursor;
308
309         bv->fitCursor();
310         bv->updateInset(this);
311         bv->owner()->view_state_changed();
312         bv->owner()->updateMenubar();
313         bv->owner()->updateToolbar();
314         if (old_par != cpar()) {
315                 bv->owner()->setLayout(cpar()->layout()->name());
316                 old_par = cpar();
317         }
318 }
319
320
321 string const InsetText::editMessage() const
322 {
323         return _("Opened Text Inset");
324 }
325
326
327 void InsetText::insetUnlock(BufferView * bv)
328 {
329         if (the_locking_inset) {
330                 the_locking_inset->insetUnlock(bv);
331                 the_locking_inset = 0;
332                 updateLocal(bv, false);
333         }
334         no_selection = true;
335         locked = false;
336
337         if (text_.selection.set())
338                 text_.clearSelection();
339         else if (owner())
340                 bv->owner()->setLayout(owner()->getLyXText(bv)
341                                        ->cursorPar()->layout()->name());
342         else
343                 bv->owner()->setLayout(bv->text->cursorPar()->layout()->name());
344         // hack for deleteEmptyParMech
345         if (!paragraphs.begin()->empty())
346                 text_.setCursor(0, 0);
347         else if (paragraphs.size() > 1)
348                 text_.setCursor(1, 0);
349 }
350
351
352 void InsetText::lockInset(BufferView * bv)
353 {
354         locked = true;
355         the_locking_inset = 0;
356         inset_pos = inset_x = inset_y = 0;
357         inset_boundary = false;
358         inset_par = paragraphs.end();
359         old_par = paragraphs.end();
360         text_.setCursorIntern(0, 0);
361         text_.clearSelection();
362         finishUndo();
363         // If the inset is empty set the language of the current font to the
364         // language to the surronding text (if different).
365         if (paragraphs.begin()->empty() && paragraphs.size() == 1 &&
366                 bv->getParentLanguage(this) != text_.current_font.language()) {
367                 LyXFont font(LyXFont::ALL_IGNORE);
368                 font.setLanguage(bv->getParentLanguage(this));
369                 setFont(bv, font, false);
370         }
371 }
372
373
374 void InsetText::lockInset(BufferView * /*bv*/, UpdatableInset * inset)
375 {
376         the_locking_inset = inset;
377         inset_x = cx() - top_x;
378         inset_y = cy();
379         inset_pos = cpos();
380         inset_par = cpar();
381         inset_boundary = cboundary();
382 }
383
384
385 bool InsetText::lockInsetInInset(BufferView * bv, UpdatableInset * inset)
386 {
387         lyxerr[Debug::INSETS] << "InsetText::LockInsetInInset("
388                               << inset << "): " << endl;
389         if (!inset)
390                 return false;
391         if (!the_locking_inset) {
392                 ParagraphList::iterator pit = paragraphs.begin();
393                 ParagraphList::iterator pend = paragraphs.end();
394
395                 for (; pit != pend; ++pit) {
396                         InsetList::iterator it = pit->insetlist.begin();
397                         InsetList::iterator const end = pit->insetlist.end();
398                         for (; it != end; ++it) {
399                                 if (it->inset == inset) {
400                                         lyxerr << "InsetText::lockInsetInInset: 1 a" << endl;
401                                         text_.setCursorIntern(  
402                                                 std::distance(paragraphs.begin(), pit), it->pos);
403                                         lyxerr << "InsetText::lockInsetInInset: 1 b" << endl;
404                                         lyxerr << "bv: " << bv << " inset: " << inset << endl;
405                                         lockInset(bv, inset);
406                                         lyxerr << "InsetText::lockInsetInInset: 1 c" << endl;
407                                         return true;
408                                 }
409                         }
410                 }
411                 lyxerr << "InsetText::lockInsetInInset: 3" << endl;
412                 return false;
413         }
414         if (inset == cpar()->getInset(cpos())) {
415                 lyxerr[Debug::INSETS] << "OK" << endl;
416                 lockInset(bv, inset);
417                 return true;
418         }
419
420         if (the_locking_inset && the_locking_inset == inset) {
421                 if (cpar() == inset_par && cpos() == inset_pos) {
422                         lyxerr[Debug::INSETS] << "OK" << endl;
423                         inset_x = cx() - top_x;
424                         inset_y = cy();
425                 } else {
426                         lyxerr[Debug::INSETS] << "cursor.pos != inset_pos" << endl;
427                 }
428         } else if (the_locking_inset) {
429                 lyxerr[Debug::INSETS] << "MAYBE" << endl;
430                 return the_locking_inset->lockInsetInInset(bv, inset);
431         }
432         lyxerr[Debug::INSETS] << "NOT OK" << endl;
433         return false;
434 }
435
436
437 bool InsetText::unlockInsetInInset(BufferView * bv, UpdatableInset * inset,
438                                    bool lr)
439 {
440         if (!the_locking_inset)
441                 return false;
442         if (the_locking_inset == inset) {
443                 the_locking_inset->insetUnlock(bv);
444                 the_locking_inset = 0;
445                 if (lr)
446                         moveRightIntern(bv, true, false);
447                 old_par = paragraphs.end(); // force layout setting
448                 if (scroll())
449                         scroll(bv, 0.0F);
450                 else
451                         updateLocal(bv, false);
452                 return true;
453         }
454         return the_locking_inset->unlockInsetInInset(bv, inset, lr);
455 }
456
457
458 void InsetText::lfunMousePress(FuncRequest const & cmd)
459 {
460         no_selection = true;
461
462         // use this to check mouse motion for selection!
463         mouse_x = cmd.x;
464         mouse_y = cmd.y;
465
466         BufferView * bv = cmd.view();
467         FuncRequest cmd1 = cmd;
468         cmd1.x -= inset_x;
469         cmd1.y -= inset_y;
470         if (!locked)
471                 lockInset(bv);
472
473         int tmp_x = cmd.x;
474         int tmp_y = cmd.y + dim_.asc - bv->top_y();
475         InsetOld * inset = getLyXText(bv)->checkInsetHit(tmp_x, tmp_y);
476
477         if (the_locking_inset) {
478                 if (the_locking_inset == inset) {
479                         the_locking_inset->localDispatch(cmd1);
480                         return;
481                 }
482                 // otherwise only unlock the_locking_inset
483                 the_locking_inset->insetUnlock(bv);
484                 the_locking_inset = 0;
485         }
486         if (!inset)
487                 no_selection = false;
488
489         if (bv->theLockingInset()) {
490                 if (isHighlyEditableInset(inset)) {
491                         // We just have to lock the inset before calling a
492                         // PressEvent on it!
493                         UpdatableInset * uinset = static_cast<UpdatableInset*>(inset);
494                         if (!bv->lockInset(uinset)) {
495                                 lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
496                         }
497                         inset->localDispatch(cmd1);
498                         if (the_locking_inset)
499                                 updateLocal(bv, false);
500                         return;
501                 }
502         }
503         if (!inset) {
504                 bool paste_internally = false;
505                 if (cmd.button() == mouse_button::button2 && getLyXText(bv)->selection.set()) {
506                         localDispatch(FuncRequest(bv, LFUN_COPY));
507                         paste_internally = true;
508                 }
509                 int old_top_y = bv->top_y();
510
511                 text_.setCursorFromCoordinates(cmd.x, cmd.y + dim_.asc);
512                 // set the selection cursor!
513                 text_.selection.cursor = text_.cursor;
514                 text_.cursor.x_fix(text_.cursor.x());
515
516                 text_.clearSelection();
517                 updateLocal(bv, false);
518
519                 bv->owner()->setLayout(cpar()->layout()->name());
520
521                 // we moved the view we cannot do mouse selection in this case!
522                 if (bv->top_y() != old_top_y)
523                         no_selection = true;
524                 old_par = cpar();
525                 // Insert primary selection with middle mouse
526                 // if there is a local selection in the current buffer,
527                 // insert this
528                 if (cmd.button() == mouse_button::button2) {
529                         if (paste_internally)
530                                 localDispatch(FuncRequest(bv, LFUN_PASTE));
531                         else
532                                 localDispatch(FuncRequest(bv, LFUN_PASTESELECTION, "paragraph"));
533                 }
534         } else {
535                 getLyXText(bv)->clearSelection();
536         }
537 }
538
539
540 bool InsetText::lfunMouseRelease(FuncRequest const & cmd)
541 {
542         BufferView * bv = cmd.view();
543         FuncRequest cmd1 = cmd;
544         cmd1.x -= inset_x;
545         cmd1.y -= inset_y;
546
547         no_selection = true;
548         if (the_locking_inset)
549                 return the_locking_inset->localDispatch(cmd1);
550
551         int tmp_x = cmd.x;
552         int tmp_y = cmd.y + dim_.asc - bv->top_y();
553         InsetOld * inset = getLyXText(bv)->checkInsetHit(tmp_x, tmp_y);
554         bool ret = false;
555         if (inset) {
556 // This code should probably be removed now. Simple insets
557 // (!highlyEditable) can actually take the localDispatch,
558 // and turn it into edit() if necessary. But we still
559 // need to deal properly with the whole relative vs.
560 // absolute mouse co-ords thing in a realiable, sensible way
561 #if 0
562                 if (isHighlyEditableInset(inset))
563                         ret = inset->localDispatch(cmd1);
564                 else {
565                         inset_x = cx(bv) - top_x;
566                         inset_y = cy();
567                         cmd1.x = cmd.x - inset_x;
568                         cmd1.y = cmd.x - inset_y;
569                         inset->edit(bv, cmd1.x, cmd1.y, cmd.button());
570                         ret = true;
571                 }
572 #endif
573                 ret = inset->localDispatch(cmd1);
574                 updateLocal(bv, false);
575
576         }
577         return ret;
578 }
579
580
581 void InsetText::lfunMouseMotion(FuncRequest const & cmd)
582 {
583         FuncRequest cmd1 = cmd;
584         cmd1.x -= inset_x;
585         cmd1.y -= inset_y;
586
587         if (the_locking_inset) {
588                 the_locking_inset->localDispatch(cmd1);
589                 return;
590         }
591
592         if (no_selection || (mouse_x == cmd.x && mouse_y == cmd.y))
593                 return;
594
595         BufferView * bv = cmd.view();
596         LyXCursor cur = text_.cursor;
597         text_.setCursorFromCoordinates (cmd.x, cmd.y + dim_.asc);
598         text_.cursor.x_fix(text_.cursor.x());
599         if (cur == text_.cursor)
600                 return;
601         text_.setSelection();
602         updateLocal(bv, false);
603 }
604
605
606 InsetOld::RESULT InsetText::localDispatch(FuncRequest const & cmd)
607 {
608         BufferView * bv = cmd.view();
609         setViewCache(bv);
610
611         switch (cmd.action) {
612         case LFUN_INSET_EDIT: {
613                 UpdatableInset::localDispatch(cmd);
614
615                 if (!bv->lockInset(this)) {
616                         lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
617                         return DISPATCHED;
618                 }
619
620                 locked = true;
621                 the_locking_inset = 0;
622                 inset_pos = 0;
623                 inset_x = 0;
624                 inset_y = 0;
625                 inset_boundary = false;
626                 inset_par = paragraphs.end();
627                 old_par = paragraphs.end();
628
629
630                 if (cmd.argument.size()) {
631                         if (cmd.argument == "left")
632                                 text_.setCursorIntern(0, 0);
633                         else 
634                                 text_.setCursor(paragraphs.size() - 1, paragraphs.back().size());
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_.cursorPar());
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_.cursorPar()->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_.cursorPar()->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_.cursorPar());
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_.cursorPar(), 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_.cursorPar();
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                 text_.setCursor(paragraphs.size() - 1, paragraphs.back().size());
1723         }
1724         lyx::find::SearchResult result =
1725                 lyx::find::find(bv, &text_, str, false, cs, mw);
1726
1727         if (result == lyx::find::SR_FOUND) {
1728                 LyXCursor cur = text_.cursor;
1729                 bv->unlockInset(bv->theLockingInset());
1730                 if (bv->lockInset(this))
1731                         locked = true;
1732                 text_.cursor = cur;
1733                 text_.setSelectionRange(str.length());
1734                 updateLocal(bv, false);
1735         }
1736         return result != lyx::find::SR_NOT_FOUND;
1737 }
1738
1739
1740 bool InsetText::checkInsertChar(LyXFont & font)
1741 {
1742         return owner() ? owner()->checkInsertChar(font) : true;
1743 }
1744
1745
1746 void InsetText::collapseParagraphs(BufferView * bv)
1747 {
1748         while (paragraphs.size() > 1) {
1749                 ParagraphList::iterator const first = paragraphs.begin();
1750                 ParagraphList::iterator second = first;
1751                 advance(second, 1);
1752                 size_t const first_par_size = first->size();
1753
1754                 if (!first->empty() &&
1755                     !second->empty() &&
1756                     !first->isSeparator(first_par_size - 1)) {
1757                         first->insertChar(first_par_size, ' ');
1758                 }
1759
1760 #warning probably broken
1761                 if (text_.selection.set()) {
1762                         if (text_.selection.start.par() == 1) {
1763                                 text_.selection.start.par(1);
1764                                 text_.selection.start.pos(text_.selection.start.pos() + first_par_size);
1765                         }
1766                         if (text_.selection.end.par() == 2) {
1767                                 text_.selection.end.par(1);
1768                                 text_.selection.end.pos(text_.selection.end.pos() + first_par_size);
1769                         }
1770                 }
1771
1772                 mergeParagraph(bv->buffer()->params(), paragraphs, first);
1773         }
1774 }
1775
1776
1777 void InsetText::getDrawFont(LyXFont & font) const
1778 {
1779         if (!owner())
1780                 return;
1781         owner()->getDrawFont(font);
1782 }
1783
1784
1785 void InsetText::appendParagraphs(Buffer * buffer, ParagraphList & plist)
1786 {
1787 #warning FIXME Check if Changes stuff needs changing here. (Lgb)
1788 // And it probably does. You have to take a look at this John. (Lgb)
1789 #warning John, have a look here. (Lgb)
1790         ParagraphList::iterator pit = plist.begin();
1791         ParagraphList::iterator ins = paragraphs.insert(paragraphs.end(), *pit);
1792         ++pit;
1793         mergeParagraph(buffer->params(), paragraphs, boost::prior(ins));
1794
1795         ParagraphList::iterator pend = plist.end();
1796         for (; pit != pend; ++pit)
1797                 paragraphs.push_back(*pit);
1798 }
1799
1800
1801 void InsetText::addPreview(PreviewLoader & loader) const
1802 {
1803         ParagraphList::const_iterator pit = paragraphs.begin();
1804         ParagraphList::const_iterator pend = paragraphs.end();
1805
1806         for (; pit != pend; ++pit) {
1807                 InsetList::const_iterator it  = pit->insetlist.begin();
1808                 InsetList::const_iterator end = pit->insetlist.end();
1809                 for (; it != end; ++it)
1810                         it->inset->addPreview(loader);
1811         }
1812 }