]> git.lyx.org Git - lyx.git/blob - src/insets/insettext.C
Fixes to various bug-reports.
[lyx.git] / src / insets / insettext.C
1 /* This file is part of
2  * ======================================================
3  * 
4  *           LyX, The Document Processor
5  *
6  *           Copyright 1998-2001 The LyX Team.
7  *
8  * ======================================================
9  */
10
11 #include <config.h>
12
13 #include <fstream>
14 #include <algorithm>
15
16 #include <cstdlib>
17 //#include <signal.h>
18
19 #ifdef __GNUG__
20 #pragma implementation
21 #endif
22
23 #include "insettext.h"
24 #include "paragraph.h"
25 #include "lyxlex.h"
26 #include "debug.h"
27 #include "lyxfont.h"
28 #include "commandtags.h"
29 #include "buffer.h"
30 #include "LyXView.h"
31 #include "BufferView.h"
32 #include "layout.h"
33 #include "LaTeXFeatures.h"
34 #include "Painter.h"
35 #include "lyx_gui_misc.h"
36 #include "lyxtext.h"
37 #include "lyxcursor.h"
38 #include "CutAndPaste.h"
39 #include "font.h"
40 #include "LColor.h"
41 #include "support/textutils.h"
42 #include "support/LAssert.h"
43 #include "support/lstrings.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 #include "gettext.h"
51 #include "lyxfunc.h"
52 #include "ParagraphParameters.h"
53 #include "undo_funcs.h"
54 #include "lyxfind.h"
55
56 using std::ostream;
57 using std::ifstream;
58 using std::endl;
59 using std::min;
60 using std::max;
61 using std::make_pair;
62
63 extern unsigned char getCurrentTextClass(Buffer *);
64 extern bool math_insert_greek(BufferView *, char);
65 extern int greek_kb_flag;
66
67
68 #warning this functions should probably go into bufferview_funcs somehow (Jug)
69
70 void InsetText::saveLyXTextState(LyXText * t) const
71 {
72         sstate.lpar = t->cursor.par();
73         sstate.pos = t->cursor.pos();
74         sstate.boundary = t->cursor.boundary();
75         sstate.selstartpar = t->selection.start.par();
76         sstate.selstartpos = t->selection.start.pos();
77         sstate.selstartboundary = t->selection.start.boundary();
78         sstate.selendpar = t->selection.end.par();
79         sstate.selendpos = t->selection.end.pos();
80         sstate.selendboundary = t->selection.end.boundary();
81         sstate.selection = t->selection.set();
82         sstate.mark_set = t->selection.mark();
83 }
84
85 void InsetText::restoreLyXTextState(BufferView * bv, LyXText * t) const
86 {
87         if (sstate.lpar) {
88                 t->selection.set(true);
89                 /* at this point just to avoid the Delete-Empty-Paragraph
90                  * Mechanism when setting the cursor */
91                 t->selection.mark(sstate.mark_set);
92                 if (sstate.selection) {
93                         t->setCursor(bv, sstate.selstartpar, sstate.selstartpos,
94                                      true, sstate.selstartboundary);
95                         t->selection.cursor = t->cursor;
96                         t->setCursor(bv, sstate.selendpar, sstate.selendpos,
97                                      true, sstate.selendboundary);
98                         t->setSelection(bv);
99                         t->setCursor(bv, sstate.lpar, sstate.pos);
100                 } else {
101                         t->setCursor(bv, sstate.lpar, sstate.pos, true, sstate.boundary);
102                         t->selection.cursor = t->cursor;
103                         t->selection.set(false);
104                 }
105         }
106 }
107
108
109 InsetText::InnerCache::InnerCache(boost::shared_ptr<LyXText> t)
110 {
111         text = t;
112         remove = false;
113 }
114
115
116 InsetText::InsetText()
117 {
118         par = new Paragraph;
119         init();
120         in_update = false;
121 }
122
123
124 InsetText::InsetText(InsetText const & ins, bool same_id)
125         : UpdatableInset()
126 {
127         par = 0;
128         init(&ins, same_id);
129         in_update = false;
130         autoBreakRows = ins.autoBreakRows;
131 }
132
133
134 InsetText & InsetText::operator=(InsetText const & it)
135 {
136         init(&it);
137         autoBreakRows = it.autoBreakRows;
138         return * this;
139 }
140
141
142 void InsetText::init(InsetText const * ins, bool same_id)
143 {
144         top_y = 0;
145         last_width = 0;
146         last_height = 0;
147         insetAscent = 0;
148         insetDescent = 0;
149         insetWidth = 0;
150         the_locking_inset = 0;
151         old_max_width = 0;
152         no_selection = false;
153         need_update = INIT;
154         drawTextXOffset = 0;
155         drawTextYOffset = 0;
156         autoBreakRows = false;
157         drawFrame_ = NEVER;
158         xpos = 0.0;
159         frame_color = LColor::insetframe;
160         if (ins) {
161                 setParagraphData(ins->par);
162                 autoBreakRows = ins->autoBreakRows;
163                 drawFrame_ = ins->drawFrame_;
164                 frame_color = ins->frame_color;
165                 if (same_id)
166                         id_ = ins->id_;
167         }
168         par->setInsetOwner(this);
169         locked = false;
170         old_par = 0;
171         last_drawn_width = -1;
172         frame_is_visible = false;
173         cached_bview = 0;
174         sstate.lpar = 0;
175         lt = 0;
176 }
177
178
179 InsetText::~InsetText()
180 {
181         cached_bview = 0;
182
183         // NOTE
184         
185         while (par) {
186                 Paragraph * tmp = par->next();
187                 delete par;
188                 par = tmp;
189         }
190 }
191
192
193 void InsetText::clear()
194 {
195         while (par) {
196                 Paragraph * tmp = par->next();
197                 delete par;
198                 par = tmp;
199         }
200         par = new Paragraph;
201         reinitLyXText();
202 }
203
204
205 Inset * InsetText::clone(Buffer const &, bool same_id) const
206 {
207         return  new InsetText(*this, same_id);
208 }
209
210
211 void InsetText::write(Buffer const * buf, ostream & os) const
212 {
213         os << "Text\n";
214         writeParagraphData(buf, os);
215 }
216
217
218 void InsetText::writeParagraphData(Buffer const * buf, ostream & os) const
219 {
220         par->writeFile(buf, os, buf->params, 0);
221 }
222
223
224 void InsetText::read(Buffer const * buf, LyXLex & lex)
225 {
226         string token;
227         int pos = 0;
228         Paragraph * return_par = 0;
229         Paragraph::depth_type depth = 0; 
230         LyXFont font(LyXFont::ALL_INHERIT);
231
232         clear();
233         
234         while (lex.IsOK()) {
235                 lex.nextToken();
236                 token = lex.GetString();
237                 if (token.empty())
238                         continue;
239                 if (token == "\\end_inset") {
240 #ifdef NO_LATEX
241                         const_cast<Buffer*>(buf)->insertErtContents(par, pos, font, false);
242 #endif
243                         break;
244                 }
245                 
246                 if (const_cast<Buffer*>(buf)->
247                         parseSingleLyXformat2Token(lex, par, return_par,
248                                                    token, pos, depth, font)) {
249                         // the_end read this should NEVER happen
250                         lex.printError("\\the_end read in inset! Error in document!");
251                         return;
252                 }
253         }
254         if (!return_par)
255                 return_par = par;
256         par = return_par;
257         while(return_par) {
258                 return_par->setInsetOwner(this);
259                 return_par = return_par->next();
260         }
261         
262         if (token != "\\end_inset") {
263                 lex.printError("Missing \\end_inset at this point. "
264                                            "Read: `$$Token'");
265         }
266         need_update = INIT;
267 }
268
269
270 int InsetText::ascent(BufferView * bv, LyXFont const &) const
271 {
272         int y_temp = 0;
273         Row * row = getLyXText(bv)->getRowNearY(y_temp);
274         insetAscent = row->ascent_of_text() + TEXT_TO_INSET_OFFSET;
275         return insetAscent;
276 }
277
278
279 int InsetText::descent(BufferView * bv, LyXFont const &) const
280 {
281         bool clear = false;
282         if (!lt) {
283                 lt = getLyXText(bv);
284                 clear = true;
285         }
286         int y_temp = 0;
287         Row * row = lt->getRowNearY(y_temp);
288         insetDescent = lt->height - row->ascent_of_text() +
289         TEXT_TO_INSET_OFFSET;
290         if (clear)
291                 lt = 0;
292         return insetDescent;
293 }
294
295
296 int InsetText::width(BufferView * bv, LyXFont const &) const
297 {
298         insetWidth = max(textWidth(bv),
299                          (int)getLyXText(bv)->width + (2 * TEXT_TO_INSET_OFFSET));
300         return insetWidth;
301 }
302
303
304 int InsetText::textWidth(BufferView * bv) const
305 {
306         if (!autoBreakRows)
307                 return -1;
308         int const w = getMaxWidth(bv, this);
309         return w;
310 }
311
312
313 void InsetText::draw(BufferView * bv, LyXFont const & f,
314                      int baseline, float & x, bool cleared) const
315 {
316         if (nodraw())
317                 return;
318
319         Painter & pain = bv->painter();
320
321         // no draw is necessary !!!
322         if ((drawFrame_ == LOCKED) && !locked && !par->size()) {
323                 top_x = int(x);
324                 top_baseline = baseline;
325                 x += width(bv, f);
326                 if (!cleared && (need_update & CLEAR_FRAME))
327                         clearFrame(pain, cleared);
328                 else if (cleared)
329                         frame_is_visible = false;
330                 need_update = NONE;
331                 return;
332         }
333
334         xpos = x;
335         if (!owner())
336                 x += static_cast<float>(scroll());
337
338         // if top_x differs we have a rule down and we don't have to clear anything
339         if (!cleared && (top_x == int(x)) &&
340                 ((need_update&(INIT|FULL)) || (top_baseline!=baseline) ||
341                  (last_drawn_width!=insetWidth))) {
342                 clearInset(pain, baseline, cleared);
343         }
344         if (cleared)
345                 frame_is_visible = false;
346
347         if (!cleared && (need_update == NONE))
348                 return;
349
350         if (top_x != int(x)) {
351                 if ((getMaxWidth(bv, this) > 0) &&
352                         (getLyXText(bv)->width != old_max_width)) {
353                         resizeLyXText(bv);
354                         need_update |= FULL;
355                         old_max_width = getLyXText(bv)->width;
356                         bv->text->status(bv, LyXText::CHANGED_IN_DRAW);
357                 }
358                 top_x = int(x);
359                 clearInset(pain, baseline, cleared);
360         }
361
362 //      lyxerr << "InsetText::draw[" << this << "](" << need_update << ":" << int(x) << ":" << top_x << ")\n";
363
364         if (cleared || (last_drawn_width != insetWidth)) {
365                 need_update |= FULL;
366                 last_drawn_width = insetWidth;
367         }
368
369         top_baseline = baseline;
370         top_y = baseline - ascent(bv, f);
371         last_width = width(bv, f);
372         last_height = ascent(bv, f) + descent(bv, f);
373
374         if (the_locking_inset && (cpar(bv) == inset_par)
375                 && (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)
380                 && !getLyXText(bv)->selection.set()) {
381                 drawFrame(pain, cleared);
382                 x += last_width; // was width(bv, f);
383                 need_update = NONE;
384                 return;
385         }
386         bool clear = false;
387         if (!lt) {
388                 lt = getLyXText(bv);
389                 clear = true;
390         }
391         x += TEXT_TO_INSET_OFFSET;
392
393         int y = 0;
394         Row * row = lt->getRowNearY(y);
395         int y_offset = baseline - row->ascent_of_text();
396         int ph = pain.paperHeight();
397         int first = 0;
398         y = y_offset;
399         while ((row != 0) && ((y+row->height()) <= 0)) {
400                 y += row->height();
401                 first += row->height();
402                 row = row->next();
403         }
404         if (y_offset < 0)
405                 y_offset = y;
406         lt->first = first;
407         if (cleared) {
408                 int yf = y_offset;
409                 y = 0;
410                 while ((row != 0) && (yf < ph)) {
411                         lt->getVisibleRow(bv, y+y_offset, int(x), row,
412                                                 y+first, cleared);
413                         y += row->height();
414                         yf += row->height();
415                         row = row->next();
416                 }
417         } else if (!locked) {
418                 if (need_update & CURSOR) {
419                         bv->screen()->toggleSelection(lt, bv, true, y_offset,int(x));
420                         lt->clearSelection();
421                         lt->selection.cursor = lt->cursor;
422                 }
423                 bv->screen()->update(lt, bv, y_offset, int(x));
424         } else {
425                 locked = false;
426                 if (need_update & SELECTION)
427                         bv->screen()->toggleToggle(lt, bv, y_offset, int(x));
428                 else if (need_update & CURSOR) {
429                         bv->screen()->toggleSelection(lt, bv, true, y_offset,int(x));
430                         lt->clearSelection();
431                         lt->selection.cursor = lt->cursor;
432                 }
433                 bv->screen()->update(lt, bv, y_offset, int(x));
434                 locked = true;
435         }
436
437         lt->refresh_y = 0;
438         lt->status(bv, LyXText::UNCHANGED);
439         if ((need_update != CURSOR_PAR) &&
440                 ((drawFrame_ == ALWAYS) || ((drawFrame_ == LOCKED) && locked)))
441                 drawFrame(pain, cleared);
442         else if (need_update & CLEAR_FRAME)
443                 clearFrame(pain, cleared);
444         x += last_width /* was width(bv, f) */ - TEXT_TO_INSET_OFFSET;
445         if (bv->text->status() == LyXText::CHANGED_IN_DRAW) {
446                 need_update |= INIT;
447         } else if (need_update != INIT)
448                 need_update = NONE;
449         if (clear)
450                 lt = 0;
451 }
452
453
454 void InsetText::drawFrame(Painter & pain, bool cleared) const
455 {
456         if (!frame_is_visible || cleared) {
457                 frame_x = top_x + 1;
458                 frame_y = top_baseline - insetAscent + 1;
459                 frame_w = insetWidth - 1;
460                 frame_h = insetAscent + insetDescent - 1;
461                 pain.rectangle(frame_x, frame_y, frame_w, frame_h,
462                                frame_color);
463                 frame_is_visible = true;
464         }
465 }
466
467
468 void InsetText::clearFrame(Painter & pain, bool cleared) const
469 {
470         if (frame_is_visible) {
471                 if (!cleared) {
472                         pain.rectangle(frame_x, frame_y, frame_w, frame_h,
473                                        backgroundColor());
474                 }
475                 frame_is_visible = false;
476         }
477 }
478
479
480 void InsetText::update(BufferView * bv, LyXFont const & font, bool reinit)
481 {
482         if (in_update)
483                 return;
484         in_update = true;
485         if (reinit) {
486                 need_update |= INIT;
487                 resizeLyXText(bv);
488                 if (owner())
489                         owner()->update(bv, font, true);
490                 in_update = false;
491                 return;
492         }
493         if (the_locking_inset) {
494                 inset_x = cx(bv) - top_x + drawTextXOffset;
495                 inset_y = cy(bv) + drawTextYOffset;
496                 the_locking_inset->update(bv, font, reinit);
497         }
498
499         bool clear = false;
500         if (!lt) {
501                 lt = getLyXText(bv);
502                 clear = true;
503         }
504         int oldw = insetWidth;
505         insetWidth = lt->width + (2 * TEXT_TO_INSET_OFFSET);
506         if (oldw != insetWidth) {
507                 resizeLyXText(bv);
508                 need_update |= FULL;
509                 if (clear)
510                         lt = 0;
511                 in_update = false;
512                 return;
513         }
514         if ((need_update & CURSOR_PAR) && (lt->status() == LyXText::UNCHANGED) &&
515                 the_locking_inset)
516         {
517                 lt->updateInset(bv, the_locking_inset);
518         }
519         if (lt->status() == LyXText::NEED_MORE_REFRESH)
520                 need_update |= FULL;
521         if (clear)
522                 lt = 0;
523         in_update = false;
524 }
525
526
527 void InsetText::setUpdateStatus(BufferView * bv, int what) const
528 {
529         bool clear = false;
530         if (!lt) {
531                 lt = getLyXText(bv);
532                 clear = true;
533         }
534         need_update |= what;
535         if (lt->status() == LyXText::NEED_MORE_REFRESH)
536                 need_update |= FULL;
537         else if (lt->status() == LyXText::NEED_VERY_LITTLE_REFRESH)
538                 need_update |= CURSOR_PAR;
539
540         // this to not draw a selection when we redraw all of it!
541         if (need_update & CURSOR) {
542                 if (lt->selection.set())
543                         need_update = FULL;
544                 lt->clearSelection();
545         }
546         if (clear)
547                 lt = 0;
548 }
549
550
551 void InsetText::updateLocal(BufferView * bv, int what, bool mark_dirty) const
552 {
553         bool clear = false;
554         if (!lt) {
555                 lt = getLyXText(bv);
556                 clear = true;
557         }
558         lt->fullRebreak(bv);
559         setUpdateStatus(bv, what);
560         if ((need_update != CURSOR) || (lt->status() != LyXText::UNCHANGED) ||
561                 lt->selection.set())
562         {
563                 bv->updateInset(const_cast<InsetText *>(this), mark_dirty);
564         }
565         bv->owner()->showState();
566         if (old_par != cpar(bv)) {
567                 bv->owner()->setLayout(cpar(bv)->getLayout());
568                 old_par = cpar(bv);
569         }
570         if (clear)
571                 lt = 0;
572 }
573
574
575 string const InsetText::editMessage() const
576 {
577         return _("Opened Text Inset");
578 }
579
580
581 void InsetText::edit(BufferView * bv, int x, int y, unsigned int button)
582 {
583         UpdatableInset::edit(bv, x, y, button);
584         
585         if (!bv->lockInset(this)) {
586                 lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
587                 return;
588         }
589         locked = true;
590         the_locking_inset = 0;
591         inset_pos = inset_x = inset_y = 0;
592         inset_boundary = false;
593         inset_par = 0;
594         old_par = 0;
595         int tmp_y = (y < 0) ? 0 : y;
596         bool clear = false;
597         if (!lt) {
598                 lt = getLyXText(bv);
599                 clear = true;
600         }
601
602         if (!checkAndActivateInset(bv, x, tmp_y, button))
603                 lt->setCursorFromCoordinates(bv, x - drawTextXOffset,
604                                             y + insetAscent);
605         lt->clearSelection();
606         finishUndo();
607         showInsetCursor(bv);
608         updateLocal(bv, CURSOR, false);
609
610         // If the inset is empty set the language of the current font to the
611         // language to the surronding text (if different).
612         if (par->size() == 0 && !par->next() &&
613                 bv->getParentLanguage(this) != lt->current_font.language()) {
614                 LyXFont font(LyXFont::ALL_IGNORE);
615                 font.setLanguage(bv->getParentLanguage(this));
616                 setFont(bv, font, false);
617         }
618         if (clear)
619                 lt = 0;
620 }
621
622
623 void InsetText::edit(BufferView * bv, bool front)
624 {
625         UpdatableInset::edit(bv, front);
626         
627         if (!bv->lockInset(this)) {
628                 lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
629                 return;
630         }
631         locked = true;
632         the_locking_inset = 0;
633         inset_pos = inset_x = inset_y = 0;
634         inset_boundary = false;
635         inset_par = 0;
636         old_par = 0;
637         bool clear = false;
638         if (!lt) {
639                 lt = getLyXText(bv);
640                 clear = true;
641         }
642         if (front)
643                 lt->setCursor(bv, par, 0);
644         else {
645                 Paragraph * p = par;
646                 while(p->next())
647                         p = p->next();
648                 lt->setCursor(bv, p, p->size()-1);
649         }
650         lt->clearSelection();
651         finishUndo();
652         showInsetCursor(bv);
653         updateLocal(bv, CURSOR, false);
654
655         // If the inset is empty set the language of the current font to the
656         // language to the surronding text (if different).
657         if (par->size() == 0 && !par->next() &&
658                 bv->getParentLanguage(this) != lt->current_font.language()) {
659                 LyXFont font(LyXFont::ALL_IGNORE);
660                 font.setLanguage(bv->getParentLanguage(this));
661                 setFont(bv, font, false);
662         }
663         if (clear)
664                 lt = 0;
665 }
666
667
668 void InsetText::insetUnlock(BufferView * bv)
669 {
670         if (the_locking_inset) {
671                 the_locking_inset->insetUnlock(bv);
672                 the_locking_inset = 0;
673         }
674         hideInsetCursor(bv);
675         no_selection = false;
676         locked = false;
677         int code;
678         if (drawFrame_ == LOCKED)
679                 code = CURSOR|CLEAR_FRAME;
680         else 
681                 code = CURSOR;
682         bool clear = false;
683         if (!lt) {
684                 lt = getLyXText(bv);
685                 clear = true;
686         }
687         if (lt->selection.set()) {
688                 lt->clearSelection();
689                 code = FULL;
690         } else if (owner()) {
691                 bv->owner()->setLayout(owner()->getLyXText(bv)
692                                        ->cursor.par()->getLayout());
693         } else
694                 bv->owner()->setLayout(bv->text->cursor.par()->getLayout());
695         updateLocal(bv, code, false);
696         if (clear)
697                 lt = 0;
698 }
699
700
701 bool InsetText::lockInsetInInset(BufferView * bv, UpdatableInset * inset)
702 {
703         lyxerr[Debug::INSETS] << "InsetText::LockInsetInInset("
704                               << inset << "): ";
705         if (!inset)
706                 return false;
707         if (inset == cpar(bv)->getInset(cpos(bv))) {
708                 lyxerr[Debug::INSETS] << "OK" << endl;
709                 the_locking_inset = inset;
710                 inset_x = cx(bv) - top_x + drawTextXOffset;
711                 inset_y = cy(bv) + drawTextYOffset;
712                 inset_pos = cpos(bv);
713                 inset_par = cpar(bv);
714                 inset_boundary = cboundary(bv);
715                 updateLocal(bv, CURSOR, false);
716                 return true;
717         } else if (the_locking_inset && (the_locking_inset == inset)) {
718                 if (cpar(bv) == inset_par && cpos(bv) == inset_pos) {
719                         lyxerr[Debug::INSETS] << "OK" << endl;
720                         inset_x = cx(bv) - top_x + drawTextXOffset;
721                         inset_y = cy(bv) + drawTextYOffset;
722                 } else {
723                         lyxerr[Debug::INSETS] << "cursor.pos != inset_pos" << endl;
724                 }
725         } else if (the_locking_inset) {
726                 lyxerr[Debug::INSETS] << "MAYBE" << endl;
727                 return the_locking_inset->lockInsetInInset(bv, inset);
728         }
729         lyxerr[Debug::INSETS] << "NOT OK" << endl;
730         return false;
731 }
732
733
734 bool InsetText::unlockInsetInInset(BufferView * bv, UpdatableInset * inset,
735                                    bool lr)
736 {
737         if (!the_locking_inset)
738                 return false;
739         if (the_locking_inset == inset) {
740                 the_locking_inset->insetUnlock(bv);
741                 getLyXText(bv)->updateInset(bv, inset);
742                 the_locking_inset = 0;
743                 if (lr)
744                         moveRight(bv, false);
745                 old_par = 0; // force layout setting
746                 if (scroll())
747                         scroll(bv, 0.0F);
748                 else
749                         updateLocal(bv, CURSOR, false);
750                 return true;
751         }
752         return the_locking_inset->unlockInsetInInset(bv, inset, lr);
753 }
754
755
756 bool InsetText::updateInsetInInset(BufferView * bv, Inset * inset)
757 {
758         if (!the_locking_inset)
759                 return false;
760         if (the_locking_inset != inset) {
761                 getLyXText(bv)->updateInset(bv, the_locking_inset);
762                 setUpdateStatus(bv, CURSOR_PAR);
763                 return the_locking_inset->updateInsetInInset(bv, inset);
764         }
765 //    updateLocal(bv, FULL, false);
766         if (getLyXText(bv)->updateInset(bv, inset))
767                 updateLocal(bv, CURSOR_PAR, false);
768         if (cpar(bv) == inset_par && cpos(bv) == inset_pos) {
769                 inset_x = cx(bv) - top_x + drawTextXOffset;
770                 inset_y = cy(bv) + drawTextYOffset;
771         }
772         return true;
773 }
774
775
776 void InsetText::insetButtonPress(BufferView * bv, int x, int y, int button)
777 {
778         no_selection = true;
779
780         int tmp_x = x - drawTextXOffset;
781         int tmp_y = y + insetAscent - getLyXText(bv)->first;
782         Inset * inset = bv->checkInsetHit(getLyXText(bv), tmp_x, tmp_y, button);
783
784         hideInsetCursor(bv);
785         if (the_locking_inset) {
786                 if (the_locking_inset == inset) {
787                         the_locking_inset->insetButtonPress(bv,x-inset_x,y-inset_y,button);
788                         no_selection = false;
789                         return;
790                 } else if (inset) {
791                         // otherwise unlock the_locking_inset and lock the new inset
792                         the_locking_inset->insetUnlock(bv);
793                         inset_x = cx(bv) - top_x + drawTextXOffset;
794                         inset_y = cy(bv) + drawTextYOffset;
795                         the_locking_inset = static_cast<UpdatableInset*>(inset);
796                         inset->insetButtonPress(bv, x - inset_x, y - inset_y, button);
797                         inset->edit(bv, x - inset_x, y - inset_y, button);
798                         if (the_locking_inset)
799                                 updateLocal(bv, CURSOR, false);
800                         no_selection = false;
801                         return;
802                 }
803                 // otherwise only unlock the_locking_inset
804                 the_locking_inset->insetUnlock(bv);
805                 the_locking_inset = 0;
806         }
807         if (bv->theLockingInset()) {
808                 if (inset && inset->editable() == Inset::HIGHLY_EDITABLE) {
809                         UpdatableInset * uinset = static_cast<UpdatableInset*>(inset);
810                         inset_x = cx(bv) - top_x + drawTextXOffset;
811                         inset_y = cy(bv) + drawTextYOffset;
812                         inset_pos = cpos(bv);
813                         inset_par = cpar(bv);
814                         inset_boundary = cboundary(bv);
815                         the_locking_inset = uinset;
816                         uinset->insetButtonPress(bv, x - inset_x, y - inset_y,
817                                                  button);
818                         uinset->edit(bv, x - inset_x, y - inset_y, 0);
819                         if (the_locking_inset)
820                                 updateLocal(bv, CURSOR, false);
821                         no_selection = false;
822                         return;
823                 }
824         }
825         if (!inset) { // && (button == 2)) {
826                 bool paste_internally = false;
827                 if ((button == 2) && getLyXText(bv)->selection.set()) {
828                         localDispatch(bv, LFUN_COPY, "");
829                         paste_internally = true;
830                 }
831                 getLyXText(bv)->setCursorFromCoordinates(bv, x-drawTextXOffset,
832                                                          y + insetAscent);
833                 getLyXText(bv)->clearSelection();
834                 updateLocal(bv, FULL, false);
835                 getLyXText(bv)->selection.cursor = getLyXText(bv)->cursor;
836                 bv->owner()->setLayout(cpar(bv)->getLayout());
837                 old_par = cpar(bv);
838                 // Insert primary selection with middle mouse
839                 // if there is a local selection in the current buffer,
840                 // insert this
841                 if (button == 2) {
842                         if (paste_internally)
843                                 localDispatch(bv, LFUN_PASTE, "");
844                         else
845                                 localDispatch(bv, LFUN_PASTESELECTION,
846                                               "paragraph");
847                 }
848         }
849         showInsetCursor(bv);
850         no_selection = false;
851 }
852
853
854 void InsetText::insetButtonRelease(BufferView * bv, int x, int y, int button)
855 {
856         UpdatableInset * inset = 0;
857
858         if (the_locking_inset) {
859                 the_locking_inset->insetButtonRelease(bv,
860                                                       x - inset_x, y - inset_y,
861                                                       button);
862         } else {
863                 if (cpar(bv)->getChar(cpos(bv)) == Paragraph::META_INSET) {
864                         inset = static_cast<UpdatableInset*>(cpar(bv)->getInset(cpos(bv)));
865                         if (inset->editable() == Inset::HIGHLY_EDITABLE) {
866                                 inset->insetButtonRelease(bv,
867                                                           x - inset_x,
868                                                           y - inset_y, button);
869                         } else {
870                                 inset_x = cx(bv) - top_x + drawTextXOffset;
871                                 inset_y = cy(bv) + drawTextYOffset;
872                                 inset->insetButtonRelease(bv,
873                                                           x - inset_x,
874                                                           y - inset_y, button);
875                                 inset->edit(bv,
876                                             x - inset_x, y - inset_y, button);
877                         }
878                         updateLocal(bv, CURSOR_PAR, false);
879                 }
880         }
881         no_selection = false;
882 }
883
884
885 void InsetText::insetMotionNotify(BufferView * bv, int x, int y, int state)
886 {
887         if (no_selection)
888                 return;
889         if (the_locking_inset) {
890                 the_locking_inset->insetMotionNotify(bv, x - inset_x,
891                                                      y - inset_y,state);
892                 return;
893         }
894         LyXText * t = getLyXText(bv);
895         hideInsetCursor(bv);
896         t->setCursorFromCoordinates(bv, x - drawTextXOffset, y + insetAscent);
897         t->setSelection(bv);
898         if (t->toggle_cursor.par() != t->toggle_end_cursor.par() ||
899                 t->toggle_cursor.pos() != t->toggle_end_cursor.pos())
900                 updateLocal(bv, SELECTION, false);
901         showInsetCursor(bv);
902 }
903
904
905 void InsetText::insetKeyPress(XKeyEvent * xke)
906 {
907         if (the_locking_inset) {
908                 the_locking_inset->insetKeyPress(xke);
909                 return;
910         }
911 }
912
913
914 UpdatableInset::RESULT
915 InsetText::localDispatch(BufferView * bv,
916                          kb_action action, string const & arg)
917 {
918         no_selection = false;
919         UpdatableInset::RESULT
920                 result= UpdatableInset::localDispatch(bv, action, arg);
921         if (result != UNDISPATCHED) {
922                 return DISPATCHED;
923         }
924
925         result = DISPATCHED;
926         if ((action < 0) && arg.empty())
927                 return FINISHED;
928
929         if (the_locking_inset) {
930                 result = the_locking_inset->localDispatch(bv, action, arg);
931                 if (result == DISPATCHED_NOUPDATE)
932                         return result;
933                 else if (result == DISPATCHED) {
934                         updateLocal(bv, CURSOR_PAR, false);
935                         return result;
936                 } else if (result == FINISHED) {
937                         bool dispatched = false;
938                         switch (action) {
939                         case LFUN_UNKNOWN_ACTION:
940                         case LFUN_BREAKPARAGRAPH:
941                         case LFUN_BREAKLINE:
942                                 moveRightIntern(bv, false, false);
943                                 break;
944                         case LFUN_RIGHT:
945                                 if (!getLyXText(bv)->cursor.par()->isRightToLeftPar(bv->buffer()->params))
946                                         moveRightIntern(bv, false, false);
947                                 dispatched = true;
948                                 break;
949                         case LFUN_LEFT:
950                                 if (getLyXText(bv)->cursor.par()->isRightToLeftPar(bv->buffer()->params))
951                                         moveRightIntern(bv, false, false);
952                                 dispatched = true;
953                                 break;
954                         default:
955                                 break;
956                         }
957                         the_locking_inset = 0;
958                         if (dispatched)
959                                 return DISPATCHED;
960                 }
961         }
962         hideInsetCursor(bv);
963         bool clear = false;
964         if (!lt) {
965                 lt = getLyXText(bv);
966                 clear = true;
967         }
968         switch (action) {
969         // Normal chars
970         case LFUN_SELFINSERT:
971                 if (bv->buffer()->isReadonly()) {
972 //          setErrorMessage(N_("Document is read only"));
973                         break;
974                 }
975                 if (!arg.empty()) {
976                         /* Automatically delete the currently selected
977                          * text and replace it with what is being
978                          * typed in now. Depends on lyxrc settings
979                          * "auto_region_delete", which defaults to
980                          * true (on). */
981
982                         setUndo(bv, Undo::INSERT,
983                                 lt->cursor.par(), lt->cursor.par()->next());
984                         bv->setState();
985                         if (lyxrc.auto_region_delete) {
986                                 if (lt->selection.set()) {
987                                         lt->cutSelection(bv, false);
988                                 }
989                         }
990                         lt->clearSelection();
991                         for (string::size_type i = 0; i < arg.length(); ++i) {
992                                 if (greek_kb_flag) {
993                                         if (!math_insert_greek(bv, arg[i])) {
994                                                 bv->owner()->getIntl()->getTrans().TranslateAndInsert(arg[i], lt);
995                                         } else if (!the_locking_inset) {
996                                                 (void)moveRight(bv, false);
997                                         }
998                                 } else {
999                                         bv->owner()->getIntl()->getTrans().TranslateAndInsert(arg[i], lt);
1000                                 }
1001                         }
1002                 }
1003                 lt->selection.cursor = lt->cursor;
1004                 updateLocal(bv, CURSOR_PAR, true);
1005                 result = DISPATCHED_NOUPDATE;
1006                 break;
1007                 // --- Cursor Movements -----------------------------------
1008         case LFUN_RIGHTSEL:
1009                 finishUndo();
1010                 moveRight(bv, false, true);
1011                 lt->setSelection(bv);
1012                 updateLocal(bv, SELECTION, false);
1013                 break;
1014         case LFUN_RIGHT:
1015                 result = moveRight(bv);
1016                 finishUndo();
1017                 updateLocal(bv, CURSOR, false);
1018                 break;
1019         case LFUN_LEFTSEL:
1020                 finishUndo();
1021                 moveLeft(bv, false, true);
1022                 lt->setSelection(bv);
1023                 updateLocal(bv, SELECTION, false);
1024                 break;
1025         case LFUN_LEFT:
1026                 finishUndo();
1027                 result = moveLeft(bv);
1028                 updateLocal(bv, CURSOR, false);
1029                 break;
1030         case LFUN_DOWNSEL:
1031                 finishUndo();
1032                 moveDown(bv);
1033                 lt->setSelection(bv);
1034                 updateLocal(bv, SELECTION, false);
1035                 break;
1036         case LFUN_DOWN:
1037                 finishUndo();
1038                 result = moveDown(bv);
1039                 updateLocal(bv, CURSOR, false);
1040                 break;
1041         case LFUN_UPSEL:
1042                 finishUndo();
1043                 moveUp(bv);
1044                 lt->setSelection(bv);
1045                 updateLocal(bv, SELECTION, false);
1046                 break;
1047         case LFUN_UP:
1048                 finishUndo();
1049                 result = moveUp(bv);
1050                 updateLocal(bv, CURSOR, false);
1051                 break;
1052         case LFUN_HOME:
1053                 finishUndo();
1054                 lt->cursorHome(bv);
1055                 updateLocal(bv, CURSOR, false);
1056                 break;
1057         case LFUN_END:
1058                 lt->cursorEnd(bv);
1059                 updateLocal(bv, CURSOR, false);
1060                 break;
1061         case LFUN_BACKSPACE: {
1062                 setUndo(bv, Undo::DELETE,
1063                         lt->cursor.par(), lt->cursor.par()->next());
1064                 if (lt->selection.set())
1065                         lt->cutSelection(bv);
1066                 else
1067                         lt->backspace(bv);
1068                 updateLocal(bv, CURSOR_PAR, true);
1069         }
1070         break;
1071         
1072         case LFUN_DELETE: {
1073                 setUndo(bv, Undo::DELETE,
1074                         lt->cursor.par(), lt->cursor.par()->next());
1075                 if (lt->selection.set()) {
1076                         lt->cutSelection(bv);
1077                 } else {
1078                         lt->Delete(bv);
1079                 }
1080                 updateLocal(bv, CURSOR_PAR, true);
1081         }
1082         break;
1083         
1084         case LFUN_CUT: {
1085                 setUndo(bv, Undo::DELETE,
1086                         lt->cursor.par(), lt->cursor.par()->next());
1087                 lt->cutSelection(bv);
1088                 updateLocal(bv, CURSOR_PAR, true);
1089         }
1090         break;
1091
1092         case LFUN_COPY:
1093                 finishUndo();
1094                 lt->copySelection(bv);
1095                 updateLocal(bv, CURSOR_PAR, false);
1096                 break;
1097         case LFUN_PASTESELECTION:
1098         {
1099                 string const clip(bv->getClipboard());
1100         
1101                 if (clip.empty())
1102                         break;
1103                 if (arg == "paragraph") {
1104                         lt->insertStringAsParagraphs(bv, clip);
1105                 } else {
1106                         lt->insertStringAsLines(bv, clip);
1107                 }
1108                 updateLocal(bv, CURSOR_PAR, true);
1109                 break;
1110         }
1111         case LFUN_PASTE: {
1112                 if (!autoBreakRows) {
1113
1114                         if (CutAndPaste::nrOfParagraphs() > 1) {
1115                                 WriteAlert(_("Impossible operation"),
1116                                                    _("Cannot include more than one paragraph!"),
1117                                                    _("Sorry."));
1118                                 break;
1119                         }
1120                 }
1121                 setUndo(bv, Undo::INSERT,
1122                         lt->cursor.par(), lt->cursor.par()->next());
1123                 lt->pasteSelection(bv);
1124                 updateLocal(bv, CURSOR_PAR, true);
1125         }
1126         break;
1127
1128         case LFUN_BREAKPARAGRAPH:
1129                 if (!autoBreakRows) {
1130                         result = DISPATCHED;
1131                         break;
1132                 }
1133                 lt->breakParagraph(bv, 0);
1134                 updateLocal(bv, FULL, true);
1135                 break;
1136         case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
1137                 if (!autoBreakRows) {
1138                         result = DISPATCHED;
1139                         break;
1140                 }
1141                 lt->breakParagraph(bv, 1);
1142                 updateLocal(bv, FULL, true);
1143                 break;
1144
1145         case LFUN_BREAKLINE: {
1146                 if (!autoBreakRows) {
1147                         result = DISPATCHED;
1148                         break;
1149                 }
1150                 setUndo(bv, Undo::INSERT,
1151                         lt->cursor.par(), lt->cursor.par()->next());
1152                 lt->insertChar(bv, Paragraph::META_NEWLINE);
1153                 updateLocal(bv, CURSOR_PAR, true);
1154         }
1155         break;
1156
1157         case LFUN_LAYOUT:
1158                 // do not set layouts on non breakable textinsets
1159                 if (autoBreakRows) {
1160                         LyXTextClass::size_type cur_layout = cpar(bv)->layout;
1161           
1162                         // Derive layout number from given argument (string)
1163                         // and current buffer's textclass (number). */    
1164                         LyXTextClassList::ClassList::size_type tclass =
1165                                 bv->buffer()->params.textclass;
1166                         std::pair <bool, LyXTextClass::size_type> layout = 
1167                                 textclasslist.NumberOfLayout(tclass, arg);
1168
1169                         // If the entry is obsolete, use the new one instead.
1170                         if (layout.first) {
1171                                 string obs = textclasslist.Style(tclass,layout.second).
1172                                         obsoleted_by();
1173                                 if (!obs.empty()) 
1174                                         layout = textclasslist.NumberOfLayout(tclass, obs);
1175                         }
1176
1177                         // see if we found the layout number:
1178                         if (!layout.first) {
1179                                 string const msg = string(N_("Layout ")) + arg + N_(" not known");
1180                                 bv->owner()->getLyXFunc()->dispatch(LFUN_MESSAGE, msg);
1181                                 break;
1182                         }
1183
1184                         if (cur_layout != layout.second) {
1185                                 cur_layout = layout.second;
1186                                 lt->setLayout(bv, layout.second);
1187                                 bv->owner()->setLayout(cpar(bv)->getLayout());
1188                                 updateLocal(bv, CURSOR_PAR, true);
1189                         }
1190                 } else {
1191                         // reset the layout box
1192                         bv->owner()->setLayout(cpar(bv)->getLayout());
1193                 }
1194                 break;
1195         case LFUN_PARAGRAPH_SPACING:
1196                 // This one is absolutely not working. When fiddling with this
1197                 // it also seems to me that the paragraphs inside the insettext
1198                 // inherit bufferparams/paragraphparams in a strange way. (Lgb)
1199         {
1200                 Paragraph * par = lt->cursor.par();
1201                 Spacing::Space cur_spacing = par->params().spacing().getSpace();
1202                 float cur_value = 1.0;
1203                 if (cur_spacing == Spacing::Other) {
1204                         cur_value = par->params().spacing().getValue();
1205                 }
1206                                 
1207                 istringstream istr(arg.c_str());
1208                 string tmp;
1209                 istr >> tmp;
1210                 Spacing::Space new_spacing = cur_spacing;
1211                 float new_value = cur_value;
1212                 if (tmp.empty()) {
1213                         lyxerr << "Missing argument to `paragraph-spacing'"
1214                                    << endl;
1215                 } else if (tmp == "single") {
1216                         new_spacing = Spacing::Single;
1217                 } else if (tmp == "onehalf") {
1218                         new_spacing = Spacing::Onehalf;
1219                 } else if (tmp == "double") {
1220                         new_spacing = Spacing::Double;
1221                 } else if (tmp == "other") {
1222                         new_spacing = Spacing::Other;
1223                         float tmpval = 0.0;
1224                         istr >> tmpval;
1225                         lyxerr << "new_value = " << tmpval << endl;
1226                         if (tmpval != 0.0)
1227                                 new_value = tmpval;
1228                 } else if (tmp == "default") {
1229                         new_spacing = Spacing::Default;
1230                 } else {
1231                         lyxerr << _("Unknown spacing argument: ")
1232                                    << arg << endl;
1233                 }
1234                 if (cur_spacing != new_spacing || cur_value != new_value) {
1235                         par->params().spacing(Spacing(new_spacing, new_value));
1236                         updateLocal(bv, CURSOR_PAR, true);
1237                 }
1238         }
1239         break;
1240         
1241         default:
1242                 if (!bv->Dispatch(action, arg))
1243                         result = UNDISPATCHED;
1244                 break;
1245         }
1246
1247         /// If the action has deleted all text in the inset, we need to change the
1248         // language to the language of the surronding text.
1249         if (par->size() == 0 && !par->next()) {
1250                 LyXFont font(LyXFont::ALL_IGNORE);
1251                 font.setLanguage(bv->getParentLanguage(this));
1252                 setFont(bv, font, false);
1253         }
1254
1255         if (result != FINISHED) {
1256                 showInsetCursor(bv);
1257         } else
1258                 bv->unlockInset(this);
1259         if (clear)
1260                 lt = 0;
1261         return result;
1262 }
1263
1264
1265 int InsetText::latex(Buffer const * buf, ostream & os, bool, bool) const
1266 {
1267         TexRow texrow;
1268         buf->latexParagraphs(os, par, 0, texrow);
1269         return texrow.rows();
1270 }
1271
1272
1273 int InsetText::ascii(Buffer const * buf, ostream & os, int linelen) const
1274 {
1275         Paragraph * p = par;
1276         unsigned int lines = 0;
1277         
1278         while (p) {
1279                 string const tmp = buf->asciiParagraph(p, linelen);
1280                 lines += countChar(tmp, '\n');
1281                 os << tmp;
1282                 p = p->next();
1283         }
1284         return lines;
1285 }
1286
1287
1288 int InsetText::docBook(Buffer const * buf, ostream & os) const
1289 {
1290         Paragraph * p = par;
1291         unsigned int lines = 0;
1292         int desc = 0;
1293         
1294         string tmp;
1295         while (p) {
1296                 buf->simpleDocBookOnePar(os, tmp, p, desc, 0);
1297                 p = p->next();
1298         }
1299         
1300         return lines;
1301 }
1302
1303
1304 void InsetText::validate(LaTeXFeatures & features) const
1305 {
1306         Paragraph * p = par;
1307         while (p) {
1308                 p->validate(features);
1309                 p = p->next();
1310         }
1311 }
1312
1313
1314 int InsetText::beginningOfMainBody(Buffer const * buf, Paragraph * p) const
1315 {
1316         if (textclasslist.Style(buf->params.textclass,
1317                                 p->getLayout()).labeltype != LABEL_MANUAL)
1318                 return 0;
1319         else
1320                 return p->beginningOfMainBody();
1321 }
1322
1323
1324 void InsetText::getCursorPos(BufferView * bv,
1325                              int & x, int & y) const
1326 {
1327         if (the_locking_inset) {
1328                 the_locking_inset->getCursorPos(bv, x, y);
1329                 return;
1330         }
1331         x = cx(bv);
1332         y = cy(bv);
1333 }
1334
1335
1336 unsigned int InsetText::insetInInsetY()
1337 {
1338         if (!the_locking_inset)
1339                 return 0;
1340
1341         return (inset_y + the_locking_inset->insetInInsetY());
1342 }
1343
1344
1345 void InsetText::toggleInsetCursor(BufferView * bv)
1346 {
1347         if (the_locking_inset) {
1348                 the_locking_inset->toggleInsetCursor(bv);
1349                 return;
1350         }
1351
1352         LyXFont const font(getLyXText(bv)->getFont(bv->buffer(), cpar(bv), cpos(bv)));
1353
1354         int const asc = lyxfont::maxAscent(font);
1355         int const desc = lyxfont::maxDescent(font);
1356   
1357         if (isCursorVisible())
1358                 bv->hideLockedInsetCursor();
1359         else
1360                 bv->showLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1361         toggleCursorVisible();
1362 }
1363
1364
1365 void InsetText::showInsetCursor(BufferView * bv, bool show)
1366 {
1367         if (the_locking_inset) {
1368                 the_locking_inset->showInsetCursor(bv, show);
1369                 return;
1370         }
1371         if (!isCursorVisible()) {
1372                 LyXFont const font =
1373                         getLyXText(bv)->getFont(bv->buffer(), cpar(bv), cpos(bv));
1374         
1375                 int const asc = lyxfont::maxAscent(font);
1376                 int const desc = lyxfont::maxDescent(font);
1377
1378                 bv->fitLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1379                 if (show)
1380                         bv->showLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1381                 setCursorVisible(true);
1382         }
1383 }
1384
1385
1386 void InsetText::hideInsetCursor(BufferView * bv)
1387 {
1388         if (isCursorVisible()) {
1389                 bv->hideLockedInsetCursor();
1390                 setCursorVisible(false);
1391         }
1392         if (the_locking_inset)
1393                 the_locking_inset->hideInsetCursor(bv);
1394 }
1395
1396
1397 void InsetText::fitInsetCursor(BufferView * bv) const
1398 {
1399         if (the_locking_inset) {
1400                 the_locking_inset->fitInsetCursor(bv);
1401                 return;
1402         }
1403         LyXFont const font =
1404                 getLyXText(bv)->getFont(bv->buffer(), cpar(bv), cpos(bv));
1405         
1406         int const asc = lyxfont::maxAscent(font);
1407         int const desc = lyxfont::maxDescent(font);
1408
1409         bv->fitLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1410 }
1411
1412
1413 UpdatableInset::RESULT
1414 InsetText::moveRight(BufferView * bv, bool activate_inset, bool selecting)
1415 {
1416         if (getLyXText(bv)->cursor.par()->isRightToLeftPar(bv->buffer()->params))
1417                 return moveLeftIntern(bv, false, activate_inset, selecting);
1418         else
1419                 return moveRightIntern(bv, false, activate_inset, selecting);
1420 }
1421
1422
1423 UpdatableInset::RESULT
1424 InsetText::moveLeft(BufferView * bv, bool activate_inset, bool selecting)
1425 {
1426         if (getLyXText(bv)->cursor.par()->isRightToLeftPar(bv->buffer()->params))
1427                 return moveRightIntern(bv, true, activate_inset, selecting);
1428         else
1429                 return moveLeftIntern(bv, true, activate_inset, selecting);
1430 }
1431
1432
1433 UpdatableInset::RESULT
1434 InsetText::moveRightIntern(BufferView * bv, bool behind, 
1435                            bool activate_inset, bool selecting)
1436 {
1437         if (!cpar(bv)->next() && (cpos(bv) >= cpar(bv)->size()))
1438                 return FINISHED;
1439         if (activate_inset && checkAndActivateInset(bv, behind))
1440                 return DISPATCHED;
1441         getLyXText(bv)->cursorRight(bv);
1442         if (!selecting)
1443                 getLyXText(bv)->selection.cursor = getLyXText(bv)->cursor;
1444         return DISPATCHED_NOUPDATE;
1445 }
1446
1447
1448 UpdatableInset::RESULT
1449 InsetText::moveLeftIntern(BufferView * bv, bool behind,
1450                           bool activate_inset, bool selecting)
1451 {
1452         if (!cpar(bv)->previous() && (cpos(bv) <= 0))
1453                 return FINISHED;
1454         getLyXText(bv)->cursorLeft(bv);
1455         if (!selecting)
1456                 getLyXText(bv)->selection.cursor = getLyXText(bv)->cursor;
1457         if (activate_inset && checkAndActivateInset(bv, behind))
1458                 return DISPATCHED;
1459         return DISPATCHED_NOUPDATE;
1460 }
1461
1462
1463 UpdatableInset::RESULT
1464 InsetText::moveUp(BufferView * bv)
1465 {
1466         if (!crow(bv)->previous())
1467                 return FINISHED;
1468         getLyXText(bv)->cursorUp(bv);
1469         return DISPATCHED_NOUPDATE;
1470 }
1471
1472
1473 UpdatableInset::RESULT
1474 InsetText::moveDown(BufferView * bv)
1475 {
1476         if (!crow(bv)->next())
1477                 return FINISHED;
1478         getLyXText(bv)->cursorDown(bv);
1479         return DISPATCHED_NOUPDATE;
1480 }
1481
1482
1483 bool InsetText::insertInset(BufferView * bv, Inset * inset)
1484 {
1485         if (the_locking_inset) {
1486                 if (the_locking_inset->insetAllowed(inset))
1487                         return the_locking_inset->insertInset(bv, inset);
1488                 return false;
1489         }
1490         inset->setOwner(this);
1491         hideInsetCursor(bv);
1492
1493         bool clear = false;
1494         if (!lt) {
1495                 lt = getLyXText(bv);
1496                 clear = true;
1497         }
1498         lt->insertInset(bv, inset);
1499 #if 0
1500         if ((cpar(bv)->getChar(cpos(bv)) != Paragraph::META_INSET) ||
1501                 (cpar(bv)->getInset(cpos(bv)) != inset))
1502                 lt->cursorLeft(bv);
1503 #endif
1504         bv->fitCursor(lt);
1505         updateLocal(bv, CURSOR_PAR|CURSOR, true);
1506         showInsetCursor(bv);
1507         if (clear)
1508                 lt = 0;
1509         return true;
1510 }
1511
1512
1513 bool InsetText::insetAllowed(Inset::Code code) const
1514 {
1515         if (the_locking_inset)
1516                 return the_locking_inset->insetAllowed(code);
1517         return true;
1518 }
1519
1520
1521 UpdatableInset * InsetText::getLockingInset() const
1522 {
1523         return the_locking_inset ? the_locking_inset->getLockingInset() :
1524                 const_cast<InsetText *>(this);
1525 }
1526
1527
1528 UpdatableInset * InsetText::getFirstLockingInsetOfType(Inset::Code c)
1529 {
1530         if (c == lyxCode())
1531                 return this;
1532         if (the_locking_inset)
1533                 return the_locking_inset->getFirstLockingInsetOfType(c);
1534         return 0;
1535 }
1536
1537
1538 bool InsetText::showInsetDialog(BufferView * bv) const
1539 {
1540         if (the_locking_inset)
1541                 return the_locking_inset->showInsetDialog(bv);
1542         return false;
1543 }
1544
1545
1546 std::vector<string> const InsetText::getLabelList() const 
1547 {
1548         std::vector<string> label_list;
1549
1550         Paragraph * tpar = par;
1551         while (tpar) {
1552                 Paragraph::inset_iterator beg = tpar->inset_iterator_begin();
1553                 Paragraph::inset_iterator end = tpar->inset_iterator_end();
1554                 for (; beg != end; ++beg) {
1555                         std::vector<string> const l = (*beg)->getLabelList();
1556                         label_list.insert(label_list.end(), l.begin(), l.end());
1557                 }
1558                 tpar = tpar->next();
1559         }
1560         return label_list;
1561 }
1562
1563
1564 void InsetText::setFont(BufferView * bv, LyXFont const & font, bool toggleall,
1565                         bool selectall)
1566 {
1567         if (the_locking_inset) {
1568                 the_locking_inset->setFont(bv, font, toggleall, selectall);
1569                 return;
1570         }
1571         bool clear = false;
1572         if (!lt) {
1573                 lt = getLyXText(bv);
1574                 clear = true;
1575         }
1576         if (lt->selection.set()) {
1577                 setUndo(bv, Undo::EDIT, lt->cursor.par(), lt->cursor.par()->next());
1578         }
1579         if (selectall)
1580                 selectAll(bv);
1581 #if 1
1582         lt->toggleFree(bv, font, toggleall);
1583 #else
1584         lt->setFont(bv, font, toggleall);
1585 #endif
1586         if (selectall)
1587                 lt->clearSelection();
1588         bv->fitCursor(lt);
1589         if (selectall || lt->selection.set())
1590                 updateLocal(bv, FULL, true);
1591         else
1592                 updateLocal(bv, CURSOR_PAR, true);
1593         if (clear)
1594                 lt = 0;
1595 }
1596
1597
1598 bool InsetText::checkAndActivateInset(BufferView * bv, bool behind)
1599 {
1600         if (cpar(bv)->getChar(cpos(bv)) == Paragraph::META_INSET) {
1601                 unsigned int x;
1602                 unsigned int y;
1603                 Inset * inset =
1604                         static_cast<UpdatableInset*>(cpar(bv)->getInset(cpos(bv)));
1605                 if (!inset || inset->editable() != Inset::HIGHLY_EDITABLE)
1606                         return false;
1607                 LyXFont const font =
1608                         getLyXText(bv)->getFont(bv->buffer(), cpar(bv), cpos(bv));
1609                 if (behind) {
1610                         x = inset->width(bv, font);
1611                         y = font.isRightToLeft() ? 0 : inset->descent(bv, font);
1612                 } else {
1613                         x = 0;
1614                         y = font.isRightToLeft() ? inset->descent(bv, font) : 0;
1615                 }
1616                 //inset_x = cx(bv) - top_x + drawTextXOffset;
1617                 //inset_y = cy(bv) + drawTextYOffset;
1618                 inset->edit(bv, x, y, 0);
1619                 if (!the_locking_inset)
1620                         return false;
1621                 updateLocal(bv, CURSOR, false);
1622                 return true;
1623         }
1624         return false;
1625 }
1626
1627
1628 bool InsetText::checkAndActivateInset(BufferView * bv, int x, int y,
1629                                       int button)
1630 {
1631         x -= drawTextXOffset;
1632         int dummyx = x;
1633         int dummyy = y + insetAscent;
1634         Inset * inset = bv->checkInsetHit(getLyXText(bv), dummyx, dummyy, button);
1635
1636         if (inset) {
1637                 if (x < 0)
1638                         x = insetWidth;
1639                 if (y < 0)
1640                         y = insetDescent;
1641                 inset_x = cx(bv) - top_x + drawTextXOffset;
1642                 inset_y = cy(bv) + drawTextYOffset;
1643                 inset->edit(bv, x - inset_x, y - inset_y, button);
1644                 if (!the_locking_inset)
1645                         return false;
1646                 updateLocal(bv, CURSOR, false);
1647                 return true;
1648         }
1649         return false;
1650 }
1651
1652
1653 int InsetText::getMaxWidth(BufferView * bv, UpdatableInset const * inset) const
1654 {
1655         int w = UpdatableInset::getMaxWidth(bv, inset);
1656         if (w < 0) {
1657                 return w;
1658         }
1659         if (owner()) {
1660                 w = w - top_x + owner()->x();
1661                 return w;
1662         }
1663         w -= (2 * TEXT_TO_INSET_OFFSET);
1664         return w - top_x;
1665 //    return  w - (2*TEXT_TO_INSET_OFFSET);
1666 }
1667
1668
1669 void InsetText::setParagraphData(Paragraph * p)
1670 {
1671         while (par) {
1672                 Paragraph * tmp = par->next();
1673                 delete par;
1674                 par = tmp;
1675         }
1676
1677         par = new Paragraph(*p);
1678         par->setInsetOwner(this);
1679         Paragraph * np = par;
1680         while (p->next()) {
1681                 p = p->next();
1682                 np->next(new Paragraph(*p));
1683                 np->next()->previous(np);
1684                 np = np->next();
1685                 np->setInsetOwner(this);
1686         }
1687         reinitLyXText();
1688 }
1689
1690
1691 void InsetText::setText(string const & data)
1692 {
1693         clear();
1694         LyXFont font(LyXFont::ALL_SANE);
1695         for (unsigned int i=0; i < data.length(); ++i)
1696                 par->insertChar(i, data[i], font);
1697 }
1698
1699
1700 void InsetText::setAutoBreakRows(bool flag)
1701 {
1702         if (flag != autoBreakRows) {
1703                 autoBreakRows = flag;
1704                 need_update = FULL;
1705                 if (!flag)
1706                         removeNewlines();
1707                 reinitLyXText();
1708         }
1709 }
1710
1711
1712 void InsetText::setDrawFrame(BufferView * bv, DrawFrame how)
1713 {
1714         if (how != drawFrame_) {
1715                 drawFrame_ = how;
1716                 if (bv)
1717                         updateLocal(bv, DRAW_FRAME, false);
1718         }
1719 }
1720
1721
1722 void InsetText::setFrameColor(BufferView * bv, LColor::color col)
1723 {
1724         if (frame_color != col) {
1725                 frame_color = col;
1726                 if (bv)
1727                         updateLocal(bv, DRAW_FRAME, false);
1728         }
1729 }
1730
1731
1732 int InsetText::cx(BufferView * bv) const
1733 {
1734         bool clear = false;
1735         if (!lt) {
1736                 lt = getLyXText(bv);
1737                 clear = true;
1738         }
1739         int x = lt->cursor.x() + top_x + TEXT_TO_INSET_OFFSET;
1740         if (the_locking_inset) {
1741                 LyXFont font = lt->getFont(bv->buffer(),
1742                                              lt->cursor.par(),
1743                                              lt->cursor.pos());
1744                 if (font.isVisibleRightToLeft())
1745                         x -= the_locking_inset->width(bv, font);
1746         }
1747         if (clear)
1748                 lt = 0;
1749         return x;
1750 }
1751
1752
1753 int InsetText::cy(BufferView * bv) const
1754 {
1755         LyXFont font;
1756         return getLyXText(bv)->cursor.y() - ascent(bv, font) + TEXT_TO_INSET_OFFSET;
1757 }
1758
1759
1760 Paragraph::size_type InsetText::cpos(BufferView * bv) const
1761 {
1762         return getLyXText(bv)->cursor.pos();
1763 }
1764
1765
1766 Paragraph * InsetText::cpar(BufferView * bv) const
1767 {
1768         return getLyXText(bv)->cursor.par();
1769 }
1770
1771
1772 bool InsetText::cboundary(BufferView * bv) const
1773 {
1774         return getLyXText(bv)->cursor.boundary();
1775 }
1776
1777
1778 Row * InsetText::crow(BufferView * bv) const
1779 {
1780         return getLyXText(bv)->cursor.row();
1781 }
1782
1783
1784 LyXText * InsetText::getLyXText(BufferView const * lbv,
1785                                           bool const recursive) const
1786 {
1787         if (!recursive && (cached_bview == lbv))
1788                 return cached_text.get();
1789         
1790         // Super UGLY! (Lgb)
1791         BufferView * bv = const_cast<BufferView *>(lbv);
1792         
1793         cached_bview = bv;
1794         Cache::iterator it = cache.find(bv);
1795
1796         if (it != cache.end()) {
1797                 if (lt || !it->second.remove) {
1798                         lyx::Assert(it->second.text.get());
1799                         cached_text = it->second.text;
1800                         if (recursive && the_locking_inset) {
1801                                 return the_locking_inset->getLyXText(bv, true);
1802                         }
1803                         return cached_text.get();
1804                 } else if (it->second.remove) {
1805                         if (locked) {
1806                                 saveLyXTextState(it->second.text.get());
1807                         } else {
1808                                 sstate.lpar = 0;
1809                         }
1810                 }
1811         }
1812         
1813         cached_text.reset(new LyXText(const_cast<InsetText *>(this)));
1814         cached_text->init(bv);
1815         restoreLyXTextState(bv, cached_text.get());
1816
1817         cache.insert(make_pair(bv, cached_text));
1818         
1819         if (the_locking_inset && recursive) {
1820                 return the_locking_inset->getLyXText(bv);
1821         }
1822         return cached_text.get();
1823 }
1824
1825
1826 void InsetText::deleteLyXText(BufferView * bv, bool recursive) const
1827 {
1828         cached_bview = 0;
1829
1830         Cache::iterator it = cache.find(bv);
1831         
1832         if (it == cache.end()) {
1833                 return;
1834         }
1835
1836         lyx::Assert(it->second.text.get());
1837
1838         it->second.remove = true;
1839         if (recursive) {
1840                 /// then remove all LyXText in text-insets
1841                 Paragraph * p = par;
1842                 for (; p; p = p->next()) {
1843                         p->deleteInsetsLyXText(bv);
1844                 }
1845         }
1846 }
1847
1848
1849 void InsetText::resizeLyXText(BufferView * bv, bool force) const
1850 {
1851         if (!par->next() && !par->size()) // no data, resize not neccessary!
1852                 return;
1853         // one endless line, resize normally not necessary
1854         if (!force && getMaxWidth(bv, this) < 0)
1855                 return;
1856
1857         Cache::iterator it = cache.find(bv);
1858         if (it == cache.end()) {
1859                 return;
1860         }
1861         lyx::Assert(it->second.text.get());
1862
1863         LyXText * t = it->second.text.get();
1864         saveLyXTextState(t);
1865         for (Paragraph * p = par; p; p = p->next()) {
1866                 p->resizeInsetsLyXText(bv);
1867         }
1868         t->init(bv, true);
1869         restoreLyXTextState(bv, t);
1870         if (the_locking_inset) {
1871                 inset_x = cx(bv) - top_x + drawTextXOffset;
1872                 inset_y = cy(bv) + drawTextYOffset;
1873         }
1874
1875         if (bv->screen()) {
1876                         t->first = bv->screen()->topCursorVisible(t);
1877         }
1878         if (!owner())
1879                 updateLocal(bv, FULL, false);
1880         else
1881                 need_update = FULL;
1882         // this will scroll the screen such that the cursor becomes visible 
1883         bv->updateScrollbar();
1884 }
1885
1886
1887 void InsetText::reinitLyXText() const
1888 {
1889         for(Cache::iterator it = cache.begin(); it != cache.end(); ++it) {
1890                 lyx::Assert(it->second.text.get());
1891
1892                 LyXText * t = it->second.text.get();
1893                 BufferView * bv = it->first;
1894
1895                 saveLyXTextState(t);
1896                 for (Paragraph * p = par; p; p = p->next()) {
1897                         p->resizeInsetsLyXText(bv);
1898                 }
1899                 t->init(bv, true);
1900                 restoreLyXTextState(bv, t);
1901                 if (the_locking_inset) {
1902                         inset_x = cx(bv) - top_x + drawTextXOffset;
1903                         inset_y = cy(bv) + drawTextYOffset;
1904                 }
1905                 if (bv->screen()) {
1906                         t->first = bv->screen()->topCursorVisible(t);
1907                 }
1908                 if (!owner())
1909                         updateLocal(bv, FULL, false);
1910                 else
1911                         need_update = FULL;
1912                 // this will scroll the screen such that the cursor becomes visible 
1913                 bv->updateScrollbar();
1914         }
1915 }
1916
1917
1918 void InsetText::removeNewlines()
1919 {
1920         for (Paragraph * p = par; p; p = p->next()) {
1921                 for (int i = 0; i < p->size(); ++i) {
1922                         if (p->getChar(i) == Paragraph::META_NEWLINE)
1923                                 p->erase(i);
1924                 }
1925         }
1926 }
1927
1928
1929 bool InsetText::nodraw() const
1930 {
1931         if (the_locking_inset)
1932                 return the_locking_inset->nodraw();
1933         return UpdatableInset::nodraw();
1934 }
1935
1936
1937 int InsetText::scroll(bool recursive) const
1938 {
1939         int sx = UpdatableInset::scroll(false);
1940
1941         if (recursive && the_locking_inset)
1942                 sx += the_locking_inset->scroll(recursive);
1943
1944         return sx;
1945 }
1946
1947
1948 bool InsetText::doClearArea() const
1949 {
1950         return !locked || (need_update & (FULL|INIT));
1951 }
1952
1953
1954 void InsetText::selectAll(BufferView * bv)
1955 {
1956         getLyXText(bv)->cursorTop(bv);
1957         getLyXText(bv)->selection.cursor = getLyXText(bv)->cursor;
1958         getLyXText(bv)->cursorBottom(bv);
1959         getLyXText(bv)->setSelection(bv);
1960 }
1961
1962
1963 void InsetText::clearSelection(BufferView * bv)
1964 {
1965         getLyXText(bv)->clearSelection();
1966 }
1967
1968
1969 void InsetText::clearInset(Painter & pain, int baseline, bool & cleared) const
1970 {
1971         int w =  insetWidth;
1972         int h = insetAscent + insetDescent;
1973         int ty = baseline - insetAscent;
1974         
1975         if (ty < 0) {
1976                 h += ty;
1977                 ty = 0;
1978         }
1979         if ((ty + h) > pain.paperHeight())
1980                 h = pain.paperHeight();
1981         if ((top_x + drawTextXOffset + w) > pain.paperWidth())
1982                 w = pain.paperWidth();
1983         pain.fillRectangle(top_x+drawTextXOffset, ty, w, h, 
1984                            backgroundColor());
1985         cleared = true;
1986         need_update = FULL;
1987 }
1988
1989
1990 Paragraph * InsetText::getParFromID(int id) const
1991 {
1992         Paragraph * result = par;
1993         Paragraph * ires = 0;
1994         while (result && result->id() != id) {
1995                 if ((ires = result->getParFromID(id)))
1996                         return ires;
1997                 result = result->next();
1998         }
1999         return result;
2000 }
2001
2002
2003 Paragraph * InsetText::firstParagraph() const
2004 {
2005         Paragraph * result;
2006         if (the_locking_inset)
2007                 if ((result = the_locking_inset->firstParagraph()))
2008                         return result;
2009         return par;
2010 }
2011
2012
2013 LyXCursor const & InsetText::cursor(BufferView * bv) const
2014 {
2015                 if (the_locking_inset)
2016                                 return the_locking_inset->cursor(bv);
2017                 return getLyXText(bv)->cursor;
2018 }
2019
2020
2021 Paragraph * InsetText::paragraph() const
2022 {
2023         return par;
2024 }
2025
2026
2027 void InsetText::paragraph(Paragraph * p)
2028 {
2029         par = p;
2030 #if 0
2031         // we now have to update/redraw all instances
2032         for (Cache::iterator cit = cache.begin(); cit != cache.end(); ++cit) {
2033                 delete cit->second;
2034                 cit->second = 0;
2035         }
2036 #endif
2037         // redraw myself when asked for
2038         need_update |= INIT;
2039 }
2040
2041
2042 Inset * InsetText::getInsetFromID(int id_arg) const
2043 {
2044         if (id_arg == id())
2045                 return const_cast<InsetText *>(this);
2046
2047         Paragraph * lp = par;
2048
2049         while(lp) {
2050                 for (Paragraph::inset_iterator it = lp->inset_iterator_begin(),
2051                          en = lp->inset_iterator_end();
2052                          it != en; ++it)
2053                 {
2054                         if ((*it)->id() == id_arg)
2055                                 return *it;
2056                         Inset * in = (*it)->getInsetFromID(id_arg);
2057                         if (in)
2058                                 return in;
2059                 }
2060                 lp = lp->next();
2061         }
2062         return 0;
2063 }
2064
2065
2066 string const InsetText::selectNextWord(BufferView * bv, float & value) const
2067 {
2068         bool clear = false;
2069         string str;
2070
2071         if (!lt) {
2072                 lt = getLyXText(bv);
2073                 clear = true;
2074         }
2075         if (the_locking_inset) {
2076                 str = the_locking_inset->selectNextWord(bv, value);
2077                 if (!str.empty()) {
2078                         value += cy(bv);
2079                         if (clear)
2080                                 lt = 0;
2081                         return str;
2082                 }
2083 #warning Dekel please have a look on this one RTL? (Jug)
2084                 // we have to go on checking so move cusor to the right
2085                 lt->cursor.pos(lt->cursor.pos() + 1);
2086         }
2087         str = lt->selectNextWord(bv, value);
2088         if (str.empty())
2089                 bv->unlockInset(const_cast<InsetText *>(this));
2090         else
2091                 value = cy(bv);
2092         if (clear)
2093                 lt = 0;
2094         return str;
2095 }
2096
2097
2098 void InsetText::selectSelectedWord(BufferView * bv)
2099 {
2100         if (the_locking_inset) {
2101                 the_locking_inset->selectSelectedWord(bv);
2102                 return;
2103         }
2104         getLyXText(bv)->selectSelectedWord(bv);
2105         updateLocal(bv, SELECTION, false);
2106 }
2107
2108
2109 void InsetText::toggleSelection(BufferView * bv, bool kill_selection)
2110 {
2111         if (the_locking_inset) {
2112                 the_locking_inset->toggleSelection(bv, kill_selection);
2113         }
2114         bool clear = false;
2115         if (!lt) {
2116                 lt = getLyXText(bv);
2117                 clear = true;
2118         }
2119
2120         int x = top_x + TEXT_TO_INSET_OFFSET;
2121
2122         int y = 0;
2123         Row * row = lt->getRowNearY(y);
2124         int y_offset = top_baseline - row->ascent_of_text();
2125         y = y_offset;
2126         while ((row != 0) && ((y+row->height()) <= 0)) {
2127                 y += row->height();
2128                 row = row->next();
2129         }
2130         if (y_offset < 0)
2131                 y_offset = y;
2132         
2133         if (need_update & SELECTION)
2134                 need_update = NONE;
2135         bv->screen()->toggleSelection(lt, bv, kill_selection, y_offset, x);
2136         if (clear)
2137                 lt = 0;
2138 }
2139
2140
2141 bool InsetText::searchForward(BufferView * bv, string const & str,
2142                               bool const & cs, bool const & mw)
2143 {
2144         if (the_locking_inset) {
2145                 if (the_locking_inset->searchForward(bv, str, cs, mw))
2146                         return true;
2147                 bool clear = false;
2148                 if (!lt) {
2149                         lt = getLyXText(bv);
2150                         clear = true;
2151                 }
2152                 Paragraph * lpar = lt->cursor.par();
2153                 Paragraph::size_type pos = lt->cursor.pos();
2154                 if (pos < lpar->size() - 1)
2155                         ++pos;
2156                 else {
2157                         pos = 0;
2158                         lpar = lpar->next();
2159                 }
2160                 if (!lpar) {
2161                         if (clear)
2162                                 lt = 0;
2163                         return false;
2164                 }
2165                 lt->setCursor(bv, lpar, pos);
2166                 if (clear)
2167                         lt = 0;
2168         }
2169         if (LyXFind(bv, str, true, true, cs , mw)) {
2170                 return true;
2171         }
2172         // we have to unlock ourself in this function by default!
2173         bv->unlockInset(const_cast<InsetText *>(this));
2174         return false;
2175 }
2176
2177 bool InsetText::searchBackward(BufferView * bv, string const & str,
2178                                bool const & cs, bool const & mw)
2179 {
2180         if (the_locking_inset)
2181                 if (the_locking_inset->searchBackward(bv, str, cs, mw))
2182                         return true;
2183         if (LyXFind(bv, str, false, true, cs, mw)) {
2184                 return true;
2185         }
2186         // we have to unlock ourself in this function by default!
2187         bv->unlockInset(const_cast<InsetText *>(this));
2188         return false;
2189 }