From dd29f1e500b31470a764c090fe3507104daf1d86 Mon Sep 17 00:00:00 2001 From: casey Date: Sat, 14 May 2016 01:11:03 -0700 Subject: [PATCH] Added IWindowGroup, which is extended by ILayout and implemented by LayoutBase. Recursive create/destroy working. --- src/musikbox/ILayout.h | 4 +- src/musikbox/IWindow.h | 9 +++- src/musikbox/IWindowGroup.h | 11 +++++ src/musikbox/LayoutBase.cpp | 60 +++++++++++++++++++++++++++ src/musikbox/LayoutBase.h | 17 ++++++++ src/musikbox/LibraryLayout.cpp | 11 +++-- src/musikbox/LibraryLayout.h | 4 +- src/musikbox/MainLayout.cpp | 4 +- src/musikbox/Window.cpp | 26 +++++++++++- src/musikbox/Window.h | 6 ++- src/musikbox/musikbox.vcxproj | 1 + src/musikbox/musikbox.vcxproj.filters | 3 ++ 12 files changed, 143 insertions(+), 13 deletions(-) create mode 100755 src/musikbox/IWindowGroup.h diff --git a/src/musikbox/ILayout.h b/src/musikbox/ILayout.h index 02e3c92e8..5d00baeb1 100755 --- a/src/musikbox/ILayout.h +++ b/src/musikbox/ILayout.h @@ -1,8 +1,8 @@ #pragma once -#include "IWindow.h" +#include "IWindowGroup.h" -class ILayout { +class ILayout : public IWindowGroup { public: virtual IWindow* FocusNext() = 0; virtual IWindow* FocusPrev() = 0; diff --git a/src/musikbox/IWindow.h b/src/musikbox/IWindow.h index 596c4bd0f..b2ec31543 100755 --- a/src/musikbox/IWindow.h +++ b/src/musikbox/IWindow.h @@ -6,6 +6,11 @@ class IWindow { public: virtual void Repaint() = 0; + virtual void SetParent(IWindow* parent) = 0; + + virtual void Create() = 0; + virtual void Destroy() = 0; + virtual void SetContentColor(int color) = 0; virtual void SetFrameColor(int color) = 0; @@ -23,4 +28,6 @@ class IWindow { virtual WINDOW* GetFrame() const = 0; virtual WINDOW* GetContent() const = 0; -}; \ No newline at end of file +}; + +typedef std::shared_ptr IWindowPtr; \ No newline at end of file diff --git a/src/musikbox/IWindowGroup.h b/src/musikbox/IWindowGroup.h new file mode 100755 index 000000000..64540ac0d --- /dev/null +++ b/src/musikbox/IWindowGroup.h @@ -0,0 +1,11 @@ +#pragma once + +#include "IWindow.h" + +class IWindowGroup { + public: + virtual bool AddWindow(IWindowPtr window) = 0; + virtual bool RemoveWindow(IWindowPtr window) = 0; + virtual size_t GetWindowCount() = 0; + virtual IWindowPtr GetWindowAt(size_t position) = 0; +}; \ No newline at end of file diff --git a/src/musikbox/LayoutBase.cpp b/src/musikbox/LayoutBase.cpp index 657b7155c..a3c12e6e9 100755 --- a/src/musikbox/LayoutBase.cpp +++ b/src/musikbox/LayoutBase.cpp @@ -1,2 +1,62 @@ #include "stdafx.h" #include "LayoutBase.h" + +LayoutBase::LayoutBase(IWindow* parent) +: Window(parent) { + this->SetFrameVisible(false); +} + +LayoutBase::~LayoutBase() { + +} + +void LayoutBase::Create() { + Window::Create(); + + for (size_t i = 0; i < this->children.size(); i++) { + this->children.at(i)->Create(); + } +} + +void LayoutBase::Destroy() { + for (size_t i = 0; i < this->children.size(); i++) { + this->children.at(i)->Destroy(); + } + + Window::Destroy(); +} + + +bool LayoutBase::AddWindow(IWindowPtr window) { + std::vector::iterator it = this->children.begin(); + for (; it != this->children.end(); it++) { + if (*it == window) { + return true; + } + } + + window->SetParent(this); + this->children.push_back(window); + + return true; +} + +bool LayoutBase::RemoveWindow(IWindowPtr window) { + std::vector::iterator it = this->children.begin(); + for ( ; it != this->children.end(); it++) { + if (*it == window) { + this->children.erase(it); + return true; + } + } + + return false; +} + +size_t LayoutBase::GetWindowCount() { + return this->children.size(); +} + +IWindowPtr LayoutBase::GetWindowAt(size_t position) { + return this->children.at(position); +} \ No newline at end of file diff --git a/src/musikbox/LayoutBase.h b/src/musikbox/LayoutBase.h index b9831afc2..ec17ecb52 100755 --- a/src/musikbox/LayoutBase.h +++ b/src/musikbox/LayoutBase.h @@ -7,9 +7,26 @@ class LayoutBase : public Window, public ILayout { public: + LayoutBase(IWindow* parent = NULL); + virtual ~LayoutBase(); + + /* IWindow */ + virtual void Create(); + virtual void Destroy(); + + /* ILayout */ virtual IWindow* FocusNext() = 0; virtual IWindow* FocusPrev() = 0; virtual IWindow* GetFocus() = 0; virtual void Layout() = 0; virtual void OnIdle() = 0; + + /* IWindowGroup */ + virtual bool AddWindow(IWindowPtr window); + virtual bool RemoveWindow(IWindowPtr window); + virtual size_t GetWindowCount(); + virtual IWindowPtr GetWindowAt(size_t position); + + private: + std::vector children; }; \ No newline at end of file diff --git a/src/musikbox/LibraryLayout.cpp b/src/musikbox/LibraryLayout.cpp index 4415b8a78..19ad7e01c 100755 --- a/src/musikbox/LibraryLayout.cpp +++ b/src/musikbox/LibraryLayout.cpp @@ -3,13 +3,18 @@ #include "Screen.h" #include "LibraryLayout.h" -LibraryLayout::LibraryLayout(LibraryPtr library) { +LibraryLayout::LibraryLayout(LibraryPtr library) +: LayoutBase() { this->SetSize(Screen::GetWidth(), Screen::GetHeight()); this->SetPosition(0, 0); this->Create(); - this->albumList.reset(new CategoryListView(this, library)); - this->trackList.reset(new TrackListView(this)); + this->albumList.reset(new CategoryListView(NULL, library)); + this->trackList.reset(new TrackListView(NULL)); + + this->AddWindow(this->albumList); + this->AddWindow(this->trackList); + this->Layout(); } diff --git a/src/musikbox/LibraryLayout.h b/src/musikbox/LibraryLayout.h index 264cbb529..e1a4784ad 100755 --- a/src/musikbox/LibraryLayout.h +++ b/src/musikbox/LibraryLayout.h @@ -20,6 +20,6 @@ class LibraryLayout : public LayoutBase { virtual void OnIdle(); private: - boost::shared_ptr albumList; - boost::shared_ptr trackList; + std::shared_ptr albumList; + std::shared_ptr trackList; }; \ No newline at end of file diff --git a/src/musikbox/MainLayout.cpp b/src/musikbox/MainLayout.cpp index a80a6289a..3da02be60 100755 --- a/src/musikbox/MainLayout.cpp +++ b/src/musikbox/MainLayout.cpp @@ -18,7 +18,9 @@ static inline IWindow* adjustFocus(IWindow* oldFocus, IWindow* newFocus) { return newFocus; } -MainLayout::MainLayout(Transport& transport, LibraryPtr library) { +MainLayout::MainLayout(Transport& transport, LibraryPtr library) +: LayoutBase() +{ this->SetSize(Screen::GetWidth(), Screen::GetHeight()); this->SetPosition(0, 0); this->SetFrameVisible(false); diff --git a/src/musikbox/Window.cpp b/src/musikbox/Window.cpp index b283f5629..7c3a816ab 100755 --- a/src/musikbox/Window.cpp +++ b/src/musikbox/Window.cpp @@ -2,6 +2,7 @@ #include "stdafx.h" #include "Window.h" +#include "IWindowGroup.h" Window::Window(IWindow *parent) { this->frame = this->content = 0; @@ -19,6 +20,23 @@ Window::~Window() { this->Destroy(); } +void Window::SetParent(IWindow* parent) { + if (this->parent != parent) { + IWindowGroup* group = dynamic_cast(parent); + + if (group) { + group->RemoveWindow(shared_from_this()); + } + + this->parent = parent; + + if (this->frame) { + this->Destroy(); + this->Create(); + } + } +} + void Window::SetSize(int width, int height) { this->width = width; this->height = height; @@ -113,7 +131,7 @@ void Window::Create() { wbkgd(this->frame, COLOR_PAIR(this->contentColor)); } - wrefresh(this->content); + wnoutrefresh(this->content); } /* otherwise we'll draw a box around the frame, and create a content @@ -137,8 +155,8 @@ void Window::Create() { wbkgd(this->content, COLOR_PAIR(this->contentColor)); } - touchwin(this->content); wnoutrefresh(this->frame); + touchwin(this->content); wnoutrefresh(this->content); doupdate(); } @@ -177,6 +195,10 @@ void Window::Repaint() { void Window::Destroy() { if (this->frame) { + wclear(this->frame); + wclear(this->content); + this->Repaint(); + delwin(this->content); if (this->content != this->frame) { diff --git a/src/musikbox/Window.h b/src/musikbox/Window.h index 0cf4a9488..e179042c3 100755 --- a/src/musikbox/Window.h +++ b/src/musikbox/Window.h @@ -3,13 +3,15 @@ #include "curses_config.h" #include "IWindow.h" -class Window : public IWindow { +class Window : public IWindow, public std::enable_shared_from_this { public: Window(IWindow* parent = NULL); virtual ~Window(); + virtual void SetParent(IWindow* parent); + virtual void Create(); - void Destroy(); + virtual void Destroy(); virtual void Repaint(); diff --git a/src/musikbox/musikbox.vcxproj b/src/musikbox/musikbox.vcxproj index 12d6fe2d4..e1d43fe03 100755 --- a/src/musikbox/musikbox.vcxproj +++ b/src/musikbox/musikbox.vcxproj @@ -147,6 +147,7 @@ + diff --git a/src/musikbox/musikbox.vcxproj.filters b/src/musikbox/musikbox.vcxproj.filters index 4f21acffc..0a77717a5 100755 --- a/src/musikbox/musikbox.vcxproj.filters +++ b/src/musikbox/musikbox.vcxproj.filters @@ -150,6 +150,9 @@ curses + + curses +