]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/GuiView.cpp
Fix toc widget on window 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                 if (d.tab_widget_->count()) {
741                         GuiWorkArea * wa = dynamic_cast<GuiWorkArea *>(d.tab_widget_->currentWidget());
742                         BOOST_ASSERT(wa);
743                         BufferView & bv = wa->bufferView();
744                         connectBufferView(bv);
745                         connectBuffer(bv.buffer());
746                         // The document structure might have changed in another view.
747                         updateToc();
748                 }
749                 return QMainWindow::event(e);
750
751         case QEvent::ShortcutOverride: {
752                 QKeyEvent * ke = static_cast<QKeyEvent*>(e);
753                 if (d.tab_widget_->count() == 0) {
754                         theLyXFunc().setLyXView(this);
755                         KeySymbol sym;
756                         setKeySymbol(&sym, ke);
757                         theLyXFunc().processKeySym(sym, q_key_state(ke->modifiers()));
758                         e->accept();
759                         return true;
760                 }
761                 if (ke->key() == Qt::Key_Tab || ke->key() == Qt::Key_Backtab) {
762                         KeySymbol sym;
763                         setKeySymbol(&sym, ke);
764                         currentWorkArea()->processKeySym(sym, NoModifier);
765                         e->accept();
766                         return true;
767                 }
768         }
769         default:
770                 return QMainWindow::event(e);
771         }
772 }
773
774
775 bool GuiViewBase::focusNextPrevChild(bool /*next*/)
776 {
777         setFocus();
778         return true;
779 }
780
781
782 void GuiViewBase::showView()
783 {
784         QMainWindow::setWindowTitle(qt_("LyX"));
785         QMainWindow::show();
786         updateFloatingGeometry();
787 }
788
789
790 void GuiViewBase::busy(bool yes)
791 {
792         if (d.tab_widget_->count()) {
793                 GuiWorkArea * wa = dynamic_cast<GuiWorkArea *>(d.tab_widget_->currentWidget());
794                 BOOST_ASSERT(wa);
795                 wa->setUpdatesEnabled(!yes);
796                 if (yes)
797                         wa->stopBlinkingCursor();
798                 else
799                         wa->startBlinkingCursor();
800         }
801
802         if (yes)
803                 QApplication::setOverrideCursor(Qt::WaitCursor);
804         else
805                 QApplication::restoreOverrideCursor();
806 }
807
808
809 GuiToolbar * GuiViewBase::makeToolbar(ToolbarInfo const & tbinfo, bool newline)
810 {
811         GuiToolbar * toolBar = new GuiToolbar(tbinfo, *this);
812
813         if (tbinfo.flags & ToolbarInfo::TOP) {
814                 if (newline)
815                         addToolBarBreak(Qt::TopToolBarArea);
816                 addToolBar(Qt::TopToolBarArea, toolBar);
817         }
818
819         if (tbinfo.flags & ToolbarInfo::BOTTOM) {
820 // Qt < 4.2.2 cannot handle ToolBarBreak on non-TOP dock.
821 #if (QT_VERSION >= 0x040202)
822                 if (newline)
823                         addToolBarBreak(Qt::BottomToolBarArea);
824 #endif
825                 addToolBar(Qt::BottomToolBarArea, toolBar);
826         }
827
828         if (tbinfo.flags & ToolbarInfo::LEFT) {
829 // Qt < 4.2.2 cannot handle ToolBarBreak on non-TOP dock.
830 #if (QT_VERSION >= 0x040202)
831                 if (newline)
832                         addToolBarBreak(Qt::LeftToolBarArea);
833 #endif
834                 addToolBar(Qt::LeftToolBarArea, toolBar);
835         }
836
837         if (tbinfo.flags & ToolbarInfo::RIGHT) {
838 // Qt < 4.2.2 cannot handle ToolBarBreak on non-TOP dock.
839 #if (QT_VERSION >= 0x040202)
840                 if (newline)
841                         addToolBarBreak(Qt::RightToolBarArea);
842 #endif
843                 addToolBar(Qt::RightToolBarArea, toolBar);
844         }
845
846         // The following does not work so I cannot restore to exact toolbar location
847         /*
848         ToolbarSection::ToolbarInfo & tbinfo = LyX::ref().session().toolbars().load(tbinfo.name);
849         toolBar->move(tbinfo.posx, tbinfo.posy);
850         */
851
852         return toolBar;
853 }
854
855
856 WorkArea * GuiViewBase::workArea(Buffer & buffer)
857 {
858         for (int i = 0; i != d.tab_widget_->count(); ++i) {
859                 GuiWorkArea * wa = dynamic_cast<GuiWorkArea *>(d.tab_widget_->widget(i));
860                 BOOST_ASSERT(wa);
861                 if (&wa->bufferView().buffer() == &buffer)
862                         return wa;
863         }
864         return 0;
865 }
866
867
868 WorkArea * GuiViewBase::addWorkArea(Buffer & buffer)
869 {
870         GuiWorkArea * wa = new GuiWorkArea(buffer, *this);
871         wa->setUpdatesEnabled(false);
872         d.tab_widget_->addTab(wa, toqstr(makeDisplayPath(buffer.fileName(), 30)));
873         wa->bufferView().updateMetrics(false);
874         if (d.stack_widget_)
875                 d.stack_widget_->setCurrentWidget(d.tab_widget_);
876         // Hide tabbar if there's only one tab.
877         d.tab_widget_->showBar(d.tab_widget_->count() > 1);
878         return wa;
879 }
880
881
882 WorkArea * GuiViewBase::currentWorkArea()
883 {
884         if (d.tab_widget_->count() == 0)
885                 return 0;
886         BOOST_ASSERT(dynamic_cast<GuiWorkArea *>(d.tab_widget_->currentWidget()));
887         return dynamic_cast<GuiWorkArea *>(d.tab_widget_->currentWidget());
888 }
889
890
891 WorkArea const * GuiViewBase::currentWorkArea() const
892 {
893         if (d.tab_widget_->count() == 0)
894                 return 0;
895         BOOST_ASSERT(dynamic_cast<GuiWorkArea const *>(d.tab_widget_->currentWidget()));
896         return dynamic_cast<GuiWorkArea const *>(d.tab_widget_->currentWidget());
897 }
898
899
900 void GuiViewBase::setCurrentWorkArea(WorkArea * work_area)
901 {
902         BOOST_ASSERT(work_area);
903
904         // Changing work area can result from opening a file so
905         // update the toc in any case.
906         updateToc();
907
908         GuiWorkArea * wa = dynamic_cast<GuiWorkArea *>(work_area);
909         BOOST_ASSERT(wa);
910         if (wa != d.tab_widget_->currentWidget())
911                 // Switch to the work area.
912                 d.tab_widget_->setCurrentWidget(wa);
913         else
914                 // Make sure the work area is up to date.
915                 currentTabChanged(d.tab_widget_->currentIndex());
916         wa->setFocus();
917 }
918
919
920 void GuiViewBase::removeWorkArea(WorkArea * work_area)
921 {
922         BOOST_ASSERT(work_area);
923         if (work_area == currentWorkArea()) {
924                 disconnectBuffer();
925                 disconnectBufferView();
926         }
927
928         // removing a work area often results from closing a file so
929         // update the toc in any case.
930         updateToc();
931
932         GuiWorkArea * gwa = dynamic_cast<GuiWorkArea *>(work_area);
933         gwa->setUpdatesEnabled(false);
934         BOOST_ASSERT(gwa);
935         int index = d.tab_widget_->indexOf(gwa);
936         d.tab_widget_->removeTab(index);
937
938         delete gwa;
939
940         if (d.tab_widget_->count()) {
941                 // make sure the next work area is enabled.
942                 d.tab_widget_->currentWidget()->setUpdatesEnabled(true);
943                 // Hide tabbar if there's only one tab.
944                 d.tab_widget_->showBar(d.tab_widget_->count() > 1);
945                 return;
946         }
947
948         getDialogs().hideBufferDependent();
949         if (d.stack_widget_) {
950                 // No more work area, switch to the background widget.
951                 d.setBackground();
952         }
953 }
954
955
956 void GuiViewBase::showMiniBuffer(bool visible)
957 {
958         d.toolbars_->showCommandBuffer(visible);
959 }
960
961
962 void GuiViewBase::openMenu(docstring const & name)
963 {
964         d.menubar_->openByName(toqstr(name));
965 }
966
967
968 void GuiViewBase::openLayoutList()
969 {
970         d.toolbars_->openLayoutList();
971 }
972
973
974 void GuiViewBase::updateLayoutChoice()
975 {
976         // Don't show any layouts without a buffer
977         if (!buffer()) {
978                 d.toolbars_->clearLayoutList();
979                 return;
980         }
981
982         // Update the layout display
983         if (d.toolbars_->updateLayoutList(buffer()->params().getTextClassPtr())) {
984                 d.current_layout = buffer()->params().getTextClass().defaultLayoutName();
985         }
986
987         docstring const & layout = currentWorkArea()->bufferView().cursor().
988                 innerParagraph().layout()->name();
989
990         if (layout != d.current_layout) {
991                 d.toolbars_->setLayout(layout);
992                 d.current_layout = layout;
993         }
994 }
995
996
997 bool GuiViewBase::isToolbarVisible(std::string const & id)
998 {
999         return d.toolbars_->visible(id);
1000 }
1001
1002 void GuiViewBase::updateToolbars()
1003 {
1004         WorkArea * wa = currentWorkArea();
1005         if (wa) {
1006                 bool const math =
1007                         wa->bufferView().cursor().inMathed();
1008                 bool const table =
1009                         lyx::getStatus(FuncRequest(LFUN_LAYOUT_TABULAR)).enabled();
1010                 bool const review =
1011                         lyx::getStatus(FuncRequest(LFUN_CHANGES_TRACK)).enabled() &&
1012                         lyx::getStatus(FuncRequest(LFUN_CHANGES_TRACK)).onoff(true);
1013
1014                 d.toolbars_->update(math, table, review);
1015         } else
1016                 d.toolbars_->update(false, false, false);
1017
1018         // update read-only status of open dialogs.
1019         getDialogs().checkStatus();
1020 }
1021
1022
1023 ToolbarInfo * GuiViewBase::getToolbarInfo(string const & name)
1024 {
1025         return d.toolbars_->getToolbarInfo(name);
1026 }
1027
1028
1029 void GuiViewBase::toggleToolbarState(string const & name, bool allowauto)
1030 {
1031         // it is possible to get current toolbar status like this,...
1032         // but I decide to obey the order of ToolbarBackend::flags
1033         // and disregard real toolbar status.
1034         // toolbars_->saveToolbarInfo();
1035         //
1036         // toggle state on/off/auto
1037         d.toolbars_->toggleToolbarState(name, allowauto);
1038         // update toolbar
1039         updateToolbars();
1040 }
1041
1042
1043 } // namespace frontend
1044 } // namespace lyx
1045
1046 #include "GuiView_moc.cpp"