Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GTK How to set font for a text_view

Tags:

gtk3

How can I set the font name and size for a text_view? Or do I have to set the font information at the buffer or at the window? Do I have to create some sort of style-sheet?

#include <gtk/gtk.h>

int main(int argc, char *argv[]) {

    GtkWidget *window;
    GtkWidget *view;
    GtkTextBuffer *buffer;

    gtk_init(&argc, &argv);

    view = gtk_text_view_new();

    buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(view));

    window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
    g_signal_connect_swapped(G_OBJECT(window), "destroy", G_CALLBACK(gtk_main_quit), NULL);
    gtk_window_set_default_size(GTK_WINDOW(window), 350, 300);

    gtk_container_add(GTK_CONTAINER(window), view);
    gtk_widget_show_all(window);

    gtk_main();

    return 0;
}
like image 570
Graeme Jensz Avatar asked Mar 30 '17 10:03

Graeme Jensz


1 Answers

The following code works.

my.c

#include <gtk/gtk.h>

int main(int argc, char *argv[]) {
    GtkWidget *view;
    GtkTextBuffer *buffer;
    GtkWidget *window;

    GdkDisplay *display;
    GdkScreen *screen;
    GtkCssProvider *provider;

    GError *error;

    gtk_init(&argc, &argv);

    view = gtk_text_view_new();
    buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(view));

    display = gdk_display_get_default ();
    screen = gdk_display_get_default_screen (display);
    provider = gtk_css_provider_new();
    gtk_style_context_add_provider_for_screen (screen, GTK_STYLE_PROVIDER (provider), GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);

    error = NULL;
    gtk_css_provider_load_from_file (provider, g_file_new_for_path("my.css"), &error);

    window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
    g_signal_connect(G_OBJECT(window), "destroy", G_CALLBACK(gtk_main_quit), NULL);
    gtk_window_set_default_size(GTK_WINDOW(window), 350, 300);

    gtk_container_add(GTK_CONTAINER(window), view);
    gtk_widget_show_all(window);

    gtk_main();

    return 0;
}

my.css

GtkTextView
{
    font:Monospace 10;
}. 
like image 81
Graeme Jensz Avatar answered Oct 05 '22 06:10

Graeme Jensz