# Some Global Utility Procs
proc DummyProc {} {
}

proc shwd { theDEZ } {
  ::AIEnv::GUIManagement::DEZShow $theDEZ
}

proc msg { type theString } {
  ::AIEnv::GUIManagement::ShowMessage $type $theString 
} 

proc notDone { } {
  msg  ERROR "Capability not yet Implemented"
}

proc trySavingBatchMeshingData {} {
    # for batch surface mesher
    # (cf. in ~cvs/scrib/ med_menus/menus_SM.tcl & lib/output.tcl)
    # returns 1 if things were saved; 0 if not. --jka
    
    if [interp exists script_interp] {
	set cmd "scrib_lib_output_save_edited_mesh_nastran"
	if {$cmd == [script_interp eval info commands $cmd]} {
	    # best case scenario
	    set stat [script_interp eval $cmd]
	    return 1
	}
    }
    return 0
}

proc exitProgram {{from_exit 0}} {
    global aie_guiOpt socket_done file_name env

    # terminate any batch application
    if [aie_check_application_running] return
    if [winfo exists .replay_ops] replay_done
    
    if ![trySavingBatchMeshingData] {
            if {0 == [aie_closeProject 0 1]} return
    }
    if [catch {
	        if $socket_done {
	            if {[info command get_socket_done_file_name] != ""} {
		            set file [get_socket_done_file_name]
	            } elseif [info exists file_name(socket_done_file_name)] {
		            set file $file_name(socket_done_file_name)
	            } elseif [info exists file_name(domain_saved)] {
		            set file $file_name(domain_saved)
	            } else {
		            set file ""
	            }
	            set ss [socket localhost $socket_done]
	            if {$file != ""} {
		            mess "OK $file\n"
	            } else {
		            puts $ss "CANCEL"
	            }
	            close $ss
	        }
    } res] { puts "ERROR $res" }
    
    hlp_sendcommand QUIT
    if {[info command [lindex $aie_guiOpt(v3ExitCmd) 0]] != ""} {
       if {[eval $aie_guiOpt(v3ExitCmd)] == 0} { return }
    }
    ic_undo_stop
    ic_aienv_license_exit
    exit
}

proc setGlobalVals {} {
  global currentDOFs
  set currentDOFs {UX UY UZ}
}

proc setGreyDEZs {} {
   
  ::AIEnv::GUIManagement::ToggleGrey createPointsXYZ 1
  ::AIEnv::GUIManagement::ToggleGrey createPointsScreen 1
  ::AIEnv::GUIManagement::ToggleGrey createPointsEndPoint 1

  ::AIEnv::GUIManagement::ToggleGrey movePointsOffset 1
  ::AIEnv::GUIManagement::ToggleGrey movePoints2P 1

  ::AIEnv::GUIManagement::ToggleGrey moveCurveOffset 1
  ::AIEnv::GUIManagement::ToggleGrey moveCurve2P 1

  ::AIEnv::GUIManagement::ToggleGrey moveSurfOffset 1
  ::AIEnv::GUIManagement::ToggleGrey moveSurf2P 1

  ::AIEnv::GUIManagement::ToggleGrey moveBodyOffset 1
  ::AIEnv::GUIManagement::ToggleGrey moveBody2P 1

  ::AIEnv::GUIManagement::ToggleGrey moveNodesOffset 1
  ::AIEnv::GUIManagement::ToggleGrey moveNodes2P 1

  ::AIEnv::GUIManagement::ToggleGrey moveElemsOffset 1
  ::AIEnv::GUIManagement::ToggleGrey moveElems2P 1

  ::AIEnv::GUIManagement::ToggleGrey mszCurveSz 1
  ::AIEnv::GUIManagement::ToggleGrey mszCurveNum 1

  ::AIEnv::GUIManagement::ToggleGrey createNodesXYZ 1
  ::AIEnv::GUIManagement::ToggleGrey createNodesScreen 1
  ::AIEnv::GUIManagement::ToggleGrey createNodesEndPoint 1
}
#--------------------------------------------------------------------------
# Geometry Procs

