summaryrefslogtreecommitdiff
path: root/examples/apps/uidesigner/vfddesigner.pas
blob: e2517e07e10203b922ca0dd10c1776c406411b49 (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
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
{
    fpGUI  -  Free Pascal GUI Library

    Copyright (C) 2006 - 2007 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:
      A lot of the main code is here.
}


unit vfddesigner;

{$mode objfpc}{$H+}

interface

uses
  Classes,
  SysUtils,
  gfxbase,
  fpgfx,
  gfx_widget,
  gui_form,
  gui_dialogs,
  gui_label,
  gui_edit,
  gui_button,
  gui_listbox,
  gui_memo,
  gui_combobox,
  gui_checkbox,
  vfdresizer,
  vfdforms,
  vfdeditors,
  vfdwidgetclass,
  vfdwidgets,
  newformdesigner;

type

  TOtherWidget = class(TfpgWidget)
  protected
    FFont: TfpgFont;
    FBackgroundColor: TfpgColor;
    procedure HandlePaint; override;
  public
    wgClassName: string;
    constructor Create(AOwner: TComponent); override;
  end;


  TFormDesigner = class;


  TDesignedForm = class(TfpgForm)
  public
    procedure AfterCreate; override;
  end;


  TWidgetDesigner = class(TObject)
  private
    procedure SetSelected(const AValue: boolean);
  public
    FFormDesigner: TFormDesigner;
    FWidget: TfpgWidget;
    FVFDClass: TVFDWidgetClass;
    FSelected: boolean;
    resizer: array[1..8] of TwgResizer;
    other: TStringList;
    constructor Create(AFormDesigner: TFormDesigner; wg: TfpgWidget; wgc: TVFDWidgetClass);
    destructor  Destroy; override;
    property    Selected: boolean read FSelected write SetSelected;
    property    Widget: TfpgWidget read FWidget;
    procedure   UpdateResizerPositions;
    property    FormDesigner: TFormDesigner read FFormDesigner;
  end;


  TFormDesigner = class(TObject)
  protected
    FWidgets: TList;
    FForm: TDesignedForm;
    FFormOther: string;
    FDragging: boolean;
    FDragPosX,
    FDragPosY: TfpgCoord;
    FWasDrag: boolean;
  protected
    // messages of the designed widgets
    procedure MsgMouseDown(var msg: TfpgMessageRec); message FPGM_MOUSEDOWN;
    procedure MsgMouseUp(var msg: TfpgMessageRec); message FPGM_MOUSEUP;
    procedure MsgMouseMove(var msg: TfpgMessageRec); message FPGM_MOUSEMOVE;
    procedure MsgKeyPress(var msg: TfpgMessageRec); message FPGM_KEYPRESS;
    procedure MsgMove(var msg: TfpgMessageRec); message FPGM_MOVE;
    procedure MsgResize(var msg: TfpgMessageRec); message FPGM_RESIZE;
    procedure MsgActivate(var msg: TfpgMessageRec); message FPGM_ACTIVATE;
  public
    OneClickMove: boolean; // the widgets can be selected and dragged within one click
    constructor Create;
    destructor Destroy; override;
    procedure ClearForm;
    procedure DefaultHandler(var msg); override;
    procedure Show;
    procedure InitTest;
    function AddWidget(wg: TfpgWidget; wgc: TVFDWidgetClass): TWidgetDesigner;
    function WidgetDesigner(wg: TfpgWidget): TWidgetDesigner;
    function FindWidgetByName(const wgname: string): TfpgWidget;
    procedure DeSelectAll;
    procedure SelectAll;
    procedure SelectNextWidget(fw: boolean);
    procedure MoveResizeWidgets(dx, dy, dw, dh: integer);
    procedure DeleteWidgets;
    procedure EditWidgetOrder;
    procedure DesignerKeyPress(var keycode: word; var shiftstate: TShiftState; var consumed: boolean);
    procedure PutControlByName(x, y: integer; cname: string);
    procedure InsertWidget(pwg: TfpgWidget; x, y: integer; wgc: TVFDWidgetClass);
    procedure OnPaletteChange(Sender: TObject);
    procedure UpdatePropWin;
    procedure OnPropTextChange(Sender: TObject);
    procedure OnPropNameChange(Sender: TObject);
    procedure OnPropPosEdit(Sender: TObject);
    procedure OnOtherChange(Sender: TObject);
    procedure OnAnchorChange(Sender: TObject);
    procedure OnEditWidget(Sender: TObject);
    function GenerateNewName(namebase: string): string;
    procedure RunWidgetEditor(wgd: TWidgetDesigner; wg: TfpgWidget);
  public
    function GetFormSourceDecl: string;
    function GetFormSourceImpl: string;
    function GetWidgetSourceImpl(wd: TWidgetDesigner; ident: string): string;
    property Form: TDesignedForm read FForm;
    property FormOther: string read FFormOther write FFormOther;
  end;


implementation

uses
  vfdmain;

{ TWidgetDesigner }

procedure TWidgetDesigner.SetSelected(const AValue: boolean);
var
  n: integer;
begin
  if FSelected = AValue then
    Exit;
  FSelected := AValue;

  if FSelected then
    Widget.MouseCursor := mcMove
  else
    Widget.MouseCursor := mcDefault;

  for n := 1 to 8 do
    if FSelected then
      resizer[n] := TwgResizer.Create(self, n)
    else
    begin
      if resizer[n] <> nil then
        resizer[n].Free;
      resizer[n] := nil;
    end;

  UpdateResizerPositions;

  if FSelected and (Widget.Parent.WinHandle > 0) then
    for n := 1 to 8 do
      resizer[n].Show;
end;

constructor TWidgetDesigner.Create(AFormDesigner: TFormDesigner; wg: TfpgWidget; wgc: TVFDWidgetClass);
var
  n: integer;
begin
  FFormDesigner := AFormDesigner;
  FWidget       := wg;
  FVFDClass     := wgc;
  for n := 1 to 8 do
    resizer[n] := nil;
  FSelected := False;
  wg.MouseCursor := mcDefault;
  other := TStringList.Create;
end;

destructor TWidgetDesigner.Destroy;
var
  n: integer;
begin
  for n := 1 to 8 do
    if resizer[n] <> nil then
      resizer[n].Free;
  other.Free;
  inherited Destroy;
end;

procedure TWidgetDesigner.UpdateResizerPositions;
var
  n: integer;
  rs: TwgResizer;
begin
  if not FSelected then
    Exit;

  for n := 1 to 8 do
  begin
    rs := resizer[n];

    if rs <> nil then
    begin
      case n of
        1:
        begin
          rs.left := Widget.left - 2;
          rs.Top  := Widget.Top - 2;
        end;
        2:
        begin
          rs.Top  := Widget.Top - 2;
          rs.left := Widget.left + Widget.Width div 2 - 2;
        end;
        3:
        begin
          rs.Top  := Widget.Top - 2;
          rs.left := Widget.left + Widget.Width - 1 - 2;
        end;
        4:
        begin
          rs.Top  := Widget.Top + Widget.Height div 2 - 2;
          rs.left := Widget.left + Widget.Width - 1 - 2;
        end;
        5:
        begin
          rs.Top  := Widget.Top + Widget.Height - 1 - 2;
          rs.left := Widget.left + Widget.Width - 1 - 2;
        end;
        6:
        begin
          rs.Top  := Widget.Top + Widget.Height - 1 - 2;
          rs.left := Widget.left + Widget.Width div 2 - 2;
        end;
        7:
        begin
          rs.Top  := Widget.Top + Widget.Height - 1 - 2;
          rs.left := Widget.left - 2;
        end;
        8:
        begin
          rs.Top  := Widget.Top + Widget.Height div 2 - 2;
          rs.left := Widget.left - 2;
        end;
      end; // case
      if rs.HasHandle then
        rs.UpdateWindowPosition;
    end;
  end;

end;

{ TFormDesigner }

procedure TFormDesigner.MsgMouseDown(var msg: TfpgMessageRec);
var
  wgd: TWidgetDesigner;
  shift: boolean;
begin
  writeln('TFormDesigner.MsgMouseDown');
  FDragging := True;
  FWasDrag  := False;
  FDragPosX := msg.Params.mouse.x;
  FDragPosy := msg.Params.mouse.y;

  if msg.dest = FForm then
    Exit;

  wgd := WidgetDesigner(TfpgWidget(msg.dest));
  if wgd = nil then
    Exit;

  if not OneClickMove then
    Exit; // this Exit disables one click move

  shift := (ssShift in msg.Params.mouse.shiftstate);

  if shift then
    Exit;

  if not wgd.Selected then
  begin
    DeSelectAll;
    wgd.Selected := True;
    UpdatePropWin;
  end;
end;

procedure TFormDesigner.MsgMouseUp(var msg: TfpgMessageRec);
var
  wgd: TWidgetDesigner;
  wgc: TVFDWidgetClass;
  pwg: TfpgWidget;
  shift: boolean;
  x, y: integer;
begin
writeln('TFormDesigner.MsgMouseUp');
  FDragging := False;

  shift := (ssShift in msg.Params.mouse.shiftstate);

  wgc := frmMain.SelectedWidget;
  pwg := TfpgWidget(msg.dest);
  wgd := WidgetDesigner(TfpgWidget(msg.dest));
  if wgd = nil then
    pwg := FForm
  else if not wgd.FVFDClass.Container then
    wgc := nil;

  if wgc <> nil then
  begin
    DeSelectAll;

    if wgc <> nil then
    begin
      x := msg.Params.mouse.x;
      y := msg.Params.mouse.y;

      if GridResolution > 1 then
      begin
        x := x - x mod GridResolution;
        y := y - y mod GridResolution;
      end;

      InsertWidget(pwg, x, y, wgc);

      if not shift then
      begin
        FForm.MouseCursor      := mcDefault;
        frmMain.SelectedWidget := nil;
      end;
    end;
  end
  else
  begin
    wgd := WidgetDesigner(TfpgWidget(msg.dest));
    if wgd = nil then
    begin
      DeSelectAll;
      UpdatePropWin;
      Exit;
    end;

    if not shift then
    begin
      if not wgd.Selected then
        DeSelectAll;
      wgd.Selected := True;
    end
    else
      wgd.Selected := not wgd.Selected;
  end;

  UpdatePropWin;
end;

procedure TFormDesigner.MsgMouseMove(var msg: TfpgMessageRec);
var
  dx, dy: integer;
  wgd: TWidgetDesigner;
begin
  if not FDragging then
    Exit;

  FWasDrag := True;

  dx := msg.Params.mouse.x - FDragPosX;
  dy := msg.Params.mouse.y - FDragPosY;

  wgd := WidgetDesigner(TfpgWidget(msg.dest));
  if (wgd = nil) or (not wgd.Selected) then
    Exit;

  if GridResolution > 1 then
  begin
    dx := dx - (dx mod GridResolution);
    dy := dy - (dy mod GridResolution);
  end;

  MoveResizeWidgets(dx, dy, 0, 0);
end;

procedure TFormDesigner.MsgKeyPress(var msg: TfpgMessageRec);
var
  key: word;
  ss: TShiftState;
  consumed: boolean;
begin
  key := msg.params.keyboard.keycode;
  ss  := msg.params.keyboard.shiftstate;

  consumed := False;

  DesignerKeyPress(key, ss, consumed);
end;

procedure TFormDesigner.MsgMove(var msg: TfpgMessageRec);
begin
  if msg.dest = FForm then
    UpdatePropWin;
end;

procedure TFormDesigner.MsgResize(var msg: TfpgMessageRec);
begin
  if msg.dest = FForm then
  begin
    DeSelectAll; // because of the anchorings
    UpdatePropWin;
  end;
end;

constructor TFormDesigner.Create;
begin
  FWidgets := TList.Create;
  FWasDrag := False;

  OneClickMove := True; //false;

  FForm      := TDesignedForm.Create(nil);
  FForm.FormDesigner := self;
  FForm.Name := maindsgn.NewFormName;
  FForm.WindowTitle := FForm.Name;
  FFormOther := '';
end;

destructor TFormDesigner.Destroy;
var
  n: integer;
begin
  for n := 0 to FWidgets.Count - 1 do
    TObject(FWidgets.Items[n]).Free;
  FWidgets.Free;

  if FForm <> nil then
    FForm.Free;
  inherited Destroy;
end;

procedure TFormDesigner.ClearForm;
var
  n: integer;
begin
  for n := 0 to FWidgets.Count - 1 do
  begin
    TWidgetDesigner(FWidgets.Items[n]).Widget.Free;
    TObject(FWidgets.Items[n]).Free;
  end;
  FWidgets.Clear;
end;

procedure TFormDesigner.DefaultHandler(var msg);
begin
  //Writeln('Designer message: ',TMessageRec(msg).msgcode,' from ',TMessageRec(msg).dest.ClassName);
end;

procedure TFormDesigner.Show;
begin
  FForm.Show;
  UpdatePropWin;
end;

procedure TFormDesigner.InitTest;
var
  l1: TfpgLabel;
  ed1: TfpgEdit;
begin
//  Exit;
  writeln('InitTest');
  l1  := CreateLabel(FForm, 10, 10, 'Test Label');
  ed1 := CreateEdit(FForm, 10, 50, 150, 0);

  AddWidget(l1, nil);
  AddWidget(ed1, nil);
end;

function TFormDesigner.AddWidget(wg: TfpgWidget; wgc: TVFDWidgetClass): TWidgetDesigner;
var
  cd: TWidgetDesigner;
begin
  cd     := TWidgetDesigner.Create(self, wg, wgc);
  FWidgets.Add(cd);
  //cd.Selected := true;
  if wg is TDesignedForm then
    TDesignedForm(wg).FormDesigner := self;
  Result := cd;
end;

function TFormDesigner.WidgetDesigner(wg: TfpgWidget): TWidgetDesigner;
var
  n: integer;
  cd: TWidgetDesigner;
begin
  Result := nil;
  for n := 0 to FWidgets.Count - 1 do
  begin
    cd := TWidgetDesigner(FWidgets.Items[n]);
    if cd.Widget = wg then
    begin
      Result := cd;
      Exit;
    end;
  end;
end;

procedure TFormDesigner.DeSelectAll;
var
  n: integer;
  cd: TWidgetDesigner;
begin
  for n := 0 to FWidgets.Count - 1 do
  begin
    cd          := TWidgetDesigner(FWidgets.Items[n]);
    cd.Selected := False;
  end;
end;

procedure TFormDesigner.SelectAll;
var
  n: integer;
  cd: TWidgetDesigner;
begin
  for n := 0 to FWidgets.Count - 1 do
  begin
    cd          := TWidgetDesigner(FWidgets.Items[n]);
    cd.Selected := True;
  end;
end;

procedure TFormDesigner.SelectNextWidget(fw: boolean);
var
  n, dir: integer;
  cd, scd: TWidgetDesigner;
begin
  if FWidgets.Count < 1 then
    Exit;

  if fw then
  begin
    n   := 0;
    dir := 1;
  end
  else
  begin
    dir := -1;
    n   := FWidgets.Count - 1;
  end;

  scd := TWidgetDesigner(FWidgets.Items[n]);

  while (n >= 0) and (n < FWidgets.Count) do
  begin
    cd := TWidgetDesigner(FWidgets.Items[n]);
    if cd.Selected then
    begin
      if fw then
      begin
        if n < FWidgets.Count - 1 then
          scd := TWidgetDesigner(FWidgets.Items[n + 1]);
      end
      else if n > 0 then
        scd := TWidgetDesigner(FWidgets.Items[n - 1]);
      break;
    end;
    n := n + dir;
  end;
  DeSelectAll;
  scd.Selected := True;
  UpdatePropWin;
end;

procedure TFormDesigner.MoveResizeWidgets(dx, dy, dw, dh: integer);
var
  n: integer;
  cd: TWidgetDesigner;
begin
  for n := 0 to FWidgets.Count - 1 do
  begin
    cd := TWidgetDesigner(FWidgets.Items[n]);
    if cd.Selected then
    begin
      if GridResolution > 1 then
      ;
      cd.Widget.MoveAndResizeBy(dx, dy, dw, dh);
      cd.UpdateResizerPositions;
    end;
  end;
  UpdatePropWin;
end;

procedure TFormDesigner.DeleteWidgets;
var
  n: integer;
  cd: TWidgetDesigner;
begin
  n := 0;
  while n < FWidgets.Count do
  begin
    cd := TWidgetDesigner(FWidgets.Items[n]);
    if cd.Selected then
    begin
      cd.Widget.Free;
      cd.Free;
      FWidgets.Delete(n);
    end
    else
      Inc(n);
  end;
end;


procedure TFormDesigner.EditWidgetOrder;
var
  frm: TWidgetOrderForm;
  n, fi: integer;
  cd: TWidgetDesigner;
  identlevel: integer;

  procedure AddChildWidgets(pwg: TfpgWidget; slist: TStrings);
  var
    f: integer;
    fcd: TWidgetDesigner;
  begin
    for f := 0 to FWidgets.Count - 1 do
    begin
      fcd := TWidgetDesigner(FWidgets.Items[f]);

      if fcd.Widget.Parent = pwg then
      begin
        frm.list.Items.AddObject(StringOfChar(' ', identlevel) + fcd.Widget.Name + ' : ' + fcd.Widget.ClassName, fcd);
        Inc(identlevel);
        AddChildWidgets(fcd.Widget, slist);
        Dec(identlevel);
      end;

      if fcd.Selected then
        fi := f + 1;
    end;
  end;

begin
  frm := TWidgetOrderForm.Create(nil);
  fi  := 1;

  identlevel := 0;

  AddChildWidgets(FForm, frm.list.Items);

  if fi <= frm.list.ItemCount then
    frm.list.FocusItem := fi;

  if frm.ShowModal = 1 then
  begin
    for n := 0 to FWidgets.Count - 1 do
      TWidgetDesigner(FWidgets.Items[n]).Widget.Visible := False;

    for n := 0 to FWidgets.Count - 1 do
      FWidgets.Items[n] := frm.List.Items.Objects[n];

    for n := 0 to FWidgets.Count - 1 do
    begin
      cd := TWidgetDesigner(FWidgets.Items[n]);
      cd.Widget.Visible := True;
    end;

    for n := 0 to FWidgets.Count - 1 do
    begin
      cd := TWidgetDesigner(FWidgets.Items[n]);
      if cd.Selected then
      begin
        // re-creating the resizers
        cd.Selected := False;
        cd.Selected := True;
      end;
    end;

  end;
  frm.Free;
end;

procedure TFormDesigner.DesignerKeyPress(var keycode: word; var shiftstate: TShiftState; var consumed: boolean);
var
  dx,
  dy: integer;
begin
  dx       := 0;
  dy       := 0;
  consumed := True;

  case keycode of
    keyLeft:    dx := -1;
    keyRight:   dx := +1;
    keyUp:      dy := -1;
    keyDown:    dy := +1;

    keyDelete: DeleteWidgets;

    keyTab: SelectNextWidget(True);
//    KEY_STAB: SelectNextWidget(False);

    keyF1: ShowMessage('ENTER, F11: switch to Properties' + LineEnding +
        'TAB, SHIFT+TAB: select next widget' + LineEnding +
        'F2: edit widget order' + LineEnding +
        'F4: edit items' + LineEnding
        , 'Small help');

    keyF2: EditWidgetOrder;

    //KEY_F4: if PropertyForm.btnEdit.Visible then PropertyForm.btnEdit.Click;

    keyF11,
    keyEnter:
      frmProperties.SetFocus;
//      GfxActivateWindow(frmProperties.WinHandle);
    else
      consumed := False;
  end;

  if (dx <> 0) or (dy <> 0) then
    if (ssShift in shiftstate) then
      MoveResizeWidgets(0, 0, dx, dy)
    else
      MoveResizeWidgets(dx, dy, 0, 0);
end;

procedure TFormDesigner.PutControlByName(x, y: integer; cname: string);
var
  wg: TfpgWidget;
  wgd: TWidgetDesigner;
  wgcname: string;
  newname: string;
  cfrm: TInsertCustomForm;
begin
  wgcname := UpperCase(cname);

  Writeln('Putting control: ', wgcname);

  wg      := nil;
  newname := 'Widget';
  if wgcname = 'LABEL' then
  begin
    newname := GenerateNewName('lbLabel');
    wg      := CreateLabel(FForm, x, y, newname);
  end
  else if wgcname = 'EDIT' then
  begin
    newname := GenerateNewName('edEdit');
    wg      := CreateEdit(FForm, x, y, 150, 0);
    TfpgEdit(wg).Text := newname;
  end
  else if wgcname = 'BUTTON' then
  begin
    newname := GenerateNewName('btnButton');
    wg      := CreateButton(FForm, x, y, 105, newname, nil);
  end
  else if wgcname = 'CHECKBOX' then
  begin
    newname := GenerateNewName('cbCheck');
    wg      := CreateCheckBox(FForm, x, y, newname);
  end
  else if wgcname = 'MEMO' then
  begin
    newname := GenerateNewName('edMemo');
    wg      := TfpgMemo.Create(FForm);
    wg.SetPosition(x, y, 120, 80);
    TfpgMemo(wg).Text := newname;
  end
  else if wgcname = 'TEXTLISTBOX' then
  begin
    newname := GenerateNewName('lstList');
    wg      := TfpgListBox.Create(FForm);
    wg.SetPosition(x, y, 160, 200);
    TfpgListBox(wg).Items.Add(newname);
  end
  else if wgcname = 'CHOICELIST' then
  begin
    newname := GenerateNewName('cbChoice');
    wg      := CreateComboBox(FForm, x, y, 150, nil);
    TfpgComboBox(wg).Items.Add(newname);
  end
  else
  begin
    // custom component
    cfrm := TInsertCustomForm.Create(nil);
    if cfrm.ShowModal = 1 then
    begin
      newname := cfrm.edName.Text;
      if newname = '' then
        newname := GenerateNewName(cfrm.edClass.Text);
      wg := TOtherWidget.Create(FForm);
      TOtherWidget(wg).wgClassName := cfrm.edClass.Text;
      wg.SetPosition(x, y, 200, 24);
    end;
    cfrm.Free;

  end;

  if wg <> nil then
  begin
    wg.Name     := newname;
    wgd         := AddWidget(wg, nil);
    wg.Visible  := True;
    DeSelectAll;
    wgd.Selected := True;
    UpdatePropWin;
  end;
end;

procedure TFormDesigner.OnPaletteChange(Sender: TObject);
begin
  if PaletteForm.clist.FocusItem > 1 then
    FForm.MouseCursor := mcCross
  else
    FForm.MouseCursor := mcDefault;
end;

procedure TFormDesigner.UpdatePropWin;
var
  n, i: integer;
  cd, scd: TWidgetDesigner;
  wg: TfpgWidget;

  wgcnt: integer;
  //btxt  : boolean;
  //bedit : boolean;

  lastpropname: string;

  wgc: TVFDWidgetClass;
begin
  wgcnt := 0;
  wg    := FForm;
  wgc   := VFDFormWidget;
  scd   := nil;
  for n := 0 to FWidgets.Count - 1 do
  begin
    cd := TWidgetDesigner(FWidgets.Items[n]);
    if cd.Selected then
    begin
      Inc(wgcnt);
      if wgcnt < 2 then
      begin
        wg  := cd.Widget;
        scd := cd;
      end;
    end;
  end;

  if scd <> nil then
    wgc := scd.FVFDClass;

  n := frmProperties.lstProps.FocusItem;
  if (n > 0) and (PropList.GetItem(n) <> nil) then
    lastpropname := PropList.GetItem(n).Name
  else
    lastpropname := '';

  i := 0;

  if PropList.Widget <> wg then
  begin
    frmProperties.lstProps.ReleaseEditor;
    PropList.Clear;
    for n := 1 to wgc.PropertyCount do
    begin
      PropList.AddItem(wgc.GetProperty(n));
      if UpperCase(wgc.GetProperty(n).Name) = UpperCase(lastPropName) then
        i := n;
    end;
    PropList.Widget := wg;
    frmProperties.lstProps.Update;
    if i > 0 then
      frmProperties.lstProps.FocusItem := i;
  end;

  with frmProperties do
  begin
    if wg is TOtherWidget then
      lbClass.Text := TOtherWidget(wg).wgClassName
    else
      lbClass.Text := wg.ClassName;
    edName.Text := wg.Name;

    if scd <> nil then
      edOther.Text := scd.other.Text
    else
      edOther.Text := FFormOther;

    edName.Visible  := (wgcnt < 2);
    edOther.Visible := (wgcnt < 2);

    lstProps.Update;
  end;

  with frmProperties do
  begin
    btnLeft.Text    := IntToStr(wg.Left);
    btnTop.Text     := IntToStr(wg.Top);
    btnWidth.Text   := IntToStr(wg.Width);
    btnHeight.Text  := IntToStr(wg.Height);

    btnAnLeft.Down   := anLeft in wg.Anchors;
    btnAnTop.Down    := anTop in wg.Anchors;
    btnAnRight.Down  := anRight in wg.Anchors;
    btnAnBottom.Down := anBottom in wg.Anchors;
  end;

  Exit;
  {
  with PropertyForm do
  begin
    if wg is TOtherWidget then lbClass.Text8 := TOtherWidget(wg).wgClassName
                          else lbClass.Text8 := wg.ClassName;

    edName.Text8 := wg.Name;

    lbLeft.Text8 := IntToStr(wg.Left);
    lbTop.Text8 := IntToStr(wg.Top);
    lbWidth.Text8 := IntToStr(wg.Width);
    lbHeight.Text8 := IntToStr(wg.Height);

    cbAL.Checked := anLeft in wg.Anchors;
    cbAT.Checked := anTop in wg.Anchors;
    cbAR.Checked := anRight in wg.Anchors;
    cbAB.Checked := anBottom in wg.Anchors;

    btxt  := true;
    bedit := false;
    lbText.Text := u8('Text:');

    if wg is TGfxForm then edText.Text := TGfxForm(wg).WindowTitle
    else if wg is TwgLabel then edText.Text := TwgLabel(wg).Text
    else if wg is TwgEdit  then edText.Text := TwgEdit(wg).Text
    else if wg is TwgButton then edText.Text := TwgButton(wg).Text
    else if wg is TwgCheckBox then edText.Text := TwgCheckBox(wg).Text
    else btxt := false;

    if not btxt and ((wg is TwgMemo) or (wg is TwgChoiceList) or (wg is TwgTextListBox))
    then
    begin
      bedit := true;
      lbText.Text := u8('Items:');
      btnEdit.Text := u8('Edit items...');
    end
    else if (wg is TwgDBGrid) then
    begin
      bedit := true;
      //lbText.Text := u8('Items:');
      btnEdit.Text := u8('Edit columns...');
    end;

    if scd <> nil then
    begin
      edOther.Text := str8to16(scd.other.Text);
    end
    else
    begin
      edOther.Text := str8to16(FFormOther);
    end;

    edText.Visible := btxt;
    lbText.Visible := btxt;
    btnEdit.Visible := bedit;
    edName.Visible := (wgcnt < 2);
    edOther.Visible := (wgcnt < 2);

  end; // with PropertyForm
}
end;

procedure TFormDesigner.OnPropTextChange(Sender: TObject);
{
var
  n : integer;
  cd : TWidgetDesigner;
  wg : TWidget;
  s : string16;
}
begin
  {
  s := PropertyForm.edText.Text;
  wg := nil;
  for n:=0 to FWidgets.Count-1 do
  begin
    cd := TWidgetDesigner(FWidgets.Items[n]);
    if cd.Selected then
    begin
      wg := cd.Widget;
      SetWidgetText(wg,s);
      if wg is TwgLabel then
      with TwgLabel(wg) do
      begin
        if Font.TextWidth16(Text) > width then
        begin
          Width := Font.TextWidth16(Text);
          UpdateWindowPosition;
          cd.UpdateResizerPositions;
        end;
      end;
    end;
  end;

  if wg = nil then
  begin
    FForm.WindowTitle := s;
  end;
}
end;

procedure TFormDesigner.OnPropNameChange(Sender: TObject);
var
  n: integer;
  cd: TWidgetDesigner;
  wg: TfpgWidget;
  s: string;
begin
  //  writeln('namechange');
  s := frmProperties.edName.Text;
  wg := nil;
  for n := 0 to FWidgets.Count - 1 do
  begin
    cd := TWidgetDesigner(FWidgets.Items[n]);
    if cd.Selected then
      wg := cd.Widget;
{
      if GetWidgetText(wg,s) and (wg.Name = str16to8(s)) then
      begin
        PropertyForm.edText.Text8 := s8;
        OnPropTextChange(sender);
      end;
}
  end;

  if wg = nil then
    wg := FForm{
    if FForm.Name = FForm.WindowTitle8 then
    begin
      FForm.WindowTitle8 := s8;
      PropertyForm.edText.Text8 := s8;
    end;
};

  try
    wg.Name := s;
  except
    // invalid name...
  end;
end;

procedure TFormDesigner.OnPropPosEdit(Sender: TObject);
var
  frm: TEditPositionForm;
  btn: TfpgButton;
  ax,
  ay: TfpgCoord;
  wg: TfpgWidget;
  n: integer;
  cd: TWidgetDesigner;
  posval: integer;
  pt: TPoint;

  procedure SetNewPos(awg: TfpgWidget; pval: integer);
  begin
    if Sender = frmProperties.btnLeft then
      awg.Left   := pval
    else if Sender = frmProperties.btnTop then
      awg.Top    := pval
    else if Sender = frmProperties.btnWidth then
      awg.Width  := pval
    else if Sender = frmProperties.btnHeight then
      awg.Height := pval;
  end;

begin
  wg := nil;
  for n := 0 to FWidgets.Count - 1 do
  begin
    cd := TWidgetDesigner(FWidgets.Items[n]);
    if cd.Selected then
    begin
      wg := cd.Widget;
      break;
    end;
  end;

  if wg = nil then
    wg := Form;

  btn      := TfpgButton(Sender);
  frm      := TEditPositionForm.Create(nil);
  btn.WindowToScreen(btn, pt);
//  GfxGetAbsolutePosition(btn.WinHandle, btn.Width, 0, ax, ay);
  ax := pt.X;
  ay := pt.Y;
  frm.Left := ax;
  frm.Top  := ay;
  if Sender = frmProperties.btnLeft then
  begin
    frm.lbPos.Text := 'Left:';
    frm.edPos.Text := IntToStr(wg.Left);
  end
  else if Sender = frmProperties.btnTop then
  begin
    frm.lbPos.Text := 'Top:';
    frm.edPos.Text := IntToStr(wg.Top);
  end
  else if Sender = frmProperties.btnWidth then
  begin
    frm.lbPos.Text := 'Width:';
    frm.edPos.Text := IntToStr(wg.Width);
  end
  else if Sender = frmProperties.btnHeight then
  begin
    frm.lbPos.Text := 'Height:';
    frm.edPos.Text := IntToStr(wg.Height);
  end;

  posval := -9999;
  if frm.ShowModal = 1 then
    posval := StrToIntDef(frm.edPos.Text, -9999);
  frm.Free;

  if posval > -999 then
  begin
    wg := nil;
    for n := 0 to FWidgets.Count - 1 do
    begin
      cd := TWidgetDesigner(FWidgets.Items[n]);
      if cd.Selected then
      begin
        wg := cd.Widget;
        SetNewPos(wg, posval);
        wg.UpdateWindowPosition;
        cd.UpdateResizerPositions;
      end;
    end;

    if wg = nil then
    begin
      SetNewPos(FForm, posval);
      FForm.UpdateWindowPosition;
    end;

  end;

  UpdatePropWin;
end;

procedure TFormDesigner.OnOtherChange(Sender: TObject);
var
  n: integer;
  cd: TWidgetDesigner;
  s: string;
  sc: integer;
begin
  sc := 0;
  s  := frmProperties.edOther.Text;
  for n := 0 to FWidgets.Count - 1 do
  begin
    cd := TWidgetDesigner(FWidgets.Items[n]);
    if cd.Selected then
    begin
      cd.other.Text := s;
      Inc(sc);
    end;
  end;

  if sc < 1 then
    FFormOther := s;
end;

procedure TFormDesigner.OnAnchorChange(Sender: TObject);
var
  n: integer;
  cd: TWidgetDesigner;
  wg: TfpgWidget;
begin
  for n := 0 to FWidgets.Count - 1 do
  begin
    cd := TWidgetDesigner(FWidgets.Items[n]);
    if cd.Selected then
    begin
      wg := cd.Widget;

      wg.Anchors := [];
      if frmProperties.btnAnLeft.Down then
        wg.Anchors := wg.Anchors + [anLeft];
      if frmProperties.btnAnTop.Down then
        wg.Anchors := wg.Anchors + [anTop];
      if frmProperties.btnAnRight.Down then
        wg.Anchors := wg.Anchors + [anRight];
      if frmProperties.btnAnBottom.Down then
        wg.Anchors := wg.Anchors + [anBottom];
    end;
  end;
end;

function TFormDesigner.GenerateNewName(namebase: string): string;
var
  nind, n: integer;
  cd: TWidgetDesigner;
  newname: string;
  bok: boolean;
begin
  nind := 1;
  repeat
    newname := namebase + IntToStr(nind);
    bok := True;
    for n := 0 to FWidgets.Count - 1 do
    begin
      cd := TWidgetDesigner(FWidgets.Items[n]);
      if cd.Widget.Name = newname then
      begin
        bok := False;
        break;
      end;
    end;
    Inc(nind);
  until bok;
  Result := newname;
end;


procedure TFormDesigner.MsgActivate(var msg: TfpgMessageRec);
begin
  maindsgn.SelectForm(self);
end;

function TFormDesigner.GetFormSourceDecl: string;
var
  n: integer;
  wd: TWidgetDesigner;
  wgclass: string;
begin
  Result := '';
  for n := 0 to FWidgets.Count - 1 do
  begin
    wd := TWidgetDesigner(FWidgets.Items[n]);
    if wd.Widget is TOtherWidget then
      wgclass := TOtherWidget(wd.Widget).wgClassName
    else
      wgclass := wd.Widget.ClassName;
    Result := Result + '    ' + wd.Widget.Name + ' : ' + wgclass + ';'#10;
  end;
end;

function TFormDesigner.GetFormSourceImpl: string;
var
  s: string;
  sl: TStringList;
  n: integer;
  wd: TWidgetDesigner;
  wg: TfpgWidget;
  wgclass, pwgname: string;
begin
  s := '';

  if maindsgn.SaveComponentNames then
    s := s + '  Name := ' + QuotedStr(FForm.Name) + ';' + LineEnding;

  s := s + '  SetPosition(' + IntToStr(FForm.Left) + ',' + IntToStr(FForm.Top) + ',' + IntToStr(
    FForm.Width) + ',' + IntToStr(FForm.Height) + ');' + LineEnding;
  s := s + '  WindowTitle := ' + QuotedStr(FForm.WindowTitle) + ';' + LineEnding;

  //adding other form properties, idented
  sl      := TStringList.Create;
  sl.Text := FFormOther;
  for n := 0 to sl.Count - 1 do
    s := s + '  ' + sl.Strings[n] + LineEnding;
  sl.Free;

  s := s + LineEnding;

  // FORM WIDGETS

  for n := 0 to FWidgets.Count - 1 do
  begin
    wd := TWidgetDesigner(FWidgets.Items[n]);
    wg := wd.Widget;
    if wg.Parent = FForm then
      pwgname := 'self'
    else
      pwgname := wg.Parent.Name;
    if wg is TOtherWidget then
      wgclass := TOtherWidget(wg).wgClassName
    else
      wgclass := wg.ClassName;

    s := s + '  ' + wg.Name + ' := ' + wgclass + '.Create(' + pwgname + ');' + LineEnding +
      '  with ' + wg.Name + ' do' + LineEnding + '  begin' + LineEnding + GetWidgetSourceImpl(wd, '    ') +
      '  end;' + LineEnding + LineEnding;
  end;

  Result := s;
end;

function TFormDesigner.GetWidgetSourceImpl(wd: TWidgetDesigner; ident: string): string;
var
  ts, cs: string;
  s: string;
  wg: TfpgWidget;
  wgc: TVFDWidgetClass;
  n: integer;

  procedure SaveItems(Name: string; sl: TStringList);
  var
    f: integer;
  begin
    for f := 0 to sl.Count - 1 do
      s := s + ident + Name + '.Add(' + QuotedStr(sl.Strings[f]) + ');' + LineEnding;
  end;

  {  procedure SaveColumns(grid : TwgDBGrid);
  var
    f : integer;
    c : TDBColumn;
    alstr : string;
  begin
    for f := 0 to grid.ColumnCount - 1 do
    begin
      c := grid.Columns[f];
      case c.Alignment of
      alRight  : alstr := 'alRight';
      alCenter : alstr := 'alCenter';
      else
        alstr := 'alLeft';
      end;
      s := s + ident + 'AddColumn8('+QuotedStr(u16u8safe(c.Title))+','+QuotedStr(c.FieldName8)
                +','+IntToStr(c.Width)+','+alstr+');'#10;
    end;
  end;
}
begin
  wg  := wd.Widget;
  wgc := wd.FVFDClass;
  s   := '';

  if maindsgn.SaveComponentNames then
    s := s + ident + 'Name := ' + QuotedStr(wg.Name) + ';' + LineEnding;

  s := s + ident + 'SetPosition(' + IntToStr(wg.Left) + ',' + IntToStr(wg.Top) + ',' +
    IntToStr(wg.Width) + ',' + IntToStr(wg.Height) + ');' + LineEnding;

  if wg.Anchors <> [anLeft, anTop] then
  begin
    ts := '[';
    cs := '';
    if anLeft in wg.Anchors then
    begin
      ts := ts + cs + 'anLeft';
      cs := ',';
    end;
    if anRight in wg.Anchors then
    begin
      ts := ts + cs + 'anRight';
      cs := ',';
    end;
    if anTop in wg.Anchors then
    begin
      ts := ts + cs + 'anTop';
      cs := ',';
    end;
    if anBottom in wg.Anchors then
    begin
      ts := ts + cs + 'anBottom';
      cs := ',';
    end;
    ts := ts + '];';
    s := s + ident + 'Anchors := ' + ts + LineEnding;
  end;

  for n := 1 to wgc.PropertyCount do
    s := s + wgc.GetProperty(n).GetPropertySource(wg, ident);

  {
  if wg is TwgMemo then
  begin
    SaveItems('Lines',TwgMemo(wg).Lines);
  end
  else if wg is TwgChoiceList then
  begin
    SaveItems('Items',TwgChoiceList(wg).Items);
  end
  else if wg is TwgTextListBox then
  begin
    SaveItems('Items',TwgTextListBox(wg).Items);
  end
  else if wg is TwgDBGrid then
  begin
    SaveColumns(TwgDBGrid(wg));
  end
  else
    if GetWidgetText(wg, ts) then
    begin
      s := s + ident + 'Text := u8('+QuotedStr(u8encode(ts))+');'#10;  // encoding with all printable characters
    end;
}

  for n := 0 to wd.other.Count - 1 do
    if trim(wd.other.Strings[n]) <> '' then
      s := s + ident + wd.other.Strings[n] + LineEnding;

  Result := s;
end;

procedure TFormDesigner.OnEditWidget(Sender: TObject);
var
  n: integer;
  cd: TWidgetDesigner;
  wg: TfpgWidget;
begin
  for n := 0 to FWidgets.Count - 1 do
  begin
    cd := TWidgetDesigner(FWidgets.Items[n]);
    if cd.Selected then
    begin
      wg := cd.Widget;

      // Running widget editor;
      RunWidgetEditor(cd, wg);

      Exit;
    end;
  end;
end;

procedure EditItems(sl: TStringList);
var
  frmie: TItemEditorForm;
  //ax,ay : integer;
begin
  frmie := TItemEditorForm.Create(nil);
  //GfxGetAbsolutePosition(PropertyForm.btnEdit.WinHandle, PropertyForm.btnEdit.width, 0, ax,ay);
  //frmie.Left := ax;
  //frmie.Top := ay;

  frmie.edItems.Lines.Assign(sl);
  if frmie.ShowModal = 1 then
  begin
//    Writeln('OK');
    sl.Assign(frmie.edItems.Lines);
  end;
  frmie.Free;
end;

procedure TFormDesigner.RunWidgetEditor(wgd: TWidgetDesigner; wg: TfpgWidget);
begin
  if wg is TfpgMemo then
  begin
    EditItems(TfpgMemo(wg).Lines);
    wg.Invalidate;
  end
  else if wg is TfpgComboBox then
  begin
    EditItems(TfpgComboBox(wg).Items);
    wg.Invalidate;
  end
  else if wg is TfpgListBox then
  begin
    EditItems(TfpgListBox(wg).Items);
    wg.Invalidate;
  end;
end;

procedure TFormDesigner.InsertWidget(pwg: TfpgWidget; x, y: integer; wgc: TVFDWidgetClass);
var
  cfrm: TInsertCustomForm;
  newname, newclassname: string;
  wg: TfpgWidget;
  wgd: TWidgetDesigner;
begin
  if wgc = nil then
    Exit;

  newname := '';

  if wgc.WidgetClass = TOtherWidget then
  begin
    newclassname := '';
    cfrm         := TInsertCustomForm.Create(nil);
    cfrm.edName.Text := GenerateNewName(wgc.NameBase);
    cfrm.edClass.Text := 'Tfpg';
    if cfrm.ShowModal = 1 then
    begin
      newname      := cfrm.edName.Text;
      newClassName := cfrm.edClass.Text;
    end;
    cfrm.Free;
    if (newname = '') or (newclassname = '') then
      Exit;
  end;

  wg := wgc.CreateWidget(pwg);
  if wg <> nil then
  begin
    wg.Left := x;
    wg.Top  := y;
    if newname = '' then
      newname := GenerateNewName(wgc.NameBase);
    wg.Name := newname;
    if wgc.WidgetClass = TOtherWidget then
      TOtherWidget(wg).wgClassName := newclassname;
    wgd          := AddWidget(wg, wgc);
    wg.Visible   := True;
    DeSelectAll;
    wgd.Selected := True;
    UpdatePropWin;
  end;
end;

function TFormDesigner.FindWidgetByName(const wgname: string): TfpgWidget;
var
  n: integer;
  wgnameuc: string;
  cd: TWidgetDesigner;
begin
  wgnameuc := UpperCase(wgname);
  Result   := nil;
  for n := 0 to FWidgets.Count - 1 do
  begin
    cd := TWidgetDesigner(FWidgets.Items[n]);
    if UpperCase(cd.Widget.Name) = wgnameuc then
    begin
      Result := cd.Widget;
      Exit;
    end;
  end;
end;

{ TDesignedForm }

procedure TDesignedForm.AfterCreate;
begin
  inherited AfterCreate;
  WindowPosition := wpUser;
  WindowTitle := 'New Form';
  SetPosition(300, 100, 300, 250);
end;


{ TOtherWidget }

procedure TOtherWidget.HandlePaint;
var
  s: string;
begin
  inherited HandlePaint;
  if not HasHandle then
    Exit;

  Canvas.BeginDraw;

  Canvas.Clear(FBackgroundColor);
  Canvas.SetFont(FFont);
  Canvas.SetColor(clWidgetFrame);
  Canvas.DrawRectangle(0, 0, Width, Height);
  Canvas.SetTextColor(clText1);
  s := Name + ' : ' + wgClassName;
  Canvas.DrawString(2, 2, s);
  
  Canvas.EndDraw;
end;

constructor TOtherWidget.Create(AOwner: TComponent);
begin
  inherited;
  wgClassName := 'TfpgWidget';
  FBackgroundColor := $C0E0C0;
  FFont   := fpgStyle.DefaultFont;
  FWidth  := 120;
  FHeight := 32;
end;

end.