proc rmiwin_mainProc {} {
global rmiwin env

    wm withdraw .
    set d [ form_init .rmiwin "ICEM ic3m main window" "" nobuttons ]
    if { $d != "" } {
            
      form_frame $d.1 ridge 2 ""		;# left frame 
      form_frame $d.2 flat 4 ""			;# right frame
      form_frame $d.3 flat 1 ""			;# message frame
      
      rmiwin_CreateLogo $d.1
      rmiwin_CreateButtons $d.2
      rmiwin_CreateMessageMask $d.3
         
      pack append $d \
      		  $d.1 { left fill expand } \
      		  $d.2 { left fill expand } \
      		  $d.3 { left fill expand }
      		  
      form_finish +200+200
    }
    set rmiwin(WorkingDirectory) [ pwd ]
    rmiwin_SearchForRMIProjects
    
    set rmiwin(ParamFile) "param.prp"
    set rmiwin(ProlxmFile) "rammice"
    set rmiwin(ProlxFile) "prol-x"
    set rmiwin(ProlinklBatchFile) "prolinkl.in"
    set rmiwin(ProsizeBatchFile) "prosize.in"

    rmiwin_DefineBubbles $d.2
    bubble_setup
    
    wm protocol .rmiwin WM_DELETE_WINDOW "rmiwin_Quit .rmiwin"
    
    if ![ rmiwin_CheckCommandPaths ] {
      rmiwin_Quit .rmiwin
    }
}

proc rmiwin_CreateLogo { w } {
global rmiwin
  
  set font -*-courier-bold-r-normal--14*
  frame $w.1 -bd 1 -relief flat
  label $w.1.1 -image $rmiwin(Logo) -bd 1 
  label $w.1.2 -image $rmiwin(Logo2) -bd 1 
  pack append $w.1 \
              $w.1.1 { top fill expand } \
              $w.1.2 { top fill expand }
  
  label $w.2 -text "IC3M" -font $font -bd 1
  pack append $w \
  	      $w.1 { top fill expand } \
  	      $w.2 { top fill expand } 
}

proc rmiwin_CreateButtons { w } {
global rmiwin
  
  button $w.1 -text " Create \n Static Mesh " -command "rmiwin_StartMed" -bd 3
  button $w.2 -text " Make \n Model File " -command "rmiwin_StartMakeModelFile" -bd 3
  button $w.3 -text " Create \n Dynamic Mesh " -command "rmiwin_StartProstar" -bd 3
  button $w.4 -image $rmiwin(Browse) -command "rmiwin_SelectDirectory WorkingDirectory" -bd 3
  button $w.5 -text " Quit " -command "rmiwin_Quit .rmiwin" -bd 3
  pack append $w \
              $w.1 { top fill expand } \
              $w.2 { top fill expand } \
              $w.3 { top fill expand } \
              $w.4 { top fill expand } \
              $w.5 { top fill expand }
}

proc rmiwin_CreateMessageMask { w } {
global rmiwin
  
  frame $w.mess -bd 6
  set rmiwin(MessFont) -*-courier-bold-r-normal--14*
  text  $w.mess.text -bd 2 -relief sunken -height 5 -width 50 \
  		    -fg black -bg white -wrap word -font $rmiwin(MessFont) \
  		    -yscrollcommand "$w.mess.scroll set" 
  scrollbar $w.mess.scroll -command "$w.mess.text yview" 
  set rmiwin(MaskMessage) $w.mess.text
  pack $w.mess.scroll -side right -fill y
  pack $w.mess.text -side left -expand 1 -fill both
  pack $w.mess -side top -expand 1 -fill both
  set rmiwin(MaskMessage) $w.mess.text
  set rmiwin(MessLines) 5
}

proc rmiwin_ShowMessage { mess { color "" } { font "" } } {
global rmiwin rmiwin_tagnumber
  
  set now [ $rmiwin(MaskMessage) index "end -1c" ]
  $rmiwin(MaskMessage) insert end $mess
  if { $color != "" } {
    if ![ info exists rmiwin_tagnumber ] { 
      set rmiwin_tagnumber 0
    }
    set tag t[ incr rmiwin_tagnumber ]
    $rmiwin(MaskMessage) tag configure $tag -foreground $color
    $rmiwin(MaskMessage) tag add $tag $now "end - 1c"
  }
  $rmiwin(MaskMessage) yview end
  update
}