proc createPointsRGProc {  } {
#   global createPointsMethod

   if {$createPointsMethod == "xyz" } {
       ::AIEnv::GUIManagement::ToggleGrey createPointsXYZ 0
       ::AIEnv::GUIManagement::ToggleGrey createPointsScreen 1
       ::AIEnv::GUIManagement::ToggleGrey createPointsEndPoint 1
   }
   if {$createPointsMethod == "screen" } {
       ::AIEnv::GUIManagement::ToggleGrey createPointsXYZ 1
       ::AIEnv::GUIManagement::ToggleGrey createPointsScreen 0
       ::AIEnv::GUIManagement::ToggleGrey createPointsEndPoint 1
   }
   if {$createPointsMethod == "end" } {
       ::AIEnv::GUIManagement::ToggleGrey createPointsXYZ 1
       ::AIEnv::GUIManagement::ToggleGrey createPointsScreen 1
       ::AIEnv::GUIManagement::ToggleGrey createPointsEndPoint 0
   }
}

proc movePointsRGProc {  } {
   global movePointsMethod

   if {$movePointsMethod == "offset" } {
     ::AIEnv::GUIManagement::ToggleGrey movePointsOffset 0
     ::AIEnv::GUIManagement::ToggleGrey movePoints2P 1
   }
   if {$movePointsMethod == "2p" } {
     ::AIEnv::GUIManagement::ToggleGrey movePointsOffset 1
     ::AIEnv::GUIManagement::ToggleGrey movePoints2P 0
   }
}
proc moveCurveRGProc {  } {
   global moveCurveMethod

   if {$moveCurveMethod == "offset" } {
     ::AIEnv::GUIManagement::ToggleGrey moveCurveOffset 0
     ::AIEnv::GUIManagement::ToggleGrey moveCurve2P 1
   }
   if {$moveCurveMethod == "2p" } {
     ::AIEnv::GUIManagement::ToggleGrey moveCurveOffset 1
     ::AIEnv::GUIManagement::ToggleGrey moveCurve2P 0
   }
}

proc moveSurfRGProc {  } {
   global moveSurfMethod

   if {$moveSurfMethod == "offset" } {
     ::AIEnv::GUIManagement::ToggleGrey moveSurfOffset 0
     ::AIEnv::GUIManagement::ToggleGrey moveSurf2P 1
   }
   if {$moveSurfMethod == "2p" } {
     ::AIEnv::GUIManagement::ToggleGrey moveSurfOffset 1
     ::AIEnv::GUIManagement::ToggleGrey moveSurf2P 0
   }
}

proc moveBodyRGProc {  } {
   global moveBodyMethod

   if {$moveBodyMethod == "offset" } {
     ::AIEnv::GUIManagement::ToggleGrey moveBodyOffset 0
     ::AIEnv::GUIManagement::ToggleGrey moveBody2P 1
   }
   if {$moveBodyMethod == "2p" } {
     ::AIEnv::GUIManagement::ToggleGrey moveBodyOffset 1
     ::AIEnv::GUIManagement::ToggleGrey moveBody2P 0
   }
}
proc moveNodesRGProc {  } {
   global moveNodesMethod

   if {$moveNodesMethod == "offset" } {
     ::AIEnv::GUIManagement::ToggleGrey moveNodesOffset 0
     ::AIEnv::GUIManagement::ToggleGrey moveNodes2P 1
   }
   if {$moveNodesMethod == "2p" } {
     ::AIEnv::GUIManagement::ToggleGrey moveNodesOffset 1
     ::AIEnv::GUIManagement::ToggleGrey moveNodes2P 0
   }
}
proc moveElemsRGProc {  } {
   global moveElemsMethod

   if {$moveElemsMethod == "offset" } {
     ::AIEnv::GUIManagement::ToggleGrey moveElemsOffset 0
     ::AIEnv::GUIManagement::ToggleGrey moveElems2P 1
   }
   if {$moveElemsMethod == "2p" } {
     ::AIEnv::GUIManagement::ToggleGrey moveElemsOffset 1
     ::AIEnv::GUIManagement::ToggleGrey moveElems2P 0
   }
}
proc mszCurveRGProc {  } {
   global mszCurveMethod

   if {$mszCurveMethod == "size" } {
     ::AIEnv::GUIManagement::ToggleGrey mszCurveSz 0
     ::AIEnv::GUIManagement::ToggleGrey mszCurveNum 1
   }
   if {$mszCurveMethod == "num" } {
     ::AIEnv::GUIManagement::ToggleGrey mszCurveSz 1
     ::AIEnv::GUIManagement::ToggleGrey mszCurveNum 0
   }
}
proc createNodesRGProc {  } {
   global createNodesMethod

   if {$createNodesMethod == "xyz" } {
       ::AIEnv::GUIManagement::ToggleGrey createNodesXYZ 0
       ::AIEnv::GUIManagement::ToggleGrey createNodesScreen 1
       ::AIEnv::GUIManagement::ToggleGrey createNodesEndPoint 1
   }
   if {$createNodesMethod == "screen" } {
       ::AIEnv::GUIManagement::ToggleGrey createNodesXYZ 1
       ::AIEnv::GUIManagement::ToggleGrey createNodesScreen 0
       ::AIEnv::GUIManagement::ToggleGrey createNodesEndPoint 1
   }
   if {$createNodesMethod == "end" } {
       ::AIEnv::GUIManagement::ToggleGrey createNodesXYZ 1
       ::AIEnv::GUIManagement::ToggleGrey createNodesScreen 1
       ::AIEnv::GUIManagement::ToggleGrey createNodesEndPoint 0
   }
}

