I am trying to make gtk_text_view_scroll_to_iter()
to work in my application, but something is wrong in my code, or probably I am facing the part where the documentation says:
Note that this function uses the currently-computed height of the lines in the text buffer. Line heights are computed in an idle handler; so this function may not have the desired effect if it’s called before the height computations.
In the following code the searched text get selected but does not scrolls to it if I call scroll_to_iter()
functions, but works fine if I call the scroll_to_mark()
function:
#include <gtk/gtk.h>
const gchar *const buffer = "This is a long message\n"
"And here goes to line two\n"
"And here goes to line two\n"
"And here goes to line two\n"
"And here goes to line two\n"
"And here goes to line two\n"
"And here goes to line two\n"
"And here goes to line two\n"
"And here goes to line two\n"
"And here goes to line two\n"
"And here goes to line two\n"
"And here goes to line two\n"
"And here goes to line two\n"
"And here goes to line two\n"
"And here goes to line two\n"
"And here goes to line two\n"
"And here goes to line two\n"
"And here goes to line two\n"
"And here goes to line two\n"
"And here goes to line two\n"
"And here goes to line two\n"
"And here goes to line two\n"
"And here goes to line two\n"
"And here goes to line two\n"
"And here goes to line two\n"
"And here goes to line two\n"
"And here goes to line two\n"
"And here goes to line two\n"
"And here goes to line two\n"
"And here goes to line two\n"
"And here goes to line two\n"
"And here goes to line two\n"
"And here goes to line two\n"
"And here goes to line two\n"
"And here goes to line two\n"
"And here goes to line two\n"
"And here goes to line two\n"
"And here goes to line two\n"
"And here goes to line two\n"
"And here goes to line two\n"
"And here goes to line two\n"
"And here goes to line two\n"
"And here goes to line two\n"
"And here goes to line two\n"
"of course we have another line here";
void scroll_to_iter ( GtkWidget *text_view, GtkTextBuffer *text_buffer, const gchar *const text );
void scroll_to_iter ( GtkWidget *text_view, GtkTextBuffer *text_buffer, const gchar *const text )
{
GtkTextIter start;
GtkTextIter match_start;
GtkTextIter match_end;
gtk_text_buffer_get_start_iter ( text_buffer, &start );
gtk_text_buffer_get_end_iter ( text_buffer, &match_end );
gboolean flag;
flag = gtk_text_iter_forward_search ( &start, text, GTK_TEXT_SEARCH_CASE_INSENSITIVE, &match_start, &match_end, NULL );
if ( flag )
{
gtk_text_buffer_select_range ( text_buffer, &match_start, &match_end );
gtk_text_view_scroll_to_iter ( GTK_TEXT_VIEW ( text_view ), &match_start, 0.0, TRUE, 0.0, 0.0 );
}
}
void scroll_to_mark ( GtkWidget *text_view, GtkTextBuffer *text_buffer, const gchar *const text );
void scroll_to_mark ( GtkWidget *text_view, GtkTextBuffer *text_buffer, const gchar *const text )
{
GtkTextIter start;
GtkTextIter match_start;
GtkTextIter match_end;
GtkTextMark *mark;
gtk_text_buffer_get_start_iter ( text_buffer, &start );
gtk_text_buffer_get_end_iter ( text_buffer, &match_end );
gboolean flag;
flag = gtk_text_iter_forward_search ( &start, text, GTK_TEXT_SEARCH_CASE_INSENSITIVE, &match_start, &match_end, NULL );
if ( flag )
{
mark = gtk_text_buffer_create_mark ( text_buffer, NULL, &match_end, TRUE );
gtk_text_buffer_select_range ( text_buffer, &match_start, &match_end );
gtk_text_view_scroll_to_mark ( GTK_TEXT_VIEW ( text_view ), mark, 0.0, TRUE, 0.0, 0.0 );
gtk_text_buffer_delete_mark ( text_buffer, mark );
}
}
int main ( void )
{
GtkWidget *window;
GtkWidget *scroll_window;
GtkWidget *text_view;
/// ***
gtk_init ( NULL, NULL );
/// *** Create a new Window
window = gtk_window_new ( GTK_WINDOW_TOPLEVEL );
gtk_window_set_default_size ( GTK_WINDOW ( window ), 400, 200 );
g_signal_connect ( window, "delete-event", gtk_main_quit, NULL );
g_signal_connect ( window, "destroy", gtk_main_quit, NULL );
/// ***
scroll_window = gtk_scrolled_window_new ( NULL, NULL );
gtk_container_add ( GTK_CONTAINER ( window ), scroll_window );
/// ***
GtkTextBuffer *text_buffer;
text_buffer = gtk_text_buffer_new ( NULL );
gtk_text_buffer_set_text ( text_buffer, buffer, -1 );
/// ***
text_view = gtk_text_view_new_with_buffer ( text_buffer );
gtk_container_add ( GTK_CONTAINER ( scroll_window ), text_view );
/// ***
scroll_to_iter ( text_view, text_buffer, "another" );
/// *** 15)
///scroll_to_mark ( text_view, text_buffer, "another" );
/// ***
gtk_widget_show_all ( window );
gtk_main ();
}
I was thinking that probably the Function gtk_text_buffer_place_cursor()
helps, but drops the selection and the text never gets scrolled to that position.
Thank you