summaryrefslogtreecommitdiff
path: root/examples/gui/filedialog/filedialog.lpr
blob: 315b788590758d9a123fb616030582d24e5d420c (plain)
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
program filedialog;

{$mode objfpc}{$H+}

uses
  {$IFDEF UNIX}{$IFDEF UseCThreads}
  cthreads,
  {$ENDIF}{$ENDIF}
  Classes, SysUtils,
  fpg_base,
  fpg_main,
  fpg_form,
  fpg_dialogs,
  fpg_button,
  fpg_edit,
  fpg_label;


type

  { TMainForm }

  TMainForm = class(TfpgForm)
  private
    {@VFD_HEAD_BEGIN: MainForm}
    btnOpenFile: TfpgButton;
    btnSaveFile: TfpgButton;
    edFilename: TfpgEdit;
    btnQuit: TfpgButton;
    btnName1: TfpgButton;
    btnName2: TfpgButton;
    btnUserPrompt: TfpgButton;
    btnUserInput: TfpgButton;
    {@VFD_HEAD_END: MainForm}
    procedure   btnQuitClick(Sender: TObject);
    procedure   btnOpenFileClick(Sender: TObject);
    procedure   btnSaveFileClick(Sender: TObject);
    procedure   btnMessageBoxClick(Sender: TObject);
    procedure   btnMessageDlgClick(Sender: TObject);
    procedure   btnUserPromptClick(Sender: TObject);
    procedure   btnUserInputClicked(Sender: TObject);
  public
    procedure   AfterCreate; override;
  end;
  

  TMyDBLoginDlg = class(TfpgPromptUserDbDialog)
  private
    function    GetDatabase: TfpgString;
  protected
    procedure   PopulateComboDb; override;
  public
    property    Database: TfpgString read GetDatabase;
  end;

{ TMyDBLoginDlg }

function TMyDBLoginDlg.GetDatabase: TfpgString;
begin
  if cbDatabases.FocusItem = -1 then
    Result := '<nothing selected>'
  else
    Result := aStringList.ValueFromIndex[cbDatabases.FocusItem];
end;

procedure TMyDBLoginDlg.PopulateComboDb;
var
  i: integer;
begin
  aStringList.Clear;
  aStringList.Add('Database1=192.168.0.1:/data/db1.gdb');
  aStringList.Add('Database2=192.168.0.10:/data/db2.gdb');
  aStringList.Add('Database3=192.168.0.150:/data/db3.gdb');
  aStringList.Add('Database4=192.168.0.200:c:\MyData\db4.gdb');
  cbDatabases.Items.Clear;
  for i := 0 to aStringList.Count-1 do
    cbDatabases.Items.Add(aStringList.Names[i]);
end;

{@VFD_NEWFORM_DECL}

{ TMainForm }

procedure TMainForm.btnUserPromptClick(Sender: TObject);
var
  dlg: TMyDBLoginDlg;
begin
  dlg := TMyDBLoginDlg.Create(nil);
  try
    dlg.WindowTitle := 'Sample Login';
    if dlg.ShowModal = mrOK then
    begin
      TfpgMessageDialog.Information('User Results',
          'User=' + dlg.UserID + #13 +
          'Pass=' + dlg.Password +  #13 +
          'Database=' + dlg.Database, [mbOK]);
//      fpgApplication.ProcessMessages;
    end;
  finally
    dlg.Free;
  end;
end;

procedure TMainForm.btnUserInputClicked(Sender: TObject);
var
  lAnswer: TfpgString;
begin
  lAnswer := '';
  if fpgInputQuery('Caption here', 'And the prompt goes here', lAnswer) then
    ShowMessage(Format('User entered <%s>', [lAnswer]));
end;

procedure TMainForm.btnQuitClick(Sender: TObject);
begin
  Close;
end;

procedure TMainForm.btnOpenFileClick(Sender: TObject);
var
  dlg: TfpgFileDialog;
begin
  dlg := TfpgFileDialog.Create(nil);
  try
    // defines 3 filters (All Files, Object Pascal and Lazarus Project)
    dlg.Filter := 'All Files (*)|*|Object Pascal (*.pas;*.lpr;*.pp)|*.pas;*.lpr;*.pp|Lazarus Project (*.lpi)|*.lpi';
    if dlg.RunOpenFile then
      edFilename.Text := dlg.FileName;
  finally
    dlg.Free;
  end;
end;

procedure TMainForm.btnSaveFileClick(Sender: TObject);
var
  dlg: TfpgFileDialog;
