Buffer is now responsible for window creation

This commit is contained in:
Maxime Coste 2011-09-08 14:30:36 +00:00
parent 60e673acba
commit 76b7c60afb
5 changed files with 16 additions and 16 deletions

View File

@ -316,14 +316,12 @@ void Buffer::append_modification(Modification&& modification)
m_current_undo_group.push_back(modification); m_current_undo_group.push_back(modification);
} }
struct window_already_registered {}; Window* Buffer::get_or_create_window()
void Buffer::register_window(Window* window)
{ {
if (std::find(m_windows.begin(), m_windows.end(), window) != m_windows.end()) if (m_windows.empty())
throw window_already_registered(); m_windows.push_front(std::unique_ptr<Window>(new Window(*this)));
m_windows.push_front(std::unique_ptr<Window>(window)); return m_windows.front().get();
} }
void Buffer::delete_window(Window* window) void Buffer::delete_window(Window* window)

View File

@ -100,7 +100,7 @@ public:
const BufferString& content() const { return m_content; } const BufferString& content() const { return m_content; }
void register_window(Window* window); Window* get_or_create_window();
void delete_window(Window* window); void delete_window(Window* window);
private: private:

View File

@ -4,6 +4,7 @@
#include "file.hh" #include "file.hh"
#include "regex_selector.hh" #include "regex_selector.hh"
#include "command_manager.hh" #include "command_manager.hh"
#include "buffer_manager.hh"
#include <unordered_map> #include <unordered_map>
#include <cassert> #include <cassert>
@ -160,17 +161,17 @@ void edit(const CommandParameters& params)
throw wrong_argument_count(); throw wrong_argument_count();
std::string filename = params[0]; std::string filename = params[0];
Buffer* buffer = NULL;
try try
{ {
Buffer* buffer = create_buffer_from_file(filename); buffer = create_buffer_from_file(filename);
if (buffer)
current_window = new Window(*buffer);
} }
catch (file_not_found& what) catch (file_not_found& what)
{ {
print_status("new file " + filename); print_status("new file " + filename);
current_window = new Window(*new Buffer(filename)); buffer = new Buffer(filename);
} }
current_window = buffer->get_or_create_window();
} }
void write_buffer(const CommandParameters& params) void write_buffer(const CommandParameters& params)
@ -293,7 +294,7 @@ int main()
try try
{ {
auto buffer = new Buffer("<scratch>"); auto buffer = new Buffer("<scratch>");
current_window = new Window(*buffer); current_window = buffer->get_or_create_window();
draw_window(*current_window); draw_window(*current_window);
int count = 0; int count = 0;

View File

@ -11,7 +11,6 @@ Window::Window(Buffer& buffer)
m_cursor(0, 0), m_cursor(0, 0),
m_dimensions(0, 0) m_dimensions(0, 0)
{ {
m_buffer.register_window(this);
} }
void Window::erase() void Window::erase()

View File

@ -34,9 +34,6 @@ public:
typedef BufferString String; typedef BufferString String;
typedef std::function<Selection (const BufferIterator&)> Selector; typedef std::function<Selection (const BufferIterator&)> Selector;
Window(Buffer& buffer);
Window(const Window&) = delete;
void erase(); void erase();
void insert(const String& string); void insert(const String& string);
void append(const String& string); void append(const String& string);
@ -69,6 +66,11 @@ public:
bool redo(); bool redo();
private: private:
friend class Buffer;
Window(Buffer& buffer);
Window(const Window&) = delete;
void scroll_to_keep_cursor_visible_ifn(); void scroll_to_keep_cursor_visible_ifn();
Buffer& m_buffer; Buffer& m_buffer;