text view lines double in display using scrollbars and set_adjustment



I have a custom search function (ie, not the builtin) used in a text
view; the search hits are highlighted and you skip between them with
ctrl-p and ctrl-n.

To facilitate this inside a ScrolledWindow, I have the following
routine:

void scrollToIter (GtkTextIter *iter) {
	GtkAdjustment *vadj = gtk_scrolled_window_get_vadjustment(
			GTK_SCROLLED_WINDOW(Scroll)), 
		*hadj = gtk_scrolled_window_get_hadjustment(
			GTK_SCROLLED_WINDOW(Scroll)); 
	GdkRectangle goal;
	gdouble x, y, 
		hpage = gtk_adjustment_get_page_size(hadj),
		vpage = gtk_adjustment_get_page_size(vadj),
		maxx = gtk_adjustment_get_upper(hadj) - hpage,
		maxy = gtk_adjustment_get_upper(vadj) - vpage;

	gtk_text_view_get_iter_location(GTK_TEXT_VIEW(TxT),iter,&goal);
	x = goal.x;
	y = goal.y - vpage/2;	/* center vertically */

	if (x<hpage) x = 0;		/* align left if possible */
	else if (x>maxx) x = maxx;
	if (y>maxy) y = maxy;

	gtk_adjustment_set_value(hadj, x);   
	gtk_adjustment_set_value(vadj, y);
}

However, when two hits are very close together -- eg, near vertically
aligned and one line apart, such as "adjustment" is in the two last
lines above -- skipping between them creates a bizarre effect: when
skipping forward/down (ctrl-n), the line with the "goal" in suddenly
repeats, eg:

	gtk_adjustment_set_value(hadj, x);   
	gtk_adjustment_set_value(vadj, y);
	gtk_adjustment_set_value(vadj, y);

At this point the cursor is on the middle line.  If you now skip back,
that line appears doubled instead:

	gtk_adjustment_set_value(hadj, x);   
	gtk_adjustment_set_value(vadj, x);
	gtk_adjustment_set_value(vadj, y);

Again, the actual cursor is on the middle line.  To deal with this I've
added the following condition:

	gdouble cury = gtk_adjustment_get_value(vadj),

	if ((y > cury+50.0) || (y < cury-50.0)) 
		gtk_adjustment_set_value(vadj, y);

Which might as well be the case anyway; I am just wondering about
this strange problem and if there is less hackish solution.  NB: the
ghost line disappears and the display appears correctly when the mouse
pointer moves out of the application window.  Also, in point of fact,
it does not happen if the two search hits are exactly vertically
aligned, as in my example, but only if they are slightly offset (say
within 5 characters).

-- 
"The angel of history[...]is turned toward the past." (Walter Benjamin)


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]