GTK+ Forums Forum Index GTK+ Forums
Discussion forum for GTK+ and Programming. Ask questions, troubleshoot problems, view and post example code, or express your opinions.
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

to read a line of text from gtk text view

 
Post new topic   Reply to topic    GTK+ Forums Forum Index -> GTK+ Programming
Author Message
rahul.sp
Familiar Face


Joined: 18 Mar 2008
Posts: 7

PostPosted: Tue Mar 18, 2008 10:41 am    Post subject: to read a line of text from gtk text view Reply with quote

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.
Back to top
Wolfgang
Familiar Face


Joined: 05 Feb 2008
Posts: 37

PostPosted: Wed Mar 19, 2008 8:41 am    Post subject: Reply with quote

Code: (C)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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
Back to top
rahul.sp
Familiar Face


Joined: 18 Mar 2008
Posts: 7

PostPosted: Wed Mar 19, 2008 9:23 am    Post subject: Reply with quote

hi,
Thanks. It works fine.
Back to top
clinisbut
Familiar Face


Joined: 10 Apr 2008
Posts: 19

PostPosted: Mon Apr 21, 2008 3:18 pm    Post subject: Reply with quote

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
Back to top
clinisbut
Familiar Face


Joined: 10 Apr 2008
Posts: 19

PostPosted: Tue Apr 22, 2008 12:28 pm    Post subject: Reply with quote

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?
Back to top
Wolfgang
Familiar Face


Joined: 05 Feb 2008
Posts: 37

PostPosted: Tue Apr 22, 2008 2:45 pm    Post subject: Reply with quote

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.
Back to top
clinisbut
Familiar Face


Joined: 10 Apr 2008
Posts: 19

PostPosted: Tue Apr 22, 2008 3:11 pm    Post subject: Reply with quote

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
Back to top
Wolfgang
Familiar Face


Joined: 05 Feb 2008
Posts: 37

PostPosted: Tue Apr 22, 2008 3:17 pm    Post subject: Reply with quote

Yes. It is exactly what you need!
Back to top
clinisbut
Familiar Face


Joined: 10 Apr 2008
Posts: 19

PostPosted: Tue Apr 22, 2008 3:28 pm    Post subject: Reply with quote

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.
Back to top
clinisbut
Familiar Face


Joined: 10 Apr 2008
Posts: 19

PostPosted: Wed Apr 23, 2008 9:33 am    Post subject: Reply with quote

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: (C++)
1
2
3
4
5
6
7
8
9

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: (C++)
1
2
3

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: (C++)
1
2
3

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.
Back to top
Wolfgang
Familiar Face


Joined: 05 Feb 2008
Posts: 37

PostPosted: Wed Apr 23, 2008 10:21 am    Post subject: Reply with quote

...
gunichar cur;
pos = g_utf8_get_next_char(pos);
cur = g_utf8_get_char_validated(pos, 6); // gets one symbol
g_print("%w", cur);
...
Back to top
clinisbut
Familiar Face


Joined: 10 Apr 2008
Posts: 19

PostPosted: Wed Apr 23, 2008 10:43 am    Post subject: Reply with quote

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 );
Back to top
Wolfgang
Familiar Face


Joined: 05 Feb 2008
Posts: 37

PostPosted: Wed Apr 23, 2008 10:57 am    Post subject: Reply with quote

I don't wanna disappoint you, but... 114 = 0x68
try:
Code: (C)
1
g_print("%c", cur);
Back to top
clinisbut
Familiar Face


Joined: 10 Apr 2008
Posts: 19

PostPosted: Wed Apr 23, 2008 12:45 pm    Post subject: Reply with quote

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!
Back to top
Display posts from previous:   
Post new topic   Reply to topic    GTK+ Forums Forum Index -> GTK+ Programming All times are GMT
Page 1 of 1

 


Powered by phpBB © 2001, 2005 phpBB Group
CodeBB 1.0 Beta 2
Protected by Anti-Spam ACP