]> git.lyx.org Git - lyx.git/blob - src/insets/insettext.C
23913b27a4a62baae813e5a6a0312520c288120c
[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 using std::make_pair;
58
59 extern unsigned char getCurrentTextClass(Buffer *);
60 extern bool math_insert_greek(BufferView *, char);
61 extern int greek_kb_flag;
62
63 InsetText::InsetText()
64 {
65         par = new Paragraph;
66         init();
67 }
68
69
70 InsetText::InsetText(InsetText const & ins)
71         : UpdatableInset()
72 {
73         par = 0;
74         init(&ins);
75         autoBreakRows = ins.autoBreakRows;
76 }
77
78
79 InsetText & InsetText::operator=(InsetText const & it)
80 {
81         init(&it);
82         autoBreakRows = it.autoBreakRows;
83         return * this;
84 }
85
86
87 void InsetText::init(InsetText const * ins)
88 {
89         top_y = 0;
90         last_width = 0;
91         last_height = 0;
92         insetAscent = 0;
93         insetDescent = 0;
94         insetWidth = 0;
95         the_locking_inset = 0;
96         old_max_width = 0;
97         no_selection = false;
98         need_update = INIT;
99         drawTextXOffset = 0;
100         drawTextYOffset = 0;
101         autoBreakRows = false;
102         drawFrame_ = NEVER;
103         xpos = 0.0;
104         if (ins) {
105                 setParagraphData(ins->par);
106                 autoBreakRows = ins->autoBreakRows;
107                 drawFrame_ = ins->drawFrame_;
108         }
109         par->setInsetOwner(this);
110         frame_color = LColor::insetframe;
111         locked = false;
112         old_par = 0;
113         last_drawn_width = -1;
114         frame_is_visible = false;
115         cached_bview = 0;
116 }
117
118
119 InsetText::~InsetText()
120 {
121         // delete all instances of LyXText before deleting the paragraps used
122         // by it.
123         cached_bview = 0;
124         for (Cache::iterator cit = cache.begin(); cit != cache.end(); ++cit) {
125                 delete (*cit).second;
126                 (*cit).second = 0;
127         }
128
129         while (par) {
130                 Paragraph * tmp = par->next();
131                 delete par;
132                 par = tmp;
133         }
134 }
135
136
137 void InsetText::clear()
138 {
139         // delete all instances of LyXText before deleting the paragraps used
140         // by it.
141         cached_bview = 0;
142         for (Cache::iterator cit = cache.begin(); cit != cache.end(); ++cit) {
143                 delete (*cit).second;
144                 (*cit).second = 0;
145         }
146
147         while (par) {
148                 Paragraph * tmp = par->next();
149                 delete par;
150                 par = tmp;
151         }
152         par = new Paragraph;
153 }
154
155
156 Inset * InsetText::clone(Buffer const &) const
157 {
158         InsetText * t = new InsetText(*this);
159         return t;
160 }
161
162
163 void InsetText::write(Buffer const * buf, ostream & os) const
164 {
165         os << "Text\n";
166         writeParagraphData(buf, os);
167 }
168
169
170 void InsetText::writeParagraphData(Buffer const * buf, ostream & os) const
171 {
172         par->writeFile(buf, os, buf->params, 0);
173 }
174
175
176 void InsetText::read(Buffer const * buf, LyXLex & lex)
177 {
178         string token;
179         int pos = 0;
180         Paragraph * return_par = 0;
181         Paragraph::depth_type depth = 0; 
182         LyXFont font(LyXFont::ALL_INHERIT);
183
184         clear();
185         
186         while (lex.IsOK()) {
187                 lex.nextToken();
188                 token = lex.GetString();
189                 if (token.empty())
190                         continue;
191                 if (token == "\\end_inset")
192                         break;
193                 if (const_cast<Buffer*>(buf)->
194                         parseSingleLyXformat2Token(lex, par, return_par,
195                                                    token, pos, depth, font)) {
196                         // the_end read this should NEVER happen
197                         lex.printError("\\the_end read in inset! Error in document!");
198                         return;
199                 }
200         }
201         if (!return_par)
202                 return_par = par;
203         par = return_par;
204         while(return_par) {
205                 return_par->setInsetOwner(this);
206                 return_par = return_par->next();
207         }
208         
209         if (token != "\\end_inset") {
210                 lex.printError("Missing \\end_inset at this point. "
211                                            "Read: `$$Token'");
212         }
213         need_update = INIT;
214 }
215
216
217 int InsetText::ascent(BufferView * bv, LyXFont const &) const
218 {
219         int y_temp = 0;
220         Row * row = getLyXText(bv)->getRowNearY(y_temp);
221         insetAscent = row->ascent_of_text() + TEXT_TO_INSET_OFFSET;
222         return insetAscent;
223 }
224
225
226 int InsetText::descent(BufferView * bv, LyXFont const &) const
227 {
228                 LyXText * t = getLyXText(bv);
229         int y_temp = 0;
230         Row * row = t->getRowNearY(y_temp);
231         insetDescent = t->height - row->ascent_of_text() +
232         TEXT_TO_INSET_OFFSET;
233         return insetDescent;
234 }
235
236
237 int InsetText::width(BufferView * bv, LyXFont const &) const
238 {
239         insetWidth = max(textWidth(bv),
240                          (int)getLyXText(bv)->width + (2 * TEXT_TO_INSET_OFFSET));
241         return insetWidth;
242 }
243
244
245 int InsetText::textWidth(BufferView * bv) const
246 {
247         int const w = getMaxWidth(bv, this);
248         return w;
249 }
250
251
252 void InsetText::draw(BufferView * bv, LyXFont const & f,
253                      int baseline, float & x, bool cleared) const
254 {
255         if (nodraw())
256                 return;
257
258         Painter & pain = bv->painter();
259
260         // no draw is necessary !!!
261         if ((drawFrame_ == LOCKED) && !locked && !par->size()) {
262                 top_x = int(x);
263                 top_baseline = baseline;
264                 x += width(bv, f);
265                 if (!cleared && (need_update & CLEAR_FRAME))
266                         clearFrame(pain, cleared);
267                 else if (cleared)
268                         frame_is_visible = false;
269                 need_update = NONE;
270                 return;
271         }
272
273         xpos = x;
274 #if 0
275         UpdatableInset::draw(bv, f, baseline, x, cleared);
276 #else
277         if (!owner())
278                 x += static_cast<float>(scroll());
279 #endif
280 #if 0
281         // update insetWidth and insetHeight with dummy calls
282         (void)ascent(bv, f);
283         (void)descent(bv, f);
284         (void)width(bv, f);
285 #endif
286
287         // if top_x differs we have a rule down and we don't have to clear anything
288         if (!cleared && (top_x == int(x)) &&
289                 ((need_update&(INIT|FULL)) || (top_baseline!=baseline) ||
290                  (last_drawn_width!=insetWidth))) {
291                 clearInset(pain, baseline, cleared);
292         }
293         if (cleared)
294                 frame_is_visible = false;
295
296         if (!cleared && (need_update == NONE))
297                 return;
298
299         if (top_x != int(x)) {
300                 if ((getMaxWidth(bv, this) > 0) &&
301                         (getLyXText(bv)->width != old_max_width)) {
302                         resizeLyXText(bv);
303                         need_update |= FULL;
304                         old_max_width = getLyXText(bv)->width;
305                         bv->text->status = LyXText::CHANGED_IN_DRAW;
306                 }
307                 top_x = int(x);
308 #if 1
309                 clearInset(pain, baseline, cleared);
310 #else
311                 return;
312 #endif
313         }
314
315 //      lyxerr << "InsetText::draw[" << this << "](" << need_update << ":" << int(x) << ":" << top_x << ")\n";
316
317         if (cleared || (last_drawn_width != insetWidth)) {
318                 need_update |= FULL;
319                 last_drawn_width = insetWidth;
320         }
321
322         top_baseline = baseline;
323         top_y = baseline - ascent(bv, f);
324         last_width = width(bv, f);
325         last_height = ascent(bv, f) + descent(bv, f);
326
327         if (the_locking_inset && (cpar(bv) == inset_par)
328                 && (cpos(bv) == inset_pos)) {
329                 inset_x = cx(bv) - top_x + drawTextXOffset;
330                 inset_y = cy(bv) + drawTextYOffset;
331         }
332         if (!cleared && (need_update == CURSOR)
333                 && !getLyXText(bv)->selection.set()) {
334                 drawFrame(pain, cleared);
335                 x += last_width; // was width(bv, f);
336                 need_update = NONE;
337                 return;
338         }
339         x += TEXT_TO_INSET_OFFSET;
340
341         int y = 0;
342         Row * row = getLyXText(bv)->getRowNearY(y);
343         int y_offset = baseline - row->ascent_of_text();
344         int ph = pain.paperHeight();
345         int first = 0;
346         y = y_offset;
347         while ((row != 0) && ((y+row->height()) <= 0)) {
348                 y += row->height();
349                 first += row->height();
350                 row = row->next();
351         }
352         if (y_offset < 0)
353                 y_offset = y;
354         getLyXText(bv)->first = first;
355         if (cleared) {
356                 int yf = y_offset;
357                 y = 0;
358                 while ((row != 0) && (yf < ph)) {
359                         getLyXText(bv)->getVisibleRow(bv, y+y_offset, int(x), row,
360                                                 y+first, cleared);
361                         y += row->height();
362                         yf += row->height();
363                         row = row->next();
364                 }
365         } else if (!locked) {
366                 if (need_update & CURSOR) {
367                         bv->screen()->ToggleSelection(getLyXText(bv), bv, true, y_offset,int(x));
368                         getLyXText(bv)->clearSelection(bv);
369                         getLyXText(bv)->selection.cursor = getLyXText(bv)->cursor;
370                 }
371                 bv->screen()->Update(getLyXText(bv), bv, y_offset, int(x));
372         } else {
373                 locked = false;
374                 if (need_update & SELECTION)
375                         bv->screen()->ToggleToggle(getLyXText(bv), bv, y_offset, int(x));
376                 else if (need_update & CURSOR) {
377                         bv->screen()->ToggleSelection(getLyXText(bv), bv, true, y_offset,int(x));
378                         getLyXText(bv)->clearSelection(bv);
379                         getLyXText(bv)->selection.cursor = getLyXText(bv)->cursor;
380                 }
381                 bv->screen()->Update(getLyXText(bv), bv, y_offset, int(x));
382                 locked = true;
383         }
384
385         getLyXText(bv)->refresh_y = 0;
386         getLyXText(bv)->status = LyXText::UNCHANGED;
387         if ((need_update != CURSOR_PAR) &&
388                 ((drawFrame_ == ALWAYS) || ((drawFrame_ == LOCKED) && locked)))
389                 drawFrame(pain, cleared);
390         else if (need_update & CLEAR_FRAME)
391                 clearFrame(pain, cleared);
392         x += last_width /* was width(bv, f) */ - TEXT_TO_INSET_OFFSET;
393         if (bv->text->status==LyXText::CHANGED_IN_DRAW) {
394                 need_update |= INIT;
395         } else if (need_update != INIT)
396                 need_update = NONE;
397 }
398
399
400 void InsetText::drawFrame(Painter & pain, bool cleared) const
401 {
402         if (!frame_is_visible || cleared) {
403                 pain.rectangle(top_x + 1, top_baseline - insetAscent + 1,
404                                insetWidth - 1, insetAscent + insetDescent - 1,
405                                frame_color);
406                 frame_is_visible = true;
407         }
408 }
409
410
411 void InsetText::clearFrame(Painter & pain, bool cleared) const
412 {
413         if (frame_is_visible) {
414                 if (!cleared) {
415                         pain.rectangle(top_x + 1, top_baseline - insetAscent + 1,
416                                        insetWidth - 1, insetAscent + insetDescent - 1,
417                                        LColor::background);
418                 }
419                 frame_is_visible = false;
420         }
421 }
422
423
424 void InsetText::update(BufferView * bv, LyXFont const & font, bool reinit)
425 {
426                 LyXText * t = getLyXText(bv);
427                 
428 #if 0
429         int ww = t->width;
430         t->BreakParagraph(bv);
431         if (ww != t->width)
432                 reinit = true;
433 #endif
434         if (reinit) {
435                 need_update |= INIT;
436                 resizeLyXText(bv);
437                 if (owner())
438                         owner()->update(bv, font, true);
439                 return;
440         }
441         if (the_locking_inset) {
442                 inset_x = cx(bv) - top_x + drawTextXOffset;
443                 inset_y = cy(bv) + drawTextYOffset;
444                 the_locking_inset->update(bv, font, reinit);
445         }
446 #if 0
447         if (need_update == INIT) {
448                 resizeLyXText(bv);
449                 need_update |= FULL;
450         }
451 #endif
452         int oldw = insetWidth;
453         insetWidth = t->width + (2 * TEXT_TO_INSET_OFFSET);
454         if (oldw != insetWidth) {
455                 resizeLyXText(bv);
456                 need_update |= FULL;
457 //              update(bv, font, reinit);
458                 return;
459         }
460         if ((need_update & CURSOR_PAR) && t->status == LyXText::UNCHANGED &&
461                 the_locking_inset) {
462                 t->updateInset(bv, the_locking_inset);
463         }
464         
465         if (t->status == LyXText::NEED_MORE_REFRESH)
466                 need_update |= FULL;
467 #if 0
468         int y_temp = 0;
469         Row * row = t->GetRowNearY(y_temp);
470         insetAscent = row->ascent_of_text() + TEXT_TO_INSET_OFFSET;
471         insetDescent = t->height - row->ascent_of_text() + TEXT_TO_INSET_OFFSET;
472 #endif
473 }
474
475
476 void InsetText::setUpdateStatus(BufferView * bv, int what) const
477 {
478         LyXText * t = getLyXText(bv);
479         need_update |= what;
480         if (t->status == LyXText::NEED_MORE_REFRESH)
481                 need_update |= FULL;
482         else if (t->status == LyXText::NEED_VERY_LITTLE_REFRESH)
483                 need_update |= CURSOR_PAR;
484
485         // this to not draw a selection when we redraw all of it!
486         if ((need_update & (INIT|FULL)) && (need_update & CURSOR))
487                 t->clearSelection(bv);
488 }
489
490
491 void InsetText::updateLocal(BufferView * bv, int what, bool mark_dirty)
492 {
493                 LyXText * t = getLyXText(bv);
494         t->fullRebreak(bv);
495         setUpdateStatus(bv, what);
496         if (need_update != CURSOR || t->status != LyXText::UNCHANGED ||
497                 t->selection.set())
498                 bv->updateInset(this, mark_dirty);
499         bv->owner()->showState();
500         if (old_par != cpar(bv)) {
501                 bv->owner()->setLayout(cpar(bv)->getLayout());
502                 old_par = cpar(bv);
503         }
504 }
505
506
507 string const InsetText::editMessage() const
508 {
509         return _("Opened Text Inset");
510 }
511
512
513 void InsetText::edit(BufferView * bv, int x, int y, unsigned int button)
514 {
515 //    par->SetInsetOwner(this);
516         UpdatableInset::edit(bv, x, y, button);
517         
518         if (!bv->lockInset(this)) {
519                 lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
520                 return;
521         }
522         locked = true;
523         the_locking_inset = 0;
524         inset_pos = inset_x = inset_y = 0;
525         inset_boundary = false;
526         inset_par = 0;
527         old_par = 0;
528         int tmp_y = (y < 0) ? 0 : y;
529         LyXText * t = getLyXText(bv);
530
531         if (!checkAndActivateInset(bv, x, tmp_y, button)) {
532                 t->setCursorFromCoordinates(bv, x - drawTextXOffset,
533                                                                         y + insetAscent);
534         }
535         
536         t->selection.cursor = t->cursor;
537         bv->text->finishUndo();
538         showInsetCursor(bv);
539         updateLocal(bv, CURSOR, false);
540
541         // If the inset is empty set the language of the current font to the
542         // language to the surronding text (if different).
543         if (par->size() == 0 && !par->next() &&
544                 bv->getParentLanguage(this) != t->current_font.language()) {
545                 LyXFont font(LyXFont::ALL_IGNORE);
546                 font.setLanguage(bv->getParentLanguage(this));
547                 setFont(bv, font, false);
548         }
549 }
550
551
552 void InsetText::insetUnlock(BufferView * bv)
553 {
554         if (the_locking_inset) {
555                 the_locking_inset->insetUnlock(bv);
556                 the_locking_inset = 0;
557         }
558         hideInsetCursor(bv);
559         no_selection = false;
560         locked = false;
561         int code = CURSOR|CLEAR_FRAME;
562         LyXText * t = getLyXText(bv);
563         if (t->selection.set()) {
564                 t->clearSelection(bv);
565                 code = FULL;
566         } else if (owner()) {
567                 bv->owner()->setLayout(owner()->getLyXText(bv)
568                                        ->cursor.par()->getLayout());
569         } else
570                 bv->owner()->setLayout(bv->text->cursor.par()->getLayout());
571         updateLocal(bv, code, false);
572 }
573
574
575 bool InsetText::lockInsetInInset(BufferView * bv, UpdatableInset * inset)
576 {
577         lyxerr[Debug::INSETS] << "InsetText::LockInsetInInset("
578                               << inset << "): ";
579         if (!inset)
580                 return false;
581         if (inset == cpar(bv)->getInset(cpos(bv))) {
582                 lyxerr[Debug::INSETS] << "OK" << endl;
583                 the_locking_inset = inset;
584                 inset_x = cx(bv) - top_x + drawTextXOffset;
585                 inset_y = cy(bv) + drawTextYOffset;
586                 inset_pos = cpos(bv);
587                 inset_par = cpar(bv);
588                 inset_boundary = cboundary(bv);
589 #if 0
590                 getLyXText(bv)->clearSelection(bv);
591                 getLyXText(bv)->sel_cursor = getLyXText(bv)->cursor;
592                 getLyXText(bv)->UpdateInset(bv, the_locking_inset);
593 #else
594                 updateLocal(bv, CURSOR, false);
595 #endif
596                 return true;
597         } else if (the_locking_inset && (the_locking_inset == inset)) {
598                 if (cpar(bv) == inset_par && cpos(bv) == inset_pos) {
599                         lyxerr[Debug::INSETS] << "OK" << endl;
600                         inset_x = cx(bv) - top_x + drawTextXOffset;
601                         inset_y = cy(bv) + drawTextYOffset;
602                 } else {
603                         lyxerr[Debug::INSETS] << "cursor.pos != inset_pos" << endl;
604                 }
605         } else if (the_locking_inset) {
606                 lyxerr[Debug::INSETS] << "MAYBE" << endl;
607                 return the_locking_inset->lockInsetInInset(bv, inset);
608         }
609         lyxerr[Debug::INSETS] << "NOT OK" << endl;
610         return false;
611 }
612
613
614 bool InsetText::unlockInsetInInset(BufferView * bv, UpdatableInset * inset,
615                                    bool lr)
616 {
617         if (!the_locking_inset)
618                 return false;
619         if (the_locking_inset == inset) {
620                 the_locking_inset->insetUnlock(bv);
621                 getLyXText(bv)->updateInset(bv, inset);
622                 the_locking_inset = 0;
623                 if (lr)
624                         moveRight(bv, false);
625                 old_par = 0; // force layout setting
626                 if (scroll())
627                         scroll(bv, 0.0F);
628                 else
629                         updateLocal(bv, CURSOR, false);
630                 return true;
631         }
632         return the_locking_inset->unlockInsetInInset(bv, inset, lr);
633 }
634
635
636 bool InsetText::updateInsetInInset(BufferView * bv, Inset * inset)
637 {
638         if (!the_locking_inset)
639                 return false;
640         if (the_locking_inset != inset) {
641                 getLyXText(bv)->updateInset(bv, the_locking_inset);
642                 setUpdateStatus(bv, CURSOR_PAR);
643                 return the_locking_inset->updateInsetInInset(bv, inset);
644         }
645 //    updateLocal(bv, FULL, false);
646         if (getLyXText(bv)->updateInset(bv, inset))
647                 updateLocal(bv, CURSOR_PAR, false);
648         if (cpar(bv) == inset_par && cpos(bv) == inset_pos) {
649                 inset_x = cx(bv) - top_x + drawTextXOffset;
650                 inset_y = cy(bv) + drawTextYOffset;
651         }
652         return true;
653 }
654
655
656 void InsetText::insetButtonPress(BufferView * bv, int x, int y, int button)
657 {
658         no_selection = true;
659
660         int tmp_x = x - drawTextXOffset;
661         int tmp_y = y + insetAscent - getLyXText(bv)->first;
662         Inset * inset = bv->checkInsetHit(getLyXText(bv), tmp_x, tmp_y, button);
663
664         hideInsetCursor(bv);
665         if (the_locking_inset) {
666                 if (the_locking_inset == inset) {
667                         the_locking_inset->insetButtonPress(bv,x-inset_x,y-inset_y,button);
668                         no_selection = false;
669                         return;
670                 } else if (inset) {
671                         // otherwise unlock the_locking_inset and lock the new inset
672                         the_locking_inset->insetUnlock(bv);
673                         inset_x = cx(bv) - top_x + drawTextXOffset;
674                         inset_y = cy(bv) + drawTextYOffset;
675                         the_locking_inset = static_cast<UpdatableInset*>(inset);
676                         inset->insetButtonPress(bv, x - inset_x, y - inset_y, button);
677                         inset->edit(bv, x - inset_x, y - inset_y, button);
678                         if (the_locking_inset)
679                                 updateLocal(bv, CURSOR, false);
680                         no_selection = false;
681                         return;
682                 }
683                 // otherwise only unlock the_locking_inset
684                 the_locking_inset->insetUnlock(bv);
685                 the_locking_inset = 0;
686         }
687         if (bv->theLockingInset()) {
688                 if (inset && inset->editable() == Inset::HIGHLY_EDITABLE) {
689                         UpdatableInset * uinset = static_cast<UpdatableInset*>(inset);
690                         inset_x = cx(bv) - top_x + drawTextXOffset;
691                         inset_y = cy(bv) + drawTextYOffset;
692                         inset_pos = cpos(bv);
693                         inset_par = cpar(bv);
694                         inset_boundary = cboundary(bv);
695                         the_locking_inset = uinset;
696                         uinset->insetButtonPress(bv, x - inset_x, y - inset_y,
697                                                  button);
698                         uinset->edit(bv, x - inset_x, y - inset_y, 0);
699                         if (the_locking_inset)
700                                 updateLocal(bv, CURSOR, false);
701                         no_selection = false;
702                         return;
703                 }
704         }
705         if (!inset) { // && (button == 2)) {
706                 bool paste_internally = false;
707                 if ((button == 2) && getLyXText(bv)->selection.set()) {
708                         localDispatch(bv, LFUN_COPY, "");
709                         paste_internally = true;
710                 }
711                 getLyXText(bv)->setCursorFromCoordinates(bv, x-drawTextXOffset,
712                                                    y + insetAscent);
713                 getLyXText(bv)->selection.cursor = getLyXText(bv)->cursor;
714                 updateLocal(bv, CURSOR, false);
715                 bv->owner()->setLayout(cpar(bv)->getLayout());
716                 old_par = cpar(bv);
717                 // Insert primary selection with middle mouse
718                 // if there is a local selection in the current buffer,
719                 // insert this
720                 if (button == 2) {
721                         if (paste_internally)
722                                 localDispatch(bv, LFUN_PASTE, "");
723                         else
724                                 localDispatch(bv, LFUN_PASTESELECTION,
725                                               "paragraph");
726                 }
727         }
728         showInsetCursor(bv);
729         no_selection = false;
730 }
731
732
733 void InsetText::insetButtonRelease(BufferView * bv, int x, int y, int button)
734 {
735         UpdatableInset * inset = 0;
736
737         if (the_locking_inset) {
738                 the_locking_inset->insetButtonRelease(bv,
739                                                       x - inset_x, y - inset_y,
740                                                       button);
741         } else {
742                 if (cpar(bv)->getChar(cpos(bv)) == Paragraph::META_INSET) {
743                         inset = static_cast<UpdatableInset*>(cpar(bv)->getInset(cpos(bv)));
744                         if (inset->editable() == Inset::HIGHLY_EDITABLE) {
745                                 inset->insetButtonRelease(bv,
746                                                           x - inset_x,
747                                                           y - inset_y, button);
748                         } else {
749                                 inset_x = cx(bv) - top_x + drawTextXOffset;
750                                 inset_y = cy(bv) + drawTextYOffset;
751                                 inset->insetButtonRelease(bv,
752                                                           x - inset_x,
753                                                           y - inset_y, button);
754                                 inset->edit(bv,
755                                             x - inset_x, y - inset_y, button);
756                         }
757                         updateLocal(bv, CURSOR_PAR, false);
758                 }
759         }
760         no_selection = false;
761 }
762
763
764 void InsetText::insetMotionNotify(BufferView * bv, int x, int y, int state)
765 {
766         if (no_selection)
767                 return;
768         if (the_locking_inset) {
769                 the_locking_inset->insetMotionNotify(bv, x - inset_x,
770                                                      y - inset_y,state);
771                 return;
772         }
773         LyXText * t = getLyXText(bv);
774         hideInsetCursor(bv);
775         t->setCursorFromCoordinates(bv, x - drawTextXOffset, y + insetAscent);
776         t->setSelection(bv);
777         if (t->toggle_cursor.par() != t->toggle_end_cursor.par() ||
778                 t->toggle_cursor.pos() != t->toggle_end_cursor.pos())
779                 updateLocal(bv, SELECTION, false);
780         showInsetCursor(bv);
781 }
782
783
784 void InsetText::insetKeyPress(XKeyEvent * xke)
785 {
786         if (the_locking_inset) {
787                 the_locking_inset->insetKeyPress(xke);
788                 return;
789         }
790 }
791
792
793 UpdatableInset::RESULT
794 InsetText::localDispatch(BufferView * bv,
795                          kb_action action, string const & arg)
796 {
797         no_selection = false;
798         UpdatableInset::RESULT
799                 result= UpdatableInset::localDispatch(bv, action, arg);
800         if (result != UNDISPATCHED) {
801                 return DISPATCHED;
802         }
803
804         result = DISPATCHED;
805         if ((action < 0) && arg.empty())
806                 return FINISHED;
807
808         if (the_locking_inset) {
809                 result = the_locking_inset->localDispatch(bv, action, arg);
810                 if (result == DISPATCHED_NOUPDATE)
811                         return result;
812                 else if (result == DISPATCHED) {
813                         updateLocal(bv, CURSOR_PAR, false);
814                         return result;
815                 } else if (result == FINISHED) {
816                         bool dispatched = false;
817                         switch (action) {
818                         case LFUN_UNKNOWN_ACTION:
819                         case LFUN_BREAKPARAGRAPH:
820                         case LFUN_BREAKLINE:
821                                 moveRightIntern(bv, false, false);
822                                 break;
823                         case LFUN_RIGHT:
824                                 if (!getLyXText(bv)->cursor.par()->isRightToLeftPar(bv->buffer()->params))
825                                         moveRightIntern(bv, false, false);
826                                 dispatched = true;
827                                 break;
828                         case LFUN_LEFT:
829                                 if (getLyXText(bv)->cursor.par()->isRightToLeftPar(bv->buffer()->params))
830                                         moveRightIntern(bv, false, false);
831                                 dispatched = true;
832                                 break;
833                         default:
834                                 break;
835                         }
836                         the_locking_inset = 0;
837                         if (dispatched)
838                                 return DISPATCHED;
839                 }
840         }
841         hideInsetCursor(bv);
842         switch (action) {
843         // Normal chars
844         case LFUN_SELFINSERT:
845                 if (bv->buffer()->isReadonly()) {
846 //          setErrorMessage(N_("Document is read only"));
847                         break;
848                 }
849                 if (!arg.empty()) {
850                         /* Automatically delete the currently selected
851                          * text and replace it with what is being
852                          * typed in now. Depends on lyxrc settings
853                          * "auto_region_delete", which defaults to
854                          * true (on). */
855
856                         bv->text->setUndo(bv->buffer(), Undo::INSERT,
857                                           bv->text->cursor.par()->previous(),
858                                           bv->text->cursor.par()->next());
859                         bv->setState();
860                         if (lyxrc.auto_region_delete) {
861                                 if (getLyXText(bv)->selection.set()) {
862                                         getLyXText(bv)->cutSelection(bv, false);
863                                 }
864                         }
865                         getLyXText(bv)->clearSelection(bv);
866                         for (string::size_type i = 0; i < arg.length(); ++i) {
867                                 if (greek_kb_flag) {
868                                         if (!math_insert_greek(bv, arg[i])) {
869                                                 bv->owner()->getIntl()->getTrans().TranslateAndInsert(arg[i], getLyXText(bv));
870                                         } else if (!the_locking_inset) {
871                                                 (void)moveRight(bv, false);
872                                         }
873                                 } else {
874                                         bv->owner()->getIntl()->getTrans().TranslateAndInsert(arg[i], getLyXText(bv));
875                                 }
876                         }
877                 }
878                 getLyXText(bv)->selection.cursor = getLyXText(bv)->cursor;
879                 updateLocal(bv, CURSOR_PAR, true);
880                 result = DISPATCHED_NOUPDATE;
881                 break;
882                 // --- Cursor Movements -----------------------------------
883         case LFUN_RIGHTSEL:
884                 bv->text->finishUndo();
885                 moveRight(bv, false, true);
886                 getLyXText(bv)->setSelection(bv);
887                 updateLocal(bv, SELECTION, false);
888                 break;
889         case LFUN_RIGHT:
890                 result = moveRight(bv);
891                 bv->text->finishUndo();
892                 updateLocal(bv, CURSOR, false);
893                 break;
894         case LFUN_LEFTSEL:
895                 bv->text->finishUndo();
896                 moveLeft(bv, false, true);
897                 getLyXText(bv)->setSelection(bv);
898                 updateLocal(bv, SELECTION, false);
899                 break;
900         case LFUN_LEFT:
901                 bv->text->finishUndo();
902                 result = moveLeft(bv);
903                 updateLocal(bv, CURSOR, false);
904                 break;
905         case LFUN_DOWNSEL:
906                 bv->text->finishUndo();
907                 moveDown(bv);
908                 getLyXText(bv)->setSelection(bv);
909                 updateLocal(bv, SELECTION, false);
910                 break;
911         case LFUN_DOWN:
912                 bv->text->finishUndo();
913                 result = moveDown(bv);
914                 updateLocal(bv, CURSOR, false);
915                 break;
916         case LFUN_UPSEL:
917                 bv->text->finishUndo();
918                 moveUp(bv);
919                 getLyXText(bv)->setSelection(bv);
920                 updateLocal(bv, SELECTION, false);
921                 break;
922         case LFUN_UP:
923                 bv->text->finishUndo();
924                 result = moveUp(bv);
925                 updateLocal(bv, CURSOR, false);
926                 break;
927         case LFUN_HOME:
928                 bv->text->finishUndo();
929                 getLyXText(bv)->cursorHome(bv);
930                 updateLocal(bv, CURSOR, false);
931                 break;
932         case LFUN_END:
933                 getLyXText(bv)->cursorEnd(bv);
934                 updateLocal(bv, CURSOR, false);
935                 break;
936         case LFUN_BACKSPACE: {
937                 bv->text->setUndo(bv->buffer(), Undo::DELETE,
938                                   bv->text->cursor.par()->previous(),
939                                   bv->text->cursor.par()->next());
940                 LyXText * t = getLyXText(bv);
941                 
942                 if (t->selection.set()) {
943                         t->cutSelection(bv);
944                 } else {
945                         t->backspace(bv);
946                 }
947                 
948                 updateLocal(bv, CURSOR_PAR, true);
949         }
950         break;
951         
952         case LFUN_DELETE:
953         {
954                 bv->text->setUndo(bv->buffer(), Undo::DELETE,
955                                   bv->text->cursor.par()->previous(),
956                                   bv->text->cursor.par()->next());
957                 LyXText * t = getLyXText(bv);
958                 
959                 if (t->selection.set()) {
960                         t->cutSelection(bv);
961                 } else {
962                         t->Delete(bv);
963                 }
964                 updateLocal(bv, CURSOR_PAR, true);
965         }
966         break;
967         
968         case LFUN_CUT:
969                 bv->text->setUndo(bv->buffer(), Undo::DELETE,
970                                   bv->text->cursor.par()->previous(),
971                                   bv->text->cursor.par()->next());
972                 getLyXText(bv)->cutSelection(bv);
973                 updateLocal(bv, CURSOR_PAR, true);
974                 break;
975         case LFUN_COPY:
976                 bv->text->finishUndo();
977                 getLyXText(bv)->copySelection(bv);
978                 updateLocal(bv, CURSOR_PAR, false);
979                 break;
980         case LFUN_PASTESELECTION:
981         {
982                 string const clip(bv->getClipboard());
983         
984                 if (clip.empty())
985                         break;
986                 if (arg == "paragraph") {
987                         getLyXText(bv)->insertStringAsParagraphs(bv, clip);
988                 } else {
989                         getLyXText(bv)->insertStringAsLines(bv, clip);
990                 }
991                 updateLocal(bv, CURSOR_PAR, true);
992                 break;
993         }
994         case LFUN_PASTE:
995                 if (!autoBreakRows) {
996
997                         if (CutAndPaste::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 //    bv->owner()->prohibitInput();
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 //    bv->owner()->allowInput();
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  */