2 * \file BufferView_pimpl.C
3 * Copyright 2002 the LyX Team
4 * Read the file COPYING
12 #pragma implementation
15 #include "BufferView_pimpl.h"
16 #include "frontends/WorkArea.h"
17 #include "frontends/screen.h"
18 #include "frontends/LyXScreenFactory.h"
19 #include "frontends/WorkAreaFactory.h"
20 #include "frontends/Dialogs.h"
21 #include "frontends/Alert.h"
22 #include "frontends/FileDialog.h"
25 #include "paragraph.h"
26 #include "frontends/LyXView.h"
27 #include "commandtags.h"
30 #include "bufferview_funcs.h"
31 #include "TextCache.h"
32 #include "bufferlist.h"
35 // added for Dispatch functions
38 #include "FloatList.h"
40 #include "ParagraphParameters.h"
41 #include "undo_funcs.h"
42 #include "funcrequest.h"
45 #include "insets/insetbib.h"
46 #include "insets/insettext.h"
47 #include "insets/inseturl.h"
48 #include "insets/insetlatexaccent.h"
49 #include "insets/insettoc.h"
50 #include "insets/insetref.h"
51 #include "insets/insetparent.h"
52 #include "insets/insetindex.h"
53 #include "insets/insetnote.h"
54 #include "insets/insetinclude.h"
55 #include "insets/insetcite.h"
56 #include "insets/insetert.h"
57 #include "insets/insetexternal.h"
58 #include "insets/insetgraphics.h"
59 #include "insets/insetfoot.h"
60 #include "insets/insetmarginal.h"
61 #include "insets/insetminipage.h"
62 #include "insets/insetfloat.h"
63 #include "insets/insettabular.h"
65 #include "insets/insettheorem.h"
66 #include "insets/insetlist.h"
68 #include "insets/insetcaption.h"
69 #include "insets/insetfloatlist.h"
71 #include "mathed/formulabase.h"
73 #include "graphics/Previews.h"
75 #include "support/LAssert.h"
76 #include "support/lstrings.h"
77 #include "support/filetools.h"
78 #include "support/lyxfunctional.h"
80 #include <boost/bind.hpp>
81 #include <boost/signals/connection.hpp>
90 extern string current_layout;
92 #ifndef CXX_GLOBAL_CSTD
105 using std::make_pair;
109 using lyx::textclass_type;
111 /* the selection possible is needed, that only motion events are
112 * used, where the bottom press event was on the drawing area too */
113 bool selection_possible = false;
115 extern BufferList bufferlist;
116 extern char ascii_type;
118 extern int bibitemMaxWidth(BufferView *, LyXFont const &);
123 unsigned int const saved_positions_num = 20;
125 // All the below connection objects are needed because of a bug in some
126 // versions of GCC (<=2.96 are on the suspects list.) By having and assigning
127 // to these connections we avoid a segfault upon startup, and also at exit.
130 boost::signals::connection timecon;
131 boost::signals::connection doccon;
132 boost::signals::connection resizecon;
133 boost::signals::connection bpresscon;
134 boost::signals::connection breleasecon;
135 boost::signals::connection motioncon;
136 boost::signals::connection doublecon;
137 boost::signals::connection triplecon;
138 boost::signals::connection kpresscon;
139 boost::signals::connection selectioncon;
140 boost::signals::connection lostcon;
145 BufferView::Pimpl::Pimpl(BufferView * bv, LyXView * owner,
146 int xpos, int ypos, int width, int height)
147 : bv_(bv), owner_(owner), buffer_(0), cursor_timeout(400),
148 using_xterm_cursor(false)
150 workarea_.reset(WorkAreaFactory::create(xpos, ypos, width, height));
151 screen_.reset(LyXScreenFactory::create(workarea()));
154 doccon = workarea().scrollDocView
155 .connect(boost::bind(&BufferView::Pimpl::scrollDocView, this, _1));
156 resizecon = workarea().workAreaResize
157 .connect(boost::bind(&BufferView::Pimpl::workAreaResize, this));
158 bpresscon = workarea().workAreaButtonPress
159 .connect(boost::bind(&BufferView::Pimpl::workAreaButtonPress, this, _1, _2, _3));
160 breleasecon = workarea().workAreaButtonRelease
161 .connect(boost::bind(&BufferView::Pimpl::workAreaButtonRelease, this, _1, _2, _3));
162 motioncon = workarea().workAreaMotionNotify
163 .connect(boost::bind(&BufferView::Pimpl::workAreaMotionNotify, this, _1, _2, _3));
164 doublecon = workarea().workAreaDoubleClick
165 .connect(boost::bind(&BufferView::Pimpl::doubleClick, this, _1, _2, _3));
166 triplecon = workarea().workAreaTripleClick
167 .connect(boost::bind(&BufferView::Pimpl::tripleClick, this, _1, _2, _3));
168 kpresscon = workarea().workAreaKeyPress
169 .connect(boost::bind(&BufferView::Pimpl::workAreaKeyPress, this, _1, _2));
170 selectioncon = workarea().selectionRequested
171 .connect(boost::bind(&BufferView::Pimpl::selectionRequested, this));
172 lostcon = workarea().selectionLost
173 .connect(boost::bind(&BufferView::Pimpl::selectionLost, this));
175 timecon = cursor_timeout.timeout
176 .connect(boost::bind(&BufferView::Pimpl::cursorToggle, this));
177 cursor_timeout.start();
178 saved_positions.resize(saved_positions_num);
182 WorkArea & BufferView::Pimpl::workarea() const
184 return *workarea_.get();
188 LyXScreen & BufferView::Pimpl::screen() const
190 return *screen_.get();
194 Painter & BufferView::Pimpl::painter() const
196 return workarea().getPainter();
200 void BufferView::Pimpl::buffer(Buffer * b)
202 lyxerr[Debug::INFO] << "Setting buffer in BufferView ("
205 buffer_->delUser(bv_);
207 // Put the old text into the TextCache, but
208 // only if the buffer is still loaded.
209 // Also set the owner of the test to 0
210 // bv_->text->owner(0);
211 textcache.add(buffer_, workarea().workWidth(), bv_->text);
212 if (lyxerr.debugging())
213 textcache.show(lyxerr, "BufferView::buffer");
218 // set current buffer
221 if (bufferlist.getState() == BufferList::CLOSING) return;
223 // if we are closing the buffer, use the first buffer as current
225 buffer_ = bufferlist.first();
229 lyxerr[Debug::INFO] << "Buffer addr: " << buffer_ << endl;
230 buffer_->addUser(bv_);
232 // If we don't have a text object for this, we make one
233 if (bv_->text == 0) {
234 resizeCurrentBuffer();
237 // FIXME: needed when ?
239 screen().topCursorVisible(bv_->text->cursor, bv_->text->first_y);
241 // Similarly, buffer-dependent dialogs should be updated or
242 // hidden. This should go here because some dialogs (eg ToC)
243 // require bv_->text.
244 owner_->getDialogs().updateBufferDependent(true);
246 lyxerr[Debug::INFO] << " No Buffer!" << endl;
247 owner_->getDialogs().hideBufferDependent();
249 // Also remove all remaining text's from the testcache.
250 // (there should not be any!) (if there is any it is a
252 if (lyxerr.debugging())
253 textcache.show(lyxerr, "buffer delete all");
259 owner_->updateMenubar();
260 owner_->updateToolbar();
261 owner_->updateLayoutChoice();
262 owner_->updateWindowTitle();
264 if (grfx::Previews::activated() && buffer_)
265 grfx::Previews::get().generateBufferPreviews(*buffer_);
269 bool BufferView::Pimpl::fitCursor()
273 if (bv_->theLockingInset()) {
274 bv_->theLockingInset()->fitInsetCursor(bv_);
277 ret = screen().fitCursor(bv_->text, bv_);
280 bv_->owner()->getDialogs().updateParagraph();
287 void BufferView::Pimpl::redoCurrentBuffer()
289 lyxerr[Debug::INFO] << "BufferView::redoCurrentBuffer" << endl;
290 if (buffer_ && bv_->text) {
291 resizeCurrentBuffer();
293 owner_->updateLayoutChoice();
299 int BufferView::Pimpl::resizeCurrentBuffer()
301 lyxerr[Debug::INFO] << "resizeCurrentBuffer" << endl;
304 Paragraph * selstartpar = 0;
305 Paragraph * selendpar = 0;
306 UpdatableInset * the_locking_inset = 0;
309 pos_type selstartpos = 0;
310 pos_type selendpos = 0;
311 bool selection = false;
312 bool mark_set = false;
314 owner_->prohibitInput();
316 owner_->message(_("Formatting document..."));
319 par = bv_->text->cursor.par();
320 pos = bv_->text->cursor.pos();
321 selstartpar = bv_->text->selection.start.par();
322 selstartpos = bv_->text->selection.start.pos();
323 selendpar = bv_->text->selection.end.par();
324 selendpos = bv_->text->selection.end.pos();
325 selection = bv_->text->selection.set();
326 mark_set = bv_->text->selection.mark();
327 the_locking_inset = bv_->theLockingInset();
328 buffer_->resizeInsets(bv_);
329 // I don't think the delete and new are necessary here we just could
330 // call only init! (Jug 20020419)
332 bv_->text = new LyXText(bv_);
333 bv_->text->init(bv_);
335 // See if we have a text in TextCache that fits
336 // the new buffer_ with the correct width.
337 bv_->text = textcache.findFit(buffer_, workarea().workWidth());
339 if (lyxerr.debugging()) {
340 lyxerr << "Found a LyXText that fits:\n";
341 textcache.show(lyxerr, make_pair(buffer_, make_pair(workarea().workWidth(), bv_->text)));
343 // Set the owner of the newly found text
344 // bv_->text->owner(bv_);
345 if (lyxerr.debugging())
346 textcache.show(lyxerr, "resizeCurrentBuffer");
348 bv_->text = new LyXText(bv_);
349 bv_->text->init(bv_);
350 //buffer_->resizeInsets(bv_);
355 bv_->text->selection.set(true);
356 // At this point just to avoid the Delete-Empty-Paragraph-
357 // Mechanism when setting the cursor.
358 bv_->text->selection.mark(mark_set);
360 bv_->text->setCursor(bv_, selstartpar, selstartpos);
361 bv_->text->selection.cursor = bv_->text->cursor;
362 bv_->text->setCursor(bv_, selendpar, selendpos);
363 bv_->text->setSelection(bv_);
364 bv_->text->setCursor(bv_, par, pos);
366 bv_->text->setCursor(bv_, par, pos);
367 bv_->text->selection.cursor = bv_->text->cursor;
368 bv_->text->selection.set(false);
370 // remake the inset locking
371 bv_->theLockingInset(the_locking_inset);
374 bv_->text->first_y = screen().topCursorVisible(bv_->text->cursor, bv_->text->first_y);
377 owner_->allowInput();
385 void BufferView::Pimpl::repaint()
387 // Regenerate the screen.
388 screen().redraw(bv_->text, bv_);
392 void BufferView::Pimpl::updateScrollbar()
395 lyxerr[Debug::GUI] << "no text in updateScrollbar" << endl;
396 workarea().setScrollbarParams(0, 0, 0);
400 LyXText const & t = *bv_->text;
402 lyxerr[Debug::GUI] << "Updating scrollbar: h " << t.height << ", first_y "
403 << t.first_y << ", default height " << t.defaultHeight() << endl;
405 workarea().setScrollbarParams(t.height, t.first_y, t.defaultHeight());
409 void BufferView::Pimpl::scrollDocView(int value)
411 lyxerr[Debug::GUI] << "scrollDocView of " << value << endl;
413 if (!buffer_) return;
415 screen().draw(bv_->text, bv_, value);
417 if (!lyxrc.cursor_follows_scrollbar) {
421 LyXText * vbt = bv_->text;
423 int const height = vbt->defaultHeight();
424 int const first = static_cast<int>((bv_->text->first_y + height));
425 int const last = static_cast<int>((bv_->text->first_y + workarea().workHeight() - height));
427 if (vbt->cursor.y() < first)
428 vbt->setCursorFromCoordinates(bv_, 0, first);
429 else if (vbt->cursor.y() > last)
430 vbt->setCursorFromCoordinates(bv_, 0, last);
434 int BufferView::Pimpl::scroll(long time)
439 LyXText const * t = bv_->text;
441 double const diff = t->defaultHeight()
442 + double(time) * double(time) * 0.125;
444 scrollDocView(int(diff));
445 workarea().setScrollbarParams(t->height, t->first_y, t->defaultHeight());
450 void BufferView::Pimpl::workAreaKeyPress(LyXKeySymPtr key,
451 key_modifier::state state)
453 bv_->owner()->getLyXFunc().processKeySym(key, state);
457 void BufferView::Pimpl::workAreaMotionNotify(int x, int y, mouse_button::state state)
459 // Only use motion with button 1
460 if (!(state & mouse_button::button1))
466 // Check for inset locking
467 if (bv_->theLockingInset()) {
468 LyXCursor cursor = bv_->text->cursor;
469 LyXFont font = bv_->text->getFont(buffer_,
470 cursor.par(), cursor.pos());
471 int width = bv_->theLockingInset()->width(bv_, font);
472 int inset_x = font.isVisibleRightToLeft()
473 ? cursor.ix() - width : cursor.ix();
474 int start_x = inset_x + bv_->theLockingInset()->scroll();
476 FuncRequest cmd(bv_, LFUN_MOUSE_MOTION,
477 x - start_x, y - cursor.iy() + bv_->text->first_y, state);
478 bv_->theLockingInset()->localDispatch(cmd);
482 // The test for not selection possible is needed, that only motion
483 // events are used, where the bottom press event was on
484 // the drawing area too
485 if (!selection_possible)
488 screen().hideCursor();
490 Row * cursorrow = bv_->text->cursor.row();
491 bv_->text->setCursorFromCoordinates(bv_, x, y + bv_->text->first_y);
493 // sorry for this but I have a strange error that the y value jumps at
494 // a certain point. This seems like an error in my xforms library or
495 // in some other local environment, but I would like to leave this here
496 // for the moment until I can remove this (Jug 20020418)
497 if (y_before < bv_->text->cursor.y())
498 lyxerr << y_before << ":" << bv_->text->cursor.y() << endl;
500 // This is to allow jumping over large insets
501 if (cursorrow == bv_->text->cursor.row()) {
502 if (y >= int(workarea().workHeight())) {
503 bv_->text->cursorDown(bv_, false);
505 bv_->text->cursorUp(bv_, false);
509 if (!bv_->text->selection.set())
510 update(bv_->text, BufferView::UPDATE); // Maybe an empty line was deleted
512 bv_->text->setSelection(bv_);
513 screen().toggleToggle(bv_->text, bv_);
519 // Single-click on work area
520 void BufferView::Pimpl::workAreaButtonPress(int xpos, int ypos,
521 mouse_button::state button)
526 // ok ok, this is a hack (for xforms)
527 if (button == mouse_button::button4) {
528 scroll(-lyxrc.wheel_jump);
529 // We shouldn't go further down as we really should only do the
530 // scrolling and be done with this. Otherwise we may open some
531 // dialogs (Jug 20020424).
533 } else if (button == mouse_button::button5) {
534 scroll(lyxrc.wheel_jump);
535 // We shouldn't go further down as we really should only do the
536 // scrolling and be done with this. Otherwise we may open some
537 // dialogs (Jug 20020424).
541 Inset * inset_hit = checkInsetHit(bv_->text, xpos, ypos);
543 // Middle button press pastes if we have a selection
544 // We do this here as if the selection was inside an inset
545 // it could get cleared on the unlocking of the inset so
546 // we have to check this first
547 bool paste_internally = false;
548 if (button == mouse_button::button2 && bv_->getLyXText()->selection.set()) {
549 owner_->dispatch(FuncRequest(LFUN_COPY));
550 paste_internally = true;
553 int const screen_first = bv_->text->first_y;
555 if (bv_->theLockingInset()) {
556 // We are in inset locking mode
558 // Check whether the inset was hit. If not reset mode,
559 // otherwise give the event to the inset
560 if (inset_hit == bv_->theLockingInset()) {
561 FuncRequest cmd(bv_, LFUN_MOUSE_PRESS, xpos, ypos, button);
562 bv_->theLockingInset()->localDispatch(cmd);
565 bv_->unlockInset(bv_->theLockingInset());
569 selection_possible = true;
570 screen().hideCursor();
572 // Clear the selection
573 screen().toggleSelection(bv_->text, bv_);
574 bv_->text->clearSelection();
575 bv_->text->fullRebreak(bv_);
579 // Single left click in math inset?
580 if (isHighlyEditableInset(inset_hit)) {
581 // Highly editable inset, like math
582 UpdatableInset * inset = static_cast<UpdatableInset *>(inset_hit);
583 selection_possible = false;
584 owner_->updateLayoutChoice();
585 owner_->message(inset->editMessage());
586 //inset->edit(bv_, xpos, ypos, button);
587 // We just have to lock the inset before calling a PressEvent on it!
588 // we don't need the edit() call here! (Jug20020329)
589 if (!bv_->lockInset(inset)) {
590 lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
592 FuncRequest cmd(bv_, LFUN_MOUSE_PRESS, xpos, ypos, button);
593 inset->localDispatch(cmd);
596 // I'm not sure we should continue here if we hit an inset (Jug20020403)
598 // Right click on a footnote flag opens float menu
599 if (button == mouse_button::button3) {
600 selection_possible = false;
604 if (!inset_hit) // otherwise it was already set in checkInsetHit(...)
605 bv_->text->setCursorFromCoordinates(bv_, xpos, ypos + screen_first);
607 bv_->text->selection.cursor = bv_->text->cursor;
608 bv_->text->cursor.x_fix(bv_->text->cursor.x());
610 owner_->updateLayoutChoice();
612 selection_possible = false;
615 // Insert primary selection with middle mouse
616 // if there is a local selection in the current buffer,
618 if (button == mouse_button::button2) {
619 if (paste_internally)
620 owner_->dispatch(FuncRequest(LFUN_PASTE));
622 owner_->dispatch(FuncRequest(LFUN_PASTESELECTION, "paragraph"));
623 selection_possible = false;
629 void BufferView::Pimpl::doubleClick(int /*x*/, int /*y*/, mouse_button::state button)
634 LyXText * text = bv_->getLyXText();
636 if (text->bv_owner && bv_->theLockingInset())
639 if (button == mouse_button::button1) {
640 if (text->bv_owner) {
641 screen().hideCursor();
642 screen().toggleSelection(text, bv_);
643 text->selectWord(bv_, LyXText::WHOLE_WORD_STRICT);
644 screen().toggleSelection(text, bv_, false);
646 text->selectWord(bv_, LyXText::WHOLE_WORD_STRICT);
648 // This will fit the cursor on the screen if necessary
649 update(text, BufferView::SELECT|BufferView::FITCUR);
650 workarea().haveSelection(bv_->getLyXText()->selection.set());
655 void BufferView::Pimpl::tripleClick(int /*x*/, int /*y*/, mouse_button::state button)
660 LyXText * text = bv_->getLyXText();
662 if (text->bv_owner && bv_->theLockingInset())
665 if (button == mouse_button::button1) {
666 if (text->bv_owner) {
667 screen().hideCursor();
668 screen().toggleSelection(text, bv_);
670 text->cursorHome(bv_);
671 text->selection.cursor = text->cursor;
672 text->cursorEnd(bv_);
673 text->setSelection(bv_);
674 if (text->bv_owner) {
675 screen().toggleSelection(text, bv_, false);
677 // This will fit the cursor on the screen if necessary
678 update(text, BufferView::SELECT|BufferView::FITCUR);
679 workarea().haveSelection(bv_->getLyXText()->selection.set());
684 void BufferView::Pimpl::selectionRequested()
691 LyXText * text = bv_->getLyXText();
693 if (text->selection.set() &&
694 (!bv_->text->xsel_cache.set() ||
695 text->selection.start != bv_->text->xsel_cache.start ||
696 text->selection.end != bv_->text->xsel_cache.end))
698 bv_->text->xsel_cache = text->selection;
699 sel = text->selectionAsString(bv_->buffer(), false);
700 } else if (!text->selection.set()) {
702 bv_->text->xsel_cache.set(false);
705 workarea().putClipboard(sel);
710 void BufferView::Pimpl::selectionLost()
715 bv_->getLyXText()->clearSelection();
717 bv_->text->xsel_cache.set(false);
722 void BufferView::Pimpl::workAreaButtonRelease(int x, int y,
723 mouse_button::state button)
725 // do nothing if we used the mouse wheel
726 if (!buffer_ || button == mouse_button::button4 || button == mouse_button::button5)
729 // If we hit an inset, we have the inset coordinates in these
730 // and inset_hit points to the inset. If we do not hit an
731 // inset, inset_hit is 0, and inset_x == x, inset_y == y.
732 Inset * inset_hit = checkInsetHit(bv_->text, x, y);
734 if (bv_->theLockingInset()) {
735 // We are in inset locking mode.
737 // LyX does a kind of work-area grabbing for insets.
738 // Only a ButtonPress FuncRequest outside the inset will
739 // force a insetUnlock.
740 FuncRequest cmd(bv_, LFUN_MOUSE_RELEASE, x, y, button);
741 bv_->theLockingInset()->localDispatch(cmd);
745 selection_possible = false;
747 if (button == mouse_button::button2)
751 if (button == mouse_button::button1) {
752 workarea().haveSelection(bv_->getLyXText()->selection.set());
756 owner_->view_state_changed();
757 owner_->updateMenubar();
758 owner_->updateToolbar();
760 // Did we hit an editable inset?
762 selection_possible = false;
764 // if we reach this point with a selection, it
765 // must mean we are currently selecting.
766 // But we don't want to open the inset
767 // because that is annoying for the user.
768 // So just pretend we didn't hit it.
769 // this is OK because a "kosher" ButtonRelease
770 // will follow a ButtonPress that clears
772 // Note this also fixes selection drawing
773 // problems if we end up opening an inset
774 if (bv_->getLyXText()->selection.set())
777 // CHECK fix this proper in 0.13
778 // well, maybe 13.0 !!!!!!!!!
780 // Following a ref shouldn't issue
781 // a push on the undo-stack
782 // anylonger, now that we have
783 // keybindings for following
784 // references and returning from
785 // references. IMHO though, it
786 // should be the inset's own business
787 // to push or not push on the undo
788 // stack. They don't *have* to
789 // alter the document...
791 // ...or maybe the SetCursorParUndo()
792 // below isn't necessary at all anylonger?
793 if (inset_hit->lyxCode() == Inset::REF_CODE) {
794 setCursorParUndo(bv_);
797 owner_->message(inset_hit->editMessage());
799 if (isHighlyEditableInset(inset_hit)) {
800 // Highly editable inset, like math
801 UpdatableInset *inset = (UpdatableInset *)inset_hit;
802 FuncRequest cmd(bv_, LFUN_MOUSE_RELEASE, x, y, button);
803 inset->localDispatch(cmd);
805 FuncRequest cmd(bv_, LFUN_MOUSE_RELEASE, x, y, button);
806 inset_hit->localDispatch(cmd);
807 // IMO this is a grosshack! Inset's should be changed so that
808 // they call the actions they have to do with the insetButtonRel.
809 // function and not in the edit(). This should be changed
812 #warning Please remove donot call inset->edit() here (Jug 20020812)
814 inset_hit->edit(bv_, x, y, button);
819 // Maybe we want to edit a bibitem ale970302
820 if (bv_->text->cursor.par()->bibkey) {
821 bool const is_rtl = bv_->text->cursor.par()->isRightToLeftPar(buffer_->params);
822 int const width = bibitemMaxWidth(bv_, buffer_->params.getLyXTextClass().defaultfont());
823 if ((is_rtl && x > bv_->text->workWidth(bv_)-20-width) ||
824 (!is_rtl && x < 20+width)) {
825 bv_->text->cursor.par()->bibkey->edit(bv_, 0, 0, mouse_button::none);
833 Box BufferView::Pimpl::insetDimensions(LyXText const & text,
834 LyXCursor const & cursor) const
836 Paragraph /*const*/ & par = *cursor.par();
837 pos_type const pos = cursor.pos();
839 lyx::Assert(par.getInset(pos));
841 Inset const & inset(*par.getInset(pos));
843 LyXFont const & font = text.getFont(buffer_, &par, pos);
845 int const width = inset.width(bv_, font);
846 int const inset_x = font.isVisibleRightToLeft()
847 ? (cursor.ix() - width) : cursor.ix();
850 inset_x + inset.scroll(),
852 cursor.iy() - inset.ascent(bv_, font),
853 cursor.iy() + inset.descent(bv_, font));
857 Inset * BufferView::Pimpl::checkInset(LyXText const & text,
858 LyXCursor const & cursor,
859 int & x, int & y) const
861 pos_type const pos(cursor.pos());
862 Paragraph /*const*/ & par(*cursor.par());
864 if (pos >= par.size() || !par.isInset(pos)) {
868 Inset /*const*/ * inset = par.getInset(pos);
870 if (!isEditableInset(inset)) {
874 Box b(insetDimensions(text, cursor));
876 if (!b.contained(x, y)) {
877 lyxerr[Debug::GUI] << "Missed inset at x,y " << x << "," << y
878 << " box " << b << endl;
882 text.setCursor(bv_, &par, pos, true);
885 // The origin of an inset is on the baseline
886 y -= text.cursor.iy();
892 Inset * BufferView::Pimpl::checkInsetHit(LyXText * text, int & x, int & y)
894 int y_tmp = y + text->first_y;
897 text->setCursorFromCoordinates(bv_, cursor, x, y_tmp);
899 Inset * inset(checkInset(*text, cursor, x, y_tmp));
906 // look at previous position
908 if (cursor.pos() == 0) {
913 text->setCursor(bv_, cursor, cursor.par(), cursor.pos() - 1, true);
915 inset = checkInset(*text, cursor, x, y_tmp);
923 void BufferView::Pimpl::workAreaResize()
925 static int work_area_width;
926 static int work_area_height;
928 bool const widthChange = workarea().workWidth() != work_area_width;
929 bool const heightChange = workarea().workHeight() != work_area_height;
931 // update from work area
932 work_area_width = workarea().workWidth();
933 work_area_height = workarea().workHeight();
937 // The visible LyXView need a resize
938 resizeCurrentBuffer();
940 // Remove all texts from the textcache
941 // This is not _really_ what we want to do. What
942 // we really want to do is to delete in textcache
943 // that does not have a BufferView with matching
944 // width, but as long as we have only one BufferView
945 // deleting all gives the same result.
946 if (lyxerr.debugging())
947 textcache.show(lyxerr, "Expose delete all");
949 // FIXME: this is aalready done in resizeCurrentBuffer() ??
950 buffer_->resizeInsets(bv_);
951 } else if (heightChange) {
952 // fitCursor() ensures we don't jump back
953 // to the start of the document on vertical
959 if (widthChange || heightChange) {
963 // always make sure that the scrollbar is sane.
965 owner_->updateLayoutChoice();
970 void BufferView::Pimpl::update()
972 if (!bv_->theLockingInset() || !bv_->theLockingInset()->nodraw()) {
973 LyXText::text_status st = bv_->text->status();
974 screen().update(bv_->text, bv_);
976 while (bv_->text->status() == LyXText::CHANGED_IN_DRAW) {
977 bv_->text->fullRebreak(bv_);
978 st = LyXText::NEED_MORE_REFRESH;
979 bv_->text->setCursor(bv_, bv_->text->cursor.par(),
980 bv_->text->cursor.pos());
981 if (bv_->text->selection.set()) {
982 bv_->text->setCursor(bv_, bv_->text->selection.start,
983 bv_->text->selection.start.par(),
984 bv_->text->selection.start.pos());
985 bv_->text->setCursor(bv_, bv_->text->selection.end,
986 bv_->text->selection.end.par(),
987 bv_->text->selection.end.pos());
990 bv_->text->status(bv_, st);
991 screen().update(bv_->text, bv_);
993 // do this here instead of in the screen::update because of
995 bv_->text->status(bv_, LyXText::UNCHANGED);
1001 // Values used when calling update:
1003 // -2 - update, move sel_cursor if selection, fitcursor
1004 // -1 - update, move sel_cursor if selection, fitcursor, mark dirty
1005 // 0 - update, move sel_cursor if selection, fitcursor
1006 // 1 - update, move sel_cursor if selection, fitcursor, mark dirty
1007 // 3 - update, move sel_cursor if selection
1010 // a simple redraw of the parts that need refresh
1012 // move sel_cursor if selection -
1013 // the text's sel_cursor is moved if there is selection is progress
1016 // fitCursor() is called and the scrollbar updated
1019 // the buffer is marked dirty.
1028 // UPDATE_ONLY = UPDATE;
1029 // UPDATE_SELECT = UPDATE | SELECT;
1030 // UPDATE_SELECT_MOVE = UPDATE | SELECT | FITCUR;
1031 // UPDATE_SELECT_MOVE_AFTER_CHANGE = UPDATE | SELECT | FITCUR | CHANGE;
1033 // update(-3) -> update(0) -> update(0) -> update(UPDATE)
1034 // update(-2) -> update(1 + 2) -> update(3) -> update(SELECT|FITCUR)
1035 // update(-1) -> update(1 + 2 + 4) -> update(7) -> update(SELECT|FITCUR|CHANGE)
1036 // update(1) -> update(1 + 2 + 4) -> update(7) -> update(SELECT|FITCUR|CHANGE)
1037 // update(3) -> update(1) -> update(1) -> update(SELECT)
1039 void BufferView::Pimpl::update(LyXText * text, BufferView::UpdateCodes f)
1041 owner_->updateLayoutChoice();
1043 if (!text->selection.set() && (f & SELECT)) {
1044 text->selection.cursor = text->cursor;
1047 text->fullRebreak(bv_);
1049 if (text->inset_owner) {
1050 text->inset_owner->setUpdateStatus(bv_, InsetText::NONE);
1051 updateInset(text->inset_owner, false);
1061 buffer_->markDirty();
1066 // Callback for cursor timer
1067 void BufferView::Pimpl::cursorToggle()
1070 cursor_timeout.restart();
1075 extern void reapSpellchecker(void);
1078 if (!bv_->theLockingInset()) {
1079 screen().cursorToggle(bv_);
1081 bv_->theLockingInset()->toggleInsetCursor(bv_);
1084 cursor_timeout.restart();
1088 void BufferView::Pimpl::cursorPrevious(LyXText * text)
1090 if (!text->cursor.row()->previous()) {
1091 if (text->first_y > 0) {
1092 int new_y = bv_->text->first_y - workarea().workHeight();
1093 screen().draw(bv_->text, bv_, new_y < 0 ? 0 : new_y);
1099 int y = text->first_y;
1100 Row * cursorrow = text->cursor.row();
1102 text->setCursorFromCoordinates(bv_, text->cursor.x_fix(), y);
1106 if (cursorrow == bv_->text->cursor.row()) {
1107 // we have a row which is higher than the workarea so we leave the
1108 // cursor on the start of the row and move only the draw up as soon
1109 // as we move the cursor or do something while inside the row (it may
1110 // span several workarea-heights) we'll move to the top again, but this
1111 // is better than just jump down and only display part of the row.
1112 new_y = bv_->text->first_y - workarea().workHeight();
1114 if (text->inset_owner) {
1115 new_y = bv_->text->cursor.iy()
1116 + bv_->theLockingInset()->insetInInsetY() + y
1117 + text->cursor.row()->height()
1118 - workarea().workHeight() + 1;
1120 new_y = text->cursor.y()
1121 - text->cursor.row()->baseline()
1122 + text->cursor.row()->height()
1123 - workarea().workHeight() + 1;
1126 screen().draw(bv_->text, bv_, new_y < 0 ? 0 : new_y);
1127 if (text->cursor.row()->previous()) {
1129 text->setCursor(bv_, cur, text->cursor.row()->previous()->par(),
1130 text->cursor.row()->previous()->pos(), false);
1131 if (cur.y() > text->first_y) {
1132 text->cursorUp(bv_, true);
1139 void BufferView::Pimpl::cursorNext(LyXText * text)
1141 if (!text->cursor.row()->next()) {
1142 int y = text->cursor.y() - text->cursor.row()->baseline() +
1143 text->cursor.row()->height();
1144 if (y > int(text->first_y + workarea().workHeight())) {
1145 screen().draw(bv_->text, bv_,
1146 bv_->text->first_y + workarea().workHeight());
1152 int y = text->first_y + workarea().workHeight();
1153 if (text->inset_owner && !text->first_y) {
1154 y -= (bv_->text->cursor.iy()
1155 - bv_->text->first_y
1156 + bv_->theLockingInset()->insetInInsetY());
1159 text->getRowNearY(y);
1161 Row * cursorrow = text->cursor.row();
1162 text->setCursorFromCoordinates(bv_, text->cursor.x_fix(), y); // + workarea().workHeight());
1166 if (cursorrow == bv_->text->cursor.row()) {
1167 // we have a row which is higher than the workarea so we leave the
1168 // cursor on the start of the row and move only the draw down as soon
1169 // as we move the cursor or do something while inside the row (it may
1170 // span several workarea-heights) we'll move to the top again, but this
1171 // is better than just jump down and only display part of the row.
1172 new_y = bv_->text->first_y + workarea().workHeight();
1174 if (text->inset_owner) {
1175 new_y = bv_->text->cursor.iy()
1176 + bv_->theLockingInset()->insetInInsetY()
1177 + y - text->cursor.row()->baseline();
1179 new_y = text->cursor.y() - text->cursor.row()->baseline();
1182 screen().draw(bv_->text, bv_, new_y);
1183 if (text->cursor.row()->next()) {
1185 text->setCursor(bv_, cur, text->cursor.row()->next()->par(),
1186 text->cursor.row()->next()->pos(), false);
1187 if (cur.y() < int(text->first_y + workarea().workHeight())) {
1188 text->cursorDown(bv_, true);
1195 bool BufferView::Pimpl::available() const
1197 if (buffer_ && bv_->text)
1203 void BufferView::Pimpl::beforeChange(LyXText * text)
1206 text->clearSelection();
1210 void BufferView::Pimpl::finishChange(bool fitcur)
1213 moveCursorUpdate(fitcur);
1214 bv_->owner()->view_state_changed();
1218 void BufferView::Pimpl::savePosition(unsigned int i)
1220 if (i >= saved_positions_num)
1222 saved_positions[i] = Position(buffer_->fileName(),
1223 bv_->text->cursor.par()->id(),
1224 bv_->text->cursor.pos());
1227 str << _("Saved bookmark") << ' ' << i;
1228 owner_->message(str.str().c_str());
1233 void BufferView::Pimpl::restorePosition(unsigned int i)
1235 if (i >= saved_positions_num)
1238 string const fname = saved_positions[i].filename;
1240 beforeChange(bv_->text);
1242 if (fname != buffer_->fileName()) {
1243 Buffer * b = bufferlist.exists(fname) ?
1244 bufferlist.getBuffer(fname) :
1245 bufferlist.loadLyXFile(fname); // don't ask, just load it
1246 if (b != 0) buffer(b);
1249 Paragraph * par = buffer_->getParFromID(saved_positions[i].par_id);
1253 bv_->text->setCursor(bv_, par,
1254 min(par->size(), saved_positions[i].par_pos));
1256 update(bv_->text, BufferView::SELECT | BufferView::FITCUR);
1259 str << _("Moved to bookmark") << ' ' << i;
1260 owner_->message(str.str().c_str());
1265 bool BufferView::Pimpl::isSavedPosition(unsigned int i)
1267 if (i >= saved_positions_num)
1270 return !saved_positions[i].filename.empty();
1274 void BufferView::Pimpl::switchKeyMap()
1276 if (!lyxrc.rtl_support)
1279 LyXText * text = bv_->getLyXText();
1280 if (text->real_current_font.isRightToLeft()
1281 && !(bv_->theLockingInset()
1282 && bv_->theLockingInset()->lyxCode()== Inset::ERT_CODE))
1284 if (owner_->getIntl().keymap == Intl::PRIMARY)
1285 owner_->getIntl().KeyMapSec();
1287 if (owner_->getIntl().keymap == Intl::SECONDARY)
1288 owner_->getIntl().KeyMapPrim();
1293 void BufferView::Pimpl::insetUnlock()
1295 if (bv_->theLockingInset()) {
1296 bv_->theLockingInset()->insetUnlock(bv_);
1297 bv_->theLockingInset(0);
1303 void BufferView::Pimpl::showCursor()
1305 if (bv_->theLockingInset())
1306 bv_->theLockingInset()->showInsetCursor(bv_);
1308 screen().showCursor(bv_->text, bv_);
1312 void BufferView::Pimpl::hideCursor()
1314 if (!bv_->theLockingInset())
1315 screen().hideCursor();
1319 void BufferView::Pimpl::toggleSelection(bool b)
1321 if (bv_->theLockingInset())
1322 bv_->theLockingInset()->toggleSelection(bv_, b);
1323 screen().toggleSelection(bv_->text, bv_, b);
1327 void BufferView::Pimpl::toggleToggle()
1329 screen().toggleToggle(bv_->text, bv_);
1333 void BufferView::Pimpl::center()
1335 LyXText * t = bv_->text;
1338 int const half_height = workarea().workHeight() / 2;
1341 if (t->cursor.y() > half_height) {
1342 new_y = t->cursor.y() - half_height;
1345 // FIXME: can we do this w/o calling screen directly ?
1346 // This updates first_y but means the fitCursor() call
1347 // from the update(FITCUR) doesn't realise that we might
1348 // have moved (e.g. from GOTOPARAGRAPH), so doesn't cause
1349 // the scrollbar to be updated as it should, so we have
1350 // to do it manually. Any operation that does a center()
1351 // and also might have moved first_y must make sure to call
1352 // updateScrollbar() currently. Never mind that this is a
1353 // pretty obfuscated way of updating t->first_y
1354 screen().draw(t, bv_, new_y);
1356 update(t, BufferView::SELECT | BufferView::FITCUR);
1360 void BufferView::Pimpl::pasteClipboard(bool asPara)
1365 screen().hideCursor();
1366 beforeChange(bv_->text);
1368 string const clip(workarea().getClipboard());
1374 bv_->getLyXText()->insertStringAsParagraphs(bv_, clip);
1376 bv_->getLyXText()->insertStringAsLines(bv_, clip);
1378 bv_->getLyXText()->clearSelection();
1379 update(bv_->text, BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
1383 void BufferView::Pimpl::stuffClipboard(string const & stuff) const
1385 workarea().putClipboard(stuff);
1390 * Dispatch functions for actions which can be valid for BufferView->text
1391 * and/or InsetText->text!!!
1396 void BufferView::Pimpl::moveCursorUpdate(bool selecting, bool fitcur)
1398 LyXText * lt = bv_->getLyXText();
1400 if (selecting || lt->selection.mark()) {
1401 lt->setSelection(bv_);
1405 updateInset(lt->inset_owner, false);
1409 update(lt, BufferView::SELECT|BufferView::FITCUR);
1411 update(lt, BufferView::SELECT);
1413 } else if (bv_->text->status() != LyXText::UNCHANGED) {
1414 bv_->theLockingInset()->hideInsetCursor(bv_);
1415 update(bv_->text, BufferView::SELECT|BufferView::FITCUR);
1419 if (!lt->selection.set())
1420 workarea().haveSelection(false);
1426 Inset * BufferView::Pimpl::getInsetByCode(Inset::Code code)
1428 LyXCursor cursor = bv_->getLyXText()->cursor;
1429 Buffer::inset_iterator it =
1430 find_if(Buffer::inset_iterator(
1431 cursor.par(), cursor.pos()),
1432 buffer_->inset_iterator_end(),
1433 lyx::compare_memfun(&Inset::lyxCode, code));
1434 return it != buffer_->inset_iterator_end() ? (*it) : 0;
1438 void BufferView::Pimpl::MenuInsertLyXFile(string const & filen)
1440 string filename = filen;
1442 if (filename.empty()) {
1443 // Launch a file browser
1444 string initpath = lyxrc.document_path;
1447 string const trypath = owner_->buffer()->filePath();
1448 // If directory is writeable, use this as default.
1449 if (IsDirWriteable(trypath))
1453 FileDialog fileDlg(bv_->owner(),
1454 _("Select LyX document to insert"),
1456 make_pair(string(_("Documents|#o#O")),
1457 string(lyxrc.document_path)),
1458 make_pair(string(_("Examples|#E#e")),
1459 string(AddPath(system_lyxdir, "examples"))));
1461 FileDialog::Result result =
1462 fileDlg.Select(initpath,
1463 _("*.lyx| LyX Documents (*.lyx)"));
1465 if (result.first == FileDialog::Later)
1468 filename = result.second;
1470 // check selected filename
1471 if (filename.empty()) {
1472 owner_->message(_("Canceled."));
1477 // get absolute path of file and add ".lyx" to the filename if
1479 filename = FileSearch(string(), filename, "lyx");
1481 string const disp_fn(MakeDisplayPath(filename));
1484 s1 << _("Inserting document") << ' '
1485 << disp_fn << " ...";
1486 owner_->message(s1.str().c_str());
1487 bool const res = bv_->insertLyXFile(filename);
1490 str << _("Document") << ' ' << disp_fn
1491 << ' ' << _("inserted.");
1492 owner_->message(str.str().c_str());
1495 str << _("Could not insert document") << ' '
1497 owner_->message(str.str().c_str());
1502 bool BufferView::Pimpl::dispatch(FuncRequest const & ev)
1504 lyxerr[Debug::ACTION] << "BufferView::Pimpl::Dispatch: action["
1505 << ev.action <<"] arg[" << ev.argument << "]" << endl;
1507 LyXTextClass const & tclass = buffer_->params.getLyXTextClass();
1509 switch (ev.action) {
1511 case LFUN_TOC_INSERT:
1513 InsetCommandParams p;
1514 p.setCmdName("tableofcontents");
1515 Inset * inset = new InsetTOC(p);
1516 if (!insertInset(inset, tclass.defaultLayoutName()))
1521 case LFUN_SCROLL_INSET:
1522 // this is not handled here as this function is only active
1523 // if we have a locking_inset and that one is (or contains)
1527 case LFUN_INSET_GRAPHICS:
1529 Inset * new_inset = new InsetGraphics;
1530 if (!insertInset(new_inset)) {
1533 // this is need because you don't use a inset->Edit()
1534 updateInset(new_inset, true);
1535 new_inset->edit(bv_);
1545 case LFUN_PASTESELECTION:
1546 pasteClipboard(ev.argument == "paragraph");
1557 case LFUN_LAYOUT_COPY:
1558 bv_->copyEnvironment();
1561 case LFUN_LAYOUT_PASTE:
1562 bv_->pasteEnvironment();
1566 case LFUN_GOTOERROR:
1567 gotoInset(Inset::ERROR_CODE, false);
1571 gotoInset(Inset::NOTE_CODE, false);
1574 case LFUN_REFERENCE_GOTO:
1576 vector<Inset::Code> tmp;
1577 tmp.push_back(Inset::LABEL_CODE);
1578 tmp.push_back(Inset::REF_CODE);
1579 gotoInset(tmp, true);
1583 case LFUN_DEPTH_MIN:
1584 changeDepth(bv_, bv_->getLyXText(), -1);
1587 case LFUN_DEPTH_PLUS:
1588 changeDepth(bv_, bv_->getLyXText(), 1);
1592 owner_->getDialogs().setUserFreeFont();
1595 case LFUN_FILE_INSERT:
1596 MenuInsertLyXFile(ev.argument);
1599 case LFUN_FILE_INSERT_ASCII_PARA:
1600 InsertAsciiFile(bv_, ev.argument, true);
1603 case LFUN_FILE_INSERT_ASCII:
1604 InsertAsciiFile(bv_, ev.argument, false);
1609 lyxerr[Debug::INFO] << "LFUN_LAYOUT: (arg) "
1610 << ev.argument << endl;
1612 // This is not the good solution to the empty argument
1613 // problem, but it will hopefully suffice for 1.2.0.
1614 // The correct solution would be to augument the
1615 // function list/array with information about what
1616 // functions needs arguments and their type.
1617 if (ev.argument.empty()) {
1618 owner_->getLyXFunc().setErrorMessage(
1619 _("LyX function 'layout' needs an argument."));
1623 // Derive layout number from given argument (string)
1624 // and current buffer's textclass (number). */
1625 bool hasLayout = tclass.hasLayout(ev.argument);
1626 string layout = ev.argument;
1628 // If the entry is obsolete, use the new one instead.
1630 string const & obs = tclass[layout]->obsoleted_by();
1636 owner_->getLyXFunc().setErrorMessage(
1637 string(N_("Layout ")) + ev.argument +
1642 bool change_layout = (current_layout != layout);
1643 LyXText * lt = bv_->getLyXText();
1644 if (!change_layout && lt->selection.set() &&
1645 lt->selection.start.par() != lt->selection.end.par())
1647 Paragraph * spar = lt->selection.start.par();
1648 Paragraph * epar = lt->selection.end.par()->next();
1649 while(spar != epar) {
1650 if (spar->layout()->name() != current_layout) {
1651 change_layout = true;
1656 if (change_layout) {
1658 current_layout = layout;
1661 | BufferView::FITCUR);
1662 lt->setLayout(bv_, layout);
1663 owner_->setLayout(layout);
1666 | BufferView::FITCUR
1667 | BufferView::CHANGE);
1674 lang(bv_, ev.argument);
1676 owner_->view_state_changed();
1681 owner_->view_state_changed();
1686 owner_->view_state_changed();
1691 owner_->view_state_changed();
1696 owner_->view_state_changed();
1701 owner_->view_state_changed();
1706 owner_->view_state_changed();
1711 owner_->view_state_changed();
1714 case LFUN_UNDERLINE:
1716 owner_->view_state_changed();
1719 case LFUN_FONT_SIZE:
1720 fontSize(bv_, ev.argument);
1721 owner_->view_state_changed();
1724 case LFUN_FONT_STATE:
1725 owner_->getLyXFunc().setMessage(currentState(bv_));
1728 case LFUN_INSERT_LABEL:
1729 MenuInsertLabel(bv_, ev.argument);
1732 case LFUN_REF_INSERT:
1733 if (ev.argument.empty()) {
1734 InsetCommandParams p("ref");
1735 owner_->getDialogs().createRef(p.getAsString());
1737 InsetCommandParams p;
1738 p.setFromString(ev.argument);
1740 InsetRef * inset = new InsetRef(p, *buffer_);
1741 if (!insertInset(inset))
1744 updateInset(inset, true);
1748 case LFUN_BOOKMARK_SAVE:
1749 savePosition(strToUnsignedInt(ev.argument));
1752 case LFUN_BOOKMARK_GOTO:
1753 restorePosition(strToUnsignedInt(ev.argument));
1758 string label = ev.argument;
1759 if (label.empty()) {
1761 static_cast<InsetRef*>(getInsetByCode(Inset::REF_CODE));
1763 label = inset->getContents();
1768 if (!label.empty()) {
1769 //bv_->savePosition(0);
1770 if (!bv_->gotoLabel(label))
1771 Alert::alert(_("Error"),
1772 _("Couldn't find this label"),
1773 _("in current document."));
1780 LyXText * lt = bv_->getLyXText();
1782 if (!lt->selection.set()) {
1784 lt->selection.cursor = lt->cursor;
1787 | BufferView::FITCUR
1788 | BufferView::CHANGE);
1789 // It is possible to make it a lot faster still
1790 // just comment out the line below...
1795 moveCursorUpdate(false);
1796 owner_->view_state_changed();
1801 case LFUN_DELETE_SKIP:
1803 LyXText * lt = bv_->getLyXText();
1805 // Reverse the effect of LFUN_BREAKPARAGRAPH_SKIP.
1807 LyXCursor cursor = lt->cursor;
1809 if (!lt->selection.set()) {
1810 if (cursor.pos() == cursor.par()->size()) {
1811 lt->cursorRight(bv_);
1812 cursor = lt->cursor;
1813 if (cursor.pos() == 0
1814 && !(cursor.par()->params().spaceTop()
1815 == VSpace (VSpace::NONE))) {
1818 cursor.par()->params().lineTop(),
1819 cursor.par()->params().lineBottom(),
1820 cursor.par()->params().pagebreakTop(),
1821 cursor.par()->params().pagebreakBottom(),
1822 VSpace(VSpace::NONE),
1823 cursor.par()->params().spaceBottom(),
1824 cursor.par()->params().spacing(),
1825 cursor.par()->params().align(),
1826 cursor.par()->params().labelWidthString(), 0);
1827 lt->cursorLeft(bv_);
1830 | BufferView::FITCUR
1831 | BufferView::CHANGE);
1833 lt->cursorLeft(bv_);
1835 lt->selection.cursor = lt->cursor;
1838 | BufferView::FITCUR
1839 | BufferView::CHANGE);
1843 lt->selection.cursor = lt->cursor;
1846 | BufferView::FITCUR
1847 | BufferView::CHANGE);
1856 case LFUN_BACKSPACE:
1858 LyXText * lt = bv_->getLyXText();
1860 if (!lt->selection.set()) {
1861 if (owner_->getIntl().getTransManager().backspace()) {
1863 lt->selection.cursor = lt->cursor;
1866 | BufferView::FITCUR
1867 | BufferView::CHANGE);
1868 // It is possible to make it a lot faster still
1869 // just comment out the line below...
1875 owner_->view_state_changed();
1880 case LFUN_BACKSPACE_SKIP:
1882 // Reverse the effect of LFUN_BREAKPARAGRAPH_SKIP.
1883 LyXText * lt = bv_->getLyXText();
1885 LyXCursor cursor = lt->cursor;
1887 if (!lt->selection.set()) {
1888 if (cursor.pos() == 0
1889 && !(cursor.par()->params().spaceTop()
1890 == VSpace (VSpace::NONE))) {
1893 cursor.par()->params().lineTop(),
1894 cursor.par()->params().lineBottom(),
1895 cursor.par()->params().pagebreakTop(),
1896 cursor.par()->params().pagebreakBottom(),
1897 VSpace(VSpace::NONE), cursor.par()->params().spaceBottom(),
1898 cursor.par()->params().spacing(),
1899 cursor.par()->params().align(),
1900 cursor.par()->params().labelWidthString(), 0);
1903 | BufferView::FITCUR
1904 | BufferView::CHANGE);
1907 lt->selection.cursor = cursor;
1910 | BufferView::FITCUR
1911 | BufferView::CHANGE);
1918 case LFUN_BREAKPARAGRAPH:
1920 LyXText * lt = bv_->getLyXText();
1923 lt->breakParagraph(bv_, 0);
1926 | BufferView::FITCUR
1927 | BufferView::CHANGE);
1928 lt->selection.cursor = lt->cursor;
1930 owner_->view_state_changed();
1934 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
1936 LyXText * lt = bv_->getLyXText();
1939 lt->breakParagraph(bv_, 1);
1942 | BufferView::FITCUR
1943 | BufferView::CHANGE);
1944 lt->selection.cursor = lt->cursor;
1946 owner_->view_state_changed();
1950 case LFUN_BREAKPARAGRAPH_SKIP:
1952 // When at the beginning of a paragraph, remove
1953 // indentation and add a "defskip" at the top.
1954 // Otherwise, do the same as LFUN_BREAKPARAGRAPH.
1955 LyXText * lt = bv_->getLyXText();
1957 LyXCursor cursor = lt->cursor;
1960 if (cursor.pos() == 0) {
1961 if (cursor.par()->params().spaceTop() == VSpace(VSpace::NONE)) {
1964 cursor.par()->params().lineTop(),
1965 cursor.par()->params().lineBottom(),
1966 cursor.par()->params().pagebreakTop(),
1967 cursor.par()->params().pagebreakBottom(),
1968 VSpace(VSpace::DEFSKIP), cursor.par()->params().spaceBottom(),
1969 cursor.par()->params().spacing(),
1970 cursor.par()->params().align(),
1971 cursor.par()->params().labelWidthString(), 1);
1972 //update(BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
1976 lt->breakParagraph(bv_, 0);
1977 //update(BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
1982 | BufferView::FITCUR
1983 | BufferView::CHANGE);
1984 lt->selection.cursor = cursor;
1986 owner_->view_state_changed();
1990 case LFUN_PARAGRAPH_SPACING:
1992 LyXText * lt = bv_->getLyXText();
1994 Paragraph * par = lt->cursor.par();
1995 Spacing::Space cur_spacing = par->params().spacing().getSpace();
1996 float cur_value = 1.0;
1997 if (cur_spacing == Spacing::Other) {
1998 cur_value = par->params().spacing().getValue();
2001 istringstream istr(ev.argument.c_str());
2005 Spacing::Space new_spacing = cur_spacing;
2006 float new_value = cur_value;
2008 lyxerr << "Missing argument to `paragraph-spacing'"
2010 } else if (tmp == "single") {
2011 new_spacing = Spacing::Single;
2012 } else if (tmp == "onehalf") {
2013 new_spacing = Spacing::Onehalf;
2014 } else if (tmp == "double") {
2015 new_spacing = Spacing::Double;
2016 } else if (tmp == "other") {
2017 new_spacing = Spacing::Other;
2020 lyxerr << "new_value = " << tmpval << endl;
2023 } else if (tmp == "default") {
2024 new_spacing = Spacing::Default;
2026 lyxerr << _("Unknown spacing argument: ")
2027 << ev.argument << endl;
2029 if (cur_spacing != new_spacing || cur_value != new_value) {
2030 par->params().spacing(Spacing(new_spacing, new_value));
2031 lt->redoParagraph(bv_);
2034 | BufferView::FITCUR
2035 | BufferView::CHANGE);
2040 case LFUN_INSET_TOGGLE:
2042 LyXText * lt = bv_->getLyXText();
2045 update(lt, BufferView::SELECT|BufferView::FITCUR);
2046 lt->toggleInset(bv_);
2047 update(lt, BufferView::SELECT|BufferView::FITCUR);
2060 InsetCommandParams p;
2061 if (ev.action == LFUN_HTMLURL)
2062 p.setCmdName("htmlurl");
2064 p.setCmdName("url");
2065 owner_->getDialogs().createUrl(p.getAsString());
2069 case LFUN_INSERT_URL:
2071 InsetCommandParams p;
2072 p.setFromString(ev.argument);
2074 InsetUrl * inset = new InsetUrl(p);
2075 if (!insertInset(inset))
2078 updateInset(inset, true);
2082 case LFUN_INSET_ERT:
2083 insertAndEditInset(new InsetERT(buffer_->params));
2086 case LFUN_INSET_EXTERNAL:
2087 insertAndEditInset(new InsetExternal);
2090 case LFUN_INSET_FOOTNOTE:
2091 insertAndEditInset(new InsetFoot(buffer_->params));
2094 case LFUN_INSET_MARGINAL:
2095 insertAndEditInset(new InsetMarginal(buffer_->params));
2098 case LFUN_INSET_MINIPAGE:
2099 insertAndEditInset(new InsetMinipage(buffer_->params));
2102 case LFUN_INSERT_NOTE:
2103 insertAndEditInset(new InsetNote(buffer_->params));
2106 case LFUN_INSET_FLOAT:
2107 // check if the float type exist
2108 if (floatList.typeExist(ev.argument)) {
2109 insertAndEditInset(new InsetFloat(buffer_->params,
2112 lyxerr << "Non-existent float type: "
2113 << ev.argument << endl;
2117 case LFUN_INSET_WIDE_FLOAT:
2118 // check if the float type exist
2119 if (floatList.typeExist(ev.argument)) {
2120 InsetFloat * new_inset =
2121 new InsetFloat(buffer_->params, ev.argument);
2122 new_inset->wide(true);
2123 insertAndEditInset(new_inset);
2125 lyxerr << "Non-existent float type: "
2126 << ev.argument << endl;
2131 case LFUN_INSET_LIST:
2132 insertAndEditInset(new InsetList);
2135 case LFUN_INSET_THEOREM:
2136 insertAndEditInset(new InsetTheorem);
2140 case LFUN_INSET_CAPTION:
2142 // Do we have a locking inset...
2143 if (bv_->theLockingInset()) {
2144 lyxerr << "Locking inset code: "
2145 << static_cast<int>(bv_->theLockingInset()->lyxCode());
2146 InsetCaption * new_inset =
2147 new InsetCaption(buffer_->params);
2148 new_inset->setOwner(bv_->theLockingInset());
2149 new_inset->setAutoBreakRows(true);
2150 new_inset->setDrawFrame(0, InsetText::LOCKED);
2151 new_inset->setFrameColor(0, LColor::captionframe);
2152 if (insertInset(new_inset))
2153 new_inset->edit(bv_);
2160 case LFUN_TABULAR_INSERT:
2162 if (ev.argument.empty()) {
2163 owner_->getDialogs().showTabularCreate();
2169 ::sscanf(ev.argument.c_str(),"%d%d", &r, &c);
2170 InsetTabular * new_inset =
2171 new InsetTabular(*buffer_, r, c);
2173 bv_->getLyXText()->real_current_font.isRightToLeft();
2174 if (!open_new_inset(new_inset, rtl))
2180 // --- accented characters ---------------------------
2183 case LFUN_CIRCUMFLEX:
2193 case LFUN_SPECIAL_CARON:
2196 case LFUN_HUNG_UMLAUT:
2199 if (ev.argument.empty()) {
2201 owner_->getLyXFunc().handleKeyFunc(ev.action);
2203 owner_->getLyXFunc().handleKeyFunc(ev.action);
2204 owner_->getIntl().getTransManager()
2205 .TranslateAndInsert(ev.argument[0], bv_->getLyXText());
2206 update(bv_->getLyXText(),
2208 | BufferView::FITCUR
2209 | BufferView::CHANGE);
2213 case LFUN_MATH_MACRO:
2214 case LFUN_MATH_DELIM:
2215 case LFUN_INSERT_MATRIX:
2216 case LFUN_INSERT_MATH:
2217 case LFUN_MATH_IMPORT_SELECTION: // Imports LaTeX from the X selection
2218 case LFUN_MATH_DISPLAY: // Open or create a displayed math inset
2219 case LFUN_MATH_MODE: // Open or create an inlined math inset
2220 case LFUN_GREEK: // Insert a single greek letter
2221 mathDispatch(FuncRequest(bv_, ev.action, ev.argument));
2224 case LFUN_CITATION_INSERT:
2226 InsetCommandParams p;
2227 p.setFromString(ev.argument);
2229 InsetCitation * inset = new InsetCitation(p);
2230 if (!insertInset(inset))
2233 updateInset(inset, true);
2237 case LFUN_INSERT_BIBTEX:
2239 // ale970405+lasgoutt970425
2240 // The argument can be up to two tokens separated
2241 // by a space. The first one is the bibstyle.
2242 string const db = token(ev.argument, ' ', 0);
2243 string bibstyle = token(ev.argument, ' ', 1);
2244 if (bibstyle.empty())
2247 InsetCommandParams p("BibTeX", db, bibstyle);
2248 InsetBibtex * inset = new InsetBibtex(p);
2250 if (insertInset(inset)) {
2251 if (ev.argument.empty())
2258 // BibTeX data bases
2259 case LFUN_BIBDB_ADD:
2261 InsetBibtex * inset =
2262 static_cast<InsetBibtex*>(getInsetByCode(Inset::BIBTEX_CODE));
2264 inset->addDatabase(ev.argument);
2269 case LFUN_BIBDB_DEL:
2271 InsetBibtex * inset =
2272 static_cast<InsetBibtex*>(getInsetByCode(Inset::BIBTEX_CODE));
2274 inset->delDatabase(ev.argument);
2279 case LFUN_BIBTEX_STYLE:
2281 InsetBibtex * inset =
2282 static_cast<InsetBibtex*>(getInsetByCode(Inset::BIBTEX_CODE));
2284 inset->setOptions(ev.argument);
2289 case LFUN_INDEX_INSERT:
2291 string entry = ev.argument;
2292 if (entry.empty()) {
2293 entry = bv_->getLyXText()->getStringToIndex(bv_);
2296 if (entry.empty()) {
2297 owner_->getDialogs().createIndex();
2301 InsetIndex * inset = new InsetIndex(InsetCommandParams("index", entry));
2303 if (!insertInset(inset)) {
2306 updateInset(inset, true);
2311 case LFUN_INDEX_PRINT:
2313 InsetCommandParams p("printindex");
2314 Inset * inset = new InsetPrintIndex(p);
2315 if (!insertInset(inset, tclass.defaultLayoutName()))
2320 case LFUN_PARENTINSERT:
2322 InsetCommandParams p("lyxparent", ev.argument);
2323 Inset * inset = new InsetParent(p, *buffer_);
2324 if (!insertInset(inset, tclass.defaultLayoutName()))
2330 case LFUN_CHILD_INSERT:
2332 InsetInclude::Params p;
2333 p.cparams.setFromString(ev.argument);
2334 p.masterFilename_ = buffer_->fileName();
2336 InsetInclude * inset = new InsetInclude(p);
2337 if (!insertInset(inset))
2340 updateInset(inset, true);
2341 bv_->owner()->getDialogs().showInclude(inset);
2346 case LFUN_FLOAT_LIST:
2347 if (floatList.typeExist(ev.argument)) {
2348 Inset * inset = new InsetFloatList(ev.argument);
2349 if (!insertInset(inset, tclass.defaultLayoutName()))
2352 lyxerr << "Non-existent float type: "
2353 << ev.argument << endl;
2357 case LFUN_THESAURUS_ENTRY:
2359 string arg = ev.argument;
2362 arg = bv_->getLyXText()->selectionAsString(buffer_,
2366 if (arg.size() > 100 || arg.empty()) {
2367 // Get word or selection
2368 bv_->getLyXText()->selectWordWhenUnderCursor(bv_, LyXText::WHOLE_WORD);
2369 arg = bv_->getLyXText()->selectionAsString(buffer_, false);
2370 // FIXME: where is getLyXText()->unselect(bv_) ?
2374 bv_->owner()->getDialogs().showThesaurus(arg);
2378 case LFUN_DATE_INSERT: // jdblair: date-insert cmd
2380 time_t now_time_t = time(NULL);
2381 struct tm * now_tm = localtime(&now_time_t);
2382 setlocale(LC_TIME, "");
2384 if (!ev.argument.empty())
2387 arg = lyxrc.date_insert_format;
2389 int const datetmp_len =
2390 ::strftime(datetmp, 32, arg.c_str(), now_tm);
2392 LyXText * lt = bv_->getLyXText();
2394 for (int i = 0; i < datetmp_len; i++) {
2395 lt->insertChar(bv_, datetmp[i]);
2398 | BufferView::FITCUR
2399 | BufferView::CHANGE);
2402 lt->selection.cursor = lt->cursor;
2403 moveCursorUpdate(false);
2407 case LFUN_UNKNOWN_ACTION:
2408 owner_->getLyXFunc().setErrorMessage(N_("Unknown function!"));
2412 FuncRequest cmd = ev;
2414 return bv_->getLyXText()->dispatch(cmd);
2421 void BufferView::Pimpl::smartQuote()
2423 LyXText const * lt = bv_->getLyXText();
2424 Paragraph const * par = lt->cursor.par();
2425 pos_type pos = lt->cursor.pos();
2429 || (par->isInset(pos - 1)
2430 && par->getInset(pos - 1)->isSpace()))
2433 c = par->getChar(pos - 1);
2437 LyXLayout_ptr const & style = par->layout();
2439 if (style->pass_thru ||
2440 par->getFontSettings(buffer_->params,
2441 pos).language()->lang() == "hebrew" ||
2442 (!insertInset(new InsetQuotes(c, buffer_->params))))
2443 bv_->owner()->dispatch(FuncRequest(LFUN_SELFINSERT, "\""));
2447 void BufferView::Pimpl::insertAndEditInset(Inset * inset)
2450 if (insertInset(inset))
2455 bool gotsel = false;
2457 if (bv_->getLyXText()->selection.set()) {
2458 bv_->getLyXText()->cutSelection(bv_, true, false);
2462 if (insertInset(inset)) {
2465 owner_->dispatch(FuncRequest(LFUN_PASTESELECTION));
2473 // Open and lock an updatable inset
2474 bool BufferView::Pimpl::open_new_inset(UpdatableInset * new_inset, bool behind)
2476 LyXText * lt = bv_->getLyXText();
2480 if (!insertInset(new_inset)) {
2484 new_inset->edit(bv_, !behind);
2489 bool BufferView::Pimpl::insertInset(Inset * inset, string const & lout)
2491 // if we are in a locking inset we should try to insert the
2492 // inset there otherwise this is a illegal function now
2493 if (bv_->theLockingInset()) {
2494 if (bv_->theLockingInset()->insetAllowed(inset))
2495 return bv_->theLockingInset()->insertInset(bv_, inset);
2499 // not quite sure if we want this...
2500 setCursorParUndo(bv_);
2503 beforeChange(bv_->text);
2504 if (!lout.empty()) {
2505 update(bv_->text, BufferView::SELECT|BufferView::FITCUR);
2506 bv_->text->breakParagraph(bv_);
2507 update(bv_->text, BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
2509 if (!bv_->text->cursor.par()->empty()) {
2510 bv_->text->cursorLeft(bv_);
2512 bv_->text->breakParagraph(bv_);
2513 update(bv_->text, BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
2517 LyXTextClass const & tclass =
2518 buffer_->params.getLyXTextClass();
2519 bool hasLayout = tclass.hasLayout(lres);
2520 string lay = tclass.defaultLayoutName();
2522 if (hasLayout != false) {
2526 // layout not fount using default
2527 lay = tclass.defaultLayoutName();
2530 bv_->text->setLayout(bv_, lay);
2532 bv_->text->setParagraph(bv_, 0, 0,
2534 VSpace(VSpace::NONE), VSpace(VSpace::NONE),
2539 update(bv_->text, BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
2542 bv_->text->insertInset(bv_, inset);
2543 update(bv_->text, BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
2550 void BufferView::Pimpl::updateInset(Inset * inset, bool mark_dirty)
2552 if (!inset || !available())
2555 // first check for locking insets
2556 if (bv_->theLockingInset()) {
2557 if (bv_->theLockingInset() == inset) {
2558 if (bv_->text->updateInset(bv_, inset)) {
2561 buffer_->markDirty();
2566 } else if (bv_->theLockingInset()->updateInsetInInset(bv_, inset)) {
2567 if (bv_->text->updateInset(bv_, bv_->theLockingInset())) {
2570 buffer_->markDirty();
2578 // then check if the inset is a top_level inset (has no owner)
2579 // if yes do the update as always otherwise we have to update the
2580 // toplevel inset where this inset is inside
2581 Inset * tl_inset = inset;
2582 while (tl_inset->owner())
2583 tl_inset = tl_inset->owner();
2585 if (tl_inset == inset) {
2586 update(bv_->text, BufferView::UPDATE);
2587 if (bv_->text->updateInset(bv_, inset)) {
2591 | BufferView::FITCUR
2592 | BufferView::CHANGE);
2594 update(bv_->text, SELECT);
2598 } else if (static_cast<UpdatableInset *>(tl_inset)
2599 ->updateInsetInInset(bv_, inset))
2601 if (bv_->text->updateInset(bv_, tl_inset)) {
2609 void BufferView::Pimpl::gotoInset(vector<Inset::Code> const & codes,
2612 if (!available()) return;
2615 beforeChange(bv_->text);
2616 update(bv_->text, BufferView::SELECT|BufferView::FITCUR);
2618 LyXCursor const & cursor = bv_->text->cursor;
2622 cursor.par()->isInset(cursor.pos())) {
2623 Inset const * inset = cursor.par()->getInset(cursor.pos());
2624 if (find(codes.begin(), codes.end(), inset->lyxCode())
2627 static_cast<InsetCommand const *>(inset)->getContents();
2631 if (!bv_->text->gotoNextInset(bv_, codes, contents)) {
2632 if (bv_->text->cursor.pos()
2633 || bv_->text->cursor.par() != bv_->text->ownerParagraph()) {
2634 LyXCursor tmp = bv_->text->cursor;
2635 bv_->text->cursor.par(bv_->text->ownerParagraph());
2636 bv_->text->cursor.pos(0);
2637 if (!bv_->text->gotoNextInset(bv_, codes, contents)) {
2638 bv_->text->cursor = tmp;
2639 bv_->owner()->message(_("No more insets"));
2642 bv_->owner()->message(_("No more insets"));
2645 update(bv_->text, BufferView::SELECT|BufferView::FITCUR);
2646 bv_->text->selection.cursor = bv_->text->cursor;
2650 void BufferView::Pimpl::gotoInset(Inset::Code code, bool same_content)
2652 gotoInset(vector<Inset::Code>(1, code), same_content);
2656 void BufferView::Pimpl::message(string const & msg)
2658 bv_->owner()->getLyXFunc().setMessage(msg);