]> git.lyx.org Git - lyx.git/blobdiff - src/insets/insettext.C
Added function firstRow() to LyXText as this function is needed in
[lyx.git] / src / insets / insettext.C
index ba5070507536e2126870bfa035ccf61656c3900c..c85c6be95f6b7667c69f05453e023ce78bb2e8dd 100644 (file)
@@ -32,7 +32,7 @@
 #include "layout.h"
 #include "LaTeXFeatures.h"
 #include "Painter.h"
-#include "lyx_gui_misc.h"
+#include "frontends/Alert.h"
 #include "lyxtext.h"
 #include "lyxcursor.h"
 #include "CutAndPaste.h"
@@ -59,6 +59,9 @@ using std::endl;
 using std::min;
 using std::max;
 using std::make_pair;
+using std::vector;
+
+using lyx::pos_type;
 
 extern unsigned char getCurrentTextClass(Buffer *);
 extern bool math_insert_greek(BufferView *, char);
@@ -216,6 +219,7 @@ void InsetText::clear()
                par = tmp;
        }
        par = new Paragraph;
+       reinitLyXText();
        need_update = INIT;
 }
 
@@ -287,9 +291,14 @@ void InsetText::read(Buffer const * buf, LyXLex & lex)
 
 int InsetText::ascent(BufferView * bv, LyXFont const &) const
 {
-       int y_temp = 0;
-       Row * row = getLyXText(bv)->getRowNearY(y_temp);
-       insetAscent = row->ascent_of_text() + TEXT_TO_INSET_OFFSET;
+       bool clear = false;
+       if (!lt) {
+               lt = getLyXText(bv);
+               clear = true;
+       }
+       insetAscent = lt->firstRow()->ascent_of_text() + TEXT_TO_INSET_OFFSET;
+       if (clear)
+               lt = 0;
        return insetAscent;
 }
 
@@ -301,10 +310,8 @@ int InsetText::descent(BufferView * bv, LyXFont const &) const
                lt = getLyXText(bv);
                clear = true;
        }
-       int y_temp = 0;
-       Row * row = lt->getRowNearY(y_temp);
-       insetDescent = lt->height - row->ascent_of_text() +
-       TEXT_TO_INSET_OFFSET;
+       insetDescent = lt->height - lt->firstRow()->ascent_of_text() +
+               TEXT_TO_INSET_OFFSET;
        if (clear)
                lt = 0;
        return insetDescent;
