]> git.lyx.org Git - lyx.git/blobdiff - src/BufferView_pimpl.C
mathed31.diff
[lyx.git] / src / BufferView_pimpl.C
index fac6aa8d9db29165deddcf5c8305ed4705257d53..dff6e8a53eac36c82d505cf1e572ceac95f45583 100644 (file)
@@ -21,6 +21,7 @@
 #include "TextCache.h"
 #include "bufferlist.h"
 #include "insets/insetbib.h"
+#include "insets/insettext.h"
 #include "lyx_gui_misc.h"
 #include "lyxrc.h"
 #include "intl.h"
@@ -33,8 +34,8 @@ using SigC::slot;
 
 using std::pair;
 using std::endl;
-using std::vector;
 using std::make_pair;
+using std::min;
 
 /* the selection possible is needed, that only motion events are 
  * used, where the bottom press event was on the drawing area too */
@@ -43,10 +44,10 @@ bool selection_possible = false;
 extern BufferList bufferlist;
 extern char ascii_type;
 
-extern "C" void TimerCB(FL_OBJECT *, long); 
 extern void sigchldhandler(pid_t pid, int * status);
 extern int bibitemMaxWidth(BufferView *, LyXFont const &);
 
+const unsigned int saved_positions_num = 20;
 
 static inline
 void waitForX()
@@ -75,11 +76,7 @@ BufferView::Pimpl::Pimpl(BufferView * b, LyXView * o,
        : bv_(b), owner_(o), cursor_timeout(400)
 {
        buffer_ = 0;
-#if 0
-       workarea_ = new WorkArea(bv_, xpos, ypos, width, height);
-#else
        workarea_ = new WorkArea(xpos, ypos, width, height);
-#endif
        // Setup the signals
        workarea_->scrollCB.connect(slot(this, &BufferView::Pimpl::scrollCB));
        workarea_->workAreaExpose
@@ -110,6 +107,7 @@ BufferView::Pimpl::Pimpl(BufferView * b, LyXView * o,
        cursor_timeout.start();
        workarea_->setFocus();
        using_xterm_cursor = false;
+       saved_positions.resize(saved_positions_num);
 }
 
 
@@ -197,14 +195,13 @@ void BufferView::Pimpl::buffer(Buffer * b)
 void BufferView::Pimpl::resize(int xpos, int ypos, int width, int height)
 {
        workarea_->resize(xpos, ypos, width, height);
-       update(SELECT);
+       update(bv_->text, SELECT);
        redraw();
 }
 
 
 void BufferView::Pimpl::resize()
 {
-       // This will resize the buffer. (Asger)
        if (buffer_)
                resizeCurrentBuffer();
 }
@@ -219,8 +216,8 @@ void BufferView::Pimpl::redraw()
 
 bool BufferView::Pimpl::fitCursor(LyXText * text)
 {
-       Assert(screen_); // it is a programming error to call fitCursor
-       // without a valid screen.
+       Assert(screen_);
        bool ret = screen_->FitCursor(text, bv_);
        if (ret)
            updateScrollbar();
@@ -245,6 +242,8 @@ int BufferView::Pimpl::resizeCurrentBuffer()
        LyXParagraph * par = 0;
        LyXParagraph * selstartpar = 0;
        LyXParagraph * selendpar = 0;
+       UpdatableInset * the_locking_inset;
+       
        int pos = 0;
        int selstartpos = 0;
        int selendpos = 0;
@@ -264,6 +263,7 @@ int BufferView::Pimpl::resizeCurrentBuffer()
                selendpos = bv_->text->sel_end_cursor.pos();
                selection = bv_->text->selection;
                mark_set = bv_->text->mark_set;
+               the_locking_inset = bv_->text->the_locking_inset;
                delete bv_->text;
                bv_->text = new LyXText(bv_);
        } else {
@@ -294,13 +294,15 @@ int BufferView::Pimpl::resizeCurrentBuffer()
                        bv_->text->SetCursor(bv_, selstartpar, selstartpos);
                        bv_->text->sel_cursor = bv_->text->cursor;
                        bv_->text->SetCursor(bv_, selendpar, selendpos);
-                       bv_->text->SetSelection();
+                       bv_->text->SetSelection(bv_);
                        bv_->text->SetCursor(bv_, par, pos);
                } else {
                        bv_->text->SetCursor(bv_, par, pos);
                        bv_->text->sel_cursor = bv_->text->cursor;
                        bv_->text->selection = false;
                }
+               // remake the inset locking
+               bv_->text->the_locking_inset = the_locking_inset;
        }
        bv_->text->first = screen_->TopCursorVisible(bv_->text);
        buffer_->resizeInsets(bv_);
@@ -311,45 +313,12 @@ int BufferView::Pimpl::resizeCurrentBuffer()
        bv_->setState();
        AllowInput(bv_);
 
-       // Now if the title form still exist kill it
-       TimerCB(0, 0);
-
+       owner_->getDialogs()->hideSplash();
        return 0;
 }
 
 
