GtkSourceView
已经有缓冲区了。以与获取常规文本缓冲区相同的方式获取它:
GtkSourceBuffer *buffer = GTK_SOURCE_BUFFER(gtk_text_view_get_buffer(GTK_TEXT_VIEW(source_view)));
这是因为GtkSourceView
它是 的子类GtkTextView
,所以文本视图函数也可以在它上面工作。
编辑:
这是一个有效的程序和空地文件。
程序:
#include <gtk/gtk.h>
#include <gtksourceview/gtksourceview.h>
#include <gtksourceview/gtksourcebuffer.h>
#include <gtksourceview/gtksourcelanguagemanager.h>
int
main(int argc, char **argv)
{
gtk_init(&argc, &argv);
GtkBuilder *builder = gtk_builder_new();
if(gtk_builder_add_from_file(builder, "sourceview.ui", NULL) == 0)
g_error("In real code, you would handle an error here");
gtk_builder_connect_signals(builder, NULL);
GtkWidget *window = GTK_WIDGET(gtk_builder_get_object(builder, "window"));
GtkSourceView *sourceview = GTK_SOURCE_VIEW(gtk_builder_get_object(builder, "gtk_sourceview"));
/* Get the buffer */
GtkSourceBuffer *sourcebuffer = GTK_SOURCE_BUFFER(gtk_text_view_get_buffer(GTK_TEXT_VIEW(sourceview)));
/* Do stuff to the buffer, to prove we've really got the GtkSourceBuffer */
gtk_text_buffer_set_text(GTK_TEXT_BUFFER(sourcebuffer),
"def hello():\n\tprint 'This should be highlighted as Python'\n", -1);
GtkSourceLanguageManager *manager = gtk_source_language_manager_get_default();
GtkSourceLanguage *python = gtk_source_language_manager_get_language(manager, "python");
gtk_source_buffer_set_language(sourcebuffer, python);
/* Okay, that should prove it, now run the program */
gtk_widget_show_all(window);
gtk_main();
return 0;
}
格莱德文件:
<?xml version="1.0" encoding="UTF-8"?>
<interface>
<!-- interface-requires gtksourceview 3.0 -->
<requires lib="gtk+" version="2.20"/>
<!-- interface-naming-policy project-wide -->
<object class="GtkSourceBuffer" id="gtksourcebuffer">
<property name="max_undo_levels">0</property>
</object>
<object class="GtkWindow" id="window">
<property name="default_width">300</property>
<property name="default_height">300</property>
<signal name="destroy" handler="gtk_main_quit" swapped="no"/>
<child>
<object class="GtkScrolledWindow" id="scrolledwindow">
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="hscrollbar_policy">automatic</property>
<property name="vscrollbar_policy">automatic</property>
<child>
<object class="GtkSourceView" id="gtk_sourceview">
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="left_margin">2</property>
<property name="right_margin">2</property>
<property name="buffer">gtksourcebuffer</property>
<property name="tab_width">4</property>
<property name="auto_indent">True</property>
<property name="indent_on_tab">False</property>
</object>
</child>
</object>
</child>
</object>
</interface>