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;
107 GuiView::GuiView(int id)
108 : QMainWindow(), LyXView(id), commandbuffer_(0), d(*new GuiViewPrivate)
110 setAttribute(Qt::WA_DeleteOnClose, true);
111 setAttribute(Qt::WA_QuitOnClose, true);
113 // setToolButtonStyle(Qt::ToolButtonIconOnly);
114 // setIconSize(QSize(12,12));
116 // bufferview_.reset(new BufferView(this, width, height));
119 // assign an icon to main form. We do not do it under Qt/Mac,
120 // since the icon is provided in the application bundle.
121 string const iconname = libFileSearch("images", "lyx", "xpm");
122 if (!iconname.empty())
123 setWindowIcon(QPixmap(toqstr(iconname)));
134 void GuiView::close()
136 QMainWindow::close();
142 menubar_.reset(new QLMenubar(this, menubackend));
143 QObject::connect(menuBar(), SIGNAL(triggered(QAction *)),
144 this, SLOT(updateMenu(QAction *)));
146 getToolbars().init();
148 statusBar()->setSizeGripEnabled(false);
150 QObject::connect(&statusbar_timer_, SIGNAL(timeout()),
151 this, SLOT(update_view_state_qt()));
153 // make sure the buttons are disabled if needed
155 updateLayoutChoice();
160 void GuiView::saveGeometry()
163 // change the ifdef to 'geometry = normalGeometry();' only
164 // when Trolltech has fixed the broken normalGeometry on X11:
165 // http://www.trolltech.com/developer/task-tracker/index_html?id=119684+&method=entry
166 // Then also the moveEvent, resizeEvent, and the
167 // code for floatingGeometry_ can be removed;
168 // adjust GuiView::setGeometry()
170 QRect geometry = normalGeometry();
172 updateFloatingGeometry();
173 QRect geometry = floatingGeometry_;
176 // save windows size and position
177 Session & session = LyX::ref().session();
178 session.sessionInfo().save("WindowWidth", convert<string>(geometry.width()));
179 session.sessionInfo().save("WindowHeight", convert<string>(geometry.height()));
180 session.sessionInfo().save("WindowIsMaximized", (isMaximized() ? "yes" : "no"));
181 if (lyxrc.geometry_xysaved) {
182 session.sessionInfo().save("WindowPosX", convert<string>(geometry.x()));
183 session.sessionInfo().save("WindowPosY", convert<string>(geometry.y()));
187 void GuiView::setGeometry(unsigned int width,
192 // only true when the -geometry option was NOT used
193 if (width != 0 && height != 0) {
194 if (posx != -1 && posy != -1) {
195 // if there are ever startup positioning problems
196 // on a virtual desktop then check the 6 lines below
197 // http://doc.trolltech.com/4.2/qdesktopwidget.html
198 QDesktopWidget& dw = *qApp->desktop();
199 QRect desk = dw.availableGeometry(dw.primaryScreen());
200 (posx >= desk.width() ? posx = 50 : true);
201 (posy >= desk.height()? posy = 50 : true);
203 // FIXME: use only setGeoemtry when Trolltech has
204 // fixed the qt4/X11 bug
205 QMainWindow::setGeometry(posx, posy,width, height);
207 resize(width, height);
211 resize(width, height);
215 setWindowState(Qt::WindowMaximized);
222 void GuiView::updateMenu(QAction * /*action*/)
228 void GuiView::setWindowTitle(docstring const & t, docstring const & it)
230 QMainWindow::setWindowTitle(toqstr(t));
231 QMainWindow::setWindowIconText(toqstr(it));
235 void GuiView::addCommandBuffer(QToolBar * toolbar)
237 commandbuffer_ = new QCommandBuffer(this, *controlcommand_);
238 focus_command_buffer.connect(boost::bind(&GuiView::focus_command_widget, this));
239 toolbar->addWidget(commandbuffer_);
243 void GuiView::message(docstring const & str)
245 statusBar()->showMessage(toqstr(str));
246 statusbar_timer_.stop();
247 statusbar_timer_.start(statusbar_timer_value);
251 void GuiView::clearMessage()
253 update_view_state_qt();
257 void GuiView::focus_command_widget()
260 commandbuffer_->focus_command();
264 void GuiView::update_view_state_qt()
266 statusBar()->showMessage(toqstr(theLyXFunc().viewStatusMessage()));
267 statusbar_timer_.stop();
270 void GuiView::initTab(QWidget* workarea)
272 d.wt = new WidgetWithTabBar(workarea);
273 setCentralWidget(d.wt);
274 QObject::connect(d.wt->tabbar, SIGNAL(currentChanged(int)),
275 this, SLOT(currentTabChanged(int)));
278 void GuiView::updateTab()
280 QTabBar& tb = *d.wt->tabbar;
282 // update when all is done
283 tb.blockSignals(true);
285 typedef std::vector<string> Strings;
286 Strings const names = theBufferList().getFileNames();
287 size_t n_size = names.size();
290 // show tabs only when there is more
291 // than one file opened
294 for (size_t i = 0; i != n_size; i++)
295 if (d.namemap.find(names[i]) == d.namemap.end())
296 addtab.push_back(names.at(i));
299 for(size_t i = 0; i<addtab.size(); i++)
301 QString tab_name = lyx::toqstr(onlyFilename(addtab.at(i)));
302 d.namemap.insert(GuiViewPrivate::NameMapPair(addtab.at(i), tab_name));
306 // check if all names showed by the tabs
307 // are also in the current bufferlist
312 std::map<string, QString>::iterator tabit = d.namemap.begin();
313 for (;tabit != d.namemap.end(); ++tabit)
316 for (size_t i = 0; i != n_size; i++)
317 if (tabit->first == names.at(i) && notall)
320 removetab.push_back(tabit->first);
325 for(size_t i = 0; i<removetab.size(); i++)
327 if (d.namemap.find(removetab.at(i)) != d.namemap.end())
329 tabit = d.namemap.find(removetab.at(i));
330 for (int i = 0; i < tb.count(); i++)
331 if (tb.tabText(i) == tabit->second)
336 d.namemap.erase(tabit);
341 if (removetab.size() > 0 || addtab.size() > 0)
344 tabit = d.namemap.begin();
345 for (;tabit != d.namemap.end(); ++tabit)
347 QTabBar& tb = *d.wt->tabbar;
348 for (int i = 0; i < tb.count(); i++)
350 if (tb.tabText(i) == tabit->second)
352 FuncRequest func(LFUN_BUFFER_SWITCH, tabit->first);
353 d.funcmap.insert(GuiViewPrivate::FuncMapPair(i, func));
361 if (view()->buffer())
363 string cur_title = view()->buffer()->fileName();
364 if (d.namemap.find(cur_title) != d.namemap.end())
366 QString tabname = d.namemap.find(cur_title)->second;
367 for (int i = 0; i < tb.count(); i++)
368 if (tb.tabText(i) == tabname)
370 tb.setCurrentIndex(i);
376 tb.blockSignals(false);
380 void GuiView::currentTabChanged (int index)
382 std::map<int, FuncRequest>::const_iterator it = d.funcmap.find(index);
383 if (it != d.funcmap.end())
384 activated(it->second);
388 void GuiView::updateStatusBar()
390 // let the user see the explicit message
391 if (statusbar_timer_.isActive())
394 statusBar()->showMessage(toqstr(theLyXFunc().viewStatusMessage()));
398 void GuiView::activated(FuncRequest const & func)
404 bool GuiView::hasFocus() const
406 return qApp->activeWindow() == this;
410 void GuiView::updateFloatingGeometry()
413 floatingGeometry_ = QRect(x(), y(), width(), height());
417 void GuiView::resizeEvent(QResizeEvent *)
419 updateFloatingGeometry();
423 void GuiView::moveEvent(QMoveEvent *)
425 updateFloatingGeometry();
429 void GuiView::closeEvent(QCloseEvent * close_event)
431 GuiImplementation & gui
432 = static_cast<GuiImplementation &>(theApp->gui());
434 vector<int> const & view_ids = gui.viewIds();
436 if (view_ids.size() == 1 && !theBufferList().quitWriteAll()) {
437 close_event->ignore();
442 gui.unregisterView(this);
448 QMainWindow::setWindowTitle(qt_("LyX"));
450 updateFloatingGeometry();
454 void GuiView::busy(bool yes)
456 static_cast<GuiWorkArea *>(work_area_)->setUpdatesEnabled(!yes);
459 work_area_->stopBlinkingCursor();
460 QApplication::setOverrideCursor(Qt::WaitCursor);
463 work_area_->startBlinkingCursor();
464 QApplication::restoreOverrideCursor();
469 Toolbars::ToolbarPtr GuiView::makeToolbar(ToolbarBackend::Toolbar const & tbb)
471 QLToolbar * Tb = new QLToolbar(tbb, *this);
472 static QLToolbar * lastTb = 0;
474 if (tbb.flags & ToolbarBackend::TOP) {
475 addToolBar(Qt::TopToolBarArea, Tb);
476 addToolBarBreak(Qt::TopToolBarArea);
478 if (tbb.flags & ToolbarBackend::BOTTOM) {
479 addToolBar(Qt::BottomToolBarArea, Tb);
482 // http://www.trolltech.com/developer/task-tracker/index_html?id=137015&method=entry
483 // Doesn't work because the toolbar will evtl. be hidden.
485 insertToolBarBreak(lastTb);
489 if (tbb.flags & ToolbarBackend::LEFT) {
490 addToolBar(Qt::LeftToolBarArea, Tb);
492 if (tbb.flags & ToolbarBackend::RIGHT) {
493 addToolBar(Qt::RightToolBarArea, Tb);
496 return Toolbars::ToolbarPtr(Tb);
499 } // namespace frontend
502 #include "GuiView_moc.cpp"