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