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();
374 d.posx_offset = posx - geometry().x();
375 d.posy_offset = posy - geometry().y();
382 void GuiView::updateMenu(QAction * /*action*/)
388 void GuiView::setWindowTitle(docstring const & t, docstring const & it)
390 QString title = windowTitle();
391 QString new_title = toqstr(t);
392 if (title != new_title) {
393 QMainWindow::setWindowTitle(new_title);
394 QMainWindow::setWindowIconText(toqstr(it));
399 void GuiView::addCommandBuffer(QToolBar * toolbar)
401 commandbuffer_ = new QCommandBuffer(this, *controlcommand_);
402 focus_command_buffer.connect(boost::bind(&GuiView::focus_command_widget, this));
403 toolbar->addWidget(commandbuffer_);
407 void GuiView::message(docstring const & str)
409 statusBar()->showMessage(toqstr(str));
410 statusbar_timer_.stop();
411 statusbar_timer_.start(statusbar_timer_value);
415 void GuiView::clearMessage()
417 update_view_state_qt();
421 void GuiView::setIconSize(unsigned int size)
423 d.lastIconSize = size;
424 QMainWindow::setIconSize(QSize(size, size));
428 void GuiView::smallSizedIcons()
430 setIconSize(d.smallIconSize);
434 void GuiView::normalSizedIcons()
436 setIconSize(d.normalIconSize);
440 void GuiView::bigSizedIcons()
442 setIconSize(d.bigIconSize);
446 void GuiView::focus_command_widget()
449 commandbuffer_->focus_command();
453 void GuiView::update_view_state_qt()
455 statusBar()->showMessage(toqstr(theLyXFunc().viewStatusMessage()));
456 statusbar_timer_.stop();
460 void GuiView::initTab(QWidget* workarea)
462 d.wt = new WidgetWithTabBar(workarea);
463 setCentralWidget(d.wt);
464 QObject::connect(d.wt->tabbar, SIGNAL(currentChanged(int)),
465 this, SLOT(currentTabChanged(int)));
469 void GuiView::updateTab()
471 QTabBar& tb = *d.wt->tabbar;
473 // update when all is done
474 tb.blockSignals(true);
476 typedef std::vector<string> Strings;
477 Strings const names = theBufferList().getFileNames();
478 size_t n_size = names.size();
481 // show tabs only when there is more
482 // than one file opened
485 for (size_t i = 0; i != n_size; i++)
486 if (d.namemap.find(names[i]) == d.namemap.end())
487 addtab.push_back(names.at(i));
490 for(size_t i = 0; i<addtab.size(); i++)
492 QString tab_name = lyx::toqstr(onlyFilename(addtab.at(i)));
493 d.namemap.insert(GuiViewPrivate::NameMapPair(addtab.at(i), tab_name));
497 // check if all names showed by the tabs
498 // are also in the current bufferlist
503 std::map<string, QString>::iterator tabit = d.namemap.begin();
504 for (;tabit != d.namemap.end(); ++tabit)
507 for (size_t i = 0; i != n_size; i++)
508 if (tabit->first == names.at(i) && notall)
511 removetab.push_back(tabit->first);
516 for(size_t i = 0; i<removetab.size(); i++)
518 if (d.namemap.find(removetab.at(i)) != d.namemap.end())
520 tabit = d.namemap.find(removetab.at(i));
521 for (int i = 0; i < tb.count(); i++)
522 if (tb.tabText(i) == tabit->second)
527 d.namemap.erase(tabit);
532 if (removetab.size() > 0 || addtab.size() > 0)
535 tabit = d.namemap.begin();
536 for (;tabit != d.namemap.end(); ++tabit)
538 QTabBar& tb = *d.wt->tabbar;
539 for (int i = 0; i < tb.count(); i++)
541 if (tb.tabText(i) == tabit->second)
543 FuncRequest func(LFUN_BUFFER_SWITCH, tabit->first);
544 d.funcmap.insert(GuiViewPrivate::FuncMapPair(i, func));
552 if (view()->buffer())
554 string cur_title = view()->buffer()->fileName();
555 if (d.namemap.find(cur_title) != d.namemap.end())
557 QString tabname = d.namemap.find(cur_title)->second;
558 for (int i = 0; i < tb.count(); i++)
559 if (tb.tabText(i) == tabname)
561 tb.setCurrentIndex(i);
567 tb.blockSignals(false);
572 void GuiView::currentTabChanged (int index)
574 std::map<int, FuncRequest>::const_iterator it = d.funcmap.find(index);
575 if (it != d.funcmap.end())
576 activated(it->second);
580 void GuiView::updateStatusBar()
582 // let the user see the explicit message
583 if (statusbar_timer_.isActive())
586 statusBar()->showMessage(toqstr(theLyXFunc().viewStatusMessage()));
590 void GuiView::activated(FuncRequest const & func)
596 bool GuiView::hasFocus() const
598 return qApp->activeWindow() == this;
602 void GuiView::updateFloatingGeometry()
605 floatingGeometry_ = QRect(x(), y(), width(), height());
609 void GuiView::resizeEvent(QResizeEvent *)
611 updateFloatingGeometry();
615 void GuiView::moveEvent(QMoveEvent *)
617 updateFloatingGeometry();
621 bool GuiView::event(QEvent * e)
623 // Useful debug code:
627 case QEvent::WindowActivate:
628 case QEvent::ActivationChange:
629 case QEvent::WindowDeactivate:
633 case QEvent::HoverEnter:
634 case QEvent::HoverLeave:
635 case QEvent::HoverMove:
636 case QEvent::StatusTip:
641 if (e->type() == QEvent::ShortcutOverride) {
642 QKeyEvent * ke = static_cast<QKeyEvent*>(e);
643 if (ke->key() == Qt::Key_Tab || ke->key() == Qt::Key_Backtab) {
644 boost::shared_ptr<QLyXKeySym> sym(new QLyXKeySym);
646 BOOST_ASSERT(work_area_);
647 work_area_->processKeySym(sym, key_modifier::none);
652 //} for the debug switch above.
654 return QMainWindow::event(e);
658 bool GuiView::focusNextPrevChild(bool /*next*/)
667 QMainWindow::setWindowTitle(qt_("LyX"));
669 updateFloatingGeometry();
673 void GuiView::busy(bool yes)
675 BOOST_ASSERT(work_area_);
676 static_cast<GuiWorkArea *>(work_area_)->setUpdatesEnabled(!yes);
679 work_area_->stopBlinkingCursor();
680 QApplication::setOverrideCursor(Qt::WaitCursor);
683 work_area_->startBlinkingCursor();
684 QApplication::restoreOverrideCursor();
689 Toolbars::ToolbarPtr GuiView::makeToolbar(ToolbarBackend::Toolbar const & tbb)
691 QLToolbar * Tb = new QLToolbar(tbb, *this);
692 //static QLToolbar * lastTb = 0;
694 if (tbb.flags & ToolbarBackend::TOP) {
695 addToolBar(Qt::TopToolBarArea, Tb);
696 addToolBarBreak(Qt::TopToolBarArea);
698 if (tbb.flags & ToolbarBackend::BOTTOM) {
699 addToolBar(Qt::BottomToolBarArea, Tb);
702 // http://www.trolltech.com/developer/task-tracker/index_html?id=137015&method=entry
703 // Doesn't work because the toolbar will evtl. be hidden.
705 insertToolBarBreak(lastTb);
709 if (tbb.flags & ToolbarBackend::LEFT) {
710 addToolBar(Qt::LeftToolBarArea, Tb);
712 if (tbb.flags & ToolbarBackend::RIGHT) {
713 addToolBar(Qt::RightToolBarArea, Tb);
716 return Toolbars::ToolbarPtr(Tb);
719 } // namespace frontend
722 #include "GuiView_moc.cpp"