-void BufferView::Pimpl::gotoError()
-{
-       if (!screen_)
-               return;
-   
-       screen_->HideCursor();
-       bv_->beforeChange();
-       update(BufferView::SELECT|BufferView::FITCUR);
-       LyXCursor tmp;
-
-       if (!bv_->text->GotoNextError(bv_)) {
-               if (bv_->text->cursor.pos() 
-                   || bv_->text->cursor.par() != bv_->text->FirstParagraph()) {
-                       tmp = bv_->text->cursor;
-                       bv_->text->cursor.par(bv_->text->FirstParagraph());
-                       bv_->text->cursor.pos(0);
-                       if (!bv_->text->GotoNextError(bv_)) {
-                               bv_->text->cursor = tmp;
-                               owner_->getMiniBuffer()
-                                       ->Set(_("No more errors"));
-                               LyXBell();
-                       }
-               } else {
-                       owner_->getMiniBuffer()->Set(_("No more errors"));
-                       LyXBell();
-               }
-       }
-       update(BufferView::SELECT|BufferView::FITCUR);
-       bv_->text->sel_cursor = bv_->text->cursor;
-}
-
-
 void BufferView::Pimpl::updateScreen()
 {
        // Regenerate the screen.
@@ -422,7 +391,7 @@ void BufferView::Pimpl::updateScrollbar()
 // Callback for scrollbar slider
 void BufferView::Pimpl::scrollCB(double value)
 {
-       if (buffer_ == 0) return;
+       if (!buffer_) return;
 
        current_scrollbar_value = long(value);
        if (current_scrollbar_value < 0)
@@ -433,30 +402,29 @@ void BufferView::Pimpl::scrollCB(double value)
 
        screen_->Draw(bv_->text, bv_, current_scrollbar_value);
 
-       if (lyxrc.cursor_follows_scrollbar) {
-               LyXText * vbt = bv_->text;
-               int height = vbt->DefaultHeight();
-               
-               if (vbt->cursor.y() < static_cast<int>((bv_->text->first + height))) {
-                       vbt->SetCursorFromCoordinates(bv_, 0,
-                                                     bv_->text->first +
-                                                     height);
-               } else if (vbt->cursor.y() >
-                          static_cast<int>((bv_->text->first+workarea_->height()-height)))
-               {
-                       vbt->SetCursorFromCoordinates(bv_, 0,
-                                                     bv_->text->first +
-                                                     workarea_->height()  -
-                                                     height);
-               }
+       if (!lyxrc.cursor_follows_scrollbar) {
+               waitForX();
+               return;
        }
+       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));
+
+       if (vbt->cursor.y() < first)
+               vbt->SetCursorFromCoordinates(bv_, 0, first);
+       else if (vbt->cursor.y() > last)
+               vbt->SetCursorFromCoordinates(bv_, 0, last);
+
        waitForX();
 }
 
 
 int BufferView::Pimpl::scrollUp(long time)
 {
-       if (buffer_ == 0) return 0;
+       if (!buffer_) return 0;
        if (!screen_) return 0;
    
        double value = workarea_->getScrollbarValue();
@@ -484,7 +452,7 @@ int BufferView::Pimpl::scrollUp(long time)
 
 int BufferView::Pimpl::scrollDown(long time)
 {
-       if (buffer_ == 0) return 0;
+       if (!buffer_) return 0;
        if (!screen_) return 0;
    
        double value= workarea_->getScrollbarValue();
@@ -524,7 +492,7 @@ void BufferView::Pimpl::workAreaMotionNotify(int x, int y, unsigned int state)
        if (!(state & Button1MotionMask))
                return;
 
-       if (buffer_ == 0 || !screen_) return;
+       if (!buffer_ || !screen_) return;
 
        // Check for inset locking
        if (bv_->theLockingInset()) {
@@ -543,22 +511,22 @@ void BufferView::Pimpl::workAreaMotionNotify(int x, int y, unsigned int state)
                return;
        }
    
-       /* The selection possible is needed, that only motion events are 
+       /* The test for not selection possible is needed, that only motion events are 
         * used, where the bottom press event was on the drawing area too */
-       if (selection_possible) {
-               screen_->HideCursor();
+       if (!selection_possible)
+               return;
+       screen_->HideCursor();
 
-               bv_->text->SetCursorFromCoordinates(bv_, x, y + bv_->text->first);
+       bv_->text->SetCursorFromCoordinates(bv_, x, y + bv_->text->first);
       
-               if (!bv_->text->selection)
-                       update(BufferView::UPDATE); // Maybe an empty line was deleted
+       if (!bv_->text->selection)
+               update(bv_->text, BufferView::UPDATE); // Maybe an empty line was deleted
       
-               bv_->text->SetSelection();
-               screen_->ToggleToggle(bv_->text, bv_);
-               fitCursor(bv_->text);
-               screen_->ShowCursor(bv_->text, bv_);
-       }
-       return;
+       bv_->text->SetSelection(bv_);
+       screen_->ToggleToggle(bv_->text, bv_);
+       fitCursor(bv_->text);
+       screen_->ShowCursor(bv_->text, bv_);
 }
 
 
@@ -569,7 +537,7 @@ void BufferView::Pimpl::workAreaButtonPress(int xpos, int ypos,
        last_click_x = -1;
        last_click_y = -1;
 
-       if (buffer_ == 0 || !screen_) return;
+       if (!buffer_ || !screen_) return;
 
        Inset * inset_hit = checkInsetHit(bv_->text, xpos, ypos, button);
 
@@ -670,34 +638,48 @@ void BufferView::Pimpl::workAreaButtonPress(int xpos, int ypos,
 void BufferView::Pimpl::doubleClick(int /*x*/, int /*y*/, unsigned int button) 
 {
        // select a word
-       if (buffer_ && !bv_->theLockingInset()) {
-               if (screen_ && button == 1) {
-                       screen_->HideCursor();
-                       screen_->ToggleSelection(bv_->text, bv_);
-                       bv_->text->SelectWord(bv_);
-                       screen_->ToggleSelection(bv_->text, bv_, false);
-                       /* This will fit the cursor on the screen
-                        * if necessary */
-                       update(BufferView::SELECT|BufferView::FITCUR);
-               }
-       }   
+       if (!buffer_)
+           return;
+
+       LyXText * text = bv_->getLyXText();
+
+       if (text->bv_owner && bv_->theLockingInset())
+           return;
+
+       if (screen_ && button == 1) {
+           screen_->HideCursor();
+           screen_->ToggleSelection(text, bv_);
+           text->SelectWord(bv_);
+           screen_->ToggleSelection(text, bv_, false);
+           /* This will fit the cursor on the screen
+            * if necessary */
+           update(text, BufferView::SELECT|BufferView::FITCUR);
+       }
 }
 
 
 void BufferView::Pimpl::tripleClick(int /*x*/, int /*y*/, unsigned int button)
 {
        // select a line
-       if (buffer_ && screen_ && !bv_->theLockingInset() && (button == 1)) {
+       if (buffer_)
+               return;
+
+       LyXText * text = bv_->getLyXText();
+
+       if (text->bv_owner && bv_->theLockingInset())
+           return;
+
+       if (screen_ && (button == 1)) {
                screen_->HideCursor();
-               screen_->ToggleSelection(bv_->text, bv_);
-               bv_->text->CursorHome(bv_);
-               bv_->text->sel_cursor = bv_->text->cursor;
-               bv_->text->CursorEnd(bv_);
-               bv_->text->SetSelection();
-               screen_->ToggleSelection(bv_->text, bv_, false);
+               screen_->ToggleSelection(text, bv_);
+               text->CursorHome(bv_);
+               text->sel_cursor = text->cursor;
+               text->CursorEnd(bv_);
+               text->SetSelection(bv_);
+               screen_->ToggleSelection(text, bv_, false);
                /* This will fit the cursor on the screen
                 * if necessary */
-               update(BufferView::SELECT|BufferView::FITCUR);
+               update(text, BufferView::SELECT|BufferView::FITCUR);
        }
 }
 
@@ -723,7 +705,7 @@ void BufferView::Pimpl::leaveView()
 void BufferView::Pimpl::workAreaButtonRelease(int x, int y,
                                              unsigned int button)
 {
-       if (buffer_ == 0 || screen_ == 0) return;
+       if (!buffer_ || !screen_) return;
 
        // If we hit an inset, we have the inset coordinates in these
        // and inset_hit points to the inset.  If we do not hit an
@@ -749,7 +731,7 @@ void BufferView::Pimpl::workAreaButtonRelease(int x, int y,
        owner_->showState();
 
        // Did we hit an editable inset?
-       if (inset_hit != 0) {
+       if (inset_hit) {
                // Inset like error, notes and figures
                selection_possible = false;
 
@@ -945,12 +927,6 @@ Inset * BufferView::Pimpl::checkInsetHit(LyXText * text, int & x, int & y,
 
 void BufferView::Pimpl::workAreaExpose()
 {
-       // this is a hack to ensure that we only call this through
-       // BufferView::redraw().
-       //if (!lgb_hack) {
-       //      redraw();
-       //}
-       
        static int work_area_width = 0;
        static unsigned int work_area_height = 0;
 
@@ -1044,20 +1020,24 @@ void BufferView::Pimpl::update()
 // update(1)  -> update(1 + 2 + 4) -> update(7) -> update(SELECT|FITCUR|CHANGE)
 // update(3)  -> update(1)         -> update(1) -> update(SELECT)
 
-void BufferView::Pimpl::update(BufferView::UpdateCodes f)
+void BufferView::Pimpl::update(LyXText * text, BufferView::UpdateCodes f)
 {
        owner_->updateLayoutChoice();
 
-       if (!bv_->text->selection && (f & SELECT)) {
-               bv_->text->sel_cursor = bv_->text->cursor;
+       if (!text->selection && (f & SELECT)) {
+               text->sel_cursor = text->cursor;
        }
 
-       bv_->text->FullRebreak(bv_);
+       text->FullRebreak(bv_);
 
-       update();
+       if (text->inset_owner) {
+           text->inset_owner->SetUpdateStatus(bv_, InsetText::CURSOR_PAR);
+           bv_->updateInset(text->inset_owner, true);
+       } else
+           update();
 
        if ((f & FITCUR)) {
-               fitCursor(bv_->text);
+               fitCursor(text);
        }
 
        if ((f & CHANGE)) {
@@ -1077,50 +1057,22 @@ void BufferView::Pimpl::cursorToggle()
        // Quite a nice place for asyncron Inset updating, isn't it?
        // Actually no! This is run even if no buffer exist... so (Lgb)
        if (!buffer_) {
-               goto set_timer_and_return;
-       }
-
-       // NOTE:
-       // On my quest to solve the gs render hangups I am now
-       // disabling the SIGHUP completely, and will do a wait
-       // now and then instead. If the guess that xforms somehow
-       // destroys something is true, this is likely (hopefully)
-       // to solve the problem...at least I hope so. Lgb
-
-       // ...Ok this seems to work...at least it does not make things
-       // worse so far. However I still see gs processes that hangs.
-       // I would really like to know _why_ they are hanging. Anyway
-       // the solution without the SIGCHLD handler seems to be easier
-       // to debug.
-
-       // When attaching gdb to a a running gs that hangs it shows
-       // that it is waiting for input(?) Is it possible for us to
-       // provide that input somehow? Or figure what it is expecing
-       // to read?
-
-       // One solution is to, after some time, look if there are some
-       // old gs processes still running and if there are: kill them
-       // and re render.
-
-       // Another solution is to provide the user an option to rerender
-       // a picture. This would, for the picture in question, check if
-       // there is a gs running for it, if so kill it, and start a new
-       // rendering process.
-
-       // these comments posted to lyx@via
-       {
-               int status = 1;
-               int pid = waitpid(static_cast<pid_t>(0), &status, WNOHANG);
-               if (pid == -1) // error find out what is wrong
-                       ; // ignore it for now.
-               else if (pid > 0)
-                       sigchldhandler(pid, &status);
+               cursor_timeout.restart();
+               return;
        }
+       int status = 1;
+       int pid = waitpid(static_cast<pid_t>(0), &status, WNOHANG);
+       if (pid == -1) // error find out what is wrong
+               ; // ignore it for now.
+       else if (pid > 0)
+               sigchldhandler(pid, &status);
 
        updatelist.update(bv_);
        
        if (!screen_) {
-               goto set_timer_and_return;
+               cursor_timeout.restart();
+               return;
        }
 
        if (!bv_->theLockingInset()) {
@@ -1129,9 +1081,7 @@ void BufferView::Pimpl::cursorToggle()
                bv_->theLockingInset()->ToggleInsetCursor(bv_);
        }
        
-  set_timer_and_return:
        cursor_timeout.restart();
-       return;
 }
 
 
@@ -1193,32 +1143,33 @@ bool BufferView::Pimpl::available() const
 }
 
 
-void BufferView::Pimpl::beforeChange()
+void BufferView::Pimpl::beforeChange(LyXText * text)
 {
        toggleSelection();
-       bv_->text->ClearSelection();
-
-       // CHECK
-       //owner_->update_timeout.stop();
+       text->ClearSelection();
 }
 
 
-void BufferView::Pimpl::savePosition()
+void BufferView::Pimpl::savePosition(unsigned int i)
 {
-       backstack.push(buffer_->fileName(),
-                      bv_->text->cursor.x(),
-                      bv_->text->cursor.y());
+       if (i >= saved_positions_num)
+               return;
+       saved_positions[i] = Position(buffer_->fileName(),
+                                     bv_->text->cursor.par()->id(),
+                                     bv_->text->cursor.pos());
+       if (i > 0)
+               owner_->getMiniBuffer()->Set(_("Saved bookmark ") + tostr(i));
 }
 
 
-void BufferView::Pimpl::restorePosition()
+void BufferView::Pimpl::restorePosition(unsigned int i)
 {
-       if (backstack.empty()) return;
-       
-       int  x, y;
-       string fname = backstack.pop(&x, &y);
+       if (i >= saved_positions_num)
+               return;
+
+       string fname = saved_positions[i].filename;
 
-       beforeChange();
+       beforeChange(bv_->text);
 
        if (fname != buffer_->fileName()) {
                Buffer * b = bufferlist.exists(fname) ?
@@ -1227,14 +1178,24 @@ void BufferView::Pimpl::restorePosition()
                if (b != 0 ) buffer(b);
        }
 
-       bv_->text->SetCursorFromCoordinates(bv_, x, y);
-       update(BufferView::SELECT|BufferView::FITCUR);
+       LyXParagraph * par = bv_->text->GetParFromID(saved_positions[i].par_id);
+       if (!par)
+               return;
+
+       bv_->text->SetCursor(bv_, par,
+                            min(par->Last(), saved_positions[i].par_pos));
+       update(bv_->text, BufferView::SELECT|BufferView::FITCUR);
+       if (i > 0)
+               owner_->getMiniBuffer()->Set(_("Moved to bookmark ") + tostr(i));
 }
 
 
-bool BufferView::Pimpl::NoSavedPositions()
+bool BufferView::Pimpl::isSavedPosition(unsigned int i)
 {
-       return backstack.empty();
+       if (i >= saved_positions_num)
+               return false;
+
+       return !saved_positions[i].filename.empty();
 }
 
 
@@ -1340,23 +1301,23 @@ void BufferView::Pimpl::toggleToggle()
 
 void BufferView::Pimpl::center() 
 {
-       beforeChange();
+       beforeChange(bv_->text);
        if (bv_->text->cursor.y() > static_cast<int>((workarea_->height() / 2))) {
                screen_->Draw(bv_->text, bv_, bv_->text->cursor.y() - workarea_->height() / 2);
        } else {
                screen_->Draw(bv_->text, bv_, 0);
        }
-       update(BufferView::SELECT|BufferView::FITCUR);
+       update(bv_->text, BufferView::SELECT|BufferView::FITCUR);
        redraw();
 }
 
 
 void BufferView::Pimpl::pasteClipboard(bool asPara) 
 {
-       if (buffer_ == 0) return;
+       if (!buffer_) return;
 
        screen_->HideCursor();
-       bv_->beforeChange();
+       beforeChange(bv_->text);
        
        string const clip(workarea_->getClipboard());
        
@@ -1367,7 +1328,7 @@ void BufferView::Pimpl::pasteClipboard(bool asPara)
        } else {
                bv_->text->InsertStringA(bv_, clip);
        }
-       update(BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
+       update(bv_->text, BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
 }