summaryrefslogtreecommitdiff
path: root/src/gui/fpg_editbtn.pas
blob: 6c871806a50be7bae451a955464dcf3ef51f8188 (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
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
{
    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;

  TFilenameSetEvent = procedure(Sender: TObject; const AOldValue, ANewValue: TfpgString) of object;

  TfpgFileNameEdit = class(TfpgBaseEditButton)
  private
    FOnFilenameSet: TFilenameSetEvent;
    FFilter: TfpgString;
    FInitialDir: TfpgString;
    procedure SetFilter(const AValue: TfpgString);
    procedure SetFileName(const AValue: TfpgString);
    function GetFileName: TfpgString;
    procedure DoFilenameSet(const AOld, ANew: TfpgString);
  protected
    procedure HandlePaint; override;
    procedure InternalButtonClick(Sender: TObject); override;
  public
    constructor Create(AOwner: TComponent); override;
  published
    property    Align;
    property    Enabled;
    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;
    property    OnFilenameSet: TFilenameSetEvent read FOnFilenameSet write FOnFilenameSet;
  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    Align;
    property    Directory: TfpgString read GetDirectory write SetDirectory;
    property    Enabled;
    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    Align;
    property    Enabled;
    property    ExtraHint;
    property    FontDesc: TfpgString read GetFontDesc write SetFontDesc;
    property    ReadOnly;
    property    TabOrder;
    property    OnButtonClick;
    property    OnShowHint;
  end;


  TfpgEditButton =  class(TfpgBaseEditButton)
  protected
    function    GetText: TfpgString;
    procedure   SetText(const AValue: TfpgString);
    procedure   HandlePaint; override;
  public
    constructor Create(AOwner: TComponent); override;
  published
    property    Align;
    property    Enabled;
    property    ExtraHint;
    property    ReadOnly;
    property    TabOrder;
    property    Text: TfpgString read GetText write SetText;
    property    OnButtonClick;
    property    OnShowHint;
  end;


implementation

uses
  fpg_constants
  ,fpg_dialogs
  ,fpg_utils
  ;

{ TfpgEditButton }

function TfpgEditButton.GetText: TfpgString;
begin
  Result := FEdit.Text;
end;

procedure TfpgEditButton.SetText(const AValue: TfpgString);
begin
  FEdit.Text := AValue;
end;

procedure TfpgEditButton.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(fpgStyle.DefaultFont);
    if Text <> '' then
    begin
      Canvas.TextColor := clText3;
      Canvas.DrawText(4, 0, Width - Height, Height, Text, [txtLeft, txtVCenter]);
    end
    else
    begin
      Canvas.TextColor := clShadow1;
      Canvas.DrawText(0, 0, Width - Height, Height, ClassName, [txtHCenter, txtVCenter]);
    end;
    img := fpgImages.GetImage('stdimg.ellipse'); // 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;

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


{ 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);
  { resizing can now occur before the component is shown, so we need extra
    checks here, like are we still busy creating everything. }
  if not (csLoading in ComponentState) then
  begin
    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;
end;

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

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

  FButton := TfpgButton.Create(self);
  with FButton do
  begin
    Name := 'FButton';
    SetPosition(self.Width - self.Height, 0, self.Height, self.Height);
    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.DoFilenameSet(const AOld, ANew: TfpgString);
begin
  if Assigned(FOnFilenameSet) then
    FOnFilenameSet(self, AOld, ANew);
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(fpgStyle.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;
  old: TfpgString;
begin
  old := FEdit.Text;
  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);
  if old <> FEdit.Text then
    DoFilenameSet(old, FEdit.Text);
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(fpgStyle.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(fpgStyle.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.