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 "qt_helpers.h"
25 #include "frontends/Application.h"
26 #include "frontends/Dialogs.h"
27 #include "frontends/Gui.h"
28 #include "frontends/WorkArea.h"
30 #include "support/filetools.h"
31 #include "support/convert.h"
32 #include "support/lstrings.h"
33 #include "support/os.h"
36 #include "BufferView.h"
37 #include "BufferList.h"
40 #include "FuncRequest.h"
44 #include "MenuBackend.h"
49 #include <QApplication>
50 #include <QCloseEvent>
51 #include <QDesktopWidget>
52 #include <QDragEnterEvent>
58 #include <QPushButton>
59 #include <QStackedWidget>
66 #include <boost/shared_ptr.hpp>
74 using support::FileName;
75 using support::libFileSearch;
76 using support::makeDisplayPath;
82 int const statusbar_timer_value = 3000;
84 class BackgroundWidget : public QWidget
87 BackgroundWidget(QString const & file, QString const & text)
89 splash_ = new QPixmap(file);
91 lyxerr << "could not load splash screen: '" << fromqstr(file) << "'" << endl;
95 QPainter pain(splash_);
96 pain.setPen(QColor(255, 255, 0));
98 // The font used to display the version info
99 font.setStyleHint(QFont::SansSerif);
100 font.setWeight(QFont::Bold);
101 font.setPointSize(convert<int>(lyxrc.font_sizes[Font::SIZE_LARGE]));
103 pain.drawText(260, 270, text);
106 void paintEvent(QPaintEvent *)
111 int x = (width() - splash_->width()) / 2;
112 int y = (height() - splash_->height()) / 2;
114 pain.drawPixmap(x, y, *splash_);
122 class TabWidget : public QTabWidget {
124 void showBar(bool show) { tabBar()->setVisible(show); }
131 struct GuiViewBase::GuiViewPrivate
138 TabWidget * tab_widget_;
139 QStackedWidget * stack_widget_;
140 BackgroundWidget * bg_widget_;
142 GuiMenubar * menubar_;
144 GuiViewPrivate() : posx_offset(0), posy_offset(0) {}
146 unsigned int smallIconSize;
147 unsigned int normalIconSize;
148 unsigned int bigIconSize;
149 // static needed by "New Window"
150 static unsigned int lastIconSize;
152 QMenu * toolBarPopup(GuiViewBase * parent)
154 // FIXME: translation
155 QMenu * menu = new QMenu(parent);
156 QActionGroup * iconSizeGroup = new QActionGroup(parent);
158 QAction * smallIcons = new QAction(iconSizeGroup);
159 smallIcons->setText(qt_("Small-sized icons"));
160 smallIcons->setCheckable(true);
161 QObject::connect(smallIcons, SIGNAL(triggered()), parent, SLOT(smallSizedIcons()));
162 menu->addAction(smallIcons);
164 QAction * normalIcons = new QAction(iconSizeGroup);
165 normalIcons->setText(qt_("Normal-sized icons"));
166 normalIcons->setCheckable(true);
167 QObject::connect(normalIcons, SIGNAL(triggered()), parent, SLOT(normalSizedIcons()));
168 menu->addAction(normalIcons);
170 QAction * bigIcons = new QAction(iconSizeGroup);
171 bigIcons->setText(qt_("Big-sized icons"));
172 bigIcons->setCheckable(true);
173 QObject::connect(bigIcons, SIGNAL(triggered()), parent, SLOT(bigSizedIcons()));
174 menu->addAction(bigIcons);
176 unsigned int cur = parent->iconSize().width();
177 if ( cur == parent->d.smallIconSize)
178 smallIcons->setChecked(true);
179 else if (cur == parent->d.normalIconSize)
180 normalIcons->setChecked(true);
181 else if (cur == parent->d.bigIconSize)
182 bigIcons->setChecked(true);
187 void initBackground()
190 LYXERR(Debug::GUI) << "show banner: " << lyxrc.show_banner << endl;
191 /// The text to be written on top of the pixmap
192 QString const text = lyx_version ? QString(lyx_version) : qt_("unknown version");
193 FileName const file = support::libFileSearch("images", "banner", "png");
197 bg_widget_ = new BackgroundWidget(toqstr(file.absFilename()), text);
205 stack_widget_->setCurrentWidget(bg_widget_);
206 bg_widget_->setUpdatesEnabled(true);
211 unsigned int GuiViewBase::GuiViewPrivate::lastIconSize = 0;
214 GuiViewBase::GuiViewBase(int id)
215 : QMainWindow(), LyXView(id), quitting_by_menu_(false),
216 d(*new GuiViewPrivate)
218 // Qt bug? signal lastWindowClosed does not work
219 setAttribute(Qt::WA_QuitOnClose, false);
220 setAttribute(Qt::WA_DeleteOnClose, true);
222 // hardcode here the platform specific icon size
223 d.smallIconSize = 14; // scaling problems
224 d.normalIconSize = 20; // ok, default
225 d.bigIconSize = 26; // better for some math icons
228 // assign an icon to main form. We do not do it under Qt/Mac,
229 // since the icon is provided in the application bundle.
230 FileName const iconname = libFileSearch("images", "lyx", "png");
231 if (!iconname.empty())
232 setWindowIcon(QPixmap(toqstr(iconname.absFilename())));
235 d.tab_widget_ = new TabWidget;
237 QPushButton * closeTabButton = new QPushButton(this);
238 FileName const file = support::libFileSearch("images", "closetab", "png");
240 QPixmap pm(toqstr(file.absFilename()));
241 closeTabButton->setIcon(QIcon(pm));
242 closeTabButton->setMaximumSize(pm.size());
243 closeTabButton->setFlat(true);
245 closeTabButton->setText("Close");
247 closeTabButton->setCursor(Qt::ArrowCursor);
248 closeTabButton->setToolTip(tr("Close tab"));
249 closeTabButton->setEnabled(true);
251 QObject::connect(d.tab_widget_, SIGNAL(currentChanged(int)),
252 this, SLOT(currentTabChanged(int)));
253 QObject::connect(closeTabButton, SIGNAL(clicked()),
254 this, SLOT(closeCurrentTab()));
256 d.tab_widget_->setCornerWidget(closeTabButton);
257 #if QT_VERSION >= 0x040200
258 d.tab_widget_->setUsesScrollButtons(true);
263 LYXERR(Debug::GUI) << "stack widget!" << endl;
264 d.stack_widget_ = new QStackedWidget;
265 d.stack_widget_->addWidget(d.bg_widget_);
266 d.stack_widget_->addWidget(d.tab_widget_);
267 setCentralWidget(d.stack_widget_);
270 setCentralWidget(d.tab_widget_);
274 setAcceptDrops(true);
278 GuiViewBase::~GuiViewBase()
285 void GuiViewBase::close()
287 quitting_by_menu_ = true;
288 QMainWindow::close();
289 quitting_by_menu_ = false;
293 void GuiViewBase::setFocus()
295 if (d.tab_widget_->count())
296 d.tab_widget_->currentWidget()->setFocus();
300 QMenu* GuiViewBase::createPopupMenu()
302 return d.toolBarPopup(this);
306 void GuiViewBase::init()
308 d.menubar_ = new GuiMenubar(this, menubackend);
312 statusBar()->setSizeGripEnabled(true);
314 QObject::connect(&statusbar_timer_, SIGNAL(timeout()),
315 this, SLOT(update_view_state_qt()));
318 d.stack_widget_->setCurrentWidget(d.bg_widget_);
322 void GuiViewBase::closeEvent(QCloseEvent * close_event)
324 // we may have been called through the close window button
325 // which bypasses the LFUN machinery.
326 if (!quitting_by_menu_ && theApp()->gui().viewIds().size() == 1) {
327 if (!theBufferList().quitWriteAll()) {
328 close_event->ignore();
333 // Make sure that no LFUN use this close to be closed View.
334 theLyXFunc().setLyXView(0);
335 // Make sure the timer time out will not trigger a statusbar update.
336 statusbar_timer_.stop();
338 theApp()->gui().unregisterView(id());
339 if (!theApp()->gui().viewIds().empty()) {
340 // Just close the window and do nothing else if this is not the
342 close_event->accept();
348 // this is the place where we leave the frontend.
349 // it is the only point at which we start quitting.
351 close_event->accept();
352 // quit the event loop
357 void GuiViewBase::dragEnterEvent(QDragEnterEvent * event)
359 if (event->mimeData()->hasUrls())
361 /// \todo Ask lyx-devel is this is enough:
362 /// if (event->mimeData()->hasFormat("text/plain"))
363 /// event->acceptProposedAction();
367 void GuiViewBase::dropEvent(QDropEvent* event)
369 QList<QUrl> files = event->mimeData()->urls();
373 LYXERR(Debug::GUI) << BOOST_CURRENT_FUNCTION
374 << " got URLs!" << endl;
375 for (int i = 0; i != files.size(); ++i) {
376 string const file = support::os::internal_path(fromqstr(
377 files.at(i).toLocalFile()));
379 dispatch(FuncRequest(LFUN_FILE_OPEN, file));
384 void GuiViewBase::saveGeometry()
386 static bool done = false;
393 // change the ifdef to 'geometry = normalGeometry();' only
394 // when Trolltech has fixed the broken normalGeometry on X11:
395 // http://www.trolltech.com/developer/task-tracker/index_html?id=119684+&method=entry
396 // Then also the moveEvent, resizeEvent, and the
397 // code for floatingGeometry_ can be removed;
398 // adjust GuiViewBase::setGeometry()
400 QRect normal_geometry;
403 normal_geometry = normalGeometry();
405 maximized = CompletelyMaximized;
407 maximized = NotMaximized;
410 normal_geometry = updateFloatingGeometry();
412 QDesktopWidget& dw = *qApp->desktop();
413 QRect desk = dw.availableGeometry(dw.primaryScreen());
414 // Qt bug on Linux: load completely maximized, vert max. save-> frameGeometry().height() is wrong
415 if (isMaximized() && desk.width() <= frameGeometry().width() && desk.height() <= frameGeometry().height()) {
416 maximized = CompletelyMaximized;
417 // maximizing does not work when the window is allready hor. or vert. maximized
418 // Tested only on KDE
419 int dh = frameGeometry().height() - height();
420 if (desk.height() <= normal_geometry.height() + dh)
421 normal_geometry.setHeight(normal_geometry.height() - 1);
422 int dw = frameGeometry().width() - width();
423 if (desk.width() <= normal_geometry.width() + dw)
424 normal_geometry.setWidth(normal_geometry.width() - 1);
425 } else if (desk.height() <= frameGeometry().height()) {
426 maximized = VerticallyMaximized;
427 } else if (desk.width() <= frameGeometry().width()) {
428 maximized = HorizontallyMaximized;
430 maximized = NotMaximized;
435 // save windows size and position
436 Session & session = LyX::ref().session();
437 session.sessionInfo().save("WindowWidth", convert<string>(normal_geometry.width()));
438 session.sessionInfo().save("WindowHeight", convert<string>(normal_geometry.height()));
439 session.sessionInfo().save("WindowMaximized", convert<string>(maximized));
440 session.sessionInfo().save("IconSizeXY", convert<string>(iconSize().width()));
441 if (lyxrc.geometry_xysaved) {
442 session.sessionInfo().save("WindowPosX", convert<string>(normal_geometry.x() + d.posx_offset));
443 session.sessionInfo().save("WindowPosY", convert<string>(normal_geometry.y() + d.posy_offset));
445 toolbars_->saveToolbarInfo();
449 void GuiViewBase::setGeometry(unsigned int width,
453 unsigned int iconSizeXY,
454 const string & geometryArg)
456 // use last value (not at startup)
457 if (d.lastIconSize != 0)
458 setIconSize(d.lastIconSize);
459 else if (iconSizeXY != 0)
460 setIconSize(iconSizeXY);
462 setIconSize(d.normalIconSize);
464 // only true when the -geometry option was NOT used
465 if (width != 0 && height != 0) {
466 if (posx != -1 && posy != -1) {
467 // if there are startup positioning problems:
468 // http://doc.trolltech.com/4.2/qdesktopwidget.html
469 QDesktopWidget& dw = *qApp->desktop();
470 if (dw.isVirtualDesktop()) {
471 if(!dw.geometry().contains(posx, posy)) {
476 // Which system doesn't use a virtual desktop?
477 // TODO save also last screen number and check if it is still availabe.
480 // FIXME: use setGeometry only when Trolltech has fixed the qt4/X11 bug
481 QWidget::setGeometry(posx, posy, width, height);
483 resize(width, height);
487 resize(width, height);
490 // remember original size
491 floatingGeometry_ = QRect(posx, posy, width, height);
493 if (maximized != NotMaximized) {
494 if (maximized == CompletelyMaximized) {
495 setWindowState(Qt::WindowMaximized);
498 // TODO How to set by the window manager?
499 // setWindowState(Qt::WindowVerticallyMaximized);
501 QDesktopWidget& dw = *qApp->desktop();
502 QRect desk = dw.availableGeometry(dw.primaryScreen());
503 if (maximized == VerticallyMaximized)
504 resize(width, desk.height());
505 if (maximized == HorizontallyMaximized)
506 resize(desk.width(), height);
513 // FIXME: move this code into parse_geometry() (LyX.cpp)
517 QRegExp re( "[=]*(?:([0-9]+)[xX]([0-9]+)){0,1}[ ]*(?:([+-][0-9]*)([+-][0-9]*)){0,1}" );
518 re.indexIn(toqstr(geometryArg.c_str()));
519 w = re.cap(1).toInt();
520 h = re.cap(2).toInt();
521 x = re.cap(3).toInt();
522 y = re.cap(4).toInt();
523 QWidget::setGeometry( x, y, w, h );
534 // For an unknown reason, the Window title update is not effective for
535 // the second windows up until it is shown on screen (Qt bug?).
538 // after show geometry() has changed (Qt bug?)
539 // we compensate the drift when storing the position
542 if (width != 0 && height != 0)
543 if (posx != -1 && posy != -1) {
545 d.posx_offset = posx - normalGeometry().x();
546 d.posy_offset = posy - normalGeometry().y();
549 if (maximized == NotMaximized) {
550 d.posx_offset = posx - geometry().x();
551 d.posy_offset = posy - geometry().y();
559 void GuiViewBase::setWindowTitle(docstring const & t, docstring const & it)
561 QString title = windowTitle();
562 QString new_title = toqstr(t);
563 if (title != new_title) {
564 QMainWindow::setWindowTitle(new_title);
565 QMainWindow::setWindowIconText(toqstr(it));
570 void GuiViewBase::message(docstring const & str)
572 statusBar()->showMessage(toqstr(str));
573 statusbar_timer_.stop();
574 statusbar_timer_.start(statusbar_timer_value);
578 void GuiViewBase::clearMessage()
580 update_view_state_qt();
584 void GuiViewBase::setIconSize(unsigned int size)
586 d.lastIconSize = size;
587 QMainWindow::setIconSize(QSize(size, size));
591 void GuiViewBase::smallSizedIcons()
593 setIconSize(d.smallIconSize);
597 void GuiViewBase::normalSizedIcons()
599 setIconSize(d.normalIconSize);
603 void GuiViewBase::bigSizedIcons()
605 setIconSize(d.bigIconSize);
609 void GuiViewBase::update_view_state_qt()
613 theLyXFunc().setLyXView(this);
614 statusBar()->showMessage(toqstr(theLyXFunc().viewStatusMessage()));
615 statusbar_timer_.stop();
619 void GuiViewBase::closeCurrentTab()
621 dispatch(FuncRequest(LFUN_BUFFER_CLOSE));
625 void GuiViewBase::currentTabChanged(int i)
628 disconnectBufferView();
629 GuiWorkArea * wa = dynamic_cast<GuiWorkArea *>(d.tab_widget_->widget(i));
631 BufferView & bv = wa->bufferView();
632 connectBufferView(bv);
633 connectBuffer(bv.buffer());
634 bv.updateMetrics(false);
635 bv.cursor().fixIfBroken();
636 wa->setUpdatesEnabled(true);
641 // Buffer-dependent dialogs should be updated or
642 // hidden. This should go here because some dialogs (eg ToC)
643 // require bv_->text.
644 getDialogs().updateBufferDependent(true);
646 updateLayoutChoice();
650 LYXERR(Debug::GUI) << "currentTabChanged " << i
651 << "File" << bv.buffer().fileName() << endl;
655 void GuiViewBase::updateStatusBar()
657 // let the user see the explicit message
658 if (statusbar_timer_.isActive())
661 statusBar()->showMessage(toqstr(theLyXFunc().viewStatusMessage()));
665 void GuiViewBase::activated(FuncRequest const & func)
671 bool GuiViewBase::hasFocus() const
673 return qApp->activeWindow() == this;
677 QRect GuiViewBase::updateFloatingGeometry()
679 QDesktopWidget& dw = *qApp->desktop();
680 QRect desk = dw.availableGeometry(dw.primaryScreen());
681 // remember only non-maximized sizes
682 if (!isMaximized() && desk.width() > frameGeometry().width() && desk.height() > frameGeometry().height()) {
683 floatingGeometry_ = QRect(x(), y(), width(), height());
685 return floatingGeometry_;
689 void GuiViewBase::resizeEvent(QResizeEvent *)
691 updateFloatingGeometry();
695 void GuiViewBase::moveEvent(QMoveEvent *)
697 updateFloatingGeometry();
701 bool GuiViewBase::event(QEvent * e)
705 // Useful debug code:
706 //case QEvent::WindowActivate:
707 //case QEvent::ActivationChange:
708 //case QEvent::WindowDeactivate:
709 //case QEvent::Paint:
710 //case QEvent::Enter:
711 //case QEvent::Leave:
712 //case QEvent::HoverEnter:
713 //case QEvent::HoverLeave:
714 //case QEvent::HoverMove:
715 //case QEvent::StatusTip:
716 //case QEvent::DragEnter:
717 //case QEvent::DragLeave:
721 case QEvent::ShortcutOverride: {
722 QKeyEvent * ke = static_cast<QKeyEvent*>(e);
723 if (d.tab_widget_->count() == 0) {
724 theLyXFunc().setLyXView(this);
725 boost::shared_ptr<GuiKeySymbol> sym(new GuiKeySymbol);
727 theLyXFunc().processKeySym(sym, q_key_state(ke->modifiers()));
731 if (ke->key() == Qt::Key_Tab || ke->key() == Qt::Key_Backtab) {
732 boost::shared_ptr<GuiKeySymbol> sym(new GuiKeySymbol);
734 currentWorkArea()->processKeySym(sym, key_modifier::none);
740 return QMainWindow::event(e);
745 bool GuiViewBase::focusNextPrevChild(bool /*next*/)
752 void GuiViewBase::showView()
754 QMainWindow::setWindowTitle(qt_("LyX"));
756 updateFloatingGeometry();
760 void GuiViewBase::busy(bool yes)
762 if (d.tab_widget_->count()) {
763 GuiWorkArea * wa = dynamic_cast<GuiWorkArea *>(d.tab_widget_->currentWidget());
765 wa->setUpdatesEnabled(!yes);
767 wa->stopBlinkingCursor();
769 wa->startBlinkingCursor();
773 QApplication::setOverrideCursor(Qt::WaitCursor);
775 QApplication::restoreOverrideCursor();
779 Toolbars::ToolbarPtr GuiViewBase::makeToolbar(ToolbarInfo const & tbinfo, bool newline)
781 GuiToolbar * toolBar = new GuiToolbar(tbinfo, *this);
783 if (tbinfo.flags & ToolbarInfo::TOP) {
785 addToolBarBreak(Qt::TopToolBarArea);
786 addToolBar(Qt::TopToolBarArea, toolBar);
789 if (tbinfo.flags & ToolbarInfo::BOTTOM) {
790 // Qt < 4.2.2 cannot handle ToolBarBreak on non-TOP dock.
791 #if (QT_VERSION >= 0x040202)
793 addToolBarBreak(Qt::BottomToolBarArea);
795 addToolBar(Qt::BottomToolBarArea, toolBar);
798 if (tbinfo.flags & ToolbarInfo::LEFT) {
799 // Qt < 4.2.2 cannot handle ToolBarBreak on non-TOP dock.
800 #if (QT_VERSION >= 0x040202)
802 addToolBarBreak(Qt::LeftToolBarArea);
804 addToolBar(Qt::LeftToolBarArea, toolBar);
807 if (tbinfo.flags & ToolbarInfo::RIGHT) {
808 // Qt < 4.2.2 cannot handle ToolBarBreak on non-TOP dock.
809 #if (QT_VERSION >= 0x040202)
811 addToolBarBreak(Qt::RightToolBarArea);
813 addToolBar(Qt::RightToolBarArea, toolBar);
816 // The following does not work so I cannot restore to exact toolbar location
818 ToolbarSection::ToolbarInfo & tbinfo = LyX::ref().session().toolbars().load(tbinfo.name);
819 toolBar->move(tbinfo.posx, tbinfo.posy);
822 return Toolbars::ToolbarPtr(toolBar);
826 WorkArea * GuiViewBase::workArea(Buffer & buffer)
828 for (int i = 0; i != d.tab_widget_->count(); ++i) {
829 GuiWorkArea * wa = dynamic_cast<GuiWorkArea *>(d.tab_widget_->widget(i));
831 if (&wa->bufferView().buffer() == &buffer)
838 WorkArea * GuiViewBase::addWorkArea(Buffer & buffer)
840 GuiWorkArea * wa = new GuiWorkArea(buffer, *this);
841 wa->setUpdatesEnabled(false);
842 d.tab_widget_->addTab(wa, toqstr(makeDisplayPath(buffer.fileName(), 30)));
843 wa->bufferView().updateMetrics(false);
845 d.stack_widget_->setCurrentWidget(d.tab_widget_);
846 // Hide tabbar if there's only one tab.
847 d.tab_widget_->showBar(d.tab_widget_->count() > 1);
852 WorkArea * GuiViewBase::currentWorkArea()
854 if (d.tab_widget_->count() == 0)
856 BOOST_ASSERT(dynamic_cast<GuiWorkArea *>(d.tab_widget_->currentWidget()));
857 return dynamic_cast<GuiWorkArea *>(d.tab_widget_->currentWidget());
861 WorkArea const * GuiViewBase::currentWorkArea() const
863 if (d.tab_widget_->count() == 0)
865 BOOST_ASSERT(dynamic_cast<GuiWorkArea const *>(d.tab_widget_->currentWidget()));
866 return dynamic_cast<GuiWorkArea const *>(d.tab_widget_->currentWidget());
870 void GuiViewBase::setCurrentWorkArea(WorkArea * work_area)
872 BOOST_ASSERT(work_area);
874 // Changing work area can result from opening a file so
875 // update the toc in any case.
878 GuiWorkArea * wa = dynamic_cast<GuiWorkArea *>(work_area);
880 if (wa != d.tab_widget_->currentWidget())
881 // Switch to the work area.
882 d.tab_widget_->setCurrentWidget(wa);
884 // Make sure the work area is up to date.
885 currentTabChanged(d.tab_widget_->currentIndex());
890 void GuiViewBase::removeWorkArea(WorkArea * work_area)
892 BOOST_ASSERT(work_area);
893 if (work_area == currentWorkArea()) {
895 disconnectBufferView();
898 // removing a work area often results from closing a file so
899 // update the toc in any case.
902 GuiWorkArea * gwa = dynamic_cast<GuiWorkArea *>(work_area);
903 gwa->setUpdatesEnabled(false);
905 int index = d.tab_widget_->indexOf(gwa);
906 d.tab_widget_->removeTab(index);
910 if (d.tab_widget_->count()) {
911 // make sure the next work area is enabled.
912 d.tab_widget_->currentWidget()->setUpdatesEnabled(true);
913 // Hide tabbar if there's only one tab.
914 d.tab_widget_->showBar(d.tab_widget_->count() > 1);
918 getDialogs().hideBufferDependent();
919 if (d.stack_widget_) {
920 // No more work area, switch to the background widget.
926 void GuiViewBase::showMiniBuffer(bool visible)
928 Toolbar * t = toolbars_->display("minibuffer", visible);
930 t->focusCommandBuffer();
934 void GuiViewBase::openMenu(docstring const & name)
936 d.menubar_->openByName(name);
939 } // namespace frontend
942 #include "GuiView_moc.cpp"