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