<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/loose.dtd"> <HTML ><HEAD ><TITLE >How do I set the cursor position in a GtkText object?</TITLE ><META NAME="GENERATOR" CONTENT="Modular DocBook HTML Stylesheet Version 1.79"><LINK REL="HOME" TITLE="GTK+ FAQ" HREF="book1.html"><LINK REL="UP" TITLE="Development with GTK+: widget specific questions" HREF="c698.html"><LINK REL="PREVIOUS" TITLE="How do I change the font of a GtkText widget?" HREF="x841.html"><LINK REL="NEXT" TITLE="About GDK" HREF="c857.html"></HEAD ><BODY CLASS="SECT1" BGCOLOR="#FFFFFF" TEXT="#000000" LINK="#0000FF" VLINK="#840084" ALINK="#0000FF" ><DIV CLASS="NAVHEADER" ><TABLE SUMMARY="Header navigation table" WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" ><TR ><TH COLSPAN="3" ALIGN="center" >GTK+ FAQ</TH ></TR ><TR ><TD WIDTH="10%" ALIGN="left" VALIGN="bottom" ><A HREF="x841.html" ACCESSKEY="P" ><<< Previous</A ></TD ><TD WIDTH="80%" ALIGN="center" VALIGN="bottom" >Development with GTK+: widget specific questions</TD ><TD WIDTH="10%" ALIGN="right" VALIGN="bottom" ><A HREF="c857.html" ACCESSKEY="N" >Next >>></A ></TD ></TR ></TABLE ><HR ALIGN="LEFT" WIDTH="100%"></DIV ><DIV CLASS="SECT1" ><H1 CLASS="SECT1" ><A NAME="AEN847" >How do I set the cursor position in a GtkText object?</A ></H1 ><P >Notice that the response is valid for any object that inherits from the GtkEditable class.</P ><P >Are you sure that you want to move the cursor position? Most of the time, while the cursor position is good, the insertion point does not match the cursor position. If this apply to what you really want, then you should use the <TT CLASS="LITERAL" >gtk_text_set_point()</TT > function. If you want to set the insertion point at the current cursor position, use the following:</P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" WIDTH="100%" ><TR ><TD ><PRE CLASS="PROGRAMLISTING" > gtk_text_set_point(GTK_TEXT(text), gtk_editable_get_position(GTK_EDITABLE(text)));</PRE ></TD ></TR ></TABLE ><P >If you want the insertion point to follow the cursor at all time, you should probably catch the button press event, and then move the insertion point. Be careful : you'll have to catch it after the widget has changed the cursor position though. Thomas Mailund Jensen proposed the following code:</P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" WIDTH="100%" ><TR ><TD ><PRE CLASS="PROGRAMLISTING" >static void insert_bar (GtkWidget *text) { /* jump to cursor mark */ gtk_text_set_point (GTK_TEXT (text), gtk_editable_get_position (GTK_EDITABLE (text))); gtk_text_insert (GTK_TEXT (text), NULL, NULL, NULL, "bar", strlen ("bar")); } int main (int argc, char *argv[]) { GtkWidget *window, *text; gtk_init (&argc, &argv); window = gtk_window_new (GTK_WINDOW_TOPLEVEL); text = gtk_text_new (NULL, NULL); gtk_text_set_editable (GTK_TEXT (text), TRUE); gtk_container_add (GTK_CONTAINER (window), text); /* connect after everything else */ gtk_signal_connect_after (GTK_OBJECT(text), "button_press_event", GTK_SIGNAL_FUNC (insert_bar), NULL); gtk_widget_show_all(window); gtk_main(); return 0; }</PRE ></TD ></TR ></TABLE ><P >Now, if you really want to change the cursor position, you should use the <TT CLASS="LITERAL" >gtk_editable_set_position()</TT > function.</P ></DIV ><DIV CLASS="NAVFOOTER" ><HR ALIGN="LEFT" WIDTH="100%"><TABLE SUMMARY="Footer navigation table" WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" ><A HREF="x841.html" ACCESSKEY="P" ><<< Previous</A ></TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="book1.html" ACCESSKEY="H" >Home</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" ><A HREF="c857.html" ACCESSKEY="N" >Next >>></A ></TD ></TR ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" >How do I change the font of a GtkText widget?</TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="c698.html" ACCESSKEY="U" >Up</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" >About GDK</TD ></TR ></TABLE ></DIV ></BODY ></HTML >