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