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