# Copyright (c) 2000, Red Hat, Inc.
# 
# This file is part of Source-Navigator.
# 
# Source-Navigator is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License as published
# by the Free Software Foundation; either version 2, or (at your option)
# any later version.
# 
# Source-Navigator is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.
# 
# You should have received a copy of the GNU General Public License along
# with Source-Navigator; see the file COPYING.  If not, write to
# the Free Software Foundation, 59 Temple Place - Suite 330, Boston,
# MA 02111-1307, USA.
# 
################################################
##
## Target Manager (Build Manager) Dialog.
## Copyright (c) 1999 Cygnus Solutions, Inc.
## Written by Ian Roxborough <irox@cygnus.com>
##
################################################

#source F:/sources/sn99r1/devo/snavigator/gui/targetmgr.tcl
itcl_class snIdeTargetMgr {
#    inherit Toplevel&

    protected thisTail ""
    protected tm_list ""    ; # targets list box widget
    protected setbox ""     ; # set entry box.
    protected tm_set_but "" ; # set button.
    protected tm_but_dup
    protected tm_but_new
    protected tm_but_edit
    protected tm_but_del
    protected tm_but_rename
    protected tm_but_done
    protected creating_new_target "0" ; # new target flag.
    protected last_selected_item "" ;


    method constructor {{args ""}} {
        set tail tail
        set thisTail "$this$tail"

#        if {$args!=""} {$thisTail configure $args}

        CreateDialog
	focus $thisTail
    }

    method CreateDialog {} {
        toplevel $thisTail
        wm title $thisTail [get_indep String IDETargetMgrTitle]

        @@grid columnconfigure $thisTail 0 -minsize 10
        @@grid rowconfigure $thisTail 1 -minsize 10

        @@grid columnconfigure $thisTail 3 -minsize 10
        @@grid rowconfigure $thisTail 8 -minsize 10

        set tm_txt [label $thisTail.txt -text [get_indep String IDETMBuildTargets] -under [get_indep Pos IDETMBuildTargets] ]
        set tm_list [listbox [frame $thisTail.fr -relief sunken -borderwidth 2].lbx -width 40 -relief flat -yscrollcommand [list $thisTail.fr.scrolly set]]
        scrollbar $thisTail.fr.scrolly -orient vertical -command [list $tm_list yview]
	grid $tm_list $thisTail.fr.scrolly -sticky news
	grid rowconfigure $thisTail.fr 0 -weight 1
        # Get a list of targets
        set targets [GetTargetsList]

        foreach target $targets {
            $tm_list insert end $target
            sn_log "IDE_DEBUG: target = <$target>"
        }
    
        set listcmdsel "$this target_selected \[$tm_list curselection\]"
        set listcmdaction "$this edit_target"
        bind $tm_list <ButtonRelease-1> "eval $listcmdsel"
        bind $tm_list <Double-1> "eval $listcmdaction"
		
        @@grid $tm_txt -column 1 -row 2 -sticky w 
        @@grid $thisTail.fr -column 1 -columnspan 2 -row 3 -rowspan 6 -sticky news

        set tm_but_fr $thisTail
        set tm_but_new [button $thisTail.new -text [get_indep String IDETMCreateTarget] -width 11 \
				-command "$this create_cb" -under [get_indep Pos IDETMCreateTarget]]
        set tm_but_dup [button $tm_but_fr.dup -text [get_indep String IDETMCopyTarget] -width 11 \
				-command "$this copy_target_cb" -under [get_indep Pos IDETMCopyTarget]]
        set tm_but_rename [button $tm_but_fr.mv -text [get_indep String IDETMRenameTarget] -width 11 \
				-command "$this rename_target_cb" -under [get_indep Pos IDETMRenameTarget]]
        set tm_but_edit [button $tm_but_fr.edit -text [get_indep String IDETMEditTarget] -width 11\
                                -command "$this edit_target" -under [get_indep Pos IDETMEditTarget]]

#        set tm_but_save [button $tm_but_fr.save -text "Save" -width 11]
        set tm_but_del [button $tm_but_fr.del -text [get_indep String IDETMDeleteTarget] -width 11 \
				-command "$this delete_target_cb" -under [get_indep Pos IDETMDeleteTarget]]
        set tm_but_done [button $tm_but_fr.done -text [get_indep String IDETargetMgrDone] \
	                        -width 11 -command "$this done_cb" -under [get_indep Pos IDETargetMgrDone]]

        @@grid $tm_but_new -column 3 -row 3 -padx 6 -pady 4
        @@grid $tm_but_dup -column 3 -row 6 -padx 6 -pady 4
        @@grid $tm_but_edit -column 3 -row 5 -padx 6 -pady 4
        @@grid $tm_but_del -column 3 -row 7 -padx 6 -pady 4
        @@grid $tm_but_rename -column 3 -row 4 -padx 6 -pady 4
        @@grid rowconfigure $thisTail 8 -weight 10
        @@grid $tm_but_done -column 3 -row 9 -padx 4 -pady 4

        set setbox [entry $thisTail.lstent]
        bind $setbox <KeyRelease> "$this EntryBoxChanged %K"
        set tm_set_but [button $thisTail.setbut \
                               -text Set \
                               -command "$this set_cb"]
        @@grid columnconfigure $thisTail 1 -weight 10

        @@grid $setbox -column 1 -columnspan 2 -row 9 -sticky we

	wm protocol $thisTail WM_DELETE_WINDOW "$tm_but_done invoke"
	bind $thisTail <Escape> "$tm_but_done invoke"

        HandleState
    }

    method raise {} {
	@@raise $thisTail
	focus $thisTail

    }

    method UpdateTargetList {} {
	# Delete old targets
	$tm_list delete 0 end

        # Get a list of targets
        set targets [GetTargetsList]

        foreach target $targets {
            $tm_list insert end $target
            sn_log "IDE_DEBUG: target = <$target>"
        }
    }

#TODO: This method has been earmark to be removed.
    method new_target {} {
        set creating_new_target 1

        # Now the user can only:
        # press Delete to cancel creation
        # or press Set to create target.

        $tm_but_dup configure -state disabled
        $tm_but_new configure -state disabled
        $tm_but_edit configure -state disabled
        $tm_but_del configure -state normal
        $tm_but_done configure -state disabled

        $tm_list insert end "new_target"
        $tm_list selection clear 0 end
        $tm_list selection set end
        target_selected [$tm_list curselection]
    }

    method target_selected {{itemnum ""}} {

	if {$itemnum!=""} {
	    set last_selected_item $itemnum
	    set item [$tm_list get $itemnum]
	    $setbox delete 0 end
	    $setbox insert end $item
	}
        HandleState
    }

    method EntryBoxChanged {{keypress ""}} {

	# If the user is hitting backspace instead of
	# typing, they probably don't want us to start
	# selecting targets for them.  Unless nothing is
	# selected then we should try to select something.

	set list_value [$tm_list curselection]

	if {$keypress=="BackSpace" && $list_value!=""} {
	    
	    # Just update the button state
	    HandleState

	    return
	}

        set entry_value [$setbox get]

        # Check to see if new Entry box value
        # matches any list box values.
        # Why? so it can be selected from the list

        set matched 0
        
        set itemnum -1

        foreach item [$tm_list get 0 end] {
            incr itemnum
            if {$item==$entry_value} {
                set matched 1
                break;
            }
        }

        if {$matched} {
            $tm_list selection clear 0 end
            $tm_list selection set $itemnum
        }

        HandleState
    }

#TODO: This method has been earmarked to be removed.
    method set_cb {} {
        #set stuff.

        #get new name
        set new_name [$setbox get]

        #TODO: check that the new name has
        #not already be given to a target.

        #set new name
        set item $last_selected_item
        $tm_list delete $item
        $tm_list insert $item $new_name
        $tm_list selection set $item

        if {$creating_new_target==1} {
            #do create a target...
	    set new_name [$setbox get]

        } else {
            #do rename target.
        }
         
        #clear entry
        $setbox delete 0 end
        #disable set button
        $tm_set_but configure -state disabled

        if {$creating_new_target==1} {
            # enable the other controls
            $tm_but_dup configure -state normal
            $tm_but_new configure -state normal
            $tm_but_edit configure -state normal
            $tm_but_del configure -state normal
            $tm_but_done configure -state normal

            #TODO: call target editor.

            set creating_new_target 0
        }

    }

    method edit_target {} {
        
        # Get the targets name
        set target_name [$setbox get]

        if {$target_name==""} { 
             bell -displayof $thisTail
	     return 
	}

        # Invoke target editor

        snEditTarget .snte $target_name
	UpdateBuildWindows
    }


    method HandleState {{selected ""} {entered ""}} {

        # Get Listbox selection
        set selection [$tm_list curselect]
        if {$selected == "" && $selection != ""} {
            set selected [$tm_list get $selection]
        }

        # Get Text Widget contents.
        if {$entered==""} {
            set entered [$setbox get]
        }

        #Nothing entered, nothing selected.
        if {$selected=="" && $entered==""} {
            $tm_but_dup configure -state disabled
            $tm_but_new configure -state disabled
            $tm_but_edit configure -state disabled
            $tm_but_del configure -state disabled
            $tm_but_rename configure -state disabled

            $tm_but_done configure -state normal
            $tm_but_done configure -default active
	    bind $thisTail <Return> "$tm_but_done invoke"
            $tm_but_edit configure -default normal
            $tm_but_new configure -default normal
            $tm_but_rename configure -default normal
            return
        }

        #something entered, nothing selected.
        if {$selected=="" && $entered!=""} {
            #only "Done" and "Create*" active
            $tm_but_dup configure -state disabled
            $tm_but_new configure -state normal
            $tm_but_edit configure -state disabled
            $tm_but_del configure -state disabled
            $tm_but_rename configure -state disabled
            $tm_but_done configure -state normal
            $tm_but_new configure -default active
	    bind $thisTail <Return> "$tm_but_new invoke"
            $tm_but_edit configure -default normal
            $tm_but_done configure -default normal
            $tm_but_rename configure -default normal
            return
        }

        #something entered, something selected (matching)
        if {$entered!="" && $selected!="" && $selected==$entered} {
            #"Done", "Edit*", "Duplicate", "Delete" and "Done" active
            $tm_but_dup configure -state normal
            $tm_but_new configure -state disabled
            $tm_but_rename configure -state disabled
            $tm_but_edit configure -state normal
            $tm_but_del configure -state normal
            $tm_but_done configure -state normal
            $tm_but_edit configure -default active
	    bind $thisTail <Return> "$tm_but_edit invoke"
            $tm_but_done configure -default normal
            $tm_but_new configure -default normal
            $tm_but_rename configure -default normal
            return
        }

        #something entered, something seleted (not matching)
        if {$entered!="" && $selected!="" && $selected!=$entered} {
            #"Done", "Create*" and "Rename" active.
            $tm_but_dup configure -state disabled
            $tm_but_new configure -state normal
            $tm_but_rename configure -state normal
            $tm_but_edit configure -state disabled
            $tm_but_del configure -state disabled
            $tm_but_done configure -state normal
            $tm_but_new configure -default active
	    bind $thisTail <Return> "$tm_but_new invoke"
            $tm_but_done configure -default normal
            $tm_but_edit configure -default normal
            $tm_but_rename configure -default normal
            return
        }
    }

    method create_cb {} {
        set new_name [$setbox get]

        CreateTarget $new_name

        $tm_list insert end "$new_name"
        $tm_list selection clear 0 end
        $tm_list selection set end

        target_selected [$tm_list curselection]

        snEditTarget .new_target $new_name
	EntryBoxChanged
	UpdateBuildWindows
    }

    method done_cb {} {

        destroy $thisTail

        $this delete

    }

    method copy_target_cb {} {
	set selected [$tm_list curselection]
	set target_name [$tm_list get $selected]
	set new_name [CopyTarget $target_name]
	UpdateTargetList
	$setbox delete 0 end
	$setbox insert end $new_name
	EntryBoxChanged
    }

    method rename_target_cb {} {
	set selected [$tm_list curselection]
	set old_name [$tm_list get $selected]

	set new_name [$setbox get]

	RenameTarget $old_name $new_name
	UpdateTargetList
	EntryBoxChanged
    }

    method delete_target_cb {} {
	set dead_name [$setbox get]
	DeleteTarget $dead_name
	UpdateTargetList
	$setbox delete 0 end
	EntryBoxChanged
    }
    
    method UpdateBuildWindows {} {
        foreach buildwindow [itcl_info objects "*" -class Make&] {
	    $buildwindow RefreshTargetInfo
	}
    }
}

