Detect cursor movement in GTK text buffer
An answer to this question on Stack Overflow.
Question
I am working on a GTK+ editor in C. I have added a feature of displaying the current line number and column number of the cursor position in the textview. Its working well. But the drawback is when I attempt to move the cursor with the arrow keys the line number and column number do not get updated. Below is my code for updating the line number and column number
update_statusbar(GtkTextBuffer *buffer,GtkStatusbar *statusbar)
{
gchar *msg;
gint row, col;
GtkTextIter iter;
gtk_statusbar_pop(statusbar, 0);
g_print("c");
gtk_text_buffer_get_iter_at_mark(buffer,
&iter, gtk_text_buffer_get_insert(buffer));
row = gtk_text_iter_get_line(&iter);
col = gtk_text_iter_get_line_offset(&iter);
msg = g_strdup_printf("Col %d Ln %d", col+1, row+1);
gtk_statusbar_push(statusbar, 0, msg);
g_free(msg);
}
int main ( int argc, char *argv[])
{
.
.
.
.
.
.
g_signal_connect(buffer, "changed", G_CALLBACK(update_statusbar), statusbar);
update_statusbar(buffer, GTK_STATUSBAR (statusbar));
}
I guess the problem is with "changed" signal. Since the cursor is moved with arrow keys, buffer doesn't get changed. So can anyone suggest me a better way to solve the problem .
Thanks in advance :).
Answer
There's a list of text buffer signals here and a list of general widget signals here.
The latter link has a signal called key-release-event which you will probably find interesting.