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