]> git.lyx.org Git - lyx.git/blob - src/insets/insettext.C
Added using directive.
[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                         CutAndPaste cap;
997
998                         if (cap.nrOfParagraphs() > 1) {
999                                 WriteAlert(_("Impossible operation"),
1000                                                    _("Cannot include more than one paragraph!"),
1001                                                    _("Sorry."));
1002                                 break;
1003                         }
1004                 }
1005                 bv->text->setUndo(bv->buffer(), Undo::INSERT,
1006                                   bv->text->cursor.par()->previous(),
1007                                   bv->text->cursor.par()->next());
1008                 getLyXText(bv)->pasteSelection(bv);
1009                 updateLocal(bv, CURSOR_PAR, true);
1010                 break;
1011         case LFUN_BREAKPARAGRAPH:
1012                 if (!autoBreakRows)
1013                         return DISPATCHED;
1014                 getLyXText(bv)->breakParagraph(bv, 0);
1015                 updateLocal(bv, FULL, true);
1016                 break;
1017         case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
1018                 if (!autoBreakRows)
1019                         return DISPATCHED;
1020                 getLyXText(bv)->breakParagraph(bv, 1);
1021                 updateLocal(bv, FULL, true);
1022                 break;
1023         case LFUN_BREAKLINE:
1024                 if (!autoBreakRows)
1025                         return DISPATCHED;
1026                 bv->text->setUndo(bv->buffer(), Undo::INSERT,
1027                                   bv->text->cursor.par()->previous(),
1028                                   bv->text->cursor.par()->next());
1029                 getLyXText(bv)->insertChar(bv, Paragraph::META_NEWLINE);
1030                 updateLocal(bv, CURSOR_PAR, true);
1031                 break;
1032         case LFUN_LAYOUT:
1033                 // do not set layouts on non breakable textinsets
1034                 if (autoBreakRows) {
1035                         LyXTextClass::size_type cur_layout = cpar(bv)->layout;
1036           
1037                         // Derive layout number from given argument (string)
1038                         // and current buffer's textclass (number). */    
1039                         LyXTextClassList::ClassList::size_type tclass =
1040                                 bv->buffer()->params.textclass;
1041                         std::pair <bool, LyXTextClass::size_type> layout = 
1042                                 textclasslist.NumberOfLayout(tclass, arg);
1043
1044                         // If the entry is obsolete, use the new one instead.
1045                         if (layout.first) {
1046                                 string obs = textclasslist.Style(tclass,layout.second).
1047                                         obsoleted_by();
1048                                 if (!obs.empty()) 
1049                                         layout = textclasslist.NumberOfLayout(tclass, obs);
1050                         }
1051
1052                         // see if we found the layout number:
1053                         if (!layout.first) {
1054                                 string const msg = string(N_("Layout ")) + arg + N_(" not known");
1055                                 bv->owner()->getLyXFunc()->Dispatch(LFUN_MESSAGE, msg);
1056                                 break;
1057                         }
1058
1059                         if (cur_layout != layout.second) {
1060                                 cur_layout = layout.second;
1061                                 getLyXText(bv)->setLayout(bv, layout.second);
1062                                 bv->owner()->setLayout(cpar(bv)->getLayout());
1063                                 updateLocal(bv, CURSOR_PAR, true);
1064                         }
1065                 } else {
1066                         // reset the layout box
1067                         bv->owner()->setLayout(cpar(bv)->getLayout());
1068                 }
1069                 break;
1070         case LFUN_PARAGRAPH_SPACING:
1071                 // This one is absolutely not working. When fiddling with this
1072                 // it also seems to me that the paragraphs inside the insettext
1073                 // inherit bufferparams/paragraphparams in a strange way. (Lgb)
1074         {
1075                 Paragraph * par = getLyXText(bv)->cursor.par();
1076                 Spacing::Space cur_spacing = par->params().spacing().getSpace();
1077                 float cur_value = 1.0;
1078                 if (cur_spacing == Spacing::Other) {
1079                         cur_value = par->params().spacing().getValue();
1080                 }
1081                                 
1082                 std::istringstream istr(arg.c_str());
1083                 string tmp;
1084                 istr >> tmp;
1085                 Spacing::Space new_spacing = cur_spacing;
1086                 float new_value = cur_value;
1087                 if (tmp.empty()) {
1088                         lyxerr << "Missing argument to `paragraph-spacing'"
1089                                    << endl;
1090                 } else if (tmp == "single") {
1091                         new_spacing = Spacing::Single;
1092                 } else if (tmp == "onehalf") {
1093                         new_spacing = Spacing::Onehalf;
1094                 } else if (tmp == "double") {
1095                         new_spacing = Spacing::Double;
1096                 } else if (tmp == "other") {
1097                         new_spacing = Spacing::Other;
1098                         float tmpval = 0.0;
1099                         istr >> tmpval;
1100                         lyxerr << "new_value = " << tmpval << endl;
1101                         if (tmpval != 0.0)
1102                                 new_value = tmpval;
1103                 } else if (tmp == "default") {
1104                         new_spacing = Spacing::Default;
1105                 } else {
1106                         lyxerr << _("Unknown spacing argument: ")
1107                                    << arg << endl;
1108                 }
1109                 if (cur_spacing != new_spacing || cur_value != new_value) {
1110                         par->params().spacing(Spacing(new_spacing, new_value));
1111                         //getLyXText(bv)->RedoParagraph(owner->view());
1112                         updateLocal(bv, CURSOR_PAR, true);
1113                         //bv->update(BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
1114                 }
1115         }
1116         break;
1117         
1118         default:
1119                 if (!bv->Dispatch(action, arg))
1120                         result = UNDISPATCHED;
1121                 break;
1122         }
1123
1124         /// If the action has deleted all text in the inset, we need to change the
1125         // language to the language to the surronding text.
1126         if (par->size() == 0 && !par->next()) {
1127                 LyXFont font(LyXFont::ALL_IGNORE);
1128                 font.setLanguage(bv->getParentLanguage(this));
1129                 setFont(bv, font, false);
1130         }
1131
1132         if (result != FINISHED) {
1133                 showInsetCursor(bv);
1134         } else
1135                 bv->unlockInset(this);
1136         return result;
1137 }
1138
1139
1140 int InsetText::latex(Buffer const * buf, ostream & os, bool, bool) const
1141 {
1142         TexRow texrow;
1143         buf->latexParagraphs(os, par, 0, texrow);
1144         return texrow.rows();
1145 }
1146
1147
1148 int InsetText::ascii(Buffer const * buf, ostream & os, int linelen) const
1149 {
1150         Paragraph * p = par;
1151         unsigned int lines = 0;
1152         
1153         while (p) {
1154                 string const tmp = buf->asciiParagraph(p, linelen);
1155                 lines += countChar(tmp, '\n');
1156                 os << tmp;
1157                 p = p->next();
1158         }
1159         return lines;
1160 }
1161
1162
1163 int InsetText::docBook(Buffer const * buf, ostream & os) const
1164 {
1165         Paragraph * p = par;
1166         unsigned int lines = 0;
1167         int desc = 0;
1168         
1169         string tmp;
1170         while (p) {
1171                 buf->simpleDocBookOnePar(os, tmp, p, desc, 0);
1172                 p = p->next();
1173         }
1174         
1175         return lines;
1176 }
1177
1178
1179 void InsetText::validate(LaTeXFeatures & features) const
1180 {
1181         Paragraph * p = par;
1182         while (p) {
1183                 p->validate(features);
1184                 p = p->next();
1185         }
1186 }
1187
1188
1189 int InsetText::beginningOfMainBody(Buffer const * buf, Paragraph * p) const
1190 {
1191         if (textclasslist.Style(buf->params.textclass,
1192                                 p->getLayout()).labeltype != LABEL_MANUAL)
1193                 return 0;
1194         else
1195                 return p->beginningOfMainBody();
1196 }
1197
1198
1199 void InsetText::getCursorPos(BufferView * bv,
1200                              int & x, int & y) const
1201 {
1202         x = cx(bv);
1203         y = cy(bv);
1204 }
1205
1206
1207 unsigned int InsetText::insetInInsetY()
1208 {
1209         if (!the_locking_inset)
1210                 return 0;
1211
1212         return (inset_y + the_locking_inset->insetInInsetY());
1213 }
1214
1215
1216 void InsetText::toggleInsetCursor(BufferView * bv)
1217 {
1218         if (the_locking_inset) {
1219                 the_locking_inset->toggleInsetCursor(bv);
1220                 return;
1221         }
1222
1223         LyXFont const font(getLyXText(bv)->getFont(bv->buffer(), cpar(bv), cpos(bv)));
1224
1225         int const asc = lyxfont::maxAscent(font);
1226         int const desc = lyxfont::maxDescent(font);
1227   
1228         if (isCursorVisible())
1229                 bv->hideLockedInsetCursor();
1230         else
1231                 bv->showLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1232         toggleCursorVisible();
1233 }
1234
1235
1236 void InsetText::showInsetCursor(BufferView * bv, bool show)
1237 {
1238         if (the_locking_inset) {
1239                 the_locking_inset->showInsetCursor(bv);
1240                 return;
1241         }
1242         if (!isCursorVisible()) {
1243                 LyXFont const font =
1244                         getLyXText(bv)->getFont(bv->buffer(), cpar(bv), cpos(bv));
1245         
1246                 int const asc = lyxfont::maxAscent(font);
1247                 int const desc = lyxfont::maxDescent(font);
1248
1249                 bv->fitLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1250                 if (show)
1251                         bv->showLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1252                 setCursorVisible(true);
1253         }
1254 }
1255
1256
1257 void InsetText::hideInsetCursor(BufferView * bv)
1258 {
1259         if (isCursorVisible()) {
1260                 bv->hideLockedInsetCursor();
1261                 setCursorVisible(false);
1262         }
1263         if (the_locking_inset)
1264                 the_locking_inset->hideInsetCursor(bv);
1265 }
1266
1267
1268 UpdatableInset::RESULT
1269 InsetText::moveRight(BufferView * bv, bool activate_inset, bool selecting)
1270 {
1271         if (getLyXText(bv)->cursor.par()->isRightToLeftPar(bv->buffer()->params))
1272                 return moveLeftIntern(bv, false, activate_inset, selecting);
1273         else
1274                 return moveRightIntern(bv, false, activate_inset, selecting);
1275 }
1276
1277
1278 UpdatableInset::RESULT
1279 InsetText::moveLeft(BufferView * bv, bool activate_inset, bool selecting)
1280 {
1281         if (getLyXText(bv)->cursor.par()->isRightToLeftPar(bv->buffer()->params))
1282                 return moveRightIntern(bv, true, activate_inset, selecting);
1283         else
1284                 return moveLeftIntern(bv, true, activate_inset, selecting);
1285 }
1286
1287
1288 UpdatableInset::RESULT
1289 InsetText::moveRightIntern(BufferView * bv, bool behind, 
1290                            bool activate_inset, bool selecting)
1291 {
1292         if (!cpar(bv)->next() && (cpos(bv) >= cpar(bv)->size()))
1293                 return FINISHED;
1294         if (activate_inset && checkAndActivateInset(bv, behind))
1295                 return DISPATCHED;
1296         getLyXText(bv)->cursorRight(bv);
1297         if (!selecting)
1298                 getLyXText(bv)->selection.cursor = getLyXText(bv)->cursor;
1299         return DISPATCHED_NOUPDATE;
1300 }
1301
1302
1303 UpdatableInset::RESULT
1304 InsetText::moveLeftIntern(BufferView * bv, bool behind,
1305                           bool activate_inset, bool selecting)
1306 {
1307         if (!cpar(bv)->previous() && (cpos(bv) <= 0))
1308                 return FINISHED;
1309         getLyXText(bv)->cursorLeft(bv);
1310         if (!selecting)
1311                 getLyXText(bv)->selection.cursor = getLyXText(bv)->cursor;
1312         if (activate_inset && checkAndActivateInset(bv, behind))
1313                 return DISPATCHED;
1314         return DISPATCHED_NOUPDATE;
1315 }
1316
1317
1318 UpdatableInset::RESULT
1319 InsetText::moveUp(BufferView * bv)
1320 {
1321         if (!crow(bv)->previous())
1322                 return FINISHED;
1323         getLyXText(bv)->cursorUp(bv);
1324         return DISPATCHED_NOUPDATE;
1325 }
1326
1327
1328 UpdatableInset::RESULT
1329 InsetText::moveDown(BufferView * bv)
1330 {
1331         if (!crow(bv)->next())
1332                 return FINISHED;
1333         getLyXText(bv)->cursorDown(bv);
1334         return DISPATCHED_NOUPDATE;
1335 }
1336
1337
1338 bool InsetText::insertInset(BufferView * bv, Inset * inset)
1339 {
1340         if (the_locking_inset) {
1341                 if (the_locking_inset->insertInsetAllowed(inset))
1342                         return the_locking_inset->insertInset(bv, inset);
1343                 return false;
1344         }
1345         bv->text->setUndo(bv->buffer(), Undo::INSERT,
1346                           bv->text->cursor.par()->previous(),
1347                           bv->text->cursor.par()->next());
1348         inset->setOwner(this);
1349         hideInsetCursor(bv);
1350         getLyXText(bv)->insertInset(bv, inset);
1351 #if 0
1352         if ((cpar(bv)->GetChar(cpos(bv)) != Paragraph::META_INSET) ||
1353                 (cpar(bv)->GetInset(cpos(bv)) != inset))
1354                 getLyXText(bv)->CursorLeft(bv);
1355 #endif
1356         bv->fitCursor(getLyXText(bv));
1357         updateLocal(bv, CURSOR_PAR|CURSOR, true);
1358         showInsetCursor(bv);
1359         return true;
1360 }
1361
1362
1363 UpdatableInset * InsetText::getLockingInset()
1364 {
1365         return the_locking_inset ? the_locking_inset->getLockingInset() : this;
1366 }
1367
1368
1369 UpdatableInset * InsetText::getFirstLockingInsetOfType(Inset::Code c)
1370 {
1371         if (c == lyxCode())
1372                 return this;
1373         if (the_locking_inset)
1374                 return the_locking_inset->getFirstLockingInsetOfType(c);
1375         return 0;
1376 }
1377
1378
1379 bool InsetText::showInsetDialog(BufferView * bv) const
1380 {
1381         if (the_locking_inset)
1382                 return the_locking_inset->showInsetDialog(bv);
1383         return false;
1384 }
1385
1386
1387 std::vector<string> const InsetText::getLabelList() const 
1388 {
1389         std::vector<string> label_list;
1390
1391         Paragraph * tpar = par;
1392         while (tpar) {
1393                 Paragraph::inset_iterator beg = tpar->inset_iterator_begin();
1394                 Paragraph::inset_iterator end = tpar->inset_iterator_end();
1395                 for (; beg != end; ++beg) {
1396                         std::vector<string> const l = (*beg)->getLabelList();
1397                         label_list.insert(label_list.end(), l.begin(), l.end());
1398                 }
1399                 tpar = tpar->next();
1400         }
1401         return label_list;
1402 }
1403
1404
1405 void InsetText::setFont(BufferView * bv, LyXFont const & font, bool toggleall,
1406                         bool selectall)
1407 {
1408         if (the_locking_inset) {
1409                 the_locking_inset->setFont(bv, font, toggleall, selectall);
1410                 return;
1411         }
1412         if (getLyXText(bv)->selection.set()) {
1413                 bv->text->setUndo(bv->buffer(), Undo::EDIT,
1414                                   bv->text->cursor.par()->previous(),
1415                                   bv->text->cursor.par()->next());
1416         }
1417         if (selectall)
1418                 selectAll(bv);
1419         getLyXText(bv)->setFont(bv, font, toggleall);
1420         if (selectall)
1421                 getLyXText(bv)->clearSelection(bv);
1422         bv->fitCursor(getLyXText(bv));
1423         if (selectall || getLyXText(bv)->selection.set())
1424                 updateLocal(bv, FULL, true);
1425         else
1426                 updateLocal(bv, CURSOR_PAR, true);
1427 }
1428
1429
1430 bool InsetText::checkAndActivateInset(BufferView * bv, bool behind)
1431 {
1432         if (cpar(bv)->getChar(cpos(bv)) == Paragraph::META_INSET) {
1433                 unsigned int x;
1434                 unsigned int y;
1435                 Inset * inset =
1436                         static_cast<UpdatableInset*>(cpar(bv)->getInset(cpos(bv)));
1437                 if (!inset || inset->editable() != Inset::HIGHLY_EDITABLE)
1438                         return false;
1439                 LyXFont const font =
1440                         getLyXText(bv)->getFont(bv->buffer(), cpar(bv), cpos(bv));
1441                 if (behind) {
1442                         x = inset->width(bv, font);
1443                         y = font.isRightToLeft() ? 0 : inset->descent(bv, font);
1444                 } else {
1445                         x = 0;
1446                         y = font.isRightToLeft() ? inset->descent(bv, font) : 0;
1447                 }
1448                 //inset_x = cx(bv) - top_x + drawTextXOffset;
1449                 //inset_y = cy(bv) + drawTextYOffset;
1450                 inset->edit(bv, x, y, 0);
1451                 if (!the_locking_inset)
1452                         return false;
1453                 updateLocal(bv, CURSOR, false);
1454                 return true;
1455         }
1456         return false;
1457 }
1458
1459
1460 bool InsetText::checkAndActivateInset(BufferView * bv, int x, int y,
1461                                       int button)
1462 {
1463         x -= drawTextXOffset;
1464         int dummyx = x;
1465         int dummyy = y + insetAscent;
1466         Inset * inset = bv->checkInsetHit(getLyXText(bv), dummyx, dummyy, button);
1467
1468         if (inset) {
1469                 if (x < 0)
1470                         x = insetWidth;
1471                 if (y < 0)
1472                         y = insetDescent;
1473                 inset_x = cx(bv) - top_x + drawTextXOffset;
1474                 inset_y = cy(bv) + drawTextYOffset;
1475                 inset->edit(bv, x - inset_x, y - inset_y, button);
1476                 if (!the_locking_inset)
1477                         return false;
1478                 updateLocal(bv, CURSOR, false);
1479                 return true;
1480         }
1481         return false;
1482 }
1483
1484
1485 int InsetText::getMaxWidth(BufferView * bv, UpdatableInset const * inset) const
1486 {
1487         int w = UpdatableInset::getMaxWidth(bv, inset);
1488         if (w < 0) {
1489                 return w;
1490         }
1491         if (owner()) {
1492                 w = w - top_x + owner()->x();
1493                 return w;
1494         }
1495         w -= (2 * TEXT_TO_INSET_OFFSET);
1496         return w - top_x;
1497 //    return  w - (2*TEXT_TO_INSET_OFFSET);
1498 }
1499
1500
1501 void InsetText::setParagraphData(Paragraph * p)
1502 {
1503         // delete all instances of LyXText before deleting the paragraps used
1504         // by it.
1505         cached_bview = 0;
1506         for (Cache::iterator cit = cache.begin(); cit != cache.end(); ++cit){
1507                 delete (*cit).second;
1508                 (*cit).second = 0;
1509         }
1510
1511         while (par) {
1512                 Paragraph * tmp = par->next();
1513                 delete par;
1514                 par = tmp;
1515         }
1516
1517         par = new Paragraph(*p);
1518         par->setInsetOwner(this);
1519         Paragraph * np = par;
1520         while (p->next()) {
1521                 p = p->next();
1522                 np->next(new Paragraph(*p));
1523                 np->next()->previous(np);
1524                 np = np->next();
1525                 np->setInsetOwner(this);
1526         }
1527
1528         need_update = INIT;
1529 }
1530
1531
1532 void InsetText::setText(string const & data)
1533 {
1534         clear();
1535         LyXFont font(LyXFont::ALL_SANE);
1536         for (unsigned int i=0; i < data.length(); ++i)
1537                 par->insertChar(i, data[i], font);
1538 }
1539
1540
1541 void InsetText::setAutoBreakRows(bool flag)
1542 {
1543         if (flag != autoBreakRows) {
1544                 autoBreakRows = flag;
1545                 need_update = FULL;
1546                 if (!flag)
1547                         removeNewlines();
1548         }
1549 }
1550
1551
1552 void InsetText::setDrawFrame(BufferView * bv, DrawFrame how)
1553 {
1554         if (how != drawFrame_) {
1555                 drawFrame_ = how;
1556                 if (bv)
1557                         updateLocal(bv, DRAW_FRAME, false);
1558         }
1559 }
1560
1561
1562 void InsetText::setFrameColor(BufferView * bv, LColor::color col)
1563 {
1564         if (frame_color != col) {
1565                 frame_color = col;
1566                 if (bv)
1567                         updateLocal(bv, DRAW_FRAME, false);
1568         }
1569 }
1570
1571
1572 int InsetText::cx(BufferView * bv) const
1573 {
1574         LyXText * text = getLyXText(bv);
1575         int x = text->cursor.x() + top_x + TEXT_TO_INSET_OFFSET;
1576         if (the_locking_inset) {
1577                 LyXFont font = text->getFont(bv->buffer(),
1578                                              text->cursor.par(),
1579                                              text->cursor.pos());
1580                 if (font.isVisibleRightToLeft())
1581                         x -= the_locking_inset->width(bv, font);
1582         }
1583         return x;
1584 }
1585
1586
1587 int InsetText::cy(BufferView * bv) const
1588 {
1589         LyXFont font;
1590         return getLyXText(bv)->cursor.y() - ascent(bv, font) + TEXT_TO_INSET_OFFSET;
1591 }
1592
1593
1594 Paragraph::size_type InsetText::cpos(BufferView * bv) const
1595 {
1596         return getLyXText(bv)->cursor.pos();
1597 }
1598
1599
1600 Paragraph * InsetText::cpar(BufferView * bv) const
1601 {
1602         return getLyXText(bv)->cursor.par();
1603 }
1604
1605
1606 bool InsetText::cboundary(BufferView * bv) const
1607 {
1608         return getLyXText(bv)->cursor.boundary();
1609 }
1610
1611
1612 Row * InsetText::crow(BufferView * bv) const
1613 {
1614         return getLyXText(bv)->cursor.row();
1615 }
1616
1617
1618 LyXText * InsetText::getLyXText(BufferView const * lbv,
1619                                 bool const recursive) const
1620 {
1621         if (!recursive && (cached_bview == lbv))
1622                 return cached_text;
1623         
1624         // Super UGLY! (Lgb)
1625         BufferView * bv = const_cast<BufferView *>(lbv);
1626         
1627         cached_bview = bv;
1628         Cache::iterator it = cache.find(bv);
1629         
1630         if (it != cache.end()) {
1631                 lyx::Assert(it->second);
1632                         
1633                 cached_text = it->second;
1634                 if (recursive && the_locking_inset) {
1635                         return the_locking_inset->getLyXText(bv);
1636                 }
1637                 return cached_text;
1638         }
1639         cached_text = new LyXText(const_cast<InsetText *>(this));
1640         cached_text->init(bv);
1641
1642         cache.insert(make_pair(bv, cached_text));
1643         
1644         if (the_locking_inset) {
1645                 cached_text->setCursor(bv, inset_par, inset_pos,
1646                                                            true, inset_boundary);
1647                 if (recursive) {
1648                         return the_locking_inset->getLyXText(bv);
1649                 }
1650         }
1651         return cached_text;
1652 }
1653
1654
1655 void InsetText::deleteLyXText(BufferView * bv, bool recursive) const
1656 {
1657         cached_bview = 0;
1658
1659         Cache::iterator it = cache.find(bv);
1660         
1661         if (it == cache.end()) {
1662                 return;
1663         }
1664
1665         lyx::Assert(it->second);
1666         
1667         delete it->second;
1668         cache.erase(bv);
1669         if (recursive) {
1670                 /// then remove all LyXText in text-insets
1671                 Paragraph * p = par;
1672                 for (; p; p = p->next()) {
1673                         p->deleteInsetsLyXText(bv);
1674                 }
1675         }
1676 }
1677
1678
1679 void InsetText::resizeLyXText(BufferView * bv, bool force) const
1680 {
1681         if (!par->next() && !par->size()) // no data, resize not neccessary!
1682                 return;
1683         // one endless line, resize normally not necessary
1684         if (!force && getMaxWidth(bv, this) < 0)
1685                 return;
1686
1687         Cache::iterator it = cache.find(bv);
1688         if (it == cache.end()) {
1689                 return;
1690         }
1691         lyx::Assert(it->second);
1692         
1693         Paragraph * lpar = 0;
1694         Paragraph * selstartpar = 0;
1695         Paragraph * selendpar = 0;
1696         Paragraph::size_type pos = 0;
1697         Paragraph::size_type selstartpos = 0;
1698         Paragraph::size_type selendpos = 0;
1699         bool boundary = false;
1700         bool selstartboundary = false;
1701         bool selendboundary = false;
1702         bool selection = false;
1703         bool mark_set = false;
1704
1705 //    ProhibitInput(bv);
1706
1707         if (locked) {
1708                 LyXText * t = getLyXText(bv);
1709                 lpar = t->cursor.par();
1710                 pos = t->cursor.pos();
1711                 boundary = t->cursor.boundary();
1712                 selstartpar = t->selection.start.par();
1713                 selstartpos = t->selection.start.pos();
1714                 selstartboundary = t->selection.start.boundary();
1715                 selendpar = t->selection.end.par();
1716                 selendpos = t->selection.end.pos();
1717                 selendboundary = t->selection.end.boundary();
1718                 selection = t->selection.set();
1719                 mark_set = t->selection.mark();
1720         }
1721         deleteLyXText(bv, (the_locking_inset == 0) || force);
1722
1723         if (lpar) {
1724                 LyXText * t = getLyXText(bv);
1725                         
1726                 t->selection.set(true);
1727                 /* at this point just to avoid the Delete-Empty-Paragraph
1728                  * Mechanism when setting the cursor */
1729                 t->selection.mark(mark_set);
1730                 if (selection) {
1731                         t->setCursor(bv, selstartpar, selstartpos, true, selstartboundary);
1732                         t->selection.cursor = t->cursor;
1733                         t->setCursor(bv, selendpar, selendpos, true, selendboundary);
1734                         t->setSelection(bv);
1735                         t->setCursor(bv, lpar, pos);
1736                 } else {
1737                         t->setCursor(bv, lpar, pos, true, boundary);
1738                         t->selection.cursor = t->cursor;
1739                         t->selection.set(false);
1740                 }
1741         }
1742         if (bv->screen()) {
1743                         LyXText * t = getLyXText(bv);
1744                         t->first = bv->screen()->TopCursorVisible(t);
1745         }
1746         
1747         // this will scroll the screen such that the cursor becomes visible 
1748         bv->updateScrollbar();
1749 //    AllowInput(bv);
1750         if (the_locking_inset) {
1751                 /// then resize all LyXText in text-insets
1752                 inset_x = cx(bv) - top_x + drawTextXOffset;
1753                 inset_y = cy(bv) + drawTextYOffset;
1754                 for (Paragraph * p = par; p; p = p->next()) {
1755                         p->resizeInsetsLyXText(bv);
1756                 }
1757         }
1758         need_update = FULL;
1759 }
1760
1761
1762 void InsetText::removeNewlines()
1763 {
1764         for (Paragraph * p = par; p; p = p->next()) {
1765                 for (int i = 0; i < p->size(); ++i) {
1766                         if (p->getChar(i) == Paragraph::META_NEWLINE)
1767                                 p->erase(i);
1768                 }
1769         }
1770 }
1771
1772
1773 bool InsetText::nodraw() const
1774 {
1775         if (the_locking_inset)
1776                 return the_locking_inset->nodraw();
1777         return UpdatableInset::nodraw();
1778 }
1779
1780
1781 int InsetText::scroll(bool recursive) const
1782 {
1783         int sx = UpdatableInset::scroll(false);
1784
1785         if (recursive && the_locking_inset)
1786                 sx += the_locking_inset->scroll(recursive);
1787
1788         return sx;
1789 }
1790
1791
1792 bool InsetText::doClearArea() const
1793 {
1794         return !locked || (need_update & (FULL|INIT));
1795 }
1796
1797
1798 void InsetText::selectAll(BufferView * bv)
1799 {
1800         getLyXText(bv)->cursorTop(bv);
1801         getLyXText(bv)->selection.cursor = getLyXText(bv)->cursor;
1802         getLyXText(bv)->cursorBottom(bv);
1803         getLyXText(bv)->setSelection(bv);
1804 }
1805
1806
1807 void InsetText::clearSelection(BufferView * bv)
1808 {
1809         getLyXText(bv)->clearSelection(bv);
1810 }
1811
1812
1813 void InsetText::clearInset(Painter & pain, int baseline, bool & cleared) const
1814 {
1815         int w =  insetWidth;
1816         int h = insetAscent + insetDescent;
1817         int ty = baseline - insetAscent;
1818         
1819         if (ty < 0) {
1820                 h += ty;
1821                 ty = 0;
1822         }
1823         if ((ty + h) > pain.paperHeight())
1824                 h = pain.paperHeight();
1825         if ((top_x + drawTextXOffset + w) > pain.paperWidth())
1826                 w = pain.paperWidth();
1827         pain.fillRectangle(top_x+drawTextXOffset, ty, w, h);
1828         cleared = true;
1829         need_update = FULL;
1830 }
1831 /* Emacs:
1832  * Local variables:
1833  * tab-width: 4
1834  * End:
1835  * vi:set tabstop=4:
1836  */