#if GTK_MAJOR_VERSION >= 4
-GtkWidget* embedded_image_create(GdkPixbuf *pix) {
- GtkWidget *grid = gtk_grid_new();
- gtk_widget_add_css_class(grid, "ui_test"); // TODO: replace with new class
-
- GdkTexture *texture = gdk_texture_new_for_pixbuf(pix);
- GtkWidget *picture = gtk_picture_new_for_paintable(GDK_PAINTABLE(texture));
+static void embedded_image_adjust_size(EmbeddedWidget *em, int max_width) {
+ GtkWidget *picture = em->wdata1;
+ GdkTexture *texture = em->wdata2;
double width = gdk_texture_get_width(texture);
double height = gdk_texture_get_height(texture);
- if(width > EDITOR_IMAGE_MAX_WIDTH) {
- height = height * (EDITOR_IMAGE_MAX_WIDTH / width);
- width = EDITOR_IMAGE_MAX_WIDTH;
+ printf("max width: %d\n", max_width);
+ if(width > max_width) {
+ height = height * (max_width / width);
+ width = max_width;
}
gtk_widget_set_size_request(picture, width, height);
+}
+
+GtkWidget* embedded_image_create(EmbeddedWidget *em, GdkPixbuf *pix) {
+ GtkWidget *grid = gtk_grid_new();
+ gtk_widget_add_css_class(grid, "ui_test"); // TODO: replace with new class
+
+ GdkTexture *texture = gdk_texture_new_for_pixbuf(pix);
+ GtkWidget *picture = gtk_picture_new_for_paintable(GDK_PAINTABLE(texture));
gtk_grid_attach(GTK_GRID(grid), picture, 0, 0, 1, 1);
+ em->wdata1 = picture;
+ em->wdata2 = texture;
+ em->adjust_size = embedded_image_adjust_size;
return grid;
}
#else
-GtkWidget* embedded_image_create(GdkPixbuf *pix) {
+static void embedded_image_adjust_size(EmbeddedWidget *em, int max_width) {
+ GtkWidget *image = em->wdata1;
+ GdkPixmap *pix = em->wdata2;
+
+ double width = gdk_pixbuf_get_width(pix);
+ double height = gdk_pixbuf_get_height(pix);
+
+ if(width > max_width) {
+ height = height * (max_width / width);
+ width = max_width;
+ }
+
+ gtk_widget_set_size_request(picture, width, height);
+}
+
+GtkWidget* embedded_image_create(EmbeddedWidget *em, GdkPixbuf *pix) {
GtkWidget *image = gtk_image_new_from_pixbuf(pix);
int width = gdk_pixbuf_get_width(pix);
int height = gdk_pixbuf_get_height(pix);
gtk_widget_set_size_request(image, width, height);
+ em->wdata1 = image;
+ em->wdata2 = pix;
+ em->adjust_size = embedded_image_adjust_size;
+
return image;
}
double y,
NoteEditor *editor);
-static void editor_update_width(NoteEditor *editor);
-
#else
static gboolean editor_textview_event_after(GtkWidget *textview, GdkEvent *ev, NoteEditor *editor);
#endif
+// different implementations for gtk versions
+static void editor_update_width(NoteEditor *editor);
+
static void editor_realize(GtkWidget *unused, NoteEditor *editor) {
editor_update_width(editor);
}
static void editor_resize(NoteEditor *editor) {
editor_update_width(editor);
- printf("editor resize: %d\n", editor->width);
}
#endif
}
}
+
+ editor_update_width(editor);
}
static void em_remove_anchor(BufferEmbeddedObjects *em, GtkTextChildAnchor *anchor) {
}
GtkTextChildAnchor *anchor = gtk_text_buffer_create_child_anchor(buffer, iter);
- GtkWidget *image = embedded_image_create(pixbuf);
+
+ // remember widget and store a reference in the textbuffer
+ // TODO: we need a cleanup strategy
+ EmbeddedWidget *em = malloc(sizeof(EmbeddedWidget));
+ memset(em, 0, sizeof(EmbeddedWidget));
+
+ GtkWidget *image = embedded_image_create(em, pixbuf);
gtk_text_view_add_child_at_anchor(textview, image, anchor);
#if GTK_MAJOR_VERSION < 4
gtk_widget_show_all(image);
#endif
- // remember widget and store a reference in the textbuffer
- // TODO: we need a cleanup strategy
- EmbeddedWidget *em = malloc(sizeof(EmbeddedWidget));
em->widget = image;
em->anchor = anchor;
em->data1 = attachment;
- em->data2 = NULL;
em->serialize = md_serialize_image;
g_object_ref(image);
g_object_ref(anchor);
g_object_set_data(G_OBJECT(anchor), "em", em);
+ int em_width = editor->width - EM_WIDGET_HPADDING;
+ if(em->adjust_size && em_width > 0) {
+ em->adjust_size(em, em_width);
+ }
+
BufferEmbeddedObjects *embedded_objects = g_object_get_data(G_OBJECT(buffer), "embedded");
cxListAdd(embedded_objects->objects, em);
}
}
}
+static void editor_gtk4_draw_func(
+ GtkDrawingArea *drawing_area,
+ cairo_t *cr,
+ int width,
+ int height,
+ gpointer userdata)
+{
+ MainWindow *wdata = userdata;
+ NoteEditor *editor = g_object_get_data(G_OBJECT(ui_textarea_gettextwidget(wdata->textview)), "editor");
+ if(editor && editor->width == 0) {
+ 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);
+ gtk_drawing_area_set_draw_func(GTK_DRAWING_AREA(drawingarea), editor_gtk4_draw_func, userdata, NULL);
g_signal_connect(drawingarea, "resize", G_CALLBACK(editor_gtk4_resize), userdata);
return drawingarea;
}
static void editor_update_width(NoteEditor *editor) {
editor->width = gtk_widget_get_width(gtk_widget_get_parent(editor->textview));
+ editor_update_embedded_widgets(editor);
}
#else
}
}
+void editor_update_embedded_widgets(NoteEditor *editor) {
+ GtkTextView *textview = GTK_TEXT_VIEW(editor->textview);
+ GtkTextBuffer *buffer = gtk_text_view_get_buffer(textview);
+
+ BufferEmbeddedObjects *embedded_objects = g_object_get_data(G_OBJECT(buffer), "embedded");
+ int em_width = editor->width - EM_WIDGET_HPADDING;
+ if(embedded_objects && em_width > 0) {
+ CxIterator i = cxListIterator(embedded_objects->objects);
+ cx_foreach(EmbeddedWidget *, em, i) {
+ if(em->adjust_size) {
+ em->adjust_size(em, em_width);
+ }
+ }
+ }
+}
+
static void buffer_embedded_objects_free(BufferEmbeddedObjects *em) {
cxListFree(em->objects);
free(em);