From 791b4e6c20c832ff753bae48a699e2e075143989 Mon Sep 17 00:00:00 2001 From: Olaf Wintermann Date: Tue, 22 Apr 2025 20:25:44 +0200 Subject: [PATCH] add function for inserting bullet lists --- application/editor.h | 2 ++ application/gtk-text.c | 32 ++++++++++++++++++++++++++++++++ application/note.c | 4 ++++ application/note.h | 1 + application/window.c | 13 ++++++++++++- application/window.h | 1 + 6 files changed, 52 insertions(+), 1 deletion(-) diff --git a/application/editor.h b/application/editor.h index 1b7d888..b678633 100644 --- a/application/editor.h +++ b/application/editor.h @@ -47,6 +47,7 @@ extern "C" { #define EDITOR_STYLE_HEADING6 "heading6" #define EDITOR_STYLE_QUOTE "quote" #define EDITOR_STYLE_CODE_BLOCK "code_block" +#define EDITOR_STYLE_LIST0 "list0" #define EDITOR_STYLE_EMPHASIS "emphasis" #define EDITOR_STYLE_STRONG "strong" #define EDITOR_STYLE_CODE "code" @@ -131,6 +132,7 @@ cxmutstr editor_get_markdown(UiText *text, const CxAllocator *a); UiBool editor_set_style(UiText *text, const char *style, UiBool enabled); void editor_set_paragraph_style(UiText *text, const char *style); +void editor_insert_list(UiText *text, UiBool oredered); #if GTK_MAJOR_VERSION >= 4 GtkWidget* editor_gtk4_workaround(UiObject *obj, UiWidgetArgs args, void *userdata); diff --git a/application/gtk-text.c b/application/gtk-text.c index 8a64848..c0626b1 100644 --- a/application/gtk-text.c +++ b/application/gtk-text.c @@ -120,6 +120,7 @@ void editor_global_init() { cxMapPut(markdown_tags, EDITOR_STYLE_HEADING5, &((MDTag){"##### ", NULL})); cxMapPut(markdown_tags, EDITOR_STYLE_HEADING6, &((MDTag){"###### ", NULL})); cxMapPut(markdown_tags, EDITOR_STYLE_CODE_BLOCK, &((MDTag){" ", NULL})); + cxMapPut(markdown_tags, EDITOR_STYLE_LIST0, &((MDTag){" - ", NULL})); cxMapPut(markdown_tags, EDITOR_STYLE_STRONG, &((MDTag){"**", "**"})); cxMapPut(markdown_tags, EDITOR_STYLE_EMPHASIS, &((MDTag){"*", "*"})); cxMapPut(markdown_tags, EDITOR_STYLE_CODE, &((MDTag){"`", "`"})); @@ -747,6 +748,10 @@ static void tagstyle_link(MDActiveStyles *style, GtkTextTag *tag) { } +static void tagstyle_list0(MDActiveStyles *style, GtkTextTag *tag) { + +} + void init_tagtable(GtkTextTagTable *table) { printf("init_tagtable\n"); GtkTextTag *tag; @@ -814,6 +819,11 @@ void init_tagtable(GtkTextTagTable *table) { g_object_set(tag, "foreground", "blue", NULL); g_object_set_data(G_OBJECT(tag), "set_style", (void*)tagstyle_link); gtk_text_tag_table_add(table, tag); + + tag = gtk_text_tag_new(EDITOR_STYLE_LIST0); + g_object_set(tag, "left-margin", 60, "right-margin", 60, "ident", 60, NULL); + g_object_set_data(G_OBJECT(tag), "set_style", (void*)tagstyle_list0); + gtk_text_tag_table_add(table, tag); } /* @@ -1164,3 +1174,25 @@ void editor_set_paragraph_style(UiText *text, const char *style) { gtk_text_iter_forward_line(&pos); } } + +void editor_insert_list(UiText *text, UiBool oredered) { + GtkTextBuffer *buffer = text->data1; + NoteEditor *editor = g_object_get_data(text->obj, "editor"); + + GtkTextMark *cursor = gtk_text_buffer_get_insert(buffer); + if(!cursor) { + fprintf(stderr, "Error: no insert mark\n"); + return; + } + GtkTextIter start, end; + gtk_text_buffer_get_iter_at_mark(buffer, &end, cursor); + + GtkTextMark *start_mark = gtk_text_buffer_create_mark(buffer, NULL, &end, TRUE); + + gtk_text_buffer_insert(buffer, &end, "•", -1); + gtk_text_buffer_get_iter_at_mark(buffer, &start, start_mark); + + gtk_text_buffer_apply_tag_by_name(buffer, EDITOR_STYLE_LIST0, &start, &end); + + gtk_text_buffer_delete_mark(buffer, start_mark); +} diff --git a/application/note.c b/application/note.c index 6bae7f0..e387272 100644 --- a/application/note.c +++ b/application/note.c @@ -255,6 +255,10 @@ void note_text_style_set_code(NoteModel *note, UiBool enabled) { } } +void note_insert_list(NoteModel *note, UiBool ordered) { + editor_insert_list(note->text, ordered); +} + void note_update_title(NotebookModel *notebook, Resource *note) { diff --git a/application/note.h b/application/note.h index d560d8f..f3024fa 100644 --- a/application/note.h +++ b/application/note.h @@ -60,6 +60,7 @@ void note_text_style_set_strong(NoteModel *note, UiBool enabled); void note_text_style_set_emphasis(NoteModel *note, UiBool enabled); void note_text_style_set_underline(NoteModel *note, UiBool enabled); void note_text_style_set_code(NoteModel *note, UiBool enabled); +void note_insert_list(NoteModel *note, UiBool ordered); void note_update_title(NotebookModel *notebook, Resource *note); diff --git a/application/window.c b/application/window.c index 757e412..fcc1dd3 100644 --- a/application/window.c +++ b/application/window.c @@ -83,7 +83,7 @@ void window_create() { ui_togglebutton(obj, .icon = "format-text-italic", .varname = "note_textnote_emphasis", .onchange = action_textnote_style_emphasis); ui_togglebutton(obj, .icon = "format-text-underline", .varname = "note_textnote_underline", .onchange = action_textnote_style_underline); ui_togglebutton(obj, .label = "code", .varname = "note_textnote_code", .onchange = action_textnote_style_code); - ui_button(obj, .icon = "view-list-bullet"); + ui_button(obj, .icon = "view-list-bullet", .onclick = action_textnote_insertlist); ui_button(obj, .icon = "view-list-ordered"); ui_button(obj, .icon = "insert-image"); ui_button(obj, .icon = "insert-link"); @@ -386,3 +386,14 @@ void action_textnote_paragraph(UiEvent *event, void *userdata) { note_set_paragraph_type(notebook->current_note->model, event->intval); } } + +void action_textnote_insertlist(UiEvent *event, void *userdata) { + if(event->set) { + return; + } + MainWindow *window = event->window; + NotebookModel *notebook = window->current_notebook; + if(notebook && notebook->current_note && notebook->current_note->model) { + note_insert_list(notebook->current_note->model, FALSE); + } +} diff --git a/application/window.h b/application/window.h index 1e32b55..a5bde01 100644 --- a/application/window.h +++ b/application/window.h @@ -63,6 +63,7 @@ void action_textnote_style_strong(UiEvent *event, void *userdata); void action_textnote_style_emphasis(UiEvent *event, void *userdata); void action_textnote_style_underline(UiEvent *event, void *userdata); void action_textnote_style_code(UiEvent *event, void *userdata); +void action_textnote_insertlist(UiEvent *event, void *userdata); -- 2.43.5