3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Lars Gullik Bjønnes
8 * \author Abdelrazak Younes
11 * Full author contact details are available in file CREDITS.
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"
26 #include "frontends/Application.h"
27 #include "frontends/Dialogs.h"
28 #include "frontends/Gui.h"
29 #include "frontends/WorkArea.h"
31 #include "support/filetools.h"
32 #include "support/convert.h"
33 #include "support/lstrings.h"
34 #include "support/os.h"
37 #include "BufferParams.h"
38 #include "BufferView.h"
39 #include "BufferList.h"
42 #include "FuncRequest.h"
47 #include "MenuBackend.h"
48 #include "Paragraph.h"
50 #include "ToolbarBackend.h"
54 #include <QApplication>
55 #include <QCloseEvent>
56 #include <QDesktopWidget>
57 #include <QDragEnterEvent>
63 #include <QPushButton>
64 #include <QStackedWidget>
75 using support::makeDisplayPath;
83 int const statusbar_timer_value = 3000;
85 class BackgroundWidget : public QWidget
88 BackgroundWidget(QString const & file, QString const & text)
90 splash_ = new QPixmap(file);
92 lyxerr << "could not load splash screen: '" << fromqstr(file) << "'" << endl;
96 QPainter pain(splash_);
97 pain.setPen(QColor(255, 255, 0));
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]));
104 pain.drawText(260, 270, text);
107 void paintEvent(QPaintEvent *)
112 int x = (width() - splash_->width()) / 2;
113 int y = (height() - splash_->height()) / 2;
115 pain.drawPixmap(x, y, *splash_);
125 struct GuiViewBase::GuiViewPrivate
132 TabWorkArea * tab_widget_;
133 QStackedWidget * stack_widget_;
134 BackgroundWidget * bg_widget_;
136 GuiMenubar * menubar_;
138 GuiToolbars * toolbars_;
140 docstring current_layout;
142 GuiViewPrivate() : posx_offset(0), posy_offset(0) {}
144 unsigned int smallIconSize;
145 unsigned int normalIconSize;
146 unsigned int bigIconSize;
147 // static needed by "New Window"
148 static unsigned int lastIconSize;
150 QMenu * toolBarPopup(GuiViewBase * parent)
152 // FIXME: translation
153 QMenu * menu = new QMenu(parent);
154 QActionGroup * iconSizeGroup = new QActionGroup(parent);
156 QAction * smallIcons = new QAction(iconSizeGroup);
157 smallIcons->setText(qt_("Small-sized icons"));
158 smallIcons->setCheckable(true);
159 QObject::connect(smallIcons, SIGNAL(triggered()), parent, SLOT(smallSizedIcons()));
160 menu->addAction(smallIcons);
162 QAction * normalIcons = new QAction(iconSizeGroup);
163 normalIcons->setText(qt_("Normal-sized icons"));
164 normalIcons->setCheckable(true);
165 QObject::connect(normalIcons, SIGNAL(triggered()), parent, SLOT(normalSizedIcons()));
166 menu->addAction(normalIcons);
168 QAction * bigIcons = new QAction(iconSizeGroup);
169 bigIcons->setText(qt_("Big-sized icons"));
170 bigIcons->setCheckable(true);
171 QObject::connect(bigIcons, SIGNAL(triggered()), parent, SLOT(bigSizedIcons()));
172 menu->addAction(bigIcons);
174 unsigned int cur = parent->iconSize().width();
175 if ( cur == parent->d.smallIconSize)
176 smallIcons->setChecked(true);
177 else if (cur == parent->d.normalIconSize)
178 normalIcons->setChecked(true);
179 else if (cur == parent->d.bigIconSize)
180 bigIcons->setChecked(true);
185 void initBackground()
188 LYXERR(Debug::GUI) << "show banner: " << lyxrc.show_banner << endl;
189 /// The text to be written on top of the pixmap
190 QString const text = lyx_version ? QString(lyx_version) : qt_("unknown version");
191 bg_widget_ = new BackgroundWidget(":/images/banner.png", text);
199 stack_widget_->setCurrentWidget(bg_widget_);
200 bg_widget_->setUpdatesEnabled(true);
205 unsigned int GuiViewBase::GuiViewPrivate::lastIconSize = 0;
208 GuiViewBase::GuiViewBase(int id)
209 : QMainWindow(), LyXView(id), quitting_by_menu_(false),
210 d(*new GuiViewPrivate)
212 // Qt bug? signal lastWindowClosed does not work
213 setAttribute(Qt::WA_QuitOnClose, false);
214 setAttribute(Qt::WA_DeleteOnClose, true);
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
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 setWindowIcon(QPixmap(":/images/lyx.png"));
227 d.tab_widget_ = new TabWorkArea;
228 QObject::connect(d.tab_widget_, SIGNAL(currentWorkAreaChanged(GuiWorkArea *)),
229 this, SLOT(on_currentWorkAreaChanged(GuiWorkArea *)));
233 LYXERR(Debug::GUI) << "stack widget!" << endl;
234 d.stack_widget_ = new QStackedWidget;
235 d.stack_widget_->addWidget(d.bg_widget_);
236 d.stack_widget_->addWidget(d.tab_widget_);
237 setCentralWidget(d.stack_widget_);
240 setCentralWidget(d.tab_widget_);
244 setAcceptDrops(true);
248 GuiViewBase::~GuiViewBase()
256 void GuiViewBase::close()
258 quitting_by_menu_ = true;
259 d.tab_widget_->closeAll();
260 QMainWindow::close();
261 quitting_by_menu_ = false;
265 void GuiViewBase::setFocus()
267 if (d.tab_widget_->count())
268 d.tab_widget_->currentWidget()->setFocus();
272 QMenu* GuiViewBase::createPopupMenu()
274 return d.toolBarPopup(this);
278 void GuiViewBase::init()
280 // GuiToolbars *must* be initialised before GuiMenubar.
281 d.toolbars_ = new GuiToolbars(*this);
282 // FIXME: GuiToolbars::init() cannot be integrated in the ctor
283 // because LyXFunc::getStatus() needs a properly initialized
284 // GuiToolbars object (for LFUN_TOOLBAR_TOGGLE).
286 d.menubar_ = new GuiMenubar(this, menubackend);
288 statusBar()->setSizeGripEnabled(true);
290 QObject::connect(&statusbar_timer_, SIGNAL(timeout()),
291 this, SLOT(update_view_state_qt()));
294 d.stack_widget_->setCurrentWidget(d.bg_widget_);
298 void GuiViewBase::closeEvent(QCloseEvent * close_event)
300 // we may have been called through the close window button
301 // which bypasses the LFUN machinery.
302 if (!quitting_by_menu_ && theApp()->gui().viewIds().size() == 1) {
303 if (!theBufferList().quitWriteAll()) {
304 close_event->ignore();
309 // Make sure that no LFUN use this close to be closed View.
310 theLyXFunc().setLyXView(0);
311 // Make sure the timer time out will not trigger a statusbar update.
312 statusbar_timer_.stop();
314 theApp()->gui().unregisterView(id());
315 if (!theApp()->gui().viewIds().empty()) {
316 // Just close the window and do nothing else if this is not the
318 close_event->accept();
324 // this is the place where we leave the frontend.
325 // it is the only point at which we start quitting.
327 close_event->accept();
328 // quit the event loop
333 void GuiViewBase::dragEnterEvent(QDragEnterEvent * event)
335 if (event->mimeData()->hasUrls())
337 /// \todo Ask lyx-devel is this is enough:
338 /// if (event->mimeData()->hasFormat("text/plain"))
339 /// event->acceptProposedAction();
343 void GuiViewBase::dropEvent(QDropEvent* event)
345 QList<QUrl> files = event->mimeData()->urls();
349 LYXERR(Debug::GUI) << BOOST_CURRENT_FUNCTION
350 << " got URLs!" << endl;
351 for (int i = 0; i != files.size(); ++i) {
352 string const file = support::os::internal_path(fromqstr(
353 files.at(i).toLocalFile()));
355 dispatch(FuncRequest(LFUN_FILE_OPEN, file));
360 void GuiViewBase::saveGeometry()
362 static bool done = false;
369 // change the ifdef to 'geometry = normalGeometry();' only
370 // when Trolltech has fixed the broken normalGeometry on X11:
371 // http://www.trolltech.com/developer/task-tracker/index_html?id=119684+&method=entry
372 // Then also the moveEvent, resizeEvent, and the
373 // code for floatingGeometry_ can be removed;
374 // adjust GuiViewBase::setGeometry()
376 QRect normal_geometry;
379 normal_geometry = normalGeometry();
381 maximized = CompletelyMaximized;
383 maximized = NotMaximized;
386 normal_geometry = updateFloatingGeometry();
388 QDesktopWidget& dw = *qApp->desktop();
389 QRect desk = dw.availableGeometry(dw.primaryScreen());
390 // Qt bug on Linux: load completely maximized, vert max. save-> frameGeometry().height() is wrong
391 if (isMaximized() && desk.width() <= frameGeometry().width() && desk.height() <= frameGeometry().height()) {
392 maximized = CompletelyMaximized;
393 // maximizing does not work when the window is allready hor. or vert. maximized
394 // Tested only on KDE
395 int dh = frameGeometry().height() - height();
396 if (desk.height() <= normal_geometry.height() + dh)
397 normal_geometry.setHeight(normal_geometry.height() - 1);
398 int dw = frameGeometry().width() - width();
399 if (desk.width() <= normal_geometry.width() + dw)
400 normal_geometry.setWidth(normal_geometry.width() - 1);
401 } else if (desk.height() <= frameGeometry().height()) {
402 maximized = VerticallyMaximized;
403 } else if (desk.width() <= frameGeometry().width()) {
404 maximized = HorizontallyMaximized;
406 maximized = NotMaximized;
411 // save windows size and position
412 Session & session = LyX::ref().session();
413 session.sessionInfo().save("WindowWidth", convert<string>(normal_geometry.width()));
414 session.sessionInfo().save("WindowHeight", convert<string>(normal_geometry.height()));
415 session.sessionInfo().save("WindowMaximized", convert<string>(maximized));
416 session.sessionInfo().save("IconSizeXY", convert<string>(iconSize().width()));
417 if (lyxrc.geometry_xysaved) {
418 session.sessionInfo().save("WindowPosX", convert<string>(normal_geometry.x() + d.posx_offset));
419 session.sessionInfo().save("WindowPosY", convert<string>(normal_geometry.y() + d.posy_offset));
421 d.toolbars_->saveToolbarInfo();
425 void GuiViewBase::setGeometry(unsigned int width,
429 unsigned int iconSizeXY,
430 const string & geometryArg)
432 // use last value (not at startup)
433 if (d.lastIconSize != 0)
434 setIconSize(d.lastIconSize);
435 else if (iconSizeXY != 0)
436 setIconSize(iconSizeXY);
438 setIconSize(d.normalIconSize);
440 // only true when the -geometry option was NOT used
441 if (width != 0 && height != 0) {
442 if (posx != -1 && posy != -1) {
443 // if there are startup positioning problems:
444 // http://doc.trolltech.com/4.2/qdesktopwidget.html
445 QDesktopWidget& dw = *qApp->desktop();
446 if (dw.isVirtualDesktop()) {
447 if(!dw.geometry().contains(posx, posy)) {
452 // Which system doesn't use a virtual desktop?
453 // TODO save also last screen number and check if it is still availabe.
456 // FIXME: use setGeometry only when Trolltech has fixed the qt4/X11 bug
457 QWidget::setGeometry(posx, posy, width, height);
459 resize(width, height);
463 resize(width, height);
466 // remember original size
467 floatingGeometry_ = QRect(posx, posy, width, height);
469 if (maximized != NotMaximized) {
470 if (maximized == CompletelyMaximized) {
471 setWindowState(Qt::WindowMaximized);
474 // TODO How to set by the window manager?
475 // setWindowState(Qt::WindowVerticallyMaximized);
477 QDesktopWidget& dw = *qApp->desktop();
478 QRect desk = dw.availableGeometry(dw.primaryScreen());
479 if (maximized == VerticallyMaximized)
480 resize(width, desk.height());
481 if (maximized == HorizontallyMaximized)
482 resize(desk.width(), height);
489 // FIXME: move this code into parse_geometry() (LyX.cpp)
493 QRegExp re( "[=]*(?:([0-9]+)[xX]([0-9]+)){0,1}[ ]*(?:([+-][0-9]*)([+-][0-9]*)){0,1}" );
494 re.indexIn(toqstr(geometryArg.c_str()));
495 w = re.cap(1).toInt();
496 h = re.cap(2).toInt();
497 x = re.cap(3).toInt();
498 y = re.cap(4).toInt();
499 QWidget::setGeometry( x, y, w, h );
510 // For an unknown reason, the Window title update is not effective for
511 // the second windows up until it is shown on screen (Qt bug?).
514 // after show geometry() has changed (Qt bug?)
515 // we compensate the drift when storing the position
518 if (width != 0 && height != 0)
519 if (posx != -1 && posy != -1) {
521 d.posx_offset = posx - normalGeometry().x();
522 d.posy_offset = posy - normalGeometry().y();
525 if (maximized == NotMaximized) {
526 d.posx_offset = posx - geometry().x();
527 d.posy_offset = posy - geometry().y();
535 void GuiViewBase::setWindowTitle(docstring const & t, docstring const & it)
537 QString title = windowTitle();
538 QString new_title = toqstr(t);
539 if (title != new_title) {
540 QMainWindow::setWindowTitle(new_title);
541 QMainWindow::setWindowIconText(toqstr(it));
543 if (Buffer const * buf = buffer())
544 d.tab_widget_->setTabText(d.tab_widget_->currentIndex(),
545 toqstr(makeDisplayPath(buf->fileName(), 30)));
549 void GuiViewBase::message(docstring const & str)
551 statusBar()->showMessage(toqstr(str));
552 statusbar_timer_.stop();
553 statusbar_timer_.start(statusbar_timer_value);
557 void GuiViewBase::clearMessage()
559 update_view_state_qt();
563 void GuiViewBase::setIconSize(unsigned int size)
565 d.lastIconSize = size;
566 QMainWindow::setIconSize(QSize(size, size));
570 void GuiViewBase::smallSizedIcons()
572 setIconSize(d.smallIconSize);
576 void GuiViewBase::normalSizedIcons()
578 setIconSize(d.normalIconSize);
582 void GuiViewBase::bigSizedIcons()
584 setIconSize(d.bigIconSize);
588 void GuiViewBase::update_view_state_qt()
592 theLyXFunc().setLyXView(this);
593 statusBar()->showMessage(toqstr(theLyXFunc().viewStatusMessage()));
594 statusbar_timer_.stop();
598 void GuiViewBase::on_currentWorkAreaChanged(GuiWorkArea * wa)
601 disconnectBufferView();
602 connectBufferView(wa->bufferView());
603 connectBuffer(wa->bufferView().buffer());
606 // Buffer-dependent dialogs should be updated or
607 // hidden. This should go here because some dialogs (eg ToC)
608 // require bv_->text.
609 getDialogs().updateBufferDependent(true);
611 updateLayoutChoice();
617 void GuiViewBase::updateStatusBar()
619 // let the user see the explicit message
620 if (statusbar_timer_.isActive())
623 statusBar()->showMessage(toqstr(theLyXFunc().viewStatusMessage()));
627 void GuiViewBase::activated(FuncRequest const & func)
633 bool GuiViewBase::hasFocus() const
635 return qApp->activeWindow() == this;
639 QRect GuiViewBase::updateFloatingGeometry()
641 QDesktopWidget& dw = *qApp->desktop();
642 QRect desk = dw.availableGeometry(dw.primaryScreen());
643 // remember only non-maximized sizes
644 if (!isMaximized() && desk.width() > frameGeometry().width() && desk.height() > frameGeometry().height()) {
645 floatingGeometry_ = QRect(x(), y(), width(), height());
647 return floatingGeometry_;
651 void GuiViewBase::resizeEvent(QResizeEvent *)
653 updateFloatingGeometry();
657 void GuiViewBase::moveEvent(QMoveEvent *)
659 updateFloatingGeometry();
663 bool GuiViewBase::event(QEvent * e)
667 // Useful debug code:
668 //case QEvent::ActivationChange:
669 //case QEvent::WindowDeactivate:
670 //case QEvent::Paint:
671 //case QEvent::Enter:
672 //case QEvent::Leave:
673 //case QEvent::HoverEnter:
674 //case QEvent::HoverLeave:
675 //case QEvent::HoverMove:
676 //case QEvent::StatusTip:
677 //case QEvent::DragEnter:
678 //case QEvent::DragLeave:
682 case QEvent::WindowActivate: {
683 theApp()->setCurrentView(*this);
684 GuiWorkArea * wa = d.tab_widget_->currentWorkArea();
686 BufferView & bv = wa->bufferView();
687 connectBufferView(bv);
688 connectBuffer(bv.buffer());
689 // The document structure, name and dialogs might have
690 // changed in another view.
692 getDialogs().updateBufferDependent(true);
694 return QMainWindow::event(e);
696 case QEvent::ShortcutOverride: {
697 QKeyEvent * ke = static_cast<QKeyEvent*>(e);
698 if (d.tab_widget_->count() == 0) {
699 theLyXFunc().setLyXView(this);
701 setKeySymbol(&sym, ke);
702 theLyXFunc().processKeySym(sym, q_key_state(ke->modifiers()));
706 if (ke->key() == Qt::Key_Tab || ke->key() == Qt::Key_Backtab) {
708 setKeySymbol(&sym, ke);
709 currentWorkArea()->processKeySym(sym, NoModifier);
715 return QMainWindow::event(e);
720 bool GuiViewBase::focusNextPrevChild(bool /*next*/)
727 void GuiViewBase::showView()
729 QMainWindow::setWindowTitle(qt_("LyX"));
731 updateFloatingGeometry();
735 void GuiViewBase::busy(bool yes)
737 GuiWorkArea * wa = d.tab_widget_->currentWorkArea();
739 wa->setUpdatesEnabled(!yes);
741 wa->stopBlinkingCursor();
743 wa->startBlinkingCursor();
747 QApplication::setOverrideCursor(Qt::WaitCursor);
749 QApplication::restoreOverrideCursor();
753 GuiToolbar * GuiViewBase::makeToolbar(ToolbarInfo const & tbinfo, bool newline)
755 GuiToolbar * toolBar = new GuiToolbar(tbinfo, *this);
757 if (tbinfo.flags & ToolbarInfo::TOP) {
759 addToolBarBreak(Qt::TopToolBarArea);
760 addToolBar(Qt::TopToolBarArea, toolBar);
763 if (tbinfo.flags & ToolbarInfo::BOTTOM) {
764 // Qt < 4.2.2 cannot handle ToolBarBreak on non-TOP dock.
765 #if (QT_VERSION >= 0x040202)
767 addToolBarBreak(Qt::BottomToolBarArea);
769 addToolBar(Qt::BottomToolBarArea, toolBar);
772 if (tbinfo.flags & ToolbarInfo::LEFT) {
773 // Qt < 4.2.2 cannot handle ToolBarBreak on non-TOP dock.
774 #if (QT_VERSION >= 0x040202)
776 addToolBarBreak(Qt::LeftToolBarArea);
778 addToolBar(Qt::LeftToolBarArea, toolBar);
781 if (tbinfo.flags & ToolbarInfo::RIGHT) {
782 // Qt < 4.2.2 cannot handle ToolBarBreak on non-TOP dock.
783 #if (QT_VERSION >= 0x040202)
785 addToolBarBreak(Qt::RightToolBarArea);
787 addToolBar(Qt::RightToolBarArea, toolBar);
790 // The following does not work so I cannot restore to exact toolbar location
792 ToolbarSection::ToolbarInfo & tbinfo = LyX::ref().session().toolbars().load(tbinfo.name);
793 toolBar->move(tbinfo.posx, tbinfo.posy);
800 WorkArea * GuiViewBase::workArea(Buffer & buffer)
802 return d.tab_widget_->workArea(buffer);
806 WorkArea * GuiViewBase::addWorkArea(Buffer & buffer)
808 GuiWorkArea * wa = new GuiWorkArea(buffer, *this);
809 wa->setUpdatesEnabled(false);
810 d.tab_widget_->addTab(wa, toqstr(makeDisplayPath(buffer.fileName(), 30)));
811 wa->bufferView().updateMetrics(false);
813 d.stack_widget_->setCurrentWidget(d.tab_widget_);
814 // Hide tabbar if there's only one tab.
815 d.tab_widget_->showBar(d.tab_widget_->count() > 1);
820 WorkArea * GuiViewBase::currentWorkArea()
822 return d.tab_widget_->currentWorkArea();
826 WorkArea const * GuiViewBase::currentWorkArea() const
828 return d.tab_widget_->currentWorkArea();
832 void GuiViewBase::setCurrentWorkArea(WorkArea * work_area)
834 BOOST_ASSERT(work_area);
836 // Changing work area can result from opening a file so
837 // update the toc in any case.
840 GuiWorkArea * wa = static_cast<GuiWorkArea *>(work_area);
842 d.tab_widget_->setCurrentWorkArea(wa);
846 void GuiViewBase::removeWorkArea(WorkArea * work_area)
848 BOOST_ASSERT(work_area);
849 if (work_area == currentWorkArea()) {
851 disconnectBufferView();
854 // removing a work area often results from closing a file so
855 // update the toc in any case.
858 GuiWorkArea * gwa = static_cast<GuiWorkArea *>(work_area);
860 d.tab_widget_->removeWorkArea(gwa);
862 getDialogs().hideBufferDependent();
864 if (d.tab_widget_->count() == 0 && d.stack_widget_)
865 // No more work area, switch to the background widget.
870 void GuiViewBase::showMiniBuffer(bool visible)
872 d.toolbars_->showCommandBuffer(visible);
876 void GuiViewBase::openMenu(docstring const & name)
878 d.menubar_->openByName(toqstr(name));
882 void GuiViewBase::openLayoutList()
884 d.toolbars_->openLayoutList();
888 void GuiViewBase::updateLayoutChoice()
890 // Don't show any layouts without a buffer
892 d.toolbars_->clearLayoutList();
896 // Update the layout display
897 if (d.toolbars_->updateLayoutList(buffer()->params().getTextClassPtr())) {
898 d.current_layout = buffer()->params().getTextClass().defaultLayoutName();
901 docstring const & layout = currentWorkArea()->bufferView().cursor().
902 innerParagraph().layout()->name();
904 if (layout != d.current_layout) {
905 d.toolbars_->setLayout(layout);
906 d.current_layout = layout;
911 bool GuiViewBase::isToolbarVisible(std::string const & id)
913 return d.toolbars_->visible(id);
916 void GuiViewBase::updateToolbars()
918 WorkArea * wa = currentWorkArea();
921 wa->bufferView().cursor().inMathed();
923 lyx::getStatus(FuncRequest(LFUN_LAYOUT_TABULAR)).enabled();
925 lyx::getStatus(FuncRequest(LFUN_CHANGES_TRACK)).enabled() &&
926 lyx::getStatus(FuncRequest(LFUN_CHANGES_TRACK)).onoff(true);
928 d.toolbars_->update(math, table, review);
930 d.toolbars_->update(false, false, false);
932 // update read-only status of open dialogs.
933 getDialogs().checkStatus();
937 ToolbarInfo * GuiViewBase::getToolbarInfo(string const & name)
939 return d.toolbars_->getToolbarInfo(name);
943 void GuiViewBase::toggleToolbarState(string const & name, bool allowauto)
945 // it is possible to get current toolbar status like this,...
946 // but I decide to obey the order of ToolbarBackend::flags
947 // and disregard real toolbar status.
948 // toolbars_->saveToolbarInfo();
950 // toggle state on/off/auto
951 d.toolbars_->toggleToolbarState(name, allowauto);
957 } // namespace frontend
960 #include "GuiView_moc.cpp"