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