proc doMszCurve {} {
	global mszCurveList
	global mszCurveMethod
	global mszVal
	global mszNum
	global mszRatio
	global str_option

	if { $mszCurveMethod == "size" } {
		foreach Item $mszCurveList {
			mess "Item: $Item\t"
			mess [ic_set_meshing_params curve $Item emax $mszVal]
			mess "\n"
			#mess [ic_set_meshing_params curve $Item erat $mszRatio]
		}
	} else {
		# We are using number of divisions
		foreach Item $mszCurveList {
			# Set the meshing parameters
			mess [geo_params_number_on_curve $mszNum $mszCurveList]
		}
	}

	set str_option(curve,sizes) tetra

	update_geom_visible curve p
}


proc TestLoadTetinFile {file} {
	global data_available
	global load_tetin_triangle_tolerance fam_visible 
    global tetin_keep_model_params
    global tdv_default
    global str_option
    global TetinFileName
    
    # Unload any previous mesh/geometry
	TestUnloadMesh
	TestUnloadGeo
	
    set TetinFileName $file
    
    foreach type {surface curve point material density loop} {
		set str_option($type,names) 0
		set str_option($type,wide) 0
		set str_option($type,dtype) detailed
		set str_option($type,stype) solid
		set str_option($type,sizes) none
	}

    mess "Loading geometry file $file ...\n"
	ic_load_tetin [list $file] 0 0
	mess "Done loading geometry file.\n"
	ic_geo_summary

	set fams [ic_geo_list_families]
	foreach type {surface curve point material density} {
		ic_geo_update_visibility $type $fams 1

	}
	ic_geo_configure_objects surface 3 solid 0 0 0 0 0 0 0 0 0 0


	tdv_home $tdv_default

}

proc LoadTetinFile {} {

    set theFile [tk_getOpenFile -filetypes {{{Tetin Files} {.tin}}} \
		     -title "Open Geometry" -parent .]
    if { $theFile != "" } {
	TestLoadTetinFile $theFile
    }
}

proc LoadDomainFile {} {
    set theFile [tk_getOpenFile -filetypes {{{Domain Files} {.unstruct}}} \
		     -title "Open Geometry" -parent .]
    if { $theFile != "" } {
	TestLoadDomainFile $theFile
    }
}

proc TestLoadDomainFile {DomainFile} {

	# Unload any previous mesh/geometry
	TestUnloadMesh
	#TestUnloadGeo

	global tdv_default

	ic_load_unstruct $DomainFile

	set Families [ic_uns_list_families]
	set Types [ic_uns_list_types]

	ic_uns_subset_configure {} -shade flat_wire
	ic_uns_update_family_type visible $Families $Types update 1

	ic_uns_set_modified 0

	tdv_home $tdv_default

}

proc TestUnloadGeo {} {
	if { [ic_geo_is_loaded] } {
		ic_unload_tetin
	}
}
proc TestUnloadMesh {} {
	if { [ic_uns_is_loaded] } {
		ic_unload_mesh
	}
}


proc TetMesh {} {
	global TetinFileName
	# Just create a dummy tetra process file

  ic_save_tetin "meshtemp.tin" 0

	set TetinFileName "meshtemp.tin"
	set Path [file dirname $TetinFileName]
	set DomainFileName [file rootname $TetinFileName]
	set DomainFileName [file join $Path $DomainFileName.unstruct]

	ic_run_tetra c:/sandia/meshtemp.tin c:/sandia/foo.uns run_cutter 1 run_smoother 1

		TestLoadDomainFile c:/sandia/foo.uns
}


