142 lines
2.7 KiB
C
142 lines
2.7 KiB
C
|
|
/*
|
|
* Copyright (C) Artsiom D.
|
|
* Copyright (C) shit-co.de
|
|
*/
|
|
|
|
|
|
#include <curses.h>
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
|
|
#include "box.h"
|
|
#include "ui.h"
|
|
#include "util.h"
|
|
|
|
|
|
static void draw_rectangle(size_t x, size_t y, size_t width, size_t height);
|
|
static void draw_vertical(struct box *, size_t x, size_t y, size_t width, size_t height);
|
|
static void draw_horizontal(struct box *, size_t x, size_t y, size_t width, size_t height);
|
|
static void draw_button(struct box *, size_t x, size_t y, size_t width, size_t height);
|
|
|
|
|
|
static void
|
|
draw_rectangle(size_t x, size_t y, size_t width, size_t height)
|
|
{
|
|
size_t w = width - 1;
|
|
size_t h = height - 1;
|
|
|
|
/* corners */
|
|
|
|
ui_char(x, y, '+');
|
|
ui_char(x, y + h, '+');
|
|
ui_char(x + w, y + h, '+');
|
|
ui_char(x + w, y, '+');
|
|
|
|
/* vertical borders */
|
|
|
|
for (size_t i = 1; i < h; i++) {
|
|
ui_char(x, y + i, '|');
|
|
}
|
|
|
|
for (size_t i = 1; i < h; i++) {
|
|
ui_char(x + w, y + i, '|');
|
|
}
|
|
|
|
/* horizontal borders */
|
|
|
|
for (size_t i = 1; i < w; i++) {
|
|
ui_char(x + i, y, '-');
|
|
}
|
|
|
|
for (size_t i = 1; i < w; i++) {
|
|
ui_char(x + i, y + h, '-');
|
|
}
|
|
}
|
|
|
|
|
|
static void
|
|
draw_button(struct box *b, size_t x, size_t y, size_t width, size_t height)
|
|
{
|
|
ui_color_on(b->color);
|
|
draw_rectangle(x, y, width, height);
|
|
ui_string(x + width / 2 - strlen(b->name) / 2, y + height / 2, b->name);
|
|
ui_color_off(b->color);
|
|
}
|
|
|
|
|
|
static void
|
|
draw_horizontal(struct box *b, size_t x, size_t y, size_t width, size_t height)
|
|
{
|
|
die("Don't call me");
|
|
}
|
|
|
|
|
|
static void
|
|
draw_vertical(struct box *b, size_t x, size_t y, size_t width, size_t height)
|
|
{
|
|
double total_fills = 0;
|
|
|
|
for (size_t i = 0; i < b->length; i++) {
|
|
total_fills += b->children[i]->fills;
|
|
}
|
|
|
|
double pixels_per_fill = (double)height / total_fills;
|
|
|
|
double current_y = y;
|
|
|
|
for (size_t i = 0; i < b->length; i++) {
|
|
struct box *child = b->children[i];
|
|
double height = child->fills * pixels_per_fill;
|
|
draw_box(child, x, current_y, width, height);
|
|
current_y += height;
|
|
}
|
|
}
|
|
|
|
|
|
struct box *
|
|
new_button_box(char *name)
|
|
{
|
|
struct box *button = malloc(sizeof(struct box));
|
|
|
|
button->type = BOX_BUTTON;
|
|
button->name = name;
|
|
button->fills = 1;
|
|
button->color = UI_RED;
|
|
|
|
return button;
|
|
}
|
|
|
|
|
|
struct box *
|
|
new_vertical_box(void)
|
|
{
|
|
struct box *box = emalloc(sizeof(struct box));
|
|
|
|
box->type = BOX_VERTICAL;
|
|
box->fills = 1;
|
|
box->color = UI_RED;
|
|
box->children = emalloc(sizeof(struct box *) * 100);
|
|
box->length = 0;
|
|
|
|
return box;
|
|
}
|
|
|
|
|
|
void
|
|
draw_box(struct box *b, size_t x, size_t y, size_t width, size_t height)
|
|
{
|
|
switch (b->type) {
|
|
case BOX_BUTTON:
|
|
draw_button(b, x, y, width, height);
|
|
break;
|
|
case BOX_VERTICAL:
|
|
draw_vertical(b, x, y, width, height);
|
|
break;
|
|
case BOX_HORIZONTAL:
|
|
draw_horizontal(b, x, y, width, height);
|
|
break;
|
|
}
|
|
}
|