help-browser.c 7.1 KB

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