changeset 70:58444cf5f227

Client: add minimalist label
author David Demelier <markand@malikania.fr>
date Mon, 19 Dec 2016 13:12:32 +0100
parents fce7e41875db
children 6950ea5c78c6
files client/main.cpp libclient/CMakeLists.txt libclient/malikania/label.cpp libclient/malikania/label.hpp libclient/malikania/theme.cpp libclient/malikania/theme.hpp
diffstat 6 files changed, 169 insertions(+), 5 deletions(-) [+]
line wrap: on
line diff
--- a/client/main.cpp	Mon Dec 19 12:58:53 2016 +0100
+++ b/client/main.cpp	Mon Dec 19 13:12:32 2016 +0100
@@ -24,6 +24,7 @@
 #include "malikania/button.hpp"
 #include "malikania/color.hpp"
 #include "malikania/frame.hpp"
+#include "malikania/label.hpp"
 #include "malikania/point.hpp"
 #include "malikania/unique_layout.hpp"
 #include "malikania/window.hpp"
@@ -33,12 +34,9 @@
     mlk::window win;
 
     auto f = std::make_shared<mlk::frame>();
-    auto b = std::make_shared<mlk::button>("click me!");
-    auto l = std::make_shared<mlk::unique_layout>(b);
+    auto w = std::make_shared<mlk::label>("Malikania");
+    auto l = std::make_shared<mlk::unique_layout>(w);
 
-    b->on_clicked.connect([] {
-        puts("clicked!!!!");
-    });
     f->move({50, 50});
     f->set_layout(l);
     win.add_frame(f);
--- a/libclient/CMakeLists.txt	Mon Dec 19 12:58:53 2016 +0100
+++ b/libclient/CMakeLists.txt	Mon Dec 19 13:12:32 2016 +0100
@@ -29,6 +29,7 @@
 #    ${libmlk-client_SOURCE_DIR}/malikania/grid_layout.hpp
     ${libmlk-client_SOURCE_DIR}/malikania/image.hpp
     ${libmlk-client_SOURCE_DIR}/malikania/key.hpp
+    ${libmlk-client_SOURCE_DIR}/malikania/label.hpp
     ${libmlk-client_SOURCE_DIR}/malikania/layout.hpp
     ${libmlk-client_SOURCE_DIR}/malikania/line.hpp
     ${libmlk-client_SOURCE_DIR}/malikania/mouse.hpp
@@ -55,6 +56,7 @@
     ${libmlk-client_SOURCE_DIR}/malikania/frame.cpp
 #    ${libmlk-client_SOURCE_DIR}/malikania/grid_layout.cpp
     ${libmlk-client_SOURCE_DIR}/malikania/image.cpp
+    ${libmlk-client_SOURCE_DIR}/malikania/label.cpp
     ${libmlk-client_SOURCE_DIR}/malikania/sprite.cpp
     ${libmlk-client_SOURCE_DIR}/malikania/theme.cpp
     ${libmlk-client_SOURCE_DIR}/malikania/unique_layout.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/libclient/malikania/label.cpp	Mon Dec 19 13:12:32 2016 +0100
