Gtk 3.22 Textview not showing margin and font color

My friend today showed my following code:

#include <gtk/gtk.h>
#include <gdk/gdk.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <stdbool.h>

static GtkWidget *window;
static GtkWidget* layoutGrid;
static GtkWidget* textLog;
static GtkTextBuffer* bufferLog;
static GtkCssProvider* cssProviderLog;
static GtkStyleContext* context;

gboolean wndDeleteEventHandler(__attribute__((unused)) GtkWidget* widget,
                                __attribute__((unused)) GdkEvent* event,
                                __attribute__((unused)) gpointer user_data)
{
    gtk_main_quit();
    
    return TRUE;
}

int main(int argc, char** argv)
{
    gtk_init(&argc , &argv);
    window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
    gtk_window_set_default_size(GTK_WINDOW(window), 640, 480);
    gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
    g_signal_connect(window, "delete-event", G_CALLBACK(wndDeleteEventHandler), NULL);

    layoutGrid = gtk_grid_new();
    gtk_widget_set_hexpand(layoutGrid, TRUE);
    gtk_widget_set_vexpand(layoutGrid, TRUE);
    gtk_container_add(GTK_CONTAINER(window), layoutGrid);

    textLog = gtk_text_view_new();
    gtk_widget_set_name(textLog, "textlog");
    bufferLog = gtk_text_view_get_buffer(GTK_TEXT_VIEW (textLog));
    cssProviderLog = gtk_css_provider_new();
    gtk_css_provider_load_from_data(cssProviderLog,
                                    "textview {"
                                    "font-family: serif;"
                                    "font-size: 30px;"
                                    "margin-top: 10px;"
                                    "margin-bottom: 10px;"
                                    "color: green;"
                                    "}",-1, NULL);
    context = gtk_widget_get_style_context(textLog);
    gtk_style_context_add_provider(context,
                                GTK_STYLE_PROVIDER(cssProviderLog),
                                GTK_STYLE_PROVIDER_PRIORITY_USER);
    gtk_text_buffer_set_text(bufferLog, "Hello, this is some text in log buffer", -1);
    gtk_widget_set_hexpand(textLog, TRUE);
    gtk_widget_set_vexpand(textLog, TRUE);
    // g_object_set(textLog, "margin-left", 20, NULL);
    // g_object_set(textLog, "margin-top", 20, NULL);
    // g_object_set(textLog, "margin-bottom", 20, NULL);
    // g_object_set(textLog, "margin-right", 20, NULL);
    gtk_grid_attach(GTK_GRID(layoutGrid), textLog, 0, 0, 1, 1);

    gtk_widget_show_all(window);
    gtk_main();
}

which was compiled with

gcc main.c `pkg-config --cflags --libs gtk+-3.0 fribidi` -o test

When running that, everything goes fine, without errors, except for that the margins and the font color added with a GtkCssProvider are not showing, whereas the correct font family and size is set. According to the Gtk docs, the CSS margin property is supported.
Setting the margins with g_object_set works. I also tested the example on my machine with Gtk 3.22 and Gtk 3.24, and the error is still the same. We are both running Ubuntu 18.04 LTS.
Is there anything my friend forgot in the code, or is this a bug ?

You need something like

textview * { }

as the selector if you want those to apply to everything in the textview widget.

See the textview documentation for details about the CSS nodes.

Though I’m not sure if that will apply the margins, I’ve had issues with those in the past, but it’ll definitely apply the color.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.