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