12-ctrl-panel-open-button.tcx 9.37 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355
global open_return 
global model_type
global tutor_wait


frame [control_panel_name].o_frame

tk_optionMenu [control_panel_name].o_frame.open_option model_type Existing New
[control_panel_name].o_frame.open_option configure -font button_font
[[control_panel_name].o_frame.open_option cget -menu] configure -font menu_font


button [control_panel_name].o_frame.open -text "Open Model:" \
       -font button_font -command {
  global local_connection
  global current_open_model
  global model_type
  global top_dir

  if {$local_connection == 0} {
    tk_messageBox -icon warning -type ok -title "Open warning" \
                  -message "You cannot use the Open Model button if the\
                            environment is not running on the same machine\
                            as ACT-R at this time, but if you want such a\
                            feature let Dan know because there are a couple\
                            of ways such a feature could be added."  
  } elseif {$current_open_model != ""} {
    tk_messageBox -icon warning -type ok -title "Open warning" \
                  -message "You must close the current model before you\
                            can open another one."
  } else {
    switch -- $model_type {
      Existing {
        set fname [tk_getOpenFile -title "Model to open" \
                            -filetypes {{"All Files" *}} \
                            -initialdir $top_dir] 
   
        if {$fname != ""} {
          open_model_file $fname
        }
      }
      New {
        set fname [tk_getSaveFile -title "Save new model as"\
                                  -filetypes {{"All Files" *}} \
                                  -initialdir $top_dir]
        if {$fname != ""} {
          write_data "(clear-all)\n" $fname
          open_model_file $fname
        }
      }
    }
  }
}
 
pack [control_panel_name].o_frame.open -side left
pack [control_panel_name].o_frame.open_option -side left

pack [control_panel_name].o_frame

send_environment_cmd \
  "create list-handler [control_panel_name].o_frame.open\
     open_return (lambda (x)) ()"

bind [control_panel_name].o_frame.open <Destroy> {
  remove_handler %W
}


