Index: trunk/MultiChannelUSB/UserInterface.tcl
===================================================================
--- trunk/MultiChannelUSB/UserInterface.tcl	(revision 168)
+++ trunk/MultiChannelUSB/UserInterface.tcl	(revision 169)
@@ -8,4 +8,15 @@
 
 wm minsize . 990 680
+
+image create bitmap leftarrow -data "
+#define leftarrow_width 5\n
+#define leftarrow_height 5\n
+static unsigned char leftarrow_bits\[\] = {\n
+0x10, 0x1C, 0x1F, 0x1C, 0x10};"
+image create bitmap rightarrow -data "
+#define rightarrow_width 5\n
+#define rightarrow_height 5\n
+static unsigned char rightarrow_bits\[\] = {\n
+0x01, 0x07, 0x1F, 0x07, 0x01};"
 
 namespace eval ::mca {
@@ -114,5 +125,5 @@
             unset handle
         }
-if {1} {
+if {0} {
         while {[catch {usb::connect 0x09FB 0x6001 1 1 0} result]} {
             set answer [tk_messageBox -icon error -type retrycancel \
@@ -429,4 +440,12 @@
         ${config}.thrs_field set 25
 
+        my set xmin_val 0
+        my set xmax_val 4095
+
+        trace add variable [myvar xmin_val] write [myproc xmin_val_update]
+        trace add variable [myvar xmax_val] write [myproc xmax_val_update]
+        
+        my stat_update
+
         set cntr_tmp 1200000000
         my set cntr_val $cntr_tmp
@@ -444,4 +463,7 @@
         ${config}.chan_frame.axisx_value configure -text 0.0
 
+        ${config}.stat_frame.tot_value configure -text 0.0
+        ${config}.stat_frame.bkg_value configure -text 0.0
+
 #        my cntr_reset
     }
@@ -461,9 +483,16 @@
         $graph legend configure -hide yes
 
+        $graph marker create line -name xmin -coords "0 -Inf 0 Inf"  -linewidth 2 -outline red
+        $graph marker create line -name xmax -coords "4095 -Inf 4095 Inf"  -linewidth 2 -outline red
+        $graph marker bind xmin <Enter> [list [self] marker_enter xmin]
+        $graph marker bind xmin <Leave> [list [self] marker_leave xmin]
+        $graph marker bind xmax <Enter> [list [self] marker_enter xmax]
+        $graph marker bind xmax <Leave> [list [self] marker_leave xmax]
+
         set config [frame ${master}.config -width 170]
 
         checkbutton ${config}.axis_check -text {log scale} -variable [myvar axis]
 
-        frame ${config}.spc1 -width 170 -height 30
+        frame ${config}.spc1 -width 170 -height 20
 
         frame ${config}.rate_frame -borderwidth 0 -width 170
@@ -471,13 +500,33 @@
         legendLabel ${config}.rate_frame 1 mean {Avg. rate, 1/s}
 
-        frame ${config}.spc2 -width 170 -height 30
+        frame ${config}.spc2 -width 170 -height 10
 
         frame ${config}.chan_frame -borderwidth 0 -width 170
         legendLabel ${config}.chan_frame 0 entr  {Total entries}
-        legendLabel ${config}.chan_frame 1 empty {}
+        frame ${config}.chan_frame.spc1 -height 10
+        grid ${config}.chan_frame.spc1 -row 1
         legendLabel ${config}.chan_frame 2 axisy {Bin entries}
         legendLabel ${config}.chan_frame 3 axisx {Bin number}
-
-        frame ${config}.spc3 -width 170 -height 30
+                
+        frame ${config}.spc3 -width 170 -height 10
+
+        label ${config}.roi -text {Region of interest}
+        frame ${config}.roi_frame -borderwidth 0 -width 170
+        label ${config}.roi_frame.min_title -anchor w -text {start:}
+        label ${config}.roi_frame.min_value -width 4 -anchor e -text {}
+        label ${config}.roi_frame.spc1 -width 5 -anchor w -text {}
+        label ${config}.roi_frame.max_title -anchor w -text {end:}
+        label ${config}.roi_frame.max_value -width 4 -anchor e -text {}
+
+        grid ${config}.roi_frame.min_title ${config}.roi_frame.min_value \
+            ${config}.roi_frame.spc1 ${config}.roi_frame.max_title \
+            ${config}.roi_frame.max_value
+
+        frame ${config}.stat_frame -borderwidth 0 -width 17
+
+        legendLabel ${config}.stat_frame 0 tot {total entries}
+        legendLabel ${config}.stat_frame 1 bkg {bkg entries}
+
+        frame ${config}.spc4 -width 170 -height 20
 
         checkbutton ${config}.thrs_check -text {amplitude threshold} -variable [myvar thrs]
@@ -486,5 +535,5 @@
             -validate all -vcmd {::mca::validate 4095 4 %P}
 
-        frame ${config}.spc4 -width 170 -height 30
+        frame ${config}.spc5 -width 170 -height 20
 
         label ${config}.cntr -text {time of exposure}
@@ -504,5 +553,5 @@
             ${config}.cntr_frame.m_field ${config}.cntr_frame.m ${config}.cntr_frame.s_field ${config}.cntr_frame.s
 
-        frame ${config}.spc5 -width 170 -height 10
+        frame ${config}.spc6 -width 170 -height 20
 
         button ${config}.start -text Start \
@@ -511,10 +560,10 @@
             -bg red -activebackground red -command [myproc cntr_reset]
 
-        frame ${config}.spc6 -width 170 -height 30
+        frame ${config}.spc7 -width 170 -height 20
 
         button ${config}.register -text Register \
             -bg lightblue -activebackground lightblue -command [myproc register]
 
-        frame ${config}.spc7 -width 170 -height 30
+        frame ${config}.spc8 -width 170 -height 20
 
         button ${config}.recover -text {Read file} \
@@ -527,15 +576,19 @@
         grid ${config}.chan_frame -sticky ew -padx 5
         grid ${config}.spc3
+        grid ${config}.roi -sticky w -pady 1 -padx 5
+        grid ${config}.roi_frame -sticky ew -padx 5
+        grid ${config}.stat_frame -sticky ew -padx 5
+        grid ${config}.spc4
         grid ${config}.thrs_check -sticky w
         grid ${config}.thrs_field -sticky ew -pady 1 -padx 5
-        grid ${config}.spc4
+        grid ${config}.spc5
         grid ${config}.cntr -sticky w -pady 1 -padx 3
         grid ${config}.cntr_frame -sticky ew -padx 5
-        grid ${config}.spc5
+        grid ${config}.spc6
         grid ${config}.start -sticky ew -pady 3 -padx 5
         grid ${config}.reset -sticky ew -pady 3 -padx 5
-        grid ${config}.spc6
+        grid ${config}.spc7
         grid ${config}.register -sticky ew -pady 3 -padx 5
-        grid ${config}.spc7
+        grid ${config}.spc8
         grid ${config}.recover -sticky ew -pady 3 -padx 5
 
@@ -549,4 +602,5 @@
         grid columnconfigure ${config}.rate_frame 1 -weight 1
         grid columnconfigure ${config}.chan_frame 1 -weight 1
+        grid columnconfigure ${config}.stat_frame 1 -weight 1
 
         my crosshairs $graph
@@ -556,4 +610,64 @@
         # create one element with data for the x and y axis, no dots
         $graph element create Spectrum1 -color blue -linewidth 2 -symbol none -smooth step -xdata [myvar xvec] -ydata [myvar yvec]
+    }
+
+# -------------------------------------------------------------------------
+
+    HstDisplay instproc marker_enter {marker} {
+        my instvar config graph
+        $graph configure -cursor hand2
+        $graph crosshairs off
+        blt::RemoveBindTag $graph zoom-$graph
+        $graph marker bind $marker <ButtonPress-1> [list [self] marker_press $marker]
+        $graph marker bind $marker <ButtonRelease-1> [list [self] marker_release $marker]
+    }
+
+# -------------------------------------------------------------------------
+
+    HstDisplay instproc marker_leave {marker} {
+        my instvar config graph
+        $graph configure -cursor crosshair
+        $graph crosshairs on
+        blt::AddBindTag $graph zoom-$graph
+        $graph marker bind $marker <ButtonPress-1> {}
+        $graph marker bind $marker <ButtonRelease-1> {}
+    }
+
+# -------------------------------------------------------------------------
+
+    HstDisplay instproc marker_press {marker} {
+        my instvar config graph
+        $graph marker bind $marker <Motion> [list [self] ${marker}_motion %W %x %y]
+    }
+
+# -------------------------------------------------------------------------
+
+    HstDisplay instproc marker_release {marker} {
+        my instvar config graph
+        $graph marker bind $marker <Motion> {}
+    }
+
+# -------------------------------------------------------------------------
+
+    HstDisplay instproc xmin_motion {W x y} {
+        my instvar config graph xmin_val
+        set index [$graph axis invtransform x $x]
+        set index [::tcl::mathfunc::round $index]
+        if {$index < 0} {
+            set index 0
+        }
+        set xmin_val $index
+    }
+
+# -------------------------------------------------------------------------
+
+    HstDisplay instproc xmax_motion {W x y} {
+        my instvar config graph xmax_val
+        set index [$graph axis invtransform x $x]
+        set index [::tcl::mathfunc::round $index]
+        if {$index > 4095} {
+            set index 4095
+        }
+        set xmax_val $index
     }
 
@@ -572,4 +686,5 @@
         }
     }
