]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/GuiView.cpp
Move private part of BufferView to a private implementation.
[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 "version.h"
51
52 #include <boost/current_function.hpp>
53
54 #include <QAction>
55 #include <QApplication>
56 #include <QCloseEvent>
57 #include <QDesktopWidget>
58 #include <QDragEnterEvent>
59 #include <QDropEvent>
60 #include <QList>
61 #include <QMenu>
62 #include <QPainter>
63 #include <QPixmap>
64 #include <QPushButton>
65 #include <QStackedWidget>
66 #include <QStatusBar>
67 #include <QToolBar>
68 #include <QUrl>
69
70 using std::endl;
71 using std::string;
72 using std::vector;
73
74 namespace lyx {
75
76 using support::FileName;
77 using support::libFileSearch;
78 using support::makeDisplayPath;
79
80 extern bool quitting;
81
82 namespace frontend {
83
84 namespace {
85
86 int const statusbar_timer_value = 3000;
87
88 class BackgroundWidget : public QWidget
89 {
90 public:
91         BackgroundWidget(QString const & file, QString const & text)
92         {
93                 splash_ = new QPixmap(file);
94                 if (!splash_) {
95                         lyxerr << "could not load splash screen: '" << fromqstr(file) << "'" << endl;
96                         return;
97                 }
98
99                 QPainter pain(splash_);
100                 pain.setPen(QColor(255, 255, 0));
101                 QFont font;
102                 // The font used to display the version info
103                 font.setStyleHint(QFont::SansSerif);
104                 font.setWeight(QFont::Bold);
105                 font.setPointSize(convert<int>(lyxrc.font_sizes[Font::SIZE_LARGE]));
106                 pain.setFont(font);
107                 pain.drawText(260, 270, text);
108         }
109
110         void paintEvent(QPaintEvent *)
111         {
112                 if (!splash_)
113                         return;
114
115                 int x = (width() - splash_->width()) / 2;
116                 int y = (height() - splash_->height()) / 2;
117                 QPainter pain(this);
118                 pain.drawPixmap(x, y, *splash_);
119         }
120
121 private:
122         QPixmap * splash_;
123 };
124
125 };
126
127
128 struct GuiViewBase::GuiViewPrivate
129 {
130         string cur_title;
131
132         int posx_offset;
133         int posy_offset;
134
135         TabWorkArea * tab_widget_;
136         QStackedWidget * stack_widget_;
137         BackgroundWidget * bg_widget_;
138         /// view's menubar
139         GuiMenubar * menubar_;
140         /// view's toolbars
141         GuiToolbars * toolbars_;
142         ///
143         docstring current_layout;
144
145         GuiViewPrivate() : posx_offset(0), posy_offset(0) {}
146
147         unsigned int smallIconSize;
148         unsigned int normalIconSize;
149         unsigned int bigIconSize;
150         // static needed by "New Window"
151         static unsigned int lastIconSize;
152
153         QMenu * toolBarPopup(GuiViewBase * parent)
154         {
155                 // FIXME: translation
156                 QMenu * menu = new QMenu(parent);
157                 QActionGroup * iconSizeGroup = new QActionGroup(parent);
158
159                 QAction * smallIcons = new QAction(iconSizeGroup);
160                 smallIcons->setText(qt_("Small-sized icons"));
161                 smallIcons->setCheckable(true);
162                 QObject::connect(smallIcons, SIGNAL(triggered()), parent, SLOT(smallSizedIcons()));
163                 menu->addAction(smallIcons);
164
165                 QAction * normalIcons = new QAction(iconSizeGroup);
166                 normalIcons->setText(qt_("Normal-sized icons"));
167                 normalIcons->setCheckable(true);
168                 QObject::connect(normalIcons, SIGNAL(triggered()), parent, SLOT(normalSizedIcons()));
169                 menu->addAction(normalIcons);
170
171                 QAction * bigIcons = new QAction(iconSizeGroup);
172                 bigIcons->setText(qt_("Big-sized icons"));
173                 bigIcons->setCheckable(true);
174                 QObject::connect(bigIcons, SIGNAL(triggered()), parent, SLOT(bigSizedIcons()));
175                 menu->addAction(bigIcons);
176
177                 unsigned int cur = parent->iconSize().width();
178                 if ( cur == parent->d.smallIconSize)
179                         smallIcons->setChecked(true);
180                 else if (cur == parent->d.normalIconSize)
181                         normalIcons->setChecked(true);
182                 else if (cur == parent->d.bigIconSize)
183                         bigIcons->setChecked(true);
184
185                 return menu;
186         }
187
188         void initBackground()
189         {
190                 bg_widget_ = 0;
191                 LYXERR(Debug::GUI) << "show banner: " << lyxrc.show_banner << endl;
192                 /// The text to be written on top of the pixmap
193                 QString const text = lyx_version ? QString(lyx_version) : qt_("unknown version");
194                 FileName const file = support::libFileSearch("images", "banner", "png");
195                 if (file.empty())
196                         return;
197
198                 bg_widget_ = new BackgroundWidget(toqstr(file.absFilename()), text);
199         }
200
201         void setBackground()
202         {
203                 if (!bg_widget_)
204                         return;
205
206                 stack_widget_->setCurrentWidget(bg_widget_);
207                 bg_widget_->setUpdatesEnabled(true);
208         }
209 };
210
211
212 unsigned int GuiViewBase::GuiViewPrivate::lastIconSize = 0;
213
214
215 GuiViewBase::GuiViewBase(int id)
216         : QMainWindow(), LyXView(id), quitting_by_menu_(false),
217           d(*new GuiViewPrivate)
218 {
219         // Qt bug? signal lastWindowClosed does not work
220         setAttribute(Qt::WA_QuitOnClose, false);
221         setAttribute(Qt::WA_DeleteOnClose, true);
222
223         // hardcode here the platform specific icon size
224         d.smallIconSize = 14;   // scaling problems
225         d.normalIconSize = 20;  // ok, default
226         d.bigIconSize = 26;             // better for some math icons
227
228 #ifndef Q_WS_MACX
229         // assign an icon to main form. We do not do it under Qt/Mac,
230         // since the icon is provided in the application bundle.
231         FileName const iconname = libFileSearch("images", "lyx", "png");
232         if (!iconname.empty())
233                 setWindowIcon(QPixmap(toqstr(iconname.absFilename())));
234 #endif
235
236         d.tab_widget_ = new TabWorkArea;
237         QObject::connect(d.tab_widget_, SIGNAL(currentWorkAreaChanged(GuiWorkArea *)),
238                 this, SLOT(on_currentWorkAreaChanged(GuiWorkArea *)));
239
240         d.initBackground();
241         if (d.bg_widget_) {
242                 LYXERR(Debug::GUI) << "stack widget!" << endl;
243                 d.stack_widget_ = new QStackedWidget;
244                 d.stack_widget_->addWidget(d.bg_widget_);
245                 d.stack_widget_->addWidget(d.tab_widget_);
246                 setCentralWidget(d.stack_widget_);
247         } else {
248                 d.stack_widget_ = 0;
249                 setCentralWidget(d.tab_widget_);
250         }
251
252         // For Drag&Drop.
253         setAcceptDrops(true);
254 }
255
256
257 GuiViewBase::~GuiViewBase()
258 {
259         delete d.menubar_;
260         delete d.toolbars_;
261         delete &d;
262 }
263
264
265 void GuiViewBase::close()
266 {
267         quitting_by_menu_ = true;
268         d.tab_widget_->closeAll();
269         QMainWindow::close();
270         quitting_by_menu_ = false;
271 }
272
273
274 void GuiViewBase::setFocus()
275 {
276         if (d.tab_widget_->count())
277                 d.tab_widget_->currentWidget()->setFocus();
278 }
279
280
281 QMenu* GuiViewBase::createPopupMenu()
282 {
283         return d.toolBarPopup(this);
284 }
285
286
287 void GuiViewBase::init()
288 {
289         // GuiToolbars *must* be initialised before GuiMenubar.
290         d.toolbars_ = new GuiToolbars(*this);
291         // FIXME: GuiToolbars::init() cannot be integrated in the ctor
292         // because LyXFunc::getStatus() needs a properly initialized
293         // GuiToolbars object (for LFUN_TOOLBAR_TOGGLE).
294         d.toolbars_->init();
295         d.menubar_ = new GuiMenubar(this, menubackend);
296
297         statusBar()->setSizeGripEnabled(true);
298
299         QObject::connect(&statusbar_timer_, SIGNAL(timeout()),
300                 this, SLOT(update_view_state_qt()));
301
302         if (d.stack_widget_)
303                 d.stack_widget_->setCurrentWidget(d.bg_widget_);
304 }
305
306
307 void GuiViewBase::closeEvent(QCloseEvent * close_event)
308 {
309         // we may have been called through the close window button
310         // which bypasses the LFUN machinery.
311         if (!quitting_by_menu_ && theApp()->gui().viewIds().size() == 1) {
312                 if (!theBufferList().quitWriteAll()) {
313                         close_event->ignore();
314                         return;
315                 }
316         }
317
318         // Make sure that no LFUN use this close to be closed View.
319         theLyXFunc().setLyXView(0);
320         // Make sure the timer time out will not trigger a statusbar update.
321         statusbar_timer_.stop();
322
323         theApp()->gui().unregisterView(id());
324         if (!theApp()->gui().viewIds().empty()) {
325                 // Just close the window and do nothing else if this is not the
326                 // last window.
327                 close_event->accept();
328                 return;
329         }
330
331         quitting = true;
332
333         // this is the place where we leave the frontend.
334         // it is the only point at which we start quitting.
335         saveGeometry();
336         close_event->accept();
337         // quit the event loop
338         qApp->quit();
339 }
340
341
342 void GuiViewBase::dragEnterEvent(QDragEnterEvent * event)
343 {
344         if (event->mimeData()->hasUrls())
345                 event->accept();
346         /// \todo Ask lyx-devel is this is enough:
347         /// if (event->mimeData()->hasFormat("text/plain"))
348         ///     event->acceptProposedAction();
349 }
350
351
352 void GuiViewBase::dropEvent(QDropEvent* event)
353 {
354         QList<QUrl> files = event->mimeData()->urls();
355         if (files.isEmpty())
356                 return;
357
358         LYXERR(Debug::GUI) << BOOST_CURRENT_FUNCTION
359                 << " got URLs!" << endl;
360         for (int i = 0; i != files.size(); ++i) {
361                 string const file = support::os::internal_path(fromqstr(
362                         files.at(i).toLocalFile()));
363                 if (!file.empty())
364                         dispatch(FuncRequest(LFUN_FILE_OPEN, file));
365         }
366 }
367
368
369 void GuiViewBase::saveGeometry()
370 {
371         static bool done = false;
372         if (done)
373                 return;
374         else
375                 done = true;
376
377         // FIXME:
378         // change the ifdef to 'geometry = normalGeometry();' only
379         // when Trolltech has fixed the broken normalGeometry on X11:
380         // http://www.trolltech.com/developer/task-tracker/index_html?id=119684+&method=entry
381         // Then also the moveEvent, resizeEvent, and the
382         // code for floatingGeometry_ can be removed;
383         // adjust GuiViewBase::setGeometry()
384
385         QRect normal_geometry;
386         int maximized;
387 #ifdef Q_WS_WIN
388         normal_geometry = normalGeometry();
389         if (isMaximized()) {
390                 maximized = CompletelyMaximized;
391         } else {
392                 maximized = NotMaximized;
393         }
394 #else
395         normal_geometry = updateFloatingGeometry();
396
397         QDesktopWidget& dw = *qApp->desktop();
398         QRect desk = dw.availableGeometry(dw.primaryScreen());
399         // Qt bug on Linux: load completely maximized, vert max. save-> frameGeometry().height() is wrong
400         if (isMaximized() && desk.width() <= frameGeometry().width() && desk.height() <= frameGeometry().height()) {
401                 maximized = CompletelyMaximized;
402                 // maximizing does not work when the window is allready hor. or vert. maximized
403                 // Tested only on KDE
404                 int dh = frameGeometry().height() - height();
405                 if (desk.height() <= normal_geometry.height() + dh)
406                         normal_geometry.setHeight(normal_geometry.height() - 1);
407                 int dw = frameGeometry().width() - width();
408                 if (desk.width() <= normal_geometry.width() + dw)
409                         normal_geometry.setWidth(normal_geometry.width() - 1);
410         } else if (desk.height() <= frameGeometry().height()) {
411                 maximized = VerticallyMaximized;
412         } else if (desk.width() <= frameGeometry().width()) {
413                 maximized = HorizontallyMaximized;
414         } else {
415                 maximized = NotMaximized;
416         }
417
418
419 #endif
420         // save windows size and position
421         Session & session = LyX::ref().session();
422         session.sessionInfo().save("WindowWidth", convert<string>(normal_geometry.width()));
423         session.sessionInfo().save("WindowHeight", convert<string>(normal_geometry.height()));
424         session.sessionInfo().save("WindowMaximized", convert<string>(maximized));
425         session.sessionInfo().save("IconSizeXY", convert<string>(iconSize().width()));
426         if (lyxrc.geometry_xysaved) {
427                 session.sessionInfo().save("WindowPosX", convert<string>(normal_geometry.x() + d.posx_offset));
428                 session.sessionInfo().save("WindowPosY", convert<string>(normal_geometry.y() + d.posy_offset));
429         }
430         d.toolbars_->saveToolbarInfo();
431 }
432
433
434 void GuiViewBase::setGeometry(unsigned int width,
435                           unsigned int height,
436                           int posx, int posy,
437                           int maximized,
438                           unsigned int iconSizeXY,
439                           const string & geometryArg)
440 {
441         // use last value (not at startup)
442         if (d.lastIconSize != 0)
443                 setIconSize(d.lastIconSize);
444         else if (iconSizeXY != 0)
445                 setIconSize(iconSizeXY);
446         else
447                 setIconSize(d.normalIconSize);
448
449         // only true when the -geometry option was NOT used
450         if (width != 0 && height != 0) {
451                 if (posx != -1 && posy != -1) {
452                         // if there are startup positioning problems:
453                         // http://doc.trolltech.com/4.2/qdesktopwidget.html
454                         QDesktopWidget& dw = *qApp->desktop();
455                         if (dw.isVirtualDesktop()) {
456                                 if(!dw.geometry().contains(posx, posy)) {
457                                         posx = 50;
458                                         posy = 50;
459                                 }
460                         } else {
461                                 // Which system doesn't use a virtual desktop?
462                                 // TODO save also last screen number and check if it is still availabe.
463                         }
464 #ifdef Q_WS_WIN
465                         // FIXME: use setGeometry only when Trolltech has fixed the qt4/X11 bug
466                         QWidget::setGeometry(posx, posy, width, height);
467 #else
468                         resize(width, height);
469                         move(posx, posy);
470 #endif
471                 } else {
472                         resize(width, height);
473                 }
474
475                 // remember original size
476                 floatingGeometry_ = QRect(posx, posy, width, height);
477
478                 if (maximized != NotMaximized) {
479                         if (maximized == CompletelyMaximized) {
480                                 setWindowState(Qt::WindowMaximized);
481                         } else {
482 #ifndef Q_WS_WIN
483                                 // TODO How to set by the window manager?
484                                 //      setWindowState(Qt::WindowVerticallyMaximized);
485                                 //      is not possible
486                                 QDesktopWidget& dw = *qApp->desktop();
487                                 QRect desk = dw.availableGeometry(dw.primaryScreen());
488                                 if (maximized == VerticallyMaximized)
489                                         resize(width, desk.height());
490                                 if (maximized == HorizontallyMaximized)
491                                         resize(desk.width(), height);
492 #endif
493                         }
494                 }
495         }
496         else
497         {
498                 // FIXME: move this code into parse_geometry() (LyX.cpp)
499 #ifdef Q_WS_WIN
500                 int x, y;
501                 int w, h;
502                 QRegExp re( "[=]*(?:([0-9]+)[xX]([0-9]+)){0,1}[ ]*(?:([+-][0-9]*)([+-][0-9]*)){0,1}" );
503                 re.indexIn(toqstr(geometryArg.c_str()));
504                 w = re.cap(1).toInt();
505                 h = re.cap(2).toInt();
506                 x = re.cap(3).toInt();
507                 y = re.cap(4).toInt();
508                 QWidget::setGeometry( x, y, w, h );
509 #else
510                 // silence warning
511                 (void)geometryArg;
512 #endif
513         }
514         
515         d.setBackground();
516         
517         show();
518
519         // For an unknown reason, the Window title update is not effective for
520         // the second windows up until it is shown on screen (Qt bug?).
521         updateWindowTitle();
522
523         // after show geometry() has changed (Qt bug?)
524         // we compensate the drift when storing the position
525         d.posx_offset = 0;
526         d.posy_offset = 0;
527         if (width != 0 && height != 0)
528                 if (posx != -1 && posy != -1) {
529 #ifdef Q_WS_WIN
530                         d.posx_offset = posx - normalGeometry().x();
531                         d.posy_offset = posy - normalGeometry().y();
532 #else
533 #ifndef Q_WS_MACX
534                         if (maximized == NotMaximized) {
535                                 d.posx_offset = posx - geometry().x();
536                                 d.posy_offset = posy - geometry().y();
537                         }
538 #endif
539 #endif
540                 }
541 }
542
543
544 void GuiViewBase::setWindowTitle(docstring const & t, docstring const & it)
545 {
546         QString title = windowTitle();
547         QString new_title = toqstr(t);
548         if (title != new_title) {
549                 QMainWindow::setWindowTitle(new_title);
550                 QMainWindow::setWindowIconText(toqstr(it));
551         }
552         if (Buffer const * buf = buffer())
553                 d.tab_widget_->setTabText(d.tab_widget_->currentIndex(),
554                         toqstr(makeDisplayPath(buf->fileName(), 30)));
555 }
556
557
558 void GuiViewBase::message(docstring const & str)
559 {
560         statusBar()->showMessage(toqstr(str));
561         statusbar_timer_.stop();
562         statusbar_timer_.start(statusbar_timer_value);
563 }
564
565
566 void GuiViewBase::clearMessage()
567 {
568         update_view_state_qt();
569 }
570
571
572 void GuiViewBase::setIconSize(unsigned int size)
573 {
574         d.lastIconSize = size;
575         QMainWindow::setIconSize(QSize(size, size));
576 }
577
578
579 void GuiViewBase::smallSizedIcons()
580 {
581         setIconSize(d.smallIconSize);
582 }
583
584
585 void GuiViewBase::normalSizedIcons()
586 {
587         setIconSize(d.normalIconSize);
588 }
589
590
591 void GuiViewBase::bigSizedIcons()
592 {
593         setIconSize(d.bigIconSize);
594 }
595
596
597 void GuiViewBase::update_view_state_qt()
598 {
599         if (!hasFocus())
600                 return;
601         theLyXFunc().setLyXView(this);
602         statusBar()->showMessage(toqstr(theLyXFunc().viewStatusMessage()));
603         statusbar_timer_.stop();
604 }
605
606
607 void GuiViewBase::on_currentWorkAreaChanged(GuiWorkArea * wa)
608 {
609         disconnectBuffer();
610         disconnectBufferView();
611         connectBufferView(wa->bufferView());
612         connectBuffer(wa->bufferView().buffer());
613
614         updateToc();
615         // Buffer-dependent dialogs should be updated or
616         // hidden. This should go here because some dialogs (eg ToC)
617         // require bv_->text.
618         getDialogs().updateBufferDependent(true);
619         updateToolbars();
620         updateLayoutChoice();
621         updateWindowTitle();
622         updateStatusBar();
623 }
624
625
626 void GuiViewBase::updateStatusBar()
627 {
628         // let the user see the explicit message
629         if (statusbar_timer_.isActive())
630                 return;
631
632         statusBar()->showMessage(toqstr(theLyXFunc().viewStatusMessage()));
633 }
634
635
636 void GuiViewBase::activated(FuncRequest const & func)
637 {
638         dispatch(func);
639 }
640
641
642 bool GuiViewBase::hasFocus() const
643 {
644         return qApp->activeWindow() == this;
645 }
646
647
648 QRect  GuiViewBase::updateFloatingGeometry()
649 {
650         QDesktopWidget& dw = *qApp->desktop();
651         QRect desk = dw.availableGeometry(dw.primaryScreen());
652         // remember only non-maximized sizes
653         if (!isMaximized() && desk.width() > frameGeometry().width() && desk.height() > frameGeometry().height()) {
654                 floatingGeometry_ = QRect(x(), y(), width(), height());
655         }
656         return floatingGeometry_;
657 }
658
659
660 void GuiViewBase::resizeEvent(QResizeEvent *)
661 {
662         updateFloatingGeometry();
663 }
664
665
666 void GuiViewBase::moveEvent(QMoveEvent *)
667 {
668         updateFloatingGeometry();
669 }
670
671
672 bool GuiViewBase::event(QEvent * e)
673 {
674         switch (e->type())
675         {
676         // Useful debug code:
677         //case QEvent::ActivationChange:
678         //case QEvent::WindowDeactivate:
679         //case QEvent::Paint:
680         //case QEvent::Enter:
681         //case QEvent::Leave:
682         //case QEvent::HoverEnter:
683         //case QEvent::HoverLeave:
684         //case QEvent::HoverMove:
685         //case QEvent::StatusTip:
686         //case QEvent::DragEnter:
687         //case QEvent::DragLeave:
688         //case QEvent::Drop:
689         //      break;
690
691         case QEvent::WindowActivate:
692                 theApp()->setCurrentView(*this);
693                 if (d.tab_widget_->count()) {
694                         GuiWorkArea * wa = dynamic_cast<GuiWorkArea *>(
695                                 d.tab_widget_->currentWidget());
696                         BOOST_ASSERT(wa);
697                         BufferView & bv = wa->bufferView();
698                         connectBufferView(bv);
699                         connectBuffer(bv.buffer());
700                         // The document structure, name and dialogs might have
701                         // changed in another view.
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"