内容简介:(This is the third part of a series about custom widgets in GTK 4.Part 1,As we said earlier, “everything is a widget.” For example, we recommend that you use a GtkLabel instead of manually rendering a pango layout, or a GtkImage instead of manually loading
(This is the third part of a series about custom widgets in GTK 4.Part 1, part 2 ).
Widgets are recommended
As we said earlier, “everything is a widget.” For example, we recommend that you use a GtkLabel instead of manually rendering a pango layout, or a GtkImage instead of manually loading and rendering a pixbuf. Using a ready-made widget ensures that you get all of the expected behaviors, such as selection handling, context menus or hi-dpi support. And it is much easier than doing it all yourself.
Delegating Layout
The default implementations of the snapshot() and measure() functions are handling child widgets automatically. The main responsibility for a custom widget is to arrange the child widgets as required. In GTK 3, this would have been done by implementing the size_allocate() function. You can still do that. But in GTK 4, a more convenient alternative is to use a layout manager. GTK comes with a number of predefined layout managers, such as GtkBoxLayout, GtkCenterLayout, GtkGridLayout, to name just a few.
A layout manager can be set up in various ways, the easiest is to set a layout manager type in your class_init function:
gtk_widget_class_set_layout_manager_type (widget_class,
GTK_TYPE_GRID_LAYOUT);
GTK will then automatically instantiate and use a layout manager of this type.
Layout managers wrap your child widgets in their own “layout child” objects, which can have properties that affect the layout. This is a replacement for child properties. And just like child properties, you can set these “layout properties” in ui files:
<child>
<object>
<property name="label">Image:</property>
<layout>
<property name="left-attach">0</property>
</layout>
</object>
</child>
Adding children
Using templates is the most convenient way to add children to a widget. In GTK 4 that works for any widget, not just for containers. If for some reason, you need to create your child widgets manually, this is best done in your init() function:
void
demo_init (DemoWidget *demo)
{
demo->label = gtk_label_new ("Image:");
gtk_widget_set_parent (demo->label, GTK_WIDGET (demo));
}
When doing that, it is important to set up the correct parent-child relationships to make your child widgets part of the overall widget heirarchy. And this setup needs to be undone in your dispose() function:
void
demo_dispose (GObject *object)
{
DemoWidget *demo = DEMO_WIDGET (object);
g_clear_pointer (&demo->label, gtk_widget_unparent);
GTK_WIDGET_CLASS (demo_widget_parent_class)->dispose (object);
}
New possibilities
Layout managers nicely isolate the layout tasks from the rest of the widget machinery, which makes it easier to experiment with new layouts.
For example, GTK 4 includes GtkConstraintLayout, which uses a constraint solver to create layouts according to a set of constraints on widget sizes and positions.
To learn more about constraints in GTK 4, read the documentation for GtkConstraintLayout .
Outlook
In the next post, we’ll look how widgets in GTK 4 handle input.
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网
猜你喜欢:本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
智能Web算法
Haralambos Marmanis、Dmitry Babenko / 阿稳、陈钢 / 电子工业出版社 / 2011-11 / 65.00元
本书涵盖了五类重要的智能算法:搜索、推荐、聚类、分类和分类器组合,并结合具体的案例讨论了它们在Web应用中的角色及要注意的问题。除了第1章的概要性介绍以及第7章对所有技术的整合应用外,第2~6章以代码示例的形式分别对这五类算法进行了介绍。 本书面向的是广大普通读者,特别是对算法感兴趣的工程师与学生,所以对于读者的知识背景并没有过多的要求。本书中的例子和思想应用广泛,所以对于希望从业务角度更好......一起来看看 《智能Web算法》 这本书的介绍吧!