]> git.lyx.org Git - lyx.git/blobdiff - src/BufferView_pimpl.C
Collapse all those LFUN_XYZ_APPLY to a single LFUN_INSET_APPLY.
[lyx.git] / src / BufferView_pimpl.C
index 6ffe90292132a884f270c4a358b4d381cb1ba8e3..f29895c2ab0a428873f2fcc97bbe959b53455343 100644 (file)
@@ -8,51 +8,44 @@
 
 #include <config.h>
 
-#ifdef __GNUG__
-#pragma implementation
-#endif
-
 #include "BufferView_pimpl.h"
-#include "frontends/WorkArea.h"
-#include "frontends/screen.h"
-#include "frontends/LyXScreenFactory.h"
-#include "frontends/WorkAreaFactory.h"
-#include "frontends/Dialogs.h"
-#include "frontends/Alert.h"
-#include "frontends/FileDialog.h"
-#include "frontends/mouse_state.h"
-#include "lyxtext.h"
-#include "lyxrow.h"
-#include "paragraph.h"
-#include "frontends/LyXView.h"
+#include "bufferlist.h"
+#include "bufferview_funcs.h"
 #include "commandtags.h"
-#include "lyxfunc.h"
 #include "debug.h"
-#include "bufferview_funcs.h"
-#include "TextCache.h"
-#include "bufferlist.h"
-#include "lyxrc.h"
-#include "intl.h"
-// added for Dispatch functions
-#include "lyx_cb.h"
-#include "lyx_main.h"
+#include "factory.h"
 #include "FloatList.h"
+#include "funcrequest.h"
 #include "gettext.h"
+#include "intl.h"
+#include "iterators.h"
+#include "lyx_cb.h" // added for Dispatch functions
+#include "lyx_main.h"
+#include "lyxfind.h"
+#include "lyxfunc.h"
+#include "lyxtext.h"
+#include "lyxrc.h"
+#include "lyxrow.h"
+#include "paragraph.h"
 #include "ParagraphParameters.h"
+#include "TextCache.h"
 #include "undo_funcs.h"
-#include "funcrequest.h"
 
-#include "insets/insetbib.h"
-#include "insets/insettext.h"
-#include "insets/insetlatexaccent.h"
-#include "insets/insettoc.h"
-#include "insets/insetindex.h"
-#include "insets/insetref.h"
-#include "insets/insetinclude.h"
-#include "insets/insetcite.h"
-#include "insets/insetgraphics.h"
-#include "insets/insetmarginal.h"
 #include "insets/insetfloatlist.h"
+#include "insets/insetgraphics.h"
+#include "insets/insetinclude.h"
+#include "insets/insetref.h"
+#include "insets/insettext.h"
+
+#include "frontends/Alert.h"
+#include "frontends/Dialogs.h"
+#include "frontends/FileDialog.h"
+#include "frontends/LyXView.h"
+#include "frontends/LyXScreenFactory.h"
+#include "frontends/mouse_state.h"
+#include "frontends/screen.h"
+#include "frontends/WorkArea.h"
+#include "frontends/WorkAreaFactory.h"
 
 #include "mathed/formulabase.h"
 
@@ -172,7 +165,9 @@ void BufferView::Pimpl::buffer(Buffer * b)
        // set current buffer
        buffer_ = b;
 