+
 # -------------------------------------------------------------------------
 
@@ -622,4 +737,48 @@
 # -------------------------------------------------------------------------
 
+    HstDisplay instproc stat_update {} {
+        my instvar config graph xmin_val xmax_val
+        set ymin_val [[myvar yvec] index $xmin_val]
+        set ymax_val [[myvar yvec] index $xmax_val]
+
+        ${config}.roi_frame.min_value configure -text $xmin_val
+        ${config}.roi_frame.max_value configure -text $xmax_val
+        
+        ${config}.stat_frame.tot_value configure \
+            -text [usb::integrateBlt [myvar yvec] $xmin_val $xmax_val 0]
+
+        ${config}.stat_frame.bkg_value configure \
+            -text [expr {($xmax_val - $xmin_val + 1) * ($ymin_val + $ymax_val) / 2.0}]
+   }
+# -------------------------------------------------------------------------
+
+    HstDisplay instproc xmin_val_update args {
+        my instvar config graph xmin_val xmax_val
+        if {$xmin_val > 4075} {
+            set xmin_val 4075
+        }
+        if {$xmin_val > $xmax_val - 20} {
+            set xmax_val [expr {$xmin_val + 20}]
+        }
+        $graph marker configure xmin -coords "$xmin_val -Inf $xmin_val Inf"
+        my stat_update
+    }
+
+# -------------------------------------------------------------------------
+
+    HstDisplay instproc xmax_val_update args {
+        my instvar config graph xmin_val xmax_val
+        if {$xmax_val < 20} {
+            set xmax_val 20
+        }
+        if {$xmax_val < $xmin_val + 20} {
+            set xmin_val [expr {$xmax_val - 20}]
+        }
+        $graph marker configure xmax -coords "$xmax_val -Inf $xmax_val Inf"
+        my stat_update
+    }
+
+# -------------------------------------------------------------------------
+
     HstDisplay instproc rate_val_update {name key op} {
         my instvar config rate_val
@@ -737,5 +896,5 @@
             set cntr_bak $cntr_tmp
             set cntr_old $cntr_tmp
-            set yvec_bak [usb::integrateBlt [myvar yvec] 0]
+            set yvec_bak [usb::integrateBlt [myvar yvec] 0 4095 0]
             set yvec_old $yvec_bak
 
@@ -824,5 +983,5 @@
 
         $controller usbCmdReadRaw $command [expr {$size * 4}] [myvar data]
-        set yvec_new [usb::integrateBlt [myvar yvec] 0]
+        set yvec_new [usb::integrateBlt [myvar yvec] 0 4095 0]
 
         set prefix [format {%x} [expr {5 + ${number}}]]
@@ -836,4 +995,5 @@
             set rate_val(mean) [expr {($yvec_new - $yvec_bak)*20000000/($cntr_bak - $cntr_new)}]
             ${config}.chan_frame.entr_value configure -text $yvec_new
+            my stat_update
 
             set yvec_old $yvec_new
@@ -913,5 +1073,8 @@
 
     HstDisplay instproc recover {} {
+        my instvar config
         my open_data
+        ${config}.chan_frame.entr_value configure -text [usb::integrateBlt [myvar yvec] 0 4095 0]
+        my stat_update
     }
 
@@ -1299,7 +1462,7 @@
 
         ${config}.chan_frame.mean_value configure \
-            -text [format {%.2e} [usb::integrateBlt [myvar yvec] 1]]
+            -text [format {%.2e} [usb::integrateBlt [myvar yvec] 0 9999 1]]
         ${config}.chan_frame.entr_value configure \
-            -text [usb::integrateBlt [myvar yvec] 0]
+            -text [usb::integrateBlt [myvar yvec] 0 9999 0]
 
     }
