GTK+ Forums

Discussion forum for GTK+ and Programming. Ask questions, troubleshoot problems, view and post example code, or express your opinions.
It is currently Mon Sep 01, 2014 1:40 pm

All times are UTC




Post new topic Reply to topic  [ 5 posts ] 
Author Message
 Post subject: print cursor position in textbuffer
PostPosted: Tue Oct 25, 2011 9:05 pm 
Offline
GTK+ Geek

Joined: Mon Jan 05, 2009 9:37 am
Posts: 68
Location: Austria
Hi All,

I would like to write a primitive text editor, and I would like to print out the position of the cursor. I connected the "changed" signal emitted by the textbuffer to a callback function. This works, as long as the textbuffer actually changes, but fails, if I use e.g., the left or right arrows (i.e., non-printable characters). My question is, how is one supposed to catch cases like this?
Thanks,
Zoltán


Top
 Profile  
 
 Post subject: Re: print cursor position in textbuffer
PostPosted: Thu Oct 27, 2011 4:19 am 
Offline
Never Seen the Sunlight

Joined: Thu Mar 24, 2011 2:10 pm
Posts: 328
Location: Sydney, Australia
Try to connect to either to a signal indicating a change in the widget that the text is displayed in, e.g. in gtk3 there's the "draw" signal of gtk_widget class. If all else fails there's always the "notify" signal for the g_object class.


Top
 Profile  
 
 Post subject: Re: print cursor position in textbuffer
PostPosted: Thu Oct 27, 2011 10:42 am 
Offline
GTK+ Geek

Joined: Mon Jan 05, 2009 9:37 am
Posts: 68
Location: Austria
Thanks, I will try that.
Cheers,
Zoltán


Top
 Profile  
 
 Post subject: Re: print cursor position in textbuffer
PostPosted: Fri Oct 28, 2011 7:52 am 
Offline
GTK+ Geek

Joined: Sun Apr 03, 2011 11:12 am
Posts: 63
Connect the key-press-event singnal. That has done the work for me.


Top
 Profile  
 
 Post subject: Re: print cursor position in textbuffer
PostPosted: Fri Oct 28, 2011 7:54 pm 
Offline
Never Seen the Sunlight

Joined: Mon Apr 28, 2008 5:52 am
Posts: 745
Location: UK
Connecting to the "key-press-event" signal will again not work for all cases. There could be times when the cursor position is changed by a mouse click, a touch sensitive screen or even by the programme.

The best by of of being notified of a potential change of cursor position is to connect a notify signal to the "cursor-position" property of the GtkTextBuffer, so that when the property is updated we will get a call back.

Below is a rough section of code that should do this.
Code:
g_signal_connect (buffer, "notify::cursor-position",
                  G_CALLBACK (gtk_text_view_notify_cursor_position),
                  view);

/*callback method*/
void
gtk_text_view_notify_cursor_position(GtkTextBuffer *buffer, GParamSpec *pspec,gpointer user_data)
{
/*calculate cursor position using insert mark*/
....
}


E.

_________________
E.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 5 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 3 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group