diff --git a/src/musikbox/CMakeLists.txt b/src/musikbox/CMakeLists.txt index 29ebec9ea..f92ac72e6 100644 --- a/src/musikbox/CMakeLists.txt +++ b/src/musikbox/CMakeLists.txt @@ -31,6 +31,7 @@ set (BOX_SRCS ./cursespp/App.cpp ./cursespp/Checkbox.cpp ./cursespp/Colors.cpp + ./cursespp/DialogOverlay.cpp ./cursespp/LayoutBase.cpp ./cursespp/LayoutStack.cpp ./cursespp/ListWindow.cpp diff --git a/src/musikbox/cursespp/App.cpp b/src/musikbox/cursespp/App.cpp index c04537295..21ea85052 100755 --- a/src/musikbox/cursespp/App.cpp +++ b/src/musikbox/cursespp/App.cpp @@ -259,12 +259,9 @@ void App::CheckShowOverlay() { if (top) { top->Layout(); top->Show(); + top->BringToTop(); } } - - if (top) { - top->BringToTop(); - } } void App::ChangeLayout(ILayoutPtr newLayout) { diff --git a/src/musikbox/cursespp/DialogOverlay.cpp b/src/musikbox/cursespp/DialogOverlay.cpp new file mode 100644 index 000000000..b629a97dd --- /dev/null +++ b/src/musikbox/cursespp/DialogOverlay.cpp @@ -0,0 +1,119 @@ +////////////////////////////////////////////////////////////////////////////// +// +// Copyright (c) 2007-2016 musikcube team +// +// All rights reserved. +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are met: +// +// * Redistributions of source code must retain the above copyright notice, +// this list of conditions and the following disclaimer. +// +// * Redistributions in binary form must reproduce the above copyright +// notice, this list of conditions and the following disclaimer in the +// documentation and/or other materials provided with the distribution. +// +// * Neither the name of the author nor the names of other contributors may +// be used to endorse or promote products derived from this software +// without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE +// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF +// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN +// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) +// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +// POSSIBILITY OF SUCH DAMAGE. +// +////////////////////////////////////////////////////////////////////////////// + +#include "DialogOverlay.h" +#include "Colors.h" +#include "Screen.h" +#include "Text.h" + +using namespace cursespp; + +#define HORIZONTAL_PADDING 4 +#define VERTICAL_PADDING 2 + +DialogOverlay::DialogOverlay() { + this->SetContentColor(CURSESPP_OVERLAY_BACKGROUND); + this->width = this->height = 0; +} + +void DialogOverlay::Layout() { + this->RecalculateSize(); + + this->MoveAndResize( + HORIZONTAL_PADDING, + VERTICAL_PADDING, + this->width, + this->height); + + this->Redraw(); +} + +DialogOverlay& DialogOverlay::SetTitle(const std::string& title) { + this->title = title; + this->RecalculateSize(); + this->Repaint(); + return *this; +} + +DialogOverlay& DialogOverlay::SetMessage(const std::string& message) { + this->message = message; + this->width = 0; /* implicitly triggers a new BreakLines() */ + this->RecalculateSize(); + this->Repaint(); + return *this; +} + +void DialogOverlay::OnVisibilityChanged(bool visible) { + if (visible) { + this->Redraw(); + } +} + +void DialogOverlay::RecalculateSize() { + int lastWidth = this->width; + this->width = std::max(0, Screen::GetWidth() - (HORIZONTAL_PADDING * 2)); + + if (lastWidth != this->width) { + /* the "-2" is for left and right padding */ + messageLines = text::BreakLines(this->message, this->width - 2); + } + + this->height = 1; /* top padding */ + this->height += (this->title.size()) ? 2 : 0; + this->height += (this->messageLines.size()) ? messageLines.size() + 1 : 0; + this->height += 1; /* shortcuts */ +} + +void DialogOverlay::Redraw() { + WINDOW* c = this->GetContent(); + + const int currentX = 1; + int currentY = 1; + + if (this->title.size()) { + wmove(c, currentY, currentX); + wattron(c, A_BOLD); + wprintw(c, text::Ellipsize(this->title, this->width - 2).c_str()); + wattroff(c, A_BOLD); + currentY += 2; + } + + if (this->message.size()) { + for (size_t i = 0; i < messageLines.size(); i++) { + wmove(c, currentY, currentX); + wprintw(c, this->messageLines.at(i).c_str()); + ++currentY; + } + } +} \ No newline at end of file diff --git a/src/musikbox/cursespp/DialogOverlay.h b/src/musikbox/cursespp/DialogOverlay.h new file mode 100644 index 000000000..f40d1ce1e --- /dev/null +++ b/src/musikbox/cursespp/DialogOverlay.h @@ -0,0 +1,65 @@ +////////////////////////////////////////////////////////////////////////////// +// +// Copyright (c) 2007-2016 musikcube team +// +// All rights reserved. +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are met: +// +// * Redistributions of source code must retain the above copyright notice, +// this list of conditions and the following disclaimer. +// +// * Redistributions in binary form must reproduce the above copyright +// notice, this list of conditions and the following disclaimer in the +// documentation and/or other materials provided with the distribution. +// +// * Neither the name of the author nor the names of other contributors may +// be used to endorse or promote products derived from this software +// without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE +// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF +// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN +// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) +// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +// POSSIBILITY OF SUCH DAMAGE. +// +////////////////////////////////////////////////////////////////////////////// + +#pragma once + +#include "LayoutBase.h" +#include "TextLabel.h" +#include + +namespace cursespp { + class DialogOverlay : + public LayoutBase, + public std::enable_shared_from_this + { + public: + DialogOverlay(); + virtual void Layout(); + + DialogOverlay& SetTitle(const std::string& title); + DialogOverlay& SetMessage(const std::string& message); + + protected: + virtual void OnVisibilityChanged(bool visible); + + private: + void Redraw(); + void RecalculateSize(); + + std::string title; + std::string message; + std::vector messageLines; + int width, height; + }; +} \ No newline at end of file diff --git a/src/musikbox/cursespp/Overlays.cpp b/src/musikbox/cursespp/Overlays.cpp index f61460816..89f13edd1 100644 --- a/src/musikbox/cursespp/Overlays.cpp +++ b/src/musikbox/cursespp/Overlays.cpp @@ -33,30 +33,20 @@ ////////////////////////////////////////////////////////////////////////////// #include "Overlays.h" -#include "LayoutBase.h" +#include "DialogOverlay.h" #include "Colors.h" #include "Screen.h" -#include - using namespace cursespp; static ILayoutPtr none; -class OverlayLayout : - public LayoutBase, - public std::enable_shared_from_this -{ - virtual void Layout() { - this->MoveAndResize(2, 0, Screen::GetWidth() - 4, 8); - } -}; - -std::shared_ptr temp; +std::shared_ptr temp; Overlays::Overlays() { - temp.reset(new OverlayLayout()); - temp->SetContentColor(CURSESPP_OVERLAY_BACKGROUND); + temp.reset(new DialogOverlay()); + temp->SetTitle("musikbox") + .SetMessage("welcome to musikbox! welcome to musikbox! welcome to musikbox! welcome to musikbox!\n\ntesting line breaks"); } ILayoutPtr Overlays::Top() { diff --git a/src/musikbox/cursespp/Text.cpp b/src/musikbox/cursespp/Text.cpp index 611e891bd..9ce07361a 100755 --- a/src/musikbox/cursespp/Text.cpp +++ b/src/musikbox/cursespp/Text.cpp @@ -232,11 +232,14 @@ namespace cursespp { std::vector BreakLines(const std::string& line, size_t width) { std::vector result; - std::vector split; - boost::algorithm::split(split, line, boost::is_any_of("\n")); - for (size_t i = 0; i < split.size(); i++) { - privateBreakLines(split.at(i), width, result); + if (width > 0) { + std::vector split; + boost::algorithm::split(split, line, boost::is_any_of("\n")); + + for (size_t i = 0; i < split.size(); i++) { + privateBreakLines(split.at(i), width, result); + } } return result;