proc rmiwin_DefineBubbles { w } {
global rmiwin

  define_strings $w.1 "" {
      Launch icemcfd to create static mesh 
  } "" ""
  define_strings $w.2 "" {
      Make a Star model file 
  } "" ""
  define_strings $w.3 "" {
     Take static mesh from step 1 and create moving mesh 
  } "" ""
  define_strings $w.4 "" {
     Choose your working directory
  } "" ""
  define_strings $w.5 "" {
     Quit ic3m main window 
  } "" ""
}

proc rmiwin_StartMed {} {
global rmiwin env

  if [ info exists rmiwin(Pid,MED) ] {
    if [ rmiwin_GetPid $rmiwin(Pid,MED) ] {
      rmiwin_ShowMessage "ICEM CFD is still running.\n"
      return
    } else {
      unset rmiwin(Pid,MED)
    }
  }
  cd $rmiwin(WorkingDirectory)
  rmiwin_ShowMessage "Starting icemcfd ...\n" blue
  set rmiwin(Pid,MED) [ eval exec $env(ICEM_ACN)/bin/icemcfd & ]
}

proc rmiwin_StartProstar {} {
global rmiwin env


  if [ info exists rmiwin(Pid,PROSTAR) ] {
    if [ rmiwin_GetPid $rmiwin(Pid,PROSTAR) ] {
      rmiwin_ShowMessage "Prostar is still running.\n"
      return
    } else {
      unset rmiwin(Pid,PROSTAR)
    }
  }
  
  if ![ info exists rmiwin(PathToTransfer) ] {
    rmiwin_ShowMessage "Select path to transfer directory.\n"
    rmiwin_SelectDirectory PathToTransfer
    if ![ info exists rmiwin(PathToTransfer) ] {
      return
    } else {
      rmiwin_ShowMessage "Path to current transfer directory is \n"
      rmiwin_ShowMessage "$rmiwin(PathToTransfer)\n" blue
    }
  }
  set cmd $env(RMIHOME)/$rmiwin(ProlxmFile)
  rmiwin_ShowMessage "Starting $rmiwin(ProlxmFile) ...\n" blue
  cd $rmiwin(PathToTransfer)
  set rmiwin(Pid,PROSTAR) [ eval exec $cmd & ]
  cd $rmiwin(WorkingDirectory)
}

proc rmiwin_StartMakeModelFile {} {
global rmiwin

  if ![ info exists rmiwin(ProlinklCmd) ] {
    return 
  }
  if ![ rmiwin_EnterName {Enter casename:} CaseName 1 ] {
    return
  } 
  if { $rmiwin(CaseName) == "" } {
    rmiwin_ShowMessage "ERROR: No casename selected.\n" red
    return
  }  

  rmiwin_OutputTransferDirectory
  cd $rmiwin(PathToTransfer)
  if [ rmiwin_SetupProstarParameters ] {
    return
  }
  rmiwin_MakeModelFile
  cd $rmiwin(WorkingDirectory)
}

