]> git.lyx.org Git - lyx.git/blob - src/insets/insettext.C
refrain from computing width in setHeightOfRow as this happens already
[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.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 = -1;
115         inset_pos = 0;
116         inset_x = 0;
117         inset_y = 0;
118         no_selection = true;
119         the_locking_inset = 0;
120         old_par = -1;
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         textwidth_ = mi.base.textwidth - 30;
226         BufferView * bv = mi.base.bv;
227         setViewCache(bv);
228         text_.metrics(mi, dim);
229         dim.asc += TEXT_TO_INSET_OFFSET;
230         dim.des += TEXT_TO_INSET_OFFSET;
231         dim.wid += 2 * TEXT_TO_INSET_OFFSET;
232         dim.wid = max(dim.wid, 10);
233         dim_ = dim;
234 }
235
236
237 int InsetText::textWidth() const
238 {
239         return textwidth_;
240 }
241
242
243 void InsetText::draw(PainterInfo & pi, int x, int y) const
244 {
245         // update our idea of where we are. Clearly, we should
246         // not have to know this information.
247         top_x = x;
248
249         int const start_x = x;
250
251         BufferView * bv = pi.base.bv;
252         Painter & pain = pi.pain;
253
254         // repaint the background if needed
255         if (backgroundColor() != LColor::background)
256                 clearInset(bv, start_x + TEXT_TO_INSET_OFFSET, y);
257
258         // no draw is necessary !!!
259         if (drawFrame_ == LOCKED && !locked && paragraphs.begin()->empty()) {
260                 top_baseline = y;
261                 return;
262         }
263
264         bv->hideCursor();
265
266         if (!owner())
267                 x += scroll();
268
269         top_baseline = y;
270         top_y = y - dim_.asc;
271
272         if (the_locking_inset
273                   && text_.cursor.par() == 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 != text_.cursor.par()) {
315                 bv->owner()->setLayout(cpar()->layout()->name());
316                 old_par = text_.cursor.par();
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 =  -1;
359         old_par = -1;
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 = text_.cursor.par();
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 (text_.cursor.par() == 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 = -1; // 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->dispatch(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->dispatch(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                         dispatch(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 = text_.cursor.par();
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                                 dispatch(FuncRequest(bv, LFUN_PASTE));
531                         else
532                                 dispatch(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->dispatch(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         if (!inset)
555                 return false;
556
557         // We still need to deal properly with the whole relative vs.
558         // absolute mouse co-ords thing in a realiable, sensible way
559         bool ret = inset->dispatch(cmd1);
560         updateLocal(bv, false);
561         return ret;
562 }
563
564
565 void InsetText::lfunMouseMotion(FuncRequest const & cmd)
566 {
567         FuncRequest cmd1 = cmd;
568         cmd1.x -= inset_x;
569         cmd1.y -= inset_y;
570
571         if (the_locking_inset) {
572                 the_locking_inset->dispatch(cmd1);
573                 return;
574         }
575
576         if (no_selection || (mouse_x == cmd.x && mouse_y == cmd.y))
577                 return;
578
579         BufferView * bv = cmd.view();
580         LyXCursor cur = text_.cursor;
581         text_.setCursorFromCoordinates (cmd.x, cmd.y + dim_.asc);
582         text_.cursor.x_fix(text_.cursor.x());
583         if (cur == text_.cursor)
584                 return;
585         text_.setSelection();
586         updateLocal(bv, false);
587 }
588
589
590 dispatch_result
591 InsetText::priv_dispatch(FuncRequest const & cmd,
592                          idx_type & idx, pos_type & pos)
593 {
594         BufferView * bv = cmd.view();
595         setViewCache(bv);
596
597         switch (cmd.action) {
598         case LFUN_INSET_EDIT: {
599                 UpdatableInset::priv_dispatch(cmd, idx, pos);
600
601                 if (!bv->lockInset(this)) {
602                         lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
603                         return DISPATCHED;
604                 }
605
606                 locked = true;
607                 the_locking_inset = 0;
608                 inset_pos = 0;
609                 inset_x = 0;
610                 inset_y = 0;
611                 inset_boundary = false;
612                 inset_par = -1;
613                 old_par = -1;
614
615
616                 if (cmd.argument.size()) {
617                         if (cmd.argument == "left")
618                                 text_.setCursorIntern(0, 0);
619                         else
620                                 text_.setCursor(paragraphs.size() - 1, paragraphs.back().size());
621                 } else {
622                         int tmp_y = (cmd.y < 0) ? 0 : cmd.y;
623                         // we put here -1 and not button as now the button in the
624                         // edit call should not be needed we will fix this in 1.3.x
625                         // cycle hopefully (Jug 20020509)
626                         // FIXME: GUII I've changed this to none: probably WRONG
627                         if (!checkAndActivateInset(bv, cmd.x, tmp_y, mouse_button::none)) {
628                                 text_.setCursorFromCoordinates(cmd.x, cmd.y + dim_.asc);
629                                 text_.cursor.x(text_.cursor.x());
630                                 text_.cursor.x_fix(text_.cursor.x());
631                         }
632                 }
633
634                 text_.clearSelection();
635                 finishUndo();
636
637                 // If the inset is empty set the language of the current font to the
638                 // language to the surronding text (if different).
639                 if (paragraphs.begin()->empty() &&
640                     paragraphs.size() == 1 &&
641                     bv->getParentLanguage(this) != text_.current_font.language())
642                 {
643                         LyXFont font(LyXFont::ALL_IGNORE);
644                         font.setLanguage(bv->getParentLanguage(this));
645                         setFont(bv, font, false);
646                 }
647
648                 updateLocal(bv, false);
649                 // Tell the paragraph dialog that we've entered an insettext.
650                 bv->dispatch(FuncRequest(LFUN_PARAGRAPH_UPDATE));
651                 return DISPATCHED;
652         }
653
654         case LFUN_MOUSE_PRESS:
655                 lfunMousePress(cmd);
656                 return DISPATCHED;
657
658         case LFUN_MOUSE_MOTION:
659                 lfunMouseMotion(cmd);
660                 return DISPATCHED;
661
662         case LFUN_MOUSE_RELEASE:
663                 return lfunMouseRelease(cmd) ? DISPATCHED : UNDISPATCHED;
664
665         default:
666                 break;
667         }
668
669         bool was_empty = paragraphs.begin()->empty() && paragraphs.size() == 1;
670         no_selection = false;
671
672         dispatch_result result = UpdatableInset::priv_dispatch(cmd, idx, pos);
673         if (result != UNDISPATCHED)
674                 return DISPATCHED;
675
676         result = DISPATCHED;
677         if (cmd.action < 0 && cmd.argument.empty())
678                 return FINISHED;
679
680         if (the_locking_inset) {
681                 result = the_locking_inset->dispatch(cmd);
682                 if (result == DISPATCHED_NOUPDATE)
683                         return result;
684                 if (result == DISPATCHED) {
685                         updateLocal(bv, false);
686                         return result;
687                 }
688                 if (result >= FINISHED) {
689                         switch (result) {
690                         case FINISHED_RIGHT:
691                                 moveRightIntern(bv, false, false);
692                                 result = DISPATCHED;
693                                 break;
694                         case FINISHED_UP:
695                                 result = moveUp(bv);
696                                 if (result >= FINISHED) {
697                                         updateLocal(bv, false);
698                                         bv->unlockInset(this);
699                                 }
700                                 break;
701                         case FINISHED_DOWN:
702                                 result = moveDown(bv);
703                                 if (result >= FINISHED) {
704                                         updateLocal(bv, false);
705                                         bv->unlockInset(this);
706                                 }
707                                 break;
708                         default:
709                                 result = DISPATCHED;
710                                 break;
711                         }
712                         the_locking_inset = 0;
713                         updateLocal(bv, false);
714                         // make sure status gets reset immediately
715                         bv->owner()->clearMessage();
716                         return result;
717                 }
718         }
719         bool updflag = false;
720
721         switch (cmd.action) {
722
723         // Normal chars
724         case LFUN_SELFINSERT:
725                 if (bv->buffer()->isReadonly()) {
726 //          setErrorMessage(N_("Document is read only"));
727                         break;
728                 }
729                 if (!cmd.argument.empty()) {
730                         /* Automatically delete the currently selected
731                          * text and replace it with what is being
732                          * typed in now. Depends on lyxrc settings
733                          * "auto_region_delete", which defaults to
734                          * true (on). */
735 #if 0
736                         // This should not be needed here and is also WRONG!
737                         recordUndo(bv, Undo::INSERT, text_.cursorPar());
738 #endif
739                         bv->switchKeyMap();
740
741                         if (lyxrc.auto_region_delete && text_.selection.set())
742                                 text_.cutSelection(false, false);
743                         text_.clearSelection();
744
745                         for (string::size_type i = 0; i < cmd.argument.length(); ++i)
746                                 bv->owner()->getIntl().getTransManager().
747                                         TranslateAndInsert(cmd.argument[i], &text_);
748                 }
749                 text_.selection.cursor = text_.cursor;
750                 updflag = true;
751                 result = DISPATCHED_NOUPDATE;
752                 break;
753
754         // cursor movements that need special handling
755
756         case LFUN_RIGHT:
757                 result = moveRight(bv);
758                 finishUndo();
759                 break;
760         case LFUN_LEFT:
761                 finishUndo();
762                 result = moveLeft(bv);
763                 break;
764         case LFUN_DOWN:
765                 finishUndo();
766                 result = moveDown(bv);
767                 break;
768         case LFUN_UP:
769                 finishUndo();
770                 result = moveUp(bv);
771                 break;
772
773         case LFUN_PRIOR:
774                 if (crow() == text_.firstRow())
775                         result = FINISHED_UP;
776                 else {
777                         text_.cursorPrevious();
778                         text_.clearSelection();
779                         result = DISPATCHED_NOUPDATE;
780                 }
781                 break;
782
783         case LFUN_NEXT:
784                 if (crow() == text_.lastRow())
785                         result = FINISHED_DOWN;
786                 else {
787                         text_.cursorNext();
788                         text_.clearSelection();
789                         result = DISPATCHED_NOUPDATE;
790                 }
791                 break;
792
793         case LFUN_BACKSPACE:
794                 if (text_.selection.set())
795                         text_.cutSelection(true, false);
796                 else
797                         text_.backspace();
798                 updflag = true;
799                 break;
800
801         case LFUN_DELETE:
802                 if (text_.selection.set())
803                         text_.cutSelection(true, false);
804                 else
805                         text_.Delete();
806                 updflag = true;
807                 break;
808
809         case LFUN_PASTE:
810                 if (!autoBreakRows_) {
811                         if (CutAndPaste::nrOfParagraphs() > 1) {
812 #ifdef WITH_WARNINGS
813 #warning FIXME horrendously bad UI
814 #endif
815                                 Alert::error(_("Paste failed"), _("Cannot include more than one paragraph."));
816                         }
817                 } else {
818                         replaceSelection(bv->getLyXText());
819                         size_t sel_index = 0;
820                         string const & arg = cmd.argument;
821                         if (isStrUnsignedInt(arg)) {
822 #warning FIXME Check if the arg is in the domain of available selections.
823                                 sel_index = strToUnsignedInt(arg);
824                         }
825                         text_.pasteSelection(sel_index);
826                         // bug 393
827                         text_.clearSelection();
828                         updflag = true;
829                 }
830                 break;
831
832         case LFUN_BREAKPARAGRAPH:
833                 if (!autoBreakRows_) {
834                         result = DISPATCHED;
835                 } else {
836                         replaceSelection(bv->getLyXText());
837                         text_.breakParagraph(paragraphs, 0);
838                         updflag = true;
839                 }
840                 break;
841
842         case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
843                 if (!autoBreakRows_) {
844                         result = DISPATCHED;
845                 } else {
846                         replaceSelection(bv->getLyXText());
847                         text_.breakParagraph(paragraphs, 1);
848                         updflag = true;
849                 }
850                 break;
851
852         case LFUN_BREAKLINE: {
853                 if (!autoBreakRows_) {
854                         result = DISPATCHED;
855                 } else {
856                         replaceSelection(bv->getLyXText());
857                         text_.insertInset(new InsetNewline);
858                         updflag = true;
859                 }
860                 break;
861         }
862
863         case LFUN_LAYOUT:
864                 // do not set layouts on non breakable textinsets
865                 if (autoBreakRows_) {
866                         string cur_layout = cpar()->layout()->name();
867
868                         // Derive layout number from given argument (string)
869                         // and current buffer's textclass (number).
870                         LyXTextClass const & tclass =
871                                 bv->buffer()->params().getLyXTextClass();
872                         string layout = cmd.argument;
873                         bool hasLayout = tclass.hasLayout(layout);
874
875                         // If the entry is obsolete, use the new one instead.
876                         if (hasLayout) {
877                                 string const & obs = tclass[layout]->obsoleted_by();
878                                 if (!obs.empty())
879                                         layout = obs;
880                         }
881
882                         // see if we found the layout number:
883                         if (!hasLayout) {
884                                 FuncRequest lf(LFUN_MESSAGE, N_("Layout ") + cmd.argument + N_(" not known"));
885                                 bv->owner()->dispatch(lf);
886                                 break;
887                         }
888
889                         if (cur_layout != layout) {
890                                 cur_layout = layout;
891                                 text_.setLayout(layout);
892                                 bv->owner()->setLayout(cpar()->layout()->name());
893                                 updflag = true;
894                         }
895                 } else {
896                         // reset the layout box
897                         bv->owner()->setLayout(cpar()->layout()->name());
898                 }
899                 break;
900
901         default:
902                 if (!bv->dispatch(cmd))
903                         result = UNDISPATCHED;
904                 break;
905         }
906
907         updateLocal(bv, updflag);
908
909         /// If the action has deleted all text in the inset, we need to change the
910         // language to the language of the surronding text.
911         if (!was_empty && paragraphs.begin()->empty() &&
912             paragraphs.size() == 1) {
913                 LyXFont font(LyXFont::ALL_IGNORE);
914                 font.setLanguage(bv->getParentLanguage(this));
915                 setFont(bv, font, false);
916         }
917
918         if (result >= FINISHED)
919                 bv->unlockInset(this);
920
921         if (result == DISPATCHED_NOUPDATE)
922                 result = DISPATCHED;
923         return result;
924 }
925
926
927 int InsetText::latex(Buffer const & buf, ostream & os,
928                      LatexRunParams const & runparams) const
929 {
930         TexRow texrow;
931         latexParagraphs(buf, paragraphs, os, texrow, runparams);
932         return texrow.rows();
933 }
934
935
936 int InsetText::ascii(Buffer const & buf, ostream & os, int linelen) const
937 {
938         unsigned int lines = 0;
939
940         ParagraphList::const_iterator beg = paragraphs.begin();
941         ParagraphList::const_iterator end = paragraphs.end();
942         ParagraphList::const_iterator it = beg;
943         for (; it != end; ++it) {
944                 string const tmp = buf.asciiParagraph(*it, linelen, it == beg);
945                 lines += lyx::count(tmp.begin(), tmp.end(), '\n');
946                 os << tmp;
947         }
948         return lines;
949 }
950
951
952 int InsetText::linuxdoc(Buffer const & buf, ostream & os) const
953 {
954         ParagraphList::iterator pit = const_cast<ParagraphList&>(paragraphs).begin();
955         ParagraphList::iterator pend = const_cast<ParagraphList&>(paragraphs).end();
956
957         // There is a confusion between the empty paragraph and the default paragraph
958         // The default paragraph is <p></p>, the empty paragraph is *empty*
959         // Since none of the floats of linuxdoc accepts standard paragraphs
960         // I disable them. I don't expect problems. (jamatos 2003/07/27)
961         for (; pit != pend; ++pit) {
962                 const string name = pit->layout()->latexname();
963                 if (name != "p")
964                         sgml::openTag(os, 1, 0, name);
965                 buf.simpleLinuxDocOnePar(os, pit, 0);
966                 if (name != "p")
967                         sgml::closeTag(os, 1, 0, name);
968         }
969         return 0;
970 }
971
972
973 int InsetText::docbook(Buffer const & buf, ostream & os, bool mixcont) const
974 {
975         unsigned int lines = 0;
976
977         vector<string> environment_stack(10);
978         vector<string> environment_inner(10);
979
980         int const command_depth = 0;
981         string item_name;
982
983         Paragraph::depth_type depth = 0; // paragraph depth
984
985         ParagraphList::iterator pit = const_cast<ParagraphList&>(paragraphs).begin();
986         ParagraphList::iterator pend = const_cast<ParagraphList&>(paragraphs).end();
987
988         for (; pit != pend; ++pit) {
989                 int desc_on = 0; // description mode
990
991                 LyXLayout_ptr const & style = pit->layout();
992
993                 // environment tag closing
994                 for (; depth > pit->params().depth(); --depth) {
995                         if (environment_inner[depth] != "!-- --") {
996                                 item_name = "listitem";
997                                 lines += sgml::closeTag(os, command_depth + depth, mixcont, item_name);
998                                 if (environment_inner[depth] == "varlistentry")
999                                         lines += sgml::closeTag(os, depth+command_depth, mixcont, environment_inner[depth]);
1000                         }
1001                         lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_stack[depth]);
1002                         environment_stack[depth].erase();
1003                         environment_inner[depth].erase();
1004                 }
1005
1006                 if (depth == pit->params().depth()
1007                    && environment_stack[depth] != style->latexname()
1008                    && !environment_stack[depth].empty()) {
1009                         if (environment_inner[depth] != "!-- --") {
1010                                 item_name= "listitem";
1011                                 lines += sgml::closeTag(os, command_depth+depth, mixcont, item_name);
1012                                 if (environment_inner[depth] == "varlistentry")
1013                                         lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_inner[depth]);
1014                         }
1015
1016                         lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_stack[depth]);
1017
1018                         environment_stack[depth].erase();
1019                         environment_inner[depth].erase();
1020                 }
1021
1022                 // Write opening SGML tags.
1023                 switch (style->latextype) {
1024                 case LATEX_PARAGRAPH:
1025                         lines += sgml::openTag(os, depth + command_depth, mixcont, style->latexname());
1026                         break;
1027
1028                 case LATEX_COMMAND:
1029                         buf.error(ErrorItem(_("Error"), _("LatexType Command not allowed here.\n"), pit->id(), 0, pit->size()));
1030                         return -1;
1031                         break;
1032
1033                 case LATEX_ENVIRONMENT:
1034                 case LATEX_ITEM_ENVIRONMENT:
1035                         if (depth < pit->params().depth()) {
1036                                 depth = pit->params().depth();
1037                                 environment_stack[depth].erase();
1038                         }
1039
1040                         if (environment_stack[depth] != style->latexname()) {
1041                                 if (environment_stack.size() == depth + 1) {
1042                                         environment_stack.push_back("!-- --");
1043                                         environment_inner.push_back("!-- --");
1044                                 }
1045                                 environment_stack[depth] = style->latexname();
1046                                 environment_inner[depth] = "!-- --";
1047                                 lines += sgml::openTag(os, depth + command_depth, mixcont, environment_stack[depth]);
1048                         } else {
1049                                 if (environment_inner[depth] != "!-- --") {
1050                                         item_name= "listitem";
1051                                         lines += sgml::closeTag(os, command_depth + depth, mixcont, item_name);
1052                                         if (environment_inner[depth] == "varlistentry")
1053                                                 lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_inner[depth]);
1054                                 }
1055                         }
1056
1057                         if (style->latextype == LATEX_ENVIRONMENT) {
1058                                 if (!style->latexparam().empty()) {
1059                                         if (style->latexparam() == "CDATA")
1060                                                 os << "<![CDATA[";
1061                                         else
1062                                           lines += sgml::openTag(os, depth + command_depth, mixcont, style->latexparam());
1063                                 }
1064                                 break;
1065                         }
1066
1067                         desc_on = (style->labeltype == LABEL_MANUAL);
1068
1069                         environment_inner[depth] = desc_on ? "varlistentry" : "listitem";
1070                         lines += sgml::openTag(os, depth + 1 + command_depth, mixcont, environment_inner[depth]);
1071
1072                         item_name = desc_on ? "term" : "para";
1073                         lines += sgml::openTag(os, depth + 1 + command_depth, mixcont, item_name);
1074
1075                         break;
1076                 default:
1077                         lines += sgml::openTag(os, depth + command_depth, mixcont, style->latexname());
1078                         break;
1079                 }
1080
1081                 buf.simpleDocBookOnePar(os, pit, desc_on, depth + 1 + command_depth);
1082
1083                 string end_tag;
1084                 // write closing SGML tags
1085                 switch (style->latextype) {
1086                 case LATEX_ENVIRONMENT:
1087                         if (!style->latexparam().empty()) {
1088                                 if (style->latexparam() == "CDATA")
1089                                         os << "]]>";
1090                                 else
1091                                         lines += sgml::closeTag(os, depth + command_depth, mixcont, style->latexparam());
1092                         }
1093                         break;
1094                 case LATEX_ITEM_ENVIRONMENT:
1095                         if (desc_on == 1)
1096                                 break;
1097                         end_tag= "para";
1098                         lines += sgml::closeTag(os, depth + 1 + command_depth, mixcont, end_tag);
1099                         break;
1100                 case LATEX_PARAGRAPH:
1101                         lines += sgml::closeTag(os, depth + command_depth, mixcont, style->latexname());
1102                         break;
1103                 default:
1104                         lines += sgml::closeTag(os, depth + command_depth, mixcont, style->latexname());
1105                         break;
1106                 }
1107         }
1108
1109         // Close open tags
1110         for (int d = depth; d >= 0; --d) {
1111                 if (!environment_stack[depth].empty()) {
1112                         if (environment_inner[depth] != "!-- --") {
1113                                 item_name = "listitem";
1114                                 lines += sgml::closeTag(os, command_depth + depth, mixcont, item_name);
1115                                if (environment_inner[depth] == "varlistentry")
1116                                        lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_inner[depth]);
1117                         }
1118
1119                         lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_stack[depth]);
1120                 }
1121         }
1122
1123         return lines;
1124 }
1125
1126
1127 void InsetText::validate(LaTeXFeatures & features) const
1128 {
1129         for_each(paragraphs.begin(), paragraphs.end(),
1130                  boost::bind(&Paragraph::validate, _1, boost::ref(features)));
1131 }
1132
1133
1134 void InsetText::getCursor(BufferView & bv, int & x, int & y) const
1135 {
1136         if (the_locking_inset) {
1137                 the_locking_inset->getCursor(bv, x, y);
1138                 return;
1139         }
1140         x = cx();
1141         y = cy() + InsetText::y();
1142 }
1143
1144
1145 void InsetText::getCursorPos(BufferView * bv, int & x, int & y) const
1146 {
1147         if (the_locking_inset) {
1148                 the_locking_inset->getCursorPos(bv, x, y);
1149                 return;
1150         }
1151         x = cx() - top_x - TEXT_TO_INSET_OFFSET;
1152         y = cy() - TEXT_TO_INSET_OFFSET;
1153 }
1154
1155
1156 int InsetText::insetInInsetY() const
1157 {
1158         if (!the_locking_inset)
1159                 return 0;
1160
1161         return inset_y + the_locking_inset->insetInInsetY();
1162 }
1163
1164
1165 void InsetText::fitInsetCursor(BufferView * bv) const
1166 {
1167         if (the_locking_inset) {
1168                 the_locking_inset->fitInsetCursor(bv);
1169                 return;
1170         }
1171
1172         LyXFont const font = text_.getFont(cpar(), cpos());
1173
1174         int const asc = font_metrics::maxAscent(font);
1175         int const desc = font_metrics::maxDescent(font);
1176
1177         bv->fitLockedInsetCursor(cx(), cy(), asc, desc);
1178 }
1179
1180
1181 dispatch_result InsetText::moveRight(BufferView * bv)
1182 {
1183         if (text_.cursorPar()->isRightToLeftPar(bv->buffer()->params()))
1184                 return moveLeftIntern(bv, false, true, false);
1185         else
1186                 return moveRightIntern(bv, true, true, false);
1187 }
1188
1189
1190 dispatch_result InsetText::moveLeft(BufferView * bv)
1191 {
1192         if (text_.cursorPar()->isRightToLeftPar(bv->buffer()->params()))
1193                 return moveRightIntern(bv, true, true, false);
1194         else
1195                 return moveLeftIntern(bv, false, true, false);
1196 }
1197
1198
1199 dispatch_result
1200 InsetText::moveRightIntern(BufferView * bv, bool front,
1201                            bool activate_inset, bool selecting)
1202 {
1203         ParagraphList::iterator c_par = cpar();
1204
1205         if (boost::next(c_par) == paragraphs.end() && cpos() >= c_par->size())
1206                 return FINISHED_RIGHT;
1207         if (activate_inset && checkAndActivateInset(bv, front))
1208                 return DISPATCHED;
1209         text_.cursorRight(bv);
1210         if (!selecting)
1211                 text_.clearSelection();
1212         return DISPATCHED_NOUPDATE;
1213 }
1214
1215
1216 dispatch_result
1217 InsetText::moveLeftIntern(BufferView * bv, bool front,
1218                           bool activate_inset, bool selecting)
1219 {
1220         if (cpar() == paragraphs.begin() && cpos() <= 0)
1221                 return FINISHED;
1222         text_.cursorLeft(bv);
1223         if (!selecting)
1224                 text_.clearSelection();
1225         if (activate_inset && checkAndActivateInset(bv, front))
1226                 return DISPATCHED;
1227         return DISPATCHED_NOUPDATE;
1228 }
1229
1230
1231 dispatch_result InsetText::moveUp(BufferView * bv)
1232 {
1233         if (crow() == text_.firstRow())
1234                 return FINISHED_UP;
1235         text_.cursorUp(bv);
1236         text_.clearSelection();
1237         return DISPATCHED_NOUPDATE;
1238 }
1239
1240
1241 dispatch_result InsetText::moveDown(BufferView * bv)
1242 {
1243         if (crow() == text_.lastRow())
1244                 return FINISHED_DOWN;
1245         text_.cursorDown(bv);
1246         text_.clearSelection();
1247         return DISPATCHED_NOUPDATE;
1248 }
1249
1250
1251 bool InsetText::insertInset(BufferView * bv, InsetOld * inset)
1252 {
1253         if (the_locking_inset) {
1254                 if (the_locking_inset->insetAllowed(inset))
1255                         return the_locking_inset->insertInset(bv, inset);
1256                 return false;
1257         }
1258         inset->setOwner(this);
1259         text_.insertInset(inset);
1260         bv->fitCursor();
1261         updateLocal(bv, true);
1262         return true;
1263 }
1264
1265
1266 bool InsetText::insetAllowed(InsetOld::Code code) const
1267 {
1268         // in_insetAllowed is a really gross hack,
1269         // to allow us to call the owner's insetAllowed
1270         // without stack overflow, which can happen
1271         // when the owner uses InsetCollapsable::insetAllowed()
1272         bool ret = true;
1273         if (in_insetAllowed)
1274                 return ret;
1275         in_insetAllowed = true;
1276         if (the_locking_inset)
1277                 ret = the_locking_inset->insetAllowed(code);
1278         else if (owner())
1279                 ret = owner()->insetAllowed(code);
1280         in_insetAllowed = false;
1281         return ret;
1282 }
1283
1284
1285 UpdatableInset * InsetText::getLockingInset() const
1286 {
1287         return the_locking_inset ? the_locking_inset->getLockingInset() :
1288                 const_cast<InsetText *>(this);
1289 }
1290
1291
1292 UpdatableInset * InsetText::getFirstLockingInsetOfType(InsetOld::Code c)
1293 {
1294         if (c == lyxCode())
1295                 return this;
1296         if (the_locking_inset)
1297                 return the_locking_inset->getFirstLockingInsetOfType(c);
1298         return 0;
1299 }
1300
1301
1302 bool InsetText::showInsetDialog(BufferView * bv) const
1303 {
1304         if (the_locking_inset)
1305                 return the_locking_inset->showInsetDialog(bv);
1306         return false;
1307 }
1308
1309
1310 void InsetText::getLabelList(Buffer const & buffer,
1311                              std::vector<string> & list) const
1312 {
1313         ParagraphList::const_iterator pit = paragraphs.begin();
1314         ParagraphList::const_iterator pend = paragraphs.end();
1315         for (; pit != pend; ++pit) {
1316                 InsetList::const_iterator beg = pit->insetlist.begin();
1317                 InsetList::const_iterator end = pit->insetlist.end();
1318                 for (; beg != end; ++beg)
1319                         beg->inset->getLabelList(buffer, list);
1320         }
1321 }
1322
1323
1324 void InsetText::setFont(BufferView * bv, LyXFont const & font, bool toggleall,
1325                         bool selectall)
1326 {
1327         if (the_locking_inset) {
1328                 the_locking_inset->setFont(bv, font, toggleall, selectall);
1329                 return;
1330         }
1331
1332         if ((paragraphs.size() == 1 && paragraphs.begin()->empty())
1333             || cpar()->empty()) {
1334                 text_.setFont(font, toggleall);
1335                 return;
1336         }
1337
1338
1339         if (text_.selection.set())
1340                 text_.recUndo(text_.cursor.par());
1341
1342         if (selectall) {
1343                 text_.cursorTop();
1344                 text_.selection.cursor = text_.cursor;
1345                 text_.cursorBottom();
1346                 text_.setSelection();
1347         }
1348
1349         text_.toggleFree(font, toggleall);
1350
1351         if (selectall)
1352                 text_.clearSelection();
1353
1354         bv->fitCursor();
1355         updateLocal(bv, true);
1356 }
1357
1358
1359 bool InsetText::checkAndActivateInset(BufferView * bv, bool front)
1360 {
1361         if (cpos() == cpar()->size())
1362                 return false;
1363         InsetOld * inset = cpar()->getInset(cpos());
1364         if (!isHighlyEditableInset(inset))
1365                 return false;
1366         FuncRequest cmd(bv, LFUN_INSET_EDIT, front ? "left" : "right");
1367         inset->dispatch(cmd);
1368         if (!the_locking_inset)
1369                 return false;
1370         updateLocal(bv, false);
1371         return true;
1372 }
1373
1374
1375 bool InsetText::checkAndActivateInset(BufferView * bv, int x, int y,
1376                                       mouse_button::state button)
1377 {
1378         int dummyx = x;
1379         int dummyy = y + dim_.asc;
1380         InsetOld * inset = getLyXText(bv)->checkInsetHit(dummyx, dummyy);
1381         // we only do the edit() call if the inset was hit by the mouse
1382         // or if it is a highly editable inset. So we should call this
1383         // function from our own edit with button < 0.
1384         // FIXME: GUII jbl. I've changed this to ::none for now which is probably
1385         // WRONG
1386         if (button == mouse_button::none && !isHighlyEditableInset(inset))
1387                 return false;
1388
1389         if (!inset)
1390                 return false;
1391         if (x < 0)
1392                 x = dim_.wid;
1393         if (y < 0)
1394                 y = dim_.des;
1395         inset_x = cx() - top_x;
1396         inset_y = cy();
1397         FuncRequest cmd(bv, LFUN_INSET_EDIT, x - inset_x, y - inset_y, button);
1398         inset->dispatch(cmd);
1399         if (!the_locking_inset)
1400                 return false;
1401         updateLocal(bv, false);
1402         return true;
1403 }
1404
1405
1406 void InsetText::markNew(bool track_changes)
1407 {
1408         ParagraphList::iterator pit = paragraphs.begin();
1409         ParagraphList::iterator end = paragraphs.end();
1410         for (; pit != end; ++pit) {
1411                 if (track_changes) {
1412                         pit->trackChanges();
1413                 } else {
1414                         // no-op when not tracking
1415                         pit->cleanChanges();
1416                 }
1417         }
1418 }
1419
1420
1421 void InsetText::setText(string const & data, LyXFont const & font)
1422 {
1423         clear(false);
1424         for (unsigned int i = 0; i < data.length(); ++i)
1425                 paragraphs.begin()->insertChar(i, data[i], font);
1426 }
1427
1428
1429 void InsetText::setAutoBreakRows(bool flag)
1430 {
1431         if (flag != autoBreakRows_) {
1432                 autoBreakRows_ = flag;
1433                 if (!flag)
1434                         removeNewlines();
1435         }
1436 }
1437
1438
1439 void InsetText::setDrawFrame(DrawFrame how)
1440 {
1441         drawFrame_ = how;
1442 }
1443
1444
1445 LColor_color InsetText::frameColor() const
1446 {
1447         return LColor::color(frame_color_);
1448 }
1449
1450
1451 void InsetText::setFrameColor(LColor_color col)
1452 {
1453         frame_color_ = col;
1454 }
1455
1456
1457 int InsetText::cx() const
1458 {
1459         int x = text_.cursor.x() + top_x + TEXT_TO_INSET_OFFSET;
1460         if (the_locking_inset) {
1461                 LyXFont font = text_.getFont(text_.cursorPar(), text_.cursor.pos());
1462                 if (font.isVisibleRightToLeft())
1463                         x -= the_locking_inset->width();
1464         }
1465         return x;
1466 }
1467
1468
1469 int InsetText::cy() const
1470 {
1471         return text_.cursor.y() - dim_.asc + TEXT_TO_INSET_OFFSET;
1472 }
1473
1474
1475 pos_type InsetText::cpos() const
1476 {
1477         return text_.cursor.pos();
1478 }
1479
1480
1481 ParagraphList::iterator InsetText::cpar() const
1482 {
1483         return text_.cursorPar();
1484 }
1485
1486
1487 bool InsetText::cboundary() const
1488 {
1489         return text_.cursor.boundary();
1490 }
1491
1492
1493 RowList::iterator InsetText::crow() const
1494 {
1495         return cpar()->getRow(cpos());
1496 }
1497
1498
1499 LyXText * InsetText::getLyXText(BufferView const * bv,
1500                                 bool const recursive) const
1501 {
1502         setViewCache(bv);
1503         if (recursive && the_locking_inset)
1504                 return the_locking_inset->getLyXText(bv, true);
1505         return &text_;
1506 }
1507
1508
1509 void InsetText::setViewCache(BufferView const * bv) const
1510 {
1511         if (bv) {
1512                 if (bv != text_.bv_owner) {
1513                         //lyxerr << "setting view cache from "
1514                         //      << text_.bv_owner << " to " << bv << "\n";
1515                         text_.init(const_cast<BufferView *>(bv));
1516                 }
1517                 text_.bv_owner = const_cast<BufferView *>(bv);
1518         }
1519 }
1520
1521
1522 void InsetText::deleteLyXText(BufferView * bv, bool recursive) const
1523 {
1524         if (recursive) {
1525                 /// then remove all LyXText in text-insets
1526                 for_each(const_cast<ParagraphList&>(paragraphs).begin(),
1527                          const_cast<ParagraphList&>(paragraphs).end(),
1528                          boost::bind(&Paragraph::deleteInsetsLyXText, _1, bv));
1529         }
1530 }
1531
1532
1533 void InsetText::removeNewlines()
1534 {
1535         ParagraphList::iterator it = paragraphs.begin();
1536         ParagraphList::iterator end = paragraphs.end();
1537         for (; it != end; ++it)
1538                 for (int i = 0; i < it->size(); ++i)
1539                         if (it->isNewline(i))
1540                                 it->erase(i);
1541 }
1542
1543
1544 int InsetText::scroll(bool recursive) const
1545 {
1546         int sx = UpdatableInset::scroll(false);
1547
1548         if (recursive && the_locking_inset)
1549                 sx += the_locking_inset->scroll(recursive);
1550
1551         return sx;
1552 }
1553
1554
1555 void InsetText::clearSelection(BufferView *)
1556 {
1557         text_.clearSelection();
1558 }
1559
1560
1561 void InsetText::clearInset(BufferView * bv, int start_x, int baseline) const
1562 {
1563         Painter & pain = bv->painter();
1564         int w = dim_.wid;
1565         int h = dim_.asc + dim_.des;
1566         int ty = baseline - dim_.asc;
1567
1568         if (ty < 0) {
1569                 h += ty;
1570                 ty = 0;
1571         }
1572         if (ty + h > pain.paperHeight())
1573                 h = pain.paperHeight();
1574         if (top_x + w > pain.paperWidth())
1575                 w = pain.paperWidth();
1576         pain.fillRectangle(start_x + 1, ty + 1, w - 3, h - 1, backgroundColor());
1577 }
1578
1579
1580 ParagraphList * InsetText::getParagraphs(int i) const
1581 {
1582         return (i == 0) ? const_cast<ParagraphList*>(&paragraphs) : 0;
1583 }
1584
1585
1586 LyXText * InsetText::getText(int i) const
1587 {
1588         return (i == 0) ? const_cast<LyXText*>(&text_) : 0;
1589 }
1590
1591
1592 LyXCursor const & InsetText::cursor(BufferView * bv) const
1593 {
1594         if (the_locking_inset)
1595                 return the_locking_inset->cursor(bv);
1596         return getLyXText(bv)->cursor;
1597 }
1598
1599
1600 InsetOld * InsetText::getInsetFromID(int id_arg) const
1601 {
1602         if (id_arg == id())
1603                 return const_cast<InsetText *>(this);
1604
1605         ParagraphList::const_iterator pit = paragraphs.begin();
1606         ParagraphList::const_iterator pend = paragraphs.end();
1607         for (; pit != pend; ++pit) {
1608                 InsetList::const_iterator it = pit->insetlist.begin();
1609                 InsetList::const_iterator end = pit->insetlist.end();
1610                 for (; it != end; ++it) {
1611                         if (it->inset->id() == id_arg)
1612                                 return it->inset;
1613                         InsetOld * in = it->inset->getInsetFromID(id_arg);
1614                         if (in)
1615                                 return in;
1616                 }
1617         }
1618         return 0;
1619 }
1620
1621
1622 WordLangTuple const
1623 InsetText::selectNextWordToSpellcheck(BufferView * bv, float & value) const
1624 {
1625         WordLangTuple word;
1626         if (the_locking_inset) {
1627                 word = the_locking_inset->selectNextWordToSpellcheck(bv, value);
1628                 if (!word.word().empty()) {
1629                         value += cy();
1630                         return word;
1631                 }
1632                 // we have to go on checking so move cursor to the next char
1633                 text_.cursor.pos(text_.cursor.pos() + 1);
1634         }
1635         word = text_.selectNextWordToSpellcheck(value);
1636         if (word.word().empty())
1637                 bv->unlockInset(const_cast<InsetText *>(this));
1638         else
1639                 value = cy();
1640         return word;
1641 }
1642
1643
1644 void InsetText::selectSelectedWord(BufferView * bv)
1645 {
1646         if (the_locking_inset) {
1647                 the_locking_inset->selectSelectedWord(bv);
1648                 return;
1649         }
1650         getLyXText(bv)->selectSelectedWord();
1651         updateLocal(bv, false);
1652 }
1653
1654
1655 bool InsetText::nextChange(BufferView * bv, lyx::pos_type & length)
1656 {
1657         if (the_locking_inset) {
1658                 if (the_locking_inset->nextChange(bv, length))
1659                         return true;
1660                 text_.cursorRight(true);
1661         }
1662         lyx::find::SearchResult result =
1663                 lyx::find::findNextChange(bv, &text_, length);
1664
1665         if (result == lyx::find::SR_FOUND) {
1666                 LyXCursor cur = text_.cursor;
1667                 bv->unlockInset(bv->theLockingInset());
1668                 if (bv->lockInset(this))
1669                         locked = true;
1670                 text_.cursor = cur;
1671                 text_.setSelectionRange(length);
1672                 updateLocal(bv, false);
1673         }
1674         return result != lyx::find::SR_NOT_FOUND;
1675 }
1676
1677
1678 bool InsetText::searchForward(BufferView * bv, string const & str,
1679                               bool cs, bool mw)
1680 {
1681         if (the_locking_inset) {
1682                 if (the_locking_inset->searchForward(bv, str, cs, mw))
1683                         return true;
1684                 text_.cursorRight(true);
1685         }
1686         lyx::find::SearchResult result =
1687                 lyx::find::find(bv, &text_, str, true, cs, mw);
1688
1689         if (result == lyx::find::SR_FOUND) {
1690                 LyXCursor cur = text_.cursor;
1691                 bv->unlockInset(bv->theLockingInset());
1692                 if (bv->lockInset(this))
1693                         locked = true;
1694                 text_.cursor = cur;
1695                 text_.setSelectionRange(str.length());
1696                 updateLocal(bv, false);
1697         }
1698         return result != lyx::find::SR_NOT_FOUND;
1699 }
1700
1701
1702 bool InsetText::searchBackward(BufferView * bv, string const & str,
1703                                bool cs, bool mw)
1704 {
1705         if (the_locking_inset) {
1706                 if (the_locking_inset->searchBackward(bv, str, cs, mw))
1707                         return true;
1708         }
1709         if (!locked) {
1710                 text_.setCursor(paragraphs.size() - 1, paragraphs.back().size());
1711         }
1712         lyx::find::SearchResult result =
1713                 lyx::find::find(bv, &text_, str, false, cs, mw);
1714
1715         if (result == lyx::find::SR_FOUND) {
1716                 LyXCursor cur = text_.cursor;
1717                 bv->unlockInset(bv->theLockingInset());
1718                 if (bv->lockInset(this))
1719                         locked = true;
1720                 text_.cursor = cur;
1721                 text_.setSelectionRange(str.length());
1722                 updateLocal(bv, false);
1723         }
1724         return result != lyx::find::SR_NOT_FOUND;
1725 }
1726
1727
1728 bool InsetText::checkInsertChar(LyXFont & font)
1729 {
1730         return owner() ? owner()->checkInsertChar(font) : true;
1731 }
1732
1733
1734 void InsetText::collapseParagraphs(BufferView * bv)
1735 {
1736         while (paragraphs.size() > 1) {
1737                 ParagraphList::iterator const first = paragraphs.begin();
1738                 ParagraphList::iterator second = first;
1739                 advance(second, 1);
1740                 size_t const first_par_size = first->size();
1741
1742                 if (!first->empty() &&
1743                     !second->empty() &&
1744                     !first->isSeparator(first_par_size - 1)) {
1745                         first->insertChar(first_par_size, ' ');
1746                 }
1747
1748 #warning probably broken
1749                 if (text_.selection.set()) {
1750                         if (text_.selection.start.par() == 1) {
1751                                 text_.selection.start.par(1);
1752                                 text_.selection.start.pos(text_.selection.start.pos() + first_par_size);
1753                         }
1754                         if (text_.selection.end.par() == 2) {
1755                                 text_.selection.end.par(1);
1756                                 text_.selection.end.pos(text_.selection.end.pos() + first_par_size);
1757                         }
1758                 }
1759
1760                 mergeParagraph(bv->buffer()->params(), paragraphs, first);
1761         }
1762 }
1763
1764
1765 void InsetText::getDrawFont(LyXFont & font) const
1766 {
1767         if (!owner())
1768                 return;
1769         owner()->getDrawFont(font);
1770 }
1771
1772
1773 void InsetText::appendParagraphs(Buffer * buffer, ParagraphList & plist)
1774 {
1775 #warning FIXME Check if Changes stuff needs changing here. (Lgb)
1776 // And it probably does. You have to take a look at this John. (Lgb)
1777 #warning John, have a look here. (Lgb)
1778         ParagraphList::iterator pit = plist.begin();
1779         ParagraphList::iterator ins = paragraphs.insert(paragraphs.end(), *pit);
1780         ++pit;
1781         mergeParagraph(buffer->params(), paragraphs, boost::prior(ins));
1782
1783         ParagraphList::iterator pend = plist.end();
1784         for (; pit != pend; ++pit)
1785                 paragraphs.push_back(*pit);
1786 }
1787
1788
1789 void InsetText::addPreview(PreviewLoader & loader) const
1790 {
1791         ParagraphList::const_iterator pit = paragraphs.begin();
1792         ParagraphList::const_iterator pend = paragraphs.end();
1793
1794         for (; pit != pend; ++pit) {
1795                 InsetList::const_iterator it  = pit->insetlist.begin();
1796                 InsetList::const_iterator end = pit->insetlist.end();
1797                 for (; it != end; ++it)
1798                         it->inset->addPreview(loader);
1799         }
1800 }