Re: GtkTextBuffer : Applying tags to newly input text
- From: cecashon aol com
- To: grim reaperworld com, gtk-app-devel-list gnome org
- Subject: Re: GtkTextBuffer : Applying tags to newly input text
- Date: Wed, 13 Jun 2018 15:33:07 -0400
Hi Gary,
Test setting up your callback with g_signal_connect_after. Then the location iter should be at the end of the
insert. Then the tag can be applied to the inserted text.
Eric
//gcc -Wall highlight1.c -o highlight1 `pkg-config --cflags --libs gtk+-3.0`
#include <gtk/gtk.h>
static gboolean set_tag=FALSE;
static void button_toggled(GtkToggleButton *button, gpointer data)
{
g_print("Button Toggled\n");
set_tag=gtk_toggle_button_get_active(button);
}
static void insert_text(GtkTextBuffer *textbuffer, GtkTextIter *location, gchar *text, gint len, GtkTextTag *
cyan_tag)
{
g_print("Insert %s Offset %i\n", text, gtk_text_iter_get_offset(location));
if(set_tag)
{
GtkTextIter *start=gtk_text_iter_copy(location);
gtk_text_iter_backward_chars(start, len);
gtk_text_buffer_apply_tag(textbuffer, cyan_tag, start, location);
gtk_text_iter_free(start);
}
}
int main(int argc, char *argv[])
{
gtk_init(&argc, &argv);
GtkWidget *window=gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_window_set_title(GTK_WINDOW(window), "Highlight");
gtk_window_set_default_size(GTK_WINDOW(window), 300, 300);
gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
gtk_container_set_border_width(GTK_CONTAINER(window), 20);
g_signal_connect(window, "destroy", G_CALLBACK(gtk_main_quit), NULL);
GtkWidget *textview=gtk_text_view_new();
gtk_text_view_set_wrap_mode(GTK_TEXT_VIEW(textview), GTK_WRAP_CHAR);
gtk_widget_set_hexpand(textview, TRUE);
gtk_widget_set_vexpand(textview, TRUE);
GtkTextBuffer *buffer=gtk_text_view_get_buffer(GTK_TEXT_VIEW(textview));
GtkTextTag *cyan_tag=gtk_text_buffer_create_tag(buffer, "cyan-tag", "background", "cyan", NULL);
g_signal_connect_after(buffer, "insert-text", G_CALLBACK(insert_text), cyan_tag);
GtkWidget *button=gtk_toggle_button_new_with_label("Cyan Highlight");
g_signal_connect(button, "toggled", G_CALLBACK(button_toggled), NULL);
GtkWidget *grid=gtk_grid_new();
gtk_grid_attach(GTK_GRID(grid), textview, 0, 0, 1, 1);
gtk_grid_attach(GTK_GRID(grid), button, 0, 1, 1, 1);
gtk_container_add(GTK_CONTAINER(window), grid);
gtk_widget_show_all(window);
gtk_main();
return 0;
}
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]