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()) {
307 // Just close the window and do nothing else if this is not the
309 close_event->accept();
313 if (view()->buffer()) {
314 // save cursor position for opened files to .lyx/session
315 // only bottom (whole doc) level pit and pos is saved.
316 LyX::ref().session().lastFilePos().save(
317 FileName(buffer()->fileName()),
318 boost::tie(view()->cursor().bottom().pit(),
319 view()->cursor().bottom().pos()));
322 // this is the place where we leave the frontend.
323 // it is the only point at which we start quitting.
325 close_event->accept();
326 // quit the event loop
331 void GuiView::dragEnterEvent(QDragEnterEvent * event)
333 if (event->mimeData()->hasUrls())
335 /// \todo Ask lyx-devel is this is enough:
336 /// if (event->mimeData()->hasFormat("text/plain"))
337 /// event->acceptProposedAction();
341 void GuiView::dropEvent(QDropEvent* event)
343 QList<QUrl> files = event->mimeData()->urls();
347 LYXERR(Debug::GUI) << BOOST_CURRENT_FUNCTION
348 << " got URLs!" << endl;
349 for (int i = 0; i!=files.size(); ++i) {
350 string const file = support::os::internal_path(fromqstr(
351 files.at(i).toLocalFile()));
353 dispatch(FuncRequest(LFUN_FILE_OPEN, file));
358 void GuiView::saveGeometry()
360 static bool done = false;
367 // change the ifdef to 'geometry = normalGeometry();' only
368 // when Trolltech has fixed the broken normalGeometry on X11:
369 // http://www.trolltech.com/developer/task-tracker/index_html?id=119684+&method=entry
370 // Then also the moveEvent, resizeEvent, and the
371 // code for floatingGeometry_ can be removed;
372 // adjust GuiView::setGeometry()
374 QRect normal_geometry;
377 normal_geometry = normalGeometry();
379 maximized = CompletelyMaximized;
381 maximized = NotMaximized;
384 normal_geometry = updateFloatingGeometry();
386 QDesktopWidget& dw = *qApp->desktop();
387 QRect desk = dw.availableGeometry(dw.primaryScreen());
388 // Qt bug on Linux: load completely maximized, vert max. save-> frameGeometry().height() is wrong
389 if (isMaximized() && desk.width() <= frameGeometry().width() && desk.height() <= frameGeometry().height()) {
390 maximized = CompletelyMaximized;
391 // maximizing does not work when the window is allready hor. or vert. maximized
392 // Tested only on KDE
393 int dh = frameGeometry().height() - height();
394 if (desk.height() <= normal_geometry.height() + dh)
395 normal_geometry.setHeight(normal_geometry.height() - 1);
396 int dw = frameGeometry().width() - width();
397 if (desk.width() <= normal_geometry.width() + dw)
398 normal_geometry.setWidth(normal_geometry.width() - 1);
399 } else if (desk.height() <= frameGeometry().height()) {
400 maximized = VerticallyMaximized;
401 } else if (desk.width() <= frameGeometry().width()) {
402 maximized = HorizontallyMaximized;
404 maximized = NotMaximized;
409 // save windows size and position
410 Session & session = LyX::ref().session();
411 session.sessionInfo().save("WindowWidth", convert<string>(normal_geometry.width()));
412 session.sessionInfo().save("WindowHeight", convert<string>(normal_geometry.height()));
413 session.sessionInfo().save("WindowMaximized", convert<string>(maximized));
414 session.sessionInfo().save("IconSizeXY", convert<string>(iconSize().width()));
415 if (lyxrc.geometry_xysaved) {
416 session.sessionInfo().save("WindowPosX", convert<string>(normal_geometry.x() + d.posx_offset));
417 session.sessionInfo().save("WindowPosY", convert<string>(normal_geometry.y() + d.posy_offset));
419 getToolbars().saveToolbarInfo();
423 void GuiView::setGeometry(unsigned int width,
427 unsigned int iconSizeXY,
428 const string & geometryArg)
430 // use last value (not at startup)
431 if (d.lastIconSize != 0)
432 setIconSize(d.lastIconSize);
433 else if (iconSizeXY != 0)
434 setIconSize(iconSizeXY);
436 setIconSize(d.normalIconSize);
438 // only true when the -geometry option was NOT used
439 if (width != 0 && height != 0) {
440 if (posx != -1 && posy != -1) {
441 // if there are startup positioning problems:
442 // http://doc.trolltech.com/4.2/qdesktopwidget.html
443 QDesktopWidget& dw = *qApp->desktop();
444 if (dw.isVirtualDesktop()) {
445 if(!dw.geometry().contains(posx, posy)) {
450 // Which system doesn't use a virtual desktop?
451 // TODO save also last screen number and check if it is still availabe.
454 // FIXME: use setGeometry only when Trolltech has fixed the qt4/X11 bug
455 QWidget::setGeometry(posx, posy, width, height);
457 resize(width, height);
461 resize(width, height);
464 // remember original size
465 floatingGeometry_ = QRect(posx, posy, width, height);
467 if (maximized != NotMaximized) {
468 if (maximized == CompletelyMaximized) {
469 setWindowState(Qt::WindowMaximized);
472 // TODO How to set by the window manager?
473 // setWindowState(Qt::WindowVerticallyMaximized);
475 QDesktopWidget& dw = *qApp->desktop();
476 QRect desk = dw.availableGeometry(dw.primaryScreen());
477 if (maximized == VerticallyMaximized)
478 resize(width, desk.height());
479 if (maximized == HorizontallyMaximized)
480 resize(desk.width(), height);
487 // FIXME: move this code into parse_geometry() (LyX.cpp)
491 QRegExp re( "[=]*(?:([0-9]+)[xX]([0-9]+)){0,1}[ ]*(?:([+-][0-9]*)([+-][0-9]*)){0,1}" );
492 re.indexIn(toqstr(geometryArg.c_str()));
493 w = re.cap(1).toInt();
494 h = re.cap(2).toInt();
495 x = re.cap(3).toInt();
496 y = re.cap(4).toInt();
497 QWidget::setGeometry( x, y, w, h );
506 // For an unknown reason, the Window title update is not effective for
507 // the second windows up until it is shown on screen (Qt bug?).
510 // after show geometry() has changed (Qt bug?)
511 // we compensate the drift when storing the position
514 if (width != 0 && height != 0)
515 if (posx != -1 && posy != -1) {
517 d.posx_offset = posx - normalGeometry().x();
518 d.posy_offset = posy - normalGeometry().y();
521 if (maximized == NotMaximized) {
522 d.posx_offset = posx - geometry().x();
523 d.posy_offset = posy - geometry().y();
531 void GuiView::updateMenu(QAction * /*action*/)
537 void GuiView::setWindowTitle(docstring const & t, docstring const & it)
539 QString title = windowTitle();
540 QString new_title = toqstr(t);
541 if (title != new_title) {
542 QMainWindow::setWindowTitle(new_title);
543 QMainWindow::setWindowIconText(toqstr(it));
548 void GuiView::addCommandBuffer(QToolBar * toolbar)
550 commandbuffer_ = new QCommandBuffer(this, *controlcommand_);
551 focus_command_buffer.connect(boost::bind(&GuiView::focus_command_widget, this));
552 toolbar->addWidget(commandbuffer_);
556 void GuiView::message(docstring const & str)
558 statusBar()->showMessage(toqstr(str));
559 statusbar_timer_.stop();
560 statusbar_timer_.start(statusbar_timer_value);
564 void GuiView::clearMessage()
566 update_view_state_qt();
570 void GuiView::setIconSize(unsigned int size)
572 d.lastIconSize = size;
573 QMainWindow::setIconSize(QSize(size, size));
577 void GuiView::smallSizedIcons()
579 setIconSize(d.smallIconSize);
583 void GuiView::normalSizedIcons()
585 setIconSize(d.normalIconSize);
589 void GuiView::bigSizedIcons()
591 setIconSize(d.bigIconSize);
595 void GuiView::focus_command_widget()
598 commandbuffer_->focus_command();
602 void GuiView::update_view_state_qt()
604 statusBar()->showMessage(toqstr(theLyXFunc().viewStatusMessage()));
605 statusbar_timer_.stop();
609 void GuiView::initTab(QWidget* workarea)
611 // construct the TabWidget with 'false' to have the tabbar at the bottom
612 d.tabWidget = new TabWidget(workarea, true);
613 setCentralWidget(d.tabWidget);
614 QObject::connect(d.tabWidget->tabbar, SIGNAL(currentChanged(int)),
615 this, SLOT(currentTabChanged(int)));
616 QObject::connect(d.tabWidget->closeTabButton, SIGNAL(clicked()),
617 this, SLOT(closeCurrentTab()));
621 void GuiView::updateTab()
623 std::vector<string> const & names = theBufferList().getFileNames();
626 if (view()->buffer()) {
627 cur_title = view()->buffer()->fileName();
630 // avoid unnecessary tabbar rebuild:
631 // check if something has changed
632 if (d.tabnames == names && d.cur_title == cur_title)
635 d.cur_title = cur_title;
637 QTabBar & tabbar = *d.tabWidget->tabbar;
639 // update when all is done
640 tabbar.blockSignals(true);
642 // remove all tab bars
643 d.tabWidget->clearTabbar();
645 // rebuild tabbar and function map from scratch
646 if (names.size() > 1) {
647 for(size_t i = 0; i < names.size(); i++) {
648 tabbar.addTab(toqstr(makeDisplayPath(names[i], 30)));
650 if (names[i] == cur_title)
651 tabbar.setCurrentIndex(i);
654 tabbar.blockSignals(false);
655 d.tabWidget->hideTabsIfNecessary();
659 void GuiView::closeCurrentTab()
661 dispatch(FuncRequest(LFUN_BUFFER_CLOSE));
665 void GuiView::currentTabChanged(int i)
667 BOOST_ASSERT(i >= 0 && size_type(i) < d.tabnames.size());
668 dispatch(FuncRequest(LFUN_BUFFER_SWITCH, d.tabnames[i]));
672 void GuiView::updateStatusBar()
674 // let the user see the explicit message
675 if (statusbar_timer_.isActive())
678 statusBar()->showMessage(toqstr(theLyXFunc().viewStatusMessage()));
682 void GuiView::activated(FuncRequest const & func)
688 bool GuiView::hasFocus() const
690 return qApp->activeWindow() == this;
694 QRect GuiView::updateFloatingGeometry()
696 QDesktopWidget& dw = *qApp->desktop();
697 QRect desk = dw.availableGeometry(dw.primaryScreen());
698 // remember only non-maximized sizes
699 if (!isMaximized() && desk.width() > frameGeometry().width() && desk.height() > frameGeometry().height()) {
700 floatingGeometry_ = QRect(x(), y(), width(), height());
702 return floatingGeometry_;
706 void GuiView::resizeEvent(QResizeEvent *)
708 updateFloatingGeometry();
712 void GuiView::moveEvent(QMoveEvent *)
714 updateFloatingGeometry();
718 bool GuiView::event(QEvent * e)
720 // Useful debug code:
724 case QEvent::WindowActivate:
725 case QEvent::ActivationChange:
726 case QEvent::WindowDeactivate:
730 case QEvent::HoverEnter:
731 case QEvent::HoverLeave:
732 case QEvent::HoverMove:
733 case QEvent::StatusTip:
738 if (e->type() == QEvent::ShortcutOverride) {
739 QKeyEvent * ke = static_cast<QKeyEvent*>(e);
740 if (ke->key() == Qt::Key_Tab || ke->key() == Qt::Key_Backtab) {
741 boost::shared_ptr<QKeySymbol> sym(new QKeySymbol);
743 BOOST_ASSERT(work_area_);
744 work_area_->processKeySym(sym, key_modifier::none);
749 //} for the debug switch above.
751 return QMainWindow::event(e);
755 bool GuiView::focusNextPrevChild(bool /*next*/)
764 QMainWindow::setWindowTitle(qt_("LyX"));
766 updateFloatingGeometry();
770 void GuiView::busy(bool yes)
772 BOOST_ASSERT(work_area_);
773 static_cast<GuiWorkArea *>(work_area_)->setUpdatesEnabled(!yes);
776 work_area_->stopBlinkingCursor();
777 QApplication::setOverrideCursor(Qt::WaitCursor);
780 work_area_->startBlinkingCursor();
781 QApplication::restoreOverrideCursor();
786 Toolbars::ToolbarPtr GuiView::makeToolbar(ToolbarInfo const & tbinfo, bool newline)
788 QLToolbar * Tb = new QLToolbar(tbinfo, *this);
790 if (tbinfo.flags & ToolbarInfo::TOP) {
792 addToolBarBreak(Qt::TopToolBarArea);
793 addToolBar(Qt::TopToolBarArea, Tb);
796 if (tbinfo.flags & ToolbarInfo::BOTTOM) {
797 // Qt < 4.2.2 cannot handle ToolBarBreak on non-TOP dock.
798 #if (QT_VERSION >= 0x040202)
800 addToolBarBreak(Qt::BottomToolBarArea);
802 addToolBar(Qt::BottomToolBarArea, Tb);
805 if (tbinfo.flags & ToolbarInfo::LEFT) {
806 // Qt < 4.2.2 cannot handle ToolBarBreak on non-TOP dock.
807 #if (QT_VERSION >= 0x040202)
809 addToolBarBreak(Qt::LeftToolBarArea);
811 addToolBar(Qt::LeftToolBarArea, Tb);
814 if (tbinfo.flags & ToolbarInfo::RIGHT) {
815 // Qt < 4.2.2 cannot handle ToolBarBreak on non-TOP dock.
816 #if (QT_VERSION >= 0x040202)
818 addToolBarBreak(Qt::RightToolBarArea);
820 addToolBar(Qt::RightToolBarArea, Tb);
823 // The following does not work so I cannot restore to exact toolbar location
825 ToolbarSection::ToolbarInfo & tbinfo = LyX::ref().session().toolbars().load(tbinfo.name);
826 Tb->move(tbinfo.posx, tbinfo.posy);
829 return Toolbars::ToolbarPtr(Tb);
832 } // namespace frontend
835 #include "GuiView_moc.cpp"