
proc workspace_add {win {title ""} {geo ""}} {
   global toplevel tdv_workspace

   if ![info exist toplevel(root)] {workspace_init $tdv_workspace}

   if {[info exist toplevel(use_sub)] && $toplevel(use_sub)} {
      if {[info exist toplevel($win,mapped)] && \
             !$toplevel($win,mapped)} { top_deiconify $win }
      workspace_framing $win $title 1 1 $geo
      top_raise $win
   } else {
      if [info exist toplevel($win,mapped)] {
         set toplevel($win,mapped) 1
      }
      workspace_maxify $win $title $geo
   }
}

proc workspace_menuselect {win} {
   global toplevel tdv_workspace

   workspace_add $win 
}

proc workspace_set_windows_menu {m} {
   global toplevel

   if ![info exist toplevel(windows_menu)] {
      set toplevel(windows_menu) ""
   }
   if {[winfo exist $m] && ([winfo class $m] == "Menu") &&
		[ info exists toplevel(windows_menu) ] } {
      set m1 [lindex $toplevel(windows_menu) 0]
      if {$m1 != ""} {copy_menu_entries ar $m1 $m}
      lappend toplevel(windows_menu) $m
   }
}

proc workspace_init {root {topbar ""}} {
   global toplevel

   if ![winfo exist $root] { return "" }
   $root configure  -bd 0 -bg #105060
 #                ^^^^^ this is important for correct positioning

   set toplevel(root) $root

   if {($topbar != "") || ![info exist toplevel(topbar)]} {
      set toplevel(topbar) $topbar
   }

 #  set toplevel(windows_menu) ""
   if ![info exist toplevel(windows_menu)] {
      set toplevel(windows_menu) ""
   }
   set tl $root._windowmanager
   set menu $tl.m
   menubutton $tl -text Windows -borderwidth 2 -relief raised -menu $menu
   
   set dy [winfo height $tl]
   if {$dy < 20} {set dy 20}
   place $tl -in $root -x 5 -rely 1.0 -y [expr -5 - $dy]
   lower $tl

   lappend toplevel(windows_menu) [menu $menu -tearoff 0]

   set toplevel(menuselect_cmd) workspace_menuselect

   if ![info exist toplevel(framew)] {set toplevel(framew) 2}

   return $root
}

