#!/bin/sh
# \
  exec oagwish "$0" "$@"

set auto_path [linsert $auto_path 0  /usr/local/oag/apps/lib/$env(HOST_ARCH)]
set auto_path [linsert $auto_path 0 /usr/local/oag/lib_patch/$env(HOST_ARCH)]

set CVSRevisionAuthor "\$Revision: 1.7 $ \$Author: shang $"

set sourceDir /home/oxygen/SHANG/oag/apps/src/tcledit
source $sourceDir/collapse_frame.tcl
source $sourceDir/instinfo.tcl
source $sourceDir/global.tcl
source $sourceDir/file_utils.tcl
source $sourceDir/file_menu.tcl
source $sourceDir/edit_menu.tcl
source $sourceDir/tools_menu.tcl
source $sourceDir/option_menu.tcl
source $sourceDir/help_menu.tcl
source $sourceDir/mark.tcl

package require  CollapsableFrame

proc init {} {
    global env USR_CNF_LANG USR_CNF_T_WIDTH USR_CNF_T_HEIGHT USR_CNF_T_TAB USR_CNF_T_FONT 
    global USR_CNF_T_FG USR_CNF_T_BG USR_CNF_T_CUR_COLOR USR_CNF_HIGHLIGHT USR_CNF_EXT
    global USR_CNF_LPR  USR_CNF_KING_EJ USR_CNF_KING_JE USR_CNF_KING_DICT
    
    if [file exist $env(HOME)/.atedit/conf.txt] {
	set fd [open $env(HOME)/.atedit/conf.txt]
	while {[gets $fd line] >= 0} {
	    if {[llength $line] == 2} {
		set w1 [lindex $line 0]
		set w2 [lindex $line 1]
		switch $w1 {
		    "LANG" {
			set USR_CNF_LANG $w2
		    }
		    "T_WIDTH" {
			if {[string is digit $w2] && $w2 > 0} {
			    set USR_CNF_T_WIDTH $w2
			}
		    }
		    "T_HEIGHT" {
			if {[string is digit $w2] && $w2 > 0} {
			    set USR_CNF_T_HEIGHT $w2
			}
		    }
		    "T_TAB" {
			if {[string is digit $w2] && $w2 > 0} {
			    set USR_CNF_T_TAB $w2
			}
		    }
		    "T_FONT" {
			set USR_CNF_T_FONT $w2
		    }
		    "T_FG" {
			set USR_CNF_T_FG $w2
		    }
		    "T_BG" {
			set USR_CNF_T_BG $w2
		    }
		    "T_CUR_COLOR" {
			set USR_CNF_T_CUR_COLOR $w2
		    }
		    "HIGHLIGHT" {
			set USR_CNF_HIGHLIGHT $w2
		    }
		    "EXT" {
			set USR_CNF_EXT $w2
		    }
		    "LPR" {
			set USR_CNF_LPR $w2
		    }
		    "KING_EJ" {
			set USR_CNF_KING_EJ $w2
		    }
		    "KING_JE" {
			set USR_CNF_KING_JE $w2
		    }
		    "KING_DICT" {
			set USR_CNF_KING_DICT $w2
		    }
		}
	    }
	}
    } else {
	set CNF_T_WIDTH 80
	set CNF_T_HEIGHT 25
	set CNF_T_TAB 4
	set USR_CNF_T_FON "fixed 13 normal"
	set USR_CNF_T_FG black
	set USR_CNF_T_BG white
	set USR_CNF_T_CUR_COLOR brown
	set USR_CNF_HIGHLIGHT "#ff00ff"
	set USR_CNF_EXT {{"All" {*}} {"Text" {*.txt}}}
	set USR_CNF_LPR {lpr %s}
	set USR_CNF_LANG  en
    }
}

