Context: store an editor instead of a window

This commit is contained in:
Maxime Coste 2012-08-05 18:23:37 +02:00
parent 2cc01d3407
commit 12e2ce0f3c

View File

@ -10,35 +10,42 @@ class Buffer;
struct Context struct Context
{ {
Context() Context() {}
: m_window(nullptr), m_buffer(nullptr) {} Context(Editor& editor)
Context(Window& window) : m_editor(&editor), m_buffer(&editor.buffer()) {}
: m_window(&window), m_buffer(&window.buffer()) {}
Context(Buffer& buffer) Context(Buffer& buffer)
: m_window(nullptr), m_buffer(&buffer) {} : m_buffer(&buffer) {}
Buffer& buffer() const Buffer& buffer() const
{ {
if (not m_buffer) if (not has_buffer())
throw runtime_error("no buffer in context"); throw runtime_error("no buffer in context");
return *m_buffer; return *m_buffer;
} }
bool has_buffer() const { return m_buffer; } bool has_buffer() const { return m_buffer; }
Editor& editor() const
{
if (not has_editor())
throw runtime_error("no editor in context");
return *m_editor.get();
}
bool has_editor() const { return m_editor; }
Window& window() const Window& window() const
{ {
if (not m_window) if (not has_window())
throw runtime_error("no window in context"); throw runtime_error("no window in context");
return *m_window; return *dynamic_cast<Window*>(m_editor.get());
} }
bool has_window() const { return m_window; } bool has_window() const { return m_editor and dynamic_cast<Window*>(m_editor.get()); }
OptionManager& option_manager() const OptionManager& option_manager() const
{ {
if (m_window) if (has_window())
return m_window->option_manager(); return window().option_manager();
if (m_buffer) if (has_buffer())
return m_buffer->option_manager(); return buffer().option_manager();
return GlobalOptionManager::instance(); return GlobalOptionManager::instance();
} }
@ -47,7 +54,7 @@ struct Context
public: public:
safe_ptr<Window> m_window; safe_ptr<Editor> m_editor;
safe_ptr<Buffer> m_buffer; safe_ptr<Buffer> m_buffer;
int m_numeric_param = 0; int m_numeric_param = 0;