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"
34 #include "BufferView.h"
35 #include "BufferList.h"
37 #include "FuncRequest.h"
43 #include "MenuBackend.h"
45 #include "BufferList.h"
48 #include <QApplication>
49 #include <QCloseEvent>
54 #include <QDesktopWidget>
55 #include <QVBoxLayout>
56 #include <QHBoxLayout>
57 #include <QToolButton>
60 #include <boost/bind.hpp>
61 #include <boost/shared_ptr.hpp>
69 using support::FileName;
70 using support::libFileSearch;
71 using support::makeDisplayPath;
77 int const statusbar_timer_value = 3000;
79 class TabWidget : public QWidget
84 QToolButton* closeTabButton;
86 void hideTabsIfNecessary()
88 if (tabbar->count() > 1) {
90 closeTabButton->show();
93 closeTabButton->hide();
97 TabWidget(QWidget* w, bool topTabBar)
99 closeTabButton = new QToolButton(this);
100 FileName const file = support::libFileSearch("images", "closetab", "xpm");
102 QPixmap pm(toqstr(file.absFilename()));
103 closeTabButton->setIcon(QIcon(pm));
105 closeTabButton->setText("Close");
107 closeTabButton->setCursor(Qt::ArrowCursor);
108 closeTabButton->setAutoRaise(true);
109 closeTabButton->setToolTip(tr("Close tab"));
110 closeTabButton->setEnabled(true);
112 tabbar = new QTabBar;
113 hlayout = new QHBoxLayout;
114 QVBoxLayout* vlayout = new QVBoxLayout;
115 hlayout->addWidget(tabbar);
116 hlayout->addStretch(1);
117 hlayout->addWidget(closeTabButton);
119 vlayout->addLayout(hlayout);
120 vlayout->addWidget(w);
122 tabbar->setShape(QTabBar::RoundedSouth);
123 vlayout->addWidget(w);
124 vlayout->addLayout(hlayout);
126 vlayout->setMargin(0);
127 hlayout->setMargin(0);
129 hideTabsIfNecessary();
134 for (int i = tabbar->count() - 1; i >= 0; --i)
135 tabbar->removeTab(i);
142 struct GuiView::GuiViewPrivate
144 vector<string> tabnames;
147 TabWidget* tabWidget;
152 GuiViewPrivate() : tabWidget(0), posx_offset(0), posy_offset(0)
155 unsigned int smallIconSize;
156 unsigned int normalIconSize;
157 unsigned int bigIconSize;
158 // static needed by "New Window"
159 static unsigned int lastIconSize;
161 QMenu* toolBarPopup(GuiView *parent)
163 // FIXME: translation
164 QMenu* menu = new QMenu(parent);
165 QActionGroup *iconSizeGroup = new QActionGroup(parent);
167 QAction *smallIcons = new QAction(iconSizeGroup);
168 smallIcons->setText(qt_("Small-sized icons"));
169 smallIcons->setCheckable(true);
170 QObject::connect(smallIcons, SIGNAL(triggered()), parent, SLOT(smallSizedIcons()));
171 menu->addAction(smallIcons);
173 QAction *normalIcons = new QAction(iconSizeGroup);
174 normalIcons->setText(qt_("Normal-sized icons"));
175 normalIcons->setCheckable(true);
176 QObject::connect(normalIcons, SIGNAL(triggered()), parent, SLOT(normalSizedIcons()));
177 menu->addAction(normalIcons);
179 QAction *bigIcons = new QAction(iconSizeGroup);
180 bigIcons->setText(qt_("Big-sized icons"));
181 bigIcons->setCheckable(true);
182 QObject::connect(bigIcons, SIGNAL(triggered()), parent, SLOT(bigSizedIcons()));
183 menu->addAction(bigIcons);
185 unsigned int cur = parent->iconSize().width();
186 if ( cur == parent->d.smallIconSize)
187 smallIcons->setChecked(true);
188 else if (cur == parent->d.normalIconSize)
189 normalIcons->setChecked(true);
190 else if (cur == parent->d.bigIconSize)
191 bigIcons->setChecked(true);
198 unsigned int GuiView::GuiViewPrivate::lastIconSize = 0;
201 GuiView::GuiView(int id)
202 : QMainWindow(), LyXView(id), commandbuffer_(0), quitting_by_menu_(false),
203 d(*new GuiViewPrivate)
205 // Qt bug? signal lastWindowClosed does not work
206 setAttribute(Qt::WA_QuitOnClose, false);
207 setAttribute(Qt::WA_DeleteOnClose, true);
209 // hardcode here the platform specific icon size
210 d.smallIconSize = 14; // scaling problems
211 d.normalIconSize = 20; // ok, default
212 d.bigIconSize = 26; // better for some math icons
215 // assign an icon to main form. We do not do it under Qt/Mac,
216 // since the icon is provided in the application bundle.
217 FileName const iconname = libFileSearch("images", "lyx", "xpm");
218 if (!iconname.empty())
219 setWindowIcon(QPixmap(toqstr(iconname.absFilename())));
231 void GuiView::close()
233 quitting_by_menu_ = true;
234 QMainWindow::close();
235 quitting_by_menu_ = false;
239 void GuiView::setFocus()
241 BOOST_ASSERT(work_area_);
242 static_cast<GuiWorkArea *>(work_area_)->setFocus();
246 QMenu* GuiView::createPopupMenu()
248 return d.toolBarPopup(this);
254 menubar_.reset(new QLMenubar(this, menubackend));
255 QObject::connect(menuBar(), SIGNAL(triggered(QAction *)),
256 this, SLOT(updateMenu(QAction *)));
258 getToolbars().init();
260 statusBar()->setSizeGripEnabled(true);
262 QObject::connect(&statusbar_timer_, SIGNAL(timeout()),
263 this, SLOT(update_view_state_qt()));
265 BOOST_ASSERT(work_area_);
266 if (!work_area_->bufferView().buffer() && !theBufferList().empty())
267 setBuffer(theBufferList().first());
269 // make sure the buttons are disabled if needed
271 updateLayoutChoice();
276 void GuiView::closeEvent(QCloseEvent * close_event)
278 // we may have been called through the close window button
279 // which bypasses the LFUN machinery.
280 if (!quitting_by_menu_ && theApp()->gui().viewIds().size() == 1) {
281 if (!theBufferList().quitWriteAll()) {
282 close_event->ignore();
287 theApp()->gui().unregisterView(id());
288 if (!theApp()->gui().viewIds().empty()) {
289 // Just close the window and do nothing else if this is not the
291 close_event->accept();
295 if (view()->buffer()) {
296 // save cursor position for opened files to .lyx/session
297 // only bottom (whole doc) level pit and pos is saved.
298 LyX::ref().session().lastFilePos().save(
299 FileName(buffer()->fileName()),
300 boost::tie(view()->cursor().bottom().pit(),
301 view()->cursor().bottom().pos()));
304 // this is the place where we leave the frontend.
305 // it is the only point at which we start quitting.
307 close_event->accept();
308 // quit the event loop
313 void GuiView::saveGeometry()
315 static bool done = false;
322 // change the ifdef to 'geometry = normalGeometry();' only
323 // when Trolltech has fixed the broken normalGeometry on X11:
324 // http://www.trolltech.com/developer/task-tracker/index_html?id=119684+&method=entry
325 // Then also the moveEvent, resizeEvent, and the
326 // code for floatingGeometry_ can be removed;
327 // adjust GuiView::setGeometry()
329 QRect normal_geometry;
332 normal_geometry = normalGeometry();
334 maximized = CompletelyMaximized;
336 maximized = NotMaximized;
339 normal_geometry = updateFloatingGeometry();
341 QDesktopWidget& dw = *qApp->desktop();
342 QRect desk = dw.availableGeometry(dw.primaryScreen());
343 // Qt bug on Linux: load completely maximized, vert max. save-> frameGeometry().height() is wrong
344 if (isMaximized() && desk.width() <= frameGeometry().width() && desk.height() <= frameGeometry().height()) {
345 maximized = CompletelyMaximized;
346 // maximizing does not work when the window is allready hor. or vert. maximized
347 // Tested only on KDE
348 int dh = frameGeometry().height() - height();
349 if (desk.height() <= normal_geometry.height() + dh)
350 normal_geometry.setHeight(normal_geometry.height() - 1);
351 int dw = frameGeometry().width() - width();
352 if (desk.width() <= normal_geometry.width() + dw)
353 normal_geometry.setWidth(normal_geometry.width() - 1);
354 } else if (desk.height() <= frameGeometry().height()) {
355 maximized = VerticallyMaximized;
356 } else if (desk.width() <= frameGeometry().width()) {
357 maximized = HorizontallyMaximized;
359 maximized = NotMaximized;
364 // save windows size and position
365 Session & session = LyX::ref().session();
366 session.sessionInfo().save("WindowWidth", convert<string>(normal_geometry.width()));
367 session.sessionInfo().save("WindowHeight", convert<string>(normal_geometry.height()));
368 session.sessionInfo().save("WindowMaximized", convert<string>(maximized));
369 session.sessionInfo().save("IconSizeXY", convert<string>(iconSize().width()));
370 if (lyxrc.geometry_xysaved) {
371 session.sessionInfo().save("WindowPosX", convert<string>(normal_geometry.x() + d.posx_offset));
372 session.sessionInfo().save("WindowPosY", convert<string>(normal_geometry.y() + d.posy_offset));
374 getToolbars().saveToolbarInfo();
378 void GuiView::setGeometry(unsigned int width,
382 unsigned int iconSizeXY,
383 const string & geometryArg)
385 // use last value (not at startup)
386 if (d.lastIconSize != 0)
387 setIconSize(d.lastIconSize);
388 else if (iconSizeXY != 0)
389 setIconSize(iconSizeXY);
391 setIconSize(d.normalIconSize);
393 // only true when the -geometry option was NOT used
394 if (width != 0 && height != 0) {
395 if (posx != -1 && posy != -1) {
396 // if there are startup positioning problems:
397 // http://doc.trolltech.com/4.2/qdesktopwidget.html
398 QDesktopWidget& dw = *qApp->desktop();
399 if (dw.isVirtualDesktop()) {
400 if(!dw.geometry().contains(posx, posy)) {
405 // Which system doesn't use a virtual desktop?
406 // TODO save also last screen number and check if it is still availabe.
409 // FIXME: use setGeometry only when Trolltech has fixed the qt4/X11 bug
410 QWidget::setGeometry(posx, posy, width, height);
412 resize(width, height);
416 resize(width, height);
419 // remember original size
420 floatingGeometry_ = QRect(posx, posy, width, height);
422 if (maximized != NotMaximized) {
423 if (maximized == CompletelyMaximized) {
424 setWindowState(Qt::WindowMaximized);
427 // TODO How to set by the window manager?
428 // setWindowState(Qt::WindowVerticallyMaximized);
430 QDesktopWidget& dw = *qApp->desktop();
431 QRect desk = dw.availableGeometry(dw.primaryScreen());
432 if (maximized == VerticallyMaximized)
433 resize(width, desk.height());
434 if (maximized == HorizontallyMaximized)
435 resize(desk.width(), height);
442 // FIXME: move this code into parse_geometry() (LyX.cpp)
446 QRegExp re( "[=]*(?:([0-9]+)[xX]([0-9]+)){0,1}[ ]*(?:([+-][0-9]*)([+-][0-9]*)){0,1}" );
447 re.indexIn(toqstr(geometryArg.c_str()));
448 w = re.cap(1).toInt();
449 h = re.cap(2).toInt();
450 x = re.cap(3).toInt();
451 y = re.cap(4).toInt();
452 QWidget::setGeometry( x, y, w, h );
461 // For an unknown reason, the Window title update is not effective for
462 // the second windows up until it is shown on screen (Qt bug?).
465 // after show geometry() has changed (Qt bug?)
466 // we compensate the drift when storing the position
469 if (width != 0 && height != 0)
470 if (posx != -1 && posy != -1) {
472 d.posx_offset = posx - normalGeometry().x();
473 d.posy_offset = posy - normalGeometry().y();
476 if (maximized == NotMaximized) {
477 d.posx_offset = posx - geometry().x();
478 d.posy_offset = posy - geometry().y();
486 void GuiView::updateMenu(QAction * /*action*/)
492 void GuiView::setWindowTitle(docstring const & t, docstring const & it)
494 QString title = windowTitle();
495 QString new_title = toqstr(t);
496 if (title != new_title) {
497 QMainWindow::setWindowTitle(new_title);
498 QMainWindow::setWindowIconText(toqstr(it));
503 void GuiView::addCommandBuffer(QToolBar * toolbar)
505 commandbuffer_ = new QCommandBuffer(this, *controlcommand_);
506 focus_command_buffer.connect(boost::bind(&GuiView::focus_command_widget, this));
507 toolbar->addWidget(commandbuffer_);
511 void GuiView::message(docstring const & str)
513 statusBar()->showMessage(toqstr(str));
514 statusbar_timer_.stop();
515 statusbar_timer_.start(statusbar_timer_value);
519 void GuiView::clearMessage()
521 update_view_state_qt();
525 void GuiView::setIconSize(unsigned int size)
527 d.lastIconSize = size;
528 QMainWindow::setIconSize(QSize(size, size));
532 void GuiView::smallSizedIcons()
534 setIconSize(d.smallIconSize);
538 void GuiView::normalSizedIcons()
540 setIconSize(d.normalIconSize);
544 void GuiView::bigSizedIcons()
546 setIconSize(d.bigIconSize);
550 void GuiView::focus_command_widget()
553 commandbuffer_->focus_command();
557 void GuiView::update_view_state_qt()
559 statusBar()->showMessage(toqstr(theLyXFunc().viewStatusMessage()));
560 statusbar_timer_.stop();
564 void GuiView::initTab(QWidget* workarea)
566 // construct the TabWidget with 'false' to have the tabbar at the bottom
567 d.tabWidget = new TabWidget(workarea, true);
568 setCentralWidget(d.tabWidget);
569 QObject::connect(d.tabWidget->tabbar, SIGNAL(currentChanged(int)),
570 this, SLOT(currentTabChanged(int)));
571 QObject::connect(d.tabWidget->closeTabButton, SIGNAL(clicked()),
572 this, SLOT(closeCurrentTab()));
576 void GuiView::updateTab()
578 std::vector<string> const & names = theBufferList().getFileNames();
581 if (view()->buffer()) {
582 cur_title = view()->buffer()->fileName();
585 // avoid unnecessary tabbar rebuild:
586 // check if something has changed
587 if (d.tabnames == names && d.cur_title == cur_title)
590 d.cur_title = cur_title;
592 QTabBar & tabbar = *d.tabWidget->tabbar;
594 // update when all is done
595 tabbar.blockSignals(true);
597 // remove all tab bars
598 d.tabWidget->clearTabbar();
600 // rebuild tabbar and function map from scratch
601 if (names.size() > 1) {
602 for(size_t i = 0; i < names.size(); i++) {
603 tabbar.addTab(toqstr(makeDisplayPath(names[i], 30)));
605 if (names[i] == cur_title)
606 tabbar.setCurrentIndex(i);
609 tabbar.blockSignals(false);
610 d.tabWidget->hideTabsIfNecessary();
614 void GuiView::closeCurrentTab()
616 dispatch(FuncRequest(LFUN_BUFFER_CLOSE));
620 void GuiView::currentTabChanged(int i)
622 BOOST_ASSERT(i >= 0 && size_type(i) < d.tabnames.size());
623 dispatch(FuncRequest(LFUN_BUFFER_SWITCH, d.tabnames[i]));
627 void GuiView::updateStatusBar()
629 // let the user see the explicit message
630 if (statusbar_timer_.isActive())
633 statusBar()->showMessage(toqstr(theLyXFunc().viewStatusMessage()));
637 void GuiView::activated(FuncRequest const & func)
643 bool GuiView::hasFocus() const
645 return qApp->activeWindow() == this;
649 QRect GuiView::updateFloatingGeometry()
651 QDesktopWidget& dw = *qApp->desktop();
652 QRect desk = dw.availableGeometry(dw.primaryScreen());
653 // remember only non-maximized sizes
654 if (!isMaximized() && desk.width() > frameGeometry().width() && desk.height() > frameGeometry().height()) {
655 floatingGeometry_ = QRect(x(), y(), width(), height());
657 return floatingGeometry_;
661 void GuiView::resizeEvent(QResizeEvent *)
663 updateFloatingGeometry();
667 void GuiView::moveEvent(QMoveEvent *)
669 updateFloatingGeometry();
673 bool GuiView::event(QEvent * e)
675 // Useful debug code:
679 case QEvent::WindowActivate:
680 case QEvent::ActivationChange:
681 case QEvent::WindowDeactivate:
685 case QEvent::HoverEnter:
686 case QEvent::HoverLeave:
687 case QEvent::HoverMove:
688 case QEvent::StatusTip:
693 if (e->type() == QEvent::ShortcutOverride) {
694 QKeyEvent * ke = static_cast<QKeyEvent*>(e);
695 if (ke->key() == Qt::Key_Tab || ke->key() == Qt::Key_Backtab) {
696 boost::shared_ptr<QKeySymbol> sym(new QKeySymbol);
698 BOOST_ASSERT(work_area_);
699 work_area_->processKeySym(sym, key_modifier::none);
704 //} for the debug switch above.
706 return QMainWindow::event(e);
710 bool GuiView::focusNextPrevChild(bool /*next*/)
719 QMainWindow::setWindowTitle(qt_("LyX"));
721 updateFloatingGeometry();
725 void GuiView::busy(bool yes)
727 BOOST_ASSERT(work_area_);
728 static_cast<GuiWorkArea *>(work_area_)->setUpdatesEnabled(!yes);
731 work_area_->stopBlinkingCursor();
732 QApplication::setOverrideCursor(Qt::WaitCursor);
735 work_area_->startBlinkingCursor();
736 QApplication::restoreOverrideCursor();
741 Toolbars::ToolbarPtr GuiView::makeToolbar(ToolbarInfo const & tbinfo, bool newline)
743 QLToolbar * Tb = new QLToolbar(tbinfo, *this);
745 if (tbinfo.flags & ToolbarInfo::TOP) {
747 addToolBarBreak(Qt::TopToolBarArea);
748 addToolBar(Qt::TopToolBarArea, Tb);
751 if (tbinfo.flags & ToolbarInfo::BOTTOM) {
752 // Qt < 4.2.2 cannot handle ToolBarBreak on non-TOP dock.
753 #if (QT_VERSION >= 0x040202)
755 addToolBarBreak(Qt::BottomToolBarArea);
757 addToolBar(Qt::BottomToolBarArea, Tb);
760 if (tbinfo.flags & ToolbarInfo::LEFT) {
761 // Qt < 4.2.2 cannot handle ToolBarBreak on non-TOP dock.
762 #if (QT_VERSION >= 0x040202)
764 addToolBarBreak(Qt::LeftToolBarArea);
766 addToolBar(Qt::LeftToolBarArea, Tb);
769 if (tbinfo.flags & ToolbarInfo::RIGHT) {
770 // Qt < 4.2.2 cannot handle ToolBarBreak on non-TOP dock.
771 #if (QT_VERSION >= 0x040202)
773 addToolBarBreak(Qt::RightToolBarArea);
775 addToolBar(Qt::RightToolBarArea, Tb);
778 // The following does not work so I cannot restore to exact toolbar location
780 ToolbarSection::ToolbarInfo & tbinfo = LyX::ref().session().toolbars().load(tbinfo.name);
781 Tb->move(tbinfo.posx, tbinfo.posy);
784 return Toolbars::ToolbarPtr(Tb);
787 } // namespace frontend
790 #include "GuiView_moc.cpp"