[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Ruby/Tk/Iwidgets tabnotebook (and scrollbar

Karl Miller

1/29/2008 1:55:00 AM

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
--
Posted via http://www.ruby-....

4 Answers

Hidetoshi NAGAI

2/5/2008 9:13:00 AM

0

From: Karl Miller <karl.miller.km@gmail.com>
Subject: Ruby/Tk/Iwidgets tabnotebook (and scrollbar)
Date: Tue, 29 Jan 2008 10:55:26 +0900
Message-ID: <870d25c134b16e7234d146b03c43a1df@ruby-forum.com>
> 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
}

> # 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

--
Hidetoshi NAGAI (nagai@ai.kyutech.ac.jp)

Hidetoshi NAGAI

2/5/2008 6:55:00 PM

0

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

> 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

--
Hidetoshi NAGAI (nagai@ai.kyutech.ac.jp)

Karl Miller

2/5/2008 10:54:00 PM

0

Hidetoshi NAGAI wrote:
> Probably the following is better. Sorry.
>
Yay! This worked very well. Thank you :)

Cheers
Karl
--
Posted via http://www.ruby-....

Hidetoshi NAGAI

2/7/2008 9:28:00 PM

0

From: Karl Miller <karl.miller.km@gmail.com>
Subject: Re: Ruby/Tk/Iwidgets tabnotebook (and scrollbar)
Date: Wed, 6 Feb 2008 07:53:53 +0900
Message-ID: <7dbb75f30cad0a50c37526bc2bbdf7ea@ruby-forum.com>
> > Probably the following is better. Sorry.
> >
> 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)
}
==================================================================
--
Hidetoshi NAGAI (nagai@ai.kyutech.ac.jp)