-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathxmemo.pas
438 lines (360 loc) · 13.5 KB
/
xmemo.pas
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
(*
Copyright (c) 2018 Steve Wright
This unit is part of the XComponents package.
This package 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.
**********************************************************************
*)
unit XMemo;
{$ifndef JScript}
{$mode objfpc}{$H+}
{$endif}
interface
uses
Classes, SysUtils, TypInfo, NodeUtils, StringUtils,
{$ifndef JScript}
LResources, Forms, Controls, StdCtrls, Graphics, Dialogs, ExtCtrls, Propedits,RTTICtrls,
LazsUtils, Events,
{$else}
HTMLUtils,
{$endif}
WrapperPanel;
type
TXMemo = class(TWrapperPanel)
private
{ Private declarations }
{$ifndef JScript}
fHandleClick:TEventHandler;
fHandleChange:TEventHandler;
procedure MemoClick(Sender:TObject);
procedure MemoChange(Sender: TObject);
{$endif}
procedure SetMyEventTypes;
function GetItemValue:string;
function GetReadOnly:Boolean;
function GetIncludeTextInSave:Boolean;
function GetMemoWidth:string;
function GetMemoHeight:string;
procedure SetItemValue(AValue:string);
procedure SetReadOnly(AValue:Boolean);
procedure SetIncludeTextInSave(AValue:Boolean);
procedure SetMemoWidth(AValue:string);
procedure SetMemoHeight(AValue:string);
protected
{ Protected declarations }
// procedure LinkLoadFromProperty(Sender: TObject); override;
// procedure LinkSaveToProperty(Sender: TObject); override;
{$ifndef JScript}
procedure DoConstructor(TheOwner:TComponent;IsDynamic:Boolean);
property ParentColor;
{$endif}
public
{ Public declarations }
{$ifndef JScript}
constructor Create(TheOwner: TComponent); override;
constructor Create(TheOwner: TComponent;IsDynamic:Boolean); override;
{$else}
constructor Create(MyForm:TForm;NodeName,NameSpace:String);
{$endif}
published
{ Published declarations }
// Properties defined for this class...
property ItemValue: String read GetItemValue write SetItemValue;
property ReadOnly: Boolean read GetReadOnly write SetReadOnly;
property IncludeTextInSave: Boolean read GetIncludeTextInSave write SetIncludeTextInSave;
property MemoHeight: String read GetMemoHeight write SetMemoHeight;
property MemoWidth: String read GetMemoWidth write SetMemoWidth;
{$ifndef JScript}
// Events to be visible in Lazarus IDE
property HandleClick: TEventHandler read FHandleClick write FHandleClick;
property HandleChange: TEventHandler read FHandleChange write FHandleChange;
{$endif}
end;
{$ifndef JScript}
procedure Register;
{$endif}
implementation
const MyNodeType='TXMemo';
var
myDefaultAttribs:TDefaultAttributesArray;
procedure TXMemo.SetMyEventTypes;
begin
MyEventTypes.Add('Click');
MyEventTypes.Add('Change');
{$ifdef JScript}
MyEventTypes.Add('MemoPaste');
{$endif}
end;
{$ifndef JScript}
procedure Register;
begin
{$I xmemo_icon.lrs}
RegisterComponents('XComponents',[TXMemo]);
// inherited from TWrapperPanel, not required here
RegisterPropertyEditor(TypeInfo(TColor), TXMemo, 'BgColor', THiddenPropertyEditor);
// suppress some of the Link properties
// RegisterPropertyEditor(TypeInfo(TAliasStrings), TXPropertyLink, 'AliasValues', THiddenPropertyEditor);
// RegisterPropertyEditor(TypeInfo(String), TXPropertyLink, 'TIElementName', THiddenPropertyEditor);
// RegisterPropertyEditor(TypeInfo(TPropertyLinkOptions), TXPropertyLink, 'Options', THiddenPropertyEditor);
end;
constructor TXMemo.Create(TheOwner:TComponent);
begin
inherited Create(TheOwner,false);
DoConstructor(TheOwner,false);
end;
constructor TXMemo.Create(TheOwner:TComponent;IsDynamic:Boolean);
begin
inherited Create(TheOwner,IsDynamic);
DoConstructor(TheOwner,IsDynamic);
end;
procedure TXMemo.DoConstructor(TheOwner:TComponent;IsDynamic:Boolean);
begin
self.BorderSpacing.Around:=glbBorderSpacing;
myControl:=TMemo.Create(self);
myControl.Parent:=self;
myControl.SetSubComponent(true); // Tell the IDE to store the modified properties
// Make sure the embedded component can not be selected/deleted within the IDE
myControl.ControlStyle := myControl.ControlStyle - [csNoDesignSelectable];
TMemo(myControl).OnEditingDone:[email protected];
TMemo(myControl).OnExit:[email protected];
myControl.OnClick:[email protected];
TMemo(myControl).WordWrap:=true;
TMemo(myControl).ScrollBars:=ssVertical;
self.SetMyEventTypes;
CreateComponentDataNode2(self,MyNodeType,myDefaultAttribs, self.myEventTypes, TheOwner,IsDynamic);
self.ParentColor:=true;
// Setting IsContainer false will prevent designer dropping new child controls into this one.
self.IsContainer:=false;
AddLabel(myControl);
end;
function CreateWidget(ParentNode:TDataNode;ScreenObjectName,NameSpace:string;position:integer;Alignment:String):TDataNode;
var
NewNode:TDataNode;
begin
NewNode:=CreateDynamicLazWidget('TXMemo',ParentNode.MyForm,ParentNode,ScreenObjectName,NameSpace,Alignment,position);
result:=NewNode;
end;
procedure TXMemo.MemoClick(Sender: TObject) ;
begin
if not (csDesigning in componentState) then
CallHandleEvent('Click',self.myNode.NodeName,self);
end;
procedure TXMemo.MemoChange(Sender: TObject) ;
var
Memo: TMemo ;
begin
Memo := TMemo(sender) ;
self.ItemValue:=Memo.text;
CallHandleEvent('Change',Memo.text,Sender);
end;
//procedure TXMemo.LinkLoadFromProperty(Sender: TObject);
//begin
// if Sender=nil then ;
// if (Link.Editor=nil) then exit;
// inherited LinkLoadFromProperty(Sender);
//
// self.ItemValue:=Link.GetAsText;
//
//end;
//
//procedure TXMemo.LinkSaveToProperty(Sender: TObject);
//begin
// if Sender=nil then ;
// if Link.Editor=nil then exit;
// Link.SetAsText(TMemo(myControl).Text);
//
//end;
procedure TXMemo.SetMemoWidth(AValue:string);
var
tc:TControl;
begin
tc:=self.myControl;
myNode.SetAttributeValue('MemoWidth',AValue);
SetHeightWidth(self.myNode,tc,'MemoWidth','MemoHeight');
tc.Update;
tc.Invalidate;
end;
procedure TXMemo.SetMemoHeight(AValue:string);
var
tc:TControl;
begin
tc:=self.myControl;
myNode.SetAttributeValue('MemoHeight',AValue);
SetHeightWidth(self.myNode,tc,'MemoWidth','MemoHeight');
tc.Update;
tc.Invalidate;
end;
{$else}
constructor TXMemo.Create(MyForm:TForm;NodeName,NameSpace:String);
begin
inherited Create(NodeName,NameSpace);
self.NodeType:=MyNodeType;
self.MyForm:=MyForm;
self.SetMyEventTypes;
self.IsContainer:=false;
SetNodePropDefaults(self,myDefaultAttribs);
end;
function CreateWidget(MyNode, ParentNode:TDataNode;ScreenObjectName,NameSpace:string;position:integer;Alignment:String):TDataNode;
var
LabelText:string;
ReadOnly:Boolean;
OnChangeString, OnClickString, OnPasteString:String;
begin
LabelText:= MyNode.getAttribute('LabelText',true).AttribValue;
ReadOnly:= StrToBool(MyNode.getAttribute('ReadOnly',true).AttribValue);
OnClickString:='onclick="event.stopPropagation();pas.Events.handleEvent(null,''Click'','''+ScreenObjectName+''','''+NameSpace+''', this.value);" ';
OnChangeString:= 'onchange="pas.NodeUtils.SetInterfaceProperty('''+ScreenObjectName+''','''+NameSpace+''',''ItemValue'',this.value); '+
'pas.Events.handleEvent(null,''Change'','''+ScreenObjectName+''','''+NameSpace+''', this.value, ''ItemValue'');" ';
OnPasteString:= 'onpaste="var pdata=(event.clipboardData || window.clipboardData).getData(''text'');' +
'pas.Events.handleEvent(null,''MemoPaste'','''+ScreenObjectName+''','''+NameSpace+''', pdata);" ';
asm
try{
var wrapper = pas.HTMLUtils.CreateWrapperDiv(MyNode,ParentNode,'UI',ScreenObjectName,NameSpace,$impl.MyNodeType,position);
var HTMLString='';
var NodeIDString = "'"+ScreenObjectName+"'";
var wrapperid = NameSpace+ScreenObjectName;
var MyObjectName=wrapperid+'Contents';
var ReadOnlyString = '';
if (ReadOnly==true) { ReadOnlyString = ' readonly ';}
var Pastetypestring="'MemoPaste'";
var labelstring='<label for="'+MyObjectName+'" id="'+MyObjectName+'Lbl'+'">'+LabelText+'</label>';
var MemoString ='<textarea id='+MyObjectName+' '+
OnPasteString +
OnClickString +
OnChangeString +
' class="widgetinner '+wrapperid+'" ' +
' style="display:inline-block; padding:1px; height:100%; width:100%;" >'+
'</textarea> ';
HTMLString = MemoString+labelstring;
var wrapper=document.getElementById(wrapperid);
wrapper.insertAdjacentHTML('beforeend', HTMLString);
}
catch(err) { alert(err.message+' in XMemo.CreateXMemo');}
end;
MyNode.ScreenObject:=MyNode;
RefreshComponentProps(myNode);
result:=myNode;
end;
function CreateinterfaceObj(MyForm:TForm;NodeName,NameSpace:String):TObject;
begin
result:=TObject(TXMemo.Create(MyForm,NodeName,NameSpace));
end;
//procedure TXMemo.LinkLoadFromProperty(Sender: TObject);
//begin
// inherited LinkLoadFromProperty(Sender);
//end;
//
//procedure TXMemo.LinkSaveToProperty(Sender: TObject);
//begin
// if Sender=nil then ;
// if Link=nil then exit;
// if Link.TIObject=nil then exit;
//// showmessage('linksavetoproperty. '+Link.TIPropertyName+' '+self.ItemValue);
//
// SetStringProp(Link.TIObject,Link.TIPropertyName,self.ItemValue);
//end;
procedure TXMemo.SetMemoWidth(AValue:string);
begin
//showmessage('memo width='+AValue);
myNode.SetAttributeValue('MemoWidth',AValue);
asm
var ob = document.getElementById(this.NameSpace+this.NodeName);
// if (ob==null) {alert(this.NodeName+' not found');}
pas.HTMLUtils.SetHeightWidthHTML(this,ob,'W',AValue);
end;
end;
procedure TXMemo.SetMemoHeight(AValue:string);
begin
myNode.SetAttributeValue('MemoHeight',AValue);
asm
var ob = document.getElementById(this.NameSpace+this.NodeName);
pas.HTMLUtils.SetHeightWidthHTML(this,ob,'H',AValue);
end;
end;
{$endif}
function TXMemo.GetItemValue:string;
begin
result:=MyNode.getAttribute('ItemValue',true).AttribValue;
end;
function TXMemo.GetReadOnly:Boolean;
begin
result:=MyStrToBool(MyNode.getAttribute('ReadOnly',true).AttribValue);
end;
function TXMemo.GetIncludeTextInSave:Boolean;
begin
result:=MyStrToBool(MyNode.getAttribute('IncludeTextInSave',true).AttribValue);
end;
function TXMemo.GetMemoHeight:string;
begin
result:=MyNode.getAttribute('MemoHeight',true).AttribValue;
end;
function TXMemo.GetMemoWidth:string;
begin
result:=MyNode.getAttribute('MemoWidth',true).AttribValue;
end;
procedure TXMemo.SetItemValue(AValue:string);
var
l:integer;
begin
myNode.SetAttributeValue('ItemValue',AValue);
{$ifndef JScript}
l:=length(AValue);
if l > 1000000 then
TMemo(myControl).Text:='<<.....text too long to edit here....>>'
else
TMemo(myControl).Text:=AValue; //!!!! hanging up when text is very long...
{$else}
asm
var ob = document.getElementById(this.NameSpace+this.NodeName+'Contents');
if (ob!=null) {
ob.value=AValue; }
end;
//LinkSaveToProperty(self);
{$endif}
end;
procedure TXMemo.SetReadOnly(AValue:Boolean);
begin
myNode.SetAttributeValue('ReadOnly',myBoolToStr(AValue),'Boolean');
{$ifndef JScript}
TMemo(myControl).ReadOnly:=AValue;
{$else}
asm
var ob = document.getElementById(this.NameSpace+this.NodeName+'Contents');
if (ob!=null) {
ob.readOnly = AValue }
end;
{$endif}
end;
procedure TXMemo.SetIncludeTextInSave(AValue:Boolean);
begin
myNode.SetAttributeValue('IncludeTextInSave',myBoolToStr(AValue),'Boolean');
end;
begin
// this is the set of node attributes that each TXMemo instance will have.
AddWrapperDefaultAttribs(myDefaultAttribs);
AddDefaultAttribute(myDefaultAttribs,'ContainerWidth','String','','',false);
AddDefaultAttribute(myDefaultAttribs,'ContainerHeight','String','','',false);
AddDefaultAttribute(myDefaultAttribs,'MemoWidth','String','400','',false);
AddDefaultAttribute(myDefaultAttribs,'MemoHeight','String','200','',false);
AddDefaultAttribute(myDefaultAttribs,'Border','Boolean','True','',false);
AddDefaultAttribute(myDefaultAttribs,'SpacingAround','Integer','0','',false);
AddDefaultAttribute(myDefaultAttribs,'LabelPos','String','Top','',false);
AddDefaultAttribute(myDefaultAttribs,'LabelText','String','Memo Box','',false);
AddDefaultAttribute(myDefaultAttribs,'ReadOnly','Boolean','False','',false);
AddDefaultAttribute(myDefaultAttribs,'ItemValue','String','....text....','',false);
AddDefaultAttribute(myDefaultAttribs,'IncludeTextInSave','Boolean','True','If false, the memo contents will be excluded from saved system data',false);
AddDefaultsToTable(MyNodeType,myDefaultAttribs);
AddExclusionAttribToTable(MyNodeType,'IncludeTextInSave','ItemValue');
AddAttribOptions(MyNodeType,'Alignment',AlignmentOptions);
AddAttribOptions(MyNodeType,'LabelPos',LabelPosOptions);
{$ifndef JScript}
RegisterClass(TXMemo);
AddNodeFuncLookup(MyNodeType,@CreateWidget);
{$else}
AddNodeFuncLookup(MyNodeType,@CreateinterfaceObj,@CreateWidget);
{$endif}
SuppressDesignerProperty('TXMemo','BgColor');
SuppressDesignerProperty(MyNodeType,'ContainerHeight');
SuppressDesignerProperty(MyNodeType,'ContainerWidth');
end.