File: sparta_mainide.pas

package info (click to toggle)
lazarus 2.0.0%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 214,460 kB
  • sloc: pascal: 1,862,622; xml: 265,709; cpp: 56,595; sh: 3,008; java: 609; makefile: 535; perl: 297; sql: 222; ansic: 137
file content (1784 lines) | stat: -rw-r--r-- 52,034 bytes parent folder | download
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
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
{
 *****************************************************************************
  See the file COPYING.modifiedLGPL.txt, included in this distribution,
  for details about the license.
 *****************************************************************************

  Author: Maciej Izak

  DaThoX 2004-2015
  FreeSparta.com
}

unit sparta_MainIDE;

{$mode delphi}{$H+}

interface

uses
  Classes, SysUtils,
  {$IFDEF USE_GENERICS_COLLECTIONS}
    Generics.Collections, Generics.Defaults,
  {$ELSE}
    ghashmap, sparta_HashUtils, gvector, contnrs,
  {$ENDIF}
  // LCL
  LCLIntf, LCLType, LMessages, ComCtrls, Controls, Forms, ExtCtrls, Graphics,
  // IdeIntf
  SrcEditorIntf, LazIDEIntf, FormEditingIntf, PropEdits, PropEditUtils, ComponentEditors,
  // Sparta
  sparta_InterfacesMDI, sparta_strconsts, sparta_DesignedForm, sparta_resizer,
  sparta_FakeForm, sparta_FakeFrame, sparta_FakeCustom;

const
  WM_SETNOFRAME = WM_USER;
  WM_BOUNDTODESIGNTABSHEET = WM_USER + 1;

type
  { TDesignFormData }

  TDesignFormData = class(TComponent, IDesignedForm, IDesignedFormIDE)
  private
    FWndMethod: TWndMethod;
    FForm: IDesignedFormIDE;
    FLastScreenshot: TBitmap;
    FPopupParent: TSourceEditorWindowInterface;
    FHiding: boolean;
{$IFDEF USE_GENERICS_COLLECTIONS}
    FFormImages: TList<TImage>;
{$ELSE}
    FFormImages: TList;
{$ENDIF}
    procedure WndMethod(var Msg: TLMessage);
    procedure SetPopupParent(AVal: TSourceEditorWindowInterface);
    procedure DoAddForm;
    procedure FormChangeBounds(Sender: TObject);
  public
{$IFDEF USE_GENERICS_COLLECTIONS}
    class var AddFormEvents: TList<TNotifyEvent>;
{$ELSE}
    class var AddFormEvents: TVector<TNotifyEvent>;
{$ENDIF}

    class constructor Init;
    class destructor Finit;

    procedure AddFormImage(AImage: TImage);
    procedure RemoveFormImage(AImage: TImage);
    procedure RepaintFormImages;

    property Form: IDesignedFormIDE read FForm implements IDesignedForm, IDesignedFormIDE;
    property LastScreenshot: TBitmap read FLastScreenshot;
    property PopupParent: TSourceEditorWindowInterface read FPopupParent write SetPopupParent;

    constructor Create(AForm: TCustomForm); reintroduce;
    destructor Destroy; override;
  end;

  { TModulePageControl }

  TModulePageControl = class(TPageControl)
  private
    FResizer: TResizer;
    FDesignFormData: TDesignFormData;
  protected
    procedure SetDesignFormData(const AValue: TDesignFormData); virtual;
  public
    destructor Destroy; override;
    procedure ShowDesignPage;
    procedure HideDesignPage;
    procedure BoundToDesignTabSheet;

    property Resizer: TResizer read FResizer;
    property DesignFormData: TDesignFormData read FDesignFormData write SetDesignFormData;
  end;

  { TSourceEditorWindowData }

  TSourceEditorWindowData = class
  private
    FActiveDesignFormData: TDesignFormData;
    FForm: TSourceEditorWindowInterface;
{$IFDEF USE_GENERICS_COLLECTIONS}
    FPageCtrlList: TDictionary<TSourceEditorInterface, TModulePageControl>;
{$ELSE}
    FPageCtrlList: THashmap<TSourceEditorInterface, TModulePageControl, THash_TObject>;
{$ENDIF}
    FLastTopParent: TControl;

    procedure SetActiveDesignFormData(const AValue: TDesignFormData);
    procedure OnChangeBounds(Sender: TObject);
    procedure AddPageCtrl(ASrcEditor: TSourceEditorInterface; APage: TModulePageControl);
    procedure RemovePageCtrl(ASrcEditor: TSourceEditorInterface);
  public
    constructor Create(AForm: TSourceEditorWindowInterface);
    destructor Destroy; override;
    property ActiveDesignFormData: TDesignFormData read FActiveDesignFormData write SetActiveDesignFormData;
    procedure BoundToDesignTabSheet;
  end;

  { TDTXTabMaster }

  TDTXTabMaster = class(TIDETabMaster)
  private
    FAutoSizeControlList: TObjectList;
  protected
    function GetTabDisplayState: TTabDisplayState; override;
    function GetTabDisplayStateEditor(Index: TSourceEditorInterface): TTabDisplayState; override;
  public
    constructor Create;
    destructor Destroy; override;
    function AutoSizeInShowDesigner(AControl: TControl): Boolean; override;
    procedure EnableAutoSizing(AControl: TControl);
    procedure ToggleFormUnit; override;
    procedure JumpToCompilerMessage(ASourceEditor: TSourceEditorInterface); override;

    procedure ShowCode(ASourceEditor: TSourceEditorInterface); override;
    procedure ShowDesigner(ASourceEditor: TSourceEditorInterface; {%H-}AIndex: Integer = 0); override;
    procedure ShowForm(AForm: TCustomForm); override;
  end;

  { TDTXComponentsMaster }

  TDTXComponentsMaster = class(TIDEComponentsMaster)
    function DrawNonVisualComponents(ALookupRoot: TComponent): Boolean; override;
  end;

  TFormHack = class(TCustomForm);

  { TSpartaMainIDE }

  TSpartaMainIDE = class(TObject)
  public
    class function GetCurrentResizer: TResizer;
    class procedure TryFreeFormData(Form: TCustomForm);

    class procedure Screen_FormAdded(Sender: TObject; Form: TCustomForm);
    class procedure Screen_FormDel(Sender: TObject; Form: TCustomForm);

    class procedure WindowCreate(Sender: TObject);
    class procedure WindowDestroy(Sender: TObject);
    class procedure WindowShow(Sender: TObject);
    class procedure WindowHide(Sender: TObject);

    class procedure EditorActivated(Sender: TObject);
    class procedure EditorDestroyed(Sender: TObject);
    class procedure EditorCreate(Sender: TObject);

    class procedure TabChange(Sender: TObject);

    class procedure GlobalOnChangeBounds(Sender: TObject);
    class procedure GlobalSNOnChangeBounds(Sender: TObject);
    class procedure OnShowDesignerForm(Sender: TObject; {%H-}AEditor: TSourceEditorInterface;
                                 AComponentPaletteClassSelected: Boolean);
    class procedure OnShowSrcEditor(Sender: TObject);

    class procedure OnShowMethod(const Name: String);
    class procedure OnDesignRefreshPropertyValues;
    class procedure OnModifiedPersistentAdded({%H-}APersistent: TPersistent; {%H-}Select: Boolean);
    class procedure OnModifiedSender(Sender: TObject);
    class procedure OnModified;
    class procedure DesignerSetFocus;
    class procedure OnDesignMouseDown(Sender: TObject; {%H-}Button: TMouseButton;
      {%H-}Shift: TShiftState; {%H-}X, {%H-}Y: Integer);
  end;

