Multiple-item widgets

来源:互联网 发布:php oa办公系统源码 编辑:程序博客网 时间:2024/04/26 05:15

学习gtkmm有几周了,英语太烂了,硬着头皮边看边记,还没把握,发上来,大家斧正

Multiple-item widgets

多项容器控件

Multiple-item widgets inherit from Gtk::Container; just as with Gtk::Bin, you use the add() and remove() methods to add and remove contained widgets. Unlike Gtk::Bin::remove(), however, the remove() method for Gtk::Container takes an argument, specifiying which widget to remove.

多项容器控件继承自Gtk::Container;象Gtk::Bin,使用add()remove()方法添加和移除被包含的子控件。但与Gtk::Bin::remove()不一样,Gtk::Container::remove()带有一个参数,指定移除哪个子控件。

§  7.2.1. Packing

§  7.2.2. An improved Hello World

§  7.2.3. Boxes

§  7.2.4. ButtonBoxes

§  7.2.5. Grid

§  7.2.6. Table

§  7.2.7. Notebook

§  7.2.8. Assistant

7.2.1. Packing

You've probably noticed that gtkmm windows seem "elastic" - they can usually be stretched in many different ways. This is due to the widget packing system.

你大概注意到gtkmm的窗口好像“橡皮面”——它们常常有多种方式被拉伸压缩。这得益于widget packing system(控件包装系统)

Many GUI toolkits require you to precisely place widgets in a window, using absolute positioning, often using a visual editor. This leads to several problems:

许多GUI工具包都需要你在窗口中仔细地放置控件,大多数情况下在可视化编辑工具中使用绝对位置放置控件。这将导致几个问题:

§  The widgets don't rearrange themselves when the window is resized. Some widgets are hidden when the window is made smaller, and lots of useless space appears when the window is made larger. 当窗口大小改变时控件不能重新排布。窗口太小时有些控件看不见,太大时又有许多空白区域没有被利用。

§  It's impossible to predict the amount of space necessary for text after it has been translated to other languages, or displayed in a different font. On Unix it is also impossible to anticipate the effects of every theme and window manager. 预测文本翻译成其它语言或改变显示字体后需占用(显示)空间的大小是不可能的。在Unix上预测每个主题和管理窗口的效果也是不可能的。

§  Changing the layout of a window "on the fly", to make some extra widgets appear, for instance, is complex. It requires tedious recalculation of every widget's position. 浮动窗口的显示层改变时,会有些额外的控件出现,如:组合控件。要对每个控件的位置进行单调的繁杂的重复计算。

gtkmm uses the packing system to solve these problems. Rather than specifying the position and size of each widget in the window, you can arrange your widgets in rows, columns, and/or tables. gtkmm can size your window automatically, based on the sizes of the widgets it contains. And the sizes of the widgets are, in turn, determined by the amount of text they contain, or the minimum and maximum sizes that you specify, and/or how you have requested that the available space should be shared between sets of widgets. You can perfect your layout by specifying padding distance and centering values for each of your widgets. gtkmm then uses all this information to resize and reposition everything sensibly and smoothly when the user manipulates the window.

gtkmm使用包装系统解决了这些问题。在指定窗口中每个控件的位置和大小时,你可以按行、列或表来排列控件。gtkmm能根据包含的子控件的大小自动确定窗口的大小。并且子控件大小是依次决定于它们包含文本的多少、你设定的最大最小值、你设定允许子控件共享可用空间。你可通过设定每个控件的相互间隔、中心值来优化布局。gtkmm顺利合理地利用所用这些信息让用户能熟练地控制窗口。

gtkmm arranges widgets hierarchically, using containers. A Container widget contains other widgets. Most gtkmm widgets are containers. Windows, Notebook tabs, and Buttons are all container widgets. There are two flavours of containers: single-child containers, which are all descendants of Gtk::Bin, and multiple-child containers, which are descendants of Gtk::Container. Most widgets in gtkmm are descendants of Gtk::Bin, including Gtk::Window.

gtkmm利用容器分层次放置子控件。容器控件可包含其它子控件。gtkmm控件大多是容器。Windows, Notebook tabs, Buttons都是容器控件。有两种特性的容器:所有Gtk::Bin的子类都是单项容器;所有Gtk::Container的子类都是多项容器。gtkmm中的大多数控件都是Gtk::Bin的子类,包括 Gtk::Window

Yes, that's correct: a Window can contain at most one widget. How, then, can we use a window for anything useful? By placing a multiple-child container in the window. The most useful container widgets are Gtk:Grid, Gtk:VBox, Gtk::HBox, and Gtk::Table.

是这样的:一个window最多放一个控件。那么我们能用window干什么事?那就是在window中放置多项容器。常用的容器控件有Gtk:Grid, Gtk:VBox, Gtk::HBox, Gtk::Table.

§  Gtk::Grid arranges its child widgets in rows and columns. Use attach(), attach_next_to() and add() to insert child widgets.
 
Gtk::Grid 在行和列中放置它的子控件。 使用 attach(), attach_next_to() add() 插入子控件。

§  Gtk::VBox and Gtk::HBox arrange their child widgets vertically and horizontally, respectively. Use pack_start() and pack_end() to insert child widgets.
Gtk::VBox Gtk::HBox各自垂直的和水平的放置子控件。使用 pack_start() pack_end()插入子控件。

§  Gtk::Table arranges its widgets in a grid. Use attach() to insert widgets.
Gtk::Table在表格的格子里放置它的子控件。使用attach()插入子控件。

There are several other containers, which we will also discuss.

还有几个其它容器,在这一起讨论。

If you've never used a packing toolkit before, it can take some getting used to. You'll probably find, however, that you don't need to rely on visual form editors quite as much as you might with other toolkits.

如果以前从未使用过包装工具集,你可能需要一定的时间来习惯。你可能会发现,不论怎样你都无需象其它一些工具包那样过多地依赖编辑器的可视化支持。

7.2.2. An improved Hello World

Let's take a look at a slightly improved helloworld, showing what we've learnt.

让我们看一看一个稍加改进的helloworld,看我们学的什么。

7-6 Hello World 2

 

源代码

 

 

After building and running this program, try resizing the window to see the behaviour. Also, try playing with the options to pack_start() while reading the Boxes section.

构建并运行这个程序后,试试改变window看看反应。阅读Boxes一节是考虑用选项参数使用pack_start()

 

原创粉丝点击