Vala中的自定义GTK小部件无法正常工作

时间:2013-04-07 08:50:49

标签: widget gtk vala

我正在尝试在Vala中创建一个自定义的GTK小部件,但我已经在第一次基本尝试时失败了,所以我想知道我哪里出错了一些帮助。我觉得我必须遗漏一些非常明显的东西,但我只是看不到它。

我有三个文件,其中包含以下内容:

start.vala

using Gtk;

namespace WTF
{
        MainWindow main_window;

        int main(string[] args)
        {
                Gtk.init(ref args);
                main_window = new MainWindow();
                Gtk.main();
                return 0;
        }
}

main_window.vala

using Gtk;

namespace WTF
{
        public class MainWindow : Window
        {
                public MainWindow()
                {
                        /* */
                        Entry entry = new Entry();
                        entry.set_text("Yo!");
                        this.add(entry);
                        /* */

                        /* 
                        CustomWidget cw = new CustomWidget();
                        this.add(cw);
                        /* */

                        this.window_position = WindowPosition.CENTER;
                        this.set_default_size(400, 200);
                        this.destroy.connect(Gtk.main_quit);

                        this.show_all();
                }
        }
}

custom_widget.vala

using Gtk;

namespace WTF
{
        public class CustomWidget : Bin
        {
                public CustomWidget()
                {
                        Entry entry = new Entry();
                        entry.set_text("Yo");
                        this.add(entry);
                        this.show_all();
                }
        }
}

如您所见,在main_window.vala中,我有两组代码。一个直接添加Entry小部件,另一个添加我的自定义小部件。如果您运行直接添加Entry小部件的那个,则会得到以下结果:

functioning widget

但是,如果您使用自定义窗口小部件运行该窗口,则会得到以下结果:

malfunctioning widget

仅供记录,这是我使用的复杂命令:

valac --pkg gtk+-2.0 start.vala main_window.vala custom_widget.vala -o wtf

修改

根据user4815162342的建议,我在自定义Bin小部件上实现了size_allocate方法,如下所示:

public override void size_allocate(Gdk.Rectangle r)
{
        stdout.printf("Size_allocate: %d,%d ; %d,%d\n", r.x, r.y, r.width, r.height);
        Allocation a = Allocation() { x = r.x, y = r.y, width = r.width, height = r.height };
        this.set_allocation(a);

        stdout.printf("\tHas child: %s\n", this.child != null ? "true" : "false");
        if (this.child != null)
        {
                int border_width = (int)this.border_width;
                Gdk.Rectangle cr = Gdk.Rectangle()
                {
                        x = r.x + border_width,
                        y = r.y + border_width,
                        width = r.width - 2 * border_width,
                        height = r.height - 2 * border_width
                };
                stdout.printf("\tChild size allocate: %d,%d ; %d, %d\n", cr.x, cr.y, cr.width, cr.height);
                this.child.size_allocate(cr);
        }
}

它在控制台中写下以下内容:

Size_allocate: 0,0 ; 400,200
        Has child: true
        Child size allocate: 0,0 ; 400, 200

窗口呈现:

partially functioning widget

1 个答案:

答案 0 :(得分:2)

GtkBin是一个抽象的单子容器,通常用于以某种方式装饰子窗口小部件,或者更改其可见性或大小。没有一些附加值,单子容器与它包含的小部件无法区分,因此不是很有用。

由于GtkBin不知道您将围绕孩子绘制什么样的装饰,因此它希望您实现自己的size_allocategtk_event_area_size_allocate中提供了一个简单的实现,gtk_button_size_allocate中的一个更复杂的实现。

This answer显示PyGTK中的最小size_allocate实现,应该可以直接移植到Vala。如果你做了比这更复杂的事情,你还需要实现expose,以及可能的其他方法,但这会让你开始。

相关问题