proc make_menu {args} {
    menu .menu
    .menu add cascade -label File(F) -underline 1 -menu .menu.file
    .menu add cascade -label Edit(E) -underline 1 -menu .menu.edit
    .menu add cascade -label Insert(I) -underline 1 -menu .menu.tools
    .menu add cascade -label Option(O) -underline 1 -menu .menu.option
    .menu add cascade -label Help(H) -underline 1 -menu .menu.help
     #make file menu
    menu .menu.file -tearoff no
    .menu.file add command -label "New(N) Ctrl-N" -underline 4 -command {cb_filemenu_new {}}
    .menu.file add command -label "Open(O) Ctrl-O" -underline 5 -command cb_filemenu_open
    .menu.file add command -label "Close and Open"  -command {cb_filemenu_close_and_open {}}
    .menu.file add command -label "Open at curcor(I)" -underline 15 -command cb_filemenu_open_at_cursor
    .menu.file add separator
    .menu.file add command -label "Save(S) Ctri-S" -underline 5 -command cb_filemenu_save
    .menu.file add command -label "Save As...(A)" -underline 11 -command cb_filemenu_save_as
    .menu.file add separator
    .menu.file add command -label "Print(P)" -underline 6 -command cb_filemenu_print
    .menu.file add separator
    .menu.file add command -label "Quit(Q)" -underline 1 -command cb_filemenu_quit
    .menu.file add separator
    .menu.file add command -label "Split(T) Ctrl-T" -underline 6 -command split_textarea
    .menu.file add command -label "Merge(M) Ctrl-M" -underline 1 -command merge_textarea
    
    .menu.file configure -bg "light sky blue" -fg black -font bold -tearoff no
    
    #make edit menu
    menu .menu.edit -tearoff no
    
    .menu.edit add command -label "Undo(U) Ctrl-Z" -underline 5 -command cb_editmenu_undo
    .menu.edit add separator
    .menu.edit add command -label "Cut(X) Ctrl-X" -underline 5 -command cb_editmenu_cut
    .menu.edit add command -label "Cope(C) Ctrl-C" -underline 5 -command cb_editmenu_copy
    .menu.edit add command -label "Paste(P) Ctrl-V" -underline 6 -command cb_editmenu_paste
    .menu.edit add separator
    .menu.edit add command -label "Find(F) Ctrl-F" -underline 5 -command cb_editmenu_find
    .menu.edit add command -label "Replace(R) Ctrl-R" -underline 7 -command cb_editmenu_replace
    .menu.edit add command -label "Replace(R) Ctrl-R" -underline 7 -command cb_editmenu_replace
    .menu.edit add separator
    .menu.edit add command -label "Select All(A) Ctrl-A" -underline 8 -command cb_editmenu_select_all
    .menu.edit add separator
    .menu.edit add command -label "Goto line(G) Ctrl-G" -underline 1 -command cb_editmenu_goto_line
    .menu.edit configure -bg "light sky blue" -fg black -font bold -tearoff no

    #make tools menu
    menu .menu.tools -tearoff no
    
    .menu.tools add command -label "Date/Time(D) Ctrl-D" -underline 10 -command cb_tools_date
    .menu.tools add command -label "Picture(P) Ctrl-P" -underline 8 -command cb_tools_picture
    .menu.tools  configure -bg "light sky blue" -fg black -font bold -tearoff no

    #make option menu
    menu .menu.option -tearoff no
    .menu.option add command -label "Option(O)" -underline 7 -command cb_optionmenu_config
    .menu.option  configure -bg "light sky blue" -fg black -font bold -tearoff no
    
    menu .menu.help -tearoff no
    .menu.help add command -label Abort(A) -underline 1 \
	-command cb_helpmenu_about
    . configure -menu .menu
}

