]> git.lyx.org Git - lyx.git/blob - src/insets/insettext.C
c963880c597d4e92c9882ee2c31ece6d6012c67c
[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
18 #ifdef __GNUG__
19 #pragma implementation
20 #endif
21
22 #include "insettext.h"
23 #include "paragraph.h"
24 #include "lyxlex.h"
25 #include "debug.h"
26 #include "lyxfont.h"
27 #include "commandtags.h"
28 #include "buffer.h"
29 #include "LyXView.h"
30 #include "BufferView.h"
31 #include "layout.h"
32 #include "LaTeXFeatures.h"
33 #include "Painter.h"
34 #include "lyx_gui_misc.h"
35 #include "lyxtext.h"
36 #include "lyxcursor.h"
37 #include "CutAndPaste.h"
38 #include "font.h"
39 #include "LColor.h"
40 #include "support/textutils.h"
41 #include "support/LAssert.h"
42 #include "lyxrow.h"
43 #include "lyxrc.h"
44 #include "intl.h"
45 #include "trans_mgr.h"
46 #include "lyxscreen.h"
47 #include "WorkArea.h"
48 #include "gettext.h"
49 #include "lyxfunc.h"
50 #include "ParagraphParameters.h"
51
52 using std::ostream;
53 using std::ifstream;
54 using std::endl;
55 using std::min;
56 using std::max;
57
58 extern unsigned char getCurrentTextClass(Buffer *);
59 extern bool math_insert_greek(BufferView *, char);
60 extern int greek_kb_flag;
61
62 InsetText::InsetText()
63 {
64         par = new Paragraph;
65         init();
66 }
67
68
69 InsetText::InsetText(InsetText const & ins)
70         : UpdatableInset()
71 {
72         par = 0;
73         init(&ins);
74         autoBreakRows = ins.autoBreakRows;
75 }
76
77
78 InsetText & InsetText::operator=(InsetText const & it)
79 {
80         init(&it);
81         autoBreakRows = it.autoBreakRows;
82         return * this;
83 }
84
85
86 void InsetText::init(InsetText const * ins)
87 {
88         top_y = 0;
89         last_width = 0;
90         last_height = 0;
91         insetAscent = 0;
92         insetDescent = 0;
93         insetWidth = 0;
94         the_locking_inset = 0;
95         old_max_width = 0;
96         no_selection = false;
97         need_update = INIT;
98         drawTextXOffset = 0;
99         drawTextYOffset = 0;
100         autoBreakRows = false;
101         drawFrame_ = NEVER;
102         xpos = 0.0;
103         if (ins) {
104                 setParagraphData(ins->par);
105                 autoBreakRows = ins->autoBreakRows;
106                 drawFrame_ = ins->drawFrame_;
107         }
108         par->setInsetOwner(this);
109         frame_color = LColor::insetframe;
110         locked = false;
111         old_par = 0;
112         last_drawn_width = -1;
113         frame_is_visible = false;
114         cached_bview = 0;
115 }
116
117
118 InsetText::~InsetText()
119 {
120         // delete all instances of LyXText before deleting the paragraps used
121         // by it.
122         cached_bview = 0;
123         for (Cache::iterator cit = cache.begin(); cit != cache.end(); ++cit) {
124                 delete (*cit).second;
125                 (*cit).second = 0;
126         }
127
128         while (par) {
129                 Paragraph * tmp = par->next();
130                 delete par;
131                 par = tmp;
132         }
133 }
134
135
136 void InsetText::clear()
137 {
138         // delete all instances of LyXText before deleting the paragraps used
139         // by it.
140         cached_bview = 0;
141         for (Cache::iterator cit = cache.begin(); cit != cache.end(); ++cit) {
142                 delete (*cit).second;
143                 (*cit).second = 0;
144         }
145
146         while (par) {
147                 Paragraph * tmp = par->next();
148                 delete par;
149                 par = tmp;
150         }
151         par = new Paragraph;
152 }
153
154
155 Inset * InsetText::clone(Buffer const &) const
156 {
157         InsetText * t = new InsetText(*this);
158         return t;
159 }
160
161
162 void InsetText::write(Buffer const * buf, ostream & os) const
163 {
164         os << "Text\n";
165         writeParagraphData(buf, os);
166 }
167
168
169 void InsetText::writeParagraphData(Buffer const * buf, ostream & os) const
170 {
171         par->writeFile(buf, os, buf->params, 0);
172 }
173
174
175 void InsetText::read(Buffer const * buf, LyXLex & lex)
176 {
177         string token;
178         int pos = 0;
179         Paragraph * return_par = 0;
180         Paragraph::depth_type depth = 0; 
181         LyXFont font(LyXFont::ALL_INHERIT);
182
183         clear();
184         
185         while (lex.IsOK()) {
186                 lex.nextToken();
187                 token = lex.GetString();
188                 if (token.empty())
189                         continue;
190                 if (token == "\\end_inset")
191                         break;
192                 if (const_cast<Buffer*>(buf)->
193                         parseSingleLyXformat2Token(lex, par, return_par,
194                                                    token, pos, depth, font)) {
195                         // the_end read this should NEVER happen
196                         lex.printError("\\the_end read in inset! Error in document!");
197                         return;
198                 }
199         }
200         if (!return_par)
201                 return_par = par;
202         par = return_par;
203         while(return_par) {
204                 return_par->setInsetOwner(this);
205                 return_par = return_par->next();
206         }
207         
208         if (token != "\\end_inset") {
209                 lex.printError("Missing \\end_inset at this point. "
210                                            "Read: `$$Token'");
211         }
212         need_update = INIT;
213 }
214
215
216 int InsetText::ascent(BufferView * bv, LyXFont const &) const
217 {
218         int y_temp = 0;
219         Row * row = getLyXText(bv)->getRowNearY(y_temp);
220         insetAscent = row->ascent_of_text() + TEXT_TO_INSET_OFFSET;
221         return insetAscent;
222 }
223
224
225 int InsetText::descent(BufferView * bv, LyXFont const &) const
226 {
227                 LyXText * t = getLyXText(bv);
228         int y_temp = 0;
229         Row * row = t->getRowNearY(y_temp);
230         insetDescent = t->height - row->ascent_of_text() +
231         TEXT_TO_INSET_OFFSET;
232         return insetDescent;
233 }
234
235
236 int InsetText::width(BufferView * bv, LyXFont const &) const
237 {
238         insetWidth = max(textWidth(bv),
239                          (int)getLyXText(bv)->width + (2 * TEXT_TO_INSET_OFFSET));
240         return insetWidth;
241 }
242
243
244 int InsetText::textWidth(BufferView * bv) const
245 {
246         int const w = getMaxWidth(bv, this);
247         return w;
248 }
249
250
251 void InsetText::draw(BufferView * bv, LyXFont const & f,
252                      int baseline, float & x, bool cleared) const
253 {
254         if (nodraw())
255                 return;
256
257         Painter & pain = bv->painter();
258
259         // no draw is necessary !!!
260         if ((drawFrame_ == LOCKED) && !locked && !par->size()) {
261                 top_x = int(x);
262                 top_baseline = baseline;
263                 x += width(bv, f);
264                 if (!cleared && (need_update & CLEAR_FRAME))
265                         clearFrame(pain, cleared);
266                 else if (cleared)
267                         frame_is_visible = false;
268                 need_update = NONE;
269                 return;
270         }
271
272         xpos = x;
273 #if 0
274         UpdatableInset::draw(bv, f, baseline, x, cleared);
275 #else
276         if (!owner())
277                 x += static_cast<float>(scroll());
278 #endif
279 #if 0
280         // update insetWidth and insetHeight with dummy calls
281         (void)ascent(bv, f);
282         (void)descent(bv, f);
283         (void)width(bv, f);
284 #endif
285
286         // if top_x differs we have a rule down and we don't have to clear anything
287         if (!cleared && (top_x == int(x)) &&
288                 ((need_update&(INIT|FULL)) || (top_baseline!=baseline) ||
289                  (last_drawn_width!=insetWidth))) {
290                 clearInset(pain, baseline, cleared);
291         }
292         if (cleared)
293                 frame_is_visible = false;
294
295         if (!cleared && (need_update == NONE))
296                 return;
297
298         if (top_x != int(x)) {
299                 if ((getMaxWidth(bv, this) > 0) &&
300                         (getLyXText(bv)->width != old_max_width)) {
301                         resizeLyXText(bv);
302                         need_update |= FULL;
303                         old_max_width = getLyXText(bv)->width;
304                         bv->text->status = LyXText::CHANGED_IN_DRAW;
305                 }
306                 top_x = int(x);
307 #if 1
308                 clearInset(pain, baseline, cleared);
309 #else
310                 return;
311 #endif
312         }
313
314 //      lyxerr << "InsetText::draw[" << this << "](" << need_update << ":" << int(x) << ":" << top_x << ")\n";
315
316         if (cleared || (last_drawn_width != insetWidth)) {
317                 need_update |= FULL;
318                 last_drawn_width = insetWidth;
319         }
320
321         top_baseline = baseline;
322         top_y = baseline - ascent(bv, f);
323         last_width = width(bv, f);
324         last_height = ascent(bv, f) + descent(bv, f);
325
326         if (the_locking_inset && (cpar(bv) == inset_par)
327                 && (cpos(bv) == inset_pos)) {
328                 inset_x = cx(bv) - top_x + drawTextXOffset;
329                 inset_y = cy(bv) + drawTextYOffset;
330         }
331         if (!cleared && (need_update == CURSOR)
332                 && !getLyXText(bv)->selection.set()) {
333                 drawFrame(pain, cleared);
334                 x += last_width; // was width(bv, f);
335                 need_update = NONE;
336                 return;
337         }
338         x += TEXT_TO_INSET_OFFSET;
339
340         int y = 0;
341         Row * row = getLyXText(bv)->getRowNearY(y);
342         int y_offset = baseline - row->ascent_of_text();
343         int ph = pain.paperHeight();
344         int first = 0;
345         y = y_offset;
346         while ((row != 0) && ((y+row->height()) <= 0)) {
347                 y += row->height();
348                 first += row->height();
349                 row = row->next();
350         }
351         if (y_offset < 0)
352                 y_offset = y;
353         getLyXText(bv)->first = first;
354         if (cleared) {
355                 int yf = y_offset;
356                 y = 0;
357                 while ((row != 0) && (yf < ph)) {
358                         getLyXText(bv)->getVisibleRow(bv, y+y_offset, int(x), row,
359                                                 y+first, cleared);
360                         y += row->height();
361                         yf += row->height();
362                         row = row->next();
363                 }
364         } else if (!locked) {
365                 if (need_update & CURSOR) {
366                         bv->screen()->ToggleSelection(getLyXText(bv), bv, true, y_offset,int(x));
367                         getLyXText(bv)->clearSelection(bv);
368                         getLyXText(bv)->selection.cursor = getLyXText(bv)->cursor;
369                 }
370                 bv->screen()->Update(getLyXText(bv), bv, y_offset, int(x));
371         } else {
372                 locked = false;
373                 if (need_update & SELECTION)
374                         bv->screen()->ToggleToggle(getLyXText(bv), bv, y_offset, int(x));
375                 else if (need_update & CURSOR) {
376                         bv->screen()->ToggleSelection(getLyXText(bv), bv, true, y_offset,int(x));
377                         getLyXText(bv)->clearSelection(bv);
378                         getLyXText(bv)->selection.cursor = getLyXText(bv)->cursor;
379                 }
380                 bv->screen()->Update(getLyXText(bv), bv, y_offset, int(x));
381                 locked = true;
382         }
383
384         getLyXText(bv)->refresh_y = 0;
385         getLyXText(bv)->status = LyXText::UNCHANGED;
386         if ((need_update != CURSOR_PAR) &&
387                 ((drawFrame_ == ALWAYS) || ((drawFrame_ == LOCKED) && locked)))
388                 drawFrame(pain, cleared);
389         else if (need_update & CLEAR_FRAME)
390                 clearFrame(pain, cleared);
391         x += last_width /* was width(bv, f) */ - TEXT_TO_INSET_OFFSET;
392         if (bv->text->status==LyXText::CHANGED_IN_DRAW) {
393                 need_update |= INIT;
394         } else if (need_update != INIT)
395                 need_update = NONE;
396 }
397
398
399 void InsetText::drawFrame(Painter & pain, bool cleared) const
400 {
401         if (!frame_is_visible || cleared) {
402                 pain.rectangle(top_x + 1, top_baseline - insetAscent + 1,
403                                insetWidth - 1, insetAscent + insetDescent - 1,
404                                frame_color);
405                 frame_is_visible = true;
406         }
407 }
408
409
410 void InsetText::clearFrame(Painter & pain, bool cleared) const
411 {
412         if (frame_is_visible) {
413                 if (!cleared) {
414                         pain.rectangle(top_x + 1, top_baseline - insetAscent + 1,
415                                        insetWidth - 1, insetAscent + insetDescent - 1,
416                                        LColor::background);
417                 }
418                 frame_is_visible = false;
419         }
420 }
421
422
423 void InsetText::update(BufferView * bv, LyXFont const & font, bool reinit)
424 {
425                 LyXText * t = getLyXText(bv);
426                 
427 #if 0
428         int ww = t->width;
429         t->BreakParagraph(bv);
430         if (ww != t->width)
431                 reinit = true;
432 #endif
433         if (reinit) {
434                 need_update |= INIT;
435                 resizeLyXText(bv);
436                 if (owner())
437                         owner()->update(bv, font, true);
438                 return;
439         }
440         if (the_locking_inset) {
441                 inset_x = cx(bv) - top_x + drawTextXOffset;
442                 inset_y = cy(bv) + drawTextYOffset;
443                 the_locking_inset->update(bv, font, reinit);
444         }
445 #if 0
446         if (need_update == INIT) {
447                 resizeLyXText(bv);
448                 need_update |= FULL;
449         }
450 #endif
451         int oldw = insetWidth;
452         insetWidth = t->width + (2 * TEXT_TO_INSET_OFFSET);
453         if (oldw != insetWidth) {
454                 resizeLyXText(bv);
455                 need_update |= FULL;
456 //              update(bv, font, reinit);
457                 return;
458         }
459         if ((need_update & CURSOR_PAR) && t->status == LyXText::UNCHANGED &&
460                 the_locking_inset) {
461                 t->updateInset(bv, the_locking_inset);
462         }
463         
464         if (t->status == LyXText::NEED_MORE_REFRESH)
465                 need_update |= FULL;
466 #if 0
467         int y_temp = 0;
468         Row * row = t->GetRowNearY(y_temp);
469         insetAscent = row->ascent_of_text() + TEXT_TO_INSET_OFFSET;
470         insetDescent = t->height - row->ascent_of_text() + TEXT_TO_INSET_OFFSET;
471 #endif
472 }
473
474
475 void InsetText::setUpdateStatus(BufferView * bv, int what) const
476 {
477         LyXText * t = getLyXText(bv);
478         need_update |= what;
479         if (t->status == LyXText::NEED_MORE_REFRESH)
480                 need_update |= FULL;
481         else if (t->status == LyXText::NEED_VERY_LITTLE_REFRESH)
482                 need_update |= CURSOR_PAR;
483
484         // this to not draw a selection when we redraw all of it!
485         if ((need_update & (INIT|FULL)) && (need_update & CURSOR))
486                 t->clearSelection(bv);
487 }
488
489
490 void InsetText::updateLocal(BufferView * bv, int what, bool mark_dirty)
491 {
492                 LyXText * t = getLyXText(bv);
493         t->fullRebreak(bv);
494         setUpdateStatus(bv, what);
495         if (need_update != CURSOR || t->status != LyXText::UNCHANGED ||
496                 t->selection.set())
497                 bv->updateInset(this, mark_dirty);
498         bv->owner()->showState();
499         if (old_par != cpar(bv)) {
500                 bv->owner()->setLayout(cpar(bv)->getLayout());
501                 old_par = cpar(bv);
502         }
503 }
504
505
506 string const InsetText::editMessage() const
507 {
508         return _("Opened Text Inset");
509 }
510
511
512 void InsetText::edit(BufferView * bv, int x, int y, unsigned int button)
513 {
514 //    par->SetInsetOwner(this);
515         UpdatableInset::edit(bv, x, y, button);
516         
517         if (!bv->lockInset(this)) {
518                 lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
519                 return;
520         }
521         locked = true;
522         the_locking_inset = 0;
523         inset_pos = inset_x = inset_y = 0;
524         inset_boundary = false;
525         inset_par = 0;
526         old_par = 0;
527         int tmp_y = (y < 0) ? 0 : y;
528         LyXText * t = getLyXText(bv);
529
530         if (!checkAndActivateInset(bv, x, tmp_y, button)) {
531                 t->setCursorFromCoordinates(bv, x - drawTextXOffset,
532                                                                         y + insetAscent);
533         }
534         
535         t->selection.cursor = t->cursor;
536         bv->text->finishUndo();
537         showInsetCursor(bv);
538         updateLocal(bv, CURSOR, false);
539
540         // If the inset is empty set the language of the current font to the
541         // language to the surronding text (if different).
542         if (par->size() == 0 && !par->next() &&
543                 bv->getParentLanguage(this) != t->current_font.language()) {
544                 LyXFont font(LyXFont::ALL_IGNORE);
545                 font.setLanguage(bv->getParentLanguage(this));
546                 setFont(bv, font, false);
547         }
548 }
549
550
551 void InsetText::insetUnlock(BufferView * bv)
552 {
553         if (the_locking_inset) {
554                 the_locking_inset->insetUnlock(bv);
555                 the_locking_inset = 0;
556         }
557         hideInsetCursor(bv);
558         no_selection = false;
559         locked = false;
560         int code = CURSOR|CLEAR_FRAME;
561         LyXText * t = getLyXText(bv);
562         if (t->selection.set()) {
563                 t->clearSelection(bv);
564                 code = FULL;
565         } else if (owner()) {
566                 bv->owner()->setLayout(owner()->getLyXText(bv)
567                                        ->cursor.par()->getLayout());
568         } else
569                 bv->owner()->setLayout(bv->text->cursor.par()->getLayout());
570         updateLocal(bv, code, false);
571 }
572
573
574 bool InsetText::lockInsetInInset(BufferView * bv, UpdatableInset * inset)
575 {
576         lyxerr[Debug::INSETS] << "InsetText::LockInsetInInset("
577                               << inset << "): ";
578         if (!inset)
579                 return false;
580         if (inset == cpar(bv)->getInset(cpos(bv))) {
581                 lyxerr[Debug::INSETS] << "OK" << endl;
582                 the_locking_inset = inset;
583                 inset_x = cx(bv) - top_x + drawTextXOffset;
584                 inset_y = cy(bv) + drawTextYOffset;
585                 inset_pos = cpos(bv);
586                 inset_par = cpar(bv);
587                 inset_boundary = cboundary(bv);
588 #if 0
589                 getLyXText(bv)->clearSelection(bv);
590                 getLyXText(bv)->sel_cursor = getLyXText(bv)->cursor;
591                 getLyXText(bv)->UpdateInset(bv, the_locking_inset);
592 #else
593                 updateLocal(bv, CURSOR, false);
594 #endif
595                 return true;
596         } else if (the_locking_inset && (the_locking_inset == inset)) {
597                 if (cpar(bv) == inset_par && cpos(bv) == inset_pos) {
598                         lyxerr[Debug::INSETS] << "OK" << endl;
599                         inset_x = cx(bv) - top_x + drawTextXOffset;
600                         inset_y = cy(bv) + drawTextYOffset;
601                 } else {
602                         lyxerr[Debug::INSETS] << "cursor.pos != inset_pos" << endl;
603                 }
604         } else if (the_locking_inset) {
605                 lyxerr[Debug::INSETS] << "MAYBE" << endl;
606                 return the_locking_inset->lockInsetInInset(bv, inset);
607         }
608         lyxerr[Debug::INSETS] << "NOT OK" << endl;
609         return false;
610 }
611
612
613 bool InsetText::unlockInsetInInset(BufferView * bv, UpdatableInset * inset,
614                                    bool lr)
615 {
616         if (!the_locking_inset)
617                 return false;
618         if (the_locking_inset == inset) {
619                 the_locking_inset->insetUnlock(bv);
620                 getLyXText(bv)->updateInset(bv, inset);
621                 the_locking_inset = 0;
622                 if (lr)
623                         moveRight(bv, false);
624                 old_par = 0; // force layout setting
625                 if (scroll())
626                         scroll(bv, 0.0F);
627                 else
628                         updateLocal(bv, CURSOR, false);
629                 return true;
630         }
631         return the_locking_inset->unlockInsetInInset(bv, inset, lr);
632 }
633
634
635 bool InsetText::updateInsetInInset(BufferView * bv, Inset * inset)
636 {
637         if (!the_locking_inset)
638                 return false;
639         if (the_locking_inset != inset) {
640                 getLyXText(bv)->updateInset(bv, the_locking_inset);
641                 setUpdateStatus(bv, CURSOR_PAR);
642                 return the_locking_inset->updateInsetInInset(bv, inset);
643         }
644 //    updateLocal(bv, FULL, false);
645         if (getLyXText(bv)->updateInset(bv, inset))
646                 updateLocal(bv, CURSOR_PAR, false);
647         if (cpar(bv) == inset_par && cpos(bv) == inset_pos) {
648                 inset_x = cx(bv) - top_x + drawTextXOffset;
649                 inset_y = cy(bv) + drawTextYOffset;
650         }
651         return true;
652 }
653
654
655 void InsetText::insetButtonPress(BufferView * bv, int x, int y, int button)
656 {
657         no_selection = true;
658
659         int tmp_x = x - drawTextXOffset;
660         int tmp_y = y + insetAscent - getLyXText(bv)->first;
661         Inset * inset = bv->checkInsetHit(getLyXText(bv), tmp_x, tmp_y, button);
662
663         hideInsetCursor(bv);
664         if (the_locking_inset) {
665                 if (the_locking_inset == inset) {
666                         the_locking_inset->insetButtonPress(bv,x-inset_x,y-inset_y,button);
667                         no_selection = false;
668                         return;
669                 } else if (inset) {
670                         // otherwise unlock the_locking_inset and lock the new inset
671                         the_locking_inset->insetUnlock(bv);
672                         inset_x = cx(bv) - top_x + drawTextXOffset;
673                         inset_y = cy(bv) + drawTextYOffset;
674                         the_locking_inset = static_cast<UpdatableInset*>(inset);
675                         inset->insetButtonPress(bv, x - inset_x, y - inset_y, button);
676                         inset->edit(bv, x - inset_x, y - inset_y, button);
677                         if (the_locking_inset)
678                                 updateLocal(bv, CURSOR, false);
679                         no_selection = false;
680                         return;
681                 }
682                 // otherwise only unlock the_locking_inset
683                 the_locking_inset->insetUnlock(bv);
684                 the_locking_inset = 0;
685         }
686         if (bv->theLockingInset()) {
687                 if (inset && inset->editable() == Inset::HIGHLY_EDITABLE) {
688                         UpdatableInset * uinset = static_cast<UpdatableInset*>(inset);
689                         inset_x = cx(bv) - top_x + drawTextXOffset;
690                         inset_y = cy(bv) + drawTextYOffset;
691                         inset_pos = cpos(bv);
692                         inset_par = cpar(bv);
693                         inset_boundary = cboundary(bv);
694                         the_locking_inset = uinset;
695                         uinset->insetButtonPress(bv, x - inset_x, y - inset_y,
696                                                  button);
697                         uinset->edit(bv, x - inset_x, y - inset_y, 0);
698                         if (the_locking_inset)
699                                 updateLocal(bv, CURSOR, false);
700                         no_selection = false;
701                         return;
702                 }
703         }
704         if (!inset) { // && (button == 2)) {
705                 bool paste_internally = false;
706                 if ((button == 2) && getLyXText(bv)->selection.set()) {
707                         localDispatch(bv, LFUN_COPY, "");
708                         paste_internally = true;
709                 }
710                 getLyXText(bv)->setCursorFromCoordinates(bv, x-drawTextXOffset,
711                                                    y + insetAscent);
712                 getLyXText(bv)->selection.cursor = getLyXText(bv)->cursor;
713                 updateLocal(bv, CURSOR, false);
714                 bv->owner()->setLayout(cpar(bv)->getLayout());
715                 old_par = cpar(bv);
716                 // Insert primary selection with middle mouse
717                 // if there is a local selection in the current buffer,
718                 // insert this
719                 if (button == 2) {
720                         if (paste_internally)
721                                 localDispatch(bv, LFUN_PASTE, "");
722                         else
723                                 localDispatch(bv, LFUN_PASTESELECTION,
724                                               "paragraph");
725                 }
726         }
727         showInsetCursor(bv);
728         no_selection = false;
729 }
730
731
732 void InsetText::insetButtonRelease(BufferView * bv, int x, int y, int button)
733 {
734         UpdatableInset * inset = 0;
735
736         if (the_locking_inset) {
737                 the_locking_inset->insetButtonRelease(bv,
738                                                       x - inset_x, y - inset_y,
739                                                       button);
740         } else {
741                 if (cpar(bv)->getChar(cpos(bv)) == Paragraph::META_INSET) {
742                         inset = static_cast<UpdatableInset*>(cpar(bv)->getInset(cpos(bv)));
743                         if (inset->editable() == Inset::HIGHLY_EDITABLE) {
744                                 inset->insetButtonRelease(bv,
745                                                           x - inset_x,
746                                                           y - inset_y, button);
747                         } else {
748                                 inset_x = cx(bv) - top_x + drawTextXOffset;
749                                 inset_y = cy(bv) + drawTextYOffset;
750                                 inset->insetButtonRelease(bv,
751                                                           x - inset_x,
752                                                           y - inset_y, button);
753                                 inset->edit(bv,
754                                             x - inset_x, y - inset_y, button);
755                         }
756                         updateLocal(bv, CURSOR_PAR, false);
757                 }
758         }
759         no_selection = false;
760 }
761
762
763 void InsetText::insetMotionNotify(BufferView * bv, int x, int y, int state)
764 {
765         if (no_selection)
766                 return;
767         if (the_locking_inset) {
768                 the_locking_inset->insetMotionNotify(bv, x - inset_x,
769                                                      y - inset_y,state);
770                 return;
771         }
772         LyXText * t = getLyXText(bv);
773         hideInsetCursor(bv);
774         t->setCursorFromCoordinates(bv, x - drawTextXOffset, y + insetAscent);
775         t->setSelection(bv);
776         if (t->toggle_cursor.par() != t->toggle_end_cursor.par() ||
777                 t->toggle_cursor.pos() != t->toggle_end_cursor.pos())
778                 updateLocal(bv, SELECTION, false);
779         showInsetCursor(bv);
780 }
781
782
783 void InsetText::insetKeyPress(XKeyEvent * xke)
784 {
785         if (the_locking_inset) {
786                 the_locking_inset->insetKeyPress(xke);
787                 return;
788         }
789 }
790
791
792 UpdatableInset::RESULT
793 InsetText::localDispatch(BufferView * bv,
794                          kb_action action, string const & arg)
795 {
796         no_selection = false;
797         UpdatableInset::RESULT
798                 result= UpdatableInset::localDispatch(bv, action, arg);
799         if (result != UNDISPATCHED) {
800                 return DISPATCHED;
801         }
802
803         result = DISPATCHED;
804         if ((action < 0) && arg.empty())
805                 return FINISHED;
806
807         if (the_locking_inset) {
808                 result = the_locking_inset->localDispatch(bv, action, arg);
809                 if (result == DISPATCHED_NOUPDATE)
810                         return result;
811                 else if (result == DISPATCHED) {
812                         updateLocal(bv, CURSOR_PAR, false);
813                         return result;
814                 } else if (result == FINISHED) {
815                         bool dispatched = false;
816                         switch (action) {
817                         case LFUN_UNKNOWN_ACTION:
818                         case LFUN_BREAKPARAGRAPH:
819                         case LFUN_BREAKLINE:
820                                 moveRightIntern(bv, false, false);
821                                 break;
822                         case LFUN_RIGHT:
823                                 if (!getLyXText(bv)->cursor.par()->isRightToLeftPar(bv->buffer()->params))
824                                         moveRightIntern(bv, false, false);
825                                 dispatched = true;
826                                 break;
827                         case LFUN_LEFT:
828                                 if (getLyXText(bv)->cursor.par()->isRightToLeftPar(bv->buffer()->params))
829                                         moveRightIntern(bv, false, false);
830                                 dispatched = true;
831                                 break;
832                         default:
833                                 break;
834                         }
835                         the_locking_inset = 0;
836                         if (dispatched)
837                                 return DISPATCHED;
838                 }
839         }
840         hideInsetCursor(bv);
841         switch (action) {
842         // Normal chars
843         case LFUN_SELFINSERT:
844                 if (bv->buffer()->isReadonly()) {
845 //          setErrorMessage(N_("Document is read only"));
846                         break;
847                 }
848                 if (!arg.empty()) {
849                         /* Automatically delete the currently selected
850                          * text and replace it with what is being
851                          * typed in now. Depends on lyxrc settings
852                          * "auto_region_delete", which defaults to
853                          * true (on). */
854
855                         bv->text->setUndo(bv->buffer(), Undo::INSERT,
856                                           bv->text->cursor.par()->previous(),
857                                           bv->text->cursor.par()->next());
858                         bv->setState();
859                         if (lyxrc.auto_region_delete) {
860                                 if (getLyXText(bv)->selection.set()) {
861                                         getLyXText(bv)->cutSelection(bv, false);
862                                 }
863                         }
864                         getLyXText(bv)->clearSelection(bv);
865                         for (string::size_type i = 0; i < arg.length(); ++i) {
866                                 if (greek_kb_flag) {
867                                         if (!math_insert_greek(bv, arg[i])) {
868                                                 bv->owner()->getIntl()->getTrans().TranslateAndInsert(arg[i], getLyXText(bv));
869                                         } else if (!the_locking_inset) {
870                                                 (void)moveRight(bv, false);
871                                         }
872                                 } else {
873                                         bv->owner()->getIntl()->getTrans().TranslateAndInsert(arg[i], getLyXText(bv));
874                                 }
875                         }
876                 }
877                 getLyXText(bv)->selection.cursor = getLyXText(bv)->cursor;
878                 updateLocal(bv, CURSOR_PAR, true);
879                 result = DISPATCHED_NOUPDATE;
880                 break;
881                 // --- Cursor Movements -----------------------------------
882         case LFUN_RIGHTSEL:
883                 bv->text->finishUndo();
884                 moveRight(bv, false, true);
885                 getLyXText(bv)->setSelection(bv);
886                 updateLocal(bv, SELECTION, false);
887                 break;
888         case LFUN_RIGHT:
889                 result = moveRight(bv);
890                 bv->text->finishUndo();
891                 updateLocal(bv, CURSOR, false);
892                 break;
893         case LFUN_LEFTSEL:
894                 bv->text->finishUndo();
895                 moveLeft(bv, false, true);
896                 getLyXText(bv)->setSelection(bv);
897                 updateLocal(bv, SELECTION, false);
898                 break;
899         case LFUN_LEFT:
900                 bv->text->finishUndo();
901                 result = moveLeft(bv);
902                 updateLocal(bv, CURSOR, false);
903                 break;
904         case LFUN_DOWNSEL:
905                 bv->text->finishUndo();
906                 moveDown(bv);
907                 getLyXText(bv)->setSelection(bv);
908                 updateLocal(bv, SELECTION, false);
909                 break;
910         case LFUN_DOWN:
911                 bv->text->finishUndo();
912                 result = moveDown(bv);
913                 updateLocal(bv, CURSOR, false);
914                 break;
915         case LFUN_UPSEL:
916                 bv->text->finishUndo();
917                 moveUp(bv);
918                 getLyXText(bv)->setSelection(bv);
919                 updateLocal(bv, SELECTION, false);
920                 break;
921         case LFUN_UP:
922                 bv->text->finishUndo();
923                 result = moveUp(bv);
924                 updateLocal(bv, CURSOR, false);
925                 break;
926         case LFUN_HOME:
927                 bv->text->finishUndo();
928                 getLyXText(bv)->cursorHome(bv);
929                 updateLocal(bv, CURSOR, false);
930                 break;
931         case LFUN_END:
932                 getLyXText(bv)->cursorEnd(bv);
933                 updateLocal(bv, CURSOR, false);
934                 break;
935         case LFUN_BACKSPACE: {
936                 bv->text->setUndo(bv->buffer(), Undo::DELETE,
937                                   bv->text->cursor.par()->previous(),
938                                   bv->text->cursor.par()->next());
939                 LyXText * t = getLyXText(bv);
940                 
941                 if (t->selection.set()) {
942                         t->cutSelection(bv);
943                 } else {
944                         t->backspace(bv);
945                 }
946                 
947                 updateLocal(bv, CURSOR_PAR, true);
948         }
949         break;
950         
951         case LFUN_DELETE:
952         {
953                 bv->text->setUndo(bv->buffer(), Undo::DELETE,
954                                   bv->text->cursor.par()->previous(),
955                                   bv->text->cursor.par()->next());
956                 LyXText * t = getLyXText(bv);
957                 
958                 if (t->selection.set()) {
959                         t->cutSelection(bv);
960                 } else {
961                         t->Delete(bv);
962                 }
963                 updateLocal(bv, CURSOR_PAR, true);
964         }
965         break;
966         
967         case LFUN_CUT:
968                 bv->text->setUndo(bv->buffer(), Undo::DELETE,
969                                   bv->text->cursor.par()->previous(),
970                                   bv->text->cursor.par()->next());
971                 getLyXText(bv)->cutSelection(bv);
972                 updateLocal(bv, CURSOR_PAR, true);
973                 break;
974         case LFUN_COPY:
975                 bv->text->finishUndo();
976                 getLyXText(bv)->copySelection(bv);
977                 updateLocal(bv, CURSOR_PAR, false);
978                 break;
979         case LFUN_PASTESELECTION:
980         {
981                 string const clip(bv->getClipboard());
982         
983                 if (clip.empty())
984                         break;
985                 if (arg == "paragraph") {
986                         getLyXText(bv)->insertStringAsParagraphs(bv, clip);
987                 } else {
988                         getLyXText(bv)->insertStringAsLines(bv, clip);
989                 }
990                 updateLocal(bv, CURSOR_PAR, true);
991                 break;
992         }
993         case LFUN_PASTE:
994                 if (!autoBreakRows) {
995                         CutAndPaste cap;
996
997                         if (cap.nrOfParagraphs() > 1) {
998                                 WriteAlert(_("Impossible operation"),
999                                                    _("Cannot include more than one paragraph!"),
1000                                                    _("Sorry."));
1001                                 break;
1002                         }
1003                 }
1004                 bv->text->setUndo(bv->buffer(), Undo::INSERT,
1005                                   bv->text->cursor.par()->previous(),
1006                                   bv->text->cursor.par()->next());
1007                 getLyXText(bv)->pasteSelection(bv);
1008                 updateLocal(bv, CURSOR_PAR, true);
1009                 break;
1010         case LFUN_BREAKPARAGRAPH:
1011                 if (!autoBreakRows)
1012                         return DISPATCHED;
1013                 getLyXText(bv)->breakParagraph(bv, 0);
1014                 updateLocal(bv, FULL, true);
1015                 break;
1016         case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
1017                 if (!autoBreakRows)
1018                         return DISPATCHED;
1019                 getLyXText(bv)->breakParagraph(bv, 1);
1020                 updateLocal(bv, FULL, true);
1021                 break;
1022         case LFUN_BREAKLINE:
1023                 if (!autoBreakRows)
1024                         return DISPATCHED;
1025                 bv->text->setUndo(bv->buffer(), Undo::INSERT,
1026                                   bv->text->cursor.par()->previous(),
1027                                   bv->text->cursor.par()->next());
1028                 getLyXText(bv)->insertChar(bv, Paragraph::META_NEWLINE);
1029                 updateLocal(bv, CURSOR_PAR, true);
1030                 break;
1031         case LFUN_LAYOUT:
1032                 // do not set layouts on non breakable textinsets
1033                 if (autoBreakRows) {
1034                         LyXTextClass::size_type cur_layout = cpar(bv)->layout;
1035           
1036                         // Derive layout number from given argument (string)
1037                         // and current buffer's textclass (number). */    
1038                         LyXTextClassList::ClassList::size_type tclass =
1039                                 bv->buffer()->params.textclass;
1040                         std::pair <bool, LyXTextClass::size_type> layout = 
1041                                 textclasslist.NumberOfLayout(tclass, arg);
1042
1043                         // If the entry is obsolete, use the new one instead.
1044                         if (layout.first) {
1045                                 string obs = textclasslist.Style(tclass,layout.second).
1046                                         obsoleted_by();
1047                                 if (!obs.empty()) 
1048                                         layout = textclasslist.NumberOfLayout(tclass, obs);
1049                         }
1050
1051                         // see if we found the layout number:
1052                         if (!layout.first) {
1053                                 string const msg = string(N_("Layout ")) + arg + N_(" not known");
1054                                 bv->owner()->getLyXFunc()->Dispatch(LFUN_MESSAGE, msg);
1055                                 break;
1056                         }
1057
1058                         if (cur_layout != layout.second) {
1059                                 cur_layout = layout.second;
1060                                 getLyXText(bv)->setLayout(bv, layout.second);
1061                                 bv->owner()->setLayout(cpar(bv)->getLayout());
1062                                 updateLocal(bv, CURSOR_PAR, true);
1063                         }
1064                 } else {
1065                         // reset the layout box
1066                         bv->owner()->setLayout(cpar(bv)->getLayout());
1067                 }
1068                 break;
1069         case LFUN_PARAGRAPH_SPACING:
1070                 // This one is absolutely not working. When fiddling with this
1071                 // it also seems to me that the paragraphs inside the insettext
1072                 // inherit bufferparams/paragraphparams in a strange way. (Lgb)
1073         {
1074                 Paragraph * par = getLyXText(bv)->cursor.par();
1075                 Spacing::Space cur_spacing = par->params().spacing().getSpace();
1076                 float cur_value = 1.0;
1077                 if (cur_spacing == Spacing::Other) {
1078                         cur_value = par->params().spacing().getValue();
1079                 }
1080                                 
1081                 std::istringstream istr(arg.c_str());
1082                 string tmp;
1083                 istr >> tmp;
1084                 Spacing::Space new_spacing = cur_spacing;
1085                 float new_value = cur_value;
1086                 if (tmp.empty()) {
1087                         lyxerr << "Missing argument to `paragraph-spacing'"
1088                                    << endl;
1089                 } else if (tmp == "single") {
1090                         new_spacing = Spacing::Single;
1091                 } else if (tmp == "onehalf") {
1092                         new_spacing = Spacing::Onehalf;
1093                 } else if (tmp == "double") {
1094                         new_spacing = Spacing::Double;
1095                 } else if (tmp == "other") {
1096                         new_spacing = Spacing::Other;
1097                         float tmpval = 0.0;
1098                         istr >> tmpval;
1099                         lyxerr << "new_value = " << tmpval << endl;
1100                         if (tmpval != 0.0)
1101                                 new_value = tmpval;
1102                 } else if (tmp == "default") {
1103                         new_spacing = Spacing::Default;
1104                 } else {
1105                         lyxerr << _("Unknown spacing argument: ")
1106                                    << arg << endl;
1107                 }
1108                 if (cur_spacing != new_spacing || cur_value != new_value) {
1109                         par->params().spacing(Spacing(new_spacing, new_value));
1110                         //getLyXText(bv)->RedoParagraph(owner->view());
1111                         updateLocal(bv, CURSOR_PAR, true);
1112                         //bv->update(BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
1113                 }
1114         }
1115         break;
1116         
1117         default:
1118                 if (!bv->Dispatch(action, arg))
1119                         result = UNDISPATCHED;
1120                 break;
1121         }
1122
1123         /// If the action has deleted all text in the inset, we need to change the
1124         // language to the language to the surronding text.
1125         if (par->size() == 0 && !par->next()) {
1126                 LyXFont font(LyXFont::ALL_IGNORE);
1127                 font.setLanguage(bv->getParentLanguage(this));
1128                 setFont(bv, font, false);
1129         }
1130
1131         if (result != FINISHED) {
1132                 showInsetCursor(bv);
1133         } else
1134                 bv->unlockInset(this);
1135         return result;
1136 }
1137
1138
1139 int InsetText::latex(Buffer const * buf, ostream & os, bool, bool) const
1140 {
1141         TexRow texrow;
1142         buf->latexParagraphs(os, par, 0, texrow);
1143         return texrow.rows();
1144 }
1145
1146
1147 int InsetText::ascii(Buffer const * buf, ostream & os, int linelen) const
1148 {
1149         Paragraph * p = par;
1150         unsigned int lines = 0;
1151         
1152         while (p) {
1153                 string const tmp = buf->asciiParagraph(p, linelen);
1154                 lines += countChar(tmp, '\n');
1155                 os << tmp;
1156                 p = p->next();
1157         }
1158         return lines;
1159 }
1160
1161
1162 int InsetText::docBook(Buffer const * buf, ostream & os) const
1163 {
1164         Paragraph * p = par;
1165         unsigned int lines = 0;
1166         int desc = 0;
1167         
1168         string tmp;
1169         while (p) {
1170                 buf->simpleDocBookOnePar(os, tmp, p, desc, 0);
1171                 p = p->next();
1172         }
1173         
1174         return lines;
1175 }
1176
1177
1178 void InsetText::validate(LaTeXFeatures & features) const
1179 {
1180         Paragraph * p = par;
1181         while (p) {
1182                 p->validate(features);
1183                 p = p->next();
1184         }
1185 }
1186
1187
1188 int InsetText::beginningOfMainBody(Buffer const * buf, Paragraph * p) const
1189 {
1190         if (textclasslist.Style(buf->params.textclass,
1191                                 p->getLayout()).labeltype != LABEL_MANUAL)
1192                 return 0;
1193         else
1194                 return p->beginningOfMainBody();
1195 }
1196
1197
1198 void InsetText::getCursorPos(BufferView * bv,
1199                              int & x, int & y) const
1200 {
1201         x = cx(bv);
1202         y = cy(bv);
1203 }
1204
1205
1206 unsigned int InsetText::insetInInsetY()
1207 {
1208         if (!the_locking_inset)
1209                 return 0;
1210
1211         return (inset_y + the_locking_inset->insetInInsetY());
1212 }
1213
1214
1215 void InsetText::toggleInsetCursor(BufferView * bv)
1216 {
1217         if (the_locking_inset) {
1218                 the_locking_inset->toggleInsetCursor(bv);
1219                 return;
1220         }
1221
1222         LyXFont const font(getLyXText(bv)->getFont(bv->buffer(), cpar(bv), cpos(bv)));
1223
1224         int const asc = lyxfont::maxAscent(font);
1225         int const desc = lyxfont::maxDescent(font);
1226   
1227         if (isCursorVisible())
1228                 bv->hideLockedInsetCursor();
1229         else
1230                 bv->showLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1231         toggleCursorVisible();
1232 }
1233
1234
1235 void InsetText::showInsetCursor(BufferView * bv, bool show)
1236 {
1237         if (the_locking_inset) {
1238                 the_locking_inset->showInsetCursor(bv);
1239                 return;
1240         }
1241         if (!isCursorVisible()) {
1242                 LyXFont const font =
1243                         getLyXText(bv)->getFont(bv->buffer(), cpar(bv), cpos(bv));
1244         
1245                 int const asc = lyxfont::maxAscent(font);
1246                 int const desc = lyxfont::maxDescent(font);
1247
1248                 bv->fitLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1249                 if (show)
1250                         bv->showLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1251                 setCursorVisible(true);
1252         }
1253 }
1254
1255
1256 void InsetText::hideInsetCursor(BufferView * bv)
1257 {
1258         if (isCursorVisible()) {
1259                 bv->hideLockedInsetCursor();
1260                 setCursorVisible(false);
1261         }
1262         if (the_locking_inset)
1263                 the_locking_inset->hideInsetCursor(bv);
1264 }
1265
1266
1267 UpdatableInset::RESULT
1268 InsetText::moveRight(BufferView * bv, bool activate_inset, bool selecting)
1269 {
1270         if (getLyXText(bv)->cursor.par()->isRightToLeftPar(bv->buffer()->params))
1271                 return moveLeftIntern(bv, false, activate_inset, selecting);
1272         else
1273                 return moveRightIntern(bv, false, activate_inset, selecting);
1274 }
1275
1276
1277 UpdatableInset::RESULT
1278 InsetText::moveLeft(BufferView * bv, bool activate_inset, bool selecting)
1279 {
1280         if (getLyXText(bv)->cursor.par()->isRightToLeftPar(bv->buffer()->params))
1281                 return moveRightIntern(bv, true, activate_inset, selecting);
1282         else
1283                 return moveLeftIntern(bv, true, activate_inset, selecting);
1284 }
1285
1286
1287 UpdatableInset::RESULT
1288 InsetText::moveRightIntern(BufferView * bv, bool behind, 
1289                            bool activate_inset, bool selecting)
1290 {
1291         if (!cpar(bv)->next() && (cpos(bv) >= cpar(bv)->size()))
1292                 return FINISHED;
1293         if (activate_inset && checkAndActivateInset(bv, behind))
1294                 return DISPATCHED;
1295         getLyXText(bv)->cursorRight(bv);
1296         if (!selecting)
1297                 getLyXText(bv)->selection.cursor = getLyXText(bv)->cursor;
1298         return DISPATCHED_NOUPDATE;
1299 }
1300
1301
1302 UpdatableInset::RESULT
1303 InsetText::moveLeftIntern(BufferView * bv, bool behind,
1304                           bool activate_inset, bool selecting)
1305 {
1306         if (!cpar(bv)->previous() && (cpos(bv) <= 0))
1307                 return FINISHED;
1308         getLyXText(bv)->cursorLeft(bv);
1309         if (!selecting)
1310                 getLyXText(bv)->selection.cursor = getLyXText(bv)->cursor;
1311         if (activate_inset && checkAndActivateInset(bv, behind))
1312                 return DISPATCHED;
1313         return DISPATCHED_NOUPDATE;
1314 }
1315
1316
1317 UpdatableInset::RESULT
1318 InsetText::moveUp(BufferView * bv)
1319 {
1320         if (!crow(bv)->previous())
1321                 return FINISHED;
1322         getLyXText(bv)->cursorUp(bv);
1323         return DISPATCHED_NOUPDATE;
1324 }
1325
1326
1327 UpdatableInset::RESULT
1328 InsetText::moveDown(BufferView * bv)
1329 {
1330         if (!crow(bv)->next())
1331                 return FINISHED;
1332         getLyXText(bv)->cursorDown(bv);
1333         return DISPATCHED_NOUPDATE;
1334 }
1335
1336
1337 bool InsetText::insertInset(BufferView * bv, Inset * inset)
1338 {
1339         if (the_locking_inset) {
1340                 if (the_locking_inset->insertInsetAllowed(inset))
1341                         return the_locking_inset->insertInset(bv, inset);
1342                 return false;
1343         }
1344         bv->text->setUndo(bv->buffer(), Undo::INSERT,
1345                           bv->text->cursor.par()->previous(),
1346                           bv->text->cursor.par()->next());
1347         inset->setOwner(this);
1348         hideInsetCursor(bv);
1349         getLyXText(bv)->insertInset(bv, inset);
1350 #if 0
1351         if ((cpar(bv)->GetChar(cpos(bv)) != Paragraph::META_INSET) ||
1352                 (cpar(bv)->GetInset(cpos(bv)) != inset))
1353                 getLyXText(bv)->CursorLeft(bv);
1354 #endif
1355         bv->fitCursor(getLyXText(bv));
1356         updateLocal(bv, CURSOR_PAR|CURSOR, true);
1357         showInsetCursor(bv);
1358         return true;
1359 }
1360
1361
1362 UpdatableInset * InsetText::getLockingInset()
1363 {
1364         return the_locking_inset ? the_locking_inset->getLockingInset() : this;
1365 }
1366
1367
1368 UpdatableInset * InsetText::getFirstLockingInsetOfType(Inset::Code c)
1369 {
1370         if (c == lyxCode())
1371                 return this;
1372         if (the_locking_inset)
1373                 return the_locking_inset->getFirstLockingInsetOfType(c);
1374         return 0;
1375 }
1376
1377
1378 bool InsetText::showInsetDialog(BufferView * bv) const
1379 {
1380         if (the_locking_inset)
1381                 return the_locking_inset->showInsetDialog(bv);
1382         return false;
1383 }
1384
1385
1386 std::vector<string> const InsetText::getLabelList() const 
1387 {
1388         std::vector<string> label_list;
1389
1390         Paragraph * tpar = par;
1391         while (tpar) {
1392                 Paragraph::inset_iterator beg = tpar->inset_iterator_begin();
1393                 Paragraph::inset_iterator end = tpar->inset_iterator_end();
1394                 for (; beg != end; ++beg) {
1395                         std::vector<string> const l = (*beg)->getLabelList();
1396                         label_list.insert(label_list.end(), l.begin(), l.end());
1397                 }
1398                 tpar = tpar->next();
1399         }
1400         return label_list;
1401 }
1402
1403
1404 void InsetText::setFont(BufferView * bv, LyXFont const & font, bool toggleall,
1405                         bool selectall)
1406 {
1407         if (the_locking_inset) {
1408                 the_locking_inset->setFont(bv, font, toggleall, selectall);
1409                 return;
1410         }
1411         if (getLyXText(bv)->selection.set()) {
1412                 bv->text->setUndo(bv->buffer(), Undo::EDIT,
1413                                   bv->text->cursor.par()->previous(),
1414                                   bv->text->cursor.par()->next());
1415         }
1416         if (selectall)
1417                 selectAll(bv);
1418         getLyXText(bv)->setFont(bv, font, toggleall);
1419         if (selectall)
1420                 getLyXText(bv)->clearSelection(bv);
1421         bv->fitCursor(getLyXText(bv));
1422         if (selectall || getLyXText(bv)->selection.set())
1423                 updateLocal(bv, FULL, true);
1424         else
1425                 updateLocal(bv, CURSOR_PAR, true);
1426 }
1427
1428
1429 bool InsetText::checkAndActivateInset(BufferView * bv, bool behind)
1430 {
1431         if (cpar(bv)->getChar(cpos(bv)) == Paragraph::META_INSET) {
1432                 unsigned int x;
1433                 unsigned int y;
1434                 Inset * inset =
1435                         static_cast<UpdatableInset*>(cpar(bv)->getInset(cpos(bv)));
1436                 if (!inset || inset->editable() != Inset::HIGHLY_EDITABLE)
1437                         return false;
1438                 LyXFont const font =
1439                         getLyXText(bv)->getFont(bv->buffer(), cpar(bv), cpos(bv));
1440                 if (behind) {
1441                         x = inset->width(bv, font);
1442                         y = font.isRightToLeft() ? 0 : inset->descent(bv, font);
1443                 } else {
1444                         x = 0;
1445                         y = font.isRightToLeft() ? inset->descent(bv, font) : 0;
1446                 }
1447                 //inset_x = cx(bv) - top_x + drawTextXOffset;
1448                 //inset_y = cy(bv) + drawTextYOffset;
1449                 inset->edit(bv, x, y, 0);
1450                 if (!the_locking_inset)
1451                         return false;
1452                 updateLocal(bv, CURSOR, false);
1453                 return true;
1454         }
1455         return false;
1456 }
1457
1458
1459 bool InsetText::checkAndActivateInset(BufferView * bv, int x, int y,
1460                                       int button)
1461 {
1462         x -= drawTextXOffset;
1463         int dummyx = x;
1464         int dummyy = y + insetAscent;
1465         Inset * inset = bv->checkInsetHit(getLyXText(bv), dummyx, dummyy, button);
1466
1467         if (inset) {
1468                 if (x < 0)
1469                         x = insetWidth;
1470                 if (y < 0)
1471                         y = insetDescent;
1472                 inset_x = cx(bv) - top_x + drawTextXOffset;
1473                 inset_y = cy(bv) + drawTextYOffset;
1474                 inset->edit(bv, x - inset_x, y - inset_y, button);
1475                 if (!the_locking_inset)
1476                         return false;
1477                 updateLocal(bv, CURSOR, false);
1478                 return true;
1479         }
1480         return false;
1481 }
1482
1483
1484 int InsetText::getMaxWidth(BufferView * bv, UpdatableInset const * inset) const
1485 {
1486         int w = UpdatableInset::getMaxWidth(bv, inset);
1487         if (w < 0) {
1488                 return w;
1489         }
1490         if (owner()) {
1491                 w = w - top_x + owner()->x();
1492                 return w;
1493         }
1494         w -= (2 * TEXT_TO_INSET_OFFSET);
1495         return w - top_x;
1496 //    return  w - (2*TEXT_TO_INSET_OFFSET);
1497 }
1498
1499
1500 void InsetText::setParagraphData(Paragraph * p)
1501 {
1502         // delete all instances of LyXText before deleting the paragraps used
1503         // by it.
1504         cached_bview = 0;
1505         for (Cache::iterator cit = cache.begin(); cit != cache.end(); ++cit){
1506                 delete (*cit).second;
1507                 (*cit).second = 0;
1508         }
1509
1510         while (par) {
1511                 Paragraph * tmp = par->next();
1512                 delete par;
1513                 par = tmp;
1514         }
1515
1516         par = new Paragraph(*p);
1517         par->setInsetOwner(this);
1518         Paragraph * np = par;
1519         while (p->next()) {
1520                 p = p->next();
1521                 np->next(new Paragraph(*p));
1522                 np->next()->previous(np);
1523                 np = np->next();
1524                 np->setInsetOwner(this);
1525         }
1526
1527         need_update = INIT;
1528 }
1529
1530
1531 void InsetText::setText(string const & data)
1532 {
1533         clear();
1534         LyXFont font(LyXFont::ALL_SANE);
1535         for (unsigned int i=0; i < data.length(); ++i)
1536                 par->insertChar(i, data[i], font);
1537 }
1538
1539
1540 void InsetText::setAutoBreakRows(bool flag)
1541 {
1542         if (flag != autoBreakRows) {
1543                 autoBreakRows = flag;
1544                 need_update = FULL;
1545                 if (!flag)
1546                         removeNewlines();
1547         }
1548 }
1549
1550
1551 void InsetText::setDrawFrame(BufferView * bv, DrawFrame how)
1552 {
1553         if (how != drawFrame_) {
1554                 drawFrame_ = how;
1555                 if (bv)
1556                         updateLocal(bv, DRAW_FRAME, false);
1557         }
1558 }
1559
1560
1561 void InsetText::setFrameColor(BufferView * bv, LColor::color col)
1562 {
1563         if (frame_color != col) {
1564                 frame_color = col;
1565                 if (bv)
1566                         updateLocal(bv, DRAW_FRAME, false);
1567         }
1568 }
1569
1570
1571 int InsetText::cx(BufferView * bv) const
1572 {
1573         LyXText * text = getLyXText(bv);
1574         int x = text->cursor.x() + top_x + TEXT_TO_INSET_OFFSET;
1575         if (the_locking_inset) {
1576                 LyXFont font = text->getFont(bv->buffer(),
1577                                              text->cursor.par(),
1578                                              text->cursor.pos());
1579                 if (font.isVisibleRightToLeft())
1580                         x -= the_locking_inset->width(bv, font);
1581         }
1582         return x;
1583 }
1584
1585
1586 int InsetText::cy(BufferView * bv) const
1587 {
1588         LyXFont font;
1589         return getLyXText(bv)->cursor.y() - ascent(bv, font) + TEXT_TO_INSET_OFFSET;
1590 }
1591
1592
1593 Paragraph::size_type InsetText::cpos(BufferView * bv) const
1594 {
1595         return getLyXText(bv)->cursor.pos();
1596 }
1597
1598
1599 Paragraph * InsetText::cpar(BufferView * bv) const
1600 {
1601         return getLyXText(bv)->cursor.par();
1602 }
1603
1604
1605 bool InsetText::cboundary(BufferView * bv) const
1606 {
1607         return getLyXText(bv)->cursor.boundary();
1608 }
1609
1610
1611 Row * InsetText::crow(BufferView * bv) const
1612 {
1613         return getLyXText(bv)->cursor.row();
1614 }
1615
1616
1617 LyXText * InsetText::getLyXText(BufferView const * lbv,
1618                                 bool const recursive) const
1619 {
1620         if (!recursive && (cached_bview == lbv))
1621                 return cached_text;
1622         
1623         // Super UGLY! (Lgb)
1624         BufferView * bv = const_cast<BufferView *>(lbv);
1625         
1626         cached_bview = bv;
1627         Cache::iterator it = cache.find(bv);
1628         
1629         if (it != cache.end()) {
1630                 lyx::Assert(it->second);
1631                         
1632                 cached_text = it->second;
1633                 if (recursive && the_locking_inset) {
1634                         return the_locking_inset->getLyXText(bv);
1635                 }
1636                 return cached_text;
1637         }
1638         cached_text = new LyXText(const_cast<InsetText *>(this));
1639         cached_text->init(bv);
1640
1641         cache.insert(make_pair(bv, cached_text));
1642         
1643         if (the_locking_inset) {
1644                 cached_text->setCursor(bv, inset_par, inset_pos,
1645                                                            true, inset_boundary);
1646                 if (recursive) {
1647                         return the_locking_inset->getLyXText(bv);
1648                 }
1649         }
1650         return cached_text;
1651 }
1652
1653
1654 void InsetText::deleteLyXText(BufferView * bv, bool recursive) const
1655 {
1656         cached_bview = 0;
1657
1658         Cache::iterator it = cache.find(bv);
1659         
1660         if (it == cache.end()) {
1661                 return;
1662         }
1663
1664         lyx::Assert(it->second);
1665         
1666         delete it->second;
1667         cache.erase(bv);
1668         if (recursive) {
1669                 /// then remove all LyXText in text-insets
1670                 Paragraph * p = par;
1671                 for (; p; p = p->next()) {
1672                         p->deleteInsetsLyXText(bv);
1673                 }
1674         }
1675 }
1676
1677
1678 void InsetText::resizeLyXText(BufferView * bv, bool force) const
1679 {
1680         if (!par->next() && !par->size()) // no data, resize not neccessary!
1681                 return;
1682         // one endless line, resize normally not necessary
1683         if (!force && getMaxWidth(bv, this) < 0)
1684                 return;
1685
1686         Cache::iterator it = cache.find(bv);
1687         if (it == cache.end()) {
1688                 return;
1689         }
1690         lyx::Assert(it->second);
1691         
1692         Paragraph * lpar = 0;
1693         Paragraph * selstartpar = 0;
1694         Paragraph * selendpar = 0;
1695         Paragraph::size_type pos = 0;
1696         Paragraph::size_type selstartpos = 0;
1697         Paragraph::size_type selendpos = 0;
1698         bool boundary = false;
1699         bool selstartboundary = false;
1700         bool selendboundary = false;
1701         bool selection = false;
1702         bool mark_set = false;
1703
1704 //    ProhibitInput(bv);
1705
1706         if (locked) {
1707                 LyXText * t = getLyXText(bv);
1708                 lpar = t->cursor.par();
1709                 pos = t->cursor.pos();
1710                 boundary = t->cursor.boundary();
1711                 selstartpar = t->selection.start.par();
1712                 selstartpos = t->selection.start.pos();
1713                 selstartboundary = t->selection.start.boundary();
1714                 selendpar = t->selection.end.par();
1715                 selendpos = t->selection.end.pos();
1716                 selendboundary = t->selection.end.boundary();
1717                 selection = t->selection.set();
1718                 mark_set = t->selection.mark();
1719         }
1720         deleteLyXText(bv, (the_locking_inset == 0) || force);
1721
1722         if (lpar) {
1723                 LyXText * t = getLyXText(bv);
1724                         
1725                 t->selection.set(true);
1726                 /* at this point just to avoid the Delete-Empty-Paragraph
1727                  * Mechanism when setting the cursor */
1728                 t->selection.mark(mark_set);
1729                 if (selection) {
1730                         t->setCursor(bv, selstartpar, selstartpos, true, selstartboundary);
1731                         t->selection.cursor = t->cursor;
1732                         t->setCursor(bv, selendpar, selendpos, true, selendboundary);
1733                         t->setSelection(bv);
1734                         t->setCursor(bv, lpar, pos);
1735                 } else {
1736                         t->setCursor(bv, lpar, pos, true, boundary);
1737                         t->selection.cursor = t->cursor;
1738                         t->selection.set(false);
1739                 }
1740         }
1741         if (bv->screen()) {
1742                         LyXText * t = getLyXText(bv);
1743                         t->first = bv->screen()->TopCursorVisible(t);
1744         }
1745         
1746         // this will scroll the screen such that the cursor becomes visible 
1747         bv->updateScrollbar();
1748 //    AllowInput(bv);
1749         if (the_locking_inset) {
1750                 /// then resize all LyXText in text-insets
1751                 inset_x = cx(bv) - top_x + drawTextXOffset;
1752                 inset_y = cy(bv) + drawTextYOffset;
1753                 for (Paragraph * p = par; p; p = p->next()) {
1754                         p->resizeInsetsLyXText(bv);
1755                 }
1756         }
1757         need_update = FULL;
1758 }
1759
1760
1761 void InsetText::removeNewlines()
1762 {
1763         for (Paragraph * p = par; p; p = p->next()) {
1764                 for (int i = 0; i < p->size(); ++i) {
1765                         if (p->getChar(i) == Paragraph::META_NEWLINE)
1766                                 p->erase(i);
1767                 }
1768         }
1769 }
1770
1771
1772 bool InsetText::nodraw() const
1773 {
1774         if (the_locking_inset)
1775                 return the_locking_inset->nodraw();
1776         return UpdatableInset::nodraw();
1777 }
1778
1779
1780 int InsetText::scroll(bool recursive) const
1781 {
1782         int sx = UpdatableInset::scroll(false);
1783
1784         if (recursive && the_locking_inset)
1785                 sx += the_locking_inset->scroll(recursive);
1786
1787         return sx;
1788 }
1789
1790
1791 bool InsetText::doClearArea() const
1792 {
1793         return !locked || (need_update & (FULL|INIT));
1794 }
1795
1796
1797 void InsetText::selectAll(BufferView * bv)
1798 {
1799         getLyXText(bv)->cursorTop(bv);
1800         getLyXText(bv)->selection.cursor = getLyXText(bv)->cursor;
1801         getLyXText(bv)->cursorBottom(bv);
1802         getLyXText(bv)->setSelection(bv);
1803 }
1804
1805
1806 void InsetText::clearSelection(BufferView * bv)
1807 {
1808         getLyXText(bv)->clearSelection(bv);
1809 }
1810
1811
1812 void InsetText::clearInset(Painter & pain, int baseline, bool & cleared) const
1813 {
1814         int w =  insetWidth;
1815         int h = insetAscent + insetDescent;
1816         int ty = baseline - insetAscent;
1817         
1818         if (ty < 0) {
1819                 h += ty;
1820                 ty = 0;
1821         }
1822         if ((ty + h) > pain.paperHeight())
1823                 h = pain.paperHeight();
1824         if ((top_x + drawTextXOffset + w) > pain.paperWidth())
1825                 w = pain.paperWidth();
1826         pain.fillRectangle(top_x+drawTextXOffset, ty, w, h);
1827         cleared = true;
1828         need_update = FULL;
1829 }
1830 /* Emacs:
1831  * Local variables:
1832  * tab-width: 4
1833  * End:
1834  * vi:set tabstop=4:
1835  */