当我在GtkScrolledWindow中有GtkTextView时,当用户在窗口小部件的底部添加新行时,它会自动滚动。当我把GtkTextView放在GtkBox然后在GtkScrolledWindow中我不能工作。我需要在GtkTextView和滚动条之间放置框,如果不将整个东西放在另一个框中,我就不能这样做。在GtkScrolledWindow中使用GtkBox时,有什么办法可以保留自动滚动行为吗?
有代码可以解决我的问题:
#include <gtk/gtk.h>
int main(int argc, char *argv[]) {
GtkWidget *window;
gtk_init(&argc, &argv);
window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_window_set_default_size(GTK_WINDOW(window),
gdk_screen_width()*0.5, gdk_screen_height()*0.5);
GtkWidget *main = gtk_box_new(0, 0);
gtk_container_add(GTK_CONTAINER(window), main);
/*/////////////////////////////////////////////////*/
/* IMPORTANT PART */
/* FIRST CASE (this one works correctly) */
GtkWidget *scrolled_window_first = gtk_scrolled_window_new(NULL, NULL);
GtkWidget *text_view_first = gtk_text_view_new();
gtk_container_add(GTK_CONTAINER(scrolled_window_first), text_view_first);
gtk_box_pack_start(GTK_BOX(main), scrolled_window_first, 1, 1, 0);
/* SECOND CASE (there is no auto scroll which I need) */
GtkWidget *scrolled_window_second = gtk_scrolled_window_new(NULL, NULL);
GtkWidget *text_view_second = gtk_text_view_new();
GtkWidget *box_from_second_example = gtk_box_new(0, 0);
GtkWidget *example_box_before_scroller = gtk_box_new(0,0);
GtkWidget *example_label = gtk_label_new("I need this box badly!");
gtk_box_pack_start(GTK_BOX(box_from_second_example), text_view_second, 1, 1, 0);
gtk_container_add(GTK_CONTAINER(scrolled_window_second), box_from_second_example);
gtk_container_add(GTK_CONTAINER(box_from_second_example), example_box_before_scroller);
gtk_box_pack_start(GTK_BOX(main), scrolled_window_second, 1, 1, 0);
/* END IMPORTANT PART */
/*/////////////////////////////////////////////////*/
gtk_container_add(GTK_CONTAINER(example_box_before_scroller), example_label);
g_signal_connect(GTK_WINDOW(window), "destroy", G_CALLBACK(gtk_main_quit), NULL);
gtk_widget_show_all(window);
gtk_main ();
return 0;
}
答案 0 :(得分:1)
您是否尝试修改textview默认创建的GtkAdjustment
GtkScrolledWindow
实现了GtkScrollable
afaik。
GtkAdjustment * gtk_scrollable_get_hadjustment (GtkScrollable *scrollable);
我认为最好的方法是检测GtkTextBuffer
的插入(通过
GtkTextBuffer * gtk_text_view_get_buffer (GtkTextView *text_view);
并将回调(测试\n
)挂钩到其insert-text
g_signal并使用
void gtk_adjustment_set_value (GtkAdjustment *adjustment,
gdouble value);
修改滚动高度。
请注意,这是未经测试的,更简单的方法可能就在附近。
答案 1 :(得分:1)
问题是GtkTextView
实施GtkScrollable
,但GtkBox
没有。这不是一个小问题:如果你想要GtkBox
方式,你应该把它放在GtkViewport
里面并添加可滚动性的东西...相当一些代码,深入理解整个事情的方式滚动。
无论如何,如果你很懒,你也可以注意GtkTextview
是GtkContainer
。换句话说,您可以在GtkTextview
周围添加小部件,而无需使用GtkBox
。不完全是你想要的,但可能足以满足你的目的:
#include <gtk/gtk.h>
gint main(gint argc, gchar **argv)
{
GtkWidget *window, *scrolled_window, *text_view, *label;
gtk_init(&argc, &argv);
text_view = gtk_text_view_new();
gtk_text_buffer_set_text(gtk_text_view_get_buffer(GTK_TEXT_VIEW(text_view)),
"1\n\n\n2\n\n\n3\n\n\n4\n\n\n5\n\n\n6\n\n\n7\n\n\n8", -1);
/* Use this to set the size you want to reserve on the right */
gtk_text_view_set_border_window_size(GTK_TEXT_VIEW(text_view),
GTK_TEXT_WINDOW_RIGHT,
130);
/* Add whatever you want instead of a GtkLabel */
gtk_text_view_add_child_in_window(GTK_TEXT_VIEW(text_view),
gtk_label_new("You badly need this"),
GTK_TEXT_WINDOW_RIGHT,
0, 0);
scrolled_window = gtk_scrolled_window_new(NULL, NULL);
gtk_container_add(GTK_CONTAINER(scrolled_window), text_view);
window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_window_set_default_size(GTK_WINDOW(window), 320, 240);
gtk_container_add(GTK_CONTAINER(window), scrolled_window);
gtk_widget_show_all(window);
gtk_main ();
return 0;
}