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