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