proc rmiwin_MakeModelFile {} {
global rmiwin env
    
  set file_mdl "$rmiwin(CaseName).mdl"
  set file_echo "$rmiwin(CaseName).echo"
  if [ file exists $file_mdl ] {
    set answer [ rmiwin_ShowQuestion Warning "A $rmiwin(CaseName).mdl file already exists."\
                 " Rename " " Overwrite " " Cancel " ]
    if { $answer == "1" } {
       if ![ rmiwin_EnterName {Enter new name:} BackupMDLName ] {
         return
       }
       set rmiwin(BackupMDLName) [ string trim $rmiwin(BackupMDLName) ]
       if { $rmiwin(BackupMDLName) == "" } {
         rmiwin_ShowMessage "ERROR: No new casename selected.\n" red
         return
       } else {
         set file_bak "$rmiwin(BackupMDLName)"
         file rename -force $file_mdl $file_bak
         if [ file exists $file_echo ] {
           file rename -force $file_echo $file_echo.$rmiwin(BackupMDLName)
         }
         rmiwin_ShowMessage "Renaming $rmiwin(CaseName).mdl to $rmiwin(BackupMDLName).\n"
       }
    } elseif { $answer == "2" } {
      set answer [ rmiwin_ShowQuestion Question "Are you sure you want to delete ?"\
                 " Yes " " No " ]
      if { $answer == "1" } {          
        file delete $file_mdl
        rmiwin_ShowMessage "Removing $rmiwin(CaseName).mdl.\n"
        set file_echo $rmiwin(CaseName).echo
        if [ file exists $file_echo ] {
          file delete $file_echo
        }
      } else {
        return
      }
    } else {
      return
    }
  }

  if [ file exists $rmiwin(ParamFile) ] {
    set rmiwin(ALREADY_EXISTS) "u"
    rmiwin_ShowMessage "Found $rmiwin(ParamFile) file.\n"
    rmiwin_ShowMessage "Using existing $rmiwin(ParamFile) file for creating model.\n"
  } else {
    set answer [ rmiwin_ShowQuestion Question "A $rmiwin(ParamFile) file\
                 does not exists.\n\
                 What do you want ?\n\
                 Continue - using default param.prp file (750 MB memory) \n\
                 Stop - user want to create the param.prp file \n"\
                 " Continue " " Stop " ]
    if { $answer == "1" } {
      set rmiwin(ALREADY_EXISTS) "u"
      set source $env(ICEM_ACN)/lib/rmi/$rmiwin(ParamFile)
      set aim $rmiwin(WorkingDirectory)/$rmiwin(PathToTransfer)
      file copy -force $source $aim
      rmiwin_ShowMessage "Using default $rmiwin(ParamFile) file for creating model.\n"
    } elseif { $answer == "2" } {
      rmiwin_ShowMessage "Make model file cancelled.\n"
      rmiwin_ShowMessage "Please use the prosize command to create the param.prp file\
                          in the project/transfer directory.\n"
      return
    }
  }
  
  rmiwin_ShowMessage "Creating model file $rmiwin(CaseName).mdl ... "
  if [ rmiwin_RunProlinkl_x ] {
    rmiwin_ShowMessage "\nERROR: No $rmiwin(CaseName).mdl file created." red
    return
  }
  if [ file exists $rmiwin(ProlinklBatchFile) ] {
    file delete $rmiwin(ProlinklBatchFile)
  }
  if [ file exists $rmiwin(ProlxFile) ] {
    file delete $rmiwin(ProlxFile)
  }
  if ![ file exists $rmiwin(CaseName).mdl ] {
    rmiwin_ShowMessage "\nERROR: No $rmiwin(CaseName).mdl file created." red
  } else {
    rmiwin_ShowMessage "Done.\n"
  }
}

proc rmiwin_SetupProstarParameters {} {
global rmiwin

  set rmiwin(inpFile) $rmiwin(CaseName).inp
  if ![ file exists $rmiwin(inpFile) ] {
    rmiwin_ShowMessage "ERROR: Missing file $rmiwin(inpFile).\n" red
    return 1
  } else {
    set fp [ open $rmiwin(inpFile) r ]
    set line [ gets $fp ]
    close $fp
  }
  if { [ lindex $line 0 ] != "!nelem" } {
    set rmiwin(MAXCEL) "800000"  
  } else {  
    set cells [ lindex $line 1 ]
    set rmiwin(MAXCEL) [ expr $cells + 100000 ]
  }

  set rmiwin(MAXEVE) "1000"
  set rmiwin(MAXTAB) "1000"
  set rmiwin(NCPDIM) "500"
  set rmiwin(MAXNCP) "20000"
  set rmiwin(POST_PROCESSING) "y"
  set rmiwin(MOVING_GRIDS) "y"
  set rmiwin(TRANSIENT_EVENT) "y"
  return 0
}

proc rmiwin_RunProlinkl_x {} {
global rmiwin env
    
  if [ file exists $rmiwin(ProlxFile) ] {
    file delete $rmiwin(ProlxFile)
  }
  
  rmiwin_CreateProlXBatchfile
  if ![ file exists $rmiwin(ProlinklBatchFile) ] {
    rmiwin_ShowMessage "ERROR: Unable to create file $rmiwin(ProlinklBatchFile) (X).\n" red
    return 1
  }
  
  catch { eval exec $rmiwin(ProlinklCmd) < $rmiwin(ProlinklBatchFile) }
  if ![ file exists $rmiwin(ProlxFile) ] {
    file delete $rmiwin(ProlinklBatchFile)
    return 1
  } else {
    file delete $rmiwin(ProlinklBatchFile)
  }
  
  rmiwin_CreateMDLBatchfile
  if ![ file exists $rmiwin(ProlinklBatchFile) ] {
    rmiwin_ShowMessage "ERROR: Unable to create file $rmiwin(ProlinklBatchFile) (MDL).\n" red
    return 1
  }
  
  set cmd "$env(STARDIR)/bin/prostar"
  eval exec $cmd < $rmiwin(ProlinklBatchFile)
  return 0
}

