view libmlk-ui/mlk/ui/label.c @ 451:90a097b1aa0f

core: font -> mlk_font
author David Demelier <markand@malikania.fr>
date Sat, 18 Feb 2023 20:40:01 +0100
parents 773a082f0b91
children 01f5580e43d1
line wrap: on
line source

/*
 * label.c -- GUI label
 *
 * Copyright (c) 2020-2023 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 <assert.h>
#include <string.h>

#include <mlk/core/font.h>
#include <mlk/core/panic.h>
#include <mlk/core/texture.h>

#include "label.h"
#include "theme.h"

void
label_draw_default(const struct theme *t, const struct label *label)
{
	assert(t);
	assert(label);

	struct mlk_font *font;
	struct texture tex;
	unsigned long color;

	font = label->flags & LABEL_FLAGS_IMPORTANT
		? t->fonts[THEME_FONT_IMPORTANT]
		: t->fonts[THEME_FONT_INTERFACE];
	color = label->flags & LABEL_FLAGS_SELECTED
		? t->colors[THEME_COLOR_SELECTED]
	        : t->colors[THEME_COLOR_NORMAL];

	/* Shadow text, only if enabled. */
	if (label->flags & LABEL_FLAGS_SHADOW) {
		if (mlk_font_render(font, &tex, label->text, t->colors[THEME_COLOR_SHADOW]) < 0)
			panic();

		texture_draw(&tex, label->x + 1, label->y + 1);
		texture_finish(&tex);
	}

	/* Normal text. */
	if (mlk_font_render(font, &tex, label->text, color) < 0)
		panic();

	texture_draw(&tex, label->x, label->y);
	texture_finish(&tex);
}

int
label_ok(const struct label *label)
{
	return label && label->text && strlen(label->text) > 0;
}

void
label_query(const struct label *label, unsigned int *w, unsigned int *h)
{
	assert(label);
	assert(label->text);

	const struct theme *t = label->theme ? label->theme : theme_default();
	const struct mlk_font *f = label->flags & LABEL_FLAGS_IMPORTANT
		? t->fonts[THEME_FONT_IMPORTANT]
		: t->fonts[THEME_FONT_INTERFACE];

	if (mlk_font_query(f, label->text, w, h) < 0)
		panic();
}

void
label_draw(const struct label *label)
{
	assert(label);
	assert(label->text);

	theme_draw_label(label->theme, label);
}