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 "QKeySymbol.h"
21 #include "QLMenubar.h"
22 #include "QLToolbar.h"
23 #include "QCommandBuffer.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 "BufferView.h"
38 #include "BufferList.h"
41 #include "FuncRequest.h"
45 #include "MenuBackend.h"
50 #include <QApplication>
51 #include <QCloseEvent>
52 #include <QDesktopWidget>
53 #include <QDragEnterEvent>
59 #include <QPushButton>
60 #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 * ev)
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 GuiView::GuiViewPrivate
140 TabWidget * tab_widget_;
141 QStackedWidget * stack_widget_;
142 BackgroundWidget * bg_widget_;
144 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 FileName const file = support::libFileSearch("images", "banner", "png");
198 bg_widget_ = new BackgroundWidget(toqstr(file.absFilename()), text);
206 stack_widget_->setCurrentWidget(bg_widget_);
207 bg_widget_->setUpdatesEnabled(true);
212 unsigned int GuiView::GuiViewPrivate::lastIconSize = 0;
215 GuiView::GuiView(int id)
216 : QMainWindow(), LyXView(id), commandbuffer_(0), quitting_by_menu_(false),
217 d(*new GuiViewPrivate)
219 // Qt bug? signal lastWindowClosed does not work
220 setAttribute(Qt::WA_QuitOnClose, false);
221 setAttribute(Qt::WA_DeleteOnClose, true);
223 // hardcode here the platform specific icon size
224 d.smallIconSize = 14; // scaling problems
225 d.normalIconSize = 20; // ok, default
226 d.bigIconSize = 26; // better for some math icons
229 // assign an icon to main form. We do not do it under Qt/Mac,
230 // since the icon is provided in the application bundle.
231 FileName const iconname = libFileSearch("images", "lyx", "xpm");
232 if (!iconname.empty())
233 setWindowIcon(QPixmap(toqstr(iconname.absFilename())));
236 d.tab_widget_ = new TabWidget;
238 QPushButton * closeTabButton = new QPushButton(this);
239 FileName const file = support::libFileSearch("images", "closetab", "xpm");
241 QPixmap pm(toqstr(file.absFilename()));
242 closeTabButton->setIcon(QIcon(pm));
243 closeTabButton->setMaximumSize(pm.size());
244 closeTabButton->setFlat(true);
246 closeTabButton->setText("Close");
248 closeTabButton->setCursor(Qt::ArrowCursor);
249 closeTabButton->setToolTip(tr("Close tab"));
250 closeTabButton->setEnabled(true);
252 QObject::connect(d.tab_widget_, SIGNAL(currentChanged(int)),
253 this, SLOT(currentTabChanged(int)));
254 QObject::connect(closeTabButton, SIGNAL(clicked()),
255 this, SLOT(closeCurrentTab()));
257 d.tab_widget_->setCornerWidget(closeTabButton);
258 #if QT_VERSION >= 0x040200
259 d.tab_widget_->setUsesScrollButtons(true);
264 lyxerr << "stack widget!" << endl;
265 d.stack_widget_ = new QStackedWidget;
266 d.stack_widget_->addWidget(d.bg_widget_);
267 d.stack_widget_->addWidget(d.tab_widget_);
268 setCentralWidget(d.stack_widget_);
271 setCentralWidget(d.tab_widget_);
275 setAcceptDrops(true);
286 void GuiView::close()
288 quitting_by_menu_ = true;
289 QMainWindow::close();
290 quitting_by_menu_ = false;
294 void GuiView::setFocus()
296 if (d.tab_widget_->count())
297 d.tab_widget_->currentWidget()->setFocus();
301 QMenu* GuiView::createPopupMenu()
303 return d.toolBarPopup(this);
309 menubar_.reset(new QLMenubar(this, menubackend));
310 QObject::connect(menuBar(), SIGNAL(triggered(QAction *)),
311 this, SLOT(updateMenu(QAction *)));
313 getToolbars().init();
315 statusBar()->setSizeGripEnabled(true);
317 QObject::connect(&statusbar_timer_, SIGNAL(timeout()),
318 this, SLOT(update_view_state_qt()));
321 d.stack_widget_->setCurrentWidget(d.bg_widget_);
325 void GuiView::closeEvent(QCloseEvent * close_event)
327 // we may have been called through the close window button
328 // which bypasses the LFUN machinery.
329 if (!quitting_by_menu_ && theApp()->gui().viewIds().size() == 1) {
330 if (!theBufferList().quitWriteAll()) {
331 close_event->ignore();
336 // Make sure that no LFUN use this close to be closed View.
337 theLyXFunc().setLyXView(0);
338 // Make sure the timer time out will not trigger a statusbar update.
339 statusbar_timer_.stop();
341 theApp()->gui().unregisterView(id());
342 if (!theApp()->gui().viewIds().empty()) {
343 // Just close the window and do nothing else if this is not the
345 close_event->accept();
351 // this is the place where we leave the frontend.
352 // it is the only point at which we start quitting.
354 close_event->accept();
355 // quit the event loop
360 void GuiView::dragEnterEvent(QDragEnterEvent * event)
362 if (event->mimeData()->hasUrls())
364 /// \todo Ask lyx-devel is this is enough:
365 /// if (event->mimeData()->hasFormat("text/plain"))
366 /// event->acceptProposedAction();
370 void GuiView::dropEvent(QDropEvent* event)
372 QList<QUrl> files = event->mimeData()->urls();
376 LYXERR(Debug::GUI) << BOOST_CURRENT_FUNCTION
377 << " got URLs!" << endl;
378 for (int i = 0; i!=files.size(); ++i) {
379 string const file = support::os::internal_path(fromqstr(
380 files.at(i).toLocalFile()));
382 dispatch(FuncRequest(LFUN_FILE_OPEN, file));
387 void GuiView::saveGeometry()
389 static bool done = false;
396 // change the ifdef to 'geometry = normalGeometry();' only
397 // when Trolltech has fixed the broken normalGeometry on X11:
398 // http://www.trolltech.com/developer/task-tracker/index_html?id=119684+&method=entry
399 // Then also the moveEvent, resizeEvent, and the
400 // code for floatingGeometry_ can be removed;
401 // adjust GuiView::setGeometry()
403 QRect normal_geometry;
406 normal_geometry = normalGeometry();
408 maximized = CompletelyMaximized;
410 maximized = NotMaximized;
413 normal_geometry = updateFloatingGeometry();
415 QDesktopWidget& dw = *qApp->desktop();
416 QRect desk = dw.availableGeometry(dw.primaryScreen());
417 // Qt bug on Linux: load completely maximized, vert max. save-> frameGeometry().height() is wrong
418 if (isMaximized() && desk.width() <= frameGeometry().width() && desk.height() <= frameGeometry().height()) {
419 maximized = CompletelyMaximized;
420 // maximizing does not work when the window is allready hor. or vert. maximized
421 // Tested only on KDE
422 int dh = frameGeometry().height() - height();
423 if (desk.height() <= normal_geometry.height() + dh)
424 normal_geometry.setHeight(normal_geometry.height() - 1);
425 int dw = frameGeometry().width() - width();
426 if (desk.width() <= normal_geometry.width() + dw)
427 normal_geometry.setWidth(normal_geometry.width() - 1);
428 } else if (desk.height() <= frameGeometry().height()) {
429 maximized = VerticallyMaximized;
430 } else if (desk.width() <= frameGeometry().width()) {
431 maximized = HorizontallyMaximized;
433 maximized = NotMaximized;
438 // save windows size and position
439 Session & session = LyX::ref().session();
440 session.sessionInfo().save("WindowWidth", convert<string>(normal_geometry.width()));
441 session.sessionInfo().save("WindowHeight", convert<string>(normal_geometry.height()));
442 session.sessionInfo().save("WindowMaximized", convert<string>(maximized));
443 session.sessionInfo().save("IconSizeXY", convert<string>(iconSize().width()));
444 if (lyxrc.geometry_xysaved) {
445 session.sessionInfo().save("WindowPosX", convert<string>(normal_geometry.x() + d.posx_offset));
446 session.sessionInfo().save("WindowPosY", convert<string>(normal_geometry.y() + d.posy_offset));
448 getToolbars().saveToolbarInfo();
452 void GuiView::setGeometry(unsigned int width,
456 unsigned int iconSizeXY,
457 const string & geometryArg)
459 // use last value (not at startup)
460 if (d.lastIconSize != 0)
461 setIconSize(d.lastIconSize);
462 else if (iconSizeXY != 0)
463 setIconSize(iconSizeXY);
465 setIconSize(d.normalIconSize);
467 // only true when the -geometry option was NOT used
468 if (width != 0 && height != 0) {
469 if (posx != -1 && posy != -1) {
470 // if there are startup positioning problems:
471 // http://doc.trolltech.com/4.2/qdesktopwidget.html
472 QDesktopWidget& dw = *qApp->desktop();
473 if (dw.isVirtualDesktop()) {
474 if(!dw.geometry().contains(posx, posy)) {
479 // Which system doesn't use a virtual desktop?
480 // TODO save also last screen number and check if it is still availabe.
483 // FIXME: use setGeometry only when Trolltech has fixed the qt4/X11 bug
484 QWidget::setGeometry(posx, posy, width, height);
486 resize(width, height);
490 resize(width, height);
493 // remember original size
494 floatingGeometry_ = QRect(posx, posy, width, height);
496 if (maximized != NotMaximized) {
497 if (maximized == CompletelyMaximized) {
498 setWindowState(Qt::WindowMaximized);
501 // TODO How to set by the window manager?
502 // setWindowState(Qt::WindowVerticallyMaximized);
504 QDesktopWidget& dw = *qApp->desktop();
505 QRect desk = dw.availableGeometry(dw.primaryScreen());
506 if (maximized == VerticallyMaximized)
507 resize(width, desk.height());
508 if (maximized == HorizontallyMaximized)
509 resize(desk.width(), height);
516 // FIXME: move this code into parse_geometry() (LyX.cpp)
520 QRegExp re( "[=]*(?:([0-9]+)[xX]([0-9]+)){0,1}[ ]*(?:([+-][0-9]*)([+-][0-9]*)){0,1}" );
521 re.indexIn(toqstr(geometryArg.c_str()));
522 w = re.cap(1).toInt();
523 h = re.cap(2).toInt();
524 x = re.cap(3).toInt();
525 y = re.cap(4).toInt();
526 QWidget::setGeometry( x, y, w, h );
537 // For an unknown reason, the Window title update is not effective for
538 // the second windows up until it is shown on screen (Qt bug?).
541 // after show geometry() has changed (Qt bug?)
542 // we compensate the drift when storing the position
545 if (width != 0 && height != 0)
546 if (posx != -1 && posy != -1) {
548 d.posx_offset = posx - normalGeometry().x();
549 d.posy_offset = posy - normalGeometry().y();
552 if (maximized == NotMaximized) {
553 d.posx_offset = posx - geometry().x();
554 d.posy_offset = posy - geometry().y();
562 void GuiView::updateMenu(QAction * /*action*/)
568 void GuiView::setWindowTitle(docstring const & t, docstring const & it)
570 QString title = windowTitle();
571 QString new_title = toqstr(t);
572 if (title != new_title) {
573 QMainWindow::setWindowTitle(new_title);
574 QMainWindow::setWindowIconText(toqstr(it));
579 void GuiView::addCommandBuffer(QToolBar * toolbar)
581 commandbuffer_ = new QCommandBuffer(this, *controlcommand_);
582 focus_command_buffer.connect(boost::bind(&GuiView::focus_command_widget, this));
583 toolbar->addWidget(commandbuffer_);
587 void GuiView::message(docstring const & str)
589 statusBar()->showMessage(toqstr(str));
590 statusbar_timer_.stop();
591 statusbar_timer_.start(statusbar_timer_value);
595 void GuiView::clearMessage()
597 update_view_state_qt();
601 void GuiView::setIconSize(unsigned int size)
603 d.lastIconSize = size;
604 QMainWindow::setIconSize(QSize(size, size));
608 void GuiView::smallSizedIcons()
610 setIconSize(d.smallIconSize);
614 void GuiView::normalSizedIcons()
616 setIconSize(d.normalIconSize);
620 void GuiView::bigSizedIcons()
622 setIconSize(d.bigIconSize);
626 void GuiView::focus_command_widget()
629 commandbuffer_->focus_command();
633 void GuiView::update_view_state_qt()
637 theLyXFunc().setLyXView(this);
638 statusBar()->showMessage(toqstr(theLyXFunc().viewStatusMessage()));
639 statusbar_timer_.stop();
643 void GuiView::closeCurrentTab()
645 dispatch(FuncRequest(LFUN_BUFFER_CLOSE));
649 void GuiView::currentTabChanged(int i)
652 disconnectBufferView();
653 GuiWorkArea * wa = dynamic_cast<GuiWorkArea *>(d.tab_widget_->widget(i));
655 BufferView & bv = wa->bufferView();
656 connectBufferView(bv);
657 connectBuffer(bv.buffer());
658 bv.updateMetrics(false);
659 bv.cursor().fixIfBroken();
660 wa->setUpdatesEnabled(true);
665 // Buffer-dependent dialogs should be updated or
666 // hidden. This should go here because some dialogs (eg ToC)
667 // require bv_->text.
668 getDialogs().updateBufferDependent(true);
671 updateLayoutChoice();
675 lyxerr << "currentTabChanged " << i
676 << "File" << bv.buffer().fileName() << endl;
680 void GuiView::updateStatusBar()
682 // let the user see the explicit message
683 if (statusbar_timer_.isActive())
686 statusBar()->showMessage(toqstr(theLyXFunc().viewStatusMessage()));
690 void GuiView::activated(FuncRequest const & func)
696 bool GuiView::hasFocus() const
698 return qApp->activeWindow() == this;
702 QRect GuiView::updateFloatingGeometry()
704 QDesktopWidget& dw = *qApp->desktop();
705 QRect desk = dw.availableGeometry(dw.primaryScreen());
706 // remember only non-maximized sizes
707 if (!isMaximized() && desk.width() > frameGeometry().width() && desk.height() > frameGeometry().height()) {
708 floatingGeometry_ = QRect(x(), y(), width(), height());
710 return floatingGeometry_;
714 void GuiView::resizeEvent(QResizeEvent *)
716 updateFloatingGeometry();
720 void GuiView::moveEvent(QMoveEvent *)
722 updateFloatingGeometry();
726 bool GuiView::event(QEvent * e)
730 // Useful debug code:
731 //case QEvent::WindowActivate:
732 //case QEvent::ActivationChange:
733 //case QEvent::WindowDeactivate:
734 //case QEvent::Paint:
735 //case QEvent::Enter:
736 //case QEvent::Leave:
737 //case QEvent::HoverEnter:
738 //case QEvent::HoverLeave:
739 //case QEvent::HoverMove:
740 //case QEvent::StatusTip:
741 //case QEvent::DragEnter:
742 //case QEvent::DragLeave:
746 case QEvent::ShortcutOverride: {
747 QKeyEvent * ke = static_cast<QKeyEvent*>(e);
748 if (d.tab_widget_->count() == 0) {
749 theLyXFunc().setLyXView(this);
750 boost::shared_ptr<QKeySymbol> sym(new QKeySymbol);
752 theLyXFunc().processKeySym(sym, q_key_state(ke->modifiers()));
756 if (ke->key() == Qt::Key_Tab || ke->key() == Qt::Key_Backtab) {
757 boost::shared_ptr<QKeySymbol> sym(new QKeySymbol);
759 currentWorkArea()->processKeySym(sym, key_modifier::none);
765 return QMainWindow::event(e);
770 bool GuiView::focusNextPrevChild(bool /*next*/)
779 QMainWindow::setWindowTitle(qt_("LyX"));
781 updateFloatingGeometry();
785 void GuiView::busy(bool yes)
787 if (d.tab_widget_->count()) {
788 GuiWorkArea * wa = dynamic_cast<GuiWorkArea *>(d.tab_widget_->currentWidget());
790 wa->setUpdatesEnabled(!yes);
792 wa->stopBlinkingCursor();
794 wa->startBlinkingCursor();
798 QApplication::setOverrideCursor(Qt::WaitCursor);
800 QApplication::restoreOverrideCursor();
804 Toolbars::ToolbarPtr GuiView::makeToolbar(ToolbarInfo const & tbinfo, bool newline)
806 QLToolbar * Tb = new QLToolbar(tbinfo, *this);
808 if (tbinfo.flags & ToolbarInfo::TOP) {
810 addToolBarBreak(Qt::TopToolBarArea);
811 addToolBar(Qt::TopToolBarArea, Tb);
814 if (tbinfo.flags & ToolbarInfo::BOTTOM) {
815 // Qt < 4.2.2 cannot handle ToolBarBreak on non-TOP dock.
816 #if (QT_VERSION >= 0x040202)
818 addToolBarBreak(Qt::BottomToolBarArea);
820 addToolBar(Qt::BottomToolBarArea, Tb);
823 if (tbinfo.flags & ToolbarInfo::LEFT) {
824 // Qt < 4.2.2 cannot handle ToolBarBreak on non-TOP dock.
825 #if (QT_VERSION >= 0x040202)
827 addToolBarBreak(Qt::LeftToolBarArea);
829 addToolBar(Qt::LeftToolBarArea, Tb);
832 if (tbinfo.flags & ToolbarInfo::RIGHT) {
833 // Qt < 4.2.2 cannot handle ToolBarBreak on non-TOP dock.
834 #if (QT_VERSION >= 0x040202)
836 addToolBarBreak(Qt::RightToolBarArea);
838 addToolBar(Qt::RightToolBarArea, Tb);
841 // The following does not work so I cannot restore to exact toolbar location
843 ToolbarSection::ToolbarInfo & tbinfo = LyX::ref().session().toolbars().load(tbinfo.name);
844 Tb->move(tbinfo.posx, tbinfo.posy);
847 return Toolbars::ToolbarPtr(Tb);
851 WorkArea * GuiView::workArea(Buffer & buffer)
853 for (int i = 0; i != d.tab_widget_->count(); ++i) {
854 GuiWorkArea * wa = dynamic_cast<GuiWorkArea *>(d.tab_widget_->widget(i));
856 if (&wa->bufferView().buffer() == &buffer)
863 WorkArea * GuiView::addWorkArea(Buffer & buffer)
865 GuiWorkArea * wa = new GuiWorkArea(buffer, *this);
866 d.tab_widget_->addTab(wa, toqstr(makeDisplayPath(buffer.fileName(), 30)));
867 wa->bufferView().updateMetrics(false);
869 d.stack_widget_->setCurrentWidget(d.tab_widget_);
870 // Hide tabbar if there's only one tab.
871 d.tab_widget_->showBar(d.tab_widget_->count() > 1);
876 WorkArea * GuiView::currentWorkArea()
878 if (d.tab_widget_->count() == 0)
880 BOOST_ASSERT(dynamic_cast<GuiWorkArea *>(d.tab_widget_->currentWidget()));
881 return dynamic_cast<GuiWorkArea *>(d.tab_widget_->currentWidget());
885 WorkArea const * GuiView::currentWorkArea() const
887 if (d.tab_widget_->count() == 0)
889 BOOST_ASSERT(dynamic_cast<GuiWorkArea const *>(d.tab_widget_->currentWidget()));
890 return dynamic_cast<GuiWorkArea const *>(d.tab_widget_->currentWidget());
894 void GuiView::setCurrentWorkArea(WorkArea * work_area)
896 BOOST_ASSERT(work_area);
898 // Changing work area can result from opening a file so
899 // update the toc in any case.
902 GuiWorkArea * wa = dynamic_cast<GuiWorkArea *>(work_area);
904 if (wa != d.tab_widget_->currentWidget())
905 // Switch to the work area.
906 d.tab_widget_->setCurrentWidget(wa);
908 // Make sure the work area is up to date.
909 currentTabChanged(d.tab_widget_->currentIndex());
914 void GuiView::removeWorkArea(WorkArea * work_area)
916 BOOST_ASSERT(work_area);
917 if (work_area == currentWorkArea()) {
919 disconnectBufferView();
922 // removing a work area often results from closing a file so
923 // update the toc in any case.
926 GuiWorkArea * gwa = dynamic_cast<GuiWorkArea *>(work_area);
928 int index = d.tab_widget_->indexOf(gwa);
929 d.tab_widget_->removeTab(index);
933 if (d.tab_widget_->count()) {
934 // make sure the next work area is enabled.
935 d.tab_widget_->currentWidget()->setUpdatesEnabled(true);
936 // Hide tabbar if there's only one tab.
937 d.tab_widget_->showBar(d.tab_widget_->count() > 1);
941 getDialogs().hideBufferDependent();
942 if (d.stack_widget_) {
943 // No more work area, switch to the background widget.
949 } // namespace frontend
952 #include "GuiView_moc.cpp"