Re: Problem identifying GtkTextTags for syntax highlighting



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 <kresander 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!
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://mail.gnome.org/mailman/listinfo/gtk-list




[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]