# Get list for targets from the db.
proc GetTargetsList {} {
    set targets_list [paf_db_proj get -key snide_targets]

    return $targets_list
}


# Create a build target
proc CreateTarget {target_name} {

    set targets [GetTargetsList]
    sn_log "IDE_DEBUG(proc CreateTarget): targets = <$targets>"

    lappend targets $target_name
    sn_log "IDE_DEBUG(proc CreateTarget): targets = <$targets>"

    #TODO: check target_name is unique

    sn_log "IDE_DEBUG: Creating build <$target_name>"

    paf_db_proj put snide_targets $targets

    paf_db_proj sync
}

proc RenameTarget {old_target_name target_name} {
    set t_obj [snBuildTarget .oldone]
    $t_obj LoadData $old_target_name
    CreateTarget $target_name
    $t_obj SaveData $target_name
    $t_obj delete

    DeleteTarget $old_target_name
}

proc DeleteTarget {target_name} {
    set del_target [snBuildTarget .targ_obj]
    $del_target DeleteData $target_name
    $del_target delete
    DeleteTargetName $target_name
}

proc DeleteTargetName {target_name} {

    set targets [GetTargetsList]

    # Find it.
    set kill [lsearch $targets $target_name]

    # Remove it.
    set targets [lreplace $targets $kill $kill]

    # Save the rest. 
    paf_db_proj put snide_targets $targets
    paf_db_proj sync
}

proc CopyTarget {target_name} {

    set targets [GetTargetsList]

    # Get a unquie name
    set copy "Copy"
    set num ""
    while {[lsearch $targets "$target_name $copy$num"]!="-1"} {
	if {$num==""} {
	    set num 1
	} else {
	    incr num
	}
    }

    # Copy it.
    set t_obj [snBuildTarget .tcopy]
    $t_obj LoadData $target_name
    CreateTarget "$target_name $copy$num"
    $t_obj SaveData "$target_name $copy$num"
    $t_obj delete

    return "$target_name $copy$num"
}

