the delegate patch

git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@20667 a592a061-630c-0410-9148-cb99ea01b6c8
This commit is contained in:
André Pönitz 2007-10-02 18:27:20 +00:00
parent fce745f111
commit b847b8c7df
18 changed files with 305 additions and 134 deletions

View File

@ -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

View File

@ -20,8 +20,6 @@
#include "support/docstring.h"
#include "support/docstream.h"
#include <boost/signal.hpp>
#include <iosfwd>
#include <string>
#include <utility>
@ -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<void()> structureChanged;
/// This signal is emitted when an embedded file is changed
boost::signal<void()> embeddingChanged;
/// This signal is emitted when some parsing error shows up.
boost::signal<void(std::string)> errors;
/// This signal is emitted when some message shows up.
boost::signal<void(docstring)> message;
/// This signal is emitted when the buffer busy status change.
boost::signal<void(bool)> busy;
/// This signal is emitted when the buffer readonly status change.
boost::signal<void(bool)> readonly;
/// Update window titles of all users.
boost::signal<void()> updateTitles;
/// Reset autosave timers for all users.
boost::signal<void()> 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<support::FileName> bibfilesCache_;
frontend::GuiBufferDelegate * gui_;
};

View File

@ -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())

View File

@ -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 <boost/bind.hpp>
#include <boost/current_function.hpp>
#include <boost/next_prior.hpp>
#include <functional>
#include <vector>
@ -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

View File

@ -23,9 +23,7 @@
#include "support/types.h"
#include <boost/tuple/tuple.hpp>
#include <boost/noncopyable.hpp>
#include <boost/signal.hpp>
#include <utility>
#include <string>
@ -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<void(docstring)> message;
void message(docstring const & msg);
/// This signal is emitted when some dialog needs to be shown.
boost::signal<void(std::string name)> showDialog;
void showDialog(std::string const & name);
/// This signal is emitted when some dialog needs to be shown with
/// some data.
boost::signal<void(std::string name,
std::string data)> 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<void(std::string name, std::string data,
Inset * inset)> 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<void(std::string name,
std::string data)> 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<Text const *, TextMetrics> 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)

View File

@ -51,6 +51,7 @@
#include <boost/current_function.hpp>
#include <boost/tuple/tuple.hpp>
#include <boost/next_prior.hpp>
#include <string>

View File

@ -68,6 +68,7 @@
#include "support/convert.h"
#include <boost/current_function.hpp>
#include <boost/next_prior.hpp>
#include <sstream>

View File

@ -59,6 +59,7 @@
#include "support/textutils.h"
#include <boost/current_function.hpp>
#include <boost/next_prior.hpp>
#include <sstream>

View File

@ -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;

79
src/frontends/Delegates.h Normal file
View File

@ -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

View File

@ -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

View File

@ -14,10 +14,9 @@
#define LYXVIEW_H
#include "frontends/Application.h"
#include "frontends/Delegates.h"
#include "support/docstring.h"
#include <boost/signal.hpp>
#include <boost/signals/trackable.hpp>
#include <boost/noncopyable.hpp>
#include <vector>
@ -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);

View File

@ -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"

View File

@ -7,8 +7,6 @@
#include <boost/function.hpp>
#include <boost/scoped_ptr.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/signals/trackable.hpp>
#include <boost/signal.hpp>
#include <boost/tuple/tuple.hpp>
#include <boost/noncopyable.hpp>

View File

@ -29,6 +29,7 @@
#include "frontends/Painter.h"
#include <boost/assert.hpp>
#include <boost/next_prior.hpp>
namespace lyx {

View File

@ -31,6 +31,8 @@
#include "support/convert.h"
#include "support/types.h"
#include <boost/next_prior.hpp>
namespace lyx {

View File

@ -31,6 +31,7 @@
#include "support/lstrings.h"
#include <boost/next_prior.hpp>
namespace lyx {

View File

@ -13,9 +13,6 @@
#include <boost/scoped_array.hpp>
#include <boost/scoped_ptr.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/signal.hpp>
#include <boost/signals/connection.hpp>
#include <boost/signals/trackable.hpp>
#include <boost/tokenizer.hpp>
#include <boost/tuple/tuple.hpp>
#include <boost/noncopyable.hpp>