From d5c796692dcd066933dbc82b8809d180a54f1669 Mon Sep 17 00:00:00 2001 From: Olaf Wintermann Date: Mon, 21 Apr 2025 16:13:09 +0200 Subject: [PATCH] add workaround for missing gtk4 resize/allocation signals --- application/editor.h | 4 ++++ application/gtk-text.c | 48 +++++++++++++++++++++++++++++++++++++++++- application/gtk-text.h | 1 + application/window.c | 3 +++ 4 files changed, 55 insertions(+), 1 deletion(-) diff --git a/application/editor.h b/application/editor.h index 690589d..1b7d888 100644 --- a/application/editor.h +++ b/application/editor.h @@ -132,6 +132,10 @@ 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); +#if GTK_MAJOR_VERSION >= 4 +GtkWidget* editor_gtk4_workaround(UiObject *obj, UiWidgetArgs args, void *userdata); +#endif + #ifdef __cplusplus } #endif diff --git a/application/gtk-text.c b/application/gtk-text.c index 66b1eab..35dd2e1 100644 --- a/application/gtk-text.c +++ b/application/gtk-text.c @@ -84,6 +84,8 @@ static gboolean editor_drop_cb( double y, NoteEditor *editor); +static void editor_update_width(NoteEditor *editor); + #else static gboolean editor_textview_event_after(GtkWidget *textview, GdkEvent *ev, NoteEditor *editor); @@ -98,8 +100,14 @@ static void editor_drop_cb( guint time, NoteEditor *editor); +static void editor_update_width(NoteEditor *editor); + #endif +static void editor_realize(GtkWidget *unused, NoteEditor *editor) { + editor_update_width(editor); +} + void editor_global_init() { markdown_tags = cxHashMapCreateSimple(sizeof(MDTag)); @@ -128,7 +136,7 @@ void editor_init_textview(UiObject *obj, UIWIDGET textview) { g_object_set_data(G_OBJECT(textview), "editor", editor); -#if GTK_CHECK_VERSION(4, 0, 0) +#if GTK_CHECK_VERSION(4, 0, 0) // gesture event controller is used for handling clicks on links GtkEventController *controller = GTK_EVENT_CONTROLLER(gtk_gesture_click_new()); g_signal_connect(controller, "released", G_CALLBACK(editor_button_released_cb), editor); @@ -163,6 +171,13 @@ void editor_init_textview(UiObject *obj, UIWIDGET textview) { #endif g_signal_connect(textview, "notify::buffer", G_CALLBACK(editor_set_buffer_cb), editor); + g_signal_connect(textview, "realize", G_CALLBACK(editor_realize), editor); + +} + +static void editor_resize(NoteEditor *editor) { + editor_update_width(editor); + printf("editor resize: %d\n", editor->width); } @@ -407,6 +422,27 @@ static gboolean editor_dnd_add_file(NoteEditor *editor, GFile *file) { #if GTK_CHECK_VERSION(4, 0, 0) +static void editor_gtk4_resize( + GtkDrawingArea *self, + gint width, + gint height, + gpointer userdata) +{ + MainWindow *wdata = userdata; + NoteEditor *editor = g_object_get_data(G_OBJECT(ui_textarea_gettextwidget(wdata->textview)), "editor"); + if(editor) { + editor_resize(editor); + } +} + +GtkWidget* editor_gtk4_workaround(UiObject *obj, UiWidgetArgs args, void *userdata) { + GtkWidget *drawingarea = gtk_drawing_area_new(); + //gtk_widget_add_css_class(drawingarea, "ui_test"); + gtk_widget_set_size_request(drawingarea, 10, 1); + g_signal_connect(drawingarea, "resize", G_CALLBACK(editor_gtk4_resize), userdata); + return drawingarea; +} + /* * handle clicks inside the textview and follow links when possible */ @@ -469,6 +505,10 @@ static gboolean editor_drop_cb( return success; } +static void editor_update_width(NoteEditor *editor) { + editor->width = gtk_widget_get_width(gtk_widget_get_parent(editor->textview)); +} + #else static void editor_drop_cb( @@ -530,6 +570,12 @@ static gboolean editor_textview_event_after(GtkWidget *textview, GdkEvent *ev, N return TRUE; } +static void editor_update_width(NoteEditor *editor) { + GtkAllocation allocation; + gtk_widget_get_allocated_size(gtk_widget_get_parent(editor->textview), &allocation, NULL); + editor->width = allocation.width; +} + #endif void editor_try_follow_link(NoteEditor *editor, GtkTextIter *pos) { diff --git a/application/gtk-text.h b/application/gtk-text.h index 60de7bf..c6eeaea 100644 --- a/application/gtk-text.h +++ b/application/gtk-text.h @@ -39,6 +39,7 @@ extern "C" { typedef struct NoteEditor { UiObject *obj; GtkWidget *textview; + int width; } NoteEditor; typedef struct MDTag { diff --git a/application/window.c b/application/window.c index bfc9b8d..3fa1bd3 100644 --- a/application/window.c +++ b/application/window.c @@ -96,6 +96,9 @@ void window_create() { //ui_widget_set_size(sw, -1, 120); } ui_widget_set_size(wdata->attachments, -1, 120); +#if GTK_MAJOR_VERSION >= 4 + ui_customwidget(obj, editor_gtk4_workaround, wdata, .hfill = TRUE); +#endif //ui_set_visible(wdata->attachments, FALSE); //ui_widget_set_groups(obj->ctx, wdata->attachments, (ui_enablefunc)ui_set_visible, APP_STATE_NOTE_HAS_ATTACHMENTS, -1); wdata->textview = ui_textarea(obj, .varname = "note_text", .vfill = TRUE, .hfill = TRUE, .hexpand = TRUE, .vexpand = TRUE, .colspan = 2, .groups = UI_GROUPS(APP_STATE_NOTE_SELECTED), .fill = UI_ON); -- 2.43.5