Re: turn on italics in TextView
- From: cecashon aol com
- To: dougm bravoecho net, gtk-app-devel-list gnome org
- Subject: Re: turn on italics in TextView
- Date: Tue, 13 Jun 2017 18:22:35 -0400
Hi Doug,
You can try using the "insert-text" callback to set your italics on the inserted text. This is what I came up
with to test. The signal is connected after so that the update occurs first. Careful about not changing the
location iter also since there is a warning in the documentation about doing so. Might not need to worry
about it here but something to be aware of.
https://developer.gnome.org/gtk3/stable/GtkTextBuffer.html#GtkTextBuffer-insert-text
Eric
/*
gcc -Wall textview2.c -o textview2 `pkg-config --cflags --libs gtk+-3.0`
Tested on GTK3.18 and Ubuntu16.04
*/
#include<gtk/gtk.h>
static gboolean italic=FALSE;
static void toggle_italic(GtkToggleButton *toggle1, gpointer data)
{
GtkWidget *label=gtk_bin_get_child(GTK_BIN(toggle1));
if(gtk_toggle_button_get_active(toggle1))
{
gtk_label_set_markup(GTK_LABEL(label), "<span style='italic'>Italics</span>");
italic=TRUE;
}
else
{
gtk_label_set_text(GTK_LABEL(label), "Italics");
italic=FALSE;
}
gtk_widget_grab_focus(GTK_WIDGET(data));
}
static void new_text(GtkTextBuffer *textbuffer, GtkTextIter *location, gchar *text, gint len, gpointer
user_data)
{
if(italic)
{
GtkTextIter *start=gtk_text_iter_copy(location);
GtkTextIter *end=gtk_text_iter_copy(location);
gtk_text_iter_backward_chars(start, len);
gtk_text_buffer_apply_tag_by_name(textbuffer, "tag1", start, end);
gtk_text_iter_free(start);
gtk_text_iter_free(end);
}
}
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), "Textview");
gtk_window_set_default_size(GTK_WINDOW(window), 300, 200);
gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
g_signal_connect(window, "destroy", G_CALLBACK(gtk_main_quit), NULL);
GtkWidget *text_view1=gtk_text_view_new();
gtk_text_view_set_wrap_mode(GTK_TEXT_VIEW(text_view1), GTK_WRAP_CHAR);
gtk_widget_set_vexpand(text_view1, TRUE);
gtk_widget_set_hexpand(text_view1, TRUE);
GtkTextBuffer *buffer=gtk_text_view_get_buffer(GTK_TEXT_VIEW(text_view1));
gtk_text_buffer_create_tag(buffer, "tag1", "style", PANGO_STYLE_ITALIC, NULL);
g_signal_connect_after(buffer, "insert-text", G_CALLBACK(new_text), NULL);
GtkWidget *toggle1=gtk_toggle_button_new_with_label("Italics");
g_signal_connect(toggle1, "toggled", G_CALLBACK(toggle_italic), text_view1);
GtkWidget *grid=gtk_grid_new();
gtk_container_set_border_width(GTK_CONTAINER(grid), 8);
gtk_grid_set_row_spacing(GTK_GRID(grid), 12);
gtk_grid_attach(GTK_GRID(grid), text_view1, 0, 0, 1, 1);
gtk_grid_attach(GTK_GRID(grid), toggle1, 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]