help-browser.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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/menubar.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 struct menu_bar menu_bar = {0};
  14. static struct menu_bar_entries menu_entries[] = {
  15. {"File", "file"},
  16. {"Go", "go"},
  17. {"Help", "help"},
  18. {NULL, NULL},
  19. };
  20. static void _menu_action_exit(struct MenuEntry * entry) {
  21. application_running = 0;
  22. }
  23. static void redraw_window(void) {
  24. draw_fill(ctx, rgb(255,255,255));
  25. render_decorations(main_window, ctx, APPLICATION_TITLE);
  26. menu_bar.x = decor_left_width;
  27. menu_bar.y = decor_top_height;
  28. menu_bar.width = ctx->width - decor_width();
  29. menu_bar_render(&menu_bar, ctx);
  30. flip(ctx);
  31. yutani_flip(yctx, main_window);
  32. }
  33. static void resize_finish(int w, int h) {
  34. yutani_window_resize_accept(yctx, main_window, w, h);
  35. reinit_graphics_yutani(ctx, main_window);
  36. redraw_window();
  37. yutani_window_resize_done(yctx, main_window);
  38. yutani_flip(yctx, main_window);
  39. }
  40. static void _menu_action_navigate(struct MenuEntry * entry) {
  41. /* go to entry->action */
  42. }
  43. static void _menu_action_back(struct MenuEntry * entry) {
  44. /* go back */
  45. }
  46. static void _menu_action_forward(struct MenuEntry * entry) {
  47. /* go forward */
  48. }
  49. static void _menu_action_about(struct MenuEntry * entry) {
  50. /* Show About dialog */
  51. if (!fork()) {
  52. system("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://github.com/klange/toaru-nih\"");
  53. exit(0);
  54. }
  55. redraw_window();
  56. }
  57. int main(int argc, char * argv[]) {
  58. yctx = yutani_init();
  59. init_decorations();
  60. main_window = yutani_window_create(yctx, 640, 480);
  61. yutani_window_move(yctx, main_window, yctx->display_width / 2 - main_window->width / 2, yctx->display_height / 2 - main_window->height / 2);
  62. ctx = init_graphics_yutani_double_buffer(main_window);
  63. yutani_window_advertise_icon(yctx, main_window, APPLICATION_TITLE, "help");
  64. menu_bar.entries = menu_entries;
  65. menu_bar.redraw_callback = redraw_window;
  66. menu_bar.set = menu_set_create();
  67. struct MenuList * m = menu_create(); /* File */
  68. menu_insert(m, menu_create_normal("exit",NULL,"Exit", _menu_action_exit));
  69. menu_set_insert(menu_bar.set, "file", m);
  70. m = menu_create(); /* Go */
  71. menu_insert(m, menu_create_normal("home","0_index.trt","Home",_menu_action_navigate));
  72. menu_insert(m, menu_create_normal("bookmark","special:contents","Topics",_menu_action_navigate));
  73. menu_insert(m, menu_create_separator());
  74. menu_insert(m, menu_create_normal("back",NULL,"Back",_menu_action_back));
  75. menu_insert(m, menu_create_normal("forward",NULL,"Forward",_menu_action_forward));
  76. menu_set_insert(menu_bar.set, "go", m);
  77. m = menu_create();
  78. menu_insert(m, menu_create_normal("help","help_browser.trt","Contents",_menu_action_navigate));
  79. menu_insert(m, menu_create_separator());
  80. menu_insert(m, menu_create_normal("star",NULL,"About " APPLICATION_TITLE,_menu_action_about));
  81. menu_set_insert(menu_bar.set, "help", m);
  82. redraw_window();
  83. while (application_running) {
  84. yutani_msg_t * m = yutani_poll(yctx);
  85. while (m) {
  86. menu_process_event(yctx, m);
  87. switch (m->type) {
  88. case YUTANI_MSG_KEY_EVENT:
  89. {
  90. struct yutani_msg_key_event * ke = (void*)m->data;
  91. if (ke->event.action == KEY_ACTION_DOWN && ke->event.keycode == 'q') {
  92. _menu_action_exit(NULL);
  93. }
  94. }
  95. break;
  96. case YUTANI_MSG_WINDOW_FOCUS_CHANGE:
  97. {
  98. struct yutani_msg_window_focus_change * wf = (void*)m->data;
  99. yutani_window_t * win = hashmap_get(yctx->windows, (void*)wf->wid);
  100. if (win) {
  101. win->focused = wf->focused;
  102. redraw_window();
  103. }
  104. }
  105. break;
  106. case YUTANI_MSG_RESIZE_OFFER:
  107. {
  108. struct yutani_msg_window_resize * wr = (void*)m->data;
  109. if (wr->wid == main_window->wid) {
  110. resize_finish(wr->width, wr->height);
  111. }
  112. }
  113. break;
  114. case YUTANI_MSG_WINDOW_MOUSE_EVENT:
  115. {
  116. struct yutani_msg_window_mouse_event * me = (void*)m->data;
  117. yutani_window_t * win = hashmap_get(yctx->windows, (void*)me->wid);
  118. if (win == main_window) {
  119. int result = decor_handle_event(yctx, m);
  120. switch (result) {
  121. case DECOR_CLOSE:
  122. _menu_action_exit(NULL);
  123. break;
  124. case DECOR_RIGHT:
  125. /* right click in decoration, show appropriate menu */
  126. decor_show_default_menu(main_window, main_window->x + me->new_x, main_window->y + me->new_y);
  127. break;
  128. default:
  129. /* Other actions */
  130. break;
  131. }
  132. /* Menu bar */
  133. menu_bar_mouse_event(yctx, main_window, &menu_bar, me, me->new_x, me->new_y);
  134. }
  135. }
  136. break;
  137. case YUTANI_MSG_WINDOW_CLOSE:
  138. case YUTANI_MSG_SESSION_END:
  139. _menu_action_exit(NULL);
  140. break;
  141. default:
  142. break;
  143. }
  144. free(m);
  145. m = yutani_poll_async(yctx);
  146. }
  147. }
  148. }