var
  normForms: Classes.TList; // normal forms
  dsgnForms: Classes.TList; // design forms
{$IFDEF USE_GENERICS_COLLECTIONS}
  SourceEditorWindows: TObjectDictionary<TSourceEditorWindowInterface, TSourceEditorWindowData>;
{$ELSE}
  SourceEditorWindows: THashmap<TSourceEditorWindowInterface, TSourceEditorWindowData, THash_TObject>;
{$ENDIF}

  LastActiveSourceEditorWindow: TSourceEditorWindowInterface = nil;
  LastActiveSourceEditor: TSourceEditorInterface = nil;

  BoundInitialized: Boolean;

function FindModulePageControl(AForm: TSourceEditorWindowInterface): TModulePageControl; overload;
function FindSourceEditorForDesigner(ADesigner: TIDesigner): TSourceEditorInterface;

implementation

uses
  sparta_ResizerFrame;

// FUTURE USE
//
//function FindDesignForm(ADesigner: TIDesigner): TCustomForm;
//var
//  f: TDesignFormData;
//begin
//  for Pointer(f) in dsgnForms do
//    with f as IDesignedForm do
//    if Form.Designer = ADesigner then
//      Exit(Form);
//
//  Result := nil;
//end;
//
//function FindDesignFormData(AForm: TSourceEditorWindowInterface): TDesignFormData; overload;
//begin
//  Result := FindDesignFormData(
//    FindModulePageControl(AForm)
//  );
//end;
//
//procedure HideAllForms;
//var
//  f: TDesignFormData;
//begin
//  for Pointer(f) in dsgnForms do
//    ShowWindow(f.Form.Form.Handle, SW_HIDE);
//end;

function FindModulePageControl(ASourceEditor: TSourceEditorInterface): TModulePageControl; overload;
var
  LParent: TWinControl;
begin
  if ASourceEditor = nil then
    Exit(nil);

  LParent := ASourceEditor.EditorControl.Parent;
  while LParent <> nil do
  begin
    if LParent is TModulePageControl then
      Exit(TModulePageControl(LParent));
    LParent := LParent.Parent;
  end;

  Result := nil;
end;

function FindModulePageControl(AForm: TSourceEditorWindowInterface): TModulePageControl; overload;
begin
  Result := FindModulePageControl(AForm.ActiveEditor);
end;

function AbsoluteFindModulePageControl(ASrcEditor: TSourceEditorInterface): TModulePageControl;
var
  LSEWD: TSourceEditorWindowData;
{$IFnDEF USE_GENERICS_COLLECTIONS}
  LIterator: THashmap<TSourceEditorWindowInterface, TSourceEditorWindowData, THash_TObject>.TIterator;
{$ENDIF}
begin
  Result := nil;
{$IFDEF USE_GENERICS_COLLECTIONS}
  for LSEWD in SourceEditorWindows.Values do
    if LSEWD.FPageCtrlList.ContainsKey(ASrcEditor) then
      Exit(LSEWD.FPageCtrlList[ASrcEditor]);
{$ELSE}
  LIterator := SourceEditorWindows.Iterator;
  if LIterator <> nil then
  try
    repeat
      LSEWD := LIterator.Value;
      if LSEWD.FPageCtrlList.contains(ASrcEditor) then
        Exit(LSEWD.FPageCtrlList[ASrcEditor]);
    until not LIterator.next;
  finally
    LIterator.Free;
  end;
{$ENDIF}

end;

function FindSourceEditorForDesigner(ADesigner: TIDesigner): TSourceEditorInterface;
var
  i: Integer;
begin
  for i := 0 to SourceEditorManagerIntf.SourceEditorCount - 1 do
    if SourceEditorManagerIntf.SourceEditors[i].GetDesigner(False) = ADesigner then
      Exit(SourceEditorManagerIntf.SourceEditors[i]);
  Result := nil;
end;

function FindDesignFormData(ADesigner: TIDesigner): TDesignFormData; overload;
var
  p: Pointer;
  f: TDesignFormData absolute p;
  fi: IDesignedForm = nil;
begin
  Result := nil;

  if ADesigner = nil then
    Exit;

  for p in dsgnForms do
  begin
    fi := f.FForm;
    with fi do
    begin
      if (Form.Designer = ADesigner) then
      begin
        Exit(f);
      end;
    end;
  end;
end;

procedure RefreshAllSourceWindowsModulePageControl;
var
  LWindow: TSourceEditorWindowInterface;
  LPageCtrl: TModulePageControl;
{$IFnDEF USE_GENERICS_COLLECTIONS}
  LIterator: THashmap<TSourceEditorWindowInterface, TSourceEditorWindowData, THash_TObject>.TIterator;
{$ENDIF}
begin
{$IFDEF USE_GENERICS_COLLECTIONS}
  for LWindow in SourceEditorWindows.Keys do
  begin
    LPageCtrl := FindModulePageControl(LWindow);

    // for example LPageCtrl is nil when we clone module to new window
    if (LPageCtrl = nil) or (csDestroying in LWindow.ComponentState) then
      Continue;

    if LWindow.ActiveEditor = nil then
      LPageCtrl.HideDesignPage
    else
      if LWindow.ActiveEditor.GetDesigner(True) <> nil then
        // TODO some check function: is displayed right form?
        LPageCtrl.ShowDesignPage
      else
        LPageCtrl.HideDesignPage;
  end;
{$ELSE}
  LIterator := SourceEditorWindows.Iterator;
  if LIterator <> nil then
  try
    repeat
      LWindow := LIterator.Key;

      LPageCtrl := FindModulePageControl(LWindow);

      // for example LPageCtrl is nil when we clone module to new window
      if (LPageCtrl = nil) or (csDestroying in LWindow.ComponentState) then
        Continue;

      if LWindow.ActiveEditor = nil then
        LPageCtrl.HideDesignPage
      else
        if LWindow.ActiveEditor.GetDesigner(True) <> nil then
          // TODO some check function: is displayed right form?
          LPageCtrl.ShowDesignPage
        else
          LPageCtrl.HideDesignPage;
    until not LIterator.next;
  finally
    LIterator.Free;
  end;
{$ENDIF}
end;

// sometimes at some level of initialization form can not contain TIDesigner
// (during ide run and when is oppened default project with some TForm1)
function FindDesignFormData(AForm: TCustomForm): TDesignFormData; overload;
var
  f: TDesignFormData;
begin
  Result := nil;

  if AForm = nil then
    Exit;

  for Pointer(f) in dsgnForms do
    with f as IDesignedForm do
      if (Form = AForm) then
        Exit(f);
end;

function FindDesignFormData(AModulePageCtrl: TModulePageControl): TDesignFormData; overload;
var
  LSourceWindow: TSourceEditorWindowInterface;
  LSourceEditor: TSourceEditorInterface;
{$IFnDEF USE_GENERICS_COLLECTIONS}
  LIterator: THashmap<TSourceEditorWindowInterface, TSourceEditorWindowData, THash_TObject>.TIterator;
{$ENDIF}
begin
  Result := nil;

  if AModulePageCtrl = nil then
    Exit;

{$IFDEF USE_GENERICS_COLLECTIONS}
  for LSourceWindow in SourceEditorWindows.Keys do
  begin
    if AModulePageCtrl.Owner = LSourceWindow then
    begin
      LSourceEditor := LSourceWindow.ActiveEditor;
      if LSourceEditor = nil then
        Exit;

      Result := FindDesignFormData(LSourceEditor.GetDesigner(True));

      Exit;
    end;
  end;
{$ELSE}
  LIterator := SourceEditorWindows.Iterator;
  if LIterator <> nil then
  try
    repeat
      LSourceWindow := LIterator.Key;

      if AModulePageCtrl.Owner = LSourceWindow then
      begin
        LSourceEditor := LSourceWindow.ActiveEditor;
        if LSourceEditor = nil then
          Exit;

        Result := FindDesignFormData(LSourceEditor.GetDesigner(True));

        Exit;
      end;
    until not LIterator.next;
  finally
    LIterator.Free;
  end;
{$ENDIF}
end;

