
set annotate .annotate_box
set bpress1 0
set bpress2 0
set bpress3 0
set bpress4 0
set overlay_annotate 0

proc annotate_box { } {

    global annotate

    if [winfo exists $annotate] {destroy $annotate}

    global env
    set env(DISPLAY) :0
    toplevel $annotate -width 435 -height 375
    wm title $annotate "Annotate"

    label $annotate.lab38 \
        -foreground #0000ca -height 0 -relief groove -text Annotate \
        -width 408

    text $annotate.tex36 \
        -background white -height 240 -width 388 -wrap word \
        -yscroll "$annotate.scr44 set" -font "HALVETICA 28"

    scrollbar $annotate.scr44 \
        -width 21 -command {$annotate.tex36 yview}

    button $annotate.page1 \
        -height 0 -text 1 -command { if {$bpress1 == 1} {display_page 1 } }
    button $annotate.page2 \
        -height 0 -text 2 -command { if {$bpress2 == 1} {display_page 2 } }
    button $annotate.page3 \
        -height 0 -text 3 -command { if {$bpress3 == 1} {display_page 3 } }
    button $annotate.page4 \
        -height 0 -text 4 -command { if {$bpress4 == 1} {display_page 4 } }

    place $annotate.page1 \
        -x 20 -y 295
    place $annotate.page2 \
        -x 60 -y 295
    place $annotate.page3 \
        -x 100 -y 295
    place $annotate.page4 \
        -x 140 -y 295


    bind $annotate.page1 <ButtonPress-1> {
    	global page_selected bpress1 bpress2 bpress3 bpress4
    	
    	if {$bpress1 == 1} {
    		set bpress1 0
    		$annotate.page1 configure -relief raise
    	} else {
    		set bpress1 1	
    		$annotate.page1 configure -relief sunken
    	}
    	
    	$annotate.page2 configure -relief raise
    	set bpress2 0
    	$annotate.page3 configure -relief raise
    	set bpress3 0
    	$annotate.page4 configure -relief raise
    	set bpress4 0
    	
    	set page_selected 1
    }


    bind $annotate.page2 <ButtonPress-1> {
    	global page_selected bpress1 bpress2 bpress3 bpress4
    	    	    	
    	if {$bpress2 == 1} {
    		set bpress2 0
    		$annotate.page2 configure -relief raise
    	} else {
    		set bpress2 1	
    		$annotate.page2 configure -relief sunken
    	}
    	
    	$annotate.page1 configure -relief raise
    	set bpress1 0
    	$annotate.page3 configure -relief raise
    	set bpress3 0
    	$annotate.page4 configure -relief raise
    	set bpress4 0
    		
    	set page_selected 2
    }

    bind $annotate.page3 <ButtonPress-1> {
    	global page_selected bpress1 bpress2 bpress3 bpress4
    	
    	if {$bpress3 == 1} {
    		set bpress3 0
    		$annotate.page3 configure -relief raise
    	} else {
    		set bpress3 1	
    		$annotate.page3 configure -relief sunken
    	}
    	    	
    	$annotate.page1 configure -relief raise
    	set bpress1 0
    	$annotate.page2 configure -relief raise
    	set bpress2 0
    	$annotate.page4 configure -relief raise         	
    	set bpress4 0
    	
    	set page_selected 3
    	
    }

    bind $annotate.page4 <ButtonPress-1> {
	global page_selected  bpress1 bpress2 bpress3 bpress
    	
    	if {$bpress4 == 1} {
    		set bpress4 0
    		$annotate.page4 configure -relief raise
    	} else {
    		set bpress4 1	
    		$annotate.page4 configure -relief sunken
    	}
    	
    	$annotate.page1 configure -relief raise
    	set bpress1 0
    	$annotate.page2 configure -relief raise
    	set bpress2 0
    	$annotate.page3 configure -relief raise
    	set bpress3 0
    	
    	set page_selected 4
    }


    # Clear the textbox only only
    button $annotate.clr_box \
         -text "Clear Box" -command {	$annotate.tex36 delete 1.0 [$annotate.tex36 index end] }
                				
    # Delete clear both the textbox and the overlay screen
    button $annotate.clr_overlay \
        -text "Clear Overlay" -command  { clear_overlay }
        			
    # Close the textbox
    button $annotate.close \
        -text Close -command {destroy $annotate}

    # Button to Display the text in overlay
    # Function "display_annotate $screen" is also called in file "annotate_box2.tcl"
    button $annotate.display -text "Display" -command {display_annotate 1}

    # Save the text in the pages selected
    button $annotate.save -text "Save" -command saving_to_page


    ###################
    # SETTING GEOMETRY
    ###################
    place $annotate.tex36 \
        -x 15 -y 40 -width 388 -height 240
    place $annotate.lab38 \
        -x 15 -y 10 -width 408 -height 20
    place $annotate.clr_box -x 220 -y 295
    place $annotate.clr_overlay -x 310 -y 295
    place $annotate.display -x 220 -y 335
    place $annotate.save -x 295 -y 335
    place $annotate.close -x 355 -y 335

    place $annotate.scr44 -x 400 -y 41 -width 21 -height 237



}


proc display_page { page } {
	global annotate
	
	$annotate.tex36 delete 1.0 [$annotate.tex36 index end]
	$annotate.tex36 insert 1.0 [get_page $page]

	
}

proc saving_to_page { } {
       global annotate bpress1 bpress2 bpress3 bpress4
       set count [$annotate.tex36 index end]
       set msg [$annotate.tex36 get 1.0  $count ]

       if {$bpress1 == 1} {
       	
       	save_page 1 $msg
       	return
       }

       if {$bpress2 == 1} {
       	save_page 2 $msg
       	return
       }

       if {$bpress3 == 1} {
       	save_page 3 $msg
       	return
       }

       if {$bpress4 == 1} {
       	save_page 4 $msg
       	return
       }

}

proc display_annotate { screen } {
       global annotate overlay_annotate	
       global overlay_text_message

       #----------------------------------------------------#
       # Clear the overlay from the old message before displaying a new message
       #----------------------------------------------------#

       clear_overlay

       #----------------------------------------------------#
       # Check which screen the annotate message is coming from
       #----------------------------------------------------#
       if {$screen == 1} {set ann_win .annotate_box} else { set ann_win .annotate_box2}

       set count [$ann_win.tex36 index end]

       	
       set msg [$ann_win.tex36 get 0.0  end ]
       	
       	     #----------------------------------------------------#
       	     # Display each message line from the annotate box to the overlay
       	     #----------------------------------------------------#
	set overlay_text_message $msg
       	
}


proc clear_overlay { } {

       global overlay_annotate
       global overlay_text_message

       set overlay_text_message ""


}