PDA

View Full Version : Simba File Opener Dialog



Zyt3x
04-30-2015, 05:31 PM
This snippet will let you open a open file dialog so the user can select a file.
The scripter can specify multiple extensions as well as the initial path to search in.

http://i.imgur.com/RmjA0Ga.png

Example script:
var
F : Integer;
S : String;

begin
writeLn('path: ', FileOpener.select(['simba', 'sex'], includePath));
if ((F := FileOpener.open(False)) > -1) then
begin
readFileString(F, S, fileSize(F));
closeFile(F);
end;
writeLn(S);
end;

type
TFileOpener = packed record
main : TForm;
dirLabel : TLabel;
dirEdit : TEdit;
upButton : TButton;
items : TListBox;
fileLabel : TLabel;
fileEdit : TEdit;
openButton, cancelButton : TButton;
extensions : TStringArray;
path : String;
end;

var
FileOpener : TFileOpener;

procedure _foRefreshItems(const fileExt : TStringArray; const dir : String);
var
I, J : Integer;
sArr : TStringArray;
begin
with FileOpener do
begin
dirEdit.setText(dir);
items.getItems().clear();

sArr := getDirectories(dir);
for I := 0 to high(sArr) do
items.getItems().add(sArr[I] + '\');
for J := 0 to high(fileExt) do
begin
sArr := getFiles(dir, fileExt[J]);
for I := 0 to high(sArr) do
items.getItems().add(sArr[I]);
end;
end;
end;

procedure _foOnSelect(const Sender : TObject); native;
begin
with FileOpener do
begin
case Sender of
items:
if items.getSelCount > 0 then
if items.getSelectedText()[length(items.getSelectedText())] = '\' then
begin
path += items.getSelectedText();
_foRefreshItems(extensions, path);
end else
fileEdit.setText(items.getSelectedText());
end;
end;
end;

procedure _foOnClick(const Sender : TObject); native;
var
sArr : TStringArray;
begin
with FileOpener do
begin
case Sender of
openButton:
if fileExists(path + fileEdit.getText()) then
begin
path += fileEdit.getText();
main.close();
end;
cancelButton:
begin
path := '';
main.close();
end;
upButton:
begin
sArr := multiBetween('\' + path + '\', '\', '\');
delete(sArr, high(sArr));
path := implode('\', sArr) + '\';
if path = '\' then path := 'C:\';
_foRefreshItems(extensions, path);
end;
end;
end;
end;

procedure _foInit(); native;
begin
with FileOpener do
begin
main.Init(nil);
with main do
begin
setCaption('Open');
setPosition(poScreenCenter);
setBorderStyle(bsDialog);
setWidth(400);
setHeight(250);
end;

dirLabel.Init(main);
with dirLabel do
begin
setParent(main);
setLeft(10);
setTop(10);
setCaption('Look in:');
end;

dirEdit.Init(main);
with dirEdit do
begin
setParent(main);
setBounds(60, 7, 275, 20);
setText(path);
setReadOnly(True);
end;

upButton.Init(main);
with upButton do
begin
setParent(main);
setBounds(360, 5, 25, 25);
setOnClick(_foOnClick);
setCaption('up');
end;

items.Init(main);
with items do
begin
setParent(main);
setBounds(10, 35, 380, 150);
setOnClick(_foOnSelect);
end;

fileLabel.Init(main);
with fileLabel do
begin
setParent(main);
setLeft(10);
setTop(200);
setCaption('File name:');
end;

fileEdit.Init(main);
with fileEdit do
begin
setParent(main);
setBounds(75, 197, 220, 20);
setText('');
setReadOnly(True);
end;

openButton.Init(main);
with openButton do
begin
setParent(main);
setCaption('Open');
setBounds(320, 195, 70, 25);
setOnClick(_foOnClick);
end;

cancelButton.Init(main);
with cancelButton do
begin
setParent(main);
setCaption('Cancel');
setBounds(320, 222, 70, 25);
setOnClick(_foOnClick);
end;

_foRefreshItems(extensions, path);

main.showModal();
main.free();
end;
end;

function TFileOpener.select(const fileExt : TStringArray; const initialPath : String): String;
begin
self.extensions := fileExt;
self.path := initialPath;
try
sync(_foInit);
except
writeLn('Could not initialize FileOpener');
finally
result := self.path;
end;
end;

function TFileOpener.open(const Shared : Boolean): Integer;
begin
result := -1;
if fileExists(self.path) then
result := openFile(path, Shared);
end;

function TFileOpener.rewrite(const Shared : Boolean): Integer;
begin
result := -1;
if fileExists(self.path) then
result := rewriteFile(path, Shared);
end;

Currently if you press up enough times, it's search path default to "C:\". If anyone cba to fix this then please post :)

EDIT: There are no failsafes, if the path doesn't end with a '\' or the extensions array is empty, then the dialog won't work.

rj
04-30-2015, 05:33 PM
Cool!

http://i.imgur.com/RmjA0Ga.png

Olly
04-30-2015, 05:46 PM
Did you know there is such a thing as TOpenDialog.. :x

Zyt3x
04-30-2015, 05:52 PM
Did you know there is such a thing as TOpenDialog.. :xNow I do! ;)
Oh well..