]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/GuiView.cpp
Fix some crashes on exit and on windows switch.
[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 "GuiImplementation.h"
19 #include "GuiWorkArea.h"
20 #include "GuiKeySymbol.h"
21 #include "GuiMenubar.h"
22 #include "GuiToolbar.h"
23 #include "GuiToolbars.h"
24 #include "qt_helpers.h"
25
26 #include "frontends/Application.h"
27 #include "frontends/Dialogs.h"
28 #include "frontends/Gui.h"
29 #include "frontends/WorkArea.h"
30
31 #include "support/filetools.h"
32 #include "support/convert.h"
33 #include "support/lstrings.h"
34 #include "support/os.h"
35
36 #include "Buffer.h"
37 #include "BufferParams.h"
38 #include "BufferView.h"
39 #include "BufferList.h"
40 #include "callback.h"
41 #include "debug.h"
42 #include "FuncRequest.h"
43 #include "Layout.h"
44 #include "LyX.h"
45 #include "LyXFunc.h"
46 #include "LyXRC.h"
47 #include "MenuBackend.h"
48 #include "Paragraph.h"
49 #include "Session.h"
50 #include "version.h"
51
52 #include <boost/current_function.hpp>
53
54 #include <QAction>
55 #include <QApplication>
56 #include <QCloseEvent>
57 #include <QDesktopWidget>
58 #include <QDragEnterEvent>
59 #include <QDropEvent>
60 #include <QList>
61 #include <QMenu>
62 #include <QPainter>
63 #include <QPixmap>
64 #include <QPushButton>
65 #include <QStackedWidget>
66 #include <QStatusBar>
67 #include <QTabBar>
68 #include <QToolBar>
69 #include <QTabWidget>
70 #include <QUrl>
71
72 using std::endl;
73 using std::string;
74 using std::vector;
75
76 namespace lyx {
77
78 using support::FileName;
79 using support::libFileSearch;
80 using support::makeDisplayPath;
81
82 namespace frontend {
83
84 namespace {
85
86 int const statusbar_timer_value = 3000;
87
88 class BackgroundWidget : public QWidget
89 {
90 public:
91         BackgroundWidget(QString const & file, QString const & text)
92         {
93                 splash_ = new QPixmap(file);
94                 if (!splash_) {
95                         lyxerr << "could not load splash screen: '" << fromqstr(file) << "'" << endl;
96                         return;
97                 }
98
99                 QPainter pain(splash_);
100                 pain.setPen(QColor(255, 255, 0));
101                 QFont font;
102                 // The font used to display the version info
103                 font.setStyleHint(QFont::SansSerif);
104                 font.setWeight(QFont::Bold);
105                 font.setPointSize(convert<int>(lyxrc.font_sizes[Font::SIZE_LARGE]));
106                 pain.setFont(font);
107                 pain.drawText(260, 270, text);
108         }
109
110         void paintEvent(QPaintEvent *)
111         {
112                 if (!splash_)
113                         return;
114
115                 int x = (width() - splash_->width()) / 2;
116                 int y = (height() - splash_->height()) / 2;
117                 QPainter pain(this);
118                 pain.drawPixmap(x, y, *splash_);
119         }
120
121 private:
122         QPixmap * splash_;
123 };
124
125
126 class TabWidget : public QTabWidget {
127 public:
128         void showBar(bool show) { tabBar()->setVisible(show); }
129 };
130
131
132 } // namespace anon
133
134
135 struct GuiViewBase::GuiViewPrivate
136 {
137         string cur_title;
138
139         int posx_offset;
140         int posy_offset;
141
142         TabWidget * tab_widget_;
143         QStackedWidget * stack_widget_;
144         BackgroundWidget * bg_widget_;
145         /// view's menubar
146         GuiMenubar * menubar_;
147         /// view's toolbars
148         GuiToolbars * toolbars_;
149         ///
150         docstring current_layout;
151
152         GuiViewPrivate() : posx_offset(0), posy_offset(0) {}
153
154         unsigned int smallIconSize;
155         unsigned int normalIconSize;
156         unsigned int bigIconSize;
157         // static needed by "New Window"
158         static unsigned int lastIconSize;
159
160         QMenu * toolBarPopup(GuiViewBase * parent)
161         {
162                 // FIXME: translation
163                 QMenu * menu = new QMenu(parent);
164                 QActionGroup * iconSizeGroup = new QActionGroup(parent);
165
166                 QAction * smallIcons = new QAction(iconSizeGroup);
167                 smallIcons->setText(qt_("Small-sized icons"));
168                 smallIcons->setCheckable(true);
169                 QObject::connect(smallIcons, SIGNAL(triggered()), parent, SLOT(smallSizedIcons()));
170                 menu->addAction(smallIcons);
171
172                 QAction * normalIcons = new QAction(iconSizeGroup);
173                 normalIcons->setText(qt_("Normal-sized icons"));
174                 normalIcons->setCheckable(true);
175                 QObject::connect(normalIcons, SIGNAL(triggered()), parent, SLOT(normalSizedIcons()));
176                 menu->addAction(normalIcons);
177
178                 QAction * bigIcons = new QAction(iconSizeGroup);
179                 bigIcons->setText(qt_("Big-sized icons"));
180                 bigIcons->setCheckable(true);
181                 QObject::connect(bigIcons, SIGNAL(triggered()), parent, SLOT(bigSizedIcons()));
182                 menu->addAction(bigIcons);
183
184                 unsigned int cur = parent->iconSize().width();
185                 if ( cur == parent->d.smallIconSize)
186                         smallIcons->setChecked(true);
187                 else if (cur == parent->d.normalIconSize)
188                         normalIcons->setChecked(true);
189                 else if (cur == parent->d.bigIconSize)
190                         bigIcons->setChecked(true);
191
192                 return menu;
193         }
194
195         void initBackground()
196         {
197                 bg_widget_ = 0;
198                 LYXERR(Debug::GUI) << "show banner: " << lyxrc.show_banner << endl;
199                 /// The text to be written on top of the pixmap
200                 QString const text = lyx_version ? QString(lyx_version) : qt_("unknown version");
201                 FileName const file = support::libFileSearch("images", "banner", "png");
202                 if (file.empty())
203                         return;
204
205                 bg_widget_ = new BackgroundWidget(toqstr(file.absFilename()), text);
206         }
207
208         void setBackground()
209         {
210                 if (!bg_widget_)
211                         return;
212
213                 stack_widget_->setCurrentWidget(bg_widget_);
214                 bg_widget_->setUpdatesEnabled(true);
215         }
216 };
217
218
219 unsigned int GuiViewBase::GuiViewPrivate::lastIconSize = 0;
220
221
222 GuiViewBase::GuiViewBase(int id)
223         : QMainWindow(), LyXView(id), quitting_by_menu_(false),
224           d(*new GuiViewPrivate)
225 {
226         // Qt bug? signal lastWindowClosed does not work
227         setAttribute(Qt::WA_QuitOnClose, false);
228         setAttribute(Qt::WA_DeleteOnClose, true);
229
230         // hardcode here the platform specific icon size
231         d.smallIconSize = 14;   // scaling problems
232         d.normalIconSize = 20;  // ok, default
233         d.bigIconSize = 26;             // better for some math icons
234
235 #ifndef Q_WS_MACX
236         // assign an icon to main form. We do not do it under Qt/Mac,
237         // since the icon is provided in the application bundle.
238         FileName const iconname = libFileSearch("images", "lyx", "png");
239         if (!iconname.empty())
240                 setWindowIcon(QPixmap(toqstr(iconname.absFilename())));
241 #endif
242
243         d.tab_widget_ = new TabWidget;
244
245         QPushButton * closeTabButton = new QPushButton(this);
246         FileName const file = support::libFileSearch("images", "closetab", "png");
247         if (!file.empty()) {
248                 QPixmap pm(toqstr(file.absFilename()));
249                 closeTabButton->setIcon(QIcon(pm));
250                 closeTabButton->setMaximumSize(pm.size());
251                 closeTabButton->setFlat(true);
252         } else {
253                 closeTabButton->setText("Close");
254         }
255         closeTabButton->setCursor(Qt::ArrowCursor);
256         closeTabButton->setToolTip(tr("Close tab"));
257         closeTabButton->setEnabled(true);
258
259         QObject::connect(d.tab_widget_, SIGNAL(currentChanged(int)),
260                         this, SLOT(currentTabChanged(int)));
261         QObject::connect(closeTabButton, SIGNAL(clicked()),
262                         this, SLOT(closeCurrentTab()));
263
264         d.tab_widget_->setCornerWidget(closeTabButton);
265 #if QT_VERSION >= 0x040200
266         d.tab_widget_->setUsesScrollButtons(true);
267 #endif
268
269         d.initBackground();
270         if (d.bg_widget_) {
271                 LYXERR(Debug::GUI) << "stack widget!" << endl;
272                 d.stack_widget_ = new QStackedWidget;
273                 d.stack_widget_->addWidget(d.bg_widget_);
274                 d.stack_widget_->addWidget(d.tab_widget_);
275                 setCentralWidget(d.stack_widget_);
276         } else {
277                 d.stack_widget_ = 0;
278                 setCentralWidget(d.tab_widget_);
279         }
280
281         // For Drag&Drop.
282         setAcceptDrops(true);
283 }
284
285
286 GuiViewBase::~GuiViewBase()
287 {
288         delete d.menubar_;
289         delete d.toolbars_;
290         delete &d;
291 }
292
293
294 void GuiViewBase::close()
295 {
296         quitting_by_menu_ = true;
297         while (d.tab_widget_->count()) {
298                 GuiWorkArea * wa = dynamic_cast<GuiWorkArea *>(d.tab_widget_->widget(0));
299                 BOOST_ASSERT(wa);
300                 d.tab_widget_->removeTab(0);
301                 delete wa;
302         }
303         QMainWindow::close();
304         quitting_by_menu_ = false;
305 }
306
307
308 void GuiViewBase::setFocus()
309 {
310         if (d.tab_widget_->count())
311                 d.tab_widget_->currentWidget()->setFocus();
312 }
313
314
315 QMenu* GuiViewBase::createPopupMenu()
316 {
317         return d.toolBarPopup(this);
318 }
319
320
321 void GuiViewBase::init()
322 {
323         // GuiToolbars *must* be initialised before GuiMenubar.
324         d.toolbars_ = new GuiToolbars(*this);
325         // FIXME: GuiToolbars::init() cannot be integrated in the ctor
326         // because LyXFunc::getStatus() needs a properly initialized
327         // GuiToolbars object (for LFUN_TOOLBAR_TOGGLE).
328         d.toolbars_->init();
329         d.menubar_ = new GuiMenubar(this, menubackend);
330
331         statusBar()->setSizeGripEnabled(true);
332
333         QObject::connect(&statusbar_timer_, SIGNAL(timeout()),
334                 this, SLOT(update_view_state_qt()));
335
336         if (d.stack_widget_)
337                 d.stack_widget_->setCurrentWidget(d.bg_widget_);
338 }
339
340
341 void GuiViewBase::closeEvent(QCloseEvent * close_event)
342 {
343         // we may have been called through the close window button
344         // which bypasses the LFUN machinery.
345         if (!quitting_by_menu_ && theApp()->gui().viewIds().size() == 1) {
346                 if (!theBufferList().quitWriteAll()) {
347                         close_event->ignore();
348                         return;
349                 }
350         }
351
352         // Make sure that no LFUN use this close to be closed View.
353         theLyXFunc().setLyXView(0);
354         // Make sure the timer time out will not trigger a statusbar update.
355         statusbar_timer_.stop();
356
357         theApp()->gui().unregisterView(id());
358         if (!theApp()->gui().viewIds().empty()) {
359                 // Just close the window and do nothing else if this is not the
360                 // last window.
361                 close_event->accept();
362                 return;
363         }
364
365         quitting = true;
366
367         // this is the place where we leave the frontend.
368         // it is the only point at which we start quitting.
369         saveGeometry();
370         close_event->accept();
371         // quit the event loop
372         qApp->quit();
373 }
374
375
376 void GuiViewBase::dragEnterEvent(QDragEnterEvent * event)
377 {
378         if (event->mimeData()->hasUrls())
379                 event->accept();
380         /// \todo Ask lyx-devel is this is enough:
381         /// if (event->mimeData()->hasFormat("text/plain"))
382         ///     event->acceptProposedAction();
383 }
384
385
386 void GuiViewBase::dropEvent(QDropEvent* event)
387 {
388         QList<QUrl> files = event->mimeData()->urls();
389         if (files.isEmpty())
390                 return;
391
392         LYXERR(Debug::GUI) << BOOST_CURRENT_FUNCTION
393                 << " got URLs!" << endl;
394         for (int i = 0; i != files.size(); ++i) {
395                 string const file = support::os::internal_path(fromqstr(
396                         files.at(i).toLocalFile()));
397                 if (!file.empty())
398                         dispatch(FuncRequest(LFUN_FILE_OPEN, file));
399         }
400 }
401
402
403 void GuiViewBase::saveGeometry()
404 {
405         static bool done = false;
406         if (done)
407                 return;
408         else
409                 done = true;
410
411         // FIXME:
412         // change the ifdef to 'geometry = normalGeometry();' only
413         // when Trolltech has fixed the broken normalGeometry on X11:
414         // http://www.trolltech.com/developer/task-tracker/index_html?id=119684+&method=entry
415         // Then also the moveEvent, resizeEvent, and the
416         // code for floatingGeometry_ can be removed;
417         // adjust GuiViewBase::setGeometry()
418
419         QRect normal_geometry;
420         int maximized;
421 #ifdef Q_WS_WIN
422         normal_geometry = normalGeometry();
423         if (isMaximized()) {
424                 maximized = CompletelyMaximized;
425         } else {
426                 maximized = NotMaximized;
427         }
428 #else
429         normal_geometry = updateFloatingGeometry();
430
431         QDesktopWidget& dw = *qApp->desktop();
432         QRect desk = dw.availableGeometry(dw.primaryScreen());
433         // Qt bug on Linux: load completely maximized, vert max. save-> frameGeometry().height() is wrong
434         if (isMaximized() && desk.width() <= frameGeometry().width() && desk.height() <= frameGeometry().height()) {
435                 maximized = CompletelyMaximized;
436                 // maximizing does not work when the window is allready hor. or vert. maximized
437                 // Tested only on KDE
438                 int dh = frameGeometry().height() - height();
439                 if (desk.height() <= normal_geometry.height() + dh)
440                         normal_geometry.setHeight(normal_geometry.height() - 1);
441                 int dw = frameGeometry().width() - width();
442                 if (desk.width() <= normal_geometry.width() + dw)
443                         normal_geometry.setWidth(normal_geometry.width() - 1);
444         } else if (desk.height() <= frameGeometry().height()) {
445                 maximized = VerticallyMaximized;
446         } else if (desk.width() <= frameGeometry().width()) {
447                 maximized = HorizontallyMaximized;
448         } else {
449                 maximized = NotMaximized;
450         }
451
452
453 #endif
454         // save windows size and position
455         Session & session = LyX::ref().session();
456         session.sessionInfo().save("WindowWidth", convert<string>(normal_geometry.width()));
457         session.sessionInfo().save("WindowHeight", convert<string>(normal_geometry.height()));
458         session.sessionInfo().save("WindowMaximized", convert<string>(maximized));
459         session.sessionInfo().save("IconSizeXY", convert<string>(iconSize().width()));
460         if (lyxrc.geometry_xysaved) {
461                 session.sessionInfo().save("WindowPosX", convert<string>(normal_geometry.x() + d.posx_offset));
462                 session.sessionInfo().save("WindowPosY", convert<string>(normal_geometry.y() + d.posy_offset));
463         }
464         d.toolbars_->saveToolbarInfo();
465 }
466
467
468 void GuiViewBase::setGeometry(unsigned int width,
469                           unsigned int height,
470                           int posx, int posy,
471                           int maximized,
472                           unsigned int iconSizeXY,
473                           const string & geometryArg)
474 {
475         // use last value (not at startup)
476         if (d.lastIconSize != 0)
477                 setIconSize(d.lastIconSize);
478         else if (iconSizeXY != 0)
479                 setIconSize(iconSizeXY);
480         else
481                 setIconSize(d.normalIconSize);
482
483         // only true when the -geometry option was NOT used
484         if (width != 0 && height != 0) {
485                 if (posx != -1 && posy != -1) {
486                         // if there are startup positioning problems:
487                         // http://doc.trolltech.com/4.2/qdesktopwidget.html
488                         QDesktopWidget& dw = *qApp->desktop();
489                         if (dw.isVirtualDesktop()) {
490                                 if(!dw.geometry().contains(posx, posy)) {
491                                         posx = 50;
492                                         posy = 50;
493                                 }
494                         } else {
495                                 // Which system doesn't use a virtual desktop?
496                                 // TODO save also last screen number and check if it is still availabe.
497                         }
498 #ifdef Q_WS_WIN
499                         // FIXME: use setGeometry only when Trolltech has fixed the qt4/X11 bug
500                         QWidget::setGeometry(posx, posy, width, height);
501 #else
502                         resize(width, height);
503                         move(posx, posy);
504 #endif
505                 } else {
506                         resize(width, height);
507                 }
508
509                 // remember original size
510                 floatingGeometry_ = QRect(posx, posy, width, height);
511
512                 if (maximized != NotMaximized) {
513                         if (maximized == CompletelyMaximized) {
514                                 setWindowState(Qt::WindowMaximized);
515                         } else {
516 #ifndef Q_WS_WIN
517                                 // TODO How to set by the window manager?
518                                 //      setWindowState(Qt::WindowVerticallyMaximized);
519                                 //      is not possible
520                                 QDesktopWidget& dw = *qApp->desktop();
521                                 QRect desk = dw.availableGeometry(dw.primaryScreen());
522                                 if (maximized == VerticallyMaximized)
523                                         resize(width, desk.height());
524                                 if (maximized == HorizontallyMaximized)
525                                         resize(desk.width(), height);
526 #endif
527                         }
528                 }
529         }
530         else
531         {
532                 // FIXME: move this code into parse_geometry() (LyX.cpp)
533 #ifdef Q_WS_WIN
534                 int x, y;
535                 int w, h;
536                 QRegExp re( "[=]*(?:([0-9]+)[xX]([0-9]+)){0,1}[ ]*(?:([+-][0-9]*)([+-][0-9]*)){0,1}" );
537                 re.indexIn(toqstr(geometryArg.c_str()));
538                 w = re.cap(1).toInt();
539                 h = re.cap(2).toInt();
540                 x = re.cap(3).toInt();
541                 y = re.cap(4).toInt();
542                 QWidget::setGeometry( x, y, w, h );
543 #else
544                 // silence warning
545                 (void)geometryArg;
546 #endif
547         }
548         
549         d.setBackground();
550         
551         show();
552
553         // For an unknown reason, the Window title update is not effective for
554         // the second windows up until it is shown on screen (Qt bug?).
555         updateWindowTitle();
556
557         // after show geometry() has changed (Qt bug?)
558         // we compensate the drift when storing the position
559         d.posx_offset = 0;
560         d.posy_offset = 0;
561         if (width != 0 && height != 0)
562                 if (posx != -1 && posy != -1) {
563 #ifdef Q_WS_WIN
564                         d.posx_offset = posx - normalGeometry().x();
565                         d.posy_offset = posy - normalGeometry().y();
566 #else
567 #ifndef Q_WS_MACX
568                         if (maximized == NotMaximized) {
569                                 d.posx_offset = posx - geometry().x();
570                                 d.posy_offset = posy - geometry().y();
571                         }
572 #endif
573 #endif
574                 }
575 }
576
577
578 void GuiViewBase::setWindowTitle(docstring const & t, docstring const & it)
579 {
580         QString title = windowTitle();
581         QString new_title = toqstr(t);
582         if (title != new_title) {
583                 QMainWindow::setWindowTitle(new_title);
584                 QMainWindow::setWindowIconText(toqstr(it));
585         }
586 }
587
588
589 void GuiViewBase::message(docstring const & str)
590 {
591         statusBar()->showMessage(toqstr(str));
592         statusbar_timer_.stop();
593         statusbar_timer_.start(statusbar_timer_value);
594 }
595
596
597 void GuiViewBase::clearMessage()
598 {
599         update_view_state_qt();
600 }
601
602
603 void GuiViewBase::setIconSize(unsigned int size)
604 {
605         d.lastIconSize = size;
606         QMainWindow::setIconSize(QSize(size, size));
607 }
608
609
610 void GuiViewBase::smallSizedIcons()
611 {
612         setIconSize(d.smallIconSize);
613 }
614
615
616 void GuiViewBase::normalSizedIcons()
617 {
618         setIconSize(d.normalIconSize);
619 }
620
621
622 void GuiViewBase::bigSizedIcons()
623 {
624         setIconSize(d.bigIconSize);
625 }
626
627
628 void GuiViewBase::update_view_state_qt()
629 {
630         if (!hasFocus())
631                 return;
632         theLyXFunc().setLyXView(this);
633         statusBar()->showMessage(toqstr(theLyXFunc().viewStatusMessage()));
634         statusbar_timer_.stop();
635 }
636
637
638 void GuiViewBase::closeCurrentTab()
639 {
640         dispatch(FuncRequest(LFUN_BUFFER_CLOSE));
641 }
642
643
644 void GuiViewBase::currentTabChanged(int i)
645 {
646         disconnectBuffer();
647         disconnectBufferView();
648         GuiWorkArea * wa = dynamic_cast<GuiWorkArea *>(d.tab_widget_->widget(i));
649         BOOST_ASSERT(wa);
650         BufferView & bv = wa->bufferView();
651         connectBufferView(bv);
652         connectBuffer(bv.buffer());
653         bv.updateMetrics(false);
654         bv.cursor().fixIfBroken();
655         wa->setUpdatesEnabled(true);
656         wa->redraw();
657         wa->setFocus();
658
659         updateToc();
660         // Buffer-dependent dialogs should be updated or
661         // hidden. This should go here because some dialogs (eg ToC)
662         // require bv_->text.
663         getDialogs().updateBufferDependent(true);
664         updateToolbars();
665         updateLayoutChoice();
666         updateWindowTitle();
667         updateStatusBar();
668
669         LYXERR(Debug::GUI) << "currentTabChanged " << i
670                 << "File" << bv.buffer().fileName() << endl;
671 }
672
673
674 void GuiViewBase::updateStatusBar()
675 {
676         // let the user see the explicit message
677         if (statusbar_timer_.isActive())
678                 return;
679
680         statusBar()->showMessage(toqstr(theLyXFunc().viewStatusMessage()));
681 }
682
683
684 void GuiViewBase::activated(FuncRequest const & func)
685 {
686         dispatch(func);
687 }
688
689
690 bool GuiViewBase::hasFocus() const
691 {
692         return qApp->activeWindow() == this;
693 }
694
695
696 QRect  GuiViewBase::updateFloatingGeometry()
697 {
698         QDesktopWidget& dw = *qApp->desktop();
699         QRect desk = dw.availableGeometry(dw.primaryScreen());
700         // remember only non-maximized sizes
701         if (!isMaximized() && desk.width() > frameGeometry().width() && desk.height() > frameGeometry().height()) {
702                 floatingGeometry_ = QRect(x(), y(), width(), height());
703         }
704         return floatingGeometry_;
705 }
706
707
708 void GuiViewBase::resizeEvent(QResizeEvent *)
709 {
710         updateFloatingGeometry();
711 }
712
713
714 void GuiViewBase::moveEvent(QMoveEvent *)
715 {
716         updateFloatingGeometry();
717 }
718
719
720 bool GuiViewBase::event(QEvent * e)
721 {
722         switch (e->type())
723         {
724         // Useful debug code:
725         //case QEvent::ActivationChange:
726         //case QEvent::WindowDeactivate:
727         //case QEvent::Paint:
728         //case QEvent::Enter:
729         //case QEvent::Leave:
730         //case QEvent::HoverEnter:
731         //case QEvent::HoverLeave:
732         //case QEvent::HoverMove:
733         //case QEvent::StatusTip:
734         //case QEvent::DragEnter:
735         //case QEvent::DragLeave:
736         //case QEvent::Drop:
737         //      break;
738
739         case QEvent::WindowActivate: {
740                 GuiWorkArea * wa = dynamic_cast<GuiWorkArea *>(d.tab_widget_->currentWidget());
741                 BOOST_ASSERT(wa);
742                 BufferView & bv = wa->bufferView();
743                 connectBufferView(bv);
744                 connectBuffer(bv.buffer());
745                 return QMainWindow::event(e);
746         }
747
748         case QEvent::ShortcutOverride: {
749                 QKeyEvent * ke = static_cast<QKeyEvent*>(e);
750                 if (d.tab_widget_->count() == 0) {
751                         theLyXFunc().setLyXView(this);
752                         KeySymbol sym;
753                         setKeySymbol(&sym, ke);
754                         theLyXFunc().processKeySym(sym, q_key_state(ke->modifiers()));
755                         e->accept();
756                         return true;
757                 }
758                 if (ke->key() == Qt::Key_Tab || ke->key() == Qt::Key_Backtab) {
759                         KeySymbol sym;
760                         setKeySymbol(&sym, ke);
761                         currentWorkArea()->processKeySym(sym, NoModifier);
762                         e->accept();
763                         return true;
764                 }
765         }
766         default:
767                 return QMainWindow::event(e);
768         }
769 }
770
771
772 bool GuiViewBase::focusNextPrevChild(bool /*next*/)
773 {
774         setFocus();
775         return true;
776 }
777
778
779 void GuiViewBase::showView()
780 {
781         QMainWindow::setWindowTitle(qt_("LyX"));
782         QMainWindow::show();
783         updateFloatingGeometry();
784 }
785
786
787 void GuiViewBase::busy(bool yes)
788 {
789         if (d.tab_widget_->count()) {
790                 GuiWorkArea * wa = dynamic_cast<GuiWorkArea *>(d.tab_widget_->currentWidget());
791                 BOOST_ASSERT(wa);
792                 wa->setUpdatesEnabled(!yes);
793                 if (yes)
794                         wa->stopBlinkingCursor();
795                 else
796                         wa->startBlinkingCursor();
797         }
798
799         if (yes)
800                 QApplication::setOverrideCursor(Qt::WaitCursor);
801         else
802                 QApplication::restoreOverrideCursor();
803 }
804
805
806 GuiToolbar * GuiViewBase::makeToolbar(ToolbarInfo const & tbinfo, bool newline)
807 {
808         GuiToolbar * toolBar = new GuiToolbar(tbinfo, *this);
809
810         if (tbinfo.flags & ToolbarInfo::TOP) {
811                 if (newline)
812                         addToolBarBreak(Qt::TopToolBarArea);
813                 addToolBar(Qt::TopToolBarArea, toolBar);
814         }
815
816         if (tbinfo.flags & ToolbarInfo::BOTTOM) {
817 // Qt < 4.2.2 cannot handle ToolBarBreak on non-TOP dock.
818 #if (QT_VERSION >= 0x040202)
819                 if (newline)
820                         addToolBarBreak(Qt::BottomToolBarArea);
821 #endif
822                 addToolBar(Qt::BottomToolBarArea, toolBar);
823         }
824
825         if (tbinfo.flags & ToolbarInfo::LEFT) {
826 // Qt < 4.2.2 cannot handle ToolBarBreak on non-TOP dock.
827 #if (QT_VERSION >= 0x040202)
828                 if (newline)
829                         addToolBarBreak(Qt::LeftToolBarArea);
830 #endif
831                 addToolBar(Qt::LeftToolBarArea, toolBar);
832         }
833
834         if (tbinfo.flags & ToolbarInfo::RIGHT) {
835 // Qt < 4.2.2 cannot handle ToolBarBreak on non-TOP dock.
836 #if (QT_VERSION >= 0x040202)
837                 if (newline)
838                         addToolBarBreak(Qt::RightToolBarArea);
839 #endif
840                 addToolBar(Qt::RightToolBarArea, toolBar);
841         }
842
843         // The following does not work so I cannot restore to exact toolbar location
844         /*
845         ToolbarSection::ToolbarInfo & tbinfo = LyX::ref().session().toolbars().load(tbinfo.name);
846         toolBar->move(tbinfo.posx, tbinfo.posy);
847         */
848
849         return toolBar;
850 }
851
852
853 WorkArea * GuiViewBase::workArea(Buffer & buffer)
854 {
855         for (int i = 0; i != d.tab_widget_->count(); ++i) {
856                 GuiWorkArea * wa = dynamic_cast<GuiWorkArea *>(d.tab_widget_->widget(i));
857                 BOOST_ASSERT(wa);
858                 if (&wa->bufferView().buffer() == &buffer)
859                         return wa;
860         }
861         return 0;
862 }
863
864
865 WorkArea * GuiViewBase::addWorkArea(Buffer & buffer)
866 {
867         GuiWorkArea * wa = new GuiWorkArea(buffer, *this);
868         wa->setUpdatesEnabled(false);
869         d.tab_widget_->addTab(wa, toqstr(makeDisplayPath(buffer.fileName(), 30)));
870         wa->bufferView().updateMetrics(false);
871         if (d.stack_widget_)
872                 d.stack_widget_->setCurrentWidget(d.tab_widget_);
873         // Hide tabbar if there's only one tab.
874         d.tab_widget_->showBar(d.tab_widget_->count() > 1);
875         return wa;
876 }
877
878
879 WorkArea * GuiViewBase::currentWorkArea()
880 {
881         if (d.tab_widget_->count() == 0)
882                 return 0;
883         BOOST_ASSERT(dynamic_cast<GuiWorkArea *>(d.tab_widget_->currentWidget()));
884         return dynamic_cast<GuiWorkArea *>(d.tab_widget_->currentWidget());
885 }
886
887
888 WorkArea const * GuiViewBase::currentWorkArea() const
889 {
890         if (d.tab_widget_->count() == 0)
891                 return 0;
892         BOOST_ASSERT(dynamic_cast<GuiWorkArea const *>(d.tab_widget_->currentWidget()));
893         return dynamic_cast<GuiWorkArea const *>(d.tab_widget_->currentWidget());
894 }
895
896
897 void GuiViewBase::setCurrentWorkArea(WorkArea * work_area)
898 {
899         BOOST_ASSERT(work_area);
900
901         // Changing work area can result from opening a file so
902         // update the toc in any case.
903         updateToc();
904
905         GuiWorkArea * wa = dynamic_cast<GuiWorkArea *>(work_area);
906         BOOST_ASSERT(wa);
907         if (wa != d.tab_widget_->currentWidget())
908                 // Switch to the work area.
909                 d.tab_widget_->setCurrentWidget(wa);
910         else
911                 // Make sure the work area is up to date.
912                 currentTabChanged(d.tab_widget_->currentIndex());
913         wa->setFocus();
914 }
915
916
917 void GuiViewBase::removeWorkArea(WorkArea * work_area)
918 {
919         BOOST_ASSERT(work_area);
920         if (work_area == currentWorkArea()) {
921                 disconnectBuffer();
922                 disconnectBufferView();
923         }
924
925         // removing a work area often results from closing a file so
926         // update the toc in any case.
927         updateToc();
928
929         GuiWorkArea * gwa = dynamic_cast<GuiWorkArea *>(work_area);
930         gwa->setUpdatesEnabled(false);
931         BOOST_ASSERT(gwa);
932         int index = d.tab_widget_->indexOf(gwa);
933         d.tab_widget_->removeTab(index);
934
935         delete gwa;
936
937         if (d.tab_widget_->count()) {
938                 // make sure the next work area is enabled.
939                 d.tab_widget_->currentWidget()->setUpdatesEnabled(true);
940                 // Hide tabbar if there's only one tab.
941                 d.tab_widget_->showBar(d.tab_widget_->count() > 1);
942                 return;
943         }
944
945         getDialogs().hideBufferDependent();
946         if (d.stack_widget_) {
947                 // No more work area, switch to the background widget.
948                 d.setBackground();
949         }
950 }
951
952
953 void GuiViewBase::showMiniBuffer(bool visible)
954 {
955         d.toolbars_->showCommandBuffer(visible);
956 }
957
958
959 void GuiViewBase::openMenu(docstring const & name)
960 {
961         d.menubar_->openByName(toqstr(name));
962 }
963
964
965 void GuiViewBase::openLayoutList()
966 {
967         d.toolbars_->openLayoutList();
968 }
969
970
971 void GuiViewBase::updateLayoutChoice()
972 {
973         // Don't show any layouts without a buffer
974         if (!buffer()) {
975                 d.toolbars_->clearLayoutList();
976                 return;
977         }
978
979         // Update the layout display
980         if (d.toolbars_->updateLayoutList(buffer()->params().getTextClassPtr())) {
981                 d.current_layout = buffer()->params().getTextClass().defaultLayoutName();
982         }
983
984         docstring const & layout = currentWorkArea()->bufferView().cursor().
985                 innerParagraph().layout()->name();
986
987         if (layout != d.current_layout) {
988                 d.toolbars_->setLayout(layout);
989                 d.current_layout = layout;
990         }
991 }
992
993
994 bool GuiViewBase::isToolbarVisible(std::string const & id)
995 {
996         return d.toolbars_->visible(id);
997 }
998
999 void GuiViewBase::updateToolbars()
1000 {
1001         WorkArea * wa = currentWorkArea();
1002         if (wa) {
1003                 bool const math =
1004                         wa->bufferView().cursor().inMathed();
1005                 bool const table =
1006                         lyx::getStatus(FuncRequest(LFUN_LAYOUT_TABULAR)).enabled();
1007                 bool const review =
1008                         lyx::getStatus(FuncRequest(LFUN_CHANGES_TRACK)).enabled() &&
1009                         lyx::getStatus(FuncRequest(LFUN_CHANGES_TRACK)).onoff(true);
1010
1011                 d.toolbars_->update(math, table, review);
1012         } else
1013                 d.toolbars_->update(false, false, false);
1014
1015         // update read-only status of open dialogs.
1016         getDialogs().checkStatus();
1017 }
1018
1019
1020 ToolbarInfo * GuiViewBase::getToolbarInfo(string const & name)
1021 {
1022         return d.toolbars_->getToolbarInfo(name);
1023 }
1024
1025
1026 void GuiViewBase::toggleToolbarState(string const & name, bool allowauto)
1027 {
1028         // it is possible to get current toolbar status like this,...
1029         // but I decide to obey the order of ToolbarBackend::flags
1030         // and disregard real toolbar status.
1031         // toolbars_->saveToolbarInfo();
1032         //
1033         // toggle state on/off/auto
1034         d.toolbars_->toggleToolbarState(name, allowauto);
1035         // update toolbar
1036         updateToolbars();
1037 }
1038
1039
1040 } // namespace frontend
1041 } // namespace lyx
1042
1043 #include "GuiView_moc.cpp"