{ TDesignFormData }

procedure TDesignFormData.WndMethod(var Msg: TLMessage);

  // Without this button F12 don't work. (after creating new for editor is inactive) :<
  procedure FixF12_ActiveEditor;
  var
    i: Integer;
  begin
    // Just do it for new created forms or the last loaded form becomes the active
    // source editor after reopening a project.
    if Form.Form.Designer <> SourceEditorManagerIntf.ActiveEditor.GetDesigner(True) then Exit;

    SourceEditorManagerIntf.ActiveEditor := nil;
    for i := 0 to SourceEditorManagerIntf.UniqueSourceEditorCount - 1 do
      if Form.Form.Designer = SourceEditorManagerIntf.UniqueSourceEditors[i].GetDesigner(True) then
      begin
        SourceEditorManagerIntf.ActiveEditor := SourceEditorManagerIntf.UniqueSourceEditors[i];
        Break;
      end;
  end;

var
  Timer: TLMTimer;
begin
  if Msg.msg = LM_TIMER then
  begin
    Timer := TLMTimer(Msg);
    if Timer.TimerID = WM_SETNOFRAME then
    begin
      KillTimer(FForm.Form.Handle, WM_SETNOFRAME);
      ShowWindow(Form.Form.Handle, SW_HIDE);
      FHiding := False;
      FixF12_ActiveEditor;
      if Form.Form is TFakeForm then
        RepaintFormImages;
      Exit;
    end;
    if Timer.TimerID = WM_BOUNDTODESIGNTABSHEET then
    begin
      KillTimer(FForm.Form.Handle, WM_BOUNDTODESIGNTABSHEET);
      SourceEditorWindows[Form.LastActiveSourceWindow].BoundToDesignTabSheet;
      Exit;
    end;
  end;

  // we need to correct ActiveEditor to right form
  // this code works correctly on Windows platform 
  // (is necessery for selecting controls after form resizing).
  // in Linux platforms below code brings problems with QT (inactive form)
  {$IFDEF WINDOWS}
  case Msg.msg of
    LM_LBUTTONDOWN, LM_RBUTTONDOWN, LM_MBUTTONDOWN, LM_XBUTTONDOWN:
      if Form.LastActiveSourceWindow <> nil then
      begin
        SourceEditorManagerIntf.ActiveSourceWindow := Form.LastActiveSourceWindow;
        SourceEditorManagerIntf.ActiveEditor := Form.LastActiveSourceWindow.ActiveEditor;
      end;
  end;
  {$ENDIF}

  FWndMethod(Msg);
end;

procedure TDesignFormData.SetPopupParent(AVal: TSourceEditorWindowInterface);
begin
  FPopupParent := AVal;
  Form.RealPopupParent := FPopupParent;
end;

class constructor TDesignFormData.Init;
begin
{$IFDEF USE_GENERICS_COLLECTIONS}
  AddFormEvents := TList<TNotifyEvent>.Create;
{$ELSE}
  AddFormEvents := TVector<TNotifyEvent>.Create;
{$ENDIF}
end;

class destructor TDesignFormData.Finit;
begin
  AddFormEvents.Free;
end;

procedure TDesignFormData.AddFormImage(AImage: TImage);
begin
  if FFormImages <> nil then
    FFormImages.Add(AImage);
end;

procedure TDesignFormData.RemoveFormImage(AImage: TImage);
begin
  if FFormImages <> nil then
    FFormImages.Remove(AImage);
end;

procedure TDesignFormData.RepaintFormImages;
var
  LImage: TImage;
begin
  if FFormImages <> nil then
  begin
    for LImage in FFormImages do
      LImage.OnResize(LImage);
  end;
end;

procedure TDesignFormData.DoAddForm;
var
{$IFDEF USE_GENERICS_COLLECTIONS}
  ne: TNotifyEvent;
{$ELSE}
  i: Integer;
{$ENDIF}
begin
{$IFDEF USE_GENERICS_COLLECTIONS}
  for ne in AddFormEvents do
    ne(Self);
{$ELSE}
  if AddFormEvents.Size > 0 then  // Arithmetic overflow without a test. Size = unsigned.
    for i := 0 to AddFormEvents.Size-1 do
      AddFormEvents[i](Self);
{$ENDIF}

end;

procedure TDesignFormData.FormChangeBounds(Sender: TObject);
begin
  if not FForm.Update then
    SetTimer(FForm.Form.Handle, WM_BOUNDTODESIGNTABSHEET, 10, nil);
end;

constructor TDesignFormData.Create(AForm: TCustomForm);
begin
  FForm := TDesignedFormImpl.Create(Self, AForm);

  AForm.AddHandlerOnChangeBounds(FormChangeBounds);

  FLastScreenshot := TBitmap.Create;
  FWndMethod := FForm.Form.WindowProc;
  FForm.Form.WindowProc := WndMethod;

  if FForm.Form is TFakeForm then
  begin
{$IFDEF USE_GENERICS_COLLECTIONS}
    FFormImages := TList<TImage>.Create;
{$ELSE}
    FFormImages := TList.Create;
{$ENDIF}
    DoAddForm;
  end;
end;

destructor TDesignFormData.Destroy;
var
  LImage: TImage;
begin
  FForm.Form.WindowProc := FWndMethod; // ! important risky point :P

  if FFormImages <> nil then
  begin
    for LImage in FFormImages do
      LImage.Free;

    FreeAndNil(FFormImages);
  end;
  FLastScreenshot.Free;

  inherited Destroy;
  Pointer(FForm) := nil;
end;

{ TModulePageControl }

procedure TModulePageControl.SetDesignFormData(const AValue: TDesignFormData);
begin
  if (AValue = FDesignFormData) then
    // for show lfm code, if we want after editing lfm go back to form without any error
    // (when we restart IDE some error can be raised )
    if Assigned(FResizer) then
    begin
      if (AValue <> nil) and (FResizer.DesignedForm = AValue as IDesignedForm) then
        Exit;
    end
    else
      Exit;

  FDesignFormData := AValue;
  if AValue = nil then
  begin
    //find
    if Assigned(FResizer) then
      FResizer.DesignedForm := nil;
  end
  else
  begin
    AValue.Form.LastActiveSourceWindow := Owner as TSourceEditorWindowInterface;
    if Assigned(FResizer) then
      FResizer.DesignedForm := AValue;
    BoundToDesignTabSheet;
  end;
end;

destructor TModulePageControl.Destroy;
begin
  DesignFormData := nil;
  inherited Destroy;
end;

procedure TModulePageControl.ShowDesignPage;
begin
  Pages[1].TabVisible := True;
end;

procedure TModulePageControl.HideDesignPage;
begin
  Pages[1].TabVisible:=False;
end;

procedure TModulePageControl.BoundToDesignTabSheet;
begin
  if (ActivePageIndex <> 1) then
    Exit;

  if Assigned(FResizer) then
    FResizer.TryBoundSizerToDesignedForm(nil);
end;

{ TSourceEditorWindowData }

procedure TSourceEditorWindowData.SetActiveDesignFormData(
  const AValue: TDesignFormData);
var
  LPageCtrl: TModulePageControl;
