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