From b847b8c7df6617f4734ba27bbaacf51078e7912a Mon Sep 17 00:00:00 2001 From: =?utf8?q?Andr=C3=A9=20P=C3=B6nitz?= Date: Tue, 2 Oct 2007 18:27:20 +0000 Subject: [PATCH] the delegate patch git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@20667 a592a061-630c-0410-9148-cb99ea01b6c8 --- src/Buffer.cpp | 73 ++++++++++++++++++++++++++++++++++- src/Buffer.h | 52 +++++++++++++------------ src/BufferList.cpp | 4 +- src/BufferView.cpp | 49 ++++++++++++++++++++++- src/BufferView.h | 28 ++++++++------ src/CutAndPaste.cpp | 1 + src/Text.cpp | 1 + src/Text2.cpp | 1 + src/TextMetrics.cpp | 3 +- src/frontends/Delegates.h | 79 ++++++++++++++++++++++++++++++++++++++ src/frontends/LyXView.cpp | 81 +++++++++++---------------------------- src/frontends/LyXView.h | 57 +++++++++++++-------------- src/frontends/WorkArea.h | 1 + src/frontends/qt4/pch.h | 2 - src/mathed/MathData.cpp | 1 + src/output_docbook.cpp | 2 + src/output_latex.cpp | 1 + src/pch.h | 3 -- 18 files changed, 305 insertions(+), 134 deletions(-) create mode 100644 src/frontends/Delegates.h diff --git a/src/Buffer.cpp b/src/Buffer.cpp index 53ee61775e..5dfdd6c70a 100644 --- a/src/Buffer.cpp +++ b/src/Buffer.cpp @@ -68,6 +68,7 @@ #include "mathed/MathSupport.h" #include "frontends/alert.h" +#include "frontends/Delegates.h" #include "frontends/WorkAreaManager.h" #include "graphics/Previews.h" @@ -233,7 +234,7 @@ Buffer::Impl::Impl(Buffer & parent, FileName const & file, bool readonly_) Buffer::Buffer(string const & file, bool readonly) - : pimpl_(new Impl(*this, FileName(file), readonly)) + : pimpl_(new Impl(*this, FileName(file), readonly)), gui_(0) { LYXERR(Debug::INFO) << "Buffer::Buffer()" << endl; } @@ -269,7 +270,7 @@ Buffer::~Buffer() } -void Buffer::changed() +void Buffer::changed() const { if (pimpl_->wa_) pimpl_->wa_->redrawAll(); @@ -1919,4 +1920,72 @@ ErrorList & Buffer::errorList(string const & type) } +void Buffer::structureChanged() const +{ + if (gui_) + gui_->structureChanged(); +} + + +void Buffer::embeddingChanged() const +{ + if (gui_) + gui_->embeddingChanged(); +} + + +void Buffer::errors(std::string const & err) const +{ + if (gui_) + gui_->errors(err); +} + + +void Buffer::message(docstring const & msg) const +{ + if (gui_) + gui_->message(msg); +} + + +void Buffer::busy(bool on) const +{ + if (gui_) + gui_->busy(on); +} + + +void Buffer::readonly(bool on) const +{ + if (gui_) + gui_->readonly(on); +} + + +void Buffer::updateTitles() const +{ + if (gui_) + gui_->updateTitles(); +} + + +void Buffer::resetAutosaveTimers() const +{ + if (gui_) + gui_->resetAutosaveTimers(); +} + + +void Buffer::closing(Buffer * buf) const +{ + if (gui_) + gui_->closing(buf); +} + + +void Buffer::setGuiDelegate(frontend::GuiBufferDelegate * gui) +{ + gui_ = gui; +} + } // namespace lyx diff --git a/src/Buffer.h b/src/Buffer.h index c0b2648366..ff5e50148a 100644 --- a/src/Buffer.h +++ b/src/Buffer.h @@ -20,8 +20,6 @@ #include "support/docstring.h" #include "support/docstream.h" -#include - #include #include #include @@ -54,6 +52,7 @@ class TocBackend; class Undo; namespace frontend { +class GuiBufferDelegate; class WorkAreaManager; } @@ -67,7 +66,7 @@ class WorkAreaManager; * I am not sure if the class is complete or * minimal, probably not. * \author Lars Gullik Bjønnes - */ + */ class Buffer { public: /// What type of log will \c getLogName() return? @@ -142,30 +141,9 @@ public: /// do we have a paragraph with this id? bool hasParWithID(int id) const; - /// This signal is emitted when the buffer is changed. - void changed(); - /// frontend::WorkAreaManager & workAreaManager() const; - /// This signal is emitted when the buffer structure is changed. - boost::signal structureChanged; - /// This signal is emitted when an embedded file is changed - boost::signal embeddingChanged; - /// This signal is emitted when some parsing error shows up. - boost::signal errors; - /// This signal is emitted when some message shows up. - boost::signal message; - /// This signal is emitted when the buffer busy status change. - boost::signal busy; - /// This signal is emitted when the buffer readonly status change. - boost::signal readonly; - /// Update window titles of all users. - boost::signal updateTitles; - /// Reset autosave timers for all users. - boost::signal resetAutosaveTimers; - - /** Save file. Takes care of auto-save files and backup file if requested. Returns \c true if the save is successful, \c false otherwise. @@ -410,6 +388,30 @@ public: EmbeddedFiles & embeddedFiles(); EmbeddedFiles const & embeddedFiles() const; //@} + + /// This function is called when the buffer is changed. + void changed() const; + /// This function is called if the buffer is being closed. + void closing(Buffer *) const; + /// This function is called when the buffer structure is changed. + void structureChanged() const; + /// This function is called when an embedded file is changed + void embeddingChanged() const; + /// This function is called when some parsing error shows up. + void errors(std::string const & err) const; + /// This function is called when the buffer busy status change. + void busy(bool on) const; + /// This function is called when the buffer readonly status change. + void readonly(bool on) const; + /// Update window titles of all users. + void updateTitles() const; + /// Reset autosave timers for all users. + void resetAutosaveTimers() const; + /// + void message(docstring const & msg) const; + + void setGuiDelegate(frontend::GuiBufferDelegate * gui); + private: /** Inserts a file into a document @@ -426,6 +428,8 @@ private: /// A cache for the bibfiles (including bibfiles of loaded child /// documents), needed for appropriate update of natbib labels. mutable std::vector bibfilesCache_; + + frontend::GuiBufferDelegate * gui_; }; diff --git a/src/BufferList.cpp b/src/BufferList.cpp index cbb7f0a456..dd1b9092e5 100644 --- a/src/BufferList.cpp +++ b/src/BufferList.cpp @@ -330,7 +330,9 @@ void BufferList::emergencyWrite(Buffer * buf) { // Use ::assert to avoid a loop, BOOST_ASSERT ends up calling ::assert // compare with 0 to avoid pointer/interger comparison - assert(buf != 0); + // ::assert(buf != 0); + if (!buf) + return; // No need to save if the buffer has not changed. if (buf->isClean()) diff --git a/src/BufferView.cpp b/src/BufferView.cpp index 79662d07d2..c95a19a0c7 100644 --- a/src/BufferView.cpp +++ b/src/BufferView.cpp @@ -59,6 +59,7 @@ #include "insets/InsetText.h" #include "frontends/alert.h" +#include "frontends/Delegates.h" #include "frontends/FileDialog.h" #include "frontends/FontMetrics.h" #include "frontends/Painter.h" @@ -74,6 +75,7 @@ #include #include +#include #include #include @@ -339,7 +341,8 @@ BufferView::BufferView(Buffer & buf) : width_(0), height_(0), buffer_(buf), wh_(0), cursor_(*this), multiparsel_cache_(false), anchor_ref_(0), offset_ref_(0), - need_centering_(false), intl_(new Intl), last_inset_(0) + need_centering_(false), intl_(new Intl), last_inset_(0), + gui_(0) { xsel_cache_.set = false; intl_->initKeyMapper(lyxrc.use_kbmap); @@ -1972,4 +1975,48 @@ void BufferView::draw(frontend::Painter & pain) height_ - metrics_info_.y2, Color::bottomarea); } + +void BufferView::message(docstring const & msg) +{ + if (gui_) + gui_->message(msg); +} + + +void BufferView::showDialog(std::string const & name) +{ + if (gui_) + gui_->showDialog(name); +} + + +void BufferView::showDialogWithData(std::string const & name, + std::string const & data) +{ + if (gui_) + gui_->showDialogWithData(name, data); +} + + +void BufferView::showInsetDialog(std::string const & name, + std::string const & data, Inset * inset) +{ + if (gui_) + gui_->showInsetDialog(name, data, inset); +} + + +void BufferView::updateDialog(std::string const & name, std::string const & data) +{ + if (gui_) + gui_->updateDialog(name, data); +} + + +void BufferView::setGuiDelegate(frontend::GuiBufferViewDelegate * gui) +{ + gui_ = gui; +} + + } // namespace lyx diff --git a/src/BufferView.h b/src/BufferView.h index 8899edd1aa..1c02f954d7 100644 --- a/src/BufferView.h +++ b/src/BufferView.h @@ -23,9 +23,7 @@ #include "support/types.h" -#include #include -#include #include #include @@ -36,6 +34,7 @@ namespace lyx { namespace support { class FileName; } namespace frontend { class Painter; } +namespace frontend { class GuiBufferViewDelegate; } class Buffer; class Change; @@ -89,7 +88,7 @@ class BufferView : boost::noncopyable { public: /// BufferView(Buffer & buffer); - + /// ~BufferView(); /// return the buffer being viewed. @@ -234,24 +233,28 @@ public: /// Intl const & getIntl() const { return *intl_.get(); } + // + // Messages to the GUI + // /// This signal is emitted when some message shows up. - boost::signal message; + void message(docstring const & msg); /// This signal is emitted when some dialog needs to be shown. - boost::signal showDialog; + void showDialog(std::string const & name); /// This signal is emitted when some dialog needs to be shown with /// some data. - boost::signal showDialogWithData; + void showDialogWithData(std::string const & name, std::string const & data); /// This signal is emitted when some inset dialogs needs to be shown. - boost::signal showInsetDialog; + void showInsetDialog(std::string const & name, std::string const & data, + Inset * inset); /// This signal is emitted when some dialogs needs to be updated. - boost::signal updateDialog; + void updateDialog(std::string const & name, std::string const & data); + + /// + void setGuiDelegate(frontend::GuiBufferViewDelegate *); private: // the position relative to (0, baseline) of outermost paragraph @@ -317,6 +320,9 @@ private: /// A map from a Text to the associated text metrics typedef std::map TextMetricsCache; mutable TextMetricsCache text_metrics_; + + // Whom to notify. Not owned, so don't delete. + frontend::GuiBufferViewDelegate * gui_; }; /// some space for drawing the 'nested' markers (in pixel) diff --git a/src/CutAndPaste.cpp b/src/CutAndPaste.cpp index 20fafc2a25..6d35b0225e 100644 --- a/src/CutAndPaste.cpp +++ b/src/CutAndPaste.cpp @@ -51,6 +51,7 @@ #include #include +#include #include diff --git a/src/Text.cpp b/src/Text.cpp index 31bc420673..fc99e1336e 100644 --- a/src/Text.cpp +++ b/src/Text.cpp @@ -68,6 +68,7 @@ #include "support/convert.h" #include +#include #include diff --git a/src/Text2.cpp b/src/Text2.cpp index 284be9f2be..15a64b0b5c 100644 --- a/src/Text2.cpp +++ b/src/Text2.cpp @@ -59,6 +59,7 @@ #include "support/textutils.h" #include +#include #include diff --git a/src/TextMetrics.cpp b/src/TextMetrics.cpp index d657e21107..dc47b42587 100644 --- a/src/TextMetrics.cpp +++ b/src/TextMetrics.cpp @@ -1281,7 +1281,8 @@ Row const & TextMetrics::getRowNearY(int y, pit_type pit) const int yy = pm.position() - pm.ascent(); BOOST_ASSERT(!pm.rows().empty()); RowList::const_iterator rit = pm.rows().begin(); - RowList::const_iterator const rlast = boost::prior(pm.rows().end()); + RowList::const_iterator rlast = pm.rows().end(); + --rlast; for (; rit != rlast; yy += rit->height(), ++rit) if (yy + rit->height() > y) break; diff --git a/src/frontends/Delegates.h b/src/frontends/Delegates.h new file mode 100644 index 0000000000..0f64958a0e --- /dev/null +++ b/src/frontends/Delegates.h @@ -0,0 +1,79 @@ +// -*- C++ -*- +/** + * \file Delegates.h + * This file is part of LyX, the document processor. + * Licence details can be found in the file COPYING. + * + * \author André Pönitz + * + * Full author contact details are available in file CREDITS. + */ + +#ifndef DELEGATES_H +#define DELEGATES_H + +#include "support/docstring.h" + +namespace lyx { + +class Buffer; +class Inset; + +namespace frontend { + +class GuiBufferViewDelegate +{ +public: + virtual ~GuiBufferViewDelegate() {} + + /// This function is called when some message shows up. + virtual void message(docstring const & msg) = 0; + + /// This function is called when some dialog needs to be shown. + virtual void showDialog(std::string const & name) = 0; + + /// This function is called when some dialog needs to be shown with + /// some data. + virtual void showDialogWithData(std::string const & name, + std::string const & data) = 0; + + /// This function is called when some inset dialogs needs to be shown. + virtual void showInsetDialog(std::string const & name, + std::string const & data, Inset * inset) = 0; + + /// This function is called when some dialogs needs to be updated. + virtual void updateDialog(std::string const & name, + std::string const & data) = 0; +}; + + +class GuiBufferDelegate +{ +public: + virtual ~GuiBufferDelegate() {} + /// This function is called when the buffer is changed. + virtual void changed() = 0; + /// This function is called when the buffer structure is changed. + virtual void structureChanged() = 0; + /// This function is called when an embedded file is changed + virtual void embeddingChanged() = 0; + /// This function is called when some parsing error shows up. + virtual void errors(std::string const &) = 0; + /// This function is called when some message shows up. + virtual void message(docstring const &) = 0; + /// This function is called when the buffer busy status change. + virtual void busy(bool) = 0; + /// This function is called when the buffer readonly status change. + virtual void readonly(bool) = 0; + /// Update window titles of all users. + virtual void updateTitles() = 0; + /// Reset autosave timers for all users. + virtual void resetAutosaveTimers() = 0; + /// This signal is emitting if the buffer is being closed. + virtual void closing(Buffer *) = 0; +}; + +} // namespace frontend +} // namespace lyx + +#endif diff --git a/src/frontends/LyXView.cpp b/src/frontends/LyXView.cpp index e59ebe1ab0..10c8e32c0c 100644 --- a/src/frontends/LyXView.cpp +++ b/src/frontends/LyXView.cpp @@ -163,87 +163,35 @@ Buffer * LyXView::loadLyXFile(FileName const & filename, bool tolastfiles) void LyXView::connectBuffer(Buffer & buf) { - if (errorsConnection_.connected()) - disconnectBuffer(); - - bufferStructureChangedConnection_ = - buf.getMasterBuffer()->structureChanged.connect( - boost::bind(&LyXView::updateToc, this)); - - bufferEmbeddingChangedConnection_ = - buf.embeddingChanged.connect( - boost::bind(&LyXView::updateEmbeddedFiles, this)); - - errorsConnection_ = - buf.errors.connect( - boost::bind(&LyXView::showErrorList, this, _1)); - - messageConnection_ = - buf.message.connect( - boost::bind(&LyXView::message, this, _1)); - - busyConnection_ = - buf.busy.connect( - boost::bind(&LyXView::busy, this, _1)); - - titleConnection_ = - buf.updateTitles.connect( - boost::bind(&LyXView::updateWindowTitle, this)); - - timerConnection_ = - buf.resetAutosaveTimers.connect( - boost::bind(&LyXView::resetAutosaveTimer, this)); - - readonlyConnection_ = - buf.readonly.connect( - boost::bind(&LyXView::showReadonly, this, _1)); + buf.setGuiDelegate(this); } void LyXView::disconnectBuffer() { - errorsConnection_.disconnect(); - bufferStructureChangedConnection_.disconnect(); - bufferEmbeddingChangedConnection_.disconnect(); - messageConnection_.disconnect(); - busyConnection_.disconnect(); - titleConnection_.disconnect(); - timerConnection_.disconnect(); - readonlyConnection_.disconnect(); + if (WorkArea * work_area = currentWorkArea()) + work_area->bufferView().setGuiDelegate(0); } void LyXView::connectBufferView(BufferView & bv) { - message_connection_ = bv.message.connect( - boost::bind(&LyXView::message, this, _1)); - show_dialog_connection_ = bv.showDialog.connect( - boost::bind(&LyXView::showDialog, this, _1)); - show_dialog_with_data_connection_ = bv.showDialogWithData.connect( - boost::bind(&LyXView::showDialogWithData, this, _1, _2)); - show_inset_dialog_connection_ = bv.showInsetDialog.connect( - boost::bind(&LyXView::showInsetDialog, this, _1, _2, _3)); - update_dialog_connection_ = bv.updateDialog.connect( - boost::bind(&LyXView::updateDialog, this, _1, _2)); + bv.setGuiDelegate(this); } void LyXView::disconnectBufferView() { - message_connection_.disconnect(); - show_dialog_connection_.disconnect(); - show_dialog_with_data_connection_.disconnect(); - show_inset_dialog_connection_.disconnect(); - update_dialog_connection_.disconnect(); + if (WorkArea * work_area = currentWorkArea()) + work_area->bufferView().setGuiDelegate(0); } void LyXView::showErrorList(string const & error_type) { ErrorList & el = buffer()->errorList(error_type); - if (!el.empty()) { + if (!el.empty()) getDialogs().show("errorlist", error_type); - } } @@ -367,5 +315,20 @@ Buffer const * LyXView::updateInset(Inset const * inset) } + +void LyXView::changed() +{ + if (WorkArea * wa = currentWorkArea()) + wa->redraw(); +} + + +void LyXView::closing(Buffer *) +{ + if (WorkArea * wa = currentWorkArea()) + removeWorkArea(wa); +} + + } // namespace frontend } // namespace lyx diff --git a/src/frontends/LyXView.h b/src/frontends/LyXView.h index 023aae1047..a8f46fbcbf 100644 --- a/src/frontends/LyXView.h +++ b/src/frontends/LyXView.h @@ -14,10 +14,9 @@ #define LYXVIEW_H #include "frontends/Application.h" +#include "frontends/Delegates.h" #include "support/docstring.h" -#include -#include #include #include @@ -52,7 +51,9 @@ class WorkArea; * Additionally we would like to support multiple views * in a single LyXView. */ -class LyXView : public boost::signals::trackable, boost::noncopyable { +class LyXView : boost::noncopyable, + public GuiBufferViewDelegate, public GuiBufferDelegate +{ public: /// LyXView(int id); @@ -177,7 +178,29 @@ public: /// show the error list to the user void showErrorList(std::string const &); -protected: + + // + // GuiBufferDelegate + // + /// This function is called when the buffer is changed. + void changed(); + /// This function is called if the buffer is being closed. + void closing(Buffer *); + /// This function is called when the buffer structure is changed. + void structureChanged() { updateToc(); } + /// This function is called when an embedded file is changed + void embeddingChanged() { updateEmbeddedFiles(); } + /// This function is called when some parsing error shows up. + void errors(std::string const & err) { showErrorList(err); } + /// This function is called when the buffer busy status change. + //void busy(bool); + /// This function is called when the buffer readonly status change. + void readonly(bool on) { showReadonly(on); } + /// Update window titles of all users. + void updateTitles() { updateWindowTitle(); } + /// Reset autosave timers for all users. + void resetAutosaveTimers() { resetAutosaveTimer(); } + /// connect to signals in the given BufferView void connectBufferView(BufferView & bv); /// disconnect from signals in the given BufferView @@ -203,32 +226,6 @@ private: /// dialogs for this view Dialogs * dialogs_; - /// buffer structure changed signal connection - boost::signals::connection bufferStructureChangedConnection_; - /// embedded file change signal connection - boost::signals::connection bufferEmbeddingChangedConnection_; - /// buffer errors signal connection - boost::signals::connection errorsConnection_; - /// buffer messages signal connection - boost::signals::connection messageConnection_; - /// buffer busy status signal connection - boost::signals::connection busyConnection_; - /// buffer title changed signal connection - boost::signals::connection titleConnection_; - /// buffer reset timers signal connection - boost::signals::connection timerConnection_; - /// buffer readonly status changed signal connection - boost::signals::connection readonlyConnection_; - - /// BufferView messages signal connection - //@{ - boost::signals::connection message_connection_; - boost::signals::connection show_dialog_connection_; - boost::signals::connection show_dialog_with_data_connection_; - boost::signals::connection show_inset_dialog_connection_; - boost::signals::connection update_dialog_connection_; - //@} - /// Bind methods for BufferView messages signal connection //@{ void showDialog(std::string const & name); diff --git a/src/frontends/WorkArea.h b/src/frontends/WorkArea.h index f0e8d95638..a541dca6b6 100644 --- a/src/frontends/WorkArea.h +++ b/src/frontends/WorkArea.h @@ -15,6 +15,7 @@ #define BASE_WORKAREA_H #include "frontends/key_state.h" +#include "frontends/Delegates.h" #include "support/Timeout.h" #include "support/docstring.h" diff --git a/src/frontends/qt4/pch.h b/src/frontends/qt4/pch.h index f687c93caf..11ebfc5c3d 100644 --- a/src/frontends/qt4/pch.h +++ b/src/frontends/qt4/pch.h @@ -7,8 +7,6 @@ #include #include #include -#include -#include #include #include diff --git a/src/mathed/MathData.cpp b/src/mathed/MathData.cpp index a841d1ad98..86741882bd 100644 --- a/src/mathed/MathData.cpp +++ b/src/mathed/MathData.cpp @@ -29,6 +29,7 @@ #include "frontends/Painter.h" #include +#include namespace lyx { diff --git a/src/output_docbook.cpp b/src/output_docbook.cpp index fd1f9b1889..b3bdae2d6b 100644 --- a/src/output_docbook.cpp +++ b/src/output_docbook.cpp @@ -31,6 +31,8 @@ #include "support/convert.h" #include "support/types.h" +#include + namespace lyx { diff --git a/src/output_latex.cpp b/src/output_latex.cpp index 247e8a399b..6a57c2c10d 100644 --- a/src/output_latex.cpp +++ b/src/output_latex.cpp @@ -31,6 +31,7 @@ #include "support/lstrings.h" +#include namespace lyx { diff --git a/src/pch.h b/src/pch.h index 715ce37abd..fbfa76b594 100644 --- a/src/pch.h +++ b/src/pch.h @@ -13,9 +13,6 @@ #include #include #include -#include -#include -#include #include #include #include -- 2.39.5