begin
  if FActiveDesignFormData = AValue then
    Exit;

  if FActiveDesignFormData <> nil then
    // don't hide now if soon form will be hidden (for example on the IDE start)
    if not FActiveDesignFormData.FHiding then
    begin
      FActiveDesignFormData.FForm.HideWindow;
    end;
  FActiveDesignFormData := AValue;

  LPageCtrl := FindModulePageControl(FForm);
  if (AValue <> nil) then
  begin
    with AValue as IDesignedForm do
    {if not AValue.FHiding and (RealBorderStyle <> bsNone) then
    begin
      //BeginUpdate;
      //RealBorderIcons := [];
      //RealBorderStyle := bsNone;
      //Form.Show;
      //EndUpdate;
    end;}
    // important when we want back to tab where was oppened form :<
    LazarusIDE.DoShowDesignerFormOfSrc(FForm.ActiveEditor);
  end;

  // when is fired DestroyEditor - from this place we can't navigate to pagecontrol by FForm (we need to handle lastactiveeditor)
  if LPageCtrl = nil then
    Exit;

  LPageCtrl.DesignFormData := AValue;
  // for USE_POPUP_PARENT_DESIGNER to eliminate form over code  << maybe not needed any more since USE_POPUP_PARENT_DESIGNER isn't supported any more
  LPageCtrl.OnChange(LPageCtrl);
end;

constructor TSourceEditorWindowData.Create(AForm: TSourceEditorWindowInterface);
begin
  FForm := AForm;
{$IFDEF USE_GENERICS_COLLECTIONS}
  FPageCtrlList := TDictionary<TSourceEditorInterface, TModulePageControl>.Create;
{$ELSE}
  FPageCtrlList := THashmap<TSourceEditorInterface, TModulePageControl, THash_TObject>.Create;
{$ENDIF}
end;

destructor TSourceEditorWindowData.Destroy;
begin
  FPageCtrlList.Free;
  inherited Destroy;
end;

procedure TSourceEditorWindowData.BoundToDesignTabSheet;
var
  LPageCtrl: TModulePageControl;
begin
  LPageCtrl := FindModulePageControl(FForm);
  if LPageCtrl <> nil then
    LPageCtrl.BoundToDesignTabSheet;
end;

procedure TSourceEditorWindowData.OnChangeBounds(Sender: TObject);
//var
//  LPageCtrl: TModulePageControl;
begin
//  LPageCtrl := FindModulePageControl(FForm);
//  if LPageCtrl <> nil then
//    LPageCtrl.BoundToDesignTabSheet;
end;

procedure TSourceEditorWindowData.AddPageCtrl(ASrcEditor: TSourceEditorInterface; APage: TModulePageControl);
begin
{$IFDEF USE_GENERICS_COLLECTIONS}
  FPageCtrlList.Add(ASrcEditor, APage);
{$ELSE}
  FPageCtrlList.insert(ASrcEditor, APage);
{$ENDIF}
  APage.Pages[1].OnChangeBounds:=OnChangeBounds;
end;

procedure TSourceEditorWindowData.RemovePageCtrl(ASrcEditor: TSourceEditorInterface);
begin
{$IFDEF USE_GENERICS_COLLECTIONS}
  FPageCtrlList.Remove(ASrcEditor);
{$ELSE}
  FPageCtrlList.Delete(ASrcEditor);
{$ENDIF}
end;

{ TDTXTabMaster }

function TDTXTabMaster.GetTabDisplayState: TTabDisplayState;
begin
  Result := GetTabDisplayStateEditor(SourceEditorManagerIntf.ActiveEditor);
end;

function TDTXTabMaster.GetTabDisplayStateEditor(Index: TSourceEditorInterface
  ): TTabDisplayState;
var
  LPageCtrl: TModulePageControl;
begin
  if Index = nil then
    Exit(tdsNone);

  LPageCtrl := FindModulePageControl(Index);
  if LPageCtrl = nil then
    Exit(tdsNone);

  case LPageCtrl.PageIndex of
    0: Exit(tdsCode);
    1: Exit(tdsDesign);
  else
    Exit(tdsOther);
  end;
end;

constructor TDTXTabMaster.Create;
begin
  FAutoSizeControlList := TObjectList.Create(False);
end;

destructor TDTXTabMaster.Destroy;
begin
  FAutoSizeControlList.Free;
  inherited Destroy;
end;

function TDTXTabMaster.AutoSizeInShowDesigner(AControl: TControl): Boolean;
begin
  FAutoSizeControlList.Add(AControl);
  Result := True;
end;

procedure TDTXTabMaster.EnableAutoSizing(AControl: TControl);
var
  AIndex: Integer;
  AutoSizeControl: TControl;
begin
  if not Assigned(AControl) then Exit;
  AutoSizeControl := nil;

  if AControl is TNonFormProxyDesignerForm then
  begin
    if (TNonFormProxyDesignerForm(AControl).LookupRoot is TControl) then
      AutoSizeControl := TControl(TNonFormProxyDesignerForm(AControl).LookupRoot);
  end
  else
    AutoSizeControl := AControl;

  AIndex := FAutoSizeControlList.IndexOf(AutoSizeControl);
  if (AIndex >= 0) then
  begin
    FAutoSizeControlList.Delete(AIndex);
    AutoSizeControl.EnableAutoSizing{$IFDEF DebugDisableAutoSizing}('TAnchorDockMaster Delayed'){$ENDIF};
  end;
end;

procedure TDTXTabMaster.ToggleFormUnit;
begin
  case TabDisplayState of
    tdsCode:
      ShowDesigner(SourceEditorManagerIntf.ActiveEditor);
    tdsDesign:
      ShowCode(SourceEditorManagerIntf.ActiveEditor);
  end;
end;

procedure TDTXTabMaster.JumpToCompilerMessage(
  ASourceEditor: TSourceEditorInterface);
begin
  SourceEditorManagerIntf.ActiveEditor := ASourceEditor;

  ShowCode(ASourceEditor);
end;

procedure TDTXTabMaster.ShowCode(ASourceEditor: TSourceEditorInterface);
begin
  if ASourceEditor = nil then
    Exit;

  FindModulePageControl(ASourceEditor).PageIndex := 0;
end;

procedure TDTXTabMaster.ShowDesigner(ASourceEditor: TSourceEditorInterface; AIndex: Integer);
var
  LPageCtrl: TModulePageControl;
  Designer: TIDesigner;
  Form: TCustomForm;
begin
  if ASourceEditor = nil then
    Exit;

  LPageCtrl := FindModulePageControl(ASourceEditor);

  if not LPageCtrl.Pages[1].TabVisible then
    Exit;

  LPageCtrl.PageIndex := 1;
  LPageCtrl.OnChange(LPageCtrl);
end;

procedure TDTXTabMaster.ShowForm(AForm: TCustomForm);
var
  LEditor: TSourceEditorInterface;
begin
  LEditor := FindSourceEditorForDesigner(AForm.Designer);

  SourceEditorManagerIntf.ActiveEditor := LEditor;

  ShowDesigner(LEditor);
end;

{ TDTXComponentsMaster }

function TDTXComponentsMaster.DrawNonVisualComponents(ALookupRoot: TComponent): Boolean;
var
  LFormData: TDesignFormData;
  LPageCtrl: TModulePageControl;
begin
  Result := True;

  LFormData := FindDesignFormData(FormEditingHook.GetDesignerForm(ALookupRoot){ALookupRoot as TCustomForm});
  if LFormData = nil then
    Exit;

  LPageCtrl := FindModulePageControl(LFormData.Form.LastActiveSourceWindow);
  if (LPageCtrl = nil) or (LPageCtrl.Resizer = nil) or (LPageCtrl.Resizer.MainDTU = nil) then
    Exit;

  Result := LPageCtrl.Resizer.MainDTU.ShowNonVisualComponents;
end;

{ TSpartaMainIDE }

class procedure TSpartaMainIDE.Screen_FormAdded(Sender: TObject; Form: TCustomForm);
var
  LSourceEditor: TSourceEditorInterface;
  LFormData: TDesignFormData;
  //i: Integer;
  LPageCtrl: TModulePageControl;
