]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/GuiView.cpp
Fix tab text update when file name changes.
[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         if (Buffer const * buf = buffer())
588                 d.tab_widget_->setTabText(d.tab_widget_->currentIndex(),
589                         toqstr(makeDisplayPath(buf->fileName(), 30)));
590 }
591
592
593 void GuiViewBase::message(docstring const & str)
594 {
595         statusBar()->showMessage(toqstr(str));
596         statusbar_timer_.stop();
597         statusbar_timer_.start(statusbar_timer_value);
598 }
599
600
601 void GuiViewBase::clearMessage()
602 {
603         update_view_state_qt();
604 }
605
606
607 void GuiViewBase::setIconSize(unsigned int size)
608 {
609         d.lastIconSize = size;
610         QMainWindow::setIconSize(QSize(size, size));
611 }
612
613
614 void GuiViewBase::smallSizedIcons()
615 {
616         setIconSize(d.smallIconSize);
617 }
618
619
620 void GuiViewBase::normalSizedIcons()
621 {
622         setIconSize(d.normalIconSize);
623 }
624
625
626 void GuiViewBase::bigSizedIcons()
627 {
628         setIconSize(d.bigIconSize);
629 }
630
631
632 void GuiViewBase::update_view_state_qt()
633 {
634         if (!hasFocus())
635                 return;
636         theLyXFunc().setLyXView(this);
637         statusBar()->showMessage(toqstr(theLyXFunc().viewStatusMessage()));
638         statusbar_timer_.stop();
639 }
640
641
642 void GuiViewBase::closeCurrentTab()
643 {
644         dispatch(FuncRequest(LFUN_BUFFER_CLOSE));
645 }
646
647
648 void GuiViewBase::currentTabChanged(int i)
649 {
650         disconnectBuffer();
651         disconnectBufferView();
652         GuiWorkArea * wa = dynamic_cast<GuiWorkArea *>(d.tab_widget_->widget(i));
653         BOOST_ASSERT(wa);
654         BufferView & bv = wa->bufferView();
655         connectBufferView(bv);
656         connectBuffer(bv.buffer());
657         bv.updateMetrics(false);
658         bv.cursor().fixIfBroken();
659         wa->setUpdatesEnabled(true);
660         wa->redraw();
661         wa->setFocus();
662
663         updateToc();
664         // Buffer-dependent dialogs should be updated or
665         // hidden. This should go here because some dialogs (eg ToC)
666         // require bv_->text.
667         getDialogs().updateBufferDependent(true);
668         updateToolbars();
669         updateLayoutChoice();
670         updateWindowTitle();
671         updateStatusBar();
672
673         LYXERR(Debug::GUI) << "currentTabChanged " << i
674                 << "File" << bv.buffer().fileName() << endl;
675 }
676
677
678 void GuiViewBase::updateStatusBar()
679 {
680         // let the user see the explicit message
681         if (statusbar_timer_.isActive())
682                 return;
683
684         statusBar()->showMessage(toqstr(theLyXFunc().viewStatusMessage()));
685 }
686
687
688 void GuiViewBase::activated(FuncRequest const & func)
689 {
690         dispatch(func);
691 }
692
693
694 bool GuiViewBase::hasFocus() const
695 {
696         return qApp->activeWindow() == this;
697 }
698
699
700 QRect  GuiViewBase::updateFloatingGeometry()
701 {
702         QDesktopWidget& dw = *qApp->desktop();
703         QRect desk = dw.availableGeometry(dw.primaryScreen());
704         // remember only non-maximized sizes
705         if (!isMaximized() && desk.width() > frameGeometry().width() && desk.height() > frameGeometry().height()) {
706                 floatingGeometry_ = QRect(x(), y(), width(), height());
707         }
708         return floatingGeometry_;
709 }
710
711
712 void GuiViewBase::resizeEvent(QResizeEvent *)
713 {
714         updateFloatingGeometry();
715 }
716
717
718 void GuiViewBase::moveEvent(QMoveEvent *)
719 {
720         updateFloatingGeometry();
721 }
722
723
724 bool GuiViewBase::event(QEvent * e)
725 {
726         switch (e->type())
727         {
728         // Useful debug code:
729         //case QEvent::ActivationChange:
730         //case QEvent::WindowDeactivate:
731         //case QEvent::Paint:
732         //case QEvent::Enter:
733         //case QEvent::Leave:
734         //case QEvent::HoverEnter:
735         //case QEvent::HoverLeave:
736         //case QEvent::HoverMove:
737         //case QEvent::StatusTip:
738         //case QEvent::DragEnter:
739         //case QEvent::DragLeave:
740         //case QEvent::Drop:
741         //      break;
742
743         case QEvent::WindowActivate:
744                 if (d.tab_widget_->count()) {
745                         GuiWorkArea * wa = dynamic_cast<GuiWorkArea *>(
746                                 d.tab_widget_->currentWidget());
747                         BOOST_ASSERT(wa);
748                         BufferView & bv = wa->bufferView();
749                         connectBufferView(bv);
750                         connectBuffer(bv.buffer());
751                         // The document structure, name and dialogs might have
752                         // changed in another view.
753                         updateToc();
754                         updateWindowTitle();
755                         getDialogs().updateBufferDependent(true);
756                 }
757                 return QMainWindow::event(e);
758
759         case QEvent::ShortcutOverride: {
760                 QKeyEvent * ke = static_cast<QKeyEvent*>(e);
761                 if (d.tab_widget_->count() == 0) {
762                         theLyXFunc().setLyXView(this);
763                         KeySymbol sym;
764                         setKeySymbol(&sym, ke);
765                         theLyXFunc().processKeySym(sym, q_key_state(ke->modifiers()));
766                         e->accept();
767                         return true;
768                 }
769                 if (ke->key() == Qt::Key_Tab || ke->key() == Qt::Key_Backtab) {
770                         KeySymbol sym;
771                         setKeySymbol(&sym, ke);
772                         currentWorkArea()->processKeySym(sym, NoModifier);
773                         e->accept();
774                         return true;
775                 }
776         }
777         default:
778                 return QMainWindow::event(e);
779         }
780 }
781
782
783 bool GuiViewBase::focusNextPrevChild(bool /*next*/)
784 {
785         setFocus();
786         return true;
787 }
788
789
790 void GuiViewBase::showView()
791 {
792         QMainWindow::setWindowTitle(qt_("LyX"));
793         QMainWindow::show();
794         updateFloatingGeometry();
795 }
796
797
798 void GuiViewBase::busy(bool yes)
799 {
800         if (d.tab_widget_->count()) {
801                 GuiWorkArea * wa = dynamic_cast<GuiWorkArea *>(d.tab_widget_->currentWidget());
802                 BOOST_ASSERT(wa);
803                 wa->setUpdatesEnabled(!yes);
804                 if (yes)
805                         wa->stopBlinkingCursor();
806                 else
807                         wa->startBlinkingCursor();
808         }
809
810         if (yes)
811                 QApplication::setOverrideCursor(Qt::WaitCursor);
812         else
813                 QApplication::restoreOverrideCursor();
814 }
815
816
817 GuiToolbar * GuiViewBase::makeToolbar(ToolbarInfo const & tbinfo, bool newline)
818 {
819         GuiToolbar * toolBar = new GuiToolbar(tbinfo, *this);
820
821         if (tbinfo.flags & ToolbarInfo::TOP) {
822                 if (newline)
823                         addToolBarBreak(Qt::TopToolBarArea);
824                 addToolBar(Qt::TopToolBarArea, toolBar);
825         }
826
827         if (tbinfo.flags & ToolbarInfo::BOTTOM) {
828 // Qt < 4.2.2 cannot handle ToolBarBreak on non-TOP dock.
829 #if (QT_VERSION >= 0x040202)
830                 if (newline)
831                         addToolBarBreak(Qt::BottomToolBarArea);
832 #endif
833                 addToolBar(Qt::BottomToolBarArea, toolBar);
834         }
835
836         if (tbinfo.flags & ToolbarInfo::LEFT) {
837 // Qt < 4.2.2 cannot handle ToolBarBreak on non-TOP dock.
838 #if (QT_VERSION >= 0x040202)
839                 if (newline)
840                         addToolBarBreak(Qt::LeftToolBarArea);
841 #endif
842                 addToolBar(Qt::LeftToolBarArea, toolBar);
843         }
844
845         if (tbinfo.flags & ToolbarInfo::RIGHT) {
846 // Qt < 4.2.2 cannot handle ToolBarBreak on non-TOP dock.
847 #if (QT_VERSION >= 0x040202)
848                 if (newline)
849                         addToolBarBreak(Qt::RightToolBarArea);
850 #endif
851                 addToolBar(Qt::RightToolBarArea, toolBar);
852         }
853
854         // The following does not work so I cannot restore to exact toolbar location
855         /*
856         ToolbarSection::ToolbarInfo & tbinfo = LyX::ref().session().toolbars().load(tbinfo.name);
857         toolBar->move(tbinfo.posx, tbinfo.posy);
858         */
859
860         return toolBar;
861 }
862
863
864 WorkArea * GuiViewBase::workArea(Buffer & buffer)
865 {
866         for (int i = 0; i != d.tab_widget_->count(); ++i) {
867                 GuiWorkArea * wa = dynamic_cast<GuiWorkArea *>(d.tab_widget_->widget(i));
868                 BOOST_ASSERT(wa);
869                 if (&wa->bufferView().buffer() == &buffer)
870                         return wa;
871         }
872         return 0;
873 }
874
875
876 WorkArea * GuiViewBase::addWorkArea(Buffer & buffer)
877 {
878         GuiWorkArea * wa = new GuiWorkArea(buffer, *this);
879         wa->setUpdatesEnabled(false);
880         d.tab_widget_->addTab(wa, toqstr(makeDisplayPath(buffer.fileName(), 30)));
881         wa->bufferView().updateMetrics(false);
882         if (d.stack_widget_)
883                 d.stack_widget_->setCurrentWidget(d.tab_widget_);
884         // Hide tabbar if there's only one tab.
885         d.tab_widget_->showBar(d.tab_widget_->count() > 1);
886         return wa;
887 }
888
889
890 WorkArea * GuiViewBase::currentWorkArea()
891 {
892         if (d.tab_widget_->count() == 0)
893                 return 0;
894         BOOST_ASSERT(dynamic_cast<GuiWorkArea *>(d.tab_widget_->currentWidget()));
895         return dynamic_cast<GuiWorkArea *>(d.tab_widget_->currentWidget());
896 }
897
898
899 WorkArea const * GuiViewBase::currentWorkArea() const
900 {
901         if (d.tab_widget_->count() == 0)
902                 return 0;
903         BOOST_ASSERT(dynamic_cast<GuiWorkArea const *>(d.tab_widget_->currentWidget()));
904         return dynamic_cast<GuiWorkArea const *>(d.tab_widget_->currentWidget());
905 }
906
907
908 void GuiViewBase::setCurrentWorkArea(WorkArea * work_area)
909 {
910         BOOST_ASSERT(work_area);
911
912         // Changing work area can result from opening a file so
913         // update the toc in any case.
914         updateToc();
915
916         GuiWorkArea * wa = dynamic_cast<GuiWorkArea *>(work_area);
917         BOOST_ASSERT(wa);
918         if (wa != d.tab_widget_->currentWidget())
919                 // Switch to the work area.
920                 d.tab_widget_->setCurrentWidget(wa);
921         else
922                 // Make sure the work area is up to date.
923                 currentTabChanged(d.tab_widget_->currentIndex());
924         wa->setFocus();
925 }
926
927
928 void GuiViewBase::removeWorkArea(WorkArea * work_area)
929 {
930         BOOST_ASSERT(work_area);
931         if (work_area == currentWorkArea()) {
932                 disconnectBuffer();
933                 disconnectBufferView();
934         }
935
936         // removing a work area often results from closing a file so
937         // update the toc in any case.
938         updateToc();
939
940         GuiWorkArea * gwa = dynamic_cast<GuiWorkArea *>(work_area);
941         gwa->setUpdatesEnabled(false);
942         BOOST_ASSERT(gwa);
943         int index = d.tab_widget_->indexOf(gwa);
944         d.tab_widget_->removeTab(index);
945
946         delete gwa;
947
948         if (d.tab_widget_->count()) {
949                 // make sure the next work area is enabled.
950                 d.tab_widget_->currentWidget()->setUpdatesEnabled(true);
951                 // Hide tabbar if there's only one tab.
952                 d.tab_widget_->showBar(d.tab_widget_->count() > 1);
953                 return;
954         }
955
956         getDialogs().hideBufferDependent();
957         if (d.stack_widget_) {
958                 // No more work area, switch to the background widget.
959                 d.setBackground();
960         }
961 }
962
963
964 void GuiViewBase::showMiniBuffer(bool visible)
965 {
966         d.toolbars_->showCommandBuffer(visible);
967 }
968
969
970 void GuiViewBase::openMenu(docstring const & name)
971 {
972         d.menubar_->openByName(toqstr(name));
973 }
974
975
976 void GuiViewBase::openLayoutList()
977 {
978         d.toolbars_->openLayoutList();
979 }
980
981
982 void GuiViewBase::updateLayoutChoice()
983 {
984         // Don't show any layouts without a buffer
985         if (!buffer()) {
986                 d.toolbars_->clearLayoutList();
987                 return;
988         }
989
990         // Update the layout display
991         if (d.toolbars_->updateLayoutList(buffer()->params().getTextClassPtr())) {
992                 d.current_layout = buffer()->params().getTextClass().defaultLayoutName();
993         }
994
995         docstring const & layout = currentWorkArea()->bufferView().cursor().
996                 innerParagraph().layout()->name();
997
998         if (layout != d.current_layout) {
999                 d.toolbars_->setLayout(layout);
1000                 d.current_layout = layout;
1001         }
1002 }
1003
1004
1005 bool GuiViewBase::isToolbarVisible(std::string const & id)
1006 {
1007         return d.toolbars_->visible(id);
1008 }
1009
1010 void GuiViewBase::updateToolbars()
1011 {
1012         WorkArea * wa = currentWorkArea();
1013         if (wa) {
1014                 bool const math =
1015                         wa->bufferView().cursor().inMathed();
1016                 bool const table =
1017                         lyx::getStatus(FuncRequest(LFUN_LAYOUT_TABULAR)).enabled();
1018                 bool const review =
1019                         lyx::getStatus(FuncRequest(LFUN_CHANGES_TRACK)).enabled() &&
1020                         lyx::getStatus(FuncRequest(LFUN_CHANGES_TRACK)).onoff(true);
1021
1022                 d.toolbars_->update(math, table, review);
1023         } else
1024                 d.toolbars_->update(false, false, false);
1025
1026         // update read-only status of open dialogs.
1027         getDialogs().checkStatus();
1028 }
1029
1030
1031 ToolbarInfo * GuiViewBase::getToolbarInfo(string const & name)
1032 {
1033         return d.toolbars_->getToolbarInfo(name);
1034 }
1035
1036
1037 void GuiViewBase::toggleToolbarState(string const & name, bool allowauto)
1038 {
1039         // it is possible to get current toolbar status like this,...
1040         // but I decide to obey the order of ToolbarBackend::flags
1041         // and disregard real toolbar status.
1042         // toolbars_->saveToolbarInfo();
1043         //
1044         // toggle state on/off/auto
1045         d.toolbars_->toggleToolbarState(name, allowauto);
1046         // update toolbar
1047         updateToolbars();
1048 }
1049
1050
1051 } // namespace frontend
1052 } // namespace lyx
1053
1054 #include "GuiView_moc.cpp"