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