GTK+ Forums

Discussion forum for GTK+ and Programming. Ask questions, troubleshoot problems, view and post example code, or express your opinions.
It is currently Wed Oct 01, 2014 5:59 am

All times are UTC




Post new topic Reply to topic  [ 14 posts ] 
Author Message
 Post subject: to read a line of text from gtk text view
PostPosted: Tue Mar 18, 2008 10:41 am 
Offline
Familiar Face

Joined: Tue Mar 18, 2008 10:26 am
Posts: 7
hi,
I have displayed some data on the text view.
When a user clicks the start of any line ,I want to read that line into a string variable. How do I do that??
Thanks.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 19, 2008 8:41 am 
Offline
GTK+ Guru

Joined: Tue Feb 05, 2008 8:39 am
Posts: 130
Code:
gchar *get_clicked_string(GtkWidget *text_view) 
{
  GtkTextBuffer *buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(text_view));
  GtkTextIter i1, it;
  gchar *text;


  gtk_text_buffer_get_iter_at_mark(buffer, &it, gtk_text_buffer_get_insert(buffer));

  k = gtk_text_iter_get_line(&it);
  gtk_text_buffer_get_iter_at_line(buffer, &it, k);
  gtk_text_buffer_get_end_iter(buffer, &i1);
  text = gtk_text_buffer_get_text(buffer, &it, &i1, FALSE);
  for(i=0;text[i] && text[i]!='\n';i++);
  text[i]=0;
  return text;

}

And don't forget g_free_text(text) after all


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 19, 2008 9:23 am 
Offline
Familiar Face

Joined: Tue Mar 18, 2008 10:26 am
Posts: 7
hi,
Thanks. It works fine.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 21, 2008 3:18 pm 
Offline
Familiar Face

Joined: Thu Apr 10, 2008 1:43 pm
Posts: 19
I would like do the same thing, but instead of only get the line clicked, I need to go through all lines and get each of them. I'm completly lost using iterators


Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 22, 2008 12:28 pm 
Offline
Familiar Face

Joined: Thu Apr 10, 2008 1:43 pm
Posts: 19
Well, I solved my problem by getting all text and then "splitting" by '\n'.

But I have another "problem".
I need to compare every char from gtktextview with a serie of chars stored in memory.
I'm using gtk_text_buffer_get_text to retrieve all text. This returns a gchar.
But is gchar valid for utf-8 encoding?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 22, 2008 2:45 pm 
Offline
GTK+ Guru

Joined: Tue Feb 05, 2008 8:39 am
Posts: 130
gtk_text_buffer_get_text returns pointer to text buffer in utf-8 of course. GTK+ operates with utf-8 strings only, if I not mistake.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 22, 2008 3:11 pm 
Offline
Familiar Face

Joined: Thu Apr 10, 2008 1:43 pm
Posts: 19
yes now I see.
Every utf-8 character can be up to 6 bytes length.
How can I distinguish every character since strlen gives me the byte count of those strings.
Which functions should I use to handle uft-8 strings?

Edit: I think I found them, but confirm it please:
http://library.gnome.org/devel/glib/stable/glib-Unicode-Manipulation.html#g-utf8-strlen


Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 22, 2008 3:17 pm 
Offline
GTK+ Guru

Joined: Tue Feb 05, 2008 8:39 am
Posts: 130
Yes. It is exactly what you need!


Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 22, 2008 3:28 pm 
Offline
Familiar Face

Joined: Thu Apr 10, 2008 1:43 pm
Posts: 19
Thank you, I reading it right now.
This is the first time I need to deal with unicode and I find it a little confusing.
At first I thought that every unicode char were represented with an int, so I didn't understand why using a gchar didn't break my strings.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 23, 2008 9:33 am 
Offline
Familiar Face

Joined: Thu Apr 10, 2008 1:43 pm
Posts: 19
Hello, I'm here again.
I'm sorry but I'm unable to read char by char from a utf-8 string with those functions.

I have a utf-8 string inside
gchar* stringWord

For example ('hello').
Well, I try to iterate through all chars with:
Code:
gchar* pos = stringWord;
cout << "First char:" << pos[0] << endl;  //Prints "h". OK
cout << "GET CHAR:" << g_utf8_get_char( pos ) << endl;  //prints "GET CHAR:104"

pos = g_utf8_next_char( pos );
cout << "First char:" << pos[0] << endl;  //Prints "e". OK
cout << "GET CHAR:" << g_utf8_get_char( pos ) << endl;  //prints "GET CHAR:111"


This works great, only cause every char is 1 byte length.
When I try to iterate a word like this: Интенз (cyrillic) every char is 2 bytes long, so
Code:
cout << "First char:" << pos[0] << endl;

Only prints first byte of char.
The problem I'm getting is that I'm not able to determine the length of each char and I can't found any function in reference chart that does this.
This is very dificult!
If I just print:
Code:
cout << "First char:" << pos << endl;

This prints from position where is pos to the end of string.
At least I found that g_utf8_next_char jumps correctly every char without matter of its length.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 23, 2008 10:21 am 
Offline
GTK+ Guru

Joined: Tue Feb 05, 2008 8:39 am
Posts: 130
...
gunichar cur;
pos = g_utf8_get_next_char(pos);
cur = g_utf8_get_char_validated(pos, 6); // gets one symbol
g_print("%w", cur);
...


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 23, 2008 10:43 am 
Offline
Familiar Face

Joined: Thu Apr 10, 2008 1:43 pm
Posts: 19
gchar* pos = stringWord;

gunichar cur = g_utf8_get_char_validated( pos, 6 );
cout << "First char:" << pos[0] << endl;
g_print("%w", cur);


g_print() throws me a segmentation fault!

If I use cout to print 'cur' variable, I'm gettint the suppossed unicode value of that char, but looking for it at unicode site does not coincide with the value I'm getting.
For example, 'h' character returns me a 104 code, but as you can see at http://www.unicode.org/charts/PDF/U0000.pdf 'h' character has code 68 . Is my assumption correct?

Edit:Forget it, at unicode site's codes are in hex, and this function returns in decimal

Edit:
g_utf8_get_char( pos ) gives me same result as
g_utf8_get_char_validated( pos, 6 );


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 23, 2008 10:57 am 
Offline
GTK+ Guru

Joined: Tue Feb 05, 2008 8:39 am
Posts: 130
I don't wanna disappoint you, but... 114 = 0x68
try:
Code:
g_print("%c", cur);


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 23, 2008 12:45 pm 
Offline
Familiar Face

Joined: Thu Apr 10, 2008 1:43 pm
Posts: 19
yes yes, you're right (seems like i've edited my last post while you were writing yours).
I think I have all working now!
Thank you!


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

All times are UTC


Who is online

Users browsing this forum: Yahoo [Bot] and 2 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