reference/ocr-simple/new_ui.tcl
changeset 0 6b8091ca909a
equal deleted inserted replaced
-1:000000000000 0:6b8091ca909a
       
     1 #
       
     2 # user interface code (tcl visuals) for OCR
       
     3 # started 9/95, Archie Russell 
       
     4 
       
     5 append OCRCHIE_ROOT [pwd] "/"
       
     6 append face_image $OCRCHIE_ROOT face_happy.xbm
       
     7 append eye_image $OCRCHIE_ROOT eye.xbm
       
     8 append write_image $OCRCHIE_ROOT edit2.xbm
       
     9 
       
    10 set xvprocess "0"
       
    11 set main_window_width 800
       
    12 set main_window_height 800
       
    13 set dummy 0
       
    14 # I'd like to be able to use the above parameters in here,
       
    15 # but I think tcl might get a little angry if I try
       
    16 # the size of the window, and the position of its upper left
       
    17 set main_window_geometry 800x800+200+100
       
    18 
       
    19 set menu_bar_width $main_window_width
       
    20 set menu_bar_height 50
       
    21 set button_bar_width $main_window_width
       
    22 set button_bar_height 50
       
    23 set display_height 400
       
    24 set edit_window_height 300
       
    25 set quit_dialog_geometry 300x135+500+500
       
    26 set save_ascii_geometry 275x140+500+500
       
    27 # save a little room for scrollbars, etc.
       
    28 
       
    29 set BACKGROUND #CCCCCC
       
    30 set FOREGROUND #000000
       
    31 # set FONT -bitstream-*-medium-r-normal--26-171-110-110-p-150-iso8859-1
       
    32 set FONT -bitstream-*-medium-r-normal--19-140-85-85-p-110-hp-roman8
       
    33 set SMALLFONT -bitstream-*-medium-r-normal--19-140-85-85-p-110-hp-roman8
       
    34 
       
    35 set EDIT_BACKGROUND #000000
       
    36 set LOW_PRECISION_BACKGROUND blue
       
    37 set MISPELLED_BACKGROUND SeaGreen
       
    38 set UNKNOWN_CHAR_BACKGROUND red
       
    39 
       
    40 set scroll_inc 30
       
    41 
       
    42 
       
    43 set canvas_width [expr $main_window_width - 30]
       
    44 
       
    45 
       
    46 proc init_user_interface {} {
       
    47     
       
    48 # tcl requires declaration of global variables used in a fxn
       
    49 
       
    50     global main_window_geometry main_window_width main_window_height menu_bar_width menu_bar_height button_bar_width button_bar_height display_height BACKGROUND FOREGROUND EDIT_BACKGROUND edit_window_height
       
    51 
       
    52 # toplevel windows are at the same level as 'xterms'    
       
    53     toplevel .main_window
       
    54 
       
    55 # $ sign means 'get the value' (otherwise uses the string)
       
    56     wm geometry .main_window $main_window_geometry
       
    57     wm title .main_window "OCR user interface"
       
    58     .main_window configure -background $BACKGROUND
       
    59 # frames are subwindows that are there mostly to 'hold' other windows
       
    60     frame .main_window.menu_bar -width $menu_bar_width -height $menu_bar_height -relief raised -bd 2 -background $BACKGROUND
       
    61     init_menu_bar
       
    62 
       
    63     frame .main_window.button_bar -width $button_bar_width -height $button_bar_height -background $BACKGROUND 
       
    64     init_button_bar
       
    65     
       
    66 
       
    67     init_display
       
    68 
       
    69     frame .main_window.edit_window -width $main_window_width -height $edit_window_height -relief ridge -bd 5 -bg $EDIT_BACKGROUND
       
    70     init_edit_window
       
    71 
       
    72 # pack puts things together: this will put the menu_bar window just above the button_bar_window above the display
       
    73     
       
    74     pack .main_window.menu_bar -side top -fill x
       
    75     pack .main_window.button_bar .main_window.display .main_window.edit_window -side top -anchor w
       
    76     focus .main_window
       
    77 }
       
    78 
       
    79 proc init_menu_bar { } {
       
    80 # this command initializes the main menu bar (stuff like file, etc)
       
    81 # shortcuts not working! why?
       
    82     global BACKGROUND FOREGROUND FONT
       
    83 
       
    84     menubutton .main_window.menu_bar.file -text "File " -menu .main_window.menu_bar.file.menu -borderwidth 2 -background $BACKGROUND -foreground $FOREGROUND -font $FONT
       
    85     init_file_menu
       
    86 
       
    87     menubutton  .main_window.menu_bar.tools -text "Tools " -menu .main_window.menu_bar.tools.menu  -borderwidth 2 -background $BACKGROUND -foreground $FOREGROUND -font $FONT
       
    88     init_tools_menu
       
    89 
       
    90     menubutton  .main_window.menu_bar.options -text "Options " -menu .main_window.menu_bar.options.menu  -borderwidth 2 -background $BACKGROUND -foreground $FOREGROUND -font $FONT
       
    91     init_options_menu
       
    92 
       
    93     pack .main_window.menu_bar.file .main_window.menu_bar.tools .main_window.menu_bar.options -side left -padx 1m -pady 1m 
       
    94 
       
    95 # if we ever put in the help junk...
       
    96 #    menubutton  .main_window.menu_bar.help -text "Help" -menu .main_window.menu_bar.options.help  -borderwidth 2 -background $BACKGROUND -foreground $FOREGROUND -font $FONT
       
    97 #    pack .main_window.menu_bar.help -side right
       
    98 }
       
    99 
       
   100 proc init_file_menu { } {
       
   101 # this creates the menu associated with the file menubutton
       
   102     global FOREGROUND BACKGROUND FONT
       
   103     menu .main_window.menu_bar.file.menu -background $BACKGROUND -foreground $FOREGROUND -font $FONT
       
   104 # and these initialize the entries in the menu (open is linked to the command popup_open_menu)
       
   105     .main_window.menu_bar.file.menu add command -label "Open..." -command popup_open_menu 
       
   106     .main_window.menu_bar.file.menu add command -label "Close" -command close_document 
       
   107     .main_window.menu_bar.file.menu add separator
       
   108 #    .main_window.menu_bar.file.menu add command -label "Save TIFF" -command popup_save_tiff_menu 
       
   109     .main_window.menu_bar.file.menu add command -label "Save ASCII" -command popup_save_ascii_menu 
       
   110     .main_window.menu_bar.file.menu add command -label "Save WORD/POS" -command popup_save_word_pos_menu 
       
   111     .main_window.menu_bar.file.menu add command -label "Save Learned Characters" -command popup_save_learned_chars_menu 
       
   112     .main_window.menu_bar.file.menu add command -label "Read Learned Characters" -command popup_read_learned_chars_menu 
       
   113 
       
   114 #    .main_window.menu_bar.file.menu add command -label "Save Setup" -command popup_save_setup_menu 
       
   115 # a separator is just a horizontal line for show
       
   116     .main_window.menu_bar.file.menu add separator
       
   117     .main_window.menu_bar.file.menu add command -label "Quit" -command popup_quit_dialog_box 
       
   118 }
       
   119 
       
   120 proc close_document { } {
       
   121     .main_window.edit_window.text_part delete 1.0 end
       
   122     .main_window.display.work_space delete all
       
   123     set COLORED_WORDS {}
       
   124     DEALLOCATE_PAGE
       
   125 }
       
   126 
       
   127 
       
   128 proc init_tools_menu { } {
       
   129 # this creates the menu associated with the tools menubutton
       
   130     global BACKGROUND FOREGROUND FONT
       
   131     menu .main_window.menu_bar.tools.menu -background $BACKGROUND -foreground $FOREGROUND -font $FONT
       
   132 # and these initialize the entries in the menu (open is linked to the command popup_open_menu)
       
   133 
       
   134 #    supposed to just find the angle and tell you what it is...
       
   135 #    .main_window.menu_bar.tools.menu add command -label "Skew Angle" -command popup_skew_angle_menu 
       
   136 
       
   137 #  supposed to let the user arbitrarily rotate the image
       
   138 #    .main_window.menu_bar.tools.menu add command -label "Rotate" -command popup_rotate_menu
       
   139 
       
   140     .main_window.menu_bar.tools.menu add command -label "Recognize" -command popup_recognize_menu
       
   141     .main_window.menu_bar.tools.menu add separator
       
   142     .main_window.menu_bar.tools.menu add command -label "Zoom in" -command ZOOM_IN
       
   143     .main_window.menu_bar.tools.menu add command -label "Zoom out" -command ZOOM_OUT
       
   144     .main_window.menu_bar.tools.menu add separator
       
   145     .main_window.menu_bar.tools.menu add command -label "Refresh" -command DISPLAY_INTERVALS 
       
   146     .main_window.menu_bar.tools.menu add command -label "Deskew" -command popup_deskew_menu
       
   147 
       
   148 # automated spell correction
       
   149 #    .main_window.menu_bar.tools.menu add command -label "SpellCorrect" -command popup_correct_menu
       
   150 
       
   151 # interactive learning
       
   152 #    .main_window.menu_bar.tools.menu add command -label "Learn Mode" -command popup_learn_mode
       
   153 }
       
   154 
       
   155 set CURRENT_DEFAULT_FONT Helvetica
       
   156 set CURRENT_DEFAULT_SIZE 9
       
   157 proc popup_learn_mode { } {
       
   158     global BACKGROUND FOREGROUND SMALLFONT FONT
       
   159     toplevel .learn 
       
   160     frame .learn.c -width 100 -height 100
       
   161     canvas .learn.c.c -background $BACKGROUND -width 100 -height 100
       
   162     pack .learn.c.c
       
   163     frame .learn.s
       
   164     entry .learn.s.learned_string -width 5 -bg $BACKGROUND -fg $FOREGROUND -font $SMALLFONT
       
   165     label .learn.s.string_message -text "Ascii" -bg $BACKGROUND -fg $FOREGROUND -font $SMALLFONT
       
   166     pack .learn.s.learned_string .learn.s.string_message -side left -expand 1 
       
   167 
       
   168     tk_optionMenu .learn.font CURRENT_DEFAULT_FONT Helvetica Courier Times 
       
   169     .learn.font configure -bg $BACKGROUND -fg $FOREGROUND -font $SMALLFONT
       
   170     .learn.font.menu configure -bg $BACKGROUND -fg $FOREGROUND
       
   171     tk_optionMenu .learn.size CURRENT_DEFAULT_SIZE 9 10 12 18 
       
   172     .learn.size configure -bg $BACKGROUND -fg $FOREGROUND -font $SMALLFONT
       
   173     .learn.size.menu configure -bg $BACKGROUND -fg $FOREGROUND
       
   174     frame .learn.buttons
       
   175     button .learn.buttons.ok -text Learn -command learn_ok -fg $FOREGROUND -background $BACKGROUND -font $SMALLFONT -width 5
       
   176     button .learn.buttons.cancel -text Skip -command learn_skip -fg $FOREGROUND -background $BACKGROUND -font $SMALLFONT -width 5
       
   177     pack .learn.buttons.ok .learn.buttons.cancel -side left -expand 1 -fill x 	    
       
   178     pack .learn.c .learn.s .learn.font .learn.size .learn.buttons -side top -fill x 
       
   179 }
       
   180 
       
   181 
       
   182     
       
   183 proc popup_deskew_menu { } {
       
   184     puts stdout "Calling get_skew"
       
   185     DESKEW 
       
   186 }
       
   187 
       
   188 proc popup_recognize_menu { } {
       
   189     puts stdout "Calling recognize from Tcl"
       
   190     FIND_LINES_AND_RECOGNIZE
       
   191 }
       
   192 
       
   193 set XV 2
       
   194 proc init_options_menu { } {
       
   195     global dummy word_certainty_value screen_view_style FOREGROUND BACKGROUND FONT XV
       
   196     menu .main_window.menu_bar.options.menu -foreground $FOREGROUND -background $BACKGROUND -font $FONT
       
   197     .main_window.menu_bar.options.menu add command -label "Warning Levels..." -command popup_confidence_menu
       
   198 
       
   199 #    .main_window.menu_bar.options.menu add command -label "Zoom Ratio" -command popup_zoom_ratio_menu
       
   200     .main_window.menu_bar.options.menu add checkbutton -label "Display Line Boundaries" -variable DISPLAY_LINE_BOUNDARIES
       
   201     .main_window.menu_bar.options.menu add checkbutton -label "Display Bounding Boxes" -variable DISPLAY_BOUNDING_BOXES
       
   202     .main_window.menu_bar.options.menu add checkbutton -label "Spellcheck" -variable SPELLCHECK
       
   203     .main_window.menu_bar.options.menu add separator
       
   204     .main_window.menu_bar.options.menu add radiobutton -label "No Display" -variable DISPLAY_IMAGE -value 0
       
   205     .main_window.menu_bar.options.menu add radiobutton -label "OCRchie Display" -variable DISPLAY_IMAGE -value 1
       
   206     .main_window.menu_bar.options.menu add radiobutton -label "xv" -variable DISPLAY_IMAGE -value $XV
       
   207     .main_window.menu_bar.options.menu add separator
       
   208     .main_window.menu_bar.options.menu add radiobutton -label "No deskew" -variable DESKEW_METHOD -value -1
       
   209     .main_window.menu_bar.options.menu add radiobutton -label "RLE rotate" -variable DESKEW_METHOD -value 1
       
   210     .main_window.menu_bar.options.menu add radiobutton -label "Bitmap rotate" -variable DESKEW_METHOD -value 0
       
   211 
       
   212 }
       
   213 
       
   214 set GLOBAL_MESSAGE "<none>"
       
   215 proc init_button_bar { } {
       
   216     global FONT BACKGROUND FOREGROUND GLOBAL_MESSAGE
       
   217     message .main_window.button_bar.msg -font $FONT -background $BACKGROUND -foreground $FOREGROUND -width 400
       
   218     pack .main_window.button_bar.msg
       
   219 }
       
   220 
       
   221 proc init_display { } {
       
   222     global display_height canvas_width FOREGROUND BACKGROUND IMAGE_DISPLAY_WIN main_window_width display_height scroll_inc 
       
   223     frame .main_window.display -width $main_window_width -height $display_height -relief ridge -bd 5 -bg $BACKGROUND
       
   224     canvas .main_window.display.work_space -bg white -xscrollcommand ".main_window.display.xscroller set" -yscrollcommand ".main_window.display.yscroller set" -xscrollincrement $scroll_inc -cursor {crosshair black gray} -width $canvas_width -height $display_height
       
   225 # two scrollbars
       
   226     scrollbar .main_window.display.xscroller -command ".main_window.display.work_space xview" -orient horizontal -background $BACKGROUND
       
   227     scrollbar .main_window.display.yscroller -command ".main_window.display.work_space yview" -background $BACKGROUND
       
   228 
       
   229     pack .main_window.display.xscroller -side bottom -fill x
       
   230     pack .main_window.display.work_space .main_window.display.yscroller -side left -fill y
       
   231     set IMAGE_DISPLAY_WIN .main_window.display.work_space
       
   232     .main_window.display.work_space configure -scrollregion { 0 0 5000 5000 }
       
   233 #    initialize_region_grab 
       
   234 }
       
   235 
       
   236 proc init_edit_window { } {
       
   237     global edit_window_height canvas_width EDIT_BACKGROUND COLORED_WORDS LOW_PRECISION_BACKGROUND MISPELLED_BACKGROUND UNKNOWN_CHAR_BACKGROUND SMALLFONT SCALE_FACTOR scroll_inc
       
   238     text .main_window.edit_window.text_part -bg $EDIT_BACKGROUND -height $edit_window_height -width $canvas_width -insertbackground yellow -insertwidth 8 -font $SMALLFONT -fg white -wrap word
       
   239     pack .main_window.edit_window.text_part -side bottom
       
   240     .main_window.edit_window.text_part tag configure LOW_PRECISION -background $LOW_PRECISION_BACKGROUND
       
   241     .main_window.edit_window.text_part tag configure MISPELLED -background $MISPELLED_BACKGROUND
       
   242     .main_window.edit_window.text_part tag configure UNKNOWN_CHAR -background $UNKNOWN_CHAR_BACKGROUND
       
   243 # Tab binding for the window is supposed to advance the cursor to the
       
   244 # next uncertain word and scroll the image display to show the image
       
   245 # of that word
       
   246     bind .main_window.edit_window.text_part <Tab> {
       
   247 	if {[llength $COLORED_WORDS] == 0} {
       
   248 	    puts stdout "No more words"
       
   249 	} else {
       
   250 	    .main_window.edit_window.text_part mark set insert [pop_colored_words]
       
   251 	    set xpos [pop_colored_words]
       
   252 	    set ypos [pop_colored_words]
       
   253 #	    puts "xpos and ypos for this word"
       
   254 	    set ulx [expr $SCALE_FACTOR * ($xpos - 300)]
       
   255 	    set uly [expr $SCALE_FACTOR * ($ypos - 100)]
       
   256 	    set lrx [expr $SCALE_FACTOR * ($xpos + 300)]
       
   257 	    set lry [expr $SCALE_FACTOR * ($ypos + 100)]
       
   258 # I could never get this scrolling to work quite right, maybe
       
   259 # someone will figure it out someday 	    
       
   260 	    .main_window.display.work_space configure -scrollregion [list $ulx $uly $lrx $lry]
       
   261 	    .main_window.display.work_space configure -scrollregion {0 0 5000 5000}
       
   262 #	    .main_window.display.work_space xview moveto [expr (($SCALE_FACTOR * $xpos) / $scroll_inc)]
       
   263 #	    .main_window.display.work_space yview moveto [expr (($SCALE_FACTOR * $ypos) / $scroll_inc)]
       
   264 	    
       
   265 	    set x [.main_window.edit_window.text_part index insert]
       
   266 #	    puts "New index is $x"
       
   267 	    .main_window.edit_window.text_part see insert
       
   268 	    set local_tags [.main_window.edit_window.text_part tag names insert]
       
   269 #	    puts "Tags at this place: $local_tags"
       
   270 	}
       
   271 	break
       
   272     }
       
   273 }
       
   274 
       
   275 proc addword { w {xpos 0} {ypos 0} {status OK}} {
       
   276     global COLORED_WORDS LOW_PRECISION_BACKGROUND MISPELLED_BACKGROUND UNKNOWN_CHAR_BACKGROUND
       
   277 #    puts stdout "Adding $w with status $status"
       
   278 
       
   279     if { ![string compare $status OK] } {
       
   280 	.main_window.edit_window.text_part insert end "$w " 
       
   281 	.main_window.edit_window.text_part mark set insert end
       
   282     } elseif { ![string compare $status LOW_PRECISION] || ![string compare $status MISPELLED] || ![string compare $status UNKNOWN_CHAR] } {
       
   283 	.main_window.edit_window.text_part insert end "$w" $status
       
   284 	.main_window.edit_window.text_part insert end " "
       
   285 #       xpos and ypos can be tags too, but they really slow things down
       
   286 #	.main_window.edit_window.text_part mark set insert end
       
   287 #	.main_window.edit_window.text_part mark set insert "end -3 char"
       
   288 #	.main_window.edit_window.text_part tag add $status "insert wordstart" "insert wordend"
       
   289 #	.main_window.edit_window.text_part tag add x$xpos "insert wordstart" "insert wordend"
       
   290 #	.main_window.edit_window.text_part tag add y$ypos "insert wordstart" "insert wordend"
       
   291 	.main_window.edit_window.text_part mark set insert "end -3 char"
       
   292 	.main_window.edit_window.text_part mark set insert "insert wordstart"
       
   293 	lappend COLORED_WORDS [.main_window.edit_window.text_part index insert]
       
   294 	lappend COLORED_WORDS $xpos
       
   295 	lappend COLORED_WORDS $ypos
       
   296 	.main_window.edit_window.text_part mark set insert end
       
   297     } else {
       
   298 	puts stdout "Unknown word status for $w: $status"
       
   299 	.main_window.edit_window.text_part insert end "$w UNKNOWNSTATUS? "
       
   300     }
       
   301 }
       
   302 
       
   303 proc pop_colored_words { } {
       
   304     global COLORED_WORDS
       
   305     set x [lindex $COLORED_WORDS 0]
       
   306     if {[llength $COLORED_WORDS] == 1} {
       
   307 	set COLORED_WORDS {}
       
   308     } elseif {[llength $COLORED_WORDS] == 0} {
       
   309 	set COLORED_WORDS $COLORED_WORDS
       
   310     } else {
       
   311 	set COLORED_WORDS [lrange $COLORED_WORDS 1 [llength $COLORED_WORDS]]
       
   312     }
       
   313     return $x
       
   314 }
       
   315 
       
   316 
       
   317 set x_init 0
       
   318 set y_init 0
       
   319 set x_final 0
       
   320 set y_final 0
       
   321 
       
   322 set started_region 0
       
   323 set region_count 0
       
   324 proc initialize_region_grab { } {
       
   325 #
       
   326 #  NONE OF THE REGION STUFF IS BEING USED CURRENTLY
       
   327 #
       
   328 # facilitates the grabbing of a rectangle of the window
       
   329 # using mouse button 1
       
   330 # and apparently a lot of other junk!
       
   331 
       
   332     global x_init y_init x_final y_final started_region region_data region_list region_id arrow_in_progress current_arrow
       
   333     
       
   334     bind .main_window.display.work_space <ButtonPress-1> {
       
   335 	if [expr ! $started_region] {
       
   336 	    grab set .main_window.display 
       
   337 	    set x_init [.main_window.display.work_space canvasx %x]
       
   338 	    set y_init [.main_window.display.work_space canvasy %y]
       
   339 
       
   340 	    set region_id [.main_window.display.work_space create rectangle $x_init $y_init $x_init $y_init -outline black -width 3]
       
   341 	    .main_window.display.work_space itemconfigure $region_id -tags region$region_id
       
   342 
       
   343 	    set started_region 1
       
   344 
       
   345 	}
       
   346     }
       
   347     bind .main_window.display.work_space <ButtonRelease-1> {
       
   348 	set x_final [.main_window.display.work_space canvasx %x]
       
   349 	set y_final [.main_window.display.work_space canvasy %y]
       
   350 	
       
   351 	.main_window.display.work_space coords region$region_id $x_init $y_init $x_final $y_final
       
   352 
       
   353 
       
   354 	# if finishing a rectangle, initialize its stuff in the array
       
   355 	if {$x_init <= $x_final} {
       
   356 	    set region_data($region_id,x_init) $x_init
       
   357 	    set region_data($region_id,x_final) $x_final
       
   358 	} else {
       
   359 	    set region_data($region_id,x_final) $x_init
       
   360 	    set region_data($region_id,x_init) $x_final
       
   361 	}
       
   362 	if {$y_init <= $y_final} {
       
   363 	    set region_data($region_id,y_init) $y_init
       
   364 	    set region_data($region_id,y_final) $y_final
       
   365 	} else {
       
   366 	    set region_data($region_id,y_init) $y_final
       
   367 	    set region_data($region_id,y_final) $y_init
       
   368 	}
       
   369 	
       
   370 	set region_data($region_id,next_region_id) 0
       
   371 	lappend region_list $region_id
       
   372 	
       
   373 	make_region_buttons $region_id	    
       
   374 
       
   375 	set started_region 0
       
   376 	grab release .main_window.display
       
   377     }
       
   378     
       
   379     bind .main_window.display.work_space <B2-Motion> {
       
   380 	if $arrow_in_progress {
       
   381 	    set curx [.main_window.display.work_space canvasx %x] 
       
   382 	    set cury [.main_window.display.work_space canvasy %y]
       
   383 	    .main_window.display.work_space coords $current_arrow 0 0 $curx $cury
       
   384 	}
       
   385     }
       
   386     bind .main_window.display.work_space <B1-Motion> {
       
   387 	if $started_region {
       
   388 
       
   389 	    set curx [.main_window.display.work_space canvasx %x] 
       
   390 	    set cury [.main_window.display.work_space canvasy %y]
       
   391 
       
   392 	    .main_window.display.work_space coords region$region_id $x_init $y_init $curx $cury
       
   393 
       
   394 
       
   395 	} 
       
   396     }
       
   397     bind .main_window.display <Leave> {
       
   398 	# on leaving the display, release control of the mouse etc.
       
   399 	# maybe make it scroll instead?
       
   400 	if $started_region {
       
   401 	    grab release .main_window.display
       
   402 	    set started_region 0
       
   403 	    .main_window.display.work_space coords region$region_id 0 0 0 0  
       
   404 	}
       
   405     }
       
   406 }
       
   407 
       
   408 set arrow_in_progress 0
       
   409 proc make_region_buttons { reg_id } {
       
   410     global region_data kill_button_data next_button_data arrow_in_progress current_arrow
       
   411 
       
   412     set x_init $region_data($reg_id,x_init)
       
   413     set y_init $region_data($reg_id,y_init)
       
   414 
       
   415     set next_num [.main_window.display.work_space create rectangle $x_init $y_init [expr $x_init + 20] [expr $y_init + 20] -fill blue -tags "region$reg_id next_button$reg_id"]
       
   416     set next_button_data($next_num,reg_id) $reg_id
       
   417     .main_window.display.work_space bind next_button$reg_id <Double-2> {
       
   418 	set reg_id $next_button_data([.main_window.display.work_space find withtag current],reg_id)
       
   419 	if { $arrow_in_progress } {
       
   420 	    finish_arrow $reg_id
       
   421 	} else {
       
   422 	    set canvas_x [.main_window.display.work_space canvasx %x] 
       
   423 	    set canvas_y [.main_window.display.work_space canvasy %y]
       
   424 	    start_arrow $reg_id $canvas_x $canvas_y
       
   425 	    puts stdout "Starting an arrow at $canvas_x $canvas_y"
       
   426 	}
       
   427     }
       
   428     set kill_num [.main_window.display.work_space create rectangle [expr $x_init + 20] $y_init [expr $x_init + 40] [expr $y_init + 20] -fill red -tags "region$reg_id kill_button$reg_id"]
       
   429 
       
   430     set kill_button_data($kill_num,reg_id) $reg_id
       
   431 
       
   432     .main_window.display.work_space bind kill_button$reg_id <Double-2> {
       
   433 	set reg_id $kill_button_data([.main_window.display.work_space find withtag current],reg_id)
       
   434 	destroy_region $reg_id .main_window.display.work_space
       
   435     }
       
   436 }
       
   437 
       
   438 proc start_arrow { reg_id x_start y_start } {
       
   439     global arrow_in_progress next_button_data region_data current_arrow
       
   440     set path_name .main_window.display.work_space
       
   441 # start an arrow in the middle of the little red button
       
   442     
       
   443     
       
   444 
       
   445     set arrow [.main_window.display.work_space create line $x_start $y_start $x_start $y_start -width 3 -arrow last -arrowshape {6.0m 8.0m 1.5m} -fill blue -tags arrow$reg_id]
       
   446 
       
   447     set region_data($reg_id,arrow) $arrow
       
   448     set arrow_in_progress 1
       
   449     set current_arrow $arrow
       
   450 }
       
   451 
       
   452 proc destroy_region { reg_id path_name } {
       
   453     $path_name delete region$reg_id
       
   454     puts stdout "Destroying $reg_id"
       
   455 }
       
   456 
       
   457 set open_menu_geometry 250x300+400+400
       
   458 set current_directory [pwd]
       
   459 set box_entry $current_directory
       
   460 set open_menu_pattern *.tif
       
   461 
       
   462 proc popup_open_menu { } {
       
   463 #
       
   464 # this procedure pops up an interactive box which can be used to open files
       
   465 # what a horrible mess.  Writing it took forever
       
   466 #
       
   467     global open_menu_geometry open_menu_pattern current_directory FONT FOREGROUND BACKGROUND SMALLFONT box_entry
       
   468 
       
   469     toplevel .open_menu
       
   470     wm geometry .open_menu $open_menu_geometry
       
   471     wm title .open_menu Open
       
   472     .open_menu configure -background $BACKGROUND
       
   473     # force the user to interact with this box
       
   474     # grab set .open_menu 
       
   475 
       
   476     # directory listing and scrollbar
       
   477     frame .open_menu.dirstuff
       
   478 
       
   479     frame .open_menu.cur_dir
       
   480     label .open_menu.cur_dir.l -fg $FOREGROUND -background $BACKGROUND -font $SMALLFONT -text "Dir: "
       
   481     entry .open_menu.cur_dir.e -relief sunken -bd 2 -textvariable box_entry -foreground $FOREGROUND -background $BACKGROUND -font $SMALLFONT
       
   482     .open_menu.cur_dir.e icursor end
       
   483     bind .open_menu.cur_dir.e <Return> {
       
   484 	set file_to_open $box_entry
       
   485 	if [file isdirectory $file_to_open] {
       
   486 	    cd $file_to_open
       
   487 	    set current_directory [pwd]
       
   488 	    clear_directory_box
       
   489 	    puts stdout "Changing to  $current_directory"
       
   490 	    fill_in_directory_box $current_directory $open_menu_pattern
       
   491 	} elseif [file exists $file_to_open] {
       
   492 	    puts stdout "Opening file $file_to_open"
       
   493 	    my_open $file_to_open
       
   494 	    destroy .open_menu
       
   495 	} else {
       
   496 	    puts stdout "Cannot acccess that file"
       
   497 	}
       
   498     }
       
   499     pack .open_menu.cur_dir.l .open_menu.cur_dir.e -side left
       
   500 
       
   501     scrollbar .open_menu.dirstuff.yscroll -command ".open_menu.dirstuff.directory yview" -background $BACKGROUND
       
   502     listbox .open_menu.dirstuff.directory -yscrollcommand ".open_menu.dirstuff.yscroll set" -width 22 -height 11 -relief raised -font $SMALLFONT -background $BACKGROUND -foreground $FOREGROUND
       
   503 
       
   504     fill_in_directory_box $current_directory $open_menu_pattern
       
   505     bind .open_menu.dirstuff.directory <Double-Button-1> {
       
   506 	set file_to_open [selection get]
       
   507 #        puts stdout "Bound button"	
       
   508 	if [file isdirectory $file_to_open] {
       
   509 	    cd $file_to_open
       
   510 	    set current_directory [pwd]
       
   511 	    clear_directory_box
       
   512 #	    puts stdout "Changing to  $current_directory"
       
   513 	    fill_in_directory_box $current_directory $open_menu_pattern
       
   514 	} elseif [file exists $file_to_open] {
       
   515 #	    puts stdout "Opening file $file_to_open"
       
   516 	    my_open $file_to_open
       
   517 	    destroy .open_menu.dirstuff.directory
       
   518 	    destroy .open_menu
       
   519 	} else {
       
   520 	    puts stdout "Cannot access that file"
       
   521 	}
       
   522     }
       
   523     # pattern for listings to match
       
   524 
       
   525 
       
   526     frame .open_menu.pattern_match -background $BACKGROUND 
       
   527     label .open_menu.pattern_match.label -text "Match files of type:" -font $SMALLFONT -background $BACKGROUND -fg $FOREGROUND
       
   528     entry .open_menu.pattern_match.entry -width 5 -relief sunken -bd 2 -textvariable open_menu_pattern -font $SMALLFONT -background $BACKGROUND -fg $FOREGROUND
       
   529     # refresh the directory listing after user presses return
       
   530     bind .open_menu.pattern_match.entry <Return> {
       
   531 	set current_directory [pwd]
       
   532 	clear_directory_box
       
   533 	fill_in_directory_box $current_directory $open_menu_pattern
       
   534     }
       
   535     
       
   536     pack .open_menu.pattern_match.label .open_menu.pattern_match.entry -side left
       
   537     pack .open_menu.dirstuff.directory .open_menu.dirstuff.yscroll -side left -fill y 
       
   538     
       
   539     pack .open_menu.pattern_match .open_menu.cur_dir .open_menu.dirstuff -side top -anchor w
       
   540     focus .open_menu.pattern_match.entry
       
   541 }
       
   542 
       
   543 proc popup_confidence_menu { } {
       
   544 # a little box for the user to change the confidence 
       
   545 # warning levels (words that get highlighted)
       
   546     global BACKGROUND FOREGROUND SMALLFONT FONT
       
   547     toplevel .confidence -background $BACKGROUND
       
   548     wm geometry .confidence 250x225+500+500
       
   549     message .confidence.m -text "Warning thresholds for the output display\n (255 = warn unless perfect)" -background $BACKGROUND -foreground $FOREGROUND -font $SMALLFONT -justify center -width 250
       
   550     scale .confidence.very_low -from 0 -to 255 -variable VERY_LOW_CONFIDENCE -orient horizontal -label "Poor (displayed in red)" -background $BACKGROUND -foreground $FOREGROUND -font $SMALLFONT
       
   551     scale .confidence.low -from 0 -to 255 -variable LOW_CONFIDENCE -orient horizontal -label "Fair (displayed in blue)" -background $BACKGROUND -foreground $FOREGROUND -font $SMALLFONT
       
   552     pack .confidence.m .confidence.very_low .confidence.low -side top -fill x
       
   553 }
       
   554 
       
   555 proc my_open { filename } {
       
   556 # 1 means success
       
   557     global IMAGE_DISPLAY_WIN SCALE_FACTOR DISPLAY_IMAGE XV xvprocess
       
   558 #   puts stdout "Opening $filename"
       
   559     page_open $filename
       
   560 #   puts stdout "Done putting into page structure"
       
   561     if { 1 }  {
       
   562 	set display_height [expr $SCALE_FACTOR * [get_page_height]]
       
   563 	set display_width [expr $SCALE_FACTOR * [get_page_width]]
       
   564 	append geometry [expr int($display_width)] x [expr int($display_height)]
       
   565 #	puts stdout "Displaying Image"
       
   566 	if { $DISPLAY_IMAGE == $XV } {
       
   567 	    set xvprocess [exec xv $filename &]
       
   568 	    puts stdout "xvprocess $xvprocess"
       
   569 	} else {
       
   570 # use the canvas...
       
   571 	DISPLAY_INTERVALS
       
   572 	}
       
   573     } else {
       
   574 	popup_image_failure_win
       
   575     }
       
   576 }
       
   577 
       
   578 set save_entry "recog.txt"
       
   579 proc popup_save_ascii_menu { } {
       
   580 #
       
   581 #  Pops up a little window for saving the ascii recognized text
       
   582 #  Should have a general function for all the saves, but now
       
   583 #  they are just cuts and pastes
       
   584 #
       
   585 #
       
   586     global save_ascii_geometry BACKGROUND FOREGROUND FONT SMALLFONT save_entry OCRCHIE_ROOT write_image
       
   587     
       
   588     set save_entry recog.txt
       
   589     toplevel .save_ascii -background $BACKGROUND
       
   590     wm geometry .save_ascii $save_ascii_geometry
       
   591     wm title .save_ascii "Save ASCII Text"
       
   592     grab set .save_ascii
       
   593     
       
   594 
       
   595     label .save_ascii.image -bitmap @$write_image -foreground $FOREGROUND -background $BACKGROUND
       
   596     frame .save_ascii.s -background $BACKGROUND
       
   597     label .save_ascii.s.txt -text "Save ascii text as:" -foreground $FOREGROUND -background $BACKGROUND -font $SMALLFONT    
       
   598     entry .save_ascii.s.ent -relief sunken -bd 2 -textvariable save_entry -foreground $FOREGROUND -background $BACKGROUND -font $SMALLFONT    
       
   599     pack .save_ascii.s.txt .save_ascii.s.ent -side top
       
   600     frame .save_ascii.buttons
       
   601     button .save_ascii.buttons.ok -text OK -command save_ascii -fg $FOREGROUND -background $BACKGROUND -font $SMALLFONT -width 5
       
   602     button .save_ascii.buttons.cancel -text Cancel -command save_ascii_cancel -fg $FOREGROUND -background $BACKGROUND -font $SMALLFONT -width 5
       
   603     pack .save_ascii.buttons.ok .save_ascii.buttons.cancel -side left -expand 1 -fill x 	
       
   604     pack .save_ascii.image .save_ascii.s .save_ascii.buttons -side top
       
   605 
       
   606     bind .save_ascii.s.ent <Return> {
       
   607 	save_ascii
       
   608     }
       
   609 }
       
   610 
       
   611 proc save_ascii_cancel { } {
       
   612     destroy .save_ascii
       
   613 }
       
   614     
       
   615 proc save_ascii { } {
       
   616     # need to put some error checking in here
       
   617     global save_entry
       
   618     set fileid [open $save_entry w]
       
   619     puts $fileid [.main_window.edit_window.text_part get 1.0 end]
       
   620     close $fileid
       
   621     destroy .save_ascii
       
   622 }
       
   623 
       
   624 proc popup_save_word_pos_menu { } {
       
   625     global save_ascii_geometry BACKGROUND FOREGROUND FONT SMALLFONT save_entry OCRCHIE_ROOT face_image
       
   626 
       
   627     set save_entry recog.wps
       
   628     toplevel .save_word_pos -background $BACKGROUND
       
   629     wm geometry .save_word_pos $save_ascii_geometry
       
   630     wm title .save_word_pos "Save in word/pos format"
       
   631     grab set .save_word_pos
       
   632 
       
   633     label .save_word_pos.image -bitmap @$face_image -foreground $FOREGROUND -background $BACKGROUND
       
   634     frame .save_word_pos.s -background $BACKGROUND
       
   635     label .save_word_pos.s.txt -text "Save word_pos text as:" -foreground $FOREGROUND -background $BACKGROUND -font $SMALLFONT    
       
   636     entry .save_word_pos.s.ent -relief sunken -bd 2 -textvariable save_entry -foreground $FOREGROUND -background $BACKGROUND -font $SMALLFONT    
       
   637     pack .save_word_pos.s.txt .save_word_pos.s.ent -side top
       
   638     frame .save_word_pos.buttons
       
   639     button .save_word_pos.buttons.ok -text OK -command save_word_pos -fg $FOREGROUND -background $BACKGROUND -font $SMALLFONT -width 5
       
   640     button .save_word_pos.buttons.cancel -text Cancel -command save_word_pos_cancel -fg $FOREGROUND -background $BACKGROUND -font $SMALLFONT -width 5
       
   641     pack .save_word_pos.buttons.ok .save_word_pos.buttons.cancel -side left -expand 1 -fill x 	
       
   642     pack .save_word_pos.image .save_word_pos.s .save_word_pos.buttons -side top
       
   643 
       
   644     bind .save_word_pos.s.ent <Return> {
       
   645 	save_word_pos
       
   646     }
       
   647 }
       
   648 
       
   649 proc save_word_pos_cancel { } {
       
   650     destroy .save_word_pos
       
   651 }
       
   652     
       
   653 proc save_word_pos { } {
       
   654     # need to put some error checking in here
       
   655     global save_entry
       
   656     WRITE_WORD_POS $save_entry
       
   657     destroy .save_word_pos
       
   658 }
       
   659 
       
   660 proc popup_save_learned_chars_menu { } {
       
   661     global save_ascii_geometry BACKGROUND FOREGROUND FONT SMALLFONT save_entry face_image
       
   662     set save_entry learn.dat
       
   663     toplevel .save_learned_chars -background $BACKGROUND
       
   664     wm geometry .save_learned_chars $save_ascii_geometry
       
   665     wm title .save_learned_chars "Write Learned Characters"
       
   666     grab set .save_learned_chars
       
   667     
       
   668     label .save_learned_chars.image -bitmap @$face_image -foreground $FOREGROUND -background $BACKGROUND
       
   669     frame .save_learned_chars.s -background $BACKGROUND
       
   670     label .save_learned_chars.s.txt -text "Save learned characters as:" -foreground $FOREGROUND -background $BACKGROUND -font $SMALLFONT    
       
   671     entry .save_learned_chars.s.ent -relief sunken -bd 2 -textvariable save_entry -foreground $FOREGROUND -background $BACKGROUND -font $SMALLFONT    
       
   672     pack .save_learned_chars.s.txt .save_learned_chars.s.ent -side top
       
   673     frame .save_learned_chars.buttons
       
   674     button .save_learned_chars.buttons.ok -text OK -command save_learned_chars -fg $FOREGROUND -background $BACKGROUND -font $SMALLFONT -width 5
       
   675     button .save_learned_chars.buttons.cancel -text Cancel -command save_learned_chars_cancel -fg $FOREGROUND -background $BACKGROUND -font $SMALLFONT -width 5
       
   676     pack .save_learned_chars.buttons.ok .save_learned_chars.buttons.cancel -side left -expand 1 -fill x 	
       
   677     pack .save_learned_chars.image .save_learned_chars.s .save_learned_chars.buttons -side top
       
   678 
       
   679     bind .save_learned_chars.s.ent <Return> {
       
   680 	save_learned_chars
       
   681     }
       
   682 }
       
   683 
       
   684 proc save_learned_chars_cancel { } {
       
   685     destroy .save_learned_chars
       
   686 }
       
   687     
       
   688 proc save_learned_chars { } {
       
   689     # need to put some error checking in here?
       
   690     global save_entry
       
   691     WRITE_LEARNED_CHARS $save_entry
       
   692     destroy .save_learned_chars
       
   693 }
       
   694 
       
   695 proc popup_read_learned_chars_menu { } {
       
   696     global save_ascii_geometry BACKGROUND FOREGROUND FONT SMALLFONT save_entry eye_image
       
   697     set save_entry learn.dat
       
   698     toplevel .read_learned_chars -background $BACKGROUND
       
   699     wm geometry .read_learned_chars $save_ascii_geometry
       
   700     wm title .read_learned_chars "Read Learned Characters"
       
   701     grab set .read_learned_chars
       
   702     
       
   703     label .read_learned_chars.image -bitmap @$eye_image -foreground $FOREGROUND -background $BACKGROUND
       
   704     frame .read_learned_chars.s -background $BACKGROUND
       
   705     label .read_learned_chars.s.txt -text "Read learned characters from:" -foreground $FOREGROUND -background $BACKGROUND -font $SMALLFONT    
       
   706     entry .read_learned_chars.s.ent -relief sunken -bd 2 -textvariable save_entry -foreground $FOREGROUND -background $BACKGROUND -font $SMALLFONT    
       
   707     pack .read_learned_chars.s.txt .read_learned_chars.s.ent -side top
       
   708     frame .read_learned_chars.buttons
       
   709     button .read_learned_chars.buttons.ok -text OK -command read_learned_chars -fg $FOREGROUND -background $BACKGROUND -font $SMALLFONT -width 5
       
   710     button .read_learned_chars.buttons.cancel -text Cancel -command read_learned_chars_cancel -fg $FOREGROUND -background $BACKGROUND -font $SMALLFONT -width 5
       
   711     pack .read_learned_chars.buttons.ok .read_learned_chars.buttons.cancel -side left -expand 1 -fill x 	
       
   712     pack .read_learned_chars.image .read_learned_chars.s .read_learned_chars.buttons -side top
       
   713 
       
   714     bind .read_learned_chars.s.ent <Return> {
       
   715 	read_learned_chars
       
   716     }
       
   717 }
       
   718 
       
   719 proc read_learned_chars_cancel { } {
       
   720     destroy .read_learned_chars
       
   721 }
       
   722     
       
   723 proc read_learned_chars { } {
       
   724     # need to put some error checking in here
       
   725     global save_entry
       
   726     LEARN_DATA $save_entry
       
   727     destroy .read_learned_chars
       
   728 }
       
   729 
       
   730 
       
   731 proc PAGE_OPEN { filename } {
       
   732 # unused
       
   733     return 1
       
   734 }
       
   735 
       
   736 proc clear_directory_box { } {
       
   737     	.open_menu.dirstuff.directory delete 0 end
       
   738 }
       
   739 
       
   740 proc fill_in_directory_box { dirname {pattern *} } {
       
   741 # fills in the directory box with directories or files matching the pattern
       
   742     foreach i [exec ls -aF $dirname] {
       
   743 	if [file isdirectory $i] {
       
   744 	    .open_menu.dirstuff.directory insert end $i
       
   745 	} elseif [string match $pattern $i] {
       
   746 	    .open_menu.dirstuff.directory insert end $i
       
   747 	}
       
   748     }
       
   749 }
       
   750 
       
   751 
       
   752 proc popup_quit_dialog_box { } {
       
   753     global quit_dialog_geometry BACKGROUND FOREGROUND FONT OCRCHIE_ROOT
       
   754 
       
   755     toplevel .quit_dialog
       
   756     wm geometry .quit_dialog $quit_dialog_geometry
       
   757     wm title .quit_dialog Quit
       
   758     grab set .quit_dialog
       
   759 
       
   760     append caution_image_name $OCRCHIE_ROOT caution.xbm
       
   761     label .quit_dialog.image -bitmap @$caution_image_name -foreground $FOREGROUND -background $BACKGROUND
       
   762    message .quit_dialog.msg -text "You are about to quit OCRchie.  All changes you have made will be lost." -font $FONT -background $BACKGROUND -fg $FOREGROUND -width 275 -justify center
       
   763     frame .quit_dialog.buttons
       
   764     button .quit_dialog.buttons.ok -text OK -command quit_ok -fg $FOREGROUND -background $BACKGROUND -font $FONT -width 5
       
   765     button .quit_dialog.buttons.cancel -text Cancel -command quit_cancel -fg $FOREGROUND -background $BACKGROUND -font $FONT -width 5
       
   766     pack .quit_dialog.buttons.ok .quit_dialog.buttons.cancel -side left -expand 1 -fill x 	
       
   767     pack .quit_dialog.image .quit_dialog.msg .quit_dialog.buttons -side top -fill x
       
   768     
       
   769 
       
   770 }
       
   771 
       
   772 proc quit_ok { } {
       
   773 #    destroy .t
       
   774 #    destroy .histogram
       
   775     destroy .main_window
       
   776     destroy .quit_dialog
       
   777     QUIT
       
   778 }
       
   779 
       
   780 proc quit_cancel { } {
       
   781     global command_not_in_progress
       
   782     set command_not_in_progress 1
       
   783     destroy .quit_dialog
       
   784 }
       
   785 
       
   786 proc clear_canvas { } {
       
   787     destroy .main_window.display.work_space
       
   788     destroy .main_window.display.xscroller
       
   789     destroy .main_window.display.yscroller
       
   790     destroy .main_window.display
       
   791     init_display
       
   792 }
       
   793 
       
   794 proc spellcheck { word } {
       
   795 # spellchecks a word
       
   796 # could change to use spell or some faster program
       
   797     global x
       
   798     set x [exec echo $word | ispell -a]
       
   799     if { ([string last * $x] == -1) && ([string last + $x] == -1) } {
       
   800 	return MISPELLED
       
   801     } else {
       
   802 	return SPELLED_CORRECTLY
       
   803     }
       
   804 }
       
   805 
       
   806     
       
   807 init_user_interface