Re: text with links
- From: Lance Dillon <riffraff169 yahoo com>
- To: gtk-app-devel-list gnome org
- Subject: Re: text with links
- Date: Mon, 11 Sep 2006 11:24:29 -0700 (PDT)
This example code does it. You may want to make some changes, but it works.
#include <stdio.h>
#include <gtk/gtk.h>
GtkTextTag *tag1;
static void print_coords(GtkWidget *w, int x, int y) {
gint bx,by;
GtkTextIter iter;
gint trail;
gtk_text_view_window_to_buffer_coords(GTK_TEXT_VIEW(w),GTK_TEXT_WINDOW_WIDGET,
x,y,&bx,&by);
gtk_text_view_get_iter_at_position(GTK_TEXT_VIEW(w),&iter,&trail,bx,by);
if (gtk_text_iter_has_tag(&iter,tag1))
g_debug("CLICKED ON LINK!");
}
static void click_link(GtkWidget *w, GdkEventButton *ev, gpointer g) {
g_debug("x==%f",ev->x);
g_debug("y==%f",ev->y);
g_debug("button==%d",ev->button);
switch (ev->type) {
case GDK_BUTTON_PRESS:
g_debug("type==single click");
print_coords(w,(int)ev->x,(int)ev->y);
break;
/*
case GDK_2BUTTON_PRESS:
g_debug("type==double click");
break;
case GDK_3BUTTON_PRESS:
g_debug("type==triple click");
break;
case GDK_BUTTON_RELEASE:
g_debug("type==button release");
break;
*/
default:
break;
}
}
int main(int argc, char **argv) {
GtkTextBuffer *buffer;
GtkTextView *tv;
GtkWidget *w;
GtkTextIter iter;
GtkTextTagTable *tb;
gtk_init(&argc, &argv);
w=gtk_window_new(GTK_WINDOW_TOPLEVEL);
g_signal_connect(G_OBJECT(w),"delete_event",G_CALLBACK(gtk_false),NULL);
g_signal_connect(G_OBJECT(w),"destroy",G_CALLBACK(gtk_main_quit),NULL);
tv=GTK_TEXT_VIEW(gtk_text_view_new());
gtk_container_add(GTK_CONTAINER(w),GTK_WIDGET(tv));
buffer=gtk_text_view_get_buffer(tv);
gtk_widget_set_size_request(GTK_WIDGET(w),300,300);
gtk_text_view_set_editable(tv,FALSE);
tb=gtk_text_buffer_get_tag_table(buffer);
tag1=gtk_text_tag_new("link");
g_object_set(G_OBJECT(tag1),"underline",PANGO_UNDERLINE_SINGLE,NULL);
g_object_set(G_OBJECT(tag1),"foreground","blue",NULL);
gtk_text_tag_table_add(GTK_TEXT_TAG_TABLE(tb),tag1);
gtk_text_buffer_get_iter_at_offset(buffer,&iter,0);
gtk_text_buffer_insert(buffer,&iter,"This is some sample text.\n\nA link is ",-1);
gtk_text_buffer_insert_with_tags(buffer,&iter,"here",-1,tag1,NULL);
gtk_text_buffer_insert(buffer,&iter,".\n\nMore text...\n",-1);
g_signal_connect(G_OBJECT(tv),"button-press-event",G_CALLBACK(click_link),NULL);
gtk_widget_show_all(w);
gtk_main();
return 0;
}
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]