proc workspace_framing {win {title ""} {resize 1} {usehead 1} {geo ""}} {
   global toplevel boldfont guibase_dir

   if [info exist toplevel($win,frame)] {return ""}

   set frame ${win}__frame
   catch {destroy $frame}
   frame $frame -borderwidth 2 -relief raised

   set toplevel($win,framew) 2
   set toplevel($win,frame) $frame
   set toplevel($win,framepos) ""

   if $resize {
      bind $frame <Motion> "top_resizecur $frame %W %x %y"
      bind $frame <1> "top_resizestart $win %W %X %Y"
      bind $frame <Button1-Motion> "top_resize $win %X %Y"
      bind $frame <ButtonRelease-1> "top_resizeend $win %X %Y"
   } else {
      bind $frame <1> "top_movestart $win %X %Y"
      bind $frame <Button1-Motion> "top_move $win %X %Y"
      bind $frame <ButtonRelease-1> "top_moveend $win %X %Y"
   }
   bind $frame <2> "top_movestart $win %X %Y"
   bind $frame <Button2-Motion> "top_move $win %X %Y"
   bind $frame <ButtonRelease-2> "top_moveend $win %X %Y"

   set toplevel($win,pos) "0 0"
   set toplevel($win,mapped) 1
   set toplevel($win,single) 1

 # place $win -in $toplevel(root) -x 20 -y 30

   if {$title == ""} {
      if ![info exist toplevel($win,title)] { set toplevel($win,title) $win }
   } else {
      set toplevel($win,title) $title
   }

   set toplevel($win,delete_proc) ""
   set toplevel($win,del_verify) "workspace_close_comfirm $win"
   set toplevel($win,raise_proc) "workspace_raise $win"

   set toplevel($win,movestart_proc)   "workspace_movestart $win"
   set toplevel($win,moveend_proc)     "workspace_moveend $win"
   set toplevel($win,resizestart_proc) "workspace_resizestart $win"
   set toplevel($win,resizeend_proc)   "workspace_resizeend $win"

   if $usehead {
      set head ${win}__head
      catch {destroy $head}
      frame $head -borderwidth 0 -relief raised -bg blue
      label $head.l -text $toplevel($win,title) \
                    -font $boldfont -bg blue -anchor w -fg white

      button $head.i -borderwidth 1 -relief raised -command \
            "workspace_iconify $win" -bitmap @$guibase_dir/window_iconify.xbm
      button $head.m -borderwidth 1 -relief raised -command \
            "workspace_maxify $win" -bitmap @$guibase_dir/window_maxify.xbm
      button $head.x -borderwidth 1 -relief raised -command \
            "workspace_close $win" -bitmap @$guibase_dir/window_close.xbm
      pack $head.x $head.m $head.i -side right -padx 1 -pady 1

      pack $head.l -side left -fill both -expand 1 -padx 6
      set toplevel($win,head) $head
      set toplevel($win,headpos) ""

      bind $head.l <Double-1> "top_raise $win"
      bind $head.l <1> "top_movestart $win %X %Y"
      bind $head.l <Button1-Motion> "top_move $win %X %Y"
      bind $head.l <ButtonRelease-1> "top_moveend $win %X %Y"

      bind $head <Double-1> "top_raise $win"
      bind $head <1> "top_movestart $win %X %Y"
      bind $head <Button1-Motion> "top_move $win %X %Y"
      bind $head <ButtonRelease-1> "top_moveend $win %X %Y"
   }

   top_add_window_menu $win $toplevel($win,title)

   set nn [scan $geo "%dx%d+%d+%d" w h x y]
   if {$nn == 4} {
      set rr [list -x $x -y $y -width $w -height $h]
   } elseif {$nn >= 2} {
      set rr [list -x 20 -y 30 -width $w -height $h]
   } else {
      set rr [list -x 20 -y 30]
   }

   eval place $win -in $toplevel(root) $rr

   bind $win <Configure> "top_fix_subparts $win"
   update idletasks
}


proc workspace_raise {win} {
   global toplevel tdv_current_viewer

   if {[info exist tdv_current_viewer] && \
       [winfo exist $tdv_current_viewer] && \
       ($win != $tdv_current_viewer)} {
      set w $tdv_current_viewer
      if {[info exist toplevel($w,head)] && \
          [winfo exist $toplevel($w,head)]} {
         $toplevel($w,head) configure -bg #999999
         $toplevel($w,head).l configure -bg #999999
      }
   }

   set tdv_current_viewer $win
   set w1 $win
   foreach {n w} [vframe_cmd $win vframes] {
      raise $w $w1; set w1 $w 
   }

   if {[info exist toplevel($win,head)] && \
       [winfo exist $toplevel($win,head)]} {
      $toplevel($win,head) configure -bg blue
      $toplevel($win,head).l configure -bg blue
   }
   update idletasks 
}

proc workspace_movestart {win} {
   if {[winfo class $win] == "Tdv_Viewer"} {
      $win configure -in_dynamics 2
   }
   foreach {n w} [vframe_cmd $win vframes] { vframe_going $w begin }
}

proc workspace_moveend {win} {
   if {[winfo class $win] == "Tdv_Viewer"} {
      $win configure -in_dynamics 0

      foreach {n w} [vframe_cmd $win vframes] { vframe_going $w end }

      $win redraw
   } else {
      foreach {n w} [vframe_cmd $win vframes] { vframe_going $w end }
   }
}

proc workspace_resizestart {win} {
   if {[winfo class $win] == "Tdv_Viewer"} {
      $win configure -in_dynamics 2
   }
   foreach {n w} [vframe_cmd $win vframes] { vframe_going $w begin }
}

