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
10 * Full author contact details are available in file CREDITS.
17 #include "GuiImplementation.h"
18 #include "GuiWorkArea.h"
19 #include "QLMenubar.h"
20 #include "QLToolbar.h"
21 #include "QCommandBuffer.h"
22 #include "qt_helpers.h"
24 #include "frontends/Application.h"
25 #include "frontends/Gui.h"
26 #include "frontends/WorkArea.h"
28 #include "support/filetools.h"
29 #include "support/convert.h"
30 #include "support/lstrings.h"
32 #include "BufferView.h"
33 #include "bufferlist.h"
35 #include "funcrequest.h"
41 #include "MenuBackend.h"
43 #include "bufferlist.h"
46 #include <QApplication>
47 #include <QCloseEvent>
52 #include <QDesktopWidget>
53 #include <QVBoxLayout>
55 #include <boost/bind.hpp>
61 using lyx::support::onlyFilename;
66 using support::libFileSearch;
72 int const statusbar_timer_value = 3000;
77 class WidgetWithTabBar : public QWidget
81 WidgetWithTabBar(QWidget* w)
84 QVBoxLayout* l = new QVBoxLayout;
92 struct GuiView::GuiViewPrivate
94 typedef std::map<int, FuncRequest> FuncMap;
95 typedef std::pair<int, FuncRequest> FuncMapPair;
96 typedef std::map<string, QString> NameMap;
97 typedef std::pair<string, QString> NameMapPair;
101 WidgetWithTabBar* wt;
106 GuiViewPrivate() : wt(0), posx_offset(0), posy_offset(0)
110 GuiView::GuiView(int id)
111 : QMainWindow(), LyXView(id), commandbuffer_(0), d(*new GuiViewPrivate)
113 setAttribute(Qt::WA_DeleteOnClose, true);
114 setAttribute(Qt::WA_QuitOnClose, true);
116 // setToolButtonStyle(Qt::ToolButtonIconOnly);
117 // setIconSize(QSize(12,12));
119 // bufferview_.reset(new BufferView(this, width, height));
122 // assign an icon to main form. We do not do it under Qt/Mac,
123 // since the icon is provided in the application bundle.
124 string const iconname = libFileSearch("images", "lyx", "xpm");
125 if (!iconname.empty())
126 setWindowIcon(QPixmap(toqstr(iconname)));
137 void GuiView::close()
139 QMainWindow::close();
142 QMenu* GuiView::createPopupMenu()
144 // disable toolbar popup menu
145 // Qt docs: Ownership of the popup menu is transferred to the caller.
151 menubar_.reset(new QLMenubar(this, menubackend));
152 QObject::connect(menuBar(), SIGNAL(triggered(QAction *)),
153 this, SLOT(updateMenu(QAction *)));
155 getToolbars().init();
157 statusBar()->setSizeGripEnabled(false);
159 QObject::connect(&statusbar_timer_, SIGNAL(timeout()),
160 this, SLOT(update_view_state_qt()));
162 if (!work_area_->bufferView().buffer() && !theBufferList().empty())
163 setBuffer(theBufferList().first());
165 // make sure the buttons are disabled if needed
167 updateLayoutChoice();
172 void GuiView::saveGeometry()
175 // change the ifdef to 'geometry = normalGeometry();' only
176 // when Trolltech has fixed the broken normalGeometry on X11:
177 // http://www.trolltech.com/developer/task-tracker/index_html?id=119684+&method=entry
178 // Then also the moveEvent, resizeEvent, and the
179 // code for floatingGeometry_ can be removed;
180 // adjust GuiView::setGeometry()
182 QRect geometry = normalGeometry();
184 updateFloatingGeometry();
185 QRect geometry = floatingGeometry_;
188 // save windows size and position
189 Session & session = LyX::ref().session();
190 session.sessionInfo().save("WindowWidth", convert<string>(geometry.width()));
191 session.sessionInfo().save("WindowHeight", convert<string>(geometry.height()));
192 session.sessionInfo().save("WindowIsMaximized", (isMaximized() ? "yes" : "no"));
193 if (lyxrc.geometry_xysaved) {
194 session.sessionInfo().save("WindowPosX", convert<string>(geometry.x() + d.posx_offset));
195 session.sessionInfo().save("WindowPosY", convert<string>(geometry.y() + d.posy_offset));
197 getToolbars().saveToolbarInfo();
200 void GuiView::setGeometry(unsigned int width,
205 // only true when the -geometry option was NOT used
206 if (width != 0 && height != 0) {
207 if (posx != -1 && posy != -1) {
208 // if there are ever startup positioning problems
209 // on a virtual desktop then check the 6 lines below
210 // http://doc.trolltech.com/4.2/qdesktopwidget.html
211 QDesktopWidget& dw = *qApp->desktop();
212 QRect desk = dw.availableGeometry(dw.primaryScreen());
213 (posx >= desk.width() ? posx = 50 : true);
214 (posy >= desk.height()? posy = 50 : true);
216 // FIXME: use setGeometry only when Trolltech has fixed the qt4/X11 bug
217 QWidget::setGeometry(posx, posy, width, height);
219 resize(width, height);
223 resize(width, height);
227 setWindowState(Qt::WindowMaximized);
232 // For an unknown reason, the Window title update is not effective for
233 // the second windows up until it is shown on screen (Qt bug?).
236 // after show geometry() has changed (Qt bug?)
237 // we compensate the drift when storing the position
240 if (width != 0 && height != 0)
241 if (posx != -1 && posy != -1) {
243 d.posx_offset = posx - normalGeometry().x();
244 d.posy_offset = posy - normalGeometry().y();
247 d.posx_offset = posx - geometry().x();
248 d.posy_offset = posy - geometry().y();
255 void GuiView::updateMenu(QAction * /*action*/)
261 void GuiView::setWindowTitle(docstring const & t, docstring const & it)
263 QString title = windowTitle();
264 QString new_title = toqstr(t);
265 if (title != new_title) {
266 QMainWindow::setWindowTitle(new_title);
267 QMainWindow::setWindowIconText(toqstr(it));
272 void GuiView::addCommandBuffer(QToolBar * toolbar)
274 commandbuffer_ = new QCommandBuffer(this, *controlcommand_);
275 focus_command_buffer.connect(boost::bind(&GuiView::focus_command_widget, this));
276 toolbar->addWidget(commandbuffer_);
280 void GuiView::message(docstring const & str)
282 statusBar()->showMessage(toqstr(str));
283 statusbar_timer_.stop();
284 statusbar_timer_.start(statusbar_timer_value);
288 void GuiView::clearMessage()
290 update_view_state_qt();
294 void GuiView::focus_command_widget()
297 commandbuffer_->focus_command();
301 void GuiView::update_view_state_qt()
303 statusBar()->showMessage(toqstr(theLyXFunc().viewStatusMessage()));
304 statusbar_timer_.stop();
307 void GuiView::initTab(QWidget* workarea)
309 d.wt = new WidgetWithTabBar(workarea);
310 setCentralWidget(d.wt);
311 QObject::connect(d.wt->tabbar, SIGNAL(currentChanged(int)),
312 this, SLOT(currentTabChanged(int)));
315 void GuiView::updateTab()
317 QTabBar& tb = *d.wt->tabbar;
319 // update when all is done
320 tb.blockSignals(true);
322 typedef std::vector<string> Strings;
323 Strings const names = theBufferList().getFileNames();
324 size_t n_size = names.size();
327 // show tabs only when there is more
328 // than one file opened
331 for (size_t i = 0; i != n_size; i++)
332 if (d.namemap.find(names[i]) == d.namemap.end())
333 addtab.push_back(names.at(i));
336 for(size_t i = 0; i<addtab.size(); i++)
338 QString tab_name = lyx::toqstr(onlyFilename(addtab.at(i)));
339 d.namemap.insert(GuiViewPrivate::NameMapPair(addtab.at(i), tab_name));
343 // check if all names showed by the tabs
344 // are also in the current bufferlist
349 std::map<string, QString>::iterator tabit = d.namemap.begin();
350 for (;tabit != d.namemap.end(); ++tabit)
353 for (size_t i = 0; i != n_size; i++)
354 if (tabit->first == names.at(i) && notall)
357 removetab.push_back(tabit->first);
362 for(size_t i = 0; i<removetab.size(); i++)
364 if (d.namemap.find(removetab.at(i)) != d.namemap.end())
366 tabit = d.namemap.find(removetab.at(i));
367 for (int i = 0; i < tb.count(); i++)
368 if (tb.tabText(i) == tabit->second)
373 d.namemap.erase(tabit);
378 if (removetab.size() > 0 || addtab.size() > 0)
381 tabit = d.namemap.begin();
382 for (;tabit != d.namemap.end(); ++tabit)
384 QTabBar& tb = *d.wt->tabbar;
385 for (int i = 0; i < tb.count(); i++)
387 if (tb.tabText(i) == tabit->second)
389 FuncRequest func(LFUN_BUFFER_SWITCH, tabit->first);
390 d.funcmap.insert(GuiViewPrivate::FuncMapPair(i, func));
398 if (view()->buffer())
400 string cur_title = view()->buffer()->fileName();
401 if (d.namemap.find(cur_title) != d.namemap.end())
403 QString tabname = d.namemap.find(cur_title)->second;
404 for (int i = 0; i < tb.count(); i++)
405 if (tb.tabText(i) == tabname)
407 tb.setCurrentIndex(i);
413 tb.blockSignals(false);
417 void GuiView::currentTabChanged (int index)
419 std::map<int, FuncRequest>::const_iterator it = d.funcmap.find(index);
420 if (it != d.funcmap.end())
421 activated(it->second);
425 void GuiView::updateStatusBar()
427 // let the user see the explicit message
428 if (statusbar_timer_.isActive())
431 statusBar()->showMessage(toqstr(theLyXFunc().viewStatusMessage()));
435 void GuiView::activated(FuncRequest const & func)
441 bool GuiView::hasFocus() const
443 return qApp->activeWindow() == this;
447 void GuiView::updateFloatingGeometry()
450 floatingGeometry_ = QRect(x(), y(), width(), height());
454 void GuiView::resizeEvent(QResizeEvent *)
456 updateFloatingGeometry();
460 void GuiView::moveEvent(QMoveEvent *)
462 updateFloatingGeometry();
466 void GuiView::closeEvent(QCloseEvent * close_event)
468 GuiImplementation & gui
469 = static_cast<GuiImplementation &>(theApp->gui());
471 vector<int> const & view_ids = gui.viewIds();
473 if (view_ids.size() == 1 && !theBufferList().quitWriteAll()) {
474 close_event->ignore();
479 gui.unregisterView(this);
485 QMainWindow::setWindowTitle(qt_("LyX"));
487 updateFloatingGeometry();
491 void GuiView::busy(bool yes)
493 static_cast<GuiWorkArea *>(work_area_)->setUpdatesEnabled(!yes);
496 work_area_->stopBlinkingCursor();
497 QApplication::setOverrideCursor(Qt::WaitCursor);
500 work_area_->startBlinkingCursor();
501 QApplication::restoreOverrideCursor();
506 Toolbars::ToolbarPtr GuiView::makeToolbar(ToolbarBackend::Toolbar const & tbb)
508 QLToolbar * Tb = new QLToolbar(tbb, *this);
509 //static QLToolbar * lastTb = 0;
511 if (tbb.flags & ToolbarBackend::TOP) {
512 addToolBar(Qt::TopToolBarArea, Tb);
513 addToolBarBreak(Qt::TopToolBarArea);
515 if (tbb.flags & ToolbarBackend::BOTTOM) {
516 addToolBar(Qt::BottomToolBarArea, Tb);
519 // http://www.trolltech.com/developer/task-tracker/index_html?id=137015&method=entry
520 // Doesn't work because the toolbar will evtl. be hidden.
522 insertToolBarBreak(lastTb);
526 if (tbb.flags & ToolbarBackend::LEFT) {
527 addToolBar(Qt::LeftToolBarArea, Tb);
529 if (tbb.flags & ToolbarBackend::RIGHT) {
530 addToolBar(Qt::RightToolBarArea, Tb);
533 return Toolbars::ToolbarPtr(Tb);
536 } // namespace frontend
539 #include "GuiView_moc.cpp"