]> git.lyx.org Git - lyx.git/blobdiff - src/BufferView_pimpl.C
remove LFUN_DEPTH; some small work on icons and menu reorganization
[lyx.git] / src / BufferView_pimpl.C
index a90b76067afa21440610035aaee377c073bad95e..ae3ee5e487429c3d77effd0e07f3701e015d9a5c 100644 (file)
@@ -73,7 +73,7 @@
 #include <clocale>
 
 
-extern lyx::layout_type current_layout;
+extern string current_layout;
 
 using std::vector;
 using std::find_if;
@@ -85,7 +85,6 @@ using std::min;
 using SigC::slot;
 
 using lyx::pos_type;
-using lyx::layout_type;
 using lyx::textclass_type;
 
 /* the selection possible is needed, that only motion events are 
@@ -215,7 +214,7 @@ void BufferView::Pimpl::buffer(Buffer * b)
                        updateScreen();
                        updateScrollbar();
                }
-               bv_->text->first = screen_->topCursorVisible(bv_->text);
+               bv_->text->first_y = screen_->topCursorVisible(bv_->text);
                owner_->updateMenubar();
                owner_->updateToolbar();
                // Similarly, buffer-dependent dialogs should be updated or
@@ -312,7 +311,7 @@ int BufferView::Pimpl::resizeCurrentBuffer()
        pos_type selendpos = 0;
        bool selection = false;
        bool mark_set  = false;
-
+       
        owner_->prohibitInput();
 
        owner_->message(_("Formatting document..."));
@@ -330,6 +329,7 @@ int BufferView::Pimpl::resizeCurrentBuffer()
                delete bv_->text;
                bv_->text = new LyXText(bv_);
                bv_->text->init(bv_);
+               buffer_->resizeInsets(bv_);
        } else {
                // See if we have a text in TextCache that fits
                // the new buffer_ with the correct width.
@@ -346,14 +346,16 @@ int BufferView::Pimpl::resizeCurrentBuffer()
                } else {
                        bv_->text = new LyXText(bv_);
                        bv_->text->init(bv_);
+                       //buffer_->resizeInsets(bv_);
                }
        }
+
        updateScreen();
 
        if (par) {
                bv_->text->selection.set(true);
-               /* at this point just to avoid the Delete-Empty-Paragraph
-                * Mechanism when setting the cursor */
+               // At this point just to avoid the Delete-Empty-Paragraph-
+               // Mechanism when setting the cursor.
                bv_->text->selection.mark(mark_set);
                if (selection) {
                        bv_->text->setCursor(bv_, selstartpar, selstartpos);
@@ -369,10 +371,9 @@ int BufferView::Pimpl::resizeCurrentBuffer()
                // remake the inset locking
                bv_->theLockingInset(the_locking_inset);
        }
-       bv_->text->first = screen_->topCursorVisible(bv_->text);
-#if 0
-       buffer_->resizeInsets(bv_);
-#endif
+
+       bv_->text->first_y = screen_->topCursorVisible(bv_->text);
+
        // this will scroll the screen such that the cursor becomes visible
        updateScrollbar();
        redraw();
@@ -409,7 +410,7 @@ void BufferView::Pimpl::updateScrollbar()
 
        if (text_height <= work_height) {
                workarea_.setScrollbarBounds(0.0, 0.0);
-               current_scrollbar_value = bv_->text->first;
+               current_scrollbar_value = bv_->text->first_y;
                workarea_.setScrollbar(current_scrollbar_value, 1.0);
                return;
        }
@@ -431,9 +432,9 @@ void BufferView::Pimpl::updateScrollbar()
                workarea_.setScrollbarIncrements(lineh);
                old_lineh = lineh;
        }
-       if (current_scrollbar_value != bv_->text->first
+       if (current_scrollbar_value != bv_->text->first_y
            || slider_size != old_slider_size) {
-               current_scrollbar_value = bv_->text->first;
+               current_scrollbar_value = bv_->text->first_y;
                workarea_.setScrollbar(current_scrollbar_value, slider_size);
                old_slider_size = slider_size;
        }
@@ -463,8 +464,8 @@ void BufferView::Pimpl::scrollCB(double value)
        LyXText * vbt = bv_->text;
  
        int const height = vbt->defaultHeight();
-       int const first = static_cast<int>((bv_->text->first + height));
-       int const last = static_cast<int>((bv_->text->first + workarea_.height() - height));
+       int const first = static_cast<int>((bv_->text->first_y + height));
+       int const last = static_cast<int>((bv_->text->first_y + workarea_.height() - height));
 
        if (vbt->cursor.y() < first)
                vbt->setCursorFromCoordinates(bv_, 0, first);
@@ -559,7 +560,7 @@ void BufferView::Pimpl::workAreaMotionNotify(int x, int y, unsigned int state)
                bv_->theLockingInset()->
                        insetMotionNotify(bv_,
                                          x - start_x,
-                                         y - cursor.y() + bv_->text->first,
+                                         y - cursor.y() + bv_->text->first_y,
                                          state);
                return;
        }
