Newer
Older
/* menu.c
Copyright (C) 1999-2003 Tom Gilbert.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to
deal in the Software without restriction, including without limitation the
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
sell copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies of the Software and its documentation and acknowledgment shall be
given in the documentation and software packages that this Software was
used.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include "feh.h"
#include "thumbnail.h"
#include "winwidget.h"
#include "filelist.h"
#include "options.h"
Window menu_cover = 0;
feh_menu *menu_root = NULL;
feh_menu *menu_main = NULL;
feh_menu *menu_single_win = NULL;
feh_menu *menu_thumbnail_viewer = NULL;
feh_menu *menu_thumbnail_win = NULL;
feh_menu *menu_bg = NULL;
static feh_menu_list *menus = NULL;
static int common_menus = 0;
Birte Kristina Friesel
committed
static feh_menu *feh_menu_func_gen_info(feh_menu * m);
static void feh_menu_func_free_info(feh_menu * m);
static void feh_menu_func_free_options(feh_menu * m);
static feh_menu *feh_menu_func_gen_options(feh_menu * m);
void feh_menu_cb(feh_menu * m, feh_menu_item * i, int action, void *data);
void feh_menu_cb_opt_fullscreen(feh_menu * m, feh_menu_item * i);
enum {
CB_CLOSE = 1, CB_EXIT, CB_RELOAD, CB_REMOVE, CB_DELETE, CB_RESET,
Birte Kristina Friesel
committed
CB_REMOVE_THUMB, CB_DELETE_THUMB, CB_BG_TILED, CB_BG_SCALED,
CB_BG_CENTERED, CB_BG_FILLED, CB_BG_TILED_NOFILE,
Birte Kristina Friesel
committed
CB_BG_SCALED_NOFILE, CB_BG_CENTERED_NOFILE, CB_BG_FILLED_NOFILE,
CB_SORT_FILENAME, CB_SORT_IMAGENAME, CB_SORT_FILESIZE, CB_SORT_RANDOMIZE,
CB_SAVE_IMAGE, CB_SAVE_FILELIST, CB_FIT, CB_OPT_DRAW_FILENAME,
CB_OPT_DRAW_ACTIONS, CB_OPT_KEEP_HTTP, CB_OPT_FREEZE_WINDOW,
CB_OPT_FULLSCREEN, CB_EDIT_ROTATE, CB_OPT_AUTO_ZOOM
Birte Kristina Friesel
committed
};
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
feh_menu *feh_menu_new(void)
{
feh_menu *m;
XSetWindowAttributes attr;
feh_menu_list *l;
static Imlib_Image bg = NULL;
static Imlib_Border border;
m = (feh_menu *) emalloc(sizeof(feh_menu));
attr.backing_store = NotUseful;
attr.override_redirect = True;
attr.colormap = cm;
attr.border_pixel = 0;
attr.background_pixmap = None;
attr.save_under = False;
attr.do_not_propagate_mask = True;
m->win = XCreateWindow(
disp, root, 1, 1, 1, 1, 0, depth, InputOutput, vis,
CWOverrideRedirect | CWSaveUnder | CWBackingStore
| CWColormap | CWBackPixmap | CWBorderPixel | CWDontPropagate, &attr);
XSelectInput(disp, m->win,
ButtonPressMask | ButtonReleaseMask | EnterWindowMask
| LeaveWindowMask | PointerMotionMask | ButtonMotionMask);
m->name = NULL;
m->fehwin = NULL;
m->pmap = 0;
m->x = 0;
m->y = 0;
m->w = 0;
m->h = 0;
m->visible = 0;
m->items = NULL;
m->next = NULL;
m->prev = NULL;
m->updates = NULL;
m->needs_redraw = 1;
m->func_free = NULL;
m->data = NULL;
m->calc = 0;
m->bg = NULL;
l = emalloc(sizeof(feh_menu_list));
l->menu = m;
l->next = menus;
menus = l;
if (!bg) {
feh_load_image_char(&bg, opt.menu_bg);
if (bg) {
border.left = border.right = border.top = border.bottom
= 4;
imlib_context_set_image(bg);
imlib_image_set_border(&border);
}
}
if (bg)
m->bg = gib_imlib_clone_image(bg);
Birte Kristina Friesel
committed
return(m);
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
}
void feh_menu_free(feh_menu * m)
{
feh_menu_item *i;
feh_menu_list *l, *pl = NULL;
if (m->name)
free(m->name);
XDestroyWindow(disp, m->win);
if (m->pmap)
XFreePixmap(disp, m->pmap);
if (m->updates)
imlib_updates_free(m->updates);
for (i = m->items; i;) {
feh_menu_item *ii;
ii = i;
i = i->next;
if (ii->func_free)
(ii->func_free) (ii->data);
if (ii->text)
free(ii->text);
if (ii->submenu)
free(ii->submenu);
free(ii);
}
for (l = menus; l; l = l->next) {
if (l->menu == m) {
if (pl)
pl->next = l->next;
else
menus = l->next;
free(l);
break;
}
pl = l;
}
if (m->bg)
gib_imlib_free_image_and_decache(m->bg);
free(m);
Birte Kristina Friesel
committed
return;
}
feh_menu_item *feh_menu_find_selected(feh_menu * m)
{
feh_menu_item *i;
for (i = m->items; i; i = i->next) {
if (MENU_ITEM_IS_SELECTED(i))
Birte Kristina Friesel
committed
return(i);
Birte Kristina Friesel
committed
return(NULL);
}
feh_menu_item *feh_menu_find_selected_r(feh_menu * m, feh_menu ** parent)
{
feh_menu_item *i, *ii;
feh_menu *mm;
for (i = m->items; i; i = i->next) {
if (MENU_ITEM_IS_SELECTED(i)) {
if (parent)
*parent = m;
Birte Kristina Friesel
committed
return(i);
} else if (i->submenu) {
mm = feh_menu_find(i->submenu);
if (mm) {
ii = feh_menu_find_selected_r(mm, parent);
if (ii)
Birte Kristina Friesel
committed
return(ii);
}
}
}
if (parent)
*parent = m;
Birte Kristina Friesel
committed
return(NULL);
}
void feh_menu_select_next(feh_menu * selected_menu, feh_menu_item * selected_item)
{
feh_menu_item *i;
if (!selected_item) {
/* jump to first item, select it */
feh_menu_select(selected_menu, selected_menu->items);
} else {
i = selected_item;
while (1) {
i = i->next;
if (!i)
i = selected_menu->items;
Birte Kristina Friesel
committed
if (i->action || i->submenu || i->func_gen_sub || i->text) {
break;
}
}
feh_menu_deselect_selected(selected_menu);
feh_menu_select(selected_menu, i);
}
}
void feh_menu_select_prev(feh_menu * selected_menu, feh_menu_item * selected_item)
{
feh_menu_item *i, *ii;
if (!selected_item) {
/* jump to last item, select it */
for (i = selected_menu->items; i->next; i = i->next);
feh_menu_select(selected_menu, i);
} else {
i = selected_item;
while (1) {
i = i->prev;
if (!i) {
i = selected_menu->items;
for (ii = selected_menu->items; ii->next; ii = ii->next);
i = ii;
}
Birte Kristina Friesel
committed
if (i->action || i->submenu || i->func_gen_sub || i->text) {
break;
}
}
feh_menu_deselect_selected(selected_menu);
feh_menu_select(selected_menu, i);
}
}
Birte Kristina Friesel
committed
void feh_menu_select_parent(feh_menu * selected_menu)
{
feh_menu *m;
feh_menu_item *i;
/* find the parent menu's item which refers to this menu's name */
if (selected_menu->prev) {
m = selected_menu->prev;
for (i = m->items; i; i = i->next) {
if (i->submenu && !strcmp(i->submenu, selected_menu->name))
break;
}
/* shouldn't ever happen */
if (i == NULL)
i = m->items;
feh_menu_deselect_selected(selected_menu);
feh_menu_select(m, i);
}
}
Birte Kristina Friesel
committed
void feh_menu_select_submenu(feh_menu * selected_menu)
{
if (selected_menu->next) {
feh_menu_deselect_selected(selected_menu);
feh_menu_select(selected_menu->next, selected_menu->next->items);
}
}
void feh_menu_item_activate(feh_menu * m, feh_menu_item * i)
{
/* watch out for this. I put it this way around so the menu
goes away *before* we perform the action, if we start
freeing menus on hiding, it will break ;-) */
Birte Kristina Friesel
committed
if ((i) && (i->action)) {
feh_menu_hide(menu_root, False);
feh_main_iteration(0);
Birte Kristina Friesel
committed
feh_menu_cb(m, i, i->action, i->data);
if (m->func_free)
Birte Kristina Friesel
committed
m->func_free(m);
}
}
feh_menu_item *feh_menu_find_at_xy(feh_menu * m, int x, int y)
{
feh_menu_item *i;
D(("looking for menu item at %d,%d\n", x, y));
for (i = m->items; i; i = i->next) {
if (XY_IN_RECT(x, y, i->x, i->y, i->w, i->h)) {
Birte Kristina Friesel
committed
return(i);
}
}
Birte Kristina Friesel
committed
return(NULL);
}
void feh_menu_deselect_selected(feh_menu * m)
{
feh_menu_item *i;
if (!m)
Birte Kristina Friesel
committed
return;
i = feh_menu_find_selected(m);
if (i) {
D(("found a selected menu, deselecting it\n"));
MENU_ITEM_SET_NORMAL(i);
m->updates = imlib_update_append_rect(m->updates, i->x, i->y, i->w, i->h);
m->needs_redraw = 1;
}
Birte Kristina Friesel
committed
return;
}
void feh_menu_select(feh_menu * m, feh_menu_item * i)
{
MENU_ITEM_SET_SELECTED(i);
m->updates = imlib_update_append_rect(m->updates, i->x, i->y, i->w, i->h);
m->needs_redraw = 1;
if (m->next) {
m->next->prev = NULL;
feh_menu_hide(m->next, TRUE);
m->next = NULL;
}
if (i->submenu) {
feh_menu *mm;
mm = feh_menu_find(i->submenu);
if (mm)
feh_menu_show_at_submenu(mm, m, i);
else if (i->func_gen_sub)
Birte Kristina Friesel
committed
feh_menu_show_at_submenu(i->func_gen_sub(m), m, i);
Birte Kristina Friesel
committed
return;
}
void feh_menu_show_at(feh_menu * m, int x, int y)
{
if (m->calc)
feh_menu_calc_size(m);
if (!menu_cover) {
XSetWindowAttributes attr;
attr.override_redirect = True;
attr.do_not_propagate_mask = True;
menu_cover = XCreateWindow(
disp, root, 0, 0, scr->width,
scr->height, 0, 0, InputOnly, vis,
CWOverrideRedirect | CWDontPropagate, &attr);
XSelectInput(disp, menu_cover,
KeyPressMask | ButtonPressMask |
ButtonReleaseMask | EnterWindowMask |
LeaveWindowMask | PointerMotionMask | ButtonMotionMask);
XRaiseWindow(disp, menu_cover);
XMapWindow(disp, menu_cover);
menu_root = m;
XUngrabPointer(disp, CurrentTime);
XSetInputFocus(disp, menu_cover, RevertToPointerRoot, CurrentTime);
}
m->visible = 1;
XMoveWindow(disp, m->win, x, y);
m->x = x;
m->y = y;
XRaiseWindow(disp, m->win);
feh_menu_redraw(m);
XMapWindow(disp, m->win);
Birte Kristina Friesel
committed
return;
}
void feh_menu_show_at_xy(feh_menu * m, winwidget winwid, int x, int y)
{
if (!m)
Birte Kristina Friesel
committed
return;
if (m->calc)
feh_menu_calc_size(m);
m->fehwin = winwid;
if ((x + m->w) > scr->width)
x = scr->width - m->w;
if ((y + m->h) > scr->height)
y = scr->height - m->h;
#if 0
/* #ifdef HAVE_LIBXINERAMA */
/* this doesn't work correctly :( -- pabs */
if (opt.xinerama && xinerama_screens) {
if ((x + m->w) > xinerama_screens[xinerama_screen].width)
x = xinerama_screens[xinerama_screen].width - m->w;
if ((y + m->h) > xinerama_screens[xinerama_screen].height)
y = xinerama_screens[xinerama_screen].height - m->h;
}
#endif /* HAVE_LIBXINERAMA */
if (x < 0)
x = 0;
if (y < 0)
y = 0;
feh_menu_move(m, x, y);
feh_menu_show(m);
Birte Kristina Friesel
committed
return;
}
void feh_menu_show_at_submenu(feh_menu * m, feh_menu * parent_m, feh_menu_item * i)
{
int mx, my;
if (!m)
Birte Kristina Friesel
committed
return;
if (m->calc)
feh_menu_calc_size(m);
mx = parent_m->x + parent_m->w;
my = parent_m->y + i->y - FEH_MENU_PAD_TOP;
m->fehwin = parent_m->fehwin;
parent_m->next = m;
m->prev = parent_m;
feh_menu_move(m, mx, my);
feh_menu_show(m);
Birte Kristina Friesel
committed
return;
}
void feh_menu_move(feh_menu * m, int x, int y)
if (!m)
Birte Kristina Friesel
committed
return;
if (m->visible)
XMoveWindow(disp, m->win, x, y);
m->x = x;
m->y = y;
Birte Kristina Friesel
committed
return;
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
}
void feh_menu_slide_all_menus_relative(int dx, int dy)
{
int i;
feh_menu_list *m;
double vector_len = 0;
int stepx = 0;
int stepy = 0;
vector_len = sqrt(dx * dx + dy * dy);
if (vector_len) {
if (dx)
stepx = rint(dx / vector_len);
if (dy)
stepy = rint(dy / vector_len);
}
for (i = 0; i < vector_len; i++) {
for (m = menus; m; m = m->next) {
if (m->menu->visible)
feh_menu_move(m->menu, m->menu->x + stepx, m->menu->y + stepy);
}
XWarpPointer(disp, None, None, 0, 0, 0, 0, stepx, stepy);
}
Birte Kristina Friesel
committed
return;
}
void feh_menu_hide(feh_menu * m, int func_free)
{
if (!m->visible)
Birte Kristina Friesel
committed
return;
if (m->next) {
m->next->prev = NULL;
feh_menu_hide(m->next, func_free);
m->next = NULL;
}
if (m == menu_root) {
if (menu_cover) {
XDestroyWindow(disp, menu_cover);
menu_cover = 0;
}
menu_root = NULL;
}
m->visible = 0;
XUnmapWindow(disp, m->win);
if (func_free && m->func_free)
Birte Kristina Friesel
committed
m->func_free(m);
else
feh_menu_deselect_selected(m);
Birte Kristina Friesel
committed
return;
}
void feh_menu_show(feh_menu * m)
{
if (!m)
Loading
Loading full blame...