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