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