proc rmiwin_CreateProlXBatchfile {} {
global rmiwin
        
  set fp [ open $rmiwin(ProlinklBatchFile) w ]
  puts $fp  "x"
  puts $fp  "n"
  puts $fp  "$rmiwin(ALREADY_EXISTS)"		;# A param.prp FILE ALREADY EXISTS.
  if { $rmiwin(ALREADY_EXISTS) == "u" } {
    close $fp
    return
  }
  puts $fp "t" 					;# IS YOUR MESH PRIMARILY HEX OR TET?
  puts $fp "$rmiwin(MAXCEL)" 			;# ENTER MAX NO. OF CELLS
  for { set i 1 } { $i < 3 } { incr i } {
    puts $fp ""
  }
  puts $fp "$rmiwin(MAXTAB)" 			;# ENTER MAX NO. OF CELL TABLE ENTRIES
  for { set i 1 } { $i < 9 } { incr i } {
    puts $fp ""
  }
  puts $fp "$rmiwin(NCPDIM)" 			;# ENTER MAX NO. OF CELLS IN EACH COUPLED SET
  puts $fp "$rmiwin(MAXNCP)" 			;# ENTER MAX NO. OF COUPLED SETS
  for { set i 1 } { $i < 5 } { incr i } {
    puts $fp ""
  }
  puts $fp "$rmiwin(POST_PROCESSING)"		;# WILL YOU BE DOING ANY POST-PROCESSING ON TRANSIENT ANALYSES?
  puts $fp "$rmiwin(MOVING_GRIDS)" 		;# WILL THE TRANSIENT INCLUDE MOVING GRIDS?
  puts $fp "$rmiwin(TRANSIENT_EVENT)" 		;# WILL YOU BE DOING A TRANSIENT WITH THE EVENT MODULE
  puts $fp "$rmiwin(MAXEVE)" 			;# HOW MANY EVENTS WILL YOU NEED?
  puts $fp "-1" 				;# use defaults    


  puts $fp  ""
  close $fp
}

proc rmiwin_CreateMDLBatchfile {} {
global rmiwin
      
  set fp [ open $rmiwin(ProlinklBatchFile) w ]
  puts $fp  "x"
  puts $fp  "$rmiwin(CaseName)"
  puts $fp  "ifile $rmiwin(CaseName)"
  puts $fp  ""
  puts $fp  "quit, save"
  close $fp
}

proc rmiwin_CheckCommandPaths {} {
global rmiwin env

  set rmiwin(ProlinklCmd) $env(STARDIR)/bin/prolinkl
  if ![ file exists $rmiwin(ProlinklCmd) ] {
    puts "ERROR: File not found - $rmiwin(ProlinklCmd).\n"
    unset rmiwin(ProlinklCmd)
    return 0
  }
  return 1
}

proc rmiwin_SelectDirectory { what } {
global rmiwin
  
  if { $what == "WorkingDirectory" } {
    set mess "Select working directory"
  } elseif { $what == "PathToTransfer" } {
    set mess "Select transfer directory"
  }
  set file [ file_select $mess ./ "Browse" 1 1 ]
  set file [ string trim $file ]
  if { $file != "" } {
    set rmiwin($what) $file
    if { $what == "WorkingDirectory" } {
      rmiwin_UnsetArrays
      rmiwin_SearchForRMIProjects
    }
  } else {
    rmiwin_ShowMessage "Nothing selected.\n"
  }
}

proc rmiwin_UnsetArrays {} {
global rmiwin

  foreach item [ array names rmiwin TransferDirectoryFound,* ] {
    unset rmiwin($item)
  }
  foreach item [ array names rmiwin Projects* ] {
    unset rmiwin($item)
  }
}     

