]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/GuiView.cpp
pimpl not needed here
[lyx.git] / src / frontends / qt4 / GuiView.cpp
1 /**
2  * \file GuiView.cpp
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 "GuiKeySymbol.h"
21 #include "GuiMenubar.h"
22 #include "GuiToolbar.h"
23 #include "GuiToolbars.h"
24 #include "qt_helpers.h"
25
26 #include "frontends/Application.h"
27 #include "frontends/Dialogs.h"
28 #include "frontends/Gui.h"
29 #include "frontends/WorkArea.h"
30
31 #include "support/filetools.h"
32 #include "support/convert.h"
33 #include "support/lstrings.h"
34 #include "support/os.h"
35
36 #include "Buffer.h"
37 #include "BufferParams.h"
38 #include "BufferView.h"
39 #include "BufferList.h"
40 #include "Cursor.h"
41 #include "debug.h"
42 #include "FuncRequest.h"
43 #include "Layout.h"
44 #include "LyX.h"
45 #include "LyXFunc.h"
46 #include "LyXRC.h"
47 #include "MenuBackend.h"
48 #include "Paragraph.h"
49 #include "Session.h"
50 #include "ToolbarBackend.h"
51 #include "version.h"
52
53 #include <QAction>
54 #include <QApplication>
55 #include <QCloseEvent>
56 #include <QDesktopWidget>
57 #include <QDragEnterEvent>
58 #include <QDropEvent>
59 #include <QList>
60 #include <QMenu>
61 #include <QPainter>
62 #include <QPixmap>
63 #include <QPushButton>
64 #include <QStackedWidget>
65 #include <QStatusBar>
66 #include <QToolBar>
67 #include <QUrl>
68
69 #include <boost/current_function.hpp>
70
71 using std::endl;
72 using std::string;
73 using std::vector;
74
75 namespace lyx {
76
77 using support::makeDisplayPath;
78
79 extern bool quitting;
80
81 namespace frontend {
82
83 namespace {
84
85 int const statusbar_timer_value = 3000;
86
87 class BackgroundWidget : public QWidget
88 {
89 public:
90         BackgroundWidget(QString const & file, QString const & text)
91         {
92                 splash_ = new QPixmap(file);
93                 if (!splash_) {
94                         lyxerr << "could not load splash screen: '" << fromqstr(file) << "'" << endl;
95                         return;
96                 }
97
98                 QPainter pain(splash_);
99                 pain.setPen(QColor(255, 255, 0));
100                 QFont font;
101                 // The font used to display the version info
102                 font.setStyleHint(QFont::SansSerif);
103                 font.setWeight(QFont::Bold);
104                 font.setPointSize(convert<int>(lyxrc.font_sizes[FONT_SIZE_LARGE]));
105                 pain.setFont(font);
106                 pain.drawText(260, 270, text);
107         }
108
109         void paintEvent(QPaintEvent *)
110         {
111                 if (!splash_)
112                         return;
113
114                 int x = (width() - splash_->width()) / 2;
115                 int y = (height() - splash_->height()) / 2;
116                 QPainter pain(this);
117                 pain.drawPixmap(x, y, *splash_);
118         }
119
120 private:
121         QPixmap * splash_;
122 };
123
124 };
125
126
127 struct GuiViewBase::GuiViewPrivate
128 {
129         string cur_title;
130
131         int posx_offset;
132         int posy_offset;
133
134         TabWorkArea * tab_widget_;
135         QStackedWidget * stack_widget_;
136         BackgroundWidget * bg_widget_;
137         /// view's menubar
138         GuiMenubar * menubar_;
139         /// view's toolbars
140         GuiToolbars * toolbars_;
141         ///
142         docstring current_layout;
143
144         GuiViewPrivate() : posx_offset(0), posy_offset(0) {}
145
146         unsigned int smallIconSize;
147         unsigned int normalIconSize;
148         unsigned int bigIconSize;
149         // static needed by "New Window"
150         static unsigned int lastIconSize;
151
152         QMenu * toolBarPopup(GuiViewBase * parent)
153         {
154                 // FIXME: translation
155                 QMenu * menu = new QMenu(parent);
156                 QActionGroup * iconSizeGroup = new QActionGroup(parent);
157
158                 QAction * smallIcons = new QAction(iconSizeGroup);
159                 smallIcons->setText(qt_("Small-sized icons"));
160                 smallIcons->setCheckable(true);
161                 QObject::connect(smallIcons, SIGNAL(triggered()), parent, SLOT(smallSizedIcons()));
162                 menu->addAction(smallIcons);
163
164                 QAction * normalIcons = new QAction(iconSizeGroup);
165                 normalIcons->setText(qt_("Normal-sized icons"));
166                 normalIcons->setCheckable(true);
167                 QObject::connect(normalIcons, SIGNAL(triggered()), parent, SLOT(normalSizedIcons()));
168                 menu->addAction(normalIcons);
169
170                 QAction * bigIcons = new QAction(iconSizeGroup);
171                 bigIcons->setText(qt_("Big-sized icons"));
172                 bigIcons->setCheckable(true);
173                 QObject::connect(bigIcons, SIGNAL(triggered()), parent, SLOT(bigSizedIcons()));
174                 menu->addAction(bigIcons);
175
176                 unsigned int cur = parent->iconSize().width();
177                 if ( cur == parent->d.smallIconSize)
178                         smallIcons->setChecked(true);
179                 else if (cur == parent->d.normalIconSize)
180                         normalIcons->setChecked(true);
181                 else if (cur == parent->d.bigIconSize)
182                         bigIcons->setChecked(true);
183
184                 return menu;
185         }
186
187         void initBackground()
188         {
189                 bg_widget_ = 0;
190                 LYXERR(Debug::GUI) << "show banner: " << lyxrc.show_banner << endl;
191                 /// The text to be written on top of the pixmap
192                 QString const text = lyx_version ? QString(lyx_version) : qt_("unknown version");
193                 bg_widget_ = new BackgroundWidget(":/images/banner.png", text);
194         }
195
196         void setBackground()
197         {
198                 if (!bg_widget_)
199                         return;
200
201                 stack_widget_->setCurrentWidget(bg_widget_);
202                 bg_widget_->setUpdatesEnabled(true);
203         }
204 };
205
206
207 unsigned int GuiViewBase::GuiViewPrivate::lastIconSize = 0;
208
209
210 GuiViewBase::GuiViewBase(int id)
211         : QMainWindow(), LyXView(id), quitting_by_menu_(false),
212           d(*new GuiViewPrivate)
213 {
214         // Qt bug? signal lastWindowClosed does not work
215         setAttribute(Qt::WA_QuitOnClose, false);
216         setAttribute(Qt::WA_DeleteOnClose, true);
217
218         // hardcode here the platform specific icon size
219         d.smallIconSize = 14;   // scaling problems
220         d.normalIconSize = 20;  // ok, default
221         d.bigIconSize = 26;             // better for some math icons
222
223 #ifndef Q_WS_MACX
224         // assign an icon to main form. We do not do it under Qt/Mac,
225         // since the icon is provided in the application bundle.
226         setWindowIcon(QPixmap(":/images/lyx.png"));
227 #endif
228
229         d.tab_widget_ = new TabWorkArea;
230         QObject::connect(d.tab_widget_, SIGNAL(currentWorkAreaChanged(GuiWorkArea *)),
231                 this, SLOT(on_currentWorkAreaChanged(GuiWorkArea *)));
232
233         d.initBackground();
234         if (d.bg_widget_) {
235                 LYXERR(Debug::GUI) << "stack widget!" << endl;
236                 d.stack_widget_ = new QStackedWidget;
237                 d.stack_widget_->addWidget(d.bg_widget_);
238                 d.stack_widget_->addWidget(d.tab_widget_);
239                 setCentralWidget(d.stack_widget_);
240         } else {
241                 d.stack_widget_ = 0;
242                 setCentralWidget(d.tab_widget_);
243         }
244
245         // For Drag&Drop.
246         setAcceptDrops(true);
247 }
248
249
250 GuiViewBase::~GuiViewBase()
251 {
252         delete d.menubar_;
253         delete d.toolbars_;
254         delete &d;
255 }
256
257
258 void GuiViewBase::close()
259 {
260         quitting_by_menu_ = true;
261         d.tab_widget_->closeAll();
262         QMainWindow::close();
263         quitting_by_menu_ = false;
264 }
265
266
267 void GuiViewBase::setFocus()
268 {
269         if (d.tab_widget_->count())
270                 d.tab_widget_->currentWidget()->setFocus();
271 }
272
273
274 QMenu* GuiViewBase::createPopupMenu()
275 {
276         return d.toolBarPopup(this);
277 }
278
279
280 void GuiViewBase::init()
281 {
282         // GuiToolbars *must* be initialised before GuiMenubar.
283         d.toolbars_ = new GuiToolbars(*this);
284         // FIXME: GuiToolbars::init() cannot be integrated in the ctor
285         // because LyXFunc::getStatus() needs a properly initialized
286         // GuiToolbars object (for LFUN_TOOLBAR_TOGGLE).
287         d.toolbars_->init();
288         d.menubar_ = new GuiMenubar(this, menubackend);
289
290         statusBar()->setSizeGripEnabled(true);
291
292         QObject::connect(&statusbar_timer_, SIGNAL(timeout()),
293                 this, SLOT(update_view_state_qt()));
294
295         if (d.stack_widget_)
296                 d.stack_widget_->setCurrentWidget(d.bg_widget_);
297 }
298
299
300 void GuiViewBase::closeEvent(QCloseEvent * close_event)
301 {
302         // we may have been called through the close window button
303         // which bypasses the LFUN machinery.
304         if (!quitting_by_menu_ && theApp()->gui().viewIds().size() == 1) {
305                 if (!theBufferList().quitWriteAll()) {
306                         close_event->ignore();
307                         return;
308                 }
309         }
310
311         // Make sure that no LFUN use this close to be closed View.
312         theLyXFunc().setLyXView(0);
313         // Make sure the timer time out will not trigger a statusbar update.
314         statusbar_timer_.stop();
315
316         theApp()->gui().unregisterView(id());
317         if (!theApp()->gui().viewIds().empty()) {
318                 // Just close the window and do nothing else if this is not the
319                 // last window.
320                 close_event->accept();
321                 return;
322         }
323
324         quitting = true;
325
326         // this is the place where we leave the frontend.
327         // it is the only point at which we start quitting.
328         saveGeometry();
329         close_event->accept();
330         // quit the event loop
331         qApp->quit();
332 }
333
334
335 void GuiViewBase::dragEnterEvent(QDragEnterEvent * event)
336 {
337         if (event->mimeData()->hasUrls())
338                 event->accept();
339         /// \todo Ask lyx-devel is this is enough:
340         /// if (event->mimeData()->hasFormat("text/plain"))
341         ///     event->acceptProposedAction();
342 }
343
344
345 void GuiViewBase::dropEvent(QDropEvent* event)
346 {
347         QList<QUrl> files = event->mimeData()->urls();
348         if (files.isEmpty())
349                 return;
350
351         LYXERR(Debug::GUI) << BOOST_CURRENT_FUNCTION
352                 << " got URLs!" << endl;
353         for (int i = 0; i != files.size(); ++i) {
354                 string const file = support::os::internal_path(fromqstr(
355                         files.at(i).toLocalFile()));
356                 if (!file.empty())
357                         dispatch(FuncRequest(LFUN_FILE_OPEN, file));
358         }
359 }
360
361
362 void GuiViewBase::saveGeometry()
363 {
364         static bool done = false;
365         if (done)
366                 return;
367         else
368                 done = true;
369
370         // FIXME:
371         // change the ifdef to 'geometry = normalGeometry();' only
372         // when Trolltech has fixed the broken normalGeometry on X11:
373         // http://www.trolltech.com/developer/task-tracker/index_html?id=119684+&method=entry
374         // Then also the moveEvent, resizeEvent, and the
375         // code for floatingGeometry_ can be removed;
376         // adjust GuiViewBase::setGeometry()
377
378         QRect normal_geometry;
379         int maximized;
380 #ifdef Q_WS_WIN
381         normal_geometry = normalGeometry();
382         if (isMaximized()) {
383                 maximized = CompletelyMaximized;
384         } else {
385                 maximized = NotMaximized;
386         }
387 #else
388         normal_geometry = updateFloatingGeometry();
389
390         QDesktopWidget& dw = *qApp->desktop();
391         QRect desk = dw.availableGeometry(dw.primaryScreen());
392         // Qt bug on Linux: load completely maximized, vert max. save-> frameGeometry().height() is wrong
393         if (isMaximized() && desk.width() <= frameGeometry().width() && desk.height() <= frameGeometry().height()) {
394                 maximized = CompletelyMaximized;
395                 // maximizing does not work when the window is allready hor. or vert. maximized
396                 // Tested only on KDE
397                 int dh = frameGeometry().height() - height();
398                 if (desk.height() <= normal_geometry.height() + dh)
399                         normal_geometry.setHeight(normal_geometry.height() - 1);
400                 int dw = frameGeometry().width() - width();
401                 if (desk.width() <= normal_geometry.width() + dw)
402                         normal_geometry.setWidth(normal_geometry.width() - 1);
403         } else if (desk.height() <= frameGeometry().height()) {
404                 maximized = VerticallyMaximized;
405         } else if (desk.width() <= frameGeometry().width()) {
406                 maximized = HorizontallyMaximized;
407         } else {
408                 maximized = NotMaximized;
409         }
410
411
412 #endif
413         // save windows size and position
414         Session & session = LyX::ref().session();
415         session.sessionInfo().save("WindowWidth", convert<string>(normal_geometry.width()));
416         session.sessionInfo().save("WindowHeight", convert<string>(normal_geometry.height()));
417         session.sessionInfo().save("WindowMaximized", convert<string>(maximized));
418         session.sessionInfo().save("IconSizeXY", convert<string>(iconSize().width()));
419         if (lyxrc.geometry_xysaved) {
420                 session.sessionInfo().save("WindowPosX", convert<string>(normal_geometry.x() + d.posx_offset));
421                 session.sessionInfo().save("WindowPosY", convert<string>(normal_geometry.y() + d.posy_offset));
422         }
423         d.toolbars_->saveToolbarInfo();
424 }
425
426
427 void GuiViewBase::setGeometry(unsigned int width,
428                           unsigned int height,
429                           int posx, int posy,
430                           int maximized,
431                           unsigned int iconSizeXY,
432                           const string & geometryArg)
433 {
434         // use last value (not at startup)
435         if (d.lastIconSize != 0)
436                 setIconSize(d.lastIconSize);
437         else if (iconSizeXY != 0)
438                 setIconSize(iconSizeXY);
439         else
440                 setIconSize(d.normalIconSize);
441
442         // only true when the -geometry option was NOT used
443         if (width != 0 && height != 0) {
444                 if (posx != -1 && posy != -1) {
445                         // if there are startup positioning problems:
446                         // http://doc.trolltech.com/4.2/qdesktopwidget.html
447                         QDesktopWidget& dw = *qApp->desktop();
448                         if (dw.isVirtualDesktop()) {
449                                 if(!dw.geometry().contains(posx, posy)) {
450                                         posx = 50;
451                                         posy = 50;
452                                 }
453                         } else {
454                                 // Which system doesn't use a virtual desktop?
455                                 // TODO save also last screen number and check if it is still availabe.
456                         }
457 #ifdef Q_WS_WIN
458                         // FIXME: use setGeometry only when Trolltech has fixed the qt4/X11 bug
459                         QWidget::setGeometry(posx, posy, width, height);
460 #else
461                         resize(width, height);
462                         move(posx, posy);
463 #endif
464                 } else {
465                         resize(width, height);
466                 }
467
468                 // remember original size
469                 floatingGeometry_ = QRect(posx, posy, width, height);
470
471                 if (maximized != NotMaximized) {
472                         if (maximized == CompletelyMaximized) {
473                                 setWindowState(Qt::WindowMaximized);
474                         } else {
475 #ifndef Q_WS_WIN
476                                 // TODO How to set by the window manager?
477                                 //      setWindowState(Qt::WindowVerticallyMaximized);
478                                 //      is not possible
479                                 QDesktopWidget& dw = *qApp->desktop();
480                                 QRect desk = dw.availableGeometry(dw.primaryScreen());
481                                 if (maximized == VerticallyMaximized)
482                                         resize(width, desk.height());
483                                 if (maximized == HorizontallyMaximized)
484                                         resize(desk.width(), height);
485 #endif
486                         }
487                 }
488         }
489         else
490         {
491                 // FIXME: move this code into parse_geometry() (LyX.cpp)
492 #ifdef Q_WS_WIN
493                 int x, y;
494                 int w, h;
495                 QRegExp re( "[=]*(?:([0-9]+)[xX]([0-9]+)){0,1}[ ]*(?:([+-][0-9]*)([+-][0-9]*)){0,1}" );
496                 re.indexIn(toqstr(geometryArg.c_str()));
497                 w = re.cap(1).toInt();
498                 h = re.cap(2).toInt();
499                 x = re.cap(3).toInt();
500                 y = re.cap(4).toInt();
501                 QWidget::setGeometry( x, y, w, h );
502 #else
503                 // silence warning
504                 (void)geometryArg;
505 #endif
506         }
507         
508         d.setBackground();
509         
510         show();
511
512         // For an unknown reason, the Window title update is not effective for
513         // the second windows up until it is shown on screen (Qt bug?).
514         updateWindowTitle();
515
516         // after show geometry() has changed (Qt bug?)
517         // we compensate the drift when storing the position
518         d.posx_offset = 0;
519         d.posy_offset = 0;
520         if (width != 0 && height != 0)
521                 if (posx != -1 && posy != -1) {
522 #ifdef Q_WS_WIN
523                         d.posx_offset = posx - normalGeometry().x();
524                         d.posy_offset = posy - normalGeometry().y();
525 #else
526 #ifndef Q_WS_MACX
527                         if (maximized == NotMaximized) {
528                                 d.posx_offset = posx - geometry().x();
529                                 d.posy_offset = posy - geometry().y();
530                         }
531 #endif
532 #endif
533                 }
534 }
535
536
537 void GuiViewBase::setWindowTitle(docstring const & t, docstring const & it)
538 {
539         QString title = windowTitle();
540         QString new_title = toqstr(t);
541         if (title != new_title) {
542                 QMainWindow::setWindowTitle(new_title);
543                 QMainWindow::setWindowIconText(toqstr(it));
544         }
545         if (Buffer const * buf = buffer())
546                 d.tab_widget_->setTabText(d.tab_widget_->currentIndex(),
547                         toqstr(makeDisplayPath(buf->absFileName(), 30)));
548 }
549
550
551 void GuiViewBase::message(docstring const & str)
552 {
553         statusBar()->showMessage(toqstr(str));
554         statusbar_timer_.stop();
555         statusbar_timer_.start(statusbar_timer_value);
556 }
557
558
559 void GuiViewBase::clearMessage()
560 {
561         update_view_state_qt();
562 }
563
564
565 void GuiViewBase::setIconSize(unsigned int size)
566 {
567         d.lastIconSize = size;
568         QMainWindow::setIconSize(QSize(size, size));
569 }
570
571
572 void GuiViewBase::smallSizedIcons()
573 {
574         setIconSize(d.smallIconSize);
575 }
576
577
578 void GuiViewBase::normalSizedIcons()
579 {
580         setIconSize(d.normalIconSize);
581 }
582
583
584 void GuiViewBase::bigSizedIcons()
585 {
586         setIconSize(d.bigIconSize);
587 }
588
589
590 void GuiViewBase::update_view_state_qt()
591 {
592         if (!hasFocus())
593                 return;
594         theLyXFunc().setLyXView(this);
595         statusBar()->showMessage(toqstr(theLyXFunc().viewStatusMessage()));
596         statusbar_timer_.stop();
597 }
598
599
600 void GuiViewBase::on_currentWorkAreaChanged(GuiWorkArea * wa)
601 {
602         disconnectBuffer();
603         disconnectBufferView();
604         connectBufferView(wa->bufferView());
605         connectBuffer(wa->bufferView().buffer());
606
607         updateToc();
608         // Buffer-dependent dialogs should be updated or
609         // hidden. This should go here because some dialogs (eg ToC)
610         // require bv_->text.
611         getDialogs().updateBufferDependent(true);
612         updateToolbars();
613         updateLayoutChoice();
614         updateWindowTitle();
615         updateStatusBar();
616 }
617
618
619 void GuiViewBase::updateStatusBar()
620 {
621         // let the user see the explicit message
622         if (statusbar_timer_.isActive())
623                 return;
624
625         statusBar()->showMessage(toqstr(theLyXFunc().viewStatusMessage()));
626 }
627
628
629 void GuiViewBase::activated(FuncRequest const & func)
630 {
631         dispatch(func);
632 }
633
634
635 bool GuiViewBase::hasFocus() const
636 {
637         return qApp->activeWindow() == this;
638 }
639
640
641 QRect  GuiViewBase::updateFloatingGeometry()
642 {
643         QDesktopWidget& dw = *qApp->desktop();
644         QRect desk = dw.availableGeometry(dw.primaryScreen());
645         // remember only non-maximized sizes
646         if (!isMaximized() && desk.width() > frameGeometry().width() && desk.height() > frameGeometry().height()) {
647                 floatingGeometry_ = QRect(x(), y(), width(), height());
648         }
649         return floatingGeometry_;
650 }
651
652
653 void GuiViewBase::resizeEvent(QResizeEvent *)
654 {
655         updateFloatingGeometry();
656 }
657
658
659 void GuiViewBase::moveEvent(QMoveEvent *)
660 {
661         updateFloatingGeometry();
662 }
663
664
665 bool GuiViewBase::event(QEvent * e)
666 {
667         switch (e->type())
668         {
669         // Useful debug code:
670         //case QEvent::ActivationChange:
671         //case QEvent::WindowDeactivate:
672         //case QEvent::Paint:
673         //case QEvent::Enter:
674         //case QEvent::Leave:
675         //case QEvent::HoverEnter:
676         //case QEvent::HoverLeave:
677         //case QEvent::HoverMove:
678         //case QEvent::StatusTip:
679         //case QEvent::DragEnter:
680         //case QEvent::DragLeave:
681         //case QEvent::Drop:
682         //      break;
683
684         case QEvent::WindowActivate: {
685                 theApp()->setCurrentView(*this);
686                 GuiWorkArea * wa = d.tab_widget_->currentWorkArea();
687                 if (wa) {
688                         BufferView & bv = wa->bufferView();
689                         connectBufferView(bv);
690                         connectBuffer(bv.buffer());
691                         // The document structure, name and dialogs might have
692                         // changed in another view.
693                         updateWindowTitle();
694                         getDialogs().updateBufferDependent(true);
695                 }
696                 return QMainWindow::event(e);
697         }
698         case QEvent::ShortcutOverride: {
699                 QKeyEvent * ke = static_cast<QKeyEvent*>(e);
700                 if (d.tab_widget_->count() == 0) {
701                         theLyXFunc().setLyXView(this);
702                         KeySymbol sym;
703                         setKeySymbol(&sym, ke);
704                         theLyXFunc().processKeySym(sym, q_key_state(ke->modifiers()));
705                         e->accept();
706                         return true;
707                 }
708                 if (ke->key() == Qt::Key_Tab || ke->key() == Qt::Key_Backtab) {
709                         KeySymbol sym;
710                         setKeySymbol(&sym, ke);
711                         currentWorkArea()->processKeySym(sym, NoModifier);
712                         e->accept();
713                         return true;
714                 }
715         }
716         default:
717                 return QMainWindow::event(e);
718         }
719 }
720
721
722 bool GuiViewBase::focusNextPrevChild(bool /*next*/)
723 {
724         setFocus();
725         return true;
726 }
727
728
729 void GuiViewBase::showView()
730 {
731         QMainWindow::setWindowTitle(qt_("LyX"));
732         QMainWindow::show();
733         updateFloatingGeometry();
734 }
735
736
737 void GuiViewBase::setBusy(bool yes)
738 {
739         GuiWorkArea * wa = d.tab_widget_->currentWorkArea();
740         if (wa) {
741                 wa->setUpdatesEnabled(!yes);
742                 if (yes)
743                         wa->stopBlinkingCursor();
744                 else
745                         wa->startBlinkingCursor();
746         }
747
748         if (yes)
749                 QApplication::setOverrideCursor(Qt::WaitCursor);
750         else
751                 QApplication::restoreOverrideCursor();
752 }
753
754
755 GuiToolbar * GuiViewBase::makeToolbar(ToolbarInfo const & tbinfo, bool newline)
756 {
757         GuiToolbar * toolBar = new GuiToolbar(tbinfo, *this);
758
759         if (tbinfo.flags & ToolbarInfo::TOP) {
760                 if (newline)
761                         addToolBarBreak(Qt::TopToolBarArea);
762                 addToolBar(Qt::TopToolBarArea, toolBar);
763         }
764
765         if (tbinfo.flags & ToolbarInfo::BOTTOM) {
766 // Qt < 4.2.2 cannot handle ToolBarBreak on non-TOP dock.
767 #if (QT_VERSION >= 0x040202)
768                 if (newline)
769                         addToolBarBreak(Qt::BottomToolBarArea);
770 #endif
771                 addToolBar(Qt::BottomToolBarArea, toolBar);
772         }
773
774         if (tbinfo.flags & ToolbarInfo::LEFT) {
775 // Qt < 4.2.2 cannot handle ToolBarBreak on non-TOP dock.
776 #if (QT_VERSION >= 0x040202)
777                 if (newline)
778                         addToolBarBreak(Qt::LeftToolBarArea);
779 #endif
780                 addToolBar(Qt::LeftToolBarArea, toolBar);
781         }
782
783         if (tbinfo.flags & ToolbarInfo::RIGHT) {
784 // Qt < 4.2.2 cannot handle ToolBarBreak on non-TOP dock.
785 #if (QT_VERSION >= 0x040202)
786                 if (newline)
787                         addToolBarBreak(Qt::RightToolBarArea);
788 #endif
789                 addToolBar(Qt::RightToolBarArea, toolBar);
790         }
791
792         // The following does not work so I cannot restore to exact toolbar location
793         /*
794         ToolbarSection::ToolbarInfo & tbinfo = LyX::ref().session().toolbars().load(tbinfo.name);
795         toolBar->move(tbinfo.posx, tbinfo.posy);
796         */
797
798         return toolBar;
799 }
800
801
802 WorkArea * GuiViewBase::workArea(Buffer & buffer)
803 {
804         return d.tab_widget_->workArea(buffer);
805 }
806
807
808 WorkArea * GuiViewBase::addWorkArea(Buffer & buffer)
809 {
810         GuiWorkArea * wa = new GuiWorkArea(buffer, *this);
811         wa->setUpdatesEnabled(false);
812         d.tab_widget_->addTab(wa, toqstr(makeDisplayPath(buffer.absFileName(), 30)));
813         wa->bufferView().updateMetrics(false);
814         if (d.stack_widget_)
815                 d.stack_widget_->setCurrentWidget(d.tab_widget_);
816         // Hide tabbar if there's only one tab.
817         d.tab_widget_->showBar(d.tab_widget_->count() > 1);
818         return wa;
819 }
820
821
822 WorkArea * GuiViewBase::currentWorkArea()
823 {
824         return d.tab_widget_->currentWorkArea();
825 }
826
827
828 WorkArea const * GuiViewBase::currentWorkArea() const
829 {
830         return d.tab_widget_->currentWorkArea();
831 }
832
833
834 void GuiViewBase::setCurrentWorkArea(WorkArea * work_area)
835 {
836         BOOST_ASSERT(work_area);
837
838         // Changing work area can result from opening a file so
839         // update the toc in any case.
840         updateToc();
841
842         GuiWorkArea * wa = static_cast<GuiWorkArea *>(work_area);
843         BOOST_ASSERT(wa);
844         d.tab_widget_->setCurrentWorkArea(wa);
845 }
846
847
848 void GuiViewBase::removeWorkArea(WorkArea * work_area)
849 {
850         BOOST_ASSERT(work_area);
851         if (work_area == currentWorkArea()) {
852                 disconnectBuffer();
853                 disconnectBufferView();
854         }
855
856         // removing a work area often results from closing a file so
857         // update the toc in any case.
858         updateToc();
859
860         GuiWorkArea * gwa = static_cast<GuiWorkArea *>(work_area);
861         BOOST_ASSERT(gwa);
862         d.tab_widget_->removeWorkArea(gwa);
863
864         getDialogs().hideBufferDependent();
865
866         if (d.tab_widget_->count() == 0 && d.stack_widget_)
867                 // No more work area, switch to the background widget.
868                 d.setBackground();
869 }
870
871
872 void GuiViewBase::showMiniBuffer(bool visible)
873 {
874         d.toolbars_->showCommandBuffer(visible);
875 }
876
877
878 void GuiViewBase::openMenu(docstring const & name)
879 {
880         d.menubar_->openByName(toqstr(name));
881 }
882
883
884 void GuiViewBase::openLayoutList()
885 {
886         d.toolbars_->openLayoutList();
887 }
888
889
890 void GuiViewBase::updateLayoutChoice()
891 {
892         // Don't show any layouts without a buffer
893         if (!buffer()) {
894                 d.toolbars_->clearLayoutList();
895                 return;
896         }
897
898         // Update the layout display
899         if (d.toolbars_->updateLayoutList(buffer()->params().getTextClassPtr())) {
900                 d.current_layout = buffer()->params().getTextClass().defaultLayoutName();
901         }
902
903         docstring const & layout = currentWorkArea()->bufferView().cursor().
904                 innerParagraph().layout()->name();
905
906         if (layout != d.current_layout) {
907                 d.toolbars_->setLayout(layout);
908                 d.current_layout = layout;
909         }
910 }
911
912
913 bool GuiViewBase::isToolbarVisible(std::string const & id)
914 {
915         return d.toolbars_->visible(id);
916 }
917
918 void GuiViewBase::updateToolbars()
919 {
920         WorkArea * wa = currentWorkArea();
921         if (wa) {
922                 bool const math =
923                         wa->bufferView().cursor().inMathed();
924                 bool const table =
925                         lyx::getStatus(FuncRequest(LFUN_LAYOUT_TABULAR)).enabled();
926                 bool const review =
927                         lyx::getStatus(FuncRequest(LFUN_CHANGES_TRACK)).enabled() &&
928                         lyx::getStatus(FuncRequest(LFUN_CHANGES_TRACK)).onoff(true);
929
930                 d.toolbars_->update(math, table, review);
931         } else
932                 d.toolbars_->update(false, false, false);
933
934         // update read-only status of open dialogs.
935         getDialogs().checkStatus();
936 }
937
938
939 ToolbarInfo * GuiViewBase::getToolbarInfo(string const & name)
940 {
941         return d.toolbars_->getToolbarInfo(name);
942 }
943
944
945 void GuiViewBase::toggleToolbarState(string const & name, bool allowauto)
946 {
947         // it is possible to get current toolbar status like this,...
948         // but I decide to obey the order of ToolbarBackend::flags
949         // and disregard real toolbar status.
950         // toolbars_->saveToolbarInfo();
951         //
952         // toggle state on/off/auto
953         d.toolbars_->toggleToolbarState(name, allowauto);
954         // update toolbar
955         updateToolbars();
956 }
957
958
959 } // namespace frontend
960 } // namespace lyx
961
962 #include "GuiView_moc.cpp"