HelenOS sources
This source file includes following definitions.
- ui_menu_entry_create
- ui_menu_entry_sep_create
- ui_menu_entry_destroy
- ui_menu_entry_set_cb
- ui_menu_entry_set_disabled
- ui_menu_entry_is_disabled
- ui_menu_entry_first
- ui_menu_entry_last
- ui_menu_entry_next
- ui_menu_entry_prev
- ui_menu_entry_column_widths
- ui_menu_entry_calc_width
- ui_menu_entry_height
- ui_menu_entry_get_accel
- ui_menu_entry_paint
- ui_menu_entry_selectable
- ui_menu_entry_press
- ui_menu_entry_release
- ui_menu_entry_activate
- ui_menu_entry_cb
- ui_menu_entry_enter
- ui_menu_entry_leave
- ui_menu_entry_pos_event
- ui_menu_entry_get_geom
#include <adt/list.h>
#include <errno.h>
#include <gfx/color.h>
#include <gfx/context.h>
#include <gfx/render.h>
#include <gfx/text.h>
#include <io/pos_event.h>
#include <stdlib.h>
#include <str.h>
#include <ui/accel.h>
#include <ui/control.h>
#include <ui/paint.h>
#include <ui/menubar.h>
#include <ui/menuentry.h>
#include <ui/window.h>
#include "../private/menubar.h"
#include "../private/menuentry.h"
#include "../private/menu.h"
#include "../private/resource.h"
enum {
menu_entry_hpad = 4,
menu_entry_vpad = 4,
menu_entry_column_pad = 8,
menu_entry_sep_height = 2,
menu_entry_hpad_text = 1,
menu_entry_vpad_text = 0,
menu_entry_column_pad_text = 2,
menu_entry_sep_height_text = 1
};
errno_t ui_menu_entry_create(ui_menu_t *menu, const char *caption,
const char *shortcut, ui_menu_entry_t **rmentry)
{
ui_menu_entry_t *mentry;
gfx_coord_t caption_w;
gfx_coord_t shortcut_w;
mentry = calloc(1, sizeof(ui_menu_entry_t));
if (mentry == NULL)
return ENOMEM;
mentry->caption = str_dup(caption);
if (mentry->caption == NULL) {
free(mentry);
return ENOMEM;
}
mentry->shortcut = str_dup(shortcut);
if (mentry->caption == NULL) {
free(mentry->caption);
free(mentry);
return ENOMEM;
}
mentry->menu = menu;
list_append(&mentry->lentries, &menu->entries);
ui_menu_entry_column_widths(mentry, &caption_w, &shortcut_w);
if (caption_w > menu->max_caption_w)
menu->max_caption_w = caption_w;
if (shortcut_w > menu->max_shortcut_w)
menu->max_shortcut_w = shortcut_w;
menu->total_h += ui_menu_entry_height(mentry);
*rmentry = mentry;
return EOK;
}
errno_t ui_menu_entry_sep_create(ui_menu_t *menu, ui_menu_entry_t **rmentry)
{
ui_menu_entry_t *mentry;
errno_t rc;
rc = ui_menu_entry_create(menu, "", "", &mentry);
if (rc != EOK)
return rc;
menu->total_h -= ui_menu_entry_height(mentry);
mentry->separator = true;
menu->total_h += ui_menu_entry_height(mentry);
*rmentry = mentry;
return EOK;
}
void ui_menu_entry_destroy(ui_menu_entry_t *mentry)
{
if (mentry == NULL)
return;
mentry->menu->total_h -= ui_menu_entry_height(mentry);
list_remove(&mentry->lentries);
if (list_empty(&mentry->menu->entries)) {
mentry->menu->total_h = 0;
mentry->menu->max_caption_w = 0;
mentry->menu->max_shortcut_w = 0;
}
free(mentry->caption);
free(mentry);
}
void ui_menu_entry_set_cb(ui_menu_entry_t *mentry, ui_menu_entry_cb_t cb,
void *arg)
{
mentry->cb = cb;
mentry->arg = arg;
}
void ui_menu_entry_set_disabled(ui_menu_entry_t *mentry, bool disabled)
{
mentry->disabled = disabled;
}
bool ui_menu_entry_is_disabled(ui_menu_entry_t *mentry)
{
return mentry->disabled;
}
ui_menu_entry_t *ui_menu_entry_first(ui_menu_t *menu)
{
link_t *link;
link = list_first(&menu->entries);
if (link == NULL)
return NULL;
return list_get_instance(link, ui_menu_entry_t, lentries);
}
ui_menu_entry_t *ui_menu_entry_last(ui_menu_t *menu)
{
link_t *link;
link = list_last(&menu->entries);
if (link == NULL)
return NULL;
return list_get_instance(link, ui_menu_entry_t, lentries);
}
ui_menu_entry_t *ui_menu_entry_next(ui_menu_entry_t *cur)
{
link_t *link;
link = list_next(&cur->lentries, &cur->menu->entries);
if (link == NULL)
return NULL;
return list_get_instance(link, ui_menu_entry_t, lentries);
}
ui_menu_entry_t *ui_menu_entry_prev(ui_menu_entry_t *cur)
{
link_t *link;
link = list_prev(&cur->lentries, &cur->menu->entries);
if (link == NULL)
return NULL;
return list_get_instance(link, ui_menu_entry_t, lentries);
}
void ui_menu_entry_column_widths(ui_menu_entry_t *mentry,
gfx_coord_t *caption_w, gfx_coord_t *shortcut_w)
{
ui_resource_t *res;
res = ui_window_get_res(mentry->menu->parent);
*caption_w = ui_text_width(res->font, mentry->caption);
*shortcut_w = ui_text_width(res->font, mentry->shortcut);
}
gfx_coord_t ui_menu_entry_calc_width(ui_menu_t *menu, gfx_coord_t caption_w,
gfx_coord_t shortcut_w)
{
ui_resource_t *res;
gfx_coord_t hpad;
gfx_coord_t width;
res = ui_window_get_res(menu->parent);
if (res->textmode)
hpad = menu_entry_hpad_text;
else
hpad = menu_entry_hpad;
width = caption_w + 2 * hpad;
if (shortcut_w != 0) {
if (res->textmode)
width += menu_entry_column_pad_text;
else
width += menu_entry_column_pad;
width += shortcut_w;
}
return width;
}
gfx_coord_t ui_menu_entry_height(ui_menu_entry_t *mentry)
{
ui_resource_t *res;
gfx_font_metrics_t metrics;
gfx_coord_t height;
gfx_coord_t vpad;
res = ui_window_get_res(mentry->menu->parent);
if (res->textmode) {
vpad = menu_entry_vpad_text;
} else {
vpad = menu_entry_vpad;
}
if (mentry->separator) {
if (res->textmode)
height = menu_entry_sep_height_text;
else
height = menu_entry_sep_height;
} else {
gfx_font_get_metrics(res->font, &metrics);
height = metrics.ascent + metrics.descent + 1;
}
return height + 2 * vpad;
}
char32_t ui_menu_entry_get_accel(ui_menu_entry_t *mentry)
{
return ui_accel_get(mentry->caption);
}
errno_t ui_menu_entry_paint(ui_menu_entry_t *mentry, gfx_coord2_t *pos)
{
ui_resource_t *res;
ui_text_fmt_t fmt;
gfx_color_t *bg_color;
ui_menu_entry_geom_t geom;
gfx_rect_t rect;
errno_t rc;
res = ui_menu_get_res(mentry->menu);
ui_menu_entry_get_geom(mentry, pos, &geom);
ui_text_fmt_init(&fmt);
fmt.font = res->font;
fmt.halign = gfx_halign_left;
fmt.valign = gfx_valign_top;
if ((mentry->held && mentry->inside) ||
mentry == mentry->menu->selected) {
fmt.color = res->wnd_sel_text_color;
fmt.hgl_color = res->wnd_sel_text_hgl_color;
bg_color = res->wnd_sel_text_bg_color;
} else if (mentry->disabled) {
fmt.color = res->wnd_dis_text_color;
fmt.hgl_color = res->wnd_dis_text_color;
bg_color = res->wnd_face_color;
} else {
fmt.color = res->wnd_text_color;
fmt.hgl_color = res->wnd_text_hgl_color;
bg_color = res->wnd_face_color;
}
rc = gfx_set_color(res->gc, bg_color);
if (rc != EOK)
goto error;
rc = gfx_fill_rect(res->gc, &geom.outer_rect);
if (rc != EOK)
goto error;
rc = ui_paint_text(&geom.caption_pos, &fmt, mentry->caption);
if (rc != EOK)
goto error;
fmt.halign = gfx_halign_right;
rc = ui_paint_text(&geom.shortcut_pos, &fmt, mentry->shortcut);
if (rc != EOK)
goto error;
if (mentry->separator) {
if (res->textmode) {
rect = geom.outer_rect;
rect.p0.x -= 1;
rect.p1.x += 1;
rc = ui_paint_text_hbrace(res, &rect, ui_box_single,
res->wnd_face_color);
if (rc != EOK)
goto error;
} else {
rect.p0 = geom.caption_pos;
rect.p1.x = geom.shortcut_pos.x;
rect.p1.y = rect.p0.y + 2;
rc = ui_paint_bevel(res->gc, &rect, res->wnd_shadow_color,
res->wnd_highlight_color, 1, NULL);
if (rc != EOK)
goto error;
}
}
rc = gfx_update(res->gc);
if (rc != EOK)
goto error;
return EOK;
error:
return rc;
}
bool ui_menu_entry_selectable(ui_menu_entry_t *mentry)
{
return !mentry->separator;
}
void ui_menu_entry_press(ui_menu_entry_t *mentry, gfx_coord2_t *pos)
{
if (mentry->held)
return;
if (mentry->separator || mentry->disabled)
return;
mentry->inside = true;
mentry->held = true;
ui_menu_entry_paint(mentry, pos);
}
void ui_menu_entry_release(ui_menu_entry_t *mentry)
{
if (!mentry->held)
return;
mentry->held = false;
if (mentry->inside)
ui_menu_entry_activate(mentry);
}
void ui_menu_entry_activate(ui_menu_entry_t *mentry)
{
ui_menu_close_req(mentry->menu);
ui_menu_entry_cb(mentry);
}
void ui_menu_entry_cb(ui_menu_entry_t *mentry)
{
if (mentry->cb != NULL)
mentry->cb(mentry, mentry->arg);
}
void ui_menu_entry_enter(ui_menu_entry_t *mentry, gfx_coord2_t *pos)
{
if (mentry->inside)
return;
mentry->inside = true;
if (mentry->held)
(void) ui_menu_entry_paint(mentry, pos);
}
void ui_menu_entry_leave(ui_menu_entry_t *mentry, gfx_coord2_t *pos)
{
if (!mentry->inside)
return;
mentry->inside = false;
if (mentry->held)
(void) ui_menu_entry_paint(mentry, pos);
}
ui_evclaim_t ui_menu_entry_pos_event(ui_menu_entry_t *mentry,
gfx_coord2_t *pos, pos_event_t *event)
{
ui_menu_entry_geom_t geom;
gfx_coord2_t ppos;
bool inside;
ppos.x = event->hpos;
ppos.y = event->vpos;
ui_menu_entry_get_geom(mentry, pos, &geom);
inside = gfx_pix_inside_rect(&ppos, &geom.outer_rect);
switch (event->type) {
case POS_PRESS:
if (inside) {
ui_menu_entry_press(mentry, pos);
return ui_claimed;
}
break;
case POS_RELEASE:
if (mentry->held) {
ui_menu_entry_release(mentry);
return ui_claimed;
}
break;
case POS_UPDATE:
if (inside && !mentry->inside) {
ui_menu_entry_enter(mentry, pos);
return ui_claimed;
} else if (!inside && mentry->inside) {
ui_menu_entry_leave(mentry, pos);
}
break;
case POS_DCLICK:
break;
}
return ui_unclaimed;
}
void ui_menu_entry_get_geom(ui_menu_entry_t *mentry, gfx_coord2_t *pos,
ui_menu_entry_geom_t *geom)
{
ui_resource_t *res;
gfx_coord_t hpad;
gfx_coord_t vpad;
gfx_coord_t width;
res = ui_menu_get_res(mentry->menu);
if (res->textmode) {
hpad = menu_entry_hpad_text;
vpad = menu_entry_vpad_text;
} else {
hpad = menu_entry_hpad;
vpad = menu_entry_vpad;
}
width = ui_menu_entry_calc_width(mentry->menu,
mentry->menu->max_caption_w, mentry->menu->max_shortcut_w);
geom->outer_rect.p0 = *pos;
geom->outer_rect.p1.x = geom->outer_rect.p0.x + width;
geom->outer_rect.p1.y = geom->outer_rect.p0.y +
ui_menu_entry_height(mentry);
geom->caption_pos.x = pos->x + hpad;
geom->caption_pos.y = pos->y + vpad;
geom->shortcut_pos.x = geom->outer_rect.p1.x - hpad;
geom->shortcut_pos.y = pos->y + vpad;
}
HelenOS homepage, sources at GitHub