开发者

Inserting newlines into a GtkTextView widget (GTK+ programming)

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.)

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+.

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_widg开发者_开发百科et_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);
}

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`

Thanks everybody!


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" 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).

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 :-)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