proc open_model_file {fname} {
  global open_return
  global current_open_model
  global current_open_model_name
  global top_dir
  global options_array
  global tutor_mode
 
  set open_return ""

  send_environment_cmd \
    "update [get_handler_name [control_panel_name].o_frame.open] \
      (lambda (x) \
        (unwind-protect \ 
           (unless (environment-busy-p) \
             (set-environment-busy) \
             (let ((result (if $options_array(use_smart_load) \
                               (smart-loader \"$fname\")
                             (safe-load \"$fname\" nil)))) \
                (set-environment-free) \
                (format t (if (= 1 (first result)) \
                            \"~%#|## Model $fname loaded. ##|#~%\" \
                            \"~%#|## Failed loading model $fname. ##|#~%\")) \     
                result)) \
             (set-environment-free)))"
 
  wait_for_non_null open_return
           
       set win [toplevel [new_variable_name .reload_response]]
  
  # hide the window for speed and aesthetic reasons
  
  wm withdraw $win

  wm geometry $win [get_configuration .reload_response $win]


 set text_frame [frame $win.text_frame -borderwidth 0]  
 
 set text_box [text $text_frame.text -yscrollcommand \
                     "$text_frame.text_scrl set" -state normal \
                     -font text_font]
  
  
  
  set text_scroll_bar [scrollbar $text_frame.text_scrl \
                                 -command "$text_box yview"]


  set the_button [button $win.but -text "Ok" -font button_font -command "destroy $win"]

  place $text_frame -x 0 -y 0 -relheight 1.0 -height -30 -relwidth 1.0
  place $the_button -relx .5 -x -30 -width 60 -rely 1.0 -y -30 -height 30

  pack $text_scroll_bar -side right -fill y
  pack $text_box -side left -expand 1 -fill both


  if {[lindex $open_return 0] == 0} {
    wm title $win "ERROR Opening"
    $text_box insert 1.0 "Error:\n[lindex $open_return 1]"
  } else {
    wm title $win "SUCCESSFUL Load"
    $text_box insert 1.0 "Successful Load:\n[lindex $open_return 1]"
  }

  wm deiconify $win
  focus $win
      
  tkwait window $win 
  set open_return ""
    
  set current_open_model $fname
  set current_open_model_name [file tail $fname]
  set tutor_mode 0

  edit_model_file .model $fname 1
}

set pmatch_id ""

proc edit_model_file {win_name file_name reload} {
  global model_file_names
  global model_file_changed
  global tutor_mode
              
  if {[winfo exists $win_name] == 1} {
     wm deiconify $win_name
     raise $win_name

     if {$reload == 1} {
       set data [read_file $file_name]

       $win_name.frame.text delete 1.0 end
       $win_name.frame.text insert end $data
     }
  } else {
     toplevel $win_name

     wm withdraw $win_name
     
     wm geometry $win_name [get_configuration $win_name]
   
     wm title $win_name [file tail [file rootname $file_name]]

     # create a text_box to hold the output and put it in the window
     # also need a frame to hold it and the scroll bar

     frame $win_name.frame -borderwidth 0  
    
      set text_box [text $win_name.frame.text -yscrollcommand \
          "$win_name.frame.vscrl set" -xscrollcommand \
          "$win_name.frame.hscrl set" -font text_font -wrap none]
          
      set model_file_names($text_box) $file_name

      set v_scrl_bar [scrollbar $win_name.frame.vscrl -command \
            "$win_name.frame.text yview"]
   
      set h_scrl_bar [scrollbar $win_name.frame.hscrl -command \
            "$win_name.frame.text xview" -orient horizontal]
   
      place $win_name.frame -x 0 -y 0 -relwidth 1.0 -relheight 1.0
        
      pack $v_scrl_bar -side right -fill y 
      pack $h_scrl_bar -side bottom -fill x 
      pack $text_box -side left -expand 1 -fill both

      set data [read_file $file_name]

      if {$data != ""} {
        $text_box delete 1.0 end
        $text_box insert end $data
      }

      set model_file_changed($text_box) 0

      bind $text_box <KeyPress-parenright> {
        global model_file_changed
        
        set model_file_changed(%W) 1
        
        set start [%W index insert]
        
        set paren_count 1
        set index_count 0
        set hit_start 0

        while { $paren_count != 0 && $hit_start == 0} {
         
          incr index_count 1

          set cur_char [%W get "$start - $index_count chars"]
          
          if {[%W index "$start - $index_count chars"] == [%W index 1.0]} {
            set hit_start 1
          }

          if { $cur_char == ")" } {
            incr paren_count 1
          } elseif {$cur_char == "(" } {
            incr paren_count -1
          }
       }

       global pmatch_id       

       if {$paren_count == 0} {
          if {[lsearch -exact [%W tag names] pmatch] != -1} {
            after cancel $pmatch_id
            %W tag delete pmatch
          }

          %W tag add pmatch "$start - $index_count chars"
          %W tag configure pmatch -underline 1
          
          set pmatch_id [after 300 {%W tag delete pmatch}]
       }
      }

      bind $text_box <KeyPress-parenleft> {
        global model_file_changed
        
        set model_file_changed(%W) 1
        
        set start [%W index insert]
        
        set paren_count 1
        set index_count -1
        set hit_end 0

        while { $paren_count != 0 && $hit_end == 0} {
         
          incr index_count 1

          set cur_char [%W get "$start + $index_count chars"]
          
          if {[%W index "$start + $index_count chars"] == [%W index end]} {
            set hit_end 1
          }

          if { $cur_char == "(" } {
            incr paren_count 1
          } elseif {$cur_char == ")" } {
            incr paren_count -1
          }
       }

       global pmatch_id       

       if {$paren_count == 0} {
          if {[lsearch -exact [%W tag names] pmatch] != -1} {
            after cancel $pmatch_id
            %W tag delete pmatch
          }

          %W tag add pmatch "$start + $index_count chars"
          %W tag configure pmatch -underline 1
          
          set pmatch_id [after 300 {%W tag delete pmatch}]
       }
      }
   
      bind $text_box <KeyPress> {
        global model_file_changed
        
        set model_file_changed(%W) 1

        if $tutor_mode {
          handle_tutor_entry %W %K
        }
      }
        
      wm protocol $win_name WM_DELETE_WINDOW "prompt_close_text $win_name"
         

      wm deiconify $win_name
      raise $win_name
  }
} 
 

proc prompt_close_text {win} {
  global model_file_names
  global model_file_changed

  if {$model_file_changed($win.frame.text) == 1} {
    set answer \
      [tk_messageBox -icon warning -title \
                     "Closing [file tail $model_file_names($win.frame.text)]" \
                     -message "Save contents before closing?" \
                     -type yesnocancel]

    switch -- $answer {
      yes {
        save_text $win.frame.text $model_file_names($win.frame.text)
        destroy $win
      }
      no {
        destroy $win 
      }
    }
  } else {
    destroy $win
  }


  global current_open_model
  global current_open_model_name
   
  set current_open_model ""
  set current_open_model_name "No Model Open"




}





proc normal_key {key} {
  return [regexp {^([a-zA-Z0-9]|underscore|minus|plus|equal|BackSpace|Shift_L|Shift_R)$} $key]
}