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"
64 #include "insets/insetoptarg.h"
66 #include "insets/insettheorem.h"
67 #include "insets/insetlist.h"
69 #include "insets/insetcaption.h"
70 #include "insets/insetfloatlist.h"
72 #include "mathed/formulabase.h"
74 #include "graphics/Previews.h"
76 #include "support/LAssert.h"
77 #include "support/lstrings.h"
78 #include "support/filetools.h"
79 #include "support/lyxfunctional.h"
81 #include <boost/bind.hpp>
82 #include <boost/signals/connection.hpp>
91 #ifndef CXX_GLOBAL_CSTD
104 using std::make_pair;
108 using lyx::textclass_type;
110 /* the selection possible is needed, that only motion events are
111 * used, where the bottom press event was on the drawing area too */
112 bool selection_possible = false;
114 extern BufferList bufferlist;
115 extern char ascii_type;
117 extern int bibitemMaxWidth(BufferView *, LyXFont const &);
122 unsigned int const saved_positions_num = 20;
124 // All the below connection objects are needed because of a bug in some
125 // versions of GCC (<=2.96 are on the suspects list.) By having and assigning
126 // to these connections we avoid a segfault upon startup, and also at exit.
129 boost::signals::connection timecon;
130 boost::signals::connection doccon;
131 boost::signals::connection resizecon;
132 boost::signals::connection bpresscon;
133 boost::signals::connection breleasecon;
134 boost::signals::connection motioncon;
135 boost::signals::connection doublecon;
136 boost::signals::connection triplecon;
137 boost::signals::connection kpresscon;
138 boost::signals::connection selectioncon;
139 boost::signals::connection lostcon;
144 BufferView::Pimpl::Pimpl(BufferView * bv, LyXView * owner,
145 int xpos, int ypos, int width, int height)
146 : bv_(bv), owner_(owner), buffer_(0), cursor_timeout(400),
147 using_xterm_cursor(false)
149 workarea_.reset(WorkAreaFactory::create(xpos, ypos, width, height));
150 screen_.reset(LyXScreenFactory::create(workarea()));
153 doccon = workarea().scrollDocView
154 .connect(boost::bind(&BufferView::Pimpl::scrollDocView, this, _1));
155 resizecon = workarea().workAreaResize
156 .connect(boost::bind(&BufferView::Pimpl::workAreaResize, this));
157 bpresscon = workarea().workAreaButtonPress
158 .connect(boost::bind(&BufferView::Pimpl::workAreaButtonPress, this, _1, _2, _3));
159 breleasecon = workarea().workAreaButtonRelease
160 .connect(boost::bind(&BufferView::Pimpl::workAreaButtonRelease, this, _1, _2, _3));
161 motioncon = workarea().workAreaMotionNotify
162 .connect(boost::bind(&BufferView::Pimpl::workAreaMotionNotify, this, _1, _2, _3));
163 doublecon = workarea().workAreaDoubleClick
164 .connect(boost::bind(&BufferView::Pimpl::doubleClick, this, _1, _2, _3));
165 triplecon = workarea().workAreaTripleClick
166 .connect(boost::bind(&BufferView::Pimpl::tripleClick, this, _1, _2, _3));
167 kpresscon = workarea().workAreaKeyPress
168 .connect(boost::bind(&BufferView::Pimpl::workAreaKeyPress, this, _1, _2));
169 selectioncon = workarea().selectionRequested
170 .connect(boost::bind(&BufferView::Pimpl::selectionRequested, this));
171 lostcon = workarea().selectionLost
172 .connect(boost::bind(&BufferView::Pimpl::selectionLost, this));
174 timecon = cursor_timeout.timeout
175 .connect(boost::bind(&BufferView::Pimpl::cursorToggle, this));
176 cursor_timeout.start();
177 saved_positions.resize(saved_positions_num);
181 WorkArea & BufferView::Pimpl::workarea() const
183 return *workarea_.get();
187 LyXScreen & BufferView::Pimpl::screen() const
189 return *screen_.get();
193 Painter & BufferView::Pimpl::painter() const
195 return workarea().getPainter();
199 void BufferView::Pimpl::buffer(Buffer * b)
201 lyxerr[Debug::INFO] << "Setting buffer in BufferView ("
204 buffer_->delUser(bv_);
206 // Put the old text into the TextCache, but
207 // only if the buffer is still loaded.
208 // Also set the owner of the test to 0
209 // bv_->text->owner(0);
210 textcache.add(buffer_, workarea().workWidth(), bv_->text);
211 if (lyxerr.debugging())
212 textcache.show(lyxerr, "BufferView::buffer");
217 // set current buffer
220 if (bufferlist.getState() == BufferList::CLOSING) return;
222 // if we are closing the buffer, use the first buffer as current
224 buffer_ = bufferlist.first();
228 lyxerr[Debug::INFO] << "Buffer addr: " << buffer_ << endl;
229 buffer_->addUser(bv_);
231 // If we don't have a text object for this, we make one
232 if (bv_->text == 0) {
233 resizeCurrentBuffer();
236 // FIXME: needed when ?
238 screen().topCursorVisible(bv_->text->cursor, bv_->text->first_y);
240 // Similarly, buffer-dependent dialogs should be updated or
241 // hidden. This should go here because some dialogs (eg ToC)
242 // require bv_->text.
243 owner_->getDialogs().updateBufferDependent(true);
245 lyxerr[Debug::INFO] << " No Buffer!" << endl;
246 owner_->getDialogs().hideBufferDependent();
248 // Also remove all remaining text's from the testcache.
249 // (there should not be any!) (if there is any it is a
251 if (lyxerr.debugging())
252 textcache.show(lyxerr, "buffer delete all");
258 owner_->updateMenubar();
259 owner_->updateToolbar();
260 owner_->updateLayoutChoice();
261 owner_->updateWindowTitle();
263 if (grfx::Previews::activated() && buffer_)
264 grfx::Previews::get().generateBufferPreviews(*buffer_);
268 bool BufferView::Pimpl::fitCursor()
272 if (bv_->theLockingInset()) {
273 bv_->theLockingInset()->fitInsetCursor(bv_);
276 ret = screen().fitCursor(bv_->text, bv_);
279 bv_->owner()->getDialogs().updateParagraph();
286 void BufferView::Pimpl::redoCurrentBuffer()
288 lyxerr[Debug::INFO] << "BufferView::redoCurrentBuffer" << endl;
289 if (buffer_ && bv_->text) {
290 resizeCurrentBuffer();
292 owner_->updateLayoutChoice();
298 int BufferView::Pimpl::resizeCurrentBuffer()
300 lyxerr[Debug::INFO] << "resizeCurrentBuffer" << endl;
303 Paragraph * selstartpar = 0;
304 Paragraph * selendpar = 0;
305 UpdatableInset * the_locking_inset = 0;
308 pos_type selstartpos = 0;
309 pos_type selendpos = 0;
310 bool selection = false;
311 bool mark_set = false;
313 owner_->prohibitInput();
315 owner_->message(_("Formatting document..."));
318 par = bv_->text->cursor.par();
319 pos = bv_->text->cursor.pos();
320 selstartpar = bv_->text->selection.start.par();
321 selstartpos = bv_->text->selection.start.pos();
322 selendpar = bv_->text->selection.end.par();
323 selendpos = bv_->text->selection.end.pos();
324 selection = bv_->text->selection.set();
325 mark_set = bv_->text->selection.mark();
326 the_locking_inset = bv_->theLockingInset();
327 buffer_->resizeInsets(bv_);
328 // I don't think the delete and new are necessary here we just could
329 // call only init! (Jug 20020419)
331 bv_->text = new LyXText(bv_);
332 bv_->text->init(bv_);
334 // See if we have a text in TextCache that fits
335 // the new buffer_ with the correct width.
336 bv_->text = textcache.findFit(buffer_, workarea().workWidth());
338 if (lyxerr.debugging()) {
339 lyxerr << "Found a LyXText that fits:\n";
340 textcache.show(lyxerr, make_pair(buffer_, make_pair(workarea().workWidth(), bv_->text)));
342 // Set the owner of the newly found text
343 // bv_->text->owner(bv_);
344 if (lyxerr.debugging())
345 textcache.show(lyxerr, "resizeCurrentBuffer");
347 bv_->text = new LyXText(bv_);
348 bv_->text->init(bv_);
349 //buffer_->resizeInsets(bv_);
354 bv_->text->selection.set(true);
355 // At this point just to avoid the Delete-Empty-Paragraph-
356 // Mechanism when setting the cursor.
357 bv_->text->selection.mark(mark_set);
359 bv_->text->setCursor(bv_, selstartpar, selstartpos);
360 bv_->text->selection.cursor = bv_->text->cursor;
361 bv_->text->setCursor(bv_, selendpar, selendpos);
362 bv_->text->setSelection(bv_);
363 bv_->text->setCursor(bv_, par, pos);
365 bv_->text->setCursor(bv_, par, pos);
366 bv_->text->selection.cursor = bv_->text->cursor;
367 bv_->text->selection.set(false);
369 // remake the inset locking
370 bv_->theLockingInset(the_locking_inset);
373 bv_->text->first_y = screen().topCursorVisible(bv_->text->cursor, bv_->text->first_y);
376 owner_->allowInput();
384 void BufferView::Pimpl::repaint()
386 // Regenerate the screen.
387 screen().redraw(bv_->text, bv_);
391 void BufferView::Pimpl::updateScrollbar()
394 lyxerr[Debug::GUI] << "no text in updateScrollbar" << endl;
395 workarea().setScrollbarParams(0, 0, 0);
399 LyXText const & t = *bv_->text;
401 lyxerr[Debug::GUI] << "Updating scrollbar: h " << t.height << ", first_y "
402 << t.first_y << ", default height " << t.defaultHeight() << endl;
404 workarea().setScrollbarParams(t.height, t.first_y, t.defaultHeight());
408 void BufferView::Pimpl::scrollDocView(int value)
410 lyxerr[Debug::GUI] << "scrollDocView of " << value << endl;
412 if (!buffer_) return;
414 screen().draw(bv_->text, bv_, value);
416 if (!lyxrc.cursor_follows_scrollbar) {
420 LyXText * vbt = bv_->text;
422 int const height = vbt->defaultHeight();
423 int const first = static_cast<int>((bv_->text->first_y + height));
424 int const last = static_cast<int>((bv_->text->first_y + workarea().workHeight() - height));
426 if (vbt->cursor.y() < first)
427 vbt->setCursorFromCoordinates(bv_, 0, first);
428 else if (vbt->cursor.y() > last)
429 vbt->setCursorFromCoordinates(bv_, 0, last);
433 int BufferView::Pimpl::scroll(long time)
438 LyXText const * t = bv_->text;
440 double const diff = t->defaultHeight()
441 + double(time) * double(time) * 0.125;
443 scrollDocView(int(diff));
444 workarea().setScrollbarParams(t->height, t->first_y, t->defaultHeight());
449 void BufferView::Pimpl::workAreaKeyPress(LyXKeySymPtr key,
450 key_modifier::state state)
452 bv_->owner()->getLyXFunc().processKeySym(key, state);
456 void BufferView::Pimpl::workAreaMotionNotify(int x, int y, mouse_button::state state)
458 // Only use motion with button 1
459 if (!(state & mouse_button::button1))
465 // Check for inset locking
466 if (bv_->theLockingInset()) {
467 LyXCursor cursor = bv_->text->cursor;
468 LyXFont font = bv_->text->getFont(buffer_,
469 cursor.par(), cursor.pos());
470 int width = bv_->theLockingInset()->width(bv_, font);
471 int inset_x = font.isVisibleRightToLeft()
472 ? cursor.ix() - width : cursor.ix();
473 int start_x = inset_x + bv_->theLockingInset()->scroll();
475 FuncRequest cmd(bv_, LFUN_MOUSE_MOTION,
476 x - start_x, y - cursor.iy() + bv_->text->first_y, state);
477 bv_->theLockingInset()->localDispatch(cmd);
481 // The test for not selection possible is needed, that only motion
482 // events are used, where the bottom press event was on
483 // the drawing area too
484 if (!selection_possible)
487 screen().hideCursor();
489 Row * cursorrow = bv_->text->cursor.row();
490 bv_->text->setCursorFromCoordinates(bv_, x, y + bv_->text->first_y);
492 // sorry for this but I have a strange error that the y value jumps at
493 // a certain point. This seems like an error in my xforms library or
494 // in some other local environment, but I would like to leave this here
495 // for the moment until I can remove this (Jug 20020418)
496 if (y_before < bv_->text->cursor.y())
497 lyxerr << y_before << ":" << bv_->text->cursor.y() << endl;
499 // This is to allow jumping over large insets
500 if (cursorrow == bv_->text->cursor.row()) {
501 if (y >= int(workarea().workHeight())) {
502 bv_->text->cursorDown(bv_, false);
504 bv_->text->cursorUp(bv_, false);
508 if (!bv_->text->selection.set())
509 update(bv_->text, BufferView::UPDATE); // Maybe an empty line was deleted
511 bv_->text->setSelection(bv_);
512 screen().toggleToggle(bv_->text, bv_);
518 // Single-click on work area
519 void BufferView::Pimpl::workAreaButtonPress(int xpos, int ypos,
520 mouse_button::state button)
525 // ok ok, this is a hack (for xforms)
526 if (button == mouse_button::button4) {
527 scroll(-lyxrc.wheel_jump);
528 // We shouldn't go further down as we really should only do the
529 // scrolling and be done with this. Otherwise we may open some
530 // dialogs (Jug 20020424).
532 } else if (button == mouse_button::button5) {
533 scroll(lyxrc.wheel_jump);
534 // We shouldn't go further down as we really should only do the
535 // scrolling and be done with this. Otherwise we may open some
536 // dialogs (Jug 20020424).
540 Inset * inset_hit = checkInsetHit(bv_->text, xpos, ypos);
542 // Middle button press pastes if we have a selection
543 // We do this here as if the selection was inside an inset
544 // it could get cleared on the unlocking of the inset so
545 // we have to check this first
546 bool paste_internally = false;
547 if (button == mouse_button::button2 && bv_->getLyXText()->selection.set()) {
548 owner_->dispatch(FuncRequest(LFUN_COPY));
549 paste_internally = true;
552 int const screen_first = bv_->text->first_y;
554 if (bv_->theLockingInset()) {
555 // We are in inset locking mode
557 // Check whether the inset was hit. If not reset mode,
558 // otherwise give the event to the inset
559 if (inset_hit == bv_->theLockingInset()) {
560 FuncRequest cmd(bv_, LFUN_MOUSE_PRESS, xpos, ypos, button);
561 bv_->theLockingInset()->localDispatch(cmd);
564 bv_->unlockInset(bv_->theLockingInset());
568 selection_possible = true;
569 screen().hideCursor();
571 // Clear the selection
572 screen().toggleSelection(bv_->text, bv_);
573 bv_->text->clearSelection();
574 bv_->text->fullRebreak(bv_);
578 // Single left click in math inset?
579 if (isHighlyEditableInset(inset_hit)) {
580 // Highly editable inset, like math
581 UpdatableInset * inset = static_cast<UpdatableInset *>(inset_hit);
582 selection_possible = false;
583 owner_->updateLayoutChoice();
584 owner_->message(inset->editMessage());
585 //inset->edit(bv_, xpos, ypos, button);
586 // We just have to lock the inset before calling a PressEvent on it!
587 // we don't need the edit() call here! (Jug20020329)
588 if (!bv_->lockInset(inset)) {
589 lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
591 FuncRequest cmd(bv_, LFUN_MOUSE_PRESS, xpos, ypos, button);
592 inset->localDispatch(cmd);
595 // I'm not sure we should continue here if we hit an inset (Jug20020403)
597 // Right click on a footnote flag opens float menu
598 if (button == mouse_button::button3) {
599 selection_possible = false;
603 if (!inset_hit) // otherwise it was already set in checkInsetHit(...)
604 bv_->text->setCursorFromCoordinates(bv_, xpos, ypos + screen_first);
606 bv_->text->selection.cursor = bv_->text->cursor;
607 bv_->text->cursor.x_fix(bv_->text->cursor.x());
609 owner_->updateLayoutChoice();
611 selection_possible = false;
614 // Insert primary selection with middle mouse
615 // if there is a local selection in the current buffer,
617 if (button == mouse_button::button2) {
618 if (paste_internally)
619 owner_->dispatch(FuncRequest(LFUN_PASTE));
621 owner_->dispatch(FuncRequest(LFUN_PASTESELECTION, "paragraph"));
622 selection_possible = false;
628 void BufferView::Pimpl::doubleClick(int /*x*/, int /*y*/, mouse_button::state button)
633 LyXText * text = bv_->getLyXText();
635 if (text->bv_owner && bv_->theLockingInset())
638 if (button == mouse_button::button1) {
639 if (text->bv_owner) {
640 screen().hideCursor();
641 screen().toggleSelection(text, bv_);
642 text->selectWord(bv_, LyXText::WHOLE_WORD_STRICT);
643 screen().toggleSelection(text, bv_, false);
645 text->selectWord(bv_, LyXText::WHOLE_WORD_STRICT);
647 // This will fit the cursor on the screen if necessary
648 update(text, BufferView::SELECT|BufferView::FITCUR);
649 workarea().haveSelection(bv_->getLyXText()->selection.set());
654 void BufferView::Pimpl::tripleClick(int /*x*/, int /*y*/, mouse_button::state button)
659 LyXText * text = bv_->getLyXText();
661 if (text->bv_owner && bv_->theLockingInset())
664 if (button == mouse_button::button1) {
665 if (text->bv_owner) {
666 screen().hideCursor();
667 screen().toggleSelection(text, bv_);
669 text->cursorHome(bv_);
670 text->selection.cursor = text->cursor;
671 text->cursorEnd(bv_);
672 text->setSelection(bv_);
673 if (text->bv_owner) {
674 screen().toggleSelection(text, bv_, false);
676 // This will fit the cursor on the screen if necessary
677 update(text, BufferView::SELECT|BufferView::FITCUR);
678 workarea().haveSelection(bv_->getLyXText()->selection.set());
683 void BufferView::Pimpl::selectionRequested()
690 LyXText * text = bv_->getLyXText();
692 if (text->selection.set() &&
693 (!bv_->text->xsel_cache.set() ||
694 text->selection.start != bv_->text->xsel_cache.start ||
695 text->selection.end != bv_->text->xsel_cache.end))
697 bv_->text->xsel_cache = text->selection;
698 sel = text->selectionAsString(bv_->buffer(), false);
699 } else if (!text->selection.set()) {
701 bv_->text->xsel_cache.set(false);
704 workarea().putClipboard(sel);
709 void BufferView::Pimpl::selectionLost()
714 bv_->getLyXText()->clearSelection();
716 bv_->text->xsel_cache.set(false);
721 void BufferView::Pimpl::workAreaButtonRelease(int x, int y,
722 mouse_button::state button)
724 // do nothing if we used the mouse wheel
725 if (!buffer_ || button == mouse_button::button4 || button == mouse_button::button5)
728 // If we hit an inset, we have the inset coordinates in these
729 // and inset_hit points to the inset. If we do not hit an
730 // inset, inset_hit is 0, and inset_x == x, inset_y == y.
731 Inset * inset_hit = checkInsetHit(bv_->text, x, y);
733 if (bv_->theLockingInset()) {
734 // We are in inset locking mode.
736 // LyX does a kind of work-area grabbing for insets.
737 // Only a ButtonPress FuncRequest outside the inset will
738 // force a insetUnlock.
739 FuncRequest cmd(bv_, LFUN_MOUSE_RELEASE, x, y, button);
740 bv_->theLockingInset()->localDispatch(cmd);
744 selection_possible = false;
746 if (button == mouse_button::button2)
750 if (button == mouse_button::button1) {
751 workarea().haveSelection(bv_->getLyXText()->selection.set());
755 owner_->view_state_changed();
756 owner_->updateMenubar();
757 owner_->updateToolbar();
759 // Did we hit an editable inset?
761 selection_possible = false;
763 // if we reach this point with a selection, it
764 // must mean we are currently selecting.
765 // But we don't want to open the inset
766 // because that is annoying for the user.
767 // So just pretend we didn't hit it.
768 // this is OK because a "kosher" ButtonRelease
769 // will follow a ButtonPress that clears
771 // Note this also fixes selection drawing
772 // problems if we end up opening an inset
773 if (bv_->getLyXText()->selection.set())
776 // CHECK fix this proper in 0.13
777 // well, maybe 13.0 !!!!!!!!!
779 // Following a ref shouldn't issue
780 // a push on the undo-stack
781 // anylonger, now that we have
782 // keybindings for following
783 // references and returning from
784 // references. IMHO though, it
785 // should be the inset's own business
786 // to push or not push on the undo
787 // stack. They don't *have* to
788 // alter the document...
790 // ...or maybe the SetCursorParUndo()
791 // below isn't necessary at all anylonger?
792 if (inset_hit->lyxCode() == Inset::REF_CODE) {
793 setCursorParUndo(bv_);
796 owner_->message(inset_hit->editMessage());
798 if (isHighlyEditableInset(inset_hit)) {
799 // Highly editable inset, like math
800 UpdatableInset *inset = (UpdatableInset *)inset_hit;
801 FuncRequest cmd(bv_, LFUN_MOUSE_RELEASE, x, y, button);
802 inset->localDispatch(cmd);
804 FuncRequest cmd(bv_, LFUN_MOUSE_RELEASE, x, y, button);
805 inset_hit->localDispatch(cmd);
806 // IMO this is a grosshack! Inset's should be changed so that
807 // they call the actions they have to do with the insetButtonRel.
808 // function and not in the edit(). This should be changed
811 #warning Please remove donot call inset->edit() here (Jug 20020812)
813 inset_hit->edit(bv_, x, y, button);
818 // Maybe we want to edit a bibitem ale970302
819 if (bv_->text->cursor.par()->bibkey) {
820 bool const is_rtl = bv_->text->cursor.par()->isRightToLeftPar(buffer_->params);
821 int const width = bibitemMaxWidth(bv_, buffer_->params.getLyXTextClass().defaultfont());
822 if ((is_rtl && x > bv_->text->workWidth(bv_)-20-width) ||
823 (!is_rtl && x < 20+width)) {
824 bv_->text->cursor.par()->bibkey->edit(bv_, 0, 0, mouse_button::none);
832 Box BufferView::Pimpl::insetDimensions(LyXText const & text,
833 LyXCursor const & cursor) const
835 Paragraph /*const*/ & par = *cursor.par();
836 pos_type const pos = cursor.pos();
838 lyx::Assert(par.getInset(pos));
840 Inset const & inset(*par.getInset(pos));
842 LyXFont const & font = text.getFont(buffer_, &par, pos);
844 int const width = inset.width(bv_, font);
845 int const inset_x = font.isVisibleRightToLeft()
846 ? (cursor.ix() - width) : cursor.ix();
849 inset_x + inset.scroll(),
851 cursor.iy() - inset.ascent(bv_, font),
852 cursor.iy() + inset.descent(bv_, font));
856 Inset * BufferView::Pimpl::checkInset(LyXText const & text,
857 LyXCursor const & cursor,
858 int & x, int & y) const
860 pos_type const pos(cursor.pos());
861 Paragraph /*const*/ & par(*cursor.par());
863 if (pos >= par.size() || !par.isInset(pos)) {
867 Inset /*const*/ * inset = par.getInset(pos);
869 if (!isEditableInset(inset)) {
873 Box b(insetDimensions(text, cursor));
875 if (!b.contained(x, y)) {
876 lyxerr[Debug::GUI] << "Missed inset at x,y " << x << "," << y
877 << " box " << b << endl;
881 text.setCursor(bv_, &par, pos, true);
884 // The origin of an inset is on the baseline
885 y -= text.cursor.iy();
891 Inset * BufferView::Pimpl::checkInsetHit(LyXText * text, int & x, int & y)
893 int y_tmp = y + text->first_y;
896 text->setCursorFromCoordinates(bv_, cursor, x, y_tmp);
898 Inset * inset(checkInset(*text, cursor, x, y_tmp));
905 // look at previous position
907 if (cursor.pos() == 0) {
912 text->setCursor(bv_, cursor, cursor.par(), cursor.pos() - 1, true);
914 inset = checkInset(*text, cursor, x, y_tmp);
922 void BufferView::Pimpl::workAreaResize()
924 static int work_area_width;
925 static int work_area_height;
927 bool const widthChange = workarea().workWidth() != work_area_width;
928 bool const heightChange = workarea().workHeight() != work_area_height;
930 // update from work area
931 work_area_width = workarea().workWidth();
932 work_area_height = workarea().workHeight();
936 // The visible LyXView need a resize
937 resizeCurrentBuffer();
939 // Remove all texts from the textcache
940 // This is not _really_ what we want to do. What
941 // we really want to do is to delete in textcache
942 // that does not have a BufferView with matching
943 // width, but as long as we have only one BufferView
944 // deleting all gives the same result.
945 if (lyxerr.debugging())
946 textcache.show(lyxerr, "Expose delete all");
948 // FIXME: this is aalready done in resizeCurrentBuffer() ??
949 buffer_->resizeInsets(bv_);
950 } else if (heightChange) {
951 // fitCursor() ensures we don't jump back
952 // to the start of the document on vertical
958 if (widthChange || heightChange) {
962 // always make sure that the scrollbar is sane.
964 owner_->updateLayoutChoice();
969 void BufferView::Pimpl::update()
971 if (!bv_->theLockingInset() || !bv_->theLockingInset()->nodraw()) {
972 LyXText::text_status st = bv_->text->status();
973 screen().update(bv_->text, bv_);
975 while (bv_->text->status() == LyXText::CHANGED_IN_DRAW) {
976 bv_->text->fullRebreak(bv_);
977 st = LyXText::NEED_MORE_REFRESH;
978 bv_->text->setCursor(bv_, bv_->text->cursor.par(),
979 bv_->text->cursor.pos());
980 if (bv_->text->selection.set()) {
981 bv_->text->setCursor(bv_, bv_->text->selection.start,
982 bv_->text->selection.start.par(),
983 bv_->text->selection.start.pos());
984 bv_->text->setCursor(bv_, bv_->text->selection.end,
985 bv_->text->selection.end.par(),
986 bv_->text->selection.end.pos());
989 bv_->text->status(bv_, st);
990 screen().update(bv_->text, bv_);
992 // do this here instead of in the screen::update because of
994 bv_->text->status(bv_, LyXText::UNCHANGED);
1000 // Values used when calling update:
1002 // -2 - update, move sel_cursor if selection, fitcursor
1003 // -1 - update, move sel_cursor if selection, fitcursor, mark dirty
1004 // 0 - update, move sel_cursor if selection, fitcursor
1005 // 1 - update, move sel_cursor if selection, fitcursor, mark dirty
1006 // 3 - update, move sel_cursor if selection
1009 // a simple redraw of the parts that need refresh
1011 // move sel_cursor if selection -
1012 // the text's sel_cursor is moved if there is selection is progress
1015 // fitCursor() is called and the scrollbar updated
1018 // the buffer is marked dirty.
1027 // UPDATE_ONLY = UPDATE;
1028 // UPDATE_SELECT = UPDATE | SELECT;
1029 // UPDATE_SELECT_MOVE = UPDATE | SELECT | FITCUR;
1030 // UPDATE_SELECT_MOVE_AFTER_CHANGE = UPDATE | SELECT | FITCUR | CHANGE;
1032 // update(-3) -> update(0) -> update(0) -> update(UPDATE)
1033 // update(-2) -> update(1 + 2) -> update(3) -> update(SELECT|FITCUR)
1034 // update(-1) -> update(1 + 2 + 4) -> update(7) -> update(SELECT|FITCUR|CHANGE)
1035 // update(1) -> update(1 + 2 + 4) -> update(7) -> update(SELECT|FITCUR|CHANGE)
1036 // update(3) -> update(1) -> update(1) -> update(SELECT)
1038 void BufferView::Pimpl::update(LyXText * text, BufferView::UpdateCodes f)
1040 owner_->updateLayoutChoice();
1042 if (!text->selection.set() && (f & SELECT)) {
1043 text->selection.cursor = text->cursor;
1046 text->fullRebreak(bv_);
1048 if (text->inset_owner) {
1049 text->inset_owner->setUpdateStatus(bv_, InsetText::NONE);
1050 updateInset(text->inset_owner, false);
1060 buffer_->markDirty();
1065 // Callback for cursor timer
1066 void BufferView::Pimpl::cursorToggle()
1069 cursor_timeout.restart();
1074 extern void reapSpellchecker(void);
1077 if (!bv_->theLockingInset()) {
1078 screen().cursorToggle(bv_);
1080 bv_->theLockingInset()->toggleInsetCursor(bv_);
1083 cursor_timeout.restart();
1087 bool BufferView::Pimpl::available() const
1089 if (buffer_ && bv_->text)
1095 void BufferView::Pimpl::beforeChange(LyXText * text)
1098 text->clearSelection();
1102 void BufferView::Pimpl::finishChange(bool fitcur)
1105 moveCursorUpdate(fitcur);
1106 bv_->owner()->view_state_changed();
1110 void BufferView::Pimpl::savePosition(unsigned int i)
1112 if (i >= saved_positions_num)
1114 saved_positions[i] = Position(buffer_->fileName(),
1115 bv_->text->cursor.par()->id(),
1116 bv_->text->cursor.pos());
1119 str << _("Saved bookmark") << ' ' << i;
1120 owner_->message(str.str().c_str());
1125 void BufferView::Pimpl::restorePosition(unsigned int i)
1127 if (i >= saved_positions_num)
1130 string const fname = saved_positions[i].filename;
1132 beforeChange(bv_->text);
1134 if (fname != buffer_->fileName()) {
1135 Buffer * b = bufferlist.exists(fname) ?
1136 bufferlist.getBuffer(fname) :
1137 bufferlist.loadLyXFile(fname); // don't ask, just load it
1138 if (b != 0) buffer(b);
1141 Paragraph * par = buffer_->getParFromID(saved_positions[i].par_id);
1145 bv_->text->setCursor(bv_, par,
1146 min(par->size(), saved_positions[i].par_pos));
1148 update(bv_->text, BufferView::SELECT | BufferView::FITCUR);
1151 str << _("Moved to bookmark") << ' ' << i;
1152 owner_->message(str.str().c_str());
1157 bool BufferView::Pimpl::isSavedPosition(unsigned int i)
1159 if (i >= saved_positions_num)
1162 return !saved_positions[i].filename.empty();
1166 void BufferView::Pimpl::switchKeyMap()
1168 if (!lyxrc.rtl_support)
1171 LyXText * text = bv_->getLyXText();
1172 if (text->real_current_font.isRightToLeft()
1173 && !(bv_->theLockingInset()
1174 && bv_->theLockingInset()->lyxCode() == Inset::ERT_CODE))
1176 if (owner_->getIntl().keymap == Intl::PRIMARY)
1177 owner_->getIntl().KeyMapSec();
1179 if (owner_->getIntl().keymap == Intl::SECONDARY)
1180 owner_->getIntl().KeyMapPrim();
1185 void BufferView::Pimpl::insetUnlock()
1187 if (bv_->theLockingInset()) {
1188 bv_->theLockingInset()->insetUnlock(bv_);
1189 bv_->theLockingInset(0);
1195 void BufferView::Pimpl::showCursor()
1197 if (bv_->theLockingInset())
1198 bv_->theLockingInset()->showInsetCursor(bv_);
1200 screen().showCursor(bv_->text, bv_);
1204 void BufferView::Pimpl::hideCursor()
1206 if (!bv_->theLockingInset())
1207 screen().hideCursor();
1211 void BufferView::Pimpl::toggleSelection(bool b)
1213 if (bv_->theLockingInset())
1214 bv_->theLockingInset()->toggleSelection(bv_, b);
1215 screen().toggleSelection(bv_->text, bv_, b);
1219 void BufferView::Pimpl::toggleToggle()
1221 screen().toggleToggle(bv_->text, bv_);
1225 void BufferView::Pimpl::center()
1227 LyXText * t = bv_->text;
1230 int const half_height = workarea().workHeight() / 2;
1233 if (t->cursor.y() > half_height) {
1234 new_y = t->cursor.y() - half_height;
1237 // FIXME: can we do this w/o calling screen directly ?
1238 // This updates first_y but means the fitCursor() call
1239 // from the update(FITCUR) doesn't realise that we might
1240 // have moved (e.g. from GOTOPARAGRAPH), so doesn't cause
1241 // the scrollbar to be updated as it should, so we have
1242 // to do it manually. Any operation that does a center()
1243 // and also might have moved first_y must make sure to call
1244 // updateScrollbar() currently. Never mind that this is a
1245 // pretty obfuscated way of updating t->first_y
1246 screen().draw(t, bv_, new_y);
1248 update(t, BufferView::SELECT | BufferView::FITCUR);
1252 void BufferView::Pimpl::stuffClipboard(string const & stuff) const
1254 workarea().putClipboard(stuff);
1259 * Dispatch functions for actions which can be valid for BufferView->text
1260 * and/or InsetText->text!!!
1264 void BufferView::Pimpl::moveCursorUpdate(bool selecting, bool fitcur)
1266 LyXText * lt = bv_->getLyXText();
1268 if (selecting || lt->selection.mark()) {
1269 lt->setSelection(bv_);
1273 updateInset(lt->inset_owner, false);
1277 update(lt, BufferView::SELECT|BufferView::FITCUR);
1279 update(lt, BufferView::SELECT);
1281 } else if (bv_->text->status() != LyXText::UNCHANGED) {
1282 bv_->theLockingInset()->hideInsetCursor(bv_);
1283 update(bv_->text, BufferView::SELECT|BufferView::FITCUR);
1287 if (!lt->selection.set())
1288 workarea().haveSelection(false);
1294 Inset * BufferView::Pimpl::getInsetByCode(Inset::Code code)
1297 LyXCursor cursor = bv_->getLyXText()->cursor;
1298 Buffer::inset_iterator it =
1299 find_if(Buffer::inset_iterator(
1300 cursor.par(), cursor.pos()),
1301 buffer_->inset_iterator_end(),
1302 lyx::compare_memfun(&Inset::lyxCode, code));
1303 return it != buffer_->inset_iterator_end() ? (*it) : 0;
1305 // Ok, this is a little bit too brute force but it
1306 // should work for now. Better infrastructure is comming. (Lgb)
1308 Buffer * b = bv_->buffer();
1309 LyXCursor cursor = bv_->getLyXText()->cursor;
1311 Buffer::inset_iterator beg = b->inset_iterator_begin();
1312 Buffer::inset_iterator end = b->inset_iterator_end();
1314 bool cursor_par_seen = false;
1316 for (; beg != end; ++beg) {
1317 if (beg.getPar() == cursor.par()) {
1318 cursor_par_seen = true;
1320 if (cursor_par_seen) {
1321 if (beg.getPar() == cursor.par()
1322 && beg.getPos() >= cursor.pos()) {
1324 } else if (beg.getPar() != cursor.par()) {
1331 // Now find the first inset that matches code.
1332 for (; beg != end; ++beg) {
1333 if (beg->lyxCode() == code) {
1343 void BufferView::Pimpl::MenuInsertLyXFile(string const & filen)
1345 string filename = filen;
1347 if (filename.empty()) {
1348 // Launch a file browser
1349 string initpath = lyxrc.document_path;
1352 string const trypath = owner_->buffer()->filePath();
1353 // If directory is writeable, use this as default.
1354 if (IsDirWriteable(trypath))
1358 FileDialog fileDlg(bv_->owner(),
1359 _("Select LyX document to insert"),
1361 make_pair(string(_("Documents|#o#O")),
1362 string(lyxrc.document_path)),
1363 make_pair(string(_("Examples|#E#e")),
1364 string(AddPath(system_lyxdir, "examples"))));
1366 FileDialog::Result result =
1367 fileDlg.Select(initpath,
1368 _("*.lyx| LyX Documents (*.lyx)"));
1370 if (result.first == FileDialog::Later)
1373 filename = result.second;
1375 // check selected filename
1376 if (filename.empty()) {
1377 owner_->message(_("Canceled."));
1382 // get absolute path of file and add ".lyx" to the filename if
1384 filename = FileSearch(string(), filename, "lyx");
1386 string const disp_fn(MakeDisplayPath(filename));
1389 s1 << _("Inserting document") << ' '
1390 << disp_fn << " ...";
1391 owner_->message(s1.str().c_str());
1392 bool const res = bv_->insertLyXFile(filename);
1395 str << _("Document") << ' ' << disp_fn
1396 << ' ' << _("inserted.");
1397 owner_->message(str.str().c_str());
1400 str << _("Could not insert document") << ' '
1402 owner_->message(str.str().c_str());
1407 bool BufferView::Pimpl::dispatch(FuncRequest const & ev)
1409 lyxerr[Debug::ACTION] << "BufferView::Pimpl::Dispatch: action["
1410 << ev.action <<"] arg[" << ev.argument << "]" << endl;
1412 LyXTextClass const & tclass = buffer_->params.getLyXTextClass();
1414 switch (ev.action) {
1416 case LFUN_TOC_INSERT:
1418 InsetCommandParams p;
1419 p.setCmdName("tableofcontents");
1420 Inset * inset = new InsetTOC(p);
1421 if (!insertInset(inset, tclass.defaultLayoutName()))
1426 case LFUN_SCROLL_INSET:
1427 // this is not handled here as this function is only active
1428 // if we have a locking_inset and that one is (or contains)
1432 case LFUN_INSET_GRAPHICS:
1434 Inset * new_inset = new InsetGraphics;
1435 if (!insertInset(new_inset)) {
1438 // this is need because you don't use a inset->Edit()
1439 updateInset(new_inset, true);
1440 new_inset->edit(bv_);
1445 case LFUN_LAYOUT_COPY:
1446 bv_->copyEnvironment();
1449 case LFUN_LAYOUT_PASTE:
1450 bv_->pasteEnvironment();
1454 case LFUN_GOTOERROR:
1455 gotoInset(Inset::ERROR_CODE, false);
1459 gotoInset(Inset::NOTE_CODE, false);
1462 case LFUN_REFERENCE_GOTO:
1464 vector<Inset::Code> tmp;
1465 tmp.push_back(Inset::LABEL_CODE);
1466 tmp.push_back(Inset::REF_CODE);
1467 gotoInset(tmp, true);
1471 case LFUN_DEPTH_MIN:
1472 changeDepth(bv_, bv_->getLyXText(), -1);
1475 case LFUN_DEPTH_PLUS:
1476 changeDepth(bv_, bv_->getLyXText(), 1);
1480 owner_->getDialogs().setUserFreeFont();
1483 case LFUN_FILE_INSERT:
1484 MenuInsertLyXFile(ev.argument);
1487 case LFUN_FILE_INSERT_ASCII_PARA:
1488 InsertAsciiFile(bv_, ev.argument, true);
1491 case LFUN_FILE_INSERT_ASCII:
1492 InsertAsciiFile(bv_, ev.argument, false);
1496 lang(bv_, ev.argument);
1498 owner_->view_state_changed();
1503 owner_->view_state_changed();
1508 owner_->view_state_changed();
1513 owner_->view_state_changed();
1518 owner_->view_state_changed();
1523 owner_->view_state_changed();
1528 owner_->view_state_changed();
1533 owner_->view_state_changed();
1536 case LFUN_UNDERLINE:
1538 owner_->view_state_changed();
1541 case LFUN_FONT_SIZE:
1542 fontSize(bv_, ev.argument);
1543 owner_->view_state_changed();
1546 case LFUN_FONT_STATE:
1547 owner_->getLyXFunc().setMessage(currentState(bv_));
1550 case LFUN_INSERT_LABEL:
1551 MenuInsertLabel(bv_, ev.argument);
1554 case LFUN_REF_INSERT:
1555 if (ev.argument.empty()) {
1556 InsetCommandParams p("ref");
1557 owner_->getDialogs().createRef(p.getAsString());
1559 InsetCommandParams p;
1560 p.setFromString(ev.argument);
1562 InsetRef * inset = new InsetRef(p, *buffer_);
1563 if (!insertInset(inset))
1566 updateInset(inset, true);
1570 case LFUN_BOOKMARK_SAVE:
1571 savePosition(strToUnsignedInt(ev.argument));
1574 case LFUN_BOOKMARK_GOTO:
1575 restorePosition(strToUnsignedInt(ev.argument));
1580 string label = ev.argument;
1581 if (label.empty()) {
1583 static_cast<InsetRef*>(getInsetByCode(Inset::REF_CODE));
1585 label = inset->getContents();
1590 if (!label.empty()) {
1591 //bv_->savePosition(0);
1592 if (!bv_->gotoLabel(label))
1593 Alert::alert(_("Error"),
1594 _("Couldn't find this label"),
1595 _("in current document."));
1607 InsetCommandParams p;
1608 if (ev.action == LFUN_HTMLURL)
1609 p.setCmdName("htmlurl");
1611 p.setCmdName("url");
1612 owner_->getDialogs().createUrl(p.getAsString());
1616 case LFUN_INSERT_URL:
1618 InsetCommandParams p;
1619 p.setFromString(ev.argument);
1621 InsetUrl * inset = new InsetUrl(p);
1622 if (!insertInset(inset))
1625 updateInset(inset, true);
1629 case LFUN_INSET_ERT:
1630 insertAndEditInset(new InsetERT(buffer_->params));
1633 case LFUN_INSET_EXTERNAL:
1634 insertAndEditInset(new InsetExternal);
1637 case LFUN_INSET_FOOTNOTE:
1638 insertAndEditInset(new InsetFoot(buffer_->params));
1641 case LFUN_INSET_MARGINAL:
1642 insertAndEditInset(new InsetMarginal(buffer_->params));
1645 case LFUN_INSET_MINIPAGE:
1646 insertAndEditInset(new InsetMinipage(buffer_->params));
1649 case LFUN_INSERT_NOTE:
1650 insertAndEditInset(new InsetNote(buffer_->params));
1653 case LFUN_INSET_OPTARG:
1654 insertAndEditInset(new InsetOptArg(buffer_->params));
1657 case LFUN_INSET_FLOAT:
1658 // check if the float type exist
1659 if (floatList.typeExist(ev.argument)) {
1660 insertAndEditInset(new InsetFloat(buffer_->params,
1663 lyxerr << "Non-existent float type: "
1664 << ev.argument << endl;
1668 case LFUN_INSET_WIDE_FLOAT:
1669 // check if the float type exist
1670 if (floatList.typeExist(ev.argument)) {
1671 InsetFloat * new_inset =
1672 new InsetFloat(buffer_->params, ev.argument);
1673 new_inset->wide(true);
1674 insertAndEditInset(new_inset);
1676 lyxerr << "Non-existent float type: "
1677 << ev.argument << endl;
1682 case LFUN_INSET_LIST:
1683 insertAndEditInset(new InsetList);
1686 case LFUN_INSET_THEOREM:
1687 insertAndEditInset(new InsetTheorem);
1691 case LFUN_INSET_CAPTION:
1693 // Do we have a locking inset...
1694 if (bv_->theLockingInset()) {
1695 lyxerr << "Locking inset code: "
1696 << static_cast<int>(bv_->theLockingInset()->lyxCode());
1697 InsetCaption * new_inset =
1698 new InsetCaption(buffer_->params);
1699 new_inset->setOwner(bv_->theLockingInset());
1700 new_inset->setAutoBreakRows(true);
1701 new_inset->setDrawFrame(0, InsetText::LOCKED);
1702 new_inset->setFrameColor(0, LColor::captionframe);
1703 if (insertInset(new_inset))
1704 new_inset->edit(bv_);
1711 case LFUN_TABULAR_INSERT:
1713 if (ev.argument.empty()) {
1714 owner_->getDialogs().showTabularCreate();
1720 ::sscanf(ev.argument.c_str(),"%d%d", &r, &c);
1721 InsetTabular * new_inset =
1722 new InsetTabular(*buffer_, r, c);
1724 bv_->getLyXText()->real_current_font.isRightToLeft();
1725 if (!open_new_inset(new_inset, rtl))
1731 // --- accented characters ---------------------------
1734 case LFUN_CIRCUMFLEX:
1744 case LFUN_SPECIAL_CARON:
1747 case LFUN_HUNG_UMLAUT:
1750 if (ev.argument.empty()) {
1752 owner_->getLyXFunc().handleKeyFunc(ev.action);
1754 owner_->getLyXFunc().handleKeyFunc(ev.action);
1755 owner_->getIntl().getTransManager()
1756 .TranslateAndInsert(ev.argument[0], bv_->getLyXText());
1757 update(bv_->getLyXText(),
1759 | BufferView::FITCUR
1760 | BufferView::CHANGE);
1764 case LFUN_MATH_MACRO:
1765 case LFUN_MATH_DELIM:
1766 case LFUN_INSERT_MATRIX:
1767 case LFUN_INSERT_MATH:
1768 case LFUN_MATH_IMPORT_SELECTION: // Imports LaTeX from the X selection
1769 case LFUN_MATH_DISPLAY: // Open or create a displayed math inset
1770 case LFUN_MATH_MODE: // Open or create an inlined math inset
1771 case LFUN_GREEK: // Insert a single greek letter
1772 mathDispatch(FuncRequest(bv_, ev.action, ev.argument));
1775 case LFUN_CITATION_INSERT:
1777 InsetCommandParams p;
1778 p.setFromString(ev.argument);
1780 InsetCitation * inset = new InsetCitation(p);
1781 if (!insertInset(inset))
1784 updateInset(inset, true);
1788 case LFUN_INSERT_BIBTEX:
1790 // ale970405+lasgoutt970425
1791 // The argument can be up to two tokens separated
1792 // by a space. The first one is the bibstyle.
1793 string const db = token(ev.argument, ' ', 0);
1794 string bibstyle = token(ev.argument, ' ', 1);
1795 if (bibstyle.empty())
1798 InsetCommandParams p("BibTeX", db, bibstyle);
1799 InsetBibtex * inset = new InsetBibtex(p);
1801 if (insertInset(inset)) {
1802 if (ev.argument.empty())
1809 // BibTeX data bases
1810 case LFUN_BIBDB_ADD:
1812 InsetBibtex * inset =
1813 static_cast<InsetBibtex*>(getInsetByCode(Inset::BIBTEX_CODE));
1815 inset->addDatabase(ev.argument);
1820 case LFUN_BIBDB_DEL:
1822 InsetBibtex * inset =
1823 static_cast<InsetBibtex*>(getInsetByCode(Inset::BIBTEX_CODE));
1825 inset->delDatabase(ev.argument);
1830 case LFUN_BIBTEX_STYLE:
1832 InsetBibtex * inset =
1833 static_cast<InsetBibtex*>(getInsetByCode(Inset::BIBTEX_CODE));
1835 inset->setOptions(ev.argument);
1840 case LFUN_INDEX_INSERT:
1842 string entry = ev.argument;
1844 entry = bv_->getLyXText()->getStringToIndex(bv_);
1846 if (entry.empty()) {
1847 owner_->getDialogs().createIndex();
1851 InsetIndex * inset = new InsetIndex(InsetCommandParams("index", entry));
1853 if (!insertInset(inset)) {
1856 updateInset(inset, true);
1861 case LFUN_INDEX_PRINT:
1863 InsetCommandParams p("printindex");
1864 Inset * inset = new InsetPrintIndex(p);
1865 if (!insertInset(inset, tclass.defaultLayoutName()))
1870 case LFUN_PARENTINSERT:
1872 InsetCommandParams p("lyxparent", ev.argument);
1873 Inset * inset = new InsetParent(p, *buffer_);
1874 if (!insertInset(inset, tclass.defaultLayoutName()))
1880 case LFUN_CHILD_INSERT:
1882 InsetInclude::Params p;
1883 p.cparams.setFromString(ev.argument);
1884 p.masterFilename_ = buffer_->fileName();
1886 InsetInclude * inset = new InsetInclude(p);
1887 if (!insertInset(inset))
1890 updateInset(inset, true);
1891 bv_->owner()->getDialogs().showInclude(inset);
1896 case LFUN_FLOAT_LIST:
1897 if (floatList.typeExist(ev.argument)) {
1898 Inset * inset = new InsetFloatList(ev.argument);
1899 if (!insertInset(inset, tclass.defaultLayoutName()))
1902 lyxerr << "Non-existent float type: "
1903 << ev.argument << endl;
1907 case LFUN_THESAURUS_ENTRY:
1909 string arg = ev.argument;
1912 arg = bv_->getLyXText()->selectionAsString(buffer_,
1916 if (arg.size() > 100 || arg.empty()) {
1917 // Get word or selection
1918 bv_->getLyXText()->selectWordWhenUnderCursor(bv_, LyXText::WHOLE_WORD);
1919 arg = bv_->getLyXText()->selectionAsString(buffer_, false);
1920 // FIXME: where is getLyXText()->unselect(bv_) ?
1924 bv_->owner()->getDialogs().showThesaurus(arg);
1928 case LFUN_DATE_INSERT: // jdblair: date-insert cmd
1930 time_t now_time_t = time(NULL);
1931 struct tm * now_tm = localtime(&now_time_t);
1932 setlocale(LC_TIME, "");
1934 if (!ev.argument.empty())
1937 arg = lyxrc.date_insert_format;
1939 int const datetmp_len =
1940 ::strftime(datetmp, 32, arg.c_str(), now_tm);
1942 LyXText * lt = bv_->getLyXText();
1944 for (int i = 0; i < datetmp_len; i++) {
1945 lt->insertChar(bv_, datetmp[i]);
1948 | BufferView::FITCUR
1949 | BufferView::CHANGE);
1952 lt->selection.cursor = lt->cursor;
1953 moveCursorUpdate(false);
1957 case LFUN_UNKNOWN_ACTION:
1958 ev.errorMessage(N_("Unknown function!"));
1962 FuncRequest cmd = ev;
1964 return bv_->getLyXText()->dispatch(cmd);
1971 void BufferView::Pimpl::smartQuote()
1973 LyXText const * lt = bv_->getLyXText();
1974 Paragraph const * par = lt->cursor.par();
1975 pos_type pos = lt->cursor.pos();
1979 || (par->isInset(pos - 1)
1980 && par->getInset(pos - 1)->isSpace()))
1983 c = par->getChar(pos - 1);
1987 LyXLayout_ptr const & style = par->layout();
1989 if (style->pass_thru ||
1990 par->getFontSettings(buffer_->params,
1991 pos).language()->lang() == "hebrew" ||
1992 (!insertInset(new InsetQuotes(c, buffer_->params))))
1993 bv_->owner()->dispatch(FuncRequest(LFUN_SELFINSERT, "\""));
1997 void BufferView::Pimpl::insertAndEditInset(Inset * inset)
2000 if (insertInset(inset))
2005 bool gotsel = false;
2007 if (bv_->getLyXText()->selection.set()) {
2008 bv_->getLyXText()->cutSelection(bv_, true, false);
2012 if (insertInset(inset)) {
2015 owner_->dispatch(FuncRequest(LFUN_PASTESELECTION));
2023 // Open and lock an updatable inset
2024 bool BufferView::Pimpl::open_new_inset(UpdatableInset * new_inset, bool behind)
2026 LyXText * lt = bv_->getLyXText();
2030 if (!insertInset(new_inset)) {
2034 new_inset->edit(bv_, !behind);
2039 bool BufferView::Pimpl::insertInset(Inset * inset, string const & lout)
2041 // if we are in a locking inset we should try to insert the
2042 // inset there otherwise this is a illegal function now
2043 if (bv_->theLockingInset()) {
2044 if (bv_->theLockingInset()->insetAllowed(inset))
2045 return bv_->theLockingInset()->insertInset(bv_, inset);
2049 // not quite sure if we want this...
2050 setCursorParUndo(bv_);
2053 beforeChange(bv_->text);
2054 if (!lout.empty()) {
2055 update(bv_->text, BufferView::SELECT|BufferView::FITCUR);
2056 bv_->text->breakParagraph(bv_);
2057 update(bv_->text, BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
2059 if (!bv_->text->cursor.par()->empty()) {
2060 bv_->text->cursorLeft(bv_);
2062 bv_->text->breakParagraph(bv_);
2063 update(bv_->text, BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
2067 LyXTextClass const & tclass =
2068 buffer_->params.getLyXTextClass();
2069 bool hasLayout = tclass.hasLayout(lres);
2070 string lay = tclass.defaultLayoutName();
2072 if (hasLayout != false) {
2076 // layout not fount using default
2077 lay = tclass.defaultLayoutName();
2080 bv_->text->setLayout(bv_, lay);
2082 bv_->text->setParagraph(bv_, 0, 0,
2084 VSpace(VSpace::NONE), VSpace(VSpace::NONE),
2089 update(bv_->text, BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
2092 bv_->text->insertInset(bv_, inset);
2093 update(bv_->text, BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
2100 void BufferView::Pimpl::updateInset(Inset * inset, bool mark_dirty)
2102 if (!inset || !available())
2105 // first check for locking insets
2106 if (bv_->theLockingInset()) {
2107 if (bv_->theLockingInset() == inset) {
2108 if (bv_->text->updateInset(bv_, inset)) {
2111 buffer_->markDirty();
2116 } else if (bv_->theLockingInset()->updateInsetInInset(bv_, inset)) {
2117 if (bv_->text->updateInset(bv_, bv_->theLockingInset())) {
2120 buffer_->markDirty();
2128 // then check if the inset is a top_level inset (has no owner)
2129 // if yes do the update as always otherwise we have to update the
2130 // toplevel inset where this inset is inside
2131 Inset * tl_inset = inset;
2132 while (tl_inset->owner())
2133 tl_inset = tl_inset->owner();
2135 if (tl_inset == inset) {
2136 update(bv_->text, BufferView::UPDATE);
2137 if (bv_->text->updateInset(bv_, inset)) {
2141 | BufferView::FITCUR
2142 | BufferView::CHANGE);
2144 update(bv_->text, SELECT);
2148 } else if (static_cast<UpdatableInset *>(tl_inset)
2149 ->updateInsetInInset(bv_, inset))
2151 if (bv_->text->updateInset(bv_, tl_inset)) {
2159 void BufferView::Pimpl::gotoInset(vector<Inset::Code> const & codes,
2162 if (!available()) return;
2165 beforeChange(bv_->text);
2166 update(bv_->text, BufferView::SELECT|BufferView::FITCUR);
2168 LyXCursor const & cursor = bv_->text->cursor;
2172 cursor.par()->isInset(cursor.pos())) {
2173 Inset const * inset = cursor.par()->getInset(cursor.pos());
2174 if (find(codes.begin(), codes.end(), inset->lyxCode())
2177 static_cast<InsetCommand const *>(inset)->getContents();
2181 if (!bv_->text->gotoNextInset(bv_, codes, contents)) {
2182 if (bv_->text->cursor.pos()
2183 || bv_->text->cursor.par() != bv_->text->ownerParagraph()) {
2184 LyXCursor tmp = bv_->text->cursor;
2185 bv_->text->cursor.par(bv_->text->ownerParagraph());
2186 bv_->text->cursor.pos(0);
2187 if (!bv_->text->gotoNextInset(bv_, codes, contents)) {
2188 bv_->text->cursor = tmp;
2189 bv_->owner()->message(_("No more insets"));
2192 bv_->owner()->message(_("No more insets"));
2195 update(bv_->text, BufferView::SELECT|BufferView::FITCUR);
2196 bv_->text->selection.cursor = bv_->text->cursor;
2200 void BufferView::Pimpl::gotoInset(Inset::Code code, bool same_content)
2202 gotoInset(vector<Inset::Code>(1, code), same_content);