proc workspace_resizeend {win} {
   if {[winfo class $win] == "Tdv_Viewer"} {
      $win configure -in_dynamics 0
      foreach {n w} [vframe_cmd $win vframes] { vframe_going $w end }
      $win redraw
   } else {
      foreach {n w} [vframe_cmd $win vframes] { vframe_going $w end }
   }
}

proc workspace_iconify {win} {
   global toplevel

   top_iconify $win
   if [info exist toplevel($win,iconify_cmd)] {
      eval $toplevel($win,iconify_cmd) $win
   }
}

proc workspace_maxify {win {title ""} {geo ""}} {
   global toplevel guibase_dir

   top_raise $win

   if {![info exist toplevel(use_sub)] || $toplevel(use_sub)} {
      set toplevel(use_sub) 0
      if {$toplevel(topbar) != ""} {
         button $toplevel(topbar).minmax -borderwidth 1 \
              -relief raised  -command "workspace_minmax" \
              -bitmap @$guibase_dir/window_minmax.xbm
         pack $toplevel(topbar).minmax  -side right -padx 1 -pady 1
      } 
   }
   set toplevel(maxify) $win   
   if ![info exist toplevel($win,frame)] {
      if {$title == ""} {
         if [info exist toplevel($win,title)] {
            set title $toplevel($win,title)
         } else {
            set title $win
         }
      } else {
         set toplevel($win,title) $title
      }
   } else {
      set geo [winfo geometry $win]
      if {$title == ""} {set title $toplevel($win,title)}
   }

   top_add_window_menu $win $title

   if {![info exist toplevel(maxify_list)] || \
             ([lsearch $toplevel(maxify_list) $win] < 0)} {
      lappend toplevel(maxify_list) $win $geo
   }
   if ![info exist toplevel(maxify_title)] {
      set toplevel(maxify_title) [wm title .]
   }

   foreach w [pack slaves $toplevel(root)] { pack forget $w }

   pack $win -in $toplevel(root) -anchor center -expand 1 -fill both \
                -ipadx 0 -ipady 0 -padx 0 -pady 0 -side top

   wm title . "$toplevel(maxify_title) $title"
}

proc workspace_minmax {} {
   global toplevel

   set toplevel(use_sub) 1
   if ![info exist toplevel(maxify)] {return ""}

   wm title . $toplevel(maxify_title)
   if {($toplevel(topbar) != "") && \
            [winfo exist $toplevel(topbar).minmax]} {
      destroy $toplevel(topbar).minmax 
   }

   if {[lsearch $toplevel(maxify_list) $toplevel(maxify)] < 0} {
      lappend toplevel(maxify_list) $toplevel(maxify) ""
   }

   foreach {win geo} $toplevel(maxify_list) {
      if {![winfo exist $win]} { continue }
      set nn [scan $geo "%dx%d+%d+%d" w h x y]
      if {$nn == 4} {
         set rr [list -x $x -y $y -width $w -height $h]
      } elseif {$nn >= 2} {
         set rr [list -x 20 -y 30 -width $w -height $h]
      } else {
         set rr [list -x 20 -y 30]
      }
      catch {pack forget $win}
      workspace_framing $win

      eval place $win -in $toplevel(root) $rr
      top_fix_subparts $win
      update idletasks
   }
   if {$win != $toplevel(maxify)} {
      set win $toplevel(maxify) 
      if [winfo exist $win] {top_raise $win}
   }

   set toplevel(maxify_list) ""
   unset toplevel(maxify)
}


proc workspace_close {win} {
   global toplevel

   top_destroy $win 
}

proc workspace_disable_close {win {m ""}} {
   global toplevel
   set toplevel($win,disable_close) $m
}

