How to apply style using GtkSourceView library

故事扮演 提交于 2019-12-11 07:38:50

问题


I'm trying to style a GtkSourceView object in particular I'd like to change the color of line numbers. I'm reading https://developer.gnome.org/gtksourceview/stable/style-reference.html. I'm able to change text but not line-numbers. The document says:

line-numbers : Text and background colors for the left margin, on which line numbers are drawn.

This is the source:

#include <gtk/gtk.h>
#include <gtksourceview/gtksource.h>

int main (int argc,
char *argv[])
{
    GtkWidget *window, *scrolled_win, *textview;
    GtkWidget *vbox, *menubar, *fileMenu, *fileMi, *quit_item;

    gtk_init (&argc, &argv);
    window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
    gtk_window_maximize(GTK_WINDOW (window));
    vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 12);
    gtk_container_add(GTK_CONTAINER(window), vbox);

    gtk_window_set_title (GTK_WINDOW (window), "Text Views");
    gtk_container_set_border_width (GTK_CONTAINER (window), 10);

    textview = gtk_source_view_new();
    menubar = gtk_menu_bar_new();
    fileMenu = gtk_menu_new();

    fileMi = gtk_menu_item_new_with_label("File");

    quit_item = gtk_menu_item_new_with_label("Quit");

    gtk_widget_set_hexpand(textview, TRUE);
    gtk_widget_set_vexpand(textview, TRUE);

    GtkCssProvider *provider = gtk_css_provider_new ();
    gtk_css_provider_load_from_path (provider,
    "gtk-widgets.css", NULL);

    GtkStyleContext *context;
    context = gtk_widget_get_style_context(textview);
    gtk_style_context_add_provider (context,
                                    GTK_STYLE_PROVIDER(provider),
                                    GTK_STYLE_PROVIDER_PRIORITY_USER);

    gtk_menu_item_set_submenu(GTK_MENU_ITEM(fileMi), fileMenu);
    gtk_menu_shell_append(GTK_MENU_SHELL(fileMenu), quit_item);
    gtk_menu_shell_append(GTK_MENU_SHELL(menubar), fileMi);
    gtk_box_pack_start(GTK_BOX(vbox), menubar, FALSE, FALSE, 0);

    scrolled_win = gtk_scrolled_window_new (NULL, NULL);
    gtk_container_add(GTK_CONTAINER(vbox), scrolled_win);
    gtk_container_add (GTK_CONTAINER (scrolled_win), textview);

    gtk_source_view_set_show_line_numbers (GTK_SOURCE_VIEW(textview), TRUE);
    gtk_widget_show_all (window);

    g_signal_connect(G_OBJECT(window), "destroy",
        G_CALLBACK(gtk_main_quit), NULL);

    g_signal_connect(G_OBJECT(quit_item), "activate",
        G_CALLBACK(gtk_main_quit), NULL);

    gtk_main();
    return 0;
}

This is the CSS file:

text {
    background-color: #002b36;
    color: #c7a21d;
}

line-numbers {
    background-color: red;
    color:red;
}

current-line-number {
    background-color: red;
    color:red;
}

selection{
    color:yellow;
}

What am I doing wrong?

来源:https://stackoverflow.com/questions/58876594/how-to-apply-style-using-gtksourceview-library

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!