]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/GuiView.cpp
Prevent two more assertions due to non-absolute filename as a parameter of file-inser...
[lyx.git] / src / frontends / qt4 / GuiView.cpp
1 /**
2  * \file GuiView.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Lars Gullik Bjønnes
7  * \author John Levon
8  * \author Abdelrazak Younes
9  * \author Peter Kümmel
10  *
11  * Full author contact details are available in file CREDITS.
12  */
13
14 #include <config.h>
15
16 #include "GuiView.h"
17
18 #include "Dialog.h"
19 #include "FileDialog.h"
20 #include "FontLoader.h"
21 #include "GuiApplication.h"
22 #include "GuiCommandBuffer.h"
23 #include "GuiCompleter.h"
24 #include "GuiWorkArea.h"
25 #include "GuiKeySymbol.h"
26 #include "GuiToc.h"
27 #include "GuiToolbar.h"
28 #include "Menus.h"
29 #include "TocModel.h"
30
31 #include "qt_helpers.h"
32
33 #include "frontends/alert.h"
34
35 #include "buffer_funcs.h"
36 #include "Buffer.h"
37 #include "BufferList.h"
38 #include "BufferParams.h"
39 #include "BufferView.h"
40 #include "Converter.h"
41 #include "Cursor.h"
42 #include "Encoding.h"
43 #include "ErrorList.h"
44 #include "Format.h"
45 #include "FuncStatus.h"
46 #include "FuncRequest.h"
47 #include "Intl.h"
48 #include "Layout.h"
49 #include "Lexer.h"
50 #include "LyXFunc.h"
51 #include "LyX.h"
52 #include "LyXRC.h"
53 #include "LyXVC.h"
54 #include "Paragraph.h"
55 #include "TextClass.h"
56 #include "Text.h"
57 #include "Toolbars.h"
58 #include "version.h"
59
60 #include "support/convert.h"
61 #include "support/debug.h"
62 #include "support/ExceptionMessage.h"
63 #include "support/FileName.h"
64 #include "support/filetools.h"
65 #include "support/gettext.h"
66 #include "support/ForkedCalls.h"
67 #include "support/lassert.h"
68 #include "support/lstrings.h"
69 #include "support/os.h"
70 #include "support/Package.h"
71 #include "support/Timeout.h"
72
73 #include <QAction>
74 #include <QApplication>
75 #include <QCloseEvent>
76 #include <QDebug>
77 #include <QDesktopWidget>
78 #include <QDragEnterEvent>
79 #include <QDropEvent>
80 #include <QList>
81 #include <QMenu>
82 #include <QMenuBar>
83 #include <QPainter>
84 #include <QPixmap>
85 #include <QPixmapCache>
86 #include <QPoint>
87 #include <QPushButton>
88 #include <QSettings>
89 #include <QShowEvent>
90 #include <QSplitter>
91 #include <QStackedWidget>
92 #include <QStatusBar>
93 #include <QTimer>
94 #include <QToolBar>
95 #include <QUrl>
96 #include <QScrollBar>
97
98 #include <boost/bind.hpp>
99
100 #ifdef HAVE_SYS_TIME_H
101 # include <sys/time.h>
102 #endif
103 #ifdef HAVE_UNISTD_H
104 # include <unistd.h>
105 #endif
106
107 using namespace std;
108 using namespace lyx::support;
109
110 namespace lyx {
111 namespace frontend {
112
113 namespace {
114
115 class BackgroundWidget : public QWidget
116 {
117 public:
118         BackgroundWidget()
119         {
120                 LYXERR(Debug::GUI, "show banner: " << lyxrc.show_banner);
121                 /// The text to be written on top of the pixmap
122                 QString const text = lyx_version ?
123                         qt_("version ") + lyx_version : qt_("unknown version");
124                 splash_ = getPixmap("images/", "banner", "png");
125
126                 QPainter pain(&splash_);
127                 pain.setPen(QColor(0, 0, 0));
128                 QFont font;
129                 // The font used to display the version info
130                 font.setStyleHint(QFont::SansSerif);
131                 font.setWeight(QFont::Bold);
132                 font.setPointSize(int(toqstr(lyxrc.font_sizes[FONT_SIZE_LARGE]).toDouble()));
133                 pain.setFont(font);
134                 pain.drawText(260, 15, text);
135         }
136
137         void paintEvent(QPaintEvent *)
138         {
139                 int x = (width() - splash_.width()) / 2;
140                 int y = (height() - splash_.height()) / 2;
141                 QPainter pain(this);
142                 pain.drawPixmap(x, y, splash_);
143         }
144
145 private:
146         QPixmap splash_;
147 };
148
149
150 /// Toolbar store providing access to individual toolbars by name.
151 typedef std::map<std::string, GuiToolbar *> ToolbarMap;
152
153 typedef boost::shared_ptr<Dialog> DialogPtr;
154
155 } // namespace anon
156
157
158 struct GuiView::GuiViewPrivate
159 {
160         GuiViewPrivate()
161                 : current_work_area_(0), current_main_work_area_(0),
162                 layout_(0), autosave_timeout_(5000),
163                 in_show_(false)
164         {
165                 // hardcode here the platform specific icon size
166                 smallIconSize = 14;  // scaling problems
167                 normalIconSize = 20; // ok, default
168                 bigIconSize = 26;    // better for some math icons
169
170                 splitter_ = new QSplitter;
171                 bg_widget_ = new BackgroundWidget;
172                 stack_widget_ = new QStackedWidget;
173                 stack_widget_->addWidget(bg_widget_);
174                 stack_widget_->addWidget(splitter_);
175                 setBackground();
176         }
177
178         ~GuiViewPrivate()
179         {
180                 delete splitter_;
181                 delete bg_widget_;
182                 delete stack_widget_;
183         }
184
185         QMenu * toolBarPopup(GuiView * parent)
186         {
187                 // FIXME: translation
188                 QMenu * menu = new QMenu(parent);
189                 QActionGroup * iconSizeGroup = new QActionGroup(parent);
190
191                 QAction * smallIcons = new QAction(iconSizeGroup);
192                 smallIcons->setText(qt_("Small-sized icons"));
193                 smallIcons->setCheckable(true);
194                 QObject::connect(smallIcons, SIGNAL(triggered()),
195                         parent, SLOT(smallSizedIcons()));
196                 menu->addAction(smallIcons);
197
198                 QAction * normalIcons = new QAction(iconSizeGroup);
199                 normalIcons->setText(qt_("Normal-sized icons"));
200                 normalIcons->setCheckable(true);
201                 QObject::connect(normalIcons, SIGNAL(triggered()),
202                         parent, SLOT(normalSizedIcons()));
203                 menu->addAction(normalIcons);
204
205                 QAction * bigIcons = new QAction(iconSizeGroup);
206                 bigIcons->setText(qt_("Big-sized icons"));
207                 bigIcons->setCheckable(true);
208                 QObject::connect(bigIcons, SIGNAL(triggered()),
209                         parent, SLOT(bigSizedIcons()));
210                 menu->addAction(bigIcons);
211
212                 unsigned int cur = parent->iconSize().width();
213                 if ( cur == parent->d.smallIconSize)
214                         smallIcons->setChecked(true);
215                 else if (cur == parent->d.normalIconSize)
216                         normalIcons->setChecked(true);
217                 else if (cur == parent->d.bigIconSize)
218                         bigIcons->setChecked(true);
219
220                 return menu;
221         }
222
223         void setBackground()
224         {
225                 stack_widget_->setCurrentWidget(bg_widget_);
226                 bg_widget_->setUpdatesEnabled(true);
227         }
228
229         TabWorkArea * tabWorkArea(int i)
230         {
231                 return dynamic_cast<TabWorkArea *>(splitter_->widget(i));
232         }
233
234         TabWorkArea * currentTabWorkArea()
235         {
236                 if (splitter_->count() == 1)
237                         // The first TabWorkArea is always the first one, if any.
238                         return tabWorkArea(0);
239
240                 for (int i = 0; i != splitter_->count(); ++i) {
241                         TabWorkArea * twa = tabWorkArea(i);
242                         if (current_main_work_area_ == twa->currentWorkArea())
243                                 return twa;
244                 }
245
246                 // None has the focus so we just take the first one.
247                 return tabWorkArea(0);
248         }
249
250 public:
251         GuiWorkArea * current_work_area_;
252         GuiWorkArea * current_main_work_area_;
253         QSplitter * splitter_;
254         QStackedWidget * stack_widget_;
255         BackgroundWidget * bg_widget_;
256         /// view's toolbars
257         ToolbarMap toolbars_;
258         /// The main layout box.
259         /** 
260          * \warning Don't Delete! The layout box is actually owned by
261          * whichever toolbar contains it. All the GuiView class needs is a
262          * means of accessing it.
263          *
264          * FIXME: replace that with a proper model so that we are not limited
265          * to only one dialog.
266          */
267         GuiLayoutBox * layout_;
268
269         ///
270         map<string, Inset *> open_insets_;
271
272         ///
273         map<string, DialogPtr> dialogs_;
274
275         unsigned int smallIconSize;
276         unsigned int normalIconSize;
277         unsigned int bigIconSize;
278         ///
279         QTimer statusbar_timer_;
280         /// auto-saving of buffers
281         Timeout autosave_timeout_;
282         /// flag against a race condition due to multiclicks, see bug #1119
283         bool in_show_;
284
285         ///
286         TocModels toc_models_;
287 };
288
289
290 GuiView::GuiView(int id)
291         : d(*new GuiViewPrivate), id_(id), closing_(false)
292 {
293         // GuiToolbars *must* be initialised before the menu bar.
294         normalSizedIcons(); // at least on Mac the default is 32 otherwise, which is huge
295         constructToolbars();
296
297         // set ourself as the current view. This is needed for the menu bar
298         // filling, at least for the static special menu item on Mac. Otherwise
299         // they are greyed out.
300         theLyXFunc().setLyXView(this);
301         
302         // Fill up the menu bar.
303         guiApp->menus().fillMenuBar(menuBar(), this, true);
304
305         setCentralWidget(d.stack_widget_);
306
307         // Start autosave timer
308         if (lyxrc.autosave) {
309                 d.autosave_timeout_.timeout.connect(boost::bind(&GuiView::autoSave, this));
310                 d.autosave_timeout_.setTimeout(lyxrc.autosave * 1000);
311                 d.autosave_timeout_.start();
312         }
313         connect(&d.statusbar_timer_, SIGNAL(timeout()),
314                 this, SLOT(clearMessage()));
315
316         // We don't want to keep the window in memory if it is closed.
317         setAttribute(Qt::WA_DeleteOnClose, true);
318
319 #if (!defined(Q_WS_WIN) && !defined(Q_WS_MACX))
320         // assign an icon to main form. We do not do it under Qt/Win or Qt/Mac,
321         // since the icon is provided in the application bundle.
322         setWindowIcon(getPixmap("images/", "lyx", "png"));
323 #endif
324
325         // For Drag&Drop.
326         setAcceptDrops(true);
327
328         statusBar()->setSizeGripEnabled(true);
329
330         // Forbid too small unresizable window because it can happen
331         // with some window manager under X11.
332         setMinimumSize(300, 200);
333
334         if (lyxrc.allow_geometry_session) {
335                 // Now take care of session management.
336                 if (restoreLayout())
337                         return;
338         }
339
340         // no session handling, default to a sane size.
341         setGeometry(50, 50, 690, 510);
342         initToolbars();
343
344         // clear session data if any.
345         QSettings settings;
346         settings.remove("views");
347 }
348
349
350 GuiView::~GuiView()
351 {
352         delete &d;
353 }
354
355
356 void GuiView::saveLayout() const
357 {
358         QSettings settings;
359         settings.beginGroup("views");
360         settings.beginGroup(QString::number(id_));
361 #ifdef Q_WS_X11
362         settings.setValue("pos", pos());
363         settings.setValue("size", size());
364 #else
365         settings.setValue("geometry", saveGeometry());
366 #endif
367         settings.setValue("layout", saveState(0));
368         settings.setValue("icon_size", iconSize());
369 }
370
371
372 bool GuiView::restoreLayout()
373 {
374         QSettings settings;
375         settings.beginGroup("views");
376         settings.beginGroup(QString::number(id_));
377         QString const icon_key = "icon_size";
378         if (!settings.contains(icon_key))
379                 return false;
380
381         setIconSize(settings.value(icon_key).toSize());
382 #ifdef Q_WS_X11
383         QPoint pos = settings.value("pos", QPoint(50, 50)).toPoint();
384         QSize size = settings.value("size", QSize(690, 510)).toSize();
385         resize(size);
386         move(pos);
387 #else
388         if (!restoreGeometry(settings.value("geometry").toByteArray()))
389                 setGeometry(50, 50, 690, 510);
390 #endif
391         // Make sure layout is correctly oriented.
392         setLayoutDirection(qApp->layoutDirection());
393
394         // Allow the toc and view-source dock widget to be restored if needed.
395         Dialog *d;
396         if ((d = findOrBuild("toc", true)))
397                 // see bug 5082
398                 d->showView();
399         if ((d = findOrBuild("view-source", true)))
400                 d->showView();
401
402         if (!restoreState(settings.value("layout").toByteArray(), 0))
403                 initToolbars();
404         updateDialogs();
405         return true;
406 }
407
408
409 GuiToolbar * GuiView::toolbar(string const & name)
410 {
411         ToolbarMap::iterator it = d.toolbars_.find(name);
412         if (it != d.toolbars_.end())
413                 return it->second;
414
415         LYXERR(Debug::GUI, "Toolbar::display: no toolbar named " << name);
416         message(bformat(_("Unknown toolbar \"%1$s\""), from_utf8(name)));
417         return 0;
418 }
419
420
421 void GuiView::constructToolbars()
422 {
423         ToolbarMap::iterator it = d.toolbars_.begin();
424         for (; it != d.toolbars_.end(); ++it)
425                 delete it->second;
426         d.toolbars_.clear();
427         d.layout_ = 0;
428
429         // extracts the toolbars from the backend
430         Toolbars::Infos::iterator cit = guiApp->toolbars().begin();
431         Toolbars::Infos::iterator end = guiApp->toolbars().end();
432         for (; cit != end; ++cit)
433                 d.toolbars_[cit->name] =  new GuiToolbar(*cit, *this);
434 }
435
436
437 void GuiView::initToolbars()
438 {
439         // extracts the toolbars from the backend
440         Toolbars::Infos::iterator cit = guiApp->toolbars().begin();
441         Toolbars::Infos::iterator end = guiApp->toolbars().end();
442         for (; cit != end; ++cit) {
443                 GuiToolbar * tb = toolbar(cit->name);
444                 if (!tb)
445                         continue;
446                 int const visibility = guiApp->toolbars().defaultVisibility(cit->name);
447                 bool newline = true;
448                 tb->setVisible(false);
449                 tb->setVisibility(visibility);
450
451                 if (visibility & Toolbars::TOP) {
452                         if (newline)
453                                 addToolBarBreak(Qt::TopToolBarArea);
454                         addToolBar(Qt::TopToolBarArea, tb);
455                 }
456
457                 if (visibility & Toolbars::BOTTOM) {
458                         // Qt < 4.2.2 cannot handle ToolBarBreak on non-TOP dock.
459 #if (QT_VERSION >= 0x040202)
460                         addToolBarBreak(Qt::BottomToolBarArea);
461 #endif
462                         addToolBar(Qt::BottomToolBarArea, tb);
463                 }
464
465                 if (visibility & Toolbars::LEFT) {
466                         // Qt < 4.2.2 cannot handle ToolBarBreak on non-TOP dock.
467 #if (QT_VERSION >= 0x040202)
468                         addToolBarBreak(Qt::LeftToolBarArea);
469 #endif
470                         addToolBar(Qt::LeftToolBarArea, tb);
471                 }
472
473                 if (visibility & Toolbars::RIGHT) {
474                         // Qt < 4.2.2 cannot handle ToolBarBreak on non-TOP dock.
475 #if (QT_VERSION >= 0x040202)
476                         addToolBarBreak(Qt::RightToolBarArea);
477 #endif
478                         addToolBar(Qt::RightToolBarArea, tb);
479                 }
480
481                 if (visibility & Toolbars::ON)
482                         tb->setVisible(true);
483         }
484 }
485
486
487 TocModels & GuiView::tocModels()
488 {
489         return d.toc_models_;
490 }
491
492
493 void GuiView::setFocus()
494 {
495         LYXERR(Debug::DEBUG, "GuiView::setFocus()" << this);
496         // Make sure LyXFunc points to the correct view.
497         guiApp->setCurrentView(this);
498         theLyXFunc().setLyXView(this);
499         QMainWindow::setFocus();
500         if (d.current_work_area_)
501                 d.current_work_area_->setFocus();
502 }
503
504
505 QMenu * GuiView::createPopupMenu()
506 {
507         return d.toolBarPopup(this);
508 }
509
510
511 void GuiView::showEvent(QShowEvent * e)
512 {
513         LYXERR(Debug::GUI, "Passed Geometry "
514                 << size().height() << "x" << size().width()
515                 << "+" << pos().x() << "+" << pos().y());
516
517         if (d.splitter_->count() == 0)
518                 // No work area, switch to the background widget.
519                 d.setBackground();
520
521         QMainWindow::showEvent(e);
522 }
523
524
525 /** Destroy only all tabbed WorkAreas. Destruction of other WorkAreas
526  ** is responsibility of the container (e.g., dialog)
527  **/
528 void GuiView::closeEvent(QCloseEvent * close_event)
529 {
530         LYXERR(Debug::DEBUG, "GuiView::closeEvent()");
531         closing_ = true;
532
533         // it can happen that this event arrives without selecting the view,
534         // e.g. when clicking the close button on a background window.
535         setFocus();
536         GuiWorkArea * active_wa = currentMainWorkArea();
537         setCurrentWorkArea(active_wa);
538
539         int splitter_count = d.splitter_->count();
540         for (; splitter_count; --splitter_count) {
541                 TabWorkArea * twa = d.tabWorkArea(0);
542                                 
543                 int twa_count = twa->count();
544                 for (; twa_count; --twa_count) {
545                         twa->setCurrentIndex(twa_count-1);
546
547                         GuiWorkArea * wa = twa->currentWorkArea();
548                         bool const is_active_wa = active_wa == wa;
549                         Buffer * b = &wa->bufferView().buffer();
550                         if (b->parent()) {
551                                 // This is a child document, just close the tab
552                                 // after saving but keep the file loaded.
553                                 if (!closeBuffer(*b, true, is_active_wa)) {
554                                         closing_ = false;
555                                         close_event->ignore();
556                                         return;
557                                 }
558                                 continue;
559                         }
560
561                         vector<Buffer *> clist = b->getChildren();
562                         for (vector<Buffer *>::const_iterator it = clist.begin();
563                                  it != clist.end(); ++it) {
564                                 if ((*it)->isClean())
565                                         continue;
566                                 Buffer * c = *it;
567                                 // If a child is dirty, do not close
568                                 // without user intervention
569                                 if (!closeBuffer(*c, false)) {
570                                         closing_ = false;
571                                         close_event->ignore();
572                                         return;
573                                 }
574                         }
575
576                         QList<int> const ids = guiApp->viewIds();
577                         for (int i = 0; i != ids.size(); ++i) {
578                                 if (id_ == ids[i])
579                                         continue;
580                                 if (guiApp->view(ids[i]).workArea(*b)) {
581                                         // FIXME 1: should we put an alert box here
582                                         // that the buffer is viewed elsewhere?
583                                         // FIXME 2: should we try to save this buffer in any case?
584                                         //saveBuffer(b);
585
586                                         // This buffer is also opened in another view, so
587                                         // close the associated work area...
588                                         removeWorkArea(wa);
589                                         // ... but don't close the buffer.
590                                         b = 0;
591                                         break;
592                                 }
593                         }
594                         // closeBuffer() needs buffer workArea still alive and
595                         // set as currrent one, and destroys it
596                         if (b && !closeBuffer(*b, true, is_active_wa)) {
597                                 closing_ = false;
598                                 close_event->ignore();
599                                 return;
600                         }
601                 }
602         }
603         // Make sure that nothing will use this close to be closed View.
604         guiApp->unregisterView(this);
605
606         if (isFullScreen()) {
607                 // Switch off fullscreen before closing.
608                 toggleFullScreen();
609                 updateDialogs();
610         }
611
612         // Make sure the timer time out will not trigger a statusbar update.
613         d.statusbar_timer_.stop();
614
615         // Saving fullscreen requires additional tweaks in the toolbar code.
616         // It wouldn't also work under linux natively.
617         if (lyxrc.allow_geometry_session) {
618                 // Save this window geometry and layout.
619                 saveLayout();
620                 // Then the toolbar private states.
621                 ToolbarMap::iterator end = d.toolbars_.end();
622                 for (ToolbarMap::iterator it = d.toolbars_.begin(); it != end; ++it)
623                         it->second->saveSession();
624                 // Now take care of all other dialogs:
625                 map<string, DialogPtr>::const_iterator it = d.dialogs_.begin();
626                 for (; it!= d.dialogs_.end(); ++it)
627                         it->second->saveSession();
628         }
629
630         close_event->accept();
631 }
632
633
634 void GuiView::dragEnterEvent(QDragEnterEvent * event)
635 {
636         if (event->mimeData()->hasUrls())
637                 event->accept();
638         /// \todo Ask lyx-devel is this is enough:
639         /// if (event->mimeData()->hasFormat("text/plain"))
640         ///     event->acceptProposedAction();
641 }
642
643
644 void GuiView::dropEvent(QDropEvent * event)
645 {
646         QList<QUrl> files = event->mimeData()->urls();
647         if (files.isEmpty())
648                 return;
649
650         LYXERR(Debug::GUI, "GuiView::dropEvent: got URLs!");
651         for (int i = 0; i != files.size(); ++i) {
652                 string const file = os::internal_path(fromqstr(
653                         files.at(i).toLocalFile()));
654                 if (!file.empty()) {
655                         // Asynchronously post the event. DropEvent usually come
656                         // from the BufferView. But reloading a file might close
657                         // the BufferView from within its own event handler.
658                         guiApp->dispatchDelayed(FuncRequest(LFUN_FILE_OPEN, file));
659                         event->accept();
660                 }
661         }
662 }
663
664
665 void GuiView::message(docstring const & str)
666 {
667         if (ForkedProcess::iAmAChild())
668                 return;
669
670         statusBar()->showMessage(toqstr(str));
671         d.statusbar_timer_.stop();
672         d.statusbar_timer_.start(3000);
673 }
674
675
676 void GuiView::smallSizedIcons()
677 {
678         setIconSize(QSize(d.smallIconSize, d.smallIconSize));
679 }
680
681
682 void GuiView::normalSizedIcons()
683 {
684         setIconSize(QSize(d.normalIconSize, d.normalIconSize));
685 }
686
687
688 void GuiView::bigSizedIcons()
689 {
690         setIconSize(QSize(d.bigIconSize, d.bigIconSize));
691 }
692
693
694 void GuiView::clearMessage()
695 {
696         if (!hasFocus())
697                 return;
698         theLyXFunc().setLyXView(this);
699         statusBar()->showMessage(toqstr(theLyXFunc().viewStatusMessage()));
700         d.statusbar_timer_.stop();
701 }
702
703
704 void GuiView::updateWindowTitle(GuiWorkArea * wa)
705 {
706         if (wa != d.current_work_area_)
707                 return;
708         setWindowTitle(qt_("LyX: ") + wa->windowTitle());
709         setWindowIconText(wa->windowIconText());
710 }
711
712
713 void GuiView::on_currentWorkAreaChanged(GuiWorkArea * wa)
714 {
715         disconnectBuffer();
716         disconnectBufferView();
717         connectBufferView(wa->bufferView());
718         connectBuffer(wa->bufferView().buffer());
719         d.current_work_area_ = wa;
720         QObject::connect(wa, SIGNAL(titleChanged(GuiWorkArea *)),
721                 this, SLOT(updateWindowTitle(GuiWorkArea *)));
722         updateWindowTitle(wa);
723
724         structureChanged();
725
726         // The document settings needs to be reinitialised.
727         updateDialog("document", "");
728
729         // Buffer-dependent dialogs must be updated. This is done here because
730         // some dialogs require buffer()->text.
731         updateDialogs();
732 }
733
734
735 void GuiView::on_lastWorkAreaRemoved()
736 {
737         if (closing_)
738                 // We already are in a close event. Nothing more to do.
739                 return;
740
741         if (d.splitter_->count() > 1)
742                 // We have a splitter so don't close anything.
743                 return;
744
745         // Reset and updates the dialogs.
746         d.toc_models_.reset(0);
747         updateDialog("document", "");
748         updateDialogs();
749
750         resetWindowTitleAndIconText();
751
752         if (lyxrc.open_buffers_in_tabs)
753                 // Nothing more to do, the window should stay open.
754                 return;
755
756         if (guiApp->viewIds().size() > 1) {
757                 close();
758                 return;
759         }
760
761 #ifdef Q_WS_MACX
762         // On Mac we also close the last window because the application stay
763         // resident in memory. On other platforms we don't close the last
764         // window because this would quit the application.
765         close();
766 #endif
767 }
768
769
770 void GuiView::updateStatusBar()
771 {
772         // let the user see the explicit message
773         if (d.statusbar_timer_.isActive())
774                 return;
775
776         theLyXFunc().setLyXView(this);
777         statusBar()->showMessage(toqstr(theLyXFunc().viewStatusMessage()));
778 }
779
780
781 bool GuiView::hasFocus() const
782 {
783         return qApp->activeWindow() == this;
784 }
785
786
787 bool GuiView::event(QEvent * e)
788 {
789         switch (e->type())
790         {
791         // Useful debug code:
792         //case QEvent::ActivationChange:
793         //case QEvent::WindowDeactivate:
794         //case QEvent::Paint:
795         //case QEvent::Enter:
796         //case QEvent::Leave:
797         //case QEvent::HoverEnter:
798         //case QEvent::HoverLeave:
799         //case QEvent::HoverMove:
800         //case QEvent::StatusTip:
801         //case QEvent::DragEnter:
802         //case QEvent::DragLeave:
803         //case QEvent::Drop:
804         //      break;
805
806         case QEvent::WindowActivate: {
807                 if (this == guiApp->currentView()) {
808                         setFocus();
809                         return QMainWindow::event(e);
810                 }
811                 guiApp->setCurrentView(this);
812                 theLyXFunc().setLyXView(this);
813                 if (d.current_work_area_) {
814                         BufferView & bv = d.current_work_area_->bufferView();
815                         connectBufferView(bv);
816                         connectBuffer(bv.buffer());
817                         // The document structure, name and dialogs might have
818                         // changed in another view.
819                         structureChanged();
820                         // The document settings needs to be reinitialised.
821                         updateDialog("document", "");
822                         updateDialogs();
823                 } else {
824                         resetWindowTitleAndIconText();
825                 }
826                 setFocus();
827                 return QMainWindow::event(e);
828         }
829
830         case QEvent::ShortcutOverride: {
831
832 // See bug 4888
833 #if (!defined Q_WS_X11) || (QT_VERSION >= 0x040500)
834                 if (isFullScreen() && menuBar()->isHidden()) {
835                         QKeyEvent * ke = static_cast<QKeyEvent*>(e);
836                         // FIXME: we should also try to detect special LyX shortcut such as
837                         // Alt-P and Alt-M. Right now there is a hack in
838                         // GuiWorkArea::processKeySym() that hides again the menubar for
839                         // those cases.
840                         if (ke->modifiers() & Qt::AltModifier && ke->key() != Qt::Key_Alt) {
841                                 menuBar()->show();
842                                 return QMainWindow::event(e);
843                         }
844                 }
845 #endif
846
847                 if (d.current_work_area_)
848                         // Nothing special to do.
849                         return QMainWindow::event(e);
850
851                 QKeyEvent * ke = static_cast<QKeyEvent*>(e);
852                 // Let Qt handle menu access and the Tab keys to navigate keys to navigate
853                 // between controls.
854                 if (ke->modifiers() & Qt::AltModifier || ke->key() == Qt::Key_Tab 
855                         || ke->key() == Qt::Key_Backtab)
856                         return QMainWindow::event(e);
857
858                 // Allow processing of shortcuts that are allowed even when no Buffer
859                 // is viewed.
860                 theLyXFunc().setLyXView(this);
861                 KeySymbol sym;
862                 setKeySymbol(&sym, ke);
863                 theLyXFunc().processKeySym(sym, q_key_state(ke->modifiers()));
864                 e->accept();
865                 return true;
866         }
867
868         default:
869                 return QMainWindow::event(e);
870         }
871 }
872
873 void GuiView::resetWindowTitleAndIconText()
874 {
875     setWindowTitle(qt_("LyX"));
876     setWindowIconText(qt_("LyX"));
877 }
878
879 bool GuiView::focusNextPrevChild(bool /*next*/)
880 {
881         setFocus();
882         return true;
883 }
884
885
886 void GuiView::setBusy(bool busy)
887 {
888         if (d.current_work_area_) {
889                 d.current_work_area_->setUpdatesEnabled(!busy);
890                 if (busy)
891                         d.current_work_area_->stopBlinkingCursor();
892                 else
893                         d.current_work_area_->startBlinkingCursor();
894         }
895
896         if (busy)
897                 QApplication::setOverrideCursor(Qt::WaitCursor);
898         else
899                 QApplication::restoreOverrideCursor();
900 }
901
902
903 GuiWorkArea * GuiView::workArea(Buffer & buffer)
904 {
905         if (currentWorkArea()
906             && &currentWorkArea()->bufferView().buffer() == &buffer)
907                 return (GuiWorkArea *) currentWorkArea();
908         if (TabWorkArea * twa = d.currentTabWorkArea())
909                 return twa->workArea(buffer);
910         return 0;
911 }
912
913
914 GuiWorkArea * GuiView::addWorkArea(Buffer & buffer)
915 {
916         // Automatically create a TabWorkArea if there are none yet.
917         TabWorkArea * tab_widget = d.splitter_->count() 
918                 ? d.currentTabWorkArea() : addTabWorkArea();
919         return tab_widget->addWorkArea(buffer, *this);
920 }
921
922
923 TabWorkArea * GuiView::addTabWorkArea()
924 {
925         TabWorkArea * twa = new TabWorkArea;
926         QObject::connect(twa, SIGNAL(currentWorkAreaChanged(GuiWorkArea *)),
927                 this, SLOT(on_currentWorkAreaChanged(GuiWorkArea *)));
928         QObject::connect(twa, SIGNAL(lastWorkAreaRemoved()),
929                          this, SLOT(on_lastWorkAreaRemoved()));
930
931         d.splitter_->addWidget(twa);
932         d.stack_widget_->setCurrentWidget(d.splitter_);
933         return twa;
934 }
935
936
937 GuiWorkArea const * GuiView::currentWorkArea() const
938 {
939         return d.current_work_area_;
940 }
941
942
943 GuiWorkArea * GuiView::currentWorkArea()
944 {
945         return d.current_work_area_;
946 }
947
948
949 GuiWorkArea const * GuiView::currentMainWorkArea() const
950 {
951         if (d.currentTabWorkArea() == NULL)
952                 return NULL;
953         return d.currentTabWorkArea()->currentWorkArea();
954 }
955
956
957 GuiWorkArea * GuiView::currentMainWorkArea()
958 {
959         if (d.currentTabWorkArea() == NULL)
960                 return NULL;
961         return d.currentTabWorkArea()->currentWorkArea();
962 }
963
964
965 void GuiView::setCurrentWorkArea(GuiWorkArea * wa)
966 {
967         LYXERR(Debug::DEBUG, "Setting current wa: " << wa << endl);
968         if (wa == NULL) {
969                 d.current_work_area_ = NULL;
970                 d.setBackground();
971                 return;
972         }
973         GuiWorkArea * old_gwa = theGuiApp()->currentView()->currentWorkArea();
974         if (old_gwa == wa)
975                 return;
976
977         theGuiApp()->setCurrentView(this);
978         d.current_work_area_ = wa;
979         for (int i = 0; i != d.splitter_->count(); ++i) {
980                 if (d.tabWorkArea(i)->setCurrentWorkArea(wa)) {
981                         //if (d.current_main_work_area_)
982                         //      d.current_main_work_area_->setFrameStyle(QFrame::NoFrame);
983                         d.current_main_work_area_ = wa;
984                         //d.current_main_work_area_->setFrameStyle(QFrame::Box | QFrame::Plain);
985                         //d.current_main_work_area_->setLineWidth(2);
986                         LYXERR(Debug::DEBUG, "Current wa: " << currentWorkArea() << ", Current main wa: " << currentMainWorkArea());
987                         return;
988                 }
989         }
990         LYXERR(Debug::DEBUG, "This is not a tabbed wa");
991         on_currentWorkAreaChanged(wa);
992         BufferView & bv = wa->bufferView();
993         bv.cursor().fixIfBroken();
994         bv.updateMetrics();
995         wa->setUpdatesEnabled(true);
996         LYXERR(Debug::DEBUG, "Current wa: " << currentWorkArea() << ", Current main wa: " << currentMainWorkArea());
997 }
998
999
1000 void GuiView::removeWorkArea(GuiWorkArea * wa)
1001 {
1002         LASSERT(wa, return);
1003         if (wa == d.current_work_area_) {
1004                 disconnectBuffer();
1005                 disconnectBufferView();
1006                 d.current_work_area_ = 0;
1007                 d.current_main_work_area_ = 0;
1008         }
1009
1010         bool found_twa = false;
1011         for (int i = 0; i != d.splitter_->count(); ++i) {
1012                 TabWorkArea * twa = d.tabWorkArea(i);
1013                 if (twa->removeWorkArea(wa)) {
1014                         // Found in this tab group, and deleted the GuiWorkArea.
1015                         found_twa = true;
1016                         if (twa->count() != 0) {
1017                                 if (d.current_work_area_ == 0)
1018                                         // This means that we are closing the current GuiWorkArea, so
1019                                         // switch to the next GuiWorkArea in the found TabWorkArea.
1020                                         setCurrentWorkArea(twa->currentWorkArea());
1021                         } else {
1022                                 // No more WorkAreas in this tab group, so delete it.
1023                                 delete twa;
1024                         }
1025                         break;
1026                 }
1027         }
1028
1029         // It is not a tabbed work area (i.e., the search work area), so it
1030         // should be deleted by other means.
1031         LASSERT(found_twa, /* */);
1032
1033         if (d.current_work_area_ == 0) {
1034                 if (d.splitter_->count() != 0) {
1035                         TabWorkArea * twa = d.currentTabWorkArea();
1036                         setCurrentWorkArea(twa->currentWorkArea());
1037                 } else {
1038                         // No more work areas, switch to the background widget.
1039                         setCurrentWorkArea(0);
1040                 }
1041         }
1042 }
1043
1044
1045 void GuiView::setLayoutDialog(GuiLayoutBox * layout)
1046 {
1047         d.layout_ = layout;
1048 }
1049
1050
1051 void GuiView::updateLayoutList()
1052 {
1053         if (d.layout_)
1054                 d.layout_->updateContents(false);
1055 }
1056
1057
1058 void GuiView::updateToolbars()
1059 {
1060         ToolbarMap::iterator end = d.toolbars_.end();
1061         if (d.current_work_area_) {
1062                 bool const math =
1063                         d.current_work_area_->bufferView().cursor().inMathed();
1064                 bool const table =
1065                         lyx::getStatus(FuncRequest(LFUN_LAYOUT_TABULAR)).enabled();
1066                 bool const review =
1067                         lyx::getStatus(FuncRequest(LFUN_CHANGES_TRACK)).enabled() &&
1068                         lyx::getStatus(FuncRequest(LFUN_CHANGES_TRACK)).onoff(true);
1069                 bool const mathmacrotemplate =
1070                         lyx::getStatus(FuncRequest(LFUN_IN_MATHMACROTEMPLATE)).enabled();
1071
1072                 for (ToolbarMap::iterator it = d.toolbars_.begin(); it != end; ++it)
1073                         it->second->update(math, table, review, mathmacrotemplate);
1074         } else
1075                 for (ToolbarMap::iterator it = d.toolbars_.begin(); it != end; ++it)
1076                         it->second->update(false, false, false, false);
1077 }
1078
1079
1080 Buffer * GuiView::buffer()
1081 {
1082         if (d.current_work_area_)
1083                 return &d.current_work_area_->bufferView().buffer();
1084         return 0;
1085 }
1086
1087
1088 Buffer const * GuiView::buffer() const
1089 {
1090         if (d.current_work_area_)
1091                 return &d.current_work_area_->bufferView().buffer();
1092         return 0;
1093 }
1094
1095
1096 void GuiView::setBuffer(Buffer * newBuffer)
1097 {
1098         LYXERR(Debug::DEBUG, "Setting buffer: " << newBuffer << std::endl);
1099         LASSERT(newBuffer, return);
1100         setBusy(true);
1101
1102         GuiWorkArea * wa = workArea(*newBuffer);
1103         if (wa == 0) {
1104                 newBuffer->masterBuffer()->updateLabels();
1105                 wa = addWorkArea(*newBuffer);
1106         } else {
1107                 //Disconnect the old buffer...there's no new one.
1108                 disconnectBuffer();
1109         }
1110         connectBuffer(*newBuffer);
1111         connectBufferView(wa->bufferView());
1112         setCurrentWorkArea(wa);
1113
1114         setBusy(false);
1115 }
1116
1117
1118 void GuiView::connectBuffer(Buffer & buf)
1119 {
1120         buf.setGuiDelegate(this);
1121 }
1122
1123
1124 void GuiView::disconnectBuffer()
1125 {
1126         if (d.current_work_area_)
1127                 d.current_work_area_->bufferView().setGuiDelegate(0);
1128 }
1129
1130
1131 void GuiView::connectBufferView(BufferView & bv)
1132 {
1133         bv.setGuiDelegate(this);
1134 }
1135
1136
1137 void GuiView::disconnectBufferView()
1138 {
1139         if (d.current_work_area_)
1140                 d.current_work_area_->bufferView().setGuiDelegate(0);
1141 }
1142
1143
1144 void GuiView::errors(string const & error_type)
1145 {
1146         ErrorList & el = buffer()->errorList(error_type);
1147         if (!el.empty())
1148                 showDialog("errorlist", error_type);
1149 }
1150
1151
1152 void GuiView::updateTocItem(std::string const & type, DocIterator const & dit)
1153 {
1154         d.toc_models_.updateItem(toqstr(type), dit);
1155 }
1156
1157
1158 void GuiView::structureChanged()
1159 {
1160         d.toc_models_.reset(view());
1161         // Navigator needs more than a simple update in this case. It needs to be
1162         // rebuilt.
1163         updateDialog("toc", "");
1164 }
1165
1166
1167 void GuiView::updateDialog(string const & name, string const & data)
1168 {
1169         if (!isDialogVisible(name))
1170                 return;
1171
1172         map<string, DialogPtr>::const_iterator it = d.dialogs_.find(name);
1173         if (it == d.dialogs_.end())
1174                 return;
1175
1176         Dialog * const dialog = it->second.get();
1177         if (dialog->isVisibleView())
1178                 dialog->initialiseParams(data);
1179 }
1180
1181
1182 BufferView * GuiView::view()
1183 {
1184         return d.current_work_area_ ? &d.current_work_area_->bufferView() : 0;
1185 }
1186
1187
1188 void GuiView::autoSave()
1189 {
1190         LYXERR(Debug::INFO, "Running autoSave()");
1191
1192         if (buffer())
1193                 view()->buffer().autoSave();
1194 }
1195
1196
1197 void GuiView::resetAutosaveTimers()
1198 {
1199         if (lyxrc.autosave)
1200                 d.autosave_timeout_.restart();
1201 }
1202
1203
1204 bool GuiView::getStatus(FuncRequest const & cmd, FuncStatus & flag)
1205 {
1206         bool enable = true;
1207         Buffer * buf = buffer();
1208
1209         /* In LyX/Mac, when a dialog is open, the menus of the
1210            application can still be accessed without giving focus to
1211            the main window. In this case, we want to disable the menu
1212            entries that are buffer-related.
1213
1214            Note that this code is not perfect, as bug 1941 attests:
1215            http://bugzilla.lyx.org/show_bug.cgi?id=1941#c4
1216         */
1217         if (cmd.origin == FuncRequest::MENU && !hasFocus())
1218                 buf = 0;
1219
1220         if (cmd.origin == FuncRequest::TOC) {
1221                 GuiToc * toc = static_cast<GuiToc*>(findOrBuild("toc", false));
1222                 FuncStatus fs;
1223                 if (toc->getStatus(view()->cursor(), cmd, fs))
1224                         flag |= fs;
1225                 else
1226                         flag.setEnabled(false);
1227                 return true;
1228         }
1229
1230         switch(cmd.action) {
1231         case LFUN_BUFFER_WRITE:
1232                 enable = buf && (buf->isUnnamed() || !buf->isClean());
1233                 break;
1234
1235         case LFUN_BUFFER_WRITE_AS:
1236                 enable = buf;
1237                 break;
1238
1239         case LFUN_SPLIT_VIEW:
1240                 if (cmd.getArg(0) == "vertical")
1241                         enable = buf && (d.splitter_->count() == 1 ||
1242                                          d.splitter_->orientation() == Qt::Vertical);
1243                 else
1244                         enable = buf && (d.splitter_->count() == 1 ||
1245                                          d.splitter_->orientation() == Qt::Horizontal);
1246                 break;
1247
1248         case LFUN_CLOSE_TAB_GROUP:
1249                 enable = d.currentTabWorkArea();
1250                 break;
1251
1252         case LFUN_TOOLBAR_TOGGLE:
1253                 if (GuiToolbar * t = toolbar(cmd.getArg(0)))
1254                         flag.setOnOff(t->isVisible());
1255                 break;
1256
1257         case LFUN_UI_TOGGLE:
1258                 flag.setOnOff(isFullScreen());
1259                 break;
1260
1261         case LFUN_DIALOG_TOGGLE:
1262                 flag.setOnOff(isDialogVisible(cmd.getArg(0)));
1263                 // fall through to set "enable"
1264         case LFUN_DIALOG_SHOW: {
1265                 string const name = cmd.getArg(0);
1266                 if (!buf)
1267                         enable = name == "aboutlyx"
1268                                 || name == "file" //FIXME: should be removed.
1269                                 || name == "prefs"
1270                                 || name == "texinfo";
1271                 else if (name == "print")
1272                         enable = buf->isExportable("dvi")
1273                                 && lyxrc.print_command != "none";
1274                 else if (name == "character") {
1275                         if (!view())
1276                                 enable = false;
1277                         else {
1278                                 InsetCode ic = view()->cursor().inset().lyxCode();
1279                                 enable = ic != ERT_CODE && ic != LISTINGS_CODE;
1280                         }
1281                 }
1282                 else if (name == "symbols") {
1283                         if (!view() || view()->cursor().inMathed())
1284                                 enable = false;
1285                         else {
1286                                 InsetCode ic = view()->cursor().inset().lyxCode();
1287                                 enable = ic != ERT_CODE && ic != LISTINGS_CODE;
1288                         }
1289                 }
1290                 else if (name == "latexlog")
1291                         enable = FileName(buf->logName()).isReadableFile();
1292                 else if (name == "spellchecker")
1293 #if defined (USE_ASPELL)
1294                         enable = !buf->isReadonly();
1295 #else
1296                         enable = false;
1297 #endif
1298                 else if (name == "vclog")
1299                         enable = buf->lyxvc().inUse();
1300                 break;
1301         }
1302
1303         case LFUN_DIALOG_UPDATE: {
1304                 string const name = cmd.getArg(0);
1305                 if (!buf)
1306                         enable = name == "prefs";
1307                 break;
1308         }
1309
1310         case LFUN_INSET_APPLY: {
1311                 string const name = cmd.getArg(0);
1312                 Inset * inset = getOpenInset(name);
1313                 if (inset) {
1314                         FuncRequest fr(LFUN_INSET_MODIFY, cmd.argument());
1315                         FuncStatus fs;
1316                         if (!inset->getStatus(view()->cursor(), fr, fs)) {
1317                                 // Every inset is supposed to handle this
1318                                 LASSERT(false, break);
1319                         }
1320                         flag |= fs;
1321                 } else {
1322                         FuncRequest fr(LFUN_INSET_INSERT, cmd.argument());
1323                         flag |= lyx::getStatus(fr);
1324                 }
1325                 enable = flag.enabled();
1326                 break;
1327         }
1328
1329         case LFUN_COMPLETION_INLINE:
1330                 if (!d.current_work_area_
1331                     || !d.current_work_area_->completer().inlinePossible(view()->cursor()))
1332                     enable = false;
1333                 break;
1334
1335         case LFUN_COMPLETION_POPUP:
1336                 if (!d.current_work_area_
1337                     || !d.current_work_area_->completer().popupPossible(view()->cursor()))
1338                     enable = false;
1339                 break;
1340
1341         case LFUN_COMPLETION_COMPLETE:
1342                 if (!d.current_work_area_
1343                         || !d.current_work_area_->completer().inlinePossible(view()->cursor()))
1344                     enable = false;
1345                 break;
1346
1347         case LFUN_COMPLETION_ACCEPT:
1348                 if (!d.current_work_area_
1349                     || (!d.current_work_area_->completer().popupVisible()
1350                         && !d.current_work_area_->completer().inlineVisible()
1351                         && !d.current_work_area_->completer().completionAvailable()))
1352                         enable = false;
1353                 break;
1354
1355         case LFUN_COMPLETION_CANCEL:
1356                 if (!d.current_work_area_
1357                     || (!d.current_work_area_->completer().popupVisible()
1358                         && !d.current_work_area_->completer().inlineVisible()))
1359                         enable = false;
1360                 break;
1361
1362         case LFUN_BUFFER_ZOOM_OUT:
1363                 enable = buf && lyxrc.zoom > 10;
1364                 break;
1365
1366         case LFUN_BUFFER_ZOOM_IN:
1367                 enable = buf;
1368                 break;
1369
1370         default:
1371                 return false;
1372         }
1373
1374         if (!enable)
1375                 flag.setEnabled(false);
1376
1377         return true;
1378 }
1379
1380
1381 static FileName selectTemplateFile()
1382 {
1383         FileDialog dlg(qt_("Select template file"));
1384         dlg.setButton1(qt_("Documents|#o#O"), toqstr(lyxrc.document_path));
1385         dlg.setButton1(qt_("Templates|#T#t"), toqstr(lyxrc.template_path));
1386
1387         FileDialog::Result result = dlg.open(toqstr(lyxrc.template_path),
1388                              QStringList(qt_("LyX Documents (*.lyx)")));
1389
1390         if (result.first == FileDialog::Later)
1391                 return FileName();
1392         if (result.second.isEmpty())
1393                 return FileName();
1394         return FileName(fromqstr(result.second));
1395 }
1396
1397
1398 Buffer * GuiView::loadDocument(FileName const & filename, bool tolastfiles)
1399 {
1400         setBusy(true);
1401
1402         Buffer * newBuffer = checkAndLoadLyXFile(filename);
1403
1404         if (!newBuffer) {
1405                 message(_("Document not loaded."));
1406                 setBusy(false);
1407                 return 0;
1408         }
1409         
1410         setBuffer(newBuffer);
1411
1412         // scroll to the position when the file was last closed
1413         if (lyxrc.use_lastfilepos) {
1414                 LastFilePosSection::FilePos filepos =
1415                         theSession().lastFilePos().load(filename);
1416                 view()->moveToPosition(filepos.pit, filepos.pos, 0, 0);
1417         }
1418
1419         if (tolastfiles)
1420                 theSession().lastFiles().add(filename);
1421
1422         setBusy(false);
1423         return newBuffer;
1424 }
1425
1426
1427 void GuiView::openDocument(string const & fname)
1428 {
1429         string initpath = lyxrc.document_path;
1430
1431         if (buffer()) {
1432                 string const trypath = buffer()->filePath();
1433                 // If directory is writeable, use this as default.
1434                 if (FileName(trypath).isDirWritable())
1435                         initpath = trypath;
1436         }
1437
1438         string filename;
1439
1440         if (fname.empty()) {
1441                 FileDialog dlg(qt_("Select document to open"), LFUN_FILE_OPEN);
1442                 dlg.setButton1(qt_("Documents|#o#O"), toqstr(lyxrc.document_path));
1443                 dlg.setButton2(qt_("Examples|#E#e"),
1444                                 toqstr(addPath(package().system_support().absFilename(), "examples")));
1445
1446                 QStringList filter(qt_("LyX Documents (*.lyx)"));
1447                 filter << qt_("LyX-1.3.x Documents (*.lyx13)")
1448                         << qt_("LyX-1.4.x Documents (*.lyx14)")
1449                         << qt_("LyX-1.5.x Documents (*.lyx15)")
1450                         << qt_("LyX-1.6.x Documents (*.lyx16)");
1451                 FileDialog::Result result =
1452                         dlg.open(toqstr(initpath), filter);
1453
1454                 if (result.first == FileDialog::Later)
1455                         return;
1456
1457                 filename = fromqstr(result.second);
1458
1459                 // check selected filename
1460                 if (filename.empty()) {
1461                         message(_("Canceled."));
1462                         return;
1463                 }
1464         } else
1465                 filename = fname;
1466
1467         // get absolute path of file and add ".lyx" to the filename if
1468         // necessary. 
1469         FileName const fullname = 
1470                         fileSearch(string(), filename, "lyx", support::may_not_exist);
1471         if (!fullname.empty())
1472                 filename = fullname.absFilename();
1473
1474         if (!fullname.onlyPath().isDirectory()) {
1475                 Alert::warning(_("Invalid filename"),
1476                                 bformat(_("The directory in the given path\n%1$s\ndoes not exist."),
1477                                 from_utf8(fullname.absFilename())));
1478                 return;
1479         }
1480         // if the file doesn't exist, let the user create one
1481         if (!fullname.exists()) {
1482                 // the user specifically chose this name. Believe him.
1483                 Buffer * const b = newFile(filename, string(), true);
1484                 if (b)
1485                         setBuffer(b);
1486                 return;
1487         }
1488
1489         docstring const disp_fn = makeDisplayPath(filename);
1490         message(bformat(_("Opening document %1$s..."), disp_fn));
1491
1492         docstring str2;
1493         Buffer * buf = loadDocument(fullname);
1494         if (buf) {
1495                 buf->updateLabels();
1496                 setBuffer(buf);
1497                 buf->errors("Parse");
1498                 str2 = bformat(_("Document %1$s opened."), disp_fn);
1499                 if (buf->lyxvc().inUse())
1500                         str2 += " " + from_utf8(buf->lyxvc().versionString()) +
1501                                 " " + _("Version control detected.");
1502         } else {
1503                 str2 = bformat(_("Could not open document %1$s"), disp_fn);
1504         }
1505         message(str2);
1506 }
1507
1508 // FIXME: clean that
1509 static bool import(GuiView * lv, FileName const & filename,
1510         string const & format, ErrorList & errorList)
1511 {
1512         FileName const lyxfile(support::changeExtension(filename.absFilename(), ".lyx"));
1513
1514         string loader_format;
1515         vector<string> loaders = theConverters().loaders();
1516         if (find(loaders.begin(), loaders.end(), format) == loaders.end()) {
1517                 for (vector<string>::const_iterator it = loaders.begin();
1518                      it != loaders.end(); ++it) {
1519                         if (!theConverters().isReachable(format, *it))
1520                                 continue;
1521
1522                         string const tofile =
1523                                 support::changeExtension(filename.absFilename(),
1524                                 formats.extension(*it));
1525                         if (!theConverters().convert(0, filename, FileName(tofile),
1526                                 filename, format, *it, errorList))
1527                                 return false;
1528                         loader_format = *it;
1529                         break;
1530                 }
1531                 if (loader_format.empty()) {
1532                         frontend::Alert::error(_("Couldn't import file"),
1533                                      bformat(_("No information for importing the format %1$s."),
1534                                          formats.prettyName(format)));
1535                         return false;
1536                 }
1537         } else
1538                 loader_format = format;
1539
1540         if (loader_format == "lyx") {
1541                 Buffer * buf = lv->loadDocument(lyxfile);
1542                 if (!buf)
1543                         return false;
1544                 buf->updateLabels();
1545                 lv->setBuffer(buf);
1546                 buf->errors("Parse");
1547         } else {
1548                 Buffer * const b = newFile(lyxfile.absFilename(), string(), true);
1549                 if (!b)
1550                         return false;
1551                 lv->setBuffer(b);
1552                 bool as_paragraphs = loader_format == "textparagraph";
1553                 string filename2 = (loader_format == format) ? filename.absFilename()
1554                         : support::changeExtension(filename.absFilename(),
1555                                           formats.extension(loader_format));
1556                 lv->view()->insertPlaintextFile(FileName(filename2), as_paragraphs);
1557                 theLyXFunc().setLyXView(lv);
1558                 lyx::dispatch(FuncRequest(LFUN_MARK_OFF));
1559         }
1560
1561         return true;
1562 }
1563
1564
1565 void GuiView::importDocument(string const & argument)
1566 {
1567         string format;
1568         string filename = split(argument, format, ' ');
1569
1570         LYXERR(Debug::INFO, format << " file: " << filename);
1571
1572         // need user interaction
1573         if (filename.empty()) {
1574                 string initpath = lyxrc.document_path;
1575
1576                 Buffer const * buf = buffer();
1577                 if (buf) {
1578                         string const trypath = buf->filePath();
1579                         // If directory is writeable, use this as default.
1580                         if (FileName(trypath).isDirWritable())
1581                                 initpath = trypath;
1582                 }
1583
1584                 docstring const text = bformat(_("Select %1$s file to import"),
1585                         formats.prettyName(format));
1586
1587                 FileDialog dlg(toqstr(text), LFUN_BUFFER_IMPORT);
1588                 dlg.setButton1(qt_("Documents|#o#O"), toqstr(lyxrc.document_path));
1589                 dlg.setButton2(qt_("Examples|#E#e"),
1590                         toqstr(addPath(package().system_support().absFilename(), "examples")));
1591
1592                 docstring filter = formats.prettyName(format);
1593                 filter += " (*.";
1594                 // FIXME UNICODE
1595                 filter += from_utf8(formats.extension(format));
1596                 filter += ')';
1597
1598                 FileDialog::Result result =
1599                         dlg.open(toqstr(initpath), fileFilters(toqstr(filter)));
1600
1601                 if (result.first == FileDialog::Later)
1602                         return;
1603
1604                 filename = fromqstr(result.second);
1605
1606                 // check selected filename
1607                 if (filename.empty())
1608                         message(_("Canceled."));
1609         }
1610
1611         if (filename.empty())
1612                 return;
1613
1614         // get absolute path of file
1615         FileName const fullname(support::makeAbsPath(filename));
1616
1617         FileName const lyxfile(support::changeExtension(fullname.absFilename(), ".lyx"));
1618
1619         // Check if the document already is open
1620         Buffer * buf = theBufferList().getBuffer(lyxfile);
1621         if (buf) {
1622                 setBuffer(buf);
1623                 if (!closeBuffer()) {
1624                         message(_("Canceled."));
1625                         return;
1626                 }
1627         }
1628
1629         docstring const displaypath = makeDisplayPath(lyxfile.absFilename(), 30);
1630
1631         // if the file exists already, and we didn't do
1632         // -i lyx thefile.lyx, warn
1633         if (lyxfile.exists() && fullname != lyxfile) {
1634
1635                 docstring text = bformat(_("The document %1$s already exists.\n\n"
1636                         "Do you want to overwrite that document?"), displaypath);
1637                 int const ret = Alert::prompt(_("Overwrite document?"),
1638                         text, 0, 1, _("&Overwrite"), _("&Cancel"));
1639
1640                 if (ret == 1) {
1641                         message(_("Canceled."));
1642                         return;
1643                 }
1644         }
1645
1646         message(bformat(_("Importing %1$s..."), displaypath));
1647         ErrorList errorList;
1648         if (import(this, fullname, format, errorList))
1649                 message(_("imported."));
1650         else
1651                 message(_("file not imported!"));
1652
1653         // FIXME (Abdel 12/08/06): Is there a need to display the error list here?
1654 }
1655
1656
1657 void GuiView::newDocument(string const & filename, bool from_template)
1658 {
1659         FileName initpath(lyxrc.document_path);
1660         Buffer * buf = buffer();
1661         if (buf) {
1662                 FileName const trypath(buf->filePath());
1663                 // If directory is writeable, use this as default.
1664                 if (trypath.isDirWritable())
1665                         initpath = trypath;
1666         }
1667
1668         string templatefile;
1669         if (from_template) {
1670                 templatefile = selectTemplateFile().absFilename();
1671                 if (templatefile.empty())
1672                         return;
1673         }
1674         
1675         Buffer * b;
1676         if (filename.empty())
1677                 b = newUnnamedFile(templatefile, initpath);
1678         else
1679                 b = newFile(filename, templatefile, true);
1680
1681         if (b)
1682                 setBuffer(b);
1683
1684         // If no new document could be created, it is unsure 
1685         // whether there is a valid BufferView.
1686         if (view())
1687                 // Ensure the cursor is correctly positioned on screen.
1688                 view()->showCursor();
1689 }
1690
1691
1692 void GuiView::insertLyXFile(docstring const & fname)
1693 {
1694         BufferView * bv = view();
1695         if (!bv)
1696                 return;
1697
1698         // FIXME UNICODE
1699         FileName filename(to_utf8(fname));
1700         
1701         if (!filename.empty()) {
1702                 bv->insertLyXFile(filename);
1703                 return;
1704         }
1705
1706         // Launch a file browser
1707         // FIXME UNICODE
1708         string initpath = lyxrc.document_path;
1709         string const trypath = bv->buffer().filePath();
1710         // If directory is writeable, use this as default.
1711         if (FileName(trypath).isDirWritable())
1712                 initpath = trypath;
1713
1714         // FIXME UNICODE
1715         FileDialog dlg(qt_("Select LyX document to insert"), LFUN_FILE_INSERT);
1716         dlg.setButton1(qt_("Documents|#o#O"), toqstr(lyxrc.document_path));
1717         dlg.setButton2(qt_("Examples|#E#e"),
1718                 toqstr(addPath(package().system_support().absFilename(),
1719                 "examples")));
1720
1721         FileDialog::Result result = dlg.open(toqstr(initpath),
1722                              QStringList(qt_("LyX Documents (*.lyx)")));
1723
1724         if (result.first == FileDialog::Later)
1725                 return;
1726
1727         // FIXME UNICODE
1728         filename.set(fromqstr(result.second));
1729
1730         // check selected filename
1731         if (filename.empty()) {
1732                 // emit message signal.
1733                 message(_("Canceled."));
1734                 return;
1735         }
1736
1737         bv->insertLyXFile(filename);
1738 }
1739
1740
1741 void GuiView::insertPlaintextFile(docstring const & fname,
1742         bool asParagraph)
1743 {
1744         BufferView * bv = view();
1745         if (!bv || !FileName::isAbsolute(to_utf8(fname)))
1746                 return;
1747
1748         // FIXME UNICODE
1749         FileName filename(to_utf8(fname));
1750         
1751         if (!filename.empty()) {
1752                 bv->insertPlaintextFile(filename, asParagraph);
1753                 return;
1754         }
1755
1756         FileDialog dlg(qt_("Select file to insert"), (asParagraph ?
1757                 LFUN_FILE_INSERT_PLAINTEXT_PARA : LFUN_FILE_INSERT_PLAINTEXT));
1758
1759         FileDialog::Result result = dlg.open(toqstr(bv->buffer().filePath()),
1760                 QStringList(qt_("All Files (*)")));
1761
1762         if (result.first == FileDialog::Later)
1763                 return;
1764
1765         // FIXME UNICODE
1766         filename.set(fromqstr(result.second));
1767
1768         // check selected filename
1769         if (filename.empty()) {
1770                 // emit message signal.
1771                 message(_("Canceled."));
1772                 return;
1773         }
1774
1775         bv->insertPlaintextFile(filename, asParagraph);
1776 }
1777
1778
1779 bool GuiView::renameBuffer(Buffer & b, docstring const & newname)
1780 {
1781         FileName fname = b.fileName();
1782         FileName const oldname = fname;
1783
1784         if (!newname.empty()) {
1785                 // FIXME UNICODE
1786                 fname = support::makeAbsPath(to_utf8(newname), oldname.onlyPath().absFilename());
1787         } else {
1788                 // Switch to this Buffer.
1789                 setBuffer(&b);
1790
1791                 // No argument? Ask user through dialog.
1792                 // FIXME UNICODE
1793                 FileDialog dlg(qt_("Choose a filename to save document as"),
1794                                    LFUN_BUFFER_WRITE_AS);
1795                 dlg.setButton1(qt_("Documents|#o#O"), toqstr(lyxrc.document_path));
1796                 dlg.setButton2(qt_("Templates|#T#t"), toqstr(lyxrc.template_path));
1797
1798                 if (!isLyXFilename(fname.absFilename()))
1799                         fname.changeExtension(".lyx");
1800
1801                 FileDialog::Result result =
1802                         dlg.save(toqstr(fname.onlyPath().absFilename()),
1803                                QStringList(qt_("LyX Documents (*.lyx)")),
1804                                      toqstr(fname.onlyFileName()));
1805
1806                 if (result.first == FileDialog::Later)
1807                         return false;
1808
1809                 fname.set(fromqstr(result.second));
1810
1811                 if (fname.empty())
1812                         return false;
1813
1814                 if (!isLyXFilename(fname.absFilename()))
1815                         fname.changeExtension(".lyx");
1816         }
1817
1818         if (FileName(fname).exists()) {
1819                 docstring const file = makeDisplayPath(fname.absFilename(), 30);
1820                 docstring text = bformat(_("The document %1$s already "
1821                                            "exists.\n\nDo you want to "
1822                                            "overwrite that document?"), 
1823                                          file);
1824                 int const ret = Alert::prompt(_("Overwrite document?"),
1825                         text, 0, 2, _("&Overwrite"), _("&Rename"), _("&Cancel"));
1826                 switch (ret) {
1827                 case 0: break;
1828                 case 1: return renameBuffer(b, docstring());
1829                 case 2: return false;
1830                 }
1831         }
1832
1833         FileName oldauto = b.getAutosaveFilename();
1834
1835         // Ok, change the name of the buffer
1836         b.setFileName(fname.absFilename());
1837         b.markDirty();
1838         bool unnamed = b.isUnnamed();
1839         b.setUnnamed(false);
1840         b.saveCheckSum(fname);
1841
1842         // bring the autosave file with us, just in case.
1843         b.moveAutosaveFile(oldauto);
1844         
1845         if (!saveBuffer(b)) {
1846                 oldauto = b.getAutosaveFilename();
1847                 b.setFileName(oldname.absFilename());
1848                 b.setUnnamed(unnamed);
1849                 b.saveCheckSum(oldname);
1850                 b.moveAutosaveFile(oldauto);
1851                 return false;
1852         }
1853
1854         return true;
1855 }
1856
1857
1858 bool GuiView::saveBuffer(Buffer & b)
1859 {
1860         if (workArea(b) && workArea(b)->inDialogMode())
1861                 return true;
1862
1863         if (b.isUnnamed())
1864                 return renameBuffer(b, docstring());
1865
1866         if (b.save()) {
1867                 theSession().lastFiles().add(b.fileName());
1868                 return true;
1869         }
1870
1871         // Switch to this Buffer.
1872         setBuffer(&b);
1873
1874         // FIXME: we don't tell the user *WHY* the save failed !!
1875         docstring const file = makeDisplayPath(b.absFileName(), 30);
1876         docstring text = bformat(_("The document %1$s could not be saved.\n\n"
1877                                    "Do you want to rename the document and "
1878                                    "try again?"), file);
1879         int const ret = Alert::prompt(_("Rename and save?"),
1880                 text, 0, 2, _("&Rename"), _("&Retry"), _("&Cancel"));
1881         switch (ret) {
1882         case 0:
1883                 if (!renameBuffer(b, docstring()))
1884                         return false;
1885                 break;
1886         case 1:
1887                 break;
1888         case 2:
1889                 return false;
1890         }
1891
1892         return saveBuffer(b);
1893 }
1894
1895
1896 bool GuiView::closeBuffer()
1897 {
1898         Buffer * buf = buffer();
1899         return buf && closeBuffer(*buf);
1900 }
1901
1902
1903 bool GuiView::closeBuffer(Buffer & buf, bool tolastopened, bool mark_active)
1904 {
1905         // goto bookmark to update bookmark pit.
1906         //FIXME: we should update only the bookmarks related to this buffer!
1907         LYXERR(Debug::DEBUG, "GuiView::closeBuffer()");
1908         for (size_t i = 0; i < theSession().bookmarks().size(); ++i)
1909                 theLyXFunc().gotoBookmark(i+1, false, false);
1910
1911         if (buf.isClean() || buf.paragraphs().empty()) {
1912                 // save in sessions if requested
1913                 // do not save childs if their master
1914                 // is opened as well
1915                 if (tolastopened)
1916                         theSession().lastOpened().add(buf.fileName(), mark_active);
1917                 if (buf.parent())
1918                         // Don't close child documents.
1919                         removeWorkArea(currentMainWorkArea());
1920                 else
1921                         theBufferList().release(&buf);
1922                 return true;
1923         }
1924         // Switch to this Buffer.
1925         setBuffer(&buf);
1926
1927         docstring file;
1928         // FIXME: Unicode?
1929         if (buf.isUnnamed())
1930                 file = from_utf8(buf.fileName().onlyFileName());
1931         else
1932                 file = buf.fileName().displayName(30);
1933
1934         // Bring this window to top before asking questions.
1935         raise();
1936         activateWindow();
1937
1938         docstring const text = bformat(_("The document %1$s has unsaved changes."
1939                 "\n\nDo you want to save the document or discard the changes?"), file);
1940         int const ret = Alert::prompt(_("Save changed document?"),
1941                 text, 0, 2, _("&Save"), _("&Discard"), _("&Cancel"));
1942
1943         switch (ret) {
1944         case 0:
1945                 if (!saveBuffer(buf))
1946                         return false;
1947                 break;
1948         case 1:
1949                 // if we crash after this we could
1950                 // have no autosave file but I guess
1951                 // this is really improbable (Jug)
1952                 buf.removeAutosaveFile();
1953                 break;
1954         case 2:
1955                 return false;
1956         }
1957
1958         // save file names to .lyx/session
1959         if (tolastopened)
1960                 theSession().lastOpened().add(buf.fileName(), mark_active);
1961
1962         if (buf.parent())
1963                 // Don't close child documents.
1964                 removeWorkArea(currentMainWorkArea());
1965         else
1966                 theBufferList().release(&buf);
1967
1968         return true;
1969 }
1970
1971
1972 void GuiView::gotoNextOrPreviousBuffer(NextOrPrevious np)
1973 {
1974         Buffer * const curbuf = buffer();
1975         Buffer * nextbuf = curbuf;
1976         while (true) {
1977                 if (np == NEXTBUFFER)
1978                         nextbuf = theBufferList().next(nextbuf);
1979                 else
1980                         nextbuf = theBufferList().previous(nextbuf);
1981                 if (nextbuf == curbuf)
1982                         break;
1983                 if (nextbuf == 0) {
1984                         nextbuf = curbuf;
1985                         break;
1986                 }
1987                 if (workArea(*nextbuf))
1988                         break;
1989         }
1990         setBuffer(nextbuf);
1991 }
1992
1993
1994 bool GuiView::dispatch(FuncRequest const & cmd)
1995 {
1996         BufferView * bv = view();
1997         // By default we won't need any update.
1998         if (bv)
1999                 bv->cursor().updateFlags(Update::None);
2000         bool dispatched = true;
2001
2002         if (cmd.origin == FuncRequest::TOC) {
2003                 GuiToc * toc = static_cast<GuiToc*>(findOrBuild("toc", false));
2004                 toc->doDispatch(bv->cursor(), cmd);
2005                 return true;
2006         }
2007
2008         switch(cmd.action) {
2009                 case LFUN_BUFFER_IMPORT:
2010                         importDocument(to_utf8(cmd.argument()));
2011                         break;
2012
2013                 case LFUN_BUFFER_SWITCH:
2014                         if (FileName::isAbsolute(to_utf8(cmd.argument()))) {
2015                                 Buffer * buffer = 
2016                                         theBufferList().getBuffer(FileName(to_utf8(cmd.argument())));
2017                                 if (buffer)
2018                                         setBuffer(buffer);
2019                                 else
2020                                         bv->cursor().message(_("Document not loaded"));
2021                         }
2022                         break;
2023
2024                 case LFUN_BUFFER_NEXT:
2025                         gotoNextOrPreviousBuffer(NEXTBUFFER);
2026                         break;
2027
2028                 case LFUN_BUFFER_PREVIOUS:
2029                         gotoNextOrPreviousBuffer(PREVBUFFER);
2030                         break;
2031
2032                 case LFUN_COMMAND_EXECUTE: {
2033                         bool const show_it = cmd.argument() != "off";
2034                         // FIXME: this is a hack, "minibuffer" should not be
2035                         // hardcoded.
2036                         if (GuiToolbar * t = toolbar("minibuffer")) {
2037                                 t->setVisible(show_it);
2038                                 if (show_it && t->commandBuffer())
2039                                         t->commandBuffer()->setFocus();
2040                         }
2041                         break;
2042                 }
2043                 case LFUN_DROP_LAYOUTS_CHOICE:
2044                         if (d.layout_)
2045                                 d.layout_->showPopup();
2046                         break;
2047
2048                 case LFUN_MENU_OPEN:
2049                         if (QMenu * menu = guiApp->menus().menu(toqstr(cmd.argument()), *this))
2050                                 menu->exec(QCursor::pos());
2051                         break;
2052
2053                 case LFUN_FILE_INSERT:
2054                         insertLyXFile(cmd.argument());
2055                         break;
2056                 case LFUN_FILE_INSERT_PLAINTEXT_PARA:
2057                         insertPlaintextFile(cmd.argument(), true);
2058                         break;
2059
2060                 case LFUN_FILE_INSERT_PLAINTEXT:
2061                         insertPlaintextFile(cmd.argument(), false);
2062                         break;
2063
2064                 case LFUN_BUFFER_WRITE:
2065                         if (bv)
2066                                 saveBuffer(bv->buffer());
2067                         break;
2068
2069                 case LFUN_BUFFER_WRITE_AS:
2070                         if (bv)
2071                                 renameBuffer(bv->buffer(), cmd.argument());
2072                         break;
2073
2074                 case LFUN_BUFFER_WRITE_ALL: {
2075                         Buffer * first = theBufferList().first();
2076                         if (!first)
2077                                 break;
2078                         message(_("Saving all documents..."));
2079                         // We cannot use a for loop as the buffer list cycles.
2080                         Buffer * b = first;
2081                         do {
2082                                 if (!b->isClean()) {
2083                                         saveBuffer(*b);
2084                                         LYXERR(Debug::ACTION, "Saved " << b->absFileName());
2085                                 }
2086                                 b = theBufferList().next(b);
2087                         } while (b != first); 
2088                         message(_("All documents saved."));
2089                         break;
2090                 }
2091
2092                 case LFUN_TOOLBAR_TOGGLE: {
2093                         string const name = cmd.getArg(0);
2094                         if (GuiToolbar * t = toolbar(name))
2095                                 t->toggle();
2096                         break;
2097                 }
2098
2099                 case LFUN_DIALOG_UPDATE: {
2100                         string const name = to_utf8(cmd.argument());
2101                         // Can only update a dialog connected to an existing inset
2102                         Inset * inset = getOpenInset(name);
2103                         if (inset) {
2104                                 FuncRequest fr(LFUN_INSET_DIALOG_UPDATE, cmd.argument());
2105                                 inset->dispatch(view()->cursor(), fr);
2106                         } else if (name == "paragraph") {
2107                                 lyx::dispatch(FuncRequest(LFUN_PARAGRAPH_UPDATE));
2108                         } else if (name == "prefs" || name == "document") {
2109                                 updateDialog(name, string());
2110                         }
2111                         break;
2112                 }
2113
2114                 case LFUN_DIALOG_TOGGLE: {
2115                         if (isDialogVisible(cmd.getArg(0)))
2116                                 dispatch(FuncRequest(LFUN_DIALOG_HIDE, cmd.argument()));
2117                         else
2118                                 dispatch(FuncRequest(LFUN_DIALOG_SHOW, cmd.argument()));
2119                         break;
2120                 }
2121
2122                 case LFUN_DIALOG_DISCONNECT_INSET:
2123                         disconnectDialog(to_utf8(cmd.argument()));
2124                         break;
2125
2126                 case LFUN_DIALOG_HIDE: {
2127                         guiApp->hideDialogs(to_utf8(cmd.argument()), 0);
2128                         break;
2129                 }
2130
2131                 case LFUN_DIALOG_SHOW: {
2132                         string const name = cmd.getArg(0);
2133                         string data = trim(to_utf8(cmd.argument()).substr(name.size()));
2134
2135                         if (name == "character") {
2136                                 data = freefont2string();
2137                                 if (!data.empty())
2138                                         showDialog("character", data);
2139                         } else if (name == "latexlog") {
2140                                 Buffer::LogType type; 
2141                                 string const logfile = buffer()->logName(&type);
2142                                 switch (type) {
2143                                 case Buffer::latexlog:
2144                                         data = "latex ";
2145                                         break;
2146                                 case Buffer::buildlog:
2147                                         data = "literate ";
2148                                         break;
2149                                 }
2150                                 data += Lexer::quoteString(logfile);
2151                                 showDialog("log", data);
2152                         } else if (name == "vclog") {
2153                                 string const data = "vc " +
2154                                         Lexer::quoteString(buffer()->lyxvc().getLogFile());
2155                                 showDialog("log", data);
2156                         } else if (name == "symbols") {
2157                                 data = bv->cursor().getEncoding()->name();
2158                                 if (!data.empty())
2159                                         showDialog("symbols", data);
2160                         // bug 5274
2161                         } else if (name == "prefs" && isFullScreen()) {
2162                                 FuncRequest fr(LFUN_INSET_INSERT, "fullscreen");
2163                                 lfunUiToggle(fr);
2164                                 showDialog("prefs", data);
2165                         } else
2166                                 showDialog(name, data);
2167                         break;
2168                 }
2169
2170                 case LFUN_INSET_APPLY: {
2171                         string const name = cmd.getArg(0);
2172                         Inset * inset = getOpenInset(name);
2173                         if (inset) {
2174                                 // put cursor in front of inset.
2175                                 if (!view()->setCursorFromInset(inset)) {
2176                                         LASSERT(false, break);
2177                                 }
2178                                 
2179                                 // useful if we are called from a dialog.
2180                                 view()->cursor().beginUndoGroup();
2181                                 view()->cursor().recordUndo();
2182                                 FuncRequest fr(LFUN_INSET_MODIFY, cmd.argument());
2183                                 inset->dispatch(view()->cursor(), fr);
2184                                 view()->cursor().endUndoGroup();
2185                         } else {
2186                                 FuncRequest fr(LFUN_INSET_INSERT, cmd.argument());
2187                                 lyx::dispatch(fr);
2188                         }
2189                         break;
2190                 }
2191
2192                 case LFUN_UI_TOGGLE:
2193                         lfunUiToggle(cmd);
2194                         // Make sure the keyboard focus stays in the work area.
2195                         setFocus();
2196                         break;
2197
2198                 case LFUN_SPLIT_VIEW:
2199                         if (Buffer * buf = buffer()) {
2200                                 string const orientation = cmd.getArg(0);
2201                                 d.splitter_->setOrientation(orientation == "vertical"
2202                                         ? Qt::Vertical : Qt::Horizontal);
2203                                 TabWorkArea * twa = addTabWorkArea();
2204                                 GuiWorkArea * wa = twa->addWorkArea(*buf, *this);
2205                                 setCurrentWorkArea(wa);
2206                         }
2207                         break;
2208
2209                 case LFUN_CLOSE_TAB_GROUP:
2210                         if (TabWorkArea * twa = d.currentTabWorkArea()) {
2211                                 delete twa;
2212                                 twa = d.currentTabWorkArea();
2213                                 // Switch to the next GuiWorkArea in the found TabWorkArea.
2214                                 if (twa) {
2215                                         // Make sure the work area is up to date.
2216                                         setCurrentWorkArea(twa->currentWorkArea());
2217                                 } else {
2218                                         setCurrentWorkArea(0);
2219                                 }
2220                         }
2221                         break;
2222                         
2223                 case LFUN_COMPLETION_INLINE:
2224                         if (d.current_work_area_)
2225                                 d.current_work_area_->completer().showInline();
2226                         break;
2227
2228                 case LFUN_COMPLETION_POPUP:
2229                         if (d.current_work_area_)
2230                                 d.current_work_area_->completer().showPopup();
2231                         break;
2232
2233
2234                 case LFUN_COMPLETION_COMPLETE:
2235                         if (d.current_work_area_)
2236                                 d.current_work_area_->completer().tab();
2237                         break;
2238
2239                 case LFUN_COMPLETION_CANCEL:
2240                         if (d.current_work_area_) {
2241                                 if (d.current_work_area_->completer().popupVisible())
2242                                         d.current_work_area_->completer().hidePopup();
2243                                 else
2244                                         d.current_work_area_->completer().hideInline();
2245                         }
2246                         break;
2247
2248                 case LFUN_COMPLETION_ACCEPT:
2249                         if (d.current_work_area_)
2250                                 d.current_work_area_->completer().activate();
2251                         break;
2252
2253                 case LFUN_BUFFER_ZOOM_IN:
2254                 case LFUN_BUFFER_ZOOM_OUT:
2255                         if (cmd.argument().empty()) {
2256                                 if (cmd.action == LFUN_BUFFER_ZOOM_IN)
2257                                         lyxrc.zoom += 20;
2258                                 else
2259                                         lyxrc.zoom -= 20;
2260                         } else
2261                                 lyxrc.zoom += convert<int>(cmd.argument());
2262
2263                         if (lyxrc.zoom < 10)
2264                                 lyxrc.zoom = 10;
2265                                 
2266                         // The global QPixmapCache is used in GuiPainter to cache text
2267                         // painting so we must reset it.
2268                         QPixmapCache::clear();
2269                         guiApp->fontLoader().update();
2270                         lyx::dispatch(FuncRequest(LFUN_SCREEN_FONT_UPDATE));
2271                         break;
2272
2273                 default:
2274                         dispatched = false;
2275                         break;
2276         }
2277
2278         // Part of automatic menu appearance feature.
2279         if (isFullScreen()) {
2280                 if (menuBar()->isVisible())
2281                         menuBar()->hide();
2282                 if (statusBar()->isVisible())
2283                         statusBar()->hide();
2284         }
2285
2286         return dispatched;
2287 }
2288
2289
2290 void GuiView::lfunUiToggle(FuncRequest const & cmd)
2291 {
2292         string const arg = cmd.getArg(0);
2293         if (arg == "scrollbar") {
2294                 // hide() is of no help
2295                 if (d.current_work_area_->verticalScrollBarPolicy() ==
2296                         Qt::ScrollBarAlwaysOff)
2297
2298                         d.current_work_area_->setVerticalScrollBarPolicy(
2299                                 Qt::ScrollBarAsNeeded);
2300                 else
2301                         d.current_work_area_->setVerticalScrollBarPolicy(
2302                                 Qt::ScrollBarAlwaysOff);
2303                 return;
2304         }
2305         if (arg == "statusbar") {
2306                 statusBar()->setVisible(!statusBar()->isVisible());
2307                 return;
2308         }
2309         if (arg == "menubar") {
2310                 menuBar()->setVisible(!menuBar()->isVisible());
2311                 return;
2312         }
2313 #if QT_VERSION >= 0x040300
2314         if (arg == "frame") {
2315                 int l, t, r, b;
2316                 getContentsMargins(&l, &t, &r, &b);
2317                 //are the frames in default state?
2318                 d.current_work_area_->setFrameStyle(QFrame::NoFrame);
2319                 if (l == 0) {
2320                         setContentsMargins(-2, -2, -2, -2);
2321                 } else {
2322                         setContentsMargins(0, 0, 0, 0);
2323                 }
2324                 return;
2325         }
2326 #endif
2327         if (arg == "fullscreen") {
2328                 toggleFullScreen();
2329                 return;
2330         }
2331
2332         message(bformat("LFUN_UI_TOGGLE " + _("%1$s unknown command!"), from_utf8(arg)));
2333 }
2334
2335
2336 void GuiView::toggleFullScreen()
2337 {
2338         if (isFullScreen()) {
2339                 for (int i = 0; i != d.splitter_->count(); ++i)
2340                         d.tabWorkArea(i)->setFullScreen(false);
2341 #if QT_VERSION >= 0x040300
2342                 setContentsMargins(0, 0, 0, 0);
2343 #endif
2344                 setWindowState(windowState() ^ Qt::WindowFullScreen);
2345                 restoreLayout();
2346                 menuBar()->show();
2347                 statusBar()->show();
2348         } else {
2349                 // bug 5274
2350                 hideDialogs("prefs", 0);
2351                 for (int i = 0; i != d.splitter_->count(); ++i)
2352                         d.tabWorkArea(i)->setFullScreen(true);
2353 #if QT_VERSION >= 0x040300
2354                 setContentsMargins(-2, -2, -2, -2);
2355 #endif
2356                 saveLayout();
2357                 setWindowState(windowState() ^ Qt::WindowFullScreen);
2358                 statusBar()->hide();
2359                 menuBar()->hide();
2360                 if (lyxrc.full_screen_toolbars) {
2361                         ToolbarMap::iterator end = d.toolbars_.end();
2362                         for (ToolbarMap::iterator it = d.toolbars_.begin(); it != end; ++it)
2363                                 it->second->hide();
2364                 }
2365         }
2366
2367         // give dialogs like the TOC a chance to adapt
2368         updateDialogs();
2369 }
2370
2371
2372 Buffer const * GuiView::updateInset(Inset const * inset)
2373 {
2374         if (!d.current_work_area_)
2375                 return 0;
2376
2377         if (inset)
2378                 d.current_work_area_->scheduleRedraw();
2379
2380         return &d.current_work_area_->bufferView().buffer();
2381 }
2382
2383
2384 void GuiView::restartCursor()
2385 {
2386         /* When we move around, or type, it's nice to be able to see
2387          * the cursor immediately after the keypress.
2388          */
2389         if (d.current_work_area_)
2390                 d.current_work_area_->startBlinkingCursor();
2391
2392         // Take this occasion to update the other GUI elements.
2393         updateDialogs();
2394         updateStatusBar();
2395 }
2396
2397
2398 void GuiView::updateCompletion(Cursor & cur, bool start, bool keep)
2399 {
2400         if (d.current_work_area_)
2401                 d.current_work_area_->completer().updateVisibility(cur, start, keep);
2402 }
2403
2404 namespace {
2405
2406 // This list should be kept in sync with the list of insets in
2407 // src/insets/Inset.cpp.  I.e., if a dialog goes with an inset, the
2408 // dialog should have the same name as the inset.
2409 // Changes should be also recorded in LFUN_DIALOG_SHOW doxygen
2410 // docs in LyXAction.cpp.
2411
2412 char const * const dialognames[] = {
2413 "aboutlyx", "bibitem", "bibtex", "box", "branch", "changes", "character",
2414 "citation", "document", "errorlist", "ert", "external", "file", "findreplace",
2415 "float", "graphics", "include", "index", "index_print", "info", "nomenclature",
2416 "label", "log", "mathdelimiter", "mathmatrix", "mathspace", "note", "paragraph",
2417 "phantom", "prefs", "print", "ref", "sendto", "space", "spellchecker",
2418 "symbols", "tabular", "tabularcreate", "thesaurus", "texinfo", "toc", "href", 
2419 "view-source", "vspace", "wrap", "listings", "findreplaceadv" };
2420
2421 char const * const * const end_dialognames =
2422         dialognames + (sizeof(dialognames) / sizeof(char *));
2423
2424 class cmpCStr {
2425 public:
2426         cmpCStr(char const * name) : name_(name) {}
2427         bool operator()(char const * other) {
2428                 return strcmp(other, name_) == 0;
2429         }
2430 private:
2431         char const * name_;
2432 };
2433
2434
2435 bool isValidName(string const & name)
2436 {
2437         return find_if(dialognames, end_dialognames,
2438                             cmpCStr(name.c_str())) != end_dialognames;
2439 }
2440
2441 } // namespace anon
2442
2443
2444 void GuiView::resetDialogs()
2445 {
2446         // Make sure that no LFUN uses any LyXView.
2447         theLyXFunc().setLyXView(0);
2448         saveLayout();
2449         menuBar()->clear();
2450         constructToolbars();
2451         guiApp->menus().fillMenuBar(menuBar(), this, false);
2452         if (d.layout_)
2453                 d.layout_->updateContents(true);
2454         // Now update controls with current buffer.
2455         theLyXFunc().setLyXView(this);
2456         restoreLayout();
2457         restartCursor();
2458 }
2459
2460
2461 Dialog * GuiView::findOrBuild(string const & name, bool hide_it)
2462 {
2463         if (!isValidName(name))
2464                 return 0;
2465
2466         map<string, DialogPtr>::iterator it = d.dialogs_.find(name);
2467
2468         if (it != d.dialogs_.end()) {
2469                 if (hide_it)
2470                         it->second->hideView();
2471                 return it->second.get();
2472         }
2473
2474         Dialog * dialog = build(name);
2475         d.dialogs_[name].reset(dialog);
2476         if (lyxrc.allow_geometry_session)
2477                 dialog->restoreSession();
2478         if (hide_it)
2479                 dialog->hideView();
2480         return dialog;
2481 }
2482
2483
2484 void GuiView::showDialog(string const & name, string const & data,
2485         Inset * inset)
2486 {
2487         if (d.in_show_)
2488                 return;
2489
2490         d.in_show_ = true;
2491         try {
2492                 Dialog * dialog = findOrBuild(name, false);
2493                 if (dialog) {
2494                         dialog->showData(data);
2495                         if (inset)
2496                                 d.open_insets_[name] = inset;
2497                 }
2498         }
2499         catch (ExceptionMessage const & ex) {
2500                 d.in_show_ = false;
2501                 throw ex;
2502         }
2503         d.in_show_ = false;
2504 }
2505
2506
2507 bool GuiView::isDialogVisible(string const & name) const
2508 {
2509         map<string, DialogPtr>::const_iterator it = d.dialogs_.find(name);
2510         if (it == d.dialogs_.end())
2511                 return false;
2512         return it->second.get()->isVisibleView();
2513 }
2514
2515
2516 void GuiView::hideDialog(string const & name, Inset * inset)
2517 {
2518         map<string, DialogPtr>::const_iterator it = d.dialogs_.find(name);
2519         if (it == d.dialogs_.end())
2520                 return;
2521
2522         if (inset && inset != getOpenInset(name))
2523                 return;
2524
2525         Dialog * const dialog = it->second.get();
2526         if (dialog->isVisibleView())
2527                 dialog->hideView();
2528         d.open_insets_[name] = 0;
2529 }
2530
2531
2532 void GuiView::disconnectDialog(string const & name)
2533 {
2534         if (!isValidName(name))
2535                 return;
2536
2537         if (d.open_insets_.find(name) != d.open_insets_.end())
2538                 d.open_insets_[name] = 0;
2539 }
2540
2541
2542 Inset * GuiView::getOpenInset(string const & name) const
2543 {
2544         if (!isValidName(name))
2545                 return 0;
2546
2547         map<string, Inset *>::const_iterator it = d.open_insets_.find(name);
2548         return it == d.open_insets_.end() ? 0 : it->second;
2549 }
2550
2551
2552 void GuiView::hideAll() const
2553 {
2554         map<string, DialogPtr>::const_iterator it  = d.dialogs_.begin();
2555         map<string, DialogPtr>::const_iterator end = d.dialogs_.end();
2556
2557         for(; it != end; ++it)
2558                 it->second->hideView();
2559 }
2560
2561
2562 void GuiView::updateDialogs()
2563 {
2564         map<string, DialogPtr>::const_iterator it  = d.dialogs_.begin();
2565         map<string, DialogPtr>::const_iterator end = d.dialogs_.end();
2566
2567         for(; it != end; ++it) {
2568                 Dialog * dialog = it->second.get();
2569                 if (dialog && dialog->isVisibleView())
2570                         dialog->checkStatus();
2571         }
2572         updateToolbars();
2573         updateLayoutList();
2574 }
2575
2576
2577 // will be replaced by a proper factory...
2578 Dialog * createGuiAbout(GuiView & lv);
2579 Dialog * createGuiBibitem(GuiView & lv);
2580 Dialog * createGuiBibtex(GuiView & lv);
2581 Dialog * createGuiBox(GuiView & lv);
2582 Dialog * createGuiBranch(GuiView & lv);
2583 Dialog * createGuiChanges(GuiView & lv);
2584 Dialog * createGuiCharacter(GuiView & lv);
2585 Dialog * createGuiCitation(GuiView & lv);
2586 Dialog * createGuiDelimiter(GuiView & lv);
2587 Dialog * createGuiDocument(GuiView & lv);
2588 Dialog * createGuiErrorList(GuiView & lv);
2589 Dialog * createGuiERT(GuiView & lv);
2590 Dialog * createGuiExternal(GuiView & lv);
2591 Dialog * createGuiFloat(GuiView & lv);
2592 Dialog * createGuiGraphics(GuiView & lv);
2593 Dialog * createGuiInclude(GuiView & lv);
2594 Dialog * createGuiIndex(GuiView & lv);
2595 Dialog * createGuiInfo(GuiView & lv);
2596 Dialog * createGuiLabel(GuiView & lv);
2597 Dialog * createGuiListings(GuiView & lv);
2598 Dialog * createGuiLog(GuiView & lv);
2599 Dialog * createGuiMathHSpace(GuiView & lv);
2600 Dialog * createGuiMathMatrix(GuiView & lv);
2601 Dialog * createGuiNomenclature(GuiView & lv);
2602 Dialog * createGuiNote(GuiView & lv);
2603 Dialog * createGuiParagraph(GuiView & lv);
2604 Dialog * createGuiPhantom(GuiView & lv);
2605 Dialog * createGuiPreferences(GuiView & lv);
2606 Dialog * createGuiPrint(GuiView & lv);
2607 Dialog * createGuiPrintindex(GuiView & lv);
2608 Dialog * createGuiRef(GuiView & lv);
2609 Dialog * createGuiSearch(GuiView & lv);
2610 Dialog * createGuiSearchAdv(GuiView & lv);
2611 Dialog * createGuiSendTo(GuiView & lv);
2612 Dialog * createGuiShowFile(GuiView & lv);
2613 Dialog * createGuiSpellchecker(GuiView & lv);
2614 Dialog * createGuiSymbols(GuiView & lv);
2615 Dialog * createGuiTabularCreate(GuiView & lv);
2616 Dialog * createGuiTabular(GuiView & lv);
2617 Dialog * createGuiTexInfo(GuiView & lv);
2618 Dialog * createGuiTextHSpace(GuiView & lv);
2619 Dialog * createGuiToc(GuiView & lv);
2620 Dialog * createGuiThesaurus(GuiView & lv);
2621 Dialog * createGuiHyperlink(GuiView & lv);
2622 Dialog * createGuiVSpace(GuiView & lv);
2623 Dialog * createGuiViewSource(GuiView & lv);
2624 Dialog * createGuiWrap(GuiView & lv);
2625
2626
2627 Dialog * GuiView::build(string const & name)
2628 {
2629         LASSERT(isValidName(name), return 0);
2630
2631         if (name == "aboutlyx")
2632                 return createGuiAbout(*this);
2633         if (name == "bibitem")
2634                 return createGuiBibitem(*this);
2635         if (name == "bibtex")
2636                 return createGuiBibtex(*this);
2637         if (name == "box")
2638                 return createGuiBox(*this);
2639         if (name == "branch")
2640                 return createGuiBranch(*this);
2641         if (name == "changes")
2642                 return createGuiChanges(*this);
2643         if (name == "character")
2644                 return createGuiCharacter(*this);
2645         if (name == "citation")
2646                 return createGuiCitation(*this);
2647         if (name == "document")
2648                 return createGuiDocument(*this);
2649         if (name == "errorlist")
2650                 return createGuiErrorList(*this);
2651         if (name == "ert")
2652                 return createGuiERT(*this);
2653         if (name == "external")
2654                 return createGuiExternal(*this);
2655         if (name == "file")
2656                 return createGuiShowFile(*this);
2657         if (name == "findreplace")
2658                 return createGuiSearch(*this);
2659         if (name == "findreplaceadv")
2660                 return createGuiSearchAdv(*this);
2661         if (name == "float")
2662                 return createGuiFloat(*this);
2663         if (name == "graphics")
2664                 return createGuiGraphics(*this);
2665         if (name == "include")
2666                 return createGuiInclude(*this);
2667         if (name == "index")
2668                 return createGuiIndex(*this);
2669         if (name == "info")
2670                 return createGuiInfo(*this);
2671         if (name == "nomenclature")
2672                 return createGuiNomenclature(*this);
2673         if (name == "label")
2674                 return createGuiLabel(*this);
2675         if (name == "log")
2676                 return createGuiLog(*this);
2677         if (name == "mathdelimiter")
2678                 return createGuiDelimiter(*this);
2679         if (name == "mathspace")
2680                 return createGuiMathHSpace(*this);
2681         if (name == "mathmatrix")
2682                 return createGuiMathMatrix(*this);
2683         if (name == "note")
2684                 return createGuiNote(*this);
2685         if (name == "paragraph")
2686                 return createGuiParagraph(*this);
2687         if (name == "phantom")
2688                 return createGuiPhantom(*this);
2689         if (name == "prefs")
2690                 return createGuiPreferences(*this);
2691         if (name == "print")
2692                 return createGuiPrint(*this);
2693         if (name == "ref")
2694                 return createGuiRef(*this);
2695         if (name == "sendto")
2696                 return createGuiSendTo(*this);
2697         if (name == "space")
2698                 return createGuiTextHSpace(*this);
2699         if (name == "spellchecker")
2700                 return createGuiSpellchecker(*this);
2701         if (name == "symbols")
2702                 return createGuiSymbols(*this);
2703         if (name == "tabular")
2704                 return createGuiTabular(*this);
2705         if (name == "tabularcreate")
2706                 return createGuiTabularCreate(*this);
2707         if (name == "texinfo")
2708                 return createGuiTexInfo(*this);
2709         if (name == "view-source")
2710                 return createGuiViewSource(*this);
2711         if (name == "thesaurus")
2712                 return createGuiThesaurus(*this);
2713         if (name == "href")
2714                 return createGuiHyperlink(*this);
2715         if (name == "index_print")
2716                 return createGuiPrintindex(*this);
2717         if (name == "listings")
2718                 return createGuiListings(*this);
2719         if (name == "toc")
2720                 return createGuiToc(*this);
2721         if (name == "vspace")
2722                 return createGuiVSpace(*this);
2723         if (name == "wrap")
2724                 return createGuiWrap(*this);
2725
2726         return 0;
2727 }
2728
2729
2730 } // namespace frontend
2731 } // namespace lyx
2732
2733 #include "moc_GuiView.cpp"