Monday, May 25, 2009

GTK-Sharp: Scrollable (Window) Table Example



using System;
using Gtk;

namespace gtkTest
{

public class WinTable
{

private Gtk.ScrolledWindow sWinRef;
private Table table;

private uint rowCount, colCount, rowSpacing, colSpacing;

// ==========================================================

public WinTable(uint pRowCount, uint pColCount, uint pRowSpacing, uint pColSpacing, Gtk.ScrolledWindow pSWinRef)
{
sWinRef = pSWinRef;

rowCount = pRowCount;
colCount = pColCount;

rowSpacing = pRowSpacing;
colSpacing = pColSpacing;

bool homogeneous = true;
table = new Table (rowCount, colCount, homogeneous);

for (uint rowNum = 1; rowNum <= rowCount; rowNum++)
table.SetRowSpacing(rowNum - 1, rowSpacing);

for (uint colNum = 1; colNum <= colCount; colNum++)
table.SetColSpacing(colNum - 1, colSpacing);

sWinRef.AddWithViewport(table);
}

// ==========================================================

/* for e.g. = 2 x 2 table, say (horiz,vert), then

(0,0) (1,0) (2,0)

(0,1) (1,1) (2,1)

(0,2) (1,2) (2,2)

*/

public void PackWidgetAtXY(Widget widget, uint x, uint y)
{
uint leftAttach = x - 1;
uint rightAttach = x;
uint topAttach = y - 1;
uint bottomAttach = y;

//Gtk.AttachOptions xOptions = new Gtk.AttachOptions(); // fill, shrink, expand

table.Attach(widget, leftAttach, rightAttach, topAttach, bottomAttach);
widget.Show();
}

// ==========================================================

public void Demo()
{

for (uint rowNum = 1; rowNum <= rowCount; rowNum++)
for (uint colNum = 1; colNum <= colCount; colNum++)
{
string text = "one\ntwo\nthree\nfour";
Label label = new Label(text);
this.PackWidgetAtXY(label, rowNum, colNum);
}

this.table.Show();
}


// ==========================================================
}


//####################################################

public partial class MainWindow: Gtk.Window
{
public MainWindow (): base (Gtk.WindowType.Toplevel)
{
Build ();
}

protected void OnDeleteEvent (object sender, DeleteEventArgs a)
{
Application.Quit ();
a.RetVal = true;
}
}

//####################################################

class MainClass
{
public static void Main (string[] args)
{
Application.Init ();
MainWindow mainWin = new MainWindow ();

uint rowCount = 8, colCount = 7;
uint rowSpacing = 80, colSpacing = 200;

mainWin.ShowAll();

Window priWin = new Window("Primary Window");
priWin.SetDefaultSize(250, 200);
priWin.SetPosition(WindowPosition.Center);

Gtk.ScrolledWindow sWin = new Gtk.ScrolledWindow();
sWin.SetPolicy(Gtk.PolicyType.Always, Gtk.PolicyType.Always);

priWin.Add(sWin);
sWin.Show();

WinTable winTable = new WinTable(rowCount, colCount, rowSpacing, colSpacing, sWin);
winTable.Demo();

priWin.Show();

Application.Run ();
}
}

}

No comments:

Post a Comment

comment: