gtk_text_buffer_place_cursor

Linux specific forum
Armoured
Enthusiast
Enthusiast
Posts: 365
Joined: Mon Jan 26, 2004 11:39 am
Location: ITALY
Contact:

gtk_text_buffer_place_cursor

Post by Armoured »

Hi :)
Please can I have an example of the use of this function?


Thanks
User avatar
dhouston
Enthusiast
Enthusiast
Posts: 430
Joined: Tue Aug 21, 2007 2:44 pm
Location: USA (Cincinnati)
Contact:

Post 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.
User avatar
dhouston
Enthusiast
Enthusiast
Posts: 430
Joined: Tue Aug 21, 2007 2:44 pm
Location: USA (Cincinnati)
Contact:

Post 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.
User avatar
dhouston
Enthusiast
Enthusiast
Posts: 430
Joined: Tue Aug 21, 2007 2:44 pm
Location: USA (Cincinnati)
Contact:

Post 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
walker
Enthusiast
Enthusiast
Posts: 634
Joined: Wed May 05, 2004 4:04 pm
Location: Germany

Post 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
Armoured
Enthusiast
Enthusiast
Posts: 365
Joined: Mon Jan 26, 2004 11:39 am
Location: ITALY
Contact:

Post by Armoured »

Ok
I need only one thing.
How I can read the current position of the cursor in the editor?
User avatar
dhouston
Enthusiast
Enthusiast
Posts: 430
Joined: Tue Aug 21, 2007 2:44 pm
Location: USA (Cincinnati)
Contact:

Post by dhouston »

@walker
Thanks for the pointer pointers.
Armoured
Enthusiast
Enthusiast
Posts: 365
Joined: Mon Jan 26, 2004 11:39 am
Location: ITALY
Contact:

Post 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()
walker
Enthusiast
Enthusiast
Posts: 634
Joined: Wed May 05, 2004 4:04 pm
Location: Germany

Post 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?)
freak
PureBasic Team
PureBasic Team
Posts: 5948
Joined: Fri Apr 25, 2003 5:21 pm
Location: Germany

Post by freak »

GtkTextView does not inherit from GtkEditable, so these functions are not valid for it.
quidquid Latine dictum sit altum videtur
walker
Enthusiast
Enthusiast
Posts: 634
Joined: Wed May 05, 2004 4:04 pm
Location: Germany

Post by walker »

:oops: got me... I guess i got confused by the EditorGadget and the GTKEditable and mixed this a little....
Post Reply