Search code examples
widgetgtkexpand

Gtk: How to prevent widget expanding beyond Width/Height request inside table view


I have made a widget which is essentially an Icon. This icon widget has Width/Height request of 150 px. I want to place these icons (programatically) inside a Table view, as below:

Desired alignment and sizing of cells in the table view

Instead what I get is this:

The resulting output

How can I force the table view not to expand each icon widget? My code for creating the icon widgets and adding them to the table is as follows:

    iconTable.Add (new MatchDayManager.SceneIconCellWidget (new MDMScene ("A Final Scene Name")));
    iconTable.NRows = noOfRows;
    iconTable.SetSizeRequest (-1, cellHeight * noOfRows);
    iconTable.CheckResize();
    ShowAll ();

Solution

  • Figured it out - I was using the iconTable.Add(...) function, when in actual fact, you should be using the iconTable.Attach(...) function.

    With the latter, you can specify the cell the item should be in using the leftAttach, rightAttach, topAttach and bottomAttach parameters. Think of these as a cell index describing which cell to place the widget from and to.

    If I wanted a widget to appear in the lower right of a 2x2 grid, I would use 1,2,1,2.

    ...this means the widget would go between row 1 and 2, and between column 1 and 2. It's done like this so that you can allow widgets to span over multiple cells e.g. by using 1, 4, 1, 2.

    My code is then as follows:

    iconTable.Attach(new Gtk.Widget ... , 0, 1,  0, 1);