]> git.lyx.org Git - lyx.git/blob - src/BufferView_pimpl.C
98ec94d012ebed5f4b037cbf6189cba851e2f4ba
[lyx.git] / src / BufferView_pimpl.C
1 /**
2  * \file BufferView_pimpl.C
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Asger Alstrup
7  * \author Alfredo Braunstein
8  * \author Lars Gullik Bjønnes
9  * \author Jean-Marc Lasgouttes
10  * \author Angus Leeming
11  * \author John Levon
12  * \author André Pönitz
13  * \author Dekel Tsur
14  * \author Jürgen Vigna
15  *
16  * Full author contact details are available in file CREDITS.
17  */
18
19 #include <config.h>
20
21 #include "BufferView_pimpl.h"
22 #include "buffer.h"
23 #include "buffer_funcs.h"
24 #include "bufferlist.h"
25 #include "bufferparams.h"
26 #include "cursor.h"
27 #include "debug.h"
28 #include "dispatchresult.h"
29 #include "factory.h"
30 #include "FloatList.h"
31 #include "funcrequest.h"
32 #include "gettext.h"
33 #include "intl.h"
34 #include "iterators.h"
35 #include "lyx_cb.h" // added for Dispatch functions
36 #include "lyx_main.h"
37 #include "lyxfind.h"
38 #include "lyxfunc.h"
39 #include "lyxtext.h"
40 #include "lyxrc.h"
41 #include "lastfiles.h"
42 #include "paragraph.h"
43 #include "paragraph_funcs.h"
44 #include "ParagraphParameters.h"
45 #include "undo.h"
46 #include "vspace.h"
47
48 #include "insets/insetref.h"
49 #include "insets/insettext.h"
50
51 #include "frontends/Alert.h"
52 #include "frontends/Dialogs.h"
53 #include "frontends/FileDialog.h"
54 #include "frontends/LyXView.h"
55 #include "frontends/LyXScreenFactory.h"
56 #include "frontends/screen.h"
57 #include "frontends/WorkArea.h"
58 #include "frontends/WorkAreaFactory.h"
59
60 #include "graphics/Previews.h"
61
62 #include "support/filetools.h"
63 #include "support/globbing.h"
64 #include "support/path_defines.h"
65 #include "support/tostr.h"
66
67 #include <boost/bind.hpp>
68
69 using lyx::pos_type;
70
71 using lyx::support::AddPath;
72 using lyx::support::bformat;
73 using lyx::support::FileFilterList;
74 using lyx::support::FileSearch;
75 using lyx::support::IsDirWriteable;
76 using lyx::support::MakeDisplayPath;
77 using lyx::support::strToUnsignedInt;
78 using lyx::support::system_lyxdir;
79
80 using std::endl;
81 using std::istringstream;
82 using std::make_pair;
83 using std::min;
84 using std::string;
85
86
87 extern BufferList bufferlist;
88
89
90 namespace {
91
92 unsigned int const saved_positions_num = 20;
93
94 // All the below connection objects are needed because of a bug in some
95 // versions of GCC (<=2.96 are on the suspects list.) By having and assigning
96 // to these connections we avoid a segfault upon startup, and also at exit.
97 // (Lgb)
98
99 boost::signals::connection dispatchcon;
100 boost::signals::connection timecon;
101 boost::signals::connection doccon;
102 boost::signals::connection resizecon;
103 boost::signals::connection kpresscon;
104 boost::signals::connection selectioncon;
105 boost::signals::connection lostcon;
106
107
108 } // anon namespace
109
110
111 BufferView::Pimpl::Pimpl(BufferView & bv, LyXView * owner,
112              int xpos, int ypos, int width, int height)
113         : bv_(&bv), owner_(owner), buffer_(0), cursor_timeout(400),
114           using_xterm_cursor(false), cursor_(bv)
115 {
116         xsel_cache_.set = false;
117
118         workarea_.reset(WorkAreaFactory::create(xpos, ypos, width, height));
119         screen_.reset(LyXScreenFactory::create(workarea()));
120
121         // Setup the signals
122         doccon = workarea().scrollDocView
123                 .connect(boost::bind(&BufferView::Pimpl::scrollDocView, this, _1));
124         resizecon = workarea().workAreaResize
125                 .connect(boost::bind(&BufferView::Pimpl::workAreaResize, this));
126         dispatchcon = workarea().dispatch
127                 .connect(boost::bind(&BufferView::Pimpl::workAreaDispatch, this, _1));
128         kpresscon = workarea().workAreaKeyPress
129                 .connect(boost::bind(&BufferView::Pimpl::workAreaKeyPress, this, _1, _2));
130         selectioncon = workarea().selectionRequested
131                 .connect(boost::bind(&BufferView::Pimpl::selectionRequested, this));
132         lostcon = workarea().selectionLost
133                 .connect(boost::bind(&BufferView::Pimpl::selectionLost, this));
134
135         timecon = cursor_timeout.timeout
136                 .connect(boost::bind(&BufferView::Pimpl::cursorToggle, this));
137         cursor_timeout.start();
138         saved_positions.resize(saved_positions_num);
139 }
140
141
142 void BufferView::Pimpl::addError(ErrorItem const & ei)
143 {
144         errorlist_.push_back(ei);
145 }
146
147
148 void BufferView::Pimpl::showReadonly(bool)
149 {
150         owner_->updateWindowTitle();
151         owner_->getDialogs().updateBufferDependent(false);
152 }
153
154
155 void BufferView::Pimpl::connectBuffer(Buffer & buf)
156 {
157         if (errorConnection_.connected())
158                 disconnectBuffer();
159
160         errorConnection_ =
161                 buf.error.connect(
162                         boost::bind(&BufferView::Pimpl::addError, this, _1));
163
164         messageConnection_ =
165                 buf.message.connect(
166                         boost::bind(&LyXView::message, owner_, _1));
167
168         busyConnection_ =
169                 buf.busy.connect(
170                         boost::bind(&LyXView::busy, owner_, _1));
171
172         titleConnection_ =
173                 buf.updateTitles.connect(
174                         boost::bind(&LyXView::updateWindowTitle, owner_));
175
176         timerConnection_ =
177                 buf.resetAutosaveTimers.connect(
178                         boost::bind(&LyXView::resetAutosaveTimer, owner_));
179
180         readonlyConnection_ =
181                 buf.readonly.connect(
182                         boost::bind(&BufferView::Pimpl::showReadonly, this, _1));
183
184         closingConnection_ =
185                 buf.closing.connect(
186                         boost::bind(&BufferView::Pimpl::setBuffer, this, (Buffer *)0));
187 }
188
189
190 void BufferView::Pimpl::disconnectBuffer()
191 {
192         errorConnection_.disconnect();
193         messageConnection_.disconnect();
194         busyConnection_.disconnect();
195         titleConnection_.disconnect();
196         timerConnection_.disconnect();
197         readonlyConnection_.disconnect();
198         closingConnection_.disconnect();
199 }
200
201
202 void BufferView::Pimpl::newFile(string const & filename, string const & tname,
203         bool isNamed)
204 {
205         setBuffer(::newFile(filename, tname, isNamed));
206 }
207
208
209 bool BufferView::Pimpl::loadLyXFile(string const & filename, bool tolastfiles)
210 {
211         // get absolute path of file and add ".lyx" to the filename if
212         // necessary
213         string s = FileSearch(string(), filename, "lyx");
214
215         bool const found = !s.empty();
216
217         if (!found)
218                 s = filename;
219
220         // file already open?
221         if (bufferlist.exists(s)) {
222                 string const file = MakeDisplayPath(s, 20);
223                 string text = bformat(_("The document %1$s is already "
224                                         "loaded.\n\nDo you want to revert "
225                                         "to the saved version?"), file);
226                 int const ret = Alert::prompt(_("Revert to saved document?"),
227                         text, 0, 1,  _("&Revert"), _("&Switch to document"));
228
229                 if (ret != 0) {
230                         setBuffer(bufferlist.getBuffer(s));
231                         return true;
232                 }
233                 // FIXME: should be LFUN_REVERT
234                 if (!bufferlist.close(bufferlist.getBuffer(s), false))
235                         return false;
236                 // Fall through to new load. (Asger)
237         }
238
239         Buffer * b;
240
241         if (found) {
242                 b = bufferlist.newBuffer(s);
243                 connectBuffer(*b);
244                 if (!::loadLyXFile(b, s)) {
245                         bufferlist.release(b);
246                         return false;
247                 }
248         } else {
249                 string text = bformat(_("The document %1$s does not yet "
250                                         "exist.\n\nDo you want to create "
251                                         "a new document?"), s);
252                 int const ret = Alert::prompt(_("Create new document?"),
253                          text, 0, 1, _("&Create"), _("Cancel"));
254
255                 if (ret == 0)
256                         b = ::newFile(s, string(), true);
257                 else
258                         return false;
259         }
260
261         setBuffer(b);
262         bv_->showErrorList(_("Parse"));
263
264         if (tolastfiles)
265                 LyX::ref().lastfiles().newFile(b->fileName());
266
267         return true;
268 }
269
270
271 WorkArea & BufferView::Pimpl::workarea() const
272 {
273         return *workarea_.get();
274 }
275
276
277 LyXScreen & BufferView::Pimpl::screen() const
278 {
279         return *screen_.get();
280 }
281
282
283 Painter & BufferView::Pimpl::painter() const
284 {
285         return workarea().getPainter();
286 }
287
288
289 void BufferView::Pimpl::top_y(int y)
290 {
291         top_y_ = y;
292 }
293
294
295 int BufferView::Pimpl::top_y() const
296 {
297         return top_y_;
298 }
299
300
301 void BufferView::Pimpl::setBuffer(Buffer * b)
302 {
303         lyxerr[Debug::INFO] << "Setting buffer in BufferView ("
304                             << b << ')' << endl;
305         if (buffer_)
306                 disconnectBuffer();
307
308         // set current buffer
309         buffer_ = b;
310
311         // reset old cursor
312         top_y_ = 0;
313         cursor_ = LCursor(*bv_);
314
315         // if we're quitting lyx, don't bother updating stuff
316         if (quitting)
317                 return;
318
319         if (buffer_) {
320                 lyxerr[Debug::INFO] << "Buffer addr: " << buffer_ << endl;
321                 connectBuffer(*buffer_);
322
323                 cursor_.push(buffer_->inset());
324                 cursor_.resetAnchor();
325                 buffer_->text().init(bv_);
326
327                 // If we don't have a text object for this, we make one
328                 //if (bv_->text() == 0)
329                 //      resizeCurrentBuffer();
330
331                 // Buffer-dependent dialogs should be updated or
332                 // hidden. This should go here because some dialogs (eg ToC)
333                 // require bv_->text.
334                 owner_->getDialogs().updateBufferDependent(true);
335                 owner_->setLayout(bv_->text()->getPar(0)->layout()->name());
336         } else {
337                 lyxerr[Debug::INFO] << "  No Buffer!" << endl;
338                 // we are closing the buffer, use the first buffer as current
339                 buffer_ = bufferlist.first();
340                 owner_->getDialogs().hideBufferDependent();
341         }
342
343         update();
344         updateScrollbar();
345         owner_->updateMenubar();
346         owner_->updateToolbar();
347         owner_->updateLayoutChoice();
348         owner_->updateWindowTitle();
349
350         if (lyx::graphics::Previews::activated() && buffer_)
351                 lyx::graphics::Previews::get().generateBufferPreviews(*buffer_);
352 }
353
354
355 bool BufferView::Pimpl::fitCursor()
356 {
357         if (!screen().fitCursor(bv_))
358                 return false;
359         updateScrollbar();
360         return true;
361 }
362
363
364 void BufferView::Pimpl::redoCurrentBuffer()
365 {
366         lyxerr[Debug::INFO] << "BufferView::redoCurrentBuffer" << endl;
367         if (buffer_ && bv_->text()) {
368                 resizeCurrentBuffer();
369                 updateScrollbar();
370                 owner_->updateLayoutChoice();
371         }
372 }
373
374
375 void BufferView::Pimpl::resizeCurrentBuffer()
376 {
377         lyxerr[Debug::INFO] << "resizeCurrentBuffer" << endl;
378
379         int par = -1;
380         int selstartpar = -1;
381         int selendpar = -1;
382
383         pos_type pos = 0;
384         pos_type selstartpos = 0;
385         pos_type selendpos = 0;
386         bool sel = false;
387         bool mark_set  = false;
388
389         owner_->busy(true);
390
391         owner_->message(_("Formatting document..."));
392
393         LyXText * text = bv_->text();
394         lyxerr << "### resizeCurrentBuffer: text " << text << endl;
395         if (!text)
396                 return;
397
398         LCursor & cur = bv_->cursor();
399         par = cur.par();
400         pos = cur.pos();
401         selstartpar = cur.selBegin().par();
402         selstartpos = cur.selBegin().pos();
403         selendpar = cur.selEnd().par();
404         selendpos = cur.selEnd().pos();
405         sel = cur.selection();
406         mark_set = cur.mark();
407         text->init(bv_);
408         update();
409
410         if (par != -1) {
411                 cur.selection() = true;
412                 // At this point just to avoid the Delete-Empty-Paragraph-
413                 // Mechanism when setting the cursor.
414                 cur.mark() = mark_set;
415                 if (sel) {
416                         text->setCursor(cur, selstartpar, selstartpos);
417                         cur.resetAnchor();
418                         text->setCursor(cur, selendpar, selendpos);
419                         cur.setSelection();
420                         text->setCursor(cur, par, pos);
421                 } else {
422                         text->setCursor(cur, par, pos);
423                         cur.resetAnchor();
424                         cur.selection() = false;
425                 }
426         }
427
428         fitCursor();
429
430         switchKeyMap();
431         owner_->busy(false);
432
433         // reset the "Formatting..." message
434         owner_->clearMessage();
435
436         updateScrollbar();
437 }
438
439
440 void BufferView::Pimpl::updateScrollbar()
441 {
442         if (!bv_->text()) {
443                 lyxerr[Debug::GUI] << "no text in updateScrollbar" << endl;
444                 lyxerr << "no text in updateScrollbar" << endl;
445                 workarea().setScrollbarParams(0, 0, 0);
446                 return;
447         }
448
449         LyXText const & t = *bv_->text();
450
451         lyxerr[Debug::GUI]
452                 << "Updating scrollbar: height: " << t.height()
453                 << " top_y: " << top_y()
454                 << " default height " << defaultRowHeight() << endl;
455
456         workarea().setScrollbarParams(t.height(), top_y(), defaultRowHeight());
457 }
458
459
460 void BufferView::Pimpl::scrollDocView(int value)
461 {
462         lyxerr[Debug::GUI] << "scrollDocView of " << value << endl;
463
464         if (!buffer_)
465                 return;
466
467         screen().hideCursor();
468
469         top_y(value);
470         screen().redraw(*bv_);
471
472         if (!lyxrc.cursor_follows_scrollbar)
473                 return;
474
475         int const height = defaultRowHeight();
476         int const first = top_y() + height;
477         int const last = top_y() + workarea().workHeight() - height;
478
479         bv_->cursor().reset();
480         LyXText * text = bv_->text();
481         CursorSlice & cur = bv_->cursor().front();
482         int y = text->cursorY(cur);
483         if (y < first)
484                 text->setCursorFromCoordinates(bv_->cursor(), 0, first);
485         else if (y > last)
486                 text->setCursorFromCoordinates(bv_->cursor(), 0, last);
487
488         owner_->updateLayoutChoice();
489 }
490
491
492 void BufferView::Pimpl::scroll(int lines)
493 {
494         if (!buffer_)
495                 return;
496
497         LyXText const * t = bv_->text();
498         int const line_height = defaultRowHeight();
499
500         // The new absolute coordinate
501         int new_top_y = top_y() + lines * line_height;
502
503         // Restrict to a valid value
504         new_top_y = std::min(t->height() - 4 * line_height, new_top_y);
505         new_top_y = std::max(0, new_top_y);
506
507         scrollDocView(new_top_y);
508
509         // Update the scrollbar.
510         workarea().setScrollbarParams(t->height(), top_y(), defaultRowHeight());
511 }
512
513
514 void BufferView::Pimpl::workAreaKeyPress(LyXKeySymPtr key,
515                                          key_modifier::state state)
516 {
517         bv_->owner()->getLyXFunc().processKeySym(key, state);
518
519         /* This is perhaps a bit of a hack. When we move
520          * around, or type, it's nice to be able to see
521          * the cursor immediately after the keypress. So
522          * we reset the toggle timeout and force the visibility
523          * of the cursor. Note we cannot do this inside
524          * dispatch() itself, because that's called recursively.
525          */
526         if (available()) {
527                 cursor_timeout.restart();
528                 screen().showCursor(*bv_);
529         }
530 }
531
532
533 void BufferView::Pimpl::selectionRequested()
534 {
535         static string sel;
536
537         if (!available())
538                 return;
539
540         LCursor & cur = bv_->cursor();
541
542         if (!cur.selection()) {
543                 xsel_cache_.set = false;
544                 return;
545         }
546
547         if (!xsel_cache_.set ||
548             cur.back() != xsel_cache_.cursor ||
549             cur.anchor_.back() != xsel_cache_.anchor)
550         {
551                 xsel_cache_.cursor = cur.back();
552                 xsel_cache_.anchor = cur.anchor_.back();
553                 xsel_cache_.set = cur.selection();
554                 sel = cur.selectionAsString(false);
555                 if (!sel.empty())
556                         workarea().putClipboard(sel);
557         } 
558 }
559
560
561 void BufferView::Pimpl::selectionLost()
562 {
563         if (available()) {
564                 screen().hideCursor();
565                 bv_->cursor().clearSelection();
566                 xsel_cache_.set = false;
567         }
568 }
569
570
571 void BufferView::Pimpl::workAreaResize()
572 {
573         static int work_area_width;
574         static int work_area_height;
575
576         bool const widthChange = workarea().workWidth() != work_area_width;
577         bool const heightChange = workarea().workHeight() != work_area_height;
578
579         // update from work area
580         work_area_width = workarea().workWidth();
581         work_area_height = workarea().workHeight();
582
583         if (buffer_ && widthChange) {
584                 // The visible LyXView need a resize
585                 resizeCurrentBuffer();
586         }
587
588         if (widthChange || heightChange)
589                 update();
590
591         // always make sure that the scrollbar is sane.
592         updateScrollbar();
593         owner_->updateLayoutChoice();
594 }
595
596
597 void BufferView::Pimpl::update()
598 {
599         //lyxerr << "BufferView::Pimpl::update(), buffer: " << buffer_ << endl;
600         // fix cursor coordinate cache in case something went wrong
601
602         // check needed to survive LyX startup
603         if (buffer_) {
604                 // update all 'visible' paragraphs
605                 ParagraphList::iterator beg;
606                 ParagraphList::iterator end;
607                 getParsInRange(buffer_->paragraphs(),
608                                top_y(), top_y() + workarea().workHeight(),
609                                beg, end);
610                 bv_->text()->redoParagraphs(beg, end);
611                 updateScrollbar();
612         }
613         screen().redraw(*bv_);
614         bv_->owner()->view_state_changed();
615 }
616
617
618 // Callback for cursor timer
619 void BufferView::Pimpl::cursorToggle()
620 {
621         if (buffer_)
622                 screen().toggleCursor(*bv_);
623         cursor_timeout.restart();
624 }
625
626
627 bool BufferView::Pimpl::available() const
628 {
629         return buffer_ && bv_->text();
630 }
631
632
633 Change const BufferView::Pimpl::getCurrentChange()
634 {
635         if (!bv_->buffer()->params().tracking_changes)
636                 return Change(Change::UNCHANGED);
637
638         LyXText * text = bv_->getLyXText();
639         LCursor & cur = bv_->cursor();
640
641         if (!cur.selection())
642                 return Change(Change::UNCHANGED);
643
644         return text->getPar(cur.selBegin())
645                 ->lookupChangeFull(cur.selBegin().pos());
646 }
647
648
649 void BufferView::Pimpl::savePosition(unsigned int i)
650 {
651         if (i >= saved_positions_num)
652                 return;
653         saved_positions[i] = Position(buffer_->fileName(),
654                                       bv_->text()->cursorPar()->id(),
655                                       bv_->text()->cursor().pos());
656         if (i > 0)
657                 owner_->message(bformat(_("Saved bookmark %1$s"), tostr(i)));
658 }
659
660
661 void BufferView::Pimpl::restorePosition(unsigned int i)
662 {
663         if (i >= saved_positions_num)
664                 return;
665
666         string const fname = saved_positions[i].filename;
667
668         bv_->cursor().clearSelection();
669
670         if (fname != buffer_->fileName()) {
671                 Buffer * b = 0;
672                 if (bufferlist.exists(fname))
673                         b = bufferlist.getBuffer(fname);
674                 else {
675                         b = bufferlist.newBuffer(fname);
676                         ::loadLyXFile(b, fname); // don't ask, just load it
677                 }
678                 if (b)
679                         setBuffer(b);
680         }
681
682         ParIterator par = buffer_->getParFromID(saved_positions[i].par_id);
683         if (par == buffer_->par_iterator_end())
684                 return;
685
686         bv_->text()->setCursor(
687                 bv_->cursor(),
688                 bv_->text()->parOffset(par.pit()),
689                 min(par->size(), saved_positions[i].par_pos));
690
691         if (i > 0)
692                 owner_->message(bformat(_("Moved to bookmark %1$s"), tostr(i)));
693 }
694
695
696 bool BufferView::Pimpl::isSavedPosition(unsigned int i)
697 {
698         return i < saved_positions_num && !saved_positions[i].filename.empty();
699 }
700
701
702 void BufferView::Pimpl::switchKeyMap()
703 {
704         if (!lyxrc.rtl_support)
705                 return;
706
707         Intl & intl = owner_->getIntl();
708         if (bv_->getLyXText()->real_current_font.isRightToLeft()) {
709                 if (intl.keymap == Intl::PRIMARY)
710                         intl.KeyMapSec();
711         } else {
712                 if (intl.keymap == Intl::SECONDARY)
713                         intl.KeyMapPrim();
714         }
715 }
716
717
718 void BufferView::Pimpl::center()
719 {
720         LyXText * text = bv_->text();
721
722         bv_->cursor().clearSelection();
723         int const half_height = workarea().workHeight() / 2;
724         int new_y = text->cursorY(bv_->cursor().front()) - half_height;
725         if (new_y < 0)
726                 new_y = 0;
727
728         // FIXME: look at this comment again ...
729         // This updates top_y() but means the fitCursor() call
730         // from the update(FITCUR) doesn't realise that we might
731         // have moved (e.g. from GOTOPARAGRAPH), so doesn't cause
732         // the scrollbar to be updated as it should, so we have
733         // to do it manually. Any operation that does a center()
734         // and also might have moved top_y() must make sure to call
735         // updateScrollbar() currently. Never mind that this is a
736         // pretty obfuscated way of updating text->top_y()
737         top_y(new_y);
738 }
739
740
741 void BufferView::Pimpl::stuffClipboard(string const & stuff) const
742 {
743         workarea().putClipboard(stuff);
744 }
745
746
747 InsetBase * BufferView::Pimpl::getInsetByCode(InsetBase::Code code)
748 {
749 #warning Does not work for mathed
750         // Ok, this is a little bit too brute force but it
751         // should work for now. Better infrastructure is coming. (Lgb)
752
753         Buffer * buf = bv_->buffer();
754         Buffer::inset_iterator beg = buf->inset_iterator_begin();
755         Buffer::inset_iterator end = buf->inset_iterator_end();
756
757         bool cursor_par_seen = false;
758
759         LCursor & cur = bv_->cursor();
760         ParagraphList::iterator pit =  bv_->getLyXText()->getPar(cur.par());
761
762         for (; beg != end; ++beg) {
763                 if (beg.getPar() == pit)
764                         cursor_par_seen = true;
765                 if (cursor_par_seen) {
766                         if (beg.getPar() == pit && beg.getPos() >= cur.pos())
767                                 break;
768                         if (beg.getPar() != pit)
769                                 break;
770                 }
771         }
772         if (beg != end) {
773                 // Now find the first inset that matches code.
774                 for (; beg != end; ++beg) {
775                         if (beg->lyxCode() == code)
776                                 return &(*beg);
777                 }
778         }
779         return 0;
780 }
781
782
783 void BufferView::Pimpl::MenuInsertLyXFile(string const & filenm)
784 {
785         string filename = filenm;
786
787         if (filename.empty()) {
788                 // Launch a file browser
789                 string initpath = lyxrc.document_path;
790
791                 if (available()) {
792                         string const trypath = owner_->buffer()->filePath();
793                         // If directory is writeable, use this as default.
794                         if (IsDirWriteable(trypath))
795                                 initpath = trypath;
796                 }
797
798                 FileDialog fileDlg(_("Select LyX document to insert"),
799                         LFUN_FILE_INSERT,
800                         make_pair(string(_("Documents|#o#O")),
801                                   string(lyxrc.document_path)),
802                         make_pair(string(_("Examples|#E#e")),
803                                   string(AddPath(system_lyxdir(), "examples"))));
804
805                 FileDialog::Result result =
806                         fileDlg.open(initpath,
807                                      FileFilterList(_("LyX Documents (*.lyx)")),
808                                      string());
809
810                 if (result.first == FileDialog::Later)
811                         return;
812
813                 filename = result.second;
814
815                 // check selected filename
816                 if (filename.empty()) {
817                         owner_->message(_("Canceled."));
818                         return;
819                 }
820         }
821
822         // get absolute path of file and add ".lyx" to the filename if
823         // necessary
824         filename = FileSearch(string(), filename, "lyx");
825
826         string const disp_fn = MakeDisplayPath(filename);
827         owner_->message(bformat(_("Inserting document %1$s..."), disp_fn));
828         if (bv_->insertLyXFile(filename))
829                 owner_->message(bformat(_("Document %1$s inserted."),
830                                         disp_fn));
831         else
832                 owner_->message(bformat(_("Could not insert document %1$s"),
833                                         disp_fn));
834 }
835
836
837 void BufferView::Pimpl::trackChanges()
838 {
839         Buffer * buf = bv_->buffer();
840         bool const tracking = buf->params().tracking_changes;
841
842         if (!tracking) {
843                 ParIterator const end = buf->par_iterator_end();
844                 for (ParIterator it = buf->par_iterator_begin(); it != end; ++it)
845                         it->trackChanges();
846                 buf->params().tracking_changes = true;
847
848                 // we cannot allow undos beyond the freeze point
849                 buf->undostack().clear();
850         } else {
851                 update();
852                 bv_->text()->setCursor(bv_->cursor(), 0, 0);
853 #warning changes FIXME
854                 bool found = lyx::find::findNextChange(bv_);
855                 if (found) {
856                         owner_->getDialogs().show("changes");
857                         return;
858                 }
859
860                 ParIterator const end = buf->par_iterator_end();
861                 for (ParIterator it = buf->par_iterator_begin(); it != end; ++it)
862                         it->untrackChanges();
863                 buf->params().tracking_changes = false;
864         }
865
866         buf->redostack().clear();
867 }
868
869
870 bool BufferView::Pimpl::workAreaDispatch(FuncRequest const & cmd0)
871 {
872         //
873         // this is only called for mouse related events (including
874         // LFUN_FILE_OPEN generated by drag-and-drop)
875         //
876         FuncRequest cmd = cmd0;
877         cmd.y += bv_->top_y();
878         //lyxerr << "*** workAreaDispatch: request: " << cmd << std::endl;
879         LCursor cur(*bv_);
880         cur.push(bv_->buffer()->inset());
881         cur.resetAnchor();
882         cur.selection() = bv_->cursor().selection();
883         switch (cmd.action) {
884
885 #if 0
886         case LFUN_MOUSE_MOTION: {
887                 if (!available())
888                         return false;
889                 FuncRequest cmd1 = cmd;
890                 DispatchResult res = cur.inset().dispatch(cur, cmd);
891                 if (fitCursor() || res.update()) {
892                         update();
893                         cur.updatePos();
894                 }
895                 return true;
896         }
897 #else
898         case LFUN_MOUSE_MOTION: 
899 #endif
900
901         case LFUN_MOUSE_PRESS:
902         case LFUN_MOUSE_RELEASE:
903         case LFUN_MOUSE_DOUBLE:
904         case LFUN_MOUSE_TRIPLE: {
905                 // We pass those directly to the Bufferview, since
906                 // otherwise selection handling breaks down
907
908                 // Doesn't go through lyxfunc, so we need to update
909                 // the layout choice etc. ourselves
910
911                 // e.g. Qt mouse press when no buffer
912                 if (!available())
913                         return false;
914
915                 screen().hideCursor();
916
917                 // either the inset under the cursor or the
918                 // surrounding LyXText will handle this event.
919
920                 // built temporary path to inset
921                 InsetBase * inset = bv_->text()->editXY(cur, cmd.x, cmd.y);
922                 lyxerr << "hit inset at tip: " << inset << endl;
923                 lyxerr << "created temp cursor:\n" << cur << endl;
924
925                 // Try to dispatch to an non-editable inset near this position
926                 DispatchResult res;
927                 if (inset)
928                         inset->dispatch(cur, cmd);
929
930                 // Dispatch to the temp cursor.
931                 // An inset (or LyXText) can assign this to bv->cursor()
932                 // if it wishes to do so.
933                 if (!res.dispatched())
934                         res = cur.dispatch(cmd);
935
936                 if (fitCursor() || res.update())
937                         update();
938
939                 // see workAreaKeyPress
940                 cursor_timeout.restart();
941                 screen().showCursor(*bv_);
942
943                 // skip these when selecting
944                 if (cmd.action != LFUN_MOUSE_MOTION) {
945                         owner_->updateLayoutChoice();
946                         owner_->updateToolbar();
947                 }
948
949                 // slight hack: this is only called currently when we
950                 // clicked somewhere, so we force through the display
951                 // of the new status here.
952                 owner_->clearMessage();
953                 return true;
954         }
955
956         case LFUN_FILE_OPEN:
957                 owner_->dispatch(cmd);
958                 return true;
959
960         default:
961                 BOOST_ASSERT(false);
962         }
963         return true;
964 }
965
966
967 bool BufferView::Pimpl::dispatch(FuncRequest const & cmd)
968 {
969         //lyxerr << "*** BufferView::Pimpl: request: " << cmd << std::endl;
970         // Make sure that the cached BufferView is correct.
971         lyxerr[Debug::ACTION] << "BufferView::Pimpl::Dispatch:"
972                 << " action[" << cmd.action << ']'
973                 << " arg[" << cmd.argument << ']'
974                 << " x[" << cmd.x << ']'
975                 << " y[" << cmd.y << ']'
976                 << " button[" << cmd.button() << ']'
977                 << endl;
978
979         LCursor & cur = bv_->cursor();
980
981         switch (cmd.action) {
982
983         case LFUN_UNDO:
984                 if (available()) {
985                         cur.message(_("Undo"));
986                         cur.clearSelection();
987                         if (!textUndo(*bv_))
988                                 cur.message(_("No further undo information"));
989                         update();
990                         switchKeyMap();
991                 }
992                 break;
993
994         case LFUN_REDO:
995                 if (available()) {
996                         cur.message(_("Redo"));
997                         cur.clearSelection();
998                         if (!textRedo(*bv_))
999                                 cur.message(_("No further redo information"));
1000                         update();
1001                         switchKeyMap();
1002                 }
1003                 break;
1004
1005         case LFUN_FILE_INSERT:
1006                 MenuInsertLyXFile(cmd.argument);
1007                 break;
1008
1009         case LFUN_FILE_INSERT_ASCII_PARA:
1010                 InsertAsciiFile(bv_, cmd.argument, true);
1011                 break;
1012
1013         case LFUN_FILE_INSERT_ASCII:
1014                 InsertAsciiFile(bv_, cmd.argument, false);
1015                 break;
1016
1017         case LFUN_FONT_STATE:
1018                 cur.message(cur.currentState());
1019                 break;
1020
1021         case LFUN_INSERT_LABEL: {
1022                 // Try and generate a valid label
1023                 string const contents = cmd.argument.empty() ?
1024                         cur.getPossibleLabel() : cmd.argument;
1025                 InsetCommandParams icp("label", contents);
1026                 string data = InsetCommandMailer::params2string("label", icp);
1027                 owner_->getDialogs().show("label", data, 0);
1028                 break;
1029         }
1030
1031         case LFUN_BOOKMARK_SAVE:
1032                 savePosition(strToUnsignedInt(cmd.argument));
1033                 break;
1034
1035         case LFUN_BOOKMARK_GOTO:
1036                 restorePosition(strToUnsignedInt(cmd.argument));
1037                 break;
1038
1039         case LFUN_REF_GOTO: {
1040                 string label = cmd.argument;
1041                 if (label.empty()) {
1042                         InsetRef * inset =
1043                                 static_cast<InsetRef*>(getInsetByCode(InsetBase::REF_CODE));
1044                         if (inset) {
1045                                 label = inset->getContents();
1046                                 savePosition(0);
1047                         }
1048                 }
1049
1050                 if (!label.empty())
1051                         bv_->gotoLabel(label);
1052                 break;
1053         }
1054
1055         case LFUN_TRACK_CHANGES:
1056                 trackChanges();
1057                 break;
1058
1059         case LFUN_MERGE_CHANGES:
1060                 owner_->getDialogs().show("changes");
1061                 break;
1062
1063         case LFUN_ACCEPT_ALL_CHANGES: {
1064                 bv_->cursor().reset();
1065 #warning FIXME changes
1066                 while (lyx::find::findNextChange(bv_))
1067                         bv_->getLyXText()->acceptChange(bv_->cursor());
1068                 update();
1069                 break;
1070         }
1071
1072         case LFUN_REJECT_ALL_CHANGES: {
1073                 bv_->cursor().reset();
1074 #warning FIXME changes
1075                 while (lyx::find::findNextChange(bv_))
1076                         bv_->getLyXText()->rejectChange(bv_->cursor());
1077                 update();
1078                 break;
1079         }
1080
1081         case LFUN_WORD_FIND:
1082                 lyx::find::find(bv_, cmd);
1083                 break;
1084
1085         case LFUN_WORD_REPLACE:
1086                 lyx::find::replace(bv_, cmd);
1087                 break;
1088
1089         case LFUN_MARK_OFF:
1090                 cur.clearSelection();
1091                 update();
1092                 cur.resetAnchor();
1093                 cur.message(N_("Mark off"));
1094                 break;
1095
1096         case LFUN_MARK_ON:
1097                 cur.clearSelection();
1098                 cur.mark() = true;
1099                 update();
1100                 cur.resetAnchor();
1101                 cur.message(N_("Mark on"));
1102                 break;
1103
1104         case LFUN_SETMARK:
1105                 cur.clearSelection();
1106                 if (cur.mark()) {
1107                         cur.mark() = false;
1108                         cur.message(N_("Mark removed"));
1109                 } else {
1110                         cur.mark() = true;
1111                         cur.message(N_("Mark set"));
1112                 }
1113                 cur.resetAnchor();
1114                 update();
1115                 break;
1116
1117         case LFUN_UNKNOWN_ACTION:
1118                 cur.errorMessage(N_("Unknown function!"));
1119                 break;
1120
1121         case LFUN_CENTER:
1122                 bv_->center();
1123                 break;
1124
1125         case LFUN_BEGINNINGBUFSEL:
1126                 bv_->cursor().reset();
1127                 if (!cur.selection())
1128                         cur.resetAnchor();
1129                 bv_->text()->cursorTop(cur);
1130                 finishUndo();
1131                 break;
1132
1133         case LFUN_ENDBUFSEL:
1134                 bv_->cursor().reset();
1135                 if (!cur.selection())
1136                         cur.resetAnchor();
1137                 bv_->text()->cursorBottom(cur);
1138                 finishUndo();
1139                 break;
1140
1141         default:
1142                 return false;
1143         }
1144
1145         return true;
1146 }