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;
77 class TabWidget : public QWidget
82 TabWidget(QWidget* w, bool topTabBar)
85 QVBoxLayout* layout = new QVBoxLayout;
87 layout->addWidget(tabbar);
90 tabbar->setShape(QTabBar::RoundedSouth);
92 layout->addWidget(tabbar);
100 for (int i = tabbar->count() - 1; i >= 0; --i)
101 tabbar->removeTab(i);
108 struct GuiView::GuiViewPrivate
110 std::vector<std::string> tabnames;
112 TabWidget* tabWidget;
117 GuiViewPrivate() : tabWidget(0), posx_offset(0), posy_offset(0)
120 unsigned int smallIconSize;
121 unsigned int normalIconSize;
122 unsigned int bigIconSize;
123 // static needed by "New Window"
124 static unsigned int lastIconSize;
126 QMenu* toolBarPopup(GuiView *parent)
128 // FIXME: translation
129 QMenu* menu = new QMenu(parent);
130 QActionGroup *iconSizeGroup = new QActionGroup(parent);
132 QAction *smallIcons = new QAction(iconSizeGroup);
133 smallIcons->setText(qt_("Small-sized icons"));
134 smallIcons->setCheckable(true);
135 QObject::connect(smallIcons, SIGNAL(triggered()), parent, SLOT(smallSizedIcons()));
136 menu->addAction(smallIcons);
138 QAction *normalIcons = new QAction(iconSizeGroup);
139 normalIcons->setText(qt_("Normal-sized icons"));
140 normalIcons->setCheckable(true);
141 QObject::connect(normalIcons, SIGNAL(triggered()), parent, SLOT(normalSizedIcons()));
142 menu->addAction(normalIcons);
145 QAction *bigIcons = new QAction(iconSizeGroup);
146 bigIcons->setText(qt_("Big-sized icons"));
147 bigIcons->setCheckable(true);
148 QObject::connect(bigIcons, SIGNAL(triggered()), parent, SLOT(bigSizedIcons()));
149 menu->addAction(bigIcons);
151 unsigned int cur = parent->iconSize().width();
152 if ( cur == parent->d.smallIconSize)
153 smallIcons->setChecked(true);
154 else if (cur == parent->d.normalIconSize)
155 normalIcons->setChecked(true);
156 else if (cur == parent->d.bigIconSize)
157 bigIcons->setChecked(true);
164 unsigned int GuiView::GuiViewPrivate::lastIconSize = 0;
167 GuiView::GuiView(int id)
168 : QMainWindow(), LyXView(id), commandbuffer_(0), quitting_by_menu_(false),
169 d(*new GuiViewPrivate)
171 // Qt bug? signal lastWindowClosed does not work
172 setAttribute(Qt::WA_QuitOnClose, false);
173 setAttribute(Qt::WA_DeleteOnClose, true);
175 // hardcode here the platform specific icon size
176 d.smallIconSize = 14; // scaling problems
177 d.normalIconSize = 20; // ok, default
178 d.bigIconSize = 26; // better for some math icons
181 // assign an icon to main form. We do not do it under Qt/Mac,
182 // since the icon is provided in the application bundle.
183 FileName const iconname = libFileSearch("images", "lyx", "xpm");
184 if (!iconname.empty())
185 setWindowIcon(QPixmap(toqstr(iconname.absFilename())));
197 void GuiView::close()
199 quitting_by_menu_ = true;
200 QMainWindow::close();
201 quitting_by_menu_ = false;
205 void GuiView::setFocus()
207 BOOST_ASSERT(work_area_);
208 static_cast<GuiWorkArea *>(work_area_)->setFocus();
212 QMenu* GuiView::createPopupMenu()
214 return d.toolBarPopup(this);
220 menubar_.reset(new QLMenubar(this, menubackend));
221 QObject::connect(menuBar(), SIGNAL(triggered(QAction *)),
222 this, SLOT(updateMenu(QAction *)));
224 getToolbars().init();
226 statusBar()->setSizeGripEnabled(true);
228 QObject::connect(&statusbar_timer_, SIGNAL(timeout()),
229 this, SLOT(update_view_state_qt()));
231 BOOST_ASSERT(work_area_);
232 if (!work_area_->bufferView().buffer() && !theBufferList().empty())
233 setBuffer(theBufferList().first());
235 // make sure the buttons are disabled if needed
237 updateLayoutChoice();
242 void GuiView::closeEvent(QCloseEvent * close_event)
244 // we may have been called through the close window button
245 // which bypasses the LFUN machinery.
246 if (!quitting_by_menu_ && theApp()->gui().viewIds().size() == 1) {
247 if (!theBufferList().quitWriteAll()) {
248 close_event->ignore();
253 theApp()->gui().unregisterView(id());
254 if (!theApp()->gui().viewIds().empty()) {
255 // Just close the window and do nothing else if this is not the
257 close_event->accept();
261 if (view()->buffer()) {
262 // save cursor position for opened files to .lyx/session
263 LyX::ref().session().lastFilePos().save(
264 FileName(buffer()->fileName()),
265 boost::tie(view()->cursor().pit(),
266 view()->cursor().pos()));
269 // this is the place where we leave the frontend.
270 // it is the only point at which we start quitting.
272 close_event->accept();
273 // quit the event loop
278 void GuiView::saveGeometry()
280 static bool done = false;
287 // change the ifdef to 'geometry = normalGeometry();' only
288 // when Trolltech has fixed the broken normalGeometry on X11:
289 // http://www.trolltech.com/developer/task-tracker/index_html?id=119684+&method=entry
290 // Then also the moveEvent, resizeEvent, and the
291 // code for floatingGeometry_ can be removed;
292 // adjust GuiView::setGeometry()
294 QRect geometry = normalGeometry();
296 updateFloatingGeometry();
297 QRect geometry = floatingGeometry_;
300 // save windows size and position
301 Session & session = LyX::ref().session();
302 session.sessionInfo().save("WindowWidth", convert<string>(geometry.width()));
303 session.sessionInfo().save("WindowHeight", convert<string>(geometry.height()));
304 session.sessionInfo().save("WindowIsMaximized", (isMaximized() ? "yes" : "no"));
305 session.sessionInfo().save("IconSizeXY", convert<string>(iconSize().width()));
306 if (lyxrc.geometry_xysaved) {
307 session.sessionInfo().save("WindowPosX", convert<string>(geometry.x() + d.posx_offset));
308 session.sessionInfo().save("WindowPosY", convert<string>(geometry.y() + d.posy_offset));
310 getToolbars().saveToolbarInfo();
314 void GuiView::setGeometry(unsigned int width,
318 unsigned int iconSizeXY,
319 const string & geometryArg)
321 // use last value (not at startup)
322 if (d.lastIconSize != 0)
323 setIconSize(d.lastIconSize);
324 else if (iconSizeXY != 0)
325 setIconSize(iconSizeXY);
327 setIconSize(d.normalIconSize);
329 // only true when the -geometry option was NOT used
330 if (width != 0 && height != 0) {
331 if (posx != -1 && posy != -1) {
332 // if there are ever startup positioning problems
333 // on a virtual desktop then check the 6 lines below
334 // http://doc.trolltech.com/4.2/qdesktopwidget.html
335 QDesktopWidget& dw = *qApp->desktop();
336 QRect desk = dw.availableGeometry(dw.primaryScreen());
337 (posx >= desk.width() ? posx = 50 : true);
338 (posy >= desk.height()? posy = 50 : true);
340 // FIXME: use setGeometry only when Trolltech has fixed the qt4/X11 bug
341 QWidget::setGeometry(posx, posy, width, height);
343 resize(width, height);
347 resize(width, height);
351 setWindowState(Qt::WindowMaximized);
355 // FIXME: move this code into parse_geometry() (lyx_main.C)
359 QRegExp re( "[=]*(?:([0-9]+)[xX]([0-9]+)){0,1}[ ]*(?:([+-][0-9]*)([+-][0-9]*)){0,1}" );
360 re.indexIn(toqstr(geometryArg.c_str()));
361 w = re.cap(1).toInt();
362 h = re.cap(2).toInt();
363 x = re.cap(3).toInt();
364 y = re.cap(4).toInt();
365 QWidget::setGeometry( x, y, w, h );
374 // For an unknown reason, the Window title update is not effective for
375 // the second windows up until it is shown on screen (Qt bug?).
378 // after show geometry() has changed (Qt bug?)
379 // we compensate the drift when storing the position
382 if (width != 0 && height != 0)
383 if (posx != -1 && posy != -1) {
385 d.posx_offset = posx - normalGeometry().x();
386 d.posy_offset = posy - normalGeometry().y();
390 d.posx_offset = posx - geometry().x();
391 d.posy_offset = posy - geometry().y();
399 void GuiView::updateMenu(QAction * /*action*/)
405 void GuiView::setWindowTitle(docstring const & t, docstring const & it)
407 QString title = windowTitle();
408 QString new_title = toqstr(t);
409 if (title != new_title) {
410 QMainWindow::setWindowTitle(new_title);
411 QMainWindow::setWindowIconText(toqstr(it));
416 void GuiView::addCommandBuffer(QToolBar * toolbar)
418 commandbuffer_ = new QCommandBuffer(this, *controlcommand_);
419 focus_command_buffer.connect(boost::bind(&GuiView::focus_command_widget, this));
420 toolbar->addWidget(commandbuffer_);
424 void GuiView::message(docstring const & str)
426 statusBar()->showMessage(toqstr(str));
427 statusbar_timer_.stop();
428 statusbar_timer_.start(statusbar_timer_value);
432 void GuiView::clearMessage()
434 update_view_state_qt();
438 void GuiView::setIconSize(unsigned int size)
440 d.lastIconSize = size;
441 QMainWindow::setIconSize(QSize(size, size));
445 void GuiView::smallSizedIcons()
447 setIconSize(d.smallIconSize);
451 void GuiView::normalSizedIcons()
453 setIconSize(d.normalIconSize);
457 void GuiView::bigSizedIcons()
459 setIconSize(d.bigIconSize);
463 void GuiView::focus_command_widget()
466 commandbuffer_->focus_command();
470 void GuiView::update_view_state_qt()
472 statusBar()->showMessage(toqstr(theLyXFunc().viewStatusMessage()));
473 statusbar_timer_.stop();
477 void GuiView::initTab(QWidget* workarea)
479 // construct the TabWidget with 'false' to have the tabbar at the bottom
480 d.tabWidget = new TabWidget(workarea, true);
481 setCentralWidget(d.tabWidget);
482 QObject::connect(d.tabWidget->tabbar, SIGNAL(currentChanged(int)),
483 this, SLOT(currentTabChanged(int)));
487 void GuiView::updateTab()
489 std::vector<string> const & names = theBufferList().getFileNames();
491 // avoid unnecessary tabbar rebuild:
492 // check if something has changed
493 if (d.tabnames == names)
497 QTabBar & tabbar = *d.tabWidget->tabbar;
499 // update when all is done
500 tabbar.blockSignals(true);
502 // remove all tab bars
503 d.tabWidget->clearTabbar();
506 if (view()->buffer()) {
507 cur_title = view()->buffer()->fileName();
510 // rebuild tabbar and function map from scratch
511 if (names.size() > 1) {
512 for(size_t i = 0; i < names.size(); i++) {
513 tabbar.addTab(toqstr(onlyFilename(names[i])));
515 if (names[i] == cur_title)
516 tabbar.setCurrentIndex(i);
519 tabbar.blockSignals(false);
523 void GuiView::currentTabChanged(int i)
525 BOOST_ASSERT(i >= 0 && size_type(i) < d.tabnames.size());
526 dispatch(FuncRequest(LFUN_BUFFER_SWITCH, d.tabnames[i]));
530 void GuiView::updateStatusBar()
532 // let the user see the explicit message
533 if (statusbar_timer_.isActive())
536 statusBar()->showMessage(toqstr(theLyXFunc().viewStatusMessage()));
540 void GuiView::activated(FuncRequest const & func)
546 bool GuiView::hasFocus() const
548 return qApp->activeWindow() == this;
552 void GuiView::updateFloatingGeometry()
555 floatingGeometry_ = QRect(x(), y(), width(), height());
559 void GuiView::resizeEvent(QResizeEvent *)
561 updateFloatingGeometry();
565 void GuiView::moveEvent(QMoveEvent *)
567 updateFloatingGeometry();
571 bool GuiView::event(QEvent * e)
573 // Useful debug code:
577 case QEvent::WindowActivate:
578 case QEvent::ActivationChange:
579 case QEvent::WindowDeactivate:
583 case QEvent::HoverEnter:
584 case QEvent::HoverLeave:
585 case QEvent::HoverMove:
586 case QEvent::StatusTip:
591 if (e->type() == QEvent::ShortcutOverride) {
592 QKeyEvent * ke = static_cast<QKeyEvent*>(e);
593 if (ke->key() == Qt::Key_Tab || ke->key() == Qt::Key_Backtab) {
594 boost::shared_ptr<QLyXKeySym> sym(new QLyXKeySym);
596 BOOST_ASSERT(work_area_);
597 work_area_->processKeySym(sym, key_modifier::none);
602 //} for the debug switch above.
604 return QMainWindow::event(e);
608 bool GuiView::focusNextPrevChild(bool /*next*/)
617 QMainWindow::setWindowTitle(qt_("LyX"));
619 updateFloatingGeometry();
623 void GuiView::busy(bool yes)
625 BOOST_ASSERT(work_area_);
626 static_cast<GuiWorkArea *>(work_area_)->setUpdatesEnabled(!yes);
629 work_area_->stopBlinkingCursor();
630 QApplication::setOverrideCursor(Qt::WaitCursor);
633 work_area_->startBlinkingCursor();
634 QApplication::restoreOverrideCursor();
639 Toolbars::ToolbarPtr GuiView::makeToolbar(ToolbarBackend::Toolbar const & tbb, bool newline)
641 QLToolbar * Tb = new QLToolbar(tbb, *this);
643 if (tbb.flags & ToolbarBackend::TOP) {
645 addToolBarBreak(Qt::TopToolBarArea);
646 addToolBar(Qt::TopToolBarArea, Tb);
649 if (tbb.flags & ToolbarBackend::BOTTOM) {
650 // Qt < 4.2.2 cannot handle ToolBarBreak on non-TOP dock.
651 #if (QT_VERSION >= 0x040202)
653 addToolBarBreak(Qt::BottomToolBarArea);
655 addToolBar(Qt::BottomToolBarArea, Tb);
658 if (tbb.flags & ToolbarBackend::LEFT) {
659 // Qt < 4.2.2 cannot handle ToolBarBreak on non-TOP dock.
660 #if (QT_VERSION >= 0x040202)
662 addToolBarBreak(Qt::LeftToolBarArea);
664 addToolBar(Qt::LeftToolBarArea, Tb);
667 if (tbb.flags & ToolbarBackend::RIGHT) {
668 // Qt < 4.2.2 cannot handle ToolBarBreak on non-TOP dock.
669 #if (QT_VERSION >= 0x040202)
671 addToolBarBreak(Qt::RightToolBarArea);
673 addToolBar(Qt::RightToolBarArea, Tb);
676 // The following does not work so I can not restore to exact toolbar location
678 ToolbarSection::ToolbarInfo & info = LyX::ref().session().toolbars().load(tbb.name);
679 Tb->move(info.posx, info.posy);
682 return Toolbars::ToolbarPtr(Tb);
685 } // namespace frontend
688 #include "GuiView_moc.cpp"