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 "QLMenubar.h"
21 #include "QLToolbar.h"
22 #include "QCommandBuffer.h"
23 #include "qt_helpers.h"
25 #include "frontends/Application.h"
26 #include "frontends/Gui.h"
27 #include "frontends/WorkArea.h"
29 #include "support/filetools.h"
30 #include "support/convert.h"
31 #include "support/lstrings.h"
33 #include "BufferView.h"
34 #include "bufferlist.h"
36 #include "funcrequest.h"
42 #include "MenuBackend.h"
44 #include "bufferlist.h"
47 #include <QApplication>
48 #include <QCloseEvent>
53 #include <QDesktopWidget>
54 #include <QVBoxLayout>
56 #include <boost/bind.hpp>
64 using support::FileName;
65 using support::onlyFilename;
67 using support::libFileSearch;
73 int const statusbar_timer_value = 3000;
78 class WidgetWithTabBar : public QWidget
82 WidgetWithTabBar(QWidget* w)
85 QVBoxLayout* l = new QVBoxLayout;
94 struct GuiView::GuiViewPrivate
96 typedef std::map<int, FuncRequest> FuncMap;
97 typedef std::pair<int, FuncRequest> FuncMapPair;
98 typedef std::map<string, QString> NameMap;
99 typedef std::pair<string, QString> NameMapPair;
103 WidgetWithTabBar* wt;
108 GuiViewPrivate() : wt(0), posx_offset(0), posy_offset(0)
111 unsigned int smallIconSize;
112 unsigned int normalIconSize;
113 unsigned int bigIconSize;
114 // static needed by "New Window"
115 static unsigned int lastIconSize;
117 QMenu* toolBarPopup(GuiView *parent)
119 // FIXME: translation
120 QMenu* menu = new QMenu(parent);
121 QActionGroup *iconSizeGroup = new QActionGroup(parent);
123 QAction *smallIcons = new QAction(iconSizeGroup);
124 smallIcons->setText("Small sized icons");
125 smallIcons->setCheckable(true);
126 QObject::connect(smallIcons, SIGNAL(triggered()), parent, SLOT(smallSizedIcons()));
127 menu->addAction(smallIcons);
129 QAction *normalIcons = new QAction(iconSizeGroup);
130 normalIcons->setText("Normal sized icons");
131 normalIcons->setCheckable(true);
132 QObject::connect(normalIcons, SIGNAL(triggered()), parent, SLOT(normalSizedIcons()));
133 menu->addAction(normalIcons);
136 QAction *bigIcons = new QAction(iconSizeGroup);
137 bigIcons->setText("Big sized icons");
138 bigIcons->setCheckable(true);
139 QObject::connect(bigIcons, SIGNAL(triggered()), parent, SLOT(bigSizedIcons()));
140 menu->addAction(bigIcons);
142 unsigned int cur = parent->iconSize().width();
143 if ( cur == parent->d.smallIconSize)
144 smallIcons->setChecked(true);
145 else if (cur == parent->d.normalIconSize)
146 normalIcons->setChecked(true);
147 else if (cur == parent->d.bigIconSize)
148 bigIcons->setChecked(true);
155 unsigned int GuiView::GuiViewPrivate::lastIconSize = 0;
158 GuiView::GuiView(int id)
159 : QMainWindow(), LyXView(id), commandbuffer_(0), quitting_by_menu_(false),
160 d(*new GuiViewPrivate)
162 // Qt bug? signal lastWindowClosed does not work
163 setAttribute(Qt::WA_QuitOnClose, false);
164 setAttribute(Qt::WA_DeleteOnClose, true);
166 // hardcode here the platform specific icon size
167 d.smallIconSize = 14; // scaling problems
168 d.normalIconSize = 20; // ok, default
169 d.bigIconSize = 26; // better for some math icons
172 // assign an icon to main form. We do not do it under Qt/Mac,
173 // since the icon is provided in the application bundle.
174 FileName const iconname = libFileSearch("images", "lyx", "xpm");
175 if (!iconname.empty())
176 setWindowIcon(QPixmap(toqstr(iconname.absFilename())));
188 void GuiView::close()
190 quitting_by_menu_ = true;
191 QMainWindow::close();
192 quitting_by_menu_ = false;
196 QMenu* GuiView::createPopupMenu()
198 return d.toolBarPopup(this);
204 menubar_.reset(new QLMenubar(this, menubackend));
205 QObject::connect(menuBar(), SIGNAL(triggered(QAction *)),
206 this, SLOT(updateMenu(QAction *)));
208 getToolbars().init();
210 statusBar()->setSizeGripEnabled(false);
212 QObject::connect(&statusbar_timer_, SIGNAL(timeout()),
213 this, SLOT(update_view_state_qt()));
215 if (!work_area_->bufferView().buffer() && !theBufferList().empty())
216 setBuffer(theBufferList().first());
218 // make sure the buttons are disabled if needed
220 updateLayoutChoice();
225 void GuiView::closeEvent(QCloseEvent * close_event)
227 // we may have been called through the close window button
228 // which bypasses the LFUN machinery.
229 if (!quitting_by_menu_) {
230 if (!theBufferList().quitWriteAll()) {
231 close_event->ignore();
235 if (view()->buffer()) {
236 // save cursor position for opened files to .lyx/session
237 LyX::ref().session().lastFilePos().save(
238 FileName(buffer()->fileName()),
239 boost::tie(view()->cursor().pit(),
240 view()->cursor().pos()));
242 theApp()->gui().unregisterView(id());
243 if (theApp()->gui().viewIds().empty())
245 // this is the place where we leave the frontend.
246 // it is the only point at which we start quitting.
248 close_event->accept();
249 // quit the event loop
252 close_event->accept();
256 void GuiView::saveGeometry()
258 static bool done = false;
265 // change the ifdef to 'geometry = normalGeometry();' only
266 // when Trolltech has fixed the broken normalGeometry on X11:
267 // http://www.trolltech.com/developer/task-tracker/index_html?id=119684+&method=entry
268 // Then also the moveEvent, resizeEvent, and the
269 // code for floatingGeometry_ can be removed;
270 // adjust GuiView::setGeometry()
272 QRect geometry = normalGeometry();
274 updateFloatingGeometry();
275 QRect geometry = floatingGeometry_;
278 // save windows size and position
279 Session & session = LyX::ref().session();
280 session.sessionInfo().save("WindowWidth", convert<string>(geometry.width()));
281 session.sessionInfo().save("WindowHeight", convert<string>(geometry.height()));
282 session.sessionInfo().save("WindowIsMaximized", (isMaximized() ? "yes" : "no"));
283 session.sessionInfo().save("IconSizeXY", convert<string>(iconSize().width()));
284 if (lyxrc.geometry_xysaved) {
285 session.sessionInfo().save("WindowPosX", convert<string>(geometry.x() + d.posx_offset));
286 session.sessionInfo().save("WindowPosY", convert<string>(geometry.y() + d.posy_offset));
288 getToolbars().saveToolbarInfo();
292 void GuiView::setGeometry(unsigned int width,
296 unsigned int iconSizeXY,
297 const std::string & geometryArg)
299 // use last value (not at startup)
300 if (d.lastIconSize != 0)
301 setIconSize(d.lastIconSize);
302 else if (iconSizeXY != 0)
303 setIconSize(iconSizeXY);
305 setIconSize(d.normalIconSize);
307 // only true when the -geometry option was NOT used
308 if (width != 0 && height != 0) {
309 if (posx != -1 && posy != -1) {
310 // if there are ever startup positioning problems
311 // on a virtual desktop then check the 6 lines below
312 // http://doc.trolltech.com/4.2/qdesktopwidget.html
313 QDesktopWidget& dw = *qApp->desktop();
314 QRect desk = dw.availableGeometry(dw.primaryScreen());
315 (posx >= desk.width() ? posx = 50 : true);
316 (posy >= desk.height()? posy = 50 : true);
318 // FIXME: use setGeometry only when Trolltech has fixed the qt4/X11 bug
319 QWidget::setGeometry(posx, posy, width, height);
321 resize(width, height);
325 resize(width, height);
329 setWindowState(Qt::WindowMaximized);
333 // FIXME: move this code into parse_geometry() (lyx_main.C)
337 QRegExp re( "[=]*(?:([0-9]+)[xX]([0-9]+)){0,1}[ ]*(?:([+-][0-9]*)([+-][0-9]*)){0,1}" );
338 re.indexIn( toqstr(geometryArg.c_str()));
339 w = re.cap( 1 ).toInt();
340 h = re.cap( 2 ).toInt();
341 x = re.cap( 3 ).toInt();
342 y = re.cap( 4 ).toInt();
343 QWidget::setGeometry( x, y, w, h );
349 // For an unknown reason, the Window title update is not effective for
350 // the second windows up until it is shown on screen (Qt bug?).
353 // after show geometry() has changed (Qt bug?)
354 // we compensate the drift when storing the position
357 if (width != 0 && height != 0)
358 if (posx != -1 && posy != -1) {
360 d.posx_offset = posx - normalGeometry().x();
361 d.posy_offset = posy - normalGeometry().y();
364 d.posx_offset = posx - geometry().x();
365 d.posy_offset = posy - geometry().y();
372 void GuiView::updateMenu(QAction * /*action*/)
378 void GuiView::setWindowTitle(docstring const & t, docstring const & it)
380 QString title = windowTitle();
381 QString new_title = toqstr(t);
382 if (title != new_title) {
383 QMainWindow::setWindowTitle(new_title);
384 QMainWindow::setWindowIconText(toqstr(it));
389 void GuiView::addCommandBuffer(QToolBar * toolbar)
391 commandbuffer_ = new QCommandBuffer(this, *controlcommand_);
392 focus_command_buffer.connect(boost::bind(&GuiView::focus_command_widget, this));
393 toolbar->addWidget(commandbuffer_);
397 void GuiView::message(docstring const & str)
399 statusBar()->showMessage(toqstr(str));
400 statusbar_timer_.stop();
401 statusbar_timer_.start(statusbar_timer_value);
405 void GuiView::clearMessage()
407 update_view_state_qt();
411 void GuiView::setIconSize(unsigned int size)
413 d.lastIconSize = size;
414 QMainWindow::setIconSize(QSize(size, size));
418 void GuiView::smallSizedIcons()
420 setIconSize(d.smallIconSize);
424 void GuiView::normalSizedIcons()
426 setIconSize(d.normalIconSize);
430 void GuiView::bigSizedIcons()
432 setIconSize(d.bigIconSize);
436 void GuiView::focus_command_widget()
439 commandbuffer_->focus_command();
443 void GuiView::update_view_state_qt()
445 statusBar()->showMessage(toqstr(theLyXFunc().viewStatusMessage()));
446 statusbar_timer_.stop();
450 void GuiView::initTab(QWidget* workarea)
452 d.wt = new WidgetWithTabBar(workarea);
453 setCentralWidget(d.wt);
454 QObject::connect(d.wt->tabbar, SIGNAL(currentChanged(int)),
455 this, SLOT(currentTabChanged(int)));
459 void GuiView::updateTab()
461 QTabBar& tb = *d.wt->tabbar;
463 // update when all is done
464 tb.blockSignals(true);
466 typedef std::vector<string> Strings;
467 Strings const names = theBufferList().getFileNames();
468 size_t n_size = names.size();
471 // show tabs only when there is more
472 // than one file opened
475 for (size_t i = 0; i != n_size; i++)
476 if (d.namemap.find(names[i]) == d.namemap.end())
477 addtab.push_back(names.at(i));
480 for(size_t i = 0; i<addtab.size(); i++)
482 QString tab_name = lyx::toqstr(onlyFilename(addtab.at(i)));
483 d.namemap.insert(GuiViewPrivate::NameMapPair(addtab.at(i), tab_name));
487 // check if all names showed by the tabs
488 // are also in the current bufferlist
493 std::map<string, QString>::iterator tabit = d.namemap.begin();
494 for (;tabit != d.namemap.end(); ++tabit)
497 for (size_t i = 0; i != n_size; i++)
498 if (tabit->first == names.at(i) && notall)
501 removetab.push_back(tabit->first);
506 for(size_t i = 0; i<removetab.size(); i++)
508 if (d.namemap.find(removetab.at(i)) != d.namemap.end())
510 tabit = d.namemap.find(removetab.at(i));
511 for (int i = 0; i < tb.count(); i++)
512 if (tb.tabText(i) == tabit->second)
517 d.namemap.erase(tabit);
522 if (removetab.size() > 0 || addtab.size() > 0)
525 tabit = d.namemap.begin();
526 for (;tabit != d.namemap.end(); ++tabit)
528 QTabBar& tb = *d.wt->tabbar;
529 for (int i = 0; i < tb.count(); i++)
531 if (tb.tabText(i) == tabit->second)
533 FuncRequest func(LFUN_BUFFER_SWITCH, tabit->first);
534 d.funcmap.insert(GuiViewPrivate::FuncMapPair(i, func));
542 if (view()->buffer())
544 string cur_title = view()->buffer()->fileName();
545 if (d.namemap.find(cur_title) != d.namemap.end())
547 QString tabname = d.namemap.find(cur_title)->second;
548 for (int i = 0; i < tb.count(); i++)
549 if (tb.tabText(i) == tabname)
551 tb.setCurrentIndex(i);
557 tb.blockSignals(false);
562 void GuiView::currentTabChanged (int index)
564 std::map<int, FuncRequest>::const_iterator it = d.funcmap.find(index);
565 if (it != d.funcmap.end())
566 activated(it->second);
570 void GuiView::updateStatusBar()
572 // let the user see the explicit message
573 if (statusbar_timer_.isActive())
576 statusBar()->showMessage(toqstr(theLyXFunc().viewStatusMessage()));
580 void GuiView::activated(FuncRequest const & func)
586 bool GuiView::hasFocus() const
588 return qApp->activeWindow() == this;
592 void GuiView::updateFloatingGeometry()
595 floatingGeometry_ = QRect(x(), y(), width(), height());
599 void GuiView::resizeEvent(QResizeEvent *)
601 updateFloatingGeometry();
605 void GuiView::moveEvent(QMoveEvent *)
607 updateFloatingGeometry();
613 QMainWindow::setWindowTitle(qt_("LyX"));
615 updateFloatingGeometry();
619 void GuiView::busy(bool yes)
621 static_cast<GuiWorkArea *>(work_area_)->setUpdatesEnabled(!yes);
624 work_area_->stopBlinkingCursor();
625 QApplication::setOverrideCursor(Qt::WaitCursor);
628 work_area_->startBlinkingCursor();
629 QApplication::restoreOverrideCursor();
634 Toolbars::ToolbarPtr GuiView::makeToolbar(ToolbarBackend::Toolbar const & tbb)
636 QLToolbar * Tb = new QLToolbar(tbb, *this);
637 //static QLToolbar * lastTb = 0;
639 if (tbb.flags & ToolbarBackend::TOP) {
640 addToolBar(Qt::TopToolBarArea, Tb);
641 addToolBarBreak(Qt::TopToolBarArea);
643 if (tbb.flags & ToolbarBackend::BOTTOM) {
644 addToolBar(Qt::BottomToolBarArea, Tb);
647 // http://www.trolltech.com/developer/task-tracker/index_html?id=137015&method=entry
648 // Doesn't work because the toolbar will evtl. be hidden.
650 insertToolBarBreak(lastTb);
654 if (tbb.flags & ToolbarBackend::LEFT) {
655 addToolBar(Qt::LeftToolBarArea, Tb);
657 if (tbb.flags & ToolbarBackend::RIGHT) {
658 addToolBar(Qt::RightToolBarArea, Tb);
661 return Toolbars::ToolbarPtr(Tb);
664 } // namespace frontend
667 #include "GuiView_moc.cpp"