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 if (!work_area_->bufferView().buffer() && !theBufferList().empty())
154 setBuffer(theBufferList().first());
156 // make sure the buttons are disabled if needed
158 updateLayoutChoice();
163 void GuiView::saveGeometry()
166 // change the ifdef to 'geometry = normalGeometry();' only
167 // when Trolltech has fixed the broken normalGeometry on X11:
168 // http://www.trolltech.com/developer/task-tracker/index_html?id=119684+&method=entry
169 // Then also the moveEvent, resizeEvent, and the
170 // code for floatingGeometry_ can be removed;
171 // adjust GuiView::setGeometry()
173 QRect geometry = normalGeometry();
175 updateFloatingGeometry();
176 QRect geometry = floatingGeometry_;
179 // save windows size and position
180 Session & session = LyX::ref().session();
181 session.sessionInfo().save("WindowWidth", convert<string>(geometry.width()));
182 session.sessionInfo().save("WindowHeight", convert<string>(geometry.height()));
183 session.sessionInfo().save("WindowIsMaximized", (isMaximized() ? "yes" : "no"));
184 if (lyxrc.geometry_xysaved) {
185 session.sessionInfo().save("WindowPosX", convert<string>(geometry.x()));
186 session.sessionInfo().save("WindowPosY", convert<string>(geometry.y()));
188 getToolbars().saveToolbarInfo();
191 void GuiView::setGeometry(unsigned int width,
196 // only true when the -geometry option was NOT used
197 if (width != 0 && height != 0) {
198 if (posx != -1 && posy != -1) {
199 // if there are ever startup positioning problems
200 // on a virtual desktop then check the 6 lines below
201 // http://doc.trolltech.com/4.2/qdesktopwidget.html
202 QDesktopWidget& dw = *qApp->desktop();
203 QRect desk = dw.availableGeometry(dw.primaryScreen());
204 (posx >= desk.width() ? posx = 50 : true);
205 (posy >= desk.height()? posy = 50 : true);
207 // FIXME: use only setGeoemtry when Trolltech has
208 // fixed the qt4/X11 bug
209 QMainWindow::setGeometry(posx, posy,width, height);
211 resize(width, height);
215 resize(width, height);
219 setWindowState(Qt::WindowMaximized);
226 void GuiView::updateMenu(QAction * /*action*/)
232 void GuiView::setWindowTitle(docstring const & t, docstring const & it)
234 QMainWindow::setWindowTitle(toqstr(t));
235 QMainWindow::setWindowIconText(toqstr(it));
239 void GuiView::addCommandBuffer(QToolBar * toolbar)
241 commandbuffer_ = new QCommandBuffer(this, *controlcommand_);
242 focus_command_buffer.connect(boost::bind(&GuiView::focus_command_widget, this));
243 toolbar->addWidget(commandbuffer_);
247 void GuiView::message(docstring const & str)
249 statusBar()->showMessage(toqstr(str));
250 statusbar_timer_.stop();
251 statusbar_timer_.start(statusbar_timer_value);
255 void GuiView::clearMessage()
257 update_view_state_qt();
261 void GuiView::focus_command_widget()
264 commandbuffer_->focus_command();
268 void GuiView::update_view_state_qt()
270 statusBar()->showMessage(toqstr(theLyXFunc().viewStatusMessage()));
271 statusbar_timer_.stop();
274 void GuiView::initTab(QWidget* workarea)
276 d.wt = new WidgetWithTabBar(workarea);
277 setCentralWidget(d.wt);
278 QObject::connect(d.wt->tabbar, SIGNAL(currentChanged(int)),
279 this, SLOT(currentTabChanged(int)));
282 void GuiView::updateTab()
284 QTabBar& tb = *d.wt->tabbar;
286 // update when all is done
287 tb.blockSignals(true);
289 typedef std::vector<string> Strings;
290 Strings const names = theBufferList().getFileNames();
291 size_t n_size = names.size();
294 // show tabs only when there is more
295 // than one file opened
298 for (size_t i = 0; i != n_size; i++)
299 if (d.namemap.find(names[i]) == d.namemap.end())
300 addtab.push_back(names.at(i));
303 for(size_t i = 0; i<addtab.size(); i++)
305 QString tab_name = lyx::toqstr(onlyFilename(addtab.at(i)));
306 d.namemap.insert(GuiViewPrivate::NameMapPair(addtab.at(i), tab_name));
310 // check if all names showed by the tabs
311 // are also in the current bufferlist
316 std::map<string, QString>::iterator tabit = d.namemap.begin();
317 for (;tabit != d.namemap.end(); ++tabit)
320 for (size_t i = 0; i != n_size; i++)
321 if (tabit->first == names.at(i) && notall)
324 removetab.push_back(tabit->first);
329 for(size_t i = 0; i<removetab.size(); i++)
331 if (d.namemap.find(removetab.at(i)) != d.namemap.end())
333 tabit = d.namemap.find(removetab.at(i));
334 for (int i = 0; i < tb.count(); i++)
335 if (tb.tabText(i) == tabit->second)
340 d.namemap.erase(tabit);
345 if (removetab.size() > 0 || addtab.size() > 0)
348 tabit = d.namemap.begin();
349 for (;tabit != d.namemap.end(); ++tabit)
351 QTabBar& tb = *d.wt->tabbar;
352 for (int i = 0; i < tb.count(); i++)
354 if (tb.tabText(i) == tabit->second)
356 FuncRequest func(LFUN_BUFFER_SWITCH, tabit->first);
357 d.funcmap.insert(GuiViewPrivate::FuncMapPair(i, func));
365 if (view()->buffer())
367 string cur_title = view()->buffer()->fileName();
368 if (d.namemap.find(cur_title) != d.namemap.end())
370 QString tabname = d.namemap.find(cur_title)->second;
371 for (int i = 0; i < tb.count(); i++)
372 if (tb.tabText(i) == tabname)
374 tb.setCurrentIndex(i);
380 tb.blockSignals(false);
384 void GuiView::currentTabChanged (int index)
386 std::map<int, FuncRequest>::const_iterator it = d.funcmap.find(index);
387 if (it != d.funcmap.end())
388 activated(it->second);
392 void GuiView::updateStatusBar()
394 // let the user see the explicit message
395 if (statusbar_timer_.isActive())
398 statusBar()->showMessage(toqstr(theLyXFunc().viewStatusMessage()));
402 void GuiView::activated(FuncRequest const & func)
408 bool GuiView::hasFocus() const
410 return qApp->activeWindow() == this;
414 void GuiView::updateFloatingGeometry()
417 floatingGeometry_ = QRect(x(), y(), width(), height());
421 void GuiView::resizeEvent(QResizeEvent *)
423 updateFloatingGeometry();
427 void GuiView::moveEvent(QMoveEvent *)
429 updateFloatingGeometry();
433 void GuiView::closeEvent(QCloseEvent * close_event)
435 GuiImplementation & gui
436 = static_cast<GuiImplementation &>(theApp->gui());
438 vector<int> const & view_ids = gui.viewIds();
440 if (view_ids.size() == 1 && !theBufferList().quitWriteAll()) {
441 close_event->ignore();
446 gui.unregisterView(this);
452 QMainWindow::setWindowTitle(qt_("LyX"));
454 updateFloatingGeometry();
458 void GuiView::busy(bool yes)
460 static_cast<GuiWorkArea *>(work_area_)->setUpdatesEnabled(!yes);
463 work_area_->stopBlinkingCursor();
464 QApplication::setOverrideCursor(Qt::WaitCursor);
467 work_area_->startBlinkingCursor();
468 QApplication::restoreOverrideCursor();
473 Toolbars::ToolbarPtr GuiView::makeToolbar(ToolbarBackend::Toolbar const & tbb)
475 QLToolbar * Tb = new QLToolbar(tbb, *this);
476 //static QLToolbar * lastTb = 0;
478 if (tbb.flags & ToolbarBackend::TOP) {
479 addToolBar(Qt::TopToolBarArea, Tb);
480 addToolBarBreak(Qt::TopToolBarArea);
482 if (tbb.flags & ToolbarBackend::BOTTOM) {
483 addToolBar(Qt::BottomToolBarArea, Tb);
486 // http://www.trolltech.com/developer/task-tracker/index_html?id=137015&method=entry
487 // Doesn't work because the toolbar will evtl. be hidden.
489 insertToolBarBreak(lastTb);
493 if (tbb.flags & ToolbarBackend::LEFT) {
494 addToolBar(Qt::LeftToolBarArea, Tb);
496 if (tbb.flags & ToolbarBackend::RIGHT) {
497 addToolBar(Qt::RightToolBarArea, Tb);
500 return Toolbars::ToolbarPtr(Tb);
503 } // namespace frontend
506 #include "GuiView_moc.cpp"