gui/implementation/Notebook.tcl


@implementation Notebook {
  - init {} {
      $super init
      $path configure -width 100 -height 100 -bd 2
      set counter 1
      label $path.test -text foo -bd 1 -relief raised -padx 3m -pady 1m
      set height [expr [winfo reqheight $path.test] -1]
      destroy $path.test

      set main [frame $path.main -height [expr $height +10] \
        -width 10 -relief raised -bd 1]
      set bar [frame $path.bar -height [expr $height + 4] -bd 0]
      set shadow [frame $path.shadow -height 2 -bd 0]

      place $bar -relx 0.0 -rely 0.0 -relwidth 1 -anchor nw
      pack propagate $bar 0
      place $main -x 0 -relwidth 1 -y $height \
        -relheight 1 -height -$height -anchor nw
      raise $main
    }

  - views: aList {
      foreach view $aList { $self add: $view }
      $self selection: [lindex $aList 0]
    }

  - views {} {
      return $views
    }

  - add: aView {
      if [$self contains: $aView] { return }
      pack [label $bar.$counter -text $aView -highlightthickness 0 \
        -relief raised -bd 1 -padx 3m -pady 1m] \
        -anchor s -side left -padx 1
      bind $bar.$counter <ButtonPress-1> "$self selection: \"$aView\"" 
      set f [frame $main.$counter -bd 10]
      incr counter
      lappend views $aView
      $self selection: $aView
      return $f
    }

  - remove: aView {
      if {![$self contains: $aView]} { return }
      views remove: $aView
      set index [$self indexOf: $aView]
      catch {destroy $bar.$index $main.$index}
      if {"$selection" == "$aView"} {
        $self selection: [lindex $views end] 
      }
    }

  - selection: aView {
      if {![$self contains: $aView]} { return }
      set index [$self indexOf: $aView]
      if [string length [set old [pack slaves $main]]] {
        set i [lindex [split $old .] end]
        $bar.$i configure -padx 3m
        pack configure $bar.$i -anchor s -padx 1
        pack forget $old
      }
      $bar.$index configure -padx 4m
      pack configure $bar.$index -anchor n -padx 0
      pack $main.$index -padx 1 -pady 1 -fill both -expand 1
      place $shadow -in $bar.$index -relwidth 1 \
        -relx 0.0 -rely 1.0 -bordermode inside
      place $shadow -y 0
      raise $main
      raise $main.$index
      update ; raise $shadow
      set selection $aView
    }

  - selection {} {
      return $selection
    }

  - contains: aView {
      if [views contains: $aView] {
        return 1
      } else {
        return 0
      }
    }

  - frameOf: aView {
      if [$self contains: $aView] {
        return $main.[$self indexOf: $aView]
      } else {
        return [error "$self doesn't contain \"$aView\""]
      }
    }
  - indexOf: aView {
      foreach view [pack slaves $bar] {
        if {"[$view cget -text]" == "$aView"} {
          return [lindex [split $view .] end]
        }
      }
      return 
    }

  - dealloc {} {
      catch {destroy $main $bar $shadow}
      return [$super dealloc]
    }
}