#
# $RCSfile: edit.itcl,v $ --
#
#      This file contains ...
#
# Copyright (c) 2004  Anton Kokalj   Email: tone.kokalj@ijs.si
#
#
# This file is distributed under the terms of the GNU General Public
# License. See the file `COPYING' in the root directory of the present
# distribution, or http://www.gnu.org/copyleft/gpl.txt .
#
# 
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
# ANTON KOKALJ BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
# AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#
#
# $Id: edit.itcl,v 1.2 2004-02-23 12:14:43 kokalj Exp $ 
#
    
proc ::pwscf::edit::inputByEditor {what guiObj} {
    variable ::pwscf::settings 
    variable edit 
    variable counter

    if { ! [info exists counter] } {
	set counter 0
    } else {
	incr counter
    }

    set moduleObj   [$guiObj getSelected moduleObj]
    set moduleIdent [$guiObj getSelected moduleIdent]    
    set tmpFile [::tclu::tempFile name pwscf_$moduleIdent]
    namespace eval ::guib "$moduleObj save $tmpFile 1"

    # check if EDITOR is OK
    
    if { [auto_execok [lindex $::pwscf::settings(EDITOR) 0]] == "" } {
	# rescue me ...
	set editor PWgui
    } else {
	# EDITOR is OK
	set editor $::pwscf::settings(EDITOR)
    }

    # do the editing

    switch -exact -- $what {
	original {
	    # edit original input with EDITOR

	    # disable selected page ..
	    set index [$guiObj getSelected tabIndex]
	    $guiObj page disable $index

	    # load file into editor ...
	    if { [string match -nocase "PWgui" $editor] } {
		defaultEditor $tmpFile $counter
		tkwait variable ::pwscf::edit::edit(done,$counter)
	    } else {
		set id [::tclu::nonblocking open]
		eval ::tclu::nonblocking exec $id $editor $tmpFile
		::tclu::nonblocking unset $id    
	    }

	    # enable pack previously disabled page ...
	    $guiObj page enable $index
	    
	    # load the edit file back into the GUI ...
	    namespace eval ::guib "$moduleObj openFile $tmpFile"
	}

	copy {
	    # edit copy of input with EDITOR
	    if { [string match -nocase "PWgui" $editor] } {
		defaultEditor $tmpFile
	    } else {
		if { [catch {eval exec $editor $tmpFile &}] } {
		    defaultEditor $tmpFile
		}
	    }
	}

	default {
	    ::tclu::ERROR "wrong mode $what, must be original or copy"
	}
    }
}


proc ::pwscf::edit::defaultEditor {file {counter ""}} {
    variable edit

    set t [::guib::widgets::toplevel [::tku::widgetName] -title "File: $file" -transient 0]

    set edit(saveFile,$t) $file
    set edit(counter,$t)  $counter    
    
    set text [::iwidgets::scrolledtext $t.text \
		  -hscrollmode dynamic -vscrollmode dynamic \
		  -wrap none -state normal]
    $text import $file

    set bb  [::iwidgets::buttonbox $t.bb]
    $bb add Cancel -text "Cancel"       -command [list ::pwscf::edit::_defaultEditorCancel $t]
    $bb add Save   -text "Save"         -command [list ::pwscf::edit::_defaultEditorSave   $t $text]
    $bb add SaveAs -text "Save As"      -command [list ::pwscf::edit::_defaultEditorSaveAs $t $text]
    $bb add Close  -text "Save & Close" -command [list ::pwscf::edit::_defaultEditorClose  $t $text]
    $bb default Close

    pack $text -side top -fill both -expand 1    
    pack $bb   -side top -fill x -expand 1    
}
proc ::pwscf::edit::_defaultEditorCancel {t} {
    variable edit
    set button [tk_messageBox \
		    -message "Changes will be lost. Really close this window?" \
                    -type yesno -icon question]
    if { $button == "yes" } {
	_defaultEditorDone $t
    }
}
proc ::pwscf::edit::_defaultEditorClose {t textWid} {
    _defaultEditorSave $t $textWid
    _defaultEditorDone $t

}
proc ::pwscf::edit::_defaultEditorDone {t} {
    variable edit
    set counter $edit(counter,$t)
    array unset edit *,$t
    if { $counter != "" } {
	set edit(done,$counter) 1
    }
    destroy $t
}
proc ::pwscf::edit::_defaultEditorSave {t textWid} {
    variable edit
    if { [info exists edit(saveFile,$t)] } {
	$textWid export $edit(saveFile,$t)
    }
}
proc ::pwscf::edit::_defaultEditorSaveAs {t textWid} {
    variable ::pwscf::pwscf
    variable edit

    set filetypes {
        {{Input Files}         {.inp}}
        {{Input Files}         {.in} }
        {{All Files}           *     }
    }

    set saveFile [tk_getSaveFile \
		      -initialdir $::pwscf::pwscf(PWD) \
		      -title      "Save File As" \
		      -defaultextension .inp \
		      -filetypes $filetypes]
    # maybe Cancel button was pressed    
    if { $saveFile == "" } {     
        return
    }
    # now save the file
    set edit(saveFile,$t) $saveFile
    _defaultEditorSave $t $textWid
    $t configure -title "File: [file tail $saveFile]"
}
