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