-       if (bufferlist.getState() == BufferList::CLOSING) return;
+       // if we're quitting lyx, don't bother updating stuff 
+       if (quitting)
+               return; 
 
        // if we are closing the buffer, use the first buffer as current
        if (!buffer_) {
@@ -265,8 +260,8 @@ int BufferView::Pimpl::resizeCurrentBuffer()
        bool selection = false;
        bool mark_set  = false;
 
-       owner_->prohibitInput();
-
+       owner_->busy(true);
        owner_->message(_("Formatting document..."));
 
        if (bv_->text) {
@@ -330,7 +325,7 @@ int BufferView::Pimpl::resizeCurrentBuffer()
        bv_->text->first_y = screen().topCursorVisible(bv_->text->cursor, bv_->text->first_y);
 
        switchKeyMap();
-       owner_->allowInput();
+       owner_->busy(false);
 
        updateScrollbar();
 
@@ -356,9 +351,9 @@ void BufferView::Pimpl::updateScrollbar()
        LyXText const & t = *bv_->text;
 
        lyxerr[Debug::GUI] << "Updating scrollbar: h " << t.height << ", first_y "
-               << t.first_y << ", default height " << t.defaultHeight() << endl;
+               << t.first_y << ", default height " << defaultRowHeight() << endl;
 
-       workarea().setScrollbarParams(t.height, t.first_y, t.defaultHeight());
+       workarea().setScrollbarParams(t.height, t.first_y, defaultRowHeight());
 }
 
 
@@ -376,7 +371,7 @@ void BufferView::Pimpl::scrollDocView(int value)
 
        LyXText * vbt = bv_->text;
 
-       int const height = vbt->defaultHeight();
+       int const height = defaultRowHeight();
        int const first = static_cast<int>((bv_->text->first_y + height));
        int const last = static_cast<int>((bv_->text->first_y + workarea().workHeight() - height));
 
@@ -394,7 +389,7 @@ void BufferView::Pimpl::scroll(int lines)
        }
 
        LyXText const * t = bv_->text;
-       int const line_height = t->defaultHeight();
+       int const line_height = defaultRowHeight();
 
        // The new absolute coordinate
        int new_first_y = t->first_y + lines * line_height;
@@ -406,7 +401,7 @@ void BufferView::Pimpl::scroll(int lines)
        scrollDocView(new_first_y);
 
        // Update the scrollbar.
-       workarea().setScrollbarParams(t->height, t->first_y, t->defaultHeight());
+       workarea().setScrollbarParams(t->height, t->first_y, defaultRowHeight());
 }
 
 
@@ -624,6 +619,21 @@ bool BufferView::Pimpl::available() const
 }
 
 
+Change const BufferView::Pimpl::getCurrentChange()
+{
+       if (!bv_->buffer()->params.tracking_changes) 
+               return Change(Change::UNCHANGED);
+
+       LyXText * t(bv_->getLyXText());
+       if (!t->selection.set())
+               return Change(Change::UNCHANGED);
+       LyXCursor const & cur(t->selection.start);
+       return cur.par()->lookupChangeFull(cur.pos());
+}
+
+
 void BufferView::Pimpl::beforeChange(LyXText * text)
 {
        toggleSelection();
@@ -857,8 +867,7 @@ void BufferView::Pimpl::MenuInsertLyXFile(string const & filen)
                                initpath = trypath;
                }
 
