Posted to tcl by egavilan at Tue Feb 19 14:53:50 GMT 2008view pretty

#######################################################
# we get three tabs, two created at start and the third a second later. Note the diferences
# of reqheight of each one

package require Tk
ttk::setTheme clam

proc instance.add { {title ""} } {
   global state

   set tabid $state(NB).[format tab%04d $state(ID)]
   set pw [ttk::panedwindow $tabid -orient vertical]

   # add the upper text
   set fup [ttk::frame $pw.up -padding 2]
   set txt [text.add  $fup]
   bind $fup <Configure> "puts \"$fup: \[winfo reqheight $fup\]\""

   # the lower text
   set fdown [ttk::frame $pw.down -padding 2]
   #set peer  [text.add $fdown $txt]
   # we want the lower pane to be hidden
   #grid propagate $fdown 0
   bind $fdown <Configure> "puts \"$fdown: \[winfo reqheight $fdown\]\""

   # in the case of resize, the upper pane should take all the space
   puts "adding $tabid"
   $pw add $fup -weight 1
   $pw add $fdown -weight 0

   # set the title
   if {$title eq ""} {set title "Untitled-$state(ID)"}

   # finally, add the tab to the notebook
   $state(NB) add $tabid -text $title

   bind $tabid <Configure> "puts \"$tabid: \[winfo reqheight $tabid\]\""

   # init the keys in the array which gather all the data
   # in this tab
   set state($tabid.txt)      $txt
   set state($tabid.filename) {}

   incr state(ID)

   #######################################################
   # if we force the new tab to show, it exposes a {bug|race condition}?
   #######################################################
   $state(NB) select $tabid
   #######################################################


   # bug workaround
   event generate $state(NB) <Expose>
}

proc text.add { parent {peerof {}} } {
   set vs [ttk::scrollbar $parent.vs -orient vertical]
   set hs [ttk::scrollbar $parent.hs -orient horizontal]

   if {$peerof eq ""} {
      set t [text $parent.txt \
         -yscrollcommand [list $vs set] \
         -xscrollcommand [list $hs set]]
   } else {
      set t [$peerof peer create $parent.txt \
         -yscrollcommand [list $vs set] \
         -xscrollcommand [list $hs set]]
   }

   $vs configure -command [list $t yview]
   $hs configure -command [list $t xview]

   grid $t $vs -sticky news
   grid $hs -sticky news
   grid columnconfigure $parent 0 -weight 1
   grid rowconfigure $parent 0 -weight 1

   bind $t <Configure> "puts \"$t: \[winfo reqheight $t\]\""

   return $t
}

proc instance.delete {} {
   global state
   set tabid [$state(NB) select]

   # release the data associated with this tab
   array unset state $tabid.*

   # finally, destroy the tab
   destroy $tabid

}

proc build.gui {} {
   global state

   set state(NB)  [ttk::notebook .nb]

   grid $state(NB)  -sticky news
   
   grid rowconfigure . 0 -weight 1
   grid columnconfigure . 0 -weight 1

   option add *Text.background white
   option add *Text.highlightThickness 0
   option add *Text.borderWidth 1
   option add *Text.selectBorderWidth 0
}

proc init {} {
   global state
   set state(ID) 0
}

############################################################
# App initialization
init
build.gui

after 1000 instance.add
instance.add
instance.add


###########################################################
# Some general bindings
event add <<New>>   <Control-N> <Control-n>
event add <<Close>> <Control-W> <Control-w>

bind . <<New>>   {instance.add}
bind . <<Close>> {instance.delete}

Comments

Posted by jenglish at Tue Feb 19 15:59:45 GMT 2008 [text] [code]

Cause of the problem: (a) the ttk::panedwindow ignores geometry requests from mapped children; (b) geometry propagation happens at idle-time; but (c) Tk_MapWindow() works immediately. Solution: add [update idletasks] before [$nb select ...] on the newly-created tab. This will allow geometry propagation to finish before mapping the tab.