Ruby/Tk/Iwidgets tabnotebook (and scrollbar)

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

From: Karl M. [email protected]
Subject: Ruby/Tk/Iwidgets tabnotebook (and scrollbar)
Date: Tue, 29 Jan 2008 10:55:26 +0900
Message-ID: [email protected]

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.

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
}

Probably the following is better. Sorry.

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)
}

Hidetoshi NAGAI wrote:

Probably the following is better. Sorry.

Yay! This worked very well. Thank you :slight_smile:

Cheers
Karl

From: Karl M. [email protected]
Subject: Re: Ruby/Tk/Iwidgets tabnotebook (and scrollbar)
Date: Wed, 6 Feb 2008 07:53:53 +0900
Message-ID: [email protected]

Probably the following is better. Sorry.

Yay! This worked very well. Thank you :slight_smile:

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)
}