C Gtk+2 Glade3:如何将 SourceBuffer 设置为从 Glade 加载的 GtkSourceView?

发布于 2024-10-19 19:54:38 字数 1323 浏览 2 评论 0原文

我正在尝试将 .c 文件加载到 GtkSourceView 小部件中。

使用C语言,使用Gtk+ 2.22.1和Glade 3.6.7和GtkSourceView 2.10.1。

我注意到在 Glade UI 中我只能配置一个 SourceView 来保存 TextBuffer;我没有找到 SourceBuffer 组件。因此,我在 Glade 中创建了一个没有默认缓冲区的 SourceView。现在我想将 SourceBuffer 设置为我的 SourceView 组件的缓冲区。

gtk_source_view_new_with_buffer() 是我发现将 SourceBuffer 附加到 SourceView 的唯一方法。问题是这个函数正在创建一个 SourceView 并且我想将一个 SourceBuffer 附加到已经创建的使用 Glade 构建的 SourceView 上。我怎样才能做到这一点?

我没有粘贴任何代码,因为没有代码可显示。我刚刚创建了一个 Glade 文件,其中包含一些 UI 组件以及 ID 为 gtk_sourceviewSourceView-2 组件。

在 C 文件中,我使用以下命令获取 SourceView 组件,

GtkSourceView *sourceview = GTK_WIDGET (gtk_builder_get_object (builder, "gtk_sourceview"));

缺少的是如何创建 SourceBuffer 并将其附加到 SourceView 组件。

谢谢!

更新:我尝试使用:

GtkSourceBuffer *sourcebuffer = GTK_SOURCE_BUFFER(gtk_text_view_get_buffer(GTK_TEXT_VIEW(sourceview)));

但我收到以下断言错误:

(tour_de_gtk:13884): Gtk-CRITICAL **: IA__gtk_text_view_get_buffer: assertion `GTK_IS_TEXT_VIEW (text_view)' failed

我可以做什么来解决这个问题?

谢谢!

I'm trying to load a .c file into a GtkSourceView widget.

Using C Language, with Gtk+ 2.22.1 and Glade 3.6.7 and GtkSourceView 2.10.1.

I noticed that in Glade UI I can only configure a SourceView to hold a TextBuffer; I did not find a SourceBuffer component. So, I created a SourceView in Glade without a default buffer. Now I want to set the SourceBuffer to be the buffer of my SourceView component.

gtk_source_view_new_with_buffer() is the only way I found to attach a SourceBuffer to a SourceView. The problem is that this function is creating a SourceView and I want to attach a SourceBuffer to an already created SourceView built with Glade. How can I do that?

I didn't paste any code because there is no code to show. I just created a Glade file with some UI component plus the SourceView-2 component with ID gtk_sourceview.

In the C file I fetch the SourceView component with

GtkSourceView *sourceview = GTK_WIDGET (gtk_builder_get_object (builder, "gtk_sourceview"));

What's missing is how to create a SourceBuffer and attach it to the SourceView component.

Thanks!

Update: I tried using:

GtkSourceBuffer *sourcebuffer = GTK_SOURCE_BUFFER(gtk_text_view_get_buffer(GTK_TEXT_VIEW(sourceview)));

But I got the following assert error:

(tour_de_gtk:13884): Gtk-CRITICAL **: IA__gtk_text_view_get_buffer: assertion `GTK_IS_TEXT_VIEW (text_view)' failed

What can I do to resolve this ?

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

拧巴小姐 2024-10-26 19:54:38

GtkSourceView 已经有一个缓冲区。获取它的方式与获取常规文本缓冲区相同:

GtkSourceBuffer *buffer = GTK_SOURCE_BUFFER(gtk_text_view_get_buffer(GTK_TEXT_VIEW(source_view)));

这是因为 GtkSourceViewGtkTextView 的子类,因此文本视图函数也适用于它。

编辑

这是一个有效的程序和林间空地文件。

程序:

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

The GtkSourceView already has a buffer. Get it the same way you get a regular text buffer:

GtkSourceBuffer *buffer = GTK_SOURCE_BUFFER(gtk_text_view_get_buffer(GTK_TEXT_VIEW(source_view)));

This is because GtkSourceView is a subclass of GtkTextView, so text view functions work on it too.

EDIT:

Here is a program and glade file that works.

Program:

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

Glade file:

<?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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文