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 "QLyXKeySym.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>
57 #include <boost/bind.hpp>
58 #include <boost/shared_ptr.hpp>
66 using support::FileName;
67 using support::onlyFilename;
69 using support::libFileSearch;
75 int const statusbar_timer_value = 3000;
80 class WidgetWithTabBar : public QWidget
84 WidgetWithTabBar(QWidget* w)
87 QVBoxLayout* l = new QVBoxLayout;
96 struct GuiView::GuiViewPrivate
98 typedef std::map<int, FuncRequest> FuncMap;
99 typedef std::pair<int, FuncRequest> FuncMapPair;
100 typedef std::map<string, QString> NameMap;
101 typedef std::pair<string, QString> NameMapPair;
105 WidgetWithTabBar* wt;
110 GuiViewPrivate() : wt(0), posx_offset(0), posy_offset(0)
113 unsigned int smallIconSize;
114 unsigned int normalIconSize;
115 unsigned int bigIconSize;
116 // static needed by "New Window"
117 static unsigned int lastIconSize;
119 QMenu* toolBarPopup(GuiView *parent)
121 // FIXME: translation
122 QMenu* menu = new QMenu(parent);
123 QActionGroup *iconSizeGroup = new QActionGroup(parent);
125 QAction *smallIcons = new QAction(iconSizeGroup);
126 smallIcons->setText("Small sized icons");
127 smallIcons->setCheckable(true);
128 QObject::connect(smallIcons, SIGNAL(triggered()), parent, SLOT(smallSizedIcons()));
129 menu->addAction(smallIcons);
131 QAction *normalIcons = new QAction(iconSizeGroup);
132 normalIcons->setText("Normal sized icons");
133 normalIcons->setCheckable(true);
134 QObject::connect(normalIcons, SIGNAL(triggered()), parent, SLOT(normalSizedIcons()));
135 menu->addAction(normalIcons);
138 QAction *bigIcons = new QAction(iconSizeGroup);
139 bigIcons->setText("Big sized icons");
140 bigIcons->setCheckable(true);
141 QObject::connect(bigIcons, SIGNAL(triggered()), parent, SLOT(bigSizedIcons()));
142 menu->addAction(bigIcons);
144 unsigned int cur = parent->iconSize().width();
145 if ( cur == parent->d.smallIconSize)
146 smallIcons->setChecked(true);
147 else if (cur == parent->d.normalIconSize)
148 normalIcons->setChecked(true);
149 else if (cur == parent->d.bigIconSize)
150 bigIcons->setChecked(true);
157 unsigned int GuiView::GuiViewPrivate::lastIconSize = 0;
160 GuiView::GuiView(int id)
161 : QMainWindow(), LyXView(id), commandbuffer_(0), quitting_by_menu_(false),
162 d(*new GuiViewPrivate)
164 // Qt bug? signal lastWindowClosed does not work
165 setAttribute(Qt::WA_QuitOnClose, false);
166 setAttribute(Qt::WA_DeleteOnClose, true);
168 // hardcode here the platform specific icon size
169 d.smallIconSize = 14; // scaling problems
170 d.normalIconSize = 20; // ok, default
171 d.bigIconSize = 26; // better for some math icons
174 // assign an icon to main form. We do not do it under Qt/Mac,
175 // since the icon is provided in the application bundle.
176 FileName const iconname = libFileSearch("images", "lyx", "xpm");
177 if (!iconname.empty())
178 setWindowIcon(QPixmap(toqstr(iconname.absFilename())));
190 void GuiView::close()
192 quitting_by_menu_ = true;
193 QMainWindow::close();
194 quitting_by_menu_ = false;
198 void GuiView::setFocus()
200 BOOST_ASSERT(work_area_);
201 static_cast<GuiWorkArea *>(work_area_)->setFocus();
205 QMenu* GuiView::createPopupMenu()
207 return d.toolBarPopup(this);
213 menubar_.reset(new QLMenubar(this, menubackend));
214 QObject::connect(menuBar(), SIGNAL(triggered(QAction *)),
215 this, SLOT(updateMenu(QAction *)));
217 getToolbars().init();
219 statusBar()->setSizeGripEnabled(false);
221 QObject::connect(&statusbar_timer_, SIGNAL(timeout()),
222 this, SLOT(update_view_state_qt()));
224 BOOST_ASSERT(work_area_);
225 if (!work_area_->bufferView().buffer() && !theBufferList().empty())
226 setBuffer(theBufferList().first());
228 // make sure the buttons are disabled if needed
230 updateLayoutChoice();
235 void GuiView::closeEvent(QCloseEvent * close_event)
237 // we may have been called through the close window button
238 // which bypasses the LFUN machinery.
239 if (!quitting_by_menu_) {
240 if (!theBufferList().quitWriteAll()) {
241 close_event->ignore();
245 if (view()->buffer()) {
246 // save cursor position for opened files to .lyx/session
247 LyX::ref().session().lastFilePos().save(
248 FileName(buffer()->fileName()),
249 boost::tie(view()->cursor().pit(),
250 view()->cursor().pos()));
252 theApp()->gui().unregisterView(id());
253 if (theApp()->gui().viewIds().empty())
255 // this is the place where we leave the frontend.
256 // it is the only point at which we start quitting.
258 close_event->accept();
259 // quit the event loop
262 close_event->accept();
266 void GuiView::saveGeometry()
268 static bool done = false;
275 // change the ifdef to 'geometry = normalGeometry();' only
276 // when Trolltech has fixed the broken normalGeometry on X11:
277 // http://www.trolltech.com/developer/task-tracker/index_html?id=119684+&method=entry
278 // Then also the moveEvent, resizeEvent, and the
279 // code for floatingGeometry_ can be removed;
280 // adjust GuiView::setGeometry()
282 QRect geometry = normalGeometry();
284 updateFloatingGeometry();
285 QRect geometry = floatingGeometry_;
288 // save windows size and position
289 Session & session = LyX::ref().session();
290 session.sessionInfo().save("WindowWidth", convert<string>(geometry.width()));
291 session.sessionInfo().save("WindowHeight", convert<string>(geometry.height()));
292 session.sessionInfo().save("WindowIsMaximized", (isMaximized() ? "yes" : "no"));
293 session.sessionInfo().save("IconSizeXY", convert<string>(iconSize().width()));
294 if (lyxrc.geometry_xysaved) {
295 session.sessionInfo().save("WindowPosX", convert<string>(geometry.x() + d.posx_offset));
296 session.sessionInfo().save("WindowPosY", convert<string>(geometry.y() + d.posy_offset));
298 getToolbars().saveToolbarInfo();
302 void GuiView::setGeometry(unsigned int width,
306 unsigned int iconSizeXY,
307 const std::string & geometryArg)
309 // use last value (not at startup)
310 if (d.lastIconSize != 0)
311 setIconSize(d.lastIconSize);
312 else if (iconSizeXY != 0)
313 setIconSize(iconSizeXY);
315 setIconSize(d.normalIconSize);
317 // only true when the -geometry option was NOT used
318 if (width != 0 && height != 0) {
319 if (posx != -1 && posy != -1) {
320 // if there are ever startup positioning problems
321 // on a virtual desktop then check the 6 lines below
322 // http://doc.trolltech.com/4.2/qdesktopwidget.html
323 QDesktopWidget& dw = *qApp->desktop();
324 QRect desk = dw.availableGeometry(dw.primaryScreen());
325 (posx >= desk.width() ? posx = 50 : true);
326 (posy >= desk.height()? posy = 50 : true);
328 // FIXME: use setGeometry only when Trolltech has fixed the qt4/X11 bug
329 QWidget::setGeometry(posx, posy, width, height);
331 resize(width, height);
335 resize(width, height);
339 setWindowState(Qt::WindowMaximized);
343 // FIXME: move this code into parse_geometry() (lyx_main.C)
347 QRegExp re( "[=]*(?:([0-9]+)[xX]([0-9]+)){0,1}[ ]*(?:([+-][0-9]*)([+-][0-9]*)){0,1}" );
348 re.indexIn( toqstr(geometryArg.c_str()));
349 w = re.cap( 1 ).toInt();
350 h = re.cap( 2 ).toInt();
351 x = re.cap( 3 ).toInt();
352 y = re.cap( 4 ).toInt();
353 QWidget::setGeometry( x, y, w, h );
359 // For an unknown reason, the Window title update is not effective for
360 // the second windows up until it is shown on screen (Qt bug?).
363 // after show geometry() has changed (Qt bug?)
364 // we compensate the drift when storing the position
367 if (width != 0 && height != 0)
368 if (posx != -1 && posy != -1) {
370 d.posx_offset = posx - normalGeometry().x();
371 d.posy_offset = posy - normalGeometry().y();
375 d.posx_offset = posx - geometry().x();
376 d.posy_offset = posy - geometry().y();
384 void GuiView::updateMenu(QAction * /*action*/)
390 void GuiView::setWindowTitle(docstring const & t, docstring const & it)
392 QString title = windowTitle();
393 QString new_title = toqstr(t);
394 if (title != new_title) {
395 QMainWindow::setWindowTitle(new_title);
396 QMainWindow::setWindowIconText(toqstr(it));
401 void GuiView::addCommandBuffer(QToolBar * toolbar)
403 commandbuffer_ = new QCommandBuffer(this, *controlcommand_);
404 focus_command_buffer.connect(boost::bind(&GuiView::focus_command_widget, this));
405 toolbar->addWidget(commandbuffer_);
409 void GuiView::message(docstring const & str)
411 statusBar()->showMessage(toqstr(str));
412 statusbar_timer_.stop();
413 statusbar_timer_.start(statusbar_timer_value);
417 void GuiView::clearMessage()
419 update_view_state_qt();
423 void GuiView::setIconSize(unsigned int size)
425 d.lastIconSize = size;
426 QMainWindow::setIconSize(QSize(size, size));
430 void GuiView::smallSizedIcons()
432 setIconSize(d.smallIconSize);
436 void GuiView::normalSizedIcons()
438 setIconSize(d.normalIconSize);
442 void GuiView::bigSizedIcons()
444 setIconSize(d.bigIconSize);
448 void GuiView::focus_command_widget()
451 commandbuffer_->focus_command();
455 void GuiView::update_view_state_qt()
457 statusBar()->showMessage(toqstr(theLyXFunc().viewStatusMessage()));
458 statusbar_timer_.stop();
462 void GuiView::initTab(QWidget* workarea)
464 d.wt = new WidgetWithTabBar(workarea);
465 setCentralWidget(d.wt);
466 QObject::connect(d.wt->tabbar, SIGNAL(currentChanged(int)),
467 this, SLOT(currentTabChanged(int)));
471 void GuiView::updateTab()
473 QTabBar& tb = *d.wt->tabbar;
475 // update when all is done
476 tb.blockSignals(true);
478 typedef std::vector<string> Strings;
479 Strings const names = theBufferList().getFileNames();
480 size_t n_size = names.size();
483 // show tabs only when there is more
484 // than one file opened
487 for (size_t i = 0; i != n_size; i++)
488 if (d.namemap.find(names[i]) == d.namemap.end())
489 addtab.push_back(names.at(i));
492 for(size_t i = 0; i<addtab.size(); i++)
494 QString tab_name = lyx::toqstr(onlyFilename(addtab.at(i)));
495 d.namemap.insert(GuiViewPrivate::NameMapPair(addtab.at(i), tab_name));
499 // check if all names showed by the tabs
500 // are also in the current bufferlist
505 std::map<string, QString>::iterator tabit = d.namemap.begin();
506 for (;tabit != d.namemap.end(); ++tabit)
509 for (size_t i = 0; i != n_size; i++)
510 if (tabit->first == names.at(i) && notall)
513 removetab.push_back(tabit->first);
518 for(size_t i = 0; i<removetab.size(); i++)
520 if (d.namemap.find(removetab.at(i)) != d.namemap.end())
522 tabit = d.namemap.find(removetab.at(i));
523 for (int i = 0; i < tb.count(); i++)
524 if (tb.tabText(i) == tabit->second)
529 d.namemap.erase(tabit);
534 if (removetab.size() > 0 || addtab.size() > 0)
537 tabit = d.namemap.begin();
538 for (;tabit != d.namemap.end(); ++tabit)
540 QTabBar& tb = *d.wt->tabbar;
541 for (int i = 0; i < tb.count(); i++)
543 if (tb.tabText(i) == tabit->second)
545 FuncRequest func(LFUN_BUFFER_SWITCH, tabit->first);
546 d.funcmap.insert(GuiViewPrivate::FuncMapPair(i, func));
554 if (view()->buffer())
556 string cur_title = view()->buffer()->fileName();
557 if (d.namemap.find(cur_title) != d.namemap.end())
559 QString tabname = d.namemap.find(cur_title)->second;
560 for (int i = 0; i < tb.count(); i++)
561 if (tb.tabText(i) == tabname)
563 tb.setCurrentIndex(i);
569 tb.blockSignals(false);
574 void GuiView::currentTabChanged (int index)
576 std::map<int, FuncRequest>::const_iterator it = d.funcmap.find(index);
577 if (it != d.funcmap.end())
578 activated(it->second);
582 void GuiView::updateStatusBar()
584 // let the user see the explicit message
585 if (statusbar_timer_.isActive())
588 statusBar()->showMessage(toqstr(theLyXFunc().viewStatusMessage()));
592 void GuiView::activated(FuncRequest const & func)
598 bool GuiView::hasFocus() const
600 return qApp->activeWindow() == this;
604 void GuiView::updateFloatingGeometry()
607 floatingGeometry_ = QRect(x(), y(), width(), height());
611 void GuiView::resizeEvent(QResizeEvent *)
613 updateFloatingGeometry();
617 void GuiView::moveEvent(QMoveEvent *)
619 updateFloatingGeometry();
623 bool GuiView::event(QEvent * e)
625 // Useful debug code:
629 case QEvent::WindowActivate:
630 case QEvent::ActivationChange:
631 case QEvent::WindowDeactivate:
635 case QEvent::HoverEnter:
636 case QEvent::HoverLeave:
637 case QEvent::HoverMove:
638 case QEvent::StatusTip:
643 if (e->type() == QEvent::ShortcutOverride) {
644 QKeyEvent * ke = static_cast<QKeyEvent*>(e);
645 if (ke->key() == Qt::Key_Tab || ke->key() == Qt::Key_Backtab) {
646 boost::shared_ptr<QLyXKeySym> sym(new QLyXKeySym);
648 BOOST_ASSERT(work_area_);
649 work_area_->processKeySym(sym, key_modifier::none);
654 //} for the debug switch above.
656 return QMainWindow::event(e);
660 bool GuiView::focusNextPrevChild(bool /*next*/)
669 QMainWindow::setWindowTitle(qt_("LyX"));
671 updateFloatingGeometry();
675 void GuiView::busy(bool yes)
677 BOOST_ASSERT(work_area_);
678 static_cast<GuiWorkArea *>(work_area_)->setUpdatesEnabled(!yes);
681 work_area_->stopBlinkingCursor();
682 QApplication::setOverrideCursor(Qt::WaitCursor);
685 work_area_->startBlinkingCursor();
686 QApplication::restoreOverrideCursor();
691 Toolbars::ToolbarPtr GuiView::makeToolbar(ToolbarBackend::Toolbar const & tbb)
693 QLToolbar * Tb = new QLToolbar(tbb, *this);
694 //static QLToolbar * lastTb = 0;
696 if (tbb.flags & ToolbarBackend::TOP) {
697 addToolBar(Qt::TopToolBarArea, Tb);
698 addToolBarBreak(Qt::TopToolBarArea);
700 if (tbb.flags & ToolbarBackend::BOTTOM) {
701 addToolBar(Qt::BottomToolBarArea, Tb);
704 // http://www.trolltech.com/developer/task-tracker/index_html?id=137015&method=entry
705 // Doesn't work because the toolbar will evtl. be hidden.
707 insertToolBarBreak(lastTb);
711 if (tbb.flags & ToolbarBackend::LEFT) {
712 addToolBar(Qt::LeftToolBarArea, Tb);
714 if (tbb.flags & ToolbarBackend::RIGHT) {
715 addToolBar(Qt::RightToolBarArea, Tb);
718 return Toolbars::ToolbarPtr(Tb);
721 } // namespace frontend
724 #include "GuiView_moc.cpp"