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