]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/GuiView.C
* QLMenubar.C (macxMenuBarInit): fix workaround for menu merging
[lyx.git] / src / frontends / qt4 / GuiView.C
1 /**
2  * \file GuiView.C
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Lars Gullik Bjønnes
7  * \author John Levon
8  * \author Abdelrazak Younes
9  * \author Peter Kümmel
10  *
11  * Full author contact details are available in file CREDITS.
12  */
13
14 #include <config.h>
15
16 #include "GuiView.h"
17
18 #include "GuiImplementation.h"
19 #include "GuiWorkArea.h"
20 #include "QLMenubar.h"
21 #include "QLToolbar.h"
22 #include "QCommandBuffer.h"
23 #include "qt_helpers.h"
24
25 #include "frontends/Application.h"
26 #include "frontends/Gui.h"
27 #include "frontends/WorkArea.h"
28
29 #include "support/filetools.h"
30 #include "support/convert.h"
31 #include "support/lstrings.h"
32
33 #include "BufferView.h"
34 #include "bufferlist.h"
35 #include "debug.h"
36 #include "funcrequest.h"
37 #include "lyx_cb.h"
38 #include "lyxrc.h"
39 #include "lyx_main.h"
40 #include "session.h"
41 #include "lyxfunc.h"
42 #include "MenuBackend.h"
43 #include "buffer.h"
44 #include "bufferlist.h"
45
46 #include <QAction>
47 #include <QApplication>
48 #include <QCloseEvent>
49 #include <QPixmap>
50 #include <QStatusBar>
51 #include <QToolBar>
52 #include <QTabBar>
53 #include <QDesktopWidget>
54 #include <QVBoxLayout>
55
56 #include <boost/bind.hpp>
57
58 using std::endl;
59 using std::string;
60 using std::vector;
61
62 namespace lyx {
63
64 using support::FileName;
65 using support::onlyFilename;
66 using support::subst;
67 using support::libFileSearch;
68
69 namespace frontend {
70
71 namespace {
72
73 int const statusbar_timer_value = 3000;
74
75 } // namespace anon
76
77
78 class WidgetWithTabBar : public QWidget
79 {
80 public:
81         QTabBar* tabbar;
82         WidgetWithTabBar(QWidget* w)
83         {
84                 tabbar = new QTabBar;
85                 QVBoxLayout* l = new QVBoxLayout;
86                 l->addWidget(tabbar);
87                 l->addWidget(w);
88                 l->setMargin(0);
89                 setLayout(l);
90         }
91 };
92
93
94 struct GuiView::GuiViewPrivate
95 {
96         typedef std::map<int, FuncRequest> FuncMap;
97         typedef std::pair<int, FuncRequest> FuncMapPair;
98         typedef std::map<string, QString> NameMap;
99         typedef std::pair<string, QString> NameMapPair;
100
101         FuncMap funcmap;
102         NameMap namemap;
103         WidgetWithTabBar* wt;
104
105         int posx_offset;
106         int posy_offset;
107
108         GuiViewPrivate() : wt(0), posx_offset(0), posy_offset(0)
109         {}
110
111         unsigned int smallIconSize;
112         unsigned int normalIconSize;
113         unsigned int bigIconSize;
114         // static needed by "New Window"
115         static unsigned int lastIconSize;
116
117         QMenu* toolBarPopup(GuiView *parent)
118         {
119                 // FIXME: translation 
120                 QMenu* menu = new QMenu(parent);
121                 QActionGroup *iconSizeGroup = new QActionGroup(parent);
122
123                 QAction *smallIcons = new QAction(iconSizeGroup);
124                 smallIcons->setText("Small sized icons");
125                 smallIcons->setCheckable(true);
126                 QObject::connect(smallIcons, SIGNAL(triggered()), parent, SLOT(smallSizedIcons()));
127                 menu->addAction(smallIcons);
128
129                 QAction *normalIcons = new QAction(iconSizeGroup);
130                 normalIcons->setText("Normal sized icons");
131                 normalIcons->setCheckable(true);
132                 QObject::connect(normalIcons, SIGNAL(triggered()), parent, SLOT(normalSizedIcons()));
133                 menu->addAction(normalIcons);
134
135
136                 QAction *bigIcons = new QAction(iconSizeGroup);
137                 bigIcons->setText("Big sized icons");
138                 bigIcons->setCheckable(true);
139                 QObject::connect(bigIcons, SIGNAL(triggered()), parent, SLOT(bigSizedIcons()));
140                 menu->addAction(bigIcons);
141
142                 unsigned int cur = parent->iconSize().width();
143                 if ( cur == parent->d.smallIconSize)
144                         smallIcons->setChecked(true);
145                 else if (cur == parent->d.normalIconSize)
146                         normalIcons->setChecked(true);
147                 else if (cur == parent->d.bigIconSize)
148                         bigIcons->setChecked(true);
149
150                 return menu;
151         }
152 };
153
154
155 unsigned int GuiView::GuiViewPrivate::lastIconSize = 0;
156
157
158 GuiView::GuiView(int id)
159         : QMainWindow(), LyXView(id), commandbuffer_(0), quitting_by_menu_(false),
160           d(*new GuiViewPrivate)
161 {
162         // Qt bug? signal lastWindowClosed does not work
163         setAttribute(Qt::WA_QuitOnClose, false);
164         setAttribute(Qt::WA_DeleteOnClose, true);
165
166         // hardcode here the platform specific icon size
167         d.smallIconSize = 14;   // scaling problems
168         d.normalIconSize = 20;  // ok, default
169         d.bigIconSize = 26;             // better for some math icons
170
171 #ifndef Q_WS_MACX
172         //  assign an icon to main form. We do not do it under Qt/Mac,
173         //  since the icon is provided in the application bundle.
174         FileName const iconname = libFileSearch("images", "lyx", "xpm");
175         if (!iconname.empty())
176                 setWindowIcon(QPixmap(toqstr(iconname.absFilename())));
177 #endif
178 }
179
180
181 GuiView::~GuiView()
182 {
183         menubar_.reset();
184         delete &d;
185 }
186
187
188 void GuiView::close()
189 {
190         quitting_by_menu_ = true;
191         QMainWindow::close();
192         quitting_by_menu_ = false;
193 }
194
195
196 QMenu* GuiView::createPopupMenu()
197 {
198         return d.toolBarPopup(this);
199 }
200
201
202 void GuiView::init()
203 {
204         menubar_.reset(new QLMenubar(this, menubackend));
205         QObject::connect(menuBar(), SIGNAL(triggered(QAction *)),
206                 this, SLOT(updateMenu(QAction *)));
207
208         getToolbars().init();
209
210         statusBar()->setSizeGripEnabled(false);
211
212         QObject::connect(&statusbar_timer_, SIGNAL(timeout()),
213                 this, SLOT(update_view_state_qt()));
214
215         if (!work_area_->bufferView().buffer() && !theBufferList().empty())
216                 setBuffer(theBufferList().first());
217
218         // make sure the buttons are disabled if needed
219         updateToolbars();
220         updateLayoutChoice();
221         updateMenubar();
222 }
223
224
225 void GuiView::closeEvent(QCloseEvent * close_event)
226 {
227         // we may have been called through the close window button
228         // which bypasses the LFUN machinery.
229         if (!quitting_by_menu_) {
230                 if (!theBufferList().quitWriteAll()) {
231                         close_event->ignore();
232                         return;
233                 }
234         }
235         if (view()->buffer()) {
236                 // save cursor position for opened files to .lyx/session
237                 LyX::ref().session().lastFilePos().save(
238                         FileName(buffer()->fileName()),
239                         boost::tie(view()->cursor().pit(),
240                         view()->cursor().pos()));
241         }
242         theApp()->gui().unregisterView(id());   
243         if (theApp()->gui().viewIds().empty())
244         {
245                 // this is the place where we leave the frontend.
246                 // it is the only point at which we start quitting.
247                 saveGeometry();
248                 close_event->accept();
249                 // quit the event loop
250                 qApp->quit();
251         }
252         close_event->accept();
253 }
254
255
256 void GuiView::saveGeometry()
257 {
258         static bool done = false;
259         if (done)
260                 return;
261         else
262                 done = true;
263
264         // FIXME:
265         // change the ifdef to 'geometry = normalGeometry();' only
266         // when Trolltech has fixed the broken normalGeometry on X11:
267         // http://www.trolltech.com/developer/task-tracker/index_html?id=119684+&method=entry
268         // Then also the moveEvent, resizeEvent, and the
269         // code for floatingGeometry_ can be removed;
270         // adjust GuiView::setGeometry()
271 #ifdef Q_WS_WIN
272         QRect geometry = normalGeometry();
273 #else
274         updateFloatingGeometry();
275         QRect geometry = floatingGeometry_;
276 #endif
277
278         // save windows size and position
279         Session & session = LyX::ref().session();
280         session.sessionInfo().save("WindowWidth", convert<string>(geometry.width()));
281         session.sessionInfo().save("WindowHeight", convert<string>(geometry.height()));
282         session.sessionInfo().save("WindowIsMaximized", (isMaximized() ? "yes" : "no"));
283         session.sessionInfo().save("IconSizeXY", convert<string>(iconSize().width()));
284         if (lyxrc.geometry_xysaved) {
285                 session.sessionInfo().save("WindowPosX", convert<string>(geometry.x() + d.posx_offset));
286                 session.sessionInfo().save("WindowPosY", convert<string>(geometry.y() + d.posy_offset));
287         }
288         getToolbars().saveToolbarInfo();
289 }
290                                                   
291
292 void GuiView::setGeometry(unsigned int width,
293                                                                   unsigned int height,
294                                                                   int posx, int posy,
295                                                                   bool maximize,
296                                                                   unsigned int iconSizeXY,
297                                                                   const std::string & geometryArg)
298 {
299         // use last value (not at startup)
300         if (d.lastIconSize != 0)
301                 setIconSize(d.lastIconSize);
302         else if (iconSizeXY != 0)
303                 setIconSize(iconSizeXY);
304         else
305                 setIconSize(d.normalIconSize);
306
307         // only true when the -geometry option was NOT used
308         if (width != 0 && height != 0) {
309                 if (posx != -1 && posy != -1) {
310                         // if there are ever startup positioning problems 
311                         // on a virtual desktop then check the 6 lines below
312                         // http://doc.trolltech.com/4.2/qdesktopwidget.html 
313                         QDesktopWidget& dw = *qApp->desktop();
314                         QRect desk = dw.availableGeometry(dw.primaryScreen());
315                         (posx >= desk.width() ? posx = 50 : true);
316                         (posy >= desk.height()? posy = 50 : true);
317 #ifdef Q_WS_WIN
318                         // FIXME: use setGeometry only when Trolltech has fixed the qt4/X11 bug
319                         QWidget::setGeometry(posx, posy, width, height);
320 #else
321                         resize(width, height);
322                         move(posx, posy);
323 #endif
324                 } else {
325                         resize(width, height);
326                 }
327
328                 if (maximize)
329                         setWindowState(Qt::WindowMaximized);
330         }
331         else
332         {
333                 // FIXME: move this code into parse_geometry() (lyx_main.C)
334 #ifdef Q_WS_WIN
335                 int x, y;
336                 int w, h;
337                 QRegExp re( "[=]*(?:([0-9]+)[xX]([0-9]+)){0,1}[ ]*(?:([+-][0-9]*)([+-][0-9]*)){0,1}" );
338                 re.indexIn( toqstr(geometryArg.c_str()));
339                 w = re.cap( 1 ).toInt();
340                 h = re.cap( 2 ).toInt();
341                 x = re.cap( 3 ).toInt();
342                 y = re.cap( 4 ).toInt();
343                 QWidget::setGeometry( x, y, w, h );
344 #endif
345         }
346
347         show();
348
349         // For an unknown reason, the Window title update is not effective for
350         // the second windows up until it is shown on screen (Qt bug?).
351         updateWindowTitle();
352
353         // after show geometry() has changed (Qt bug?)
354         // we compensate the drift when storing the position
355         d.posx_offset = 0;
356         d.posy_offset = 0;
357         if (width != 0 && height != 0) 
358                 if (posx != -1 && posy != -1) {
359 #ifdef Q_WS_WIN
360                         d.posx_offset = posx - normalGeometry().x();
361                         d.posy_offset = posy - normalGeometry().y();
362 #else
363                         if (!maximize) {
364                                 d.posx_offset = posx - geometry().x();
365                                 d.posy_offset = posy - geometry().y();
366                         }
367 #endif
368                 }
369 }
370
371
372 void GuiView::updateMenu(QAction * /*action*/)
373 {
374         menubar_->update();
375 }
376
377
378 void GuiView::setWindowTitle(docstring const & t, docstring const & it)
379 {
380         QString title = windowTitle();
381         QString new_title = toqstr(t);
382         if (title != new_title) {
383                 QMainWindow::setWindowTitle(new_title);
384                 QMainWindow::setWindowIconText(toqstr(it));
385         }
386 }
387
388
389 void GuiView::addCommandBuffer(QToolBar * toolbar)
390 {
391         commandbuffer_ = new QCommandBuffer(this, *controlcommand_);
392         focus_command_buffer.connect(boost::bind(&GuiView::focus_command_widget, this));
393         toolbar->addWidget(commandbuffer_);
394 }
395
396
397 void GuiView::message(docstring const & str)
398 {
399         statusBar()->showMessage(toqstr(str));
400         statusbar_timer_.stop();
401         statusbar_timer_.start(statusbar_timer_value);
402 }
403
404
405 void GuiView::clearMessage()
406 {
407         update_view_state_qt();
408 }
409
410
411 void GuiView::setIconSize(unsigned int size)
412 {
413         d.lastIconSize = size;
414         QMainWindow::setIconSize(QSize(size, size));
415 }
416
417
418 void GuiView::smallSizedIcons()
419 {
420         setIconSize(d.smallIconSize);
421 }
422
423
424 void GuiView::normalSizedIcons()
425 {
426         setIconSize(d.normalIconSize);
427 }
428
429
430 void GuiView::bigSizedIcons()
431 {
432         setIconSize(d.bigIconSize);
433 }
434
435
436 void GuiView::focus_command_widget()
437 {
438         if (commandbuffer_)
439                 commandbuffer_->focus_command();
440 }
441
442
443 void GuiView::update_view_state_qt()
444 {
445         statusBar()->showMessage(toqstr(theLyXFunc().viewStatusMessage()));
446         statusbar_timer_.stop();
447 }
448
449
450 void GuiView::initTab(QWidget* workarea)
451 {
452         d.wt = new WidgetWithTabBar(workarea);
453         setCentralWidget(d.wt);
454         QObject::connect(d.wt->tabbar, SIGNAL(currentChanged(int)),
455                         this, SLOT(currentTabChanged(int)));
456 }
457
458
459 void GuiView::updateTab()
460 {
461         QTabBar& tb = *d.wt->tabbar;
462
463         // update when all  is done
464         tb.blockSignals(true);
465
466         typedef std::vector<string> Strings;
467         Strings const names = theBufferList().getFileNames();
468         size_t n_size = names.size();
469
470         Strings addtab;
471         // show tabs only when there is more 
472         // than one file opened
473         if (n_size > 1)
474         {
475                 for (size_t i = 0; i != n_size; i++) 
476                         if (d.namemap.find(names[i]) == d.namemap.end())
477                                 addtab.push_back(names.at(i));
478         }
479
480         for(size_t i = 0; i<addtab.size(); i++)
481         {
482                 QString tab_name = lyx::toqstr(onlyFilename(addtab.at(i))); 
483                 d.namemap.insert(GuiViewPrivate::NameMapPair(addtab.at(i), tab_name));
484                 tb.addTab(tab_name);
485         }
486
487         // check if all names showed by the tabs
488         // are also in the current bufferlist
489         Strings removetab;
490         bool notall = true;
491         if (n_size < 2)
492                 notall = false;
493         std::map<string, QString>::iterator tabit = d.namemap.begin();
494         for (;tabit != d.namemap.end(); ++tabit)
495         {
496                 bool found = false;
497                 for (size_t i = 0; i != n_size; i++) 
498                         if (tabit->first == names.at(i) && notall)
499                                 found = true;
500                 if (!found)
501                         removetab.push_back(tabit->first);
502         }
503         
504
505         // remove tabs
506         for(size_t i = 0; i<removetab.size(); i++)
507         {
508                 if (d.namemap.find(removetab.at(i)) != d.namemap.end())
509                 {
510                         tabit = d.namemap.find(removetab.at(i));
511                         for (int i = 0; i < tb.count(); i++)
512                                 if (tb.tabText(i) == tabit->second)
513                                 {
514                                         tb.removeTab(i);
515                                         break;
516                                 }
517                         d.namemap.erase(tabit);
518                 }
519         }
520
521         // rebuild func map
522         if (removetab.size() > 0 || addtab.size() > 0)
523         {
524                 d.funcmap.clear();
525                 tabit = d.namemap.begin();
526                 for (;tabit != d.namemap.end(); ++tabit)
527                 {
528                         QTabBar& tb = *d.wt->tabbar;
529                         for (int i = 0; i < tb.count(); i++)
530                         {
531                                 if (tb.tabText(i) == tabit->second)
532                                 {
533                                         FuncRequest func(LFUN_BUFFER_SWITCH, tabit->first);
534                                         d.funcmap.insert(GuiViewPrivate::FuncMapPair(i, func));
535                                         break;
536                                 }
537                         }
538                 }
539         }
540
541         // set current tab
542         if (view()->buffer()) 
543         {
544                 string cur_title = view()->buffer()->fileName();
545                 if (d.namemap.find(cur_title) != d.namemap.end())
546                 {
547                         QString tabname = d.namemap.find(cur_title)->second;
548                         for (int i = 0; i < tb.count(); i++)
549                                 if (tb.tabText(i) == tabname)
550                                 {
551                                         tb.setCurrentIndex(i);
552                                         break;
553                                 }
554                 }
555         }
556
557         tb.blockSignals(false);
558         d.wt->update();
559 }
560
561
562 void GuiView::currentTabChanged (int index)
563 {
564         std::map<int, FuncRequest>::const_iterator it = d.funcmap.find(index);
565         if (it != d.funcmap.end())
566                 activated(it->second);
567 }
568
569
570 void GuiView::updateStatusBar()
571 {
572         // let the user see the explicit message
573         if (statusbar_timer_.isActive())
574                 return;
575
576         statusBar()->showMessage(toqstr(theLyXFunc().viewStatusMessage()));
577 }
578
579
580 void GuiView::activated(FuncRequest const & func)
581 {
582         dispatch(func);
583 }
584
585
586 bool GuiView::hasFocus() const
587 {
588         return qApp->activeWindow() == this;
589 }
590
591
592 void  GuiView::updateFloatingGeometry()
593 {
594         if (!isMaximized())
595                 floatingGeometry_ = QRect(x(), y(), width(), height());
596 }
597
598
599 void GuiView::resizeEvent(QResizeEvent *)
600 {
601         updateFloatingGeometry();
602 }
603
604
605 void GuiView::moveEvent(QMoveEvent *)
606 {
607         updateFloatingGeometry();
608 }
609
610
611 void GuiView::show()
612 {
613         QMainWindow::setWindowTitle(qt_("LyX"));
614         QMainWindow::show();
615         updateFloatingGeometry();
616 }
617
618
619 void GuiView::busy(bool yes)
620 {
621         static_cast<GuiWorkArea *>(work_area_)->setUpdatesEnabled(!yes);
622
623         if (yes) {
624                 work_area_->stopBlinkingCursor();
625                 QApplication::setOverrideCursor(Qt::WaitCursor);
626         }
627         else {
628                 work_area_->startBlinkingCursor();
629                 QApplication::restoreOverrideCursor();
630         }
631 }
632
633
634 Toolbars::ToolbarPtr GuiView::makeToolbar(ToolbarBackend::Toolbar const & tbb)
635 {
636         QLToolbar * Tb = new QLToolbar(tbb, *this);
637         //static QLToolbar * lastTb = 0;
638
639         if (tbb.flags & ToolbarBackend::TOP) {
640                         addToolBar(Qt::TopToolBarArea, Tb);
641                         addToolBarBreak(Qt::TopToolBarArea);
642         }
643         if (tbb.flags & ToolbarBackend::BOTTOM) {
644                 addToolBar(Qt::BottomToolBarArea, Tb);
645                 /*
646                 // Qt bug:
647                 // http://www.trolltech.com/developer/task-tracker/index_html?id=137015&method=entry
648                 // Doesn't work because the toolbar will evtl. be hidden.
649                 if (lastTb)
650                         insertToolBarBreak(lastTb);
651                 lastTb = Tb;
652                 */
653         }
654         if (tbb.flags & ToolbarBackend::LEFT) {
655                 addToolBar(Qt::LeftToolBarArea, Tb);
656         }
657         if (tbb.flags & ToolbarBackend::RIGHT) {
658                 addToolBar(Qt::RightToolBarArea, Tb);
659         }
660
661         return Toolbars::ToolbarPtr(Tb);
662 }
663
664 } // namespace frontend
665 } // namespace lyx
666
667 #include "GuiView_moc.cpp"