插入新行成GtkTextView窗口小部件(GTK +编程) [英] Inserting newlines into a GtkTextView widget (GTK+ programming)

查看:218
本文介绍了插入新行成GtkTextView窗口小部件(GTK +编程)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有点击它复制并附加从部件GtkEntry中的文成GtkTextView窗口小部件时的按钮。 (这code是一个例子的修改版本中的文本视图控件一章中的 GTK +发展基金会的。)

I've got a button which when clicked copies and appends the text from a GtkEntry widget into a GtkTextView widget. (This code is a modified version of an example found in the "The Text View Widget" chapter of Foundations of GTK+ Development.)

我在寻找它被复制并追加文本,使得每行文本将是在GtkTextView部件自己的行前插入一个换行符。我会怎么做呢?我是全新的,以GTK +。

I'm looking to insert a newline character before the text which gets copied and appended, such that each line of text will be on its own line in the GtkTextView widget. How would I do this? I'm brand new to GTK+.

这里的code样品:<​​/ P>

Here's the code sample:

#include <gtk/gtk.h>

typedef struct
{
     GtkWidget *entry, *textview;
} Widgets;

static void insert_text (GtkButton*, Widgets*);

int main (int argc,
          char *argv[])
{
     GtkWidget *window, *scrolled_win, *hbox, *vbox, *insert;
     Widgets *w = g_slice_new (Widgets);

     gtk_init (&argc, &argv);

     window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
     gtk_window_set_title (GTK_WINDOW (window), "Text Iterators");
     gtk_container_set_border_width (GTK_CONTAINER (window), 10);
     gtk_widget_set_size_request (window, -1, 200);

     w->textview = gtk_text_view_new ();
     w->entry = gtk_entry_new ();
     insert = gtk_button_new_with_label ("Insert Text");

     g_signal_connect (G_OBJECT (insert), "clicked",
               G_CALLBACK (insert_text),
               (gpointer) w);

     scrolled_win = gtk_scrolled_window_new (NULL, NULL);
     gtk_container_add (GTK_CONTAINER (scrolled_win), w->textview);

     hbox = gtk_hbox_new (FALSE, 5);
     gtk_box_pack_start_defaults (GTK_BOX (hbox), w->entry);
     gtk_box_pack_start_defaults (GTK_BOX (hbox), insert);

     vbox = gtk_vbox_new (FALSE, 5);
     gtk_box_pack_start (GTK_BOX (vbox), scrolled_win, TRUE, TRUE, 0);
     gtk_box_pack_start (GTK_BOX (vbox), hbox, FALSE, TRUE, 0);

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

     gtk_main();
     return 0;
}

/* Insert the text from the GtkEntry into the GtkTextView. */
static void
insert_text (GtkButton *button,
             Widgets *w)
{
     GtkTextBuffer *buffer;
     GtkTextMark *mark;
     GtkTextIter iter;
     const gchar *text;

     buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (w->textview));
     text = gtk_entry_get_text (GTK_ENTRY (w->entry));

     mark = gtk_text_buffer_get_insert (buffer);
     gtk_text_buffer_get_iter_at_mark (buffer, &iter, mark);
     gtk_text_buffer_insert (buffer, &iter, text, -1);
}

您可以使用下面的命令编译此code(假设该文件名为file.c中):

You can compile this code using the following command (assuming the file is named file.c):

gcc file.c -o file `pkg-config --cflags --libs gtk+-2.0`

谢谢大家!

推荐答案

也许你可以只需插入换行符以同样的方式插入其它字符:

Perhaps you could just insert the newline character the same way you insert other characters:

...
mark = gtk_text_buffer_get_insert (buffer);
gtk_text_buffer_get_iter_at_mark (buffer, &iter, mark);

/* Insert newline (only if there's already text in the buffer). */
if (gtk_text_buffer_get_char_count(buffer))
    gtk_text_buffer_insert (buffer, &iter, "\n", 1);

gtk_text_buffer_insert (buffer, &iter, text, -1);
...

\\ n是包含在C换行符的字符串,它在GTK工作得很好(除非它不会在Windows中的一些奇怪的原因)

"\n" is a string containing the newline character in C, and it works just fine in GTK (unless it doesn't in Windows for some weird reason).

无益导语:在 1 可以很容易被 1 在这里;它只是告诉GTK只需要读取1个字符,这可能会快一点。诚然,code的该行应该几乎从来没有成为一个瓶颈: - )

Unhelpful blurb: The 1 can just as easily be -1 here; it just tells GTK it only needs to read 1 character, which might be a little faster. Granted, that line of code should almost never be a bottleneck :-)

这篇关于插入新行成GtkTextView窗口小部件(GTK +编程)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