]> git.lyx.org Git - lyx.git/blobdiff - src/frontends/qt4/GuiWorkArea.cpp
fix memory leaks
[lyx.git] / src / frontends / qt4 / GuiWorkArea.cpp
index 29be5d68acc287f2c1976cfe951f703544a0b417..2c53eb524ff99b995b1e6a4b8e0073ae0ffb8367 100644 (file)
 #include "BufferView.h"
 #include "CoordCache.h"
 #include "Cursor.h"
-#include "debug.h"
 #include "Font.h"
 #include "FuncRequest.h"
-#include "gettext.h"
 #include "GuiApplication.h"
 #include "GuiKeySymbol.h"
 #include "GuiPainter.h"
 #include "graphics/GraphicsImage.h"
 #include "graphics/GraphicsLoader.h"
 
+#include "support/debug.h"
+#include "support/gettext.h"
 #include "support/FileName.h"
-#include "support/ForkedcallsController.h"
+#include "support/ForkedCalls.h"
 
 #include "frontends/Application.h"
-#include "frontends/Dialogs.h"  // only used in setReadOnly
 #include "frontends/FontMetrics.h"
 #include "frontends/WorkAreaManager.h"
 
@@ -56,7 +55,6 @@
 #include <QTimer>
 
 #include <boost/bind.hpp>
-#include <boost/current_function.hpp>
 
 #ifdef Q_WS_X11
 #include <QX11Info>
@@ -80,7 +78,6 @@ using std::string;
 namespace lyx {
 
 using support::FileName;
-using support::ForkedcallsController;
 
 
 /// return the LyX mouse button state from Qt's
@@ -192,24 +189,17 @@ SyntheticMouseEvent::SyntheticMouseEvent()
 
 
 
-// All the below connection objects are needed because of a bug in some
-// versions of GCC (<=2.96 are on the suspects list.) By having and assigning
-// to these connections we avoid a segfault upon startup, and also at exit.
-// (Lgb)
-
-static boost::signals::connection timecon;
-
-
 GuiWorkArea::GuiWorkArea(Buffer & buffer, GuiView & lv)
        : buffer_view_(new BufferView(buffer)), lyx_view_(&lv),
-         cursor_visible_(false), cursor_timeout_(400),
+         cursor_visible_(false),
     need_resize_(false), schedule_redraw_(false),
                preedit_lines_(1)
 {
        buffer.workAreaManager().add(this);
        // Setup the signals
-       timecon = cursor_timeout_.timeout
-               .connect(boost::bind(&GuiWorkArea::toggleCursor, this));
+       cursor_timeout_.setInterval(400);
+       connect(&cursor_timeout_, SIGNAL(timeout()),
+               this, SLOT(toggleCursor()));
 
        cursor_timeout_.start();
 
@@ -217,7 +207,7 @@ GuiWorkArea::GuiWorkArea(Buffer & buffer, GuiView & lv)
        cursor_ = new frontend::CursorWidget();
        cursor_->hide();
 
-       setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded);
+       setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
        setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
        setAcceptDrops(true);
        setMouseTracking(true);
@@ -247,12 +237,8 @@ GuiWorkArea::GuiWorkArea(Buffer & buffer, GuiView & lv)
        // PageStep only depends on the viewport height.
        verticalScrollBar()->setPageStep(viewport()->height());
 
-       LYXERR(Debug::GUI) << BOOST_CURRENT_FUNCTION
-               << "\n Area width\t" << width()
-               << "\n Area height\t" << height()
-               << "\n viewport width\t" << viewport()->width()
-               << "\n viewport height\t" << viewport()->height()
-               << endl;
+       LYXERR(Debug::GUI, "viewport width: " << viewport()->width()
+               << "  viewport height: " << viewport()->height());
 
        // Enables input methods for asian languages.
        // Must be set when creating custom text editing widgets.
@@ -265,6 +251,7 @@ GuiWorkArea::~GuiWorkArea()
 {
        buffer_view_->buffer().workAreaManager().remove(this);
        delete buffer_view_;
+       delete cursor_;
 }
 
 
@@ -296,7 +283,7 @@ void GuiWorkArea::stopBlinkingCursor()
 void GuiWorkArea::startBlinkingCursor()
 {
        showCursor();
-       cursor_timeout_.restart();
+       cursor_timeout_.start();
 }
 
 
@@ -308,7 +295,7 @@ void GuiWorkArea::redraw()
 
        // No need to do anything if this is the current view. The BufferView
        // metrics are already up to date.
-       if (lyx_view_ != theApp()->currentView()) {
+       if (lyx_view_ != guiApp->currentView()) {
                // FIXME: it would be nice to optimize for the off-screen case.
                buffer_view_->updateMetrics();
                buffer_view_->cursor().fixIfBroken();
@@ -323,18 +310,11 @@ void GuiWorkArea::redraw()
                showCursor();
        }
        
-       ViewMetricsInfo const & vi = buffer_view_->viewMetricsInfo();
-
-       LYXERR(Debug::WORKAREA) << "WorkArea::redraw screen" << endl;
-
-       int const ymin = std::max(vi.y1, 0);
-       int const ymax = vi.p2 < vi.size - 1 ? vi.y2 : height();
-
-       expose(0, ymin, width(), ymax - ymin);
+       LYXERR(Debug::WORKAREA, "WorkArea::redraw screen");
+       updateScreen();
+       update(0, 0, viewport()->width(), viewport()->height());
 
-       //LYXERR(Debug::WORKAREA)
-       //<< "  ymin = " << ymin << "  width() = " << width()
-//             << "  ymax-ymin = " << ymax-ymin << std::endl;
+       lyx_view_->updateStatusBar();
 
        if (lyxerr.debugging(Debug::WORKAREA))
                buffer_view_->coordCache().dump();
@@ -385,7 +365,7 @@ void GuiWorkArea::dispatch(FuncRequest const & cmd0, KeyModifier mod)
 
        // Skip these when selecting
        if (cmd.action != LFUN_MOUSE_MOTION) {
-               lyx_view_->updateLayoutChoice(false);
+               lyx_view_->updateLayoutList();
                lyx_view_->updateToolbars();
        }
 
@@ -408,18 +388,10 @@ void GuiWorkArea::resizeBufferView()
        // WARNING: Please don't put any code that will trigger a repaint here!
        // We are already inside a paint event.
        lyx_view_->setBusy(true);
-       buffer_view_->resize(width(), height());
-       lyx_view_->updateLayoutChoice(false);
+       buffer_view_->resize(viewport()->width(), viewport()->height());
+       lyx_view_->updateLayoutList();
        lyx_view_->setBusy(false);
-}
-
-
-void GuiWorkArea::updateScrollbar()
-{
-       buffer_view_->updateScrollbar();
-       ScrollbarParameters const & scroll_ = buffer_view_->scrollbarParameters();
-       setScrollbarParams(scroll_.height, scroll_.position,
-               scroll_.lineScrollHeight);
+       need_resize_ = false;
 }
 
 
@@ -456,7 +428,7 @@ void GuiWorkArea::showCursor()
        y -= asc;
 
        // if it doesn't touch the screen, don't try to show it
-       if (y + h < 0 || y >= height())
+       if (y + h < 0 || y >= viewport()->height())
                return;
 
        cursor_visible_ = true;
@@ -484,28 +456,25 @@ void GuiWorkArea::toggleCursor()
        // Use this opportunity to deal with any child processes that
        // have finished but are waiting to communicate this fact
        // to the rest of LyX.
-       ForkedcallsController & fcc = ForkedcallsController::get();
-       fcc.handleCompletedProcesses();
-
-       cursor_timeout_.restart();
+       support::ForkedCallsController::handleCompletedProcesses();
 }
 
 