set BINDIR /home/oxygen/SHANG/tcl-editors/atedit
proc make_toolbar {args} {
    global CST_SBAR_FONT BINDIR
    frame .toolbar
    ButtonBox  .toolbar.filebar   -spacing 0 -padx 1 -pady 1
    Separator  .toolbar.s1        -orient vertical
    ButtonBox  .toolbar.editbar   -spacing 0 -padx 1 -pady 1
    Separator  .toolbar.s2        -orient vertical
    ButtonBox  .toolbar.printbar  -spacing 0 -padx 1 -pady 1
    Separator  .toolbar.s3        -orient vertical
    ButtonBox  .toolbar.findbar   -spacing 0 -padx 1 -pady 1
    Separator  .toolbar.s4        -orient vertical
    ButtonBox  .toolbar.markbar   -spacing 0 -padx 1 -pady 1
    Separator  .toolbar.s5        -orient vertical
    ButtonBox  .toolbar.closebar   -spacing 0 -padx 1 -pady 1
    Separator  .toolbar.s6        -orient vertical
   
    .toolbar.filebar add -image [Bitmap::get new]                  \
                 -highlightthickness 0 -takefocus 0 -relief link   \
                 -borderwidth 1 -padx 1 -pady 1 -helptext "open new tab"   \
                 -command {cb_filemenu_new {}}
    .toolbar.filebar add -image [Bitmap::get open]                 \
                 -highlightthickness 0 -takefocus 0 -relief link   \
                 -borderwidth 1 -padx 1 -pady 1 -helptext "open file"   \
                 -command cb_filemenu_open
    .toolbar.filebar add -image [Bitmap::get save]                 \
                 -highlightthickness 0 -takefocus 0 -relief link   \
                 -borderwidth 1 -padx 1 -pady 1 -helptext "save file"   \
                 -command cb_filemenu_save
   
    .toolbar.editbar add -image [Bitmap::get undo]                 \
	-highlightthickness 0 -takefocus 0 -relief link   \
	-borderwidth 1 -padx 1 -pady 1 -helptext "undo"   \
	-command cb_editmenu_undo
    
    .toolbar.editbar add -image [Bitmap::get cut]                  \
                 -highlightthickness 0 -takefocus 0 -relief link   \
                 -borderwidth 1 -padx 1 -pady 1 -helptext "cut"   \
                 -command cb_editmenu_cut
    .toolbar.editbar add -image [Bitmap::get copy]                 \
                 -highlightthickness 0 -takefocus 0 -relief link   \
                 -borderwidth 1 -padx 1 -pady 1 -helptext "copy"   \
                 -command cb_editmenu_copy
    .toolbar.editbar add -image [Bitmap::get paste]                \
                 -highlightthickness 0 -takefocus 0 -relief link   \
                 -borderwidth 1 -padx 1 -pady 1 -helptext "paste"   \
	-command cb_editmenu_paste
    .toolbar.printbar add -image [Bitmap::get print]               \
                 -highlightthickness 0 -takefocus 0 -relief link   \
                 -borderwidth 1 -padx 1 -pady 1 -helptext "print"   \
                 -command cb_filemenu_print
    .toolbar.findbar add -image [xpm-to-image $BINDIR/icon/find.xpm] \
                 -highlightthickness 0 -takefocus 0 -relief link   \
                 -borderwidth 1 -padx 1 -pady 1 -helptext "find"   \
                 -command cb_editmenu_find
    .toolbar.findbar add -image [xpm-to-image $BINDIR/icon/findf.xpm] \
                 -highlightthickness 0 -takefocus 0 -relief link   \
                 -borderwidth 1 -padx 1 -pady 1 -helptext "find forwards"   \
                 -command {event generate .base.textarea <<FindForwards>>}
    .toolbar.findbar add -image [xpm-to-image $BINDIR/icon/findb.xpm] \
                 -highlightthickness 0 -takefocus 0 -relief link   \
                 -borderwidth 1 -padx 1 -pady 1 -helptext "find backwards"   \
                 -command {event generate .base.textarea <<FindBackwards>>}
    .toolbar.markbar add -image [xpm-to-image $BINDIR/icon/mark.xpm] \
                 -highlightthickness 0 -takefocus 0 -relief link   \
                 -borderwidth 1 -padx 1 -pady 1 -helptext "mark"   \
                 -command cb_mark_set
    .toolbar.markbar add -image [xpm-to-image $BINDIR/icon/gotomark.xpm] \
                 -highlightthickness 0 -takefocus 0 -relief link   \
                 -borderwidth 1 -padx 1 -pady 1 -helptext "go to mark"   \
                 -command cb_goto_mark
    .toolbar.closebar add -text "x" -highlightthickness 0 -takefocus 0 -relief link   \
                 -borderwidth 1 -padx 1 -pady 1 -helptext "close tab"   \
                 -command close_tab
    pack .toolbar.filebar  -side left -anchor w
    pack .toolbar.s1       -side left -fill y -padx 4 -pady 4
    pack .toolbar.editbar  -side left -anchor w
    pack .toolbar.s2       -side left -fill y -padx 4 -pady 4
    pack .toolbar.printbar -side left -anchor w
    pack .toolbar.s3       -side left -fill y -padx 4 -pady 4
    pack .toolbar.findbar  -side left -anchor w
    pack .toolbar.s4       -side left -fill y -padx 4 -pady 4
    pack .toolbar.markbar  -side left -anchor w
    pack .toolbar.s5       -side left -fill y -padx 4 -pady 4
    pack .toolbar.closebar  -side left -anchor w
    pack .toolbar.s6    -side left -fill y -padx 4 -pady 4
    pack .toolbar -side top  -fill x -padx 1
    
}