@@ -423,8 +430,7 @@ void InsetText::draw(BufferView * bv, LyXFont const & f,
                inset_y = cy(bv) + drawTextYOffset;
        }
        if (!cleared && (need_update == CURSOR)
-               && !getLyXText(bv)->selection.set())
-       {
+           && !getLyXText(bv)->selection.set()) {
                drawFrame(pain, cleared);
                x += last_width; // was width(bv, f);
                need_update = NONE;
@@ -437,12 +443,11 @@ void InsetText::draw(BufferView * bv, LyXFont const & f,
        }
        x += TEXT_TO_INSET_OFFSET;
 
-       int y = 0;
-       Row * row = lt->getRowNearY(y);
+       Row * row = lt->firstRow();
        int y_offset = baseline - row->ascent_of_text();
        int ph = pain.paperHeight();
        int first = 0;
-       y = y_offset;
+       int y = y_offset;
        while ((row != 0) && ((y+row->height()) <= 0)) {
                y += row->height();
                first += row->height();
@@ -470,9 +475,9 @@ void InsetText::draw(BufferView * bv, LyXFont const & f,
                bv->screen()->update(lt, bv, y_offset, int(x));
        } else {
                locked = false;
-               if (need_update & SELECTION)
+               if (need_update & SELECTION) {
                        bv->screen()->toggleToggle(lt, bv, y_offset, int(x));
-               else if (need_update & CURSOR) {
+               else if (need_update & CURSOR) {
                        bv->screen()->toggleSelection(lt, bv, true, y_offset,int(x));
                        lt->clearSelection();
                        lt->selection.cursor = lt->cursor;
@@ -484,15 +489,20 @@ void InsetText::draw(BufferView * bv, LyXFont const & f,
        lt->refresh_y = 0;
        lt->status(bv, LyXText::UNCHANGED);
        if ((need_update != CURSOR_PAR) &&
-               ((drawFrame_ == ALWAYS) || ((drawFrame_ == LOCKED) && locked)))
+           ((drawFrame_ == ALWAYS) || ((drawFrame_ == LOCKED) && locked))) {
                drawFrame(pain, cleared);
-       else if (need_update & CLEAR_FRAME)
+       } else if (need_update & CLEAR_FRAME) {
                clearFrame(pain, cleared);
+       }
+       
        x += last_width /* was width(bv, f) */ - TEXT_TO_INSET_OFFSET;
+       
        if (bv->text->status() == LyXText::CHANGED_IN_DRAW) {
                need_update |= FULL;
-       } else if (need_update != INIT)
+       } else if (need_update != INIT) {
                need_update = NONE;
+       }
+       
        if (clear)
                lt = 0;
 }
@@ -506,7 +516,8 @@ void InsetText::drawFrame(Painter & pain, bool cleared) const
                frame_y = top_baseline - insetAscent + ttoD2;
                frame_w = last_width - TEXT_TO_INSET_OFFSET;
                frame_h = insetAscent + insetDescent - TEXT_TO_INSET_OFFSET;
-               pain.rectangle(frame_x, frame_y, frame_w, frame_h,
+               pain.rectangle(frame_x, frame_y,
+                              frame_w, frame_h,
                               frame_color);
                frame_is_visible = true;
        }
@@ -537,6 +548,7 @@ void InsetText::update(BufferView * bv, LyXFont const & font, bool reinit)
        in_update = true;
        if (reinit || need_update == INIT) {
                need_update = FULL;
+               // we should put this call where we set need_update to INIT!
                reinitLyXText();
                if (owner())
                        owner()->update(bv, font, true);
@@ -870,7 +882,7 @@ void InsetText::insetButtonPress(BufferView * bv, int x, int y, int button)
                the_locking_inset = 0;
        }
        if (bv->theLockingInset()) {
-               if (inset && inset->editable() == Inset::HIGHLY_EDITABLE) {
+               if (isHighlyEditableInset(inset)) {
                        UpdatableInset * uinset = static_cast<UpdatableInset*>(inset);
                        inset_x = cx(bv) - top_x + drawTextXOffset;
                        inset_y = cy(bv) + drawTextYOffset;
@@ -879,7 +891,7 @@ void InsetText::insetButtonPress(BufferView * bv, int x, int y, int button)
                        inset_boundary = cboundary(bv);
                        the_locking_inset = uinset;
                        uinset->insetButtonPress(bv, x - inset_x, y - inset_y,
-                                                button);
+                                                button);
                        uinset->edit(bv, x - inset_x, y - inset_y, 0);
                        if (the_locking_inset)
                                updateLocal(bv, CURSOR, false);
@@ -915,7 +927,7 @@ void InsetText::insetButtonPress(BufferView * bv, int x, int y, int button)
                                localDispatch(bv, LFUN_PASTE, "");
                        else
                                localDispatch(bv, LFUN_PASTESELECTION,
-                                             "paragraph");
+                                             "paragraph");
                }
                if (clear)
                        lt = 0;
@@ -934,9 +946,9 @@ void InsetText::insetButtonRelease(BufferView * bv, int x, int y, int button)
                                                      x - inset_x, y - inset_y,
                                                      button);
        } else {
-               if (cpar(bv)->getChar(cpos(bv)) == Paragraph::META_INSET) {
+               if (cpar(bv)->isInset(cpos(bv))) {
                        inset = static_cast<UpdatableInset*>(cpar(bv)->getInset(cpos(bv)));
-                       if (inset->editable() == Inset::HIGHLY_EDITABLE) {
+                       if (isHighlyEditableInset(inset)) {
                                inset->insetButtonRelease(bv,
                                                          x - inset_x,
                                                          y - inset_y, button);
@@ -1008,30 +1020,33 @@ InsetText::localDispatch(BufferView * bv,
                else if (result == DISPATCHED) {
                        updateLocal(bv, CURSOR_PAR, false);
                        return result;
-               } else if (result == FINISHED) {
-                       bool dispatched = false;
-                       switch (action) {
-                       case LFUN_UNKNOWN_ACTION:
-                       case LFUN_BREAKPARAGRAPH:
-                       case LFUN_BREAKLINE:
+               } else if (result >= FINISHED) {
+                       switch(result) {
+                       case FINISHED_RIGHT:
                                moveRightIntern(bv, false, false);
+                               result = DISPATCHED;
                                break;
-                       case LFUN_RIGHT:
-                               if (!getLyXText(bv)->cursor.par()->isRightToLeftPar(bv->buffer()->params))
-                                       moveRightIntern(bv, false, false);
-                               dispatched = true;
+                       case FINISHED_UP:
+                               if ((result = moveUp(bv)) >= FINISHED) {
+                                       updateLocal(bv, CURSOR, false);
+                                       bv->unlockInset(this);
+                               }
                                break;
-                       case LFUN_LEFT:
-                               if (getLyXText(bv)->cursor.par()->isRightToLeftPar(bv->buffer()->params))
-                                       moveRightIntern(bv, false, false);
-                               dispatched = true;
+                       case FINISHED_DOWN:
+                               if ((result = moveDown(bv)) >= FINISHED) {
+                                       updateLocal(bv, CURSOR, false);
+                                       bv->unlockInset(this);
+                               }
                                break;
                        default:
+                               result = DISPATCHED;
                                break;
                        }
                        the_locking_inset = 0;
-                       if (dispatched)
-                               return DISPATCHED;
+#ifdef WITH_WARNINGS
+#warning I changed this to always return Dispatched maybe it is wrong (20011001 Jug)
+#endif
+                       return result;
                }
        }
        hideInsetCursor(bv);
@@ -1179,7 +1194,7 @@ InsetText::localDispatch(BufferView * bv,
                if (!autoBreakRows) {
 
                        if (CutAndPaste::nrOfParagraphs() > 1) {
-                               WriteAlert(_("Impossible operation"),
+                               Alert::alert(_("Impossible operation"),
                                                   _("Cannot include more than one paragraph!"),
                                                   _("Sorry."));
                                break;
@@ -1352,16 +1367,187 @@ int InsetText::ascii(Buffer const * buf, ostream & os, int linelen) const
 }
 
 
-int InsetText::docBook(Buffer const * buf, ostream & os) const
+int InsetText::docbook(Buffer const * buf, ostream & os) const
 {
        Paragraph * p = par;
        unsigned int lines = 0;
-       int desc = 0;
+
+       vector<string> environment_stack(10);
+       vector<string> environment_inner(10);
+       
+       int const command_depth = 0;
+       string item_name;
        
-       string tmp;
+       Paragraph::depth_type depth = 0; // paragraph depth
+
        while (p) {
-               buf->simpleDocBookOnePar(os, tmp, p, desc, 0);
+               string sgmlparam;
+               int desc_on = 0; // description mode
+
+               LyXLayout const & style =
+                       textclasslist.Style(buf->params.textclass,
+                                           p->layout);
+
+               // environment tag closing
+               for (; depth > p->params().depth(); --depth) {
+                       if (environment_inner[depth] != "!-- --") {
+                               item_name = "listitem";
+                               buf->sgmlCloseTag(os, command_depth + depth,
+                                            item_name);
+                               if (environment_inner[depth] == "varlistentry")
+                                       buf->sgmlCloseTag(os, depth+command_depth,
+                                                    environment_inner[depth]);
+                       }
+                       buf->sgmlCloseTag(os, depth + command_depth,
+                                    environment_stack[depth]);
+                       environment_stack[depth].erase();
+                       environment_inner[depth].erase();
+               }
+
+               if (depth == p->params().depth()
+                  && environment_stack[depth] != style.latexname()
+                  && !environment_stack[depth].empty()) {
+                       if (environment_inner[depth] != "!-- --") {
+                               item_name= "listitem";
+                               buf->sgmlCloseTag(os, command_depth+depth,
+                                                 item_name);
+                               if (environment_inner[depth] == "varlistentry")
+                                       buf->sgmlCloseTag(os,
+                                                         depth + command_depth,
+                                                         environment_inner[depth]);
+                       }
+                       
+                       buf->sgmlCloseTag(os, depth + command_depth,
+                                         environment_stack[depth]);
+                       
+                       environment_stack[depth].erase();
+                       environment_inner[depth].erase();
+                }
+
+               // Write opening SGML tags.
+               switch (style.latextype) {
+               case LATEX_PARAGRAPH:
+                       buf->sgmlOpenTag(os, depth + command_depth,
+                                        style.latexname());
+                       break;
+
+               case LATEX_COMMAND:
+                       buf->sgmlError(p, 0,
+                                      _("Error : LatexType Command not allowed here.\n"));
+                       return -1;
+                       break;
+
+               case LATEX_ENVIRONMENT:
+               case LATEX_ITEM_ENVIRONMENT:
+                       if (depth < p->params().depth()) {
+                               depth = p->params().depth();
+                               environment_stack[depth].erase();
+                       }
+
+                       if (environment_stack[depth] != style.latexname()) {
+                               if(environment_stack.size() == depth + 1) {
+                                       environment_stack.push_back("!-- --");
+                                       environment_inner.push_back("!-- --");
+                               }
+                               environment_stack[depth] = style.latexname();
+                               environment_inner[depth] = "!-- --";
+                               buf->sgmlOpenTag(os, depth + command_depth,
+                                                environment_stack[depth]);
+                       } else {
+                               if (environment_inner[depth] != "!-- --") {
+                                       item_name= "listitem";
+                                       buf->sgmlCloseTag(os,
+                                                         command_depth + depth,
+                                                         item_name);
+                                       if (environment_inner[depth] == "varlistentry")
+                                               buf->sgmlCloseTag(os,
+                                                                 depth + command_depth,
+                                                                 environment_inner[depth]);
+                               }
+                       }
+                       
+                       if (style.latextype == LATEX_ENVIRONMENT) {
+                               if (!style.latexparam().empty()) {
+                                       if(style.latexparam() == "CDATA")
+                                               os << "<![CDATA[";
+                                       else
+                                               buf->sgmlOpenTag(os, depth + command_depth,
+                                                                style.latexparam());
+                               }
+                               break;
+                       }
+
+                       desc_on = (style.labeltype == LABEL_MANUAL);
+
+                       if (desc_on)
+                               environment_inner[depth]= "varlistentry";
+                       else
+                               environment_inner[depth]= "listitem";
+
+                       buf->sgmlOpenTag(os, depth + 1 + command_depth,
+                                        environment_inner[depth]);
+
+                       if (desc_on) {
+                               item_name= "term";
+                               buf->sgmlOpenTag(os, depth + 1 + command_depth,
+                                                item_name);
+                       } else {
+                               item_name= "para";
+                               buf->sgmlOpenTag(os, depth + 1 + command_depth,
+                                                item_name);
+                       }
+                       break;
+               default:
+                       buf->sgmlOpenTag(os, depth + command_depth,
+                                        style.latexname());
+                       break;
+               }
+
+               buf->simpleDocBookOnePar(os, p, desc_on,
+                                        depth + 1 + command_depth);
                p = p->next();
+
+               string end_tag;
+               // write closing SGML tags
+               switch (style.latextype) {
+               case LATEX_ENVIRONMENT:
+                       if (!style.latexparam().empty()) {
+                               if(style.latexparam() == "CDATA")
+                                       os << "]]>";
+                               else
+                                       buf->sgmlCloseTag(os, depth + command_depth,
+                                                         style.latexparam());
+                       }
+                       break;
+               case LATEX_ITEM_ENVIRONMENT:
+                       if (desc_on == 1) break;
+                       end_tag= "para";
+                       buf->sgmlCloseTag(os, depth + 1 + command_depth, end_tag);
+                       break;
+               case LATEX_PARAGRAPH:
+                       buf->sgmlCloseTag(os, depth + command_depth, style.latexname());
+                       break;
+               default:
+                       buf->sgmlCloseTag(os, depth + command_depth, style.latexname());
+                       break;
+               }
+       }
+
+       // Close open tags
+       for (int d = depth; d >= 0; --d) {
+               if (!environment_stack[depth].empty()) {
+                       if (environment_inner[depth] != "!-- --") {
+                               item_name = "listitem";
+                               buf->sgmlCloseTag(os, command_depth + depth,
+                                                 item_name);
+                               if (environment_inner[depth] == "varlistentry")
+                                      buf->sgmlCloseTag(os, depth + command_depth,
+                                                        environment_inner[depth]);
+                       }
+                       
+                       buf->sgmlCloseTag(os, depth + command_depth,
+                                         environment_stack[depth]);
+               }
        }
        
        return lines;
@@ -1564,7 +1750,7 @@ bool InsetText::insertInset(BufferView * bv, Inset * inset)
        }
        lt->insertInset(bv, inset);
 #if 0
-       if ((cpar(bv)->getChar(cpos(bv)) != Paragraph::META_INSET) ||
+       if ((!cpar(bv)->isInset(cpos(bv))) ||
                (cpar(bv)->getInset(cpos(bv)) != inset))
                lt->cursorLeft(bv);
 #endif
@@ -1664,12 +1850,12 @@ void InsetText::setFont(BufferView * bv, LyXFont const & font, bool toggleall,
 
 bool InsetText::checkAndActivateInset(BufferView * bv, bool behind)
 {
-       if (cpar(bv)->getChar(cpos(bv)) == Paragraph::META_INSET) {
+       if (cpar(bv)->isInset(cpos(bv))) {
                unsigned int x;
                unsigned int y;
                Inset * inset =
                        static_cast<UpdatableInset*>(cpar(bv)->getInset(cpos(bv)));
-               if (!inset || inset->editable() != Inset::HIGHLY_EDITABLE)
+               if (!isHighlyEditableInset(inset))
                        return false;
                LyXFont const font =
                        getLyXText(bv)->getFont(bv->buffer(), cpar(bv), cpos(bv));
@@ -1756,6 +1942,7 @@ void InsetText::setParagraphData(Paragraph * p)
                np = np->next();
                np->setInsetOwner(this);
        }
+       reinitLyXText();
        need_update = INIT;
 }
 
@@ -1828,7 +2015,7 @@ int InsetText::cy(BufferView * bv) const
 }
 
 
-Paragraph::size_type InsetText::cpos(BufferView * bv) const
+pos_type InsetText::cpos(BufferView * bv) const
 {
        return getLyXText(bv)->cursor.pos();
 }
@@ -1855,8 +2042,11 @@ Row * InsetText::crow(BufferView * bv) const
 LyXText * InsetText::getLyXText(BufferView const * lbv,
                                 bool const recursive) const
 {
-       if (!recursive && (cached_bview == lbv))
+       if (!recursive && (cached_bview == lbv)) {
+               LyXText * lt = cached_text.get();
+               lyx::Assert(lt && lt->firstRow()->par() == par);
                return cached_text.get();
+       }
        
        // Super UGLY! (Lgb)
        BufferView * bv = const_cast<BufferView *>(lbv);
@@ -2145,13 +2335,7 @@ Paragraph * InsetText::paragraph() const
 void InsetText::paragraph(Paragraph * p)
 {
        par = p;
-#if 0
-       // we now have to update/redraw all instances
-       for (Cache::iterator cit = cache.begin(); cit != cache.end(); ++cit) {
-               delete cit->second;
-               cit->second = 0;
-       }
-#endif
+       reinitLyXText();
        // redraw myself when asked for
        need_update = INIT;
 }
@@ -2181,7 +2365,7 @@ Inset * InsetText::getInsetFromID(int id_arg) const
 }
 
 
-string const InsetText::selectNextWord(BufferView * bv, float & value) const
+string const InsetText::selectNextWordToSpellcheck(BufferView * bv, float & value) const
 {
        bool clear = false;
        string str;
@@ -2191,7 +2375,7 @@ string const InsetText::selectNextWord(BufferView * bv, float & value) const
                clear = true;
        }
        if (the_locking_inset) {
-               str = the_locking_inset->selectNextWord(bv, value);
+               str = the_locking_inset->selectNextWordToSpellcheck(bv, value);
                if (!str.empty()) {
                        value += cy(bv);
                        if (clear)
@@ -2203,7 +2387,7 @@ string const InsetText::selectNextWord(BufferView * bv, float & value) const
                // we have to go on checking so move cusor to the right
                lt->cursor.pos(lt->cursor.pos() + 1);
        }
-       str = lt->selectNextWord(bv, value);
+       str = lt->selectNextWordToSpellcheck(bv, value);
        if (str.empty())
                bv->unlockInset(const_cast<InsetText *>(this));
        else
@@ -2238,10 +2422,9 @@ void InsetText::toggleSelection(BufferView * bv, bool kill_selection)
 
        int x = top_x + TEXT_TO_INSET_OFFSET;
 
-       int y = 0;
-       Row * row = lt->getRowNearY(y);
+       Row * row = lt->firstRow();
        int y_offset = top_baseline - row->ascent_of_text();
-       y = y_offset;
+       int y = y_offset;
        while ((row != 0) && ((y+row->height()) <= 0)) {
                y += row->height();
                row = row->next();
@@ -2269,7 +2452,7 @@ bool InsetText::searchForward(BufferView * bv, string const & str,
                        clear = true;
                }
                Paragraph * lpar = lt->cursor.par();
-               Paragraph::size_type pos = lt->cursor.pos();
+               pos_type pos = lt->cursor.pos();
                if (pos < lpar->size() - 1)
                        ++pos;
                else {
@@ -2308,3 +2491,11 @@ bool InsetText::searchBackward(BufferView * bv, string const & str,
        bv->unlockInset(const_cast<InsetText *>(this));
        return false;
 }
+
+
+bool InsetText::checkInsertChar(LyXFont & font)
+{
+       if (owner())
+               return owner()->checkInsertChar(font);
+       return true;
+}