1

如何初始化GtkScrolledWindow以通过尽可能增长来避免滚动条?

默认情况下,它似乎尽可能小。

演示代码(退出SIGINT):

#include <gtk/gtk.h>

int main(int argc, char *argv[]) {
    GtkWidget* window;
    GtkWidget* content;
    GtkWidget* sw;

    gtk_init(&argc, &argv);
    window = gtk_window_new(GTK_WINDOW_TOPLEVEL);

    sw = gtk_scrolled_window_new(NULL, NULL);
    gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(sw),
            GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
    gtk_container_add(GTK_CONTAINER(window), sw);
    gtk_widget_show(sw);

    content = gtk_button_new_with_label("This is a very, very"
            "very very very very very very very very long text");
    gtk_scrolled_window_add_with_viewport(GTK_SCROLLED_WINDOW(sw), content);
    gtk_widget_show(content);

    gtk_widget_show(window);
    gtk_main();

    return 0;
}
4

2 回答 2

1

我认为这既有效又不是丑陋的黑客(很多):

int main(int argc, char *argv[]) {
    GtkWidget* window;
    GtkWidget* content;
    GtkWidget* sw;

    GtkRequisition size;
    GtkWidget* viewport;

    gtk_init(&argc, &argv);
    window = gtk_window_new(GTK_WINDOW_TOPLEVEL);

    sw = gtk_scrolled_window_new(NULL, NULL);
    gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(sw),
            GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
    gtk_container_add(GTK_CONTAINER(window), sw);
    gtk_widget_show(sw);

    content = gtk_button_new_with_label("This is a very, very"
            "very very very very very very very very long text");
    gtk_scrolled_window_add_with_viewport(GTK_SCROLLED_WINDOW(sw), content);
    gtk_widget_show(content);

    gtk_widget_show(window);

    viewport = gtk_widget_get_ancestor(content, GTK_TYPE_VIEWPORT);
    gtk_widget_size_request(viewport, &size);
    gtk_widget_set_size_request(sw, size.width, size.height);

    gtk_main();

    return 0;
}
于 2009-03-03T06:40:59.597 回答
0

我不确定执行此操作的正确方法是什么,但我发现最适合我的是设置窗口的大小,并且其中包含的小部件通常会正确调整大小:

gtk_window_set_default_size(GTK_WINDOW(window), 1000, 500);

或者,您可以设置 GtkScrolledWindow 的大小:

gtk_widget_set_size_request(window, 500, 250);

请注意,特别是在最后一种情况下,在计算像素大小时,可能必须考虑本地化、字体大小和其他此类细节。

于 2009-01-15T09:39:05.237 回答