GTK如何设置text_view的字体

时间:2017-03-30 10:40:59

标签: gtk3

如何设置text_view的字体名称和大小?或者我必须在缓冲区或窗口设置字体信息?我是否必须创建某种样式表?

#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;
}

2 个答案:

答案 0 :(得分:2)

以下代码有效。

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;
}. 

答案 1 :(得分:0)

我知道这是2岁的问题,但是如果有人遇到任何问题。 请注意,该字体的Pango语法在Gtk + 3中已弃用。

如果要使用GtkSourceView:

#include <gtksourceview/gtksource.h>

必须单独安装并用

编译
`pkg-config --cflags --libs gtksourceview-3.0`

或使用常规的GtkTextView

 GtkCssProvider *cssProvider;
 GtkSourceView *view;
 GError *error = NULL;
 GtkStyleContext *context;

 /* new css provider */
 cssProvider = gtk_css_provider_new(); 

 view = GTK_SOURCE_VIEW(gtk_source_view_new ());

 /* widget name for css syntax */
 gtk_widget_set_name (GTK_WIDGET(view), "cssView");   

 /* load css file */
 gtk_css_provider_load_from_path (cssProvider, "main.css", &error); 

 /* get GtkStyleContext from widget   */
 context = gtk_widget_get_style_context(GTK_WIDGET(view));  

 /* finally load style provider */
 gtk_style_context_add_provider(context,    
                             GTK_STYLE_PROVIDER(cssProvider), 
                             GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);

和“ main.css”文件位于同一目录

#cssView { 
      font: 15px "Monospace";
      color: #ff0000;          /*in case you need red color*/
}

实际上,在Gtk + 3中,您可以通过非常简单的方式添加和删除CSS类:

您需要调用一个回调:

 gtk_style_context_add_class(context, "redFonts");

然后再换一个:

 gtk_style_context_remove_class(context, "redFonts");

您的“ main.css”应类似于:

#tab1Content {
    font: 15px "Monospace";
}
.redFonts{
    color: #ff0000;
}