-
Notifications
You must be signed in to change notification settings - Fork 0
/
BitMapEditorDialog.pas
4172 lines (3745 loc) · 118 KB
/
BitMapEditorDialog.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
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
//License <---- Read me
//MIT License
//
//Copyright (c) 2019 Everest Software LLC https://www.hmisys.com
//
//Permission is hereby granted, free of charge, to any person obtaining a copy
//of this software and associated documentation files (the "Software"), to deal
//in the Software without restriction, including without limitation the rights
//to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//copies of the Software, and to permit persons to whom the Software is
//furnished to do so, subject to the following conditions:
//
//The above copyright notice and this permission notice shall be included in all
//copies or substantial portions of the Software.
//
//THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
//SOFTWARE.
unit BitMapEditorDialog;
interface
uses Winapi.Windows,Winapi.Messages,System.SysUtils,System.Variants,System.Classes,
Vcl.Graphics,Vcl.Controls,Vcl.Forms,Vcl.Dialogs,Vcl.ExtCtrls,Vcl.Menus,
System.ImageList,Vcl.ImgList,Vcl.StdCtrls,Vcl.ComCtrls,Vcl.ToolWin,Vcl.Grids;
type
TBMEditorMain = class;
TPointsArray = array of TPoint;
TBMDrawingSurface = class;
{$REGION 'bmSettingsRecord define'}
bmSettingsRec = record
bitmap:TBitmap;
colors:array[0..29] of TColor;
canAlterSize:boolean;
windowTop,windowLeft,windowWidth,windowHeight,maxUndoCount:integer;
maxBitmapWidth,maxBitmapHeight,minBitmapWidth,minBitmapHeight:integer; //set to -1 to ignore
procedure AssignIn(inSettings:bmSettingsRec);
procedure AssignOut(inSettings:bmSettingsRec);
procedure Initialize;
end;
{$ENDREGION}
{$REGION 'seletRectsRec'}
selectRectsRec = record
index:integer;
totalRect:TRect;
edgeRects:array [0..7] of TRect;
drawingSurface:TBMDrawingSurface;
procedure DrawLineSelectionRectangles(aCanvas:TCanvas);
procedure DrawSelectionRectangle(aCanvas:TCanvas);
procedure DrawSelectionRectangles(aCanvas:TCanvas);
procedure GetWidthHeight(var w,h:integer);
function PointInResizeRectangle(aPt:TPoint):integer;
function PointInTotalRectangle(aPt: TPoint):boolean;
end;
{$ENDREGION}
{$REGION 'selectionRecord'}
selectionRecord = record
clipRgn:HRGN;
bMap:TBitmap;
pointsCount:integer;
rects:selectRectsRec;
active,moving,resizing:boolean;
pointsArray:TPointsArray;
procedure CopyToBitMapFromCanvas(aCanvas:TCanvas; aRect:TRect);
procedure Draw(aCanvas:TCanvas);
procedure Move(x,y:integer);
function PointInBounds(aPt:TPoint):boolean;
function PointInResize(aPt:TPoint):integer;
procedure Reset;
procedure ResetPointsVaribles;
end;
{$ENDREGION}
{$REGION 'TBMDrawingSurface define'}
TBMDrawingSurface = class(TCustomPanel)
protected
procedure Paint;override;
procedure WmEraseBkgnd(var Msg: TWmEraseBkgnd);message WM_ERASEBKGND;
private
scaleIndex:integer;
xForm,normalxForm:TXForm; //used for scaling
selectRec:selectionRecord;
property canvas;
property OnMouseMove;
property OnMouseUp;
property OnMouseDown;
procedure Draw;
function GetScaleAmount:single;
function GetScalePercent:string;
procedure SetPenForpmNotXor(pStyle:TPenStyle = psDot);
procedure SetScalingIfOn;
procedure SetScalingNormal;
procedure SetUp(w,h:integer);
procedure TearDown;
end;
{$ENDREGION}
{$REGION 'graphic element define'}
geRecord = record
bMap:TBitmap;
shapeType:integer; //same as tool button values
penStyle:TPenStyle;
brushStyle:TBrushStyle;
selectRect:selectRectsRec;
penColor,fillColor:TColor;
drawSurface:TBMDrawingSurface;
penWidth,pointsCount:integer;
pointsArray:TPointsArray;
selected,moving,resizing:boolean;
text,fontName:string;
fontSize:integer;
fontStyle:TFontStyles;
fontOpaque:boolean;
procedure Assign(var varRec:geRecord);
procedure Destroy;
procedure Draw(aCanvas:TCanvas);
procedure Move(x,y:integer);
function PointInBounds(aPt:TPoint):boolean;
function PointInResize(aPt:TPoint):integer;
procedure ResizeComplete;
procedure SelectionRectangleChange(diffPt:TPoint; boundRect:TRect);
function TestMouseDownBitmapStyle(x,y:integer):boolean;
procedure TransferPolyPointToCanvas(aCanvas:TCanvas);
end;
{$ENDREGION}
{$REGION 'TUndoItem'}
TUndoItem = class
aPt:TPoint; //for moves
bMap:TBitMap;
clipRgn:HRGN;
action:integer; //see consts below
penStyle:TPenStyle;
brushStyle:TBrushStyle;
totalRect,redoRect:TRect;
penColor,fillColor:TColor;
shapeType,penWidth:integer;
destructor Destroy;override;
function SaveFromGE(ge:geRecord):boolean;
procedure SaveFromUi(unUI:TUndoItem);
procedure SaveToGE(var ge:geRecord);
end;
{$ENDREGION}
{$REGION 'TUndoController'}
TUndoController = class
index:integer;
undoList:TList;
form:TBMEditorMain;
procedure FreeUndoList;
constructor Create;
destructor Destroy;override;
procedure PreSaveCheck;
procedure RedoUserAction;
procedure SaveAttAChangeAction(action:integer);
procedure SaveBitmap(action:integer);
procedure SaveCreateAction(action:integer);
procedure SaveMoveAction(action:integer; aPt:TPoint);
procedure SaveResizeAction(action:integer; aRect:TRect);
procedure UndoUserAction;
procedure ZeroListOfNonBMSave;
end;
{$ENDREGION}
{$REGION 'resizeRecord'}
resizeRecord = record
mode:integer; //0=percentage, 1 = pixels
horzValue,vertValue:integer;
keepAspectRatio:boolean;
ratio:double;
inRect:TRect;
isGE:boolean; //if false is select rec
procedure Reset;
end;
{$ENDREGION}
TBMEditorMain = class(TForm)
TopMenuBar: TMainMenu;
FileMI: TMenuItem;
EditMI: TMenuItem;
SaveAndExitMI: TMenuItem;
ExitMI: TMenuItem;
N1: TMenuItem;
PropertiesMI: TMenuItem;
UndoMI: TMenuItem;
RedoMI: TMenuItem;
N2: TMenuItem;
CutMI: TMenuItem;
CopyMI: TMenuItem;
PasteMI: TMenuItem;
TopPanel: TPanel;
N3: TMenuItem;
SelectallMI: TMenuItem;
N4: TMenuItem;
RotateMI: TMenuItem;
ResizeMI: TMenuItem;
ToolsBtnBar: TToolBar;
PencilBtn: TToolButton;
ToolBarImageList: TImageList;
BucketBtn: TToolButton;
TextBtn: TToolButton;
EraserBtn: TToolButton;
DropperBtn: TToolButton;
ToolBar1: TToolBar;
NoFillBtn: TToolButton;
BrushSolidBtn: TToolButton;
ToolButton3: TToolButton;
ToolButton4: TToolButton;
ToolButton5: TToolButton;
ToolButton6: TToolButton;
ToolButton7: TToolButton;
FillImageList: TImageList;
ToolButton8: TToolButton;
SelectToolBar: TToolBar;
SelectBtn: TToolButton;
FreeSelectBtn: TToolButton;
SelectImageList: TImageList;
CircleBtn: TToolButton;
RectangleBtn: TToolButton;
LineToolBtn: TToolButton;
RoundRectBtn: TToolButton;
PenWidthCombo: TComboBox;
PenStyleCombo: TComboBox;
PenPaintBox: TPaintBox;
FillPaintBox: TPaintBox;
ColorGrid: TDrawGrid;
OnlyColorDialog: TColorDialog;
MainScrollBox: TScrollBox;
PropertiesPanel: TPanel;
PropertyCloseBtn: TButton;
Label1: TLabel;
Label2: TLabel;
BMHeightEdit: TEdit;
BMWidthEdit: TEdit;
TextPanel: TPanel;
Label4: TLabel;
TextEditCloseBtn: TButton;
FontCombo: TComboBox;
FontNameLbl: TLabel;
BoldCB: TCheckBox;
ItalicCB: TCheckBox;
UnderlineCB: TCheckBox;
StrikeoutCB: TCheckBox;
FontSizeComboLbl: TLabel;
FontSizeCombo: TComboBox;
OpaqueCB: TCheckBox;
TextMemoEdit: TMemo;
FlipMI: TMenuItem;
FlipHorizontalMI: TMenuItem;
FlipverticalMI: TMenuItem;
ResizePanel: TPanel;
ResizePanelCancelBtn: TButton;
ResizePanelOKBtn: TButton;
ResizeRG: TRadioGroup;
ResizeHorizontalLbl: TLabel;
ReaizeVerticalLbl: TLabel;
ResizeHorzEdit: TEdit;
ResizeVertEdit: TEdit;
ResizeMaintainCB: TCheckBox;
BottomPanel: TPanel;
BottomBar: TStatusBar;
ZoomPanel: TPanel;
ZoomTrackBar: TTrackBar;
ZoomLabel: TLabel;
procedure ExitMIClick(Sender: TObject);
procedure PencilBtnClick(Sender: TObject);
procedure SelectBtnClick(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
procedure FormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
procedure FormMouseWheel(Sender: TObject; Shift: TShiftState; WheelDelta: Integer; MousePos: TPoint; var Handled: Boolean);
procedure PenWidthComboDrawItem(Control: TWinControl; Index: Integer; Rect: TRect; State: TOwnerDrawState);
procedure PenStyleComboDrawItem(Control: TWinControl; Index: Integer; Rect: TRect; State: TOwnerDrawState);
procedure PenPaintBoxClick(Sender: TObject);
procedure PenPaintBoxPaint(Sender: TObject);
procedure ColorGridDrawCell(Sender: TObject; ACol, ARow: Integer;Rect: TRect; State: TGridDrawState);
procedure ColorGridMouseUp(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
procedure ColorGridDblClick(Sender: TObject);
procedure PropertiesMIClick(Sender: TObject);
procedure PropertyCloseBtnClick(Sender: TObject);
procedure PenWidthComboSelect(Sender: TObject);
procedure PenStyleComboSelect(Sender: TObject);
procedure NoFillBtnClick(Sender: TObject);
procedure UndoMIClick(Sender: TObject);
procedure EditMIClick(Sender: TObject);
procedure TopPanelMouseEnter(Sender: TObject);
procedure BoldCBClick(Sender: TObject);
procedure FontComboClick(Sender: TObject);
procedure TextMemoEditChange(Sender: TObject);
procedure TextEditCloseBtnClick(Sender: TObject);
procedure CopyMIClick(Sender: TObject);
procedure CutMIClick(Sender: TObject);
procedure PasteMIClick(Sender: TObject);
procedure SelectallMIClick(Sender: TObject);
procedure RotateMIClick(Sender: TObject);
procedure ResizeMIClick(Sender: TObject);
procedure FlipHorizontalMIClick(Sender: TObject);
procedure ResizePanelCancelBtnClick(Sender: TObject);
procedure ResizeHorzEditChange(Sender: TObject);
procedure ResizePanelOKBtnClick(Sender: TObject);
procedure ResizeRGClick(Sender: TObject);
procedure FormResize(Sender: TObject);
procedure ZoomTrackBarChange(Sender: TObject);
procedure FileMIClick(Sender: TObject);
procedure FormShow(Sender: TObject);
procedure SaveAndExitMIClick(Sender: TObject);
protected
procedure AcceptFiles(var Message:TMessage ); message WM_DROPFILES;
private
ge:geRecord;
workBM:TBitmap;
brushStlye:TBrushStyle;
undoer:TUndoController;
resizeRec:resizeRecord;
penColor,fillColor:TColor;
leftMouseDownActive:boolean;
drawSurface:TBMDrawingSurface;
eraserPointsArray:TPointsArray;
mouseDownPt,mouseAtNowPoint:TPoint;
selectedColor,selectedTool,eraserPointsCount:integer;
procedure BlockResizeChangeEvent(blockIt:boolean);
procedure DoBucket(Button: TMouseButton);
procedure DoEraser(aPt:TPoint);
procedure DeSelectAllOnToolBar(aBar:TToolBar);
procedure DoFreeSelectToolMouseUp;
procedure DrawSurfaceMouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
procedure DrawSurfaceMouseMove(Sender: TObject; Shift: TShiftState; X,Y: Integer);
procedure DrawSurfaceMouseUp(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
procedure DropperMouseDown(Button: TMouseButton);
procedure EndGECreation;
procedure EndGETextCreation;
procedure FillRectangleWithFillColor(aRect:TRect);
procedure HandleMouseDownInSelectionRectangle(index:integer);
procedure NewGEMouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
procedure NewGEMouseMove(Sender: TObject; Shift: TShiftState; X,Y: Integer);
procedure NewGEMouseUp(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
procedure NewGELineMouseUp(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
procedure NewGEPolyMouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
procedure NewGEPolyMouseMove(Sender: TObject; Shift: TShiftState; X,Y: Integer);
procedure NewGEPolyMouseUp(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
procedure NewGETextMouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
procedure NewGETextMouseMove(Sender: TObject; Shift: TShiftState; X,Y: Integer);
procedure NewGETextMouseUp(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
procedure RestoreOnMouseKeyEvents;
procedure SaveTextStuffToGE;
procedure SelectionMadeMouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
procedure SelectionMadeMouseMove(Sender: TObject; Shift: TShiftState; X,Y: Integer);
procedure SelectionMadeMouseUp(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
procedure SetFillColor(aColor:TColor);
procedure SetGeProperties;
procedure SetSizesForDrawing;
procedure SetToolButton(value:integer);
procedure ShowHideRezisePanel(state:boolean);
procedure ShowHideTextPanel(state:boolean);
procedure UndoRedo(which:integer);
procedure UpdateStatusBar(x:integer = 0; y:integer = 0);
procedure ZoomTheViewFinal;
public
{ Public declarations }
end;
function LaunchBitMapEditor(var inSettings:bmSettingsRec):boolean;
implementation
{$R *.dfm}
{$R BMCursor.res}
uses System.UITypes,System.Types,Math,Vcl.Clipbrd,RotateBitmapUnit,Winapi.ShellAPI,
Winapi.ShlObj,Winapi.ActiveX,System.Win.ComObj,Vcl.Imaging.pngimage,Vcl.Imaging.jpeg;
var
gSettings:bmSettingsRec;
BMEditorMain:TBMEditorMain;
function LaunchBitMapEditor(var inSettings:bmSettingsRec):boolean;
begin
gSettings.AssignIn(inSettings);
BMEditorMain:=TBMEditorMain.Create(nil);
try
result:=(BMEditorMain.ShowModal = mrOk);
inSettings.AssignOut(gSettings);
finally
BMEditorMain.Release;
end;
end;
{$REGION 'const'}
const
cComboColor = $00FF8000;
cOurFont = 'Calibri';
cWordClear = 'Clear';
cBuckCursor = 1;
cEraserCursor = 2;
cSelectTool = 0; cFreeSelectTool = 1;
cPencilTool = 2; cTextTool = 3;
cBucketTool = 4; cLineTool = 5;
{cZoomTool = 6;} cRectangleTool = 7;
cEraserTool = 8; cCircleTool = 9;
cDropperTool = 10; cRoundRectTool = 11;
cBitmapTool = 255;
//bottom status bar
cBBSpacer = 0; cBBx = 2;
cBBy = 4; cBBw = 6;
cBBh = 8; cBBScale = 9;
//undo
cUndoGECreate = 1; cUndoSelectCreate = 2;
cUndoSelectMove = 3; cUndoGEResize = 4;
cUndoSelectResize = 5; cUndoGEAttributeChange = 6;
cUndoGEMove = 7; cUndoSaveBM = 8;
cUndoSelectFreeCreate = 9;
ROP_DstCopy = $00AA0029;//$00220326;
type
scaleRecord = record
amount:single;
percentString:string;
end;
var
cScaleRecords: array [0..9] of scaleRecord =
((amount:0.25; percentString:'25'),
(amount:0.5; percentString:'50'),
(amount:0.75; percentString:'75'),
(amount:1.0; percentString:'100'),
(amount:1.25; percentString:'125'),
(amount:1.50; percentString:'150'),
(amount:1.75; percentString:'175'),
(amount:2.0; percentString:'200'),
(amount:4.0; percentString:'400'),
(amount:8.0; percentString:'800'));
{$ENDREGION}
{$REGION 'misc functions'}
function CalculateOutsideRectangleForPolygon(pap:TPointsArray; count:integer):TRect;
var
aPoint:TPoint;
i,left,top,right,bottom:integer;
begin
//we need to determine the rectangle that bounds the points
left:=maxInt;
top:=maxInt;
right:=0;
bottom:=0;
for i:= 0 to count - 1 do
begin
aPoint:=pap[i];
if (aPoint.x < left) then
left:=aPoint.x;
if (aPoint.x > right) then
right:=aPoint.x;
if (aPoint.y < top) then
top:=aPoint.y;
if (aPoint.y > bottom) then
bottom:=aPoint.y;
end;
result:=Rect(left,top,right,bottom);
end;
procedure DetermineSelectRects(ownerRect:TRect; var selectRects:array of TRect);
var
midH,midV:integer;
begin
//top left
selectRects[0].left:=ownerRect.left - 3;
selectRects[0].top:=ownerRect.top - 3;
selectRects[0].right:=ownerRect.left + 3;
selectRects[0].bottom:=ownerRect.top + 3;
//top middle
midH:=(ownerRect.right + ownerRect.left) div 2;
selectRects[1].left:=midH - 3;
selectRects[1].top:=ownerRect.top - 3;
selectRects[1].right:=midH + 3;
selectRects[1].bottom:=ownerRect.top + 3;
//top right
selectRects[2].left:=ownerRect.right - 3;
selectRects[2].top:=ownerRect.top - 3;
selectRects[2].right:=ownerRect.right + 3;
selectRects[2].bottom:=ownerRect.top + 3;
//right middle
midV:=(ownerRect.bottom + ownerRect.top) div 2;
selectRects[3].left:=ownerRect.right - 3;
selectRects[3].top:=midV - 3;
selectRects[3].right:=ownerRect.right + 3;
selectRects[3].bottom:=midV + 3;
//right bottom
selectRects[4].left:=ownerRect.right - 3;
selectRects[4].top:=ownerRect.bottom - 3;
selectRects[4].right:=ownerRect.right + 3;
selectRects[4].bottom:=ownerRect.bottom + 3;
//bottom middle
selectRects[5]:=selectRects[1];
OffsetRect(selectRects[5],0,(ownerRect.bottom - ownerRect.top));
//bottom left
selectRects[6]:=selectRects[0];
OffsetRect(selectRects[6],0,(ownerRect.bottom - ownerRect.top));
//middle left
selectRects[7]:=selectRects[3];
OffsetRect(selectRects[7],-(ownerRect.right - ownerRect.left),0);
end;
procedure DetermineSelectRectsLine(ownerRect:TRect; var selectRects:array of TRect);
begin
//start point
selectRects[0].left:=ownerRect.left - 3;
selectRects[0].top:=ownerRect.top - 3;
selectRects[0].right:=ownerRect.left + 3;
selectRects[0].bottom:=ownerRect.top + 3;
//end point
selectRects[4].left:=ownerRect.right - 3;
selectRects[4].top:=ownerRect.bottom - 3;
selectRects[4].right:=ownerRect.right + 3;
selectRects[4].bottom:=ownerRect.bottom + 3;
end;
function DuplicateHRGN(inHRGN:HRGN):HRGN;
begin
result:=CreateRectRgn(0,0,0,0);
CombineRgn(result,inHRGN,0,RGN_COPY);
end;
procedure FrameRectEX(aCanvas:TCanvas; rectangleToFrame:TRect);
begin
with rectangleToFrame do
aCanvas.Polygon([Point(left,top),
Point(right,top), //top line
Point(right, bottom), //right line
Point(left, bottom)]); //bottom line
end;
function GetFontsListSansAmpersand:TStringList;
var
i:integer;
begin
result:=TStringList.Create;
Screen.ResetFonts;
for i:=0 to Screen.Fonts.Count - 1 do
if (pos('@',Screen.Fonts[i]) = 0) then
result.Add(Screen.Fonts[i]);
end;
function ICONToBitmap(const fileName:string):TBitmap;
var
hIcon:THandle;
info:TIconInfo;
begin
result:=nil;
hIcon:=0;
try
hIcon:=LoadImage(0,PChar(fileName),IMAGE_ICON,0, 0,LR_LOADFROMFILE);
if (hIcon = 0) then
Exit;
result:=TBitMap.Create;
if not Assigned(result) then
Exit;
result.handleType:=bmDIB;
GetIconInfo(hIcon,info);
result.Width:=(info.xHotspot * 2);
result.Height:=(info.yHotspot * 2);
DrawIconEx(result.Canvas.Handle, 0, 0, hIcon, 0, 0, 0, 0, DI_IMAGE);
finally
if (hIcon <> 0) then
DestroyIcon(hIcon);
end;
end;
function IndexFromStringArray(const valueString:string; stringArray:array of string):integer;
var
i:integer;
begin
result:=-1;
for i:= low(stringArray) to high(stringArray) do
if (valueString = stringArray[i]) then
begin
result:=i;
Break;
end
end;
function IsLinkFile(const fName:string):boolean;
begin
result:=(UpperCase(ExtractFileExt(fName)) = '.LNK');
end;
function LightenColor(percent:byte; aColor:TColor):TColor;
var
r,g,b:integer;
begin
{$R-}
r:=GetRValue(aColor);
g:=GetGValue(aColor);
b:=GetBValue(aColor);
r:=Round((R * percent) / 100) + Round(255 - percent / 100 * 255);
g:=Round((G * percent) / 100) + Round(255 - percent / 100 * 255);
b:=Round((B * percent) / 100) + Round(255 - percent / 100 * 255);
result:=RGB(r,g,b);
{$R+}
end;
procedure Lock45(left,top:integer; var right,bottom:integer);
var
offSet:integer;
begin
offSet:=abs(right - left);
if right > left then
right:=left + offSet //1-2, 10-11 o clock
else
right:=left - offSet; //4-5, 7-8
if top > bottom then
bottom:=top - offSet //1-2, 10-11 o clock
else
bottom:=top + offSet; //4-5, 7-8
end;
procedure LineLocking(left,top:integer; var right,bottom:integer);
var
ratio:double;
begin
if (right = left) or (bottom = top) then
Exit;
ratio:=abs((right - left) / (bottom - top));
if ratio > 1.75 then
bottom:=top //we are closer to the horziontal plane
else if ratio < 0.25 then
right:=left //we are closer to the vertical plane
else
Lock45(left,top,right,bottom);
end;
procedure MapArrayOfPoints(thePoints:TPointsArray; pointCount:integer;
sourceRectangle,destinationRectangle:TRect);
var
i,x:integer;
percentFromLeft,percentFromTop:double;
sourceWidth,destinationWidth,sourceHeight,destinationHeight:integer;
begin
sourceWidth:=sourceRectangle.right - sourceRectangle.left;
sourceHeight:=sourceRectangle.bottom - sourceRectangle.top;
destinationWidth:=destinationRectangle.right - destinationRectangle.left;
destinationHeight:=destinationRectangle.bottom - destinationRectangle.top;
for i:= 0 to pointCount - 1 do
begin
//calculate the distance from the left edge of the source rectangle and then
//calculate the new point from the new rectangle
x:=thePoints[i].x - sourceRectangle.left;
if (sourceWidth <> 0) then
percentFromLeft:=(x / sourceWidth)
else
percentFromLeft:=1;
thePoints[i].x:=Round((percentFromLeft * destinationWidth) + destinationRectangle.left);
//do the same for the vertical value
x:=thePoints[i].y - sourceRectangle.top;
if (sourceHeight <> 0) then
percentFromTop:=(x / sourceHeight)
else
percentFromTop:=1;
thePoints[i].y:=Round((percentFromTop * destinationHeight) + destinationRectangle.top);
end;
end;
procedure ResiseRect(i: integer; diffPt: TPoint; var aRect:TRect);
begin
case i of
0:
begin
aRect.Top:=aRect.Top + diffPt.Y;
aRect.Left:=aRect.Left + diffPt.X;
end;
1: aRect.Top:=aRect.Top + diffPt.Y;
2:
begin
aRect.Top:=aRect.Top + diffPt.Y;
aRect.Right:=aRect.Right + diffPt.X;
end;
3: aRect.right:=aRect.right + diffPt.x;
4:
begin
aRect.Bottom:=aRect.Bottom + diffPt.Y;
aRect.Right:=aRect.Right + diffPt.X;
end;
5: aRect.Bottom:=aRect.Bottom + diffPt.y;
6:
begin
aRect.Bottom:=aRect.Bottom+ diffPt.Y;
aRect.Left:=aRect.Left + diffPt.X;
end;
7: aRect.Left:=aRect.Left + diffPt.x;
end;
end;
function ResolveShortCutLink( const path: String ): String;
var
link: IShellLink;
storage:IPersistFile;
filedata:TWin32FindData;
buf: Array[0..MAX_PATH] of Char;
widepath:WideString;
begin
OleCheck(CoCreateInstance(CLSID_ShellLink, nil,CLSCTX_INPROC_SERVER,
IShellLink, link ));
OleCheck(link.QueryInterface( IPersistFile,storage ));
widepath:=path;
Result:='';
if Succeeded(storage.Load(@widepath[1], STGM_READ )) then
if Succeeded(link.Resolve(GetActiveWindow, SLR_NOUPDATE )) then
if Succeeded(link.GetPath(buf,sizeof(buf),filedata, SLGP_UNCPRIORITY)) then
Result:=buf;
storage := nil;
link:= nil;
end;
procedure ScalePointFromNative(var x,y:integer; amount:single);
begin
if (amount = 1) then //if 1, we are not zoomed
Exit;
x:=round(x * amount);
y:=round(y * amount);
end;
procedure ScalePointToNative(var x,y:integer; amount:single);
begin
if (amount = 1) then //if 1, we are not zoomed
Exit;
x:=round(x * (1 / amount));
y:=round(y * (1 / amount));
end;
procedure ScaleRectangleFromNative(var aRect:TRect; amount:single);
begin
//if 1 then we are not zoomed
if (amount = 1) then
Exit;
with aRect do
begin
left:=round(left * amount);
top:=round(top * amount);
right:=round(right * amount);
bottom:=round(bottom * amount);
end;
end;
procedure ScaleRectangleToNative(var aRect:TRect; amount:single);
begin
//if 1 then we are not zoomed
if (amount = 1) then
Exit;
with aRect do
begin
left:=round(left * (1 / amount));
top:=round(top * (1 / amount));
right:=round(right * (1 / amount));
bottom:=round(bottom * (1 / amount));
end;
end;
procedure SetPenForpmNotXor(aCanvas:TCanvas; pStyle:TPenStyle = psDot);
begin
aCanvas.pen.width:=1;
aCanvas.pen.style:=pStyle;
if (pStyle = psDot) then
aCanvas.pen.mode:=pmNotXor
else
aCanvas.pen.mode:=pmCopy;
aCanvas.pen.color:=clBlack xor clBlack;
aCanvas.brush.color:=clWhite;
end;
function SubtractPoints(const pointA, pointB:TPoint):TPoint;
begin
result.x:=pointA.x - pointB.x;
result.y:=pointA.y - pointB.y
end;
procedure SwapTwo(var a,b:integer);inline;
var
t:integer;
begin
t:=b;
b:=a;
a:=t;
end;
procedure ValidateLine(var aRect:TRect);
begin
//we want the line left to always be the starting point
if (aRect.left > aRect.right) then
begin
SwapTwo(aRect.left,aRect.right);
SwapTwo(aRect.top,aRect.bottom);
end;
end;
procedure ValidateRectangle(var aRect:TRect);
begin
//we want a rectangle left and top to be less than the right and bottom
if (aRect.top > aRect.bottom) then
SwapTwo(aRect.top,aRect.bottom);
if (aRect.left > aRect.right) then
SwapTwo(aRect.left,aRect.right);
end;
{$ENDREGION}
{$REGION 'bmSettingsRecord implement'}
procedure bmSettingsRec.AssignIn(inSettings:bmSettingsRec);
var //for incoming
i:integer;
begin
bitmap:=inSettings.bitmap;
windowTop:=inSettings.windowTop;
windowLeft:=inSettings.windowLeft;
windowWidth:=inSettings.windowWidth;
windowHeight:=inSettings.windowHeight;
for i:= 0 to high(colors) do
colors[i]:=inSettings.colors[i];
maxBitmapWidth:=inSettings.maxBitmapWidth;
maxBitmapHeight:=inSettings.maxBitmapHeight;
minBitmapWidth:=inSettings.minBitmapWidth;
minBitmapHeight:=inSettings.minBitmapHeight;
maxUndoCount:=inSettings.maxUndoCount;
canAlterSize:=inSettings.canAlterSize;
end;
procedure bmSettingsRec.AssignOut(inSettings:bmSettingsRec);
begin
windowTop:=inSettings.windowTop;
windowLeft:=inSettings.windowLeft;
windowWidth:=inSettings.windowWidth;
windowHeight:=inSettings.windowHeight;
end;
procedure bmSettingsRec.Initialize;
var
i:integer;
begin
windowTop:=100;
windowLeft:=100;
windowWidth:=800;
windowHeight:=620;
colors[0]:=clWhite; colors[1]:=clBlack; colors[2]:=clRed;
colors[3]:=clGreen; colors[4]:=clBlue; colors[5]:=clYellow;
colors[6]:=clNavy; colors[7]:=clPurple; colors[8]:=clTeal;
colors[9]:=clGray; colors[10]:=clSilver; colors[11]:=clLime;
colors[12]:=clMaroon; colors[13]:=clOlive; colors[14]:=clFuchsia;
colors[15]:=clAqua; colors[16]:=clLtGray; colors[17]:=clDkGray;
colors[18]:=clMoneyGreen; colors[19]:=TColor($0080FF); //orange;
for i:=20 to high(colors) do //for customr colors
colors[i]:=clNone;
//load any colors here
maxBitmapWidth:=-1; maxBitmapHeight:=-1;
minBitmapWidth:=-1; minBitmapHeight:=-1;
maxUndoCount:=100;
canAlterSize:=true;
end;
{$ENDREGION}
procedure TBMEditorMain.AcceptFiles(var Message: TMessage);
const
cnMaxFileNameLen = 255;
cFileExt:array [0..2] of string =('.bmp','.ico','.png');
cBMPExt = 0;
cICOExt = 1;
cPNGExt = 2;
var
newBitMap:TBitMap;
fileExt,s1:string;
index,nCount:integer;
newPNGFile:TPngImage;
acFileName:array [0..cnMaxFileNameLen] of char;
begin
if ge.selected and not IsRectEmpty(ge.selectRect.totalRect)then
EndGECreation;
try
RestoreOnMouseKeyEvents;
drawSurface.selectRec.Reset;
nCount:=DragQueryFile(Message.WParam,$FFFFFFFF,acFileName,cnMaxFileNameLen); //how many files
if (nCount > 1) then
begin
ShowMessage('Only one file per drag and drop operation.');
Exit;
end;
DragQueryFile(Message.WParam, 0, acFileName, cnMaxFileNameLen);
fileExt:=LowerCase(ExtractFileExt(acFileName));
s1:=acFileName;
if IsLinkFile(s1) then
s1:=ResolveShortCutLink(s1);
if not FileExists(s1) then //make sure the file is there
Exit;
index:=IndexFromStringArray(fileExt,cFileExt);
case index of
cBMPExt: //bmp
begin
newBitMap:=TBitMap.Create;
if not Assigned(newBitMap) then
Exit;
newBitMap.handleType:=bmDIB;
newBitMap.LoadFromFile(s1);
if newBitMap.Empty then
begin
newBitMap.Free;
Exit;
end;
end;
cICOExt: //ico
begin
newBitMap:=ICONToBitmap(s1);
if not Assigned(newBitMap) or newBitMap.Empty then
begin
newBitMap.Free;
Exit;
end;
end;
cPNGExt:
begin
newBitMap:=TBitMap.Create;
if not Assigned(newBitMap) then
Exit;
newPNGFile:=TPngImage.Create;
if not Assigned(newPNGFile) then
Exit;
try
newPNGFile.LoadFromFile(s1);
newBitMap.SetSize(newPNGFile.Width,newPNGFile.Height);
newBitMap.Canvas.CopyRect(Rect(0,0,newPNGFile.Width,newPNGFile.Height),
newPNGFile.Canvas,Rect(0,0,newPNGFile.Width,newPNGFile.Height));
finally
newPNGFile.Free;
end;
end
else
begin
ShowMessage('Unknown file type' + ' ' + acFileName);
Exit;
end;
end;
if Assigned(ge.bMap) then
ge.bMap.Free;
ge.bMap:=newBitMap;
ge.shapeType:=cBitmapTool;
ge.selectRect.totalRect:=Rect(0,0,newBitMap.Width,newBitMap.Height);
ge.selected:=true;
undoer.SaveCreateAction(cUndoGECreate);
drawSurface.OnMouseDown:=SelectionMadeMouseDown;
drawSurface.OnMouseMove:=SelectionMadeMouseMove;
drawSurface.OnMouseUp:=SelectionMadeMouseUp;
finally
DragFinish(Message.WParam); // let Windows know that you're done
drawSurface.Invalidate;
end;
end;
procedure TBMEditorMain.BoldCBClick(Sender: TObject);
begin
SaveTextStuffToGE;
end;