proc get_tab_frame {} {
    global frameIndex noteBook textWidget summaryWidget
    set frameIndex [.tn index [.tn select]]
    set parent .tn.note$frameIndex
    set textWidget $parent.text.textarea
    set summaryWidget [$parent.title getframe].sum.frame.summary
    #display_cursor_pos
    #focus $textWidget
    return $parent
}

proc split_textarea {} {
    global frameIndex USR_CNF_T_HEIGHT USR_CNF_HIGHLIGHT
    set frameIndex [.tn index [.tn select]]
    set parent .tn.note$frameIndex
    $parent.text.textarea configure -height [expr $USR_CNF_T_HEIGHT /2] 
    
    frame $parent.text1
    pack [$parent.text.textarea peer create $parent.text1.textarea]
    $parent.text1.textarea configure -height [expr $USR_CNF_T_HEIGHT /2]
    
    $parent.text1.textarea configure -yscrollcommand "$parent.text1.y set"
    make_textarea $parent.text1
}
proc merge_textarea {} {
    global USR_CNF_T_HEIGHT
    set parent [get_tab_frame]
    catch {destroy $parent.text1}
    $parent.text.textarea configure -height $USR_CNF_T_HEIGHT
}

proc make_textarea {parent} {
    global noteBook
    global USR_CNF_T_WIDTH USR_CNF_T_FG USR_CNF_T_BG T_BD USR_CNF_T_FONT  T_CSR_WIDTH USR_CNF_T_TAB USR_CNF_T_HEIGHT
    global USR_CNF_HIGHLIGHT TEMP_FROM_ANOTHER_EVENT tcl_platform textAreaParent

    if ![winfo exist $parent.textarea] {
	text     $parent.textarea -width $USR_CNF_T_WIDTH                       \
	    -height $USR_CNF_T_HEIGHT                   \
	    -fg $USR_CNF_T_FG -bg $USR_CNF_T_BG -bd $T_BD \
	    -font $USR_CNF_T_FONT                         \
	    -insertwidth $T_CSR_WIDTH                     \
	    -yscrollcommand "$parent.y set"                 \
	    -tabs [expr $USR_CNF_T_TAB * 8]               \
	    -exportselection true                         \
	    -undo true
    }
    $parent.textarea tag configure sel -background $USR_CNF_HIGHLIGHT
    
    scrollbar  $parent.y -command "$parent.textarea yview" -orient vertical
    
    bind $parent.textarea <<Copy>> {
	tk_textCopy %W
	set parent1 [get_tab_frame]
	$parent1.text.textarea tag remove sel 1.0 end
	set TEMP_FROM_ANOTHER_EVENT 1
	break
    }
    
    bind $parent.textarea <<Paste>> {
	set parent1 [get_tab_frame]
	if {[llength [$parent1.text.textarea tag ranges sel]] > 0} {
	    set si [lindex [$parent1.text.extarea tag ranges sel] 0]
	    set ei [lindex [$parent1.text.textarea tag ranges sel] 1]
	    $parent1.text.textarea delete $si $ei
	    $parent1.text.textarea tag remove sel 1.0 end
	}
	tk_textPaste %W
	set TEMP_FROM_ANOTHER_EVENT 1
	set_text_modified "A"
	break
    }
    bind $parent.textarea <<New>> {
	cb_filemenu_new {}
	set TEMP_FROM_ANOTHER_EVENT 1
	break
    }
    bind $parent.textarea <<Open>> {
	cb_filemenu_open
	set TEMP_FROM_ANOTHER_EVENT 1
	break
    }
    bind $parent.textarea <<Save>> {
	cb_filemenu_save
	set TEMP_FROM_ANOTHER_EVENT 1
	break
    }
    bind $parent.textarea <<Find>> {
	cb_editmenu_find
	set TEMP_FROM_ANOTHER_EVENT 1
	# 'break' is needed not to excute bind scripts of Text widget.
	break
    }
    bind $parent.textarea <<FindForwards>> {
	editmenu_search_forwards
	set TEMP_FROM_ANOTHER_EVENT 1
	break
    }
    bind $parent.textarea <<FindBackwards>> {
	editmenu_search_backwards
	set TEMP_FROM_ANOTHER_EVENT 1
	break
    }
    bind $parent.textarea <<Replace>> {
	cb_editmenu_replace
	break
    }
    bind $parent.textarea <<SelectAll>> {
	cb_editmenu_select_all
	set TEMP_FROM_ANOTHER_EVENT 1
	break
    }
    bind $parent.textarea <<GotoLine>> {
	cb_editmenu_goto_line
	set TEMP_FROM_ANOTHER_EVENT 1
	break
    }
    bind $parent.textarea <Button-1> {
	display_cursor_pos
    }
    bind $parent.textarea <<InsertDate>> {
	cb_tools_date
	set TEMP_FROM_ANOTHER_EVENT 1
	set_text_modified "A"
	break
    }
    bind $parent.textarea <<InsertPicture>> {
	cb_tools_picture
	set TEMP_FROM_ANOTHER_EVENT 1
	set_text_modified "A"
	break
    }
    switch $tcl_platform(platform) {
	windows {
	    bind $parent.textarea <KeyPress> {
		bind_textarea_key_pressed %A %K
	    }
	}
	unix {
	    bind $parent.textarea <KeyRelease> {
		bind_textarea_key_pressed %A %K
	    }
	}
	default {
	    bind $parent.textarea <KeyRelease> {
		bind_textarea_key_pressed %A %K
	    }
	}
    }
    bind $parent.textarea <<Split>> {
	split_textarea
	set TEMP_FROM_ANOTHER_EVENT 1
	break
    }
    
    bind $parent.textarea <<Merge>> {
	merge_textarea
	set TEMP_FROM_ANOTHER_EVENT 1
	break
    }
    
    menu $parent.textarea.popup -tearoff no
    $parent.textarea.popup add command -label Paste -underline 1 \
	-command cb_editmenu_paste
    $parent.textarea.popup add separator
    $parent.textarea.popup add command -label Cute  -underline 1 \
	-command cb_editmenu_cut
    $parent.textarea.popup add command -label Copy  -underline 1 \
	-command cb_editmenu_copy
    bind $parent.textarea <3> { tk_popup $parent.textarea.popup %X %Y }
    pack $parent -fill both -expand 1 -padx 1 -pady 1
    ##  pack .base.textarea -fill both -expand 1
    
    grid $parent.textarea -column 0 -row 0 -sticky news
    grid $parent.y        -column 1 -row 0 -sticky news
    grid rowconfigure $parent 0 -weight 1
    grid columnconfigure $parent 0 -weight 1
}

