2022-07-21 10:56:46 +03:00
|
|
|
######################################################
|
|
|
|
# ProjMan 2
|
|
|
|
# Distributed under GNU Public License
|
|
|
|
# Author: Sergey Kalinin svk@nuk-svk.ru
|
|
|
|
# Copyright (c) "", 2022, https://nuk-svk.ru
|
|
|
|
######################################################
|
|
|
|
# Working with files module
|
|
|
|
######################################################
|
|
|
|
|
|
|
|
|
|
|
|
namespace eval FileOper {
|
|
|
|
variable types
|
|
|
|
|
|
|
|
set ::types {
|
|
|
|
{"All files" *}
|
2022-08-12 15:23:20 +03:00
|
|
|
}
|
2022-07-21 10:56:46 +03:00
|
|
|
|
|
|
|
proc OpenDialog {} {
|
|
|
|
global env
|
|
|
|
set dir $env(HOME)
|
|
|
|
set fullPath [tk_getOpenFile -initialdir $dir -filetypes $::types -parent .]
|
|
|
|
set file [string range $fullPath [expr [string last "/" $fullPath]+1] end]
|
|
|
|
regsub -all "." $file "_" node
|
|
|
|
set dir [file dirname $fullPath]
|
|
|
|
set file [file tail $fullPath]
|
|
|
|
set name [file rootname $file]
|
|
|
|
set ext [string range [file extension $file] 1 end]
|
|
|
|
if {$fullPath != ""} {
|
|
|
|
puts $fullPath
|
|
|
|
return $fullPath
|
|
|
|
} else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
proc OpenFolderDialog {} {
|
|
|
|
global env
|
|
|
|
#global tree node types dot env noteBook fontNormal fontBold fileList noteBook projDir activeProject imgDir editor rootDir
|
|
|
|
# set dir $projDir
|
|
|
|
set dir $env(HOME)
|
|
|
|
set fullPath [tk_chooseDirectory -initialdir $dir -parent .]
|
|
|
|
set file [string range $fullPath [expr [string last "/" $fullPath]+1] end]
|
|
|
|
regsub -all "." $file "_" node
|
|
|
|
set dir [file dirname $fullPath]
|
|
|
|
# EditFile .frmBody.frmCat.noteBook.ffiles.frmTreeFiles.treeFiles $node $fullPath
|
|
|
|
puts $fullPath
|
|
|
|
|
|
|
|
return $fullPath
|
|
|
|
}
|
2022-07-21 16:01:28 +03:00
|
|
|
|
|
|
|
proc CloseAll {} {
|
|
|
|
global nbEditor modified
|
|
|
|
foreach nbItem [array names modified] {
|
|
|
|
if {$modified($nbItem) eq "true"} {
|
|
|
|
$nbEditor select $nbItem
|
|
|
|
puts "close tab $nbItem"
|
|
|
|
if {[Close] eq "cancel"} {return "cancel"}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-21 10:56:46 +03:00
|
|
|
proc Close {} {
|
|
|
|
global nbEditor modified tree
|
|
|
|
set nbItem [$nbEditor select]
|
2022-08-24 15:04:19 +03:00
|
|
|
puts "close tab $nbItem"
|
|
|
|
|
2022-07-21 10:56:46 +03:00
|
|
|
if {$nbItem == ""} {return}
|
|
|
|
if {$modified($nbItem) eq "true"} {
|
2022-07-21 16:01:28 +03:00
|
|
|
set answer [tk_messageBox -message [::msgcat::mc "File was modifyed"] \
|
|
|
|
-icon question -type yesnocancel \
|
|
|
|
-detail [::msgcat::mc "Do you want to save it?"]]
|
|
|
|
switch $answer {
|
|
|
|
yes Save
|
|
|
|
no {}
|
|
|
|
cancel {return "cancel"}
|
|
|
|
}
|
2022-07-21 10:56:46 +03:00
|
|
|
}
|
|
|
|
$nbEditor forget $nbItem
|
|
|
|
destroy $nbItem
|
|
|
|
set treeItem "file::[string range $nbItem [expr [string last "." $nbItem] +1] end ]"
|
2022-07-27 16:55:04 +03:00
|
|
|
if [$tree exists $treeItem] {
|
2022-08-24 15:04:19 +03:00
|
|
|
# delete all functions from tree item
|
|
|
|
set children [$tree children $treeItem]
|
|
|
|
if {$children ne ""} {
|
|
|
|
foreach i $children {
|
|
|
|
$tree delete $i
|
|
|
|
}
|
|
|
|
}
|
2022-07-27 16:55:04 +03:00
|
|
|
if {[$tree parent $treeItem] eq ""} {
|
|
|
|
$tree delete $treeItem
|
|
|
|
}
|
2022-07-21 10:56:46 +03:00
|
|
|
}
|
2022-07-21 16:01:28 +03:00
|
|
|
unset modified($nbItem)
|
2022-07-21 10:56:46 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
proc Save {} {
|
|
|
|
global nbEditor tree env
|
|
|
|
set nbEditorItem [$nbEditor select]
|
|
|
|
puts "Saved editor text: $nbEditorItem"
|
|
|
|
if [string match "*untitled*" $nbEditorItem] {
|
|
|
|
set filePath [tk_getSaveFile -initialdir $env(HOME) -filetypes $::types -parent .]
|
|
|
|
if {$filePath eq ""} {
|
|
|
|
return
|
|
|
|
}
|
2022-07-27 16:55:04 +03:00
|
|
|
# set fileName [string range $filePath [expr [string last "/" $filePath]+1] end]
|
|
|
|
set fileName [file tail $filePath]
|
|
|
|
$nbEditor tab $nbEditorItem -text $fileName
|
2022-07-25 16:04:25 +03:00
|
|
|
# set treeitem [Tree::InsertItem $tree {} $filePath "file" $fileName]
|
2022-07-27 16:55:04 +03:00
|
|
|
set lblName "lbl[string range $nbEditorItem [expr [string last "." $nbEditorItem] +1] end]"
|
|
|
|
$nbEditorItem.$lblName configure -text $filePath
|
2022-07-21 10:56:46 +03:00
|
|
|
} else {
|
|
|
|
set treeItem "file::[string range $nbEditorItem [expr [string last "." $nbEditorItem] +1] end ]"
|
|
|
|
set filePath [Tree::GetItemID $tree $treeItem]
|
|
|
|
}
|
|
|
|
set editedText [$nbEditorItem.frmText.t get 0.0 end]
|
|
|
|
set f [open $filePath "w+"]
|
|
|
|
puts -nonewline $f $editedText
|
|
|
|
puts "$f was saved"
|
|
|
|
close $f
|
|
|
|
ResetModifiedFlag $nbEditorItem
|
|
|
|
}
|
|
|
|
|
|
|
|
proc SaveAll {} {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
proc Delete {} {
|
|
|
|
set node [$tree selection get]
|
|
|
|
set fullPath [$tree itemcget $node -data]
|
|
|
|
set dir [file dirname $fullPath]
|
|
|
|
set file [file tail $fullPath]
|
|
|
|
set answer [tk_messageBox -message "[::msgcat::mc "Delete file"] \"$file\"?"\
|
|
|
|
-type yesno -icon question -default yes]
|
|
|
|
case $answer {
|
|
|
|
yes {
|
|
|
|
FileDialog $tree close
|
|
|
|
file delete -force "$fullPath"
|
|
|
|
$tree delete $node
|
|
|
|
$tree configure -redraw 1
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-12 15:23:20 +03:00
|
|
|
proc ReadFolder {directory} {
|
|
|
|
global tree dir
|
|
|
|
puts "Read the folder $directory"
|
2022-07-21 10:56:46 +03:00
|
|
|
set rList ""
|
2022-08-12 15:23:20 +03:00
|
|
|
if {[catch {cd $directory}] != 0} {
|
2022-07-21 10:56:46 +03:00
|
|
|
return ""
|
|
|
|
}
|
2022-08-12 15:23:20 +03:00
|
|
|
set parent [Tree::InsertItem $tree {} $directory "directory" $directory]
|
2022-07-21 10:56:46 +03:00
|
|
|
# if {[ $tree item $parent -open] eq "false"} {
|
|
|
|
# $tree item $parent -open true
|
|
|
|
# } else {
|
|
|
|
# $tree item $parent -open false
|
|
|
|
# }
|
|
|
|
# Getting an files and directorues lists
|
|
|
|
foreach file [glob -nocomplain *] {
|
2022-08-12 15:23:20 +03:00
|
|
|
lappend rList [list [file join $directory $file]]
|
2022-07-21 10:56:46 +03:00
|
|
|
if [file isdirectory $file] {
|
|
|
|
lappend lstDir $file
|
|
|
|
} else {
|
|
|
|
lappend lstFiles $file
|
|
|
|
}
|
|
|
|
}
|
|
|
|
# Sort lists and insert into tree
|
|
|
|
if {[info exists lstDir] && [llength $lstDir] > 0} {
|
|
|
|
foreach f [lsort $lstDir] {
|
2022-08-12 15:23:20 +03:00
|
|
|
puts " Tree insert item: [Tree::InsertItem $tree $parent [file join $directory $f] "directory" $f]"
|
2022-07-21 10:56:46 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if {[info exists lstFiles] && [llength $lstFiles] > 0} {
|
|
|
|
foreach f [lsort $lstFiles] {
|
2022-08-12 15:23:20 +03:00
|
|
|
puts "Tree insert item: [Tree::InsertItem $tree $parent [file join $directory $f] "file" $f]"
|
2022-07-21 10:56:46 +03:00
|
|
|
}
|
|
|
|
}
|
2022-08-12 15:23:20 +03:00
|
|
|
# Чтение структуры файлов в каталоге
|
|
|
|
# пока криво работает
|
|
|
|
# Accept $dir(lib) $directory
|
2022-07-21 10:56:46 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
proc ReadFile {fileFullPath itemName} {
|
|
|
|
set txt $itemName.frmText.t
|
|
|
|
if ![string match "*untitled*" $itemName] {
|
|
|
|
set file [open "$fileFullPath" r]
|
2022-08-24 15:04:19 +03:00
|
|
|
$txt insert end [chan read -nonewline $file]
|
2022-07-21 10:56:46 +03:00
|
|
|
close $file
|
|
|
|
}
|
|
|
|
# Delete emty last line
|
|
|
|
if {[$txt get {end-1 line} end] eq "\n" || [$txt get {end-1 line} end] eq "\r\n"} {
|
|
|
|
$txt delete {end-1 line} end
|
|
|
|
puts ">[$txt get {end-1 line} end]<"
|
|
|
|
}
|
2022-08-24 15:04:19 +03:00
|
|
|
$txt see 1.0
|
2022-07-21 10:56:46 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
proc Edit {fileFullPath} {
|
|
|
|
global nbEditor tree
|
|
|
|
set filePath [file dirname $fileFullPath]
|
|
|
|
set fileName [file tail $fileFullPath]
|
|
|
|
regsub -all {\.|/|\\|\s} $fileFullPath "_" itemName
|
|
|
|
set itemName "$nbEditor.$itemName"
|
2022-08-01 16:24:42 +03:00
|
|
|
set treeItemName [Tree::InsertItem $tree {} $fileFullPath "file" $fileName]
|
2022-07-21 10:56:46 +03:00
|
|
|
if {[winfo exists $itemName] == 0} {
|
|
|
|
NB::InsertItem $nbEditor $fileFullPath "file"
|
|
|
|
Editor::Editor $fileFullPath $nbEditor $itemName
|
|
|
|
ReadFile $fileFullPath $itemName
|
|
|
|
$itemName.frmText.t highlight 1.0 end
|
|
|
|
ResetModifiedFlag $itemName
|
2022-08-24 15:04:19 +03:00
|
|
|
$itemName.frmText.t see 1.1
|
2022-07-21 10:56:46 +03:00
|
|
|
}
|
|
|
|
$nbEditor select $itemName
|
2022-08-01 16:24:42 +03:00
|
|
|
Editor::ReadStructure $itemName.frmText.t $treeItemName
|
2022-08-24 15:04:19 +03:00
|
|
|
|
|
|
|
focus -force $itemName.frmText.t.t
|
2022-07-21 10:56:46 +03:00
|
|
|
|
|
|
|
return $itemName
|
|
|
|
}
|
|
|
|
}
|