proc get_fam_visible {fam} {
    global fam_visible
    if [info exists fam_visible($fam)] {
	return $fam_visible($fam)
    } else {
	return 0
    }
}

proc get_uns_type_visible {type} {
    return 1
}

#proc get_geom_visible {type} {
#    return 1
#}

##########################################################################################
# Dummy procs
#

#proc busy {args} {
#
#}

proc update_fam_window {args} {
    global fam_visible fam_visible_prev

    foreach f [ic_geo_list_families] {
	if ![info exists fam_visible($f)] {
	    set fam_visible($f) 1
	}
    }

    if [info exists fam_visible_prev] {
	unset fam_visible_prev
    }
}


proc update_family_type {map fvar tvar what force} {
}

proc update_geom_window {args} {
}

proc update_uns_visible {} {
    aie_updateMeshVis
}

proc update_uns_available {args} {
}

proc update_uns_visible_subset {args} {
}

#proc update_uns_disp {args} {
#}

proc initialize_unstruct_visibility {args} {
}

proc uninitialize_unstruct_visibility {args} {
}

proc initialize_struct_visibility {args} {
}

proc uninitialize_struct_visibility {args} {
}

proc disp_pack_list {args} {
}

proc gui_change_visibility {type what on} {
    global geom_visible uns_type_visible fam_visible

    switch $type {
	geometry {
	    set geom_visible($what) $on
	}
	unstruct {
	    set uns_type_visible($what) $on
	}
	family {
	    set fam_visible($what) $on
	}
	default {
	    error "Unknown type value"
	}
    }
}

proc add_new_family {f v} {
    global fam_visible
    set fam_visible($f) $v
}

proc disp_new_stuff {args} {
}

proc display_update {args} {
    # this is BAD  aie_create_initial_assemblies
}

proc undo_redo_update {args} {
}

proc fam_delete_unused_on_unload {args} {
}

proc get_famnames {args} {
}

proc struct_comptopo_callback {args} {
}

proc disp_update_number_list {args} {
}

proc orient_command {args} {
}

proc finish_new_elems {} {
}

proc display_quad_results {} {
}

#proc set_title {args} {
#}

proc clear_fam_visible_prev {args} {
}

if {[info command wm] != ""} {
    proc run_application_before {} {
	wm iconify .
	update
	return [application_wait]
    }
    proc run_application_after {ww} {
	destroy $ww
	wm deiconify .
    }
}

if {[info proc aie_load_lcs] == ""} {
    proc aie_load_lcs {args} {
	# dummy
    }
}

if {[info proc aie_set_lcs_current] == ""} {
    proc aie_set_lcs_current {args} {
	# dummy
    }
}

# overwrite the builtin tk dialogs/file selection on window systems
# required in case of a second display