proc rmiwin_SearchForRMIProjects {} {
global rmiwin
  
  cd $rmiwin(WorkingDirectory)
  rmiwin_ShowMessage "Your current working directory is "
  rmiwin_ShowMessage "$rmiwin(WorkingDirectory).\n" blue
  if [ file exists $rmiwin(WorkingDirectory)/mulcad ] {
    set answer [ rmiwin_ShowQuestion Question "Mulcad directory found in your working directory.\n\
                 Which directory style do you want to use ?" " Mulcad " " New " ]
    if { $answer == "1" } {
      set rmiwin(MulcadUsed) 1
      rmiwin_ShowMessage "Using mulcad directory style to search for projects with a transfer directory.\n"
      set path mulcad
    } elseif { $answer == "2" } {
      catch { unset rmiwin(MulcadUsed) }
      rmiwin_ShowMessage "Using new directory style to search for projects with a transfer directory.\n"
      set path ./
    }
  } else {
    catch { unset rmiwin(MulcadUsed) }
    rmiwin_ShowMessage "No mulcad directory found in your working directory.\n"
    rmiwin_ShowMessage "Using new directory style to search for projects with a transfer directory.\n"
    set path ./
  }
  rmiwin_ShowMessage "Searching for transfer directories ...\n"
  rmiwin_SearchForTransferDirectory $path
  rmiwin_VerifyTransferDirectory
}