@@ -572,7 +573,7 @@ void BufferView::Pimpl::workAreaMotionNotify(int x, int y, unsigned int state)
  
        screen_->hideCursor();
 
-       bv_->text->setCursorFromCoordinates(bv_, x, y + bv_->text->first);
+       bv_->text->setCursorFromCoordinates(bv_, x, y + bv_->text->first_y);
       
        if (!bv_->text->selection.set())
                update(bv_->text, BufferView::UPDATE); // Maybe an empty line was deleted
@@ -588,7 +589,8 @@ void BufferView::Pimpl::workAreaMotionNotify(int x, int y, unsigned int state)
 void BufferView::Pimpl::workAreaButtonPress(int xpos, int ypos,
                                             unsigned int button)
 {
-       if (!buffer_ || !screen_.get()) return;
+       if (!buffer_ || !screen_.get())
+               return;
 
        Inset * inset_hit = checkInsetHit(bv_->text, xpos, ypos);
 
@@ -604,6 +606,16 @@ void BufferView::Pimpl::workAreaButtonPress(int xpos, int ypos,
                }
        }
        
+       // Middle button press pastes if we have a selection
+       // We do this here as if the selection was inside an inset
+       // it could get cleared on the unlocking of the inset so
+       // we have to check this first
+       bool paste_internally = false;
+       if (button == 2 && bv_->getLyXText()->selection.set()) {
+               owner_->getLyXFunc()->dispatch(LFUN_COPY);
+               paste_internally = true;
+       }
+       
        if (bv_->theLockingInset()) {
                // We are in inset locking mode
                
@@ -622,15 +634,7 @@ void BufferView::Pimpl::workAreaButtonPress(int xpos, int ypos,
                selection_possible = true;
        screen_->hideCursor();
 
-       int const screen_first = bv_->text->first;
-       
-       // Middle button press pastes if we have a selection
-       bool paste_internally = false;
-       if (button == 2
-           && bv_->text->selection.set()) {
-               owner_->getLyXFunc()->dispatch(LFUN_COPY);
-               paste_internally = true;
-       }
+       int const screen_first = bv_->text->first_y;
        
        // Clear the selection
        screen_->toggleSelection(bv_->text, bv_);
@@ -678,7 +682,7 @@ void BufferView::Pimpl::workAreaButtonPress(int xpos, int ypos,
                        owner_->getLyXFunc()->dispatch(LFUN_PASTE);
                else
                        owner_->getLyXFunc()->dispatch(LFUN_PASTESELECTION,
-                                                      "paragraph");
+                                                      "paragraph");
                selection_possible = false;
                return;
        }
@@ -757,10 +761,12 @@ void BufferView::Pimpl::selectionRequested()
  
 void BufferView::Pimpl::selectionLost()
 {
-       hideCursor();
-       toggleSelection();
-       bv_->getLyXText()->clearSelection();
-       showCursor(); 
+       if (active() && available()) { 
+               hideCursor();
+               toggleSelection();
+               bv_->getLyXText()->clearSelection();
+               showCursor(); 
+       }
 }
 
  
@@ -870,9 +876,7 @@ void BufferView::Pimpl::workAreaButtonRelease(int x, int y,
 
        // Maybe we want to edit a bibitem ale970302
        if (bv_->text->cursor.par()->bibkey && x < 20 + 
-           bibitemMaxWidth(bv_, textclasslist.
-                           TextClass(buffer_->
-                                     params.textclass).defaultfont())) {
+           bibitemMaxWidth(bv_, textclasslist[buffer_->params.textclass].defaultfont())) {
                bv_->text->cursor.par()->bibkey->edit(bv_, 0, 0, 0);
        }
 
@@ -944,7 +948,7 @@ Inset * BufferView::Pimpl::checkInsetHit(LyXText * text, int & x, int & y)
        if (!screen_.get())
                return 0;
   
-       int y_tmp = y + text->first;
+       int y_tmp = y + text->first_y;
  
        LyXCursor cursor;
        text->setCursorFromCoordinates(bv_, cursor, x, y_tmp);
@@ -998,6 +1002,7 @@ void BufferView::Pimpl::workAreaExpose()
                        if (lyxerr.debugging())
                                textcache.show(lyxerr, "Expose delete all");
                        textcache.clear();
+                       buffer_->resizeInsets(bv_);
                } else if (heightChange) {
                        // Rebuild image of current screen
                        updateScreen();
@@ -1145,10 +1150,9 @@ void BufferView::Pimpl::cursorPrevious(LyXText * text)
        if (!text->cursor.row()->previous())
                return;
        
-       int y = text->first;
-       if (text->inset_owner)
-               y += bv_->text->first;
+       int y = text->first_y;
        Row * cursorrow = text->cursor.row();
+       
        text->setCursorFromCoordinates(bv_, bv_->text->cursor.x_fix(), y);
        finishUndo();
        // This is to allow jumping over large insets
@@ -1171,7 +1175,7 @@ void BufferView::Pimpl::cursorNext(LyXText * text)
        if (!text->cursor.row()->next())
                return;
        
-       int y = text->first + workarea_.height();
+       int y = text->first_y + workarea_.height();
 //     if (text->inset_owner)
 //             y += bv_->text->first;
        text->getRowNearY(y);
@@ -1193,7 +1197,8 @@ void BufferView::Pimpl::cursorNext(LyXText * text)
 
 bool BufferView::Pimpl::available() const
 {
-       if (buffer_ && bv_->text) return true;
+       if (buffer_ && bv_->text)
+               return true;
        return false;
 }
 
@@ -1386,14 +1391,16 @@ void BufferView::Pimpl::center()
 
 void BufferView::Pimpl::pasteClipboard(bool asPara) 
 {
-       if (!buffer_) return;
+       if (!buffer_)
+               return;
 
        screen_->hideCursor();
        beforeChange(bv_->text);
        
        string const clip(workarea_.getClipboard());
        
-       if (clip.empty()) return;
+       if (clip.empty())
+               return;
 
        if (asPara) {
                bv_->getLyXText()->insertStringAsParagraphs(bv_, clip);
@@ -1425,9 +1432,13 @@ void BufferView::Pimpl::moveCursorUpdate(bool selecting)
                lt->setSelection(bv_);
                if (lt->bv_owner)
                        toggleToggle();
+               else
+                       updateInset(lt->inset_owner, false);
+       }
+       if (lt->bv_owner) {
+               update(lt, BufferView::SELECT|BufferView::FITCUR);
+               showCursor();
        }
-       update(lt, BufferView::SELECT|BufferView::FITCUR);
-       showCursor();
        
        /* ---> Everytime the cursor is moved, show the current font state. */
        // should this too me moved out of this func?
@@ -1568,7 +1579,7 @@ bool BufferView::Pimpl::Dispatch(kb_action action, string const & argument)
                
        case LFUN_PASTESELECTION:
        {
-               bool asPara = false;
+               bool asPara = false;
                if (argument == "paragraph")
                        asPara = true;
                pasteClipboard(asPara);
@@ -1633,10 +1644,6 @@ bool BufferView::Pimpl::Dispatch(kb_action action, string const & argument)
                hfill();
                break;
                
-       case LFUN_DEPTH:
-               changeDepth(bv_, bv_->getLyXText(), 0);
-               break;
-               
        case LFUN_DEPTH_MIN:
                changeDepth(bv_, bv_->getLyXText(), -1);
                break;
@@ -1669,34 +1676,34 @@ bool BufferView::Pimpl::Dispatch(kb_action action, string const & argument)
                // Derive layout number from given argument (string)
                // and current buffer's textclass (number). */    
                textclass_type tclass = buffer_->params.textclass;
-               pair <bool, layout_type> layout = 
-                       textclasslist.NumberOfLayout(tclass, argument);
+               bool hasLayout =
+                       textclasslist[tclass].hasLayout(argument);
+               string layout = argument;
 
                // If the entry is obsolete, use the new one instead.
-               if (layout.first) {
-                       string obs = textclasslist.Style(tclass, layout.second)
+               if (hasLayout) {
+                       string const & obs = textclasslist[tclass][layout]
                                .obsoleted_by();
                        if (!obs.empty()) 
-                               layout = textclasslist.NumberOfLayout(tclass, obs);
+                               layout = obs;
                }
 
-               // see if we found the layout number:
-               if (!layout.first) {
+               if (!hasLayout) {
                        owner_->getLyXFunc()->setErrorMessage(
                                string(N_("Layout ")) + argument +
                                N_(" not known"));
                        break;
                }
 
-               if (current_layout != layout.second) {
+               if (current_layout != layout) {
                        LyXText * lt = bv_->getLyXText();
                        hideCursor();
-                       current_layout = layout.second;
+                       current_layout = layout;
                        update(lt,
                               BufferView::SELECT
                               | BufferView::FITCUR);
-                       lt->setLayout(bv_, layout.second);
-                       owner_->setLayout(layout.second);
+                       lt->setLayout(bv_, layout);
+                       owner_->setLayout(layout);
                        update(lt,
                               BufferView::SELECT
                               | BufferView::FITCUR
@@ -2254,7 +2261,7 @@ bool BufferView::Pimpl::Dispatch(kb_action action, string const & argument)
        case LFUN_ENDSEL:
        {
                LyXText * lt = bv_->getLyXText();
-               
+
                update(lt, BufferView::SELECT|BufferView::FITCUR);
                lt->cursorEnd(bv_);
                finishUndo();
@@ -2341,9 +2348,7 @@ bool BufferView::Pimpl::Dispatch(kb_action action, string const & argument)
        {
                LyXText * lt = bv_->getLyXText();
 
-               LyXLayout const & style = textclasslist
-                       .Style(buffer_->params.textclass,
-                              lt->cursor.par()->getLayout());
+               LyXLayout const & style = textclasslist[buffer_->params.textclass][lt->cursor.par()->layout()];
 
                if (style.free_spacing) {
                        lt->insertChar(bv_, ' ');
@@ -2352,7 +2357,7 @@ bool BufferView::Pimpl::Dispatch(kb_action action, string const & argument)
                               | BufferView::FITCUR
                               | BufferView::CHANGE);
                } else {
-                       protectedBlank(lt);
+                       specialChar(InsetSpecialChar::PROTECTED_SEPARATOR);
                }
                moveCursorUpdate(false);
        }
@@ -2747,12 +2752,8 @@ bool BufferView::Pimpl::Dispatch(kb_action action, string const & argument)
        }
        break;
        
-       case LFUN_INSET_TEXT:
-               insertAndEditInset(new InsetText);
-               break;
-       
        case LFUN_INSET_ERT:
-               insertAndEditInset(new InsetERT);
+               insertAndEditInset(new InsetERT(buffer_->params));
                break;
        
        case LFUN_INSET_EXTERNAL:
@@ -2760,25 +2761,26 @@ bool BufferView::Pimpl::Dispatch(kb_action action, string const & argument)
                break;
        
        case LFUN_INSET_FOOTNOTE:
-               insertAndEditInset(new InsetFoot);
+               insertAndEditInset(new InsetFoot(buffer_->params));
                break;
 
        case LFUN_INSET_MARGINAL:
-               insertAndEditInset(new InsetMarginal);
+               insertAndEditInset(new InsetMarginal(buffer_->params));
                break;
 
        case LFUN_INSET_MINIPAGE:
-               insertAndEditInset(new InsetMinipage);
+               insertAndEditInset(new InsetMinipage(buffer_->params));
                break;
 
        case LFUN_INSERT_NOTE:
-               insertAndEditInset(new InsetNote);
+               insertAndEditInset(new InsetNote(buffer_->params));
                break;
 
        case LFUN_INSET_FLOAT:
                // check if the float type exist
                if (floatList.typeExist(argument)) {
-                       insertAndEditInset(new InsetFloat(argument));
+                       insertAndEditInset(new InsetFloat(buffer_->params,
+                                                         argument));
                } else {
                        lyxerr << "Non-existent float type: "
                               << argument << endl;
@@ -2789,7 +2791,8 @@ bool BufferView::Pimpl::Dispatch(kb_action action, string const & argument)
        {
                // check if the float type exist
                if (floatList.typeExist(argument)) {
-                       InsetFloat * new_inset = new InsetFloat(argument);
+                       InsetFloat * new_inset =
+                               new InsetFloat(buffer_->params, argument);
                        new_inset->wide(true);
                        if (insertInset(new_inset))
                                new_inset->edit(bv_);
@@ -2819,7 +2822,8 @@ bool BufferView::Pimpl::Dispatch(kb_action action, string const & argument)
                if (bv_->theLockingInset()) {
                        lyxerr << "Locking inset code: "
                               << static_cast<int>(bv_->theLockingInset()->lyxCode());
-                       InsetCaption * new_inset = new InsetCaption;
+                       InsetCaption * new_inset =
+                               new InsetCaption(buffer_->params);
                        new_inset->setOwner(bv_->theLockingInset());
                        new_inset->setAutoBreakRows(true);
                        new_inset->setDrawFrame(0, InsetText::LOCKED);
@@ -2879,7 +2883,7 @@ bool BufferView::Pimpl::Dispatch(kb_action action, string const & argument)
        break;
        
        case LFUN_GETLAYOUT:
-               owner_->getLyXFunc()->setMessage(tostr(bv_->getLyXText()->cursor.par()->layout));
+               owner_->getLyXFunc()->setMessage(tostr(bv_->getLyXText()->cursor.par()->layout()));
                break;
                        
        case LFUN_GETFONT:
@@ -3255,27 +3259,6 @@ void BufferView::Pimpl::hfill()
 }
 
 
-void BufferView::Pimpl::protectedBlank(LyXText * lt)
-{
-       if (available()) {
-               hideCursor();
-               update(lt, BufferView::SELECT|BufferView::FITCUR);
-               InsetSpecialChar * new_inset =
-                       new InsetSpecialChar(InsetSpecialChar::PROTECTED_SEPARATOR);
-#ifdef WITH_WARNINGS
-#warning Why is this code different from specialChar() below? (JMarc)
-// the code in specialChar is a generic version of what used to exist
-// for other special chars. I did not merge this case because of the
-// call to updateInset(), but what does it do?
-#endif
-               if (!insertInset(new_inset))
-                       delete new_inset;
-               else
-                       updateInset(new_inset, true);
-       }
-}
-
-
 void BufferView::Pimpl::specialChar(InsetSpecialChar::Kind kind)
 {
        if (available()) {
@@ -3287,6 +3270,8 @@ void BufferView::Pimpl::specialChar(InsetSpecialChar::Kind kind)
                        new InsetSpecialChar(kind);
                if (!insertInset(new_inset))
                        delete new_inset;
+               else
+                       updateInset(new_inset, true);
        }
 }
 
@@ -3308,8 +3293,8 @@ void BufferView::Pimpl::smartQuote()
 
        hideCursor();
 
-       LyXLayout const & style = textclasslist.Style(
-               bv_->buffer()->params.textclass, par->getLayout());
+       LyXLayout const & style =
+               textclasslist[bv_->buffer()->params.textclass][par->layout()];
        
        if (style.pass_thru ||
                (!insertInset(new InsetQuotes(c, bv_->buffer()->params))))
@@ -3369,17 +3354,20 @@ bool BufferView::Pimpl::insertInset(Inset * inset, string const & lout)
                        update(bv_->text, BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
                }
 
-               pair<bool, layout_type> lres =
-                       textclasslist.NumberOfLayout(buffer_->params .textclass, lout);
-               layout_type lay = 0;
-               if (lres.first != false) {
+               string lres = lout;
+               LyXTextClass const & tclass =
+                       textclasslist[buffer_->params.textclass];
+               bool hasLayout = tclass.hasLayout(lres);
+               string lay = tclass.defaultLayoutName();
+
+               if (hasLayout != false) {
                        // layout found
-                       lay = lres.second;
+                       lay = lres;
                } else {
-                       // layout not fount using default "Standard" (0)
-                       lay = 0;
+                       // layout not fount using default
+                       lay = tclass.defaultLayoutName();
                }
-                
+
                bv_->text->setLayout(bv_, lay);
                
                bv_->text->setParagraph(bv_, 0, 0,
@@ -3402,7 +3390,7 @@ bool BufferView::Pimpl::insertInset(Inset * inset, string const & lout)
 
 void BufferView::Pimpl::updateInset(Inset * inset, bool mark_dirty)
 {
-       if (!inset)
+       if (!inset || !available())
                return;
 
        // first check for locking insets
@@ -3428,9 +3416,14 @@ void BufferView::Pimpl::updateInset(Inset * inset, bool mark_dirty)
                }
        }
   
-       // then check the current buffer
-       if (available()) {
-               hideCursor();
+       // then check if the inset is a top_level inset (has no owner)
+       // if yes do the update as always otherwise we have to update the
+       // toplevel inset where this inset is inside
+       Inset * tl_inset = inset;
+       while(tl_inset->owner())
+               tl_inset = tl_inset->owner();
+       hideCursor();
+       if (tl_inset == inset) {
                update(bv_->text, BufferView::UPDATE);
                if (bv_->text->updateInset(bv_, inset)) {
                        if (mark_dirty) {
@@ -3443,6 +3436,13 @@ void BufferView::Pimpl::updateInset(Inset * inset, bool mark_dirty)
                        }
                        return;
                }
+       } else if (static_cast<UpdatableInset *>(tl_inset)
+                          ->updateInsetInInset(bv_, inset))
+       {
+                       if (bv_->text->updateInset(bv_,  tl_inset)) {
+                               update();
+                               updateScrollbar();
+                       }
        }
 }