Hi Ken, I am not sure if event-after would be a clean approach.
However , following way for getting notified for any event which results in cursor-position change for text buffer will be useful. This will be called only if the cursor-position property is updated. 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*/ .... } regards , Sanny On Mon, Mar 1, 2010 at 2:33 PM, Ken Resander <kresan...@yahoo.com> wrote: > Hi Sanny, > > Many thanks! > > Both your suggestions work fine for getting current cursor position by > mouse clicks. > > I am collecting the key values via key-press-event, but so does the text > default handler, so the problem is still present for key navigation. > However, I could make it work by using the key-release event instead. Lucky! > > My fumbles also took me to the mark-set signal of GtkTextBuffer. This > signal is emitted when text marks change. It works for both mouse and > keyboard. However, a single mouse click produces four events, for example > the following when I click keyword 'while': > > findtag ret 28 iter 153571352 mark insert > markset MTOKEN Keyword(4) > findtag ret 29 iter 153572264 mark selection_bound > markset MTOKEN Keyword(4) > findtag ret 30 iter 153580256 mark (null) > markset MTOKEN Keyword(4) > findtag ret 31 iter 153580720 mark (null) > markset MTOKEN Keyword(4) > > The first insert mark is the one I want so I can filter out the others. The > last two are unnamed and I don't know what they are. > > However, when I drag the mouse from letter h to letter l in the 'while' > keyword I get the following avalanche of about 70 events: > > findtag ret 32 iter 153571408 mark insert > markset MTOKEN Keyword(4) > findtag ret 33 iter 153571352 mark selection_bound > markset MTOKEN Keyword(4) > findtag ret 34 iter 153576792 mark (null) > markset MTOKEN Keyword(4) > findtag ret 35 iter 152410200 mark (null) > markset MTOKEN Keyword(4) > findtag ret 36 iter 153571464 mark insert > markset MTOKEN Keyword(4) > findtag ret 37 iter 153571872 mark (null) > markset MTOKEN Keyword(4) > findtag ret 38 iter 153572688 mark (null) > markset MTOKEN Keyword(4) > findtag ret 39 iter 153577664 mark (null) > markset MTOKEN Keyword(4) > findtag ret 40 iter 153573152 mark (null) > markset MTOKEN Keyword(4) > findtag ret 41 iter 153578128 mark insert > markset MTOKEN Keyword(4) > findtag ret 42 iter 153576976 mark (null) > markset MTOKEN Keyword(4) > findtag ret 43 iter 153580552 mark insert > markset MTOKEN Keyword(4) > findtag ret 44 iter 152410624 mark (null) > ..... > > and when I resize the screen I get hundreds of events for a 10-line test > text. For a normal program there would many thousands of these unwanted > events. The mark-set would work, but I feel uneasy about it. > > I also discovered and tried the GtkWidget signal event-efter. It worked. > > Initialise: > [code] > g_signal_connect ( G_OBJECT (e) , "event-after" , > G_CALLBACK (eventaftercb) , buf ) ; > [/code] > > with callback: > [code] > static gboolean eventaftercb (GtkWidget * w, GdkEvent * ev, gpointer > user_data ) > { > if ( (ev->type == GDK_BUTTON_PRESS) ) > { > printf ( "%d BTNpress\n" , (int)ev->type ) ; > return buttonpresscb ( w, (GdkEventButton *)ev , (char *)user_data ) > ; > } > else if ( (ev->type == GDK_KEY_PRESS) ) > { > printf ( "%d keyPress\n" , (int)ev->type ) ; > return keypresscb ( w, (GdkEventKey *)ev , (char *)user_data ) ; > } > return true ; > } > [/code] > > Need to test all this a bit more, but as of now, I prefer the event-after > method. > > Would be grateful for comments. > > Ken > > > > > --- On *Mon, 1/3/10, Sanny Chawla <sanny.cha...@gmail.com>* wrote: > > > From: Sanny Chawla <sanny.cha...@gmail.com> > Subject: Re: Problem identifying GtkTextTags for syntax highlighting > To: "Ken Resander" <kresan...@yahoo.com> > Cc: gtk-list@gnome.org > Date: Monday, 1 March, 2010, 2:22 AM > > > Hi Ken, > > The cursor value which your code is calculating from insert mark is getting > updated in button-release-event. So the syntax tokens can be identified in > button-release-event callback. > > You can even try using below method to find the cursor position in > button-press-event itself. > 1. Convert the x,y coordinates received in the button-press-event to buffer > coordindates. > 2. Calculate the GtkTextIter from the buffer coordinates. > > eg : > /* Button press callback */ > { > gtk_text_view_window_to_buffer_coords(text_view, > gtk_text_view_get_window_type (GTK_TEXT_VIEW(text_view), > event->window),event->x, event->y, &x, &y); > gtk_text_view_get_iter_at_location (GTK_TEXT_VIEW (text_view), &curr, > x, y); > > /* Identify if curr iter is associated with syntax token tag array */ > ... > ... > } > > regards , > Sanny > > > On Sun, Feb 28, 2010 at 5:23 PM, Ken Resander > <kresan...@yahoo.com<http://mc/compose?to=kresan...@yahoo.com> > > wrote: > >> I am trying to do a bit of syntax highlighting. I am using >> gtk_text_buffer_create_tag and gtk_text_buffer_insert_with_tags to >> syntax-highlight the intial text (from file). That works fine. >> >> The program needs to identify existing syntax tokens in order to do >> appropriate syntax checking should the user decide to go back and amend them >> and I am having problems with this. >> >> >> Find a token-tag from current position: >> [code] >> static int findtagfromcursor ( GtkTextBuffer * buf ) >> { >> GtkTextMark * mark = gtk_text_buffer_get_insert (buf); >> GtkTextIter curiter ; >> gtk_text_buffer_get_iter_at_mark (buf, &curiter, mark); >> int k = 0 ; >> while ( k < NUMTOKENS ) >> { >> if ( gtk_text_iter_has_tag ( &curiter , tags [ k ] ) ) >> { >> return k ; >> } >> k++ ; >> } >> return -1 ; >> } >> [/code] >> >> The tags array is set by calls to gtk_text_buffer_create_tag when the text >> is read in. >> >> Initialisation: >> [code] >> GtkWidget * e = gtk_text_view_new ( ) ; >> GtkTextBuffer * buf = gtk_text_view_get_buffer (GTK_TEXT_VIEW (e)); >> gtk_text_view_set_editable ( (GtkTextView *)e , true ) ; >> g_signal_connect_after ( G_OBJECT (e) , "button-press-event" , >> G_CALLBACK (buttonpresscb) , buf ) ; >> [/code] >> >> Skeleton callback: >> [code] >> static gboolean buttonpresscb ( GtkWidget * w, GdkEventButton *event, char >> * data ) >> { >> GtkTextBuffer * buf = (GtkTextBuffer *)data ; >> int tokenix = findtagfromcursor ( buf ) ; >> if ( tokenix != -1 ) >> { >> printf ( "MTOKEN %s(%d): \n" tokenlkup [ tokenix ] , tokenix ) ; >> } >> else >> { >> printf ( "MTOKEN %d: \n" , tokenix ) ; >> } >> return false; >> } >> [/code] >> >> Textview text: >> text text ... TOKEN2TEXT text text... TOKEN0TEXT text text ... TOKEN1TEXT >> >> >> 'text text...' is unadorned text. >> TOKEN0TEXT, TOKEN1TEXT, TOKEN2TEXT ... are categories of text associated >> with different visual attributes. The program identifies these with numbers >> 0,1,2 internally. When the user clicks on a token the program needs to >> identify the token. >> >> When I click TOKEN2TEXT the callback reports no-token (-1). Then when I >> click TOKEN0TEXT it reports the TOKEN2TEXT (2) and when I click TOKEN1TEXT >> it reports TOKEN0TEXT (0) and so on. >> >> I thought this is because buttonpresscb is called before the text handler >> that changes the current text position to the mouse-click position, so I >> changed the connect call to g_signal_connect_after ( G_OBJECT (e) , "but... >> ). >> >> That did not work. I received no buttonpress events at all. >> >> Why? I am totally stuck, so would be most grateful for advice. >> >> >> ------------------------------ >> New Email addresses available on Yahoo! >> <http://sg.rd.yahoo.com/aa/mail/domainchoice/mail/signature/*http://mail.promotions.yahoo.com/newdomains/aa/> >> Get the Email name you've always wanted on the new @ymail and @rocketmail. >> Hurry before someone else does! >> >> _______________________________________________ >> gtk-list mailing list >> gtk-list@gnome.org <http://mc/compose?to=gtk-l...@gnome.org> >> http://mail.gnome.org/mailman/listinfo/gtk-list >> >> > > ------------------------------ > Get your new Email address! > <http://sg.rd.yahoo.com/aa/mail/domainchoice/mail/signature/*http://mail.promotions.yahoo.com/newdomains/aa/> > Grab the Email name you've always wanted before someone else does! > _______________________________________________ > gtk-list mailing list > gtk-list@gnome.org > http://mail.gnome.org/mailman/listinfo/gtk-list > >
_______________________________________________ gtk-list mailing list gtk-list@gnome.org http://mail.gnome.org/mailman/listinfo/gtk-list