View script | License | Download script | History | Other scripts by: luce80 |
29-Mar 7:32 UTC
[0.04] 30.489k
[0.04] 30.489k
Archive version of: file-requester.r ... version: 9 ... luce80 19-Jun-2013Amendment note: Added file renaming || Publicly available? Yes REBOL [ Title: "Open a file or directory requester" Date: 18-06-2013 Version: 1.3.1 File: %file-requester.r Author: "Marco Antoniazzi" Rights: "Copyright (C) 2012-2013 Marco Antoniazzi" Purpose: "Requests a file or directory" eMail: [luce80 AT libero DOT it] History: [ 0.0.1 [02-09-2012 "First version"] 1.0.0 [08-09-2012 "Finished"] 1.0.1 [09-09-2012 "Minor fixes"] 1.0.2 [21-09-2012 "Sort drop down path tree, update scroller on resize"] 1.0.3 [27-10-2012 "Fixed what-dir for keep, returned full path"] 1.0.4 [03-11-2012 "Minor fixes, return rebol file!"] 1.0.5 [09-06-2013 "Fix external loading of tooltip style"] 1.0.6 [12-06-2013 "Fixed All filter on Linux, hide hidden files on Linux,made styles local"] 1.0.7 [15-06-2013 "Added some key shortcuts, wheel-scroll on drop-list"] 1.2.1 [16-06-2013 "Added multi-selection and sorting (now has same functionality as original)"] 1.2.2 [17-06-2013 "Fixed silly bug of convert string to block of files, default All filter on Linux"] 1.3.1 [18-06-2013 "Added file renaming (press F2)"] ] Note: { Filters can be in the form: "*.a" or "*.a;*.b" or "Short description (*.a)" or "Short description (*.a;*.b)" or a block of such strings. Use arrows to move in list of files. Use F2 to rename currently selected file } Category: [util vid files] library: [ level: 'intermediate platform: 'all type: 'function domain: [gui files] tested-under: [View 2.7.8.3.1 2.7.8.4.3] support: none license: 'LGPL2 see-also: none ] thumbnail: http://i44.tinypic.com/2jajn9c.png comment: "2-Sep-2012 GUI automatically generated by VID_build. Author: Marco Antoniazzi" todo: { - add possibility to not show hidden files (done for Linux) - show dates and sizes - allow searching - display preview } ] file-req: context [ ; files, filter, new folder, drop-list suffix?: func [ {Return the suffix (ext) of a filename or url, else tail of it.} path [any-string!] /local suff ][ either all [ suff: find/last path #"." not find suff #"/" ] [suff] [tail path] ] dir?: func [file [file!]] [#"/" = last file] is-link?: func [file [file!] /local str] [ ; WARNING: THIS IS A DIRTY HACK! if all [ equal? suffix? file %.lnk System/version/4 = 3 ;Win attempt [str: read file] equal? head str find str #{4C000000} ] [str] ] parent-dir: func [file [file!]] [ if equal? length? file 1 [return %/] file: head remove back tail dirize file head remove/part find/tail/last file %/ tail file ] online?: does [not error? try [close open tcp://www.google.com:80]] on-win?: does [system/version/4 = 3] gather-each: func ['word [word!] data [series!] body [block!] /into result [block!]] [;collect result: any [result copy []] forskip data 1 [set word first data if do body [insert tail result first data]] head result ] sort_files: func [files [block!]] [ sort/compare files func [a b] select [ "Name" [a < b] "Date" [(any [modified? a 1-1-0]) < any [modified? b 1-1-0]] "Size" [(any [size? a 0]) < any [size? b 0]] ] get-face choice-sort if "decreasing" = get-face choice-order [files: reverse files] files ] filter_and_sort: func [list [block!] /local temp-list filters par temp-dir-list temp-files-list] [ temp-dir-list: copy list if not get-face check-hidden [remove-each item temp-dir-list [found? find/any/match to-string item ".*"]] remove-each item temp-dir-list [not dir? item] sort_files temp-dir-list dirs-count: length? temp-dir-list if only-dirs [return temp-dir-list] ; apply filters filters: copy get-face choice-filter if par: find filters "(" [filters: next par] if par: find filters ")" [clear par] filters: parse filters ";" temp-list: copy [] filtering-list: copy list forall filters [ gather-each/into item filtering-list [found? find/any/match item first filters] temp-list ] temp-files-list: copy temp-list if not get-face check-hidden [remove-each item temp-files-list [found? find/any/match to-string item ".*"]] remove-each item temp-files-list [dir? item] sort_files temp-files-list clear temp-list insert temp-files-list temp-dir-list temp-files-list ] add_folder: func [/local name][ name: request-text/title/default/offset "Enter the new folder name below" "New folder" btn-+/offset - 240x-46 unless none? name [attempt [make-dir path-name/:name replace_file-list path-name]] ] rename_file: func [/local scroller-pos old-name name] [ if (length? list-of-files/picked) = 1 [ scroller-pos: get-face list-of-files/sld old-name: first get-face list-of-files name: request-text/title/default/offset "Enter the new file name below" form old-name btn-rename/offset - 160x-46 unless none? name [ attempt [ rename old-name to-rebol-file name replace_file-list path-name set-face/no-show list-of-files/sld scroller-pos do-face list-of-files/sld none ] ] focus list-of-files ; to unfocus fields ] ] build_path-tree: func [/local path-tree paths n] [ path-tree: copy [] insert path-tree read %/ remove-each item path-tree [error? try [read/part join %/ item 1]] forall path-tree [change path-tree to-local-file first path-tree] sort path-tree paths: parse/all path-name "/" paths: next next paths n: 0 forall paths [insert/dup first paths " " n: n + 2] if (length? head paths) >= 2 [ insert next find path-tree second head paths paths ] head path-tree ] show_drop-list: func [face [object!]] [ paths-list/data: build_path-tree show paths-list/update show-popup/window/away paths-lay face/parent-face do-events ] select_folder: func [dir [string!] /local path] [ path: find/tail copy path-name dirize trim dir dir: either path [dirize head clear path][to-file rejoin ["/" dir "/"]] if replace_file-list dir [add_to_undo-list] hide-popup ] ; update, enquote, select_line, undo update_list: func [] [ file-list: filter_and_sort orig-file-list clear list-of-files/data clear list-of-files/picked ; update and redraw file names text-list append list-of-files/data file-list set-face list-of-files/sld 0 do-face list-of-files/sld none show list-of-files/update ] replace_file-list: func [dir-name [file! none!] /init /local temp-dir-list current] [ if error? try [temp-dir-list: read dir-name] [request/ok/type/offset "Can not read directory, please verify that the name is correct" 'alert req-win/offset + 100x100 return false] current: what-dir change-dir dir-name focus list-of-files ; to unfocus fields old-path-name: path-name path-name: dir-name clear orig-file-list orig-file-list: copy temp-dir-list update_list set-face info-path to-local-file dir-name unless init [clear-face field-selected] change-dir current true ] enclose: func [s [series!] vals][head insert tail insert copy s first vals last vals] enquote: func [s [series!]][enclose s {"}] ;-- " select_line: func [/key dir [word!] /local path str old-index new-index files] [ if empty? list-of-files/data [return false] if empty? list-of-files/picked [ if find [up down home end] dir [ ; try to select 1st file append clear list-of-files/picked pick list-of-files/data 1 show list-of-files/update if not dir? first list-of-files/picked [set-face field-selected list-of-files/picked] return true ] ] if empty? path: get-face list-of-files [return false] path: last path either none? dir [ case [ dir? path [ if replace_file-list path-name/:path [add_to_undo-list] ] str: is-link? path-name/:path [ ; WARNING: THIS IS A DIRTY HACK! str: back find/any/last str "?:" ; find a: b: c: etc. drive letters str: trim/with/all mold copy/part str find str #{00000000} "{^^@}" ; trim 0s if replace_file-list dirize to-rebol-file str [add_to_undo-list] ] 'else [ files: copy list-of-files/picked if (length? files) > 1 [forall files [change files enquote first files]] set-face field-selected files return false ] ] ][ dir: switch/default dir [ up [-1] down [1] ;page-up [negate visible-lines] ;page-down [visible-lines] home [-10000] ; a great number end [10000] ; a great number ] [return false] new-index: dir + old-index: index? find list-of-files/data first list-of-files/picked new-index: min max 1 new-index length? list-of-files/data if new-index = old-index [return false] append clear list-of-files/picked pick list-of-files/data new-index show list-of-files/update either dir? first list-of-files/picked [clear-face field-selected][set-face field-selected list-of-files/picked] true ] ] add_to_undo-list: does [ insert undo-list old-path-name ] undo: does [ if empty? undo-list [exit] insert redo-list path-name path-name: take undo-list replace_file-list path-name ] redo: does [ if empty? redo-list [exit] insert undo-list path-name path-name: take redo-list replace_file-list path-name ] ; gui load-script-thru: func ["Load a script from www.rebol.org thru the cache" name [file!] /flash "Flash a message to the user" /local cache-name modul ][ cache-name: rejoin [view-root/public/www.rebol.org "/" name] modul: any [ attempt [read cache-name] ; try the cache attempt [read name] ; try current dir attempt [ ; try downloading it from www.rebol.org modul: rejoin [http://www.rebol.org/download-a-script.r?script-name= name] read either flash [request-download/to modul cache-name cache-name][modul] ] ] if modul [clear back back tail modul: load modul] ; remove example code modul ] if not value? 'Add-tooltip-2-faces [; tooltip style already loaded? attempt [do load-script-thru/flash %simple-tooltip-style.r] ] file-req-styles: stylize [ choice: choice white - 20 font [style: none size: 11 colors: [0.0.0 255.150.55] shadow: none] edge [size: 1x1] dir-list: text-list 500x200 with [ update: func [/local tot-rows visible-rows item] [ tot-rows: length? data visible-rows: lc sld/redrag visible-rows / max 1 tot-rows either visible-rows >= tot-rows [ sld/step: 0.0 ][ sld/step: 1 / (tot-rows - visible-rows) if item: find data picked/1 [ sld/data: (index? item) / tot-rows ; simple but it works if sld/data < sld/step [sld/data: 0] sn: to-integer sld/data / sld/step ] ] self ] append init [ sld/action: func [face value] [ ;patched if sn = value: max 0 to-integer value * ((length? slf/data) - lc) [exit] ; I always hated that "1 +" ! sn: value show sub-area ] if not on-win? [iter/size/y: iter/size/y + 1] iter/feel: make iter/feel [ redraw-super: :redraw redraw: func [f a i /local fil] [ redraw-super f a i fil: to-file iter/text iter/font/color: case [ dir? fil [blue] is-link? path-name/:fil [blue] true [black] ] iter/font/style: either dir? fil ['bold][[]] ] combine: func [block [block!] value] [if not find block value [append block value]] picked-num-s: picked-num: old-overed: 0 engage-super: :engage engage: func [face action event /local fil] [ engage-super face action event if event/double-click [ fil: to-file iter/text either dir? fil [ do-face face none ][ if not empty? get-face field-selected [hide-popup] ] ] if all [action = 'down many = true not empty? picked] [ overed: index? find data last picked either event/shift [picked-num-s: overed] [picked-num: overed] if all [event/shift greater? picked-num 0 greater? picked-num-s 0 not-equal? picked-num picked-num-s] [ for n picked-num picked-num-s sign? (picked-num-s - picked-num) [ combine picked pick data n ] do-face slf none ] ] ] ] ] ] ] req-win: [ do [sp: 4x4] origin sp space sp styles file-req-styles Across style text text feel none style btn btn 24x24 btn "<" [undo] help "Back to previous folder" btn ">" [redo] help "Forward" btn "^^" [ if not none? path-name [ replace_file-list parent-dir path-name add_to_undo-list ] ] help "Go up one folder" style field field white white edge [color: gray + 30 effect: 'ibevel size: 1x1] info-path: field 365 [if replace_file-list dirize to-rebol-file face/text [add_to_undo-list]] pad (sp * -1x0) arrow-d: arrow down white 24x24 [show_drop-list face] help "Select a parent directory" btn-+: btn "+" 252.223.44 [add_folder] help "Create a new folder" return text "Sort by" choice-sort: choice "Name" "Date" "Size" 100x20 [replace_file-list path-name] text "in" choice-order: choice "increasing" "decreasing" 100x20 [replace_file-list path-name] text "order" pad 80 btn-rename: btn "Rename" 100x20 252.223.44 [rename_file] help "Rename selected file or folder" return list-of-files: dir-list 500x200 [select_line] return field-selected: field 336 pad 0x2 text-filter: text "Filter:" pad 0x-2 choice-filter: choice "All files (*.*)" "Rebol files (*.r)" 120 [replace_file-list path-name] return pad 0x10 check-hidden: check-line "Show hidden files" no [replace_file-list path-name] pad 172x0 btn-ok: btn "Select" 100x24 #"^M" [cancelled: false hide-popup] btn-cancel: btn "Cancel" 100 #"^(esc)" [set-face field-selected "" cancelled: true hide-popup] key keycode [left] (- sp) [undo] key keycode [right] (- sp) [redo] key keycode [f2] (- sp) [rename_file] do [btn-rename/offset/x: btn-cancel/offset/x] ] ; make styles local info-path: arrow-d: btn-+: list-of-files: field-selected: text-filter: choice-filter: check-hidden: btn-ok: btn-cancel: none req-win: layout req-win if on-win? [remove find req-win/pane check-hidden] remove find req-win/pane list-of-files insert tail req-win/pane list-of-files ; put on top (to hide lower styles) req-win/user-data: reduce ['size req-win/size] req-win/options: [resize min-size 500x306] if value? 'Add-tooltip-2-faces [Add-tooltip-2-faces req-win] paths-lay: layout/offset [ origin 0x0 at 0x0 paths-list: text-list (info-path/size * 1x0 + arrow-d/size * 1x0 + 0x200) [select_folder value] ] info-path/offset + (info-path/size * 0x1) ; patch to avoid closing pop-up with scroll-wheel system/view/popface-feel-win-away: make system/view/popface-feel-win [ process-outside-event: func [event] [ unless find [move time active inactive scroll-line] event/type [hide-popup] event ] ] resize-faces: func [siz [pair!]] [ foreach [face pair] reduce [ arrow-d 1x0 btn-+ 1x0 btn-rename 1x0 field-selected 0x1 text-filter 1x1 choice-filter 1x1 check-hidden 0x1 btn-ok 1x1 btn-cancel 1x1 ][face/offset: face/offset + (siz * pair)] foreach [face pair] reduce [ info-path 1x0 field-selected 1x0 paths-lay 1x0 ][face/size: face/size + (siz * pair)] foreach [face pair] reduce [ list-of-files 1x1 paths-list 1x0 ][face/resize face/size + (siz * pair)] list-of-files/update ] req-win/feel: make req-win/feel [ detect: func [face event /local siz][ switch event/type [ close [set-face field-selected "" cancelled: true hide-popup return none] scroll-line [ either found? find req-win/pane paths-lay [ either event/offset/y >= 0 [scroll-drag/page paths-list/sld] [scroll-drag/page/back paths-list/sld] ][ either event/offset/y >= 0 [scroll-drag list-of-files/sld] [scroll-drag/back list-of-files/sld] ] ] resize [ siz: face/size - face/user-data/size ; compute size difference face/user-data/size: face/size ; store new size resize-faces siz show face ] key [ if all [system/view/focal-face system/view/focal-face/feel = ctx-text/edit] [ ; editing has precedence return event ] if word? event/key [if select_line/key event/key [return none]] if all [system/view/focal-face = list-of-files event/key = #"^M" dir? any [pick list-of-files/picked 1 %a]] [if select_line [return none]] if face: find-key-face face event/key [ if get in face 'action [do-face face event/key] return none ] ] ] event ] ] ; main old-path-name: path-name: none orig-file-list: ; the unsorted file list file-list: [] ; the sorted file list undo-list: copy [] redo-list: copy [] only-dirs: false dirs-count: 0 many: true set 'request_file func [ "Requests a file using a popup list of files and directories." /title "Change heading on request." title-line "Title line of request" button-text "Button text for selection" /file name "Default file name or block of file names" /filter filt "Filter or block of filters" /keep "Keep previous settings and results" /only "Return only a single file, not a block." /path "Return absolute path followed by relative files." /save "Request file for saving, otherwise loading." /local result dir-path ][ req-win/text: any [title-line "Select a File:"] field-selected/text: any [all [name form name] ""] if filt [ filt: compose [(filt)] either on-win? [if not find form filt "*.*" [insert tail filt "All files (*.*)"]][insert tail filt "All files (*)"] choice-filter/text: first choice-filter/data: choice-filter/texts: filt ] btn-ok/text: any [button-text either save ["Save"]["Select"]] if only-dirs [list-of-files/resize list-of-files/size - 0x30] many: either only [false][true] only-dirs: false replace_file-list/init any [path-name path-name: what-dir] show-popup req-win do-events unfocus if none? keep [ ; restore defaults choice-filter/text: first choice-filter/data: choice-filter/texts: ["All files (*.*)" "Rebol files (*.r)"] choice-sort/text: first choice-sort/data: head choice-sort/data choice-order/text: first choice-order/data: head choice-order/data path-name: what-dir ] result: get-face field-selected if "" = trim result [return none] ; convert string to block of files result: parse result none forall result [change result to-rebol-file first result] dir-path: dirize to-rebol-file get-face info-path if only [return join dir-path result/1] either path [ insert result dir-path ][ foreach file result [insert file dir-path] ] head result ] set 'request_dir func [ "Requests a directory using a popup list." /title "Change heading on request." title-line /dir "Set starting directory" where [file!] /keep "Keep previous directory path" /offset xy [pair!] /local offs result ][ req-win/text: any [title-line "Select a directory:"] if offset [offs: req-win/offset req-win/offset: xy] btn-ok/text: "Select" ; restore default value if not only-dirs [list-of-files/resize list-of-files/size + 0x30] only-dirs: true replace_file-list any [where path-name path-name: what-dir] show-popup req-win do-events unfocus result: path-name if none? keep [ ; restore defaults req-win/offset: offs choice-sort/text: first choice-sort/data: head choice-sort/data choice-order/text: first choice-order/data: head choice-order/data path-name: what-dir ] if cancelled [return none] result ] ] do ; just comment this line to avoid executing examples [ probe request_file/keep/title/filter "gimme file" "Load" ["*.r; *.c" "files (*.s ; *.t)"] probe request_dir/title/offset "gimme dir" 100x100 probe request_dir/title/offset "gimme dir" 100x100 probe request_file/path/title "gimme file again" "Get it" halt ] |