Ruby/Tk/Iwidgets tabnotebook (and scrollbar)

K

Karl Miller

Hi

I've created an iwidgets tabnotebook, with a large number of tabs,
and attached a scrollbar.

Moving the scroll bar successfully moves back and forth amongst pages,
but the subset of tabs displayed does not change.

e.g say you have 20 tabs, and there is room for 1..6 to be displayed.

As you scroll from tab #6 to tab #7, the tabs shown should change
from 1..6 to 2..7

Using notebook.select or notebook.view also has this behaviour.

Am I doing something wrong?

Cheers
Karl

#!/usr/bin/env ruby
require 'tk'
require 'tkextlib/iwidgets'

# Create the tabnotebook widget and pack it.
tn = Tk::Iwidgets::Tabnotebook.new:)width=>300, :height=>100)
tn.pack:)anchor=>:nw, :fill=>:both, :expand=>true,
:side=>:top, :padx=>10, :pady=>0)

# Add twenty pages to the tabnotebook
1.upto(20) { |t|
txt = 'Page ' + t.to_s
tn.add:)label => txt)
}

# Add a button on page 1 to change to tab 20
button = TkButton.new(tn.child_site(0),:text =>'Go to Page 20').pack
button.command {
tn.select(19) # tn.view etc have the same problem
}

# Select the first page of the tabnotebook.
tn.select(0)

# Create the scrollbar
# and the notebook together, then pack the scrollbar
tn.xscrollbar(TkScrollbar.new).pack:)fill=>:x,
:expand=>true, :padx=>10)

Tk.mainloop
 
H

Hidetoshi NAGAI

From: Karl Miller <[email protected]>
Subject: Ruby/Tk/Iwidgets tabnotebook (and scrollbar)
Date: Tue, 29 Jan 2008 10:55:26 +0900
Message-ID: said:
I've created an iwidgets tabnotebook, with a large number of tabs,
and attached a scrollbar.

Moving the scroll bar successfully moves back and forth amongst pages,
but the subset of tabs displayed does not change. (snip)
Am I doing something wrong?

You must control start position of the tabset.
#!/usr/bin/env ruby
require 'tk'
require 'tkextlib/iwidgets'

# Create the tabnotebook widget and pack it.
tn = Tk::Iwidgets::Tabnotebook.new:)width=>300, :height=>100)
tn.pack:)anchor=>:nw, :fill=>:both, :expand=>true,
:side=>:top, :padx=>10, :pady=>0)

# Add twenty pages to the tabnotebook
1.upto(20) { |t|
txt = 'Page ' + t.to_s
tn.add:)label => txt)
}

# Command to control the tabset
tabset = tn.component_widget('tabset')
tabset.command{|idx|
left = tabset.tabcget(idx, :left)
prev = tabset.index(idx) - 1
if prev < 0
tabset.start = 0
elsif left < 0
tabset.start -= left
elsif left + (tabwidth = tabset.tabcget(idx,:width)) > (tabset_width = tabset.width)
tabset.start -= left - tabset_width + 2*(left - tabset.tabcget(prev,:left)) - tabwidth + 1
end
}
 
H

Hidetoshi NAGAI

Probably the following is better. Sorry.
#!/usr/bin/env ruby
require 'tk'
require 'tkextlib/iwidgets'

# Create the tabnotebook widget and pack it.
tn = Tk::Iwidgets::Tabnotebook.new:)width=>300, :height=>100)
tn.pack:)anchor=>:nw, :fill=>:both, :expand=>true,
:side=>:top, :padx=>10, :pady=>0)

# Add twenty pages to the tabnotebook
1.upto(20) { |t|
txt = 'Page ' + t.to_s
tn.add:)label => txt)
}

# Command to control the tabset
class Tk::Iwidgets::Tabnotebook
def show_tab(idx)
if (prev = @tabset.index(idx) - 1) < 0
@tabset.start = 0
return
end
case cget:)tabpos)
when 's', 'n'
poskey = :left
szkey = :width
when 'e', 'w'
poskey = :top
szkey = :height
end
if (hd = @tabset.tabcget(idx, poskey)) < 0
@tabset.start -= hd
elsif hd + (sz = @tabset.tabcget(idx, szkey)) > (tabs_sz = @tabset[szkey])
@tabset.start -= hd - tabs_sz + 2*(hd - @tabset.tabcget(prev, poskey)) - sz + 1
end
end
end

tn.component_widget('tabset').command{|idx|
tn.view(idx)
tn.show_tab(idx)
}
 
H

Hidetoshi NAGAI

From: Karl Miller <[email protected]>
Subject: Re: Ruby/Tk/Iwidgets tabnotebook (and scrollbar)
Date: Wed, 6 Feb 2008 07:53:53 +0900
Message-ID: said:
Yay! This worked very well. Thank you :)

The previous version does not work with some of widget options
(e.g. tn.equaltabs = false, tn.gap = 2, and so on).
And, 'show_tab' method should be implemented on Tk::Iwidgets::Tabset.
The following is the final version (I hope so ;-)).
If it has no problem, I'll add 'show_tab' method to official files.

==================================================================
# Command to control the tabset
class Tk::Iwidgets::Tabnotebook
def show_tab(idx)
@tabset.show_tab(idx)
self
end
end

class Tk::Iwidgets::Tabset
def show_tab(idx)
if index(idx) == 0
self.start = 0
return
end

reutrn unless @canvas ||= self.winfo_children[0]

delta = 1 if (delta = cget:)gap)) == 'overlap' ||
(delta = self.winfo_pixels(delta) + 1) <= 0

case cget:)tabpos)
when 's', 'n'
if (head = tabcget(idx, :left)) < 0
self.start -= head
return
end
tabs_size = @canvas.winfo_width
tab_start, tab_end = @canvas .
find_overlapping(head, 0, head + delta, @canvas.winfo_height) .
find_all{|id| @canvas.itemtype(id) == TkcPolygon} .
map!{|id| bbox = @canvas.bbox(id); [bbox[0], bbox[2]]} . max

when 'e', 'w'
if (head = tabcget(idx, :top)) < 0
self.start -= head
return
end
tabs_size = @canvas.winfo_height
tab_start, tab_end = @canvas .
find_overlapping(0, head, @canvas.winfo_width, head + delta) .
find_all{|id| @canvas.itemtype(id) == TkcPolygon} .
map!{|id| bbox = @canvas.bbox(id); [bbox[1], bbox[3]]} . max
end

if (size = tab_end - tab_start + 1) > tabs_size
self.start -= tab_start
elsif head + size > tabs_size
self.start -= head + size - tabs_size
end

self
end
end

tn.component_widget('tabset').command{|idx|
tn.view(idx)
tn.show_tab(idx)
}

# An example to following to resizing of the Tabnotebook widget
tn.bind('Configure'){
tn.show_tab:)select)
}
==================================================================
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,058
Latest member
QQXCharlot

Latest Threads

Top