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.

2530 lines
69 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;
typedef struct _Ephoto_History Ephoto_History;
struct _Ephoto_Single_Browser
{
Ephoto *ephoto;
Evas_Object *main;
Evas_Object *mhbox;
Evas_Object *table;
Evas_Object *viewer;
Evas_Object *nolabel;
Evas_Object *event;
Evas_Object *edit_main;
Elm_Object_Item *save;
const char *pending_path;
Ephoto_Entry *entry;
Ephoto_Orient orient;
Eina_List *handlers;
Eina_List *entries;
Eina_List *history;
unsigned int history_pos;
Eina_Bool editing:1;
Eina_Bool cropping:1;
unsigned int *edited_image_data;
Evas_Coord ew;
Evas_Coord eh;
};
struct _Ephoto_Viewer
{
Eina_List *handlers;
Eio_Monitor *monitor;
Eina_List *monitor_handlers;
Evas_Object *scroller;
Evas_Object *table;
Evas_Object *image;
double zoom;
Eina_Bool fit:1;
Eina_Bool zoom_first:1;
double duration;
int frame_count;
int cur_frame;
Ecore_Timer *anim_timer;
};
struct _Ephoto_History
{
unsigned int *im_data;
Evas_Coord w;
Evas_Coord h;
Ephoto_Orient orient;
};
/*Common Callbacks*/
static const char *_ephoto_get_edje_group(const char *path);
static char *_ephoto_get_file_size(const char *path);
static void _ephoto_update_bottom_bar(Ephoto_Single_Browser *sb);
/*Main Callbacks*/
static void _ephoto_main_edit_menu(Ephoto_Single_Browser *sb);
static void _ephoto_main_key_down(void *data, Evas *e EINA_UNUSED,
Evas_Object *obj EINA_UNUSED,void *event_info EINA_UNUSED);
static void _ephoto_show_settings(void *data, Evas_Object *obj EINA_UNUSED,
void *event_info EINA_UNUSED);
static void _ephoto_main_back(void *data, Evas_Object *obj EINA_UNUSED,
void *event_info EINA_UNUSED);
static void _ephoto_main_del(void *data, Evas *e EINA_UNUSED,
Evas_Object *obj EINA_UNUSED, void *event_info EINA_UNUSED);
static void _next_entry(Ephoto_Single_Browser *sb);
/*Common*/
static const char *
_ephoto_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
_ephoto_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(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->ephoto->infolabel, image_info);
}
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
_menu_dismissed_cb(void *data, Evas_Object *obj,
void *event_info EINA_UNUSED)
{
Ephoto_Single_Browser *sb = data;
evas_object_del(obj);
elm_object_focus_set(sb->event, EINA_TRUE);
}
/*Image Viewer Callbacks*/
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 (sb->editing)
return;
if (ev->button == 3)
{
_ephoto_main_edit_menu(sb);
_ephoto_update_bottom_bar(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;
Ecore_Event_Handler *handler;
if (v->monitor)
{
eio_monitor_del(v->monitor);
EINA_LIST_FREE(v->monitor_handlers, handler)
ecore_event_handler_del(handler);
}
if (v->anim_timer)
ecore_timer_del(v->anim_timer);
free(v);
}
static Eina_Bool
_monitor_cb(void *data, int type,
void *event EINA_UNUSED)
{
Ephoto_Single_Browser *sb = data;
Ephoto_Viewer *v = evas_object_data_get(sb->viewer, "viewer");
if (sb->edited_image_data)
return ECORE_CALLBACK_PASS_ON;
if (type == EIO_MONITOR_FILE_MODIFIED)
{
if (!ecore_file_exists(sb->entry->path))
{
if (eina_list_count(sb->entries) > 1)
_next_entry(sb);
else
_ephoto_main_back(sb, NULL, NULL);
ephoto_entry_free(sb->ephoto, sb->entry);
}
else
{
Evas_Object *tmp;
Evas_Coord w, h;
const char *group = _ephoto_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);
evas_object_image_file_set(v->image, sb->entry->path, group);
evas_object_show(v->image);
}
}
}
return ECORE_CALLBACK_PASS_ON;
}
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(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(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(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
_orient_apply(Ephoto_Single_Browser *sb)
{
Ephoto_History *eh = NULL;
Ephoto_Viewer *v = evas_object_data_get(sb->viewer, "viewer");
Evas_Coord w, h;
Eina_List *l;
char buf[PATH_MAX];
switch (sb->orient)
{
case EPHOTO_ORIENT_0:
evas_object_image_orient_set(v->image, EVAS_IMAGE_ORIENT_NONE);
break;
case EPHOTO_ORIENT_90:
evas_object_image_orient_set(v->image, EVAS_IMAGE_ORIENT_90);
break;
case EPHOTO_ORIENT_180:
evas_object_image_orient_set(v->image, EVAS_IMAGE_ORIENT_180);
break;
case EPHOTO_ORIENT_270:
evas_object_image_orient_set(v->image, EVAS_IMAGE_ORIENT_270);
break;
case EPHOTO_ORIENT_FLIP_HORIZ:
evas_object_image_orient_set(v->image, EVAS_IMAGE_FLIP_HORIZONTAL);
break;
case EPHOTO_ORIENT_FLIP_VERT:
evas_object_image_orient_set(v->image, EVAS_IMAGE_FLIP_VERTICAL);
break;
case EPHOTO_ORIENT_FLIP_HORIZ_90:
evas_object_image_orient_set(v->image, EVAS_IMAGE_FLIP_TRANSPOSE);
break;
case EPHOTO_ORIENT_FLIP_VERT_90:
evas_object_image_orient_set(v->image, EVAS_IMAGE_FLIP_TRANSVERSE);
break;
default:
return;
}
elm_table_unpack(v->table, v->image);
elm_object_content_unset(v->scroller);
evas_object_image_size_get(v->image, &w, &h);
sb->edited_image_data =
evas_object_image_data_get(v->image, EINA_FALSE);
sb->ew = w;
sb->eh = h;
if (sb->history_pos < (eina_list_count(sb->history)-1))
{
int count;
count = sb->history_pos + 1;
l = eina_list_nth_list(sb->history, count);
while (l)
{
eh = eina_list_data_get(l);
sb->history = eina_list_remove_list(sb->history, l);
free(eh->im_data);
free(eh);
eh = NULL;
l = eina_list_nth_list(sb->history, count);
}
}
eh = calloc(1, sizeof(Ephoto_History));
eh->im_data = malloc(sizeof(unsigned int) * sb->ew * sb->eh);
eh->im_data = memcpy(eh->im_data, sb->edited_image_data,
sizeof(unsigned int) * sb->ew * sb->eh);
eh->w = sb->ew;
eh->h = sb->eh;
eh->orient = sb->orient;
sb->history = eina_list_append(sb->history, eh);
sb->history_pos = eina_list_count(sb->history) - 1;
snprintf(buf, PATH_MAX, "%s [%s]", sb->entry->basename, _("MODIFIED"));
ephoto_title_set(sb->ephoto, buf);
_ephoto_update_bottom_bar(sb);
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 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 void
_zoom_set(Ephoto_Single_Browser *sb, double zoom)
{
if (zoom <= 0.0)
return;
_viewer_zoom_set(sb->viewer, zoom);
}
static void
_zoom_fit(Ephoto_Single_Browser *sb)
{
if (sb->viewer)
_viewer_zoom_fit(sb->viewer);
}
static void
_zoom_in(Ephoto_Single_Browser *sb)
{
double change = (1.0 + ZOOM_STEP);
_viewer_zoom_set(sb->viewer, _viewer_zoom_get(sb->viewer) * change);
}
static void
_zoom_out(Ephoto_Single_Browser *sb)
{
double change = (1.0 - ZOOM_STEP);
_viewer_zoom_set(sb->viewer, _viewer_zoom_get(sb->viewer) * change);
}
static void
_zoom_in_cb(void *data, Evas_Object *obj EINA_UNUSED,
void *event_info EINA_UNUSED)
{
Ephoto_Single_Browser *sb = data;
Ephoto_Viewer *v = evas_object_data_get(sb->viewer, "viewer");
v->zoom_first = EINA_FALSE;
_zoom_in(sb);
}
static void
_zoom_out_cb(void *data, Evas_Object *obj EINA_UNUSED,
void *event_info EINA_UNUSED)
{
Ephoto_Single_Browser *sb = data;
Ephoto_Viewer *v = evas_object_data_get(sb->viewer, "viewer");