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