[5d466a1] | 1 | /*
|
---|
[96c6a00] | 2 | * Copyright (c) 2022 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>
|
---|
| 41 | #include <ui/menuentry.h>
|
---|
| 42 | #include "menu.h"
|
---|
| 43 | #include "nav.h"
|
---|
| 44 |
|
---|
| 45 | /** Create navigator menu.
|
---|
| 46 | *
|
---|
[6aa85c1] | 47 | * @param window Navigator window
|
---|
[5d466a1] | 48 | * @param rmenu Place to store pointer to new menu
|
---|
| 49 | * @return EOK on success or an error code
|
---|
| 50 | */
|
---|
[6aa85c1] | 51 | errno_t nav_menu_create(ui_window_t *window, nav_menu_t **rmenu)
|
---|
[5d466a1] | 52 | {
|
---|
| 53 | nav_menu_t *menu;
|
---|
| 54 | ui_menu_t *mfile;
|
---|
[f59212cc] | 55 | ui_menu_entry_t *mopen;
|
---|
| 56 | ui_menu_entry_t *mfsep;
|
---|
[5d466a1] | 57 | ui_menu_entry_t *mexit;
|
---|
| 58 | gfx_rect_t arect;
|
---|
| 59 | gfx_rect_t rect;
|
---|
| 60 | errno_t rc;
|
---|
| 61 |
|
---|
| 62 | menu = calloc(1, sizeof(nav_menu_t));
|
---|
| 63 | if (menu == NULL)
|
---|
| 64 | return ENOMEM;
|
---|
| 65 |
|
---|
[6aa85c1] | 66 | menu->window = window;
|
---|
| 67 | menu->ui = ui_window_get_ui(window);
|
---|
| 68 |
|
---|
| 69 | rc = ui_menu_bar_create(menu->ui, menu->window,
|
---|
[5d466a1] | 70 | &menu->menubar);
|
---|
| 71 | if (rc != EOK)
|
---|
| 72 | goto error;
|
---|
| 73 |
|
---|
[96c6a00] | 74 | rc = ui_menu_create(menu->menubar, "~F~ile", &mfile);
|
---|
[5d466a1] | 75 | if (rc != EOK)
|
---|
| 76 | goto error;
|
---|
| 77 |
|
---|
[c38ab6c] | 78 | rc = ui_menu_entry_create(mfile, "~O~pen", "Enter", &mopen);
|
---|
[f59212cc] | 79 | if (rc != EOK)
|
---|
| 80 | goto error;
|
---|
| 81 |
|
---|
| 82 | ui_menu_entry_set_cb(mopen, nav_menu_file_open, (void *) menu);
|
---|
| 83 |
|
---|
| 84 | rc = ui_menu_entry_sep_create(mfile, &mfsep);
|
---|
| 85 | if (rc != EOK)
|
---|
| 86 | goto error;
|
---|
| 87 |
|
---|
[c38ab6c] | 88 | rc = ui_menu_entry_create(mfile, "E~x~it", "Ctrl-Q", &mexit);
|
---|
[5d466a1] | 89 | if (rc != EOK)
|
---|
| 90 | goto error;
|
---|
| 91 |
|
---|
[f59212cc] | 92 | ui_menu_entry_set_cb(mexit, nav_menu_file_exit, (void *) menu);
|
---|
[5d466a1] | 93 |
|
---|
[6aa85c1] | 94 | ui_window_get_app_rect(menu->window, &arect);
|
---|
[5d466a1] | 95 |
|
---|
| 96 | rect.p0 = arect.p0;
|
---|
| 97 | rect.p1.x = arect.p1.x;
|
---|
| 98 | rect.p1.y = arect.p0.y + 1;
|
---|
| 99 | ui_menu_bar_set_rect(menu->menubar, &rect);
|
---|
| 100 |
|
---|
| 101 | *rmenu = menu;
|
---|
| 102 | return EOK;
|
---|
| 103 | error:
|
---|
| 104 | nav_menu_destroy(menu);
|
---|
| 105 | return rc;
|
---|
| 106 | }
|
---|
| 107 |
|
---|
[f59212cc] | 108 | /** Set navigator menu callbacks.
|
---|
| 109 | *
|
---|
| 110 | * @param menu Menu
|
---|
| 111 | * @param cb Callbacks
|
---|
| 112 | * @param arg Argument to callback functions
|
---|
| 113 | */
|
---|
| 114 | void nav_menu_set_cb(nav_menu_t *menu, nav_menu_cb_t *cb, void *arg)
|
---|
| 115 | {
|
---|
| 116 | menu->cb = cb;
|
---|
| 117 | menu->cb_arg = arg;
|
---|
| 118 | }
|
---|
| 119 |
|
---|
[5d466a1] | 120 | /** Destroy navigator menu.
|
---|
| 121 | *
|
---|
| 122 | * @param menu Menu
|
---|
| 123 | */
|
---|
| 124 | void nav_menu_destroy(nav_menu_t *menu)
|
---|
| 125 | {
|
---|
[6aa85c1] | 126 | if (menu->menubar != NULL)
|
---|
[5d466a1] | 127 | ui_menu_bar_destroy(menu->menubar);
|
---|
| 128 |
|
---|
| 129 | free(menu);
|
---|
| 130 | }
|
---|
| 131 |
|
---|
[6aa85c1] | 132 | /** Return base UI control for the menu bar.
|
---|
| 133 | *
|
---|
| 134 | * @param menu Navigator menu
|
---|
| 135 | * @return UI control
|
---|
| 136 | */
|
---|
| 137 | ui_control_t *nav_menu_ctl(nav_menu_t *menu)
|
---|
| 138 | {
|
---|
| 139 | return ui_menu_bar_ctl(menu->menubar);
|
---|
| 140 | }
|
---|
| 141 |
|
---|
[f59212cc] | 142 | /** File / Open menu entry selected.
|
---|
| 143 | *
|
---|
| 144 | * @param mentry Menu entry
|
---|
| 145 | * @param arg Argument (navigator_t *)
|
---|
| 146 | */
|
---|
| 147 | void nav_menu_file_open(ui_menu_entry_t *mentry, void *arg)
|
---|
| 148 | {
|
---|
| 149 | nav_menu_t *menu = (nav_menu_t *)arg;
|
---|
| 150 |
|
---|
| 151 | if (menu->cb != NULL && menu->cb->file_open != NULL)
|
---|
| 152 | menu->cb->file_open(menu->cb_arg);
|
---|
| 153 | }
|
---|
| 154 |
|
---|
[5d466a1] | 155 | /** File / Exit menu entry selected.
|
---|
| 156 | *
|
---|
| 157 | * @param mentry Menu entry
|
---|
| 158 | * @param arg Argument (navigator_t *)
|
---|
| 159 | */
|
---|
[f59212cc] | 160 | void nav_menu_file_exit(ui_menu_entry_t *mentry, void *arg)
|
---|
[5d466a1] | 161 | {
|
---|
[f59212cc] | 162 | nav_menu_t *menu = (nav_menu_t *)arg;
|
---|
[5d466a1] | 163 |
|
---|
[f59212cc] | 164 | if (menu->cb != NULL && menu->cb->file_exit != NULL)
|
---|
| 165 | menu->cb->file_exit(menu->cb_arg);
|
---|
[5d466a1] | 166 | }
|
---|
| 167 |
|
---|
| 168 | /** @}
|
---|
| 169 | */
|
---|