begin
  if IsFormDesign(Form) then
  begin
    // Form like TForm1 etc...
    if (csDesignInstance in Form.ComponentState) or (Form is TNonFormProxyDesignerForm) then
    begin
      LFormData := TDesignFormData.Create(Form);
      LFormData.FHiding:=True;
      dsgnForms.Add(LFormData);

      LSourceEditor := FindSourceEditorForDesigner(Form.Designer);

      if LSourceEditor <> nil then
      begin
        LPageCtrl := FindModulePageControl(LSourceEditor);
        if LPageCtrl <> nil then
        begin
          LPageCtrl.ShowDesignPage;
          LPageCtrl.DesignFormData := LFormData;
        end;
      end;

      SetTimer(Form.Handle, WM_SETNOFRAME, 10, nil);
    end;
  end
  else
  begin
    // ONDREJ: the following code marged with (on-del) seems to help with nothing
    // but slows down loading forms and make them flicker.
    // I therefore commented it out. Please revert if there'll be regressions.
    {  // (on-del)
    if not BoundInitialized then
    begin
      for i := 0 to Screen.FormCount - 1 do
        if Screen.Forms[i] = Form then
          Continue
        else
        begin
          Screen.Forms[i].AddHandlerOnChangeBounds(GlobalOnChangeBounds);
        end;
      BoundInitialized := True;
    end;}

    if Form is TSourceEditorWindowInterface then
    begin
      Form.AddHandlerOnChangeBounds(GlobalSNOnChangeBounds);
      //Form.PopupMode := pmExplicit; // (on-del)
      normForms.Add(Form); // (on-del)
    end
    else
    begin
      //Form.AddHandlerOnChangeBounds(GlobalOnChangeBounds); // (on-del)
    end;

    //Forms.Add(Form); // (on-del)
  end;
end;

class procedure TSpartaMainIDE.TryFreeFormData(Form: TCustomForm);
var
  LSEWD: TSourceEditorWindowData;
  mpc: TModulePageControl;
  LFormData: TDesignFormData;
{$IFnDEF USE_GENERICS_COLLECTIONS}
  LIterator: THashmap<TSourceEditorWindowInterface, TSourceEditorWindowData, THash_TObject>.TIterator;
  LIterator2: THashmap<TSourceEditorInterface, TModulePageControl, THash_TObject>.TIterator;
{$ENDIF}
begin
  Form.Parent := nil;
  Application.ProcessMessages; // For TFrame - System Error. Code: 1400. Invalid window handle.

  LFormData := FindDesignFormData(Form);
  dsgnForms.Remove(LFormData);

{$IFDEF USE_GENERICS_COLLECTIONS}
  for LSEWD in SourceEditorWindows.Values do
  begin
    if LSEWD.ActiveDesignFormData <> nil then
      if LSEWD.ActiveDesignFormData.Form.Form = Form then
        LSEWD.FActiveDesignFormData := nil; // important - we can't call OnChange tab, because tab don't exist anymore

    for mpc in LSEWD.FPageCtrlList.Values do
      if mpc.DesignFormData <> nil then
         if mpc.DesignFormData.Form.Form = Form then
            mpc.DesignFormData := nil;
  end;
{$ELSE}
  LIterator := SourceEditorWindows.Iterator;
  if LIterator <> nil then
  try
    repeat
      LSEWD := LIterator.Value;
      if LSEWD.ActiveDesignFormData <> nil then
        if LSEWD.ActiveDesignFormData.Form.Form = Form then
          LSEWD.FActiveDesignFormData := nil; // important - we can't call OnChange tab, because tab don't exist anymore

      LIterator2 := LSEWD.FPageCtrlList.Iterator;
      if LIterator2 <> nil then
      try
        repeat
          mpc := LIterator2.Value;
          if mpc.DesignFormData <> nil then
             if mpc.DesignFormData.Form.Form = Form then
                mpc.DesignFormData := nil;
        until not LIterator2.next;
      finally
        LIterator2.Free;
      end;
    until not LIterator.next;
  finally
    LIterator.Free;
  end;
{$ENDIF}

  LFormData.Free;
end;

class procedure TSpartaMainIDE.Screen_FormDel(Sender: TObject; Form: TCustomForm);
begin
  if not IsFormDesign(Form) then
  begin
    if Form is TSourceEditorWindowInterface then
      Form.RemoveHandlerOnChangeBounds(GlobalSNOnChangeBounds)
    else
      Form.RemoveHandlerOnChangeBounds(GlobalOnChangeBounds)
  end
  else
    TryFreeFormData(Form);
end;

class procedure TSpartaMainIDE.WindowCreate(Sender: TObject);
var
  LSourceEditorWindow: TSourceEditorWindowInterface;
begin
  if Sender.ClassNameIs('TSourceNotebook') then
  begin
    LSourceEditorWindow := Sender as TSourceEditorWindowInterface;
{$IFDEF USE_GENERICS_COLLECTIONS}
    SourceEditorWindows.Add(LSourceEditorWindow, TSourceEditorWindowData.Create(LSourceEditorWindow));
{$ELSE}
    SourceEditorWindows.insert(LSourceEditorWindow, TSourceEditorWindowData.Create(LSourceEditorWindow));
{$ENDIF}
  end;
end;

class procedure TSpartaMainIDE.WindowDestroy(Sender: TObject);
var
  p: Pointer;
  f: TDesignFormData absolute p;
begin
  for p in dsgnForms do
    if f.FForm.LastActiveSourceWindow = Sender then
      f.FForm.LastActiveSourceWindow := nil;
{$IFDEF USE_GENERICS_COLLECTIONS}
  SourceEditorWindows.Remove(Sender as TSourceEditorWindowInterface);
{$ELSE}
  SourceEditorWindows[Sender as TSourceEditorWindowInterface].Free;
  SourceEditorWindows.Delete(Sender as TSourceEditorWindowInterface);
{$ENDIF}
  if LastActiveSourceEditorWindow = Sender then
    LastActiveSourceEditorWindow := nil;
end;

class procedure TSpartaMainIDE.WindowShow(Sender: TObject);
var
  LWindow: TSourceEditorWindowInterface;
  LWindowData: TSourceEditorWindowData;
  LDesignedForm: IDesignedForm;
begin
  LWindow := Sender as TSourceEditorWindowInterface;

{$IFDEF USE_GENERICS_COLLECTIONS}
  if not SourceEditorWindows.TryGetValue(LWindow, LWindowData) or
    (LWindowData.ActiveDesignFormData = nil)
  then
    Exit;
{$ELSE}
  if not SourceEditorWindows.contains(LWindow) then
    Exit;
  LWindowData := SourceEditorWindows[LWindow];
  if LWindowData.ActiveDesignFormData = nil then
    Exit;
{$ENDIF}

  LDesignedForm := LWindowData.ActiveDesignFormData as IDesignedForm;
  LDesignedForm.ShowWindow;
end;

class procedure TSpartaMainIDE.WindowHide(Sender: TObject);
var
  LWindow: TSourceEditorWindowInterface;
  LWindowData: TSourceEditorWindowData;
  LDesignedForm: IDesignedForm;
begin
  LWindow := Sender as TSourceEditorWindowInterface;

{$IFDEF USE_GENERICS_COLLECTIONS}
  if not SourceEditorWindows.TryGetValue(LWindow, LWindowData) or
    (LWindowData.ActiveDesignFormData = nil)
  then
    Exit;
{$ELSE}
  if not SourceEditorWindows.contains(LWindow) then
    Exit;
  LWindowData := SourceEditorWindows[LWindow];
  if LWindowData.ActiveDesignFormData = nil then
    Exit;
{$ENDIF}

  LDesignedForm := LWindowData.ActiveDesignFormData as IDesignedForm;
  LDesignedForm.HideWindow;
end;

class procedure TSpartaMainIDE.DesignerSetFocus;
var
  LResizer: TResizer;
