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/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"
48 #include <QApplication>
49 #include <QCloseEvent>
50 #include <QDesktopWidget>
51 #include <QDragEnterEvent>
53 #include <QHBoxLayout>
57 #include <QPushButton>
62 #include <QVBoxLayout>
65 #include <boost/bind.hpp>
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 TabWidget : public QWidget
89 QPushButton* closeTabButton;
91 void hideTabsIfNecessary()
93 if (tabbar->count() > 1) {
95 closeTabButton->show();
98 closeTabButton->hide();
102 TabWidget(QWidget* w, bool topTabBar)
104 closeTabButton = new QPushButton(this);
105 FileName const file = support::libFileSearch("images", "closetab", "xpm");
107 QPixmap pm(toqstr(file.absFilename()));
108 closeTabButton->setIcon(QIcon(pm));
109 closeTabButton->setMaximumSize(pm.size());
110 closeTabButton->setFlat(true);
112 closeTabButton->setText("Close");
114 closeTabButton->setCursor(Qt::ArrowCursor);
115 closeTabButton->setToolTip(tr("Close tab"));
116 closeTabButton->setEnabled(true);
118 tabbar = new QTabBar;
119 #if QT_VERSION >= 0x040200
120 tabbar->setUsesScrollButtons(true);
122 hlayout = new QHBoxLayout;
123 QVBoxLayout* vlayout = new QVBoxLayout;
124 hlayout->addWidget(tabbar);
125 hlayout->addWidget(closeTabButton);
127 vlayout->addLayout(hlayout);
128 vlayout->addWidget(w);
130 tabbar->setShape(QTabBar::RoundedSouth);
131 vlayout->addWidget(w);
132 vlayout->addLayout(hlayout);
134 vlayout->setMargin(0);
135 vlayout->setSpacing(0);
136 hlayout->setMargin(0);
138 hideTabsIfNecessary();
143 for (int i = tabbar->count() - 1; i >= 0; --i)
144 tabbar->removeTab(i);
151 struct GuiView::GuiViewPrivate
153 vector<string> tabnames;
156 TabWidget* tabWidget;
161 GuiViewPrivate() : tabWidget(0), posx_offset(0), posy_offset(0)
164 unsigned int smallIconSize;
165 unsigned int normalIconSize;
166 unsigned int bigIconSize;
167 // static needed by "New Window"
168 static unsigned int lastIconSize;
170 QMenu* toolBarPopup(GuiView *parent)
172 // FIXME: translation
173 QMenu* menu = new QMenu(parent);
174 QActionGroup *iconSizeGroup = new QActionGroup(parent);
176 QAction *smallIcons = new QAction(iconSizeGroup);
177 smallIcons->setText(qt_("Small-sized icons"));
178 smallIcons->setCheckable(true);
179 QObject::connect(smallIcons, SIGNAL(triggered()), parent, SLOT(smallSizedIcons()));
180 menu->addAction(smallIcons);
182 QAction *normalIcons = new QAction(iconSizeGroup);
183 normalIcons->setText(qt_("Normal-sized icons"));
184 normalIcons->setCheckable(true);
185 QObject::connect(normalIcons, SIGNAL(triggered()), parent, SLOT(normalSizedIcons()));
186 menu->addAction(normalIcons);
188 QAction *bigIcons = new QAction(iconSizeGroup);
189 bigIcons->setText(qt_("Big-sized icons"));
190 bigIcons->setCheckable(true);
191 QObject::connect(bigIcons, SIGNAL(triggered()), parent, SLOT(bigSizedIcons()));
192 menu->addAction(bigIcons);
194 unsigned int cur = parent->iconSize().width();
195 if ( cur == parent->d.smallIconSize)
196 smallIcons->setChecked(true);
197 else if (cur == parent->d.normalIconSize)
198 normalIcons->setChecked(true);
199 else if (cur == parent->d.bigIconSize)
200 bigIcons->setChecked(true);
207 unsigned int GuiView::GuiViewPrivate::lastIconSize = 0;
210 GuiView::GuiView(int id)
211 : QMainWindow(), LyXView(id), commandbuffer_(0), quitting_by_menu_(false),
212 d(*new GuiViewPrivate)
214 // Qt bug? signal lastWindowClosed does not work
215 setAttribute(Qt::WA_QuitOnClose, false);
216 setAttribute(Qt::WA_DeleteOnClose, true);
218 // hardcode here the platform specific icon size
219 d.smallIconSize = 14; // scaling problems
220 d.normalIconSize = 20; // ok, default
221 d.bigIconSize = 26; // better for some math icons
224 // assign an icon to main form. We do not do it under Qt/Mac,
225 // since the icon is provided in the application bundle.
226 FileName const iconname = libFileSearch("images", "lyx", "xpm");
227 if (!iconname.empty())
228 setWindowIcon(QPixmap(toqstr(iconname.absFilename())));
232 setAcceptDrops(true);
243 void GuiView::close()
245 quitting_by_menu_ = true;
246 QMainWindow::close();
247 quitting_by_menu_ = false;
251 void GuiView::setFocus()
253 BOOST_ASSERT(work_area_);
254 static_cast<GuiWorkArea *>(work_area_)->setFocus();
258 QMenu* GuiView::createPopupMenu()
260 return d.toolBarPopup(this);
266 menubar_.reset(new QLMenubar(this, menubackend));
267 QObject::connect(menuBar(), SIGNAL(triggered(QAction *)),
268 this, SLOT(updateMenu(QAction *)));
270 getToolbars().init();
272 statusBar()->setSizeGripEnabled(true);
274 QObject::connect(&statusbar_timer_, SIGNAL(timeout()),
275 this, SLOT(update_view_state_qt()));
277 BOOST_ASSERT(work_area_);
278 if (!work_area_->bufferView().buffer() && !theBufferList().empty())
279 setBuffer(theBufferList().first());
281 // make sure the buttons are disabled if needed
283 updateLayoutChoice();
288 void GuiView::closeEvent(QCloseEvent * close_event)
290 // we may have been called through the close window button
291 // which bypasses the LFUN machinery.
292 if (!quitting_by_menu_ && theApp()->gui().viewIds().size() == 1) {
293 if (!theBufferList().quitWriteAll()) {
294 close_event->ignore();
299 // Make sure that no LFUN use this close to be closed View.
300 theLyXFunc().setLyXView(0);
301 // Make sure the timer time out will not trigger a statusbar update.
302 statusbar_timer_.stop();
304 theApp()->gui().unregisterView(id());
305 if (!theApp()->gui().viewIds().empty()) {
306 // Just close the window and do nothing else if this is not the
308 close_event->accept();
314 if (view()->buffer()) {
315 // save cursor position for opened files to .lyx/session
316 // only bottom (whole doc) level pit and pos is saved.
317 LyX::ref().session().lastFilePos().save(
318 FileName(buffer()->fileName()),
319 boost::tie(view()->cursor().bottom().pit(),
320 view()->cursor().bottom().pos()));
323 // this is the place where we leave the frontend.
324 // it is the only point at which we start quitting.
326 close_event->accept();
327 // quit the event loop
332 void GuiView::dragEnterEvent(QDragEnterEvent * event)
334 if (event->mimeData()->hasUrls())
336 /// \todo Ask lyx-devel is this is enough:
337 /// if (event->mimeData()->hasFormat("text/plain"))
338 /// event->acceptProposedAction();
342 void GuiView::dropEvent(QDropEvent* event)
344 QList<QUrl> files = event->mimeData()->urls();
348 LYXERR(Debug::GUI) << BOOST_CURRENT_FUNCTION
349 << " got URLs!" << endl;
350 for (int i = 0; i!=files.size(); ++i) {
351 string const file = support::os::internal_path(fromqstr(
352 files.at(i).toLocalFile()));
354 dispatch(FuncRequest(LFUN_FILE_OPEN, file));
359 void GuiView::saveGeometry()
361 static bool done = false;
368 // change the ifdef to 'geometry = normalGeometry();' only
369 // when Trolltech has fixed the broken normalGeometry on X11:
370 // http://www.trolltech.com/developer/task-tracker/index_html?id=119684+&method=entry
371 // Then also the moveEvent, resizeEvent, and the
372 // code for floatingGeometry_ can be removed;
373 // adjust GuiView::setGeometry()
375 QRect normal_geometry;
378 normal_geometry = normalGeometry();
380 maximized = CompletelyMaximized;
382 maximized = NotMaximized;
385 normal_geometry = updateFloatingGeometry();
387 QDesktopWidget& dw = *qApp->desktop();
388 QRect desk = dw.availableGeometry(dw.primaryScreen());
389 // Qt bug on Linux: load completely maximized, vert max. save-> frameGeometry().height() is wrong
390 if (isMaximized() && desk.width() <= frameGeometry().width() && desk.height() <= frameGeometry().height()) {
391 maximized = CompletelyMaximized;
392 // maximizing does not work when the window is allready hor. or vert. maximized
393 // Tested only on KDE
394 int dh = frameGeometry().height() - height();
395 if (desk.height() <= normal_geometry.height() + dh)
396 normal_geometry.setHeight(normal_geometry.height() - 1);
397 int dw = frameGeometry().width() - width();
398 if (desk.width() <= normal_geometry.width() + dw)
399 normal_geometry.setWidth(normal_geometry.width() - 1);
400 } else if (desk.height() <= frameGeometry().height()) {
401 maximized = VerticallyMaximized;
402 } else if (desk.width() <= frameGeometry().width()) {
403 maximized = HorizontallyMaximized;
405 maximized = NotMaximized;
410 // save windows size and position
411 Session & session = LyX::ref().session();
412 session.sessionInfo().save("WindowWidth", convert<string>(normal_geometry.width()));
413 session.sessionInfo().save("WindowHeight", convert<string>(normal_geometry.height()));
414 session.sessionInfo().save("WindowMaximized", convert<string>(maximized));
415 session.sessionInfo().save("IconSizeXY", convert<string>(iconSize().width()));
416 if (lyxrc.geometry_xysaved) {
417 session.sessionInfo().save("WindowPosX", convert<string>(normal_geometry.x() + d.posx_offset));
418 session.sessionInfo().save("WindowPosY", convert<string>(normal_geometry.y() + d.posy_offset));
420 getToolbars().saveToolbarInfo();
424 void GuiView::setGeometry(unsigned int width,
428 unsigned int iconSizeXY,
429 const string & geometryArg)
431 // use last value (not at startup)
432 if (d.lastIconSize != 0)
433 setIconSize(d.lastIconSize);
434 else if (iconSizeXY != 0)
435 setIconSize(iconSizeXY);
437 setIconSize(d.normalIconSize);
439 // only true when the -geometry option was NOT used
440 if (width != 0 && height != 0) {
441 if (posx != -1 && posy != -1) {
442 // if there are startup positioning problems:
443 // http://doc.trolltech.com/4.2/qdesktopwidget.html
444 QDesktopWidget& dw = *qApp->desktop();
445 if (dw.isVirtualDesktop()) {
446 if(!dw.geometry().contains(posx, posy)) {
451 // Which system doesn't use a virtual desktop?
452 // TODO save also last screen number and check if it is still availabe.
455 // FIXME: use setGeometry only when Trolltech has fixed the qt4/X11 bug
456 QWidget::setGeometry(posx, posy, width, height);
458 resize(width, height);
462 resize(width, height);
465 // remember original size
466 floatingGeometry_ = QRect(posx, posy, width, height);
468 if (maximized != NotMaximized) {
469 if (maximized == CompletelyMaximized) {
470 setWindowState(Qt::WindowMaximized);
473 // TODO How to set by the window manager?
474 // setWindowState(Qt::WindowVerticallyMaximized);
476 QDesktopWidget& dw = *qApp->desktop();
477 QRect desk = dw.availableGeometry(dw.primaryScreen());
478 if (maximized == VerticallyMaximized)
479 resize(width, desk.height());
480 if (maximized == HorizontallyMaximized)
481 resize(desk.width(), height);
488 // FIXME: move this code into parse_geometry() (LyX.cpp)
492 QRegExp re( "[=]*(?:([0-9]+)[xX]([0-9]+)){0,1}[ ]*(?:([+-][0-9]*)([+-][0-9]*)){0,1}" );
493 re.indexIn(toqstr(geometryArg.c_str()));
494 w = re.cap(1).toInt();
495 h = re.cap(2).toInt();
496 x = re.cap(3).toInt();
497 y = re.cap(4).toInt();
498 QWidget::setGeometry( x, y, w, h );
507 // For an unknown reason, the Window title update is not effective for
508 // the second windows up until it is shown on screen (Qt bug?).
511 // after show geometry() has changed (Qt bug?)
512 // we compensate the drift when storing the position
515 if (width != 0 && height != 0)
516 if (posx != -1 && posy != -1) {
518 d.posx_offset = posx - normalGeometry().x();
519 d.posy_offset = posy - normalGeometry().y();
522 if (maximized == NotMaximized) {
523 d.posx_offset = posx - geometry().x();
524 d.posy_offset = posy - geometry().y();
532 void GuiView::updateMenu(QAction * /*action*/)
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));
549 void GuiView::addCommandBuffer(QToolBar * toolbar)
551 commandbuffer_ = new QCommandBuffer(this, *controlcommand_);
552 focus_command_buffer.connect(boost::bind(&GuiView::focus_command_widget, this));
553 toolbar->addWidget(commandbuffer_);
557 void GuiView::message(docstring const & str)
559 statusBar()->showMessage(toqstr(str));
560 statusbar_timer_.stop();
561 statusbar_timer_.start(statusbar_timer_value);
565 void GuiView::clearMessage()
567 update_view_state_qt();
571 void GuiView::setIconSize(unsigned int size)
573 d.lastIconSize = size;
574 QMainWindow::setIconSize(QSize(size, size));
578 void GuiView::smallSizedIcons()
580 setIconSize(d.smallIconSize);
584 void GuiView::normalSizedIcons()
586 setIconSize(d.normalIconSize);
590 void GuiView::bigSizedIcons()
592 setIconSize(d.bigIconSize);
596 void GuiView::focus_command_widget()
599 commandbuffer_->focus_command();
603 void GuiView::update_view_state_qt()
607 theLyXFunc().setLyXView(this);
608 statusBar()->showMessage(toqstr(theLyXFunc().viewStatusMessage()));
609 statusbar_timer_.stop();
613 void GuiView::initTab(QWidget* workarea)
615 // construct the TabWidget with 'false' to have the tabbar at the bottom
616 d.tabWidget = new TabWidget(workarea, true);
617 setCentralWidget(d.tabWidget);
618 QObject::connect(d.tabWidget->tabbar, SIGNAL(currentChanged(int)),
619 this, SLOT(currentTabChanged(int)));
620 QObject::connect(d.tabWidget->closeTabButton, SIGNAL(clicked()),
621 this, SLOT(closeCurrentTab()));
625 void GuiView::updateTab()
627 std::vector<string> const & names = theBufferList().getFileNames();
630 if (view()->buffer()) {
631 cur_title = view()->buffer()->fileName();
634 // avoid unnecessary tabbar rebuild:
635 // check if something has changed
636 if (d.tabnames == names && d.cur_title == cur_title)
639 d.cur_title = cur_title;
641 QTabBar & tabbar = *d.tabWidget->tabbar;
643 // update when all is done
644 tabbar.blockSignals(true);
646 // remove all tab bars
647 d.tabWidget->clearTabbar();
649 // rebuild tabbar and function map from scratch
650 if (names.size() > 1) {
651 for(size_t i = 0; i < names.size(); i++) {
652 tabbar.addTab(toqstr(makeDisplayPath(names[i], 30)));
654 if (names[i] == cur_title)
655 tabbar.setCurrentIndex(i);
658 tabbar.blockSignals(false);
659 d.tabWidget->hideTabsIfNecessary();
663 void GuiView::closeCurrentTab()
665 dispatch(FuncRequest(LFUN_BUFFER_CLOSE));
669 void GuiView::currentTabChanged(int i)
671 BOOST_ASSERT(i >= 0 && size_type(i) < d.tabnames.size());
672 dispatch(FuncRequest(LFUN_BUFFER_SWITCH, d.tabnames[i]));
676 void GuiView::updateStatusBar()
678 // let the user see the explicit message
679 if (statusbar_timer_.isActive())
682 statusBar()->showMessage(toqstr(theLyXFunc().viewStatusMessage()));
686 void GuiView::activated(FuncRequest const & func)
692 bool GuiView::hasFocus() const
694 return qApp->activeWindow() == this;
698 QRect GuiView::updateFloatingGeometry()
700 QDesktopWidget& dw = *qApp->desktop();
701 QRect desk = dw.availableGeometry(dw.primaryScreen());
702 // remember only non-maximized sizes
703 if (!isMaximized() && desk.width() > frameGeometry().width() && desk.height() > frameGeometry().height()) {
704 floatingGeometry_ = QRect(x(), y(), width(), height());
706 return floatingGeometry_;
710 void GuiView::resizeEvent(QResizeEvent *)
712 updateFloatingGeometry();
716 void GuiView::moveEvent(QMoveEvent *)
718 updateFloatingGeometry();
722 bool GuiView::event(QEvent * e)
726 // Useful debug code:
727 //case QEvent::WindowActivate:
728 //case QEvent::ActivationChange:
729 //case QEvent::WindowDeactivate:
730 //case QEvent::Paint:
731 //case QEvent::Enter:
732 //case QEvent::Leave:
733 //case QEvent::HoverEnter:
734 //case QEvent::HoverLeave:
735 //case QEvent::HoverMove:
736 //case QEvent::StatusTip:
737 //case QEvent::DragEnter:
738 //case QEvent::DragLeave:
742 case QEvent::ShortcutOverride: {
743 QKeyEvent * ke = static_cast<QKeyEvent*>(e);
744 if (ke->key() == Qt::Key_Tab || ke->key() == Qt::Key_Backtab) {
745 boost::shared_ptr<QKeySymbol> sym(new QKeySymbol);
747 BOOST_ASSERT(work_area_);
748 work_area_->processKeySym(sym, key_modifier::none);
754 return QMainWindow::event(e);
759 bool GuiView::focusNextPrevChild(bool /*next*/)
768 QMainWindow::setWindowTitle(qt_("LyX"));
770 updateFloatingGeometry();
774 void GuiView::busy(bool yes)
776 BOOST_ASSERT(work_area_);
777 static_cast<GuiWorkArea *>(work_area_)->setUpdatesEnabled(!yes);
780 work_area_->stopBlinkingCursor();
781 QApplication::setOverrideCursor(Qt::WaitCursor);
784 work_area_->startBlinkingCursor();
785 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);
836 } // namespace frontend
839 #include "GuiView_moc.cpp"