help-browser.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. #include <stdio.h>
  2. #include <unistd.h>
  3. #include <toaru/yutani.h>
  4. #include <toaru/graphics.h>
  5. #include <toaru/decorations.h>
  6. #include <toaru/menu.h>
  7. #include <toaru/sdf.h>
  8. #define APPLICATION_TITLE "Help Browser"
  9. static yutani_t * yctx;
  10. static yutani_window_t * main_window;
  11. static gfx_context_t * ctx;
  12. static int application_running = 1;
  13. static gfx_context_t * contents = NULL;
  14. static sprite_t * contents_sprite = NULL;
  15. static struct menu_bar menu_bar = {0};
  16. static struct menu_bar_entries menu_entries[] = {
  17. {"File", "file"},
  18. {"Go", "go"},
  19. {"Help", "help"},
  20. {NULL, NULL},
  21. };
  22. static void _menu_action_exit(struct MenuEntry * entry) {
  23. application_running = 0;
  24. }
  25. static void redraw_text(void) {
  26. draw_sdf_string(contents, 30, 30, "Hello, world.", 16, rgb(0,0,0), SDF_FONT_THIN);
  27. }
  28. static void reinitialize_contents(void) {
  29. if (contents) {
  30. free(contents);
  31. }
  32. if (contents_sprite) {
  33. sprite_free(contents_sprite);
  34. }
  35. /* Calculate height for current directory */
  36. int calculated_height = 200;
  37. contents_sprite = create_sprite(main_window->width - decor_width(), calculated_height, ALPHA_EMBEDDED);
  38. contents = init_graphics_sprite(contents_sprite);
  39. draw_fill(contents, rgb(255,255,255));
  40. /* Draw file entries */
  41. redraw_text();
  42. }
  43. static void redraw_window(void) {
  44. draw_fill(ctx, rgb(255,255,255));
  45. render_decorations(main_window, ctx, APPLICATION_TITLE);
  46. menu_bar.x = decor_left_width;
  47. menu_bar.y = decor_top_height;
  48. menu_bar.width = ctx->width - decor_width();
  49. menu_bar.window = main_window;
  50. menu_bar_render(&menu_bar, ctx);
  51. gfx_clear_clip(ctx);
  52. gfx_add_clip(ctx, decor_left_width, decor_top_height + MENU_BAR_HEIGHT, ctx->width - decor_width(), ctx->height - MENU_BAR_HEIGHT - decor_height());
  53. draw_sprite(ctx, contents_sprite, decor_left_width, decor_top_height + MENU_BAR_HEIGHT);
  54. gfx_clear_clip(ctx);
  55. gfx_add_clip(ctx, 0, 0, ctx->width, ctx->height);
  56. flip(ctx);
  57. yutani_flip(yctx, main_window);
  58. }
  59. static void resize_finish(int w, int h) {
  60. int height_changed = (main_window->width != (unsigned int)w);
  61. yutani_window_resize_accept(yctx, main_window, w, h);
  62. reinit_graphics_yutani(ctx, main_window);
  63. if (height_changed) {
  64. reinitialize_contents();
  65. }
  66. redraw_window();
  67. yutani_window_resize_done(yctx, main_window);
  68. yutani_flip(yctx, main_window);
  69. }
  70. static void _menu_action_navigate(struct MenuEntry * entry) {
  71. /* go to entry->action */
  72. }
  73. static void _menu_action_back(struct MenuEntry * entry) {
  74. /* go back */
  75. }
  76. static void _menu_action_forward(struct MenuEntry * entry) {
  77. /* go forward */
  78. }
  79. static void _menu_action_about(struct MenuEntry * entry) {
  80. /* Show About dialog */
  81. char about_cmd[1024] = "\0";
  82. strcat(about_cmd, "about \"About Help Browser\" /usr/share/icons/48/help.bmp \"ToaruOS Help Browser\" \"(C) 2018 K. Lange\n-\nPart of ToaruOS, which is free software\nreleased under the NCSA/University of Illinois\nlicense.\n-\n%https://toaruos.org\n%https://gitlab.com/toarus\" ");
  83. char coords[100];
  84. sprintf(coords, "%d %d &", (int)main_window->x + (int)main_window->width / 2, (int)main_window->y + (int)main_window->height / 2);
  85. strcat(about_cmd, coords);
  86. system(about_cmd);
  87. redraw_window();
  88. }
  89. int main(int argc, char * argv[]) {
  90. yctx = yutani_init();
  91. init_decorations();
  92. main_window = yutani_window_create(yctx, 640, 480);
  93. yutani_window_move(yctx, main_window, yctx->display_width / 2 - main_window->width / 2, yctx->display_height / 2 - main_window->height / 2);
  94. ctx = init_graphics_yutani_double_buffer(main_window);
  95. yutani_window_advertise_icon(yctx, main_window, APPLICATION_TITLE, "help");
  96. menu_bar.entries = menu_entries;
  97. menu_bar.redraw_callback = redraw_window;
  98. menu_bar.set = menu_set_create();
  99. struct MenuList * m = menu_create(); /* File */
  100. menu_insert(m, menu_create_normal("exit",NULL,"Exit", _menu_action_exit));
  101. menu_set_insert(menu_bar.set, "file", m);
  102. m = menu_create(); /* Go */
  103. menu_insert(m, menu_create_normal("home","0_index.trt","Home",_menu_action_navigate));
  104. menu_insert(m, menu_create_normal("bookmark","special:contents","Topics",_menu_action_navigate));
  105. menu_insert(m, menu_create_separator());
  106. menu_insert(m, menu_create_normal("back",NULL,"Back",_menu_action_back));
  107. menu_insert(m, menu_create_normal("forward",NULL,"Forward",_menu_action_forward));
  108. menu_set_insert(menu_bar.set, "go", m);
  109. m = menu_create();
  110. menu_insert(m, menu_create_normal("help","help_browser.trt","Contents",_menu_action_navigate));
  111. menu_insert(m, menu_create_separator());
  112. menu_insert(m, menu_create_normal("star",NULL,"About " APPLICATION_TITLE,_menu_action_about));
  113. menu_set_insert(menu_bar.set, "help", m);
  114. reinitialize_contents();
  115. redraw_window();
  116. while (application_running) {
  117. yutani_msg_t * m = yutani_poll(yctx);
  118. while (m) {
  119. if (menu_process_event(yctx, m)) {
  120. redraw_window();
  121. }
  122. switch (m->type) {
  123. case YUTANI_MSG_KEY_EVENT:
  124. {
  125. struct yutani_msg_key_event * ke = (void*)m->data;
  126. if (ke->event.action == KEY_ACTION_DOWN && ke->event.keycode == 'q') {
  127. _menu_action_exit(NULL);
  128. }
  129. }
  130. break;
  131. case YUTANI_MSG_WINDOW_FOCUS_CHANGE:
  132. {
  133. struct yutani_msg_window_focus_change * wf = (void*)m->data;
  134. yutani_window_t * win = hashmap_get(yctx->windows, (void*)wf->wid);
  135. if (win == main_window) {
  136. win->focused = wf->focused;
  137. redraw_window();
  138. }
  139. }
  140. break;
  141. case YUTANI_MSG_RESIZE_OFFER:
  142. {
  143. struct yutani_msg_window_resize * wr = (void*)m->data;
  144. if (wr->wid == main_window->wid) {
  145. resize_finish(wr->width, wr->height);
  146. }
  147. }
  148. break;
  149. case YUTANI_MSG_WINDOW_MOUSE_EVENT:
  150. {
  151. struct yutani_msg_window_mouse_event * me = (void*)m->data;
  152. yutani_window_t * win = hashmap_get(yctx->windows, (void*)me->wid);
  153. if (win == main_window) {
  154. int result = decor_handle_event(yctx, m);
  155. switch (result) {
  156. case DECOR_CLOSE:
  157. _menu_action_exit(NULL);
  158. break;
  159. case DECOR_RIGHT:
  160. /* right click in decoration, show appropriate menu */
  161. decor_show_default_menu(main_window, main_window->x + me->new_x, main_window->y + me->new_y);
  162. break;
  163. default:
  164. /* Other actions */
  165. break;
  166. }
  167. /* Menu bar */
  168. menu_bar_mouse_event(yctx, main_window, &menu_bar, me, me->new_x, me->new_y);
  169. }
  170. }
  171. break;
  172. case YUTANI_MSG_WINDOW_CLOSE:
  173. case YUTANI_MSG_SESSION_END:
  174. _menu_action_exit(NULL);
  175. break;
  176. default:
  177. break;
  178. }
  179. free(m);
  180. m = yutani_poll_async(yctx);
  181. }
  182. }
  183. }