Close button support in decorations library
This commit is contained in:
parent
62de55b5f4
commit
49752793b7
Before Width: | Height: | Size: 223 B After Width: | Height: | Size: 223 B |
BIN
hdd/usr/share/ttk/inactive/button-close.png
Normal file
BIN
hdd/usr/share/ttk/inactive/button-close.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 209 B |
@ -129,31 +129,6 @@ ttk_button * ttk_button_new(char * title, void (*callback)(void *, struct yutani
|
||||
return out;
|
||||
}
|
||||
|
||||
static cairo_surface_t * close_button_sprite;
|
||||
void ttk_render_decor_button_close(void * s, cairo_t * cr) {
|
||||
cairo_save(cr);
|
||||
cairo_set_source_rgb(cr, 244.0, 244.0, 244.0);
|
||||
|
||||
double x = ((ttk_object *)s)->x;
|
||||
double y = ((ttk_object *)s)->y;
|
||||
|
||||
cairo_set_source_surface(cr, close_button_sprite, x + 1, y + 1);
|
||||
cairo_paint(cr);
|
||||
|
||||
cairo_restore(cr);
|
||||
}
|
||||
|
||||
ttk_button * ttk_decor_button_close(void (*callback)(void *, struct yutani_msg_window_mouse_event *)) {
|
||||
if (!close_button_sprite) {
|
||||
close_button_sprite = cairo_image_surface_create_from_png("/usr/share/ttk/common/button-close.png"); /* TTK_PATH ? something less dumb? */
|
||||
}
|
||||
ttk_button * out = ttk_button_new("Close" /* For future tooltips */, callback);
|
||||
((ttk_object *)out)->render_func = ttk_render_decor_button_close;
|
||||
((ttk_object *)out)->width = 10;
|
||||
((ttk_object *)out)->height = 10;
|
||||
return out;
|
||||
}
|
||||
|
||||
ttk_raw_surface * ttk_raw_surface_new(int width, int height) {
|
||||
ttk_raw_surface * out = malloc(sizeof(ttk_raw_surface));
|
||||
|
||||
@ -212,9 +187,6 @@ void ttk_check_click(struct yutani_msg_window_mouse_event * evt) {
|
||||
}
|
||||
} else if (evt->command == YUTANI_MOUSE_EVENT_DOWN) {
|
||||
fprintf(stderr, "Mouse down: %d, %d\n", evt->new_x, evt->new_y);
|
||||
if (evt->new_y < decor_top_height && evt->new_x < (wina->width - 28)) {
|
||||
yutani_window_drag_start(yctx, wina);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -279,7 +251,6 @@ uint16_t quit = 0;
|
||||
ttk_button * button_red;
|
||||
ttk_button * button_green;
|
||||
ttk_button * button_blue;
|
||||
ttk_button * close_button;
|
||||
|
||||
ttk_button * button_thick;
|
||||
ttk_button * button_thin;
|
||||
@ -299,10 +270,6 @@ static void set_color(void * button, struct yutani_msg_window_mouse_event * even
|
||||
ttk_render();
|
||||
}
|
||||
|
||||
static void quit_app(void * button, struct yutani_msg_window_mouse_event * event) {
|
||||
quit = 1;
|
||||
}
|
||||
|
||||
static void set_thickness_thick(void * button, struct yutani_msg_window_mouse_event * event) {
|
||||
#if 0
|
||||
button_thick->fill_color = rgb(127,127,127);
|
||||
@ -332,7 +299,6 @@ static void set_thickness_thin(void * button, struct yutani_msg_window_mouse_eve
|
||||
static void resize_finish(int width, int height) {
|
||||
yutani_window_resize_accept(yctx, wina, width, height);
|
||||
reinit_graphics_yutani(ctx, wina);
|
||||
((ttk_object *)close_button)->x = wina->width - 28;
|
||||
ttk_render();
|
||||
yutani_window_resize_done(yctx, wina);
|
||||
yutani_flip(yctx, wina);
|
||||
@ -414,11 +380,6 @@ int main (int argc, char ** argv) {
|
||||
setup_ttk(wina);
|
||||
|
||||
|
||||
close_button = ttk_decor_button_close(quit_app);
|
||||
|
||||
((ttk_object *)close_button)->x = width - 28;
|
||||
((ttk_object *)close_button)->y = 16;
|
||||
|
||||
button_blue = ttk_button_new("Blue", set_color);
|
||||
ttk_position((ttk_object *)button_blue, decor_left_width + 3, decor_top_height + 3, 100, 20);
|
||||
button_blue->fill_color = rgb(0,0,255);
|
||||
@ -485,6 +446,10 @@ int main (int argc, char ** argv) {
|
||||
case YUTANI_MSG_WINDOW_MOUSE_EVENT:
|
||||
{
|
||||
struct yutani_msg_window_mouse_event * me = (void*)m->data;
|
||||
if (decor_handle_event(yctx, m) == DECOR_CLOSE) {
|
||||
quit = 1;
|
||||
break;
|
||||
}
|
||||
if (me->command == YUTANI_MOUSE_EVENT_DRAG && me->buttons & YUTANI_MOUSE_BUTTON_LEFT) {
|
||||
keep_drawing(me);
|
||||
yutani_flip(yctx, wina);
|
||||
|
@ -241,13 +241,8 @@ char handle_event(yutani_msg_t * m) {
|
||||
}
|
||||
break;
|
||||
case YUTANI_MSG_WINDOW_MOUSE_EVENT:
|
||||
{
|
||||
struct yutani_msg_window_mouse_event * me = (void*)m->data;
|
||||
if (me->command == YUTANI_MOUSE_EVENT_DOWN && me->buttons & YUTANI_MOUSE_BUTTON_LEFT) {
|
||||
if (me->new_y < decor_top_height) {
|
||||
yutani_window_drag_start(yctx, window);
|
||||
}
|
||||
}
|
||||
if (decor_handle_event(yctx, m) == DECOR_CLOSE) {
|
||||
return 'q';
|
||||
}
|
||||
break;
|
||||
case YUTANI_MSG_SESSION_END:
|
||||
|
@ -277,11 +277,14 @@ int main(int argc, char * argv[]) {
|
||||
break;
|
||||
case YUTANI_MSG_WINDOW_MOUSE_EVENT:
|
||||
{
|
||||
struct yutani_msg_window_mouse_event * me = (void*)m->data;
|
||||
if (me->command == YUTANI_MOUSE_EVENT_DOWN && me->buttons & YUTANI_MOUSE_BUTTON_LEFT) {
|
||||
if (me->new_y < decor_top_height) {
|
||||
yutani_window_drag_start(yctx, window);
|
||||
}
|
||||
int result = decor_handle_event(yctx, m);
|
||||
switch (result) {
|
||||
case DECOR_CLOSE:
|
||||
playing = 0;
|
||||
break;
|
||||
default:
|
||||
/* Other actions */
|
||||
break;
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
@ -154,13 +154,8 @@ int main (int argc, char ** argv) {
|
||||
}
|
||||
break;
|
||||
case YUTANI_MSG_WINDOW_MOUSE_EVENT:
|
||||
{
|
||||
struct yutani_msg_window_mouse_event * me = (void*)m->data;
|
||||
if (me->command == YUTANI_MOUSE_EVENT_DOWN && me->buttons & YUTANI_MOUSE_BUTTON_LEFT) {
|
||||
if (me->new_y < decor_top_height) {
|
||||
yutani_window_drag_start(yctx, wina);
|
||||
}
|
||||
}
|
||||
if (decor_handle_event(yctx, m) == DECOR_CLOSE) {
|
||||
should_exit = 1;
|
||||
}
|
||||
break;
|
||||
default:
|
||||
|
@ -1115,11 +1115,10 @@ void * handle_incoming(void * garbage) {
|
||||
case YUTANI_MSG_WINDOW_MOUSE_EVENT:
|
||||
{
|
||||
struct yutani_msg_window_mouse_event * me = (void*)m->data;
|
||||
if (me->command == YUTANI_MOUSE_EVENT_DOWN && me->buttons & YUTANI_MOUSE_BUTTON_LEFT) {
|
||||
if (!_no_frame) {
|
||||
if (me->new_y < decor_top_height) {
|
||||
yutani_window_drag_start(yctx, window);
|
||||
}
|
||||
if (!_no_frame) {
|
||||
if (decor_handle_event(yctx, m) == DECOR_CLOSE) {
|
||||
kill(child_pid, SIGKILL);
|
||||
exit_application = 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -423,12 +423,13 @@ int ttk_run(ttk_window_t * window) {
|
||||
case YUTANI_MSG_WINDOW_MOUSE_EVENT:
|
||||
{
|
||||
struct yutani_msg_window_mouse_event * me = (void*)m->data;
|
||||
if (decor_handle_event(yctx, m) == DECOR_CLOSE) {
|
||||
goto done;
|
||||
}
|
||||
if (me->command == YUTANI_MOUSE_EVENT_DOWN && me->buttons & YUTANI_MOUSE_BUTTON_LEFT) {
|
||||
ttk_window_t * win = hashmap_get(ttk_wids_to_windows, (void*)me->wid);
|
||||
if (win) {
|
||||
if (me->new_y < decor_top_height) {
|
||||
yutani_window_drag_start(yctx, win->core_window);
|
||||
}
|
||||
/* Do something */
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -21,7 +21,7 @@ uint32_t decor_right_width = 6;
|
||||
#define TEXT_OFFSET_X 10
|
||||
#define TEXT_OFFSET_Y 16
|
||||
|
||||
#define INACTIVE 8
|
||||
#define INACTIVE 9
|
||||
|
||||
#define BORDERCOLOR rgb(60,60,60)
|
||||
#define BORDERCOLOR_INACTIVE rgb(30,30,30)
|
||||
@ -29,6 +29,8 @@ uint32_t decor_right_width = 6;
|
||||
#define TEXTCOLOR rgb(230,230,230)
|
||||
#define TEXTCOLOR_INACTIVE rgb(140,140,140)
|
||||
|
||||
#define TTK_FANCY_PATH "/usr/share/ttk/"
|
||||
|
||||
static int u_height = 33;
|
||||
static int ul_width = 10;
|
||||
static int ur_width = 10;
|
||||
@ -42,7 +44,7 @@ static int lly_offset = 3;
|
||||
static int lrx_offset = 3;
|
||||
static int lry_offset = 3;
|
||||
|
||||
static sprite_t * sprites[16];
|
||||
static sprite_t * sprites[20];
|
||||
|
||||
#define TEXT_OFFSET 24
|
||||
|
||||
@ -52,6 +54,11 @@ static void init_sprite_png(int id, char * path) {
|
||||
}
|
||||
|
||||
static void (*render_decorations_)(yutani_window_t *, gfx_context_t *, char *, int) = NULL;
|
||||
static int (*check_button_press)(yutani_window_t *, int x, int y) = NULL;
|
||||
|
||||
static void (*callback_close)(yutani_window_t *) = NULL;
|
||||
static void (*callback_resize)(yutani_window_t *) = NULL;
|
||||
|
||||
|
||||
static void render_decorations_simple(yutani_window_t * window, gfx_context_t * ctx, char * title, int decors_active) {
|
||||
|
||||
@ -82,8 +89,10 @@ static void render_decorations_simple(yutani_window_t * window, gfx_context_t *
|
||||
GFX(ctx, i, decor_top_height - 1) = color;
|
||||
GFX(ctx, i, window->height - 1) = color;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static int check_button_press_simple(yutani_window_t * window, int x, int y) {
|
||||
return 0; /* no buttons in simple mode */
|
||||
}
|
||||
|
||||
static void initialize_simple() {
|
||||
@ -93,6 +102,7 @@ static void initialize_simple() {
|
||||
decor_right_width = 1;
|
||||
|
||||
render_decorations_ = render_decorations_simple;
|
||||
check_button_press = check_button_press_simple;
|
||||
}
|
||||
|
||||
static void render_decorations_fancy(yutani_window_t * window, gfx_context_t * ctx, char * title, int decors_active) {
|
||||
@ -144,26 +154,40 @@ static void render_decorations_fancy(yutani_window_t * window, gfx_context_t * c
|
||||
} else {
|
||||
draw_string(ctx, title_offset, TEXT_OFFSET, rgb(147,147,147), title);
|
||||
}
|
||||
|
||||
/* Buttons */
|
||||
draw_sprite(ctx, sprites[decors_active + 8], width - 28, 16);
|
||||
}
|
||||
|
||||
static int check_button_press_fancy(yutani_window_t * window, int x, int y) {
|
||||
if (x >= window->width - 28 && x <= window->width - 18 &&
|
||||
y >= 16 && y <= 26) {
|
||||
return DECOR_CLOSE;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void initialize_fancy() {
|
||||
init_sprite_png(0, "/usr/share/ttk/active/ul.png");
|
||||
init_sprite_png(1, "/usr/share/ttk/active/um.png");
|
||||
init_sprite_png(2, "/usr/share/ttk/active/ur.png");
|
||||
init_sprite_png(3, "/usr/share/ttk/active/ml.png");
|
||||
init_sprite_png(4, "/usr/share/ttk/active/mr.png");
|
||||
init_sprite_png(5, "/usr/share/ttk/active/ll.png");
|
||||
init_sprite_png(6, "/usr/share/ttk/active/lm.png");
|
||||
init_sprite_png(7, "/usr/share/ttk/active/lr.png");
|
||||
init_sprite_png(0, TTK_FANCY_PATH "active/ul.png");
|
||||
init_sprite_png(1, TTK_FANCY_PATH "active/um.png");
|
||||
init_sprite_png(2, TTK_FANCY_PATH "active/ur.png");
|
||||
init_sprite_png(3, TTK_FANCY_PATH "active/ml.png");
|
||||
init_sprite_png(4, TTK_FANCY_PATH "active/mr.png");
|
||||
init_sprite_png(5, TTK_FANCY_PATH "active/ll.png");
|
||||
init_sprite_png(6, TTK_FANCY_PATH "active/lm.png");
|
||||
init_sprite_png(7, TTK_FANCY_PATH "active/lr.png");
|
||||
init_sprite_png(8, TTK_FANCY_PATH "active/button-close.png");
|
||||
|
||||
init_sprite_png(INACTIVE + 0, "/usr/share/ttk/inactive/ul.png");
|
||||
init_sprite_png(INACTIVE + 1, "/usr/share/ttk/inactive/um.png");
|
||||
init_sprite_png(INACTIVE + 2, "/usr/share/ttk/inactive/ur.png");
|
||||
init_sprite_png(INACTIVE + 3, "/usr/share/ttk/inactive/ml.png");
|
||||
init_sprite_png(INACTIVE + 4, "/usr/share/ttk/inactive/mr.png");
|
||||
init_sprite_png(INACTIVE + 5, "/usr/share/ttk/inactive/ll.png");
|
||||
init_sprite_png(INACTIVE + 6, "/usr/share/ttk/inactive/lm.png");
|
||||
init_sprite_png(INACTIVE + 7, "/usr/share/ttk/inactive/lr.png");
|
||||
init_sprite_png(INACTIVE + 0, TTK_FANCY_PATH "inactive/ul.png");
|
||||
init_sprite_png(INACTIVE + 1, TTK_FANCY_PATH "inactive/um.png");
|
||||
init_sprite_png(INACTIVE + 2, TTK_FANCY_PATH "inactive/ur.png");
|
||||
init_sprite_png(INACTIVE + 3, TTK_FANCY_PATH "inactive/ml.png");
|
||||
init_sprite_png(INACTIVE + 4, TTK_FANCY_PATH "inactive/mr.png");
|
||||
init_sprite_png(INACTIVE + 5, TTK_FANCY_PATH "inactive/ll.png");
|
||||
init_sprite_png(INACTIVE + 6, TTK_FANCY_PATH "inactive/lm.png");
|
||||
init_sprite_png(INACTIVE + 7, TTK_FANCY_PATH "inactive/lr.png");
|
||||
init_sprite_png(INACTIVE + 8, TTK_FANCY_PATH "inactive/button-close.png");
|
||||
|
||||
decor_top_height = 33;
|
||||
decor_bottom_height = 6;
|
||||
@ -171,6 +195,7 @@ static void initialize_fancy() {
|
||||
decor_right_width = 6;
|
||||
|
||||
render_decorations_ = render_decorations_fancy;
|
||||
check_button_press = check_button_press_fancy;
|
||||
}
|
||||
|
||||
void render_decorations(yutani_window_t * window, gfx_context_t * ctx, char * title) {
|
||||
@ -206,4 +231,49 @@ uint32_t decor_height() {
|
||||
return decor_top_height + decor_bottom_height;
|
||||
}
|
||||
|
||||
void decor_set_close_callback(void (*callback)(yutani_window_t *)) {
|
||||
callback_close = callback;
|
||||
}
|
||||
|
||||
void decor_set_resize_callback(void (*callback)(yutani_window_t *)) {
|
||||
callback_resize = callback;
|
||||
}
|
||||
|
||||
int decor_handle_event(yutani_t * yctx, yutani_msg_t * m) {
|
||||
if (m) {
|
||||
switch (m->type) {
|
||||
case YUTANI_MSG_WINDOW_MOUSE_EVENT:
|
||||
{
|
||||
struct yutani_msg_window_mouse_event * me = (void*)m->data;
|
||||
if (me->new_y < decor_top_height) {
|
||||
yutani_window_t * window = hashmap_get(yctx->windows, (void*)me->wid);
|
||||
if (window) {
|
||||
int button = check_button_press(window, me->new_x, me->new_y);
|
||||
if (me->command == YUTANI_MOUSE_EVENT_DOWN && me->buttons & YUTANI_MOUSE_BUTTON_LEFT) {
|
||||
if (!button) {
|
||||
yutani_window_drag_start(yctx, window);
|
||||
return DECOR_OTHER;
|
||||
}
|
||||
}
|
||||
if (me->command == YUTANI_MOUSE_EVENT_CLICK) {
|
||||
/* Determine if we clicked on a button */
|
||||
switch (button) {
|
||||
case DECOR_CLOSE:
|
||||
if (callback_close) callback_close(window);
|
||||
break;
|
||||
case DECOR_RESIZE:
|
||||
if (callback_resize) callback_resize(window);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
return button;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
@ -33,4 +33,15 @@ void init_decorations();
|
||||
uint32_t decor_width();
|
||||
uint32_t decor_height();
|
||||
|
||||
int decor_handle_event(yutani_t * yctx, yutani_msg_t * m);
|
||||
|
||||
/* Callbacks for handle_event */
|
||||
void decor_set_close_callback(void (*callback)(yutani_window_t *));
|
||||
void decor_set_resize_callback(void (*callback)(yutani_window_t *));
|
||||
|
||||
/* Responses from handle_event */
|
||||
#define DECOR_OTHER 1
|
||||
#define DECOR_CLOSE 2
|
||||
#define DECOR_RESIZE 3
|
||||
|
||||
#endif /* DECORATION_H */
|
||||
|
Loading…
x
Reference in New Issue
Block a user