123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242 |
- /* vim: tabstop=4 shiftwidth=4 noexpandtab
- * This file is part of ToaruOS and is released under the terms
- * of the NCSA / University of Illinois License - see LICENSE.md
- * Copyright (C) 2018 K. Lange
- *
- * help-browser - Display documentation.
- *
- * This is a work-in-progress reimplementation of the help browser
- * from mainline ToaruOS. It is currently incomplete.
- *
- * Eventually, this should be a rich text document browser, almost
- * akin to a web browser. Right now it just says "Hello, world."
- */
- #include <stdio.h>
- #include <unistd.h>
- #include <toaru/yutani.h>
- #include <toaru/graphics.h>
- #include <toaru/decorations.h>
- #include <toaru/menu.h>
- #include <toaru/sdf.h>
- #define APPLICATION_TITLE "Help Browser"
- static yutani_t * yctx;
- static yutani_window_t * main_window;
- static gfx_context_t * ctx;
- static int application_running = 1;
- static gfx_context_t * contents = NULL;
- static sprite_t * contents_sprite = NULL;
- static struct menu_bar menu_bar = {0};
- static struct menu_bar_entries menu_entries[] = {
- {"File", "file"},
- {"Go", "go"},
- {"Help", "help"},
- {NULL, NULL},
- };
- static void _menu_action_exit(struct MenuEntry * entry) {
- application_running = 0;
- }
- static void redraw_text(void) {
- draw_sdf_string(contents, 30, 30, "Hello, world.", 16, rgb(0,0,0), SDF_FONT_THIN);
- }
- static void reinitialize_contents(void) {
- if (contents) {
- free(contents);
- }
- if (contents_sprite) {
- sprite_free(contents_sprite);
- }
- /* Calculate height for current directory */
- int calculated_height = 200;
- struct decor_bounds bounds;
- decor_get_bounds(main_window, &bounds);
- contents_sprite = create_sprite(main_window->width - bounds.width, calculated_height, ALPHA_EMBEDDED);
- contents = init_graphics_sprite(contents_sprite);
- draw_fill(contents, rgb(255,255,255));
- /* Draw file entries */
- redraw_text();
- }
- static void redraw_window(void) {
- draw_fill(ctx, rgb(255,255,255));
- render_decorations(main_window, ctx, APPLICATION_TITLE);
- struct decor_bounds bounds;
- decor_get_bounds(main_window, &bounds);
- menu_bar.x = bounds.left_width;
- menu_bar.y = bounds.top_height;
- menu_bar.width = ctx->width - bounds.width;
- menu_bar.window = main_window;
- menu_bar_render(&menu_bar, ctx);
- gfx_clear_clip(ctx);
- gfx_add_clip(ctx, bounds.left_width, bounds.top_height + MENU_BAR_HEIGHT, ctx->width - bounds.width, ctx->height - MENU_BAR_HEIGHT - bounds.height);
- draw_sprite(ctx, contents_sprite, bounds.left_width, bounds.top_height + MENU_BAR_HEIGHT);
- gfx_clear_clip(ctx);
- gfx_add_clip(ctx, 0, 0, ctx->width, ctx->height);
- flip(ctx);
- yutani_flip(yctx, main_window);
- }
- static void resize_finish(int w, int h) {
- int height_changed = (main_window->width != (unsigned int)w);
- yutani_window_resize_accept(yctx, main_window, w, h);
- reinit_graphics_yutani(ctx, main_window);
- if (height_changed) {
- reinitialize_contents();
- }
- redraw_window();
- yutani_window_resize_done(yctx, main_window);
- yutani_flip(yctx, main_window);
- }
- static void _menu_action_navigate(struct MenuEntry * entry) {
- /* go to entry->action */
- }
- static void _menu_action_back(struct MenuEntry * entry) {
- /* go back */
- }
- static void _menu_action_forward(struct MenuEntry * entry) {
- /* go forward */
- }
- static void _menu_action_about(struct MenuEntry * entry) {
- /* Show About dialog */
- char about_cmd[1024] = "\0";
- 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\" ");
- char coords[100];
- sprintf(coords, "%d %d &", (int)main_window->x + (int)main_window->width / 2, (int)main_window->y + (int)main_window->height / 2);
- strcat(about_cmd, coords);
- system(about_cmd);
- redraw_window();
- }
- int main(int argc, char * argv[]) {
- yctx = yutani_init();
- init_decorations();
- main_window = yutani_window_create(yctx, 640, 480);
- yutani_window_move(yctx, main_window, yctx->display_width / 2 - main_window->width / 2, yctx->display_height / 2 - main_window->height / 2);
- ctx = init_graphics_yutani_double_buffer(main_window);
- yutani_window_advertise_icon(yctx, main_window, APPLICATION_TITLE, "help");
- menu_bar.entries = menu_entries;
- menu_bar.redraw_callback = redraw_window;
- menu_bar.set = menu_set_create();
- struct MenuList * m = menu_create(); /* File */
- menu_insert(m, menu_create_normal("exit",NULL,"Exit", _menu_action_exit));
- menu_set_insert(menu_bar.set, "file", m);
- m = menu_create(); /* Go */
- menu_insert(m, menu_create_normal("home","0_index.trt","Home",_menu_action_navigate));
- menu_insert(m, menu_create_normal("bookmark","special:contents","Topics",_menu_action_navigate));
- menu_insert(m, menu_create_separator());
- menu_insert(m, menu_create_normal("back",NULL,"Back",_menu_action_back));
- menu_insert(m, menu_create_normal("forward",NULL,"Forward",_menu_action_forward));
- menu_set_insert(menu_bar.set, "go", m);
- m = menu_create();
- menu_insert(m, menu_create_normal("help","help_browser.trt","Contents",_menu_action_navigate));
- menu_insert(m, menu_create_separator());
- menu_insert(m, menu_create_normal("star",NULL,"About " APPLICATION_TITLE,_menu_action_about));
- menu_set_insert(menu_bar.set, "help", m);
- reinitialize_contents();
- redraw_window();
- while (application_running) {
- yutani_msg_t * m = yutani_poll(yctx);
- while (m) {
- if (menu_process_event(yctx, m)) {
- redraw_window();
- }
- switch (m->type) {
- case YUTANI_MSG_KEY_EVENT:
- {
- struct yutani_msg_key_event * ke = (void*)m->data;
- if (ke->event.action == KEY_ACTION_DOWN && ke->event.keycode == 'q') {
- _menu_action_exit(NULL);
- }
- }
- break;
- case YUTANI_MSG_WINDOW_FOCUS_CHANGE:
- {
- struct yutani_msg_window_focus_change * wf = (void*)m->data;
- yutani_window_t * win = hashmap_get(yctx->windows, (void*)wf->wid);
- if (win == main_window) {
- win->focused = wf->focused;
- redraw_window();
- }
- }
- break;
- case YUTANI_MSG_RESIZE_OFFER:
- {
- struct yutani_msg_window_resize * wr = (void*)m->data;
- if (wr->wid == main_window->wid) {
- resize_finish(wr->width, wr->height);
- }
- }
- break;
- case YUTANI_MSG_WINDOW_MOUSE_EVENT:
- {
- struct yutani_msg_window_mouse_event * me = (void*)m->data;
- yutani_window_t * win = hashmap_get(yctx->windows, (void*)me->wid);
- if (win == main_window) {
- int result = decor_handle_event(yctx, m);
- switch (result) {
- case DECOR_CLOSE:
- _menu_action_exit(NULL);
- break;
- case DECOR_RIGHT:
- /* right click in decoration, show appropriate menu */
- decor_show_default_menu(main_window, main_window->x + me->new_x, main_window->y + me->new_y);
- break;
- default:
- /* Other actions */
- break;
- }
- /* Menu bar */
- menu_bar_mouse_event(yctx, main_window, &menu_bar, me, me->new_x, me->new_y);
- }
- }
- break;
- case YUTANI_MSG_WINDOW_CLOSE:
- case YUTANI_MSG_SESSION_END:
- _menu_action_exit(NULL);
- break;
- default:
- break;
- }
- free(m);
- m = yutani_poll_async(yctx);
- }
- }
- }
|