file.tcl 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  1. ###########################################################
  2. # Name: file.tcl
  3. # Author: Daniele Bonini (posta@elettronica.lol)
  4. # Date: 27/11/2023
  5. # Desc: File Menu namespace of RadXIDE.
  6. #
  7. # File Menu namespace scaffolding and most of the code
  8. # here presented and distributed contains excerpts
  9. # from [alited](https://github.com/aplsimple/alited
  10. # by Alex Plotnikov and contributors to the project.
  11. # The original code of these excerpts could be
  12. # borrowed from other sources which the author
  13. # and the contributors to this RadXIDE have no
  14. # knowledge about.
  15. #
  16. # License: MIT. Copyrights 5 Mode (Last implementation and adaptations.)
  17. # Copyright (c) 2021-2023 Alex Plotnikov https://aplsimple.github.io (original scaffolding and excerpts.)
  18. #
  19. ###########################################################
  20. namespace eval file {
  21. #_________________________ addFile ______________________ #
  22. proc addFile {} {
  23. # Add a file to the current project
  24. namespace upvar ::radxide dan dan
  25. set tree $dan(TREEVIEW)
  26. if {[set ID [$tree selection]] eq {}} return
  27. ::radxide::tree::addFile $ID
  28. }
  29. #_________________________ closeFile ______________________ #
  30. proc closeFile {} {
  31. # Close the current file
  32. namespace upvar ::radxide dan dan menu menu project project
  33. # var reset
  34. set project(CUR_FILE_PATH) ""
  35. # ide text reset
  36. set t $dan(TEXT)
  37. $t config -state normal
  38. $t delete 1.0 end
  39. $t insert end ""
  40. $t config -state disabled
  41. ::radxide::win::fillGutter $dan(TEXT) $dan(GUTTEXT) 5 1 "#FFFFFF" "#222223"
  42. # Update file Menu
  43. $menu(FILE) entryconfigure $menu(SAVE_AS_ENTRY_IDX) -state disabled
  44. $menu(FILE) entryconfigure $menu(SAVE_ENTRY_IDX) -state disabled
  45. $menu(FILE) entryconfigure $menu(CLOSE_ENTRY_IDX) -state disabled
  46. $menu(EDIT) entryconfigure $menu(UNDO_ENTRY_IDX) -state disabled
  47. $menu(EDIT) entryconfigure $menu(REDO_ENTRY_IDX) -state disabled
  48. $menu(EDIT) entryconfigure $menu(COPY_ENTRY_IDX) -state disabled
  49. $menu(EDIT) entryconfigure $menu(PASTE_ENTRY_IDX) -state disabled
  50. $menu(EDIT) entryconfigure $menu(CUT_ENTRY_IDX) -state disabled
  51. $menu(EDIT) entryconfigure $menu(INDENT_ENTRY_IDX) -state disabled
  52. $menu(EDIT) entryconfigure $menu(UNINDENT_ENTRY_IDX) -state disabled
  53. $menu(EDIT) entryconfigure $menu(FIND_ENTRY_IDX) -state disabled
  54. $menu(EDIT) entryconfigure $menu(GOTO_ENTRY_IDX) -state disabled
  55. ::radxide::main::updateAppTitle
  56. }
  57. #_________________________ closeProject ______________________ #
  58. proc closeProject {} {
  59. # Close all the files
  60. namespace upvar ::radxide dan dan menu menu project project
  61. # var reset
  62. set project(NAME) ""
  63. set project(ROOT) ""
  64. set project(PATH) ""
  65. set project(CUR_FILE_PATH) ""
  66. # ide text reset
  67. set t $dan(TEXT)
  68. $t config -state normal
  69. $t delete 1.0 end
  70. $t insert end ""
  71. $t config -state disabled
  72. ::radxide::win::fillGutter $dan(TEXT) $dan(GUTTEXT) 5 1 "#FFFFFF" "#222223"
  73. # ide treeviee reset
  74. set tree $dan(TREEVIEW)
  75. ::radxide::tree::delete $tree {}
  76. # Update file Menu
  77. $menu(FILE) entryconfigure $menu(ADD_FILE_ENTRY_IDX) -state disabled
  78. $menu(FILE) entryconfigure $menu(SAVE_AS_ENTRY_IDX) -state disabled
  79. $menu(FILE) entryconfigure $menu(SAVE_ENTRY_IDX) -state disabled
  80. $menu(FILE) entryconfigure $menu(CLOSE_ENTRY_IDX) -state disabled
  81. $menu(FILE) entryconfigure $menu(CLOSE_PROJECT_ENTRY_IDX) -state disabled
  82. $menu(EDIT) entryconfigure $menu(UNDO_ENTRY_IDX) -state disabled
  83. $menu(EDIT) entryconfigure $menu(REDO_ENTRY_IDX) -state disabled
  84. $menu(EDIT) entryconfigure $menu(COPY_ENTRY_IDX) -state disabled
  85. $menu(EDIT) entryconfigure $menu(PASTE_ENTRY_IDX) -state disabled
  86. $menu(EDIT) entryconfigure $menu(CUT_ENTRY_IDX) -state disabled
  87. $menu(EDIT) entryconfigure $menu(INDENT_ENTRY_IDX) -state disabled
  88. $menu(EDIT) entryconfigure $menu(UNINDENT_ENTRY_IDX) -state disabled
  89. $menu(EDIT) entryconfigure $menu(FIND_ENTRY_IDX) -state disabled
  90. $menu(EDIT) entryconfigure $menu(GOTO_ENTRY_IDX) -state disabled
  91. ::radxide::main::updateAppTitle
  92. }
  93. # ________________________ createProjectStru _______________________ #
  94. proc createProjectStru {pjroot pjname} {
  95. namespace upvar ::radxide dan dan
  96. set init_txt ""
  97. set init_txt ${init_txt}project_name=$pjname\n
  98. set init_txt ${init_txt}project_root=$pjroot\n
  99. set fh [open $dan(WORKDIR)/$pjname.radx {WRONLY CREAT EXCL}]
  100. puts $fh $init_txt
  101. close $fh
  102. file mkdir $pjroot
  103. file mkdir $pjroot/Private
  104. file mkdir $pjroot/Public
  105. }
  106. # ________________________ newProject _______________________ #
  107. proc newProject {} {
  108. # Create a new project
  109. namespace upvar ::radxide dan dan project project icons icons menu menu
  110. set types {
  111. {{Project Files} {.radx} }
  112. {{All Files} * }
  113. }
  114. set oriprojectpath [tk_getSaveFile -initialdir $dan(WORKDIR) -filetypes $types]
  115. if {$oriprojectpath ne ""} {
  116. # Check: Parent path must equal to Working Dir..
  117. if {[string range $oriprojectpath 0 [expr [string last "/" $oriprojectpath]-1]] ne $dan(WORKDIR)} {
  118. tk_messageBox -title $dan(TITLE) -icon error -message "Project must be inside the Working Dir!"
  119. return
  120. }
  121. # Parsing project name and path ..
  122. set projectpath $oriprojectpath
  123. set projectname [string range $projectpath [expr [string last "/" $projectpath]+1] end]
  124. set projectname [string tolower $projectname]
  125. set projectname [string totitle $projectname]
  126. set projectname [string map {.radx ""} $projectname]
  127. set project(NAME) $projectname
  128. set project(ROOT) $dan(WORKDIR)/$project(NAME)
  129. set projectpath $project(ROOT).radx
  130. set project(PATH) $projectpath
  131. # Check: Project existance..
  132. if {[file exists $oriprojectpath] || [file exists $project(PATH)]} {
  133. tk_messageBox -title $dan(TITLE) -icon error -message "Project already exists!"
  134. return
  135. }
  136. # tk_messageBox -title $dan(TITLE) -icon error -message $project(NAME)
  137. # tk_messageBox -title $dan(TITLE) -icon error -message $project(ROOT)
  138. # tk_messageBox -title $dan(TITLE) -icon error -message $project(PATH)
  139. # Creating Project file structure
  140. createProjectStru $project(ROOT) $project(NAME)
  141. # Update Treeview
  142. #catch {::winfun::clearTree $dan(TREEVIEW) $project(TREE_PROJECT_ROOT)}
  143. #set project(TREE_PROJECT_ROOT) [$dan(TREEVIEW) insert {} 0 -text $project(NAME) -image $icons(PROJECT-ICONI) -open true]
  144. #set project(TREE_PRIVATE_ROOT) [$dan(TREEVIEW) insert $project(TREE_PROJECT_ROOT) 1 -text "Private" -image $icons(PRIVATEF-ICONI)]
  145. #set project(TREE_PUBLIC_ROOT) [$dan(TREEVIEW) insert $project(TREE_PROJECT_ROOT) 2 -text "Public" -image $icons(PUBLICF-ICONI)]
  146. ::radxide::tree::create
  147. # Update file Menu
  148. $menu(FILE) entryconfigure $menu(ADD_FILE_ENTRY_IDX) -state normal
  149. $menu(FILE) entryconfigure $menu(CLOSE_PROJECT_ENTRY_IDX) -state normal
  150. # Update text editor
  151. #$dan(TEXT) configure -state normal
  152. }
  153. }
  154. #_________________________ openProject ______________________ #
  155. proc openProject {} {
  156. # Open an existing project
  157. namespace upvar ::radxide dan dan project project icons icons menu menu
  158. set types {
  159. {{Project Files} {.radx} }
  160. {{All Files} * }
  161. }
  162. set oriprojectpath [tk_getOpenFile -initialdir $dan(WORKDIR) -filetypes $types]
  163. if {$oriprojectpath ne ""} {
  164. # Check: Parent path must equal to Working Dir..
  165. if {[string range $oriprojectpath 0 [expr [string last "/" $oriprojectpath]-1]] ne $dan(WORKDIR)} {
  166. tk_messageBox -title $dan(TITLE) -icon error -message "Project must be inside the Working Dir!"
  167. return
  168. }
  169. # Parsing project name and path ..
  170. set projectpath $oriprojectpath
  171. set projectname [string range $projectpath [expr [string last "/" $projectpath]+1] end]
  172. set projectname [string tolower $projectname]
  173. set projectname [string totitle $projectname]
  174. set projectname [string map {.radx ""} $projectname]
  175. set project(NAME) $projectname
  176. #tk_messageBox -title $dan(TITLE) -icon error -message $projectname
  177. #project_root=/home/pocahontas/.radxwork/Pippo
  178. if [catch {open $oriprojectpath} in] {
  179. tk_messageBox -title $dan(TITLE) -icon error -message "Cannot open $projectname."
  180. return
  181. }
  182. set project(ROOT) ""
  183. while {[gets $in line] >= 0} {
  184. regexp {^project_root\=(.+)} $line x project(ROOT)
  185. if {$project(ROOT) ne ""} {
  186. break
  187. }
  188. }
  189. #tk_messageBox -title $dan(TITLE) -icon error -message $projectname
  190. #tk_messageBox -title $dan(TITLE) -icon error -message $project(ROOT)
  191. #set project(ROOT) [gets $in line]
  192. set projectpath $project(ROOT).radx
  193. set project(PATH) $projectpath
  194. # Update Treeview
  195. ::radxide::tree::create
  196. # Update file Menu
  197. $menu(FILE) entryconfigure $menu(ADD_FILE_ENTRY_IDX) -state normal
  198. $menu(FILE) entryconfigure $menu(CLOSE_PROJECT_ENTRY_IDX) -state normal
  199. # Update text editor
  200. #$dan(TEXT) configure -state normal
  201. }
  202. }
  203. #____________________________ quit ______________________ #
  204. proc quit {} {
  205. # Quit the application
  206. radxide::quit
  207. }
  208. # ________________________ saveFileByName _______________________ #
  209. proc saveFileByName {{withGUI no}} {
  210. namespace upvar ::radxide dan dan project project
  211. #tk_messageBox -title $dan(TITLE) -icon error -message "file=$project(CUR_FILE_PATH)"
  212. if {$withGUI eq yes} {
  213. set initialdir [string range $project(CUR_FILE_PATH) 0 [expr [string last "/" $project(CUR_FILE_PATH)]-1]]
  214. set initialfile [string range $project(CUR_FILE_PATH) [expr [string last "/" $project(CUR_FILE_PATH)]+1] end]
  215. set types {
  216. {{HTML Files} {.html} }
  217. {{PHP Files} {.php} }
  218. {{INC Files} {.inc} }
  219. {{All Files} * }
  220. }
  221. set orifilepath [tk_getSaveFile -initialdir $initialdir -initialfile $initialfile -filetypes $types -defaultextension .php]
  222. if {$orifilepath eq ""} {return}
  223. # Check: Parent path must equal to Working Dir..
  224. if {([string first $project(ROOT)/Private $orifilepath] eq -1) && ([string first $project(ROOT)/Public $orifilepath] eq -1)} {
  225. tk_messageBox -title $dan(TITLE) -icon error -message "File must be saved inside a valid destination in the Working Dir ($project(ROOT)) !"
  226. return
  227. }
  228. set fname $orifilepath
  229. } else {
  230. set fname $project(CUR_FILE_PATH)
  231. }
  232. set t $dan(TEXT)
  233. set stxt [string trim [$t get 1.0 end]]
  234. if {![file writable $fname]} {
  235. tk_messageBox -title $dan(TITLE) -icon error -message "File not writable!"
  236. } else {
  237. ::radxide::filelib::saveFile $fname $stxt
  238. }
  239. }
  240. #_________________________ saveFileAs ______________________ #
  241. proc saveFileAs {} {
  242. # Save the current file as..
  243. namespace upvar ::radxide dan dan
  244. #tk_messageBox -title $dan(TITLE) -icon info -message "Saved as!"
  245. namespace upvar ::radxide project project
  246. if {$project(CUR_FILE_PATH) ne ""} {
  247. saveFileByName yes
  248. }
  249. }
  250. #_________________________ saveFile ______________________ #
  251. proc saveFile {} {
  252. # Save the current file
  253. namespace upvar ::radxide dan dan project project
  254. #tk_messageBox -title $dan(TITLE) -icon info -message "Saved!"
  255. if {$project(CUR_FILE_PATH) ne ""} {
  256. saveFileByName no
  257. }
  258. }
  259. #_________________________#
  260. }
  261. # _________________________________ EOF _________________________________ #