proc workspace_close_comfirm {win {msg ""}} {
   global toplevel

   if [info exist toplevel($win,disable_close)] {
      if {$toplevel($win,disable_close) != ""} {
         tk_dialog .cfm_fg "Warning Message" \
               $toplevel($win,disable_close) "" 0 Dismiss
      }
      return 0
   }

   set title $win
   if [info exist toplevel($win,title)] {set title $toplevel($win,title)}
   if {$msg == ""} {set msg "Are You Sure You Want to Close $title"}
 
   return ![tk_dialog .cfm_fg "Confirm to close $title" $msg "" \
              0 Confirm Cancel]
}

proc workspace_progress_init {w title num} {
   global toplevel

   frame $w -bg #119999
   set toplevel(progress,win) $w

   set lab2  [label $w.lab2 -textvariable toplevel(progress,text) \
                     -background gray -font {times 16} -width 50]
   set prg   [ProgressBar $w.prg -width 200 -height 20 \
                     -background white -variable toplevel(progress,index) \
                     -maximum $num]

   pack $lab2 $prg -side top  -expand 1
}

proc workspace_progress_start {w text num} {
   global toplevel

   set toplevel(progress,text) $text
   set toplevel(progress,index) 0

   if {[info exist toplevel(progress,win)] && \
            [winfo exist $toplevel(progress,win)]} {
      set fm  $progress_data(win)
      $fm.prg configure -maximum $num

      # title set as $text
   } else {
      frame $w -bg #119999
      set toplevel(progress,win) $w

      set lab1  [label $w.lab1 -text "   " -background #119999 -font {times 16}]
      set lab2  [label $w.lab2 -textvariable toplevel(progress,text) \
                     -background #119999 -font {times 16} -width 50]
      set prg   [ProgressBar $w.prg -width 500 -height 28 \
                     -background white -variable toplevel(progress,index) \
                     -maximum $num]

      pack $lab1 $prg $lab2 -side top  -expand 0

     # busy 1
   }     

   workspace_form_raise $w
}

proc workspace_progress_incr {text} {
   global toplevel

   if {[info exist toplevel(progress,win)] && \
            [winfo exist $toplevel(progress,win)]} {
      incr  toplevel(progress,index)
      set toplevel(progress,text) $text
      update idletasks
   } else {
      workspace_progress_start .progress $text 10
   }
}

proc workspace_progress_end {} {
   global toplevel

   #busy 0
   if ![info exist toplevel(progress,win)] {return ""}

   if [winfo exist $toplevel(progress,win)] {
      pack forget $toplevel(progress,win)
      destroy $toplevel(progress,win)
   }
   unset toplevel(progress,win)
}

proc workspace_form_clean {} {
   global toplevel

   if {[info exist toplevel(form)] && \
             [winfo exist $toplevel(form)]} {
      workspace_form_done $toplevel(form)
      destroy $toplevel(form) 
      unset toplevel(form)
   }
}

proc workspace_form_init {w buttons} {
   global toplevel

   workspace_form_clean
   set toplevel(form) $w

   frame $w -bg #119999

   form_init_sub [frame $w.main]

   if {$buttons == ""} {
       set buttons "accept cancel"
   }

   set button_specs [form_init_buttons $w $buttons]

   form_init_bottom $w $w.bottom $button_specs

   $w.bottom  configure -background #119999

   pack $w.main $w.bottom -pady 2 -side top -expand 1

   return $w.main
}

proc workspace_form_raise {w} {
   global toplevel

   if {![info exist toplevel(use_sub)] || !$toplevel(use_sub)} {
      workspace_minmax
   }

   foreach s [place slaves $toplevel(root)] {
      if [info exist toplevel($s,head)] { workspace_iconify $s } 
   }

   foreach s [pack slaves $toplevel(root)] { pack forget $s }

   pack $w -in $toplevel(root) -anchor center -expand 1 -fill both \
                -ipadx 0 -ipady 0 -padx 0 -pady 0 -side top
}

proc workspace_form_done {w {flag 1}} {
   global form_done_flag

   pack forget $w
   set form_done_flag($w) $flag

   update idle
}
