]> uap-core.de Git - note.git/commitdiff
add function for inserting bullet lists
authorOlaf Wintermann <olaf.wintermann@gmail.com>
Tue, 22 Apr 2025 18:25:44 +0000 (20:25 +0200)
committerOlaf Wintermann <olaf.wintermann@gmail.com>
Tue, 22 Apr 2025 18:25:44 +0000 (20:25 +0200)
application/editor.h
application/gtk-text.c
application/note.c
application/note.h
application/window.c
application/window.h

index 1b7d888c32f488abc02a42a0655349b2d1ecb335..b678633ab451deb079a7ab0988fe7a4a40d245f2 100644 (file)
@@ -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);
index 8a648484b25aa6f1cf52909bb460b8a1b0628347..c0626b13280c8cb0b82bfe6bedc4f7157e932cb3 100644 (file)
@@ -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);
+}
index 6bae7f07172ca29a4608b1034a6b056a767bf5e4..e3872726b25df6ef209525c193f0b3c9f4358b15 100644 (file)
@@ -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) {
index d560d8f4b1001bee2b7d8dfe00b0418b9e331932..f3024fa8ae87ce21223048a902add95b6efb2054 100644 (file)
@@ -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);
 
index 757e412bb89304cebb1a92eaee995b592d8363c5..fcc1dd363942928249a4c108bcc1b40f2817e01d 100644 (file)
@@ -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);
+    }
+}
index 1e32b55077db4d3c6435a3093bd8c1914f75e02f..a5bde01ac9c4275a47bc83e9d7b560a9fdec132d 100644 (file)
@@ -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);