menu.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977
  1. /* vim: tabstop=4 shiftwidth=4 noexpandtab
  2. * This file is part of ToaruOS and is released under the terms
  3. * of the NCSA / University of Illinois License - see LICENSE.md
  4. * Copyright (C) 2018 K. Lange
  5. *
  6. * menu - Provides menus.
  7. *
  8. * C reimplementation of the original Python menu library.
  9. * Used to provide menu bars and the applications menu.
  10. */
  11. #include <stdio.h>
  12. #include <unistd.h>
  13. #include <fcntl.h>
  14. #include <math.h>
  15. #include <sys/types.h>
  16. #include <dlfcn.h>
  17. #include <toaru/yutani.h>
  18. #include <toaru/graphics.h>
  19. #include <toaru/sdf.h>
  20. #include <toaru/hashmap.h>
  21. #include <toaru/list.h>
  22. #include <toaru/icon_cache.h>
  23. #include <toaru/menu.h>
  24. #define MENU_ENTRY_HEIGHT 20
  25. #define MENU_BACKGROUND rgb(239,238,232)
  26. #define MENU_ICON_SIZE 16
  27. #define HILIGHT_BORDER_TOP rgb(54,128,205)
  28. #define HILIGHT_GRADIENT_TOP rgb(93,163,236)
  29. #define HILIGHT_GRADIENT_BOTTOM rgb(56,137,220)
  30. #define HILIGHT_BORDER_BOTTOM rgb(47,106,167)
  31. static hashmap_t * menu_windows = NULL;
  32. static yutani_t * my_yctx = NULL;
  33. static struct MenuList * hovered_menu = NULL;
  34. int menu_definitely_close(struct MenuList * menu);
  35. /** Freetype extension renderer functions */
  36. static int _have_freetype = 0;
  37. static void (*freetype_set_font_face)(int face) = NULL;
  38. static void (*freetype_set_font_size)(int size) = NULL;
  39. static int (*freetype_draw_string)(gfx_context_t * ctx, int x, int y, uint32_t fg, const char * s) = NULL;
  40. static int (*freetype_draw_string_width)(char * s) = NULL;
  41. __attribute__((constructor))
  42. static void _init_menus(void) {
  43. menu_windows = hashmap_create_int(10);
  44. void * freetype = dlopen("libtoaru_ext_freetype_fonts.so", 0);
  45. if (freetype) {
  46. _have_freetype = 1;
  47. freetype_set_font_face = dlsym(freetype, "freetype_set_font_face");
  48. freetype_set_font_size = dlsym(freetype, "freetype_set_font_size");
  49. freetype_draw_string = dlsym(freetype, "freetype_draw_string");
  50. freetype_draw_string_width = dlsym(freetype, "freetype_draw_string_width");
  51. }
  52. }
  53. hashmap_t * menu_get_windows_hash(void) {
  54. return menu_windows;
  55. }
  56. static int string_width(const char * s) {
  57. if (_have_freetype) {
  58. freetype_set_font_face(0); /* regular non-monospace */
  59. freetype_set_font_size(13);
  60. return freetype_draw_string_width((char *)s);
  61. } else {
  62. return draw_sdf_string_width((char *)s, 16, SDF_FONT_THIN);
  63. }
  64. }
  65. static int draw_string(gfx_context_t * ctx, int x, int y, uint32_t color, const char * s) {
  66. if (_have_freetype) {
  67. freetype_set_font_face(0); /* regular non-monospace */
  68. freetype_set_font_size(13);
  69. return freetype_draw_string(ctx, x+2, y + 13 /* I think? */, color, s);
  70. } else {
  71. return draw_sdf_string(ctx, x, y, s, 16, color, SDF_FONT_THIN);
  72. }
  73. }
  74. void _menu_draw_MenuEntry_Normal(gfx_context_t * ctx, struct MenuEntry * self, int offset) {
  75. struct MenuEntry_Normal * _self = (struct MenuEntry_Normal *)self;
  76. _self->offset = offset;
  77. /* Background gradient */
  78. if (_self->hilight) {
  79. draw_line(ctx, 1, _self->width-2, offset, offset, HILIGHT_BORDER_TOP);
  80. draw_line(ctx, 1, _self->width-2, offset + _self->height - 1, offset + _self->height - 1, HILIGHT_BORDER_BOTTOM);
  81. for (int i = 1; i < self->height-1; ++i) {
  82. int thing = ((i - 1) * 256) / (_self->height - 2);
  83. if (thing > 255) thing = 255;
  84. if (thing < 0) thing = 0;
  85. uint32_t c = interp_colors(HILIGHT_GRADIENT_TOP, HILIGHT_GRADIENT_BOTTOM, thing);
  86. draw_line(ctx, 1, self->width-2, offset + i, offset + i, c);
  87. }
  88. }
  89. /* Icon */
  90. if (_self->icon) {
  91. sprite_t * icon = icon_get_16(_self->icon);
  92. if (icon->width == MENU_ICON_SIZE) {
  93. draw_sprite(ctx, icon, 4, offset + 2);
  94. } else {
  95. draw_sprite_scaled(ctx, icon, 4, offset + 2, MENU_ICON_SIZE, MENU_ICON_SIZE);
  96. }
  97. }
  98. /* Foreground text color */
  99. uint32_t color = _self->hilight ? rgb(255,255,255) : rgb(0,0,0);
  100. /* Draw title */
  101. draw_string(ctx, 22, offset + 1, color, _self->title);
  102. }
  103. void _menu_focus_MenuEntry_Normal(struct MenuEntry * self, int focused) {
  104. if (focused) {
  105. if (self->_owner && self->_owner->child) {
  106. menu_definitely_close(self->_owner->child);
  107. self->_owner->child = NULL;
  108. }
  109. }
  110. }
  111. void _menu_activate_MenuEntry_Normal(struct MenuEntry * self, int flags) {
  112. struct MenuEntry_Normal * _self = (struct MenuEntry_Normal *)self;
  113. list_t * menu_keys = hashmap_keys(menu_windows);
  114. hovered_menu = NULL;
  115. foreach(_key, menu_keys) {
  116. yutani_window_t * window = hashmap_get(menu_windows, (void*)_key->value);
  117. if (window) {
  118. struct MenuList * menu = window->user_data;
  119. menu_definitely_close(menu);
  120. if (menu->parent && menu->parent->child == menu) {
  121. menu->parent->child = NULL;
  122. }
  123. }
  124. }
  125. list_free(menu_keys);
  126. free(menu_keys);
  127. if (_self->callback) {
  128. _self->callback(_self);
  129. }
  130. }
  131. struct MenuEntry * menu_create_normal(const char * icon, const char * action, const char * title, void (*callback)(struct MenuEntry *)) {
  132. struct MenuEntry_Normal * out = malloc(sizeof(struct MenuEntry_Normal));
  133. out->_type = MenuEntry_Normal;
  134. out->height = MENU_ENTRY_HEIGHT;
  135. out->hilight = 0;
  136. out->renderer = _menu_draw_MenuEntry_Normal;
  137. out->focus_change = _menu_focus_MenuEntry_Normal;
  138. out->activate = _menu_activate_MenuEntry_Normal;
  139. out->icon = icon ? strdup(icon) : NULL;
  140. out->title = strdup(title);
  141. out->action = action ? strdup(action) : NULL;
  142. out->callback = callback;
  143. out->rwidth = 50 + string_width(out->title);
  144. return (struct MenuEntry *)out;
  145. }
  146. void _menu_draw_MenuEntry_Submenu(gfx_context_t * ctx, struct MenuEntry * self, int offset) {
  147. struct MenuEntry_Submenu * _self = (struct MenuEntry_Submenu *)self;
  148. int h = _self->hilight;
  149. if (_self->_owner && _self->_my_child && _self->_owner->child == _self->_my_child) {
  150. _self->hilight = 1;
  151. }
  152. _menu_draw_MenuEntry_Normal(ctx,self,offset);
  153. /* Draw the tick on the right side to indicate this is a submenu */
  154. uint32_t color = _self->hilight ? rgb(255,255,255) : rgb(0,0,0);
  155. sprite_t * tick = icon_get_16("menu-tick");
  156. draw_sprite_alpha_paint(ctx, tick, _self->width - 16, offset + 2, 1.0, color);
  157. _self->hilight = h;
  158. }
  159. void _menu_focus_MenuEntry_Submenu(struct MenuEntry * self, int focused) {
  160. if (focused) {
  161. self->activate(self, focused);
  162. }
  163. }
  164. void _menu_activate_MenuEntry_Submenu(struct MenuEntry * self, int focused) {
  165. struct MenuEntry_Submenu * _self = (struct MenuEntry_Submenu *)self;
  166. if (_self->_owner && _self->_owner->set) {
  167. /* Show a menu */
  168. struct MenuList * new_menu = menu_set_get_menu(_self->_owner->set, (char *)_self->action);
  169. if (_self->_owner->child && _self->_owner->child != new_menu) {
  170. menu_definitely_close(_self->_owner->child);
  171. _self->_owner->child = NULL;
  172. }
  173. new_menu->parent = _self->_owner;
  174. new_menu->parent->child = new_menu;
  175. _self->_my_child = new_menu;
  176. if (new_menu->closed) {
  177. menu_show(new_menu, _self->_owner->window->ctx);
  178. if (_self->_owner->window->width + _self->_owner->window->x - 2 + new_menu->window->width > _self->_owner->window->ctx->display_width) {
  179. yutani_window_move(_self->_owner->window->ctx, new_menu->window, _self->_owner->window->x + 2 - new_menu->window->width, _self->_owner->window->y + _self->offset - 4);
  180. } else {
  181. yutani_window_move(_self->_owner->window->ctx, new_menu->window, _self->_owner->window->width + _self->_owner->window->x - 2, _self->_owner->window->y + _self->offset - 4);
  182. }
  183. }
  184. }
  185. }
  186. struct MenuEntry * menu_create_submenu(const char * icon, const char * action, const char * title) {
  187. struct MenuEntry_Submenu * out = malloc(sizeof(struct MenuEntry_Submenu));
  188. out->_type = MenuEntry_Submenu;
  189. out->height = MENU_ENTRY_HEIGHT;
  190. out->hilight = 0;
  191. out->renderer = _menu_draw_MenuEntry_Submenu;
  192. out->focus_change = _menu_focus_MenuEntry_Submenu;
  193. out->activate = _menu_activate_MenuEntry_Submenu;
  194. out->icon = icon ? strdup(icon) : NULL;
  195. out->title = strdup(title);
  196. out->action = action ? strdup(action) : NULL;
  197. out->rwidth = 50 + string_width(out->title);
  198. return (struct MenuEntry *)out;
  199. }
  200. void _menu_draw_MenuEntry_Separator(gfx_context_t * ctx, struct MenuEntry * self, int offset) {
  201. self->offset = offset;
  202. draw_line(ctx, 2, self->width-4, offset+3, offset+3, rgb(178,178,178));
  203. draw_line(ctx, 2, self->width-5, offset+4, offset+4, rgb(250,250,250));
  204. }
  205. void _menu_focus_MenuEntry_Separator(struct MenuEntry * self, int focused) {
  206. if (focused) {
  207. if (self->_owner && self->_owner->child) {
  208. menu_definitely_close(self->_owner->child);
  209. self->_owner->child = NULL;
  210. }
  211. }
  212. }
  213. void _menu_activate_MenuEntry_Separator(struct MenuEntry * self, int focused) {
  214. }
  215. struct MenuEntry * menu_create_separator(void) {
  216. struct MenuEntry_Separator * out = malloc(sizeof(struct MenuEntry_Separator));
  217. out->_type = MenuEntry_Separator;
  218. out->height = 6;
  219. out->hilight = 0;
  220. out->renderer = _menu_draw_MenuEntry_Separator;
  221. out->focus_change = _menu_focus_MenuEntry_Separator;
  222. out->rwidth = 10; /* at least a bit please */
  223. out->activate = _menu_activate_MenuEntry_Separator;
  224. return (struct MenuEntry *)out;
  225. }
  226. void menu_update_title(struct MenuEntry * self, char * new_title) {
  227. if (self->_type == MenuEntry_Normal) {
  228. struct MenuEntry_Normal * _self = (struct MenuEntry_Normal *)self;
  229. if (_self->title) {
  230. free((void*)_self->title);
  231. }
  232. _self->title = strdup(new_title);
  233. _self->rwidth = 50 + string_width(_self->title);
  234. } else if (self->_type == MenuEntry_Submenu) {
  235. struct MenuEntry_Submenu * _self = (struct MenuEntry_Submenu *)self;
  236. if (_self->title) {
  237. free((void*)_self->title);
  238. }
  239. _self->title = strdup(new_title);
  240. _self->rwidth = 50 + string_width(_self->title);
  241. }
  242. }
  243. static int _close_enough(struct yutani_msg_window_mouse_event * me) {
  244. if (me->command == YUTANI_MOUSE_EVENT_RAISE && sqrt(pow(me->new_x - me->old_x, 2) + pow(me->new_y - me->old_y, 2)) < 10) {
  245. return 1;
  246. }
  247. return 0;
  248. }
  249. static char read_buf[1024];
  250. static size_t available = 0;
  251. static size_t offset = 0;
  252. static size_t read_from = 0;
  253. static char * read_line(FILE * f, char * out, ssize_t len) {
  254. while (len > 0) {
  255. if (available == 0) {
  256. if (offset == 1024) {
  257. offset = 0;
  258. }
  259. size_t r = read(fileno(f), &read_buf[offset], 1024 - offset);
  260. read_from = offset;
  261. available = r;
  262. offset += available;
  263. }
  264. if (available == 0) {
  265. *out = '\0';
  266. return out;
  267. }
  268. while (read_from < offset && len > 0) {
  269. *out = read_buf[read_from];
  270. len--;
  271. read_from++;
  272. available--;
  273. if (*out == '\n') {
  274. return out;
  275. }
  276. out++;
  277. }
  278. }
  279. return out;
  280. }
  281. static void _menu_calculate_dimensions(struct MenuList * menu, int * height, int * width) {
  282. list_t * list = menu->entries;
  283. *width = 0;
  284. *height = 8; /* TODO top and height */
  285. foreach(node, list) {
  286. struct MenuEntry * entry = node->value;
  287. *height += entry->height;
  288. if (*width < entry->rwidth) {
  289. *width = entry->rwidth;
  290. }
  291. }
  292. /* Go back through and update actual widths */
  293. foreach(node, list) {
  294. struct MenuEntry * entry = node->value;
  295. entry->width = *width;
  296. }
  297. }
  298. struct MenuList * menu_set_get_root(struct MenuSet * menu) {
  299. return (void*)hashmap_get(menu->_menus,"_");
  300. }
  301. struct MenuList * menu_set_get_menu(struct MenuSet * menu, char * submenu) {
  302. return (void*)hashmap_get(menu->_menus, submenu);
  303. }
  304. void menu_insert(struct MenuList * menu, struct MenuEntry * entry) {
  305. list_insert(menu->entries, entry);
  306. entry->_owner = menu;
  307. }
  308. struct MenuList * menu_create(void) {
  309. struct MenuList * p = malloc(sizeof(struct MenuList));
  310. p->entries = list_create();
  311. p->ctx = NULL;
  312. p->window = NULL;
  313. p->set = NULL;
  314. p->child = NULL;
  315. p->_bar = NULL;
  316. p->parent = NULL;
  317. p->closed = 1;
  318. return p;
  319. }
  320. struct MenuSet * menu_set_create(void) {
  321. struct MenuSet * _out = malloc(sizeof(struct MenuSet));
  322. _out->_menus = hashmap_create(10);
  323. return _out;
  324. }
  325. void menu_set_insert(struct MenuSet * set, char * action, struct MenuList * menu) {
  326. hashmap_set(set->_menus, action, menu);
  327. menu->set = set;
  328. }
  329. struct MenuSet * menu_set_from_description(const char * path, void (*callback)(struct MenuEntry *)) {
  330. FILE * f;
  331. if (!strcmp(path,"-")) {
  332. f = stdin;
  333. } else {
  334. f = fopen(path,"r");
  335. }
  336. if (!f) {
  337. return NULL;
  338. }
  339. struct MenuSet * _out = malloc(sizeof(struct MenuSet));
  340. hashmap_t * out = hashmap_create(10);
  341. _out->_menus = out;
  342. struct MenuList * current_menu = NULL;
  343. /* Read through the file */
  344. char line[256];
  345. while (1) {
  346. memset(line, 0, 256);
  347. read_line(f, line, 256);
  348. if (!*line) break;
  349. if (line[strlen(line)-1] == '\n') {
  350. line[strlen(line)-1] = '\0';
  351. }
  352. if (!*line) continue; /* skip blank */
  353. if (*line == ':') {
  354. /* New menu */
  355. struct MenuList * p = malloc(sizeof(struct MenuList));
  356. p->entries = list_create();
  357. p->ctx = NULL;
  358. p->window = NULL;
  359. p->set = _out;
  360. p->child = NULL;
  361. p->_bar = NULL;
  362. p->parent = NULL;
  363. p->closed = 1;
  364. hashmap_set(out, line+1, p);
  365. current_menu = p;
  366. } else if (*line == '#') {
  367. /* Comment */
  368. continue;
  369. } else if (*line == '-') {
  370. if (!current_menu) {
  371. fprintf(stderr, "Tried to add separator with no active menu.\n");
  372. goto failure;
  373. }
  374. menu_insert(current_menu, menu_create_separator());
  375. } else if (*line == '&') {
  376. if (!current_menu) {
  377. fprintf(stderr, "Tried to add submenu with no active menu.\n");
  378. goto failure;
  379. }
  380. char * action = line+1;
  381. char * icon = strstr(action,",");
  382. if (!icon) {
  383. fprintf(stderr, "Malformed line in submenu: no icon\n");
  384. goto failure;
  385. }
  386. *icon = '\0';
  387. icon++;
  388. char * title = strstr(icon,",");
  389. if (!title) {
  390. fprintf(stderr, "Malformed line in submenu: no title\n");
  391. goto failure;
  392. }
  393. *title = '\0';
  394. title++;
  395. menu_insert(current_menu, menu_create_submenu(icon,action,title));
  396. } else {
  397. if (!current_menu) {
  398. fprintf(stderr, "Tried to add item with no active menu.\n");
  399. goto failure;
  400. }
  401. char * action = line;
  402. char * icon = strstr(action,",");
  403. if (!icon) {
  404. fprintf(stderr, "Malformed line in action: no icon\n");
  405. goto failure;
  406. }
  407. *icon = '\0';
  408. icon++;
  409. char * title = strstr(icon,",");
  410. if (!title) {
  411. fprintf(stderr, "Malformed line in action: no title\n");
  412. goto failure;
  413. }
  414. *title = '\0';
  415. title++;
  416. menu_insert(current_menu, menu_create_normal(icon,action,title,callback));
  417. }
  418. }
  419. return _out;
  420. failure:
  421. fprintf(stderr, "malformed description file\n");
  422. if (f != stdin) {
  423. fclose(f);
  424. }
  425. free(out);
  426. return NULL;
  427. }
  428. static void _menu_redraw(yutani_window_t * menu_window, yutani_t * yctx, struct MenuList * menu) {
  429. gfx_context_t * ctx = menu->ctx;
  430. list_t * entries = menu->entries;
  431. /* Window background */
  432. draw_fill(ctx, MENU_BACKGROUND);
  433. /* Window border */
  434. draw_line(ctx, 0, ctx->width-1, 0, 0, rgb(109,111,112));
  435. draw_line(ctx, 0, 0, 0, ctx->height-1, rgb(109,111,112));
  436. draw_line(ctx, ctx->width-1, ctx->width-1, 0, ctx->height-1, rgb(109,111,112));
  437. draw_line(ctx, 0, ctx->width-1, ctx->height-1, ctx->height-1, rgb(109,111,112));
  438. /* Draw menu entries */
  439. int offset = 4;
  440. foreach(node, entries) {
  441. struct MenuEntry * entry = node->value;
  442. if (entry->renderer) {
  443. entry->renderer(ctx, entry, offset);
  444. }
  445. offset += entry->height;
  446. }
  447. /* Expose menu */
  448. flip(ctx);
  449. yutani_flip(yctx, menu_window);
  450. }
  451. void menu_show(struct MenuList * menu, yutani_t * yctx) {
  452. /* Calculate window dimensions */
  453. int height, width;
  454. _menu_calculate_dimensions(menu,&height, &width);
  455. my_yctx = yctx;
  456. menu->closed = 0;
  457. /* Create window */
  458. yutani_window_t * menu_window = yutani_window_create_flags(yctx, width, height, YUTANI_WINDOW_FLAG_ALT_ANIMATION);
  459. if (menu->ctx) {
  460. reinit_graphics_yutani(menu->ctx, menu_window);
  461. } else {
  462. menu->ctx = init_graphics_yutani_double_buffer(menu_window);
  463. }
  464. menu_window->user_data = menu;
  465. menu->window = menu_window;
  466. _menu_redraw(menu_window, yctx, menu);
  467. hashmap_set(menu_windows, (void*)menu_window->wid, menu_window);
  468. }
  469. void menu_show_at(struct MenuList * menu, yutani_window_t * parent, int x, int y) {
  470. int final_x;
  471. int final_y;
  472. menu_show(menu, parent->ctx);
  473. final_x = x + parent->x;
  474. final_y = y + parent->y;
  475. if (final_x + menu->window->width > parent->ctx->display_width) final_x -= menu->window->width;
  476. if (final_y + menu->window->height > parent->ctx->display_height) final_y -= menu->window->height;
  477. yutani_window_move(parent->ctx, menu->window, final_x, final_y);
  478. }
  479. int menu_has_eventual_child(struct MenuList * root, struct MenuList * child) {
  480. if (!child) return 0;
  481. if (root == child) return 1;
  482. struct MenuList * candidate = root->child;
  483. while (candidate && candidate != child) {
  484. if (candidate == root->child) {
  485. root->child = NULL;
  486. return 1;
  487. }
  488. candidate = root->child;
  489. }
  490. return (candidate == child);
  491. }
  492. int menu_definitely_close(struct MenuList * menu) {
  493. if (menu->child) {
  494. menu_definitely_close(menu->child);
  495. menu->child = NULL;
  496. }
  497. if (menu->closed) {
  498. return 0;
  499. }
  500. /* if focused_widget, leave focus on widget */
  501. foreach(node, menu->entries) {
  502. struct MenuEntry * entry = node->value;
  503. entry->hilight = 0;
  504. }
  505. menu->closed = 1;
  506. yutani_wid_t wid = menu->window->wid;
  507. yutani_close(menu->window->ctx, menu->window);
  508. menu->window = NULL;
  509. hashmap_remove(menu_windows, (void*)wid);
  510. return 0;
  511. }
  512. int menu_leave(struct MenuList * menu) {
  513. if (!hovered_menu) {
  514. while (menu->parent) {
  515. menu = menu->parent;
  516. }
  517. menu_definitely_close(menu);
  518. return 0;
  519. }
  520. if (!menu_has_eventual_child(menu, hovered_menu)) {
  521. /* Get all menus */
  522. list_t * menu_keys = hashmap_keys(menu_windows);
  523. foreach(_key, menu_keys) {
  524. yutani_window_t * window = hashmap_get(menu_windows, (void *)_key->value);
  525. if (window) {
  526. struct MenuList * menu = window->user_data;
  527. if (!hovered_menu || (menu != hovered_menu->child && !menu_has_eventual_child(menu, hovered_menu))) {
  528. menu_definitely_close(menu);
  529. if (menu->parent && menu->parent->child == menu) {
  530. menu->parent->child = NULL;
  531. }
  532. }
  533. }
  534. }
  535. list_free(menu_keys);
  536. free(menu_keys);
  537. }
  538. return 0;
  539. }
  540. void menu_key_action(struct MenuList * menu, struct yutani_msg_key_event * me) {
  541. if (me->event.action != KEY_ACTION_DOWN) return;
  542. yutani_window_t * window = menu->window;
  543. yutani_t * yctx = window->ctx;
  544. hovered_menu = menu;
  545. /* Find hilighted entry */
  546. struct MenuEntry * hilighted = NULL;
  547. struct MenuEntry * previous = NULL;
  548. struct MenuEntry * next = NULL;
  549. int got_it = 0;
  550. foreach(node, menu->entries) {
  551. struct MenuEntry * entry = node->value;
  552. if (entry->hilight) {
  553. hilighted = entry;
  554. got_it = 1;
  555. continue;
  556. }
  557. if (got_it) {
  558. next = entry;
  559. break;
  560. }
  561. previous = entry;
  562. }
  563. if (me->event.keycode == KEY_ARROW_DOWN) {
  564. if (hilighted) {
  565. hilighted->hilight = 0;
  566. hilighted = next;
  567. }
  568. if (!hilighted) {
  569. /* Use the first entry */
  570. hilighted = menu->entries->head->value;
  571. }
  572. hilighted->hilight = 1;
  573. _menu_redraw(window,yctx,menu);
  574. } else if (me->event.keycode == KEY_ARROW_UP) {
  575. if (hilighted) {
  576. hilighted->hilight = 0;
  577. hilighted = previous;
  578. }
  579. if (!hilighted) {
  580. /* Use the last entry */
  581. hilighted = menu->entries->tail->value;
  582. }
  583. hilighted->hilight = 1;
  584. _menu_redraw(window,yctx,menu);
  585. } else if (me->event.keycode == KEY_ARROW_RIGHT) {
  586. if (!hilighted) {
  587. hilighted = menu->entries->head->value;
  588. }
  589. if (hilighted) {
  590. hilighted->hilight = 1;
  591. if (hilighted->_type == MenuEntry_Submenu) {
  592. hilighted->activate(hilighted, 0);
  593. _menu_redraw(window,yctx,menu);
  594. } else {
  595. struct menu_bar * bar = NULL;
  596. struct MenuList * p = menu;
  597. do {
  598. if (p->_bar) {
  599. bar = p->_bar;
  600. break;
  601. }
  602. } while ((p = p->parent));
  603. if (bar) {
  604. menu_definitely_close(p);
  605. int active = (bar->active_entry_idx + 1 + bar->num_entries) % (bar->num_entries);
  606. bar->active_entry = &bar->entries[active];
  607. if (bar->redraw_callback) {
  608. bar->redraw_callback();
  609. }
  610. menu_bar_show_menu(yctx, bar->window, bar, -1, bar->active_entry);
  611. } else {
  612. _menu_redraw(window,yctx,menu);
  613. }
  614. }
  615. }
  616. } else if (me->event.key == '\n') {
  617. if (!hilighted) {
  618. hilighted = menu->entries->head->value;
  619. }
  620. if (hilighted) {
  621. hilighted->hilight = 1;
  622. hilighted->activate(hilighted, 0);
  623. }
  624. } else if (me->event.keycode == KEY_ARROW_LEFT) {
  625. if (menu->parent) {
  626. hovered_menu = menu->parent;
  627. } /* else previous from menu bar? */
  628. menu_definitely_close(menu);
  629. if (menu->_bar) {
  630. int active = (menu->_bar->active_entry_idx - 1 + menu->_bar->num_entries) % (menu->_bar->num_entries);
  631. menu->_bar->active_entry = &menu->_bar->entries[active];
  632. if (menu->_bar->redraw_callback) {
  633. menu->_bar->redraw_callback();
  634. }
  635. menu_bar_show_menu(yctx, menu->_bar->window, menu->_bar, -1, menu->_bar->active_entry);
  636. } else if (menu->parent && menu->parent->window) {
  637. yutani_focus_window(yctx, menu->parent->window->wid);
  638. }
  639. } else if (me->event.keycode == KEY_ESCAPE) {
  640. hovered_menu = NULL;
  641. menu_leave(menu);
  642. }
  643. }
  644. void menu_mouse_action(struct MenuList * menu, struct yutani_msg_window_mouse_event * me) {
  645. yutani_window_t * window = menu->window;
  646. yutani_t * yctx = window->ctx;
  647. int offset = 4;
  648. int changed = 0;
  649. foreach(node, menu->entries) {
  650. struct MenuEntry * entry = node->value;
  651. if (me->new_y >= offset && me->new_y < offset + entry->height &&
  652. me->new_x >= 0 && me->new_x < entry->width) {
  653. if (!entry->hilight) {
  654. changed = 1;
  655. entry->hilight = 1;
  656. entry->focus_change(entry, 1);
  657. }
  658. if (me->command == YUTANI_MOUSE_EVENT_CLICK || _close_enough(me)) {
  659. if (entry->activate) {
  660. entry->activate(entry, 0);
  661. }
  662. }
  663. } else {
  664. if (entry->hilight) {
  665. changed = 1;
  666. entry->hilight = 0;
  667. entry->focus_change(entry, 0);
  668. }
  669. }
  670. offset += entry->height;
  671. }
  672. if (changed) {
  673. _menu_redraw(window,yctx,menu);
  674. }
  675. }
  676. void menu_force_redraw(struct MenuList * menu) {
  677. yutani_window_t * window = menu->window;
  678. yutani_t * yctx = window->ctx;
  679. _menu_redraw(window,yctx,menu);
  680. }
  681. struct MenuList * menu_any_contains(int x, int y) {
  682. struct MenuList * out = NULL;
  683. list_t * menu_keys = hashmap_keys(menu_windows);
  684. foreach(_key, menu_keys) {
  685. yutani_window_t * window = hashmap_get(menu_windows, (void*)_key->value);
  686. if (window) {
  687. if (x >= (int)window->x && x < (int)window->x + (int)window->width && y >= (int)window->y && y < (int)window->y + (int)window->height) {
  688. out = window->user_data;
  689. break;
  690. }
  691. }
  692. }
  693. list_free(menu_keys);
  694. free(menu_keys);
  695. return out;
  696. }
  697. int menu_process_event(yutani_t * yctx, yutani_msg_t * m) {
  698. if (m) {
  699. switch (m->type) {
  700. case YUTANI_MSG_KEY_EVENT:
  701. {
  702. struct yutani_msg_key_event * me = (void*)m->data;
  703. if (hashmap_has(menu_windows, (void*)me->wid)) {
  704. yutani_window_t * window = hashmap_get(menu_windows, (void *)me->wid);
  705. struct MenuList * menu = window->user_data;
  706. menu_key_action(menu, me);
  707. }
  708. }
  709. break;
  710. case YUTANI_MSG_WINDOW_MOUSE_EVENT:
  711. {
  712. struct yutani_msg_window_mouse_event * me = (void*)m->data;
  713. if (hashmap_has(menu_windows, (void*)me->wid)) {
  714. yutani_window_t * window = hashmap_get(menu_windows, (void *)me->wid);
  715. struct MenuList * menu = window->user_data;
  716. if (me->new_x >= 0 && me->new_x < (int)window->width && me->new_y >= 0 && me->new_y < (int)window->height) {
  717. if (hovered_menu != menu) {
  718. hovered_menu = menu;
  719. }
  720. } else {
  721. if (hovered_menu) {
  722. struct MenuList * t = menu_any_contains(me->new_x + window->x, me->new_y + window->y);
  723. if (t) {
  724. hovered_menu = t;
  725. } else {
  726. hovered_menu = NULL;
  727. }
  728. }
  729. }
  730. menu_mouse_action(menu, me);
  731. }
  732. }
  733. break;
  734. case YUTANI_MSG_WINDOW_FOCUS_CHANGE:
  735. {
  736. struct yutani_msg_window_focus_change * me = (void*)m->data;
  737. if (hashmap_has(menu_windows, (void*)me->wid)) {
  738. yutani_window_t * window = hashmap_get(menu_windows, (void *)me->wid);
  739. struct MenuList * menu = window->user_data;
  740. if (!me->focused) {
  741. /* XXX leave menu */
  742. menu_leave(menu);
  743. /* if root and not window.root.menus and window.root.focused */
  744. return 1;
  745. } else {
  746. window->focused = me->focused;
  747. /* Redraw? */
  748. }
  749. }
  750. }
  751. break;
  752. default:
  753. break;
  754. }
  755. }
  756. return 0;
  757. }
  758. void menu_bar_render(struct menu_bar * self, gfx_context_t * ctx) {
  759. int _x = self->x;
  760. int _y = self->y;
  761. int width = self->width;
  762. uint32_t menu_bar_color = rgb(59,59,59);
  763. for (int y = 0; y < MENU_BAR_HEIGHT; ++y) {
  764. for (int x = 0; x < width; ++x) {
  765. GFX(ctx, x+_x,y+_y) = menu_bar_color;
  766. }
  767. }
  768. /* for each menu entry */
  769. int offset = _x;
  770. struct menu_bar_entries * _entries = self->entries;
  771. if (!self->num_entries) {
  772. while (_entries->title) {
  773. _entries++;
  774. self->num_entries++;
  775. }
  776. _entries = self->entries;
  777. }
  778. while (_entries->title) {
  779. int w = string_width(_entries->title) + 10;
  780. if ((self->active_menu && hashmap_has(menu_get_windows_hash(), (void*)self->active_menu_wid)) && _entries == self->active_entry) {
  781. for (int y = _y; y < _y + MENU_BAR_HEIGHT; ++y) {
  782. for (int x = offset + 2; x < offset + 2 + w; ++x) {
  783. GFX(ctx, x, y) = rgb(93,163,236);
  784. }
  785. }
  786. }
  787. offset += draw_string(ctx, offset + 4, _y + 2, 0xFFFFFFFF, _entries->title) + 10;
  788. _entries++;
  789. }
  790. }
  791. void menu_bar_show_menu(yutani_t * yctx, yutani_window_t * window, struct menu_bar * self, int offset, struct menu_bar_entries * _entries) {
  792. struct MenuList * new_menu = menu_set_get_menu(self->set, _entries->action);
  793. int i = 0;
  794. if (offset == -1) {
  795. /* Must calculate */
  796. offset = self->x;
  797. struct menu_bar_entries * e = self->entries;
  798. while (e->title) {
  799. if (e == _entries) break;
  800. offset += string_width(e->title) + 10;
  801. e++;
  802. i++;
  803. }
  804. } else {
  805. struct menu_bar_entries * e = self->entries;
  806. while (e->title) {
  807. if (e == _entries) break;
  808. e++;
  809. i++;
  810. }
  811. }
  812. menu_show(new_menu, yctx);
  813. yutani_window_move(yctx, new_menu->window, window->x + offset, window->y + self->y + MENU_BAR_HEIGHT);
  814. self->active_menu = new_menu;
  815. self->active_menu->_bar = self;
  816. self->active_menu_wid = new_menu->window->wid;
  817. self->active_entry = _entries;
  818. self->active_entry_idx = i;
  819. if (self->redraw_callback) {
  820. self->redraw_callback();
  821. }
  822. }
  823. 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) {
  824. if (x < self->x || x >= self->x + self->width || y < self->y || y >= self->y + 24 /* base height */) {
  825. return 0;
  826. }
  827. int offset = self->x;
  828. struct menu_bar_entries * _entries = self->entries;
  829. while (_entries->title) {
  830. int w = string_width(_entries->title) + 10;
  831. if (x >= offset && x < offset + w) {
  832. if (me->command == YUTANI_MOUSE_EVENT_CLICK || _close_enough(me)) {
  833. menu_bar_show_menu(yctx, window, self,offset,_entries);
  834. } else if (self->active_menu && hashmap_has(menu_get_windows_hash(), (void*)self->active_menu_wid) && _entries != self->active_entry) {
  835. menu_definitely_close(self->active_menu);
  836. menu_bar_show_menu(yctx, window, self,offset,_entries);
  837. }
  838. }
  839. offset += w;
  840. _entries++;
  841. }
  842. if (x >= offset && me->command == YUTANI_MOUSE_EVENT_DOWN && me->buttons & YUTANI_MOUSE_BUTTON_LEFT) {
  843. yutani_window_drag_start(yctx, window);
  844. }
  845. return 0;
  846. }