]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/GuiWorkArea.cpp
small facelift for close button on tabwidget
[lyx.git] / src / frontends / qt4 / GuiWorkArea.cpp
1 /**
2  * \file GuiWorkArea.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author John Levon
7  * \author Abdelrazak Younes
8  *
9  * Full author contact details are available in file CREDITS.
10  */
11
12 #include <config.h>
13
14 #include "GuiWorkArea.h"
15
16 #include "GuiApplication.h"
17 #include "GuiPainter.h"
18 #include "GuiKeySymbol.h"
19 #include "qt_helpers.h"
20
21 #include "frontends/LyXView.h"
22
23 #include "Buffer.h"
24 #include "BufferView.h"
25 #include "Color.h"
26 #include "Cursor.h"
27 #include "debug.h"
28 #include "FuncRequest.h"
29 #include "LyXFunc.h"
30 #include "LyXRC.h"
31 #include "version.h"
32
33 #include "support/filetools.h" // LibFileSearch
34
35 #include "graphics/GraphicsImage.h"
36 #include "graphics/GraphicsLoader.h"
37
38 #include <QInputContext>
39 #include <QLayout>
40 #include <QMainWindow>
41 #include <QPainter>
42 #include <QToolButton>
43 #include <QPalette>
44 #include <QScrollBar>
45 #include <QTabBar>
46 #include <QTimer>
47
48 #include <boost/bind.hpp>
49 #include <boost/current_function.hpp>
50
51 #ifdef Q_WS_X11
52 #include <QX11Info>
53 extern "C" int XEventsQueued(Display *display, int mode);
54 #endif
55
56 #ifdef Q_WS_WIN
57 int const CursorWidth = 2;
58 #else
59 int const CursorWidth = 1;
60 #endif
61
62 #undef KeyPress
63 #undef NoModifier 
64
65 using std::endl;
66 using std::string;
67
68 namespace lyx {
69
70 using support::FileName;
71
72 /// return the LyX mouse button state from Qt's
73 static mouse_button::state q_button_state(Qt::MouseButton button)
74 {
75         mouse_button::state b = mouse_button::none;
76         switch (button) {
77                 case Qt::LeftButton:
78                         b = mouse_button::button1;
79                         break;
80                 case Qt::MidButton:
81                         b = mouse_button::button2;
82                         break;
83                 case Qt::RightButton:
84                         b = mouse_button::button3;
85                         break;
86                 default:
87                         break;
88         }
89         return b;
90 }
91
92
93 /// return the LyX mouse button state from Qt's
94 mouse_button::state q_motion_state(Qt::MouseButtons state)
95 {
96         mouse_button::state b = mouse_button::none;
97         if (state & Qt::LeftButton)
98                 b |= mouse_button::button1;
99         if (state & Qt::MidButton)
100                 b |= mouse_button::button2;
101         if (state & Qt::RightButton)
102                 b |= mouse_button::button3;
103         return b;
104 }
105
106
107 namespace frontend {
108
109 class CursorWidget {
110 public:
111         CursorWidget() {}
112
113         void draw(QPainter & painter)
114         {
115                 if (show_ && rect_.isValid()) {
116                         switch (shape_) {
117                         case L_SHAPE:
118                                 painter.fillRect(rect_.x(), rect_.y(), CursorWidth, rect_.height(), color_);
119                                 painter.setPen(color_);
120                                 painter.drawLine(rect_.bottomLeft().x() + CursorWidth, rect_.bottomLeft().y(),
121                                                                                                  rect_.bottomRight().x(), rect_.bottomLeft().y());
122                                 break;
123                         
124                         case REVERSED_L_SHAPE:
125                                 painter.fillRect(rect_.x() + rect_.height() / 3, rect_.y(), CursorWidth, rect_.height(), color_);
126                                 painter.setPen(color_);
127                                 painter.drawLine(rect_.bottomRight().x() - CursorWidth, rect_.bottomLeft().y(),
128                                                                                                          rect_.bottomLeft().x(), rect_.bottomLeft().y());
129                                 break;
130                                         
131                         default:
132                                 painter.fillRect(rect_, color_);
133                                 break;
134                         }
135                 }
136         }
137
138         void update(int x, int y, int h, CursorShape shape)
139         {
140                 color_ = guiApp->colorCache().get(Color::cursor);
141                 shape_ = shape;
142                 switch (shape) {
143                 case L_SHAPE:
144                         rect_ = QRect(x, y, CursorWidth + h / 3, h);
145                         break;
146                 case REVERSED_L_SHAPE:
147                         rect_ = QRect(x - h / 3, y, CursorWidth + h / 3, h);
148                         break;
149                 default: 
150                         rect_ = QRect(x, y, CursorWidth, h);
151                         break;
152                 }
153         }
154
155         void show(bool set_show = true) { show_ = set_show; }
156         void hide() { show_ = false; }
157
158         QRect const & rect() { return rect_; }
159
160 private:
161         ///
162         CursorShape shape_;
163         ///
164         bool show_;
165         ///
166         QColor color_;
167         ///
168         QRect rect_;
169 };
170
171
172 // This is a 'heartbeat' generating synthetic mouse move events when the
173 // cursor is at the top or bottom edge of the viewport. One scroll per 0.2 s
174 SyntheticMouseEvent::SyntheticMouseEvent()
175         : timeout(200), restart_timeout(true),
176           x_old(-1), y_old(-1), scrollbar_value_old(-1.0)
177 {}
178
179
180 GuiWorkArea::GuiWorkArea(Buffer & buf, LyXView & lv)
181         : WorkArea(buf, lv), need_resize_(false), schedule_redraw_(false),
182           preedit_lines_(1)
183 {
184         screen_ = QPixmap(viewport()->width(), viewport()->height());
185         cursor_ = new frontend::CursorWidget();
186         cursor_->hide();
187
188         setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded);
189         setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
190         setAcceptDrops(true);
191         setMouseTracking(true);
192         setMinimumSize(100, 70);
193
194         viewport()->setAutoFillBackground(false);
195         // We don't need double-buffering nor SystemBackground on
196         // the viewport because we have our own backing pixmap.
197         viewport()->setAttribute(Qt::WA_NoSystemBackground);
198
199         setFocusPolicy(Qt::WheelFocus);
200
201         viewport()->setCursor(Qt::IBeamCursor);
202
203         synthetic_mouse_event_.timeout.timeout.connect(
204                 boost::bind(&GuiWorkArea::generateSyntheticMouseEvent,
205                             this));
206
207         // Initialize the vertical Scroll Bar
208         QObject::connect(verticalScrollBar(), SIGNAL(actionTriggered(int)),
209                 this, SLOT(adjustViewWithScrollBar(int)));
210
211         // disable context menu for the scrollbar
212         verticalScrollBar()->setContextMenuPolicy(Qt::NoContextMenu);
213
214         // PageStep only depends on the viewport height.
215         verticalScrollBar()->setPageStep(viewport()->height());
216
217         LYXERR(Debug::GUI) << BOOST_CURRENT_FUNCTION
218                 << "\n Area width\t" << width()
219                 << "\n Area height\t" << height()
220                 << "\n viewport width\t" << viewport()->width()
221                 << "\n viewport height\t" << viewport()->height()
222                 << endl;
223
224         // Enables input methods for asian languages.
225         // Must be set when creating custom text editing widgets.
226         setAttribute(Qt::WA_InputMethodEnabled, true);
227 }
228
229
230 void GuiWorkArea::setScrollbarParams(int h, int scroll_pos, int scroll_line_step)
231 {
232         if (verticalScrollBarPolicy() != Qt::ScrollBarAlwaysOn)
233                 setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
234
235         verticalScrollBar()->setTracking(false);
236
237         // do what cursor movement does (some grey)
238         h += height() / 4;
239         int scroll_max_ = std::max(0, h - height());
240
241         verticalScrollBar()->setRange(0, scroll_max_);
242         verticalScrollBar()->setSliderPosition(scroll_pos);
243         verticalScrollBar()->setSingleStep(scroll_line_step);
244         verticalScrollBar()->setValue(scroll_pos);
245
246         verticalScrollBar()->setTracking(true);
247 }
248
249
250 void GuiWorkArea::adjustViewWithScrollBar(int action)
251 {
252         stopBlinkingCursor();
253         if (action == QAbstractSlider::SliderPageStepAdd)
254                 buffer_view_->scrollDown(viewport()->height());
255         else if (action == QAbstractSlider::SliderPageStepSub)
256                 buffer_view_->scrollUp(viewport()->height());
257         else
258                 buffer_view_->scrollDocView(verticalScrollBar()->sliderPosition());
259
260         if (lyxrc.cursor_follows_scrollbar) {
261                 buffer_view_->setCursorFromScrollbar();
262                 lyx_view_->updateLayoutChoice();
263         }
264         // Show the cursor immediately after any operation.
265         startBlinkingCursor();
266         QApplication::syncX();
267 }
268
269
270 void GuiWorkArea::focusInEvent(QFocusEvent * /*event*/)
271 {
272         // Repaint the whole screen.
273         // Note: this is different from redraw() as only the backing pixmap
274         // will be redrawn, which is cheap.
275         viewport()->repaint();
276
277         startBlinkingCursor();
278 }
279
280
281 void GuiWorkArea::focusOutEvent(QFocusEvent * /*event*/)
282 {
283         stopBlinkingCursor();
284 }
285
286
287 void GuiWorkArea::mousePressEvent(QMouseEvent * e)
288 {
289         if (dc_event_.active && dc_event_ == *e) {
290                 dc_event_.active = false;
291                 FuncRequest cmd(LFUN_MOUSE_TRIPLE,
292                         e->x(), e->y(),
293                         q_button_state(e->button()));
294                 dispatch(cmd);
295                 return;
296         }
297
298         inputContext()->reset();
299
300         FuncRequest const cmd(LFUN_MOUSE_PRESS, e->x(), e->y(),
301                 q_button_state(e->button()));
302         dispatch(cmd, q_key_state(e->modifiers()));
303 }
304
305
306 void GuiWorkArea::mouseReleaseEvent(QMouseEvent * e)
307 {
308         if (synthetic_mouse_event_.timeout.running())
309                 synthetic_mouse_event_.timeout.stop();
310
311         FuncRequest const cmd(LFUN_MOUSE_RELEASE, e->x(), e->y(),
312                               q_button_state(e->button()));
313         dispatch(cmd);
314 }
315
316
317 void GuiWorkArea::mouseMoveEvent(QMouseEvent * e)
318 {
319         // we kill the triple click if we move
320         doubleClickTimeout();
321         FuncRequest cmd(LFUN_MOUSE_MOTION, e->x(), e->y(),
322                               q_motion_state(e->buttons()));
323
324         // If we're above or below the work area...
325         if (e->y() <= 20 || e->y() >= viewport()->height() - 20) {
326                 // Make sure only a synthetic event can cause a page scroll,
327                 // so they come at a steady rate:
328                 if (e->y() <= 20)
329                         // _Force_ a scroll up:
330                         cmd.y = -40;
331                 else
332                         cmd.y = viewport()->height();
333                 // Store the event, to be handled when the timeout expires.
334                 synthetic_mouse_event_.cmd = cmd;
335
336                 if (synthetic_mouse_event_.timeout.running())
337                         // Discard the event. Note that it _may_ be handled
338                         // when the timeout expires if
339                         // synthetic_mouse_event_.cmd has not been overwritten.
340                         // Ie, when the timeout expires, we handle the
341                         // most recent event but discard all others that
342                         // occurred after the one used to start the timeout
343                         // in the first place.
344                         return;
345                 else {
346                         synthetic_mouse_event_.restart_timeout = true;
347                         synthetic_mouse_event_.timeout.start();
348                         // Fall through to handle this event...
349                 }
350
351         } else if (synthetic_mouse_event_.timeout.running()) {
352                 // Store the event, to be possibly handled when the timeout
353                 // expires.
354                 // Once the timeout has expired, normal control is returned
355                 // to mouseMoveEvent (restart_timeout = false).
356                 // This results in a much smoother 'feel' when moving the
357                 // mouse back into the work area.
358                 synthetic_mouse_event_.cmd = cmd;
359                 synthetic_mouse_event_.restart_timeout = false;
360                 return;
361         }
362
363         // Has anything changed on-screen since the last QMouseEvent
364         // was received?
365         double const scrollbar_value = verticalScrollBar()->value();
366         if (e->x() != synthetic_mouse_event_.x_old ||
367             e->y() != synthetic_mouse_event_.y_old ||
368             scrollbar_value != synthetic_mouse_event_.scrollbar_value_old) {
369                 // Yes it has. Store the params used to check this.
370                 synthetic_mouse_event_.x_old = e->x();
371                 synthetic_mouse_event_.y_old = e->y();
372                 synthetic_mouse_event_.scrollbar_value_old = scrollbar_value;
373
374                 // ... and dispatch the event to the LyX core.
375                 dispatch(cmd);
376         }
377 }
378
379
380 void GuiWorkArea::wheelEvent(QWheelEvent * e)
381 {
382         // Wheel rotation by one notch results in a delta() of 120 (see
383         // documentation of QWheelEvent)
384         int const lines = qApp->wheelScrollLines() * e->delta() / 120;
385         verticalScrollBar()->setValue(verticalScrollBar()->value() -
386                         lines *  verticalScrollBar()->singleStep());
387         adjustViewWithScrollBar();
388 }
389
390
391 void GuiWorkArea::generateSyntheticMouseEvent()
392 {
393 // Set things off to generate the _next_ 'pseudo' event.
394         if (synthetic_mouse_event_.restart_timeout)
395                 synthetic_mouse_event_.timeout.start();
396
397         // Has anything changed on-screen since the last timeout signal
398         // was received?
399         double const scrollbar_value = verticalScrollBar()->value();
400         if (scrollbar_value != synthetic_mouse_event_.scrollbar_value_old) {
401                 // Yes it has. Store the params used to check this.
402                 synthetic_mouse_event_.scrollbar_value_old = scrollbar_value;
403
404                 // ... and dispatch the event to the LyX core.
405                 dispatch(synthetic_mouse_event_.cmd);
406         }
407 }
408
409
410 void GuiWorkArea::keyPressEvent(QKeyEvent * ev)
411 {
412         // do nothing if there are other events
413         // (the auto repeated events come too fast)
414         // \todo FIXME: remove hard coded Qt keys, process the key binding
415 #ifdef Q_WS_X11
416         if (XEventsQueued(QX11Info::display(), 0) > 1 && ev->isAutoRepeat() 
417                         && (Qt::Key_PageDown || Qt::Key_PageUp)) {
418                 LYXERR(Debug::KEY)      
419                         << BOOST_CURRENT_FUNCTION << endl
420                         << "system is busy: scroll key event ignored" << endl;
421                 ev->ignore();
422                 return;
423         }
424 #endif
425
426         LYXERR(Debug::KEY) << BOOST_CURRENT_FUNCTION
427                 << " count=" << ev->count()
428                 << " text=" << fromqstr(ev->text())
429                 << " isAutoRepeat=" << ev->isAutoRepeat()
430                 << " key=" << ev->key()
431                 << endl;
432
433         KeySymbol sym;
434         setKeySymbol(&sym, ev);
435         processKeySym(sym, q_key_state(ev->modifiers()));
436 }
437
438
439 void GuiWorkArea::doubleClickTimeout()
440 {
441         dc_event_.active = false;
442 }
443
444
445 void GuiWorkArea::mouseDoubleClickEvent(QMouseEvent * ev)
446 {
447         dc_event_ = double_click(ev);
448         QTimer::singleShot(QApplication::doubleClickInterval(), this,
449                            SLOT(doubleClickTimeout()));
450         FuncRequest cmd(LFUN_MOUSE_DOUBLE,
451                         ev->x(), ev->y(),
452                         q_button_state(ev->button()));
453         dispatch(cmd);
454 }
455
456
457 void GuiWorkArea::resizeEvent(QResizeEvent * ev)
458 {
459         QAbstractScrollArea::resizeEvent(ev);
460         need_resize_ = true;
461 }
462
463
464 void GuiWorkArea::update(int x, int y, int w, int h)
465 {
466         viewport()->repaint(x, y, w, h);
467 }
468
469
470 void GuiWorkArea::paintEvent(QPaintEvent * ev)
471 {
472         QRect const rc = ev->rect();
473         /*
474         LYXERR(Debug::PAINTING) << "paintEvent begin: x: " << rc.x()
475                 << " y: " << rc.y()
476                 << " w: " << rc.width()
477                 << " h: " << rc.height() << endl;
478         */
479
480         if (need_resize_) {
481                 verticalScrollBar()->setPageStep(viewport()->height());
482                 screen_ = QPixmap(viewport()->width(), viewport()->height());
483                 resizeBufferView();
484                 updateScreen();
485                 WorkArea::hideCursor();
486                 WorkArea::showCursor();
487                 need_resize_ = false;
488         }
489
490         QPainter pain(viewport());
491         pain.drawPixmap(rc, screen_, rc);
492         cursor_->draw(pain);
493 }
494
495
496 void GuiWorkArea::expose(int x, int y, int w, int h)
497 {
498         updateScreen();
499         update(x, y, w, h);
500 }
501
502
503 void GuiWorkArea::updateScreen()
504 {
505         GuiPainter pain(&screen_);
506         verticalScrollBar()->show();
507         buffer_view_->draw(pain);
508 }
509
510
511 void GuiWorkArea::showCursor(int x, int y, int h, CursorShape shape)
512 {
513         if (schedule_redraw_) {
514                 buffer_view_->updateMetrics(false);
515                 updateScreen();
516                 viewport()->update(QRect(0, 0, viewport()->width(), viewport()->height()));
517                 schedule_redraw_ = false;
518                 // Show the cursor immediately after the update.
519                 hideCursor();
520                 toggleCursor();
521                 return;
522         }
523
524         cursor_->update(x, y, h, shape);
525         cursor_->show();
526         viewport()->update(cursor_->rect());
527 }
528
529
530 void GuiWorkArea::removeCursor()
531 {
532         cursor_->hide();
533         //if (!qApp->focusWidget())
534                 viewport()->update(cursor_->rect());
535 }
536
537
538 void GuiWorkArea::inputMethodEvent(QInputMethodEvent * e)
539 {
540         QString const & commit_string = e->commitString();
541         docstring const & preedit_string
542                 = qstring_to_ucs4(e->preeditString());
543
544         if (!commit_string.isEmpty()) {
545
546                 LYXERR(Debug::KEY) << BOOST_CURRENT_FUNCTION
547                         << " preeditString =" << fromqstr(e->preeditString())
548                         << " commitString  =" << fromqstr(e->commitString())
549                         << endl;
550
551                 int key = 0;
552
553                 // FIXME Iwami 04/01/07: we should take care also of UTF16 surrogates here.
554                 for (int i = 0; i < commit_string.size(); ++i) {
555                         QKeyEvent ev(QEvent::KeyPress, key, Qt::NoModifier, commit_string[i]);
556                         keyPressEvent(&ev);
557                 }
558         }
559
560         // Hide the cursor during the kana-kanji transformation.
561         if (preedit_string.empty())
562                 startBlinkingCursor();
563         else
564                 stopBlinkingCursor();
565
566         // last_width : for checking if last preedit string was/wasn't empty.
567         static bool last_width = false;
568         if (!last_width && preedit_string.empty()) {
569                 // if last_width is last length of preedit string.
570                 e->accept();
571                 return;
572         }
573
574         GuiPainter pain(&screen_);
575         buffer_view_->updateMetrics(false);
576         buffer_view_->draw(pain);
577         Font font = buffer_view_->cursor().getFont();
578         FontMetrics const & fm = theFontMetrics(font);
579         int height = fm.maxHeight();
580         int cur_x = cursor_->rect().left();
581         int cur_y = cursor_->rect().bottom();
582
583         // redraw area of preedit string.
584         update(0, cur_y - height, GuiWorkArea::width(),
585                 (height + 1) * preedit_lines_);
586
587         if (preedit_string.empty()) {
588                 last_width = false;
589                 preedit_lines_ = 1;
590                 e->accept();
591                 return;
592         }
593         last_width = true;
594
595         // att : stores an IM attribute.
596         QList<QInputMethodEvent::Attribute> const & att = e->attributes();
597
598         // get attributes of input method cursor.
599         // cursor_pos : cursor position in preedit string.
600         size_t cursor_pos = 0;
601         bool cursor_is_visible = false;
602         for (int i = 0; i < att.size(); ++i) {
603                 if (att.at(i).type == QInputMethodEvent::Cursor) {
604                         cursor_pos = att.at(i).start;
605                         cursor_is_visible = att.at(i).length != 0;
606                         break;
607                 }
608         }
609
610         size_t preedit_length = preedit_string.length();
611
612         // get position of selection in input method.
613         // FIXME: isn't there a way to do this simplier?
614         // rStart : cursor position in selected string in IM.
615         size_t rStart = 0;
616         // rLength : selected string length in IM.
617         size_t rLength = 0;
618         if (cursor_pos < preedit_length) {
619                 for (int i = 0; i < att.size(); ++i) {
620                         if (att.at(i).type == QInputMethodEvent::TextFormat) {
621                                 if (att.at(i).start <= int(cursor_pos)
622                                         && int(cursor_pos) < att.at(i).start + att.at(i).length) {
623                                                 rStart = att.at(i).start;
624                                                 rLength = att.at(i).length;
625                                                 if (!cursor_is_visible)
626                                                         cursor_pos += rLength;
627                                                 break;
628                                 }
629                         }
630                 }
631         }
632         else {
633                 rStart = cursor_pos;
634                 rLength = 0;
635         }
636
637         int const right_margin = rightMargin();
638         Painter::preedit_style ps;
639         // Most often there would be only one line:
640         preedit_lines_ = 1;
641         for (size_t pos = 0; pos != preedit_length; ++pos) {
642                 char_type const typed_char = preedit_string[pos];
643                 // reset preedit string style
644                 ps = Painter::preedit_default;
645
646                 // if we reached the right extremity of the screen, go to next line.
647                 if (cur_x + fm.width(typed_char) > GuiWorkArea::width() - right_margin) {
648                         cur_x = right_margin;
649                         cur_y += height + 1;
650                         ++preedit_lines_;
651                 }
652                 // preedit strings are displayed with dashed underline
653                 // and partial strings are displayed white on black indicating
654                 // that we are in selecting mode in the input method.
655                 // FIXME: rLength == preedit_length is not a changing condition
656                 // FIXME: should be put out of the loop.
657                 if (pos >= rStart
658                         && pos < rStart + rLength
659                         && !(cursor_pos < rLength && rLength == preedit_length))
660                         ps = Painter::preedit_selecting;
661
662                 if (pos == cursor_pos
663                         && (cursor_pos < rLength && rLength == preedit_length))
664                         ps = Painter::preedit_cursor;
665
666                 // draw one character and update cur_x.
667                 cur_x += pain.preeditText(cur_x, cur_y, typed_char, font, ps);
668         }
669
670         // update the preedit string screen area.
671         update(0, cur_y - preedit_lines_*height, GuiWorkArea::width(),
672                 (height + 1) * preedit_lines_);
673
674         // Don't forget to accept the event!
675         e->accept();
676 }
677
678
679 QVariant GuiWorkArea::inputMethodQuery(Qt::InputMethodQuery query) const
680 {
681         QRect cur_r(0,0,0,0);
682         switch (query) {
683                 // this is the CJK-specific composition window position.
684                 case Qt::ImMicroFocus:
685                         cur_r = cursor_->rect();
686                         if (preedit_lines_ != 1)
687                                 cur_r.moveLeft(10);
688                         cur_r.moveBottom(cur_r.bottom() + cur_r.height() * preedit_lines_);
689                         // return lower right of cursor in LyX.
690                         return cur_r;
691                 default:
692                         return QWidget::inputMethodQuery(query);
693         }
694 }
695
696 ////////////////////////////////////////////////////////////////////
697 // TabWorkArea implementation.
698 ////////////////////////////////////////////////////////////////////
699 TabWorkArea::TabWorkArea(QWidget * parent): QTabWidget(parent)
700 {
701         QPalette pal = palette();
702         pal.setColor(QPalette::Active, QPalette::Button, pal.color(QPalette::Active, QPalette::Window));
703         pal.setColor(QPalette::Disabled, QPalette::Button, pal.color(QPalette::Disabled, QPalette::Window));
704         pal.setColor(QPalette::Inactive, QPalette::Button, pal.color(QPalette::Inactive, QPalette::Window));
705
706         QToolButton * closeTabButton = new QToolButton(this);
707     closeTabButton->setPalette(pal);
708         FileName const file = support::libFileSearch("images", "closetab", "png");
709         if (!file.empty()) {
710                 QPixmap pm(toqstr(file.absFilename()));
711                 closeTabButton->setIcon(QIcon(pm));
712         } else {
713                 closeTabButton->setText("Close");
714         }
715         closeTabButton->setAutoRaise(true);
716         closeTabButton->setCursor(Qt::ArrowCursor);
717         closeTabButton->setToolTip(tr("Close tab"));
718         closeTabButton->setEnabled(true);
719
720         QObject::connect(this, SIGNAL(currentChanged(int)),
721                 this, SLOT(on_currentTabChanged(int)));
722         QObject::connect(closeTabButton, SIGNAL(clicked()),
723                 this, SLOT(closeCurrentTab()));
724
725         setCornerWidget(closeTabButton);
726 #if QT_VERSION >= 0x040200
727         setUsesScrollButtons(true);
728 #endif
729 }
730
731
732 void TabWorkArea::showBar(bool show)
733 {
734         tabBar()->setVisible(show);
735 }
736
737
738 void TabWorkArea::closeAll()
739 {
740         while (count()) {
741                 GuiWorkArea * wa = dynamic_cast<GuiWorkArea *>(widget(0));
742                 BOOST_ASSERT(wa);
743                 removeTab(0);
744                 delete wa;
745         }
746 }
747
748
749 bool TabWorkArea::setCurrentWorkArea(GuiWorkArea * work_area)
750 {
751         BOOST_ASSERT(work_area);
752         int index = indexOf(work_area);
753         if (index == -1)
754                 return false;
755
756         if (index == currentIndex())
757                 // Make sure the work area is up to date.
758                 on_currentTabChanged(index);
759         else
760                 // Switch to the work area.
761                 setCurrentIndex(index);
762         work_area->setFocus();
763
764         return true;
765 }
766
767
768 bool TabWorkArea::removeWorkArea(GuiWorkArea * work_area)
769 {
770         BOOST_ASSERT(work_area);
771         int index = indexOf(work_area);
772         if (index == -1)
773                 return false;
774
775         work_area->setUpdatesEnabled(false);
776         removeTab(index);
777         delete work_area;
778
779         if (count()) {
780                 // make sure the next work area is enabled.
781                 currentWidget()->setUpdatesEnabled(true);
782                 // Hide tabbar if there's only one tab.
783                 showBar(count() > 1);
784         }
785         return true;
786 }
787
788
789 void TabWorkArea::on_currentTabChanged(int i)
790 {
791         GuiWorkArea * wa = dynamic_cast<GuiWorkArea *>(widget(i));
792         BOOST_ASSERT(wa);
793         BufferView & bv = wa->bufferView();
794         bv.updateMetrics(false);
795         bv.cursor().fixIfBroken();
796         wa->setUpdatesEnabled(true);
797         wa->redraw();
798         wa->setFocus();
799         ///
800         currentWorkAreaChanged(wa);
801
802         LYXERR(Debug::GUI) << "currentTabChanged " << i
803                 << "File" << bv.buffer().fileName() << endl;
804 }
805
806
807 void TabWorkArea::closeCurrentTab()
808 {
809         lyx::dispatch(FuncRequest(LFUN_BUFFER_CLOSE));
810 }
811
812 } // namespace frontend
813 } // namespace lyx
814
815 #include "GuiWorkArea_moc.cpp"