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