-void GuiWorkArea::setScrollbarParams(int h, int scroll_pos, int scroll_line_step)
+void GuiWorkArea::updateScrollbar()
 {
-       if (verticalScrollBarPolicy() != Qt::ScrollBarAlwaysOn)
-               setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
-
        verticalScrollBar()->setTracking(false);
 
+       buffer_view_->updateScrollbar();
+       ScrollbarParameters const & scroll_ = buffer_view_->scrollbarParameters();
+
        // do what cursor movement does (some grey)
-       h += height() / 4;
-       int scroll_max_ = std::max(0, h - height());
+       int const h = scroll_.height + viewport()->height() / 4;
+       int scroll_max_ = std::max(0, h - viewport()->height());
 
        verticalScrollBar()->setRange(0, scroll_max_);
-       verticalScrollBar()->setSliderPosition(scroll_pos);
-       verticalScrollBar()->setSingleStep(scroll_line_step);
-       verticalScrollBar()->setValue(scroll_pos);
+       verticalScrollBar()->setSliderPosition(scroll_.position);
+       verticalScrollBar()->setSingleStep(scroll_.lineScrollHeight);
+       verticalScrollBar()->setValue(scroll_.position);
 
        verticalScrollBar()->setTracking(true);
 }
@@ -523,7 +492,7 @@ void GuiWorkArea::adjustViewWithScrollBar(int action)
 
        if (lyxrc.cursor_follows_scrollbar) {
                buffer_view_->setCursorFromScrollbar();
-               lyx_view_->updateLayoutChoice(false);
+               lyx_view_->updateLayoutList();
        }
        // Show the cursor immediately after any operation.
        startBlinkingCursor();