if {[info command winfo] != "" && [winfo screenwidth .] > 2048} {

global is_win

if {[info proc ::tk_messageBox] == "" && $is_win} {
proc ::tk_messageBox {args} {
	return [eval tk::MessageBox $args]
}
}

if {[info proc ::tk::MessageBox] == "" && $is_win} {

image create bitmap ::tk::dialog::b1 -foreground black \
-data "#define b1_width 32\n#define b1_height 32
static unsigned char q1_bits[] = {
   0x00, 0xf8, 0x1f, 0x00, 0x00, 0x07, 0xe0, 0x00, 0xc0, 0x00, 0x00, 0x03,
   0x20, 0x00, 0x00, 0x04, 0x10, 0x00, 0x00, 0x08, 0x08, 0x00, 0x00, 0x10,
   0x04, 0x00, 0x00, 0x20, 0x02, 0x00, 0x00, 0x40, 0x02, 0x00, 0x00, 0x40,
   0x01, 0x00, 0x00, 0x80, 0x01, 0x00, 0x00, 0x80, 0x01, 0x00, 0x00, 0x80,
   0x01, 0x00, 0x00, 0x80, 0x01, 0x00, 0x00, 0x80, 0x01, 0x00, 0x00, 0x80,
   0x01, 0x00, 0x00, 0x80, 0x02, 0x00, 0x00, 0x40, 0x02, 0x00, 0x00, 0x40,
   0x04, 0x00, 0x00, 0x20, 0x08, 0x00, 0x00, 0x10, 0x10, 0x00, 0x00, 0x08,
   0x60, 0x00, 0x00, 0x04, 0x80, 0x03, 0x80, 0x03, 0x00, 0x0c, 0x78, 0x00,
   0x00, 0x30, 0x04, 0x00, 0x00, 0x40, 0x04, 0x00, 0x00, 0x40, 0x04, 0x00,
   0x00, 0x80, 0x04, 0x00, 0x00, 0x00, 0x05, 0x00, 0x00, 0x00, 0x06, 0x00,
   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};"
image create bitmap ::tk::dialog::b2 -foreground white \
-data "#define b2_width 32\n#define b2_height 32
static unsigned char b2_bits[] = {
   0x00, 0x00, 0x00, 0x00, 0x00, 0xf8, 0x1f, 0x00, 0x00, 0xff, 0xff, 0x00,
   0xc0, 0xff, 0xff, 0x03, 0xe0, 0xff, 0xff, 0x07, 0xf0, 0xff, 0xff, 0x0f,
   0xf8, 0xff, 0xff, 0x1f, 0xfc, 0xff, 0xff, 0x3f, 0xfc, 0xff, 0xff, 0x3f,
   0xfe, 0xff, 0xff, 0x7f, 0xfe, 0xff, 0xff, 0x7f, 0xfe, 0xff, 0xff, 0x7f,
   0xfe, 0xff, 0xff, 0x7f, 0xfe, 0xff, 0xff, 0x7f, 0xfe, 0xff, 0xff, 0x7f,
   0xfe, 0xff, 0xff, 0x7f, 0xfc, 0xff, 0xff, 0x3f, 0xfc, 0xff, 0xff, 0x3f,
   0xf8, 0xff, 0xff, 0x1f, 0xf0, 0xff, 0xff, 0x0f, 0xe0, 0xff, 0xff, 0x07,
   0x80, 0xff, 0xff, 0x03, 0x00, 0xfc, 0x7f, 0x00, 0x00, 0xf0, 0x07, 0x00,
   0x00, 0xc0, 0x03, 0x00, 0x00, 0x80, 0x03, 0x00, 0x00, 0x80, 0x03, 0x00,
   0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00,
   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};"
image create bitmap ::tk::dialog::q -foreground blue \
-data "#define q_width 32\n#define q_height 32
static unsigned char q_bits[] = {
   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xe0, 0x07, 0x00,
   0x00, 0x10, 0x0f, 0x00, 0x00, 0x18, 0x1e, 0x00, 0x00, 0x38, 0x1e, 0x00,
   0x00, 0x38, 0x1e, 0x00, 0x00, 0x10, 0x0f, 0x00, 0x00, 0x80, 0x07, 0x00,
   0x00, 0xc0, 0x01, 0x00, 0x00, 0xc0, 0x00, 0x00, 0x00, 0xc0, 0x00, 0x00,
   0x00, 0x00, 0x00, 0x00, 0x00, 0xc0, 0x00, 0x00, 0x00, 0xe0, 0x01, 0x00,
   0x00, 0xe0, 0x01, 0x00, 0x00, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};"
image create bitmap ::tk::dialog::i -foreground blue \
-data "#define i_width 32\n#define i_height 32
static unsigned char i_bits[] = {
   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
   0x00, 0xe0, 0x01, 0x00, 0x00, 0xf0, 0x03, 0x00, 0x00, 0xf0, 0x03, 0x00,
   0x00, 0xe0, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
   0x00, 0xf8, 0x03, 0x00, 0x00, 0xf0, 0x03, 0x00, 0x00, 0xe0, 0x03, 0x00,
   0x00, 0xe0, 0x03, 0x00, 0x00, 0xe0, 0x03, 0x00, 0x00, 0xe0, 0x03, 0x00,
   0x00, 0xe0, 0x03, 0x00, 0x00, 0xe0, 0x03, 0x00, 0x00, 0xf0, 0x07, 0x00,
   0x00, 0xf8, 0x0f, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};"
image create bitmap ::tk::dialog::w1 -foreground black \
-data "#define w1_width 32\n#define w1_height 32
static unsigned char w1_bits[] = {
   0x00, 0x80, 0x01, 0x00, 0x00, 0x40, 0x02, 0x00, 0x00, 0x20, 0x04, 0x00,
   0x00, 0x10, 0x04, 0x00, 0x00, 0x10, 0x08, 0x00, 0x00, 0x08, 0x08, 0x00,
   0x00, 0x08, 0x10, 0x00, 0x00, 0x04, 0x10, 0x00, 0x00, 0x04, 0x20, 0x00,
   0x00, 0x02, 0x20, 0x00, 0x00, 0x02, 0x40, 0x00, 0x00, 0x01, 0x40, 0x00,
   0x00, 0x01, 0x80, 0x00, 0x80, 0x00, 0x80, 0x00, 0x80, 0x00, 0x00, 0x01,
   0x40, 0x00, 0x00, 0x01, 0x40, 0x00, 0x00, 0x02, 0x20, 0x00, 0x00, 0x02,
   0x20, 0x00, 0x00, 0x04, 0x10, 0x00, 0x00, 0x04, 0x10, 0x00, 0x00, 0x08,
   0x08, 0x00, 0x00, 0x08, 0x08, 0x00, 0x00, 0x10, 0x04, 0x00, 0x00, 0x10,
   0x04, 0x00, 0x00, 0x20, 0x02, 0x00, 0x00, 0x20, 0x01, 0x00, 0x00, 0x40,
   0x01, 0x00, 0x00, 0x40, 0x01, 0x00, 0x00, 0x40, 0x02, 0x00, 0x00, 0x20,
   0xfc, 0xff, 0xff, 0x1f, 0x00, 0x00, 0x00, 0x00};"
image create bitmap ::tk::dialog::w2 -foreground yellow \
-data "#define w2_width 32\n#define w2_height 32
static unsigned char w2_bits[] = {
   0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x01, 0x00, 0x00, 0xc0, 0x03, 0x00,
   0x00, 0xe0, 0x03, 0x00, 0x00, 0xe0, 0x07, 0x00, 0x00, 0xf0, 0x07, 0x00,
   0x00, 0xf0, 0x0f, 0x00, 0x00, 0xf8, 0x0f, 0x00, 0x00, 0xf8, 0x1f, 0x00,
   0x00, 0xfc, 0x1f, 0x00, 0x00, 0xfc, 0x3f, 0x00, 0x00, 0xfe, 0x3f, 0x00,
   0x00, 0xfe, 0x7f, 0x00, 0x00, 0xff, 0x7f, 0x00, 0x00, 0xff, 0xff, 0x00,
   0x80, 0xff, 0xff, 0x00, 0x80, 0xff, 0xff, 0x01, 0xc0, 0xff, 0xff, 0x01,
   0xc0, 0xff, 0xff, 0x03, 0xe0, 0xff, 0xff, 0x03, 0xe0, 0xff, 0xff, 0x07,
   0xf0, 0xff, 0xff, 0x07, 0xf0, 0xff, 0xff, 0x0f, 0xf8, 0xff, 0xff, 0x0f,
   0xf8, 0xff, 0xff, 0x1f, 0xfc, 0xff, 0xff, 0x1f, 0xfe, 0xff, 0xff, 0x3f,
   0xfe, 0xff, 0xff, 0x3f, 0xfe, 0xff, 0xff, 0x3f, 0xfc, 0xff, 0xff, 0x1f,
   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};"
image create bitmap ::tk::dialog::w3 -foreground black \
-data "#define w3_width 32\n#define w3_height 32
static unsigned char w3_bits[] = {
   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
   0x00, 0xc0, 0x03, 0x00, 0x00, 0xe0, 0x07, 0x00, 0x00, 0xe0, 0x07, 0x00,
   0x00, 0xe0, 0x07, 0x00, 0x00, 0xe0, 0x07, 0x00, 0x00, 0xe0, 0x07, 0x00,
   0x00, 0xc0, 0x03, 0x00, 0x00, 0xc0, 0x03, 0x00, 0x00, 0xc0, 0x03, 0x00,
   0x00, 0x80, 0x01, 0x00, 0x00, 0x80, 0x01, 0x00, 0x00, 0x80, 0x01, 0x00,
   0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x01, 0x00, 0x00, 0xc0, 0x03, 0x00,
   0x00, 0xc0, 0x03, 0x00, 0x00, 0x80, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00,
   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};"


proc ::tk::MessageBox {args} {
    global tcl_platform tk_strictMotif
    variable ::tk::Priv

    set w ::tk::PrivMsgBox
    upvar $w data

    #
    # The default value of the title is space (" ") not the empty string
    # because for some window managers, a 
    #		wm title .foo ""
    # causes the window title to be "foo" instead of the empty string.
    #
    set specs {
	{-default "" "" ""}
        {-icon "" "" "info"}
        {-message "" "" ""}
        {-parent "" "" .}
        {-title "" "" " "}
        {-type "" "" "ok"}
    }

    tclParseConfigSpec $w $specs "" $args

    if {[lsearch -exact {info warning error question} $data(-icon)] == -1} {
	error "bad -icon value \"$data(-icon)\": must be error, info, question, or warning"
    }
    if {[string equal [tk windowingsystem] "classic"]
	    || [string equal [tk windowingsystem] "aqua"]} {
	switch -- $data(-icon) {
	    "error"     {set data(-icon) "stop"}
	    "warning"   {set data(-icon) "caution"}
	    "info"      {set data(-icon) "note"}
	}
    }

    if {![winfo exists $data(-parent)]} {
	error "bad window path name \"$data(-parent)\""
    }

    switch -- $data(-type) {
	abortretryignore { 
	    set names [list abort retry ignore]
	    set labels [list &Abort &Retry &Ignore]
	}
	ok {
	    set names [list ok]
	    set labels {&OK}
	}
	okcancel {
	    set names [list ok cancel]
	    set labels [list &OK &Cancel]
	}
	retrycancel {
	    set names [list retry cancel]
	    set labels [list &Retry &Cancel]
	}
	yesno {
	    set names [list yes no]
	    set labels [list &Yes &No]
	}
	yesnocancel {
	    set names [list yes no cancel]
	    set labels [list &Yes &No &Cancel]
	}
	default {
	    error "bad -type value \"$data(-type)\": must be\
		    abortretryignore, ok, okcancel, retrycancel,\
		    yesno, or yesnocancel"
	}
    }

    set buttons {}
    foreach name $names lab $labels {
	lappend buttons [list $name -text [mc $lab]]
    }

    # If no default button was specified, the default default is the 
    # first button (Bug: 2218).

    if {$data(-default) == ""} {
	set data(-default) [lindex [lindex $buttons 0] 0]
    }

    set valid 0
    foreach btn $buttons {
	if {[string equal [lindex $btn 0] $data(-default)]} {
	    set valid 1
	    break
	}
    }
    if {!$valid} {
	error "invalid default button \"$data(-default)\""
    }

    # 2. Set the dialog to be a child window of $parent
    #
    #
    if {[string compare $data(-parent) .]} {
	set w $data(-parent).__tk__messagebox
    } else {
	set w .__tk__messagebox
    }

    # 3. Create the top-level window and divide it into top
    # and bottom parts.

    catch {destroy $w}
    toplevel $w -class Dialog
    wm title $w $data(-title)
    wm iconname $w Dialog
    wm protocol $w WM_DELETE_WINDOW { }
    # There is only one background colour for the whole dialog
    set bg [$w cget -background]

    # Message boxes should be transient with respect to their parent so that
    # they always stay on top of the parent window.  But some window managers
    # will simply create the child window as withdrawn if the parent is not
    # viewable (because it is withdrawn or iconified).  This is not good for
    # "grab"bed windows.  So only make the message box transient if the parent
    # is viewable.
    #
    if {[winfo viewable [winfo toplevel $data(-parent)]] } {
	wm transient $w $data(-parent)
    }    

    if {[string equal [tk windowingsystem] "classic"]
	    || [string equal [tk windowingsystem] "aqua"]} {
	unsupported::MacWindowStyle style $w dBoxProc
    }

    frame $w.bot -background $bg
    pack $w.bot -side bottom -fill both
    frame $w.top -background $bg
    pack $w.top -side top -fill both -expand 1
    if {![string equal [tk windowingsystem] "classic"]
	    && ![string equal [tk windowingsystem] "aqua"]} {
	$w.bot configure -relief raised -bd 1
	$w.top configure -relief raised -bd 1
    }

    # 4. Fill the top part with bitmap and message (use the option
    # database for -wraplength and -font so that they can be
    # overridden by the caller).

    option add *Dialog.msg.wrapLength 3i widgetDefault
    if {[string equal [tk windowingsystem] "classic"]
	    || [string equal [tk windowingsystem] "aqua"]} {
	option add *Dialog.msg.font system widgetDefault
    } else {
	option add *Dialog.msg.font {Times 14} widgetDefault
    }

    label $w.msg -anchor nw -justify left -text $data(-message) \
	    -background $bg
    if {[string compare $data(-icon) ""]} {
	if {([string equal [tk windowingsystem] "classic"]
		|| [string equal [tk windowingsystem] "aqua"])
		|| ([winfo depth $w] < 4) || $tk_strictMotif} {
	    label $w.bitmap -bitmap $data(-icon) -background $bg
	} else {
	    canvas $w.bitmap -width 32 -height 32 -highlightthickness 0 \
		    -background $bg
	    switch $data(-icon) {
		error {
		    $w.bitmap create oval 0 0 31 31 -fill red -outline black
		    $w.bitmap create line 9 9 23 23 -fill white -width 4
		    $w.bitmap create line 9 23 23 9 -fill white -width 4
		}
		info {
		    $w.bitmap create image 0 0 -anchor nw \
			    -image ::tk::dialog::b1
		    $w.bitmap create image 0 0 -anchor nw \
			    -image ::tk::dialog::b2
		    $w.bitmap create image 0 0 -anchor nw \
			    -image ::tk::dialog::i
		}
		question {
		    $w.bitmap create image 0 0 -anchor nw \
			    -image ::tk::dialog::b1
		    $w.bitmap create image 0 0 -anchor nw \
			    -image ::tk::dialog::b2
		    $w.bitmap create image 0 0 -anchor nw \
			    -image ::tk::dialog::q
		}
		default {
		    $w.bitmap create image 0 0 -anchor nw \
			    -image ::tk::dialog::w1
		    $w.bitmap create image 0 0 -anchor nw \
			    -image ::tk::dialog::w2
		    $w.bitmap create image 0 0 -anchor nw \
			    -image ::tk::dialog::w3
		}
	    }
	}
    }
    grid $w.bitmap $w.msg -in $w.top -sticky news -padx 2m -pady 2m
    grid columnconfigure $w.top 1 -weight 1
    grid rowconfigure $w.top 0 -weight 1

    # 5. Create a row of buttons at the bottom of the dialog.

    set i 0
    foreach but $buttons {
	set name [lindex $but 0]
	set opts [lrange $but 1 end]
	if {![llength $opts]} {
	    # Capitalize the first letter of $name
	    set capName [string toupper $name 0]
	    set opts [list -text $capName]
	}

	eval [list tk::AmpWidget button $w.$name -padx 3m] $opts \
		[list -command [list set tk::Priv(button) $name]]

	if {[string equal $name $data(-default)]} {
	    $w.$name configure -default active
	} else {
	    $w.$name configure -default normal
	}
	grid $w.$name -in $w.bot -row 0 -column $i -padx 3m -pady 2m -sticky ew
	grid columnconfigure $w.bot $i -uniform buttons
        incr i

	# create the binding for the key accelerator, based on the underline
	#
        # set underIdx [$w.$name cget -under]
        # if {$underIdx >= 0} {
        #     set key [string index [$w.$name cget -text] $underIdx]
        #     bind $w <Alt-[string tolower $key]>  [list $w.$name invoke]
        #     bind $w <Alt-[string toupper $key]>  [list $w.$name invoke]
        # }
    }
    bind $w <Alt-Key> [list ::tk::AltKeyInDialog $w %A]

    if {[string compare {} $data(-default)]} {
	bind $w <FocusIn> {
	    if {[string equal Button [winfo class %W]]} {
		%W configure -default active
	    }
	}
	bind $w <FocusOut> {
	    if {[string equal Button [winfo class %W]]} {
		%W configure -default normal
	    }
	}
    }

    # 6. Create a binding for <Return> on the dialog

    bind $w <Return> {
	if {[string equal Button [winfo class %W]]} {
	    tk::ButtonInvoke %W
	}
    }

    # 7. Withdraw the window, then update all the geometry information
    # so we know how big it wants to be, then center the window in the
    # display and de-iconify it.

    ::tk::PlaceWindow $w widget $data(-parent)

    # 8. Set a grab and claim the focus too.

    if {[string compare $data(-default) ""]} {
	set focus $w.$data(-default)
    } else {
	set focus $w
    }
    ::tk::SetFocusGrab $w $focus

    # 9. Wait for the user to respond, then restore the focus and
    # return the index of the selected button.  Restore the focus
    # before deleting the window, since otherwise the window manager
    # may take the focus away so we can't redirect it.  Finally,
    # restore any grab that was in effect.

    vwait ::tk::Priv(button)

    ::tk::RestoreFocusGrab $w $focus

    return $Priv(button)
}
}
}