begin
  dlg := TfpgFileDialog.Create(nil);
  try
    if edFilename.Text <> '' then
      dlg.Filename := edFilename.Text;
    if dlg.RunSaveFile then
      edFilename.Text := dlg.FileName;
  finally
    dlg.Free;
  end;
end;

procedure TMainForm.btnMessageBoxClick(Sender: TObject);
begin
  ShowMessage('This is some pretty cool stuff');
end;

procedure TMainForm.btnMessageDlgClick(Sender: TObject);
begin
  TfpgMessageDialog.AboutFPGui('My title here');
  TfpgMessageDialog.Critical('Something Critical...', 'And this is where the text goes.', mbAbortRetryIgnore, mbAbort);
  TfpgMessageDialog.Warning('Some Warning...', 'And this is where the text goes.', mbYesNoCancel, mbNo);
  TfpgMessageDialog.Information('Some Information...', 'And this is where the text goes.', [mbOK], mbNoButton);
  TfpgMessageDialog.Question('Some Question...', 'Did everything work okay?', mbYesNo, mbNoButton);
end;

procedure TMainForm.AfterCreate;
begin
  inherited AfterCreate;
  {@VFD_BODY_BEGIN: MainForm}
  Name := 'MainForm';
  SetPosition(330, 199, 419, 138);
  WindowTitle := 'File dialog test';
  Hint := '';
  MinWidth := 300;
  MinHeight := 135;

  btnOpenFile := TfpgButton.Create(self);
  with btnOpenFile do
  begin
    Name := 'btnOpenFile';
    SetPosition(8, 8, 80, 23);
    Text := 'Open File...';
    FontDesc := '#Label1';
    Hint := '';
    ImageName := '';
    TabOrder := 1;
    OnClick := @btnOpenFileClick;
  end;

  btnSaveFile := TfpgButton.Create(self);
  with btnSaveFile do
  begin
    Name := 'btnSaveFile';
    SetPosition(8, 34, 80, 23);
    Text := 'Save File...';
    FontDesc := '#Label1';
    Hint := '';
    ImageName := '';
    TabOrder := 2;
    OnClick := @btnSaveFileClick;
  end;

  edFilename := TfpgEdit.Create(self);
  with edFilename do
  begin
    Name := 'edFilename';
    SetPosition(8, 70, 400, 24);
    Anchors := [anLeft,anRight,anTop];
    ExtraHint := '';
    Hint := '';
    TabOrder := 2;
    Text := '';
    FontDesc := '#Edit1';
  end;

  btnQuit := TfpgButton.Create(self);
  with btnQuit do
  begin
    Name := 'btnQuit';
    SetPosition(329, 107, 80, 23);
    Anchors := [anRight,anBottom];
    Text := 'Quit';
    FontDesc := '#Label1';
    Hint := '';
    ImageName := 'stdimg.Quit';
    TabOrder := 6;
    OnClick := @btnQuitClick;
  end;

  btnName1 := TfpgButton.Create(self);
  with btnName1 do
  begin
    Name := 'btnName1';
    SetPosition(148, 8, 116, 27);
    Text := 'Message Box';
    FontDesc := '#Label1';
    Hint := '';
    ImageName := '';
    TabOrder := 3;
    OnClick := @btnMessageBoxClick;
  end;

  btnName2 := TfpgButton.Create(self);
  with btnName2 do
  begin
    Name := 'btnName2';
    SetPosition(272, 8, 131, 27);
    Text := 'Message Dialog';
    FontDesc := '#Label1';
    Hint := '';
    ImageName := '';
    TabOrder := 4;
    OnClick := @btnMessageDlgClick;
  end;

  btnUserPrompt := TfpgButton.Create(self);
  with btnUserPrompt do
  begin
    Name := 'btnUserPrompt';
    SetPosition(272, 40, 131, 24);
    Text := 'User Prompt';
    FontDesc := '#Label1';
    Hint := '';
    ImageName := '';
    TabOrder := 5;
    OnClick := @btnUserPromptClick;
  end;

  btnUserInput := TfpgButton.Create(self);
  with btnUserInput do
  begin
    Name := 'btnUserInput';
    SetPosition(148, 40, 116, 24);
    Text := 'User Input';
    FontDesc := '#Label1';
    Hint := '';
    ImageName := '';
    TabOrder := 8;
    OnClick :=@btnUserInputClicked;
  end;

  {@VFD_BODY_END: MainForm}
end;

{@VFD_NEWFORM_IMPL}

procedure MainProc;
var
  frm: TMainForm;
begin
  fpgApplication.Initialize;
  frm := TMainForm.Create(nil);
  frm.Show;
  fpgApplication.Run;
  frm.Free;
end;

begin
  MainProc;
end.