简体   繁体   English

如何让 GtkLayout 滚动?

[英]How to make GtkLayout scroll?

My problem is related to GtkLayout, which, I have read is responsible for creating infinite scrollable areas with widgets.我的问题与 GtkLayout 有关,我读过它负责使用小部件创建无限可滚动区域。 I've created one using the following code:我使用以下代码创建了一个:

    GtkWidget *window;
    GtkWidget *button1;
    GtkWidget *button2;
    GtkWidget *layout;

    window = gtk_application_window_new (app);
    gtk_window_set_title (GTK_WINDOW (window), "Window wowowwow");
    gtk_window_set_default_size (GTK_WINDOW (window), 200, 200);



    layout = gtk_layout_new(NULL, NULL);
    gtk_container_add (GTK_CONTAINER (window), layout);

    button1 = gtk_button_new_with_label ("Hello World");
    g_signal_connect (button1, "clicked", G_CALLBACK (print_hello), NULL);
    g_signal_connect_swapped (button1, "clicked", G_CALLBACK (gtk_widget_destroy), window);
    gtk_layout_put(GTK_LAYOUT(layout), button1, 20,20);

    button2 = gtk_button_new_with_label ("Button 2");
    g_signal_connect_swapped (button2, "clicked", G_CALLBACK (gtk_widget_destroy), button2);
    gtk_layout_put(GTK_LAYOUT(layout), button2, 200,200);
    
    GtkAdjustment *hadjustment = gtk_adjustment_new(0, 0, 1000, 1, 1, 20);
    gtk_layout_set_size(GTK_LAYOUT(layout), 1000,1000);
    gtk_scrollable_set_hadjustment(GTK_SCROLLABLE(layout), hadjustment);

    gtk_widget_show_all (window);

This shows a window that looks like following:这显示了一个 window,如下所示:

Window initial Window 初始

.. and when resized: .. 调整大小时:

Window resized Window 调整大小

The problem is, I want the contents of GtkLayout to be scrollable.问题是,我希望 GtkLayout 的内容是可滚动的。 I think the problem lies within the adjustment object, that I have to configure, but I don't know what exactly the parameters mean and the documentation is very scarce about this.我认为问题出在我必须配置的调整 object 上,但我不知道参数的确切含义,并且文档非常稀缺。 So, I'm looking here forward for help.所以,我期待在这里寻求帮助。

To make it work I had to put the GtkLayout inside a GtkScrolledWindow and override the default dimensions of the layout.为了使它工作,我必须将 GtkLayout 放在 GtkScrolledWindow 中并覆盖布局的默认尺寸。 The code that works looks like following:有效的代码如下所示:

#include <gtk/gtk.h>

static void
print_hello (GtkWidget *widget,
             gpointer   data)
{
  g_print ("Hello World\n");
}

static void
activate (GtkApplication *app,
          gpointer        user_data)
{
    GtkWidget *window;
    GtkWidget *scrolled_window;
    GtkWidget *layout;

    window = gtk_application_window_new (app);
    gtk_window_set_title (GTK_WINDOW (window), "Window");
    gtk_window_set_default_size (GTK_WINDOW (window), 200, 200);

    scrolled_window = gtk_scrolled_window_new(NULL, NULL);
    gtk_container_add (GTK_CONTAINER (window), scrolled_window);

    layout = gtk_layout_new(NULL, NULL);
    gtk_container_add(GTK_CONTAINER(scrolled_window), layout);

    int i = 0;

    while (i < 10) {
        GtkWidget *a_button;
        a_button = gtk_button_new_with_label("Button");
        gtk_layout_put(GTK_LAYOUT(layout), a_button, 10, 10 + 40 * i);
        i += 1;
    }

    gtk_layout_set_size(GTK_LAYOUT(layout), 1000,1000);

    gtk_widget_show_all (window);
}

int
main (int    argc,
      char **argv)
{
      GtkApplication *app;
      int status;

      app = gtk_application_new ("org.gtk.example", G_APPLICATION_FLAGS_NONE);
      g_signal_connect (app, "activate", G_CALLBACK (activate), NULL);
      status = g_application_run (G_APPLICATION (app), argc, argv);
      g_object_unref (app);

      return status;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM