]> git.lyx.org Git - lyx.git/blob - src/insets/insettext.C
Minor code shuffle.
[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         x = cx(bv);
1271         y = cy(bv);
1272 }
1273
1274
1275 unsigned int InsetText::insetInInsetY()
1276 {
1277         if (!the_locking_inset)
1278                 return 0;
1279
1280         return (inset_y + the_locking_inset->insetInInsetY());
1281 }
1282
1283
1284 void InsetText::toggleInsetCursor(BufferView * bv)
1285 {
1286         if (the_locking_inset) {
1287                 the_locking_inset->toggleInsetCursor(bv);
1288                 return;
1289         }
1290
1291         LyXFont const font(getLyXText(bv)->getFont(bv->buffer(), cpar(bv), cpos(bv)));
1292
1293         int const asc = lyxfont::maxAscent(font);
1294         int const desc = lyxfont::maxDescent(font);
1295   
1296         if (isCursorVisible())
1297                 bv->hideLockedInsetCursor();
1298         else
1299                 bv->showLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1300         toggleCursorVisible();
1301 }
1302
1303
1304 void InsetText::showInsetCursor(BufferView * bv, bool show)
1305 {
1306         if (the_locking_inset) {
1307                 the_locking_inset->showInsetCursor(bv);
1308                 return;
1309         }
1310         if (!isCursorVisible()) {
1311                 LyXFont const font =
1312                         getLyXText(bv)->getFont(bv->buffer(), cpar(bv), cpos(bv));
1313         
1314                 int const asc = lyxfont::maxAscent(font);
1315                 int const desc = lyxfont::maxDescent(font);
1316
1317                 bv->fitLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1318                 if (show)
1319                         bv->showLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1320                 setCursorVisible(true);
1321         }
1322 }
1323
1324
1325 void InsetText::hideInsetCursor(BufferView * bv)
1326 {
1327         if (isCursorVisible()) {
1328                 bv->hideLockedInsetCursor();
1329                 setCursorVisible(false);
1330         }
1331         if (the_locking_inset)
1332                 the_locking_inset->hideInsetCursor(bv);
1333 }
1334
1335
1336 UpdatableInset::RESULT
1337 InsetText::moveRight(BufferView * bv, bool activate_inset, bool selecting)
1338 {
1339         if (getLyXText(bv)->cursor.par()->isRightToLeftPar(bv->buffer()->params))
1340                 return moveLeftIntern(bv, false, activate_inset, selecting);
1341         else
1342                 return moveRightIntern(bv, false, activate_inset, selecting);
1343 }
1344
1345
1346 UpdatableInset::RESULT
1347 InsetText::moveLeft(BufferView * bv, bool activate_inset, bool selecting)
1348 {
1349         if (getLyXText(bv)->cursor.par()->isRightToLeftPar(bv->buffer()->params))
1350                 return moveRightIntern(bv, true, activate_inset, selecting);
1351         else
1352                 return moveLeftIntern(bv, true, activate_inset, selecting);
1353 }
1354
1355
1356 UpdatableInset::RESULT
1357 InsetText::moveRightIntern(BufferView * bv, bool behind, 
1358                            bool activate_inset, bool selecting)
1359 {
1360         if (!cpar(bv)->next() && (cpos(bv) >= cpar(bv)->size()))
1361                 return FINISHED;
1362         if (activate_inset && checkAndActivateInset(bv, behind))
1363                 return DISPATCHED;
1364         getLyXText(bv)->cursorRight(bv);
1365         if (!selecting)
1366                 getLyXText(bv)->selection.cursor = getLyXText(bv)->cursor;
1367         return DISPATCHED_NOUPDATE;
1368 }
1369
1370
1371 UpdatableInset::RESULT
1372 InsetText::moveLeftIntern(BufferView * bv, bool behind,
1373                           bool activate_inset, bool selecting)
1374 {
1375         if (!cpar(bv)->previous() && (cpos(bv) <= 0))
1376                 return FINISHED;
1377         getLyXText(bv)->cursorLeft(bv);
1378         if (!selecting)
1379                 getLyXText(bv)->selection.cursor = getLyXText(bv)->cursor;
1380         if (activate_inset && checkAndActivateInset(bv, behind))
1381                 return DISPATCHED;
1382         return DISPATCHED_NOUPDATE;
1383 }
1384
1385
1386 UpdatableInset::RESULT
1387 InsetText::moveUp(BufferView * bv)
1388 {
1389         if (!crow(bv)->previous())
1390                 return FINISHED;
1391         getLyXText(bv)->cursorUp(bv);
1392         return DISPATCHED_NOUPDATE;
1393 }
1394
1395
1396 UpdatableInset::RESULT
1397 InsetText::moveDown(BufferView * bv)
1398 {
1399         if (!crow(bv)->next())
1400                 return FINISHED;
1401         getLyXText(bv)->cursorDown(bv);
1402         return DISPATCHED_NOUPDATE;
1403 }
1404
1405
1406 bool InsetText::insertInset(BufferView * bv, Inset * inset)
1407 {
1408         if (the_locking_inset) {
1409                 if (the_locking_inset->insertInsetAllowed(inset))
1410                         return the_locking_inset->insertInset(bv, inset);
1411                 return false;
1412         }
1413         inset->setOwner(this);
1414         hideInsetCursor(bv);
1415
1416         bool clear = false;
1417         if (!lt) {
1418                 lt = getLyXText(bv);
1419                 clear = true;
1420         }
1421         lt->insertInset(bv, inset);
1422         if ((cpar(bv)->getChar(cpos(bv)) != Paragraph::META_INSET) ||
1423                 (cpar(bv)->getInset(cpos(bv)) != inset))
1424                 lt->cursorLeft(bv);
1425         bv->fitCursor(lt);
1426         updateLocal(bv, CURSOR_PAR|CURSOR, true);
1427         showInsetCursor(bv);
1428         if (clear)
1429                 lt = 0;
1430         return true;
1431 }
1432
1433
1434 bool InsetText::insertInsetAllowed(Inset * in) const
1435 {
1436         if (the_locking_inset)
1437                 return the_locking_inset->insertInsetAllowed(in);
1438         return true;
1439 }
1440
1441 bool InsetText::insertInsetAllowed(Inset::Code code) const
1442 {
1443         if (the_locking_inset)
1444                 return the_locking_inset->insertInsetAllowed(code);
1445         return true;
1446 }
1447
1448
1449 UpdatableInset * InsetText::getLockingInset() const
1450 {
1451         return the_locking_inset ? the_locking_inset->getLockingInset() :
1452                 const_cast<InsetText *>(this);
1453 }
1454
1455
1456 UpdatableInset * InsetText::getFirstLockingInsetOfType(Inset::Code c)
1457 {
1458         if (c == lyxCode())
1459                 return this;
1460         if (the_locking_inset)
1461                 return the_locking_inset->getFirstLockingInsetOfType(c);
1462         return 0;
1463 }
1464
1465
1466 bool InsetText::showInsetDialog(BufferView * bv) const
1467 {
1468         if (the_locking_inset)
1469                 return the_locking_inset->showInsetDialog(bv);
1470         return false;
1471 }
1472
1473
1474 std::vector<string> const InsetText::getLabelList() const 
1475 {
1476         std::vector<string> label_list;
1477
1478         Paragraph * tpar = par;
1479         while (tpar) {
1480                 Paragraph::inset_iterator beg = tpar->inset_iterator_begin();
1481                 Paragraph::inset_iterator end = tpar->inset_iterator_end();
1482                 for (; beg != end; ++beg) {
1483                         std::vector<string> const l = (*beg)->getLabelList();
1484                         label_list.insert(label_list.end(), l.begin(), l.end());
1485                 }
1486                 tpar = tpar->next();
1487         }
1488         return label_list;
1489 }
1490
1491
1492 void InsetText::setFont(BufferView * bv, LyXFont const & font, bool toggleall,
1493                         bool selectall)
1494 {
1495         if (the_locking_inset) {
1496                 the_locking_inset->setFont(bv, font, toggleall, selectall);
1497                 return;
1498         }
1499         bool clear = false;
1500         if (!lt) {
1501                 lt = getLyXText(bv);
1502                 clear = true;
1503         }
1504         if (lt->selection.set()) {
1505                 setUndo(bv, Undo::EDIT, lt->cursor.par(), lt->cursor.par()->next());
1506         }
1507         if (selectall)
1508                 selectAll(bv);
1509 #if 1
1510         lt->toggleFree(bv, font, toggleall);
1511 #else
1512         lt->setFont(bv, font, toggleall);
1513 #endif
1514         if (selectall)
1515                 lt->clearSelection(bv);
1516         bv->fitCursor(lt);
1517         if (selectall || lt->selection.set())
1518                 updateLocal(bv, FULL, true);
1519         else
1520                 updateLocal(bv, CURSOR_PAR, true);
1521         if (clear)
1522                 lt = 0;
1523 }
1524
1525
1526 bool InsetText::checkAndActivateInset(BufferView * bv, bool behind)
1527 {
1528         if (cpar(bv)->getChar(cpos(bv)) == Paragraph::META_INSET) {
1529                 unsigned int x;
1530                 unsigned int y;
1531                 Inset * inset =
1532                         static_cast<UpdatableInset*>(cpar(bv)->getInset(cpos(bv)));
1533                 if (!inset || inset->editable() != Inset::HIGHLY_EDITABLE)
1534                         return false;
1535                 LyXFont const font =
1536                         getLyXText(bv)->getFont(bv->buffer(), cpar(bv), cpos(bv));
1537                 if (behind) {
1538                         x = inset->width(bv, font);
1539                         y = font.isRightToLeft() ? 0 : inset->descent(bv, font);
1540                 } else {
1541                         x = 0;
1542                         y = font.isRightToLeft() ? inset->descent(bv, font) : 0;
1543                 }
1544                 //inset_x = cx(bv) - top_x + drawTextXOffset;
1545                 //inset_y = cy(bv) + drawTextYOffset;
1546                 inset->edit(bv, x, y, 0);
1547                 if (!the_locking_inset)
1548                         return false;
1549                 updateLocal(bv, CURSOR, false);
1550                 return true;
1551         }
1552         return false;
1553 }
1554
1555
1556 bool InsetText::checkAndActivateInset(BufferView * bv, int x, int y,
1557                                       int button)
1558 {
1559         x -= drawTextXOffset;
1560         int dummyx = x;
1561         int dummyy = y + insetAscent;
1562         Inset * inset = bv->checkInsetHit(getLyXText(bv), dummyx, dummyy, button);
1563
1564         if (inset) {
1565                 if (x < 0)
1566                         x = insetWidth;
1567                 if (y < 0)
1568                         y = insetDescent;
1569                 inset_x = cx(bv) - top_x + drawTextXOffset;
1570                 inset_y = cy(bv) + drawTextYOffset;
1571                 inset->edit(bv, x - inset_x, y - inset_y, button);
1572                 if (!the_locking_inset)
1573                         return false;
1574                 updateLocal(bv, CURSOR, false);
1575                 return true;
1576         }
1577         return false;
1578 }
1579
1580
1581 int InsetText::getMaxWidth(BufferView * bv, UpdatableInset const * inset) const
1582 {
1583         int w = UpdatableInset::getMaxWidth(bv, inset);
1584         if (w < 0) {
1585                 return w;
1586         }
1587         if (owner()) {
1588                 w = w - top_x + owner()->x();
1589                 return w;
1590         }
1591         w -= (2 * TEXT_TO_INSET_OFFSET);
1592         return w - top_x;
1593 //    return  w - (2*TEXT_TO_INSET_OFFSET);
1594 }
1595
1596
1597 void InsetText::setParagraphData(Paragraph * p)
1598 {
1599         cached_bview = 0;
1600
1601         // now also delete all caches this should be safe, hopefully
1602         cache.clear();
1603
1604         while (par) {
1605                 Paragraph * tmp = par->next();
1606                 delete par;
1607                 par = tmp;
1608         }
1609
1610         par = new Paragraph(*p);
1611         par->setInsetOwner(this);
1612         Paragraph * np = par;
1613         while (p->next()) {
1614                 p = p->next();
1615                 np->next(new Paragraph(*p));
1616                 np->next()->previous(np);
1617                 np = np->next();
1618                 np->setInsetOwner(this);
1619         }
1620
1621         need_update = INIT;
1622 }
1623
1624
1625 void InsetText::setText(string const & data)
1626 {
1627         clear();
1628         LyXFont font(LyXFont::ALL_SANE);
1629         for (unsigned int i=0; i < data.length(); ++i)
1630                 par->insertChar(i, data[i], font);
1631 }
1632
1633
1634 void InsetText::setAutoBreakRows(bool flag)
1635 {
1636         if (flag != autoBreakRows) {
1637                 autoBreakRows = flag;
1638                 need_update = FULL;
1639                 if (!flag)
1640                         removeNewlines();
1641         }
1642 }
1643
1644
1645 void InsetText::setDrawFrame(BufferView * bv, DrawFrame how)
1646 {
1647         if (how != drawFrame_) {
1648                 drawFrame_ = how;
1649                 if (bv)
1650                         updateLocal(bv, DRAW_FRAME, false);
1651         }
1652 }
1653
1654
1655 void InsetText::setFrameColor(BufferView * bv, LColor::color col)
1656 {
1657         if (frame_color != col) {
1658                 frame_color = col;
1659                 if (bv)
1660                         updateLocal(bv, DRAW_FRAME, false);
1661         }
1662 }
1663
1664
1665 int InsetText::cx(BufferView * bv) const
1666 {
1667         bool clear = false;
1668         if (!lt) {
1669                 lt = getLyXText(bv);
1670                 clear = true;
1671         }
1672         int x = lt->cursor.x() + top_x + TEXT_TO_INSET_OFFSET;
1673         if (the_locking_inset) {
1674                 LyXFont font = lt->getFont(bv->buffer(),
1675                                              lt->cursor.par(),
1676                                              lt->cursor.pos());
1677                 if (font.isVisibleRightToLeft())
1678                         x -= the_locking_inset->width(bv, font);
1679         }
1680         if (clear)
1681                 lt = 0;
1682         return x;
1683 }
1684
1685
1686 int InsetText::cy(BufferView * bv) const
1687 {
1688         LyXFont font;
1689         return getLyXText(bv)->cursor.y() - ascent(bv, font) + TEXT_TO_INSET_OFFSET;
1690 }
1691
1692
1693 Paragraph::size_type InsetText::cpos(BufferView * bv) const
1694 {
1695         return getLyXText(bv)->cursor.pos();
1696 }
1697
1698
1699 Paragraph * InsetText::cpar(BufferView * bv) const
1700 {
1701         return getLyXText(bv)->cursor.par();
1702 }
1703
1704
1705 bool InsetText::cboundary(BufferView * bv) const
1706 {
1707         return getLyXText(bv)->cursor.boundary();
1708 }
1709
1710
1711 Row * InsetText::crow(BufferView * bv) const
1712 {
1713         return getLyXText(bv)->cursor.row();
1714 }
1715
1716
1717 LyXText * InsetText::getLyXText(BufferView const * lbv,
1718                                           bool const recursive) const
1719 {
1720         if (!recursive && (cached_bview == lbv))
1721                 return cached_text.get();
1722         
1723         // Super UGLY! (Lgb)
1724         BufferView * bv = const_cast<BufferView *>(lbv);
1725         
1726         cached_bview = bv;
1727         Cache::iterator it = cache.find(bv);
1728         
1729         if (it != cache.end() && (lt || !it->second.remove)) {
1730                 lyx::Assert(it->second.text.get());
1731                         
1732                 cached_text = it->second.text;
1733                 if (recursive && the_locking_inset) {
1734                         return the_locking_inset->getLyXText(bv);
1735                 }
1736                 return cached_text.get();
1737         } else if (it->second.remove) {
1738                 if (locked) {
1739                         saveLyXTextState(it->second.text.get());
1740                 } else {
1741                         sstate.lpar = 0;
1742                 }
1743                 cache.erase(bv);
1744 //              raise(SIGSTOP);
1745         }
1746
1747         cached_text.reset(new LyXText(const_cast<InsetText *>(this)));
1748         cached_text->init(bv);
1749         restoreLyXTextState(bv, cached_text.get());
1750
1751         cache.insert(make_pair(bv, cached_text));
1752         
1753         if (the_locking_inset && recursive) {
1754                 return the_locking_inset->getLyXText(bv);
1755         }
1756         return cached_text.get();
1757 }
1758
1759
1760 void InsetText::deleteLyXText(BufferView * bv, bool recursive) const
1761 {
1762         cached_bview = 0;
1763
1764         Cache::iterator it = cache.find(bv);
1765         
1766         if (it == cache.end()) {
1767                 return;
1768         }
1769
1770         lyx::Assert(it->second.text.get());
1771
1772         it->second.remove = true;
1773         if (recursive) {
1774                 /// then remove all LyXText in text-insets
1775                 Paragraph * p = par;
1776                 for (; p; p = p->next()) {
1777                         p->deleteInsetsLyXText(bv);
1778                 }
1779         }
1780 }
1781
1782
1783 void InsetText::resizeLyXText(BufferView * bv, bool force) const
1784 {
1785         if (!par->next() && !par->size()) // no data, resize not neccessary!
1786                 return;
1787         // one endless line, resize normally not necessary
1788         if (!force && getMaxWidth(bv, this) < 0)
1789                 return;
1790
1791         Cache::iterator it = cache.find(bv);
1792         if (it == cache.end()) {
1793                 return;
1794         }
1795         lyx::Assert(it->second.text.get());
1796
1797         deleteLyXText(bv, (the_locking_inset == 0) || force);
1798
1799         if (bv->screen()) {
1800                         LyXText * t = getLyXText(bv);
1801                         t->first = bv->screen()->topCursorVisible(t);
1802         }
1803         
1804         // this will scroll the screen such that the cursor becomes visible 
1805         bv->updateScrollbar();
1806         if (the_locking_inset) {
1807                 /// then resize all LyXText in text-insets
1808                 inset_x = cx(bv) - top_x + drawTextXOffset;
1809                 inset_y = cy(bv) + drawTextYOffset;
1810                 for (Paragraph * p = par; p; p = p->next()) {
1811                         p->resizeInsetsLyXText(bv);
1812                 }
1813         }
1814         need_update = FULL;
1815 }
1816
1817
1818 void InsetText::removeNewlines()
1819 {
1820         for (Paragraph * p = par; p; p = p->next()) {
1821                 for (int i = 0; i < p->size(); ++i) {
1822                         if (p->getChar(i) == Paragraph::META_NEWLINE)
1823                                 p->erase(i);
1824                 }
1825         }
1826 }
1827
1828
1829 bool InsetText::nodraw() const
1830 {
1831         if (the_locking_inset)
1832                 return the_locking_inset->nodraw();
1833         return UpdatableInset::nodraw();
1834 }
1835
1836
1837 int InsetText::scroll(bool recursive) const
1838 {
1839         int sx = UpdatableInset::scroll(false);
1840
1841         if (recursive && the_locking_inset)
1842                 sx += the_locking_inset->scroll(recursive);
1843
1844         return sx;
1845 }
1846
1847
1848 bool InsetText::doClearArea() const
1849 {
1850         return !locked || (need_update & (FULL|INIT));
1851 }
1852
1853
1854 void InsetText::selectAll(BufferView * bv)
1855 {
1856         getLyXText(bv)->cursorTop(bv);
1857         getLyXText(bv)->selection.cursor = getLyXText(bv)->cursor;
1858         getLyXText(bv)->cursorBottom(bv);
1859         getLyXText(bv)->setSelection(bv);
1860 }
1861
1862
1863 void InsetText::clearSelection(BufferView * bv)
1864 {
1865         getLyXText(bv)->clearSelection(bv);
1866 }
1867
1868
1869 void InsetText::clearInset(Painter & pain, int baseline, bool & cleared) const
1870 {
1871         int w =  insetWidth;
1872         int h = insetAscent + insetDescent;
1873         int ty = baseline - insetAscent;
1874         
1875         if (ty < 0) {
1876                 h += ty;
1877                 ty = 0;
1878         }
1879         if ((ty + h) > pain.paperHeight())
1880                 h = pain.paperHeight();
1881         if ((top_x + drawTextXOffset + w) > pain.paperWidth())
1882                 w = pain.paperWidth();
1883         pain.fillRectangle(top_x+drawTextXOffset, ty, w, h);
1884         cleared = true;
1885         need_update = FULL;
1886 }
1887
1888
1889 Paragraph * InsetText::getParFromID(int id) const
1890 {
1891         Paragraph * result = par;
1892         Paragraph * ires = 0;
1893         while (result && result->id() != id) {
1894                 if ((ires = result->getParFromID(id)))
1895                         return ires;
1896                 result = result->next();
1897         }
1898         return result;
1899 }
1900
1901
1902 Paragraph * InsetText::firstParagraph() const
1903 {
1904         Paragraph * result;
1905         if (the_locking_inset)
1906                 if ((result = the_locking_inset->firstParagraph()))
1907                         return result;
1908         return par;
1909 }
1910
1911
1912 LyXCursor const & InsetText::cursor(BufferView * bv) const
1913 {
1914                 if (the_locking_inset)
1915                                 return the_locking_inset->cursor(bv);
1916                 return getLyXText(bv)->cursor;
1917 }
1918
1919
1920 Paragraph * InsetText::paragraph() const
1921 {
1922         return par;
1923 }
1924
1925
1926 void InsetText::paragraph(Paragraph * p)
1927 {
1928         par = p;
1929 #if 0
1930         // we now have to update/redraw all instances
1931         for (Cache::iterator cit = cache.begin(); cit != cache.end(); ++cit) {
1932                 delete cit->second;
1933                 cit->second = 0;
1934         }
1935 #endif
1936         // redraw myself when asked for
1937         need_update |= INIT;
1938 }
1939
1940
1941 Inset * InsetText::getInsetFromID(int id_arg) const
1942 {
1943         if (id_arg == id())
1944                 return const_cast<InsetText *>(this);
1945
1946         Paragraph * lp = par;
1947
1948         while(lp) {
1949                 for (Paragraph::inset_iterator it = lp->inset_iterator_begin(),
1950                          en = lp->inset_iterator_end();
1951                          it != en; ++it)
1952                 {
1953                         if ((*it)->id() == id_arg)
1954                                 return *it;
1955                         Inset * in = (*it)->getInsetFromID(id_arg);
1956                         if (in)
1957                                 return in;
1958                 }
1959                 lp = lp->next();
1960         }
1961         return 0;
1962 }