@@ -0,0 +1,41 @@
+/*
+ * label.cpp -- GUI label element
+ *
+ * Copyright (c) 2013-2016 Malikania Authors
+ *
+ * Permission to use, copy, modify, and/or distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+
+#include "label.hpp"
+#include "size.hpp"
+#include "theme.hpp"
+#include "window.hpp"
+
+namespace mlk {
+
+label::label(std::string text) noexcept
+    : m_text(std::move(text))
+{
+}
+
+void label::draw(window& w, const rectangle& rect)
+{
+    w.theme().draw_label(w, *this, rect);
+}
+
+mlk::size label::size(window& w) const noexcept
+{
+    return w.theme().size_label(*this);
+}
+
+} // !mlk
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/libclient/malikania/label.hpp	Mon Dec 19 13:12:32 2016 +0100
@@ -0,0 +1,93 @@
+/*
+ * label.hpp -- GUI label element
+ *
+ * Copyright (c) 2013-2016 Malikania Authors
+ *
+ * Permission to use, copy, modify, and/or distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+
+#ifndef MALIKANIA_CLIENT_LABEL_HPP
+#define MALIKANIA_CLIENT_LABEL_HPP
+
+/**
+ * \file label.hp
+ * \brief GUI label element.
+ */
+
+#include <string>
+
+#include "widget.hpp"
+
+namespace mlk {
+
+/**
+ * \brief Basic label for displaying test.
+ */
+class label : public widget {
+private:
+    std::string m_text;
+
+public:
+    /**
+     * Create a label with an optional text.
+     *
+     * \param text the text
+     */
+    label(std::string text = "") noexcept;
+
+    /**
+     * Get the text.
+     *
+     * \return the label text
+     */
+    inline const std::string& text() const noexcept
+    {
+        return m_text;
+    }
+
+    /**
+     * Overloaded function.
+     *
+     * \return the text
+     */
+    inline std::string& text() noexcept
+    {
+        return m_text;
+    }
+
+    /**
+     * Set the label text.
+     *
+     * \param text the text
+     */
+    inline void set_text(std::string text) noexcept
+    {
+        m_text = std::move(text);
+    }
+
+    using widget::draw;
+
+    /**
+     * \copydoc widget::draw
+     */
+    void draw(window& w, const rectangle& rect) override;
+
+    /**
+     * \copydoc widget::size
+     */
+    mlk::size size(window& w) const noexcept override;
+};
+
+} // !mlk
+
+#endif // !MALIKANIA_CLIENT_LABEL_HPP
--- a/libclient/malikania/theme.cpp	Mon Dec 19 12:58:53 2016 +0100
+++ b/libclient/malikania/theme.cpp	Mon Dec 19 13:12:32 2016 +0100
@@ -19,6 +19,7 @@
 #include "button.hpp"
 #include "color.hpp"
 #include "frame.hpp"
+#include "label.hpp"
 #include "layout.hpp"
 #include "point.hpp"
 #include "rectangle.hpp"
@@ -52,6 +53,11 @@
     };
 }
 
+size theme::size_label(const label& l)
+{
+    return m_font.clip(l.text());
+}
+
 void theme::draw_frame(window& win, const frame &f)
 {
     auto layout_size = f.layout()->size(win);
@@ -101,4 +107,10 @@
     w.draw_text(b.text(), m_font, point{rect.x() + 5, rect.y() + 5});
 }
 
+void theme::draw_label(window& w, const label& label, const rectangle& rect)
+{
+    w.set_drawing_color(m_text_color);
+    w.draw_text(label.text(), m_font, mlk::point(rect.x(), rect.y()));
+}
+
 } // !mlk
--- a/libclient/malikania/theme.hpp	Mon Dec 19 12:58:53 2016 +0100
+++ b/libclient/malikania/theme.hpp	Mon Dec 19 13:12:32 2016 +0100
@@ -32,6 +32,7 @@
 
 class button;
 class frame;
+class label;
 class rectangle;
 class point;
 class window;
@@ -82,6 +83,14 @@
     virtual size size_button(const button& b);
 
     /**
+     * Get the required bouding rectangle size for this label.
+     *
+     * \param l the label
+     * \return the size
+     */
+    virtual size size_label(const label& l);
+
+    /**
      * Draw the frame.
      *
      * \param w the main window
@@ -96,6 +105,15 @@
      * \param b the button
      */
     virtual void draw_button(window& w, const button& b, const rectangle& rect);
+
+    /**
+     * Draw a label.
+     *
+     * \param w the main window
+     * \param l the label
+     * \param rect the bounding rectangle.
+     */
+    virtual void draw_label(window& w, const label& l, const rectangle& rect);
 };
 
 } // !mlk