help-browser.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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. contents_sprite = create_sprite(main_window->width - decor_width(), calculated_height, ALPHA_EMBEDDED);
  51. contents = init_graphics_sprite(contents_sprite);
  52. draw_fill(contents, rgb(255,255,255));
  53. /* Draw file entries */
  54. redraw_text();
  55. }
  56. static void redraw_window(void) {
  57. draw_fill(ctx, rgb(255,255,255));
  58. render_decorations(main_window, ctx, APPLICATION_TITLE);
  59. menu_bar.x = decor_left_width;
  60. menu_bar.y = decor_top_height;
  61. menu_bar.width = ctx->width - decor_width();
  62. menu_bar.window = main_window;
  63. menu_bar_render(&menu_bar, ctx);
  64. gfx_clear_clip(ctx);
  65. gfx_add_clip(ctx, decor_left_width, decor_top_height + MENU_BAR_HEIGHT, ctx->width - decor_width(), ctx->height - MENU_BAR_HEIGHT - decor_height());
  66. draw_sprite(ctx, contents_sprite, decor_left_width, decor_top_height + MENU_BAR_HEIGHT);
  67. gfx_clear_clip(ctx);
  68. gfx_add_clip(ctx, 0, 0, ctx->width, ctx->height);
  69. flip(ctx);
  70. yutani_flip(yctx, main_window);
  71. }
  72. static void resize_finish(int w, int h) {
  73. int height_changed = (main_window->width != (unsigned int)w);
  74. yutani_window_resize_accept(yctx, main_window, w, h);
  75. reinit_graphics_yutani(ctx, main_window);
  76. if (height_changed) {
  77. reinitialize_contents();
  78. }
  79. redraw_window();
  80. yutani_window_resize_done(yctx, main_window);
  81. yutani_flip(yctx, main_window);
  82. }
  83. static void _menu_action_navigate(struct MenuEntry * entry) {
  84. /* go to entry->action */
  85. }
  86. static void _menu_action_back(struct MenuEntry * entry) {
  87. /* go back */
  88. }
  89. static void _menu_action_forward(struct MenuEntry * entry) {
  90. /* go forward */
  91. }
  92. static void _menu_action_about(struct MenuEntry * entry) {
  93. /* Show About dialog */
  94. char about_cmd[1024] = "\0";
  95. 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\" ");
  96. char coords[100];
  97. sprintf(coords, "%d %d &", (int)main_window->x + (int)main_window->width / 2, (int)main_window->y + (int)main_window->height / 2);
  98. strcat(about_cmd, coords);
  99. system(about_cmd);
  100. redraw_window();
  101. }
  102. int main(int argc, char * argv[]) {
  103. yctx = yutani_init();
  104. init_decorations();
  105. main_window = yutani_window_create(yctx, 640, 480);
  106. yutani_window_move(yctx, main_window, yctx->display_width / 2 - main_window->width / 2, yctx->display_height / 2 - main_window->height / 2);
  107. ctx = init_graphics_yutani_double_buffer(main_window);
  108. yutani_window_advertise_icon(yctx, main_window, APPLICATION_TITLE, "help");
  109. menu_bar.entries = menu_entries;
  110. menu_bar.redraw_callback = redraw_window;
  111. menu_bar.set = menu_set_create();
  112. struct MenuList * m = menu_create(); /* File */
  113. menu_insert(m, menu_create_normal("exit",NULL,"Exit", _menu_action_exit));
  114. menu_set_insert(menu_bar.set, "file", m);
  115. m = menu_create(); /* Go */
  116. menu_insert(m, menu_create_normal("home","0_index.trt","Home",_menu_action_navigate));
  117. menu_insert(m, menu_create_normal("bookmark","special:contents","Topics",_menu_action_navigate));
  118. menu_insert(m, menu_create_separator());
  119. menu_insert(m, menu_create_normal("back",NULL,"Back",_menu_action_back));
  120. menu_insert(m, menu_create_normal("forward",NULL,"Forward",_menu_action_forward));
  121. menu_set_insert(menu_bar.set, "go", m);
  122. m = menu_create();
  123. menu_insert(m, menu_create_normal("help","help_browser.trt","Contents",_menu_action_navigate));
  124. menu_insert(m, menu_create_separator());
  125. menu_insert(m, menu_create_normal("star",NULL,"About " APPLICATION_TITLE,_menu_action_about));
  126. menu_set_insert(menu_bar.set, "help", m);
  127. reinitialize_contents();
  128. redraw_window();
  129. while (application_running) {
  130. yutani_msg_t * m = yutani_poll(yctx);
  131. while (m) {
  132. if (menu_process_event(yctx, m)) {
  133. redraw_window();
  134. }
  135. switch (m->type) {
  136. case YUTANI_MSG_KEY_EVENT:
  137. {
  138. struct yutani_msg_key_event * ke = (void*)m->data;
  139. if (ke->event.action == KEY_ACTION_DOWN && ke->event.keycode == 'q') {
  140. _menu_action_exit(NULL);
  141. }
  142. }
  143. break;
  144. case YUTANI_MSG_WINDOW_FOCUS_CHANGE:
  145. {
  146. struct yutani_msg_window_focus_change * wf = (void*)m->data;
  147. yutani_window_t * win = hashmap_get(yctx->windows, (void*)wf->wid);
  148. if (win == main_window) {
  149. win->focused = wf->focused;
  150. redraw_window();
  151. }
  152. }
  153. break;
  154. case YUTANI_MSG_RESIZE_OFFER:
  155. {
  156. struct yutani_msg_window_resize * wr = (void*)m->data;
  157. if (wr->wid == main_window->wid) {
  158. resize_finish(wr->width, wr->height);
  159. }
  160. }
  161. break;
  162. case YUTANI_MSG_WINDOW_MOUSE_EVENT:
  163. {
  164. struct yutani_msg_window_mouse_event * me = (void*)m->data;
  165. yutani_window_t * win = hashmap_get(yctx->windows, (void*)me->wid);
  166. if (win == main_window) {
  167. int result = decor_handle_event(yctx, m);
  168. switch (result) {
  169. case DECOR_CLOSE:
  170. _menu_action_exit(NULL);
  171. break;
  172. case DECOR_RIGHT:
  173. /* right click in decoration, show appropriate menu */
  174. decor_show_default_menu(main_window, main_window->x + me->new_x, main_window->y + me->new_y);
  175. break;
  176. default:
  177. /* Other actions */
  178. break;
  179. }
  180. /* Menu bar */
  181. menu_bar_mouse_event(yctx, main_window, &menu_bar, me, me->new_x, me->new_y);
  182. }
  183. }
  184. break;
  185. case YUTANI_MSG_WINDOW_CLOSE:
  186. case YUTANI_MSG_SESSION_END:
  187. _menu_action_exit(NULL);
  188. break;
  189. default:
  190. break;
  191. }
  192. free(m);
  193. m = yutani_poll_async(yctx);
  194. }
  195. }
  196. }