const GtkTextIter *end,
NoteEditor *editor);
+static void edit_insert_text_cb(
+ GtkTextBuffer *buffer,
+ const GtkTextIter *location,
+ gchar *text,
+ gint len,
+ NoteEditor *editor);
+
#if GTK_CHECK_VERSION(4, 0, 0)
static void editor_button_released_cb(
GtkGestureClick *gesture,
}
+static void editor_reset_tmp_styles(NoteEditor *editor) {
+ editor->disable_bold = FALSE;
+ editor->disable_underline = FALSE;
+ editor->disable_italic = FALSE;
+ editor->disable_code = FALSE;
+}
+
static void editor_resize(NoteEditor *editor) {
editor_update_width(editor);
}
} else {
GtkTextMark *mark = gtk_text_buffer_get_insert(buffer);
gtk_text_buffer_get_iter_at_mark(buffer, &pos, mark);
+ gtk_text_iter_backward_char(&pos);
}
MDActiveStyles styles = { 0 };
}
tags = tags->next;
}
+ g_slist_free(tags);
NoteModel *note = notemodel_current(editor->obj);
if(note) {
note_update_current_style(note, &styles);
}
+
+ editor_reset_tmp_styles(editor);
}
}
editor_update_width(editor);
+ editor_reset_tmp_styles(editor);
}
static void em_remove_anchor(BufferEmbeddedObjects *em, GtkTextChildAnchor *anchor) {
}
+static void edit_insert_text_cb(
+ GtkTextBuffer *buffer,
+ const GtkTextIter *location,
+ gchar *text,
+ gint len,
+ NoteEditor *editor)
+{
+ GtkTextIter begin = *location;
+ GtkTextIter end = begin;
+
+ if(!gtk_text_iter_backward_chars(&begin, len)) {
+ return; // should not happen
+ }
+
+ GtkTextIter prev = begin;
+ if(gtk_text_iter_backward_char(&prev)) {
+ // a previous position exists, apply tags from it
+ GSList *tags = gtk_text_iter_get_tags(&prev);
+ while(tags) {
+ GtkTextTag *tag = tags->data;
+ GValue name_value = G_VALUE_INIT;
+ g_object_get_property(G_OBJECT(tag), "name", &name_value);
+ const char *name = g_value_get_string(&name_value);
+ //printf("apply tag: %s\n", name);
+ if(name && name[0] == '_') {
+ // non-paragraph styles start with an underscore
+ // we don't need to apply paragraph styles, the inserted
+ // text will already be inside the paragraph bounds
+
+ // check if the style is temporarily disabled
+ gboolean style_disabled =
+ (editor->disable_bold && !strcmp(name, EDITOR_STYLE_STRONG)) ||
+ (editor->disable_italic && !strcmp(name, EDITOR_STYLE_EMPHASIS)) ||
+ (editor->disable_code && !strcmp(name, EDITOR_STYLE_CODE));
+ if(!style_disabled) {
+ gtk_text_buffer_apply_tag(buffer, tags->data, &begin, &end);
+ }
+ }
+ g_value_unset(&name_value);
+
+ tags = tags->next;
+ }
+ g_slist_free(tags);
+ }
+}
+
static gboolean path_is_image_file(cxstring path) {
if(path.length == 0) {
return FALSE;
}
tags = tags->next;
}
+ g_slist_free(tags);
}
void editor_update_embedded_widgets(NoteEditor *editor) {
if(editor) {
g_signal_connect(buf, "mark-set", G_CALLBACK(editor_set_cursor_cb), editor);
g_signal_connect(buf, "delete-range", G_CALLBACK(editor_delete_range_cb), editor);
+ g_signal_connect_after(buf, "insert-text", G_CALLBACK(edit_insert_text_cb), editor);
}
BufferEmbeddedObjects *embedded_objects = malloc(sizeof(BufferEmbeddedObjects));
fprintf(stderr, "Error: editor_set_style: no text buffer\n");
return FALSE;
}
+ NoteEditor *editor = g_object_get_data(text->obj, "editor");
+ if(!editor) {
+ fprintf(stderr, "Error: editor_set_style: no editor\n");
+ return FALSE;
+ }
if(!gtk_text_buffer_get_has_selection(buffer)) {
- return FALSE;
+ if(!strcmp(style, EDITOR_STYLE_STRONG)) {
+ editor->disable_bold = !enabled;
+ } else if(!strcmp(style, EDITOR_STYLE_EMPHASIS)) {
+ editor->disable_italic = !enabled;
+ } else if(!strcmp(style, EDITOR_STYLE_CODE)) {
+ editor->disable_code = !enabled;
+ }
+ gtk_widget_grab_focus(editor->textview);
+ return TRUE;
}
GtkTextIter begin, end;
gtk_text_buffer_get_selection_bounds(buffer, &begin, &end);
fprintf(stderr, "Error: tag %s not found\n", style);
}
}
-
+ gtk_widget_grab_focus(editor->textview);
return TRUE;
}