# Best way to create table-like lists?

**URL:** <https://www.ruby-forum.com/t/best-way-to-create-table-like-lists/107577>\
**Category:** Ruby-Gnome 2\
**Created:** [June 23, 2007, 4:16pm UTC](https://www.ruby-forum.com/t/best-way-to-create-table-like-lists/107577 "2007-06-23T16:16:10Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![joaz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/919ad9/32.png) [@joaz](https://www.ruby-forum.com/u/joaz)\
**Post date:** [June 23, 2007, 4:16pm UTC](https://www.ruby-forum.com/t/best-way-to-create-table-like-lists/107577/1 "2007-06-23T16:16:10Z")

</div>

Hi,

I tried different approaches to create table-like lists. What I need

- a head (1-3 rows), non-scrollable
- a scrollable body
- head and body cells must align

My first draft was:  
VBox  
-\> Table (for head), childs are labels  
-\> Scrolledwindow  
—\> Viewport  
------\> Table (for data), childs are labels

The problem is that head table and body table are not aligned. For  
example because the scrollbars which are not present in head table.

My next attempt is this class, using a Layout object allowing me to  
attach object in a fixed position.

Draft:  
VBox  
-\> Layout (for head)  
—\> Labels (fixed position)  
-\> Scrollwindow  
—\> Layout (for body)  
-----\> Labels (fixed position)

#table.rb  
class Table

attr\_accessor :widths  
attr\_accessor :number\_of\_columns  
attr\_accessor :height  
attr\_accessor :x  
attr\_accessor :y  
attr\_accessor :color  
attr\_accessor :head\_data  
attr\_accessor :head  
attr\_accessor :body  
attr\_accessor :body\_scrollwindow  
attr\_accessor :x\_pos #bad name should be p pos in pixel

def initialize(widths)  
set\_column\_widths(widths)  
self.height = 16  
self.x = 0  
self.y = 0  
set\_color

```
# Head
self.head = Gtk::Layout.new

# Body in a scrollwindow
scrollwindow = Gtk::ScrolledWindow.new
self.body =

```

Gtk::Layout.new(scrollwindow.hadjustment,scrollwindow.vadjustment)  
scrollwindow.add body  
self.body\_scrollwindow = scrollwindow

end

def set\_column\_widths(widths)  
self.widths = widths  
self.number\_of\_columns = widths.size  
self.x\_pos = []  
cur = 0  
i = 0  
widths.each do |w|  
self.x\_pos[i] = cur  
cur = cur + w  
i = i + 1  
end  
end

def head\_data(data)  
self.head\_data = data  
data.each do |h|  
add\_head\_label(h)  
end  
end

def add(str)  
label = Gtk::Label.new(str.to\_s)  
label.set\_alignment(0,0)  
label.width\_request = self.widths[self.x]  
label.height\_request = self.height  
eventbox = Gtk::EventBox.new  
eventbox \<\< label  
eventbox.modify\_bg(Gtk::STATE\_NORMAL,self.color)  
add\_widget(eventbox)  
end

def add\_widget(widget,target=self.body)  
target.put(widget,self.x\_pos[self.x],self.height\*self.y)  
self.x = self.x + 1  
check\_if\_linebreak  
end

def check\_if\_linebreak  
if self.x \>= self.number\_of\_columns  
self.x = 0  
self.y = self.y + 1  
set\_color  
end  
end

def attach(box)  
total\_width = 0  
self.widths.each{|w| total\_width = total\_width + w}  
total\_height = y \* self.height

```
self.head.height_request = self.height
self.body.set_size(total_width,total_height)

box.pack_start(self.head,false,true,0)
box.pack_start(self.body_scrollwindow,true,true,0)

box.show_all

```

end

private  
def set\_color  
if self.y % 2 == 0  
self.color = Gdk::Color::parse("#cccccc")  
else  
self.color = Gdk::Color::parse("#dddddd")  
end  
end

def add\_head\_label(str)  
label = Gtk::Label.new(str.to\_s)  
label.set\_alignment(0,0)  
label.width\_request = self.widths[self.x]  
label.height\_request = self.height  
eventbox = Gtk::EventBox.new  
eventbox \<\< label  
eventbox.modify\_bg(Gtk::STATE\_NORMAL,Gdk::Color::parse("#cacaca"))  
add\_widget(eventbox,self.head)  
end

end

This is a test program for this table:

# test.rb

require ‘gtk2’  
require ‘table.rb’

window = Gtk::Window.new  
head = [“Test”,“Test2”,“next”,“something else”,“Hello World”]  
table = Table.new([40,55,55,100,150])  
table.head\_data(head)  
100.times do  
table.add(“test”)  
table.add(“test…”)  
table.add("…")  
table.add(“test”)  
table.add(“lorem ipsum”)  
end  
vbox = Gtk::VBox.new

table.attach(vbox)  
window.add(vbox)  
window.set\_default\_size(640,480)

window.show\_all

Gtk.main

What’s annoying me is that I have a gap between head and body of the  
table. It seems to be in the scrolledwindow above the Layout object.  
How to debug this?

Are there any other proposals to do this kind of table? I’ve seen that  
gtk-extra has a spreadsheet widget, but I haven’t seen any ruby bindings  
for it and/or a win32 binary (which is unfortunately mandatory).

---

<div class="post-metadata">

**Author:** ![joaz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/919ad9/32.png) [@joaz](https://www.ruby-forum.com/u/joaz)\
**Post date:** [June 23, 2007, 4:41pm UTC](https://www.ruby-forum.com/t/best-way-to-create-table-like-lists/107577/2 "2007-06-23T16:41:08Z")

</div>

Am Samstag, den 23.06.2007, 16:16 +0200 schrieb joaz:

> Hi,
> 
> I tried different approaches to create table-like lists. What I need
> 
> - a head (1-3 rows), non-scrollable
> - a scrollable body
> - head and body cells must align

Hi,

you can use the Gtk::TreeView as the display widget and the  
Gtk::ListStore for your data.

Cheers  
detlef

* * *

This [SF.net](http://SF.net) email is sponsored by DB2 Express  
Download DB2 Express C - the FREE version of DB2 express and take  
control of your XML. No limits. Just data. Click to get it now.  
[http://sourceforge.net/powerbar/db2/](http://sourceforge.net/powerbar/db2/)

---

<div class="post-metadata">

**Author:** ![joaz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/919ad9/32.png) [@joaz](https://www.ruby-forum.com/u/joaz)\
**Post date:** [June 24, 2007, 3:57pm UTC](https://www.ruby-forum.com/t/best-way-to-create-table-like-lists/107577/3 "2007-06-24T15:57:00Z")

</div>

Detlef R. wrote:

> Hi,
> 
> you can use the Gtk::TreeView as the display widget and the  
> Gtk::ListStore for your data.
> 
> Cheers  
> detlef  
> Hi Detlef,

Thanks for your suggestion.

I made this list:

# test.rb

require ‘gtk2’

window = Gtk::Window.new

content\_types = Integer, String, String, String, Float  
liststore = Gtk::ListStore.new(\*content\_types)  
i = 0  
100.times do  
iter = liststore.append  
iter[0] = i # row identified  
iter[1] = “test#{i}”  
iter[2] = “next#{i}”  
iter[3] = “lorem ipsum#{i}”  
iter[4] = rand(10000).to\_f / 100

i = i + 1  
end

view = Gtk::TreeView.new(liststore)  
view.enable\_search=true  
view.search\_column=2

# render columns

i = 1  
[“test”,“super”,“perfect”,“lorem”].each do |name|  
renderer = Gtk::CellRendererText.new  
col = Gtk::TreeViewColumn.new(name, renderer, :text =\> i)  
col.sort\_column\_id = i  
col.resizable = true  
view.append\_column(col)

i = i + 1  
end

# double click on row

view.signal\_connect(“row-activated”) do |view, path, column|  
puts “Row #{path.to\_str} was clicked!”

if iter = view.model.get\_iter(path)  
puts “Double-clicked row contains id #{iter[0]}!”  
end  
end

# show in a scrolled window

scrolledwindow = Gtk::ScrolledWindow.new.add view

window.add scrolledwindow  
window.set\_default\_size(640,480)

window.show\_all

Gtk.main

That works nicely so far. But I need another feature: The application  
should be API based and gets an already filtered list from the server.

That means I want to have a text field for searching above or below  
every column head.  
Is there any way to include or stick this to the table head?

I’ve seen the Gtk::CellEditable interface, but this has to stick with  
the Head of the table. It may not disappear when scrolling.

Any ideas?

---

<div class="post-metadata">

**Author:** ![joaz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/919ad9/32.png) [@joaz](https://www.ruby-forum.com/u/joaz)\
**Post date:** [June 28, 2007, 1:30am UTC](https://www.ruby-forum.com/t/best-way-to-create-table-like-lists/107577/4 "2007-06-28T01:30:24Z")

</div>

It looks like the TreeViewColumn object has a widget property, that you  
can assign instead of a title. This should let you customize your  
headers as much as you like. Like put a vbox in the title containing a  
Gtk::Label and a Gtk::Entry(which you can rig up to search with signals  
and whatnot)

> That works nicely so far. But I need another feature: The application  
> should be API based and gets an already filtered list from the server.
> 
> That means I want to have a text field for searching above or below  
> every column head.  
> Is there any way to include or stick this to the table head?
> 
> I’ve seen the Gtk::CellEditable interface, but this has to stick with  
> the Head of the table. It may not disappear when scrolling.
> 
> Any ideas?

---

<div class="post-metadata">

**Author:** ![joaz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/919ad9/32.png) [@joaz](https://www.ruby-forum.com/u/joaz)\
**Post date:** [June 28, 2007, 4:28pm UTC](https://www.ruby-forum.com/t/best-way-to-create-table-like-lists/107577/5 "2007-06-28T16:28:37Z")

</div>

Conan R. wrote:

> It looks like the TreeViewColumn object has a widget property, that you  
> can assign instead of a title. This should let you customize your  
> headers as much as you like. Like put a vbox in the title containing a  
> Gtk::Label and a Gtk::Entry(which you can rig up to search with signals  
> and whatnot)

I tried this:  
col = Gtk::TreeViewColumn.new(name, renderer, :text =\> i)  
col.sort\_column\_id = i  
col.resizable = true

box = Gtk::VBox.new  
box \<\< Gtk::Entry.new

box \<\< Gtk::Label.new(name)  
box.show\_all  
col.set\_widget(box)

The problem is, that the Gtk::Entry is not editable at all because it  
catches any mouse click on the header for the sorting function.

I think I’ll switch to a sidebar for searching.

---

<div class="post-metadata">

**Author:** ![joaz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/919ad9/32.png) [@joaz](https://www.ruby-forum.com/u/joaz)\
**Post date:** [June 28, 2007, 5:30pm UTC](https://www.ruby-forum.com/t/best-way-to-create-table-like-lists/107577/6 "2007-06-28T17:30:49Z")

</div>

Hmm, that make sense. You could always disable the sorting(hopefully  
then it would forward all clicks to the widget) and add another widget  
to the vbox for sorting, maybe a button or something. But at this  
point, it probably does make more sense to have a sidebar.

joaz wrote:

> I tried this:  
> col = Gtk::TreeViewColumn.new(name, renderer, :text =\> i)  
> col.sort\_column\_id = i  
> col.resizable = true
> 
> box = Gtk::VBox.new  
> box \<\< Gtk::Entry.new
> 
> box \<\< Gtk::Label.new(name)  
> box.show\_all  
> col.set\_widget(box)
> 
> The problem is, that the Gtk::Entry is not editable at all because it  
> catches any mouse click on the header for the sorting function.
> 
> I think I’ll switch to a sidebar for searching.
