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 // Make sure that no LFUN use this close to be closed View.
292 theLyXFunc().setLyXView(0);
293 // Make sure the timer time out will not trigger a statusbar update.
294 statusbar_timer_.stop();
296 theApp()->gui().unregisterView(id());
297 if (!theApp()->gui().viewIds().empty()) {
299 // Just close the window and do nothing else if this is not the
301 close_event->accept();
305 if (view()->buffer()) {
306 // save cursor position for opened files to .lyx/session
307 // only bottom (whole doc) level pit and pos is saved.
308 LyX::ref().session().lastFilePos().save(
309 FileName(buffer()->fileName()),
310 boost::tie(view()->cursor().bottom().pit(),
311 view()->cursor().bottom().pos()));
314 // this is the place where we leave the frontend.
315 // it is the only point at which we start quitting.
317 close_event->accept();
318 // quit the event loop
323 void GuiView::saveGeometry()
325 static bool done = false;
332 // change the ifdef to 'geometry = normalGeometry();' only
333 // when Trolltech has fixed the broken normalGeometry on X11:
334 // http://www.trolltech.com/developer/task-tracker/index_html?id=119684+&method=entry
335 // Then also the moveEvent, resizeEvent, and the
336 // code for floatingGeometry_ can be removed;
337 // adjust GuiView::setGeometry()
339 QRect normal_geometry;
342 normal_geometry = normalGeometry();
344 maximized = CompletelyMaximized;
346 maximized = NotMaximized;
349 normal_geometry = updateFloatingGeometry();
351 QDesktopWidget& dw = *qApp->desktop();
352 QRect desk = dw.availableGeometry(dw.primaryScreen());
353 // Qt bug on Linux: load completely maximized, vert max. save-> frameGeometry().height() is wrong
354 if (isMaximized() && desk.width() <= frameGeometry().width() && desk.height() <= frameGeometry().height()) {
355 maximized = CompletelyMaximized;
356 // maximizing does not work when the window is allready hor. or vert. maximized
357 // Tested only on KDE
358 int dh = frameGeometry().height() - height();
359 if (desk.height() <= normal_geometry.height() + dh)
360 normal_geometry.setHeight(normal_geometry.height() - 1);
361 int dw = frameGeometry().width() - width();
362 if (desk.width() <= normal_geometry.width() + dw)
363 normal_geometry.setWidth(normal_geometry.width() - 1);
364 } else if (desk.height() <= frameGeometry().height()) {
365 maximized = VerticallyMaximized;
366 } else if (desk.width() <= frameGeometry().width()) {
367 maximized = HorizontallyMaximized;
369 maximized = NotMaximized;
374 // save windows size and position
375 Session & session = LyX::ref().session();
376 session.sessionInfo().save("WindowWidth", convert<string>(normal_geometry.width()));
377 session.sessionInfo().save("WindowHeight", convert<string>(normal_geometry.height()));
378 session.sessionInfo().save("WindowMaximized", convert<string>(maximized));
379 session.sessionInfo().save("IconSizeXY", convert<string>(iconSize().width()));
380 if (lyxrc.geometry_xysaved) {
381 session.sessionInfo().save("WindowPosX", convert<string>(normal_geometry.x() + d.posx_offset));
382 session.sessionInfo().save("WindowPosY", convert<string>(normal_geometry.y() + d.posy_offset));
384 getToolbars().saveToolbarInfo();
388 void GuiView::setGeometry(unsigned int width,
392 unsigned int iconSizeXY,
393 const string & geometryArg)
395 // use last value (not at startup)
396 if (d.lastIconSize != 0)
397 setIconSize(d.lastIconSize);
398 else if (iconSizeXY != 0)
399 setIconSize(iconSizeXY);
401 setIconSize(d.normalIconSize);
403 // only true when the -geometry option was NOT used
404 if (width != 0 && height != 0) {
405 if (posx != -1 && posy != -1) {
406 // if there are startup positioning problems:
407 // http://doc.trolltech.com/4.2/qdesktopwidget.html
408 QDesktopWidget& dw = *qApp->desktop();
409 if (dw.isVirtualDesktop()) {
410 if(!dw.geometry().contains(posx, posy)) {
415 // Which system doesn't use a virtual desktop?
416 // TODO save also last screen number and check if it is still availabe.
419 // FIXME: use setGeometry only when Trolltech has fixed the qt4/X11 bug
420 QWidget::setGeometry(posx, posy, width, height);
422 resize(width, height);
426 resize(width, height);
429 // remember original size
430 floatingGeometry_ = QRect(posx, posy, width, height);
432 if (maximized != NotMaximized) {
433 if (maximized == CompletelyMaximized) {
434 setWindowState(Qt::WindowMaximized);
437 // TODO How to set by the window manager?
438 // setWindowState(Qt::WindowVerticallyMaximized);
440 QDesktopWidget& dw = *qApp->desktop();
441 QRect desk = dw.availableGeometry(dw.primaryScreen());
442 if (maximized == VerticallyMaximized)
443 resize(width, desk.height());
444 if (maximized == HorizontallyMaximized)
445 resize(desk.width(), height);
452 // FIXME: move this code into parse_geometry() (LyX.cpp)
456 QRegExp re( "[=]*(?:([0-9]+)[xX]([0-9]+)){0,1}[ ]*(?:([+-][0-9]*)([+-][0-9]*)){0,1}" );
457 re.indexIn(toqstr(geometryArg.c_str()));
458 w = re.cap(1).toInt();
459 h = re.cap(2).toInt();
460 x = re.cap(3).toInt();
461 y = re.cap(4).toInt();
462 QWidget::setGeometry( x, y, w, h );
471 // For an unknown reason, the Window title update is not effective for
472 // the second windows up until it is shown on screen (Qt bug?).
475 // after show geometry() has changed (Qt bug?)
476 // we compensate the drift when storing the position
479 if (width != 0 && height != 0)
480 if (posx != -1 && posy != -1) {
482 d.posx_offset = posx - normalGeometry().x();
483 d.posy_offset = posy - normalGeometry().y();
486 if (maximized == NotMaximized) {
487 d.posx_offset = posx - geometry().x();
488 d.posy_offset = posy - geometry().y();
496 void GuiView::updateMenu(QAction * /*action*/)
502 void GuiView::setWindowTitle(docstring const & t, docstring const & it)
504 QString title = windowTitle();
505 QString new_title = toqstr(t);
506 if (title != new_title) {
507 QMainWindow::setWindowTitle(new_title);
508 QMainWindow::setWindowIconText(toqstr(it));
513 void GuiView::addCommandBuffer(QToolBar * toolbar)
515 commandbuffer_ = new QCommandBuffer(this, *controlcommand_);
516 focus_command_buffer.connect(boost::bind(&GuiView::focus_command_widget, this));
517 toolbar->addWidget(commandbuffer_);
521 void GuiView::message(docstring const & str)
523 statusBar()->showMessage(toqstr(str));
524 statusbar_timer_.stop();
525 statusbar_timer_.start(statusbar_timer_value);
529 void GuiView::clearMessage()
531 update_view_state_qt();
535 void GuiView::setIconSize(unsigned int size)
537 d.lastIconSize = size;
538 QMainWindow::setIconSize(QSize(size, size));
542 void GuiView::smallSizedIcons()
544 setIconSize(d.smallIconSize);
548 void GuiView::normalSizedIcons()
550 setIconSize(d.normalIconSize);
554 void GuiView::bigSizedIcons()
556 setIconSize(d.bigIconSize);
560 void GuiView::focus_command_widget()
563 commandbuffer_->focus_command();
567 void GuiView::update_view_state_qt()
569 statusBar()->showMessage(toqstr(theLyXFunc().viewStatusMessage()));
570 statusbar_timer_.stop();
574 void GuiView::initTab(QWidget* workarea)
576 // construct the TabWidget with 'false' to have the tabbar at the bottom
577 d.tabWidget = new TabWidget(workarea, true);
578 setCentralWidget(d.tabWidget);
579 QObject::connect(d.tabWidget->tabbar, SIGNAL(currentChanged(int)),
580 this, SLOT(currentTabChanged(int)));
581 QObject::connect(d.tabWidget->closeTabButton, SIGNAL(clicked()),
582 this, SLOT(closeCurrentTab()));
586 void GuiView::updateTab()
588 std::vector<string> const & names = theBufferList().getFileNames();
591 if (view()->buffer()) {
592 cur_title = view()->buffer()->fileName();
595 // avoid unnecessary tabbar rebuild:
596 // check if something has changed
597 if (d.tabnames == names && d.cur_title == cur_title)
600 d.cur_title = cur_title;
602 QTabBar & tabbar = *d.tabWidget->tabbar;
604 // update when all is done
605 tabbar.blockSignals(true);
607 // remove all tab bars
608 d.tabWidget->clearTabbar();
610 // rebuild tabbar and function map from scratch
611 if (names.size() > 1) {
612 for(size_t i = 0; i < names.size(); i++) {
613 tabbar.addTab(toqstr(makeDisplayPath(names[i], 30)));
615 if (names[i] == cur_title)
616 tabbar.setCurrentIndex(i);
619 tabbar.blockSignals(false);
620 d.tabWidget->hideTabsIfNecessary();
624 void GuiView::closeCurrentTab()
626 dispatch(FuncRequest(LFUN_BUFFER_CLOSE));
630 void GuiView::currentTabChanged(int i)
632 BOOST_ASSERT(i >= 0 && size_type(i) < d.tabnames.size());
633 dispatch(FuncRequest(LFUN_BUFFER_SWITCH, d.tabnames[i]));
637 void GuiView::updateStatusBar()
639 // let the user see the explicit message
640 if (statusbar_timer_.isActive())
643 statusBar()->showMessage(toqstr(theLyXFunc().viewStatusMessage()));
647 void GuiView::activated(FuncRequest const & func)
653 bool GuiView::hasFocus() const
655 return qApp->activeWindow() == this;
659 QRect GuiView::updateFloatingGeometry()
661 QDesktopWidget& dw = *qApp->desktop();
662 QRect desk = dw.availableGeometry(dw.primaryScreen());
663 // remember only non-maximized sizes
664 if (!isMaximized() && desk.width() > frameGeometry().width() && desk.height() > frameGeometry().height()) {
665 floatingGeometry_ = QRect(x(), y(), width(), height());
667 return floatingGeometry_;
671 void GuiView::resizeEvent(QResizeEvent *)
673 updateFloatingGeometry();
677 void GuiView::moveEvent(QMoveEvent *)
679 updateFloatingGeometry();
683 bool GuiView::event(QEvent * e)
685 // Useful debug code:
689 case QEvent::WindowActivate:
690 case QEvent::ActivationChange:
691 case QEvent::WindowDeactivate:
695 case QEvent::HoverEnter:
696 case QEvent::HoverLeave:
697 case QEvent::HoverMove:
698 case QEvent::StatusTip:
703 if (e->type() == QEvent::ShortcutOverride) {
704 QKeyEvent * ke = static_cast<QKeyEvent*>(e);
705 if (ke->key() == Qt::Key_Tab || ke->key() == Qt::Key_Backtab) {
706 boost::shared_ptr<QKeySymbol> sym(new QKeySymbol);
708 BOOST_ASSERT(work_area_);
709 work_area_->processKeySym(sym, key_modifier::none);
714 //} for the debug switch above.
716 return QMainWindow::event(e);
720 bool GuiView::focusNextPrevChild(bool /*next*/)
729 QMainWindow::setWindowTitle(qt_("LyX"));
731 updateFloatingGeometry();
735 void GuiView::busy(bool yes)
737 BOOST_ASSERT(work_area_);
738 static_cast<GuiWorkArea *>(work_area_)->setUpdatesEnabled(!yes);
741 work_area_->stopBlinkingCursor();
742 QApplication::setOverrideCursor(Qt::WaitCursor);
745 work_area_->startBlinkingCursor();
746 QApplication::restoreOverrideCursor();
751 Toolbars::ToolbarPtr GuiView::makeToolbar(ToolbarInfo const & tbinfo, bool newline)
753 QLToolbar * Tb = new QLToolbar(tbinfo, *this);
755 if (tbinfo.flags & ToolbarInfo::TOP) {
757 addToolBarBreak(Qt::TopToolBarArea);
758 addToolBar(Qt::TopToolBarArea, Tb);
761 if (tbinfo.flags & ToolbarInfo::BOTTOM) {
762 // Qt < 4.2.2 cannot handle ToolBarBreak on non-TOP dock.
763 #if (QT_VERSION >= 0x040202)
765 addToolBarBreak(Qt::BottomToolBarArea);
767 addToolBar(Qt::BottomToolBarArea, Tb);
770 if (tbinfo.flags & ToolbarInfo::LEFT) {
771 // Qt < 4.2.2 cannot handle ToolBarBreak on non-TOP dock.
772 #if (QT_VERSION >= 0x040202)
774 addToolBarBreak(Qt::LeftToolBarArea);
776 addToolBar(Qt::LeftToolBarArea, Tb);
779 if (tbinfo.flags & ToolbarInfo::RIGHT) {
780 // Qt < 4.2.2 cannot handle ToolBarBreak on non-TOP dock.
781 #if (QT_VERSION >= 0x040202)
783 addToolBarBreak(Qt::RightToolBarArea);
785 addToolBar(Qt::RightToolBarArea, Tb);
788 // The following does not work so I cannot restore to exact toolbar location
790 ToolbarSection::ToolbarInfo & tbinfo = LyX::ref().session().toolbars().load(tbinfo.name);
791 Tb->move(tbinfo.posx, tbinfo.posy);
794 return Toolbars::ToolbarPtr(Tb);
797 } // namespace frontend
800 #include "GuiView_moc.cpp"