view libclient/malikania/client/window.cpp @ 143:7627d614f3e0

Client: add const overload to take const font in draw_text
author David Demelier <markand@malikania.fr>
date Thu, 28 Sep 2017 12:49:27 +0200
parents b80d37e71b87
children 4b292c20124c
line wrap: on
line source

/*
 * window.cpp -- main window and basic drawing
 *
 * Copyright (c) 2013-2017 David Demelier <markand@malikania.fr>
 *
 * 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 <iostream>
#include <stdexcept>

#include "color.hpp"
#include "frame.hpp"
#include "layout.hpp"
#include "theme.hpp"
#include "widget.hpp"
#include "window_backend.hpp"

namespace mlk {

namespace client {

bool window::is_frame_bound(frame& f, const point& ev_pos) noexcept
{
    auto frame_size = f.size(*this);
    auto frame_padding = f.padding();
    auto frame_pos = f.position();

    return ev_pos.x() >= frame_pos.x() + static_cast<int>(frame_padding.width()) &&
           ev_pos.y() >= frame_pos.y() + static_cast<int>(frame_padding.height()) &&
           ev_pos.x() <= frame_pos.x() + static_cast<int>(frame_padding.width()  + frame_size.width()) &&
           ev_pos.y() <= frame_pos.y() + static_cast<int>(frame_padding.height() + frame_size.height());
}

window::window(unsigned width, unsigned height, const std::string& title)
    : backend_(std::make_unique<backend_impl>(*this, width, height, title))
    , theme_(std::make_unique<mlk::client::theme>())
{
}

window::window(window&&) noexcept = default;

window::~window() noexcept = default;

void window::start_edit()
{
    if (!is_editing_) {
        backend_->start_edit();
        is_editing_ = true;
    }
}

void window::stop_edit()
{
    if (is_editing_) {
        backend_->stop_edit();
        is_editing_ = false;
    }
}

void window::clear()
{
    backend_->clear();
}

void window::present()
{
    backend_->present();
}

void window::close() noexcept
{
    is_open_ = false;
    backend_->close();
}

color window::drawing_color() const
{
    return backend_->drawing_color();
}

void window::set_drawing_color(const color& color)
{
    backend_->set_drawing_color(color);
}

void window::draw_line(const line& line)
{
    backend_->draw_line(line);
}

void window::draw_lines(const std::vector<point>& points)
{
    backend_->draw_lines(points);
}

void window::draw_point(const point& point)
{
    backend_->draw_point(point);
}

void window::draw_points(const std::vector<point>& points)
{
    backend_->draw_points(points);
}

void window::draw_rectangle(const rectangle& rectangle)
{
    backend_->draw_rectangle(rectangle);
}

void window::draw_rectangles(const std::vector<rectangle>& rectangles)
{
    backend_->draw_rectangles(rectangles);
}

void window::draw_frames()
{
    for (const auto& f : frames_) {
        f->draw(*this);
    }
}

void window::fill_rectangle(const rectangle& rectangle)
{
    backend_->fill_rectangle(rectangle);
}

void window::fill_rectangles(const std::vector<rectangle>& rectangles)
{
    backend_->fill_rectangles(rectangles);
}

void window::draw_text(const std::string& text, const font& font, const rectangle& rectangle)
{
    backend_->draw_text(text, font, rectangle);
}

void window::draw_text(const std::string& text, const font& font, const point& point)
{
    backend_->draw_text(text, font, point);
}

void window::poll(mlk::client::dispatcher& dp)
{
    backend_->poll(dp);
}

void window::handle_key_down(const key_event&)
{
}

void window::handle_key_up(const key_event&)
{
}

void window::handle_mouse_down(const mouse_click_event& ev)
{
    for (const auto& f : frames_) {
        if (is_frame_bound(*f, ev.pos)) {
            f->layout()->handle_mouse_down({
                ev.button,
                point(ev.pos.x() - f->position().x(), ev.pos.y() - f->position().y())
            });
        }
    }
}

void window::handle_mouse_up(const mouse_click_event&)
{
}

void window::handle_mouse_wheel(const mouse_wheel_event&)
{
}

void window::handle_quit()
{
    is_open_ = false;
}

window& window::operator=(window&&) noexcept = default;

} // !client

} // !mlk