From d6afa06c80315d2a51dabc1be427ffad56cd42d2 Mon Sep 17 00:00:00 2001 From: "K. Lange" Date: Sun, 20 May 2018 16:47:35 +0900 Subject: [PATCH] Menu bar in a separate library --- apps/terminal.c | 100 +++---------------------------- base/usr/include/toaru/menu.h | 2 + base/usr/include/toaru/menubar.h | 29 +++++++++ lib/menubar.c | 74 +++++++++++++++++++++++ util/auto-dep.py | 3 +- 5 files changed, 115 insertions(+), 93 deletions(-) create mode 100644 base/usr/include/toaru/menubar.h create mode 100644 lib/menubar.c diff --git a/apps/terminal.c b/apps/terminal.c index a8552cb0..c725d4c5 100644 --- a/apps/terminal.c +++ b/apps/terminal.c @@ -44,6 +44,7 @@ #include #include #include +#include #include #include "terminal-palette.h" @@ -115,7 +116,7 @@ uint32_t window_height = 480; char terminal_title[TERMINAL_TITLE_SIZE]; size_t terminal_title_length = 0; gfx_context_t * ctx; -static void render_decors(); +static void render_decors(void); void term_clear(); void flush_unused_images(void); @@ -333,26 +334,6 @@ void input_buffer_stuff(char * str) { write(fd_master, str, s); } -struct menu_bar_entries { - char * title; - char * action; -}; - -struct menu_bar { - int x; - int y; - int width; - - struct menu_bar_entries * entries; - - struct MenuSet * set; - - struct menu_bar_entries * active_entry; - struct MenuList * active_menu; - int active_menu_wid; -}; - - struct menu_bar terminal_menu_bar = {0}; struct menu_bar_entries terminal_menu_entries[] = { {"File", "file"}, @@ -362,74 +343,8 @@ struct menu_bar_entries terminal_menu_entries[] = { {NULL, NULL}, }; -void menu_bar_render(struct menu_bar * self) { - int _x = self->x; - int _y = self->y; - int width = self->width; - uint32_t menu_bar_color = rgb(59,59,59); - for (int y = 0; y < menu_bar_height; ++y) { - for (int x = 0; x < width; ++x) { - GFX(ctx, x+_x,y+_y) = menu_bar_color; - } - } - - /* for each menu entry */ - int offset = _x; - struct menu_bar_entries * _entries = self->entries; - - while (_entries->title) { - int w = draw_sdf_string_width(_entries->title, 16, SDF_FONT_THIN) + 10; - if ((self->active_menu && hashmap_has(menu_get_windows_hash(), (void*)self->active_menu_wid)) && _entries == self->active_entry) { - for (int y = _y; y < _y + 24; ++y) { - for (int x = offset + 2; x < offset + 2 + w; ++x) { - GFX(ctx, x, y) = rgb(93,163,236); - } - } - } - offset += draw_sdf_string(ctx, offset + 4, _y + 2, _entries->title, 16, rgb(255,255,255), SDF_FONT_THIN) + 10; - _entries++; - } -} - -void menu_bar_show_menu(struct menu_bar * self, int offset, struct menu_bar_entries * _entries) { - struct MenuList * new_menu = menu_set_get_menu(self->set, _entries->action); - menu_show(new_menu, yctx); - yutani_window_move(yctx, new_menu->window, window->x + offset, window->y + self->y + 24); - self->active_menu = new_menu; - self->active_menu_wid = new_menu->window->wid; - self->active_entry = _entries; - render_decors(); /* XXX this is specific to terminal, needs a redraw callback */ -} - -int menu_bar_mouse_event(struct menu_bar * self, struct yutani_msg_window_mouse_event * me, int x, int y) { - if (x < self->x || x >= self->x + self->width || y < self->y || y >= self->y + 24 /* base height */) { - return 0; - } - - int offset = self->x; - - struct menu_bar_entries * _entries = self->entries; - - while (_entries->title) { - int w = draw_sdf_string_width(_entries->title, 16, SDF_FONT_THIN) + 10; - if (x >= offset && x < offset + w) { - if (me->command == YUTANI_MOUSE_EVENT_CLICK) { - menu_bar_show_menu(self,offset,_entries); - } else if (self->active_menu && hashmap_has(menu_get_windows_hash(), (void*)self->active_menu_wid) && _entries != self->active_entry) { - menu_definitely_close(self->active_menu); - menu_bar_show_menu(self,offset,_entries); - } - } - - offset += w; - _entries++; - } - - return 0; -} - -static void render_decors() { +static void render_decors(void) { /* XXX Make the decorations library support Yutani windows */ if (_fullscreen) return; if (!_no_frame) { @@ -437,8 +352,7 @@ static void render_decors() { terminal_menu_bar.x = decor_left_width; terminal_menu_bar.y = decor_top_height; terminal_menu_bar.width = window_width; - terminal_menu_bar.entries = terminal_menu_entries; - menu_bar_render(&terminal_menu_bar); + menu_bar_render(&terminal_menu_bar, ctx); } yutani_window_advertise_icon(yctx, window, terminal_title_length ? terminal_title : "Terminal", "utilities-terminal"); l_x = 0; l_y = 0; @@ -1753,7 +1667,7 @@ void * handle_incoming(void) { break; } - menu_bar_mouse_event(&terminal_menu_bar, me, me->new_x, me->new_y); + menu_bar_mouse_event(yctx, window, &terminal_menu_bar, me, me->new_x, me->new_y); } if (!_no_frame) { @@ -1993,11 +1907,13 @@ int main(int argc, char ** argv) { } /* Set up menus */ + terminal_menu_bar.entries = terminal_menu_entries; + terminal_menu_bar.redraw_callback = render_decors; + struct MenuEntry * _menu_exit = menu_create_normal("exit","exit","Exit", _menu_action_exit); struct MenuEntry * _menu_copy = menu_create_normal(NULL, NULL, "Copy", _menu_action_copy); struct MenuEntry * _menu_paste = menu_create_normal(NULL, NULL, "Paste", _menu_action_paste); - menu_right_click = menu_create(); menu_insert(menu_right_click, _menu_copy); menu_insert(menu_right_click, _menu_paste); diff --git a/base/usr/include/toaru/menu.h b/base/usr/include/toaru/menu.h index 8da907d7..fa0bd470 100644 --- a/base/usr/include/toaru/menu.h +++ b/base/usr/include/toaru/menu.h @@ -1,3 +1,5 @@ +#pragma once + #include #include #include diff --git a/base/usr/include/toaru/menubar.h b/base/usr/include/toaru/menubar.h new file mode 100644 index 00000000..0658b475 --- /dev/null +++ b/base/usr/include/toaru/menubar.h @@ -0,0 +1,29 @@ +#pragma once + +#include + +#define MENU_BAR_HEIGHT 24 + +struct menu_bar_entries { + char * title; + char * action; +}; + +struct menu_bar { + int x; + int y; + int width; + + struct menu_bar_entries * entries; + + struct MenuSet * set; + + struct menu_bar_entries * active_entry; + struct MenuList * active_menu; + int active_menu_wid; + + void (*redraw_callback)(void); +}; + +extern void menu_bar_render(struct menu_bar * self, gfx_context_t * ctx); +extern int menu_bar_mouse_event(yutani_t * yctx, yutani_window_t * window, struct menu_bar * self, struct yutani_msg_window_mouse_event * me, int x, int y); diff --git a/lib/menubar.c b/lib/menubar.c new file mode 100644 index 00000000..68e36437 --- /dev/null +++ b/lib/menubar.c @@ -0,0 +1,74 @@ +#include +#include +#include +#include +#include + +void menu_bar_render(struct menu_bar * self, gfx_context_t * ctx) { + int _x = self->x; + int _y = self->y; + int width = self->width; + + uint32_t menu_bar_color = rgb(59,59,59); + for (int y = 0; y < MENU_BAR_HEIGHT; ++y) { + for (int x = 0; x < width; ++x) { + GFX(ctx, x+_x,y+_y) = menu_bar_color; + } + } + + /* for each menu entry */ + int offset = _x; + struct menu_bar_entries * _entries = self->entries; + + while (_entries->title) { + int w = draw_sdf_string_width(_entries->title, 16, SDF_FONT_THIN) + 10; + if ((self->active_menu && hashmap_has(menu_get_windows_hash(), (void*)self->active_menu_wid)) && _entries == self->active_entry) { + for (int y = _y; y < _y + MENU_BAR_HEIGHT; ++y) { + for (int x = offset + 2; x < offset + 2 + w; ++x) { + GFX(ctx, x, y) = rgb(93,163,236); + } + } + } + offset += draw_sdf_string(ctx, offset + 4, _y + 2, _entries->title, 16, rgb(255,255,255), SDF_FONT_THIN) + 10; + _entries++; + } +} + +void menu_bar_show_menu(yutani_t * yctx, yutani_window_t * window, struct menu_bar * self, int offset, struct menu_bar_entries * _entries) { + struct MenuList * new_menu = menu_set_get_menu(self->set, _entries->action); + menu_show(new_menu, yctx); + yutani_window_move(yctx, new_menu->window, window->x + offset, window->y + self->y + MENU_BAR_HEIGHT); + self->active_menu = new_menu; + self->active_menu_wid = new_menu->window->wid; + self->active_entry = _entries; + if (self->redraw_callback) { + self->redraw_callback(); + } +} + +int menu_bar_mouse_event(yutani_t * yctx, yutani_window_t * window, struct menu_bar * self, struct yutani_msg_window_mouse_event * me, int x, int y) { + if (x < self->x || x >= self->x + self->width || y < self->y || y >= self->y + 24 /* base height */) { + return 0; + } + + int offset = self->x; + + struct menu_bar_entries * _entries = self->entries; + + while (_entries->title) { + int w = draw_sdf_string_width(_entries->title, 16, SDF_FONT_THIN) + 10; + if (x >= offset && x < offset + w) { + if (me->command == YUTANI_MOUSE_EVENT_CLICK) { + menu_bar_show_menu(yctx, window, self,offset,_entries); + } else if (self->active_menu && hashmap_has(menu_get_windows_hash(), (void*)self->active_menu_wid) && _entries != self->active_entry) { + menu_definitely_close(self->active_menu); + menu_bar_show_menu(yctx, window, self,offset,_entries); + } + } + + offset += w; + _entries++; + } + + return 0; +} diff --git a/util/auto-dep.py b/util/auto-dep.py index 8fd930bf..4108b250 100755 --- a/util/auto-dep.py +++ b/util/auto-dep.py @@ -36,7 +36,8 @@ class Classifier(object): '': (None, '-ltoaru_termemu', ['']), '': (None, '-ltoaru_sdf', ['', '']), '': (None, '-ltoaru_icon_cache', ['', '']), - '': (None, '-ltoaru_menu', ['', '', '', '']), + '': (None, '-ltoaru_menu', ['', '', '', '', '']), + '': (None, '-ltoaru_menubar', ['', '', '', '', '']), } def __init__(self, filename):