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