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 <QPushButton>
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 QPushButton* closeTabButton;
86 void hideTabsIfNecessary()
88 if (tabbar->count() > 1) {
90 closeTabButton->show();
93 closeTabButton->hide();
97 TabWidget(QWidget* w, bool topTabBar)
99 closeTabButton = new QPushButton(this);
100 FileName const file = support::libFileSearch("images", "closetab", "xpm");
102 QPixmap pm(toqstr(file.absFilename()));
103 closeTabButton->setIcon(QIcon(pm));
104 closeTabButton->setMaximumSize(pm.size());
105 closeTabButton->setFlat(true);
107 closeTabButton->setText("Close");
109 closeTabButton->setCursor(Qt::ArrowCursor);
110 closeTabButton->setToolTip(tr("Close tab"));
111 closeTabButton->setEnabled(true);
113 tabbar = new QTabBar;
114 #if QT_VERSION >= 0x040200
115 tabbar->setUsesScrollButtons(true);
117 hlayout = new QHBoxLayout;
118 QVBoxLayout* vlayout = new QVBoxLayout;
119 hlayout->addWidget(tabbar);
120 hlayout->addWidget(closeTabButton);
122 vlayout->addLayout(hlayout);
123 vlayout->addWidget(w);
125 tabbar->setShape(QTabBar::RoundedSouth);
126 vlayout->addWidget(w);
127 vlayout->addLayout(hlayout);
129 vlayout->setMargin(0);
130 vlayout->setSpacing(0);
131 hlayout->setMargin(0);
133 hideTabsIfNecessary();
138 for (int i = tabbar->count() - 1; i >= 0; --i)
139 tabbar->removeTab(i);
146 struct GuiView::GuiViewPrivate
148 vector<string> tabnames;
151 TabWidget* tabWidget;
156 GuiViewPrivate() : tabWidget(0), posx_offset(0), posy_offset(0)
159 unsigned int smallIconSize;
160 unsigned int normalIconSize;
161 unsigned int bigIconSize;
162 // static needed by "New Window"
163 static unsigned int lastIconSize;
165 QMenu* toolBarPopup(GuiView *parent)
167 // FIXME: translation
168 QMenu* menu = new QMenu(parent);
169 QActionGroup *iconSizeGroup = new QActionGroup(parent);
171 QAction *smallIcons = new QAction(iconSizeGroup);
172 smallIcons->setText(qt_("Small-sized icons"));
173 smallIcons->setCheckable(true);
174 QObject::connect(smallIcons, SIGNAL(triggered()), parent, SLOT(smallSizedIcons()));
175 menu->addAction(smallIcons);
177 QAction *normalIcons = new QAction(iconSizeGroup);
178 normalIcons->setText(qt_("Normal-sized icons"));
179 normalIcons->setCheckable(true);
180 QObject::connect(normalIcons, SIGNAL(triggered()), parent, SLOT(normalSizedIcons()));
181 menu->addAction(normalIcons);
183 QAction *bigIcons = new QAction(iconSizeGroup);
184 bigIcons->setText(qt_("Big-sized icons"));
185 bigIcons->setCheckable(true);
186 QObject::connect(bigIcons, SIGNAL(triggered()), parent, SLOT(bigSizedIcons()));
187 menu->addAction(bigIcons);
189 unsigned int cur = parent->iconSize().width();
190 if ( cur == parent->d.smallIconSize)
191 smallIcons->setChecked(true);
192 else if (cur == parent->d.normalIconSize)
193 normalIcons->setChecked(true);
194 else if (cur == parent->d.bigIconSize)
195 bigIcons->setChecked(true);
202 unsigned int GuiView::GuiViewPrivate::lastIconSize = 0;
205 GuiView::GuiView(int id)
206 : QMainWindow(), LyXView(id), commandbuffer_(0), quitting_by_menu_(false),
207 d(*new GuiViewPrivate)
209 // Qt bug? signal lastWindowClosed does not work
210 setAttribute(Qt::WA_QuitOnClose, false);
211 setAttribute(Qt::WA_DeleteOnClose, true);
213 // hardcode here the platform specific icon size
214 d.smallIconSize = 14; // scaling problems
215 d.normalIconSize = 20; // ok, default
216 d.bigIconSize = 26; // better for some math icons
219 // assign an icon to main form. We do not do it under Qt/Mac,
220 // since the icon is provided in the application bundle.
221 FileName const iconname = libFileSearch("images", "lyx", "xpm");
222 if (!iconname.empty())
223 setWindowIcon(QPixmap(toqstr(iconname.absFilename())));
235 void GuiView::close()
237 quitting_by_menu_ = true;
238 QMainWindow::close();
239 quitting_by_menu_ = false;
243 void GuiView::setFocus()
245 BOOST_ASSERT(work_area_);
246 static_cast<GuiWorkArea *>(work_area_)->setFocus();
250 QMenu* GuiView::createPopupMenu()
252 return d.toolBarPopup(this);
258 menubar_.reset(new QLMenubar(this, menubackend));
259 QObject::connect(menuBar(), SIGNAL(triggered(QAction *)),
260 this, SLOT(updateMenu(QAction *)));
262 getToolbars().init();
264 statusBar()->setSizeGripEnabled(true);
266 QObject::connect(&statusbar_timer_, SIGNAL(timeout()),
267 this, SLOT(update_view_state_qt()));
269 BOOST_ASSERT(work_area_);
270 if (!work_area_->bufferView().buffer() && !theBufferList().empty())
271 setBuffer(theBufferList().first());
273 // make sure the buttons are disabled if needed
275 updateLayoutChoice();
280 void GuiView::closeEvent(QCloseEvent * close_event)
282 // we may have been called through the close window button
283 // which bypasses the LFUN machinery.
284 if (!quitting_by_menu_ && theApp()->gui().viewIds().size() == 1) {
285 if (!theBufferList().quitWriteAll()) {
286 close_event->ignore();
291 theApp()->gui().unregisterView(id());
292 if (!theApp()->gui().viewIds().empty()) {
293 // Just close the window and do nothing else if this is not the
295 close_event->accept();
299 if (view()->buffer()) {
300 // save cursor position for opened files to .lyx/session
301 // only bottom (whole doc) level pit and pos is saved.
302 LyX::ref().session().lastFilePos().save(
303 FileName(buffer()->fileName()),
304 boost::tie(view()->cursor().bottom().pit(),
305 view()->cursor().bottom().pos()));
308 // this is the place where we leave the frontend.
309 // it is the only point at which we start quitting.
311 close_event->accept();
312 // quit the event loop
317 void GuiView::saveGeometry()
319 static bool done = false;
326 // change the ifdef to 'geometry = normalGeometry();' only
327 // when Trolltech has fixed the broken normalGeometry on X11:
328 // http://www.trolltech.com/developer/task-tracker/index_html?id=119684+&method=entry
329 // Then also the moveEvent, resizeEvent, and the
330 // code for floatingGeometry_ can be removed;
331 // adjust GuiView::setGeometry()
333 QRect normal_geometry;
336 normal_geometry = normalGeometry();
338 maximized = CompletelyMaximized;
340 maximized = NotMaximized;
343 normal_geometry = updateFloatingGeometry();
345 QDesktopWidget& dw = *qApp->desktop();
346 QRect desk = dw.availableGeometry(dw.primaryScreen());
347 // Qt bug on Linux: load completely maximized, vert max. save-> frameGeometry().height() is wrong
348 if (isMaximized() && desk.width() <= frameGeometry().width() && desk.height() <= frameGeometry().height()) {
349 maximized = CompletelyMaximized;
350 // maximizing does not work when the window is allready hor. or vert. maximized
351 // Tested only on KDE
352 int dh = frameGeometry().height() - height();
353 if (desk.height() <= normal_geometry.height() + dh)
354 normal_geometry.setHeight(normal_geometry.height() - 1);
355 int dw = frameGeometry().width() - width();
356 if (desk.width() <= normal_geometry.width() + dw)
357 normal_geometry.setWidth(normal_geometry.width() - 1);
358 } else if (desk.height() <= frameGeometry().height()) {
359 maximized = VerticallyMaximized;
360 } else if (desk.width() <= frameGeometry().width()) {
361 maximized = HorizontallyMaximized;
363 maximized = NotMaximized;
368 // save windows size and position
369 Session & session = LyX::ref().session();
370 session.sessionInfo().save("WindowWidth", convert<string>(normal_geometry.width()));
371 session.sessionInfo().save("WindowHeight", convert<string>(normal_geometry.height()));
372 session.sessionInfo().save("WindowMaximized", convert<string>(maximized));
373 session.sessionInfo().save("IconSizeXY", convert<string>(iconSize().width()));
374 if (lyxrc.geometry_xysaved) {
375 session.sessionInfo().save("WindowPosX", convert<string>(normal_geometry.x() + d.posx_offset));
376 session.sessionInfo().save("WindowPosY", convert<string>(normal_geometry.y() + d.posy_offset));
378 getToolbars().saveToolbarInfo();
382 void GuiView::setGeometry(unsigned int width,
386 unsigned int iconSizeXY,
387 const string & geometryArg)
389 // use last value (not at startup)
390 if (d.lastIconSize != 0)
391 setIconSize(d.lastIconSize);
392 else if (iconSizeXY != 0)
393 setIconSize(iconSizeXY);
395 setIconSize(d.normalIconSize);
397 // only true when the -geometry option was NOT used
398 if (width != 0 && height != 0) {
399 if (posx != -1 && posy != -1) {
400 // if there are startup positioning problems:
401 // http://doc.trolltech.com/4.2/qdesktopwidget.html
402 QDesktopWidget& dw = *qApp->desktop();
403 if (dw.isVirtualDesktop()) {
404 if(!dw.geometry().contains(posx, posy)) {
409 // Which system doesn't use a virtual desktop?
410 // TODO save also last screen number and check if it is still availabe.
413 // FIXME: use setGeometry only when Trolltech has fixed the qt4/X11 bug
414 QWidget::setGeometry(posx, posy, width, height);
416 resize(width, height);
420 resize(width, height);
423 // remember original size
424 floatingGeometry_ = QRect(posx, posy, width, height);
426 if (maximized != NotMaximized) {
427 if (maximized == CompletelyMaximized) {
428 setWindowState(Qt::WindowMaximized);
431 // TODO How to set by the window manager?
432 // setWindowState(Qt::WindowVerticallyMaximized);
434 QDesktopWidget& dw = *qApp->desktop();
435 QRect desk = dw.availableGeometry(dw.primaryScreen());
436 if (maximized == VerticallyMaximized)
437 resize(width, desk.height());
438 if (maximized == HorizontallyMaximized)
439 resize(desk.width(), height);
446 // FIXME: move this code into parse_geometry() (LyX.cpp)
450 QRegExp re( "[=]*(?:([0-9]+)[xX]([0-9]+)){0,1}[ ]*(?:([+-][0-9]*)([+-][0-9]*)){0,1}" );
451 re.indexIn(toqstr(geometryArg.c_str()));
452 w = re.cap(1).toInt();
453 h = re.cap(2).toInt();
454 x = re.cap(3).toInt();
455 y = re.cap(4).toInt();
456 QWidget::setGeometry( x, y, w, h );
465 // For an unknown reason, the Window title update is not effective for
466 // the second windows up until it is shown on screen (Qt bug?).
469 // after show geometry() has changed (Qt bug?)
470 // we compensate the drift when storing the position
473 if (width != 0 && height != 0)
474 if (posx != -1 && posy != -1) {
476 d.posx_offset = posx - normalGeometry().x();
477 d.posy_offset = posy - normalGeometry().y();
480 if (maximized == NotMaximized) {
481 d.posx_offset = posx - geometry().x();
482 d.posy_offset = posy - geometry().y();
490 void GuiView::updateMenu(QAction * /*action*/)
496 void GuiView::setWindowTitle(docstring const & t, docstring const & it)
498 QString title = windowTitle();
499 QString new_title = toqstr(t);
500 if (title != new_title) {
501 QMainWindow::setWindowTitle(new_title);
502 QMainWindow::setWindowIconText(toqstr(it));
507 void GuiView::addCommandBuffer(QToolBar * toolbar)
509 commandbuffer_ = new QCommandBuffer(this, *controlcommand_);
510 focus_command_buffer.connect(boost::bind(&GuiView::focus_command_widget, this));
511 toolbar->addWidget(commandbuffer_);
515 void GuiView::message(docstring const & str)
517 statusBar()->showMessage(toqstr(str));
518 statusbar_timer_.stop();
519 statusbar_timer_.start(statusbar_timer_value);
523 void GuiView::clearMessage()
525 update_view_state_qt();
529 void GuiView::setIconSize(unsigned int size)
531 d.lastIconSize = size;
532 QMainWindow::setIconSize(QSize(size, size));
536 void GuiView::smallSizedIcons()
538 setIconSize(d.smallIconSize);
542 void GuiView::normalSizedIcons()
544 setIconSize(d.normalIconSize);
548 void GuiView::bigSizedIcons()
550 setIconSize(d.bigIconSize);
554 void GuiView::focus_command_widget()
557 commandbuffer_->focus_command();
561 void GuiView::update_view_state_qt()
563 statusBar()->showMessage(toqstr(theLyXFunc().viewStatusMessage()));
564 statusbar_timer_.stop();
568 void GuiView::initTab(QWidget* workarea)
570 // construct the TabWidget with 'false' to have the tabbar at the bottom
571 d.tabWidget = new TabWidget(workarea, true);
572 setCentralWidget(d.tabWidget);
573 QObject::connect(d.tabWidget->tabbar, SIGNAL(currentChanged(int)),
574 this, SLOT(currentTabChanged(int)));
575 QObject::connect(d.tabWidget->closeTabButton, SIGNAL(clicked()),
576 this, SLOT(closeCurrentTab()));
580 void GuiView::updateTab()
582 std::vector<string> const & names = theBufferList().getFileNames();
585 if (view()->buffer()) {
586 cur_title = view()->buffer()->fileName();
589 // avoid unnecessary tabbar rebuild:
590 // check if something has changed
591 if (d.tabnames == names && d.cur_title == cur_title)
594 d.cur_title = cur_title;
596 QTabBar & tabbar = *d.tabWidget->tabbar;
598 // update when all is done
599 tabbar.blockSignals(true);
601 // remove all tab bars
602 d.tabWidget->clearTabbar();
604 // rebuild tabbar and function map from scratch
605 if (names.size() > 1) {
606 for(size_t i = 0; i < names.size(); i++) {
607 tabbar.addTab(toqstr(makeDisplayPath(names[i], 30)));
609 if (names[i] == cur_title)
610 tabbar.setCurrentIndex(i);
613 tabbar.blockSignals(false);
614 d.tabWidget->hideTabsIfNecessary();
618 void GuiView::closeCurrentTab()
620 dispatch(FuncRequest(LFUN_BUFFER_CLOSE));
624 void GuiView::currentTabChanged(int i)
626 BOOST_ASSERT(i >= 0 && size_type(i) < d.tabnames.size());
627 dispatch(FuncRequest(LFUN_BUFFER_SWITCH, d.tabnames[i]));
631 void GuiView::updateStatusBar()
633 // let the user see the explicit message
634 if (statusbar_timer_.isActive())
637 statusBar()->showMessage(toqstr(theLyXFunc().viewStatusMessage()));
641 void GuiView::activated(FuncRequest const & func)
647 bool GuiView::hasFocus() const
649 return qApp->activeWindow() == this;
653 QRect GuiView::updateFloatingGeometry()
655 QDesktopWidget& dw = *qApp->desktop();
656 QRect desk = dw.availableGeometry(dw.primaryScreen());
657 // remember only non-maximized sizes
658 if (!isMaximized() && desk.width() > frameGeometry().width() && desk.height() > frameGeometry().height()) {
659 floatingGeometry_ = QRect(x(), y(), width(), height());
661 return floatingGeometry_;
665 void GuiView::resizeEvent(QResizeEvent *)
667 updateFloatingGeometry();
671 void GuiView::moveEvent(QMoveEvent *)
673 updateFloatingGeometry();
677 bool GuiView::event(QEvent * e)
679 // Useful debug code:
683 case QEvent::WindowActivate:
684 case QEvent::ActivationChange:
685 case QEvent::WindowDeactivate:
689 case QEvent::HoverEnter:
690 case QEvent::HoverLeave:
691 case QEvent::HoverMove:
692 case QEvent::StatusTip:
697 if (e->type() == QEvent::ShortcutOverride) {
698 QKeyEvent * ke = static_cast<QKeyEvent*>(e);
699 if (ke->key() == Qt::Key_Tab || ke->key() == Qt::Key_Backtab) {
700 boost::shared_ptr<QKeySymbol> sym(new QKeySymbol);
702 BOOST_ASSERT(work_area_);
703 work_area_->processKeySym(sym, key_modifier::none);
708 //} for the debug switch above.
710 return QMainWindow::event(e);
714 bool GuiView::focusNextPrevChild(bool /*next*/)
723 QMainWindow::setWindowTitle(qt_("LyX"));
725 updateFloatingGeometry();
729 void GuiView::busy(bool yes)
731 BOOST_ASSERT(work_area_);
732 static_cast<GuiWorkArea *>(work_area_)->setUpdatesEnabled(!yes);
735 work_area_->stopBlinkingCursor();
736 QApplication::setOverrideCursor(Qt::WaitCursor);
739 work_area_->startBlinkingCursor();
740 QApplication::restoreOverrideCursor();
745 Toolbars::ToolbarPtr GuiView::makeToolbar(ToolbarInfo const & tbinfo, bool newline)
747 QLToolbar * Tb = new QLToolbar(tbinfo, *this);
749 if (tbinfo.flags & ToolbarInfo::TOP) {
751 addToolBarBreak(Qt::TopToolBarArea);
752 addToolBar(Qt::TopToolBarArea, Tb);
755 if (tbinfo.flags & ToolbarInfo::BOTTOM) {
756 // Qt < 4.2.2 cannot handle ToolBarBreak on non-TOP dock.
757 #if (QT_VERSION >= 0x040202)
759 addToolBarBreak(Qt::BottomToolBarArea);
761 addToolBar(Qt::BottomToolBarArea, Tb);
764 if (tbinfo.flags & ToolbarInfo::LEFT) {
765 // Qt < 4.2.2 cannot handle ToolBarBreak on non-TOP dock.
766 #if (QT_VERSION >= 0x040202)
768 addToolBarBreak(Qt::LeftToolBarArea);
770 addToolBar(Qt::LeftToolBarArea, Tb);
773 if (tbinfo.flags & ToolbarInfo::RIGHT) {
774 // Qt < 4.2.2 cannot handle ToolBarBreak on non-TOP dock.
775 #if (QT_VERSION >= 0x040202)
777 addToolBarBreak(Qt::RightToolBarArea);
779 addToolBar(Qt::RightToolBarArea, Tb);
782 // The following does not work so I cannot restore to exact toolbar location
784 ToolbarSection::ToolbarInfo & tbinfo = LyX::ref().session().toolbars().load(tbinfo.name);
785 Tb->move(tbinfo.posx, tbinfo.posy);
788 return Toolbars::ToolbarPtr(Tb);
791 } // namespace frontend
794 #include "GuiView_moc.cpp"