begin
  LResizer := GetCurrentResizer;
  if LResizer<>nil then
    LResizer.ActiveResizeFrame.DesignerSetFocus;
end;

class procedure TSpartaMainIDE.EditorActivated(Sender: TObject);
var
  LDesigner: TIDesigner;
  LSourceEditor: TSourceEditorInterface;
{$IFnDEF USE_GENERICS_COLLECTIONS}
  LIterator: THashmap<TSourceEditorInterface, TModulePageControl, THash_TObject>.TIterator;
{$ENDIF}

  function LastSourceEditorNotFound: boolean;
  var
    i: Integer;
    se: TSourceEditorInterface;
  begin
    if (LastActiveSourceEditorWindow = nil) or (LastActiveSourceEditor = nil) then
      Exit(False);

{$IFDEF USE_GENERICS_COLLECTIONS}
    for se in SourceEditorWindows[LastActiveSourceEditorWindow].FPageCtrlList.Keys do
    begin
      Result := True;
      for i := 0 to LastActiveSourceEditorWindow.Count - 1 do
        if se = LastActiveSourceEditorWindow.Items[i] then
        begin
          Result := False;
          Break;
        end;

      if Result then
      begin
        LastActiveSourceEditor := se; // after moving code editor into other window, sometimes IDE switch to other tab :\ damn... this line prevent this.
        Exit;
      end;
    end;
{$ELSE}
    LIterator := SourceEditorWindows[LastActiveSourceEditorWindow].FPageCtrlList.Iterator;
    if LIterator <> nil then
    try
      repeat
        se := LIterator.Key;
        Result := True;
        for i := 0 to LastActiveSourceEditorWindow.Count - 1 do
          if se = LastActiveSourceEditorWindow.Items[i] then
          begin
            Result := False;
            Break;
          end;
        if Result then
        begin
          LastActiveSourceEditor := se; // after moving code editor into other window, sometimes IDE switch to other tab :\ damn... this line prevent this.
          Exit;
        end;
      until not LIterator.next;
    finally
      LIterator.Free;
    end;
{$ENDIF}

    Result := False;
  end;

var
  LPageCtrl: TModulePageControl;
  LSourceEditorWindow: TSourceEditorWindowInterface;
  LDesignFormData: TDesignFormData;
begin
  if Sender is TSourceEditorInterface then
  begin
    LSourceEditor := TSourceEditorInterface(Sender);
    // if we create directly new project then Activate is called without EditorCreate...
    if not (LSourceEditor.EditorControl.Parent.Parent is TModulePageControl) then
    begin
      // possible is situation when we moved tab into other window
      // then was not called event EditorDestroy - that generates problems with switching tabs
      // or when we moving tab to first window ( then is raising : duplicates not allowed in dictionary).
      if LastSourceEditorNotFound then
        EditorDestroyed(nil);
      EditorCreate(Sender);
    end;

    LDesigner := LSourceEditor.GetDesigner(True);

    // should be performed during EditorCreate (parent of parent is module page ctrl)
    LPageCtrl := TModulePageControl(LSourceEditor.EditorControl.Parent.Parent);
    if LPageCtrl = nil then
      Exit;

    if LDesigner = nil then
      LPageCtrl.HideDesignPage
    else
    begin
      if LPageCtrl.Resizer = nil then
        LPageCtrl.FResizer := TResizer.Create(LPageCtrl.Pages[1], TResizerFrame);

      LPageCtrl.ShowDesignPage;
    end;

    LSourceEditorWindow := TSourceEditorWindowInterface(LPageCtrl.Owner);

    LastActiveSourceEditorWindow := LSourceEditorWindow;
    LastActiveSourceEditor := LSourceEditor;

    LDesignFormData := FindDesignFormData(LPageCtrl);

    // when we switch tab, design form should be hidden
    if (LDesigner = nil) or (LDesignFormData = nil) then
      SourceEditorWindows[LSourceEditorWindow].ActiveDesignFormData := nil
    else
    begin
      // during form  loading for example from package, ActiveDesignFormData assignment,
      // blocks the message queue responsible for hiding form
      // We can't check it because there are some forms where designing is not handled yet.
      // (for that kind of forms is returned empty designformdata)
      // maybe we can fix this in future
      if not LDesignFormData.FHiding then
        // Prevent unexpected events (when is deactivated some control outside designed form)
        if (LDesignFormData.Form.LastActiveSourceWindow = LSourceEditorWindow)
        // important!!! for many error - switching between editors...
        and (LPageCtrl.PageIndex = 1) then
          SourceEditorWindows[LSourceEditorWindow].ActiveDesignFormData := LDesignFormData
        else
          SourceEditorWindows[LSourceEditorWindow].ActiveDesignFormData := nil;
    end;

    case LPageCtrl.PageIndex of
      0: if LDesignFormData <> nil then LDesignFormData.Form.HideWindow;
      1:
        begin
          // Michl: I comment the next line, as it breaks other editors
          // (like Actionlist Editor). Imho in all cases all designer
          // were already active, so it isn't needed. See issue #33872
          // LazarusIDE.DoShowDesignerFormOfSrc(LSourceEditorWindow.ActiveEditor);

          // for lfm edition...
          with LDesignFormData as IDesignedForm do
          if not LDesignFormData.FHiding {and (RealBorderStyle <> bsNone)} then
          begin
              //BeginUpdate;
              //RealBorderIcons := [];
              //RealBorderStyle := bsNone;
              //Form.Show;
              //EndUpdate;
              LPageCtrl.BoundToDesignTabSheet;

              SetTimer(Form.Handle, WM_BOUNDTODESIGNTABSHEET, 10, nil);
          end;
        end;
    end;
  end
  else
  begin
    RefreshAllSourceWindowsModulePageControl;
  end;
end;

class procedure TSpartaMainIDE.EditorDestroyed(Sender: TObject);
var
  LSourceEditor: TSourceEditorInterface;
  LPageCtrl: TModulePageControl;
  LSourceEditorWindow: TSourceEditorWindowInterface;
  LFormData: TDesignFormData;
begin
  // sender is here as special parameter, because is possible situation where is moved editor
  // to another window and was not triggered EditorDestroy - for more info goto editoractivate
  if Sender = nil then
    LSourceEditor := LastActiveSourceEditor
  else
    LSourceEditor := TSourceEditorInterface(Sender);

  // parent don't exist anymore and we must search in each window...
  if Sender = nil then // but not for Sender = nil :P
    LPageCtrl := SourceEditorWindows[LastActiveSourceEditorWindow].FPageCtrlList[LastActiveSourceEditor]
  else
    LPageCtrl := AbsoluteFindModulePageControl(LSourceEditor);

  if LPageCtrl = nil then
    Exit;

  LFormData := FindDesignFormData(LSourceEditor.GetDesigner(False));

  // goto first comment (forced destroy)
  if Sender = nil then
    LSourceEditorWindow := LastActiveSourceEditorWindow
  else
    LSourceEditorWindow := TSourceEditorWindowInterface(LPageCtrl.Owner);

  if LFormData <> nil then
  begin
    SourceEditorWindows[LSourceEditorWindow].ActiveDesignFormData := nil;
    LFormData.Form.LastActiveSourceWindow := nil;
  end;

  SourceEditorWindows[LSourceEditorWindow].RemovePageCtrl(LSourceEditor);
  LPageCtrl.Free;

  if LastActiveSourceEditor = LSourceEditor then
    LastActiveSourceEditor := nil;
end;

class function TSpartaMainIDE.GetCurrentResizer: TResizer;
var
  LForm: TCustomForm;
  LFormData: TDesignFormData;
  LSourceWindow: TSourceEditorWindowInterface;
  LPageCtrl: TModulePageControl;