-               FileDialog fileDlg(bv_->owner(),
-                                  _("Select LyX document to insert"),
+               FileDialog fileDlg(_("Select LyX document to insert"),
                        LFUN_FILE_INSERT,
                        make_pair(string(_("Documents|#o#O")),
                                  string(lyxrc.document_path)),
@@ -889,9 +898,9 @@ void BufferView::Pimpl::MenuInsertLyXFile(string const & filen)
 
        ostringstream s1;
 #if USE_BOOST_FORMAT
-       s1 << boost::format(_("Inserting document %1$s ...")) % disp_fn;
+       s1 << boost::format(_("Inserting document %1$s...")) % disp_fn;
 #else
-       s1 << _("Inserting document ") << disp_fn << _(" ...");
+       s1 << _("Inserting document ") << disp_fn << _("...");
 #endif
        owner_->message(STRCONV(s1.str()));
        bool const res = bv_->insertLyXFile(filename);
@@ -915,8 +924,49 @@ void BufferView::Pimpl::MenuInsertLyXFile(string const & filen)
 }
 
 
-bool BufferView::Pimpl::dispatch(FuncRequest const & ev)
+void BufferView::Pimpl::trackChanges()
+{
+       Buffer * buf(bv_->buffer());
+       bool const tracking(buf->params.tracking_changes);
+
+       if (!tracking) {
+               ParIterator const end = buf->par_iterator_end();
+               for (ParIterator it = buf->par_iterator_begin(); it != end; ++it) {
+                       (*it)->trackChanges();
+               }
+               buf->params.tracking_changes = true;
+
+               // we cannot allow undos beyond the freeze point
+               buf->undostack.clear();
+       } else {
+               bv_->update(bv_->text, BufferView::SELECT | BufferView::FITCUR);
+               bv_->text->setCursor(bv_, &(*buf->paragraphs.begin()), 0);
+#warning changes FIXME 
+               //moveCursorUpdate(false);
+
+               bool found = lyxfind::findNextChange(bv_);
+               if (found) {
+                       owner_->getDialogs().showMergeChanges();
+                       return;
+               }
+
+               ParIterator const end = buf->par_iterator_end();
+               for (ParIterator it = buf->par_iterator_begin(); it != end; ++it) {
+                       (*it)->untrackChanges();
+               }
+               buf->params.tracking_changes = false;
+       }
+       buf->redostack.clear();
+}
+
+
+bool BufferView::Pimpl::dispatch(FuncRequest const & ev_in)
 {
+       // Make sure that the cached BufferView is correct.
+       FuncRequest ev = ev_in;
+       ev.setView(bv_);
+
        lyxerr[Debug::ACTION] << "BufferView::Pimpl::Dispatch:"
                << " action[" << ev.action << ']'
                << " arg[" << ev.argument << ']'
@@ -1044,22 +1094,6 @@ bool BufferView::Pimpl::dispatch(FuncRequest const & ev)
                MenuInsertLabel(bv_, ev.argument);
                break;
 
-       case LFUN_REF_INSERT:
-               if (ev.argument.empty()) {
-                       InsetCommandParams p("ref");
-                       owner_->getDialogs().createRef(p.getAsString());
-               } else {
-                       InsetCommandParams p;
-                       p.setFromString(ev.argument);
-
-                       InsetRef * inset = new InsetRef(p, *buffer_);
-                       if (!insertInset(inset))
-                               delete inset;
-                       else
-                               updateInset(inset, true);
-               }
-               break;
-
        case LFUN_BOOKMARK_SAVE:
                savePosition(strToUnsignedInt(ev.argument));
                break;
@@ -1131,75 +1165,39 @@ bool BufferView::Pimpl::dispatch(FuncRequest const & ev)
        case LFUN_MATH_DISPLAY:          // Open or create a displayed math inset
        case LFUN_MATH_MODE:             // Open or create an inlined math inset
        case LFUN_GREEK:                 // Insert a single greek letter
-               mathDispatch(FuncRequest(bv_, ev.action, ev.argument));
+               mathDispatch(ev);
                break;
 
-       case LFUN_CITATION_INSERT:
-       {
-               InsetCommandParams p;
-               p.setFromString(ev.argument);
+       case LFUN_INSET_APPLY: {
+               string const name = ev.getArg(0);
 
-               InsetCitation * inset = new InsetCitation(p);
-               if (!insertInset(inset))
-                       delete inset;
-               else {
-                       inset->setLoadingBuffer(bv_->buffer(), false);
-                       updateInset(inset, true);
+               InsetBase * base = owner_->getDialogs().getOpenInset(name);
+               Inset * inset = 0;
+               if (base) {
+                       // This works both for 'original' and 'mathed' insets.
+                       // Note that the localDispatch performs updateInset
+                       // also.
+                       base->localDispatch(ev);
+               } else {
+                       inset = createInset(ev);
+                       if (inset && insertInset(inset)) {
+                               updateInset(inset, true);
+                       } else {
+                               delete inset;
+                       }
                }
 
-       }
-       break;
-
-       case LFUN_INSERT_BIBTEX:
-       {
-               // ale970405+lasgoutt970425
-               // The argument can be up to two tokens separated
-               // by a space. The first one is the bibstyle.
-               string const db = token(ev.argument, ' ', 0);
-               string bibstyle = token(ev.argument, ' ', 1);
-               if (bibstyle.empty())
-                       bibstyle = "plain";
-
-               InsetCommandParams p("BibTeX", db, bibstyle);
-               InsetBibtex * inset = new InsetBibtex(p);
-
-               if (insertInset(inset)) {
-                       if (ev.argument.empty())
-                               inset->edit(bv_);
-               } else
-                       delete inset;
-       }
-       break;
-
-       // BibTeX data bases
-       case LFUN_BIBDB_ADD:
-       {
-               InsetBibtex * inset =
-                       static_cast<InsetBibtex*>(getInsetByCode(Inset::BIBTEX_CODE));
-               if (inset) {
-                       inset->addDatabase(ev.argument);
+               if (name == "bibitem") {
+                       // We need to do a redraw because the maximum
+                       // InsetBibitem width could have changed
+#warning please check you mean repaint() not update(),
+#warning and whether the repaint() is needed at all
+                       bv_->repaint();
+                       bv_->fitCursor();
                }
        }
        break;
 
-       case LFUN_BIBDB_DEL:
-       {
-               InsetBibtex * inset =
-                       static_cast<InsetBibtex*>(getInsetByCode(Inset::BIBTEX_CODE));
-               if (inset)
-                       inset->delDatabase(ev.argument);
-       }
-       break;
-
-       case LFUN_BIBTEX_STYLE:
-       {
-               InsetBibtex * inset =
-                       static_cast<InsetBibtex*>(getInsetByCode(Inset::BIBTEX_CODE));
-               if (inset)
-                       inset->setOptions(ev.argument);
-       }
-       break;
-
        case LFUN_CHILD_INSERT:
        {
                InsetInclude::Params p;
@@ -1249,6 +1247,56 @@ bool BufferView::Pimpl::dispatch(FuncRequest const & ev)
        }
                break;
 
+       case LFUN_TRACK_CHANGES:
+               trackChanges();
+               break;
+       case LFUN_MERGE_CHANGES:
+               owner_->getDialogs().showMergeChanges();
+               break;
+       case LFUN_ACCEPT_ALL_CHANGES: {
+               bv_->update(bv_->text, BufferView::SELECT | BufferView::FITCUR);
+               bv_->text->setCursor(bv_, &(*bv_->buffer()->paragraphs.begin()), 0);
+#warning FIXME changes 
+               //moveCursorUpdate(false);
+
+               while (lyxfind::findNextChange(bv_)) {
+                       bv_->getLyXText()->acceptChange(bv_);
+               }
+               update(bv_->text,
+                       BufferView::SELECT | BufferView::FITCUR | BufferView::CHANGE);
+               break;
+       }
+       case LFUN_REJECT_ALL_CHANGES: {
+               bv_->update(bv_->text, BufferView::SELECT | BufferView::FITCUR);
+               bv_->text->setCursor(bv_, &(*bv_->buffer()->paragraphs.begin()), 0);
+#warning FIXME changes 
+               //moveCursorUpdate(false);
+
+               while (lyxfind::findNextChange(bv_)) {
+                       bv_->getLyXText()->rejectChange(bv_);
+               }
+               update(bv_->text,
+                       BufferView::SELECT | BufferView::FITCUR | BufferView::CHANGE);
+               break;
+       }
+       case LFUN_ACCEPT_CHANGE: {
+               bv_->getLyXText()->acceptChange(bv_);
+               update(bv_->text,
+                       BufferView::SELECT | BufferView::FITCUR | BufferView::CHANGE);
+               break;
+       }
+
+       case LFUN_REJECT_CHANGE: {
+               bv_->getLyXText()->rejectChange(bv_);
+               update(bv_->text,
+                       BufferView::SELECT | BufferView::FITCUR | BufferView::CHANGE);
+               break;
+       }
        case LFUN_UNKNOWN_ACTION:
                ev.errorMessage(N_("Unknown function!"));
                break;