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>
67 #include <boost/bind.hpp>
68 #include <boost/shared_ptr.hpp>
69 #include <boost/current_function.hpp>
77 using support::FileName;
78 using support::libFileSearch;
79 using support::makeDisplayPath;
85 int const statusbar_timer_value = 3000;
87 class BackgroundWidget: public QWidget
90 BackgroundWidget(QString const & file, QString const & text)
92 splash_ = new QPixmap(file);
94 lyxerr << "could not load splash screen: '" << fromqstr(file) << "'" << endl;
98 QPainter pain(splash_);
99 pain.setPen(QColor(255, 255, 0));
101 // The font used to display the version info
102 font.setStyleHint(QFont::SansSerif);
103 font.setWeight(QFont::Bold);
104 font.setPointSize(convert<int>(lyxrc.font_sizes[Font::SIZE_LARGE]));
106 pain.drawText(260, 270, text);
109 void paintEvent(QPaintEvent * ev)
114 int x = (width() - splash_->width()) / 2;
115 int y = (height() - splash_->height()) / 2;
117 pain.drawPixmap(x, y, *splash_);
125 class TabWidget: public QTabWidget {
127 void showBar(bool show) { tabBar()->setVisible(show); }
134 struct GuiView::GuiViewPrivate
141 TabWidget * tab_widget_;
142 QStackedWidget * stack_widget_;
143 BackgroundWidget * bg_widget_;
145 QLMenubar * menubar_;
147 GuiViewPrivate() : posx_offset(0), posy_offset(0)
150 unsigned int smallIconSize;
151 unsigned int normalIconSize;
152 unsigned int bigIconSize;
153 // static needed by "New Window"
154 static unsigned int lastIconSize;
156 QMenu* toolBarPopup(GuiView *parent)
158 // FIXME: translation
159 QMenu* menu = new QMenu(parent);
160 QActionGroup *iconSizeGroup = new QActionGroup(parent);
162 QAction *smallIcons = new QAction(iconSizeGroup);
163 smallIcons->setText(qt_("Small-sized icons"));
164 smallIcons->setCheckable(true);
165 QObject::connect(smallIcons, SIGNAL(triggered()), parent, SLOT(smallSizedIcons()));
166 menu->addAction(smallIcons);
168 QAction *normalIcons = new QAction(iconSizeGroup);
169 normalIcons->setText(qt_("Normal-sized icons"));
170 normalIcons->setCheckable(true);
171 QObject::connect(normalIcons, SIGNAL(triggered()), parent, SLOT(normalSizedIcons()));
172 menu->addAction(normalIcons);
174 QAction *bigIcons = new QAction(iconSizeGroup);
175 bigIcons->setText(qt_("Big-sized icons"));
176 bigIcons->setCheckable(true);
177 QObject::connect(bigIcons, SIGNAL(triggered()), parent, SLOT(bigSizedIcons()));
178 menu->addAction(bigIcons);
180 unsigned int cur = parent->iconSize().width();
181 if ( cur == parent->d.smallIconSize)
182 smallIcons->setChecked(true);
183 else if (cur == parent->d.normalIconSize)
184 normalIcons->setChecked(true);
185 else if (cur == parent->d.bigIconSize)
186 bigIcons->setChecked(true);
191 void initBackground()
194 LYXERR(Debug::GUI) << "show banner: " << lyxrc.show_banner << endl;
195 /// The text to be written on top of the pixmap
196 QString const text = lyx_version ? QString(lyx_version) : qt_("unknown version");
197 FileName const file = support::libFileSearch("images", "banner", "png");
201 bg_widget_ = new BackgroundWidget(toqstr(file.absFilename()), text);
209 stack_widget_->setCurrentWidget(bg_widget_);
210 bg_widget_->setUpdatesEnabled(true);
215 unsigned int GuiView::GuiViewPrivate::lastIconSize = 0;
218 GuiView::GuiView(int id)
219 : QMainWindow(), LyXView(id), commandbuffer_(0), quitting_by_menu_(false),
220 d(*new GuiViewPrivate)
222 // Qt bug? signal lastWindowClosed does not work
223 setAttribute(Qt::WA_QuitOnClose, false);
224 setAttribute(Qt::WA_DeleteOnClose, true);
226 // hardcode here the platform specific icon size
227 d.smallIconSize = 14; // scaling problems
228 d.normalIconSize = 20; // ok, default
229 d.bigIconSize = 26; // better for some math icons
232 // assign an icon to main form. We do not do it under Qt/Mac,
233 // since the icon is provided in the application bundle.
234 FileName const iconname = libFileSearch("images", "lyx", "xpm");
235 if (!iconname.empty())
236 setWindowIcon(QPixmap(toqstr(iconname.absFilename())));
239 d.tab_widget_ = new TabWidget;
241 QPushButton * closeTabButton = new QPushButton(this);
242 FileName const file = support::libFileSearch("images", "closetab", "xpm");
244 QPixmap pm(toqstr(file.absFilename()));
245 closeTabButton->setIcon(QIcon(pm));
246 closeTabButton->setMaximumSize(pm.size());
247 closeTabButton->setFlat(true);
249 closeTabButton->setText("Close");
251 closeTabButton->setCursor(Qt::ArrowCursor);
252 closeTabButton->setToolTip(tr("Close tab"));
253 closeTabButton->setEnabled(true);
255 QObject::connect(d.tab_widget_, SIGNAL(currentChanged(int)),
256 this, SLOT(currentTabChanged(int)));
257 QObject::connect(closeTabButton, SIGNAL(clicked()),
258 this, SLOT(closeCurrentTab()));
260 d.tab_widget_->setCornerWidget(closeTabButton);
261 #if QT_VERSION >= 0x040200
262 d.tab_widget_->setUsesScrollButtons(true);
267 lyxerr << "stack widget!" << endl;
268 d.stack_widget_ = new QStackedWidget;
269 d.stack_widget_->addWidget(d.bg_widget_);
270 d.stack_widget_->addWidget(d.tab_widget_);
271 setCentralWidget(d.stack_widget_);
274 setCentralWidget(d.tab_widget_);
278 setAcceptDrops(true);
289 void GuiView::close()
291 quitting_by_menu_ = true;
292 QMainWindow::close();
293 quitting_by_menu_ = false;
297 void GuiView::setFocus()
299 if (d.tab_widget_->count())
300 d.tab_widget_->currentWidget()->setFocus();
304 QMenu* GuiView::createPopupMenu()
306 return d.toolBarPopup(this);
312 d.menubar_ = new QLMenubar(this, menubackend);
316 statusBar()->setSizeGripEnabled(true);
318 QObject::connect(&statusbar_timer_, SIGNAL(timeout()),
319 this, SLOT(update_view_state_qt()));
322 d.stack_widget_->setCurrentWidget(d.bg_widget_);
326 void GuiView::closeEvent(QCloseEvent * close_event)
328 // we may have been called through the close window button
329 // which bypasses the LFUN machinery.
330 if (!quitting_by_menu_ && theApp()->gui().viewIds().size() == 1) {
331 if (!theBufferList().quitWriteAll()) {
332 close_event->ignore();
337 // Make sure that no LFUN use this close to be closed View.
338 theLyXFunc().setLyXView(0);
339 // Make sure the timer time out will not trigger a statusbar update.
340 statusbar_timer_.stop();
342 theApp()->gui().unregisterView(id());
343 if (!theApp()->gui().viewIds().empty()) {
344 // Just close the window and do nothing else if this is not the
346 close_event->accept();
352 // this is the place where we leave the frontend.
353 // it is the only point at which we start quitting.
355 close_event->accept();
356 // quit the event loop
361 void GuiView::dragEnterEvent(QDragEnterEvent * event)
363 if (event->mimeData()->hasUrls())
365 /// \todo Ask lyx-devel is this is enough:
366 /// if (event->mimeData()->hasFormat("text/plain"))
367 /// event->acceptProposedAction();
371 void GuiView::dropEvent(QDropEvent* event)
373 QList<QUrl> files = event->mimeData()->urls();
377 LYXERR(Debug::GUI) << BOOST_CURRENT_FUNCTION
378 << " got URLs!" << endl;
379 for (int i = 0; i!=files.size(); ++i) {
380 string const file = support::os::internal_path(fromqstr(
381 files.at(i).toLocalFile()));
383 dispatch(FuncRequest(LFUN_FILE_OPEN, file));
388 void GuiView::saveGeometry()
390 static bool done = false;
397 // change the ifdef to 'geometry = normalGeometry();' only
398 // when Trolltech has fixed the broken normalGeometry on X11:
399 // http://www.trolltech.com/developer/task-tracker/index_html?id=119684+&method=entry
400 // Then also the moveEvent, resizeEvent, and the
401 // code for floatingGeometry_ can be removed;
402 // adjust GuiView::setGeometry()
404 QRect normal_geometry;
407 normal_geometry = normalGeometry();
409 maximized = CompletelyMaximized;
411 maximized = NotMaximized;
414 normal_geometry = updateFloatingGeometry();
416 QDesktopWidget& dw = *qApp->desktop();
417 QRect desk = dw.availableGeometry(dw.primaryScreen());
418 // Qt bug on Linux: load completely maximized, vert max. save-> frameGeometry().height() is wrong
419 if (isMaximized() && desk.width() <= frameGeometry().width() && desk.height() <= frameGeometry().height()) {
420 maximized = CompletelyMaximized;
421 // maximizing does not work when the window is allready hor. or vert. maximized
422 // Tested only on KDE
423 int dh = frameGeometry().height() - height();
424 if (desk.height() <= normal_geometry.height() + dh)
425 normal_geometry.setHeight(normal_geometry.height() - 1);
426 int dw = frameGeometry().width() - width();
427 if (desk.width() <= normal_geometry.width() + dw)
428 normal_geometry.setWidth(normal_geometry.width() - 1);
429 } else if (desk.height() <= frameGeometry().height()) {
430 maximized = VerticallyMaximized;
431 } else if (desk.width() <= frameGeometry().width()) {
432 maximized = HorizontallyMaximized;
434 maximized = NotMaximized;
439 // save windows size and position
440 Session & session = LyX::ref().session();
441 session.sessionInfo().save("WindowWidth", convert<string>(normal_geometry.width()));
442 session.sessionInfo().save("WindowHeight", convert<string>(normal_geometry.height()));
443 session.sessionInfo().save("WindowMaximized", convert<string>(maximized));
444 session.sessionInfo().save("IconSizeXY", convert<string>(iconSize().width()));
445 if (lyxrc.geometry_xysaved) {
446 session.sessionInfo().save("WindowPosX", convert<string>(normal_geometry.x() + d.posx_offset));
447 session.sessionInfo().save("WindowPosY", convert<string>(normal_geometry.y() + d.posy_offset));
449 toolbars_->saveToolbarInfo();
453 void GuiView::setGeometry(unsigned int width,
457 unsigned int iconSizeXY,
458 const string & geometryArg)
460 // use last value (not at startup)
461 if (d.lastIconSize != 0)
462 setIconSize(d.lastIconSize);
463 else if (iconSizeXY != 0)
464 setIconSize(iconSizeXY);
466 setIconSize(d.normalIconSize);
468 // only true when the -geometry option was NOT used
469 if (width != 0 && height != 0) {
470 if (posx != -1 && posy != -1) {
471 // if there are startup positioning problems:
472 // http://doc.trolltech.com/4.2/qdesktopwidget.html
473 QDesktopWidget& dw = *qApp->desktop();
474 if (dw.isVirtualDesktop()) {
475 if(!dw.geometry().contains(posx, posy)) {
480 // Which system doesn't use a virtual desktop?
481 // TODO save also last screen number and check if it is still availabe.
484 // FIXME: use setGeometry only when Trolltech has fixed the qt4/X11 bug
485 QWidget::setGeometry(posx, posy, width, height);
487 resize(width, height);
491 resize(width, height);
494 // remember original size
495 floatingGeometry_ = QRect(posx, posy, width, height);
497 if (maximized != NotMaximized) {
498 if (maximized == CompletelyMaximized) {
499 setWindowState(Qt::WindowMaximized);
502 // TODO How to set by the window manager?
503 // setWindowState(Qt::WindowVerticallyMaximized);
505 QDesktopWidget& dw = *qApp->desktop();
506 QRect desk = dw.availableGeometry(dw.primaryScreen());
507 if (maximized == VerticallyMaximized)
508 resize(width, desk.height());
509 if (maximized == HorizontallyMaximized)
510 resize(desk.width(), height);
517 // FIXME: move this code into parse_geometry() (LyX.cpp)
521 QRegExp re( "[=]*(?:([0-9]+)[xX]([0-9]+)){0,1}[ ]*(?:([+-][0-9]*)([+-][0-9]*)){0,1}" );
522 re.indexIn(toqstr(geometryArg.c_str()));
523 w = re.cap(1).toInt();
524 h = re.cap(2).toInt();
525 x = re.cap(3).toInt();
526 y = re.cap(4).toInt();
527 QWidget::setGeometry( x, y, w, h );
538 // For an unknown reason, the Window title update is not effective for
539 // the second windows up until it is shown on screen (Qt bug?).
542 // after show geometry() has changed (Qt bug?)
543 // we compensate the drift when storing the position
546 if (width != 0 && height != 0)
547 if (posx != -1 && posy != -1) {
549 d.posx_offset = posx - normalGeometry().x();
550 d.posy_offset = posy - normalGeometry().y();
553 if (maximized == NotMaximized) {
554 d.posx_offset = posx - geometry().x();
555 d.posy_offset = posy - geometry().y();
563 void GuiView::setWindowTitle(docstring const & t, docstring const & it)
565 QString title = windowTitle();
566 QString new_title = toqstr(t);
567 if (title != new_title) {
568 QMainWindow::setWindowTitle(new_title);
569 QMainWindow::setWindowIconText(toqstr(it));
574 void GuiView::addCommandBuffer(QToolBar * toolbar)
576 commandbuffer_ = new QCommandBuffer(this);
577 toolbar->addWidget(commandbuffer_);
581 void GuiView::message(docstring const & str)
583 statusBar()->showMessage(toqstr(str));
584 statusbar_timer_.stop();
585 statusbar_timer_.start(statusbar_timer_value);
589 void GuiView::clearMessage()
591 update_view_state_qt();
595 void GuiView::setIconSize(unsigned int size)
597 d.lastIconSize = size;
598 QMainWindow::setIconSize(QSize(size, size));
602 void GuiView::smallSizedIcons()
604 setIconSize(d.smallIconSize);
608 void GuiView::normalSizedIcons()
610 setIconSize(d.normalIconSize);
614 void GuiView::bigSizedIcons()
616 setIconSize(d.bigIconSize);
620 void GuiView::update_view_state_qt()
624 theLyXFunc().setLyXView(this);
625 statusBar()->showMessage(toqstr(theLyXFunc().viewStatusMessage()));
626 statusbar_timer_.stop();
630 void GuiView::closeCurrentTab()
632 dispatch(FuncRequest(LFUN_BUFFER_CLOSE));
636 void GuiView::currentTabChanged(int i)
639 disconnectBufferView();
640 GuiWorkArea * wa = dynamic_cast<GuiWorkArea *>(d.tab_widget_->widget(i));
642 BufferView & bv = wa->bufferView();
643 connectBufferView(bv);
644 connectBuffer(bv.buffer());
645 bv.updateMetrics(false);
646 bv.cursor().fixIfBroken();
647 wa->setUpdatesEnabled(true);
652 // Buffer-dependent dialogs should be updated or
653 // hidden. This should go here because some dialogs (eg ToC)
654 // require bv_->text.
655 getDialogs().updateBufferDependent(true);
657 updateLayoutChoice();
661 lyxerr << "currentTabChanged " << i
662 << "File" << bv.buffer().fileName() << endl;
666 void GuiView::updateStatusBar()
668 // let the user see the explicit message
669 if (statusbar_timer_.isActive())
672 statusBar()->showMessage(toqstr(theLyXFunc().viewStatusMessage()));
676 void GuiView::activated(FuncRequest const & func)
682 bool GuiView::hasFocus() const
684 return qApp->activeWindow() == this;
688 QRect GuiView::updateFloatingGeometry()
690 QDesktopWidget& dw = *qApp->desktop();
691 QRect desk = dw.availableGeometry(dw.primaryScreen());
692 // remember only non-maximized sizes
693 if (!isMaximized() && desk.width() > frameGeometry().width() && desk.height() > frameGeometry().height()) {
694 floatingGeometry_ = QRect(x(), y(), width(), height());
696 return floatingGeometry_;
700 void GuiView::resizeEvent(QResizeEvent *)
702 updateFloatingGeometry();
706 void GuiView::moveEvent(QMoveEvent *)
708 updateFloatingGeometry();
712 bool GuiView::event(QEvent * e)
716 // Useful debug code:
717 //case QEvent::WindowActivate:
718 //case QEvent::ActivationChange:
719 //case QEvent::WindowDeactivate:
720 //case QEvent::Paint:
721 //case QEvent::Enter:
722 //case QEvent::Leave:
723 //case QEvent::HoverEnter:
724 //case QEvent::HoverLeave:
725 //case QEvent::HoverMove:
726 //case QEvent::StatusTip:
727 //case QEvent::DragEnter:
728 //case QEvent::DragLeave:
732 case QEvent::ShortcutOverride: {
733 QKeyEvent * ke = static_cast<QKeyEvent*>(e);
734 if (d.tab_widget_->count() == 0) {
735 theLyXFunc().setLyXView(this);
736 boost::shared_ptr<QKeySymbol> sym(new QKeySymbol);
738 theLyXFunc().processKeySym(sym, q_key_state(ke->modifiers()));
742 if (ke->key() == Qt::Key_Tab || ke->key() == Qt::Key_Backtab) {
743 boost::shared_ptr<QKeySymbol> sym(new QKeySymbol);
745 currentWorkArea()->processKeySym(sym, key_modifier::none);
751 return QMainWindow::event(e);
756 bool GuiView::focusNextPrevChild(bool /*next*/)
765 QMainWindow::setWindowTitle(qt_("LyX"));
767 updateFloatingGeometry();
771 void GuiView::busy(bool yes)
773 if (d.tab_widget_->count()) {
774 GuiWorkArea * wa = dynamic_cast<GuiWorkArea *>(d.tab_widget_->currentWidget());
776 wa->setUpdatesEnabled(!yes);
778 wa->stopBlinkingCursor();
780 wa->startBlinkingCursor();
784 QApplication::setOverrideCursor(Qt::WaitCursor);
786 QApplication::restoreOverrideCursor();
790 Toolbars::ToolbarPtr GuiView::makeToolbar(ToolbarInfo const & tbinfo, bool newline)
792 QLToolbar * Tb = new QLToolbar(tbinfo, *this);
794 if (tbinfo.flags & ToolbarInfo::TOP) {
796 addToolBarBreak(Qt::TopToolBarArea);
797 addToolBar(Qt::TopToolBarArea, Tb);
800 if (tbinfo.flags & ToolbarInfo::BOTTOM) {
801 // Qt < 4.2.2 cannot handle ToolBarBreak on non-TOP dock.
802 #if (QT_VERSION >= 0x040202)
804 addToolBarBreak(Qt::BottomToolBarArea);
806 addToolBar(Qt::BottomToolBarArea, Tb);
809 if (tbinfo.flags & ToolbarInfo::LEFT) {
810 // Qt < 4.2.2 cannot handle ToolBarBreak on non-TOP dock.
811 #if (QT_VERSION >= 0x040202)
813 addToolBarBreak(Qt::LeftToolBarArea);
815 addToolBar(Qt::LeftToolBarArea, Tb);
818 if (tbinfo.flags & ToolbarInfo::RIGHT) {
819 // Qt < 4.2.2 cannot handle ToolBarBreak on non-TOP dock.
820 #if (QT_VERSION >= 0x040202)
822 addToolBarBreak(Qt::RightToolBarArea);
824 addToolBar(Qt::RightToolBarArea, Tb);
827 // The following does not work so I cannot restore to exact toolbar location
829 ToolbarSection::ToolbarInfo & tbinfo = LyX::ref().session().toolbars().load(tbinfo.name);
830 Tb->move(tbinfo.posx, tbinfo.posy);
833 return Toolbars::ToolbarPtr(Tb);
837 WorkArea * GuiView::workArea(Buffer & buffer)
839 for (int i = 0; i != d.tab_widget_->count(); ++i) {
840 GuiWorkArea * wa = dynamic_cast<GuiWorkArea *>(d.tab_widget_->widget(i));
842 if (&wa->bufferView().buffer() == &buffer)
849 WorkArea * GuiView::addWorkArea(Buffer & buffer)
851 GuiWorkArea * wa = new GuiWorkArea(buffer, *this);
852 d.tab_widget_->addTab(wa, toqstr(makeDisplayPath(buffer.fileName(), 30)));
853 wa->bufferView().updateMetrics(false);
855 d.stack_widget_->setCurrentWidget(d.tab_widget_);
856 // Hide tabbar if there's only one tab.
857 d.tab_widget_->showBar(d.tab_widget_->count() > 1);
862 WorkArea * GuiView::currentWorkArea()
864 if (d.tab_widget_->count() == 0)
866 BOOST_ASSERT(dynamic_cast<GuiWorkArea *>(d.tab_widget_->currentWidget()));
867 return dynamic_cast<GuiWorkArea *>(d.tab_widget_->currentWidget());
871 WorkArea const * GuiView::currentWorkArea() const
873 if (d.tab_widget_->count() == 0)
875 BOOST_ASSERT(dynamic_cast<GuiWorkArea const *>(d.tab_widget_->currentWidget()));
876 return dynamic_cast<GuiWorkArea const *>(d.tab_widget_->currentWidget());
880 void GuiView::setCurrentWorkArea(WorkArea * work_area)
882 BOOST_ASSERT(work_area);
884 // Changing work area can result from opening a file so
885 // update the toc in any case.
888 GuiWorkArea * wa = dynamic_cast<GuiWorkArea *>(work_area);
890 if (wa != d.tab_widget_->currentWidget())
891 // Switch to the work area.
892 d.tab_widget_->setCurrentWidget(wa);
894 // Make sure the work area is up to date.
895 currentTabChanged(d.tab_widget_->currentIndex());
900 void GuiView::removeWorkArea(WorkArea * work_area)
902 BOOST_ASSERT(work_area);
903 if (work_area == currentWorkArea()) {
905 disconnectBufferView();
908 // removing a work area often results from closing a file so
909 // update the toc in any case.
912 GuiWorkArea * gwa = dynamic_cast<GuiWorkArea *>(work_area);
914 int index = d.tab_widget_->indexOf(gwa);
915 d.tab_widget_->removeTab(index);
919 if (d.tab_widget_->count()) {
920 // make sure the next work area is enabled.
921 d.tab_widget_->currentWidget()->setUpdatesEnabled(true);
922 // Hide tabbar if there's only one tab.
923 d.tab_widget_->showBar(d.tab_widget_->count() > 1);
927 getDialogs().hideBufferDependent();
928 if (d.stack_widget_) {
929 // No more work area, switch to the background widget.
935 void GuiView::showMiniBuffer(bool visible)
940 toolbars_->display("minibuffer", visible);
941 commandbuffer_->focus_command();
945 void GuiView::openMenu(docstring const & name)
947 d.menubar_->openByName(name);
950 } // namespace frontend
953 #include "GuiView_moc.cpp"