Results 1 to 4 of 4

Thread: Simba File Opener Dialog

  1. #1
    Join Date
    Jan 2007
    Posts
    8,876
    Mentioned
    123 Post(s)
    Quoted
    327 Post(s)

    Default Simba File Opener Dialog

    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.



    Example script:
    Simba Code:
    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;

    Simba Code:
    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.
    Last edited by Zyt3x; 04-30-2015 at 06:39 PM. Reason: added pic from @rj;

  2. #2
    Join Date
    Sep 2010
    Posts
    5,762
    Mentioned
    136 Post(s)
    Quoted
    2739 Post(s)

    Default

    Cool!


  3. #3
    Join Date
    Nov 2011
    Location
    England
    Posts
    3,072
    Mentioned
    296 Post(s)
    Quoted
    1094 Post(s)

    Default

    Did you know there is such a thing as TOpenDialog.. :x

  4. #4
    Join Date
    Jan 2007
    Posts
    8,876
    Mentioned
    123 Post(s)
    Quoted
    327 Post(s)

    Default

    Quote Originally Posted by Olly View Post
    Did you know there is such a thing as TOpenDialog.. :x
    Now I do!
    Oh well..

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •