// SPDX-FileCopyrightText: 2009 Ingo Ruhnke <grumbel@gmail.com>
// SPDX-License-Identifier: GPL-3.0-or-later

#include "axis_widget.hpp"

AxisWidget::AxisWidget(int width, int height)
  : Gtk::Alignment(Gtk::ALIGN_CENTER, Gtk::ALIGN_START, 0.0f, 0.0f),
    x(0), y(0)
{
  //modify_bg(Gtk::STATE_NORMAL , Gdk::Color("white"));
  //modify_fg(Gtk::STATE_NORMAL , Gdk::Color("black"));
  add(drawingarea);
  drawingarea.signal_draw().connect(sigc::mem_fun(this, &AxisWidget::on_draw));
  drawingarea.set_size_request(width, height);
}

bool
AxisWidget::on_draw(const Cairo::RefPtr<Cairo::Context>& cr)
{
    int w  = drawingarea.get_allocation().get_width()  - 10;
    int h  = drawingarea.get_allocation().get_height() - 10;
    int px = w/2 + (w/2  * x);
    int py = h/2 + (h/2 * y);

    cr->translate(5, 5);

    // Outer Rectangle
    cr->set_source_rgb(0.0, 0.0, 0.0);
    cr->set_line_width(1.0);
    cr->rectangle(0, 0, w, h);
    cr->stroke();

    // BG Circle
    cr->arc(w/2, h/2, w/2, 0.0, 2.0 * M_PI);
    cr->set_source_rgba(0.0, 0.0, 0.0, 0.1);
    cr->fill();

    // Cross
    cr->set_line_width(0.5);
    cr->set_source_rgba(0.0, 0.0, 0.0, 0.5);
    cr->move_to(w/2, 0);
    cr->line_to(w/2, h);

    cr->set_source_rgba(0.0, 0.0, 0.0, 0.5);
    cr->move_to(0, h/2);
    cr->line_to(w, h/2);
    cr->stroke();

    // Cursor
    cr->set_source_rgb(0.0, 0.0, 0.0);
    cr->set_line_width(2.0);
    cr->move_to(px, py-5);
    cr->line_to(px, py+5);
    cr->move_to(px-5, py);
    cr->line_to(px+5, py);
    cr->stroke();

  return true;
}

void
AxisWidget::set_x_axis(double x_)
{
  x = x_;
  queue_draw();
}

void
AxisWidget::set_y_axis(double y_)
{
  y = y_;
  queue_draw();
}

/* EOF */

