]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/GuiView.cpp
Put LyXView on a diet, step 1: get rid of toolbars direct access from the core. Toolb...
[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 "QCommandBuffer.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 "BufferView.h"
38 #include "BufferList.h"
39 #include "callback.h"
40 #include "debug.h"
41 #include "FuncRequest.h"
42 #include "LyX.h"
43 #include "LyXFunc.h"
44 #include "LyXRC.h"
45 #include "MenuBackend.h"
46 #include "Session.h"
47 #include "version.h"
48
49 #include <QAction>
50 #include <QApplication>
51 #include <QCloseEvent>
52 #include <QDesktopWidget>
53 #include <QDragEnterEvent>
54 #include <QDropEvent>
55 #include <QList>
56 #include <QMimeData>
57 #include <QPainter>
58 #include <QPixmap>
59 #include <QPushButton>
60 #include <QStackedWidget>
61 #include <QStatusBar>
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
144         GuiViewPrivate() : posx_offset(0), posy_offset(0)
145         {}
146
147         unsigned int smallIconSize;
148         unsigned int normalIconSize;
149         unsigned int bigIconSize;
150         // static needed by "New Window"
151         static unsigned int lastIconSize;
152
153         QMenu* toolBarPopup(GuiView *parent)
154         {
155                 // FIXME: translation
156                 QMenu* menu = new QMenu(parent);
157                 QActionGroup *iconSizeGroup = new QActionGroup(parent);
158
159                 QAction *smallIcons = new QAction(iconSizeGroup);
160                 smallIcons->setText(qt_("Small-sized icons"));
161                 smallIcons->setCheckable(true);
162                 QObject::connect(smallIcons, SIGNAL(triggered()), parent, SLOT(smallSizedIcons()));
163                 menu->addAction(smallIcons);
164
165                 QAction *normalIcons = new QAction(iconSizeGroup);
166                 normalIcons->setText(qt_("Normal-sized icons"));
167                 normalIcons->setCheckable(true);
168                 QObject::connect(normalIcons, SIGNAL(triggered()), parent, SLOT(normalSizedIcons()));
169                 menu->addAction(normalIcons);
170
171                 QAction *bigIcons = new QAction(iconSizeGroup);
172                 bigIcons->setText(qt_("Big-sized icons"));
173                 bigIcons->setCheckable(true);
174                 QObject::connect(bigIcons, SIGNAL(triggered()), parent, SLOT(bigSizedIcons()));
175                 menu->addAction(bigIcons);
176
177                 unsigned int cur = parent->iconSize().width();
178                 if ( cur == parent->d.smallIconSize)
179                         smallIcons->setChecked(true);
180                 else if (cur == parent->d.normalIconSize)
181                         normalIcons->setChecked(true);
182                 else if (cur == parent->d.bigIconSize)
183                         bigIcons->setChecked(true);
184
185                 return menu;
186         }
187
188         void initBackground()
189         {
190                 bg_widget_ = 0;
191                 LYXERR(Debug::GUI) << "show banner: " << lyxrc.show_banner << endl;
192                 /// The text to be written on top of the pixmap
193                 QString const text = lyx_version ? QString(lyx_version) : qt_("unknown version");
194                 FileName const file = support::libFileSearch("images", "banner", "png");
195                 if (file.empty())
196                         return;
197
198                 bg_widget_ = new BackgroundWidget(toqstr(file.absFilename()), text);
199         }
200
201         void setBackground()
202         {
203                 if (!bg_widget_)
204                         return;
205
206                 stack_widget_->setCurrentWidget(bg_widget_);
207                 bg_widget_->setUpdatesEnabled(true);
208         }
209 };
210
211
212 unsigned int GuiView::GuiViewPrivate::lastIconSize = 0;
213
214
215 GuiView::GuiView(int id)
216         : QMainWindow(), LyXView(id), commandbuffer_(0), quitting_by_menu_(false),
217           d(*new GuiViewPrivate)
218 {
219         // Qt bug? signal lastWindowClosed does not work
220         setAttribute(Qt::WA_QuitOnClose, false);
221         setAttribute(Qt::WA_DeleteOnClose, true);
222
223         // hardcode here the platform specific icon size
224         d.smallIconSize = 14;   // scaling problems
225         d.normalIconSize = 20;  // ok, default
226         d.bigIconSize = 26;             // better for some math icons
227
228 #ifndef Q_WS_MACX
229         //  assign an icon to main form. We do not do it under Qt/Mac,
230         //  since the icon is provided in the application bundle.
231         FileName const iconname = libFileSearch("images", "lyx", "xpm");
232         if (!iconname.empty())
233                 setWindowIcon(QPixmap(toqstr(iconname.absFilename())));
234 #endif
235
236         d.tab_widget_ = new TabWidget;
237
238         QPushButton * closeTabButton = new QPushButton(this);
239         FileName const file = support::libFileSearch("images", "closetab", "xpm");
240         if (!file.empty()) {
241                 QPixmap pm(toqstr(file.absFilename()));
242                 closeTabButton->setIcon(QIcon(pm));
243                 closeTabButton->setMaximumSize(pm.size());
244                 closeTabButton->setFlat(true);
245         } else {
246                 closeTabButton->setText("Close");
247         }
248         closeTabButton->setCursor(Qt::ArrowCursor);
249         closeTabButton->setToolTip(tr("Close tab"));
250         closeTabButton->setEnabled(true);
251
252         QObject::connect(d.tab_widget_, SIGNAL(currentChanged(int)),
253                         this, SLOT(currentTabChanged(int)));
254         QObject::connect(closeTabButton, SIGNAL(clicked()),
255                         this, SLOT(closeCurrentTab()));
256
257         d.tab_widget_->setCornerWidget(closeTabButton);
258 #if QT_VERSION >= 0x040200
259         d.tab_widget_->setUsesScrollButtons(true);
260 #endif
261
262         d.initBackground();
263         if (d.bg_widget_) {
264                 lyxerr << "stack widget!" << endl;
265                 d.stack_widget_ = new QStackedWidget;
266                 d.stack_widget_->addWidget(d.bg_widget_);
267                 d.stack_widget_->addWidget(d.tab_widget_);
268                 setCentralWidget(d.stack_widget_);
269         } else {
270                 d.stack_widget_ = 0;
271                 setCentralWidget(d.tab_widget_);
272         }
273
274         // For Drag&Drop.
275         setAcceptDrops(true);
276 }
277
278
279 GuiView::~GuiView()
280 {
281         menubar_.reset();
282         delete &d;
283 }
284
285
286 void GuiView::close()
287 {
288         quitting_by_menu_ = true;
289         QMainWindow::close();
290         quitting_by_menu_ = false;
291 }
292
293
294 void GuiView::setFocus()
295 {
296         if (d.tab_widget_->count())
297                 d.tab_widget_->currentWidget()->setFocus();
298 }
299
300
301 QMenu* GuiView::createPopupMenu()
302 {
303         return d.toolBarPopup(this);
304 }
305
306
307 void GuiView::init()
308 {
309         menubar_.reset(new QLMenubar(this, menubackend));
310         QObject::connect(menuBar(), SIGNAL(triggered(QAction *)),
311                 this, SLOT(updateMenu(QAction *)));
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::updateMenu(QAction * /*action*/)
563 {
564         menubar_->update();
565 }
566
567
568 void GuiView::setWindowTitle(docstring const & t, docstring const & it)
569 {
570         QString title = windowTitle();
571         QString new_title = toqstr(t);
572         if (title != new_title) {
573                 QMainWindow::setWindowTitle(new_title);
574                 QMainWindow::setWindowIconText(toqstr(it));
575         }
576 }
577
578
579 void GuiView::addCommandBuffer(QToolBar * toolbar)
580 {
581         commandbuffer_ = new QCommandBuffer(this, *controlcommand_);
582         toolbar->addWidget(commandbuffer_);
583 }
584
585
586 void GuiView::message(docstring const & str)
587 {
588         statusBar()->showMessage(toqstr(str));
589         statusbar_timer_.stop();
590         statusbar_timer_.start(statusbar_timer_value);
591 }
592
593
594 void GuiView::clearMessage()
595 {
596         update_view_state_qt();
597 }
598
599
600 void GuiView::setIconSize(unsigned int size)
601 {
602         d.lastIconSize = size;
603         QMainWindow::setIconSize(QSize(size, size));
604 }
605
606
607 void GuiView::smallSizedIcons()
608 {
609         setIconSize(d.smallIconSize);
610 }
611
612
613 void GuiView::normalSizedIcons()
614 {
615         setIconSize(d.normalIconSize);
616 }
617
618
619 void GuiView::bigSizedIcons()
620 {
621         setIconSize(d.bigIconSize);
622 }
623
624
625 void GuiView::update_view_state_qt()
626 {
627         if (!hasFocus())
628                 return;
629         theLyXFunc().setLyXView(this);
630         statusBar()->showMessage(toqstr(theLyXFunc().viewStatusMessage()));
631         statusbar_timer_.stop();
632 }
633
634
635 void GuiView::closeCurrentTab()
636 {
637         dispatch(FuncRequest(LFUN_BUFFER_CLOSE));
638 }
639
640
641 void GuiView::currentTabChanged(int i)
642 {
643         disconnectBuffer();
644         disconnectBufferView();
645         GuiWorkArea * wa = dynamic_cast<GuiWorkArea *>(d.tab_widget_->widget(i));
646         BOOST_ASSERT(wa);
647         BufferView & bv = wa->bufferView();
648         connectBufferView(bv);
649         connectBuffer(bv.buffer());
650         bv.updateMetrics(false);
651         bv.cursor().fixIfBroken();
652         wa->setUpdatesEnabled(true);
653         wa->redraw();
654         wa->setFocus();
655
656         updateToc();
657         // Buffer-dependent dialogs should be updated or
658         // hidden. This should go here because some dialogs (eg ToC)
659         // require bv_->text.
660         getDialogs().updateBufferDependent(true);
661         updateMenubar();
662         updateToolbars();
663         updateLayoutChoice();
664         updateWindowTitle();
665         updateStatusBar();
666
667         lyxerr << "currentTabChanged " << i
668                 << "File" << bv.buffer().fileName() << endl;
669 }
670
671
672 void GuiView::updateStatusBar()
673 {
674         // let the user see the explicit message
675         if (statusbar_timer_.isActive())
676                 return;
677
678         statusBar()->showMessage(toqstr(theLyXFunc().viewStatusMessage()));
679 }
680
681
682 void GuiView::activated(FuncRequest const & func)
683 {
684         dispatch(func);
685 }
686
687
688 bool GuiView::hasFocus() const
689 {
690         return qApp->activeWindow() == this;
691 }
692
693
694 QRect  GuiView::updateFloatingGeometry()
695 {
696         QDesktopWidget& dw = *qApp->desktop();
697         QRect desk = dw.availableGeometry(dw.primaryScreen());
698         // remember only non-maximized sizes
699         if (!isMaximized() && desk.width() > frameGeometry().width() && desk.height() > frameGeometry().height()) {
700                 floatingGeometry_ = QRect(x(), y(), width(), height());
701         }
702         return floatingGeometry_;
703 }
704
705
706 void GuiView::resizeEvent(QResizeEvent *)
707 {
708         updateFloatingGeometry();
709 }
710
711
712 void GuiView::moveEvent(QMoveEvent *)
713 {
714         updateFloatingGeometry();
715 }
716
717
718 bool GuiView::event(QEvent * e)
719 {
720         switch (e->type())
721         {
722         // Useful debug code:
723         //case QEvent::WindowActivate:
724         //case QEvent::ActivationChange:
725         //case QEvent::WindowDeactivate:
726         //case QEvent::Paint:
727         //case QEvent::Enter:
728         //case QEvent::Leave:
729         //case QEvent::HoverEnter:
730         //case QEvent::HoverLeave:
731         //case QEvent::HoverMove:
732         //case QEvent::StatusTip:
733         //case QEvent::DragEnter:
734         //case QEvent::DragLeave:
735         //case QEvent::Drop:
736         //      break;
737
738         case QEvent::ShortcutOverride: {
739                 QKeyEvent * ke = static_cast<QKeyEvent*>(e);
740                 if (d.tab_widget_->count() == 0) {
741                         theLyXFunc().setLyXView(this);
742                         boost::shared_ptr<QKeySymbol> sym(new QKeySymbol);
743                         sym->set(ke);
744                         theLyXFunc().processKeySym(sym, q_key_state(ke->modifiers()));
745                         e->accept();
746                         return true;
747                 }
748                 if (ke->key() == Qt::Key_Tab || ke->key() == Qt::Key_Backtab) {
749                         boost::shared_ptr<QKeySymbol> sym(new QKeySymbol);
750                         sym->set(ke);
751                         currentWorkArea()->processKeySym(sym, key_modifier::none);
752                         e->accept();
753                         return true;
754                 }
755         }
756         default:
757                 return QMainWindow::event(e);
758         }
759 }
760
761
762 bool GuiView::focusNextPrevChild(bool /*next*/)
763 {
764         setFocus();
765         return true;
766 }
767
768
769 void GuiView::show()
770 {
771         QMainWindow::setWindowTitle(qt_("LyX"));
772         QMainWindow::show();
773         updateFloatingGeometry();
774 }
775
776
777 void GuiView::busy(bool yes)
778 {
779         if (d.tab_widget_->count()) {
780                 GuiWorkArea * wa = dynamic_cast<GuiWorkArea *>(d.tab_widget_->currentWidget());
781                 BOOST_ASSERT(wa);
782                 wa->setUpdatesEnabled(!yes);
783                 if (yes)
784                         wa->stopBlinkingCursor();
785                 else
786                         wa->startBlinkingCursor();
787         }
788
789         if (yes)
790                 QApplication::setOverrideCursor(Qt::WaitCursor);
791         else
792                 QApplication::restoreOverrideCursor();
793 }
794
795
796 Toolbars::ToolbarPtr GuiView::makeToolbar(ToolbarInfo const & tbinfo, bool newline)
797 {
798         QLToolbar * Tb = new QLToolbar(tbinfo, *this);
799
800         if (tbinfo.flags & ToolbarInfo::TOP) {
801                 if (newline)
802                         addToolBarBreak(Qt::TopToolBarArea);
803                 addToolBar(Qt::TopToolBarArea, Tb);
804         }
805
806         if (tbinfo.flags & ToolbarInfo::BOTTOM) {
807 // Qt < 4.2.2 cannot handle ToolBarBreak on non-TOP dock.
808 #if (QT_VERSION >= 0x040202)
809                 if (newline)
810                         addToolBarBreak(Qt::BottomToolBarArea);
811 #endif
812                 addToolBar(Qt::BottomToolBarArea, Tb);
813         }
814
815         if (tbinfo.flags & ToolbarInfo::LEFT) {
816 // Qt < 4.2.2 cannot handle ToolBarBreak on non-TOP dock.
817 #if (QT_VERSION >= 0x040202)
818                 if (newline)
819                         addToolBarBreak(Qt::LeftToolBarArea);
820 #endif
821                 addToolBar(Qt::LeftToolBarArea, Tb);
822         }
823
824         if (tbinfo.flags & ToolbarInfo::RIGHT) {
825 // Qt < 4.2.2 cannot handle ToolBarBreak on non-TOP dock.
826 #if (QT_VERSION >= 0x040202)
827                 if (newline)
828                         addToolBarBreak(Qt::RightToolBarArea);
829 #endif
830                 addToolBar(Qt::RightToolBarArea, Tb);
831         }
832
833         // The following does not work so I cannot restore to exact toolbar location
834         /*
835         ToolbarSection::ToolbarInfo & tbinfo = LyX::ref().session().toolbars().load(tbinfo.name);
836         Tb->move(tbinfo.posx, tbinfo.posy);
837         */
838
839         return Toolbars::ToolbarPtr(Tb);
840 }
841
842
843 WorkArea * GuiView::workArea(Buffer & buffer)
844 {
845         for (int i = 0; i != d.tab_widget_->count(); ++i) {
846                 GuiWorkArea * wa = dynamic_cast<GuiWorkArea *>(d.tab_widget_->widget(i));
847                 BOOST_ASSERT(wa);
848                 if (&wa->bufferView().buffer() == &buffer)
849                         return wa;
850         }
851         return 0;
852 }
853
854
855 WorkArea * GuiView::addWorkArea(Buffer & buffer)
856 {
857         GuiWorkArea * wa = new GuiWorkArea(buffer, *this);
858         d.tab_widget_->addTab(wa, toqstr(makeDisplayPath(buffer.fileName(), 30)));
859         wa->bufferView().updateMetrics(false);
860         if (d.stack_widget_)
861                 d.stack_widget_->setCurrentWidget(d.tab_widget_);
862         // Hide tabbar if there's only one tab.
863         d.tab_widget_->showBar(d.tab_widget_->count() > 1);
864         return wa;
865 }
866
867
868 WorkArea * GuiView::currentWorkArea()
869 {
870         if (d.tab_widget_->count() == 0)
871                 return 0;
872         BOOST_ASSERT(dynamic_cast<GuiWorkArea *>(d.tab_widget_->currentWidget()));
873         return dynamic_cast<GuiWorkArea *>(d.tab_widget_->currentWidget());
874 }
875
876
877 WorkArea const * GuiView::currentWorkArea() const
878 {
879         if (d.tab_widget_->count() == 0)
880                 return 0;
881         BOOST_ASSERT(dynamic_cast<GuiWorkArea const *>(d.tab_widget_->currentWidget()));
882         return dynamic_cast<GuiWorkArea const *>(d.tab_widget_->currentWidget());
883 }
884
885
886 void GuiView::setCurrentWorkArea(WorkArea * work_area)
887 {
888         BOOST_ASSERT(work_area);
889
890         // Changing work area can result from opening a file so
891         // update the toc in any case.
892         updateToc();
893
894         GuiWorkArea * wa = dynamic_cast<GuiWorkArea *>(work_area);
895         BOOST_ASSERT(wa);
896         if (wa != d.tab_widget_->currentWidget())
897                 // Switch to the work area.
898                 d.tab_widget_->setCurrentWidget(wa);
899         else
900                 // Make sure the work area is up to date.
901                 currentTabChanged(d.tab_widget_->currentIndex());
902         wa->setFocus();
903 }
904
905
906 void GuiView::removeWorkArea(WorkArea * work_area)
907 {
908         BOOST_ASSERT(work_area);
909         if (work_area == currentWorkArea()) {
910                 disconnectBuffer();
911                 disconnectBufferView();
912         }
913
914         // removing a work area often results from closing a file so
915         // update the toc in any case.
916         updateToc();
917
918         GuiWorkArea * gwa = dynamic_cast<GuiWorkArea *>(work_area);
919         BOOST_ASSERT(gwa);
920         int index = d.tab_widget_->indexOf(gwa);
921         d.tab_widget_->removeTab(index);
922
923         delete gwa;
924
925         if (d.tab_widget_->count()) {
926                 // make sure the next work area is enabled.
927                 d.tab_widget_->currentWidget()->setUpdatesEnabled(true);
928                 // Hide tabbar if there's only one tab.
929                 d.tab_widget_->showBar(d.tab_widget_->count() > 1);
930                 return;
931         }
932
933         getDialogs().hideBufferDependent();
934         if (d.stack_widget_) {
935                 // No more work area, switch to the background widget.
936                 d.setBackground();
937         }
938 }
939
940
941 void GuiView::showMiniBuffer(bool visible)
942 {
943         if (!commandbuffer_)
944                 return;
945
946         toolbars_->display("minibuffer", visible);
947         commandbuffer_->focus_command();
948 }
949
950 } // namespace frontend
951 } // namespace lyx
952
953 #include "GuiView_moc.cpp"