]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/GuiView.cpp
Move most of the Gui specific code in Toolbars to its new qt4 specialization GuiToolbars.
[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         QMainWindow::close();
298         quitting_by_menu_ = false;
299 }
300
301
302 void GuiViewBase::setFocus()
303 {
304         if (d.tab_widget_->count())
305                 d.tab_widget_->currentWidget()->setFocus();
306 }
307
308
309 QMenu* GuiViewBase::createPopupMenu()
310 {
311         return d.toolBarPopup(this);
312 }
313
314
315 void GuiViewBase::init()
316 {
317         // GuiToolbars *must* be initialised before GuiMenubar.
318         d.toolbars_ = new GuiToolbars(*this);
319         // FIXME: GuiToolbars::init() cannot be integrated in the ctor
320         // because LyXFunc::getStatus() needs a properly initialized
321         // GuiToolbars object (for LFUN_TOOLBAR_TOGGLE).
322         d.toolbars_->init();
323         d.menubar_ = new GuiMenubar(this, menubackend);
324
325         statusBar()->setSizeGripEnabled(true);
326
327         QObject::connect(&statusbar_timer_, SIGNAL(timeout()),
328                 this, SLOT(update_view_state_qt()));
329
330         if (d.stack_widget_)
331                 d.stack_widget_->setCurrentWidget(d.bg_widget_);
332 }
333
334
335 void GuiViewBase::closeEvent(QCloseEvent * close_event)
336 {
337         // we may have been called through the close window button
338         // which bypasses the LFUN machinery.
339         if (!quitting_by_menu_ && theApp()->gui().viewIds().size() == 1) {
340                 if (!theBufferList().quitWriteAll()) {
341                         close_event->ignore();
342                         return;
343                 }
344         }
345
346         // Make sure that no LFUN use this close to be closed View.
347         theLyXFunc().setLyXView(0);
348         // Make sure the timer time out will not trigger a statusbar update.
349         statusbar_timer_.stop();
350
351         theApp()->gui().unregisterView(id());
352         if (!theApp()->gui().viewIds().empty()) {
353                 // Just close the window and do nothing else if this is not the
354                 // last window.
355                 close_event->accept();
356                 return;
357         }
358
359         quitting = true;
360
361         // this is the place where we leave the frontend.
362         // it is the only point at which we start quitting.
363         saveGeometry();
364         close_event->accept();
365         // quit the event loop
366         qApp->quit();
367 }
368
369
370 void GuiViewBase::dragEnterEvent(QDragEnterEvent * event)
371 {
372         if (event->mimeData()->hasUrls())
373                 event->accept();
374         /// \todo Ask lyx-devel is this is enough:
375         /// if (event->mimeData()->hasFormat("text/plain"))
376         ///     event->acceptProposedAction();
377 }
378
379
380 void GuiViewBase::dropEvent(QDropEvent* event)
381 {
382         QList<QUrl> files = event->mimeData()->urls();
383         if (files.isEmpty())
384                 return;
385
386         LYXERR(Debug::GUI) << BOOST_CURRENT_FUNCTION
387                 << " got URLs!" << endl;
388         for (int i = 0; i != files.size(); ++i) {
389                 string const file = support::os::internal_path(fromqstr(
390                         files.at(i).toLocalFile()));
391                 if (!file.empty())
392                         dispatch(FuncRequest(LFUN_FILE_OPEN, file));
393         }
394 }
395
396
397 void GuiViewBase::saveGeometry()
398 {
399         static bool done = false;
400         if (done)
401                 return;
402         else
403                 done = true;
404
405         // FIXME:
406         // change the ifdef to 'geometry = normalGeometry();' only
407         // when Trolltech has fixed the broken normalGeometry on X11:
408         // http://www.trolltech.com/developer/task-tracker/index_html?id=119684+&method=entry
409         // Then also the moveEvent, resizeEvent, and the
410         // code for floatingGeometry_ can be removed;
411         // adjust GuiViewBase::setGeometry()
412
413         QRect normal_geometry;
414         int maximized;
415 #ifdef Q_WS_WIN
416         normal_geometry = normalGeometry();
417         if (isMaximized()) {
418                 maximized = CompletelyMaximized;
419         } else {
420                 maximized = NotMaximized;
421         }
422 #else
423         normal_geometry = updateFloatingGeometry();
424
425         QDesktopWidget& dw = *qApp->desktop();
426         QRect desk = dw.availableGeometry(dw.primaryScreen());
427         // Qt bug on Linux: load completely maximized, vert max. save-> frameGeometry().height() is wrong
428         if (isMaximized() && desk.width() <= frameGeometry().width() && desk.height() <= frameGeometry().height()) {
429                 maximized = CompletelyMaximized;
430                 // maximizing does not work when the window is allready hor. or vert. maximized
431                 // Tested only on KDE
432                 int dh = frameGeometry().height() - height();
433                 if (desk.height() <= normal_geometry.height() + dh)
434                         normal_geometry.setHeight(normal_geometry.height() - 1);
435                 int dw = frameGeometry().width() - width();
436                 if (desk.width() <= normal_geometry.width() + dw)
437                         normal_geometry.setWidth(normal_geometry.width() - 1);
438         } else if (desk.height() <= frameGeometry().height()) {
439                 maximized = VerticallyMaximized;
440         } else if (desk.width() <= frameGeometry().width()) {
441                 maximized = HorizontallyMaximized;
442         } else {
443                 maximized = NotMaximized;
444         }
445
446
447 #endif
448         // save windows size and position
449         Session & session = LyX::ref().session();
450         session.sessionInfo().save("WindowWidth", convert<string>(normal_geometry.width()));
451         session.sessionInfo().save("WindowHeight", convert<string>(normal_geometry.height()));
452         session.sessionInfo().save("WindowMaximized", convert<string>(maximized));
453         session.sessionInfo().save("IconSizeXY", convert<string>(iconSize().width()));
454         if (lyxrc.geometry_xysaved) {
455                 session.sessionInfo().save("WindowPosX", convert<string>(normal_geometry.x() + d.posx_offset));
456                 session.sessionInfo().save("WindowPosY", convert<string>(normal_geometry.y() + d.posy_offset));
457         }
458         d.toolbars_->saveToolbarInfo();
459 }
460
461
462 void GuiViewBase::setGeometry(unsigned int width,
463                           unsigned int height,
464                           int posx, int posy,
465                           int maximized,
466                           unsigned int iconSizeXY,
467                           const string & geometryArg)
468 {
469         // use last value (not at startup)
470         if (d.lastIconSize != 0)
471                 setIconSize(d.lastIconSize);
472         else if (iconSizeXY != 0)
473                 setIconSize(iconSizeXY);
474         else
475                 setIconSize(d.normalIconSize);
476
477         // only true when the -geometry option was NOT used
478         if (width != 0 && height != 0) {
479                 if (posx != -1 && posy != -1) {
480                         // if there are startup positioning problems:
481                         // http://doc.trolltech.com/4.2/qdesktopwidget.html
482                         QDesktopWidget& dw = *qApp->desktop();
483                         if (dw.isVirtualDesktop()) {
484                                 if(!dw.geometry().contains(posx, posy)) {
485                                         posx = 50;
486                                         posy = 50;
487                                 }
488                         } else {
489                                 // Which system doesn't use a virtual desktop?
490                                 // TODO save also last screen number and check if it is still availabe.
491                         }
492 #ifdef Q_WS_WIN
493                         // FIXME: use setGeometry only when Trolltech has fixed the qt4/X11 bug
494                         QWidget::setGeometry(posx, posy, width, height);
495 #else
496                         resize(width, height);
497                         move(posx, posy);
498 #endif
499                 } else {
500                         resize(width, height);
501                 }
502
503                 // remember original size
504                 floatingGeometry_ = QRect(posx, posy, width, height);
505
506                 if (maximized != NotMaximized) {
507                         if (maximized == CompletelyMaximized) {
508                                 setWindowState(Qt::WindowMaximized);
509                         } else {
510 #ifndef Q_WS_WIN
511                                 // TODO How to set by the window manager?
512                                 //      setWindowState(Qt::WindowVerticallyMaximized);
513                                 //      is not possible
514                                 QDesktopWidget& dw = *qApp->desktop();
515                                 QRect desk = dw.availableGeometry(dw.primaryScreen());
516                                 if (maximized == VerticallyMaximized)
517                                         resize(width, desk.height());
518                                 if (maximized == HorizontallyMaximized)
519                                         resize(desk.width(), height);
520 #endif
521                         }
522                 }
523         }
524         else
525         {
526                 // FIXME: move this code into parse_geometry() (LyX.cpp)
527 #ifdef Q_WS_WIN
528                 int x, y;
529                 int w, h;
530                 QRegExp re( "[=]*(?:([0-9]+)[xX]([0-9]+)){0,1}[ ]*(?:([+-][0-9]*)([+-][0-9]*)){0,1}" );
531                 re.indexIn(toqstr(geometryArg.c_str()));
532                 w = re.cap(1).toInt();
533                 h = re.cap(2).toInt();
534                 x = re.cap(3).toInt();
535                 y = re.cap(4).toInt();
536                 QWidget::setGeometry( x, y, w, h );
537 #else
538                 // silence warning
539                 (void)geometryArg;
540 #endif
541         }
542         
543         d.setBackground();
544         
545         show();
546
547         // For an unknown reason, the Window title update is not effective for
548         // the second windows up until it is shown on screen (Qt bug?).
549         updateWindowTitle();
550
551         // after show geometry() has changed (Qt bug?)
552         // we compensate the drift when storing the position
553         d.posx_offset = 0;
554         d.posy_offset = 0;
555         if (width != 0 && height != 0)
556                 if (posx != -1 && posy != -1) {
557 #ifdef Q_WS_WIN
558                         d.posx_offset = posx - normalGeometry().x();
559                         d.posy_offset = posy - normalGeometry().y();
560 #else
561 #ifndef Q_WS_MACX
562                         if (maximized == NotMaximized) {
563                                 d.posx_offset = posx - geometry().x();
564                                 d.posy_offset = posy - geometry().y();
565                         }
566 #endif
567 #endif
568                 }
569 }
570
571
572 void GuiViewBase::setWindowTitle(docstring const & t, docstring const & it)
573 {
574         QString title = windowTitle();
575         QString new_title = toqstr(t);
576         if (title != new_title) {
577                 QMainWindow::setWindowTitle(new_title);
578                 QMainWindow::setWindowIconText(toqstr(it));
579         }
580 }
581
582
583 void GuiViewBase::message(docstring const & str)
584 {
585         statusBar()->showMessage(toqstr(str));
586         statusbar_timer_.stop();
587         statusbar_timer_.start(statusbar_timer_value);
588 }
589
590
591 void GuiViewBase::clearMessage()
592 {
593         update_view_state_qt();
594 }
595
596
597 void GuiViewBase::setIconSize(unsigned int size)
598 {
599         d.lastIconSize = size;
600         QMainWindow::setIconSize(QSize(size, size));
601 }
602
603
604 void GuiViewBase::smallSizedIcons()
605 {
606         setIconSize(d.smallIconSize);
607 }
608
609
610 void GuiViewBase::normalSizedIcons()
611 {
612         setIconSize(d.normalIconSize);
613 }
614
615
616 void GuiViewBase::bigSizedIcons()
617 {
618         setIconSize(d.bigIconSize);
619 }
620
621
622 void GuiViewBase::update_view_state_qt()
623 {
624         if (!hasFocus())
625                 return;
626         theLyXFunc().setLyXView(this);
627         statusBar()->showMessage(toqstr(theLyXFunc().viewStatusMessage()));
628         statusbar_timer_.stop();
629 }
630
631
632 void GuiViewBase::closeCurrentTab()
633 {
634         dispatch(FuncRequest(LFUN_BUFFER_CLOSE));
635 }
636
637
638 void GuiViewBase::currentTabChanged(int i)
639 {
640         disconnectBuffer();
641         disconnectBufferView();
642         GuiWorkArea * wa = dynamic_cast<GuiWorkArea *>(d.tab_widget_->widget(i));
643         BOOST_ASSERT(wa);
644         BufferView & bv = wa->bufferView();
645         connectBufferView(bv);
646         connectBuffer(bv.buffer());
647         bv.updateMetrics(false);
648         bv.cursor().fixIfBroken();
649         wa->setUpdatesEnabled(true);
650         wa->redraw();
651         wa->setFocus();
652
653         updateToc();
654         // Buffer-dependent dialogs should be updated or
655         // hidden. This should go here because some dialogs (eg ToC)
656         // require bv_->text.
657         getDialogs().updateBufferDependent(true);
658         updateToolbars();
659         updateLayoutChoice();
660         updateWindowTitle();
661         updateStatusBar();
662
663         LYXERR(Debug::GUI) << "currentTabChanged " << i
664                 << "File" << bv.buffer().fileName() << endl;
665 }
666
667
668 void GuiViewBase::updateStatusBar()
669 {
670         // let the user see the explicit message
671         if (statusbar_timer_.isActive())
672                 return;
673
674         statusBar()->showMessage(toqstr(theLyXFunc().viewStatusMessage()));
675 }
676
677
678 void GuiViewBase::activated(FuncRequest const & func)
679 {
680         dispatch(func);
681 }
682
683
684 bool GuiViewBase::hasFocus() const
685 {
686         return qApp->activeWindow() == this;
687 }
688
689
690 QRect  GuiViewBase::updateFloatingGeometry()
691 {
692         QDesktopWidget& dw = *qApp->desktop();
693         QRect desk = dw.availableGeometry(dw.primaryScreen());
694         // remember only non-maximized sizes
695         if (!isMaximized() && desk.width() > frameGeometry().width() && desk.height() > frameGeometry().height()) {
696                 floatingGeometry_ = QRect(x(), y(), width(), height());
697         }
698         return floatingGeometry_;
699 }
700
701
702 void GuiViewBase::resizeEvent(QResizeEvent *)
703 {
704         updateFloatingGeometry();
705 }
706
707
708 void GuiViewBase::moveEvent(QMoveEvent *)
709 {
710         updateFloatingGeometry();
711 }
712
713
714 bool GuiViewBase::event(QEvent * e)
715 {
716         switch (e->type())
717         {
718         // Useful debug code:
719         //case QEvent::WindowActivate:
720         //case QEvent::ActivationChange:
721         //case QEvent::WindowDeactivate:
722         //case QEvent::Paint:
723         //case QEvent::Enter:
724         //case QEvent::Leave:
725         //case QEvent::HoverEnter:
726         //case QEvent::HoverLeave:
727         //case QEvent::HoverMove:
728         //case QEvent::StatusTip:
729         //case QEvent::DragEnter:
730         //case QEvent::DragLeave:
731         //case QEvent::Drop:
732         //      break;
733
734         case QEvent::ShortcutOverride: {
735                 QKeyEvent * ke = static_cast<QKeyEvent*>(e);
736                 if (d.tab_widget_->count() == 0) {
737                         theLyXFunc().setLyXView(this);
738                         KeySymbol sym;
739                         setKeySymbol(&sym, ke);
740                         theLyXFunc().processKeySym(sym, q_key_state(ke->modifiers()));
741                         e->accept();
742                         return true;
743                 }
744                 if (ke->key() == Qt::Key_Tab || ke->key() == Qt::Key_Backtab) {
745                         KeySymbol sym;
746                         setKeySymbol(&sym, ke);
747                         currentWorkArea()->processKeySym(sym, key_modifier::none);
748                         e->accept();
749                         return true;
750                 }
751         }
752         default:
753                 return QMainWindow::event(e);
754         }
755 }
756
757
758 bool GuiViewBase::focusNextPrevChild(bool /*next*/)
759 {
760         setFocus();
761         return true;
762 }
763
764
765 void GuiViewBase::showView()
766 {
767         QMainWindow::setWindowTitle(qt_("LyX"));
768         QMainWindow::show();
769         updateFloatingGeometry();
770 }
771
772
773 void GuiViewBase::busy(bool yes)
774 {
775         if (d.tab_widget_->count()) {
776                 GuiWorkArea * wa = dynamic_cast<GuiWorkArea *>(d.tab_widget_->currentWidget());
777                 BOOST_ASSERT(wa);
778                 wa->setUpdatesEnabled(!yes);
779                 if (yes)
780                         wa->stopBlinkingCursor();
781                 else
782                         wa->startBlinkingCursor();
783         }
784
785         if (yes)
786                 QApplication::setOverrideCursor(Qt::WaitCursor);
787         else
788                 QApplication::restoreOverrideCursor();
789 }
790
791
792 GuiToolbar * GuiViewBase::makeToolbar(ToolbarInfo const & tbinfo, bool newline)
793 {
794         GuiToolbar * toolBar = new GuiToolbar(tbinfo, *this);
795
796         if (tbinfo.flags & ToolbarInfo::TOP) {
797                 if (newline)
798                         addToolBarBreak(Qt::TopToolBarArea);
799                 addToolBar(Qt::TopToolBarArea, toolBar);
800         }
801
802         if (tbinfo.flags & ToolbarInfo::BOTTOM) {
803 // Qt < 4.2.2 cannot handle ToolBarBreak on non-TOP dock.
804 #if (QT_VERSION >= 0x040202)
805                 if (newline)
806                         addToolBarBreak(Qt::BottomToolBarArea);
807 #endif
808                 addToolBar(Qt::BottomToolBarArea, toolBar);
809         }
810
811         if (tbinfo.flags & ToolbarInfo::LEFT) {
812 // Qt < 4.2.2 cannot handle ToolBarBreak on non-TOP dock.
813 #if (QT_VERSION >= 0x040202)
814                 if (newline)
815                         addToolBarBreak(Qt::LeftToolBarArea);
816 #endif
817                 addToolBar(Qt::LeftToolBarArea, toolBar);
818         }
819
820         if (tbinfo.flags & ToolbarInfo::RIGHT) {
821 // Qt < 4.2.2 cannot handle ToolBarBreak on non-TOP dock.
822 #if (QT_VERSION >= 0x040202)
823                 if (newline)
824                         addToolBarBreak(Qt::RightToolBarArea);
825 #endif
826                 addToolBar(Qt::RightToolBarArea, toolBar);
827         }
828
829         // The following does not work so I cannot restore to exact toolbar location
830         /*
831         ToolbarSection::ToolbarInfo & tbinfo = LyX::ref().session().toolbars().load(tbinfo.name);
832         toolBar->move(tbinfo.posx, tbinfo.posy);
833         */
834
835         return toolBar;
836 }
837
838
839 WorkArea * GuiViewBase::workArea(Buffer & buffer)
840 {
841         for (int i = 0; i != d.tab_widget_->count(); ++i) {
842                 GuiWorkArea * wa = dynamic_cast<GuiWorkArea *>(d.tab_widget_->widget(i));
843                 BOOST_ASSERT(wa);
844                 if (&wa->bufferView().buffer() == &buffer)
845                         return wa;
846         }
847         return 0;
848 }
849
850
851 WorkArea * GuiViewBase::addWorkArea(Buffer & buffer)
852 {
853         GuiWorkArea * wa = new GuiWorkArea(buffer, *this);
854         wa->setUpdatesEnabled(false);
855         d.tab_widget_->addTab(wa, toqstr(makeDisplayPath(buffer.fileName(), 30)));
856         wa->bufferView().updateMetrics(false);
857         if (d.stack_widget_)
858                 d.stack_widget_->setCurrentWidget(d.tab_widget_);
859         // Hide tabbar if there's only one tab.
860         d.tab_widget_->showBar(d.tab_widget_->count() > 1);
861         return wa;
862 }
863
864
865 WorkArea * GuiViewBase::currentWorkArea()
866 {
867         if (d.tab_widget_->count() == 0)
868                 return 0;
869         BOOST_ASSERT(dynamic_cast<GuiWorkArea *>(d.tab_widget_->currentWidget()));
870         return dynamic_cast<GuiWorkArea *>(d.tab_widget_->currentWidget());
871 }
872
873
874 WorkArea const * GuiViewBase::currentWorkArea() const
875 {
876         if (d.tab_widget_->count() == 0)
877                 return 0;
878         BOOST_ASSERT(dynamic_cast<GuiWorkArea const *>(d.tab_widget_->currentWidget()));
879         return dynamic_cast<GuiWorkArea const *>(d.tab_widget_->currentWidget());
880 }
881
882
883 void GuiViewBase::setCurrentWorkArea(WorkArea * work_area)
884 {
885         BOOST_ASSERT(work_area);
886
887         // Changing work area can result from opening a file so
888         // update the toc in any case.
889         updateToc();
890
891         GuiWorkArea * wa = dynamic_cast<GuiWorkArea *>(work_area);
892         BOOST_ASSERT(wa);
893         if (wa != d.tab_widget_->currentWidget())
894                 // Switch to the work area.
895                 d.tab_widget_->setCurrentWidget(wa);
896         else
897                 // Make sure the work area is up to date.
898                 currentTabChanged(d.tab_widget_->currentIndex());
899         wa->setFocus();
900 }
901
902
903 void GuiViewBase::removeWorkArea(WorkArea * work_area)
904 {
905         BOOST_ASSERT(work_area);
906         if (work_area == currentWorkArea()) {
907                 disconnectBuffer();
908                 disconnectBufferView();
909         }
910
911         // removing a work area often results from closing a file so
912         // update the toc in any case.
913         updateToc();
914
915         GuiWorkArea * gwa = dynamic_cast<GuiWorkArea *>(work_area);
916         gwa->setUpdatesEnabled(false);
917         BOOST_ASSERT(gwa);
918         int index = d.tab_widget_->indexOf(gwa);
919         d.tab_widget_->removeTab(index);
920
921         delete gwa;
922
923         if (d.tab_widget_->count()) {
924                 // make sure the next work area is enabled.
925                 d.tab_widget_->currentWidget()->setUpdatesEnabled(true);
926                 // Hide tabbar if there's only one tab.
927                 d.tab_widget_->showBar(d.tab_widget_->count() > 1);
928                 return;
929         }
930
931         getDialogs().hideBufferDependent();
932         if (d.stack_widget_) {
933                 // No more work area, switch to the background widget.
934                 d.setBackground();
935         }
936 }
937
938
939 void GuiViewBase::showMiniBuffer(bool visible)
940 {
941         d.toolbars_->display("minibuffer", visible);
942
943         // FIXME: do something about command buffer focus.
944 //      if (t)
945 //              t->focusCommandBuffer();
946 }
947
948
949 void GuiViewBase::openMenu(docstring const & name)
950 {
951         d.menubar_->openByName(toqstr(name));
952 }
953
954
955 void GuiViewBase::openLayoutList()
956 {
957         d.toolbars_->openLayoutList();
958 }
959
960
961 void GuiViewBase::updateLayoutChoice()
962 {
963         // Don't show any layouts without a buffer
964         if (!buffer()) {
965                 d.toolbars_->clearLayoutList();
966                 return;
967         }
968
969         // Update the layout display
970         if (d.toolbars_->updateLayoutList(buffer()->params().getTextClassPtr())) {
971                 d.current_layout = buffer()->params().getTextClass().defaultLayoutName();
972         }
973
974         docstring const & layout = currentWorkArea()->bufferView().cursor().
975                 innerParagraph().layout()->name();
976
977         if (layout != d.current_layout) {
978                 d.toolbars_->setLayout(layout);
979                 d.current_layout = layout;
980         }
981 }
982
983
984 bool GuiViewBase::isToolbarVisible(std::string const & id)
985 {
986         return d.toolbars_->visible(id);
987 }
988
989 void GuiViewBase::updateToolbars()
990 {
991         WorkArea * wa = currentWorkArea();
992         if (wa) {
993                 bool const math =
994                         wa->bufferView().cursor().inMathed();
995                 bool const table =
996                         lyx::getStatus(FuncRequest(LFUN_LAYOUT_TABULAR)).enabled();
997                 bool const review =
998                         lyx::getStatus(FuncRequest(LFUN_CHANGES_TRACK)).enabled() &&
999                         lyx::getStatus(FuncRequest(LFUN_CHANGES_TRACK)).onoff(true);
1000
1001                 d.toolbars_->update(math, table, review);
1002         } else
1003                 d.toolbars_->update(false, false, false);
1004
1005         // update read-only status of open dialogs.
1006         getDialogs().checkStatus();
1007 }
1008
1009
1010 ToolbarInfo * GuiViewBase::getToolbarInfo(string const & name)
1011 {
1012         return d.toolbars_->getToolbarInfo(name);
1013 }
1014
1015
1016 void GuiViewBase::toggleToolbarState(string const & name, bool allowauto)
1017 {
1018         // it is possible to get current toolbar status like this,...
1019         // but I decide to obey the order of ToolbarBackend::flags
1020         // and disregard real toolbar status.
1021         // toolbars_->saveToolbarInfo();
1022         //
1023         // toggle state on/off/auto
1024         d.toolbars_->toggleToolbarState(name, allowauto);
1025         // update toolbar
1026         updateToolbars();
1027 }
1028
1029
1030 } // namespace frontend
1031 } // namespace lyx
1032
1033 #include "GuiView_moc.cpp"