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 "TextClass.h"
51 #include "ToolbarBackend.h"
55 #include <QApplication>
56 #include <QCloseEvent>
57 #include <QDesktopWidget>
58 #include <QDragEnterEvent>
64 #include <QPushButton>
65 #include <QStackedWidget>
70 #include <boost/current_function.hpp>
78 using support::makeDisplayPath;
86 int const statusbar_timer_value = 3000;
88 class BackgroundWidget : public QWidget
91 BackgroundWidget(QString const & file, QString const & text)
93 splash_ = new QPixmap(file);
95 lyxerr << "could not load splash screen: '" << fromqstr(file) << "'" << endl;
99 QPainter pain(splash_);
100 pain.setPen(QColor(255, 255, 0));
102 // The font used to display the version info
103 font.setStyleHint(QFont::SansSerif);
104 font.setWeight(QFont::Bold);
105 font.setPointSize(convert<int>(lyxrc.font_sizes[FONT_SIZE_LARGE]));
107 pain.drawText(260, 270, text);
110 void paintEvent(QPaintEvent *)
115 int x = (width() - splash_->width()) / 2;
116 int y = (height() - splash_->height()) / 2;
118 pain.drawPixmap(x, y, *splash_);
128 struct GuiView::GuiViewPrivate
135 TabWorkArea * tab_widget_;
136 QStackedWidget * stack_widget_;
137 BackgroundWidget * bg_widget_;
139 GuiMenubar * menubar_;
141 GuiToolbars * toolbars_;
143 docstring current_layout;
145 GuiViewPrivate() : posx_offset(0), posy_offset(0) {}
147 unsigned int smallIconSize;
148 unsigned int normalIconSize;
149 unsigned int bigIconSize;
150 // static needed by "New Window"
151 static unsigned int lastIconSize;
153 QMenu * toolBarPopup(GuiView * parent)
155 // FIXME: translation
156 QMenu * menu = new QMenu(parent);
157 QActionGroup * iconSizeGroup = new QActionGroup(parent);
159 QAction * smallIcons = new QAction(iconSizeGroup);
160 smallIcons->setText(qt_("Small-sized icons"));
161 smallIcons->setCheckable(true);
162 QObject::connect(smallIcons, SIGNAL(triggered()), parent, SLOT(smallSizedIcons()));
163 menu->addAction(smallIcons);
165 QAction * normalIcons = new QAction(iconSizeGroup);
166 normalIcons->setText(qt_("Normal-sized icons"));
167 normalIcons->setCheckable(true);
168 QObject::connect(normalIcons, SIGNAL(triggered()), parent, SLOT(normalSizedIcons()));
169 menu->addAction(normalIcons);
171 QAction * bigIcons = new QAction(iconSizeGroup);
172 bigIcons->setText(qt_("Big-sized icons"));
173 bigIcons->setCheckable(true);
174 QObject::connect(bigIcons, SIGNAL(triggered()), parent, SLOT(bigSizedIcons()));
175 menu->addAction(bigIcons);
177 unsigned int cur = parent->iconSize().width();
178 if ( cur == parent->d.smallIconSize)
179 smallIcons->setChecked(true);
180 else if (cur == parent->d.normalIconSize)
181 normalIcons->setChecked(true);
182 else if (cur == parent->d.bigIconSize)
183 bigIcons->setChecked(true);
188 void initBackground()
191 LYXERR(Debug::GUI) << "show banner: " << lyxrc.show_banner << endl;
192 /// The text to be written on top of the pixmap
193 QString const text = lyx_version ? QString(lyx_version) : qt_("unknown version");
194 bg_widget_ = new BackgroundWidget(":/images/banner.png", text);
202 stack_widget_->setCurrentWidget(bg_widget_);
203 bg_widget_->setUpdatesEnabled(true);
208 unsigned int GuiView::GuiViewPrivate::lastIconSize = 0;
211 GuiView::GuiView(int id)
212 : QMainWindow(), LyXView(id), quitting_by_menu_(false),
213 d(*new GuiViewPrivate)
215 // Qt bug? signal lastWindowClosed does not work
216 setAttribute(Qt::WA_QuitOnClose, false);
217 setAttribute(Qt::WA_DeleteOnClose, true);
219 // hardcode here the platform specific icon size
220 d.smallIconSize = 14; // scaling problems
221 d.normalIconSize = 20; // ok, default
222 d.bigIconSize = 26; // better for some math icons
225 // assign an icon to main form. We do not do it under Qt/Mac,
226 // since the icon is provided in the application bundle.
227 setWindowIcon(QPixmap(":/images/lyx.png"));
230 d.tab_widget_ = new TabWorkArea;
231 QObject::connect(d.tab_widget_, SIGNAL(currentWorkAreaChanged(GuiWorkArea *)),
232 this, SLOT(on_currentWorkAreaChanged(GuiWorkArea *)));
236 LYXERR(Debug::GUI) << "stack widget!" << endl;
237 d.stack_widget_ = new QStackedWidget;
238 d.stack_widget_->addWidget(d.bg_widget_);
239 d.stack_widget_->addWidget(d.tab_widget_);
240 setCentralWidget(d.stack_widget_);
243 setCentralWidget(d.tab_widget_);
247 setAcceptDrops(true);
259 void GuiView::close()
261 quitting_by_menu_ = true;
262 d.tab_widget_->closeAll();
263 QMainWindow::close();
264 quitting_by_menu_ = false;
268 void GuiView::setFocus()
270 if (d.tab_widget_->count())
271 d.tab_widget_->currentWidget()->setFocus();
275 QMenu* GuiView::createPopupMenu()
277 return d.toolBarPopup(this);
283 // GuiToolbars *must* be initialised before GuiMenubar.
284 d.toolbars_ = new GuiToolbars(*this);
285 // FIXME: GuiToolbars::init() cannot be integrated in the ctor
286 // because LyXFunc::getStatus() needs a properly initialized
287 // GuiToolbars object (for LFUN_TOOLBAR_TOGGLE).
289 d.menubar_ = new GuiMenubar(this, menubackend);
291 statusBar()->setSizeGripEnabled(true);
293 QObject::connect(&statusbar_timer_, SIGNAL(timeout()),
294 this, SLOT(update_view_state_qt()));
297 d.stack_widget_->setCurrentWidget(d.bg_widget_);
301 void GuiView::closeEvent(QCloseEvent * close_event)
303 // we may have been called through the close window button
304 // which bypasses the LFUN machinery.
305 if (!quitting_by_menu_ && theApp()->gui().viewIds().size() == 1) {
306 if (!theBufferList().quitWriteAll()) {
307 close_event->ignore();
312 // Make sure that no LFUN use this close to be closed View.
313 theLyXFunc().setLyXView(0);
314 // Make sure the timer time out will not trigger a statusbar update.
315 statusbar_timer_.stop();
317 theApp()->gui().unregisterView(id());
318 if (!theApp()->gui().viewIds().empty()) {
319 // Just close the window and do nothing else if this is not the
321 close_event->accept();
327 // this is the place where we leave the frontend.
328 // it is the only point at which we start quitting.
330 close_event->accept();
331 // quit the event loop
336 void GuiView::dragEnterEvent(QDragEnterEvent * event)
338 if (event->mimeData()->hasUrls())
340 /// \todo Ask lyx-devel is this is enough:
341 /// if (event->mimeData()->hasFormat("text/plain"))
342 /// event->acceptProposedAction();
346 void GuiView::dropEvent(QDropEvent* event)
348 QList<QUrl> files = event->mimeData()->urls();
352 LYXERR(Debug::GUI) << BOOST_CURRENT_FUNCTION
353 << " got URLs!" << endl;
354 for (int i = 0; i != files.size(); ++i) {
355 string const file = support::os::internal_path(fromqstr(
356 files.at(i).toLocalFile()));
358 dispatch(FuncRequest(LFUN_FILE_OPEN, file));
363 void GuiView::saveGeometry()
365 static bool done = false;
372 // change the ifdef to 'geometry = normalGeometry();' only
373 // when Trolltech has fixed the broken normalGeometry on X11:
374 // http://www.trolltech.com/developer/task-tracker/index_html?id=119684+&method=entry
375 // Then also the moveEvent, resizeEvent, and the
376 // code for floatingGeometry_ can be removed;
377 // adjust GuiView::setGeometry()
379 QRect normal_geometry;
382 normal_geometry = normalGeometry();
384 maximized = CompletelyMaximized;
386 maximized = NotMaximized;
389 normal_geometry = updateFloatingGeometry();
391 QDesktopWidget& dw = *qApp->desktop();
392 QRect desk = dw.availableGeometry(dw.primaryScreen());
393 // Qt bug on Linux: load completely maximized, vert max. save-> frameGeometry().height() is wrong
394 if (isMaximized() && desk.width() <= frameGeometry().width() && desk.height() <= frameGeometry().height()) {
395 maximized = CompletelyMaximized;
396 // maximizing does not work when the window is allready hor. or vert. maximized
397 // Tested only on KDE
398 int dh = frameGeometry().height() - height();
399 if (desk.height() <= normal_geometry.height() + dh)
400 normal_geometry.setHeight(normal_geometry.height() - 1);
401 int dw = frameGeometry().width() - width();
402 if (desk.width() <= normal_geometry.width() + dw)
403 normal_geometry.setWidth(normal_geometry.width() - 1);
404 } else if (desk.height() <= frameGeometry().height()) {
405 maximized = VerticallyMaximized;
406 } else if (desk.width() <= frameGeometry().width()) {
407 maximized = HorizontallyMaximized;
409 maximized = NotMaximized;
414 // save windows size and position
415 Session & session = LyX::ref().session();
416 session.sessionInfo().save("WindowWidth", convert<string>(normal_geometry.width()));
417 session.sessionInfo().save("WindowHeight", convert<string>(normal_geometry.height()));
418 session.sessionInfo().save("WindowMaximized", convert<string>(maximized));
419 session.sessionInfo().save("IconSizeXY", convert<string>(iconSize().width()));
420 if (lyxrc.geometry_xysaved) {
421 session.sessionInfo().save("WindowPosX", convert<string>(normal_geometry.x() + d.posx_offset));
422 session.sessionInfo().save("WindowPosY", convert<string>(normal_geometry.y() + d.posy_offset));
424 d.toolbars_->saveToolbarInfo();
428 void GuiView::setGeometry(unsigned int width,
432 unsigned int iconSizeXY,
433 const string & geometryArg)
435 // use last value (not at startup)
436 if (d.lastIconSize != 0)
437 setIconSize(d.lastIconSize);
438 else if (iconSizeXY != 0)
439 setIconSize(iconSizeXY);
441 setIconSize(d.normalIconSize);
443 // only true when the -geometry option was NOT used
444 if (width != 0 && height != 0) {
445 if (posx != -1 && posy != -1) {
446 // if there are startup positioning problems:
447 // http://doc.trolltech.com/4.2/qdesktopwidget.html
448 QDesktopWidget& dw = *qApp->desktop();
449 if (dw.isVirtualDesktop()) {
450 if(!dw.geometry().contains(posx, posy)) {
455 // Which system doesn't use a virtual desktop?
456 // TODO save also last screen number and check if it is still availabe.
459 // FIXME: use setGeometry only when Trolltech has fixed the qt4/X11 bug
460 QWidget::setGeometry(posx, posy, width, height);
462 resize(width, height);
466 resize(width, height);
469 // remember original size
470 floatingGeometry_ = QRect(posx, posy, width, height);
472 if (maximized != NotMaximized) {
473 if (maximized == CompletelyMaximized) {
474 setWindowState(Qt::WindowMaximized);
477 // TODO How to set by the window manager?
478 // setWindowState(Qt::WindowVerticallyMaximized);
480 QDesktopWidget& dw = *qApp->desktop();
481 QRect desk = dw.availableGeometry(dw.primaryScreen());
482 if (maximized == VerticallyMaximized)
483 resize(width, desk.height());
484 if (maximized == HorizontallyMaximized)
485 resize(desk.width(), height);
492 // FIXME: move this code into parse_geometry() (LyX.cpp)
496 QRegExp re( "[=]*(?:([0-9]+)[xX]([0-9]+)){0,1}[ ]*(?:([+-][0-9]*)([+-][0-9]*)){0,1}" );
497 re.indexIn(toqstr(geometryArg.c_str()));
498 w = re.cap(1).toInt();
499 h = re.cap(2).toInt();
500 x = re.cap(3).toInt();
501 y = re.cap(4).toInt();
502 QWidget::setGeometry( x, y, w, h );
513 // For an unknown reason, the Window title update is not effective for
514 // the second windows up until it is shown on screen (Qt bug?).
517 // after show geometry() has changed (Qt bug?)
518 // we compensate the drift when storing the position
521 if (width != 0 && height != 0)
522 if (posx != -1 && posy != -1) {
524 d.posx_offset = posx - normalGeometry().x();
525 d.posy_offset = posy - normalGeometry().y();
528 if (maximized == NotMaximized) {
529 d.posx_offset = posx - geometry().x();
530 d.posy_offset = posy - geometry().y();
538 void GuiView::setWindowTitle(docstring const & t, docstring const & it)
540 QString title = windowTitle();
541 QString new_title = toqstr(t);
542 if (title != new_title) {
543 QMainWindow::setWindowTitle(new_title);
544 QMainWindow::setWindowIconText(toqstr(it));
546 if (Buffer const * buf = buffer())
547 d.tab_widget_->setTabText(d.tab_widget_->currentIndex(),
548 toqstr(buf->fileName().displayName(30)));
552 void GuiView::message(docstring const & str)
554 statusBar()->showMessage(toqstr(str));
555 statusbar_timer_.stop();
556 statusbar_timer_.start(statusbar_timer_value);
560 void GuiView::clearMessage()
562 update_view_state_qt();
566 void GuiView::setIconSize(unsigned int size)
568 d.lastIconSize = size;
569 QMainWindow::setIconSize(QSize(size, size));
573 void GuiView::smallSizedIcons()
575 setIconSize(d.smallIconSize);
579 void GuiView::normalSizedIcons()
581 setIconSize(d.normalIconSize);
585 void GuiView::bigSizedIcons()
587 setIconSize(d.bigIconSize);
591 void GuiView::update_view_state_qt()
595 theLyXFunc().setLyXView(this);
596 statusBar()->showMessage(toqstr(theLyXFunc().viewStatusMessage()));
597 statusbar_timer_.stop();
601 void GuiView::on_currentWorkAreaChanged(GuiWorkArea * wa)
604 disconnectBufferView();
605 connectBufferView(wa->bufferView());
606 connectBuffer(wa->bufferView().buffer());
609 // Buffer-dependent dialogs should be updated or
610 // hidden. This should go here because some dialogs (eg ToC)
611 // require bv_->text.
612 getDialogs().updateBufferDependent(true);
614 updateLayoutChoice();
620 void GuiView::updateStatusBar()
622 // let the user see the explicit message
623 if (statusbar_timer_.isActive())
626 statusBar()->showMessage(toqstr(theLyXFunc().viewStatusMessage()));
630 void GuiView::activated(FuncRequest const & func)
636 bool GuiView::hasFocus() const
638 return qApp->activeWindow() == this;
642 QRect GuiView::updateFloatingGeometry()
644 QDesktopWidget& dw = *qApp->desktop();
645 QRect desk = dw.availableGeometry(dw.primaryScreen());
646 // remember only non-maximized sizes
647 if (!isMaximized() && desk.width() > frameGeometry().width() && desk.height() > frameGeometry().height()) {
648 floatingGeometry_ = QRect(x(), y(), width(), height());
650 return floatingGeometry_;
654 void GuiView::resizeEvent(QResizeEvent *)
656 updateFloatingGeometry();
660 void GuiView::moveEvent(QMoveEvent *)
662 updateFloatingGeometry();
666 bool GuiView::event(QEvent * e)
670 // Useful debug code:
671 //case QEvent::ActivationChange:
672 //case QEvent::WindowDeactivate:
673 //case QEvent::Paint:
674 //case QEvent::Enter:
675 //case QEvent::Leave:
676 //case QEvent::HoverEnter:
677 //case QEvent::HoverLeave:
678 //case QEvent::HoverMove:
679 //case QEvent::StatusTip:
680 //case QEvent::DragEnter:
681 //case QEvent::DragLeave:
685 case QEvent::WindowActivate: {
686 theApp()->setCurrentView(*this);
687 GuiWorkArea * wa = d.tab_widget_->currentWorkArea();
689 BufferView & bv = wa->bufferView();
690 connectBufferView(bv);
691 connectBuffer(bv.buffer());
692 // The document structure, name and dialogs might have
693 // changed in another view.
695 getDialogs().updateBufferDependent(true);
697 return QMainWindow::event(e);
699 case QEvent::ShortcutOverride: {
700 QKeyEvent * ke = static_cast<QKeyEvent*>(e);
701 if (d.tab_widget_->count() == 0) {
702 theLyXFunc().setLyXView(this);
704 setKeySymbol(&sym, ke);
705 theLyXFunc().processKeySym(sym, q_key_state(ke->modifiers()));
709 if (ke->key() == Qt::Key_Tab || ke->key() == Qt::Key_Backtab) {
711 setKeySymbol(&sym, ke);
712 currentWorkArea()->processKeySym(sym, NoModifier);
718 return QMainWindow::event(e);
723 bool GuiView::focusNextPrevChild(bool /*next*/)
730 void GuiView::showView()
732 QMainWindow::setWindowTitle(qt_("LyX"));
734 updateFloatingGeometry();
738 void GuiView::setBusy(bool yes)
740 GuiWorkArea * wa = d.tab_widget_->currentWorkArea();
742 wa->setUpdatesEnabled(!yes);
744 wa->stopBlinkingCursor();
746 wa->startBlinkingCursor();
750 QApplication::setOverrideCursor(Qt::WaitCursor);
752 QApplication::restoreOverrideCursor();
756 GuiToolbar * GuiView::makeToolbar(ToolbarInfo const & tbinfo, bool newline)
758 GuiToolbar * toolBar = new GuiToolbar(tbinfo, *this);
760 if (tbinfo.flags & ToolbarInfo::TOP) {
762 addToolBarBreak(Qt::TopToolBarArea);
763 addToolBar(Qt::TopToolBarArea, toolBar);
766 if (tbinfo.flags & ToolbarInfo::BOTTOM) {
767 // Qt < 4.2.2 cannot handle ToolBarBreak on non-TOP dock.
768 #if (QT_VERSION >= 0x040202)
770 addToolBarBreak(Qt::BottomToolBarArea);
772 addToolBar(Qt::BottomToolBarArea, toolBar);
775 if (tbinfo.flags & ToolbarInfo::LEFT) {
776 // Qt < 4.2.2 cannot handle ToolBarBreak on non-TOP dock.
777 #if (QT_VERSION >= 0x040202)
779 addToolBarBreak(Qt::LeftToolBarArea);
781 addToolBar(Qt::LeftToolBarArea, toolBar);
784 if (tbinfo.flags & ToolbarInfo::RIGHT) {
785 // Qt < 4.2.2 cannot handle ToolBarBreak on non-TOP dock.
786 #if (QT_VERSION >= 0x040202)
788 addToolBarBreak(Qt::RightToolBarArea);
790 addToolBar(Qt::RightToolBarArea, toolBar);
793 // The following does not work so I cannot restore to exact toolbar location
795 ToolbarSection::ToolbarInfo & tbinfo = LyX::ref().session().toolbars().load(tbinfo.name);
796 toolBar->move(tbinfo.posx, tbinfo.posy);
803 WorkArea * GuiView::workArea(Buffer & buffer)
805 return d.tab_widget_->workArea(buffer);
809 WorkArea * GuiView::addWorkArea(Buffer & buffer)
811 GuiWorkArea * wa = new GuiWorkArea(buffer, *this);
812 wa->setUpdatesEnabled(false);
813 d.tab_widget_->addTab(wa, toqstr(buffer.fileName().displayName(30)));
814 wa->bufferView().updateMetrics();
816 d.stack_widget_->setCurrentWidget(d.tab_widget_);
817 // Hide tabbar if there's only one tab.
818 d.tab_widget_->showBar(d.tab_widget_->count() > 1);
823 WorkArea * GuiView::currentWorkArea()
825 return d.tab_widget_->currentWorkArea();
829 WorkArea const * GuiView::currentWorkArea() const
831 return d.tab_widget_->currentWorkArea();
835 void GuiView::setCurrentWorkArea(WorkArea * work_area)
837 BOOST_ASSERT(work_area);
839 // Changing work area can result from opening a file so
840 // update the toc in any case.
843 GuiWorkArea * wa = static_cast<GuiWorkArea *>(work_area);
845 d.tab_widget_->setCurrentWorkArea(wa);
849 void GuiView::removeWorkArea(WorkArea * work_area)
851 BOOST_ASSERT(work_area);
852 if (work_area == currentWorkArea()) {
854 disconnectBufferView();
857 // removing a work area often results from closing a file so
858 // update the toc in any case.
861 GuiWorkArea * gwa = static_cast<GuiWorkArea *>(work_area);
863 d.tab_widget_->removeWorkArea(gwa);
865 getDialogs().hideBufferDependent();
867 if (d.tab_widget_->count() == 0 && d.stack_widget_)
868 // No more work area, switch to the background widget.
873 void GuiView::showMiniBuffer(bool visible)
875 d.toolbars_->showCommandBuffer(visible);
879 void GuiView::openMenu(docstring const & name)
881 d.menubar_->openByName(toqstr(name));
885 void GuiView::openLayoutList()
887 d.toolbars_->openLayoutList();
891 void GuiView::updateLayoutChoice()
893 // Don't show any layouts without a buffer
895 d.toolbars_->clearLayoutList();
899 // Update the layout display
900 if (d.toolbars_->updateLayoutList(buffer()->params().getTextClassPtr())) {
901 d.current_layout = buffer()->params().getTextClass().defaultLayoutName();
904 docstring const & layout = currentWorkArea()->bufferView().cursor().
905 innerParagraph().layout()->name();
907 if (layout != d.current_layout) {
908 d.toolbars_->setLayout(layout);
909 d.current_layout = layout;
914 bool GuiView::isToolbarVisible(std::string const & id)
916 return d.toolbars_->visible(id);
919 void GuiView::updateToolbars()
921 WorkArea * wa = currentWorkArea();
924 wa->bufferView().cursor().inMathed();
926 lyx::getStatus(FuncRequest(LFUN_LAYOUT_TABULAR)).enabled();
928 lyx::getStatus(FuncRequest(LFUN_CHANGES_TRACK)).enabled() &&
929 lyx::getStatus(FuncRequest(LFUN_CHANGES_TRACK)).onoff(true);
931 d.toolbars_->update(math, table, review);
933 d.toolbars_->update(false, false, false);
935 // update read-only status of open dialogs.
936 getDialogs().checkStatus();
940 ToolbarInfo * GuiView::getToolbarInfo(string const & name)
942 return d.toolbars_->getToolbarInfo(name);
946 void GuiView::toggleToolbarState(string const & name, bool allowauto)
948 // it is possible to get current toolbar status like this,...
949 // but I decide to obey the order of ToolbarBackend::flags
950 // and disregard real toolbar status.
951 // toolbars_->saveToolbarInfo();
953 // toggle state on/off/auto
954 d.toolbars_->toggleToolbarState(name, allowauto);
960 } // namespace frontend
963 #include "GuiView_moc.cpp"