-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path_ClickToEditFrame.pas
More file actions
177 lines (145 loc) · 4.98 KB
/
Copy path_ClickToEditFrame.pas
File metadata and controls
177 lines (145 loc) · 4.98 KB
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
unit _ClickToEditFrame;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, Buttons, ExtCtrls, StdCtrls;
type
TClickToEdit = class(TFrame)
protected
_realText: String;
_readOnly: Boolean;
public
published
YesButton: TSpeedButton;
NoButton: TSpeedButton;
TextEdit: TEdit;
TextDisplay: TPanel;
constructor Create(Owner: TComponent); override;
// Custom functions
procedure SetDisplayText(NewValue: String); virtual;
procedure BeginEditting; virtual;
procedure DoneEditting(AcceptText: boolean); virtual;
// Events
procedure TextDisplayClick(Sender: TObject);
procedure TextDisplayMouseEnter(Sender: TObject);
procedure TextDisplayMouseLeave(Sender: TObject);
procedure YesButtonClick(Sender: TObject);
procedure NoButtonClick(Sender: TObject);
procedure TextEditKeyPress(Sender: TObject; var Key: Char);
procedure TextEditExit(Sender: TObject);
property DisplayText: string read _realText write SetDisplayText;
property ReadOnly: Boolean read _readOnly write _readOnly;
end;
implementation
uses Main, UDebug;
{$R *.dfm}
// Set initial state
constructor TClickToEdit.Create(Owner: TComponent);
begin
inherited;
_realText := '';
_readOnly := False;
end;
procedure TClickToEdit.SetDisplayText(NewValue: String);
begin
_realText := NewValue;
if Length(_realText) = 0
then begin
TextDisplay.Caption := '[Click to edit...]';
TextDisplay.Font.Style := [fsItalic];
TextDisplay.Font.Color := $666666;
end
else begin
// "&" does that magic hotkey / underlining thing; the escape seems to be "&&"
TextDisplay.Caption := ' ' + StringReplace(_realText, '&', '&&', [rfReplaceAll]);
TextDisplay.Font.Style := [];
TextDisplay.Font.Color := clBlack;
end;
end;
// Main editting functions
procedure TClickToEdit.BeginEditting;
var
OldEdittingValue: TClickToEdit;
begin
if not ReadOnly then
begin
// Make sure the correct text is in the edit box, and flip the display
TextEdit.Text := _realText;
TextDisplay.Visible := false;
TextEdit.Visible := true;
TextEdit.SetFocus;
// Detect if another input has refused to give up focus!
If MainForm.Editting <> Nil
Then Begin
OldEdittingValue := MainForm.Editting;
// Return focus to the control which is still in edit mode
MainForm.ActiveControl := MainForm.Editting.TextEdit;
// This will have caused a call to DoneEditting, so we need to restore the form's tracking var
MainForm.Editting := OldEdittingValue;
End Else Begin
TextEdit.SelectAll;
// Display the Yes/No buttons; order is important: they stack up from the right
NoButton.Visible := true;
YesButton.Visible := true;
// Warn the main frame that we're in edit mode, and not to interfere
// Doing this last allows other elements to react to us taking focus first
MainForm.Editting := Self;
End;
end;
end;
procedure TClickToEdit.DoneEditting(AcceptText: boolean);
begin
// Read the text out of the edit box (if accepted), and flip the display
If AcceptText
Then DisplayText := TextEdit.Text;
// Swap display for edit box
TextEdit.Visible := false;
TextDisplay.Visible := true;
// Hide the Yes/No buttons
YesButton.Visible := false;
NoButton.Visible := false;
// Tell the main frame we're all done
MainForm.Editting := nil;
end;
// Event Handlers - mostly either accept or reject changes
procedure TClickToEdit.TextEditKeyPress(Sender: TObject; var Key: Char);
begin
case Ord(Key) of
13: // Return
Begin
DoneEditting(true);
Key := #0;
End;
27: // Escape
Begin
DoneEditting(false);
Key := #0;
End;
end;
end;
procedure TClickToEdit.TextDisplayClick(Sender: TObject);
begin
BeginEditting;
end;
procedure TClickToEdit.TextDisplayMouseEnter(Sender: TObject);
begin
If Not ReadOnly Then
TextDisplay.BevelOuter := bvRaised;
end;
procedure TClickToEdit.TextDisplayMouseLeave(Sender: TObject);
begin
TextDisplay.BevelOuter := bvLowered;
end;
procedure TClickToEdit.YesButtonClick(Sender: TObject);
begin
DoneEditting(true);
end;
procedure TClickToEdit.NoButtonClick(Sender: TObject);
begin
DoneEditting(false);
end;
procedure TClickToEdit.TextEditExit(Sender: TObject);
begin
Self.DoneEditting(true);
end;
end.