yakov pushed a commit to branch master. http://git.enlightenment.org/tools/erigo.git/commit/?id=1be14afa8f8067d399bf141e8b8105eb849d0b75
commit 1be14afa8f8067d399bf141e8b8105eb849d0b75 Author: Yakov Goldberg <yako...@samsung.com> Date: Mon Nov 9 13:19:11 2015 +0200 Refactoring undo-redo Clean up, merge undo() and redo() functions into the one context_undo_redo_apply, etc. --- src/bin/gui/editor.c | 404 +++++---------------------------------------------- src/lib/undoredo.c | 115 +++++++++++++++ src/lib/undoredo.h | 3 + 3 files changed, 152 insertions(+), 370 deletions(-) diff --git a/src/bin/gui/editor.c b/src/bin/gui/editor.c index 352de07..678a15b 100644 --- a/src/bin/gui/editor.c +++ b/src/bin/gui/editor.c @@ -161,23 +161,6 @@ main_wdg_info_frame_get(const Main_Wdg_Info *wi) return wi ? wi->frame : NULL; } -Eina_Bool -widget_name_change(Gui_Widget *wdg, const char *str) -{ - const Gui_Context *ctx = wdg_context_get(wdg); - if (strcmp(wdg_name_get(wdg), str) && !eid_rename(wdg_eid_get(wdg), str)) - { - ERR("Name \"%s\" already exists!", str); - return EINA_FALSE; - } - - const Gui_Widget *sel = gui_context_data_get(ctx, SELECTED_WDG); - objtree_item_refresh(wdg); - objtree_item_selected_set(sel); - - return EINA_TRUE; -} - /* x_fix/y_fix - correction of initial position of smart_parent. whis is not resized before elm_run. */ static void _canvas_free_coords_get(const Gui_Context *ctx, Evas_Coord *x, Evas_Coord *y) @@ -2317,9 +2300,6 @@ _drag_start_post(const Gui_Widget *wdg, const Eo *_wdg_eo) wdg_parent_set((Gui_Widget *) wdg, NULL); } -static Eina_Bool -__undo_no_update(const Gui_Context *ctx); - /* This callback is added only for dragging of objects on canvas. */ static void _dragdone_post_cb(Eina_Bool accept, const Eo *wdg_eo, const Gui_Widget *drag_start_wdg) @@ -2673,183 +2653,6 @@ _wdg_parent_win_reload(const Gui_Widget *wdg) _editor_wdg_selected_set(NULL); } -/* Undo last action */ -static Eina_Bool -__undo_no_update(const Gui_Context *ctx) -{ - EINA_SAFETY_ON_NULL_RETURN_VAL(ctx, EINA_FALSE); - const Memento *memento; - const Memento_Command *cmd; - /* Save head memento for post function. */ - memento = context_current_memento_get(ctx); - if (!memento) return EINA_FALSE; - - const Eina_List *cmds_list = memento_commands_get(memento), *itr; - EINA_LIST_REVERSE_FOREACH(cmds_list, itr, cmd) - { - Memento_Type type = memento_command_type_get(cmd); - Eid *eid = memento_command_wdg_get(cmd); - Gui_Widget *wdg = wdg_get(eid); - switch (type) - { - case MEMENTO_PROPERTY: - { - Gui_Widget_Property *old_prop, *new_prop; - const Op_Desc *op_desc = NULL; - - if (!wdg) - { - ERR("wdg not found"); - return EINA_FALSE; - } - - old_prop = memento_command_old_pointer_get(cmd); - new_prop = memento_command_new_pointer_get(cmd); - /* If last action was 'Drop prop', new_prop is NULL. */ - if (new_prop) - { - op_desc = prop_op_desc_get(new_prop); - wdg_prop_remove(wdg, new_prop); - } - - proplayout_view_switch(PROPERTY_TYPE); - if (old_prop) - { - op_desc = prop_op_desc_get(old_prop); - wdg_prop_add(wdg, old_prop); - if (_editor_wdg_selected_get() != wdg) - { - objtree_item_selected_set(wdg); - } - propview_item_update(old_prop); - } - /* If old_property == NULL, need to reload widget. */ - else - { - objtree_item_selected_set(wdg); - } - if (op_desc) - propview_item_selected_set(op_desc); - break; - } - case MEMENTO_OBJ_CONTAINER: - { - Object_Container *old_container; - - if (!wdg) - { - ERR("wdg not found"); - return EINA_FALSE; - } - - old_container = memento_command_old_pointer_get(cmd); - - wdg_obj_container_unset(wdg); - wdg_obj_container_set(wdg, old_container); - break; - } - case MEMENTO_ITEM_CONTAINER_ITEM: - { - Item_Container_Item *old_it; - - if (!wdg) - { - ERR("wdg not found"); - return EINA_FALSE; - } - - old_it = memento_command_old_pointer_get(cmd); - - item_container_item_set_on_id(item_container_item_eid_get(old_it), old_it); - break; - } - case MEMENTO_CALLBACK: - { - Gui_Widget_Callback *old_cb; - - if (!wdg) - { - ERR("wdg not found"); - return EINA_FALSE; - } - - old_cb = memento_command_old_pointer_get(cmd); - Eid *cb_eid = cb_eid_get(old_cb); - - cb_set_on_id(cb_eid, old_cb); - - proplayout_view_switch(CALLBACK_TYPE); - _editor_wdg_selected_set(wdg); - cbview_item_selected_set(cb_eid); - break; - } - case MEMENTO_CALLBACK_ADD_DEL: - { - Callback_Container *old_cc; - - if (!wdg) - { - ERR("wdg not found"); - return EINA_FALSE; - } - old_cc = memento_command_old_pointer_get(cmd); - wdg_cb_container_unset(wdg); - wdg_cb_container_set(wdg, old_cc); - break; - } - case MEMENTO_WIDGET: - { - Eina_Bool old_state = (intptr_t) memento_command_old_pointer_get(cmd); - eid_active_set(eid, old_state); - break; - } - case MEMENTO_WIDGET_PARENT: - { - Eid *old_parent_id = (Eid *) memento_command_old_pointer_get(cmd); - wdg_parent_set(wdg, eid_name_get(old_parent_id)); - break; - } - case MEMENTO_WIDGET_NAME: - { - if (eid) - { - const char *old_name = (const char *)memento_command_old_pointer_get(cmd); - widget_name_change(wdg_get(eid), old_name); - } - break; - } - case MEMENTO_WIDGET_PUBLIC: - { - if (eid) - { - Eina_Bool old_public_state = (Eina_Bool) (intptr_t) memento_command_old_pointer_get(cmd); - wdg_public_set(wdg_get(eid), old_public_state); - } - break; - } - case MEMENTO_ACTION: - { - if (eid) - { - Gui_Action *old_action = (Gui_Action *)memento_command_old_pointer_get(cmd); - action_set(eid, old_action); - } - - proplayout_view_switch(CALLBACK_TYPE); - /* FIXME: Focus on wdg/cb needed */ - break; - } - case MEMENTO_NONE: - { - break; - } - default: - ERR("case - default"); - } - } - return EINA_TRUE; -} - static void _editor_undo_redo_post(const Memento *memento) { @@ -2864,6 +2667,16 @@ _editor_undo_redo_post(const Memento *memento) { case MEMENTO_PROPERTY: { + objtree_item_selected_set(memento_wdg); + proplayout_view_switch(PROPERTY_TYPE); + // FIXME + // depending on direction need + // to update old or new value in propview + // propview_item_update(to); + + //set prop selected + //if (op_desc) + // propview_item_selected_set(op_desc); break; } case MEMENTO_OBJ_CONTAINER: @@ -2876,6 +2689,14 @@ _editor_undo_redo_post(const Memento *memento) } case MEMENTO_CALLBACK: { + proplayout_view_switch(CALLBACK_TYPE); + _editor_wdg_selected_set(memento_wdg); + + // FIXME + // depending on direction need + // to select old or new value in propview + //Eid *cb_eid = cb_eid_get(to); + //cbview_item_selected_set(cb_eid); break; } case MEMENTO_CALLBACK_ADD_DEL: @@ -2895,9 +2716,18 @@ _editor_undo_redo_post(const Memento *memento) break; } case MEMENTO_NONE: - case MEMENTO_ACTION: case MEMENTO_WIDGET_NAME: { + /*FIXME: + * update item in objtree*/ + const Gui_Widget *sel = gui_context_data_get(_active_context_get(), SELECTED_WDG); + objtree_item_refresh(memento_wdg); + objtree_item_selected_set(sel); + break; + } + case MEMENTO_ACTION: + { + proplayout_view_switch(CALLBACK_TYPE); break; } case MEMENTO_WIDGET_PUBLIC: @@ -2920,195 +2750,29 @@ _editor_undo(const Gui_Context *ctx) /* Save head memento for post function. */ head_memento = context_current_memento_get(ctx); - if (! __undo_no_update(ctx)) return EINA_FALSE; + if (!context_undo_redo_apply(ctx, EINA_FALSE)) return EINA_FALSE; /* Update propview. */ context_memento_not_updated_add((Gui_Context *) ctx, head_memento, EINA_FALSE); _editor_undo_redo_post(head_memento); - context_memento_move_to_prev((Gui_Context *) ctx); eo_do(g->main_win->toolbar_redo_it, elm_wdg_item_disabled_set(context_can_redo(ctx) ? EINA_FALSE : EINA_TRUE)); eo_do(g->main_win->toolbar_undo_it, elm_wdg_item_disabled_set(context_can_undo(ctx) ? EINA_FALSE : EINA_TRUE)); return EINA_TRUE; } -/* Redo last action */ +/* Undo last action */ static Eina_Bool _editor_redo(const Gui_Context *ctx) { EINA_SAFETY_ON_NULL_RETURN_VAL(ctx, EINA_FALSE); - const Memento *memento, *head_memento; - const Memento_Command *cmd; - context_memento_move_to_next((Gui_Context *) ctx); - head_memento = memento = context_current_memento_get(ctx); - - const Eina_List *cmds_list = memento_commands_get(memento), *itr; - EINA_LIST_FOREACH(cmds_list, itr, cmd) - { - Memento_Type type = memento_command_type_get(cmd); - Eid *eid = memento_command_wdg_get(cmd); - Gui_Widget *wdg = wdg_get(eid); - switch (type) - { - case MEMENTO_PROPERTY: - { - const Op_Desc *op_desc = NULL; - - if (!wdg) - { - ERR("wdg not found"); - return EINA_FALSE; - } - - Gui_Widget_Property *new_prop, *old_prop; - old_prop = memento_command_old_pointer_get(cmd); - new_prop = memento_command_new_pointer_get(cmd); - - if (old_prop) - { - wdg_prop_remove(wdg, old_prop); - op_desc = prop_op_desc_get(old_prop); - } - - proplayout_view_switch(PROPERTY_TYPE); - if (new_prop) - { - op_desc = prop_op_desc_get(new_prop); - wdg_prop_add(wdg, new_prop); - if (_editor_wdg_selected_get() != wdg) - { - objtree_item_selected_set(wdg); - } - propview_item_update(new_prop); - } - else - { - objtree_item_selected_set(wdg); - } - if (op_desc) - propview_item_selected_set(op_desc); - break; - } - case MEMENTO_CALLBACK: - { - Gui_Widget_Callback *new_cb; - - if (!wdg) - { - ERR("wdg not found"); - return EINA_FALSE; - } - - new_cb = memento_command_new_pointer_get(cmd); - Eid *cb_eid = cb_eid_get(new_cb); - - cb_set_on_id(cb_eid, new_cb); - - proplayout_view_switch(CALLBACK_TYPE); - _editor_wdg_selected_set(wdg); - cbview_item_selected_set(cb_eid); - break; - } - case MEMENTO_CALLBACK_ADD_DEL: - { - Callback_Container *new_cc; - - if (!wdg) - { - ERR("wdg not found"); - return EINA_FALSE; - } - new_cc = memento_command_new_pointer_get(cmd); - wdg_cb_container_unset(wdg); - wdg_cb_container_set(wdg, new_cc); - - proplayout_view_switch(CALLBACK_TYPE); - _editor_wdg_selected_set(wdg); - break; - } - case MEMENTO_OBJ_CONTAINER: - { - Object_Container *new_container; - - if (!wdg) - { - ERR("wdg not found"); - return EINA_FALSE; - } - - new_container = memento_command_new_pointer_get(cmd); - - wdg_obj_container_unset(wdg); - wdg_obj_container_set(wdg, new_container); - break; - } - case MEMENTO_ITEM_CONTAINER_ITEM: - { - Item_Container_Item *new_it; - - if (!wdg) - { - ERR("wdg not found"); - return EINA_FALSE; - } - - new_it = memento_command_new_pointer_get(cmd); - - item_container_item_set_on_id(item_container_item_eid_get(new_it), new_it); - break; - } - case MEMENTO_WIDGET: - { - Eina_Bool new_state = (intptr_t) memento_command_new_pointer_get(cmd); - eid_active_set(eid, new_state); - break; - } - case MEMENTO_WIDGET_PARENT: - { - Eid *new_parent_id = (Eid *) memento_command_new_pointer_get(cmd); - wdg_parent_set(wdg, eid_name_get(new_parent_id)); - break; - } - case MEMENTO_WIDGET_NAME: - { - if (eid) - { - const char *new_name = (const char *) memento_command_new_pointer_get(cmd); - widget_name_change(wdg_get(eid), new_name); - } - break; - } - case MEMENTO_WIDGET_PUBLIC: - { - if (eid) - { - Eina_Bool new_public_state = (Eina_Bool) (intptr_t) memento_command_new_pointer_get(cmd); - wdg_public_set(wdg_get(eid), new_public_state); - } - break; - } - case MEMENTO_ACTION: - { - if (eid) - { - Gui_Action *new_action = (Gui_Action *)memento_command_new_pointer_get(cmd); - action_set(eid, new_action); - } + const Memento *head_memento; - proplayout_view_switch(CALLBACK_TYPE); - /* FIXME: Focus on wdg/cb needed */ - break; - } - case MEMENTO_NONE: - { - break; - } - default: - ERR("case - default"); - } - } + if (!context_undo_redo_apply(ctx, EINA_TRUE)) return EINA_FALSE; /* Update propview. */ + head_memento = context_current_memento_get(ctx); context_memento_not_updated_add((Gui_Context *) ctx, head_memento, EINA_TRUE); + _editor_undo_redo_post(head_memento); eo_do(g->main_win->toolbar_redo_it, elm_wdg_item_disabled_set(context_can_redo(ctx) ? EINA_FALSE : EINA_TRUE)); diff --git a/src/lib/undoredo.c b/src/lib/undoredo.c index 2ac95f5..6491322 100644 --- a/src/lib/undoredo.c +++ b/src/lib/undoredo.c @@ -545,6 +545,121 @@ memento_command_new_pointer_get(const Memento_Command *cmd) return NULL; } +Eina_Bool +context_undo_redo_apply(const Gui_Context *ctx, Eina_Bool memento_redo) +{ + EINA_SAFETY_ON_NULL_RETURN_VAL(ctx, EINA_FALSE); + const Memento *memento; + const Memento_Command *cmd; + + /*For redo*/ + if (memento_redo) context_memento_move_to_next((Gui_Context *) ctx); + + memento = context_current_memento_get(ctx); + if (!memento) return EINA_FALSE; + + const Eina_List *cmds_list = memento_commands_get(memento), *itr; + itr = memento_redo ? cmds_list : eina_list_last(cmds_list); + + /* According to direction we must iterate forward/backward through commands. + * Following 'for' loop is expanded EINA_LIST_FOREACH and EINA_LIST_REVERSE_FOREACH */ + for (cmd = eina_list_data_get(itr); + itr; + itr = (memento_redo ? eina_list_next(itr) : eina_list_prev(itr)), + cmd = eina_list_data_get(itr)) + { + Memento_Type type = memento_command_type_get(cmd); + Eid *eid = memento_command_wdg_get(cmd); + + void *to, *from; + if (memento_redo) + { + from = memento_command_old_pointer_get(cmd); + to = memento_command_new_pointer_get(cmd); + } + else + { + from = memento_command_new_pointer_get(cmd); + to = memento_command_old_pointer_get(cmd); + } + undo_debug("%s : %s on %p: %p -> %p", + memento_redo ? "Redo" : "Undo", + _memento_type_get_as_string(cmd->type), + cmd->eid, from, to); + + switch (type) + { + case MEMENTO_PROPERTY: + { + Gui_Widget *wdg = wdg_get(eid); + if (from) wdg_prop_remove(wdg, from); + if (to) wdg_prop_add(wdg, to); + break; + } + case MEMENTO_OBJ_CONTAINER: + { + Gui_Widget *wdg = wdg_get(eid); + wdg_obj_container_unset(wdg); + wdg_obj_container_set(wdg, to); + break; + } + case MEMENTO_ITEM_CONTAINER_ITEM: + { + item_container_item_set_on_id(item_container_item_eid_get(to), to); + break; + } + case MEMENTO_CALLBACK: + { + Eid *cb_eid = cb_eid_get(to); + cb_set_on_id(cb_eid, to); + break; + } + case MEMENTO_CALLBACK_ADD_DEL: + { + Gui_Widget *wdg = wdg_get(eid); + wdg_cb_container_unset(wdg); + wdg_cb_container_set(wdg, to); + break; + } + case MEMENTO_WIDGET: + { + eid_active_set(eid, (intptr_t) to); + break; + } + case MEMENTO_WIDGET_PARENT: + { + Gui_Widget *wdg = wdg_get(eid); + wdg_parent_set(wdg, eid_name_get(to)); + break; + } + case MEMENTO_WIDGET_NAME: + { + eid_rename(eid, to); + break; + } + case MEMENTO_WIDGET_PUBLIC: + { + wdg_public_set(wdg_get(eid), (intptr_t) to); + break; + } + case MEMENTO_ACTION: + { + action_set(eid, to); + break; + } + case MEMENTO_NONE: + { + break; + } + default: + ERR("case - default"); + } + } + + if (!memento_redo) context_memento_move_to_prev((Gui_Context *) ctx); + return EINA_TRUE; +} + #if 0 /* Assigns source memento's new pointer to dest memento's new pointer. * Source memento must be deleted. diff --git a/src/lib/undoredo.h b/src/lib/undoredo.h index bdffcfb..71036eb 100644 --- a/src/lib/undoredo.h +++ b/src/lib/undoredo.h @@ -61,4 +61,7 @@ memento_command_old_pointer_get(const Memento_Command *cmd); void * memento_command_new_pointer_get(const Memento_Command *cmd); +Eina_Bool +context_undo_redo_apply(const Gui_Context *ctx, Eina_Bool memento_redo); + #endif --