Page 1 of 1

gtk_text_buffer_place_cursor

Posted: Wed Nov 28, 2007 11:46 pm
by Armoured
Hi :)
Please can I have an example of the use of this function?


Thanks

Posted: Thu Nov 29, 2007 3:50 pm
by dhouston
I need to solve this, too - or make a virtue of necessity and always add lines at the top.

Code: Select all

buffer=gtk_text_view_get_buffer_(GadgetID(#EDT_IO_MAIN))
chars=gtk_text_buffer_get_char_count_(buffer)
lines=gtk_text_buffer_get_line_count_(buffer)
gtk_text_buffer_place_cursor_(buffer,buffer+chars-1)
The first 3 lines work but no matter what I try for the second argument for gtk_text_buffer_place_cursor_(), I get an Invalid Memory Access error.

Posted: Fri Nov 30, 2007 12:35 pm
by dhouston
I found some C code at http://www.computergeekmatt.com/gtk/servercode.html which does what we want (I think) but I'm not sure I understand C syntax well enough to convert it to PureBasic. Specifically, I'm not sure what the & in &iter means nor what to use for FALSE.

Code: Select all

	string = gtk_entry_get_text(GTK_ENTRY(w->textBox)); //get the message to send.
	g_strdup(string);
	buffer=gtk_text_view_get_buffer(GTK_TEXT_VIEW(w->textArea));//set buffer to the text in the text area
	gtk_text_buffer_get_end_iter(buffer, &iter); //Get the end of the buffer
	gtk_text_buffer_place_cursor(buffer, &iter); //set the cursor at the end of the buffer
	gtk_text_buffer_insert_at_cursor(buffer, "\nYOU said: ", -1); //write to the buffer at the cursor
Also, I'm not sure whether this scrolls the window to display the bottom line or merely sets the insertion point in the buffer.

Code: Select all

	gtk_text_buffer_get_end_iter(buffer, &iter);
	gtk_text_buffer_place_cursor(buffer, &iter);

	mark = gtk_text_buffer_create_mark(buffer, "cursor", &iter, FALSE); //create a mark in the buffer to scroll to

	gtk_text_view_scroll_to_mark(GTK_TEXT_VIEW(w->textArea), mark, //scroll to the mark
									0, FALSE, 0, 0);
This seems to actually scroll the window.

Posted: Fri Nov 30, 2007 4:14 pm
by dhouston
I've tried this code and still get an Invalid Memory access at gtk_text_buffer_place_cursor. That's the only line that causes an error - if I comment it out, the code runs but the window does not scroll. I've tried with both iter and *iter with the same result.

Code: Select all

buffer=gtk_text_view_get_buffer_(GadgetID(#EDT_IO_MAIN));//set buffer to the text in the text area
gtk_text_buffer_get_end_iter_(buffer,*iter);
gtk_text_buffer_place_cursor_(buffer,*iter);
mark=gtk_text_buffer_create_mark_(buffer,"cursor",*iter,False);//create a mark in the buffer to scroll to
gtk_text_view_scroll_to_mark_(GadgetID(#EDT_IO_MAIN)),mark,0,False,0,0);//scroll to the mark

Posted: Sat Dec 01, 2007 9:13 am
by walker
first declare a var like

Code: Select all

mypointertoiteration.GtkTextIter
Then you have to get the position with gtk_text_buffer_get_iter_at_offset_(buffer,@pointertoiteration,position)

Code: Select all


gtk_text_buffer_get_iter_at_offset_(buffer,@mypointertoiteration,10)
(position can be a value from 0 up to the count of chars in your text)
and then you can apply the cursor with

Code: Select all

gtk_text_buffer_place_cursor_(buffer,@mypointertoiteration)       
The buffer is a pointer to the created GTKTextBuffer containing your text

Posted: Mon Dec 03, 2007 6:05 pm
by Armoured
Ok
I need only one thing.
How I can read the current position of the cursor in the editor?

Posted: Tue Dec 04, 2007 1:41 am
by dhouston
@walker
Thanks for the pointer pointers.

Posted: Wed Dec 05, 2007 8:39 am
by Armoured
Thanks Walker!

Code: Select all

Procedure SetCursorPos(Id.l,pos.l)
    Protected mypointertoiteration.GtkTextIter, *buffer.l
    *buffer = gtk_text_view_get_buffer_(GadgetID(Id))
    gtk_text_buffer_get_iter_at_offset_(*buffer,@mypointertoiteration,pos)
    gtk_text_buffer_place_cursor_(*buffer,@mypointertoiteration)
EndProcedure


Procedure.l GetCursorPos(Id.l)
    Protected mypointertoiteration.GtkTextIter, *buffer.l,cursor.l
    *buffer = gtk_text_view_get_buffer_(GadgetID(Id))
    cursor = gtk_text_buffer_get_insert_(*buffer)
    gtk_text_buffer_get_iter_at_mark_(*buffer, @mypointertoiteration, cursor)
    ProcedureReturn gtk_text_iter_get_offset_(@mypointertoiteration)
EndProcedure
These are my two procedures to control the cursor in an EditorGadget()

Posted: Wed Dec 05, 2007 9:32 am
by walker
no problem.. and thanks for posting the 2 procedures.. so everyone can benefit from it 8)

a question @ Fred / Freak: Why does gtk_editable_get_position_(*editable) and gtk_editable_set_position_(*editable,position) do NOT work on an EditorGadget? regarding to the GTK doc it should :roll: (do I miss anything?)

Posted: Wed Dec 05, 2007 10:01 am
by freak
GtkTextView does not inherit from GtkEditable, so these functions are not valid for it.

Posted: Wed Dec 05, 2007 11:45 am
by walker
:oops: got me... I guess i got confused by the EditorGadget and the GTKEditable and mixed this a little....