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