summaryrefslogtreecommitdiff
path: root/src/gui/fpg_editbtn.pas
blob: 70c6da004aaec85f05076841f4446beff5bd8be9 (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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
{
    fpGUI  -  Free Pascal GUI Toolkit

    Copyright (C) 2006 - 2010 See the file AUTHORS.txt, included in this
    distribution, for details of the copyright.

    See the file COPYING.modifiedLGPL, included in this distribution,
    for details about redistributing fpGUI.

    This program 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.

    Description:
      This unit contains various "composite" components. Components that
      work together as a single component.
}

unit fpg_editbtn;

{$mode objfpc}{$H+}

interface

uses
  Classes
  ,fpg_base
  ,fpg_main
  ,fpg_widget
  ,fpg_edit
  ,fpg_button
  ,fpg_panel
  ;

type
  TfpgBaseEditButton = class(TfpgAbstractPanel)
  private
    FOnButtonClick: TNotifyEvent;
    FReadOnly: Boolean;
    procedure SetReadOnly(const AValue: Boolean);
    function GetExtraHint: TfpgString;
    procedure SetExtraHint(const AValue: TfpgString);
  protected
    FEdit: TfpgEdit;
    FButton: TfpgButton;
    function  GetOnShowHint: THintEvent; override;
    procedure SetOnShowHint(const AValue: THintEvent); override;
    procedure SetHint(const AValue: TfpgString); override;
    function  GetHint: TfpgString; override;
    procedure InternalButtonClick(Sender: TObject); virtual;
    procedure HandleResize(AWidth, AHeight: TfpgCoord); override;
    property  ExtraHint: TfpgString read GetExtraHint write SetExtraHint;
    property  ReadOnly: Boolean read FReadOnly write SetReadOnly default False;
    property  OnButtonClick: TNotifyEvent read FOnButtonClick write FOnButtonClick;
  public
    constructor Create(AOwner: TComponent); override;
  end;


  TfpgFileNameEdit = class(TfpgBaseEditButton)
  private
    FFilter: TfpgString;
    FInitialDir: TfpgString;
    procedure SetFilter(const AValue: TfpgString);
    procedure SetFileName(const AValue: TfpgString);
    function GetFileName: TfpgString;
  protected
    procedure HandlePaint; override;
    procedure InternalButtonClick(Sender: TObject); override;
  public
    constructor Create(AOwner: TComponent); override;
  published
    property ExtraHint;
    property FileName: TfpgString read GetFileName write SetFileName;
    property InitialDir: TfpgString read FInitialDir write FInitialDir;
    property Filter: TfpgString read FFilter write SetFilter;
    property ReadOnly;
    property TabOrder;
    property OnButtonClick;
    property OnShowHint;
  end;


  TfpgDirectoryEdit = class(TfpgBaseEditButton)
  private
    FRootDirectory: TfpgString;
    function GetDirectory: TfpgString;
    procedure SetDirectory(const AValue: TfpgString);
  protected
    procedure HandlePaint; override;
    procedure InternalButtonClick(Sender: TObject); override;
  public
    constructor Create(AOwner: TComponent); override;
  published
    property Directory: TfpgString read GetDirectory write SetDirectory;
    property ExtraHint;
    property RootDirectory: TfpgString read FRootDirectory write FRootDirectory;
    property ReadOnly;
    property TabOrder;
    property OnButtonClick;
    property OnShowHint;
  end;


  TfpgFontEdit = class(TfpgBaseEditButton)
  protected
    function GetFontDesc: TfpgString; virtual;
    procedure SetFontDesc(const AValue: TfpgString); virtual;
    procedure HandlePaint; override;
    procedure InternalButtonClick(Sender: TObject); override;
  public
    constructor Create(AOwner: TComponent); override;
  published
    property FontDesc: TfpgString read GetFontDesc write SetFontDesc;
    property ReadOnly;
    property TabOrder;
    property OnButtonClick;
    property OnShowHint;
  end;


implementation

uses
  fpg_constants
  ,fpg_dialogs
  ,fpg_utils
  ;


{ TfpgBaseEditButton }

procedure TfpgBaseEditButton.SetReadOnly(const AValue: Boolean);
begin
  if FReadOnly = AValue then
    Exit;
  FReadOnly := AValue;
  FEdit.ReadOnly := FReadOnly;
  FButton.Enabled := not FReadOnly;   // Buttons don't have ReadOnly property.
end;

function TfpgBaseEditButton.GetExtraHint: TfpgString;
begin
  Result := FEdit.ExtraHint;
end;

procedure TfpgBaseEditButton.SetExtraHint(const AValue: TfpgString);
begin
  FEdit.ExtraHint := AValue;
end;

function TfpgBaseEditButton.GetOnShowHint: THintEvent;
begin
  // rewire the FEdit event to the parent (composite) component
  Result := FEdit.OnShowHint;
end;

procedure TfpgBaseEditButton.SetOnShowHint(const AValue: THintEvent);
begin
  // rewire the FEdit event to the parent (composite) component
  FEdit.OnShowHint := AValue;
end;

procedure TfpgBaseEditButton.SetHint(const AValue: TfpgString);
begin
  FEdit.Hint := AValue;
end;

function TfpgBaseEditButton.GetHint: TfpgString;
begin
  Result := FEdit.Hint;
end;

procedure TfpgBaseEditButton.InternalButtonClick(Sender: TObject);
begin
  // do nothing
  if Assigned(OnButtonClick) then
    OnButtonClick(self);
end;

procedure TfpgBaseEditButton.HandleResize(AWidth, AHeight: TfpgCoord);
begin
  inherited HandleResize(AWidth, AHeight);
  if csDesigning in ComponentState then
  begin
    FEdit.Visible := False;
    FButton.Visible := False;
  end
  else
  begin
    FEdit.SetPosition(0, 0, AWidth - AHeight, AHeight);
    FButton.SetPosition(AWidth - AHeight, 0, AHeight, AHeight);
  end;
end;

constructor TfpgBaseEditButton.Create(AOwner: TComponent);
begin
  inherited Create(AOwner);
  FWidth          := 140;
  FHeight         := 24;
  FReadOnly       := False;

  FEdit := TfpgEdit.Create(self);
  with FEdit do
  begin
    Name := 'FEdit';
    Text := '';
    FontDesc := '#Edit1';
    TabOrder := 0;
  end;

  FButton := TfpgButton.Create(self);
  with FButton do
  begin
    Name := 'FButton';
    Text := '';
    FontDesc := '#Label1';
    ImageMargin := -1;
    ImageName := 'stdimg.elipses';
    ImageSpacing := 0;
    TabOrder := 1;
    OnClick := @InternalButtonClick;
  end;
end;



{ TfpgFileNameEdit }

constructor TfpgFileNameEdit.Create(AOwner: TComponent);
begin
  inherited Create(AOwner);
  FFilter         := '';
  FButton.ImageName := 'stdimg.folderfile';
end;

procedure TfpgFileNameEdit.SetFilter(const AValue: TfpgString);
begin
  FFilter := AValue;
end;

procedure TfpgFileNameEdit.SetFileName(const AValue: TfpgString);
begin
  FEdit.Text := AValue;
end;

function TfpgFileNameEdit.GetFileName: TfpgString;
begin
  Result := FEdit.Text;
end;

procedure TfpgFileNameEdit.HandlePaint;
var
  img: TfpgImage;
begin
  inherited HandlePaint;
  // only so that it looks pretty in the UI Designer
  if csDesigning in ComponentState then
  begin
    FEdit.Visible := False;
    FButton.Visible := False;
    Canvas.Clear(clBoxColor);
    fpgStyle.DrawControlFrame(Canvas, 0, 0, Width - Height, Height);
    fpgStyle.DrawButtonFace(Canvas, Width - Height, 0, Height, Height, [btfIsEmbedded]);
    Canvas.SetFont(fpgApplication.DefaultFont);
    if Filename <> '' then
    begin
      Canvas.TextColor := clText3;
      Canvas.DrawText(4, 0, Width - Height, Height, Filename, [txtLeft, txtVCenter]);
    end
    else
    begin
      Canvas.TextColor := clShadow1;
      Canvas.DrawText(0, 0, Width - Height, Height, ClassName, [txtHCenter, txtVCenter]);
    end;
    img := fpgImages.GetImage('stdimg.folderfile'); // don't free the img instance - we only got a reference
    if img <> nil then
      Canvas.DrawImage(Width-Height+((Height-img.Width) div 2), (Height-img.Height) div 2, img);
  end;
end;

procedure TfpgFileNameEdit.InternalButtonClick(Sender: TObject);
var
  dlg: TfpgFileDialog;
begin
  dlg := TfpgFileDialog.Create(nil);
  try
    if FileName = '' then
    begin
      if FInitialDir <> '' then
        dlg.InitialDir := FInitialDir;
    end
    else
    begin
      // Use path of existing filename
      dlg.InitialDir := fpgExtractFilePath(FileName);
      if dlg.InitialDir = '' then    // FileName had no path
        dlg.InitialDir := FInitialDir;
    end;
    if FFilter = '' then
      dlg.Filter := rsAllFiles + ' (' + AllFilesMask + ')' + '|' + AllFilesMask
    else
      dlg.Filter :=  FFilter + '|' + rsAllFiles + ' (' + AllFilesMask + ')' + '|' + AllFilesMask;
    if dlg.RunOpenFile then
    begin
      FEdit.Text := dlg.FileName;
    end;
  finally
    dlg.Free;
  end;
  inherited InternalButtonClick(Sender);
end;


{ TfpgDirectoryEdit}

constructor TfpgDirectoryEdit.Create(AOwner: TComponent);
begin
  inherited Create(AOwner);
  FButton.ImageName := 'stdimg.folder';
end;

function TfpgDirectoryEdit.GetDirectory: TfpgString;
begin
  Result := FEdit.Text;
end;

procedure TfpgDirectoryEdit.SetDirectory(const AValue: TfpgString);
begin
  FEdit.Text := AValue;
end;

procedure TfpgDirectoryEdit.HandlePaint;
var
  img: TfpgImage;
begin
  inherited HandlePaint;
  // only so that it looks pretty in the UI Designer
  if csDesigning in ComponentState then
  begin
    FEdit.Visible := False;
    FButton.Visible := False;
    Canvas.Clear(clBoxColor);
    fpgStyle.DrawControlFrame(Canvas, 0, 0, Width - Height, Height);
    fpgStyle.DrawButtonFace(Canvas, Width - Height, 0, Height, Height, [btfIsEmbedded]);
    Canvas.SetFont(fpgApplication.DefaultFont);
    if Directory <> '' then
    begin
      Canvas.TextColor := clText3;
      Canvas.DrawText(4, 0, Width - Height, Height, Directory, [txtLeft, txtVCenter]);
    end
    else
    begin
      Canvas.TextColor := clShadow1;
      Canvas.DrawText(0, 0, Width - Height, Height, ClassName, [txtHCenter, txtVCenter]);
    end;
    img := fpgImages.GetImage('stdimg.folder'); // don't free the img instance - we only got a reference
    if img <> nil then
      Canvas.DrawImage(Width-Height+((Height-img.Width) div 2), (Height-img.Height) div 2, img);
  end;
end;

procedure TfpgDirectoryEdit.InternalButtonClick(Sender: TObject);
var
  dlg: TfpgSelectDirDialog;
begin
  dlg := TfpgSelectDirDialog.Create(nil);
  try
    if FRootDirectory <> '' then
      dlg.RootDirectory := FRootDirectory;
    dlg.SelectedDir := Directory;
    if dlg.ShowModal = mrOK then
    begin
      FEdit.Text:= dlg.SelectedDir;
    end;
  finally
    dlg.Free;
  end;
  inherited InternalButtonClick(Sender);
end;


{ TfpgFontEdit }

function TfpgFontEdit.GetFontDesc: TfpgString;
begin
  Result := FEdit.Text;
end;

procedure TfpgFontEdit.SetFontDesc(const AValue: TfpgString);
begin
  FEdit.Text := AValue;
end;

procedure TfpgFontEdit.HandlePaint;
var
  img: TfpgImage;
begin
  inherited HandlePaint;
  // only so that it looks pretty in the UI Designer
  if csDesigning in ComponentState then
  begin
    FEdit.Visible := False;
    FButton.Visible := False;
    Canvas.Clear(clBoxColor);
    fpgStyle.DrawControlFrame(Canvas, 0, 0, Width - Height, Height);
    fpgStyle.DrawButtonFace(Canvas, Width - Height, 0, Height, Height, [btfIsEmbedded]);
    Canvas.TextColor := clShadow1;
    Canvas.SetFont(fpgApplication.DefaultFont);
    Canvas.DrawText(0, 0, Width - Height, Height, ClassName, [txtHCenter, txtVCenter]);
    img := fpgImages.GetImage('stdimg.font'); // don't free the img instance - we only got a reference
    if img <> nil then
      Canvas.DrawImage(Width-Height+((Height-img.Width) div 2), (Height-img.Height) div 2, img);
  end;
end;

procedure TfpgFontEdit.InternalButtonClick(Sender: TObject);
var
  f: TfpgString;
begin
  f := FontDesc;
  if SelectFontDialog(f) then
    FontDesc := f;
  inherited InternalButtonClick(Sender);
end;

constructor TfpgFontEdit.Create(AOwner: TComponent);
begin
  inherited Create(AOwner);
  FButton.ImageName := 'stdimg.font';
end;


end.