[5d466a1] | 1 | /*
|
---|
[46bd63c9] | 2 | * Copyright (c) 2023 Jiri Svoboda
|
---|
[5d466a1] | 3 | * All rights reserved.
|
---|
| 4 | *
|
---|
| 5 | * Redistribution and use in source and binary forms, with or without
|
---|
| 6 | * modification, are permitted provided that the following conditions
|
---|
| 7 | * are met:
|
---|
| 8 | *
|
---|
| 9 | * - Redistributions of source code must retain the above copyright
|
---|
| 10 | * notice, this list of conditions and the following disclaimer.
|
---|
| 11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
| 12 | * notice, this list of conditions and the following disclaimer in the
|
---|
| 13 | * documentation and/or other materials provided with the distribution.
|
---|
| 14 | * - The name of the author may not be used to endorse or promote products
|
---|
| 15 | * derived from this software without specific prior written permission.
|
---|
| 16 | *
|
---|
| 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
| 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
| 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
| 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
| 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
| 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
| 23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
| 24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
| 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
| 27 | */
|
---|
| 28 |
|
---|
| 29 | /** @addtogroup nav
|
---|
| 30 | * @{
|
---|
| 31 | */
|
---|
| 32 | /** @file Navigator.
|
---|
| 33 | *
|
---|
| 34 | * HelenOS file manager.
|
---|
| 35 | */
|
---|
| 36 |
|
---|
| 37 | #include <errno.h>
|
---|
| 38 | #include <stdlib.h>
|
---|
| 39 | #include <ui/menu.h>
|
---|
| 40 | #include <ui/menubar.h>
|
---|
[46bd63c9] | 41 | #include <ui/menudd.h>
|
---|
[5d466a1] | 42 | #include <ui/menuentry.h>
|
---|
| 43 | #include "menu.h"
|
---|
| 44 | #include "nav.h"
|
---|
| 45 |
|
---|
| 46 | /** Create navigator menu.
|
---|
| 47 | *
|
---|
[6aa85c1] | 48 | * @param window Navigator window
|
---|
[5d466a1] | 49 | * @param rmenu Place to store pointer to new menu
|
---|
| 50 | * @return EOK on success or an error code
|
---|
| 51 | */
|
---|
[6aa85c1] | 52 | errno_t nav_menu_create(ui_window_t *window, nav_menu_t **rmenu)
|
---|
[5d466a1] | 53 | {
|
---|
| 54 | nav_menu_t *menu;
|
---|
| 55 | ui_menu_t *mfile;
|
---|
[f59212cc] | 56 | ui_menu_entry_t *mopen;
|
---|
| 57 | ui_menu_entry_t *mfsep;
|
---|
[5d466a1] | 58 | ui_menu_entry_t *mexit;
|
---|
| 59 | gfx_rect_t arect;
|
---|
| 60 | gfx_rect_t rect;
|
---|
| 61 | errno_t rc;
|
---|
| 62 |
|
---|
| 63 | menu = calloc(1, sizeof(nav_menu_t));
|
---|
| 64 | if (menu == NULL)
|
---|
| 65 | return ENOMEM;
|
---|
| 66 |
|
---|
[6aa85c1] | 67 | menu->window = window;
|
---|
| 68 | menu->ui = ui_window_get_ui(window);
|
---|
| 69 |
|
---|
| 70 | rc = ui_menu_bar_create(menu->ui, menu->window,
|
---|
[5d466a1] | 71 | &menu->menubar);
|
---|
| 72 | if (rc != EOK)
|
---|
| 73 | goto error;
|
---|
| 74 |
|
---|
[46bd63c9] | 75 | rc = ui_menu_dd_create(menu->menubar, "~F~ile", NULL, &mfile);
|
---|
[5d466a1] | 76 | if (rc != EOK)
|
---|
| 77 | goto error;
|
---|
| 78 |
|
---|
[c38ab6c] | 79 | rc = ui_menu_entry_create(mfile, "~O~pen", "Enter", &mopen);
|
---|
[f59212cc] | 80 | if (rc != EOK)
|
---|
| 81 | goto error;
|
---|
| 82 |
|
---|
| 83 | ui_menu_entry_set_cb(mopen, nav_menu_file_open, (void *) menu);
|
---|
| 84 |
|
---|
| 85 | rc = ui_menu_entry_sep_create(mfile, &mfsep);
|
---|
| 86 | if (rc != EOK)
|
---|
| 87 | goto error;
|
---|
| 88 |
|
---|
[c38ab6c] | 89 | rc = ui_menu_entry_create(mfile, "E~x~it", "Ctrl-Q", &mexit);
|
---|
[5d466a1] | 90 | if (rc != EOK)
|
---|
| 91 | goto error;
|
---|
| 92 |
|
---|
[f59212cc] | 93 | ui_menu_entry_set_cb(mexit, nav_menu_file_exit, (void *) menu);
|
---|
[5d466a1] | 94 |
|
---|
[6aa85c1] | 95 | ui_window_get_app_rect(menu->window, &arect);
|
---|
[5d466a1] | 96 |
|
---|
| 97 | rect.p0 = arect.p0;
|
---|
| 98 | rect.p1.x = arect.p1.x;
|
---|
| 99 | rect.p1.y = arect.p0.y + 1;
|
---|
| 100 | ui_menu_bar_set_rect(menu->menubar, &rect);
|
---|
| 101 |
|
---|
| 102 | *rmenu = menu;
|
---|
| 103 | return EOK;
|
---|
| 104 | error:
|
---|
| 105 | nav_menu_destroy(menu);
|
---|
| 106 | return rc;
|
---|
| 107 | }
|
---|
| 108 |
|
---|
[f59212cc] | 109 | /** Set navigator menu callbacks.
|
---|
| 110 | *
|
---|
| 111 | * @param menu Menu
|
---|
| 112 | * @param cb Callbacks
|
---|
| 113 | * @param arg Argument to callback functions
|
---|
| 114 | */
|
---|
| 115 | void nav_menu_set_cb(nav_menu_t *menu, nav_menu_cb_t *cb, void *arg)
|
---|
| 116 | {
|
---|
| 117 | menu->cb = cb;
|
---|
| 118 | menu->cb_arg = arg;
|
---|
| 119 | }
|
---|
| 120 |
|
---|
[5d466a1] | 121 | /** Destroy navigator menu.
|
---|
| 122 | *
|
---|
| 123 | * @param menu Menu
|
---|
| 124 | */
|
---|
| 125 | void nav_menu_destroy(nav_menu_t *menu)
|
---|
| 126 | {
|
---|
[6aa85c1] | 127 | if (menu->menubar != NULL)
|
---|
[5d466a1] | 128 | ui_menu_bar_destroy(menu->menubar);
|
---|
| 129 |
|
---|
| 130 | free(menu);
|
---|
| 131 | }
|
---|
| 132 |
|
---|
[6aa85c1] | 133 | /** Return base UI control for the menu bar.
|
---|
| 134 | *
|
---|
| 135 | * @param menu Navigator menu
|
---|
| 136 | * @return UI control
|
---|
| 137 | */
|
---|
| 138 | ui_control_t *nav_menu_ctl(nav_menu_t *menu)
|
---|
| 139 | {
|
---|
| 140 | return ui_menu_bar_ctl(menu->menubar);
|
---|
| 141 | }
|
---|
| 142 |
|
---|
[f59212cc] | 143 | /** File / Open menu entry selected.
|
---|
| 144 | *
|
---|
| 145 | * @param mentry Menu entry
|
---|
| 146 | * @param arg Argument (navigator_t *)
|
---|
| 147 | */
|
---|
| 148 | void nav_menu_file_open(ui_menu_entry_t *mentry, void *arg)
|
---|
| 149 | {
|
---|
| 150 | nav_menu_t *menu = (nav_menu_t *)arg;
|
---|
| 151 |
|
---|
| 152 | if (menu->cb != NULL && menu->cb->file_open != NULL)
|
---|
| 153 | menu->cb->file_open(menu->cb_arg);
|
---|
| 154 | }
|
---|
| 155 |
|
---|
[5d466a1] | 156 | /** File / Exit menu entry selected.
|
---|
| 157 | *
|
---|
| 158 | * @param mentry Menu entry
|
---|
| 159 | * @param arg Argument (navigator_t *)
|
---|
| 160 | */
|
---|
[f59212cc] | 161 | void nav_menu_file_exit(ui_menu_entry_t *mentry, void *arg)
|
---|
[5d466a1] | 162 | {
|
---|
[f59212cc] | 163 | nav_menu_t *menu = (nav_menu_t *)arg;
|
---|
[5d466a1] | 164 |
|
---|
[f59212cc] | 165 | if (menu->cb != NULL && menu->cb->file_exit != NULL)
|
---|
| 166 | menu->cb->file_exit(menu->cb_arg);
|
---|
[5d466a1] | 167 | }
|
---|
| 168 |
|
---|
| 169 | /** @}
|
---|
| 170 | */
|
---|