begin
  Result := nil;
  if (FormEditingHook = nil) or (GlobalDesignHook = nil) then
    Exit;
  LForm := FormEditingHook.GetDesignerForm(GlobalDesignHook.LookupRoot);
  LFormData := FindDesignFormData(LForm);
  if LFormData=nil then Exit;
  LSourceWindow := (LFormData as IDesignedFormIDE).LastActiveSourceWindow;
  LPageCtrl := FindModulePageControl(LSourceWindow);
  Result := LPageCtrl.Resizer;
end;

class procedure TSpartaMainIDE.EditorCreate(Sender: TObject);

var
  LSourceEditor: TSourceEditorInterface;

  function CreateModulePageControl: TModulePageControl;
  var
    LNewTabSheet: TTabSheet;
    LSourceEditorWindow: TSourceEditorWindowInterface;
    LParent: TWinControl;
  begin
    Result := TModulePageControl.Create(LSourceEditor.EditorControl.Owner);

    Result.TabPosition := tpBottom;
    Result.Align:=alClient;
    LParent := LSourceEditor.EditorControl.Parent;

    LNewTabSheet := TTabSheet.Create(Result);
    LNewTabSheet.PageControl := Result;
    LNewTabSheet.Caption := SCode;
    LSourceEditor.EditorControl.Parent := LNewTabSheet;  // ! SynEdit :)

    LNewTabSheet := TTabSheet.Create(Result);
    LNewTabSheet.PageControl := Result;
    LNewTabSheet.Caption := SDesigner;

    Result.OnChange := TabChange;

    Result.Parent := LParent;

    LSourceEditorWindow := TSourceEditorWindowInterface(Result.Owner);
    SourceEditorWindows[LSourceEditorWindow].AddPageCtrl(LSourceEditor, Result)
  end;

begin
  LSourceEditor := Sender as TSourceEditorInterface;
  if not (LSourceEditor.EditorControl.Parent.Parent is TModulePageControl) then
    CreateModulePageControl;
end;

class procedure TSpartaMainIDE.TabChange(Sender: TObject);
var
  LActiveSourceWindow: TSourceEditorWindowInterface;
  w: TSourceEditorWindowInterface;
{$IFDEF USE_GENERICS_COLLECTIONS}
  p: TPair<TSourceEditorInterface, TModulePageControl>;
{$ELSE}
  p: THashmap<TSourceEditorInterface, TModulePageControl, THash_TObject>.TPair;
  LIterator: THashmap<TSourceEditorWindowInterface, TSourceEditorWindowData, THash_TObject>.TIterator;
  LIterator2: THashmap<TSourceEditorInterface, TModulePageControl, THash_TObject>.TIterator;
{$ENDIF}
  LDesigner: TIDesigner;
  LFormData: TDesignFormData;
  LPageCtrl: TModulePageControl;
  LSourceWndData: TSourceEditorWindowData;
begin
  // activate proper source editor window when user is clicking on page.
  // (at clicking time can be active other source window)
  LActiveSourceWindow := TComponent(Sender).Owner as TSourceEditorWindowInterface;
  if LActiveSourceWindow <> SourceEditorManagerIntf.ActiveSourceWindow then
    SourceEditorManagerIntf.ActiveSourceWindow := LActiveSourceWindow;

  LPageCtrl := TModulePageControl(Sender);
  // in case there is no module and is visible page other than code page.
  if (LActiveSourceWindow.ActiveEditor <> nil) and (LPageCtrl <> nil) then
  begin
    LDesigner := LActiveSourceWindow.ActiveEditor.GetDesigner(True);
    LFormData := FindDesignFormData(LDesigner);

{$IFDEF USE_GENERICS_COLLECTIONS}
    if (LFormData <> nil) and SourceEditorWindows.TryGetValue(LActiveSourceWindow, LSourceWndData) then
    begin
      case LPageCtrl.ActivePageIndex of
        0:
          begin
            LSourceWndData.ActiveDesignFormData := nil;
          end;
        1:
          begin
            //  deactivate design tab in other page control :)
            for w in SourceEditorWindows.Keys do
              if w = LActiveSourceWindow then
                Continue
              else
                for p in SourceEditorWindows[w].FPageCtrlList do
                  if (p.Value.DesignFormData = LFormData) and (p.Value <> Sender) then
                  begin
                    IDETabMaster.ShowCode(p.Key);
                  end;

            LSourceWndData.ActiveDesignFormData := LFormData;
            // to handle windows with different size
            LPageCtrl.BoundToDesignTabSheet;
          end;
      end;
    end;
{$ELSE}
    if (LFormData <> nil) and SourceEditorWindows.contains(LActiveSourceWindow) then
    begin
      LSourceWndData := SourceEditorWindows[LActiveSourceWindow];
      case LPageCtrl.ActivePageIndex of
        0:
          begin
            LSourceWndData.ActiveDesignFormData := nil;
          end;
        1:
          begin
            // deactivate design tab in other page control :)
            LIterator := SourceEditorWindows.Iterator;
            if LIterator <> nil then
            try
              repeat
                w := LIterator.Key;
                if w = LActiveSourceWindow then
                  Continue
                else
                begin
                  LIterator2 := SourceEditorWindows[w].FPageCtrlList.Iterator;
                  if LIterator2 <> nil then
                  try
                    repeat
                      p := LIterator2.Data;
                      if (p.Value.DesignFormData = LFormData) and (p.Value <> Sender) then
                        IDETabMaster.ShowCode(p.Key);
                    until not LIterator2.next;
                  finally
                    LIterator2.Free;
                  end;
                end;
              until not LIterator.next;
            finally
              LIterator.Free;
            end;

            LSourceWndData.ActiveDesignFormData := LFormData;
            // enable autosizing after creating a new form, see issue #32207
            TDTXTabMaster(IDETabMaster).EnableAutoSizing(LFormData.Form.Form);
            // to handle windows with different size
            LPageCtrl.BoundToDesignTabSheet;
          end;
      end;
    end;
{$ENDIF}
  end;
end;

class procedure TSpartaMainIDE.GlobalOnChangeBounds(Sender: TObject);
var
  sewd: TSourceEditorWindowData;
{$IFnDEF USE_GENERICS_COLLECTIONS}
  LIterator: THashmap<TSourceEditorWindowInterface, TSourceEditorWindowData, THash_TObject>.TIterator;
{$ENDIF}
begin
{$IFDEF USE_GENERICS_COLLECTIONS}
  for sewd in SourceEditorWindows.Values do
  begin
    sewd.OnChangeBounds(Sender);
  end;
{$ELSE}
  LIterator := SourceEditorWindows.Iterator;
  if LIterator <> nil then
  try
    repeat
      sewd := LIterator.Value;
      sewd.OnChangeBounds(Sender)
    until not LIterator.next;
  finally
    LIterator.Free;
  end;
{$ENDIF}
end;

class procedure TSpartaMainIDE.GlobalSNOnChangeBounds(Sender: TObject);
var
  LWindow: TSourceEditorWindowInterface;
  LWindowData: TSourceEditorWindowData;
  LDesignForm: TDesignFormData;
begin
  // Check parent. Maybe is different? If yes then window changed state (docked/undocked) and we need to perform few actions
  LWindow := Sender as TSourceEditorWindowInterface;

  // dock/undock event :)
{$IFDEF USE_GENERICS_COLLECTIONS}
  if not SourceEditorWindows.TryGetValue(LWindow, LWindowData) then
    Exit;
{$ELSE}
  if not SourceEditorWindows.contains(LWindow) then
    Exit;
  LWindowData := SourceEditorWindows[LWindow];
{$ENDIF}
  if LWindowData.FLastTopParent <> LWindow.GetTopParent then
  begin
    LWindowData.FLastTopParent := LWindow.GetTopParent;
    // refresh for popupparent
    LDesignForm := LWindowData.ActiveDesignFormData;
    LWindowData.ActiveDesignFormData := nil;
    LWindowData.ActiveDesignFormData := LDesignForm;
    if LDesignForm <> nil then
    begin
      LDesignForm.Form.Form.Parent := FindModulePageControl(LWindow).Resizer.ActiveResizeFrame.FormHandler;
      SetTimer(LDesignForm.Form.Form.Handle, WM_BOUNDTODESIGNTABSHEET, 10, nil);
    end;
  end;

  LWindowData.OnChangeBounds(Sender);
