我希望GtkTextView
中有一个可滚动的GtkWindow
,但我只希望GtkTextView
部分滚动而不是整个窗口。我尝试将GTKTextView
置于GtkScrolledWindow
内并将GtkScrolledWindow
放在GtkFixed
容器内,但GtkTextView
未显示。但是,当我将GtkTextView
直接放在GtkFixed
容器中时,它会显示出来。
#include <gtk/gtk.h>
GtkWidget *window, *scrolled_window, *fixed, *log_box, *button1;
GtkTextBuffer *log_box_buffer;
static void button1_clicked(GtkWidget *widget, gpointer data) {
printf("button1_clicked\n");
gtk_text_buffer_insert_at_cursor(log_box_buffer, "You clicked the button.\n", 24);
}
static void app_activate(GtkApplication *app, gpointer user_data) {
window = gtk_application_window_new(app);
gtk_window_set_title(GTK_WINDOW(window), "Window Title Here");
gtk_window_set_default_size(GTK_WINDOW(window), 700, 400);
gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
scrolled_window = gtk_scrolled_window_new(NULL, NULL);
fixed = gtk_fixed_new();
button1 = gtk_button_new_with_label("Button 1");
g_signal_connect(button1, "clicked", G_CALLBACK(button1_clicked), NULL);
log_box_buffer = gtk_text_buffer_new(NULL);
log_box = gtk_text_view_new_with_buffer(log_box_buffer);
gtk_fixed_put(GTK_FIXED(fixed), button1, 50, 50);
/* Here I tried to put the textview inside of the scrolled window and
add the scrolled window to the fixed container. The textview
doesn't show up when I do this. */
gtk_container_add(GTK_CONTAINER(scrolled_window), log_box);
gtk_fixed_put(GTK_FIXED(fixed), scrolled_window, 200, 50);
/* I also tried putting the textview directly in the fixed container.
This shows up, but obviously I can't scroll it. */
// gtk_fixed_put(GTK_FIXED(fixed), log_box, 200, 50);
gtk_container_add(GTK_CONTAINER(window), fixed);
gtk_widget_show_all(window);
}
int main(int argc, char **argv) {
GtkApplication *app;
int status;
app = gtk_application_new("the.application.id", G_APPLICATION_FLAGS_NONE);
g_signal_connect(app, "activate", G_CALLBACK(app_activate), NULL);
status = g_application_run(G_APPLICATION(app), argc, argv);
g_object_unref(app);
return status;
}
答案 0 :(得分:1)
不确定为什么要使用固定容器。如果是这样,您必须提供滚动窗口的宽度和高度,其中包含textview。我编译了你的代码并且它有效。只需添加:
gtk_widget_set_size_request (GTK_WIDGET(scrolled_window), 200, 200);
这将在滚动窗口上设置200乘200的大小。