toaruos/lib/decor-fancy.c

159 lines
4.8 KiB
C
Raw Normal View History

2018-02-25 15:54:39 +03:00
#include <stdint.h>
2018-03-19 05:38:11 +03:00
#include <toaru/yutani.h>
#include <toaru/graphics.h>
#include <toaru/decorations.h>
2018-04-18 08:53:04 +03:00
#include <toaru/sdf.h>
2018-02-25 15:54:39 +03:00
#define INACTIVE 10
2018-02-25 15:54:39 +03:00
#define TTK_FANCY_PATH "/usr/share/ttk/"
static int u_height = 33;
static int ul_width = 10;
static int ur_width = 10;
2018-04-25 08:03:29 +03:00
#if 0
2018-02-25 15:54:39 +03:00
static int ml_width = 6;
2018-04-25 08:03:29 +03:00
#endif
2018-02-25 15:54:39 +03:00
static int mr_width = 6;
static int l_height = 9;
static int ll_width = 9;
static int lr_width = 9;
2018-04-25 08:03:29 +03:00
#if 0
2018-02-25 15:54:39 +03:00
static int llx_offset = 3;
static int lly_offset = 3;
static int lrx_offset = 3;
static int lry_offset = 3;
2018-04-25 08:03:29 +03:00
#endif
2018-02-25 15:54:39 +03:00
static sprite_t * sprites[20];
2018-03-01 09:10:57 +03:00
#define TEXT_OFFSET 10
2018-02-25 15:54:39 +03:00
static void init_sprite(int id, char * path) {
sprites[id] = malloc(sizeof(sprite_t));
load_sprite(sprites[id], path);
sprites[id]->alpha = ALPHA_EMBEDDED;
}
static void render_decorations_fancy(yutani_window_t * window, gfx_context_t * ctx, char * title, int decors_active) {
int width = window->width;
int height = window->height;
2018-04-25 08:03:29 +03:00
for (int j = 0; j < (int)decor_top_height; ++j) {
2018-02-25 15:54:39 +03:00
for (int i = 0; i < width; ++i) {
GFX(ctx,i,j) = 0;
}
}
2018-04-25 08:03:29 +03:00
for (int j = (int)decor_top_height; j < height - (int)decor_bottom_height; ++j) {
for (int i = 0; i < (int)decor_left_width; ++i) {
2018-02-25 15:54:39 +03:00
GFX(ctx,i,j) = 0;
}
2018-04-25 08:03:29 +03:00
for (int i = width - (int)decor_right_width; i < width; ++i) {
2018-02-25 15:54:39 +03:00
GFX(ctx,i,j) = 0;
}
}
2018-04-25 08:03:29 +03:00
for (int j = height - (int)decor_bottom_height; j < height; ++j) {
2018-02-25 15:54:39 +03:00
for (int i = 0; i < width; ++i) {
GFX(ctx,i,j) = 0;
}
}
if (decors_active == DECOR_INACTIVE) decors_active = INACTIVE;
draw_sprite(ctx, sprites[decors_active + 0], 0, 0);
for (int i = 0; i < width - (ul_width + ur_width); ++i) {
draw_sprite(ctx, sprites[decors_active + 1], i + ul_width, 0);
}
draw_sprite(ctx, sprites[decors_active + 2], width - ur_width, 0);
for (int i = 0; i < height - (u_height + l_height); ++i) {
draw_sprite(ctx, sprites[decors_active + 3], 0, i + u_height);
draw_sprite(ctx, sprites[decors_active + 4], width - mr_width, i + u_height);
}
draw_sprite(ctx, sprites[decors_active + 5], 0, height - l_height);
for (int i = 0; i < width - (ll_width + lr_width); ++i) {
draw_sprite(ctx, sprites[decors_active + 6], i + ll_width, height - l_height);
}
draw_sprite(ctx, sprites[decors_active + 7], width - lr_width, height - l_height);
char * tmp_title = strdup(title);
int t_l = strlen(tmp_title);
#define EXTRA_SPACE 40
2018-04-18 12:05:19 +03:00
if (draw_sdf_string_width(tmp_title, 18, SDF_FONT_BOLD) + EXTRA_SPACE > width) {
while (t_l >= 0 && (draw_sdf_string_width(tmp_title, 18, SDF_FONT_BOLD) + EXTRA_SPACE > width)) {
2018-02-25 15:54:39 +03:00
tmp_title[t_l] = '\0';
t_l--;
}
}
if (strlen(tmp_title)) {
2018-04-18 12:05:19 +03:00
int title_offset = (width / 2) - (draw_sdf_string_width(tmp_title, 18, SDF_FONT_BOLD) / 2);
2018-02-25 15:54:39 +03:00
if (decors_active == 0) {
2018-04-18 12:05:19 +03:00
draw_sdf_string(ctx, title_offset, TEXT_OFFSET, tmp_title, 18, rgb(226,226,226), SDF_FONT_BOLD);
2018-02-25 15:54:39 +03:00
} else {
2018-04-18 12:05:19 +03:00
draw_sdf_string(ctx, title_offset, TEXT_OFFSET, tmp_title, 18, rgb(147,147,147), SDF_FONT_BOLD);
2018-02-25 15:54:39 +03:00
}
}
free(tmp_title);
/* Buttons */
draw_sprite(ctx, sprites[decors_active + 8], width - 28, 16);
if (!(window->decorator_flags & DECOR_FLAG_NO_MAXIMIZE)) {
draw_sprite(ctx, sprites[decors_active + 9], width - 50, 16);
}
2018-02-25 15:54:39 +03:00
}
static int check_button_press_fancy(yutani_window_t * window, int x, int y) {
2018-04-25 08:03:29 +03:00
if (x >= (int)window->width - 28 && x <= (int)window->width - 18 &&
2018-02-25 15:54:39 +03:00
y >= 16 && y <= 26) {
return DECOR_CLOSE;
}
if (!(window->decorator_flags & DECOR_FLAG_NO_MAXIMIZE)) {
if (x >= (int)window->width - 50 && x <= (int)window->width - 40 &&
y >= 16 && y <= 26) {
return DECOR_MAXIMIZE;
}
}
2018-02-25 15:54:39 +03:00
return 0;
}
void decor_init() {
init_sprite(0, TTK_FANCY_PATH "active/ul.bmp");
init_sprite(1, TTK_FANCY_PATH "active/um.bmp");
init_sprite(2, TTK_FANCY_PATH "active/ur.bmp");
init_sprite(3, TTK_FANCY_PATH "active/ml.bmp");
init_sprite(4, TTK_FANCY_PATH "active/mr.bmp");
init_sprite(5, TTK_FANCY_PATH "active/ll.bmp");
init_sprite(6, TTK_FANCY_PATH "active/lm.bmp");
init_sprite(7, TTK_FANCY_PATH "active/lr.bmp");
init_sprite(8, TTK_FANCY_PATH "active/button-close.bmp");
init_sprite(9, TTK_FANCY_PATH "active/button-maximize.bmp");
2018-02-25 15:54:39 +03:00
init_sprite(INACTIVE + 0, TTK_FANCY_PATH "inactive/ul.bmp");
init_sprite(INACTIVE + 1, TTK_FANCY_PATH "inactive/um.bmp");
init_sprite(INACTIVE + 2, TTK_FANCY_PATH "inactive/ur.bmp");
init_sprite(INACTIVE + 3, TTK_FANCY_PATH "inactive/ml.bmp");
init_sprite(INACTIVE + 4, TTK_FANCY_PATH "inactive/mr.bmp");
init_sprite(INACTIVE + 5, TTK_FANCY_PATH "inactive/ll.bmp");
init_sprite(INACTIVE + 6, TTK_FANCY_PATH "inactive/lm.bmp");
init_sprite(INACTIVE + 7, TTK_FANCY_PATH "inactive/lr.bmp");
init_sprite(INACTIVE + 8, TTK_FANCY_PATH "inactive/button-close.bmp");
init_sprite(INACTIVE + 9, TTK_FANCY_PATH "inactive/button-maximize.bmp");
2018-02-25 15:54:39 +03:00
decor_top_height = 33;
decor_bottom_height = 6;
decor_left_width = 6;
decor_right_width = 6;
decor_render_decorations = render_decorations_fancy;
decor_check_button_press = check_button_press_fancy;
}