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