proc SetDailyDir {noteIndex} {
    global noteBook
    set noteBook($noteIndex.dir) [APSGoToDailyDirectory]
    set noteBook($noteIndex.filename) $noteBook($noteIndex.dir)/[file tail $noteBook($noteIndex.filename)]
    updatefilename $noteIndex
}

proc updatefilename {noteIndex} {
    global noteBook
    #set noteBook($noteIndex.filename) $noteBook($noteIndex.dir)/[file tail $noteBook($noteIndex.filename)]
    if $noteBook($noteIndex.modified) {
	set noteBook($noteIndex.label) "$noteBook($noteIndex.filename) (Changed)"
    } else {
	set noteBook($noteIndex.label) $noteBook($noteIndex.filename)
    }
    .tn tab $noteIndex -text [file tail $noteBook($noteIndex.filename)]
    update
}

proc close_tab {} {
    global noteBook noteBooks frameIndex
    
    if {$noteBooks<1} {
	return
    }
    get_tab_frame
    if $noteBook($frameIndex.modified) {
	set res \
	    [tk_messageBox -type yesnocancel -title Save -icon warning \
		 -message "$noteBook($frameIndex.filename) changed. Do you want to save it?"]
	if {$res == "cancel"} {
	    return
	}
	if {[cb_filemenu_save] != 0} {
	    return;
	}
	set $noteBook($frameIndex.modified) 0
    }
    .tn hide $frameIndex
    set noteBook($frameIndex.invalid) 1
}

