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