@@ -678,20 +647,15 @@ void GuiWorkArea::keyPressEvent(QKeyEvent * ev)
 #ifdef Q_WS_X11
        if (XEventsQueued(QX11Info::display(), 0) > 1 && ev->isAutoRepeat() 
                        && (Qt::Key_PageDown || Qt::Key_PageUp)) {
-               LYXERR(Debug::KEY)      
-                       << BOOST_CURRENT_FUNCTION << endl
-                       << "system is busy: scroll key event ignored" << endl;
+               LYXERR(Debug::KEY, "system is busy: scroll key event ignored");
                ev->ignore();
                return;
        }
 #endif
 
-       LYXERR(Debug::KEY) << BOOST_CURRENT_FUNCTION
-               << " count=" << ev->count()
-               << " text=" << fromqstr(ev->text())
-               << " isAutoRepeat=" << ev->isAutoRepeat()
-               << " key=" << ev->key()
-               << endl;
+       LYXERR(Debug::KEY, " count: " << ev->count()
+               << " text: " << fromqstr(ev->text())
+               << " isAutoRepeat: " << ev->isAutoRepeat() << " key: " << ev->key());
 
        KeySymbol sym;
        setKeySymbol(&sym, ev);
@@ -733,12 +697,8 @@ void GuiWorkArea::update(int x, int y, int w, int h)
 void GuiWorkArea::paintEvent(QPaintEvent * ev)
 {
        QRect const rc = ev->rect();
-       /*
-       LYXERR(Debug::PAINTING) << "paintEvent begin: x: " << rc.x()
-               << " y: " << rc.y()
-               << " w: " << rc.width()
-               << " h: " << rc.height() << endl;
-       */
+       // LYXERR(Debug::PAINTING, "paintEvent begin: x: " << rc.x()
+       //      << " y: " << rc.y() << " w: " << rc.width() << " h: " << rc.height());
 
        if (need_resize_) {
                verticalScrollBar()->setPageStep(viewport()->height());
@@ -747,7 +707,6 @@ void GuiWorkArea::paintEvent(QPaintEvent * ev)
                updateScreen();
                hideCursor();
                showCursor();
-               need_resize_ = false;
        }
 
        QPainter pain(viewport());
@@ -756,30 +715,9 @@ void GuiWorkArea::paintEvent(QPaintEvent * ev)
 }
 
 
-void GuiWorkArea::expose(int x, int y, int w, int h)
-{
-       updateScreen();
-       update(x, y, w, h);
-}
-
-
-void GuiWorkArea::setWindowTitle(docstring const & t, docstring const & it)
-{
-       QString title = windowTitle();
-       QString new_title = toqstr(t);
-       if (title == new_title)
-               return;
-
-       QWidget::setWindowTitle(new_title);
-       QWidget::setWindowIconText(toqstr(it));
-       titleChanged(this);
-}
-
-
 void GuiWorkArea::updateScreen()
 {
        GuiPainter pain(&screen_);
-       verticalScrollBar()->show();
        buffer_view_->draw(pain);
 }
 