set noteBooks 0
set frameIndex 0
proc make_new_tab {parent filename} {
    global noteBooks frameIndex CST_SBAR_FONT noteBook USR_CNF_T_FONT
    if ![string length $filename] {
	set noteBook($noteBooks.dir) [pwd]
	if !$noteBooks {
	    set noteBook($noteBooks.filename) $noteBook($noteBooks.dir)/NoteBook
	    set filename NoteBook
	} else {
	    set noteBook($noteBooks.filename) $noteBook($noteBooks.dir)/NoteBook$noteBooks
	    set filename NoteBook$noteBooks
	}
    } else {
	for {set i 0} {$i<$noteBooks} {incr i} {
	    if {!$noteBook($i.invalid) && [string compare $filename $noteBook($i.filename)]==0} {
		set res \
		    [tk_messageBox -type yesnocancel -title "$filename already opened" -icon warning \
			 -message "$filename already opened. Do you want to close current one and re-open it?"]
		switch $res {
		    yes {
			.tn select $i
			cb_filemenu_close_and_open $filename
		    }
		    default {
			return
		    }
		}
	    }
	}

	set noteBook($noteBooks.dir) [file dir $filename]
	set noteBook($noteBooks.filename) $filename
	set filename [file tail $filename]
    }
    set command "global frameIndex;set frameIndex $noteBooks"
    frame $parent.note$noteBooks
   # pack [frame $parent.note$noteBooks.b] -fill x -expand 0
   # button $parent.note$noteBooks.b.close -text "x" -command "CloseTab $noteBooks"
   # pack $parent.note$noteBooks.b.close -side left -padx 2
    $parent add $parent.note$noteBooks -text $filename 
    
   # pack $parent.note$noteBooks -fill both -expand 1
    #$parent select $parent.note$noteBooks
   
    
    set tabParent $parent.note$noteBooks
    
    CollapsableFrame $tabParent.title \
	-text "Title " \
	-height 280 \
	-font "fixed 12 normal" 
    pack $tabParent.title \
	-in [winfo parent $tabParent.title] \
	-anchor center \
	-expand 0 \
	-fill x \
	-side top
    frame      $tabParent.sbar
    label    $tabParent.sbar.s1 -width 11 -justify left -font $CST_SBAR_FONT \
	-relief sunken -anchor w
    set noteBook($frameIndex.label) "$noteBook($frameIndex.filename)"
    label    $tabParent.sbar.s2 -justify left -font $CST_SBAR_FONT -anchor w \
	-relief sunken -textvariable noteBook($frameIndex.label)
    
    pack $tabParent.sbar -fill x -expand 0
    pack $tabParent.sbar.s1 -side left -padx 2
    pack $tabParent.sbar.s2 -side left -fill x -expand 1

    make_title $tabParent $noteBooks
    frame $tabParent.text
    make_textarea $tabParent.text
    .tn select $noteBooks
    if [file exist $noteBook($noteBooks.filename)] {
	cb_open_file $noteBook($noteBooks.filename)
    }
    set noteBook($noteBooks.modified) 0
    set noteBook($noteBooks.invalid) 0
    incr noteBooks
}
proc make_title {parent0 noteIndex} {
    global noteBook USR_CNF_T_WIDTH USR_CNF_T_HEIGHT USR_CNF_T_FG USR_CNF_T_BG T_BD USR_CNF_T_FONT T_CSR_WIDTH \
	USR_CNF_T_TAB
    
    set width 110
    set parent [$parent0.title getframe]
    APSFrameGrid .grid -parent $parent -yList {x1 x2 x3 x4 x5 x6}
    place $parent.grid -x 1 -y 15
    APSLabeledEntry .dir -parent $parent.grid.x1 -label "Filename" -textVariable noteBook($noteIndex.filename) -width $width
    APSButton .daily -parent $parent.grid.x1.dir -packOption "-side right" -size small -text "daily" \
	-command "SetDailyDir $noteIndex"
    bind $parent.grid.x1.dir.entry <KeyPress> "updatefilename $noteIndex"
  
    APSLabeledEntry .title -parent $parent.grid.x2 -label "Topic" -textVariable noteBook($noteIndex.topic) -width $width
  
    bind $parent.grid.x2.title.entry <KeyPress> "set_text_modified A"
    APSLabeledEntry .author -parent $parent.grid.x3 -label "Personnel Invovled" -textVariable noteBook($noteIndex.studiers) -width $width
    bind $parent.grid.x3.author.entry <KeyPress> "set_text_modified A"

    APSLabeledEntry .key -parent $parent.grid.x4 -label "Keywords" -textVariable noteBook($noteIndex.keywords) -width $width
    bind $parent.grid.x4.key.entry <KeyPress> "set_text_modified A"

    APSLabeledEntry .plan -parent $parent.grid.x5 -label "Studies plan" -textVariable noteBook($noteIndex.studyPlan) -width $width
    bind $parent.grid.x5.plan.entry  <KeyPress> "set_text_modified A"

    APSLabeledEntry .chron -parent $parent.grid.x6 -label "Stuides Chronology" \
	-textVariable noteBook($noteIndex.studyChron) -width $width
    bind $parent.grid.x6.chron.entry  <KeyPress> "set_text_modified A"
    
    
    APSFrame .sum -parent $parent -packOption "-expand true -fill x"
    place $parent.sum -x 1 -y 195
    APSLabel .label -parent $parent.sum.frame -text "Summary               " -packOption "-side left" -fgColor red
    text $parent.sum.frame.summary -width 90 -height 5 -bd 1 -relief sunken  -wrap word -setgrid 1 -font "courier 12"
    pack $parent.sum.frame.summary  -side left -fill both -expand 1
    set summaryWidget $parent.sum.frame.summary
    bind $summaryWidget <KeyPress> "set_text_modified A"
}

