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/bind.hpp>
67 #include <boost/shared_ptr.hpp>
68 #include <boost/current_function.hpp>
76 using support::FileName;
77 using support::libFileSearch;
78 using support::makeDisplayPath;
84 int const statusbar_timer_value = 3000;
86 class BackgroundWidget : public QWidget
89 BackgroundWidget(QString const & file, QString const & text)
91 splash_ = new QPixmap(file);
93 lyxerr << "could not load splash screen: '" << fromqstr(file) << "'" << endl;
97 QPainter pain(splash_);
98 pain.setPen(QColor(255, 255, 0));
100 // The font used to display the version info
101 font.setStyleHint(QFont::SansSerif);
102 font.setWeight(QFont::Bold);
103 font.setPointSize(convert<int>(lyxrc.font_sizes[Font::SIZE_LARGE]));
105 pain.drawText(260, 270, text);
108 void paintEvent(QPaintEvent *)
113 int x = (width() - splash_->width()) / 2;
114 int y = (height() - splash_->height()) / 2;
116 pain.drawPixmap(x, y, *splash_);
124 class TabWidget : public QTabWidget {
126 void showBar(bool show) { tabBar()->setVisible(show); }
133 struct GuiViewBase::GuiViewPrivate
140 TabWidget * tab_widget_;
141 QStackedWidget * stack_widget_;
142 BackgroundWidget * bg_widget_;
144 GuiMenubar * menubar_;
146 GuiViewPrivate() : posx_offset(0), posy_offset(0) {}
148 unsigned int smallIconSize;
149 unsigned int normalIconSize;
150 unsigned int bigIconSize;
151 // static needed by "New Window"
152 static unsigned int lastIconSize;
154 QMenu * toolBarPopup(GuiViewBase * parent)
156 // FIXME: translation
157 QMenu * menu = new QMenu(parent);
158 QActionGroup * iconSizeGroup = new QActionGroup(parent);
160 QAction * smallIcons = new QAction(iconSizeGroup);
161 smallIcons->setText(qt_("Small-sized icons"));
162 smallIcons->setCheckable(true);
163 QObject::connect(smallIcons, SIGNAL(triggered()), parent, SLOT(smallSizedIcons()));
164 menu->addAction(smallIcons);
166 QAction * normalIcons = new QAction(iconSizeGroup);
167 normalIcons->setText(qt_("Normal-sized icons"));
168 normalIcons->setCheckable(true);
169 QObject::connect(normalIcons, SIGNAL(triggered()), parent, SLOT(normalSizedIcons()));
170 menu->addAction(normalIcons);
172 QAction * bigIcons = new QAction(iconSizeGroup);
173 bigIcons->setText(qt_("Big-sized icons"));
174 bigIcons->setCheckable(true);
175 QObject::connect(bigIcons, SIGNAL(triggered()), parent, SLOT(bigSizedIcons()));
176 menu->addAction(bigIcons);
178 unsigned int cur = parent->iconSize().width();
179 if ( cur == parent->d.smallIconSize)
180 smallIcons->setChecked(true);
181 else if (cur == parent->d.normalIconSize)
182 normalIcons->setChecked(true);
183 else if (cur == parent->d.bigIconSize)
184 bigIcons->setChecked(true);
189 void initBackground()
192 LYXERR(Debug::GUI) << "show banner: " << lyxrc.show_banner << endl;
193 /// The text to be written on top of the pixmap
194 QString const text = lyx_version ? QString(lyx_version) : qt_("unknown version");
195 FileName const file = support::libFileSearch("images", "banner", "png");
199 bg_widget_ = new BackgroundWidget(toqstr(file.absFilename()), text);
207 stack_widget_->setCurrentWidget(bg_widget_);
208 bg_widget_->setUpdatesEnabled(true);
213 unsigned int GuiViewBase::GuiViewPrivate::lastIconSize = 0;
216 GuiViewBase::GuiViewBase(int id)
217 : QMainWindow(), LyXView(id), quitting_by_menu_(false),
218 d(*new GuiViewPrivate)
220 // Qt bug? signal lastWindowClosed does not work
221 setAttribute(Qt::WA_QuitOnClose, false);
222 setAttribute(Qt::WA_DeleteOnClose, true);
224 // hardcode here the platform specific icon size
225 d.smallIconSize = 14; // scaling problems
226 d.normalIconSize = 20; // ok, default
227 d.bigIconSize = 26; // better for some math icons
230 // assign an icon to main form. We do not do it under Qt/Mac,
231 // since the icon is provided in the application bundle.
232 FileName const iconname = libFileSearch("images", "lyx", "png");
233 if (!iconname.empty())
234 setWindowIcon(QPixmap(toqstr(iconname.absFilename())));
237 d.tab_widget_ = new TabWidget;
239 QPushButton * closeTabButton = new QPushButton(this);
240 FileName const file = support::libFileSearch("images", "closetab", "png");
242 QPixmap pm(toqstr(file.absFilename()));
243 closeTabButton->setIcon(QIcon(pm));
244 closeTabButton->setMaximumSize(pm.size());
245 closeTabButton->setFlat(true);
247 closeTabButton->setText("Close");
249 closeTabButton->setCursor(Qt::ArrowCursor);
250 closeTabButton->setToolTip(tr("Close tab"));
251 closeTabButton->setEnabled(true);
253 QObject::connect(d.tab_widget_, SIGNAL(currentChanged(int)),
254 this, SLOT(currentTabChanged(int)));
255 QObject::connect(closeTabButton, SIGNAL(clicked()),
256 this, SLOT(closeCurrentTab()));
258 d.tab_widget_->setCornerWidget(closeTabButton);
259 #if QT_VERSION >= 0x040200
260 d.tab_widget_->setUsesScrollButtons(true);
265 LYXERR(Debug::GUI) << "stack widget!" << endl;
266 d.stack_widget_ = new QStackedWidget;
267 d.stack_widget_->addWidget(d.bg_widget_);
268 d.stack_widget_->addWidget(d.tab_widget_);
269 setCentralWidget(d.stack_widget_);
272 setCentralWidget(d.tab_widget_);
276 setAcceptDrops(true);
280 GuiViewBase::~GuiViewBase()
287 void GuiViewBase::close()
289 quitting_by_menu_ = true;
290 QMainWindow::close();
291 quitting_by_menu_ = false;
295 void GuiViewBase::setFocus()
297 if (d.tab_widget_->count())
298 d.tab_widget_->currentWidget()->setFocus();
302 QMenu* GuiViewBase::createPopupMenu()
304 return d.toolBarPopup(this);
308 void GuiViewBase::init()
310 d.menubar_ = new GuiMenubar(this, menubackend);
314 statusBar()->setSizeGripEnabled(true);
316 QObject::connect(&statusbar_timer_, SIGNAL(timeout()),
317 this, SLOT(update_view_state_qt()));
320 d.stack_widget_->setCurrentWidget(d.bg_widget_);
324 void GuiViewBase::closeEvent(QCloseEvent * close_event)
326 // we may have been called through the close window button
327 // which bypasses the LFUN machinery.
328 if (!quitting_by_menu_ && theApp()->gui().viewIds().size() == 1) {
329 if (!theBufferList().quitWriteAll()) {
330 close_event->ignore();
335 // Make sure that no LFUN use this close to be closed View.
336 theLyXFunc().setLyXView(0);
337 // Make sure the timer time out will not trigger a statusbar update.
338 statusbar_timer_.stop();
340 theApp()->gui().unregisterView(id());
341 if (!theApp()->gui().viewIds().empty()) {
342 // Just close the window and do nothing else if this is not the
344 close_event->accept();
350 // this is the place where we leave the frontend.
351 // it is the only point at which we start quitting.
353 close_event->accept();
354 // quit the event loop
359 void GuiViewBase::dragEnterEvent(QDragEnterEvent * event)
361 if (event->mimeData()->hasUrls())
363 /// \todo Ask lyx-devel is this is enough:
364 /// if (event->mimeData()->hasFormat("text/plain"))
365 /// event->acceptProposedAction();
369 void GuiViewBase::dropEvent(QDropEvent* event)
371 QList<QUrl> files = event->mimeData()->urls();
375 LYXERR(Debug::GUI) << BOOST_CURRENT_FUNCTION
376 << " got URLs!" << endl;
377 for (int i = 0; i != files.size(); ++i) {
378 string const file = support::os::internal_path(fromqstr(
379 files.at(i).toLocalFile()));
381 dispatch(FuncRequest(LFUN_FILE_OPEN, file));
386 void GuiViewBase::saveGeometry()
388 static bool done = false;
395 // change the ifdef to 'geometry = normalGeometry();' only
396 // when Trolltech has fixed the broken normalGeometry on X11:
397 // http://www.trolltech.com/developer/task-tracker/index_html?id=119684+&method=entry
398 // Then also the moveEvent, resizeEvent, and the
399 // code for floatingGeometry_ can be removed;
400 // adjust GuiViewBase::setGeometry()
402 QRect normal_geometry;
405 normal_geometry = normalGeometry();
407 maximized = CompletelyMaximized;
409 maximized = NotMaximized;
412 normal_geometry = updateFloatingGeometry();
414 QDesktopWidget& dw = *qApp->desktop();
415 QRect desk = dw.availableGeometry(dw.primaryScreen());
416 // Qt bug on Linux: load completely maximized, vert max. save-> frameGeometry().height() is wrong
417 if (isMaximized() && desk.width() <= frameGeometry().width() && desk.height() <= frameGeometry().height()) {
418 maximized = CompletelyMaximized;
419 // maximizing does not work when the window is allready hor. or vert. maximized
420 // Tested only on KDE
421 int dh = frameGeometry().height() - height();
422 if (desk.height() <= normal_geometry.height() + dh)
423 normal_geometry.setHeight(normal_geometry.height() - 1);
424 int dw = frameGeometry().width() - width();
425 if (desk.width() <= normal_geometry.width() + dw)
426 normal_geometry.setWidth(normal_geometry.width() - 1);
427 } else if (desk.height() <= frameGeometry().height()) {
428 maximized = VerticallyMaximized;
429 } else if (desk.width() <= frameGeometry().width()) {
430 maximized = HorizontallyMaximized;
432 maximized = NotMaximized;
437 // save windows size and position
438 Session & session = LyX::ref().session();
439 session.sessionInfo().save("WindowWidth", convert<string>(normal_geometry.width()));
440 session.sessionInfo().save("WindowHeight", convert<string>(normal_geometry.height()));
441 session.sessionInfo().save("WindowMaximized", convert<string>(maximized));
442 session.sessionInfo().save("IconSizeXY", convert<string>(iconSize().width()));
443 if (lyxrc.geometry_xysaved) {
444 session.sessionInfo().save("WindowPosX", convert<string>(normal_geometry.x() + d.posx_offset));
445 session.sessionInfo().save("WindowPosY", convert<string>(normal_geometry.y() + d.posy_offset));
447 toolbars_->saveToolbarInfo();
451 void GuiViewBase::setGeometry(unsigned int width,
455 unsigned int iconSizeXY,
456 const string & geometryArg)
458 // use last value (not at startup)
459 if (d.lastIconSize != 0)
460 setIconSize(d.lastIconSize);
461 else if (iconSizeXY != 0)
462 setIconSize(iconSizeXY);
464 setIconSize(d.normalIconSize);
466 // only true when the -geometry option was NOT used
467 if (width != 0 && height != 0) {
468 if (posx != -1 && posy != -1) {
469 // if there are startup positioning problems:
470 // http://doc.trolltech.com/4.2/qdesktopwidget.html
471 QDesktopWidget& dw = *qApp->desktop();
472 if (dw.isVirtualDesktop()) {
473 if(!dw.geometry().contains(posx, posy)) {
478 // Which system doesn't use a virtual desktop?
479 // TODO save also last screen number and check if it is still availabe.
482 // FIXME: use setGeometry only when Trolltech has fixed the qt4/X11 bug
483 QWidget::setGeometry(posx, posy, width, height);
485 resize(width, height);
489 resize(width, height);
492 // remember original size
493 floatingGeometry_ = QRect(posx, posy, width, height);
495 if (maximized != NotMaximized) {
496 if (maximized == CompletelyMaximized) {
497 setWindowState(Qt::WindowMaximized);
500 // TODO How to set by the window manager?
501 // setWindowState(Qt::WindowVerticallyMaximized);
503 QDesktopWidget& dw = *qApp->desktop();
504 QRect desk = dw.availableGeometry(dw.primaryScreen());
505 if (maximized == VerticallyMaximized)
506 resize(width, desk.height());
507 if (maximized == HorizontallyMaximized)
508 resize(desk.width(), height);
515 // FIXME: move this code into parse_geometry() (LyX.cpp)
519 QRegExp re( "[=]*(?:([0-9]+)[xX]([0-9]+)){0,1}[ ]*(?:([+-][0-9]*)([+-][0-9]*)){0,1}" );
520 re.indexIn(toqstr(geometryArg.c_str()));
521 w = re.cap(1).toInt();
522 h = re.cap(2).toInt();
523 x = re.cap(3).toInt();
524 y = re.cap(4).toInt();
525 QWidget::setGeometry( x, y, w, h );
536 // For an unknown reason, the Window title update is not effective for
537 // the second windows up until it is shown on screen (Qt bug?).
540 // after show geometry() has changed (Qt bug?)
541 // we compensate the drift when storing the position
544 if (width != 0 && height != 0)
545 if (posx != -1 && posy != -1) {
547 d.posx_offset = posx - normalGeometry().x();
548 d.posy_offset = posy - normalGeometry().y();
551 if (maximized == NotMaximized) {
552 d.posx_offset = posx - geometry().x();
553 d.posy_offset = posy - geometry().y();
561 void GuiViewBase::setWindowTitle(docstring const & t, docstring const & it)
563 QString title = windowTitle();
564 QString new_title = toqstr(t);
565 if (title != new_title) {
566 QMainWindow::setWindowTitle(new_title);
567 QMainWindow::setWindowIconText(toqstr(it));
572 void GuiViewBase::message(docstring const & str)
574 statusBar()->showMessage(toqstr(str));
575 statusbar_timer_.stop();
576 statusbar_timer_.start(statusbar_timer_value);
580 void GuiViewBase::clearMessage()
582 update_view_state_qt();
586 void GuiViewBase::setIconSize(unsigned int size)
588 d.lastIconSize = size;
589 QMainWindow::setIconSize(QSize(size, size));
593 void GuiViewBase::smallSizedIcons()
595 setIconSize(d.smallIconSize);
599 void GuiViewBase::normalSizedIcons()
601 setIconSize(d.normalIconSize);
605 void GuiViewBase::bigSizedIcons()
607 setIconSize(d.bigIconSize);
611 void GuiViewBase::update_view_state_qt()
615 theLyXFunc().setLyXView(this);
616 statusBar()->showMessage(toqstr(theLyXFunc().viewStatusMessage()));
617 statusbar_timer_.stop();
621 void GuiViewBase::closeCurrentTab()
623 dispatch(FuncRequest(LFUN_BUFFER_CLOSE));
627 void GuiViewBase::currentTabChanged(int i)
630 disconnectBufferView();
631 GuiWorkArea * wa = dynamic_cast<GuiWorkArea *>(d.tab_widget_->widget(i));
633 BufferView & bv = wa->bufferView();
634 connectBufferView(bv);
635 connectBuffer(bv.buffer());
636 bv.updateMetrics(false);
637 bv.cursor().fixIfBroken();
638 wa->setUpdatesEnabled(true);
643 // Buffer-dependent dialogs should be updated or
644 // hidden. This should go here because some dialogs (eg ToC)
645 // require bv_->text.
646 getDialogs().updateBufferDependent(true);
648 updateLayoutChoice();
652 LYXERR(Debug::GUI) << "currentTabChanged " << i
653 << "File" << bv.buffer().fileName() << endl;
657 void GuiViewBase::updateStatusBar()
659 // let the user see the explicit message
660 if (statusbar_timer_.isActive())
663 statusBar()->showMessage(toqstr(theLyXFunc().viewStatusMessage()));
667 void GuiViewBase::activated(FuncRequest const & func)
673 bool GuiViewBase::hasFocus() const
675 return qApp->activeWindow() == this;
679 QRect GuiViewBase::updateFloatingGeometry()
681 QDesktopWidget& dw = *qApp->desktop();
682 QRect desk = dw.availableGeometry(dw.primaryScreen());
683 // remember only non-maximized sizes
684 if (!isMaximized() && desk.width() > frameGeometry().width() && desk.height() > frameGeometry().height()) {
685 floatingGeometry_ = QRect(x(), y(), width(), height());
687 return floatingGeometry_;
691 void GuiViewBase::resizeEvent(QResizeEvent *)
693 updateFloatingGeometry();
697 void GuiViewBase::moveEvent(QMoveEvent *)
699 updateFloatingGeometry();
703 bool GuiViewBase::event(QEvent * e)
707 // Useful debug code:
708 //case QEvent::WindowActivate:
709 //case QEvent::ActivationChange:
710 //case QEvent::WindowDeactivate:
711 //case QEvent::Paint:
712 //case QEvent::Enter:
713 //case QEvent::Leave:
714 //case QEvent::HoverEnter:
715 //case QEvent::HoverLeave:
716 //case QEvent::HoverMove:
717 //case QEvent::StatusTip:
718 //case QEvent::DragEnter:
719 //case QEvent::DragLeave:
723 case QEvent::ShortcutOverride: {
724 QKeyEvent * ke = static_cast<QKeyEvent*>(e);
725 if (d.tab_widget_->count() == 0) {
726 theLyXFunc().setLyXView(this);
727 boost::shared_ptr<GuiKeySymbol> sym(new GuiKeySymbol);
729 theLyXFunc().processKeySym(sym, q_key_state(ke->modifiers()));
733 if (ke->key() == Qt::Key_Tab || ke->key() == Qt::Key_Backtab) {
734 boost::shared_ptr<GuiKeySymbol> sym(new GuiKeySymbol);
736 currentWorkArea()->processKeySym(sym, key_modifier::none);
742 return QMainWindow::event(e);
747 bool GuiViewBase::focusNextPrevChild(bool /*next*/)
754 void GuiViewBase::showView()
756 QMainWindow::setWindowTitle(qt_("LyX"));
758 updateFloatingGeometry();
762 void GuiViewBase::busy(bool yes)
764 if (d.tab_widget_->count()) {
765 GuiWorkArea * wa = dynamic_cast<GuiWorkArea *>(d.tab_widget_->currentWidget());
767 wa->setUpdatesEnabled(!yes);
769 wa->stopBlinkingCursor();
771 wa->startBlinkingCursor();
775 QApplication::setOverrideCursor(Qt::WaitCursor);
777 QApplication::restoreOverrideCursor();
781 Toolbars::ToolbarPtr GuiViewBase::makeToolbar(ToolbarInfo const & tbinfo, bool newline)
783 GuiToolbar * toolBar = new GuiToolbar(tbinfo, *this);
785 if (tbinfo.flags & ToolbarInfo::TOP) {
787 addToolBarBreak(Qt::TopToolBarArea);
788 addToolBar(Qt::TopToolBarArea, toolBar);
791 if (tbinfo.flags & ToolbarInfo::BOTTOM) {
792 // Qt < 4.2.2 cannot handle ToolBarBreak on non-TOP dock.
793 #if (QT_VERSION >= 0x040202)
795 addToolBarBreak(Qt::BottomToolBarArea);
797 addToolBar(Qt::BottomToolBarArea, toolBar);
800 if (tbinfo.flags & ToolbarInfo::LEFT) {
801 // Qt < 4.2.2 cannot handle ToolBarBreak on non-TOP dock.
802 #if (QT_VERSION >= 0x040202)
804 addToolBarBreak(Qt::LeftToolBarArea);
806 addToolBar(Qt::LeftToolBarArea, toolBar);
809 if (tbinfo.flags & ToolbarInfo::RIGHT) {
810 // Qt < 4.2.2 cannot handle ToolBarBreak on non-TOP dock.
811 #if (QT_VERSION >= 0x040202)
813 addToolBarBreak(Qt::RightToolBarArea);
815 addToolBar(Qt::RightToolBarArea, toolBar);
818 // The following does not work so I cannot restore to exact toolbar location
820 ToolbarSection::ToolbarInfo & tbinfo = LyX::ref().session().toolbars().load(tbinfo.name);
821 toolBar->move(tbinfo.posx, tbinfo.posy);
824 return Toolbars::ToolbarPtr(toolBar);
828 WorkArea * GuiViewBase::workArea(Buffer & buffer)
830 for (int i = 0; i != d.tab_widget_->count(); ++i) {
831 GuiWorkArea * wa = dynamic_cast<GuiWorkArea *>(d.tab_widget_->widget(i));
833 if (&wa->bufferView().buffer() == &buffer)
840 WorkArea * GuiViewBase::addWorkArea(Buffer & buffer)
842 GuiWorkArea * wa = new GuiWorkArea(buffer, *this);
843 wa->setUpdatesEnabled(false);
844 d.tab_widget_->addTab(wa, toqstr(makeDisplayPath(buffer.fileName(), 30)));
845 wa->bufferView().updateMetrics(false);
847 d.stack_widget_->setCurrentWidget(d.tab_widget_);
848 // Hide tabbar if there's only one tab.
849 d.tab_widget_->showBar(d.tab_widget_->count() > 1);
854 WorkArea * GuiViewBase::currentWorkArea()
856 if (d.tab_widget_->count() == 0)
858 BOOST_ASSERT(dynamic_cast<GuiWorkArea *>(d.tab_widget_->currentWidget()));
859 return dynamic_cast<GuiWorkArea *>(d.tab_widget_->currentWidget());
863 WorkArea const * GuiViewBase::currentWorkArea() const
865 if (d.tab_widget_->count() == 0)
867 BOOST_ASSERT(dynamic_cast<GuiWorkArea const *>(d.tab_widget_->currentWidget()));
868 return dynamic_cast<GuiWorkArea const *>(d.tab_widget_->currentWidget());
872 void GuiViewBase::setCurrentWorkArea(WorkArea * work_area)
874 BOOST_ASSERT(work_area);
876 // Changing work area can result from opening a file so
877 // update the toc in any case.
880 GuiWorkArea * wa = dynamic_cast<GuiWorkArea *>(work_area);
882 if (wa != d.tab_widget_->currentWidget())
883 // Switch to the work area.
884 d.tab_widget_->setCurrentWidget(wa);
886 // Make sure the work area is up to date.
887 currentTabChanged(d.tab_widget_->currentIndex());
892 void GuiViewBase::removeWorkArea(WorkArea * work_area)
894 BOOST_ASSERT(work_area);
895 if (work_area == currentWorkArea()) {
897 disconnectBufferView();
900 // removing a work area often results from closing a file so
901 // update the toc in any case.
904 GuiWorkArea * gwa = dynamic_cast<GuiWorkArea *>(work_area);
905 gwa->setUpdatesEnabled(false);
907 int index = d.tab_widget_->indexOf(gwa);
908 d.tab_widget_->removeTab(index);
912 if (d.tab_widget_->count()) {
913 // make sure the next work area is enabled.
914 d.tab_widget_->currentWidget()->setUpdatesEnabled(true);
915 // Hide tabbar if there's only one tab.
916 d.tab_widget_->showBar(d.tab_widget_->count() > 1);
920 getDialogs().hideBufferDependent();
921 if (d.stack_widget_) {
922 // No more work area, switch to the background widget.
928 void GuiViewBase::showMiniBuffer(bool visible)
930 Toolbar * t = toolbars_->display("minibuffer", visible);
932 t->focusCommandBuffer();
936 void GuiViewBase::openMenu(docstring const & name)
938 d.menubar_->openByName(name);
941 } // namespace frontend
944 #include "GuiView_moc.cpp"