proc rmiwin_SearchForTransferDirectory { path } {
global rmiwin
  
  set rmiwin(ProjectsFound) ""
  if [ info exists rmiwin(MulcadUsed) ] {
    set projects [ glob -nocomplain $path/*/* ]
  } else {
    set projects [ glob -nocomplain $path/* ]
  }
  foreach item $projects {
    if ![ file isdirectory $item ] {
      continue
    }
    set proj [ lindex [ split $item / ] 1 ]
    if [ info exists rmiwin(MulcadUsed) ] {
      set subp [ lindex [ split $item / ] 2 ]
      set name $proj/$subp
    } else {
      set name $proj
    }
    set transfer [ glob -nocomplain $item/transfer ]
    foreach item_transfer $transfer {
      set rmiwin(TransferDirectoryFound,$name,$item_transfer) 1
      set rmiwin(Projects,$name,vrt) ""
      set rmiwin(Projects,$name,inp) ""
      set rmiwin(Projects,$name,cel) ""
      foreach type { vrt inp cel } {
        set rmiwin(Projects,$name,$type) [ glob -nocomplain $item_transfer/*.$type ]
        if { ($rmiwin(Projects,$name,vrt) != "") && \
             ($rmiwin(Projects,$name,inp) != "") && \
             ($rmiwin(Projects,$name,cel) != "") } {
          set case [ file rootname [ file tail $rmiwin(Projects,$name,$type) ] ]
          if ![ info exists found($name,$case) ] {
            lappend rmiwin(ProjectsFound) [ list $name $case ]
            set found($name,$case) 1
          }
        }
      }
    }
  }
}

proc rmiwin_OutputTransferDirectory {} {
global rmiwin
  
  foreach item [ array names rmiwin TransferDirectoryFound,* ] {
    if { $rmiwin($item) == "1" } {
      if { [ lindex [ split $item , ] 1 ] != $rmiwin(ProjectName) } {
        continue
      }
      foreach i $rmiwin(ProjectsFound) {
        if { [ lindex $i 0 ] == $rmiwin(ProjectName) } {
          set path [ lindex [ split $item , ] 2 ]
          rmiwin_ShowMessage "Path to current transfer directory is \n"
          rmiwin_ShowMessage "$path\n" blue
          set rmiwin(PathToTransfer) $path
          break
        }
      }
    }
  }
}

proc rmiwin_VerifyTransferDirectory {} {
global rmiwin
  
  if { $rmiwin(ProjectsFound) == "" } {
    set found_transfer 0
    foreach item [ array names rmiwin TransferDirectoryFound,* ] {
      if { $rmiwin($item) == "1" } {
        set name [ lindex [ split $item , ] 1 ]
        rmiwin_ShowMessage "Found a transfer directory in project $name but it\
                            contains no ic3m files.\n"
        if !$found_transfer {
          set found_transfer 1
        }
      }
    }
    if !$found_transfer {
      rmiwin_ShowMessage "No transfer directories found in your current working directory.\
                          \nPlease change your working directory.\n" red
    } else {
      rmiwin_ShowMessage "Maybe you should change your current working directory or launch\
                          ICEM ic3m by step 1 to create the missing files.\n"
    }
  } else {
    set found_transfer 1
    rmiwin_ShowMessage "Found [ llength $rmiwin(ProjectsFound) ] project(s) with a\
                        transfer directory.\n"
  }
}

proc rmiwin_GetPid { pid } {

  set list [ split [ exec ps ] \n ]
  for { set i 1 } { $i < [ expr [ llength $list ] ] } { incr i } {
    set p [ lindex [ lindex $list $i ] 0 ]
    if { $p == $pid } {
      return 1
    }
  }
  return 0
}

proc rmiwin_ShowQuestion { what mess one two { three "" } } {
global okQuestion

  set win [ toplevel .question ]
  set font -*-courier-bold-r-normal--14*
  wm title $win $what
  bind $win <Visibility> "raise $win"
  set okQuestion 1
  label $win.label -text $mess -bg white -font $font
  frame $win.f -bd 3
  set opts "-bd 3 -bg gray -activebackground gray -padx 3 -pady 3"
  eval button $win.f.yes -text "$one" -command \"set okQuestion 1\" $opts
  eval button $win.f.no -text "$two" -command \"set okQuestion 2\" $opts
  if { $three != "" } {
    eval button $win.f.cancel -text "$three" -command \"set okQuestion 0\" $opts
  }
  pack append $win.f \
  	      $win.f.yes { left fill } \
  	      $win.f.no { left fill }
  if { $three != "" } {
    pack append $win.f \
  	      $win.f.cancel { left fill }
  }
  pack $win.label -side top -expand 1 -fill both
  pack $win.f -side bottom
  wm resizable $win 0 0 
  wm geometry $win +300+300  
  grab set $win
  tkwait variable okQuestion
  grab release $win
  catch { destroy $win }
  return $okQuestion
}

proc rmiwin_EnterName { mess var { list "" } } {
global rmiwin okQuestion

  set rmiwin($var) ""
  set win [ toplevel .getinput ]
  set font -*-courier-bold-r-normal--14*
  if { $list == "1" } {
    wm title $win "Make Model File"
  } elseif { $list == "2" } {
    wm title $win "Create Dynamic Mesh"
  }
  bind $win <Visibility> "raise $win"
  set okQuestion 1
  frame $win.i -bd 7
  label $win.i.label -text "$mess " -font $font
  entry $win.i.entry -textvariable rmiwin($var) -bg white -font $font
  pack append $win.i \
  	      $win.i.label { left fill } \
  	      $win.i.entry { left fill }

  if { $list != "" } {
    frame $win.b -bd 3
    label $win.b.label -text "Available projects and casenames" -font $font
    rmiwin_BuildCanvasBox $win.b 75
    pack append $win.b \
                $win.b.label { top fill expand } \
                $win.b.box { top fill expand }
    set i 0
    set rmiwin(ProjectName) ""
    foreach item $rmiwin(ProjectsFound) {
      set frame [ frame $win.b.box.canvas.$i ]
      set proj [ lindex $item 0 ]
      set case [ lindex $item 1 ]
      $win.b.box.canvas create window 0 [ expr $i * 25 ] -window $frame -anchor nw
      radiobutton $frame.sel$i -text "" -anchor w -variable selected -value sel($proj,$case) \
      			       -command "set rmiwin(ProjectName) $proj; \
      		 	                 set rmiwin(CaseName) $case"
      $frame.sel$i deselect
      label $frame.pn$i -text "Project:" -anchor w -font $font
      label $frame.proj$i -text $proj -width 25 -anchor w -font $font -fg blue
      label $frame.cn$i -text "Casename:" -anchor w -font $font
      label $frame.case$i -text $case -width 20 -anchor w -font $font -fg blue
      pack append $frame \
                  $frame.sel$i { left fill } \
                  $frame.pn$i { left fill } \
                  $frame.proj$i { left fill } \
                  $frame.cn$i { left fill } \
                  $frame.case$i { left fill }
      incr i
    }
    set sc $win.b.box.scroll
    set last $i
    $sc set 0.0 [ expr { ($last < 5) ? 1.0 : ($last / 5.0) } ]
    set sh [ expr 25 * $last ]
    set rw [ winfo reqwidth $win.b.box.canvas ]
    $win.b.box.canvas configure -width 550 -scrollregion "0 0 $rw $sh"
  }
  
  frame $win.f -bd 3
  set opts "-bd 3 -bg gray -activebackground gray -width 7 -padx 3 -pady 3"
  eval button $win.f.ok -text "OK" -command \"set okQuestion 1\" $opts
  eval button $win.f.cancel -text "Cancel" -command \"set okQuestion 0\" $opts
  pack append $win.f \
  	      $win.f.ok { left fill } \
  	      $win.f.cancel { left fill }
  pack $win.i -side top -expand 1 -fill both
  if { $list != "" } {
    pack $win.b -side top
  }
  pack $win.f -side bottom
  wm resizable $win 0 0 
  wm geometry $win +300+300  
  grab set $win
  tkwait variable okQuestion
  if { ($list != "") && ($okQuestion == "1") &&
       ($rmiwin(ProjectName) == "") } {
    foreach item $rmiwin(ProjectsFound) {
      if { [ lindex $item 1 ] == "$rmiwin($var)" } {
        set rmiwin(ProjectName) [ lindex $item 0 ]
        if ![ info exists found ] {
          set found 1
        } else {
          incr found
        }
      }
    }
    if ![ info exists found ] {
      if { $rmiwin($var) == "" } {
        rmiwin_ShowMessage "ERROR: Please select a casename.\n" red
      } else {
        rmiwin_ShowMessage "ERROR: Casename $rmiwin($var). No such casename found in any project.\
                           \nTry to select a casename again.\n" red
      }
      set okQuestion 0
    } elseif { $found > "1" } {
      rmiwin_ShowMessage "ERROR: Found $found projects with casename $rmiwin($var).\
                          \nTry to select a casename again using the radiobutton.\n" red
      set okQuestion 0
    }
  }
  grab release $win
  catch { destroy $win }
  return $okQuestion
}

proc rmiwin_BuildListBox { win width height } {

  frame $win.box -bd 3
  listbox $win.box.files -relief sunken -bd 3 -width $width -height $height \
                         -yscrollcommand "$win.box.scroll set"
  scrollbar $win.box.scroll -command "$win.box.files yview"
  pack append $win.box $win.box.files { left fill expand } $win.box.scroll { left fill } 
}

proc rmiwin_BuildCanvasBox { win height } {

  frame $win.box -bd 3 -relief sunken
  canvas $win.box.canvas -yscrollcommand "$win.box.scroll set" -height $height
  scrollbar $win.box.scroll -orient v -command "$win.box.canvas yview"  
  pack append $win.box $win.box.canvas { left fill expand } $win.box.scroll { left fill }
}


proc rmiwin_Quit { w } {
  
  destroy $w
  puts "Exit from ic3m main window.\n"
  exit
}

set is_win 0
set has_blt 0
set small_screen_hack 0
set language_text_form_override 0

source $env(ICEM_ACN)/lib/guibase/misc.tcl
source $env(ICEM_ACN)/lib/guibase/sizes.tcl
source $env(ICEM_ACN)/lib/guibase/settings.tcl
source $env(ICEM_ACN)/lib/guibase/forms.tcl
source $env(ICEM_ACN)/lib/guibase/formutil.tcl
source $env(ICEM_ACN)/lib/guibase/mainwindow.tcl
source $env(ICEM_ACN)/lib/guibase/fileselect.tcl
source $env(ICEM_ACN)/lib/guibase/bubble.tcl
if [file exists $env(ICEM_ACN)/lib/guibase/toplevel.tcl] {
  source $env(ICEM_ACN)/lib/guibase/toplevel.tcl
}

set busy_cur [list @$env(ICEM_ACN)/lib/guibase/busy.cur orange]

set rmiwin(Logo)  [ image create photo -file $env(ICEM_ACN)/lib/rmi/icemcfd.gif ]
set rmiwin(Logo2) [ image create photo -file $env(ICEM_ACN)/lib/rmi/starcd.gif ]
set rmiwin(Browse) [ image create photo -file $env(ICEM_ACN)/lib/rmi/browse.gif ]

rmiwin_mainProc
