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>
51 #include <QDesktopWidget>
53 #include <boost/bind.hpp>
59 using lyx::support::onlyFilename;
64 using support::libFileSearch;
70 int const statusbar_timer_value = 3000;
75 class WidgetWithTabBar : public QWidget
79 WidgetWithTabBar(QWidget* w)
82 QVBoxLayout* l = new QVBoxLayout;
90 struct GuiView::GuiViewPrivate
92 typedef std::map<int, FuncRequest> FuncMap;
93 typedef std::pair<int, FuncRequest> FuncMapPair;
94 typedef std::map<string, QString> NameMap;
95 typedef std::pair<string, QString> NameMapPair;
105 GuiView::GuiView(int id)
106 : QMainWindow(), LyXView(id), commandbuffer_(0), d(*new GuiViewPrivate)
108 setAttribute(Qt::WA_DeleteOnClose, true);
109 setAttribute(Qt::WA_QuitOnClose, true);
111 // setToolButtonStyle(Qt::ToolButtonIconOnly);
112 // setIconSize(QSize(12,12));
114 // bufferview_.reset(new BufferView(this, width, height));
117 // assign an icon to main form. We do not do it under Qt/Mac,
118 // since the icon is provided in the application bundle.
119 string const iconname = libFileSearch("images", "lyx", "xpm");
120 if (!iconname.empty())
121 setWindowIcon(QPixmap(toqstr(iconname)));
132 void GuiView::close()
134 QMainWindow::close();
140 menubar_.reset(new QLMenubar(this, menubackend));
141 QObject::connect(menuBar(), SIGNAL(triggered(QAction *)),
142 this, SLOT(updateMenu(QAction *)));
144 getToolbars().init();
146 statusBar()->setSizeGripEnabled(false);
148 QObject::connect(&statusbar_timer_, SIGNAL(timeout()),
149 this, SLOT(update_view_state_qt()));
151 // make sure the buttons are disabled if needed
153 updateLayoutChoice();
158 void GuiView::saveGeometry()
161 // change the ifdef to 'geometry = normalGeometry();' only
162 // when Trolltech has fixed the broken normalGeometry on X11:
163 // http://www.trolltech.com/developer/task-tracker/index_html?id=119684+&method=entry
164 // Then also the moveEvent, resizeEvent, and the
165 // code for floatingGeometry_ can be removed;
166 // adjust GuiView::setGeometry()
168 QRect geometry = normalGeometry();
170 updateFloatingGeometry();
171 QRect geometry = floatingGeometry_;
174 // save windows size and position
175 Session & session = LyX::ref().session();
176 session.sessionInfo().save("WindowWidth", convert<string>(geometry.width()));
177 session.sessionInfo().save("WindowHeight", convert<string>(geometry.height()));
178 session.sessionInfo().save("WindowIsMaximized", (isMaximized() ? "yes" : "no"));
179 if (lyxrc.geometry_xysaved) {
180 session.sessionInfo().save("WindowPosX", convert<string>(geometry.x()));
181 session.sessionInfo().save("WindowPosY", convert<string>(geometry.y()));
185 void GuiView::setGeometry(unsigned int width,
190 // only true when the -geometry option was NOT used
191 if (width != 0 && height != 0) {
192 if (posx != -1 && posy != -1) {
193 // if there are ever startup positioning problems
194 // on a virtual desktop then check the 6 lines below
195 // http://doc.trolltech.com/4.2/qdesktopwidget.html
196 QDesktopWidget& dw = *qApp->desktop();
197 QRect desk = dw.availableGeometry(dw.primaryScreen());
198 (posx >= desk.width() ? posx = 50 : true);
199 (posy >= desk.height()? posy = 50 : true);
201 // FIXME: use only setGeoemtry when Trolltech has
202 // fixed the qt4/X11 bug
203 QMainWindow::setGeometry(posx, posy,width, height);
205 resize(width, height);
209 resize(width, height);
213 setWindowState(Qt::WindowMaximized);
220 void GuiView::updateMenu(QAction * /*action*/)
226 void GuiView::setWindowTitle(docstring const & t, docstring const & it)
228 QMainWindow::setWindowTitle(toqstr(t));
229 QMainWindow::setWindowIconText(toqstr(it));
233 void GuiView::addCommandBuffer(QToolBar * toolbar)
235 commandbuffer_ = new QCommandBuffer(this, *controlcommand_);
236 focus_command_buffer.connect(boost::bind(&GuiView::focus_command_widget, this));
237 toolbar->addWidget(commandbuffer_);
241 void GuiView::message(docstring const & str)
243 statusBar()->showMessage(toqstr(str));
244 statusbar_timer_.stop();
245 statusbar_timer_.start(statusbar_timer_value);
249 void GuiView::clearMessage()
251 update_view_state_qt();
255 void GuiView::focus_command_widget()
258 commandbuffer_->focus_command();
262 void GuiView::update_view_state_qt()
264 statusBar()->showMessage(toqstr(theLyXFunc().viewStatusMessage()));
265 statusbar_timer_.stop();
268 void GuiView::initTab(QWidget* workarea)
270 d.wt = new WidgetWithTabBar(workarea);
271 setCentralWidget(d.wt);
272 QObject::connect(d.wt->tabbar, SIGNAL(currentChanged(int)),
273 this, SLOT(currentTabChanged(int)));
276 void GuiView::updateTab()
278 QTabBar& tb = *d.wt->tabbar;
280 // update when all is done
281 tb.blockSignals(true);
283 typedef std::vector<string> Strings;
284 Strings const names = theBufferList().getFileNames();
285 size_t n_size = names.size();
288 // show tabs only when there is more
289 // than one file opened
292 for (size_t i = 0; i != n_size; i++)
293 if (d.namemap.find(names[i]) == d.namemap.end())
294 addtab.push_back(names.at(i));
297 for(size_t i = 0; i<addtab.size(); i++)
299 QString tab_name = lyx::toqstr(onlyFilename(addtab.at(i)));
300 d.namemap.insert(GuiViewPrivate::NameMapPair(addtab.at(i), tab_name));
304 // check if all names showed by the tabs
305 // are also in the current bufferlist
310 std::map<string, QString>::iterator tabit = d.namemap.begin();
311 for (;tabit != d.namemap.end(); ++tabit)
314 for (size_t i = 0; i != n_size; i++)
315 if (tabit->first == names.at(i) && notall)
318 removetab.push_back(tabit->first);
323 for(size_t i = 0; i<removetab.size(); i++)
325 if (d.namemap.find(removetab.at(i)) != d.namemap.end())
327 tabit = d.namemap.find(removetab.at(i));
328 for (int i = 0; i < tb.count(); i++)
329 if (tb.tabText(i) == tabit->second)
334 d.namemap.erase(tabit);
339 if (removetab.size() > 0 || addtab.size() > 0)
342 tabit = d.namemap.begin();
343 for (;tabit != d.namemap.end(); ++tabit)
345 QTabBar& tb = *d.wt->tabbar;
346 for (int i = 0; i < tb.count(); i++)
348 if (tb.tabText(i) == tabit->second)
350 FuncRequest func(LFUN_BUFFER_SWITCH, tabit->first);
351 d.funcmap.insert(GuiViewPrivate::FuncMapPair(i, func));
359 if (view()->buffer())
361 string cur_title = view()->buffer()->fileName();
362 if (d.namemap.find(cur_title) != d.namemap.end())
364 QString tabname = d.namemap.find(cur_title)->second;
365 for (int i = 0; i < tb.count(); i++)
366 if (tb.tabText(i) == tabname)
368 tb.setCurrentIndex(i);
374 tb.blockSignals(false);
378 void GuiView::currentTabChanged (int index)
380 std::map<int, FuncRequest>::const_iterator it = d.funcmap.find(index);
381 if (it != d.funcmap.end())
382 activated(it->second);
386 void GuiView::updateStatusBar()
388 // let the user see the explicit message
389 if (statusbar_timer_.isActive())
392 statusBar()->showMessage(toqstr(theLyXFunc().viewStatusMessage()));
396 void GuiView::activated(FuncRequest const & func)
402 bool GuiView::hasFocus() const
404 return qApp->activeWindow() == this;
408 void GuiView::updateFloatingGeometry()
411 floatingGeometry_ = QRect(x(), y(), width(), height());
415 void GuiView::resizeEvent(QResizeEvent *)
417 updateFloatingGeometry();
421 void GuiView::moveEvent(QMoveEvent *)
423 updateFloatingGeometry();
427 void GuiView::closeEvent(QCloseEvent * close_event)
429 GuiImplementation & gui
430 = static_cast<GuiImplementation &>(theApp->gui());
432 vector<int> const & view_ids = gui.viewIds();
434 if (view_ids.size() == 1 && !theBufferList().quitWriteAll()) {
435 close_event->ignore();
440 gui.unregisterView(this);
446 QMainWindow::setWindowTitle(qt_("LyX"));
448 updateFloatingGeometry();
452 void GuiView::busy(bool yes)
454 static_cast<GuiWorkArea *>(work_area_)->setUpdatesEnabled(!yes);
457 work_area_->stopBlinkingCursor();
458 QApplication::setOverrideCursor(Qt::WaitCursor);
461 work_area_->startBlinkingCursor();
462 QApplication::restoreOverrideCursor();
467 Toolbars::ToolbarPtr GuiView::makeToolbar(ToolbarBackend::Toolbar const & tbb)
469 QLToolbar * Tb = new QLToolbar(tbb, *this);
470 static QLToolbar * lastTb = 0;
472 if (tbb.flags & ToolbarBackend::TOP) {
473 addToolBar(Qt::TopToolBarArea, Tb);
474 addToolBarBreak(Qt::TopToolBarArea);
476 if (tbb.flags & ToolbarBackend::BOTTOM) {
477 addToolBar(Qt::BottomToolBarArea, Tb);
479 // Qt bug. Doesn't work because the
480 // toolbar will evtl. be hidden.
482 insertToolBarBreak(lastTb);
486 if (tbb.flags & ToolbarBackend::LEFT) {
487 addToolBar(Qt::LeftToolBarArea, Tb);
489 if (tbb.flags & ToolbarBackend::RIGHT) {
490 addToolBar(Qt::RightToolBarArea, Tb);
493 return Toolbars::ToolbarPtr(Tb);
496 } // namespace frontend
499 #include "GuiView_moc.cpp"