end;

class procedure TSpartaMainIDE.OnDesignMouseDown(Sender: TObject;
  Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
  DesignerSetFocus;
end;

class procedure TSpartaMainIDE.OnModifiedSender(Sender: TObject);
begin
  OnModified;
end;

class procedure TSpartaMainIDE.OnModified;
var
  LResizer: TResizer;
begin
  LResizer := GetCurrentResizer;
  if LResizer<>nil then
    LResizer.ActiveResizeFrame.OnModified;
end;

class procedure TSpartaMainIDE.OnModifiedPersistentAdded(
  APersistent: TPersistent; Select: Boolean);
begin
  OnModified;
end;

class procedure TSpartaMainIDE.OnShowDesignerForm(Sender: TObject; AEditor: TSourceEditorInterface;
                                 AComponentPaletteClassSelected: Boolean);
var
  LForm: TDesignFormData;
  LPageCtrl, p: TModulePageControl;
  w: TSourceEditorWindowInterface;
  e: TSourceEditorInterface;
{$IFnDEF USE_GENERICS_COLLECTIONS}
  LIterator: THashmap<TSourceEditorWindowInterface, TSourceEditorWindowData, THash_TObject>.TIterator;
{$ENDIF}
begin
  LForm := FindDesignFormData(TCustomForm(Sender).Designer);
  if LForm = nil then
    Exit;

  if LForm.FHiding then
    Exit;

  LPageCtrl := FindModulePageControl(SourceEditorManagerIntf.ActiveEditor);

  if LPageCtrl = nil then
    Exit; // it should not happen but who knows :P Lazarus IDE is sometimes mischievous

  if AComponentPaletteClassSelected then
  begin
    // if form is already opened do nothing, if not then show form for active module.
{$IFDEF USE_GENERICS_COLLECTIONS}
    for w in SourceEditorWindows.Keys do
    begin
      e := w.ActiveEditor;
      if (e = nil) or (e.GetDesigner(True) <> LForm.Form.Form.Designer) then
        Continue;

      p := FindModulePageControl(e);
      if p.PageIndex = 1 then
        Exit;
    end;
{$ELSE}
    LIterator := SourceEditorWindows.Iterator;
    if LIterator <> nil then
    try
      repeat
        w := LIterator.Key;
        e := w.ActiveEditor;
        if (e = nil) or (e.GetDesigner(True) <> LForm.Form.Form.Designer) then
          Continue;

        p := FindModulePageControl(e);
        if p.PageIndex = 1 then
          Exit;
      until not LIterator.next;
    finally
      LIterator.Free;
    end;
{$ENDIF}
  end;

  IDETabMaster.ShowDesigner(SourceEditorManagerIntf.ActiveEditor);
end;

class procedure TSpartaMainIDE.OnShowSrcEditor(Sender: TObject);
begin
  IDETabMaster.ShowCode(Sender as TSourceEditorInterface);
end;

class procedure TSpartaMainIDE.OnShowMethod(const Name: String);
var
  LForm: TDesignFormData;
  LSecondEditor: TSourceEditorInterface = nil;
  i: Integer;
  LSourceWindow: TSourceEditorWindowInterface;
begin
  if FormEditingHook = nil then
    Exit;
  LForm := FindDesignFormData(FormEditingHook.GetCurrentDesigner);
  if LForm = nil then
    Exit;

  for i := 0 to SourceEditorManagerIntf.SourceWindowCount - 1 do
  begin
    LSourceWindow := SourceEditorManagerIntf.SourceWindows[i];
    if LForm.Form.LastActiveSourceWindow = LSourceWindow then
      Continue;

    if LSourceWindow.ActiveEditor <> nil then
      if LSourceWindow.ActiveEditor.GetDesigner(True) = LForm.Form.Form.Designer then
      begin
        LSecondEditor := LSourceWindow.ActiveEditor;
        Break;
      end;
  end;

  if LSecondEditor = nil then
  begin
    if LForm.Form.LastActiveSourceWindow <> nil then
    begin
      IDETabMaster.ShowCode(LForm.Form.LastActiveSourceWindow.ActiveEditor);
    end;
  end
  else
  begin
    IDETabMaster.ShowCode(LSecondEditor);
  end;

  if LSecondEditor <> nil then
  begin
    LazarusIDE.DoShowMethod(LSecondEditor, Name);
  end;
end;

class procedure TSpartaMainIDE.OnDesignRefreshPropertyValues;
var
  LForm: TCustomForm;
  LSourceWindow: TSourceEditorWindowInterface;
  LFormData: TDesignFormData;
  LPageCtrl: TModulePageControl;

  function RootIsSelected: Boolean;
  var
    LSelection: TPersistentSelectionList;
    i: integer;
  begin
    Result := False;
    LSelection := TPersistentSelectionList.Create;
    GlobalDesignHook.GetSelection(LSelection);
    for i := 0 to LSelection.Count - 1 do
      if LSelection.Items[i] = GlobalDesignHook.LookupRoot then
      begin
        Result := True;
        Break;
      end;
    LSelection.Free;
  end;

begin
  if (GlobalDesignHook.LookupRoot is TCustomFrame) then
  begin
    if not RootIsSelected then
      Exit;

    LForm := FormEditingHook.GetDesignerForm(GlobalDesignHook.LookupRoot);
    LFormData := FindDesignFormData(LForm);
    LSourceWindow := (LFormData as IDesignedFormIDE).LastActiveSourceWindow;
    LPageCtrl := FindModulePageControl(LSourceWindow);
    TFakeFrame(LForm).SetBounds(LForm.Left-1,LForm.Top-1,TFakeFrame(LForm).Width,TFakeFrame(LForm).Height);
    //LPageCtrl.BoundToDesignTabSheet;
  end
  else
  if (GlobalDesignHook.LookupRoot is TCustomForm) then
  begin
    if not RootIsSelected then
      Exit;

    LForm := TCustomForm(GlobalDesignHook.LookupRoot);
    LFormData := FindDesignFormData(LForm);
    LFormData.RepaintFormImages;
  end;
end;

{$IFnDEF USE_GENERICS_COLLECTIONS}
class procedure FreeSourceEditorWindowsValues;
var
  LIterator: THashmap<TSourceEditorWindowInterface, TSourceEditorWindowData, THash_TObject>.TIterator;
begin
  LIterator := SourceEditorWindows.Iterator;
  if LIterator <> nil then
  try
    repeat
      LIterator.Value.Free;
    until not LIterator.next;
  finally
    LIterator.Free;
  end;
end;
{$ENDIF}

initialization
  dsgnForms := Classes.TList.Create;
{$IFDEF USE_GENERICS_COLLECTIONS}
  SourceEditorWindows := TObjectDictionary<TSourceEditorWindowInterface, TSourceEditorWindowData>.Create([doOwnsValues]);
{$ELSE}
  SourceEditorWindows := THashmap<TSourceEditorWindowInterface, TSourceEditorWindowData, THash_TObject>.Create();
{$ENDIF}
  normForms := Classes.TList.Create;

finalization
  normForms.Free;
{$IFnDEF USE_GENERICS_COLLECTIONS}
  FreeSourceEditorWindowsValues;
{$ENDIF}
  SourceEditorWindows.Free;
  FreeAndNil(dsgnForms);
end.