GTK+ -- from knowing nothing to knowing something (3)

来源:互联网 发布:淘宝和京东用户人群 编辑:程序博客网 时间:2024/05/23 00:00
 

About layout 

We usually have more than just one widget on out interface, so using gtk_container_add() to pack into the window is obviously not sufficient (the function may only be called once for a window).  In this case, layout containers should be used. There are a few such containers, the three ones that I used most are box, table and fixed.

Let's do a little comparison:

  1. GtkBox - easy to use (just need to call a simple function gtk_box_pack_start or gtk_box_pack_end to pack the widgets into it), automatically resizable (that is, when you maximize or resize the window, it resizes accordingly), but not convenient when some widgets should be larger or smaller than others. Therefore better used on a series of widgets that share the same size.
  2. GtkTable - a little bit more difficult than box (when packing, shoud specify the column and row where the widget starts and the number of column and row that widget extends to), but this also create the possibility to control how large a widget should be (for example, taking up only one "block" or four "block" of the table). Also, widgets inside table are also auto-resizable. But still, unless you define a table with MANY columns and rows, it's still not easy to pack the widget like exactly what you want.
  3. GtkFixed - even more difficult to use if you decide to write it manually, for it requires the x, y coordinators of the top-left corner of the widget to be set. However, if you are using something with WYSWYG function (such as Glade), it won't be a problem anymore. After that, the size of the widget may be set using gtk_widget_set_size_request (). In this way, the widget will be pack exactly on the position where you want it to be and with the size you set. In addition, one function of GtkFixed, gtk_fixed_move is also interesting. With this function, you may change the position of your widget dynamically (ex. a button clicked, the widget will move to a new place). There is only one thing - widgets inside GtkFixed are not auto-resizable. If using on widows that do not need to be resized, it would be fine, but when you resize a windwo using it, you can image the ugly look of your interface. Although there may be some ways to "cause" the widgets resize accordingly (ex. catch the resize event of the window, set the size and position of every widget accordingly), they are usually troublesome and inconvenient. At least I don't know an easy way to do it, if you have a solution, comments are warmly welcome and thanks in advance.

After understanding the layout related issues, we may start to write our own interface while getting ourselves familiar to various widgets. But don't forget a few references are always helpful on our way. Let me list a few here.

Useful Resources

  1. www.gtk.org - the official website, you can't miss it. Tutorial section has an easy-to-follow guide, with sample code, though some of the functions used are outdated. Therefore you should always check the api reference for newest stable version or the version you are coding with. The api reference has some sample code as well, though they are very limited. And remember, the hiararchy of the widgets is also something you want to check frequently, because many functions you want to use may appear on the widget's parent and parent's parent.
  2. http://scentric.net/tutorial/ - Gtk+ 2.0 Tree View Tutorial. A tutorial on treeview, a very powerful but very complicated widget in GTK.
  3. http://203.64.187.41/gtk/GTK-Index.html - a tutorial written in Traditional Chinese, with sample code that may be newer than the tutorial on the official website

While some widgets are a bit more trickier than others, I will discuss them more in the upcoming posts from a witness also new leaner's perspective.

原创粉丝点击