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