@@ -819,15 +757,13 @@ void GuiWorkArea::inputMethodEvent(QInputMethodEvent * e)
 
        if (!commit_string.isEmpty()) {
 
-               LYXERR(Debug::KEY) << BOOST_CURRENT_FUNCTION
-                       << " preeditString =" << fromqstr(e->preeditString())
-                       << " commitString  =" << fromqstr(e->commitString())
-                       << endl;
+               LYXERR(Debug::KEY, "preeditString: " << fromqstr(e->preeditString())
+                       << " commitString: " << fromqstr(e->commitString()));
 
                int key = 0;
 
                // FIXME Iwami 04/01/07: we should take care also of UTF16 surrogates here.
-               for (int i = 0; i < commit_string.size(); ++i) {
+               for (int i = 0; i != commit_string.size(); ++i) {
                        QKeyEvent ev(QEvent::KeyPress, key, Qt::NoModifier, commit_string[i]);
                        keyPressEvent(&ev);
                }
@@ -857,7 +793,7 @@ void GuiWorkArea::inputMethodEvent(QInputMethodEvent * e)
        int cur_y = cursor_->rect().bottom();
 
        // redraw area of preedit string.
-       update(0, cur_y - height, GuiWorkArea::width(),
+       update(0, cur_y - height, viewport()->width(),
                (height + 1) * preedit_lines_);
 
        if (preedit_string.empty()) {
@@ -875,7 +811,7 @@ void GuiWorkArea::inputMethodEvent(QInputMethodEvent * e)
        // cursor_pos : cursor position in preedit string.
        size_t cursor_pos = 0;
        bool cursor_is_visible = false;
-       for (int i = 0; i < att.size(); ++i) {
+       for (int i = 0; i != att.size(); ++i) {
                if (att.at(i).type == QInputMethodEvent::Cursor) {
                        cursor_pos = att.at(i).start;
                        cursor_is_visible = att.at(i).length != 0;
@@ -892,7 +828,7 @@ void GuiWorkArea::inputMethodEvent(QInputMethodEvent * e)
        // rLength : selected string length in IM.
        size_t rLength = 0;
        if (cursor_pos < preedit_length) {
-               for (int i = 0; i < att.size(); ++i) {
+               for (int i = 0; i != att.size(); ++i) {
                        if (att.at(i).type == QInputMethodEvent::TextFormat) {
                                if (att.at(i).start <= int(cursor_pos)
                                        && int(cursor_pos) < att.at(i).start + att.at(i).length) {
@@ -920,7 +856,7 @@ void GuiWorkArea::inputMethodEvent(QInputMethodEvent * e)
                ps = Painter::preedit_default;
 
                // if we reached the right extremity of the screen, go to next line.
-               if (cur_x + fm.width(typed_char) > GuiWorkArea::width() - right_margin) {
+               if (cur_x + fm.width(typed_char) > viewport()->width() - right_margin) {
                        cur_x = right_margin;
                        cur_y += height + 1;
                        ++preedit_lines_;
@@ -944,7 +880,7 @@ void GuiWorkArea::inputMethodEvent(QInputMethodEvent * e)
        }
 
        // update the preedit string screen area.
-       update(0, cur_y - preedit_lines_*height, GuiWorkArea::width(),
+       update(0, cur_y - preedit_lines_*height, viewport()->width(),
                (height + 1) * preedit_lines_);
 
        // Don't forget to accept the event!
@@ -988,7 +924,14 @@ void GuiWorkArea::updateWindowTitle()
                        maximize_title += _(" (read only)");
        }
 
-       setWindowTitle(maximize_title, minimize_title);
+       QString title = windowTitle();
+       QString new_title = toqstr(maximize_title);
+       if (title == new_title)
+               return;
+
+       QWidget::setWindowTitle(new_title);
+       QWidget::setWindowIconText(toqstr(minimize_title));
+       titleChanged(this);
 }
 
 
@@ -996,7 +939,7 @@ void GuiWorkArea::setReadOnly(bool)
 {
        updateWindowTitle();
        if (this == lyx_view_->currentWorkArea())
-               lyx_view_->getDialogs().updateBufferDependent(false);
+               lyx_view_->updateBufferDependent(false);
 }
 
 
@@ -1031,14 +974,13 @@ TabWorkArea::TabWorkArea(QWidget * parent) : QTabWidget(parent)
                this, SLOT(closeCurrentTab()));
 
        setCornerWidget(closeTabButton);
-#if QT_VERSION >= 0x040200
        setUsesScrollButtons(true);
-#endif
 }
 
 
 void TabWorkArea::showBar(bool show)
 {
+       tabBar()->setEnabled(show);
        tabBar()->setVisible(show);
 }
 
@@ -1096,6 +1038,22 @@ bool TabWorkArea::setCurrentWorkArea(GuiWorkArea * work_area)
 }
 
 
+GuiWorkArea * TabWorkArea::addWorkArea(Buffer & buffer, GuiView & view)
+{
+       GuiWorkArea * wa = new GuiWorkArea(buffer, view);
+       wa->setUpdatesEnabled(false);
+       // Hide tabbar if there's no tab (avoid a resize and a flashing tabbar
+       // when hiding it again below).
+       showBar(count() > 0);
+       addTab(wa, wa->windowTitle());
+       QObject::connect(wa, SIGNAL(titleChanged(GuiWorkArea *)),
+               this, SLOT(updateTabText(GuiWorkArea *)));
+       // Hide tabbar if there's only one tab.
+       showBar(count() > 1);
+       return wa;
+}
+
+
 bool TabWorkArea::removeWorkArea(GuiWorkArea * work_area)
 {
        BOOST_ASSERT(work_area);
@@ -1122,16 +1080,16 @@ void TabWorkArea::on_currentTabChanged(int i)
        GuiWorkArea * wa = dynamic_cast<GuiWorkArea *>(widget(i));
        BOOST_ASSERT(wa);
        BufferView & bv = wa->bufferView();
-       bv.updateMetrics();
        bv.cursor().fixIfBroken();
+       bv.updateMetrics();
        wa->setUpdatesEnabled(true);
        wa->redraw();
        wa->setFocus();
        ///
        currentWorkAreaChanged(wa);
 
-       LYXERR(Debug::GUI) << "currentTabChanged " << i
-               << "File" << bv.buffer().absFileName() << endl;
+       LYXERR(Debug::GUI, "currentTabChanged " << i
+               << "File" << bv.buffer().absFileName());
 }