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"
23 #include "frontends/mouse_state.h"
26 #include "paragraph.h"
27 #include "frontends/LyXView.h"
28 #include "commandtags.h"
31 #include "bufferview_funcs.h"
32 #include "TextCache.h"
33 #include "bufferlist.h"
36 // added for Dispatch functions
39 #include "FloatList.h"
41 #include "ParagraphParameters.h"
42 #include "undo_funcs.h"
43 #include "funcrequest.h"
46 #include "insets/insetbib.h"
47 #include "insets/insettext.h"
48 #include "insets/inseturl.h"
49 #include "insets/insetlatexaccent.h"
50 #include "insets/insettoc.h"
51 #include "insets/insetref.h"
52 #include "insets/insetparent.h"
53 #include "insets/insetindex.h"
54 #include "insets/insetinclude.h"
55 #include "insets/insetcite.h"
56 #include "insets/insetgraphics.h"
57 #include "insets/insetmarginal.h"
58 #include "insets/insetcaption.h"
59 #include "insets/insetfloatlist.h"
61 #include "mathed/formulabase.h"
63 #include "graphics/Previews.h"
65 #include "support/LAssert.h"
66 #include "support/lstrings.h"
67 #include "support/filetools.h"
69 #include <boost/bind.hpp>
70 #include <boost/signals/connection.hpp>
86 extern BufferList bufferlist;
91 unsigned int const saved_positions_num = 20;
93 // All the below connection objects are needed because of a bug in some
94 // versions of GCC (<=2.96 are on the suspects list.) By having and assigning
95 // to these connections we avoid a segfault upon startup, and also at exit.
98 boost::signals::connection dispatchcon;
99 boost::signals::connection timecon;
100 boost::signals::connection doccon;
101 boost::signals::connection resizecon;
102 boost::signals::connection kpresscon;
103 boost::signals::connection selectioncon;
104 boost::signals::connection lostcon;
108 * Return the on-screen dimensions of the inset at the cursor.
109 * Pre-condition: the cursor must be at an inset.
111 Box insetDimensions(BufferView * bv, LyXText const & text,
112 LyXCursor const & cursor)
114 Paragraph /*const*/ & par = *cursor.par();
115 pos_type const pos = cursor.pos();
117 lyx::Assert(par.getInset(pos));
119 Inset const & inset(*par.getInset(pos));
121 LyXFont const & font = text.getFont(bv->buffer(), &par, pos);
123 int const width = inset.width(bv, font);
124 int const inset_x = font.isVisibleRightToLeft()
125 ? (cursor.ix() - width) : cursor.ix();
128 inset_x + inset.scroll(),
130 cursor.iy() - inset.ascent(bv, font),
131 cursor.iy() + inset.descent(bv, font));
136 * check if the given co-ordinates are inside an inset at the
137 * given cursor, if one exists. If so, the inset is returned,
138 * and the co-ordinates are made relative. Otherwise, 0 is returned.
140 Inset * checkInset(BufferView * bv, LyXText const & text,
141 LyXCursor const & cursor, int & x, int & y)
143 pos_type const pos = cursor.pos();
144 Paragraph /*const*/ & par(*cursor.par());
146 if (pos >= par.size() || !par.isInset(pos)) {
150 Inset /*const*/ * inset = par.getInset(pos);
152 if (!isEditableInset(inset))
155 Box b = insetDimensions(bv, text, cursor);
157 if (!b.contained(x, y)) {
158 lyxerr[Debug::GUI] << "Missed inset at x,y " << x << "," << y
159 << " box " << b << endl;
163 text.setCursor(bv, &par, pos, true);
166 // The origin of an inset is on the baseline
167 y -= text.cursor.iy();
175 BufferView::Pimpl::Pimpl(BufferView * bv, LyXView * owner,
176 int xpos, int ypos, int width, int height)
177 : bv_(bv), owner_(owner), buffer_(0), cursor_timeout(400),
178 using_xterm_cursor(false)
180 workarea_.reset(WorkAreaFactory::create(xpos, ypos, width, height));
181 screen_.reset(LyXScreenFactory::create(workarea()));
184 doccon = workarea().scrollDocView
185 .connect(boost::bind(&BufferView::Pimpl::scrollDocView, this, _1));
186 resizecon = workarea().workAreaResize
187 .connect(boost::bind(&BufferView::Pimpl::workAreaResize, this));
188 dispatchcon = workarea().dispatch
189 .connect(boost::bind(&BufferView::Pimpl::dispatch, this, _1));
190 kpresscon = workarea().workAreaKeyPress
191 .connect(boost::bind(&BufferView::Pimpl::workAreaKeyPress, this, _1, _2));
192 selectioncon = workarea().selectionRequested
193 .connect(boost::bind(&BufferView::Pimpl::selectionRequested, this));
194 lostcon = workarea().selectionLost
195 .connect(boost::bind(&BufferView::Pimpl::selectionLost, this));
197 timecon = cursor_timeout.timeout
198 .connect(boost::bind(&BufferView::Pimpl::cursorToggle, this));
199 cursor_timeout.start();
200 saved_positions.resize(saved_positions_num);
204 WorkArea & BufferView::Pimpl::workarea() const
206 return *workarea_.get();
210 LyXScreen & BufferView::Pimpl::screen() const
212 return *screen_.get();
216 Painter & BufferView::Pimpl::painter() const
218 return workarea().getPainter();
222 void BufferView::Pimpl::buffer(Buffer * b)
224 lyxerr[Debug::INFO] << "Setting buffer in BufferView ("
227 buffer_->delUser(bv_);
229 // Put the old text into the TextCache, but
230 // only if the buffer is still loaded.
231 // Also set the owner of the test to 0
232 // bv_->text->owner(0);
233 textcache.add(buffer_, workarea().workWidth(), bv_->text);
234 if (lyxerr.debugging())
235 textcache.show(lyxerr, "BufferView::buffer");
240 // set current buffer
243 if (bufferlist.getState() == BufferList::CLOSING) return;
245 // if we are closing the buffer, use the first buffer as current
247 buffer_ = bufferlist.first();
251 lyxerr[Debug::INFO] << "Buffer addr: " << buffer_ << endl;
252 buffer_->addUser(bv_);
254 // If we don't have a text object for this, we make one
255 if (bv_->text == 0) {
256 resizeCurrentBuffer();
259 // FIXME: needed when ?
261 screen().topCursorVisible(bv_->text->cursor, bv_->text->first_y);
263 // Similarly, buffer-dependent dialogs should be updated or
264 // hidden. This should go here because some dialogs (eg ToC)
265 // require bv_->text.
266 owner_->getDialogs().updateBufferDependent(true);
268 lyxerr[Debug::INFO] << " No Buffer!" << endl;
269 owner_->getDialogs().hideBufferDependent();
271 // Also remove all remaining text's from the testcache.
272 // (there should not be any!) (if there is any it is a
274 if (lyxerr.debugging())
275 textcache.show(lyxerr, "buffer delete all");
281 owner_->updateMenubar();
282 owner_->updateToolbar();
283 owner_->updateLayoutChoice();
284 owner_->updateWindowTitle();
286 if (grfx::Previews::activated() && buffer_)
287 grfx::Previews::get().generateBufferPreviews(*buffer_);
291 bool BufferView::Pimpl::fitCursor()
295 if (bv_->theLockingInset()) {
296 bv_->theLockingInset()->fitInsetCursor(bv_);
299 ret = screen().fitCursor(bv_->text, bv_);
302 bv_->owner()->getDialogs().updateParagraph();
309 void BufferView::Pimpl::redoCurrentBuffer()
311 lyxerr[Debug::INFO] << "BufferView::redoCurrentBuffer" << endl;
312 if (buffer_ && bv_->text) {
313 resizeCurrentBuffer();
315 owner_->updateLayoutChoice();
321 int BufferView::Pimpl::resizeCurrentBuffer()
323 lyxerr[Debug::INFO] << "resizeCurrentBuffer" << endl;
326 Paragraph * selstartpar = 0;
327 Paragraph * selendpar = 0;
328 UpdatableInset * the_locking_inset = 0;
331 pos_type selstartpos = 0;
332 pos_type selendpos = 0;
333 bool selection = false;
334 bool mark_set = false;
336 owner_->prohibitInput();
338 owner_->message(_("Formatting document..."));
341 par = bv_->text->cursor.par();
342 pos = bv_->text->cursor.pos();
343 selstartpar = bv_->text->selection.start.par();
344 selstartpos = bv_->text->selection.start.pos();
345 selendpar = bv_->text->selection.end.par();
346 selendpos = bv_->text->selection.end.pos();
347 selection = bv_->text->selection.set();
348 mark_set = bv_->text->selection.mark();
349 the_locking_inset = bv_->theLockingInset();
350 buffer_->resizeInsets(bv_);
351 // I don't think the delete and new are necessary here we just could
352 // call only init! (Jug 20020419)
354 bv_->text = new LyXText(bv_);
355 bv_->text->init(bv_);
357 // See if we have a text in TextCache that fits
358 // the new buffer_ with the correct width.
359 bv_->text = textcache.findFit(buffer_, workarea().workWidth());
361 if (lyxerr.debugging()) {
362 lyxerr << "Found a LyXText that fits:\n";
363 textcache.show(lyxerr, make_pair(buffer_, make_pair(workarea().workWidth(), bv_->text)));
365 // Set the owner of the newly found text
366 // bv_->text->owner(bv_);
367 if (lyxerr.debugging())
368 textcache.show(lyxerr, "resizeCurrentBuffer");
370 bv_->text = new LyXText(bv_);
371 bv_->text->init(bv_);
372 //buffer_->resizeInsets(bv_);
377 bv_->text->selection.set(true);
378 // At this point just to avoid the Delete-Empty-Paragraph-
379 // Mechanism when setting the cursor.
380 bv_->text->selection.mark(mark_set);
382 bv_->text->setCursor(bv_, selstartpar, selstartpos);
383 bv_->text->selection.cursor = bv_->text->cursor;
384 bv_->text->setCursor(bv_, selendpar, selendpos);
385 bv_->text->setSelection(bv_);
386 bv_->text->setCursor(bv_, par, pos);
388 bv_->text->setCursor(bv_, par, pos);
389 bv_->text->selection.cursor = bv_->text->cursor;
390 bv_->text->selection.set(false);
392 // remake the inset locking
393 bv_->theLockingInset(the_locking_inset);
396 bv_->text->first_y = screen().topCursorVisible(bv_->text->cursor, bv_->text->first_y);
399 owner_->allowInput();
407 void BufferView::Pimpl::repaint()
409 // Regenerate the screen.
410 screen().redraw(bv_->text, bv_);
414 void BufferView::Pimpl::updateScrollbar()
417 lyxerr[Debug::GUI] << "no text in updateScrollbar" << endl;
418 workarea().setScrollbarParams(0, 0, 0);
422 LyXText const & t = *bv_->text;
424 lyxerr[Debug::GUI] << "Updating scrollbar: h " << t.height << ", first_y "
425 << t.first_y << ", default height " << t.defaultHeight() << endl;
427 workarea().setScrollbarParams(t.height, t.first_y, t.defaultHeight());
431 void BufferView::Pimpl::scrollDocView(int value)
433 lyxerr[Debug::GUI] << "scrollDocView of " << value << endl;
435 if (!buffer_) return;
437 screen().draw(bv_->text, bv_, value);
439 if (!lyxrc.cursor_follows_scrollbar) {
443 LyXText * vbt = bv_->text;
445 int const height = vbt->defaultHeight();
446 int const first = static_cast<int>((bv_->text->first_y + height));
447 int const last = static_cast<int>((bv_->text->first_y + workarea().workHeight() - height));
449 if (vbt->cursor.y() < first)
450 vbt->setCursorFromCoordinates(bv_, 0, first);
451 else if (vbt->cursor.y() > last)
452 vbt->setCursorFromCoordinates(bv_, 0, last);
456 int BufferView::Pimpl::scroll(long time)
461 LyXText const * t = bv_->text;
463 double const diff = t->defaultHeight()
464 + double(time) * double(time) * 0.125;
466 scrollDocView(int(diff));
467 workarea().setScrollbarParams(t->height, t->first_y, t->defaultHeight());
472 void BufferView::Pimpl::workAreaKeyPress(LyXKeySymPtr key,
473 key_modifier::state state)
475 bv_->owner()->getLyXFunc().processKeySym(key, state);
479 void BufferView::Pimpl::selectionRequested()
486 LyXText * text = bv_->getLyXText();
488 if (text->selection.set() &&
489 (!bv_->text->xsel_cache.set() ||
490 text->selection.start != bv_->text->xsel_cache.start ||
491 text->selection.end != bv_->text->xsel_cache.end))
493 bv_->text->xsel_cache = text->selection;
494 sel = text->selectionAsString(bv_->buffer(), false);
495 } else if (!text->selection.set()) {
497 bv_->text->xsel_cache.set(false);
500 workarea().putClipboard(sel);
505 void BufferView::Pimpl::selectionLost()
510 bv_->getLyXText()->clearSelection();
512 bv_->text->xsel_cache.set(false);
518 Inset * BufferView::Pimpl::checkInsetHit(LyXText * text, int & x, int & y)
520 int y_tmp = y + text->first_y;
523 text->setCursorFromCoordinates(bv_, cursor, x, y_tmp);
525 Inset * inset = checkInset(bv_, *text, cursor, x, y_tmp);
532 // look at previous position
533 if (cursor.pos() == 0) {
538 text->setCursor(bv_, cursor, cursor.par(), cursor.pos() - 1, true);
540 inset = checkInset(bv_, *text, cursor, x, y_tmp);
548 void BufferView::Pimpl::workAreaResize()
550 static int work_area_width;
551 static int work_area_height;
553 bool const widthChange = workarea().workWidth() != work_area_width;
554 bool const heightChange = workarea().workHeight() != work_area_height;
556 // update from work area
557 work_area_width = workarea().workWidth();
558 work_area_height = workarea().workHeight();
562 // The visible LyXView need a resize
563 resizeCurrentBuffer();
565 // Remove all texts from the textcache
566 // This is not _really_ what we want to do. What
567 // we really want to do is to delete in textcache
568 // that does not have a BufferView with matching
569 // width, but as long as we have only one BufferView
570 // deleting all gives the same result.
571 if (lyxerr.debugging())
572 textcache.show(lyxerr, "Expose delete all");
574 // FIXME: this is already done in resizeCurrentBuffer() ??
575 buffer_->resizeInsets(bv_);
576 } else if (heightChange) {
577 // fitCursor() ensures we don't jump back
578 // to the start of the document on vertical
584 if (widthChange || heightChange) {
588 // always make sure that the scrollbar is sane.
590 owner_->updateLayoutChoice();
595 void BufferView::Pimpl::update()
597 if (!bv_->theLockingInset() || !bv_->theLockingInset()->nodraw()) {
598 LyXText::text_status st = bv_->text->status();
599 screen().update(bv_->text, bv_);
601 while (bv_->text->status() == LyXText::CHANGED_IN_DRAW) {
602 bv_->text->fullRebreak(bv_);
603 st = LyXText::NEED_MORE_REFRESH;
604 bv_->text->setCursor(bv_, bv_->text->cursor.par(),
605 bv_->text->cursor.pos());
606 if (bv_->text->selection.set()) {
607 bv_->text->setCursor(bv_, bv_->text->selection.start,
608 bv_->text->selection.start.par(),
609 bv_->text->selection.start.pos());
610 bv_->text->setCursor(bv_, bv_->text->selection.end,
611 bv_->text->selection.end.par(),
612 bv_->text->selection.end.pos());
615 bv_->text->status(bv_, st);
616 screen().update(bv_->text, bv_);
618 // do this here instead of in the screen::update because of
620 bv_->text->status(bv_, LyXText::UNCHANGED);
626 // Values used when calling update:
628 // -2 - update, move sel_cursor if selection, fitcursor
629 // -1 - update, move sel_cursor if selection, fitcursor, mark dirty
630 // 0 - update, move sel_cursor if selection, fitcursor
631 // 1 - update, move sel_cursor if selection, fitcursor, mark dirty
632 // 3 - update, move sel_cursor if selection
635 // a simple redraw of the parts that need refresh
637 // move sel_cursor if selection -
638 // the text's sel_cursor is moved if there is selection is progress
641 // fitCursor() is called and the scrollbar updated
644 // the buffer is marked dirty.
653 // UPDATE_ONLY = UPDATE;
654 // UPDATE_SELECT = UPDATE | SELECT;
655 // UPDATE_SELECT_MOVE = UPDATE | SELECT | FITCUR;
656 // UPDATE_SELECT_MOVE_AFTER_CHANGE = UPDATE | SELECT | FITCUR | CHANGE;
658 // update(-3) -> update(0) -> update(0) -> update(UPDATE)
659 // update(-2) -> update(1 + 2) -> update(3) -> update(SELECT|FITCUR)
660 // update(-1) -> update(1 + 2 + 4) -> update(7) -> update(SELECT|FITCUR|CHANGE)
661 // update(1) -> update(1 + 2 + 4) -> update(7) -> update(SELECT|FITCUR|CHANGE)
662 // update(3) -> update(1) -> update(1) -> update(SELECT)
664 void BufferView::Pimpl::update(LyXText * text, BufferView::UpdateCodes f)
666 owner_->updateLayoutChoice();
668 if (!text->selection.set() && (f & SELECT)) {
669 text->selection.cursor = text->cursor;
672 text->fullRebreak(bv_);
674 if (text->inset_owner) {
675 text->inset_owner->setUpdateStatus(bv_, InsetText::NONE);
676 updateInset(text->inset_owner, false);
686 buffer_->markDirty();
691 // Callback for cursor timer
692 void BufferView::Pimpl::cursorToggle()
695 cursor_timeout.restart();
700 extern void reapSpellchecker(void);
703 if (!bv_->theLockingInset()) {
704 screen().cursorToggle(bv_);
706 bv_->theLockingInset()->toggleInsetCursor(bv_);
709 cursor_timeout.restart();
713 bool BufferView::Pimpl::available() const
715 if (buffer_ && bv_->text)
721 void BufferView::Pimpl::beforeChange(LyXText * text)
724 text->clearSelection();
728 void BufferView::Pimpl::savePosition(unsigned int i)
730 if (i >= saved_positions_num)
732 saved_positions[i] = Position(buffer_->fileName(),
733 bv_->text->cursor.par()->id(),
734 bv_->text->cursor.pos());
737 str << _("Saved bookmark") << ' ' << i;
738 owner_->message(str.str().c_str());
743 void BufferView::Pimpl::restorePosition(unsigned int i)
745 if (i >= saved_positions_num)
748 string const fname = saved_positions[i].filename;
750 beforeChange(bv_->text);
752 if (fname != buffer_->fileName()) {
753 Buffer * b = bufferlist.exists(fname) ?
754 bufferlist.getBuffer(fname) :
755 bufferlist.loadLyXFile(fname); // don't ask, just load it
756 if (b != 0) buffer(b);
759 Paragraph * par = buffer_->getParFromID(saved_positions[i].par_id);
763 bv_->text->setCursor(bv_, par,
764 min(par->size(), saved_positions[i].par_pos));
766 update(bv_->text, BufferView::SELECT | BufferView::FITCUR);
769 str << _("Moved to bookmark") << ' ' << i;
770 owner_->message(str.str().c_str());
775 bool BufferView::Pimpl::isSavedPosition(unsigned int i)
777 if (i >= saved_positions_num)
780 return !saved_positions[i].filename.empty();
784 void BufferView::Pimpl::switchKeyMap()
786 if (!lyxrc.rtl_support)
789 LyXText * text = bv_->getLyXText();
790 if (text->real_current_font.isRightToLeft()
791 && !(bv_->theLockingInset()
792 && bv_->theLockingInset()->lyxCode() == Inset::ERT_CODE))
794 if (owner_->getIntl().keymap == Intl::PRIMARY)
795 owner_->getIntl().KeyMapSec();
797 if (owner_->getIntl().keymap == Intl::SECONDARY)
798 owner_->getIntl().KeyMapPrim();
803 void BufferView::Pimpl::insetUnlock()
805 if (bv_->theLockingInset()) {
806 bv_->theLockingInset()->insetUnlock(bv_);
807 bv_->theLockingInset(0);
813 void BufferView::Pimpl::showCursor()
815 if (bv_->theLockingInset())
816 bv_->theLockingInset()->showInsetCursor(bv_);
818 screen().showCursor(bv_->text, bv_);
822 void BufferView::Pimpl::hideCursor()
824 if (!bv_->theLockingInset())
825 screen().hideCursor();
829 void BufferView::Pimpl::toggleSelection(bool b)
831 if (bv_->theLockingInset())
832 bv_->theLockingInset()->toggleSelection(bv_, b);
833 screen().toggleSelection(bv_->text, bv_, b);
837 void BufferView::Pimpl::toggleToggle()
839 screen().toggleToggle(bv_->text, bv_);
843 void BufferView::Pimpl::center()
845 LyXText * t = bv_->text;
848 int const half_height = workarea().workHeight() / 2;
851 if (t->cursor.y() > half_height) {
852 new_y = t->cursor.y() - half_height;
855 // FIXME: can we do this w/o calling screen directly ?
856 // This updates first_y but means the fitCursor() call
857 // from the update(FITCUR) doesn't realise that we might
858 // have moved (e.g. from GOTOPARAGRAPH), so doesn't cause
859 // the scrollbar to be updated as it should, so we have
860 // to do it manually. Any operation that does a center()
861 // and also might have moved first_y must make sure to call
862 // updateScrollbar() currently. Never mind that this is a
863 // pretty obfuscated way of updating t->first_y
864 screen().draw(t, bv_, new_y);
866 update(t, BufferView::SELECT | BufferView::FITCUR);
870 void BufferView::Pimpl::stuffClipboard(string const & stuff) const
872 workarea().putClipboard(stuff);
877 * Dispatch functions for actions which can be valid for BufferView->text
878 * and/or InsetText->text!!!
882 Inset * BufferView::Pimpl::getInsetByCode(Inset::Code code)
885 LyXCursor cursor = bv_->getLyXText()->cursor;
886 Buffer::inset_iterator it =
887 find_if(Buffer::inset_iterator(
888 cursor.par(), cursor.pos()),
889 buffer_->inset_iterator_end(),
890 lyx::compare_memfun(&Inset::lyxCode, code));
891 return it != buffer_->inset_iterator_end() ? (*it) : 0;
893 // Ok, this is a little bit too brute force but it
894 // should work for now. Better infrastructure is comming. (Lgb)
896 Buffer * b = bv_->buffer();
897 LyXCursor cursor = bv_->getLyXText()->cursor;
899 Buffer::inset_iterator beg = b->inset_iterator_begin();
900 Buffer::inset_iterator end = b->inset_iterator_end();
902 bool cursor_par_seen = false;
904 for (; beg != end; ++beg) {
905 if (beg.getPar() == cursor.par()) {
906 cursor_par_seen = true;
908 if (cursor_par_seen) {
909 if (beg.getPar() == cursor.par()
910 && beg.getPos() >= cursor.pos()) {
912 } else if (beg.getPar() != cursor.par()) {
919 // Now find the first inset that matches code.
920 for (; beg != end; ++beg) {
921 if (beg->lyxCode() == code) {
931 void BufferView::Pimpl::MenuInsertLyXFile(string const & filen)
933 string filename = filen;
935 if (filename.empty()) {
936 // Launch a file browser
937 string initpath = lyxrc.document_path;
940 string const trypath = owner_->buffer()->filePath();
941 // If directory is writeable, use this as default.
942 if (IsDirWriteable(trypath))
946 FileDialog fileDlg(bv_->owner(),
947 _("Select LyX document to insert"),
949 make_pair(string(_("Documents|#o#O")),
950 string(lyxrc.document_path)),
951 make_pair(string(_("Examples|#E#e")),
952 string(AddPath(system_lyxdir, "examples"))));
954 FileDialog::Result result =
955 fileDlg.Select(initpath,
956 _("*.lyx| LyX Documents (*.lyx)"));
958 if (result.first == FileDialog::Later)
961 filename = result.second;
963 // check selected filename
964 if (filename.empty()) {
965 owner_->message(_("Canceled."));
970 // get absolute path of file and add ".lyx" to the filename if
972 filename = FileSearch(string(), filename, "lyx");
974 string const disp_fn(MakeDisplayPath(filename));
977 s1 << _("Inserting document") << ' '
978 << disp_fn << " ...";
979 owner_->message(s1.str().c_str());
980 bool const res = bv_->insertLyXFile(filename);
983 str << _("Document") << ' ' << disp_fn
984 << ' ' << _("inserted.");
985 owner_->message(str.str().c_str());
988 str << _("Could not insert document") << ' '
990 owner_->message(str.str().c_str());
995 bool BufferView::Pimpl::dispatch(FuncRequest const & ev)
997 lyxerr[Debug::ACTION] << "BufferView::Pimpl::Dispatch:"
998 << " action[" << ev.action <<"]"
999 << " arg[" << ev.argument << "]"
1000 << " x[" << ev.x << "]"
1001 << " y[" << ev.y << "]"
1002 << " button[" << ev.button() << "]"
1005 LyXTextClass const & tclass = buffer_->params.getLyXTextClass();
1007 switch (ev.action) {
1009 case LFUN_TOC_INSERT:
1011 InsetCommandParams p;
1012 p.setCmdName("tableofcontents");
1013 Inset * inset = new InsetTOC(p);
1014 if (!insertInset(inset, tclass.defaultLayoutName()))
1019 case LFUN_SCROLL_INSET:
1020 // this is not handled here as this function is only active
1021 // if we have a locking_inset and that one is (or contains)
1025 case LFUN_INSET_GRAPHICS:
1027 Inset * new_inset = new InsetGraphics;
1028 if (!insertInset(new_inset)) {
1031 // this is need because you don't use a inset->Edit()
1032 updateInset(new_inset, true);
1033 new_inset->edit(bv_);
1038 case LFUN_LAYOUT_COPY:
1039 bv_->copyEnvironment();
1042 case LFUN_LAYOUT_PASTE:
1043 bv_->pasteEnvironment();
1047 case LFUN_DEPTH_MIN:
1048 changeDepth(bv_, bv_->getLyXText(), -1);
1051 case LFUN_DEPTH_PLUS:
1052 changeDepth(bv_, bv_->getLyXText(), 1);
1056 owner_->getDialogs().setUserFreeFont();
1059 case LFUN_FILE_INSERT:
1060 MenuInsertLyXFile(ev.argument);
1063 case LFUN_FILE_INSERT_ASCII_PARA:
1064 InsertAsciiFile(bv_, ev.argument, true);
1067 case LFUN_FILE_INSERT_ASCII:
1068 InsertAsciiFile(bv_, ev.argument, false);
1072 lang(bv_, ev.argument);
1074 owner_->view_state_changed();
1079 owner_->view_state_changed();
1084 owner_->view_state_changed();
1089 owner_->view_state_changed();
1094 owner_->view_state_changed();
1099 owner_->view_state_changed();
1104 owner_->view_state_changed();
1109 owner_->view_state_changed();
1112 case LFUN_UNDERLINE:
1114 owner_->view_state_changed();
1117 case LFUN_FONT_SIZE:
1118 fontSize(bv_, ev.argument);
1119 owner_->view_state_changed();
1122 case LFUN_FONT_STATE:
1123 owner_->getLyXFunc().setMessage(currentState(bv_));
1126 case LFUN_INSERT_LABEL:
1127 MenuInsertLabel(bv_, ev.argument);
1130 case LFUN_REF_INSERT:
1131 if (ev.argument.empty()) {
1132 InsetCommandParams p("ref");
1133 owner_->getDialogs().createRef(p.getAsString());
1135 InsetCommandParams p;
1136 p.setFromString(ev.argument);
1138 InsetRef * inset = new InsetRef(p, *buffer_);
1139 if (!insertInset(inset))
1142 updateInset(inset, true);
1146 case LFUN_BOOKMARK_SAVE:
1147 savePosition(strToUnsignedInt(ev.argument));
1150 case LFUN_BOOKMARK_GOTO:
1151 restorePosition(strToUnsignedInt(ev.argument));
1156 string label = ev.argument;
1157 if (label.empty()) {
1159 static_cast<InsetRef*>(getInsetByCode(Inset::REF_CODE));
1161 label = inset->getContents();
1166 if (!label.empty()) {
1167 //bv_->savePosition(0);
1168 if (!bv_->gotoLabel(label))
1169 Alert::alert(_("Error"),
1170 _("Couldn't find this label"),
1171 _("in current document."));
1179 InsetCommandParams p;
1180 if (ev.action == LFUN_HTMLURL)
1181 p.setCmdName("htmlurl");
1183 p.setCmdName("url");
1184 owner_->getDialogs().createUrl(p.getAsString());
1188 case LFUN_INSERT_URL:
1190 InsetCommandParams p;
1191 p.setFromString(ev.argument);
1193 InsetUrl * inset = new InsetUrl(p);
1194 if (!insertInset(inset))
1197 updateInset(inset, true);
1201 case LFUN_INSET_CAPTION:
1203 // Do we have a locking inset...
1204 if (bv_->theLockingInset()) {
1205 lyxerr << "Locking inset code: "
1206 << static_cast<int>(bv_->theLockingInset()->lyxCode());
1207 InsetCaption * new_inset =
1208 new InsetCaption(buffer_->params);
1209 new_inset->setOwner(bv_->theLockingInset());
1210 new_inset->setAutoBreakRows(true);
1211 new_inset->setDrawFrame(0, InsetText::LOCKED);
1212 new_inset->setFrameColor(0, LColor::captionframe);
1213 if (insertInset(new_inset))
1214 new_inset->edit(bv_);
1222 // --- accented characters ---------------------------
1225 case LFUN_CIRCUMFLEX:
1235 case LFUN_SPECIAL_CARON:
1238 case LFUN_HUNG_UMLAUT:
1241 if (ev.argument.empty()) {
1243 owner_->getLyXFunc().handleKeyFunc(ev.action);
1245 owner_->getLyXFunc().handleKeyFunc(ev.action);
1246 owner_->getIntl().getTransManager()
1247 .TranslateAndInsert(ev.argument[0], bv_->getLyXText());
1248 update(bv_->getLyXText(),
1250 | BufferView::FITCUR
1251 | BufferView::CHANGE);
1255 case LFUN_MATH_MACRO:
1256 case LFUN_MATH_DELIM:
1257 case LFUN_INSERT_MATRIX:
1258 case LFUN_INSERT_MATH:
1259 case LFUN_MATH_IMPORT_SELECTION: // Imports LaTeX from the X selection
1260 case LFUN_MATH_DISPLAY: // Open or create a displayed math inset
1261 case LFUN_MATH_MODE: // Open or create an inlined math inset
1262 case LFUN_GREEK: // Insert a single greek letter
1263 mathDispatch(FuncRequest(bv_, ev.action, ev.argument));
1266 case LFUN_CITATION_INSERT:
1268 InsetCommandParams p;
1269 p.setFromString(ev.argument);
1271 InsetCitation * inset = new InsetCitation(p);
1272 if (!insertInset(inset))
1275 updateInset(inset, true);
1279 case LFUN_INSERT_BIBTEX:
1281 // ale970405+lasgoutt970425
1282 // The argument can be up to two tokens separated
1283 // by a space. The first one is the bibstyle.
1284 string const db = token(ev.argument, ' ', 0);
1285 string bibstyle = token(ev.argument, ' ', 1);
1286 if (bibstyle.empty())
1289 InsetCommandParams p("BibTeX", db, bibstyle);
1290 InsetBibtex * inset = new InsetBibtex(p);
1292 if (insertInset(inset)) {
1293 if (ev.argument.empty())
1300 // BibTeX data bases
1301 case LFUN_BIBDB_ADD:
1303 InsetBibtex * inset =
1304 static_cast<InsetBibtex*>(getInsetByCode(Inset::BIBTEX_CODE));
1306 inset->addDatabase(ev.argument);
1311 case LFUN_BIBDB_DEL:
1313 InsetBibtex * inset =
1314 static_cast<InsetBibtex*>(getInsetByCode(Inset::BIBTEX_CODE));
1316 inset->delDatabase(ev.argument);
1321 case LFUN_BIBTEX_STYLE:
1323 InsetBibtex * inset =
1324 static_cast<InsetBibtex*>(getInsetByCode(Inset::BIBTEX_CODE));
1326 inset->setOptions(ev.argument);
1331 case LFUN_INDEX_INSERT:
1333 string entry = ev.argument;
1335 entry = bv_->getLyXText()->getStringToIndex(bv_);
1337 if (entry.empty()) {
1338 owner_->getDialogs().createIndex();
1342 InsetIndex * inset = new InsetIndex(InsetCommandParams("index", entry));
1344 if (!insertInset(inset)) {
1347 updateInset(inset, true);
1352 case LFUN_INDEX_PRINT:
1354 InsetCommandParams p("printindex");
1355 Inset * inset = new InsetPrintIndex(p);
1356 if (!insertInset(inset, tclass.defaultLayoutName()))
1361 case LFUN_PARENTINSERT:
1363 InsetCommandParams p("lyxparent", ev.argument);
1364 Inset * inset = new InsetParent(p, *buffer_);
1365 if (!insertInset(inset, tclass.defaultLayoutName()))
1371 case LFUN_CHILD_INSERT:
1373 InsetInclude::Params p;
1374 p.cparams.setFromString(ev.argument);
1375 p.masterFilename_ = buffer_->fileName();
1377 InsetInclude * inset = new InsetInclude(p);
1378 if (!insertInset(inset))
1381 updateInset(inset, true);
1382 bv_->owner()->getDialogs().showInclude(inset);
1387 case LFUN_FLOAT_LIST:
1388 if (tclass.floats().typeExist(ev.argument)) {
1389 Inset * inset = new InsetFloatList(ev.argument);
1390 if (!insertInset(inset, tclass.defaultLayoutName()))
1393 lyxerr << "Non-existent float type: "
1394 << ev.argument << endl;
1398 case LFUN_THESAURUS_ENTRY:
1400 string arg = ev.argument;
1403 arg = bv_->getLyXText()->selectionAsString(buffer_,
1407 if (arg.size() > 100 || arg.empty()) {
1408 // Get word or selection
1409 bv_->getLyXText()->selectWordWhenUnderCursor(bv_, LyXText::WHOLE_WORD);
1410 arg = bv_->getLyXText()->selectionAsString(buffer_, false);
1411 // FIXME: where is getLyXText()->unselect(bv_) ?
1415 bv_->owner()->getDialogs().showThesaurus(arg);
1419 case LFUN_UNKNOWN_ACTION:
1420 ev.errorMessage(N_("Unknown function!"));
1424 return bv_->getLyXText()->dispatch(FuncRequest(ev, bv_));
1431 bool BufferView::Pimpl::insertInset(Inset * inset, string const & lout)
1433 // if we are in a locking inset we should try to insert the
1434 // inset there otherwise this is a illegal function now
1435 if (bv_->theLockingInset()) {
1436 if (bv_->theLockingInset()->insetAllowed(inset))
1437 return bv_->theLockingInset()->insertInset(bv_, inset);
1441 // not quite sure if we want this...
1442 setCursorParUndo(bv_);
1445 beforeChange(bv_->text);
1446 if (!lout.empty()) {
1447 update(bv_->text, BufferView::SELECT|BufferView::FITCUR);
1448 bv_->text->breakParagraph(bv_);
1449 update(bv_->text, BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
1451 if (!bv_->text->cursor.par()->empty()) {
1452 bv_->text->cursorLeft(bv_);
1454 bv_->text->breakParagraph(bv_);
1455 update(bv_->text, BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
1459 LyXTextClass const & tclass =
1460 buffer_->params.getLyXTextClass();
1461 bool hasLayout = tclass.hasLayout(lres);
1462 string lay = tclass.defaultLayoutName();
1464 if (hasLayout != false) {
1468 // layout not fount using default
1469 lay = tclass.defaultLayoutName();
1472 bv_->text->setLayout(bv_, lay);
1474 bv_->text->setParagraph(bv_, 0, 0,
1476 VSpace(VSpace::NONE), VSpace(VSpace::NONE),
1481 update(bv_->text, BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
1484 bv_->text->insertInset(bv_, inset);
1485 update(bv_->text, BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
1492 void BufferView::Pimpl::updateInset(Inset * inset, bool mark_dirty)
1494 if (!inset || !available())
1497 // first check for locking insets
1498 if (bv_->theLockingInset()) {
1499 if (bv_->theLockingInset() == inset) {
1500 if (bv_->text->updateInset(bv_, inset)) {
1503 buffer_->markDirty();
1508 } else if (bv_->theLockingInset()->updateInsetInInset(bv_, inset)) {
1509 if (bv_->text->updateInset(bv_, bv_->theLockingInset())) {
1512 buffer_->markDirty();
1520 // then check if the inset is a top_level inset (has no owner)
1521 // if yes do the update as always otherwise we have to update the
1522 // toplevel inset where this inset is inside
1523 Inset * tl_inset = inset;
1524 while (tl_inset->owner())
1525 tl_inset = tl_inset->owner();
1527 if (tl_inset == inset) {
1528 update(bv_->text, BufferView::UPDATE);
1529 if (bv_->text->updateInset(bv_, inset)) {
1533 | BufferView::FITCUR
1534 | BufferView::CHANGE);
1536 update(bv_->text, SELECT);
1540 } else if (static_cast<UpdatableInset *>(tl_inset)
1541 ->updateInsetInInset(bv_, inset))
1543 if (bv_->text->updateInset(bv_, tl_inset)) {