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