]> uap-core.de Git - note.git/commitdiff
add workaround for missing gtk4 resize/allocation signals
authorOlaf Wintermann <olaf.wintermann@gmail.com>
Mon, 21 Apr 2025 14:13:09 +0000 (16:13 +0200)
committerOlaf Wintermann <olaf.wintermann@gmail.com>
Mon, 21 Apr 2025 14:13:09 +0000 (16:13 +0200)
application/editor.h
application/gtk-text.c
application/gtk-text.h
application/window.c

index 690589d20973e5a9463246f3142150e551b8ff42..1b7d888c32f488abc02a42a0655349b2d1ecb335 100644 (file)
@@ -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
index 66b1eab8838eb6c4efb513a9ec3c115c36390bab..35dd2e1d071dd0dfbe8174356730913da377a1a5 100644 (file)
@@ -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) {
index 60de7bfcc1a416de7399ff26b8b6ac720401c93b..c6eeaead4f1c4ab5edfece0afc4a160c99c42fce 100644 (file)
@@ -39,6 +39,7 @@ extern "C" {
 typedef struct NoteEditor {
     UiObject *obj;
     GtkWidget *textview;
+    int width;
 } NoteEditor;
     
 typedef struct MDTag {
index bfc9b8defa29bb5bb295e8b232ef8ac4c574ca83..3fa1bd3c50bf3a3c72422e738850bfd886e5476c 100644 (file)
@@ -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);