From 65bb05487eb99ac559330b4764c1567aa1f4aa48 Mon Sep 17 00:00:00 2001 From: Olaf Wintermann Date: Thu, 22 Jan 2026 22:06:41 +0100 Subject: [PATCH] implement moving notebooks to other groups --- application/nbconfig.c | 101 +++++++++++++++++++++++++++++++---------- application/nbconfig.h | 2 +- 2 files changed, 78 insertions(+), 25 deletions(-) diff --git a/application/nbconfig.c b/application/nbconfig.c index d5c77c0..03ab7f2 100644 --- a/application/nbconfig.c +++ b/application/nbconfig.c @@ -134,8 +134,7 @@ void nbconfig_build_current_names_map(NotebookConfigDialog *wdata, Resource *res static void nbconfig_close(UiEvent *event, void *userdata) { NotebookConfigDialog *wdata = event->window; - //nbconfig_group_save(wdata); - //nbconfig_notebook_save(wdata); + nbconfig_notebook_save(wdata); } static int nbconfig_list_activate_event(UiEvent *event, UiList *list, void **listelm) { @@ -192,12 +191,12 @@ void nbconfig_notebook_save(NotebookConfigDialog *nbconfig) { res->parent_id = parent->resource_id; } - note_store_save_notebook_async(application_global_obj(), res, prev_parent_id, notebook_saved, res); } static void nbconfig_load_notebook(NotebookConfigDialog *wdata, Resource *res) { Notebook *nb = res->notebook; + wdata->valuechange = TRUE; ui_set(wdata->tab1_notebook_name, res->nodename); if(res->parent_id != wdata->root_resource_id) { @@ -217,6 +216,8 @@ static void nbconfig_load_notebook(NotebookConfigDialog *wdata, Resource *res) { UiContext *ctx = wdata->obj->ctx; ui_free(ctx, wdata->selected_notebook_name); wdata->selected_notebook_name = ui_strdup(ctx, res->nodename); + + wdata->valuechange = FALSE; } static void nbconfig_notebooklist_onselect(UiEvent *event, void *userdata) { @@ -237,6 +238,46 @@ static void nbconfig_notebooklist_onselect(UiEvent *event, void *userdata) { nbconfig_build_current_names_map(wdata, res); } +static int get_notebook_insertpos(NotebookConfigDialog *wdata, int64_t parent_id) { + CxList *nblist = wdata->tab1_notebooks->data; + size_t insert_pos = 0; + int group_index = -1; + CxIterator i = cxListIterator(nblist); + cx_foreach(Resource *, res, i) { + if(group_index >= 0) { + if(res->parent_id != parent_id) { + break; + } + } else if(res->parent_id == parent_id) { + group_index = (int)i.index; + } + insert_pos = i.index; + } + insert_pos++; + return insert_pos; +} + +static void nbconfig_group_onselect(UiEvent *event, void *userdata) { + NotebookConfigDialog *wdata = event->window; + if(wdata->valuechange || !wdata->selected_notebook) { + return; + } + + Resource *newparent = ui_list_get(wdata->tab1_groups, event->intval); + + CxList *nblist = wdata->tab1_notebooks->data; + size_t res_index = cxListFind(nblist, wdata->selected_notebook); + + cxListRemove(nblist, res_index); + int new_insert_pos = get_notebook_insertpos(wdata, newparent->resource_id); + cxListInsert(nblist, new_insert_pos, wdata->selected_notebook); + ui_list_update(wdata->tab1_notebooks); + + wdata->valuechange = TRUE; + ui_list_setselection(wdata->tab1_notebooks, new_insert_pos); + wdata->valuechange = FALSE; +} + static void nbconfig_tab_view_changed(UiEvent *event, void *userdata) { } @@ -245,6 +286,8 @@ static void add_notebook(NotebookConfigDialog *wdata, int64_t parent_id, const c NoteStore *store = note_store_get(); UiContext *ctx = wdata->obj->ctx; + nbconfig_notebook_save(wdata); + Resource *notebook = cxCalloc(store->mp->allocator, 1, sizeof(Resource)); notebook->notebook = cxCalloc(store->mp->allocator, 1, sizeof(Notebook)); notebook->parent_id = parent_id; @@ -270,26 +313,21 @@ static void add_notebook(NotebookConfigDialog *wdata, int64_t parent_id, const c } else { cxListAdd(wdata->notebooks, notebook); CxList *nblist = wdata->tab1_notebooks->data; - size_t insert_pos = 0; - int group_index = -1; - CxIterator i = cxListIterator(nblist); - cx_foreach(Resource *, res, i) { - if(group_index >= 0) { - if(res->parent_id != parent_id) { - break; - } - } else if(res->parent_id == parent_id) { - group_index = (int)i.index; - } - insert_pos = i.index; - } - insert_pos++; + size_t insert_pos = get_notebook_insertpos(wdata, parent_id); cxListInsert(nblist, insert_pos, notebook); ui_list_update(wdata->tab1_notebooks); - ui_list_update_row(wdata->tab1_groups, group_index); + + int grouplist_index = -1; + CxIterator i = cxListIterator(wdata->groups); + cx_foreach(Resource *, res, i) { + if(res->resource_id == parent_id) { + grouplist_index = (int)i.index; + } + } wdata->valuechange = TRUE; + ui_list_setselection(wdata->tab1_groups, grouplist_index); ui_list_setselection(wdata->tab1_notebooks, insert_pos); wdata->valuechange = FALSE; } @@ -307,12 +345,27 @@ static void nbconfig_notebooklist_add_group(UiEvent *event, void *userdata) { static void nbconfig_notebooklist_add_notebook(UiEvent *event, void *userdata) { NotebookConfigDialog *wdata = event->window; - Resource *parent = cxListAt(wdata->groups, 0); - if(!parent) { - fprintf(stderr, "Error: cannot create a notebook without a group\n"); - return; + int64_t parent_resource_id; + if(wdata->selected_notebook) { + if(wdata->selected_notebook->parent_id == wdata->root_resource_id) { + // The currently selected notebook is a group + // A new notebook should be created in this group + parent_resource_id = wdata->selected_notebook->resource_id; + } else { + // Create a new notebook within the same parent as the + // currently selected notebook + parent_resource_id = wdata->selected_notebook->parent_id; + } + } else { + // no selected notebook, use the first group as parent + Resource *parent = cxListAt(wdata->groups, 0); + if(!parent) { + fprintf(stderr, "Error: cannot create a notebook without a group\n"); + return; + } + parent_resource_id = parent->resource_id; } - add_notebook(wdata, parent->resource_id, "New"); + add_notebook(wdata, parent_resource_id, "New"); } static void notebook_count_children_result(UiEvent *event, int64_t num, int error, void *userdata) { @@ -488,7 +541,7 @@ void notebook_config_dialog(void) { ui_grid(obj, .columnspacing = 10, .rowspacing = 10, .fill = TRUE, .def_vfill = TRUE) { ui_rlabel(obj, .label = "Group"); - ui_dropdown(obj, .list = wdata->tab1_groups, .getvalue2 = reslist_getvalue, .getvalue2data = wdata); + ui_dropdown(obj, .list = wdata->tab1_groups, .getvalue2 = reslist_getvalue, .getvalue2data = wdata, .onactivate = nbconfig_group_onselect); ui_newline(obj); ui_rlabel(obj, .label = "Name"); diff --git a/application/nbconfig.h b/application/nbconfig.h index a24da0a..6f41974 100644 --- a/application/nbconfig.h +++ b/application/nbconfig.h @@ -61,7 +61,7 @@ typedef struct NotebookConfigDialog { Resource *selected_notebook; Repository *selected_repository; char *selected_notebook_name; - cchar *selected_repository_name; + char *selected_repository_name; // a map of all used names in the current context // (all resource names in selected_group -> parent -> children) -- 2.47.3