File: frmexample.pp

package info (click to toggle)
lazarus 0.9.28.2-12
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 93,096 kB
  • ctags: 89,312
  • sloc: pascal: 820,189; xml: 202,194; makefile: 110,323; sh: 2,460; perl: 395; sql: 174; ansic: 133
file content (124 lines) | stat: -rw-r--r-- 3,665 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
{
 ***************************************************************************
 *                                                                         *
 *   This source is free software; you can redistribute it and/or modify   *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This code is distributed in the hope that it will be useful, but      *
 *   WITHOUT ANY WARRANTY; without even the implied warranty of            *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU     *
 *   General Public License for more details.                              *
 *                                                                         *
 *   A copy of the GNU General Public License is available on the World    *
 *   Wide Web at <http://www.gnu.org/copyleft/gpl.html>. You can also      *
 *   obtain it by writing to the Free Software Foundation,                 *
 *   Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.        *
 *                                                                         *
 ***************************************************************************

  Author: Michael Van Canneyt
}
unit FrmExample;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, FileUtil, LResources, Forms, Controls, Graphics, Dialogs,
  Buttons, EditBtn, StdCtrls;

type

  { TExampleForm }

  TExampleForm = class(TForm)
    BOK: TButton;
    BCancel: TButton;
    EFileName: TFileNameEdit;
    LEFileName: TLabel;
    procedure ExampleFormCloseQuery(Sender: TObject; var CanClose: boolean);
  private
    function GetExampleDir: String;
    function GetExampleName: String;
    procedure SetExampleDir(const AValue: String);
    procedure SetExampleName(const AValue: String);
    function CheckFilePath(const AValue: String): boolean;
    { private declarations }
  public
    { public declarations }
    Property ExampleName : String Read GetExampleName Write SetExampleName;
    Property ExampleDir: String read GetExampleDir write SetExampleDir;
  end; 

var
  ExampleForm: TExampleForm;

implementation

{ TExampleForm }


{ TExampleForm }

procedure TExampleForm.ExampleFormCloseQuery(Sender: TObject;
  var CanClose: boolean);
var
  S: String;
begin
  if Sender = nil then exit;
  if ModalResult = mrOk then begin
    S := ExtractFilePath(EFilename.Text);
    CanClose := CheckFilePath(S);
  end;
end;

function TExampleForm.GetExampleDir: String;
begin
  Result := EFileName.InitialDir;
end;

function TExampleForm.GetExampleName: String;
begin
  Result:=EFilename.Text; //EFileName.FileName;
  if ExampleDir<>'' then
    Result := ExtractRelativePath(ExampleDir, Result);
end;

procedure TExampleForm.SetExampleDir(const AValue: String);
begin
  EFileName.InitialDir := AValue;
end;

procedure TExampleForm.SetExampleName(const AValue: String);
begin
  EFileName.FileName:=AValue;
end;

function TExampleForm.CheckFilePath(const AValue: String): boolean;
begin
  Result := True;
  
  // Check Empty Path and filename
  if (Length(ExtractFilePath(AValue))=0) and FileExistsUTF8(ExampleDir+AValue) then
    exit;
  
  // Check partial file path within ExampleDir
  if FileExistsUTF8(ExampleDir + AValue) then
    exit;
  
  // it might be a full path
  if FileExistsUTF8(AValue) and (Pos(ExampleDir, AValue)<>0) then
    exit;

  Result := false;
  ShowMessage('Invalid file or path');
end;

initialization
  {$I frmexample.lrs}

end.