]> git.lyx.org Git - lyx.git/blob - src/insets/insettext.C
Fix export of graphics file with relative path name.
[lyx.git] / src / insets / insettext.C
1 /* This file is part of
2  * ======================================================
3  *
4  *           LyX, The Document Processor
5  *
6  *           Copyright 1998-2001 The LyX Team.
7  *
8  * ======================================================
9  */
10
11 #include <config.h>
12
13 #ifdef __GNUG__
14 #pragma implementation
15 #endif
16
17 #include "insettext.h"
18 #include "paragraph.h"
19 #include "lyxlex.h"
20 #include "debug.h"
21 #include "lyxfont.h"
22 #include "commandtags.h"
23 #include "buffer.h"
24 #include "LyXView.h"
25 #include "BufferView.h"
26 #include "lyxtextclasslist.h"
27 #include "LaTeXFeatures.h"
28 #include "Painter.h"
29 #include "lyxtext.h"
30 #include "lyxcursor.h"
31 #include "CutAndPaste.h"
32 #include "font.h"
33 #include "LColor.h"
34 #include "lyxrow.h"
35 #include "lyxrc.h"
36 #include "intl.h"
37 #include "trans_mgr.h"
38 #include "lyxscreen.h"
39 #include "gettext.h"
40 #include "lyxfunc.h"
41 #include "ParagraphParameters.h"
42 #include "undo_funcs.h"
43 #include "lyxfind.h"
44
45 #include "frontends/Alert.h"
46 #include "frontends/Dialogs.h"
47
48 #include "support/textutils.h"
49 #include "support/LAssert.h"
50 #include "support/lstrings.h"
51 #include "support/lyxalgo.h" // lyx::count
52
53 #include <fstream>
54 #include <algorithm>
55 #include <cstdlib>
56 //#include <csignal>
57
58 using std::ostream;
59 using std::ifstream;
60 using std::endl;
61 using std::min;
62 using std::max;
63 using std::make_pair;
64 using std::vector;
65 using std::pair;
66
67 using lyx::pos_type;
68 using lyx::textclass_type;
69
70 extern unsigned char getCurrentTextClass(Buffer *);
71 extern bool math_insert_greek(BufferView *, char);
72 extern int greek_kb_flag;
73
74
75 // These functions should probably go into bufferview_funcs somehow (Jug)
76
77 void InsetText::saveLyXTextState(LyXText * t) const
78 {
79         // check if my paragraphs are still valid
80         Paragraph * p = par;
81         while (p) {
82                 if (p == t->cursor.par())
83                         break;
84                 p = p->next();
85         }
86
87         if (p && t->cursor.pos() <= p->size()) {
88                 sstate.lpar = t->cursor.par();
89                 sstate.pos = t->cursor.pos();
90                 sstate.boundary = t->cursor.boundary();
91                 sstate.selstartpar = t->selection.start.par();
92                 sstate.selstartpos = t->selection.start.pos();
93                 sstate.selstartboundary = t->selection.start.boundary();
94                 sstate.selendpar = t->selection.end.par();
95                 sstate.selendpos = t->selection.end.pos();
96                 sstate.selendboundary = t->selection.end.boundary();
97                 sstate.selection = t->selection.set();
98                 sstate.mark_set = t->selection.mark();
99                 sstate.refresh = t->refresh_row != 0;
100         } else {
101                 sstate.lpar = 0;
102         }
103 }
104
105 void InsetText::restoreLyXTextState(BufferView * bv, LyXText * t) const
106 {
107         if (sstate.lpar) {
108                 t->selection.set(true);
109                 /* at this point just to avoid the Delete-Empty-Paragraph
110                  * Mechanism when setting the cursor */
111                 t->selection.mark(sstate.mark_set);
112                 if (sstate.selection) {
113                         t->setCursor(bv, sstate.selstartpar, sstate.selstartpos,
114                                      true, sstate.selstartboundary);
115                         t->selection.cursor = t->cursor;
116                         t->setCursor(bv, sstate.selendpar, sstate.selendpos,
117                                      true, sstate.selendboundary);
118                         t->setSelection(bv);
119                         t->setCursor(bv, sstate.lpar, sstate.pos);
120                 } else {
121                         t->setCursor(bv, sstate.lpar, sstate.pos, true, sstate.boundary);
122                         t->selection.cursor = t->cursor;
123                         t->selection.set(false);
124                 }
125                 if (sstate.refresh) {
126                 }
127         }
128 }
129
130
131 InsetText::InnerCache::InnerCache(boost::shared_ptr<LyXText> t)
132 {
133         text = t;
134         remove = false;
135 }
136
137
138 InsetText::InsetText(BufferParams const & bp)
139         : UpdatableInset(), lt(0), in_update(false), do_resize(0),
140           do_reinit(false)
141 {
142         par = new Paragraph;
143         par->layout(textclasslist[bp.textclass].defaultLayoutName());
144
145         init();
146 }
147
148
149 InsetText::InsetText(InsetText const & in, bool same_id)
150         : UpdatableInset(in, same_id), lt(0), in_update(false), do_resize(0),
151           do_reinit(false)
152 {
153         par = 0;
154         init(&in, same_id);
155 }
156
157
158 InsetText & InsetText::operator=(InsetText const & it)
159 {
160         init(&it);
161         return * this;
162 }
163
164
165 void InsetText::init(InsetText const * ins, bool same_id)
166 {
167         if (ins) {
168                 setParagraphData(ins->par, same_id);
169                 autoBreakRows = ins->autoBreakRows;
170                 drawFrame_ = ins->drawFrame_;
171                 frame_color = ins->frame_color;
172                 if (same_id)
173                         id_ = ins->id_;
174         } else {
175                 Paragraph * p = par;
176                 while (p) {
177                         p->setInsetOwner(this);
178                         p = p->next();
179                 }
180                 the_locking_inset = 0;
181                 drawFrame_ = NEVER;
182                 frame_color = LColor::insetframe;
183                 autoBreakRows = false;
184         }
185         top_y = 0;
186         insetAscent = 0;
187         insetDescent = 0;
188         insetWidth = 0;
189         old_max_width = 0;
190         no_selection = false;
191         need_update = FULL;
192         drawTextXOffset = 0;
193         drawTextYOffset = 0;
194         xpos = 0.0;
195         locked = false;
196         old_par = 0;
197         last_drawn_width = -1;
198         frame_is_visible = false;
199         cached_bview = 0;
200         sstate.lpar = 0;
201         in_insetAllowed = false;
202 }
203
204
205 InsetText::~InsetText()
206 {
207         cached_bview = 0;
208
209         // NOTE
210
211         while (par) {
212                 Paragraph * tmp = par->next();
213                 delete par;
214                 par = tmp;
215         }
216 }
217
218
219 void InsetText::clear()
220 {
221         // This is a gross hack...
222         string old_layout = par->layout();
223
224         while (par) {
225                 Paragraph * tmp = par->next();
226                 delete par;
227                 par = tmp;
228         }
229         par = new Paragraph;
230         par->setInsetOwner(this);
231         par->layout(old_layout);
232
233         reinitLyXText();
234         need_update = INIT;
235 }
236
237
238 Inset * InsetText::clone(Buffer const &, bool same_id) const
239 {
240         return new InsetText(*this, same_id);
241 }
242
243
244 void InsetText::write(Buffer const * buf, ostream & os) const
245 {
246         os << "Text\n";
247         writeParagraphData(buf, os);
248 }
249
250
251 void InsetText::writeParagraphData(Buffer const * buf, ostream & os) const
252 {
253         par->writeFile(buf, os, buf->params, 0);
254 }
255
256
257 void InsetText::read(Buffer const * buf, LyXLex & lex)
258 {
259         string token;
260         int pos = 0;
261         Paragraph * return_par = 0;
262         Paragraph::depth_type depth = 0;
263         LyXFont font(LyXFont::ALL_INHERIT);
264
265         clear();
266
267         while (lex.isOK()) {
268                 lex.nextToken();
269                 token = lex.getString();
270                 if (token.empty())
271                         continue;
272                 if (token == "\\end_inset") {
273 #ifndef NO_COMPABILITY
274                         const_cast<Buffer*>(buf)->insertErtContents(par, pos, false);
275 #endif
276                         break;
277                 }
278
279                 if (const_cast<Buffer*>(buf)->
280                         parseSingleLyXformat2Token(lex, par, return_par,
281                                                    token, pos, depth, font)) {
282                         // the_end read this should NEVER happen
283                         lex.printError("\\the_end read in inset! Error in document!");
284                         return;
285                 }
286         }
287         if (!return_par)
288                 return_par = par;
289         par = return_par;
290         while (return_par) {
291                 return_par->setInsetOwner(this);
292                 return_par = return_par->next();
293         }
294
295         if (token != "\\end_inset") {
296                 lex.printError("Missing \\end_inset at this point. "
297                                            "Read: `$$Token'");
298         }
299         need_update = FULL;
300 }
301
302
303 int InsetText::ascent(BufferView * bv, LyXFont const &) const
304 {
305         insetAscent = getLyXText(bv)->firstRow()->ascent_of_text() +
306                 TEXT_TO_INSET_OFFSET;
307         return insetAscent;
308 }
309
310
311 int InsetText::descent(BufferView * bv, LyXFont const &) const
312 {
313         LyXText * llt = getLyXText(bv);
314         insetDescent = llt->height - llt->firstRow()->ascent_of_text() +
315                 TEXT_TO_INSET_OFFSET;
316         return insetDescent;
317 }
318
319
320 int InsetText::width(BufferView * bv, LyXFont const &) const
321 {
322         insetWidth = max(textWidth(bv), (int)getLyXText(bv)->width) +
323                 (2 * TEXT_TO_INSET_OFFSET);
324         insetWidth = max(insetWidth, 10);
325         return insetWidth;
326 }
327
328
329 int InsetText::textWidth(BufferView * bv, bool fordraw) const
330 {
331         int w;
332         if (!autoBreakRows) {
333                 w = -1;
334         } else {
335                 w = getMaxWidth(bv, this);
336         }
337         if (fordraw) {
338                 return max(w - (2 * TEXT_TO_INSET_OFFSET),
339                            (int)getLyXText(bv)->width);
340         } else if (w < 0) {
341             return -1;
342         }
343         return w - (2 * TEXT_TO_INSET_OFFSET);
344 }
345
346
347 void InsetText::draw(BufferView * bv, LyXFont const & f,
348                      int baseline, float & x, bool cleared) const
349 {
350         if (nodraw())
351                 return;
352
353         Painter & pain = bv->painter();
354
355         // this is the first thing we have to ask because if the x pos
356         // changed we have to do a complete rebreak of the text as we
357         // may have few space to draw in. Well we should check on this too
358         int old_x = top_x;
359         if (top_x != int(x)) {
360                 top_x = int(x);
361                 topx_set = true;
362                 int nw = getMaxWidth(bv, this);
363                 if (nw > 0 && old_max_width != nw) {
364                         need_update = INIT;
365                         old_max_width = nw;
366                         bv->text->status(bv, LyXText::CHANGED_IN_DRAW);
367                         return;
368                 }
369         }
370
371         // call these methods so that insetWidth, insetAscent and
372         // insetDescent have the right values.
373         width(bv, f);
374         ascent(bv, f);
375         descent(bv, f);
376
377         // repaint the background if needed
378         if (cleared && backgroundColor() != LColor::background) {
379                 clearInset(bv, baseline, cleared);
380         }
381
382         // no draw is necessary !!!
383         if ((drawFrame_ == LOCKED) && !locked && !par->size()) {
384                 top_baseline = baseline;
385                 x += width(bv, f);
386                 if (need_update & CLEAR_FRAME)
387                         clearFrame(pain, cleared);
388                 need_update = NONE;
389                 return;
390         }
391
392         xpos = x;
393         if (!owner())
394                 x += static_cast<float>(scroll());
395
396         // if top_x differs we did it already
397         if (!cleared && (old_x == int(x))
398             && ((need_update&(INIT|FULL)) || (top_baseline != baseline)
399                 ||(last_drawn_width != insetWidth)))
400         {
401                 // Condition necessary to eliminate bug 59 attachment 37
402                 if (baseline > 0)
403                         clearInset(bv, baseline, cleared);
404         }
405
406         if (cleared)
407                 frame_is_visible = false;
408
409         if (!cleared && (need_update == NONE)) {
410                 if (locked)
411                         drawFrame(pain, cleared);
412                 return;
413         }
414
415         top_baseline = baseline;
416         top_y = baseline - insetAscent;
417
418         if (last_drawn_width != insetWidth) {
419                 if (!cleared)
420                         clearInset(bv, baseline, cleared);
421                 need_update |= FULL;
422                 last_drawn_width = insetWidth;
423         }
424
425         if (the_locking_inset && (cpar(bv) == inset_par)
426                 && (cpos(bv) == inset_pos)) {
427                 inset_x = cx(bv) - top_x + drawTextXOffset;
428                 inset_y = cy(bv) + drawTextYOffset;
429         }
430         if (!cleared && (need_update == CURSOR)
431             && !getLyXText(bv)->selection.set()) {
432                 drawFrame(pain, cleared);
433                 x += insetWidth;
434                 need_update = NONE;
435                 return;
436         }
437         bool clear = false;
438         if (!lt) {
439                 lt = getLyXText(bv);
440                 clear = true;
441         }
442         x += TEXT_TO_INSET_OFFSET;
443
444         Row * row = lt->firstRow();
445         int y_offset = baseline - row->ascent_of_text();
446         int ph = pain.paperHeight();
447         int first = 0;
448         int y = y_offset;
449         while ((row != 0) && ((y+row->height()) <= 0)) {
450                 y += row->height();
451                 first += row->height();
452                 row = row->next();
453         }
454         if (y_offset < 0)
455                 y_offset = y;
456         lt->first_y = first;
457         if (cleared || (need_update&(INIT|FULL))) {
458                 int yf = y_offset;
459                 y = 0;
460                 while ((row != 0) && (yf < ph)) {
461                         lt->getVisibleRow(bv, y+y_offset, int(x), row,
462                                                 y+first, cleared);
463                         y += row->height();
464                         yf += row->height();
465                         row = row->next();
466                 }
467         } else if (!locked) {
468                 if (need_update & CURSOR) {
469                         bv->screen()->toggleSelection(lt, bv, true, y_offset,int(x));
470                         lt->clearSelection();
471                         lt->selection.cursor = lt->cursor;
472                 }
473                 bv->screen()->update(lt, bv, y_offset, int(x));
474         } else {
475                 locked = false;
476                 if (need_update & SELECTION) {
477                         bv->screen()->toggleToggle(lt, bv, y_offset, int(x));
478                 } else if (need_update & CURSOR) {
479                         bv->screen()->toggleSelection(lt, bv, true, y_offset,int(x));
480                         lt->clearSelection();
481                         lt->selection.cursor = lt->cursor;
482                 }
483                 bv->screen()->update(lt, bv, y_offset, int(x));
484                 locked = true;
485         }
486
487         lt->refresh_y = 0;
488         lt->status(bv, LyXText::UNCHANGED);
489         if ((drawFrame_ == ALWAYS) ||
490                 ((cleared || (need_update != CURSOR_PAR)) &&
491                  (drawFrame_ == LOCKED) && locked))
492         {
493                 drawFrame(pain, cleared);
494         } else if (need_update & CLEAR_FRAME) {
495                 clearFrame(pain, cleared);
496         }
497
498         x += insetWidth - TEXT_TO_INSET_OFFSET;
499
500         if (bv->text->status() == LyXText::CHANGED_IN_DRAW) {
501                 need_update |= FULL;
502         } else if (need_update != INIT) {
503                 need_update = NONE;
504         }
505         if (clear)
506                 lt = 0;
507 }
508
509
510 void InsetText::drawFrame(Painter & pain, bool cleared) const
511 {
512         static int const ttoD2 = TEXT_TO_INSET_OFFSET / 2;
513         if (!frame_is_visible || cleared) {
514                 frame_x = top_x + ttoD2;
515                 frame_y = top_baseline - insetAscent + ttoD2;
516                 frame_w = insetWidth - TEXT_TO_INSET_OFFSET;
517                 frame_h = insetAscent + insetDescent - TEXT_TO_INSET_OFFSET;
518                 pain.rectangle(frame_x, frame_y, frame_w, frame_h,
519                                frame_color);
520                 frame_is_visible = true;
521         }
522 }
523
524
525 void InsetText::clearFrame(Painter & pain, bool cleared) const
526 {
527         if (frame_is_visible) {
528                 if (!cleared) {
529                         pain.rectangle(frame_x, frame_y, frame_w, frame_h,
530                                        backgroundColor());
531                 }
532                 frame_is_visible = false;
533         }
534 }
535
536
537 void InsetText::update(BufferView * bv, LyXFont const & font, bool reinit)
538 {
539         if (in_update) {
540                 if (reinit && owner()) {
541                         reinitLyXText();
542                         owner()->update(bv, font, true);
543                 }
544                 return;
545         }
546         in_update = true;
547         if (reinit || need_update == INIT) {
548                 need_update = FULL;
549                 // we should put this call where we set need_update to INIT!
550                 reinitLyXText();
551                 if (owner())
552                         owner()->update(bv, font, true);
553                 in_update = false;
554                 return;
555         }
556         if (the_locking_inset) {
557                 inset_x = cx(bv) - top_x + drawTextXOffset;
558                 inset_y = cy(bv) + drawTextYOffset;
559                 the_locking_inset->update(bv, font, reinit);
560         }
561
562         bool clear = false;
563         if (!lt) {
564                 lt = getLyXText(bv);
565                 clear = true;
566         }
567         if ((need_update & CURSOR_PAR) && (lt->status() == LyXText::UNCHANGED) &&
568                 the_locking_inset)
569         {
570                 lt->updateInset(bv, the_locking_inset);
571         }
572         if (lt->status() == LyXText::NEED_MORE_REFRESH)
573                 need_update |= FULL;
574         if (clear)
575                 lt = 0;
576         in_update = false;
577 }
578
579
580 void InsetText::setUpdateStatus(BufferView * bv, int what) const
581 {
582         // this does nothing dangerous so use only a localized buffer
583         LyXText * llt = getLyXText(bv);
584
585         need_update |= what;
586         // we have to redraw us full if our LyXText NEEDS_MORE_REFRES or
587         // if we don't break row so that we only have one row to update!
588         if ((llt->status() == LyXText::NEED_MORE_REFRESH) ||
589             (!autoBreakRows &&
590              (llt->status() == LyXText::NEED_VERY_LITTLE_REFRESH)))
591         {
592                 need_update |= FULL;
593         } else if (llt->status() == LyXText::NEED_VERY_LITTLE_REFRESH) {
594                 need_update |= CURSOR_PAR;
595         }
596
597         // this to not draw a selection when we redraw all of it!
598         if (need_update & CURSOR && !(need_update & SELECTION)) {
599                 if (llt->selection.set())
600                         need_update = FULL;
601                 llt->clearSelection();
602         }
603 }
604
605
606 void InsetText::updateLocal(BufferView * bv, int what, bool mark_dirty) const
607 {
608         if (!autoBreakRows && par->next())
609                 collapseParagraphs(bv->buffer()->params);
610         bool clear = false;
611         if (!lt) {
612                 lt = getLyXText(bv);
613                 clear = true;
614         }
615         lt->fullRebreak(bv);
616         setUpdateStatus(bv, what);
617         bool flag = (((need_update != CURSOR) && (need_update != NONE)) ||
618                      (lt->status() != LyXText::UNCHANGED) || lt->selection.set());
619         if (!lt->selection.set())
620                 lt->selection.cursor = lt->cursor;
621         if (clear)
622                 lt = 0;
623         if (flag)
624                 bv->updateInset(const_cast<InsetText *>(this), mark_dirty);
625         else
626                 bv->fitCursor();
627
628         if (need_update == CURSOR)
629                 need_update = NONE;
630         bv->owner()->showState();
631         bv->owner()->updateMenubar();
632         bv->owner()->updateToolbar();
633         if (old_par != cpar(bv)) {
634                 bv->owner()->setLayout(cpar(bv)->layout());
635                 old_par = cpar(bv);
636         }
637 }
638
639
640 string const InsetText::editMessage() const
641 {
642         return _("Opened Text Inset");
643 }
644
645
646 void InsetText::edit(BufferView * bv, int x, int y, unsigned int button)
647 {
648         UpdatableInset::edit(bv, x, y, button);
649
650         if (!bv->lockInset(this)) {
651                 lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
652                 return;
653         }
654         locked = true;
655         the_locking_inset = 0;
656         inset_pos = inset_x = inset_y = 0;
657         inset_boundary = false;
658         inset_par = 0;
659         old_par = 0;
660         int tmp_y = (y < 0) ? 0 : y;
661         bool clear = false;
662         if (!lt) {
663                 lt = getLyXText(bv);
664                 clear = true;
665         }
666         if (!checkAndActivateInset(bv, x, tmp_y, button))
667                 lt->setCursorFromCoordinates(bv, x - drawTextXOffset,
668                                             y + insetAscent);
669         lt->clearSelection();
670         finishUndo();
671         // If the inset is empty set the language of the current font to the
672         // language to the surronding text (if different).
673         if (par->size() == 0 && !par->next() &&
674                 bv->getParentLanguage(this) != lt->current_font.language())
675         {
676                 LyXFont font(LyXFont::ALL_IGNORE);
677                 font.setLanguage(bv->getParentLanguage(this));
678                 setFont(bv, font, false);
679         }
680 //      showInsetCursor(bv);
681         if (clear)
682                 lt = 0;
683
684         int code = CURSOR;
685         if (drawFrame_ == LOCKED)
686                 code = CURSOR|DRAW_FRAME;
687         updateLocal(bv, code, false);
688
689         // Tell the paragraph dialog that we've entered an insettext.
690         bv->owner()->getDialogs()->updateParagraph();
691 }
692
693
694 void InsetText::edit(BufferView * bv, bool front)
695 {
696         UpdatableInset::edit(bv, front);
697
698         if (!bv->lockInset(this)) {
699                 lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
700                 return;
701         }
702         locked = true;
703         the_locking_inset = 0;
704         inset_pos = inset_x = inset_y = 0;
705         inset_boundary = false;
706         inset_par = 0;
707         old_par = 0;
708         bool clear = false;
709         if (!lt) {
710                 lt = getLyXText(bv);
711                 clear = true;
712         }
713         if (front)
714                 lt->setCursor(bv, par, 0);
715         else {
716                 Paragraph * p = par;
717                 while (p->next())
718                         p = p->next();
719 //              int const pos = (p->size() ? p->size()-1 : p->size());
720                 lt->setCursor(bv, p, p->size());
721         }
722         lt->clearSelection();
723         finishUndo();
724         // If the inset is empty set the language of the current font to the
725         // language to the surronding text (if different).
726         if (par->size() == 0 && !par->next() &&
727                 bv->getParentLanguage(this) != lt->current_font.language()) {
728                 LyXFont font(LyXFont::ALL_IGNORE);
729                 font.setLanguage(bv->getParentLanguage(this));
730                 setFont(bv, font, false);
731         }
732 //      showInsetCursor(bv);
733         if (clear)
734                 lt = 0;
735         int code = CURSOR;
736         if (drawFrame_ == LOCKED)
737                 code = CURSOR|DRAW_FRAME;
738         updateLocal(bv, code, false);
739 }
740
741
742 void InsetText::insetUnlock(BufferView * bv)
743 {
744         if (the_locking_inset) {
745                 the_locking_inset->insetUnlock(bv);
746                 the_locking_inset = 0;
747         }
748         hideInsetCursor(bv);
749         no_selection = false;
750         locked = false;
751         int code;
752         if (drawFrame_ == LOCKED)
753                 code = CURSOR|CLEAR_FRAME;
754         else
755                 code = CURSOR;
756         bool clear = false;
757         if (!lt) {
758                 lt = getLyXText(bv);
759                 clear = true;
760         }
761         if (lt->selection.set()) {
762                 lt->clearSelection();
763                 code = FULL;
764         } else if (owner()) {
765                 bv->owner()->setLayout(owner()->getLyXText(bv)
766                                        ->cursor.par()->layout());
767         } else
768                 bv->owner()->setLayout(bv->text->cursor.par()->layout());
769         // hack for deleteEmptyParMech
770         if (par->size()) {
771                 lt->setCursor(bv, par, 0);
772         } else if (par->next()) {
773                 lt->setCursor(bv, par->next(), 0);
774         }
775         if (clear)
776                 lt = 0;
777         updateLocal(bv, code, false);
778 }
779
780 void InsetText::lockInset(BufferView * bv, UpdatableInset * inset)
781 {
782         the_locking_inset = inset;
783         inset_x = cx(bv) - top_x + drawTextXOffset;
784         inset_y = cy(bv) + drawTextYOffset;
785         inset_pos = cpos(bv);
786         inset_par = cpar(bv);
787         inset_boundary = cboundary(bv);
788         updateLocal(bv, CURSOR, false);
789 }
790
791
792 bool InsetText::lockInsetInInset(BufferView * bv, UpdatableInset * inset)
793 {
794         lyxerr[Debug::INSETS] << "InsetText::LockInsetInInset("
795                               << inset << "): ";
796         if (!inset)
797                 return false;
798         if (!the_locking_inset) {
799                 Paragraph * p = par;
800                 int const id = inset->id();
801                 while(p) {
802                         Paragraph::inset_iterator it =
803                                 p->inset_iterator_begin();
804                         Paragraph::inset_iterator const end =
805                                 p->inset_iterator_end();
806                         for (; it != end; ++it) {
807                                 if ((*it) == inset) {
808                                         getLyXText(bv)->setCursorIntern(bv, p, it.getPos());
809                                         lockInset(bv, inset);
810                                         return true;
811                                 }
812                                 if ((*it)->getInsetFromID(id)) {
813                                         getLyXText(bv)->setCursorIntern(bv, p, it.getPos());
814                                         (*it)->edit(bv);
815                                         return the_locking_inset->lockInsetInInset(bv, inset);
816                                 }
817                         }
818                         p = p->next();
819                 }
820                 return false;
821         }
822         if (inset == cpar(bv)->getInset(cpos(bv))) {
823                 lyxerr[Debug::INSETS] << "OK" << endl;
824                 lockInset(bv, inset);
825                 return true;
826         } else if (the_locking_inset && (the_locking_inset == inset)) {
827                 if (cpar(bv) == inset_par && cpos(bv) == inset_pos) {
828                         lyxerr[Debug::INSETS] << "OK" << endl;
829                         inset_x = cx(bv) - top_x + drawTextXOffset;
830                         inset_y = cy(bv) + drawTextYOffset;
831                 } else {
832                         lyxerr[Debug::INSETS] << "cursor.pos != inset_pos" << endl;
833                 }
834         } else if (the_locking_inset) {
835                 lyxerr[Debug::INSETS] << "MAYBE" << endl;
836                 return the_locking_inset->lockInsetInInset(bv, inset);
837         }
838         lyxerr[Debug::INSETS] << "NOT OK" << endl;
839         return false;
840 }
841
842
843 bool InsetText::unlockInsetInInset(BufferView * bv, UpdatableInset * inset,
844                                    bool lr)
845 {
846         if (!the_locking_inset)
847                 return false;
848         if (the_locking_inset == inset) {
849                 the_locking_inset->insetUnlock(bv);
850                 getLyXText(bv)->updateInset(bv, inset);
851                 the_locking_inset = 0;
852                 if (lr)
853                         moveRight(bv, false);
854                 old_par = 0; // force layout setting
855                 if (scroll())
856                         scroll(bv, 0.0F);
857                 else
858                         updateLocal(bv, CURSOR, false);
859                 return true;
860         }
861         return the_locking_inset->unlockInsetInInset(bv, inset, lr);
862 }
863
864
865 bool InsetText::updateInsetInInset(BufferView * bv, Inset * inset)
866 {
867         if (!autoBreakRows && par->next())
868                 collapseParagraphs(bv->buffer()->params);
869         if (inset == this)
870                 return true;
871         bool clear = false;
872         if (!lt) {
873                 lt = getLyXText(bv);
874                 clear = true;
875         }
876         if (inset->owner() != this) {
877                 int ustat = CURSOR_PAR;
878                 bool found = false;
879                 UpdatableInset * tl_inset = the_locking_inset;
880                 if (tl_inset)
881                         found = tl_inset->updateInsetInInset(bv, inset);
882                 if (!found) {
883                         tl_inset = static_cast<UpdatableInset *>(inset);
884                         while(tl_inset->owner() && tl_inset->owner() != this)
885                                 tl_inset = static_cast<UpdatableInset *>(tl_inset->owner());
886                         if (!tl_inset->owner())
887                                 return false;
888                         found = tl_inset->updateInsetInInset(bv, inset);
889                         ustat = FULL;
890                 }
891                 if (found)
892                         lt->updateInset(bv, tl_inset);
893                 if (clear)
894                         lt = 0;
895                 if (found)
896                         setUpdateStatus(bv, ustat);
897                 return found;
898         }
899         bool found = lt->updateInset(bv, inset);
900         if (clear)
901                 lt = 0;
902         if (found) {
903                 setUpdateStatus(bv, CURSOR_PAR);
904                 if (the_locking_inset &&
905                     cpar(bv) == inset_par && cpos(bv) == inset_pos)
906                 {
907                         inset_x = cx(bv) - top_x + drawTextXOffset;
908                         inset_y = cy(bv) + drawTextYOffset;
909                 }
910         }
911         return found;
912 }
913
914
915 void InsetText::insetButtonPress(BufferView * bv, int x, int y, int button)
916 {
917         no_selection = true;
918
919         // use this to check mouse motion for selection!
920         mouse_x = x;
921         mouse_y = y;
922
923         int tmp_x = x - drawTextXOffset;
924         int tmp_y = y + insetAscent - getLyXText(bv)->first_y;
925         Inset * inset = bv->checkInsetHit(getLyXText(bv), tmp_x, tmp_y);
926
927         hideInsetCursor(bv);
928         if (the_locking_inset) {
929                 if (the_locking_inset == inset) {
930                         the_locking_inset->insetButtonPress(bv,
931                                                             x - inset_x,
932                                                             y - inset_y,
933                                                             button);
934                         no_selection = false;
935                         return;
936                 } else if (inset) {
937                         // otherwise unlock the_locking_inset and lock the new inset
938                         the_locking_inset->insetUnlock(bv);
939                         inset_x = cx(bv) - top_x + drawTextXOffset;
940                         inset_y = cy(bv) + drawTextYOffset;
941                         the_locking_inset = 0;
942                         inset->insetButtonPress(bv, x - inset_x,
943                                                 y - inset_y, button);
944 //                      inset->edit(bv, x - inset_x, y - inset_y, button);
945                         if (the_locking_inset)
946                                 updateLocal(bv, CURSOR, false);
947                         no_selection = false;
948                         return;
949                 }
950                 // otherwise only unlock the_locking_inset
951                 the_locking_inset->insetUnlock(bv);
952                 the_locking_inset = 0;
953         }
954         if (bv->theLockingInset()) {
955                 if (isHighlyEditableInset(inset)) {
956                         UpdatableInset * uinset = static_cast<UpdatableInset*>(inset);
957                         inset_x = cx(bv) - top_x + drawTextXOffset;
958                         inset_y = cy(bv) + drawTextYOffset;
959                         inset_pos = cpos(bv);
960                         inset_par = cpar(bv);
961                         inset_boundary = cboundary(bv);
962                         the_locking_inset = uinset;
963                         uinset->insetButtonPress(bv, x - inset_x, y - inset_y,
964                                                  button);
965                         uinset->edit(bv, x - inset_x, y - inset_y, 0);
966                         if (the_locking_inset)
967                                 updateLocal(bv, CURSOR, false);
968                         no_selection = false;
969                         return;
970                 }
971         }
972         if (!inset) { // && (button == 2)) {
973                 bool paste_internally = false;
974                 if ((button == 2) && getLyXText(bv)->selection.set()) {
975                         localDispatch(bv, LFUN_COPY, "");
976                         paste_internally = true;
977                 }
978                 bool clear = false;
979                 if (!lt) {
980                         lt = getLyXText(bv);
981                         clear = true;
982                 }
983
984                 lt->setCursorFromCoordinates(bv, x - drawTextXOffset,
985                                              y + insetAscent);
986                 // set the selection cursor!
987                 lt->selection.cursor = lt->cursor;
988                 lt->cursor.x_fix(lt->cursor.x());
989
990                 if (lt->selection.set()) {
991                         lt->clearSelection();
992                         if (clear)
993                                 lt = 0;
994                         updateLocal(bv, FULL, false);
995                 } else {
996                         lt->clearSelection();
997                         if (clear)
998                                 lt = 0;
999                         updateLocal(bv, CURSOR, false);
1000                 }
1001                 bv->owner()->setLayout(cpar(bv)->layout());
1002                 old_par = cpar(bv);
1003                 // Insert primary selection with middle mouse
1004                 // if there is a local selection in the current buffer,
1005                 // insert this
1006                 if (button == 2) {
1007                         if (paste_internally)
1008                                 localDispatch(bv, LFUN_PASTE, "");
1009                         else
1010                                 localDispatch(bv, LFUN_PASTESELECTION,
1011                                               "paragraph");
1012                 }
1013         } else {
1014                 getLyXText(bv)->clearSelection();
1015         }
1016         showInsetCursor(bv);
1017         no_selection = false;
1018 }
1019
1020
1021 bool InsetText::insetButtonRelease(BufferView * bv, int x, int y, int button)
1022 {
1023         if (the_locking_inset) {
1024                 return the_locking_inset->insetButtonRelease(bv,
1025                                                              x - inset_x, y - inset_y,
1026                                                              button);
1027         }
1028         int tmp_x = x - drawTextXOffset;
1029         int tmp_y = y + insetAscent - getLyXText(bv)->first_y;
1030         Inset * inset = bv->checkInsetHit(getLyXText(bv), tmp_x, tmp_y);
1031         bool ret = false;
1032         if (inset) {
1033                 if (isHighlyEditableInset(inset)) {
1034                         ret = inset->insetButtonRelease(bv, x - inset_x,
1035                                                         y - inset_y, button);
1036                 } else {
1037                         inset_x = cx(bv) - top_x + drawTextXOffset;
1038                         inset_y = cy(bv) + drawTextYOffset;
1039                         ret = inset->insetButtonRelease(bv, x - inset_x,
1040                                                         y - inset_y, button);
1041                         inset->edit(bv, x - inset_x,
1042                                     y - inset_y, button);
1043                 }
1044                 updateLocal(bv, CURSOR_PAR, false);
1045         }
1046         return ret;
1047 }
1048
1049
1050 void InsetText::insetMotionNotify(BufferView * bv, int x, int y, int state)
1051 {
1052         if (no_selection || ((mouse_x == x) && (mouse_y == y)))
1053                 return;
1054         if (the_locking_inset) {
1055                 the_locking_inset->insetMotionNotify(bv, x - inset_x,
1056                                                      y - inset_y,state);
1057                 return;
1058         }
1059         bool clear = false;
1060         if (!lt) {
1061                 lt = getLyXText(bv);
1062                 clear = true;
1063         }
1064         hideInsetCursor(bv);
1065         LyXCursor cur = lt->cursor;
1066         lt->setCursorFromCoordinates(bv, x - drawTextXOffset, y + insetAscent);
1067         if (cur == lt->cursor) {
1068                 if (clear)
1069                         lt = 0;
1070                 return;
1071         }
1072         lt->setSelection(bv);
1073         bool flag = (lt->toggle_cursor.par() != lt->toggle_end_cursor.par() ||
1074                                  lt->toggle_cursor.pos() != lt->toggle_end_cursor.pos());
1075         if (clear)
1076                 lt = 0;
1077         if (flag) {
1078                 updateLocal(bv, SELECTION, false);
1079         }
1080         showInsetCursor(bv);
1081 }
1082
1083
1084 void InsetText::insetKeyPress(XKeyEvent * xke)
1085 {
1086         if (the_locking_inset) {
1087                 the_locking_inset->insetKeyPress(xke);
1088                 return;
1089         }
1090 }
1091
1092
1093 UpdatableInset::RESULT
1094 InsetText::localDispatch(BufferView * bv,
1095                          kb_action action, string const & arg)
1096 {
1097         bool was_empty = par->size() == 0 && !par->next();
1098         no_selection = false;
1099         UpdatableInset::RESULT
1100                 result= UpdatableInset::localDispatch(bv, action, arg);
1101         if (result != UNDISPATCHED) {
1102                 return DISPATCHED;
1103         }
1104
1105         result = DISPATCHED;
1106         if ((action < 0) && arg.empty())
1107                 return FINISHED;
1108
1109         if (the_locking_inset) {
1110                 result = the_locking_inset->localDispatch(bv, action, arg);
1111                 if (result == DISPATCHED_NOUPDATE)
1112                         return result;
1113                 else if (result == DISPATCHED) {
1114                         updateLocal(bv, CURSOR_PAR, false);
1115                         return result;
1116                 } else if (result >= FINISHED) {
1117                         switch (result) {
1118                         case FINISHED_RIGHT:
1119                                 moveRightIntern(bv, false, false);
1120                                 result = DISPATCHED;
1121                                 break;
1122                         case FINISHED_UP:
1123                                 if ((result = moveUp(bv)) >= FINISHED) {
1124                                         updateLocal(bv, CURSOR, false);
1125                                         bv->unlockInset(this);
1126                                 }
1127                                 break;
1128                         case FINISHED_DOWN:
1129                                 if ((result = moveDown(bv)) >= FINISHED) {
1130                                         updateLocal(bv, CURSOR, false);
1131                                         bv->unlockInset(this);
1132                                 }
1133                                 break;
1134                         default:
1135                                 result = DISPATCHED;
1136                                 break;
1137                         }
1138                         the_locking_inset = 0;
1139 #ifdef WITH_WARNINGS
1140 #warning I changed this to always return Dispatched maybe it is wrong (20011001 Jug)
1141 #endif
1142                         return result;
1143                 }
1144         }
1145         hideInsetCursor(bv);
1146         bool clear = false;
1147         if (!lt) {
1148                 lt = getLyXText(bv);
1149                 clear = true;
1150         }
1151         int updwhat = 0;
1152         int updflag = false;
1153         switch (action) {
1154         // Normal chars
1155         case LFUN_SELFINSERT:
1156                 if (bv->buffer()->isReadonly()) {
1157 //          setErrorMessage(N_("Document is read only"));
1158                         break;
1159                 }
1160                 if (!arg.empty()) {
1161                         /* Automatically delete the currently selected
1162                          * text and replace it with what is being
1163                          * typed in now. Depends on lyxrc settings
1164                          * "auto_region_delete", which defaults to
1165                          * true (on). */
1166
1167                         setUndo(bv, Undo::INSERT,
1168                                 lt->cursor.par(), lt->cursor.par()->next());
1169                         bv->setState();
1170                         if (lyxrc.auto_region_delete) {
1171                                 if (lt->selection.set()) {
1172                                         lt->cutSelection(bv, false);
1173                                 }
1174                         }
1175                         lt->clearSelection();
1176                         for (string::size_type i = 0; i < arg.length(); ++i) {
1177                                 bv->owner()->getIntl()->getTrans().TranslateAndInsert(arg[i], lt);
1178                         }
1179                 }
1180                 lt->selection.cursor = lt->cursor;
1181                 updwhat = CURSOR_PAR;
1182                 updflag = true;
1183                 result = DISPATCHED_NOUPDATE;
1184                 break;
1185                 // --- Cursor Movements -----------------------------------
1186         case LFUN_RIGHTSEL:
1187                 finishUndo();
1188                 moveRight(bv, false, true);
1189                 lt->setSelection(bv);
1190                 updwhat = SELECTION;
1191                 break;
1192         case LFUN_RIGHT:
1193                 result = moveRight(bv);
1194                 finishUndo();
1195                 updwhat = CURSOR;
1196                 break;
1197         case LFUN_LEFTSEL:
1198                 finishUndo();
1199                 moveLeft(bv, false, true);
1200                 lt->setSelection(bv);
1201                 updwhat = SELECTION;
1202                 break;
1203         case LFUN_LEFT:
1204                 finishUndo();
1205                 result = moveLeft(bv);
1206                 updwhat = CURSOR;
1207                 break;
1208         case LFUN_DOWNSEL:
1209                 finishUndo();
1210                 moveDown(bv);
1211                 lt->setSelection(bv);
1212                 updwhat = SELECTION;
1213                 break;
1214         case LFUN_DOWN:
1215                 finishUndo();
1216                 result = moveDown(bv);
1217                 updwhat = CURSOR;
1218                 break;
1219         case LFUN_UPSEL:
1220                 finishUndo();
1221                 moveUp(bv);
1222                 lt->setSelection(bv);
1223                 updwhat = SELECTION;
1224                 break;
1225         case LFUN_UP:
1226                 finishUndo();
1227                 result = moveUp(bv);
1228                 updwhat = CURSOR;
1229                 break;
1230         case LFUN_HOME:
1231                 finishUndo();
1232                 lt->cursorHome(bv);
1233                 updwhat = CURSOR;
1234                 break;
1235         case LFUN_END:
1236                 lt->cursorEnd(bv);
1237                 updwhat = CURSOR;
1238                 break;
1239         case LFUN_BACKSPACE: {
1240                 setUndo(bv, Undo::DELETE,
1241                         lt->cursor.par(), lt->cursor.par()->next());
1242                 if (lt->selection.set())
1243                         lt->cutSelection(bv);
1244                 else
1245                         lt->backspace(bv);
1246                 updwhat = CURSOR_PAR;
1247                 updflag = true;
1248         }
1249         break;
1250
1251         case LFUN_DELETE: {
1252                 setUndo(bv, Undo::DELETE,
1253                         lt->cursor.par(), lt->cursor.par()->next());
1254                 if (lt->selection.set()) {
1255                         lt->cutSelection(bv);
1256                 } else {
1257                         lt->Delete(bv);
1258                 }
1259                 updwhat = CURSOR_PAR;
1260                 updflag = true;
1261         }
1262         break;
1263
1264         case LFUN_CUT: {
1265                 setUndo(bv, Undo::DELETE,
1266                         lt->cursor.par(), lt->cursor.par()->next());
1267                 lt->cutSelection(bv);
1268                 updwhat = CURSOR_PAR;
1269                 updflag = true;
1270         }
1271         break;
1272
1273         case LFUN_COPY:
1274                 finishUndo();
1275                 lt->copySelection(bv);
1276                 updwhat = CURSOR_PAR;
1277                 break;
1278         case LFUN_PASTESELECTION:
1279         {
1280                 string const clip(bv->getClipboard());
1281
1282                 if (clip.empty())
1283                         break;
1284                 if (arg == "paragraph") {
1285                         lt->insertStringAsParagraphs(bv, clip);
1286                 } else {
1287                         lt->insertStringAsLines(bv, clip);
1288                 }
1289                 updwhat = CURSOR_PAR;
1290                 updflag = true;
1291                 break;
1292         }
1293         case LFUN_PASTE: {
1294                 if (!autoBreakRows) {
1295
1296                         if (CutAndPaste::nrOfParagraphs() > 1) {
1297                                 Alert::alert(_("Impossible operation"),
1298                                                    _("Cannot include more than one paragraph!"),
1299                                                    _("Sorry."));
1300                                 break;
1301                         }
1302                 }
1303                 setUndo(bv, Undo::INSERT,
1304                         lt->cursor.par(), lt->cursor.par()->next());
1305                 lt->pasteSelection(bv);
1306                 updwhat = CURSOR_PAR;
1307                 updflag = true;
1308         }
1309         break;
1310
1311         case LFUN_BREAKPARAGRAPH:
1312                 if (!autoBreakRows) {
1313                         result = DISPATCHED;
1314                         break;
1315                 }
1316                 lt->breakParagraph(bv, 0);
1317                 updwhat = FULL;
1318                 updflag = true;
1319                 break;
1320         case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
1321                 if (!autoBreakRows) {
1322                         result = DISPATCHED;
1323                         break;
1324                 }
1325                 lt->breakParagraph(bv, 1);
1326                 updwhat = FULL;
1327                 updflag = true;
1328                 break;
1329
1330         case LFUN_BREAKLINE: {
1331                 if (!autoBreakRows) {
1332                         result = DISPATCHED;
1333                         break;
1334                 }
1335                 setUndo(bv, Undo::INSERT,
1336                         lt->cursor.par(), lt->cursor.par()->next());
1337                 lt->insertChar(bv, Paragraph::META_NEWLINE);
1338                 updwhat = CURSOR_PAR;
1339                 updflag = true;
1340         }
1341         break;
1342
1343         case LFUN_LAYOUT:
1344                 // do not set layouts on non breakable textinsets
1345                 if (autoBreakRows) {
1346                         string cur_layout = cpar(bv)->layout();
1347
1348                         // Derive layout number from given argument (string)
1349                         // and current buffer's textclass (number). */
1350                         textclass_type tclass = bv->buffer()->params.textclass;
1351                         string layout = arg;
1352                         bool hasLayout = textclasslist[tclass].hasLayout(layout);
1353
1354                         // If the entry is obsolete, use the new one instead.
1355                         if (hasLayout) {
1356                                 string const & obs =
1357                                         textclasslist[tclass][layout].
1358                                         obsoleted_by();
1359                                 if (!obs.empty())
1360                                         layout = obs;
1361                         }
1362
1363                         // see if we found the layout number:
1364                         if (!hasLayout) {
1365                                 string const msg = string(N_("Layout ")) + arg + N_(" not known");
1366                                 bv->owner()->getLyXFunc()->dispatch(LFUN_MESSAGE, msg);
1367                                 break;
1368                         }
1369
1370                         if (cur_layout != layout) {
1371                                 cur_layout = layout;
1372                                 lt->setLayout(bv, layout);
1373                                 bv->owner()->setLayout(cpar(bv)->layout());
1374                                 updwhat = CURSOR_PAR;
1375                                 updflag = true;
1376                         }
1377                 } else {
1378                         // reset the layout box
1379                         bv->owner()->setLayout(cpar(bv)->layout());
1380                 }
1381                 break;
1382         case LFUN_PARAGRAPH_SPACING:
1383                 // This one is absolutely not working. When fiddling with this
1384                 // it also seems to me that the paragraphs inside the insettext
1385                 // inherit bufferparams/paragraphparams in a strange way. (Lgb)
1386         {
1387                 Paragraph * par = lt->cursor.par();
1388                 Spacing::Space cur_spacing = par->params().spacing().getSpace();
1389                 float cur_value = 1.0;
1390                 if (cur_spacing == Spacing::Other) {
1391                         cur_value = par->params().spacing().getValue();
1392                 }
1393
1394                 istringstream istr(arg.c_str());
1395                 string tmp;
1396                 istr >> tmp;
1397                 Spacing::Space new_spacing = cur_spacing;
1398                 float new_value = cur_value;
1399                 if (tmp.empty()) {
1400                         lyxerr << "Missing argument to `paragraph-spacing'"
1401                                    << endl;
1402                 } else if (tmp == "single") {
1403                         new_spacing = Spacing::Single;
1404                 } else if (tmp == "onehalf") {
1405                         new_spacing = Spacing::Onehalf;
1406                 } else if (tmp == "double") {
1407                         new_spacing = Spacing::Double;
1408                 } else if (tmp == "other") {
1409                         new_spacing = Spacing::Other;
1410                         float tmpval = 0.0;
1411                         istr >> tmpval;
1412                         lyxerr << "new_value = " << tmpval << endl;
1413                         if (tmpval != 0.0)
1414                                 new_value = tmpval;
1415                 } else if (tmp == "default") {
1416                         new_spacing = Spacing::Default;
1417                 } else {
1418                         lyxerr << _("Unknown spacing argument: ")
1419                                    << arg << endl;
1420                 }
1421                 if (cur_spacing != new_spacing || cur_value != new_value) {
1422                         par->params().spacing(Spacing(new_spacing, new_value));
1423                         updwhat = CURSOR_PAR;
1424                         updflag = true;
1425                 }
1426         }
1427         break;
1428
1429         default:
1430                 if (!bv->Dispatch(action, arg))
1431                         result = UNDISPATCHED;
1432                 break;
1433         }
1434
1435         if (clear)
1436                 lt = 0;
1437         if (updwhat > 0)
1438                 updateLocal(bv, updwhat, updflag);
1439         /// If the action has deleted all text in the inset, we need to change the
1440         // language to the language of the surronding text.
1441         if (!was_empty && par->size() == 0 && !par->next()) {
1442                 LyXFont font(LyXFont::ALL_IGNORE);
1443                 font.setLanguage(bv->getParentLanguage(this));
1444                 setFont(bv, font, false);
1445         }
1446
1447         if (result >= FINISHED)
1448                 bv->unlockInset(this);
1449         return result;
1450 }
1451
1452
1453 int InsetText::latex(Buffer const * buf, ostream & os, bool, bool) const
1454 {
1455         TexRow texrow;
1456         buf->latexParagraphs(os, par, 0, texrow);
1457         return texrow.rows();
1458 }
1459
1460
1461 int InsetText::ascii(Buffer const * buf, ostream & os, int linelen) const
1462 {
1463         Paragraph * p = par;
1464         unsigned int lines = 0;
1465
1466         while (p) {
1467                 string const tmp = buf->asciiParagraph(p, linelen, p->previous()==0);
1468                 lines += lyx::count(tmp.begin(), tmp.end(), '\n');
1469                 os << tmp;
1470                 p = p->next();
1471         }
1472         return lines;
1473 }
1474
1475
1476 int InsetText::docbook(Buffer const * buf, ostream & os) const
1477 {
1478         Paragraph * p = par;
1479         unsigned int lines = 0;
1480
1481         vector<string> environment_stack(10);
1482         vector<string> environment_inner(10);
1483
1484         int const command_depth = 0;
1485         string item_name;
1486
1487         Paragraph::depth_type depth = 0; // paragraph depth
1488
1489         while (p) {
1490                 string sgmlparam;
1491                 int desc_on = 0; // description mode
1492
1493                 LyXLayout const & style =
1494                         textclasslist[buf->params.textclass][p->layout()];
1495
1496                 // environment tag closing
1497                 for (; depth > p->params().depth(); --depth) {
1498                         if (environment_inner[depth] != "!-- --") {
1499                                 item_name = "listitem";
1500                                 buf->sgmlCloseTag(os, command_depth + depth,
1501                                              item_name);
1502                                 if (environment_inner[depth] == "varlistentry")
1503                                         buf->sgmlCloseTag(os, depth+command_depth,
1504                                                      environment_inner[depth]);
1505                         }
1506                         buf->sgmlCloseTag(os, depth + command_depth,
1507                                      environment_stack[depth]);
1508                         environment_stack[depth].erase();
1509                         environment_inner[depth].erase();
1510                 }
1511
1512                 if (depth == p->params().depth()
1513                    && environment_stack[depth] != style.latexname()
1514                    && !environment_stack[depth].empty()) {
1515                         if (environment_inner[depth] != "!-- --") {
1516                                 item_name= "listitem";
1517                                 buf->sgmlCloseTag(os, command_depth+depth,
1518                                                   item_name);
1519                                 if (environment_inner[depth] == "varlistentry")
1520                                         buf->sgmlCloseTag(os,
1521                                                           depth + command_depth,
1522                                                           environment_inner[depth]);
1523                         }
1524
1525                         buf->sgmlCloseTag(os, depth + command_depth,
1526                                           environment_stack[depth]);
1527
1528                         environment_stack[depth].erase();
1529                         environment_inner[depth].erase();
1530                 }
1531
1532                 // Write opening SGML tags.
1533                 switch (style.latextype) {
1534                 case LATEX_PARAGRAPH:
1535                         buf->sgmlOpenTag(os, depth + command_depth,
1536                                          style.latexname());
1537                         break;
1538
1539                 case LATEX_COMMAND:
1540                         buf->sgmlError(p, 0,
1541                                        _("Error : LatexType Command not allowed here.\n"));
1542                         return -1;
1543                         break;
1544
1545                 case LATEX_ENVIRONMENT:
1546                 case LATEX_ITEM_ENVIRONMENT:
1547                         if (depth < p->params().depth()) {
1548                                 depth = p->params().depth();
1549                                 environment_stack[depth].erase();
1550                         }
1551
1552                         if (environment_stack[depth] != style.latexname()) {
1553                                 if (environment_stack.size() == depth + 1) {
1554                                         environment_stack.push_back("!-- --");
1555                                         environment_inner.push_back("!-- --");
1556                                 }
1557                                 environment_stack[depth] = style.latexname();
1558                                 environment_inner[depth] = "!-- --";
1559                                 buf->sgmlOpenTag(os, depth + command_depth,
1560                                                  environment_stack[depth]);
1561                         } else {
1562                                 if (environment_inner[depth] != "!-- --") {
1563                                         item_name= "listitem";
1564                                         buf->sgmlCloseTag(os,
1565                                                           command_depth + depth,
1566                                                           item_name);
1567                                         if (environment_inner[depth] == "varlistentry")
1568                                                 buf->sgmlCloseTag(os,
1569                                                                   depth + command_depth,
1570                                                                   environment_inner[depth]);
1571                                 }
1572                         }
1573
1574                         if (style.latextype == LATEX_ENVIRONMENT) {
1575                                 if (!style.latexparam().empty()) {
1576                                         if (style.latexparam() == "CDATA")
1577                                                 os << "<![CDATA[";
1578                                         else
1579                                                 buf->sgmlOpenTag(os, depth + command_depth,
1580                                                                  style.latexparam());
1581                                 }
1582                                 break;
1583                         }
1584
1585                         desc_on = (style.labeltype == LABEL_MANUAL);
1586
1587                         if (desc_on)
1588                                 environment_inner[depth]= "varlistentry";
1589                         else
1590                                 environment_inner[depth]= "listitem";
1591
1592                         buf->sgmlOpenTag(os, depth + 1 + command_depth,
1593                                          environment_inner[depth]);
1594
1595                         if (desc_on) {
1596                                 item_name= "term";
1597                                 buf->sgmlOpenTag(os, depth + 1 + command_depth,
1598                                                  item_name);
1599                         } else {
1600                                 item_name= "para";
1601                                 buf->sgmlOpenTag(os, depth + 1 + command_depth,
1602                                                  item_name);
1603                         }
1604                         break;
1605                 default:
1606                         buf->sgmlOpenTag(os, depth + command_depth,
1607                                          style.latexname());
1608                         break;
1609                 }
1610
1611                 buf->simpleDocBookOnePar(os, p, desc_on,
1612                                          depth + 1 + command_depth);
1613                 p = p->next();
1614
1615                 string end_tag;
1616                 // write closing SGML tags
1617                 switch (style.latextype) {
1618                 case LATEX_ENVIRONMENT:
1619                         if (!style.latexparam().empty()) {
1620                                 if (style.latexparam() == "CDATA")
1621                                         os << "]]>";
1622                                 else
1623                                         buf->sgmlCloseTag(os, depth + command_depth,
1624                                                           style.latexparam());
1625                         }
1626                         break;
1627                 case LATEX_ITEM_ENVIRONMENT:
1628                         if (desc_on == 1) break;
1629                         end_tag= "para";
1630                         buf->sgmlCloseTag(os, depth + 1 + command_depth, end_tag);
1631                         break;
1632                 case LATEX_PARAGRAPH:
1633                         buf->sgmlCloseTag(os, depth + command_depth, style.latexname());
1634                         break;
1635                 default:
1636                         buf->sgmlCloseTag(os, depth + command_depth, style.latexname());
1637                         break;
1638                 }
1639         }
1640
1641         // Close open tags
1642         for (int d = depth; d >= 0; --d) {
1643                 if (!environment_stack[depth].empty()) {
1644                         if (environment_inner[depth] != "!-- --") {
1645                                 item_name = "listitem";
1646                                 buf->sgmlCloseTag(os, command_depth + depth,
1647                                                   item_name);
1648                                if (environment_inner[depth] == "varlistentry")
1649                                        buf->sgmlCloseTag(os, depth + command_depth,
1650                                                          environment_inner[depth]);
1651                         }
1652
1653                         buf->sgmlCloseTag(os, depth + command_depth,
1654                                           environment_stack[depth]);
1655                 }
1656         }
1657
1658         return lines;
1659 }
1660
1661
1662 void InsetText::validate(LaTeXFeatures & features) const
1663 {
1664         Paragraph * p = par;
1665         while (p) {
1666                 p->validate(features);
1667                 p = p->next();
1668         }
1669 }
1670
1671
1672 int InsetText::beginningOfMainBody(Buffer const * buf, Paragraph * p) const
1673 {
1674         if (textclasslist[buf->params.textclass][p->layout()].labeltype != LABEL_MANUAL)
1675                 return 0;
1676         else
1677                 return p->beginningOfMainBody();
1678 }
1679
1680
1681 void InsetText::getCursorPos(BufferView * bv,
1682                              int & x, int & y) const
1683 {
1684         if (the_locking_inset) {
1685                 the_locking_inset->getCursorPos(bv, x, y);
1686                 return;
1687         }
1688         x = cx(bv);
1689         y = cy(bv);
1690 }
1691
1692
1693 unsigned int InsetText::insetInInsetY()
1694 {
1695         if (!the_locking_inset)
1696                 return 0;
1697
1698         return (inset_y + the_locking_inset->insetInInsetY());
1699 }
1700
1701
1702 void InsetText::toggleInsetCursor(BufferView * bv)
1703 {
1704         if (the_locking_inset) {
1705                 the_locking_inset->toggleInsetCursor(bv);
1706                 return;
1707         }
1708
1709         LyXFont const font(getLyXText(bv)->getFont(bv->buffer(), cpar(bv), cpos(bv)));
1710
1711         int const asc = lyxfont::maxAscent(font);
1712         int const desc = lyxfont::maxDescent(font);
1713
1714         if (isCursorVisible())
1715                 bv->hideLockedInsetCursor();
1716         else
1717                 bv->showLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1718         toggleCursorVisible();
1719 }
1720
1721
1722 void InsetText::showInsetCursor(BufferView * bv, bool show)
1723 {
1724         if (the_locking_inset) {
1725                 the_locking_inset->showInsetCursor(bv, show);
1726                 return;
1727         }
1728         if (!isCursorVisible()) {
1729                 LyXFont const font =
1730                         getLyXText(bv)->getFont(bv->buffer(), cpar(bv), cpos(bv));
1731
1732                 int const asc = lyxfont::maxAscent(font);
1733                 int const desc = lyxfont::maxDescent(font);
1734
1735                 bv->fitLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1736                 if (show)
1737                         bv->showLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1738                 setCursorVisible(true);
1739         }
1740 }
1741
1742
1743 void InsetText::hideInsetCursor(BufferView * bv)
1744 {
1745         if (isCursorVisible()) {
1746                 bv->hideLockedInsetCursor();
1747                 setCursorVisible(false);
1748         }
1749         if (the_locking_inset)
1750                 the_locking_inset->hideInsetCursor(bv);
1751 }
1752
1753
1754 void InsetText::fitInsetCursor(BufferView * bv) const
1755 {
1756         if (the_locking_inset) {
1757                 the_locking_inset->fitInsetCursor(bv);
1758                 return;
1759         }
1760         LyXFont const font =
1761                 getLyXText(bv)->getFont(bv->buffer(), cpar(bv), cpos(bv));
1762
1763         int const asc = lyxfont::maxAscent(font);
1764         int const desc = lyxfont::maxDescent(font);
1765
1766         bv->fitLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1767 }
1768
1769
1770 UpdatableInset::RESULT
1771 InsetText::moveRight(BufferView * bv, bool activate_inset, bool selecting)
1772 {
1773         if (getLyXText(bv)->cursor.par()->isRightToLeftPar(bv->buffer()->params))
1774                 return moveLeftIntern(bv, false, activate_inset, selecting);
1775         else
1776                 return moveRightIntern(bv, false, activate_inset, selecting);
1777 }
1778
1779
1780 UpdatableInset::RESULT
1781 InsetText::moveLeft(BufferView * bv, bool activate_inset, bool selecting)
1782 {
1783         if (getLyXText(bv)->cursor.par()->isRightToLeftPar(bv->buffer()->params))
1784                 return moveRightIntern(bv, true, activate_inset, selecting);
1785         else
1786                 return moveLeftIntern(bv, true, activate_inset, selecting);
1787 }
1788
1789
1790 UpdatableInset::RESULT
1791 InsetText::moveRightIntern(BufferView * bv, bool behind,
1792                            bool activate_inset, bool selecting)
1793 {
1794         if (!cpar(bv)->next() && (cpos(bv) >= cpar(bv)->size()))
1795                 return FINISHED_RIGHT;
1796         if (activate_inset && checkAndActivateInset(bv, behind))
1797                 return DISPATCHED;
1798         getLyXText(bv)->cursorRight(bv);
1799         if (!selecting)
1800                 getLyXText(bv)->selection.cursor = getLyXText(bv)->cursor;
1801         return DISPATCHED_NOUPDATE;
1802 }
1803
1804
1805 UpdatableInset::RESULT
1806 InsetText::moveLeftIntern(BufferView * bv, bool behind,
1807                           bool activate_inset, bool selecting)
1808 {
1809         if (!cpar(bv)->previous() && (cpos(bv) <= 0))
1810                 return FINISHED;
1811         getLyXText(bv)->cursorLeft(bv);
1812         if (!selecting)
1813                 getLyXText(bv)->selection.cursor = getLyXText(bv)->cursor;
1814         if (activate_inset && checkAndActivateInset(bv, behind))
1815                 return DISPATCHED;
1816         return DISPATCHED_NOUPDATE;
1817 }
1818
1819
1820 UpdatableInset::RESULT
1821 InsetText::moveUp(BufferView * bv)
1822 {
1823         if (!crow(bv)->previous())
1824                 return FINISHED_UP;
1825         getLyXText(bv)->cursorUp(bv);
1826         return DISPATCHED_NOUPDATE;
1827 }
1828
1829
1830 UpdatableInset::RESULT
1831 InsetText::moveDown(BufferView * bv)
1832 {
1833         if (!crow(bv)->next())
1834                 return FINISHED_DOWN;
1835         getLyXText(bv)->cursorDown(bv);
1836         return DISPATCHED_NOUPDATE;
1837 }
1838
1839
1840 bool InsetText::insertInset(BufferView * bv, Inset * inset)
1841 {
1842         if (the_locking_inset) {
1843                 if (the_locking_inset->insetAllowed(inset))
1844                         return the_locking_inset->insertInset(bv, inset);
1845                 return false;
1846         }
1847         bool clear = false;
1848         if (!lt) {
1849                 lt = getLyXText(bv);
1850                 clear = true;
1851         }
1852         setUndo(bv, Undo::FINISH, lt->cursor.par(), lt->cursor.par()->next());
1853         freezeUndo();
1854         inset->setOwner(this);
1855         hideInsetCursor(bv);
1856         lt->insertInset(bv, inset);
1857         bv->fitCursor();
1858         if (clear)
1859                 lt = 0;
1860         updateLocal(bv, CURSOR_PAR|CURSOR, true);
1861         unFreezeUndo();
1862         return true;
1863 }
1864
1865
1866 bool InsetText::insetAllowed(Inset::Code code) const
1867 {
1868         // in_insetAllowed is a really gross hack,
1869         // to allow us to call the owner's insetAllowed
1870         // without stack overflow, which can happen
1871         // when the owner uses InsetCollapsable::insetAllowed()
1872         bool ret = true;
1873         if (in_insetAllowed)
1874                 return ret;
1875         in_insetAllowed = true;
1876         if (the_locking_inset)
1877                 ret = the_locking_inset->insetAllowed(code);
1878         else if (owner())
1879                 ret = owner()->insetAllowed(code);
1880         in_insetAllowed = false;
1881         return ret;
1882 }
1883
1884
1885 UpdatableInset * InsetText::getLockingInset() const
1886 {
1887         return the_locking_inset ? the_locking_inset->getLockingInset() :
1888                 const_cast<InsetText *>(this);
1889 }
1890
1891
1892 UpdatableInset * InsetText::getFirstLockingInsetOfType(Inset::Code c)
1893 {
1894         if (c == lyxCode())
1895                 return this;
1896         if (the_locking_inset)
1897                 return the_locking_inset->getFirstLockingInsetOfType(c);
1898         return 0;
1899 }
1900
1901
1902 bool InsetText::showInsetDialog(BufferView * bv) const
1903 {
1904         if (the_locking_inset)
1905                 return the_locking_inset->showInsetDialog(bv);
1906         return false;
1907 }
1908
1909
1910 vector<string> const InsetText::getLabelList() const
1911 {
1912         vector<string> label_list;
1913
1914         Paragraph * tpar = par;
1915         while (tpar) {
1916                 Paragraph::inset_iterator beg = tpar->inset_iterator_begin();
1917                 Paragraph::inset_iterator end = tpar->inset_iterator_end();
1918                 for (; beg != end; ++beg) {
1919                         vector<string> const l = (*beg)->getLabelList();
1920                         label_list.insert(label_list.end(), l.begin(), l.end());
1921                 }
1922                 tpar = tpar->next();
1923         }
1924         return label_list;
1925 }
1926
1927
1928 void InsetText::setFont(BufferView * bv, LyXFont const & font, bool toggleall,
1929                         bool selectall)
1930 {
1931         if (the_locking_inset) {
1932                 the_locking_inset->setFont(bv, font, toggleall, selectall);
1933                 return;
1934         }
1935         if ((!par->next() && !par->size()) || !cpar(bv)->size()) {
1936                 getLyXText(bv)->setFont(bv, font, toggleall);
1937                 return;
1938         }
1939         bool clear = false;
1940         if (!lt) {
1941                 lt = getLyXText(bv);
1942                 clear = true;
1943         }
1944         if (lt->selection.set()) {
1945                 setUndo(bv, Undo::EDIT, lt->cursor.par(), lt->cursor.par()->next());
1946         }
1947         if (selectall)
1948                 selectAll(bv);
1949         lt->toggleFree(bv, font, toggleall);
1950         if (selectall)
1951                 lt->clearSelection();
1952         bv->fitCursor();
1953         bool flag = (selectall || lt->selection.set());
1954         if (clear)
1955                 lt = 0;
1956         if (flag)
1957                 updateLocal(bv, FULL, true);
1958         else
1959                 updateLocal(bv, CURSOR_PAR, true);
1960 }
1961
1962
1963 bool InsetText::checkAndActivateInset(BufferView * bv, bool behind)
1964 {
1965         if (cpar(bv)->isInset(cpos(bv))) {
1966                 unsigned int x;
1967                 unsigned int y;
1968                 Inset * inset =
1969                         static_cast<UpdatableInset*>(cpar(bv)->getInset(cpos(bv)));
1970                 if (!isHighlyEditableInset(inset))
1971                         return false;
1972                 LyXFont const font =
1973                         getLyXText(bv)->getFont(bv->buffer(), cpar(bv), cpos(bv));
1974                 if (behind) {
1975                         x = inset->width(bv, font);
1976                         y = font.isRightToLeft() ? 0 : inset->descent(bv, font);
1977                 } else {
1978                         x = 0;
1979                         y = font.isRightToLeft() ? inset->descent(bv, font) : 0;
1980                 }
1981                 //inset_x = cx(bv) - top_x + drawTextXOffset;
1982                 //inset_y = cy(bv) + drawTextYOffset;
1983                 inset->edit(bv, x, y, 0);
1984                 if (!the_locking_inset)
1985                         return false;
1986                 updateLocal(bv, CURSOR, false);
1987                 return true;
1988         }
1989         return false;
1990 }
1991
1992
1993 bool InsetText::checkAndActivateInset(BufferView * bv, int x, int y,
1994                                       int button)
1995 {
1996         x -= drawTextXOffset;
1997         int dummyx = x;
1998         int dummyy = y + insetAscent;
1999         Inset * inset = bv->checkInsetHit(getLyXText(bv), dummyx, dummyy);
2000
2001         if (inset) {
2002                 if (x < 0)
2003                         x = insetWidth;
2004                 if (y < 0)
2005                         y = insetDescent;
2006                 inset_x = cx(bv) - top_x + drawTextXOffset;
2007                 inset_y = cy(bv) + drawTextYOffset;
2008                 inset->edit(bv, x - inset_x, y - inset_y, button);
2009                 if (!the_locking_inset)
2010                         return false;
2011                 updateLocal(bv, CURSOR, false);
2012                 return true;
2013         }
2014         return false;
2015 }
2016
2017
2018 int InsetText::getMaxWidth(BufferView * bv, UpdatableInset const * inset) const
2019 {
2020 #if 0
2021         int w = UpdatableInset::getMaxWidth(bv, inset);
2022         if (w < 0) {
2023                 return -1;
2024         }
2025         if (owner()) {
2026                 w = w - top_x + owner()->x();
2027                 return w;
2028         }
2029         w -= (2 * TEXT_TO_INSET_OFFSET);
2030         return w - top_x;
2031 #else
2032         return UpdatableInset::getMaxWidth(bv, inset);
2033 #endif
2034 }
2035
2036
2037 void InsetText::setParagraphData(Paragraph * p, bool same_id)
2038 {
2039         // we have to unlock any locked inset otherwise we're in troubles
2040         the_locking_inset = 0;
2041         while (par) {
2042                 Paragraph * tmp = par->next();
2043                 delete par;
2044                 par = tmp;
2045         }
2046
2047         par = new Paragraph(*p, same_id);
2048         par->setInsetOwner(this);
2049         Paragraph * np = par;
2050         while (p->next()) {
2051                 p = p->next();
2052                 np->next(new Paragraph(*p, same_id));
2053                 np->next()->previous(np);
2054                 np = np->next();
2055                 np->setInsetOwner(this);
2056         }
2057         reinitLyXText();
2058         need_update = INIT;
2059 }
2060
2061
2062 void InsetText::setText(string const & data)
2063 {
2064         clear();
2065         LyXFont font(LyXFont::ALL_SANE);
2066         for (unsigned int i=0; i < data.length(); ++i)
2067                 par->insertChar(i, data[i], font);
2068 }
2069
2070
2071 void InsetText::setAutoBreakRows(bool flag)
2072 {
2073         if (flag != autoBreakRows) {
2074                 autoBreakRows = flag;
2075                 if (!flag)
2076                         removeNewlines();
2077                 need_update = INIT;
2078         }
2079 }
2080
2081
2082 void InsetText::setDrawFrame(BufferView * bv, DrawFrame how)
2083 {
2084         if (how != drawFrame_) {
2085                 drawFrame_ = how;
2086                 if (bv)
2087                         updateLocal(bv, DRAW_FRAME, false);
2088         }
2089 }
2090
2091
2092 void InsetText::setFrameColor(BufferView * bv, LColor::color col)
2093 {
2094         if (frame_color != col) {
2095                 frame_color = col;
2096                 if (bv)
2097                         updateLocal(bv, DRAW_FRAME, false);
2098         }
2099 }
2100
2101
2102 int InsetText::cx(BufferView * bv) const
2103 {
2104         // we do nothing dangerous so we use a local cache
2105         LyXText * llt = getLyXText(bv);
2106         int x = llt->cursor.x() + top_x + TEXT_TO_INSET_OFFSET;
2107         if (the_locking_inset) {
2108                 LyXFont font = llt->getFont(bv->buffer(), llt->cursor.par(),
2109                                             llt->cursor.pos());
2110                 if (font.isVisibleRightToLeft())
2111                         x -= the_locking_inset->width(bv, font);
2112         }
2113         return x;
2114 }
2115
2116
2117 int InsetText::cy(BufferView * bv) const
2118 {
2119         LyXFont font;
2120         return getLyXText(bv)->cursor.y() - ascent(bv, font) + TEXT_TO_INSET_OFFSET;
2121 }
2122
2123
2124 pos_type InsetText::cpos(BufferView * bv) const
2125 {
2126         return getLyXText(bv)->cursor.pos();
2127 }
2128
2129
2130 Paragraph * InsetText::cpar(BufferView * bv) const
2131 {
2132         return getLyXText(bv)->cursor.par();
2133 }
2134
2135
2136 bool InsetText::cboundary(BufferView * bv) const
2137 {
2138         return getLyXText(bv)->cursor.boundary();
2139 }
2140
2141
2142 Row * InsetText::crow(BufferView * bv) const
2143 {
2144         return getLyXText(bv)->cursor.row();
2145 }
2146
2147
2148 LyXText * InsetText::getLyXText(BufferView const * lbv,
2149                                 bool const recursive) const
2150 {
2151         if (cached_bview == lbv) {
2152                 if (recursive && the_locking_inset)
2153                         return the_locking_inset->getLyXText(lbv, true);
2154                 LyXText * lt = cached_text.get();
2155                 lyx::Assert(lt && lt->firstRow()->par() == par);
2156                 return lt;
2157         }
2158         // Super UGLY! (Lgb)
2159         BufferView * bv = const_cast<BufferView *>(lbv);
2160
2161         cached_bview = bv;
2162         Cache::iterator it = cache.find(bv);
2163
2164         if (it != cache.end()) {
2165                 if (do_reinit) {
2166                         reinitLyXText();
2167                 } else if (do_resize) {
2168                         resizeLyXText(do_resize);
2169                 } else {
2170                         if (lt || !it->second.remove) {
2171                                 lyx::Assert(it->second.text.get());
2172                                 cached_text = it->second.text;
2173                                 if (recursive && the_locking_inset) {
2174                                         return the_locking_inset->getLyXText(bv, true);
2175                                 }
2176                                 return cached_text.get();
2177                         } else if (it->second.remove) {
2178                                 if (locked) {
2179                                         saveLyXTextState(it->second.text.get());
2180                                 } else {
2181                                         sstate.lpar = 0;
2182                                 }
2183                         }
2184                         //
2185                         // when we have to reinit the existing LyXText!
2186                         //
2187                         it->second.text->init(bv);
2188                         restoreLyXTextState(bv, it->second.text.get());
2189                         it->second.remove = false;
2190                 }
2191                 cached_text = it->second.text;
2192                 if (the_locking_inset && recursive) {
2193                         return the_locking_inset->getLyXText(bv);
2194                 }
2195                 return cached_text.get();
2196         }
2197         ///
2198         // we are here only if we don't have a BufferView * in the cache!!!
2199         ///
2200         cached_text.reset(new LyXText(const_cast<InsetText *>(this)));
2201         cached_text->init(bv);
2202         restoreLyXTextState(bv, cached_text.get());
2203
2204         cache.insert(make_pair(bv, cached_text));
2205
2206         if (the_locking_inset && recursive) {
2207                 return the_locking_inset->getLyXText(bv);
2208         }
2209         return cached_text.get();
2210 }
2211
2212
2213 void InsetText::deleteLyXText(BufferView * bv, bool recursive) const
2214 {
2215         cached_bview = 0;
2216
2217         Cache::iterator it = cache.find(bv);
2218
2219         if (it == cache.end()) {
2220                 return;
2221         }
2222
2223         lyx::Assert(it->second.text.get());
2224
2225         it->second.remove = true;
2226         if (recursive) {
2227                 /// then remove all LyXText in text-insets
2228                 Paragraph * p = par;
2229                 for (; p; p = p->next()) {
2230                         p->deleteInsetsLyXText(bv);
2231                 }
2232         }
2233 }
2234
2235
2236 void InsetText::resizeLyXText(BufferView * bv, bool force) const
2237 {
2238         if (lt) {
2239                 // we cannot resize this because we are in use!
2240                 // so do this on the next possible getLyXText()
2241                 do_resize = bv;
2242                 return;
2243         }
2244         do_resize = 0;
2245 //      lyxerr << "InsetText::resizeLyXText\n";
2246         if (!par->next() && !par->size()) { // no data, resize not neccessary!
2247                 // we have to do this as a fixed width may have changed!
2248                 LyXText * t = getLyXText(bv);
2249                 saveLyXTextState(t);
2250                 t->init(bv, true);
2251                 restoreLyXTextState(bv, t);
2252                 return;
2253         }
2254         // one endless line, resize normally not necessary
2255         if (!force && getMaxWidth(bv, this) < 0)
2256                 return;
2257
2258         Cache::iterator it = cache.find(bv);
2259         if (it == cache.end()) {
2260                 return;
2261         }
2262         lyx::Assert(it->second.text.get());
2263
2264         LyXText * t = it->second.text.get();
2265         saveLyXTextState(t);
2266         for (Paragraph * p = par; p; p = p->next()) {
2267                 p->resizeInsetsLyXText(bv);
2268         }
2269         t->init(bv, true);
2270         restoreLyXTextState(bv, t);
2271         if (the_locking_inset) {
2272                 inset_x = cx(bv) - top_x + drawTextXOffset;
2273                 inset_y = cy(bv) + drawTextYOffset;
2274         }
2275
2276         if (bv->screen()) {
2277                 t->first_y = bv->screen()->topCursorVisible(t);
2278         }
2279         if (!owner()) {
2280                 updateLocal(bv, FULL, false);
2281                 // this will scroll the screen such that the cursor becomes visible
2282                 bv->updateScrollbar();
2283         } else {
2284                 need_update |= FULL;
2285         }
2286 }
2287
2288
2289 void InsetText::reinitLyXText() const
2290 {
2291         if (lt) {
2292                 // we cannot resize this because we are in use!
2293                 // so do this on the next possible getLyXText()
2294                 do_reinit = true;
2295                 return;
2296         }
2297         do_reinit = false;
2298         do_resize = 0;
2299 //      lyxerr << "InsetText::reinitLyXText\n";
2300         for(Cache::iterator it = cache.begin(); it != cache.end(); ++it) {
2301                 lyx::Assert(it->second.text.get());
2302
2303                 LyXText * t = it->second.text.get();
2304                 BufferView * bv = it->first;
2305
2306                 saveLyXTextState(t);
2307                 for (Paragraph * p = par; p; p = p->next()) {
2308                         p->resizeInsetsLyXText(bv);
2309                 }
2310                 t->init(bv, true);
2311                 restoreLyXTextState(bv, t);
2312                 if (the_locking_inset) {
2313                         inset_x = cx(bv) - top_x + drawTextXOffset;
2314                         inset_y = cy(bv) + drawTextYOffset;
2315                 }
2316                 if (bv->screen()) {
2317                         t->first_y = bv->screen()->topCursorVisible(t);
2318                 }
2319                 if (!owner()) {
2320                         updateLocal(bv, FULL, false);
2321                         // this will scroll the screen such that the cursor becomes visible
2322                         bv->updateScrollbar();
2323                 } else {
2324                         need_update = FULL;
2325                 }
2326         }
2327 }
2328
2329
2330 void InsetText::removeNewlines()
2331 {
2332         bool changed = false;
2333
2334         for (Paragraph * p = par; p; p = p->next()) {
2335                 for (int i = 0; i < p->size(); ++i) {
2336                         if (p->getChar(i) == Paragraph::META_NEWLINE) {
2337                                 changed = true;
2338                                 p->erase(i);
2339                         }
2340                 }
2341         }
2342         if (changed)
2343                 reinitLyXText();
2344 }
2345
2346
2347 bool InsetText::nodraw() const
2348 {
2349         if (the_locking_inset)
2350                 return the_locking_inset->nodraw();
2351         return UpdatableInset::nodraw();
2352 }
2353
2354
2355 int InsetText::scroll(bool recursive) const
2356 {
2357         int sx = UpdatableInset::scroll(false);
2358
2359         if (recursive && the_locking_inset)
2360                 sx += the_locking_inset->scroll(recursive);
2361
2362         return sx;
2363 }
2364
2365
2366 bool InsetText::doClearArea() const
2367 {
2368         return !locked || (need_update & (FULL|INIT));
2369 }
2370
2371
2372 void InsetText::selectAll(BufferView * bv)
2373 {
2374         getLyXText(bv)->cursorTop(bv);
2375         getLyXText(bv)->selection.cursor = getLyXText(bv)->cursor;
2376         getLyXText(bv)->cursorBottom(bv);
2377         getLyXText(bv)->setSelection(bv);
2378 }
2379
2380
2381 void InsetText::clearSelection(BufferView * bv)
2382 {
2383         getLyXText(bv)->clearSelection();
2384 }
2385
2386
2387 void InsetText::clearInset(BufferView * bv, int baseline, bool & cleared) const
2388 {
2389         Painter & pain = bv->painter();
2390         int w = insetWidth;
2391         int h = insetAscent + insetDescent;
2392         int ty = baseline - insetAscent;
2393
2394         if (ty < 0) {
2395                 h += ty;
2396                 ty = 0;
2397         }
2398         if ((ty + h) > pain.paperHeight())
2399                 h = pain.paperHeight();
2400         if ((top_x + drawTextXOffset + w) > pain.paperWidth())
2401                 w = pain.paperWidth();
2402 //      w -= TEXT_TO_INSET_OFFSET;
2403         pain.fillRectangle(top_x, ty, w+1, h+1, backgroundColor());
2404         cleared = true;
2405         need_update = FULL;
2406         frame_is_visible = false;
2407 }
2408
2409
2410 Paragraph * InsetText::getParFromID(int id) const
2411 {
2412 #if 0
2413         Paragraph * result = par;
2414         Paragraph * ires = 0;
2415         while (result && result->id() != id) {
2416                 if ((ires = result->getParFromID(id)))
2417                         return ires;
2418                 result = result->next();
2419         }
2420         return result;
2421 #else
2422         Paragraph * tmp = par;
2423         while (tmp) {
2424                 if (tmp->id() == id) {
2425                         return tmp;
2426                 }
2427                 Paragraph * tmp2 = tmp->getParFromID(id);
2428                 if (tmp2 != 0) {
2429                         return tmp2;
2430                 }
2431                 tmp = tmp->next();
2432         }
2433         return 0;
2434 #endif
2435 }
2436
2437
2438 Paragraph * InsetText::firstParagraph() const
2439 {
2440         Paragraph * result;
2441         if (the_locking_inset)
2442                 if ((result = the_locking_inset->firstParagraph()))
2443                         return result;
2444         return par;
2445 }
2446
2447
2448 Paragraph * InsetText::getFirstParagraph(int i) const
2449 {
2450         return (i == 0) ? par : 0;
2451 }
2452
2453
2454 LyXCursor const & InsetText::cursor(BufferView * bv) const
2455 {
2456                 if (the_locking_inset)
2457                                 return the_locking_inset->cursor(bv);
2458                 return getLyXText(bv)->cursor;
2459 }
2460
2461
2462 Paragraph * InsetText::paragraph() const
2463 {
2464         return par;
2465 }
2466
2467
2468 void InsetText::paragraph(Paragraph * p)
2469 {
2470         // GENERAL COMMENT: We don't have to free the old paragraphs as the
2471         // caller of this function has to take care of it. This IS important
2472         // as we could have to insert a paragraph before this one and just
2473         // link the actual to a new ones next and set it with this function
2474         // and are done!
2475         par = p;
2476         // set ourself as owner for all the paragraphs inserted!
2477         Paragraph * np = par;
2478         while (np) {
2479                 np->setInsetOwner(this);
2480                 np = np->next();
2481         }
2482         reinitLyXText();
2483         // redraw myself when asked for
2484         need_update = INIT;
2485 }
2486
2487
2488 Inset * InsetText::getInsetFromID(int id_arg) const
2489 {
2490         if (id_arg == id())
2491                 return const_cast<InsetText *>(this);
2492
2493         Paragraph * lp = par;
2494
2495         while (lp) {
2496                 for (Paragraph::inset_iterator it = lp->inset_iterator_begin(),
2497                          en = lp->inset_iterator_end();
2498                          it != en; ++it)
2499                 {
2500                         if ((*it)->id() == id_arg)
2501                                 return *it;
2502                         Inset * in = (*it)->getInsetFromID(id_arg);
2503                         if (in)
2504                                 return in;
2505                 }
2506                 lp = lp->next();
2507         }
2508         return 0;
2509 }
2510
2511
2512 string const InsetText::selectNextWordToSpellcheck(BufferView * bv, float & value) const
2513 {
2514         bool clear = false;
2515         string str;
2516
2517         if (!lt) {
2518                 lt = getLyXText(bv);
2519                 clear = true;
2520         }
2521         if (the_locking_inset) {
2522                 str = the_locking_inset->selectNextWordToSpellcheck(bv, value);
2523                 if (!str.empty()) {
2524                         value += cy(bv);
2525                         if (clear)
2526                                 lt = 0;
2527                         return str;
2528                 }
2529                 // we have to go on checking so move cusor to the next char
2530                 lt->cursor.pos(lt->cursor.pos() + 1);
2531         }
2532         str = lt->selectNextWordToSpellcheck(bv, value);
2533         if (str.empty())
2534                 bv->unlockInset(const_cast<InsetText *>(this));
2535         else
2536                 value = cy(bv);
2537         if (clear)
2538                 lt = 0;
2539         return str;
2540 }
2541
2542
2543 void InsetText::selectSelectedWord(BufferView * bv)
2544 {
2545         if (the_locking_inset) {
2546                 the_locking_inset->selectSelectedWord(bv);
2547                 return;
2548         }
2549         getLyXText(bv)->selectSelectedWord(bv);
2550         updateLocal(bv, SELECTION, false);
2551 }
2552
2553
2554 void InsetText::toggleSelection(BufferView * bv, bool kill_selection)
2555 {
2556         if (the_locking_inset) {
2557                 the_locking_inset->toggleSelection(bv, kill_selection);
2558         }
2559         bool clear = false;
2560         if (!lt) {
2561                 lt = getLyXText(bv);
2562                 clear = true;
2563         }
2564
2565         int x = top_x + TEXT_TO_INSET_OFFSET;
2566
2567         Row * row = lt->firstRow();
2568         int y_offset = top_baseline - row->ascent_of_text();
2569         int y = y_offset;
2570         while ((row != 0) && ((y+row->height()) <= 0)) {
2571                 y += row->height();
2572                 row = row->next();
2573         }
2574         if (y_offset < 0)
2575                 y_offset = y;
2576
2577         if (need_update & SELECTION)
2578                 need_update = NONE;
2579         bv->screen()->toggleSelection(lt, bv, kill_selection, y_offset, x);
2580         if (clear)
2581                 lt = 0;
2582 }
2583
2584
2585 bool InsetText::searchForward(BufferView * bv, string const & str,
2586                               bool cs, bool mw)
2587 {
2588         if (the_locking_inset) {
2589                 if (the_locking_inset->searchForward(bv, str, cs, mw))
2590                         return true;
2591                 bool clear = false;
2592                 if (!lt) {
2593                         lt = getLyXText(bv);
2594                         clear = true;
2595                 }
2596                 Paragraph * lpar = lt->cursor.par();
2597                 pos_type pos = lt->cursor.pos();
2598                 if (pos < lpar->size() - 1)
2599                         ++pos;
2600                 else {
2601                         pos = 0;
2602                         lpar = lpar->next();
2603                 }
2604                 if (!lpar) {
2605                         if (clear)
2606                                 lt = 0;
2607                         // we have to unlock ourself in this function by default!
2608                         bv->unlockInset(const_cast<InsetText *>(this));
2609                         return false;
2610                 }
2611                 lt->setCursor(bv, lpar, pos);
2612                 if (clear)
2613                         lt = 0;
2614         }
2615         if (LyXFind(bv, str, true, true, cs , mw)) {
2616                 return true;
2617         }
2618         // we have to unlock ourself in this function by default!
2619         bv->unlockInset(const_cast<InsetText *>(this));
2620         return false;
2621 }
2622
2623 bool InsetText::searchBackward(BufferView * bv, string const & str,
2624                                bool cs, bool mw)
2625 {
2626         if (the_locking_inset)
2627                 if (the_locking_inset->searchBackward(bv, str, cs, mw))
2628                         return true;
2629         if (LyXFind(bv, str, false, true, cs, mw)) {
2630                 return true;
2631         }
2632         // we have to unlock ourself in this function by default!
2633         bv->unlockInset(const_cast<InsetText *>(this));
2634         return false;
2635 }
2636
2637
2638 bool InsetText::checkInsertChar(LyXFont & font)
2639 {
2640         if (owner())
2641                 return owner()->checkInsertChar(font);
2642         return true;
2643 }
2644
2645
2646 void InsetText::collapseParagraphs(BufferParams const & bparams) const
2647 {
2648         while(par->next()) {
2649                 if (!par->isSeparator(par->size()-1))
2650                         par->insertChar(par->size()-1, ' ');
2651                 par->pasteParagraph(bparams);
2652         }
2653         reinitLyXText();
2654 }
2655
2656
2657 void InsetText::getDrawFont(LyXFont & font) const
2658 {
2659         if (!owner())
2660                 return;
2661         owner()->getDrawFont(font);
2662 }
2663
2664
2665 void InsetText::appendParagraphs(BufferParams const & bparams,
2666                                  Paragraph * newpar)
2667 {
2668         Paragraph * buf;
2669         Paragraph * tmpbuf = newpar;
2670         Paragraph * lastbuffer = buf = new Paragraph(*tmpbuf, false);
2671
2672         while (tmpbuf->next()) {
2673                 tmpbuf = tmpbuf->next();
2674                 lastbuffer->next(new Paragraph(*tmpbuf, false));
2675                 lastbuffer->next()->previous(lastbuffer);
2676                 lastbuffer = lastbuffer->next();
2677         }
2678         lastbuffer = par;
2679         while (lastbuffer->next())
2680                 lastbuffer = lastbuffer->next();
2681         if (newpar->size() && lastbuffer->size() &&
2682                 !lastbuffer->isSeparator(lastbuffer->size()-1))
2683         {
2684                 lastbuffer->insertChar(lastbuffer->size(), ' ');
2685         }
2686
2687         // make the buf exactly the same layout than our last paragraph
2688         buf->makeSameLayout(lastbuffer);
2689
2690         // paste it!
2691         lastbuffer->next(buf);
2692         buf->previous(lastbuffer);
2693         lastbuffer->pasteParagraph(bparams);
2694
2695         reinitLyXText();
2696 }