From d564ba1e49d960246f7dbaf96dfed0e614f79217 Mon Sep 17 00:00:00 2001 From: Olaf Wintermann Date: Sat, 24 Jan 2026 15:59:03 +0100 Subject: [PATCH] implement nbconfig_notebooklist_move_up/down --- application/nbconfig.c | 73 ++++++++++++++++++++++++++++++++++++++++++ application/nbconfig.h | 3 ++ 2 files changed, 76 insertions(+) diff --git a/application/nbconfig.c b/application/nbconfig.c index 2786593..8d11b80 100644 --- a/application/nbconfig.c +++ b/application/nbconfig.c @@ -402,12 +402,85 @@ static void nbconfig_notebooklist_delete(UiEvent *event, void *userdata) { ui_listselection_free(sel); } +Resource* nbconfig_notebooklist_find_prev(NotebookConfigDialog *wdata, Resource *res) { + Resource *prev = NULL; + CxIterator i = cxListIterator(wdata->tab1_notebooks->data); + cx_foreach(Resource *, nb, i) { + if(res == nb && res->parent_id == prev->parent_id) { + return prev; + } + prev = nb; + } + return NULL; +} + +Resource* nbconfig_notebooklist_find_next(NotebookConfigDialog *wdata, Resource *res) { + CxIterator i = cxListIterator(wdata->tab1_notebooks->data); + int res_found = 0; + cx_foreach(Resource *, nb, i) { + if(res == nb) { + res_found = 1; + } else if(res_found) { + if(nb->parent_id == res->parent_id) { + return nb; + } + } + } + return NULL; +} + +static void swap_result(UiEvent *event, int error, void *userdata) { + if(error == 0) { + note_store_groups_updated(); + } else { + fprintf(stderr, "Error: cannot wrap notebook positions\n"); + } +} + static void nbconfig_notebooklist_move_up(UiEvent *event, void *userdata) { + NotebookConfigDialog *wdata = event->window; + CxList *nblist = wdata->tab1_notebooks->data; + if(!wdata->selected_notebook) { + return; + } + + Resource *prev = nbconfig_notebooklist_find_prev(wdata, wdata->selected_notebook); + if(prev) { + note_store_notebook_swap_position_async(wdata->obj, prev->notebook, wdata->selected_notebook->notebook, swap_result, wdata); + + // update UI + size_t prev_index = cxListFind(nblist, prev); + size_t res_index = cxListFind(nblist, wdata->selected_notebook); + cxListSwap(nblist, prev_index, res_index); + ui_list_update(wdata->tab1_notebooks); + wdata->valuechange = TRUE; + ui_list_setselection(wdata->tab1_notebooks, prev_index); + wdata->valuechange = FALSE; + } } static void nbconfig_notebooklist_move_down(UiEvent *event, void *userdata) { + NotebookConfigDialog *wdata = event->window; + CxList *nblist = wdata->tab1_notebooks->data; + if(!wdata->selected_notebook) { + return; + } + + Resource *next = nbconfig_notebooklist_find_next(wdata, wdata->selected_notebook); + if(next) { + note_store_notebook_swap_position_async(wdata->obj, next->notebook, wdata->selected_notebook->notebook, swap_result, wdata); + + // update UI + size_t next_index = cxListFind(nblist, next); + size_t res_index = cxListFind(nblist, wdata->selected_notebook); + cxListSwap(nblist, next_index, res_index); + ui_list_update(wdata->tab1_notebooks); + wdata->valuechange = TRUE; + ui_list_setselection(wdata->tab1_notebooks, next_index); + wdata->valuechange = FALSE; + } } static void nbconfig_notebooklist_name_changed(UiEvent *event, void *userdata) { diff --git a/application/nbconfig.h b/application/nbconfig.h index 414f35b..3b2c5e3 100644 --- a/application/nbconfig.h +++ b/application/nbconfig.h @@ -80,6 +80,9 @@ void nbconfig_update_lists(NotebookConfigDialog *wdata); void nbconfig_build_current_names_map(NotebookConfigDialog *wdata, Resource *res); +Resource* nbconfig_notebooklist_find_prev(NotebookConfigDialog *wdata, Resource *res); +Resource* nbconfig_notebooklist_find_next(NotebookConfigDialog *wdata, Resource *res); + void nbconfig_notebook_save(NotebookConfigDialog *nbconfig); void notebook_config_dialog(void); -- 2.47.3