]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/GuiView.cpp
Put LyXView on a diet, step 2: get rid of menubar direct access from the core. Menuba...
[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         /// 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), commandbuffer_(0), 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         QObject::connect(menuBar(), SIGNAL(triggered(QAction *)),
313                 this, SLOT(updateMenu(QAction *)));
314
315         toolbars_->init();
316
317         statusBar()->setSizeGripEnabled(true);
318
319         QObject::connect(&statusbar_timer_, SIGNAL(timeout()),
320                 this, SLOT(update_view_state_qt()));
321
322         if (d.stack_widget_)
323                 d.stack_widget_->setCurrentWidget(d.bg_widget_);
324 }
325
326
327 void GuiView::closeEvent(QCloseEvent * close_event)
328 {
329         // we may have been called through the close window button
330         // which bypasses the LFUN machinery.
331         if (!quitting_by_menu_ && theApp()->gui().viewIds().size() == 1) {
332                 if (!theBufferList().quitWriteAll()) {
333                         close_event->ignore();
334                         return;
335                 }
336         }
337
338         // Make sure that no LFUN use this close to be closed View.
339         theLyXFunc().setLyXView(0);
340         // Make sure the timer time out will not trigger a statusbar update.
341         statusbar_timer_.stop();
342
343         theApp()->gui().unregisterView(id());
344         if (!theApp()->gui().viewIds().empty()) {
345                 // Just close the window and do nothing else if this is not the
346                 // last window.
347                 close_event->accept();
348                 return;
349         }
350
351         quitting = true;
352
353         // this is the place where we leave the frontend.
354         // it is the only point at which we start quitting.
355         saveGeometry();
356         close_event->accept();
357         // quit the event loop
358         qApp->quit();
359 }
360
361
362 void GuiView::dragEnterEvent(QDragEnterEvent * event)
363 {
364         if (event->mimeData()->hasUrls())
365                 event->accept();
366         /// \todo Ask lyx-devel is this is enough:
367         /// if (event->mimeData()->hasFormat("text/plain"))
368         ///     event->acceptProposedAction();
369 }
370
371
372 void GuiView::dropEvent(QDropEvent* event)
373 {
374         QList<QUrl> files = event->mimeData()->urls();
375         if (files.isEmpty())
376                 return;
377
378         LYXERR(Debug::GUI) << BOOST_CURRENT_FUNCTION
379                 << " got URLs!" << endl;
380         for (int i = 0; i!=files.size(); ++i) {
381                 string const file = support::os::internal_path(fromqstr(
382                         files.at(i).toLocalFile()));
383                 if (!file.empty())
384                         dispatch(FuncRequest(LFUN_FILE_OPEN, file));
385         }
386 }
387
388
389 void GuiView::saveGeometry()
390 {
391         static bool done = false;
392         if (done)
393                 return;
394         else
395                 done = true;
396
397         // FIXME:
398         // change the ifdef to 'geometry = normalGeometry();' only
399         // when Trolltech has fixed the broken normalGeometry on X11:
400         // http://www.trolltech.com/developer/task-tracker/index_html?id=119684+&method=entry
401         // Then also the moveEvent, resizeEvent, and the
402         // code for floatingGeometry_ can be removed;
403         // adjust GuiView::setGeometry()
404
405         QRect normal_geometry;
406         int maximized;
407 #ifdef Q_WS_WIN
408         normal_geometry = normalGeometry();
409         if (isMaximized()) {
410                 maximized = CompletelyMaximized;
411         } else {
412                 maximized = NotMaximized;
413         }
414 #else
415         normal_geometry = updateFloatingGeometry();
416
417         QDesktopWidget& dw = *qApp->desktop();
418         QRect desk = dw.availableGeometry(dw.primaryScreen());
419         // Qt bug on Linux: load completely maximized, vert max. save-> frameGeometry().height() is wrong
420         if (isMaximized() && desk.width() <= frameGeometry().width() && desk.height() <= frameGeometry().height()) {
421                 maximized = CompletelyMaximized;
422                 // maximizing does not work when the window is allready hor. or vert. maximized
423                 // Tested only on KDE
424                 int dh = frameGeometry().height() - height();
425                 if (desk.height() <= normal_geometry.height() + dh)
426                         normal_geometry.setHeight(normal_geometry.height() - 1);
427                 int dw = frameGeometry().width() - width();
428                 if (desk.width() <= normal_geometry.width() + dw)
429                         normal_geometry.setWidth(normal_geometry.width() - 1);
430         } else if (desk.height() <= frameGeometry().height()) {
431                 maximized = VerticallyMaximized;
432         } else if (desk.width() <= frameGeometry().width()) {
433                 maximized = HorizontallyMaximized;
434         } else {
435                 maximized = NotMaximized;
436         }
437
438
439 #endif
440         // save windows size and position
441         Session & session = LyX::ref().session();
442         session.sessionInfo().save("WindowWidth", convert<string>(normal_geometry.width()));
443         session.sessionInfo().save("WindowHeight", convert<string>(normal_geometry.height()));
444         session.sessionInfo().save("WindowMaximized", convert<string>(maximized));
445         session.sessionInfo().save("IconSizeXY", convert<string>(iconSize().width()));
446         if (lyxrc.geometry_xysaved) {
447                 session.sessionInfo().save("WindowPosX", convert<string>(normal_geometry.x() + d.posx_offset));
448                 session.sessionInfo().save("WindowPosY", convert<string>(normal_geometry.y() + d.posy_offset));
449         }
450         toolbars_->saveToolbarInfo();
451 }
452
453
454 void GuiView::setGeometry(unsigned int width,
455                           unsigned int height,
456                           int posx, int posy,
457                           int maximized,
458                           unsigned int iconSizeXY,
459                           const string & geometryArg)
460 {
461         // use last value (not at startup)
462         if (d.lastIconSize != 0)
463                 setIconSize(d.lastIconSize);
464         else if (iconSizeXY != 0)
465                 setIconSize(iconSizeXY);
466         else
467                 setIconSize(d.normalIconSize);
468
469         // only true when the -geometry option was NOT used
470         if (width != 0 && height != 0) {
471                 if (posx != -1 && posy != -1) {
472                         // if there are startup positioning problems:
473                         // http://doc.trolltech.com/4.2/qdesktopwidget.html
474                         QDesktopWidget& dw = *qApp->desktop();
475                         if (dw.isVirtualDesktop()) {
476                                 if(!dw.geometry().contains(posx, posy)) {
477                                         posx = 50;
478                                         posy = 50;
479                                 }
480                         } else {
481                                 // Which system doesn't use a virtual desktop?
482                                 // TODO save also last screen number and check if it is still availabe.
483                         }
484 #ifdef Q_WS_WIN
485                         // FIXME: use setGeometry only when Trolltech has fixed the qt4/X11 bug
486                         QWidget::setGeometry(posx, posy, width, height);
487 #else
488                         resize(width, height);
489                         move(posx, posy);
490 #endif
491                 } else {
492                         resize(width, height);
493                 }
494
495                 // remember original size
496                 floatingGeometry_ = QRect(posx, posy, width, height);
497
498                 if (maximized != NotMaximized) {
499                         if (maximized == CompletelyMaximized) {
500                                 setWindowState(Qt::WindowMaximized);
501                         } else {
502 #ifndef Q_WS_WIN
503                                 // TODO How to set by the window manager?
504                                 //      setWindowState(Qt::WindowVerticallyMaximized);
505                                 //      is not possible
506                                 QDesktopWidget& dw = *qApp->desktop();
507                                 QRect desk = dw.availableGeometry(dw.primaryScreen());
508                                 if (maximized == VerticallyMaximized)
509                                         resize(width, desk.height());
510                                 if (maximized == HorizontallyMaximized)
511                                         resize(desk.width(), height);
512 #endif
513                         }
514                 }
515         }
516         else
517         {
518                 // FIXME: move this code into parse_geometry() (LyX.cpp)
519 #ifdef Q_WS_WIN
520                 int x, y;
521                 int w, h;
522                 QRegExp re( "[=]*(?:([0-9]+)[xX]([0-9]+)){0,1}[ ]*(?:([+-][0-9]*)([+-][0-9]*)){0,1}" );
523                 re.indexIn(toqstr(geometryArg.c_str()));
524                 w = re.cap(1).toInt();
525                 h = re.cap(2).toInt();
526                 x = re.cap(3).toInt();
527                 y = re.cap(4).toInt();
528                 QWidget::setGeometry( x, y, w, h );
529 #else
530                 // silence warning
531                 (void)geometryArg;
532 #endif
533         }
534         
535         d.setBackground();
536         
537         show();
538
539         // For an unknown reason, the Window title update is not effective for
540         // the second windows up until it is shown on screen (Qt bug?).
541         updateWindowTitle();
542
543         // after show geometry() has changed (Qt bug?)
544         // we compensate the drift when storing the position
545         d.posx_offset = 0;
546         d.posy_offset = 0;
547         if (width != 0 && height != 0)
548                 if (posx != -1 && posy != -1) {
549 #ifdef Q_WS_WIN
550                         d.posx_offset = posx - normalGeometry().x();
551                         d.posy_offset = posy - normalGeometry().y();
552 #else
553 #ifndef Q_WS_MACX
554                         if (maximized == NotMaximized) {
555                                 d.posx_offset = posx - geometry().x();
556                                 d.posy_offset = posy - geometry().y();
557                         }
558 #endif
559 #endif
560                 }
561 }
562
563
564 void GuiView::updateMenu(QAction * /*action*/)
565 {
566         d.menubar_->update();
567 }
568
569
570 void GuiView::setWindowTitle(docstring const & t, docstring const & it)
571 {
572         QString title = windowTitle();
573         QString new_title = toqstr(t);
574         if (title != new_title) {
575                 QMainWindow::setWindowTitle(new_title);
576                 QMainWindow::setWindowIconText(toqstr(it));
577         }
578 }
579
580
581 void GuiView::addCommandBuffer(QToolBar * toolbar)
582 {
583         commandbuffer_ = new QCommandBuffer(this, *controlcommand_);
584         toolbar->addWidget(commandbuffer_);
585 }
586
587
588 void GuiView::message(docstring const & str)
589 {
590         statusBar()->showMessage(toqstr(str));
591         statusbar_timer_.stop();
592         statusbar_timer_.start(statusbar_timer_value);
593 }
594
595
596 void GuiView::clearMessage()
597 {
598         update_view_state_qt();
599 }
600
601
602 void GuiView::setIconSize(unsigned int size)
603 {
604         d.lastIconSize = size;
605         QMainWindow::setIconSize(QSize(size, size));
606 }
607
608
609 void GuiView::smallSizedIcons()
610 {
611         setIconSize(d.smallIconSize);
612 }
613
614
615 void GuiView::normalSizedIcons()
616 {
617         setIconSize(d.normalIconSize);
618 }
619
620
621 void GuiView::bigSizedIcons()
622 {
623         setIconSize(d.bigIconSize);
624 }
625
626
627 void GuiView::update_view_state_qt()
628 {
629         if (!hasFocus())
630                 return;
631         theLyXFunc().setLyXView(this);
632         statusBar()->showMessage(toqstr(theLyXFunc().viewStatusMessage()));
633         statusbar_timer_.stop();
634 }
635
636
637 void GuiView::closeCurrentTab()
638 {
639         dispatch(FuncRequest(LFUN_BUFFER_CLOSE));
640 }
641
642
643 void GuiView::currentTabChanged(int i)
644 {
645         disconnectBuffer();
646         disconnectBufferView();
647         GuiWorkArea * wa = dynamic_cast<GuiWorkArea *>(d.tab_widget_->widget(i));
648         BOOST_ASSERT(wa);
649         BufferView & bv = wa->bufferView();
650         connectBufferView(bv);
651         connectBuffer(bv.buffer());
652         bv.updateMetrics(false);
653         bv.cursor().fixIfBroken();
654         wa->setUpdatesEnabled(true);
655         wa->redraw();
656         wa->setFocus();
657
658         updateToc();
659         // Buffer-dependent dialogs should be updated or
660         // hidden. This should go here because some dialogs (eg ToC)
661         // require bv_->text.
662         getDialogs().updateBufferDependent(true);
663         updateToolbars();
664         updateLayoutChoice();
665         updateWindowTitle();
666         updateStatusBar();
667
668         lyxerr << "currentTabChanged " << i
669                 << "File" << bv.buffer().fileName() << endl;
670 }
671
672
673 void GuiView::updateStatusBar()
674 {
675         // let the user see the explicit message
676         if (statusbar_timer_.isActive())
677                 return;
678
679         statusBar()->showMessage(toqstr(theLyXFunc().viewStatusMessage()));
680 }
681
682
683 void GuiView::activated(FuncRequest const & func)
684 {
685         dispatch(func);
686 }
687
688
689 bool GuiView::hasFocus() const
690 {
691         return qApp->activeWindow() == this;
692 }
693
694
695 QRect  GuiView::updateFloatingGeometry()
696 {
697         QDesktopWidget& dw = *qApp->desktop();
698         QRect desk = dw.availableGeometry(dw.primaryScreen());
699         // remember only non-maximized sizes
700         if (!isMaximized() && desk.width() > frameGeometry().width() && desk.height() > frameGeometry().height()) {
701                 floatingGeometry_ = QRect(x(), y(), width(), height());
702         }
703         return floatingGeometry_;
704 }
705
706
707 void GuiView::resizeEvent(QResizeEvent *)
708 {
709         updateFloatingGeometry();
710 }
711
712
713 void GuiView::moveEvent(QMoveEvent *)
714 {
715         updateFloatingGeometry();
716 }
717
718
719 bool GuiView::event(QEvent * e)
720 {
721         switch (e->type())
722         {
723         // Useful debug code:
724         //case QEvent::WindowActivate:
725         //case QEvent::ActivationChange:
726         //case QEvent::WindowDeactivate:
727         //case QEvent::Paint:
728         //case QEvent::Enter:
729         //case QEvent::Leave:
730         //case QEvent::HoverEnter:
731         //case QEvent::HoverLeave:
732         //case QEvent::HoverMove:
733         //case QEvent::StatusTip:
734         //case QEvent::DragEnter:
735         //case QEvent::DragLeave:
736         //case QEvent::Drop:
737         //      break;
738
739         case QEvent::ShortcutOverride: {
740                 QKeyEvent * ke = static_cast<QKeyEvent*>(e);
741                 if (d.tab_widget_->count() == 0) {
742                         theLyXFunc().setLyXView(this);
743                         boost::shared_ptr<QKeySymbol> sym(new QKeySymbol);
744                         sym->set(ke);
745                         theLyXFunc().processKeySym(sym, q_key_state(ke->modifiers()));
746                         e->accept();
747                         return true;
748                 }
749                 if (ke->key() == Qt::Key_Tab || ke->key() == Qt::Key_Backtab) {
750                         boost::shared_ptr<QKeySymbol> sym(new QKeySymbol);
751                         sym->set(ke);
752                         currentWorkArea()->processKeySym(sym, key_modifier::none);
753                         e->accept();
754                         return true;
755                 }
756         }
757         default:
758                 return QMainWindow::event(e);
759         }
760 }
761
762
763 bool GuiView::focusNextPrevChild(bool /*next*/)
764 {
765         setFocus();
766         return true;
767 }
768
769
770 void GuiView::show()
771 {
772         QMainWindow::setWindowTitle(qt_("LyX"));
773         QMainWindow::show();
774         updateFloatingGeometry();
775 }
776
777
778 void GuiView::busy(bool yes)
779 {
780         if (d.tab_widget_->count()) {
781                 GuiWorkArea * wa = dynamic_cast<GuiWorkArea *>(d.tab_widget_->currentWidget());
782                 BOOST_ASSERT(wa);
783                 wa->setUpdatesEnabled(!yes);
784                 if (yes)
785                         wa->stopBlinkingCursor();
786                 else
787                         wa->startBlinkingCursor();
788         }
789
790         if (yes)
791                 QApplication::setOverrideCursor(Qt::WaitCursor);
792         else
793                 QApplication::restoreOverrideCursor();
794 }
795
796
797 Toolbars::ToolbarPtr GuiView::makeToolbar(ToolbarInfo const & tbinfo, bool newline)
798 {
799         QLToolbar * Tb = new QLToolbar(tbinfo, *this);
800
801         if (tbinfo.flags & ToolbarInfo::TOP) {
802                 if (newline)
803                         addToolBarBreak(Qt::TopToolBarArea);
804                 addToolBar(Qt::TopToolBarArea, Tb);
805         }
806
807         if (tbinfo.flags & ToolbarInfo::BOTTOM) {
808 // Qt < 4.2.2 cannot handle ToolBarBreak on non-TOP dock.
809 #if (QT_VERSION >= 0x040202)
810                 if (newline)
811                         addToolBarBreak(Qt::BottomToolBarArea);
812 #endif
813                 addToolBar(Qt::BottomToolBarArea, Tb);
814         }
815
816         if (tbinfo.flags & ToolbarInfo::LEFT) {
817 // Qt < 4.2.2 cannot handle ToolBarBreak on non-TOP dock.
818 #if (QT_VERSION >= 0x040202)
819                 if (newline)
820                         addToolBarBreak(Qt::LeftToolBarArea);
821 #endif
822                 addToolBar(Qt::LeftToolBarArea, Tb);
823         }
824
825         if (tbinfo.flags & ToolbarInfo::RIGHT) {
826 // Qt < 4.2.2 cannot handle ToolBarBreak on non-TOP dock.
827 #if (QT_VERSION >= 0x040202)
828                 if (newline)
829                         addToolBarBreak(Qt::RightToolBarArea);
830 #endif
831                 addToolBar(Qt::RightToolBarArea, Tb);
832         }
833
834         // The following does not work so I cannot restore to exact toolbar location
835         /*
836         ToolbarSection::ToolbarInfo & tbinfo = LyX::ref().session().toolbars().load(tbinfo.name);
837         Tb->move(tbinfo.posx, tbinfo.posy);
838         */
839
840         return Toolbars::ToolbarPtr(Tb);
841 }
842
843
844 WorkArea * GuiView::workArea(Buffer & buffer)
845 {
846         for (int i = 0; i != d.tab_widget_->count(); ++i) {
847                 GuiWorkArea * wa = dynamic_cast<GuiWorkArea *>(d.tab_widget_->widget(i));
848                 BOOST_ASSERT(wa);
849                 if (&wa->bufferView().buffer() == &buffer)
850                         return wa;
851         }
852         return 0;
853 }
854
855
856 WorkArea * GuiView::addWorkArea(Buffer & buffer)
857 {
858         GuiWorkArea * wa = new GuiWorkArea(buffer, *this);
859         d.tab_widget_->addTab(wa, toqstr(makeDisplayPath(buffer.fileName(), 30)));
860         wa->bufferView().updateMetrics(false);
861         if (d.stack_widget_)
862                 d.stack_widget_->setCurrentWidget(d.tab_widget_);
863         // Hide tabbar if there's only one tab.
864         d.tab_widget_->showBar(d.tab_widget_->count() > 1);
865         return wa;
866 }
867
868
869 WorkArea * GuiView::currentWorkArea()
870 {
871         if (d.tab_widget_->count() == 0)
872                 return 0;
873         BOOST_ASSERT(dynamic_cast<GuiWorkArea *>(d.tab_widget_->currentWidget()));
874         return dynamic_cast<GuiWorkArea *>(d.tab_widget_->currentWidget());
875 }
876
877
878 WorkArea const * GuiView::currentWorkArea() const
879 {
880         if (d.tab_widget_->count() == 0)
881                 return 0;
882         BOOST_ASSERT(dynamic_cast<GuiWorkArea const *>(d.tab_widget_->currentWidget()));
883         return dynamic_cast<GuiWorkArea const *>(d.tab_widget_->currentWidget());
884 }
885
886
887 void GuiView::setCurrentWorkArea(WorkArea * work_area)
888 {
889         BOOST_ASSERT(work_area);
890
891         // Changing work area can result from opening a file so
892         // update the toc in any case.
893         updateToc();
894
895         GuiWorkArea * wa = dynamic_cast<GuiWorkArea *>(work_area);
896         BOOST_ASSERT(wa);
897         if (wa != d.tab_widget_->currentWidget())
898                 // Switch to the work area.
899                 d.tab_widget_->setCurrentWidget(wa);
900         else
901                 // Make sure the work area is up to date.
902                 currentTabChanged(d.tab_widget_->currentIndex());
903         wa->setFocus();
904 }
905
906
907 void GuiView::removeWorkArea(WorkArea * work_area)
908 {
909         BOOST_ASSERT(work_area);
910         if (work_area == currentWorkArea()) {
911                 disconnectBuffer();
912                 disconnectBufferView();
913         }
914
915         // removing a work area often results from closing a file so
916         // update the toc in any case.
917         updateToc();
918
919         GuiWorkArea * gwa = dynamic_cast<GuiWorkArea *>(work_area);
920         BOOST_ASSERT(gwa);
921         int index = d.tab_widget_->indexOf(gwa);
922         d.tab_widget_->removeTab(index);
923
924         delete gwa;
925
926         if (d.tab_widget_->count()) {
927                 // make sure the next work area is enabled.
928                 d.tab_widget_->currentWidget()->setUpdatesEnabled(true);
929                 // Hide tabbar if there's only one tab.
930                 d.tab_widget_->showBar(d.tab_widget_->count() > 1);
931                 return;
932         }
933
934         getDialogs().hideBufferDependent();
935         if (d.stack_widget_) {
936                 // No more work area, switch to the background widget.
937                 d.setBackground();
938         }
939 }
940
941
942 void GuiView::showMiniBuffer(bool visible)
943 {
944         if (!commandbuffer_)
945                 return;
946
947         toolbars_->display("minibuffer", visible);
948         commandbuffer_->focus_command();
949 }
950
951
952 void GuiView::openMenu(docstring const & name)
953 {
954         d.menubar_->openByName(name);
955 }
956
957 } // namespace frontend
958 } // namespace lyx
959
960 #include "GuiView_moc.cpp"