proc color_line {} {
    global USR_CNF_T_CUR_COLOR
    set parent [get_tab_frame]
    $parent.text.textarea tag delete colorline
    $parent.text.textarea tag add colorline \
                            [$parent.text.textarea index "insert linestart"] \
                            [$parent.text.textarea index "insert lineend"  ]
    $parent.text.textarea tag configure colorline -foreground $USR_CNF_T_CUR_COLOR
}

proc display_cursor_pos {} {
    after 10 {
	set parent [get_tab_frame]
	$parent.sbar.s1 configure -text [$parent.text.textarea index insert]
        $parent.sbar.s2 configure -text {}
        color_line
    }
}

proc set_text_modified {A} {
    global M2001 M9002
    global TEMP_TEXT_MODIFIED TEMP_FILENAME
    global TEMP_FROM_ANOTHER_EVENT frameIndex noteBook

    set frameIndex [.tn index [.tn select]]
   
    if {$TEMP_FROM_ANOTHER_EVENT == 1} {
        set TEMP_FROM_ANOTHER_EVENT 0
        return
    }
    if {[string compare "" $A] != 0} {
        set TEMP_TEXT_MODIFIED 1
        set TEMP_FROM_ANOTHER_EVENT 0
	set noteBook($frameIndex.modified) 1
	set noteBook($frameIndex.label) "$noteBook($frameIndex.filename) (Changed)"
    }
    set TEMP_FROM_ANOTHER_EVENT 0
}

proc bind_textarea_key_pressed {a k} {
    global frameIndex
    set parent [get_tab_frame]
    switch $k {
        "XF86_Switch_VT_3" {
            event generate $parent.text.textarea <<FindBackwards>>
        }
        "Escape" {
            return
        }
        "Delete" {
            set_text_modified A
            return
        }
    } 
    set_text_modified $a
    display_cursor_pos
}


set apsInfoBalloon(active) 0

package require BWidget
init
wm protocol . WM_DELETE_WINDOW cb_filemenu_quit

font create MyFont -family "fixed" -size 11 
option add *font MyFont

make_toolbar
make_menu
#create notebook
pack [ttk::notebook .tn] -expand 1 -fill both
#pack .userFrame.tn -side top -expand 1 -fill both
#ttk::notebook::enableTraversal .userFrame.tn
make_new_tab .tn {}
#display_cursor_pos
#focus .tn.note0.text.textarea