You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1820 lines
46 KiB

#include "ephoto.h"
#define ZOOM_STEP 0.2
static Ecore_Timer *_1s_hold = NULL;
typedef struct _Ephoto_Single_Browser Ephoto_Single_Browser;
typedef struct _Ephoto_Viewer Ephoto_Viewer;
struct _Ephoto_Single_Browser
{
Ephoto *ephoto;
Evas_Object *main;
Evas_Object *mhbox;
Evas_Object *table;
Evas_Object *viewer;
Evas_Object *infolabel;
Evas_Object *nolabel;
Evas_Object *botbox;
Evas_Object *event;
Elm_Object_Item *save;
const char *pending_path;
Ephoto_Entry *entry;
Ephoto_Orient orient;
Eina_List *handlers;
Eina_List *entries;
Eina_Bool editing:1;
Eina_Bool cropping:1;
unsigned int *edited_image_data;
int ew;
int eh;
};
struct _Ephoto_Viewer
{
Eina_List *handlers;
Ecore_File_Monitor *monitor;
Evas_Object *scroller;
Evas_Object *table;
Evas_Object *image;
double zoom;
Eina_Bool fit:1;
Eina_Bool zoom_first:1;
};
static void _zoom_set(Ephoto_Single_Browser *sb, double zoom);
static void _zoom_in(Ephoto_Single_Browser *sb);
static void _zoom_out(Ephoto_Single_Browser *sb);
static void _key_down(void *data, Evas *e EINA_UNUSED,
Evas_Object *obj EINA_UNUSED, void *event_info);
static void _edit_menu(Ephoto_Single_Browser *sb);
static void _back(void *data, Evas_Object *obj EINA_UNUSED,
void *event_info EINA_UNUSED);
static char *_ephoto_get_file_size(const char *path);
static void _update_bottom_bar(Ephoto_Single_Browser *sb);
static void
_viewer_del(void *data, Evas *e EINA_UNUSED, Evas_Object *obj EINA_UNUSED,
void *event_info EINA_UNUSED)
{
Ephoto_Viewer *v = data;
if (v->monitor)
ecore_file_monitor_del(v->monitor);
free(v);
}
static Evas_Object *
_image_create_icon(void *data, Evas_Object *parent, Evas_Coord *xoff,
Evas_Coord *yoff)
{
Evas_Object *ic;
Evas_Object *io = data;
const char *f, *g;
Evas_Coord x, y, w, h, xm, ym;
elm_image_file_get(io, &f, &g);
ic = elm_image_add(parent);
elm_image_file_set(ic, f, g);
evas_object_geometry_get(io, &x, &y, &w, &h);
evas_object_move(ic, x, y);
evas_object_resize(ic, 60, 60);
evas_object_show(ic);
evas_pointer_canvas_xy_get(evas_object_evas_get(io), &xm, &ym);
if (xoff)
*xoff = xm - 30;
if (yoff)
*yoff = ym - 30;
return ic;
}
static Eina_Bool
_1s_hold_time(void *data)
{
const char *f;
char dd[PATH_MAX];
Evas_Object *io = data;
elm_image_file_get(io, &f, NULL);
snprintf(dd, sizeof(dd), "file://%s", f);
elm_drag_start(io, ELM_SEL_FORMAT_IMAGE, dd, ELM_XDND_ACTION_COPY,
_image_create_icon, io, NULL, NULL, NULL, NULL, NULL, NULL);
_1s_hold = NULL;
return ECORE_CALLBACK_CANCEL;
}
static void
_image_mouse_down_cb(void *data EINA_UNUSED, Evas *e EINA_UNUSED,
Evas_Object *obj, void *event_info)
{
Evas_Object *io = obj;
Evas_Event_Mouse_Down *ev = event_info;
if (ev->flags != EVAS_BUTTON_NONE)
{
ecore_timer_del(_1s_hold);
_1s_hold = NULL;
}
else if (ev->button == 1)
{
_1s_hold = ecore_timer_add(0.5, _1s_hold_time, io);
}
}
static void
_image_mouse_up_cb(void *data, Evas *e EINA_UNUSED,
Evas_Object *obj EINA_UNUSED, void *event_info)
{
Ephoto_Single_Browser *sb = data;
Evas_Event_Mouse_Up *ev = event_info;
if ((ev->button == 1) && (ev->flags == EVAS_BUTTON_DOUBLE_CLICK))
{
elm_win_fullscreen_set(sb->ephoto->win,
!elm_win_fullscreen_get(sb->ephoto->win));
}
ecore_timer_del(_1s_hold);
_1s_hold = NULL;
}
static void
_scroller_mouse_up_cb(void *data, Evas *e EINA_UNUSED,
Evas_Object *obj EINA_UNUSED, void *event_info)
{
Ephoto_Single_Browser *sb = data;
Evas_Event_Mouse_Up *ev = event_info;
if (ev->button == 3)
{
_edit_menu(sb);
_update_bottom_bar(sb);
}
}
static const char *
_get_edje_group(const char *path)
{
const char *group = NULL;
const char *ext = strrchr(path, '.');
if (ext)
{
ext++;
if ((strcasecmp(ext, "edj") == 0))
{
if (edje_file_group_exists(path, "e/desktop/background"))
group = "e/desktop/background";
else
{
Eina_List *g = edje_file_collection_list(path);
group = eina_list_data_get(g);
edje_file_collection_list_free(g);
}
}
}
return group;
}
static void
_monitor_cb(void *data, Ecore_File_Monitor *em EINA_UNUSED,
Ecore_File_Event event, const char *path EINA_UNUSED)
{
Ephoto_Single_Browser *sb = data;
Ephoto_Viewer *v = evas_object_data_get(sb->viewer, "viewer");
if (event == ECORE_FILE_EVENT_MODIFIED)
{
if (!ecore_file_exists(sb->entry->path))
ephoto_entry_free(sb->ephoto, sb->entry);
else
{
Evas_Object *tmp;
Evas_Coord w, h;
const char *group = _get_edje_group(sb->entry->path);
tmp = evas_object_image_add(evas_object_evas_get(v->table));
evas_object_image_file_set(tmp, sb->entry->path, group);
evas_object_image_size_get(tmp, &w, &h);
evas_object_del(tmp);
if (w > 0 && h > 0)
{
evas_object_hide(v->image);
elm_image_file_set(v->image, sb->entry->path, group);
evas_object_show(v->image);
}
}
}
return;
}
static Evas_Object *
_viewer_add(Evas_Object *parent, const char *path, Ephoto_Single_Browser *sb)
{
Ephoto_Viewer *v = calloc(1, sizeof(Ephoto_Viewer));
int err;
v->zoom_first = EINA_TRUE;
Evas_Coord w, h;
const char *group = _get_edje_group(path);
v->scroller = elm_scroller_add(parent);
evas_object_size_hint_weight_set(v->scroller, EVAS_HINT_EXPAND,
EVAS_HINT_EXPAND);
evas_object_size_hint_align_set(v->scroller,
EVAS_HINT_FILL, EVAS_HINT_FILL);
evas_object_data_set(v->scroller, "viewer", v);
evas_object_event_callback_add(v->scroller, EVAS_CALLBACK_MOUSE_UP,
_scroller_mouse_up_cb, sb);
evas_object_event_callback_add(v->scroller, EVAS_CALLBACK_DEL, _viewer_del,
v);
evas_object_show(v->scroller);
v->table = elm_table_add(v->scroller);
evas_object_size_hint_weight_set(v->table, EVAS_HINT_EXPAND,
EVAS_HINT_EXPAND);
evas_object_size_hint_align_set(v->table, EVAS_HINT_FILL, EVAS_HINT_FILL);
elm_object_content_set(v->scroller, v->table);
evas_object_show(v->table);
v->image = elm_image_add(v->table);
elm_image_preload_disabled_set(v->image, EINA_TRUE);
elm_image_file_set(v->image, path, group);
err = evas_object_image_load_error_get(elm_image_object_get(v->image));
if (err != EVAS_LOAD_ERROR_NONE)
goto error;
evas_object_image_size_get(elm_image_object_get(v->image), &w, &h);
elm_drop_target_add(v->image, ELM_SEL_FORMAT_IMAGE, NULL, NULL, NULL, NULL,
NULL, NULL, NULL, NULL);
evas_object_size_hint_min_set(v->image, w, h);
evas_object_size_hint_max_set(v->image, w, h);
evas_object_event_callback_add(v->image, EVAS_CALLBACK_MOUSE_DOWN,
_image_mouse_down_cb, sb);
evas_object_event_callback_add(v->image, EVAS_CALLBACK_MOUSE_UP,
_image_mouse_up_cb, sb);
elm_table_pack(v->table, v->image, 0, 0, 1, 1);
evas_object_show(v->image);
if (elm_image_animated_available_get(v->image))
{
elm_image_animated_set(v->image, EINA_TRUE);
elm_image_animated_play_set(v->image, EINA_TRUE);
}
v->monitor = ecore_file_monitor_add(path, _monitor_cb, sb);
return v->scroller;
error:
evas_object_event_callback_del(v->scroller, EVAS_CALLBACK_DEL, _viewer_del);
evas_object_data_del(v->scroller, "viewer");
free(v);
return NULL;
}
static void
_viewer_zoom_apply(Ephoto_Viewer *v, double zoom)
{
v->zoom = zoom;
Evas_Coord w, h;
Evas_Object *image;
image = v->image;
evas_object_image_size_get(elm_image_object_get(image), &w, &h);
w *= zoom;
h *= zoom;
evas_object_size_hint_min_set(v->image, w, h);
evas_object_size_hint_max_set(v->image, w, h);
}
static void
_viewer_zoom_fit_apply(Ephoto_Viewer *v)
{
Evas_Coord cw, ch, iw, ih;
Evas_Object *image;
double zx, zy, zoom;
image = v->image;
evas_object_geometry_get(v->scroller, NULL, NULL, &cw, &ch);
evas_object_image_size_get(elm_image_object_get(image), &iw, &ih);
if ((cw <= 0) || (ch <= 0))
return;
EINA_SAFETY_ON_TRUE_RETURN(iw <= 0);
EINA_SAFETY_ON_TRUE_RETURN(ih <= 0);
zx = (double) (cw-15) / (double) iw;
zy = (double) (ch-15) / (double) ih;
zoom = (zx < zy) ? zx : zy;
_viewer_zoom_apply(v, zoom);
}
static void
_viewer_resized(void *data, Evas *e EINA_UNUSED, Evas_Object *obj EINA_UNUSED,
void *event_info EINA_UNUSED)
{
Ephoto_Viewer *v = data;
if (v->zoom_first)
{
Evas_Coord cw, ch, iw, ih;
Evas_Object *image;
image = v->image;
evas_object_geometry_get(v->scroller, NULL, NULL, &cw, &ch);
evas_object_image_size_get(elm_image_object_get(image), &iw, &ih);
if ((cw <= 0) || (ch <= 0))
return;
EINA_SAFETY_ON_TRUE_RETURN(iw <= 0);
EINA_SAFETY_ON_TRUE_RETURN(ih <= 0);
if (iw < cw && ih < ch)
_viewer_zoom_apply(v, 1);
else
_viewer_zoom_fit_apply(v);
}
else
_viewer_zoom_fit_apply(v);
}
static void
_viewer_zoom_set(Evas_Object *obj, double zoom)
{
Ephoto_Viewer *v = evas_object_data_get(obj, "viewer");
_viewer_zoom_apply(v, zoom);
if (v->fit)
{
evas_object_event_callback_del_full(v->scroller, EVAS_CALLBACK_RESIZE,
_viewer_resized, v);
v->fit = EINA_FALSE;
}
}
static double
_viewer_zoom_get(Evas_Object *obj)
{
Ephoto_Viewer *v = evas_object_data_get(obj, "viewer");
return v->zoom;
}
static void
_viewer_zoom_fit(Evas_Object *obj)
{
Ephoto_Viewer *v = evas_object_data_get(obj, "viewer");
if (v->fit)
return;
v->fit = EINA_TRUE;
evas_object_event_callback_add(v->scroller, EVAS_CALLBACK_RESIZE,
_viewer_resized, v);
_viewer_zoom_fit_apply(v);
}
static void
_update_bottom_bar(Ephoto_Single_Browser *sb)
{
Ephoto_Viewer *v = evas_object_data_get(sb->viewer, "viewer");
char image_info[PATH_MAX], *tmp;
Evas_Coord w, h;
if (sb->editing)
return;
evas_object_image_size_get(elm_image_object_get(v->image),
&w, &h);
tmp = _ephoto_get_file_size(sb->entry->path);
snprintf(image_info, PATH_MAX,
"<b>%s:</b> %s <b>%s:</b> %dx%d <b>%s:</b> %s",
_("Type"), efreet_mime_type_get(sb->entry->path),
_("Resolution"), w, h, _("File Size"), tmp);
free(tmp);
elm_object_text_set(sb->infolabel, image_info);
elm_notify_timeout_set(sb->botbox, 5);
evas_object_show(sb->botbox);
}
static void
_orient_apply(Ephoto_Single_Browser *sb)
{
Ephoto_Viewer *v = evas_object_data_get(sb->viewer, "viewer");
int w, h;
switch (sb->orient)
{
case EPHOTO_ORIENT_0:
elm_image_orient_set(v->image, ELM_IMAGE_ORIENT_NONE);
break;
case EPHOTO_ORIENT_90:
elm_image_orient_set(v->image, ELM_IMAGE_ROTATE_90);
break;
case EPHOTO_ORIENT_180:
elm_image_orient_set(v->image, ELM_IMAGE_ROTATE_180);
break;
case EPHOTO_ORIENT_270:
elm_image_orient_set(v->image, ELM_IMAGE_ROTATE_270);
break;
case EPHOTO_ORIENT_FLIP_HORIZ:
elm_image_orient_set(v->image, ELM_IMAGE_FLIP_HORIZONTAL);
break;
case EPHOTO_ORIENT_FLIP_VERT:
elm_image_orient_set(v->image, ELM_IMAGE_FLIP_VERTICAL);
break;
case EPHOTO_ORIENT_FLIP_HORIZ_90:
elm_image_orient_set(v->image, ELM_IMAGE_FLIP_TRANSPOSE);
break;
case EPHOTO_ORIENT_FLIP_VERT_90:
elm_image_orient_set(v->image, ELM_IMAGE_FLIP_TRANSVERSE);
break;
default:
return;
}
elm_table_unpack(v->table, v->image);
elm_object_content_unset(v->scroller);
elm_image_object_size_get(v->image, &w, &h);
sb->edited_image_data =
evas_object_image_data_get(elm_image_object_get(v->image), EINA_FALSE);
sb->ew = w;
sb->eh = h;
evas_object_size_hint_min_set(v->image, w, h);
evas_object_size_hint_max_set(v->image, w, h);
elm_table_pack(v->table, v->image, 0, 0, 1, 1);
elm_object_content_set(v->scroller, v->table);
if (v->fit)
_viewer_zoom_fit_apply(v);
else
_viewer_zoom_set(sb->viewer, _viewer_zoom_get(sb->viewer));
}
static void
_rotate_counterclock(Ephoto_Single_Browser *sb)
{
switch (sb->orient)
{
case EPHOTO_ORIENT_0:
sb->orient = EPHOTO_ORIENT_270;
break;
case EPHOTO_ORIENT_90:
sb->orient = EPHOTO_ORIENT_0;
break;
case EPHOTO_ORIENT_180:
sb->orient = EPHOTO_ORIENT_90;
break;
case EPHOTO_ORIENT_270:
sb->orient = EPHOTO_ORIENT_180;
break;
case EPHOTO_ORIENT_FLIP_HORIZ:
sb->orient = EPHOTO_ORIENT_FLIP_VERT_90;
break;
case EPHOTO_ORIENT_FLIP_VERT_90:
sb->orient = EPHOTO_ORIENT_FLIP_VERT;
break;
case EPHOTO_ORIENT_FLIP_VERT:
sb->orient = EPHOTO_ORIENT_FLIP_HORIZ_90;
break;
case EPHOTO_ORIENT_FLIP_HORIZ_90:
sb->orient = EPHOTO_ORIENT_FLIP_HORIZ;
break;
default:
sb->orient = EPHOTO_ORIENT_0;
break;
}
_orient_apply(sb);
}
static void
_rotate_clock(Ephoto_Single_Browser *sb)
{
switch (sb->orient)
{
case EPHOTO_ORIENT_0:
sb->orient = EPHOTO_ORIENT_90;
break;
case EPHOTO_ORIENT_90:
sb->orient = EPHOTO_ORIENT_180;
break;
case EPHOTO_ORIENT_180:
sb->orient = EPHOTO_ORIENT_270;
break;
case EPHOTO_ORIENT_270:
sb->orient = EPHOTO_ORIENT_0;
break;
case EPHOTO_ORIENT_FLIP_HORIZ:
sb->orient = EPHOTO_ORIENT_FLIP_HORIZ_90;
break;
case EPHOTO_ORIENT_FLIP_VERT_90:
sb->orient = EPHOTO_ORIENT_FLIP_HORIZ;
break;
case EPHOTO_ORIENT_FLIP_VERT:
sb->orient = EPHOTO_ORIENT_FLIP_VERT_90;
break;
case EPHOTO_ORIENT_FLIP_HORIZ_90:
sb->orient = EPHOTO_ORIENT_FLIP_VERT;
break;
default:
sb->orient = EPHOTO_ORIENT_0;
break;
}
_orient_apply(sb);
}
static void
_flip_horiz(Ephoto_Single_Browser *sb)
{
switch (sb->orient)
{
case EPHOTO_ORIENT_0:
sb->orient = EPHOTO_ORIENT_FLIP_HORIZ;
break;
case EPHOTO_ORIENT_90:
sb->orient = EPHOTO_ORIENT_FLIP_VERT_90;
break;
case EPHOTO_ORIENT_180:
sb->orient = EPHOTO_ORIENT_FLIP_VERT;
break;
case EPHOTO_ORIENT_270:
sb->orient = EPHOTO_ORIENT_FLIP_HORIZ_90;
break;
case EPHOTO_ORIENT_FLIP_HORIZ:
sb->orient = EPHOTO_ORIENT_0;
break;
case EPHOTO_ORIENT_FLIP_VERT_90:
sb->orient = EPHOTO_ORIENT_90;
break;
case EPHOTO_ORIENT_FLIP_VERT:
sb->orient = EPHOTO_ORIENT_180;
break;
case EPHOTO_ORIENT_FLIP_HORIZ_90:
sb->orient = EPHOTO_ORIENT_270;
break;
default:
sb->orient = EPHOTO_ORIENT_0;
break;
}
_orient_apply(sb);
}
static void
_flip_vert(Ephoto_Single_Browser *sb)
{
switch (sb->orient)
{
case EPHOTO_ORIENT_0:
sb->orient = EPHOTO_ORIENT_FLIP_VERT;
break;
case EPHOTO_ORIENT_90:
sb->orient = EPHOTO_ORIENT_FLIP_HORIZ_90;
break;
case EPHOTO_ORIENT_180:
sb->orient = EPHOTO_ORIENT_FLIP_HORIZ;
break;
case EPHOTO_ORIENT_270:
sb->orient = EPHOTO_ORIENT_FLIP_VERT_90;
break;
case EPHOTO_ORIENT_FLIP_HORIZ:
sb->orient = EPHOTO_ORIENT_180;
break;
case EPHOTO_ORIENT_FLIP_VERT_90:
sb->orient = EPHOTO_ORIENT_270;
break;
case EPHOTO_ORIENT_FLIP_VERT:
sb->orient = EPHOTO_ORIENT_0;
break;
case EPHOTO_ORIENT_FLIP_HORIZ_90:
sb->orient = EPHOTO_ORIENT_90;
break;
default:
sb->orient = EPHOTO_ORIENT_0;
break;
}
_orient_apply(sb);
}
static void
_mouse_wheel(void *data, Evas *e EINA_UNUSED, Evas_Object *obj EINA_UNUSED,
void *event_info)
{
Ephoto_Single_Browser *sb = data;
Evas_Event_Mouse_Wheel *ev = event_info;
if (!evas_key_modifier_is_set(ev->modifiers, "Control"))
return;
if (ev->z > 0)
_zoom_out(sb);
else
_zoom_in(sb);
}
static Ephoto_Entry *
_first_entry_find(Ephoto_Single_Browser *sb)
{
return eina_list_nth(sb->entries, 0);
}
static Ephoto_Entry *
_last_entry_find(Ephoto_Single_Browser *sb)
{
return eina_list_last_data_get(sb->entries);
}
static char *
_ephoto_get_file_size(const char *path)
{
char isize[PATH_MAX];
Eina_File *f = eina_file_open(path, EINA_FALSE);
size_t size = eina_file_size_get(f);
eina_file_close(f);
double dsize = (double) size;
if (dsize < 1024.0)
snprintf(isize, sizeof(isize), "%'.0f%s", dsize, ngettext("B", "B",
dsize));
else
{
dsize /= 1024.0;
if (dsize < 1024)
snprintf(isize, sizeof(isize), "%'.0f%s", dsize,
ngettext("KB", "KB", dsize));
else
{
dsize /= 1024.0;
if (dsize < 1024)
snprintf(isize, sizeof(isize), "%'.1f%s", dsize,
ngettext("MB", "MB", dsize));
else
{
dsize /= 1024.0;
if (dsize < 1024)
snprintf(isize, sizeof(isize), "%'.1f%s", dsize,
ngettext("GB", "GB", dsize));
else
{
dsize /= 1024.0;
snprintf(isize, sizeof(isize), "%'.1f%s", dsize,
ngettext("TB", "TB", dsize));
}
}
}
}
return strdup(isize);
}
static void
_ephoto_single_browser_recalc(Ephoto_Single_Browser *sb)
{
if (sb->viewer)
{
evas_object_del(sb->viewer);
sb->viewer = NULL;
}
if (sb->nolabel)
{
evas_object_del(sb->nolabel);
sb->nolabel = NULL;
}
if (sb->entry)
{
const char *bname = ecore_file_file_get(sb->entry->path);
sb->viewer = _viewer_add(sb->main, sb->entry->path, sb);
if (sb->viewer)
{
elm_box_pack_start(sb->mhbox, sb->viewer);
evas_object_show(sb->viewer);
evas_object_event_callback_add(sb->viewer,
EVAS_CALLBACK_MOUSE_WHEEL, _mouse_wheel, sb);
_update_bottom_bar(sb);
ephoto_title_set(sb->ephoto, bname);
if (!_ephoto_file_image_can_save(strrchr(bname, '.')+1))
elm_object_item_disabled_set(sb->save, EINA_TRUE);
else
elm_object_item_disabled_set(sb->save, EINA_FALSE);
}
else
{
sb->nolabel = elm_label_add(sb->mhbox);