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