Insert new lines into the GtkTextView widget (GTK + programming)

I have a button that, when I click on a copy, adds text from the GtkEntry widget to the GtkTextView widget. (This code is a modified version of the example found in the "Text View Widget" chapter in GTK + Development Fundamentals.)

I want to insert a newline character before the text that is copied and added, so that each line of text will be on a separate line in the GtkTextView widget. How can I do it? I am new to GTK +.

Here is an example code:

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

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

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

Thanks everyone!

+4
source share
1 answer

Perhaps you could just insert a 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 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 fine in GTK (unless it works on Windows for some strange reason).

Unhelpful blurb: 1 could be just as easy -1 here; he just tells GTK that he only needs to read 1 character, which can be a little faster. Of course, this line of code will almost never be a bottleneck :-)

+4
source

Source: https://habr.com/ru/post/1309264/


All Articles