-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathkmemodlgimage.pas
295 lines (270 loc) · 8.31 KB
/
kmemodlgimage.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
{ @abstract(This unit contains a dialog for image editing)
@author(Tomas Krysl)
Copyright (c) Tomas Krysl<BR><BR>
<B>License:</B><BR>
This code is distributed as a freeware. You are free to use it as part
of your application for any purpose including freeware, commercial and
shareware applications. The origin of this source code must not be
misrepresented; you must not claim your authorship. All redistributions
of the original or modified source code must retain the original copyright
notice. The Author accepts no liability for any damage that may result
from using this code.
}
unit kmemodlgimage; // lowercase name because of Lazarus/Linux
interface
uses
{$IFDEF FPC}
LCLType, LCLIntf, LMessages, LCLProc, LResources,
{$ELSE}
Windows, Messages,
{$ENDIF}
SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, KControls, KMemo, ExtCtrls, ExtDlgs, KButtons, KEdits,
ComCtrls;
type
{ TKMemoImageForm }
TKMemoImageForm = class(TForm)
BUOk: TButton;
BUCancel: TButton;
BUBrowse: TButton;
ODMain: TOpenPictureDialog;
PCMain: TPageControl;
TSBasic: TTabSheet;
TSAdvanced: TTabSheet;
GBShading: TGroupBox;
LBBorderWidth: TLabel;
LBBorderColor: TLabel;
LBShading: TLabel;
EDBorderWidth: TKNumberEdit;
CLBBorder: TKColorButton;
CLBShading: TKColorButton;
GBCrop: TGroupBox;
LBCropLeft: TLabel;
LBCropRight: TLabel;
LBCropTop: TLabel;
LBCropBottom: TLabel;
EDCropLeft: TKNumberEdit;
EDCropRight: TKNumberEdit;
EDCropTop: TKNumberEdit;
EDCropBottom: TKNumberEdit;
GBPosition: TGroupBox;
RBPositionText: TRadioButton;
RBPositionRelative: TRadioButton;
RBPositionAbsolute: TRadioButton;
EDOffsetX: TKNumberEdit;
EDOffsetY: TKNumberEdit;
GBSize: TGroupBox;
EDScaleX: TKNumberEdit;
EDScaleY: TKNumberEdit;
CBProportional: TCheckBox;
GBWrap: TGroupBox;
RBWrapAround: TRadioButton;
RBWrapAroundLeft: TRadioButton;
RBWrapAroundRight: TRadioButton;
RBWrapTopBottom: TRadioButton;
GBPreview: TGroupBox;
MEPreview: TKMemo;
EDExplicitWidth: TKNumberEdit;
EDExplicitHeight: TKNumberEdit;
BUResetOriginalSize: TButton;
procedure BUBrowseClick(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure EDScaleXChange(Sender: TObject);
procedure EDScaleYChange(Sender: TObject);
procedure RBPositionTextClick(Sender: TObject);
procedure EDScaleXExit(Sender: TObject);
procedure CBProportionalClick(Sender: TObject);
procedure BUResetOriginalSizeClick(Sender: TObject);
private
{ Private declarations }
FMemo: TKMemo;
FPreviewImage: TKMemoImageBlock;
FLockUpdate: Boolean;
procedure UpdateFields;
public
{ Public declarations }
procedure Clear;
procedure Load(AMemo: TKMemo; AItem: TKMemoImageBlock);
procedure Save(AItem: TKMemoImageBlock);
end;
implementation
{$IFDEF FPC}
{$R *.lfm}
{$ELSE}
{$R *.dfm}
{$ENDIF}
uses
KEditCommon, KGraphics;
{ TKMemoHyperlinkForm }
procedure TKMemoImageForm.BUBrowseClick(Sender: TObject);
begin
if ODMain.Execute then
FPreviewImage.LoadFromFile(ODMain.FileName);
end;
procedure TKMemoImageForm.BUResetOriginalSizeClick(Sender: TObject);
begin
FLockUpdate := True;
try
EDExplicitWidth.Value := 0;
EDExplicitHeight.Value := 0;
EDScaleX.ValueAsInt := 100;
EDScaleY.ValueAsInt := 100;
finally
FLockUpdate := False;
end;
EDScaleXExit(Sender);
end;
procedure TKMemoImageForm.CBProportionalClick(Sender: TObject);
begin
EDScaleXChange(Sender);
EDScaleXExit(Sender);
end;
procedure TKMemoImageForm.Clear;
begin
end;
procedure TKMemoImageForm.EDScaleXChange(Sender: TObject);
begin
if CBProportional.Checked and not FLockUpdate then
begin
FLockUpdate := True;
try
EDScaleY.ValueAsInt := EDScaleX.ValueAsInt;
finally
FLockUpdate := False;
end;
end;
end;
procedure TKMemoImageForm.EDScaleXExit(Sender: TObject);
begin
if not FLockUpdate then
Save(FPreviewImage);
end;
procedure TKMemoImageForm.EDScaleYChange(Sender: TObject);
begin
if CBProportional.Checked and not FLockUpdate then
begin
FLockUpdate := True;
try
EDScaleX.ValueAsInt := EDScaleY.ValueAsInt;
finally
FLockUpdate := False;
end;
end;
end;
procedure TKMemoImageForm.FormCreate(Sender: TObject);
begin
PCMain.ActivePageIndex := 0;
end;
procedure TKMemoImageForm.Load(AMemo: TKMemo; AItem: TKMemoImageBlock);
begin
Assert(AMemo <> nil);
Assert(AItem <> nil);
FMemo := AMemo;
FLockUpdate := True; // lock scaling constraint
try
MEPreview.Clear;
FPreviewImage := MEPreview.Blocks.AddImageBlock(nil);
FPreviewImage.Assign(AItem);
FPreviewImage.Position := mbpRelative;
FPreviewImage.LeftOffset := 0;
FPreviewImage.TopOffset := 0;
FPreviewImage.Select(-1, 0, False);
case AItem.Position of
mbpText: RBPositionText.Checked := True;
mbpRelative: RBPositionRelative.Checked := True;
mbpAbsolute: RBPositionAbsolute.Checked := True;
end;
EDOffsetX.Value := FMemo.Px2PtX(AItem.LeftOffset);
EDOffsetY.Value := FMemo.Px2PtY(AItem.TopOffset);
EDExplicitWidth.Value := FMemo.Px2PtX(AItem.ExplicitWidth);
EDExplicitHeight.Value := FMemo.Px2PtY(AItem.ExplicitHeight);
EDScaleX.ValueAsInt := AItem.LogScaleX;
EDScaleY.ValueAsInt := AItem.LogScaleY;
CBProportional.Checked := AItem.ScaleX = AItem.ScaleY;
case AItem.ImageStyle.WrapMode of
wrAround, wrTight: RBWrapAround.Checked := True;
wrAroundLeft, wrTightLeft: RBWrapAroundLeft.Checked := True;
wrAroundRight, wrTightRight: RBWrapAroundRight.Checked := True;
else
RBWrapTopBottom.Checked := True;
end;
EDBorderWidth.Value := FMemo.Px2PtX(AItem.ImageStyle.BorderWidth);
CLBBorder.DlgColor := AItem.ImageStyle.BorderColor;
if AItem.ImageStyle.Brush.Style <> bsClear then
CLBShading.DlgColor := AItem.ImageStyle.Brush.Color
else
CLBShading.DlgColor := clNone;
EDCropLeft.Value := FMemo.Px2PtX(AItem.Crop.Left);
EDCropRight.Value := FMemo.Px2PtX(AItem.Crop.Right);
EDCropTop.Value := FMemo.Px2PtY(AItem.Crop.Top);
EDCropBottom.Value := FMemo.Px2PtY(AItem.Crop.Bottom);
UpdateFields;
finally
FLockUpdate := False;
end;
end;
procedure TKMemoImageForm.RBPositionTextClick(Sender: TObject);
begin
UpdateFields;
end;
procedure TKMemoImageForm.Save(AItem: TKMemoImageBlock);
begin
Assert(AItem <> nil);
AItem.LockUpdate;
try
if AItem <> FPreviewImage then
begin
AItem.Image := FPreviewImage.Image;
if RBPositionText.Checked then
AItem.Position := mbpText
else if RBPositionRelative.Checked then
AItem.Position := mbpRelative
else
AItem.Position := mbpAbsolute;
if AItem.Position = mbpText then
begin
AItem.LeftOffset := 0;
AItem.TopOffset := 0;
end else
begin
AItem.LeftOffset := FMemo.Pt2PxX(EDOffsetX.Value);
AItem.TopOffset := FMemo.Pt2PxY(EDOffsetY.Value);
end;
end;
AItem.ExplicitWidth := FMemo.Pt2PxX(EDExplicitWidth.Value);
AItem.ExplicitHeight := FMemo.Pt2PxY(EDExplicitHeight.Value);
AItem.LogScaleX := EDScaleX.ValueAsInt;
AItem.LogScaleY := EDScaleY.ValueAsInt;
if RBWrapAround.Checked then
AItem.ImageStyle.WrapMode := wrAround
else if RBWrapAroundLeft.Checked then
AItem.ImageStyle.WrapMode := wrAroundLeft
else if RBWrapAroundRight.Checked then
AItem.ImageStyle.WrapMode := wrAroundRight
else
AItem.ImageStyle.WrapMode := wrTopBottom;
AItem.ImageStyle.BorderWidth := FMemo.Pt2PxX(EDBorderWidth.Value);
AItem.ImageStyle.BorderColor := CLBBorder.DlgColor;
if CLBShading.DlgColor <> clNone then
AItem.ImageStyle.Brush.Color := CLBShading.DlgColor;
AItem.Crop.Left := FMemo.Pt2PxX(EDCropLeft.Value);
AItem.Crop.Right := FMemo.Pt2PxX(EDCropRight.Value);
AItem.Crop.Top := FMemo.Pt2PxY(EDCropTop.Value);
AItem.Crop.Bottom := FMemo.Pt2PxY(EDCropBottom.Value);
finally
AItem.UnLockUpdate;
end;
end;
procedure TKMemoImageForm.UpdateFields;
var
RelOrAbs: Boolean;
begin
RelOrAbs := not RBPositionText.Checked;
RBWrapAround.Enabled := RelOrAbs;
RBWrapAroundLeft.Enabled := RelOrAbs;
RBWrapAroundRight.Enabled := RelOrAbs;
RBWrapTopBottom.Enabled := RelOrAbs;
EDOffsetX.Enabled := RelOrAbs;
EDOffsetY.Enabled := RelOrAbs;
end;
end.