2013-09-19

Programming - part 1: Gtktables

After not having blogged for months (and being embarrassed after
the large number of blogs from last year) I decided to blog again,
And I also decided, after not having done so in a serious (more or less)
manner for several years, to work on a programming project,
and maybe actually get it doing what I wanted to, in other words, finish it.

I am a Linux geek, so I am using the Gnome and Gtk
libraries to create my masterpiece ( :) ).

I program in c mostly, when I am programming
large projects.

One of the problems I ran accross was understanding tables.

Consider this actual c source code:

GtkWidget * table;

table = gtk_table_new(5,3,FALSE);

This is easy enough to understand: in simple terms, make a table called
(strangely enough) table. It is to be 5 rows high and 3 columns wide,
the "FALSE" indicates that it is to be "not homogeneous" (cells won't
have their size set by the size of the largest element contained in the
table).

Easy

BUT

The table documentation and all the tutorials I could find (two tutorials)
were driving me batty. Determining how to place widgets/text/whatever
in specific cells was, confusing, to say the least.

Here is an example attach command:

gtk_table_attach(GTK_TABLE(table),label,1,2,1,2,GTK_FILL,
                                                                    GTK_FILL,0,0);

OK - what did it mean??

First of all, the gtk_table_attach command places an item in a table cell.

Here is how the docs described it:

void                gtk_table_attach      (GtkTable *table,
                                           GtkWidget *child,
                                           guint left_attach,
                                           guint right_attach,
                                           guint top_attach,
                                           guint bottom_attach,
                                           GtkAttachOptions xoptions,
                                           GtkAttachOptions yoptions,
                                           guint xpadding,
                                           guint ypadding);


The docs followed with a simple example:

To make a button occupy the lower right cell of a 2x2 table, use

1 
2 
3 
4 
5 
gtk_table_attach (table, button,
                  1, 2, // left, right attach
                  1, 2, // top, bottom attach
                  xoptions, yoptions,
                  xpadding, ypadding);
 

HUH???

To my tired mind, the instructions were a little hazy.

The actual confusing bits were: 

guint left_attach,
guint right_attach,
guint top_attach,
guint bottom_attach,

For anyone else running into this, here is how I eventually came to
understand it. Consider this table:

3 columns wide.
5 rows high.
Notice, I have placed a
number at the end of each
dividing line, both vertically
and horizontally.


With example placements in each cell. (Tables in gtk are
always numbered starting at zero.)

Notice the repeating number patterns?

So, taking the number in the first cell on the top left as an example,
the first digit (0) represents the first vertical line on the left side of
the cell. The second digit (1) represents the first vertical line on the
right side of the cell.

OK so far?

The third digit (0) represents the line on the top of the cell.
The fourth digit (1) represents the line on the bottom of the cell.

Therefore this:

gtk_table_attach(GTK_TABLE(table),label,1,2,1,2,GTK_FILL,
                                                                    GTK_FILL,0,0);

means place a label into the table cell second from the left and second from
the top. To place an object (widget/text/whatever) in the very first cell
top row, top left the attach command would look like this:

gtk_table_attach(GTK_TABLE(table),label,0,1,0,1,GTK_FILL,
                                                                    GTK_FILL,0,0);

Hope that saves someone a bit of time.

γ˜γ‚ƒγΎγŸ

Blog Archive

Contributors

CatWalker

CatWalker
CatWalker at 29