file-browser.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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 "File 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_input_path(struct MenuEntry * entry) {
  41. }
  42. static void _menu_action_navigate(struct MenuEntry * entry) {
  43. /* go to entry->action */
  44. }
  45. static void _menu_action_up(struct MenuEntry * entry) {
  46. /* go up */
  47. }
  48. static void _menu_action_help(struct MenuEntry * entry) {
  49. /* show help documentation */
  50. if (!fork()) {
  51. system("help-browser file-browser.trt");
  52. exit(0);
  53. }
  54. redraw_window();
  55. }
  56. static void _menu_action_about(struct MenuEntry * entry) {
  57. /* Show About dialog */
  58. if (!fork()) {
  59. system("about \"About File Browser\" /usr/share/icons/48/folder.bmp \"ToaruOS File 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\"");
  60. exit(0);
  61. }
  62. redraw_window();
  63. }
  64. int main(int argc, char * argv[]) {
  65. yctx = yutani_init();
  66. init_decorations();
  67. main_window = yutani_window_create(yctx, 640, 480);
  68. yutani_window_move(yctx, main_window, yctx->display_width / 2 - main_window->width / 2, yctx->display_height / 2 - main_window->height / 2);
  69. ctx = init_graphics_yutani_double_buffer(main_window);
  70. yutani_window_advertise_icon(yctx, main_window, APPLICATION_TITLE, "folder");
  71. menu_bar.entries = menu_entries;
  72. menu_bar.redraw_callback = redraw_window;
  73. menu_bar.set = menu_set_create();
  74. struct MenuList * m = menu_create(); /* File */
  75. menu_insert(m, menu_create_normal("exit",NULL,"Exit", _menu_action_exit));
  76. menu_set_insert(menu_bar.set, "file", m);
  77. m = menu_create(); /* Go */
  78. menu_insert(m, menu_create_normal("open",NULL,"Path...", _menu_action_input_path));
  79. menu_insert(m, menu_create_separator());
  80. menu_insert(m, menu_create_normal("home",getenv("HOME"),"Home",_menu_action_navigate));
  81. menu_insert(m, menu_create_normal(NULL,"/","File System",_menu_action_navigate));
  82. menu_insert(m, menu_create_normal("up",NULL,"Up",_menu_action_up));
  83. menu_set_insert(menu_bar.set, "go", m);
  84. m = menu_create();
  85. menu_insert(m, menu_create_normal("help",NULL,"Contents",_menu_action_help));
  86. menu_insert(m, menu_create_separator());
  87. menu_insert(m, menu_create_normal("star",NULL,"About " APPLICATION_TITLE,_menu_action_about));
  88. menu_set_insert(menu_bar.set, "help", m);
  89. redraw_window();
  90. while (application_running) {
  91. yutani_msg_t * m = yutani_poll(yctx);
  92. while (m) {
  93. menu_process_event(yctx, m);
  94. switch (m->type) {
  95. case YUTANI_MSG_KEY_EVENT:
  96. {
  97. struct yutani_msg_key_event * ke = (void*)m->data;
  98. if (ke->event.action == KEY_ACTION_DOWN && ke->event.keycode == 'q') {
  99. _menu_action_exit(NULL);
  100. }
  101. }
  102. break;
  103. case YUTANI_MSG_WINDOW_FOCUS_CHANGE:
  104. {
  105. struct yutani_msg_window_focus_change * wf = (void*)m->data;
  106. yutani_window_t * win = hashmap_get(yctx->windows, (void*)wf->wid);
  107. if (win) {
  108. win->focused = wf->focused;
  109. redraw_window();
  110. }
  111. }
  112. break;
  113. case YUTANI_MSG_RESIZE_OFFER:
  114. {
  115. struct yutani_msg_window_resize * wr = (void*)m->data;
  116. if (wr->wid == main_window->wid) {
  117. resize_finish(wr->width, wr->height);
  118. }
  119. }
  120. break;
  121. case YUTANI_MSG_WINDOW_MOUSE_EVENT:
  122. {
  123. struct yutani_msg_window_mouse_event * me = (void*)m->data;
  124. yutani_window_t * win = hashmap_get(yctx->windows, (void*)me->wid);
  125. if (win == main_window) {
  126. int result = decor_handle_event(yctx, m);
  127. switch (result) {
  128. case DECOR_CLOSE:
  129. _menu_action_exit(NULL);
  130. break;
  131. case DECOR_RIGHT:
  132. /* right click in decoration, show appropriate menu */
  133. decor_show_default_menu(main_window, main_window->x + me->new_x, main_window->y + me->new_y);
  134. break;
  135. default:
  136. /* Other actions */
  137. break;
  138. }
  139. /* Menu bar */
  140. menu_bar_mouse_event(yctx, main_window, &menu_bar, me, me->new_x, me->new_y);
  141. }
  142. }
  143. break;
  144. case YUTANI_MSG_WINDOW_CLOSE:
  145. case YUTANI_MSG_SESSION_END:
  146. _menu_action_exit(NULL);
  147. break;
  148. default:
  149. break;
  150. }
  151. free(m);
  152. m = yutani_poll_async(yctx);
  153. }
  154. }
  155. }