menu.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955
  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. int menu_has_eventual_child(struct MenuList * root, struct MenuList * child) {
  470. if (!child) return 0;
  471. if (root == child) return 1;
  472. struct MenuList * candidate = root->child;
  473. while (candidate && candidate != child) {
  474. if (candidate == root->child) {
  475. root->child = NULL;
  476. return 1;
  477. }
  478. candidate = root->child;
  479. }
  480. return (candidate == child);
  481. }
  482. int menu_definitely_close(struct MenuList * menu) {
  483. if (menu->child) {
  484. menu_definitely_close(menu->child);
  485. menu->child = NULL;
  486. }
  487. if (menu->closed) {
  488. return 0;
  489. }
  490. /* if focused_widget, leave focus on widget */
  491. foreach(node, menu->entries) {
  492. struct MenuEntry * entry = node->value;
  493. entry->hilight = 0;
  494. }
  495. menu->closed = 1;
  496. yutani_wid_t wid = menu->window->wid;
  497. yutani_close(menu->window->ctx, menu->window);
  498. menu->window = NULL;
  499. hashmap_remove(menu_windows, (void*)wid);
  500. return 0;
  501. }
  502. int menu_leave(struct MenuList * menu) {
  503. if (!hovered_menu) {
  504. while (menu->parent) {
  505. menu = menu->parent;
  506. }
  507. menu_definitely_close(menu);
  508. return 0;
  509. }
  510. if (!menu_has_eventual_child(menu, hovered_menu)) {
  511. /* Get all menus */
  512. list_t * menu_keys = hashmap_keys(menu_windows);
  513. foreach(_key, menu_keys) {
  514. yutani_window_t * window = hashmap_get(menu_windows, (void *)_key->value);
  515. if (window) {
  516. struct MenuList * menu = window->user_data;
  517. if (!hovered_menu || (menu != hovered_menu->child && !menu_has_eventual_child(menu, hovered_menu))) {
  518. menu_definitely_close(menu);
  519. if (menu->parent && menu->parent->child == menu) {
  520. menu->parent->child = NULL;
  521. }
  522. }
  523. }
  524. }
  525. list_free(menu_keys);
  526. free(menu_keys);
  527. }
  528. return 0;
  529. }
  530. void menu_key_action(struct MenuList * menu, struct yutani_msg_key_event * me) {
  531. if (me->event.action != KEY_ACTION_DOWN) return;
  532. yutani_window_t * window = menu->window;
  533. yutani_t * yctx = window->ctx;
  534. hovered_menu = menu;
  535. /* Find hilighted entry */
  536. struct MenuEntry * hilighted = NULL;
  537. struct MenuEntry * previous = NULL;
  538. struct MenuEntry * next = NULL;
  539. int got_it = 0;
  540. foreach(node, menu->entries) {
  541. struct MenuEntry * entry = node->value;
  542. if (entry->hilight) {
  543. hilighted = entry;
  544. got_it = 1;
  545. continue;
  546. }
  547. if (got_it) {
  548. next = entry;
  549. break;
  550. }
  551. previous = entry;
  552. }
  553. if (me->event.keycode == KEY_ARROW_DOWN) {
  554. if (hilighted) {
  555. hilighted->hilight = 0;
  556. hilighted = next;
  557. }
  558. if (!hilighted) {
  559. /* Use the first entry */
  560. hilighted = menu->entries->head->value;
  561. }
  562. hilighted->hilight = 1;
  563. _menu_redraw(window,yctx,menu);
  564. } else if (me->event.keycode == KEY_ARROW_UP) {
  565. if (hilighted) {
  566. hilighted->hilight = 0;
  567. hilighted = previous;
  568. }
  569. if (!hilighted) {
  570. /* Use the last entry */
  571. hilighted = menu->entries->tail->value;
  572. }
  573. hilighted->hilight = 1;
  574. _menu_redraw(window,yctx,menu);
  575. } else if (me->event.keycode == KEY_ARROW_RIGHT) {
  576. if (!hilighted) {
  577. hilighted = menu->entries->head->value;
  578. }
  579. if (hilighted) {
  580. hilighted->hilight = 1;
  581. if (hilighted->_type == MenuEntry_Submenu) {
  582. hilighted->activate(hilighted, 0);
  583. _menu_redraw(window,yctx,menu);
  584. } else {
  585. struct menu_bar * bar = NULL;
  586. struct MenuList * p = menu;
  587. do {
  588. if (p->_bar) {
  589. bar = p->_bar;
  590. break;
  591. }
  592. } while ((p = p->parent));
  593. if (bar) {
  594. menu_definitely_close(p);
  595. int active = (bar->active_entry_idx + 1 + bar->num_entries) % (bar->num_entries);
  596. bar->active_entry = &bar->entries[active];
  597. if (bar->redraw_callback) {
  598. bar->redraw_callback();
  599. }
  600. menu_bar_show_menu(yctx, bar->window, bar, -1, bar->active_entry);
  601. } else {
  602. _menu_redraw(window,yctx,menu);
  603. }
  604. }
  605. }
  606. } else if (me->event.key == '\n') {
  607. if (!hilighted) {
  608. hilighted = menu->entries->head->value;
  609. }
  610. if (hilighted) {
  611. hilighted->hilight = 1;
  612. hilighted->activate(hilighted, 0);
  613. }
  614. } else if (me->event.keycode == KEY_ARROW_LEFT) {
  615. if (menu->parent) {
  616. hovered_menu = menu->parent;
  617. } /* else previous from menu bar? */
  618. menu_definitely_close(menu);
  619. if (menu->_bar) {
  620. int active = (menu->_bar->active_entry_idx - 1 + menu->_bar->num_entries) % (menu->_bar->num_entries);
  621. menu->_bar->active_entry = &menu->_bar->entries[active];
  622. if (menu->_bar->redraw_callback) {
  623. menu->_bar->redraw_callback();
  624. }
  625. menu_bar_show_menu(yctx, menu->_bar->window, menu->_bar, -1, menu->_bar->active_entry);
  626. } else if (menu->parent && menu->parent->window) {
  627. yutani_focus_window(yctx, menu->parent->window->wid);
  628. }
  629. } else if (me->event.keycode == KEY_ESCAPE) {
  630. hovered_menu = NULL;
  631. menu_leave(menu);
  632. }
  633. }
  634. void menu_mouse_action(struct MenuList * menu, struct yutani_msg_window_mouse_event * me) {
  635. yutani_window_t * window = menu->window;
  636. yutani_t * yctx = window->ctx;
  637. int offset = 4;
  638. int changed = 0;
  639. foreach(node, menu->entries) {
  640. struct MenuEntry * entry = node->value;
  641. if (me->new_y >= offset && me->new_y < offset + entry->height &&
  642. me->new_x >= 0 && me->new_x < entry->width) {
  643. if (!entry->hilight) {
  644. changed = 1;
  645. entry->hilight = 1;
  646. entry->focus_change(entry, 1);
  647. }
  648. if (me->command == YUTANI_MOUSE_EVENT_CLICK || _close_enough(me)) {
  649. if (entry->activate) {
  650. entry->activate(entry, 0);
  651. }
  652. }
  653. } else {
  654. if (entry->hilight) {
  655. changed = 1;
  656. entry->hilight = 0;
  657. entry->focus_change(entry, 0);
  658. }
  659. }
  660. offset += entry->height;
  661. }
  662. if (changed) {
  663. _menu_redraw(window,yctx,menu);
  664. }
  665. }
  666. struct MenuList * menu_any_contains(int x, int y) {
  667. struct MenuList * out = NULL;
  668. list_t * menu_keys = hashmap_keys(menu_windows);
  669. foreach(_key, menu_keys) {
  670. yutani_window_t * window = hashmap_get(menu_windows, (void*)_key->value);
  671. if (window) {
  672. if (x >= (int)window->x && x < (int)window->x + (int)window->width && y >= (int)window->y && y < (int)window->y + (int)window->height) {
  673. out = window->user_data;
  674. break;
  675. }
  676. }
  677. }
  678. list_free(menu_keys);
  679. free(menu_keys);
  680. return out;
  681. }
  682. int menu_process_event(yutani_t * yctx, yutani_msg_t * m) {
  683. if (m) {
  684. switch (m->type) {
  685. case YUTANI_MSG_KEY_EVENT:
  686. {
  687. struct yutani_msg_key_event * me = (void*)m->data;
  688. if (hashmap_has(menu_windows, (void*)me->wid)) {
  689. yutani_window_t * window = hashmap_get(menu_windows, (void *)me->wid);
  690. struct MenuList * menu = window->user_data;
  691. menu_key_action(menu, me);
  692. }
  693. }
  694. break;
  695. case YUTANI_MSG_WINDOW_MOUSE_EVENT:
  696. {
  697. struct yutani_msg_window_mouse_event * me = (void*)m->data;
  698. if (hashmap_has(menu_windows, (void*)me->wid)) {
  699. yutani_window_t * window = hashmap_get(menu_windows, (void *)me->wid);
  700. struct MenuList * menu = window->user_data;
  701. if (me->new_x >= 0 && me->new_x < (int)window->width && me->new_y >= 0 && me->new_y < (int)window->height) {
  702. if (hovered_menu != menu) {
  703. hovered_menu = menu;
  704. }
  705. } else {
  706. if (hovered_menu) {
  707. struct MenuList * t = menu_any_contains(me->new_x + window->x, me->new_y + window->y);
  708. if (t) {
  709. hovered_menu = t;
  710. } else {
  711. hovered_menu = NULL;
  712. }
  713. }
  714. }
  715. menu_mouse_action(menu, me);
  716. }
  717. }
  718. break;
  719. case YUTANI_MSG_WINDOW_FOCUS_CHANGE:
  720. {
  721. struct yutani_msg_window_focus_change * me = (void*)m->data;
  722. if (hashmap_has(menu_windows, (void*)me->wid)) {
  723. yutani_window_t * window = hashmap_get(menu_windows, (void *)me->wid);
  724. struct MenuList * menu = window->user_data;
  725. if (!me->focused) {
  726. /* XXX leave menu */
  727. menu_leave(menu);
  728. /* if root and not window.root.menus and window.root.focused */
  729. return 1;
  730. } else {
  731. window->focused = me->focused;
  732. /* Redraw? */
  733. }
  734. }
  735. }
  736. break;
  737. default:
  738. break;
  739. }
  740. }
  741. return 0;
  742. }
  743. void menu_bar_render(struct menu_bar * self, gfx_context_t * ctx) {
  744. int _x = self->x;
  745. int _y = self->y;
  746. int width = self->width;
  747. uint32_t menu_bar_color = rgb(59,59,59);
  748. for (int y = 0; y < MENU_BAR_HEIGHT; ++y) {
  749. for (int x = 0; x < width; ++x) {
  750. GFX(ctx, x+_x,y+_y) = menu_bar_color;
  751. }
  752. }
  753. /* for each menu entry */
  754. int offset = _x;
  755. struct menu_bar_entries * _entries = self->entries;
  756. if (!self->num_entries) {
  757. while (_entries->title) {
  758. _entries++;
  759. self->num_entries++;
  760. }
  761. _entries = self->entries;
  762. }
  763. while (_entries->title) {
  764. int w = string_width(_entries->title) + 10;
  765. if ((self->active_menu && hashmap_has(menu_get_windows_hash(), (void*)self->active_menu_wid)) && _entries == self->active_entry) {
  766. for (int y = _y; y < _y + MENU_BAR_HEIGHT; ++y) {
  767. for (int x = offset + 2; x < offset + 2 + w; ++x) {
  768. GFX(ctx, x, y) = rgb(93,163,236);
  769. }
  770. }
  771. }
  772. offset += draw_string(ctx, offset + 4, _y + 2, 0xFFFFFFFF, _entries->title) + 10;
  773. _entries++;
  774. }
  775. }
  776. void menu_bar_show_menu(yutani_t * yctx, yutani_window_t * window, struct menu_bar * self, int offset, struct menu_bar_entries * _entries) {
  777. struct MenuList * new_menu = menu_set_get_menu(self->set, _entries->action);
  778. int i = 0;
  779. if (offset == -1) {
  780. /* Must calculate */
  781. offset = self->x;
  782. struct menu_bar_entries * e = self->entries;
  783. while (e->title) {
  784. if (e == _entries) break;
  785. offset += string_width(e->title) + 10;
  786. e++;
  787. i++;
  788. }
  789. } else {
  790. struct menu_bar_entries * e = self->entries;
  791. while (e->title) {
  792. if (e == _entries) break;
  793. e++;
  794. i++;
  795. }
  796. }
  797. menu_show(new_menu, yctx);
  798. yutani_window_move(yctx, new_menu->window, window->x + offset, window->y + self->y + MENU_BAR_HEIGHT);
  799. self->active_menu = new_menu;
  800. self->active_menu->_bar = self;
  801. self->active_menu_wid = new_menu->window->wid;
  802. self->active_entry = _entries;
  803. self->active_entry_idx = i;
  804. if (self->redraw_callback) {
  805. self->redraw_callback();
  806. }
  807. }
  808. 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) {
  809. if (x < self->x || x >= self->x + self->width || y < self->y || y >= self->y + 24 /* base height */) {
  810. return 0;
  811. }
  812. int offset = self->x;
  813. struct menu_bar_entries * _entries = self->entries;
  814. while (_entries->title) {
  815. int w = string_width(_entries->title) + 10;
  816. if (x >= offset && x < offset + w) {
  817. if (me->command == YUTANI_MOUSE_EVENT_CLICK || _close_enough(me)) {
  818. menu_bar_show_menu(yctx, window, self,offset,_entries);
  819. } else if (self->active_menu && hashmap_has(menu_get_windows_hash(), (void*)self->active_menu_wid) && _entries != self->active_entry) {
  820. menu_definitely_close(self->active_menu);
  821. menu_bar_show_menu(yctx, window, self,offset,_entries);
  822. }
  823. }
  824. offset += w;
  825. _entries++;
  826. }
  827. if (x >= offset && me->command == YUTANI_MOUSE_EVENT_DOWN && me->buttons & YUTANI_MOUSE_BUTTON_LEFT) {
  828. yutani_window_drag_start(yctx, window);
  829. }
  830. return 0;
  831. }