-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathunit1.pas
921 lines (787 loc) · 28 KB
/
unit1.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
unit Unit1;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls, ExtCtrls,
FPImage, FPReadJPEG, FPReadPNG, FPReadGIF, FPWriteBMP, LazFileUtils,
IntfGraphics, Math, {x11rotation,} GoComicsAPI, x, Gtk2, Gdk2, Gdk2x, xatom;
const
margin = 10;
type
{ TForm1 }
TForm1 = class(TForm)
zoomIn: TButton;
lastButton: TButton;
firstButton: TButton;
ComboBox1: TComboBox;
PrevButton: TButton;
NextButton: TButton;
ShowComicButton: TButton;
SaveComicButton: TButton;
Memo1: TMemo;
Image1: TImage;
zoomOut: TButton;
procedure ComboBox1Change(Sender: TObject);
procedure firstButtonClick(Sender: TObject);
procedure FormActivate(Sender: TObject);
procedure lastButtonClick(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure FormResize(Sender: TObject);
procedure FormShow(Sender: TObject);
procedure FormClose(Sender: TObject; var CloseAction: TCloseAction);
procedure ShowComicButtonClick(Sender: TObject);
procedure SaveComicButtonClick(Sender: TObject);
procedure PrevButtonClick(Sender: TObject);
procedure NextButtonClick(Sender: TObject);
procedure Image1MouseWheel(Sender: TObject; Shift: TShiftState; WheelDelta: Integer; MousePos: TPoint; var Handled: Boolean);
procedure zoomInClick(Sender: TObject);
procedure zoomOutClick(Sender: TObject);
procedure ZoomImage(ZoomFactor: Double);
procedure ResetAndReloadComic;
private
FPrevClientWidth, FPrevClientHeight: Integer;
FWinPropertySet: boolean; //for hildon
FIsComicLoaded: Boolean; // to not fetch when loaded
FComicStream: TMemoryStream;
FFileName: string;
FContentType: string;
FCurrentDate: TDateTime;
FCurrentComic: string;
FGoComics: TGoComics;
FPrevComicUrl: string;
FNextComicUrl: string;
FFirstComicUrl: string;
FLastComicUrl: string;
//FRotationHandler: TX11Rotation;
FCachedBitmap: TBitmap; // Cached bitmap
FIsPortrait: Boolean; // Current orientation state
FScaleFactor: Double; // Zoom factor
FOffsetX, FOffsetY: Integer; // Pan offsets
// Variables to keep track of pan state
FLastMouseX, FLastMouseY: Integer;
FIsPanning: Boolean;
FComic_Section: Real;
procedure LoadImageFromStream(Stream: TMemoryStream; const ContentType: string);
procedure ResizeImage;
function GetComicsDailyDir: string;
function GetFileExtension(const ContentType: string): string;
procedure LoadComic(const Comic: string; const ComicDate: TDateTime);
procedure LoadLatestComic(const Comic: string);
procedure UpdateButtonStates;
procedure UpdateLayout;
procedure UpdateNavigationUrls;
procedure HandleRotation(Sender: TObject; IsPortrait: Boolean);
procedure CacheImage(Bitmap: TBitmap);
procedure LoadCachedImage;
// Methods for zooming and panning
procedure StartPan(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
procedure PerformPan(Sender: TObject; Shift: TShiftState; X, Y: Integer);
procedure EndPan(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
public
end;
var
Form1: TForm1;
implementation
{$R *.lfm}
{ TForm1 }
procedure TForm1.FormCreate(Sender: TObject);
begin
ComboBox1.Items.Add('andycapp');
ComboBox1.Items.Add('calvinandhobbes');
ComboBox1.Items.Add('cats-cafe');
ComboBox1.Items.Add('citizendog');
ComboBox1.Items.Add('dinosaur-comics');
ComboBox1.Items.Add('fminus');
ComboBox1.Items.Add('fredbasset');
ComboBox1.Items.Add('garfield');
ComboBox1.Items.Add('liz-climo-cartoons');
ComboBox1.Items.Add('lola');
ComboBox1.Items.Add('peanuts');
ComboBox1.Items.Add('pearlsbeforeswine');
ComboBox1.Items.Add('perry-bible-fellowship');
ComboBox1.Items.Add('poorly-drawn-lines');
ComboBox1.Items.Add('sarahs-scribbles');
ComboBox1.Items.Add('savage-chickens');
ComboBox1.Items.Add('wizardofid');
ComboBox1.ItemIndex := 0; // Select the first item by default
ComboBox1.ReadOnly := True;
Memo1.Enabled := False;
Memo1.Visible := False;
Form1.Caption := 'comics daily';
prevButton.Enabled := False;
nextButton.Enabled := False;
firstButton.Enabled := False;
lastButton.Enabled := False;
zoomIn.Enabled := False;
zoomOut.Enabled := False;
SaveComicButton.Enabled := False;
UpdateLayout; // Adjust the layout based on the initial form size
// Ensure FormShow event is connected
Self.OnShow := @FormShow;
Self.OnClose := @FormClose;
FPrevClientWidth := 0;
FPrevClientHeight := 0;
FCachedBitmap := nil;
FScaleFactor := 1.0;
FOffsetX := 0;
FOffsetY := 0;
// Set up mouse event handlers for zooming and panning
Image1.OnMouseDown := @StartPan;
Image1.OnMouseMove := @PerformPan;
Image1.OnMouseUp := @EndPan;
Image1.OnMouseWheel := @Image1MouseWheel;
zoomIn.OnClick := @zoomInClick;
zoomOut.OnClick := @zoomOutClick;
end;
procedure TForm1.FormShow(Sender: TObject);
begin
// Create and start the rotation handler
//if not Assigned(FRotationHandler) then
//begin
// FRotationHandler := TX11Rotation.Create(TWindow(Handle));
// FRotationHandler.OnRotation := @HandleRotation;
// FRotationHandler.Start;
// WriteLn('FormShow: Started rotation handler');
//end;
end;
procedure TForm1.FormClose(Sender: TObject; var CloseAction: TCloseAction);
begin
// Stop the rotation handler
//if Assigned(FRotationHandler) then
//begin
// FRotationHandler.Stop;
// FreeAndNil(FRotationHandler);
// WriteLn('FormClose: Stopped rotation handler');
//end;
FreeAndNil(FCachedBitmap);
end;
procedure TForm1.HandleRotation(Sender: TObject; IsPortrait: Boolean);
begin
if IsPortrait then
WriteLn('HandleRotation: Portrait mode detected')
else
WriteLn('HandleRotation: Landscape mode detected');
FIsPortrait := IsPortrait;
ResetAndReloadComic; // Recalculate and reload the image size to fit the new layout
end;
procedure TForm1.UpdateNavigationUrls;
begin
FPrevComicUrl := FGoComics.PrevComicUrl;
FNextComicUrl := FGoComics.NextComicUrl;
FFirstComicUrl := FGoComics.FirstComicUrl;
FLastComicUrl := FGoComics.LastComicUrl;
// Show the navigation URLs for debugging purposes
//ShowMessage('Prev: ' + FPrevComicUrl + ' Next: ' + FNextComicUrl + ' First: ' + FFirstComicUrl + ' Last: ' + FLastComicUrl);
end;
procedure TForm1.ComboBox1Change(Sender: TObject);
begin
PrevButton.Enabled := False;
NextButton.Enabled := False;
firstButton.Enabled := False;
lastButton.Enabled := False;
FPrevComicUrl := '';
FNextComicUrl := '';
FFirstComicUrl := '';
FLastComicUrl := '';
// Clear cached image as the comic selection has changed
FreeAndNil(FCachedBitmap);
end;
procedure TForm1.ShowComicButtonClick(Sender: TObject);
begin
if FIsComicLoaded and (FCurrentComic = ComboBox1.Text) then
begin
// Reset and reload the comic from the cached stream
if Assigned(FCachedBitmap) then
begin
ResetAndReloadComic;
end;
end
else
begin
// Fetch and load the comic from the internet
FCurrentComic := ComboBox1.Text;
FreeAndNil(FGoComics); // Free previous instance if it exists
FGoComics := TGoComics.Create(FCurrentComic); // Initialize GoComics object
// Restore saved URLs
FGoComics.PrevComicUrl := FPrevComicUrl;
FGoComics.NextComicUrl := FNextComicUrl;
FGoComics.FirstComicUrl := FFirstComicUrl;
FGoComics.LastComicUrl := FLastComicUrl;
FComicStream := TMemoryStream.Create; // Initialize FComicStream
LoadLatestComic(FCurrentComic);
UpdateButtonStates;
FIsComicLoaded := True;
end;
end;
procedure TForm1.firstButtonClick(Sender: TObject);
begin
if FGoComics.FirstComicUrl <> '' then
begin
FCurrentDate := FGoComics.FirstComicDate;
LoadComic(FCurrentComic, FCurrentDate);
UpdateNavigationUrls;
UpdateButtonStates;
end;
end;
procedure TForm1.FormActivate(Sender: TObject);
var
widget: PGtkWidget;
atom: TGdkAtom;
value: cardinal;
begin
widget := PGtkWidget(Handle);
if (widget <> nil) and (widget^.window <> nil) and (not FWinPropertySet) then
begin
FWinPropertySet := True;
atom := gdk_atom_intern('_HILDON_PORTRAIT_MODE_SUPPORT', True);
value := 1;
if atom <> 0 then
begin
gdk_property_change(widget^.window, atom,
gdk_x11_xatom_to_atom(XA_CARDINAL), 32,
GDK_PROP_MODE_REPLACE, @value, 1);
end;
end;
end;
procedure TForm1.lastButtonClick(Sender: TObject);
begin
LoadLatestComic(FCurrentComic);
UpdateButtonStates;
end;
procedure TForm1.PrevButtonClick(Sender: TObject);
begin
if FGoComics.PrevComicUrl <> '' then
begin
FCurrentDate := FGoComics.PrevComicDate;
LoadComic(FCurrentComic, FCurrentDate);
UpdateNavigationUrls;
UpdateButtonStates;
end;
end;
procedure TForm1.NextButtonClick(Sender: TObject);
begin
if (FGoComics.NextComicUrl <> '') and (FCurrentDate < Now) then
begin
FCurrentDate := FGoComics.NextComicDate;
LoadComic(FCurrentComic, FCurrentDate);
UpdateNavigationUrls;
UpdateButtonStates;
end;
end;
procedure TForm1.FormResize(Sender: TObject);
begin
WriteLn('form resize');
UpdateLayout; // Adjust the layout when the form is resized
if (FPrevClientWidth = 0) or (FPrevClientWidth <> ClientWidth) then
begin
ResizeImage; // Resize the image when the form is resized
end;
FPrevClientWidth := ClientWidth;
FPrevClientHeight := ClientHeight;
//Refresh;
end;
procedure TForm1.LoadLatestComic(const Comic: string);
var
LatestComicUrl, DateStr: string;
ComicDate: TDateTime;
begin
if Comic = '' then
begin
Memo1.Lines.Add('No comic selected.');
Exit;
end;
Memo1.Lines.Add('Fetching the latest comic for ' + Comic + '...');
try
LatestComicUrl := FGoComics.GetLatestComicUrl;
DateStr := Copy(LatestComicUrl, Length(LatestComicUrl) - 9, 10);
ComicDate := EncodeDate(StrToInt(Copy(DateStr, 1, 4)),
StrToInt(Copy(DateStr, 6, 2)),
StrToInt(Copy(DateStr, 9, 2)));
FCurrentDate := ComicDate;
LoadComic(Comic, ComicDate);
UpdateNavigationUrls;
UpdateButtonStates;
finally
// GoComics object is managed globally now, no need to free here
end;
end;
procedure TForm1.LoadComic(const Comic: string; const ComicDate: TDateTime);
var
RetryDate: TDateTime;
begin
if Comic = '' then
begin
Memo1.Lines.Add('No comic selected.');
Exit;
end;
Form1.Caption := Comic + ' ' + DateToStr(ComicDate);
Memo1.Lines.Add('Fetching comic for ' + Comic + ' on ' + DateToStr(ComicDate) + '...');
try
FreeAndNil(FComicStream); // Free previous instance if it exists
FComicStream := TMemoryStream.Create;
try
try
FComicStream := FGoComics.GetImageUrl(ComicDate, FFileName, FContentType);
except
on E: Exception do
begin
Memo1.Lines.Add('Error fetching comic for ' + DateToStr(ComicDate) + ': ' + E.Message);
RetryDate := ComicDate - 1; // Try the previous day
Memo1.Lines.Add('Retrying with previous day''s comic...');
try
FComicStream := FGoComics.GetImageUrl(RetryDate, FFileName, FContentType);
except
on E: Exception do
begin
Memo1.Lines.Add('Error fetching comic for ' + DateToStr(RetryDate) + ': ' + E.Message);
raise;
end;
end;
FCurrentDate := RetryDate;
end;
end;
Memo1.Lines.Add('Content Type: ' + FContentType); // Log content type for debugging
if Pos('text/html', FContentType) > 0 then
raise Exception.Create('Received HTML instead of image. Please check the image URL.');
LoadImageFromStream(FComicStream, FContentType);
Memo1.Lines.Add('Comic displayed successfully for ' + DateToStr(FCurrentDate));
SaveComicButton.Enabled := True;
finally
// Do not free FComicStream here as it is still needed for the image display
end;
except
on E: Exception do
begin
Memo1.Lines.Add('Error: ' + E.Message);
end;
end;
// Save the current navigation URLs
UpdateNavigationUrls;
end;
procedure TForm1.SaveComicButtonClick(Sender: TObject);
var
FileStream: TFileStream;
FullFilePath: string;
Extension: string;
begin
if Assigned(FComicStream) and (FComicStream.Size > 0) then
begin
Extension := GetFileExtension(FContentType);
FullFilePath := IncludeTrailingPathDelimiter(GetComicsDailyDir) + ChangeFileExt(FFileName, Extension);
FileStream := TFileStream.Create(FullFilePath, fmCreate);
try
FComicStream.Position := 0;
FileStream.CopyFrom(FComicStream, FComicStream.Size);
Memo1.Lines.Add('Comic saved successfully as ' + FullFilePath);
finally
FileStream.Free;
end;
end
else
Memo1.Lines.Add('No comic to save.');
end;
procedure TForm1.LoadImageFromStream(Stream: TMemoryStream; const ContentType: string);
var
Img: TFPMemoryImage;
Reader: TFPCustomImageReader;
Bitmap: TBitmap;
x, y: Integer;
Color2: TFPColor;
Scale: Double;
NewWidth, NewHeight: Integer;
InsectPositionX: Integer;
MovingInsect: TImage;
InsectPaths: array[0..2] of string;
InsectImages: array[0..2] of TPicture;
i, InsectFrameIndex: Integer;
FrameChangeInterval: Integer;
FrameChangeCounter: Integer;
begin
Img := TFPMemoryImage.Create(0, 0);
try
if Pos('jpeg', ContentType) > 0 then
Reader := TFPReaderJPEG.Create
else if Pos('png', ContentType) > 0 then
Reader := TFPReaderPNG.Create
else if Pos('gif', ContentType) > 0 then
Reader := TFPReaderGIF.Create
else
raise Exception.Create('Unsupported image format: ' + ContentType);
try
Stream.Position := 0;
Img.LoadFromStream(Stream, Reader);
// Initialize the insect image paths
InsectPaths[0] := '/usr/share/pixmaps/comics-daily/comics-daily-insect-lf.png';
InsectPaths[1] := '/usr/share/pixmaps/comics-daily/comics-daily-insect.png';
InsectPaths[2] := '/usr/share/pixmaps/comics-daily/comics-daily-insect-rf.png';
// Load the insect images
for i := 0 to 2 do
begin
if not FileExists(InsectPaths[i]) then
InsectPaths[i] := ExtractFilePath(ParamStr(0)) + ExtractFileName(InsectPaths[i]); // Look in current directory
if FileExists(InsectPaths[i]) then
begin
InsectImages[i] := TPicture.Create;
InsectImages[i].LoadFromFile(InsectPaths[i]);
end
else
raise Exception.Create('Insect image not found: ' + InsectPaths[i]);
end;
// Create and configure the moving insect image
MovingInsect := TImage.Create(Self);
MovingInsect.Parent := Self;
MovingInsect.Transparent := True;
MovingInsect.SetBounds(0, 0, 48, 48); // Initial position and size
MovingInsect.Top := Round(ClientHeight * FComic_Section + margin);
MovingInsect.Visible := True; // Show the moving insect
// Set initial frame
InsectFrameIndex := 0;
MovingInsect.Picture.Assign(InsectImages[InsectFrameIndex]);
// Initialize frame change variables
FrameChangeInterval := 5; // Change frame every 5 pixels
FrameChangeCounter := 0;
// Convert TFPMemoryImage to TBitmap
Bitmap := TBitmap.Create;
try
Bitmap.SetSize(Img.Width, Img.Height);
Bitmap.PixelFormat := pf24bit;
for y := 0 to Img.Height - 1 do
begin
for x := 0 to Img.Width - 1 do
begin
Color2 := Img.Colors[x, y];
Bitmap.Canvas.Pixels[x, y] := RGBToColor(Color2.red shr 8, Color2.green shr 8, Color2.blue shr 8);
end;
// Move the insect image
InsectPositionX := (y * ClientWidth) div Img.Height;
MovingInsect.Left := InsectPositionX;
// Update the frame counter
Inc(FrameChangeCounter);
if FrameChangeCounter >= FrameChangeInterval then
begin
// Move to the next frame in the sequence
InsectFrameIndex := (InsectFrameIndex + 1) mod 4; // There are 4 frames in the sequence
case InsectFrameIndex of
0: MovingInsect.Picture.Assign(InsectImages[0]); // comics-daily-insect-lf.png
1: MovingInsect.Picture.Assign(InsectImages[1]); // comics-daily-insect.png
2: MovingInsect.Picture.Assign(InsectImages[2]); // comics-daily-insect-rf.png
3: MovingInsect.Picture.Assign(InsectImages[1]); // comics-daily-insect.png
end;
FrameChangeCounter := 0;
end;
// Force a repaint to update the position of the insect image
Application.ProcessMessages;
// Optional delay
// Sleep(10); // Adjust as needed
// Uncomment the following line for debugging
// writeln('Insect x=', MovingInsect.Left, ' y=', MovingInsect.Top);
end;
// Hide the moving insect
MovingInsect.Visible := False;
MovingInsect.Free; // Free the insect image
// Free the insect images
for i := 0 to 2 do
InsectImages[i].Free;
// Cache the bitmap
CacheImage(Bitmap);
// Calculate scaling to fit within Image1 while preserving aspect ratio
Scale := Min(Image1.Width / Bitmap.Width, Image1.Height / Bitmap.Height);
NewWidth := Round(Bitmap.Width * Scale);
NewHeight := Round(Bitmap.Height * Scale);
// Clear the image before drawing and set the background to black
Image1.Picture.Bitmap.SetSize(Image1.Width, Image1.Height);
Image1.Picture.Bitmap.Canvas.Brush.Color := clBlack; // Set background to black
Image1.Picture.Bitmap.Canvas.FillRect(0, 0, Image1.Width, Image1.Height);
// Resize Image1 to fit the scaled image
Image1.Picture.Bitmap.SetSize(NewWidth, NewHeight);
Image1.Picture.Bitmap.Canvas.StretchDraw(Rect(0, 0, NewWidth, NewHeight), Bitmap);
// Center the image in the middle of Image1
Image1.Left := 0;
Image1.Top := 0;
finally
Bitmap.Free;
end;
finally
Reader.Free;
end;
finally
Img.Free;
end;
end;
procedure TForm1.ResetAndReloadComic;
begin
// Reset the scale factor and offsets
FScaleFactor := 1.0;
FOffsetX := 0;
FOffsetY := 0;
// Reset the layout
UpdateLayout;
// Load the cached image
LoadCachedImage;
end;
procedure TForm1.ResizeImage;
var
NewWidth, NewHeight: Integer;
SrcRect, DestRect: TRect;
begin
if Assigned(FComicStream) and (FComicStream.Size > 0) then
begin
// Check if a cached image exists
if Assigned(FCachedBitmap) then
begin
// Calculate new dimensions based on the scale factor
NewWidth := Round(FCachedBitmap.Width * FScaleFactor);
NewHeight := Round(FCachedBitmap.Height * FScaleFactor);
// Create source and destination rectangles for stretching/cropping
SrcRect := Rect(0, 0, FCachedBitmap.Width, FCachedBitmap.Height);
// Ensure the new dimensions fit within the Image1 boundaries
DestRect.Left := FOffsetX;
DestRect.Top := FOffsetY;
DestRect.Right := DestRect.Left + NewWidth;
DestRect.Bottom := DestRect.Top + NewHeight;
// Clear the image before drawing
Image1.Picture.Bitmap.SetSize(Image1.Width, Image1.Height);
Image1.Picture.Bitmap.Canvas.Brush.Color := clBlack;
Image1.Picture.Bitmap.Canvas.FillRect(0, 0, Image1.Width, Image1.Height);
// Draw the cached bitmap onto the Image1 canvas
Image1.Picture.Bitmap.Canvas.StretchDraw(DestRect, FCachedBitmap);
// Ensure the image remains within the boundaries of Image1
if DestRect.Left > 0 then
FOffsetX := 0
else if DestRect.Right < Image1.Width then
FOffsetX := Image1.Width - NewWidth;
if DestRect.Top > 0 then
FOffsetY := 0
else if DestRect.Bottom < Image1.Height then
FOffsetY := Image1.Height - NewHeight;
// Prevent offsets from going out of bounds
if NewWidth <= Image1.Width then
FOffsetX := 0;
if NewHeight <= Image1.Height then
FOffsetY := 0;
end
else
begin
// If no cached image is available, load from the stream
FComicStream.Position := 0;
LoadImageFromStream(FComicStream, FContentType);
end;
end;
end;
procedure TForm1.CacheImage(Bitmap: TBitmap);
begin
if not Assigned(FCachedBitmap) then
FCachedBitmap := TBitmap.Create;
FCachedBitmap.Assign(Bitmap);
end;
procedure TForm1.LoadCachedImage;
var
Scale: Double;
NewWidth, NewHeight: Integer;
begin
if Assigned(FCachedBitmap) then
begin
// Calculate scaling to fit within Image1 while preserving aspect ratio
Scale := Min(Image1.Width / FCachedBitmap.Width, Image1.Height / FCachedBitmap.Height);
NewWidth := Round(FCachedBitmap.Width * Scale);
NewHeight := Round(FCachedBitmap.Height * Scale);
// Clear the image before drawing and set the background to black
Image1.Picture.Bitmap.SetSize(Image1.Width, Image1.Height);
Image1.Picture.Bitmap.Canvas.Brush.Color := clBlack;
Image1.Picture.Bitmap.Canvas.FillRect(0, 0, Image1.Width, Image1.Height);
// Resize Image1 to fit the scaled image
Image1.Picture.Bitmap.SetSize(NewWidth, NewHeight);
Image1.Picture.Bitmap.Canvas.StretchDraw(Rect(0, 0, NewWidth, NewHeight), FCachedBitmap);
// Center the image in the middle of Image1
Image1.Left := 0; //(ClientWidth - Image1.Width) div 2;
Image1.Top := 0; //(ClientHeight - Image1.Height) div 2;
end;
end;
function TForm1.GetFileExtension(const ContentType: string): string;
begin
if Pos('image/gif', ContentType) > 0 then
Result := '.gif'
else if Pos('image/jpeg', ContentType) > 0 then
Result := '.jpg'
else if Pos('image/png', ContentType) > 0 then
Result := '.png'
else
Result := ''; // Default or raise an error for unsupported types
end;
function TForm1.GetComicsDailyDir: string;
var
HomeDir: string;
begin
HomeDir := GetEnvironmentVariable('HOME');
if HomeDir = '' then
raise Exception.Create('Could not find home directory.');
Result := IncludeTrailingPathDelimiter(HomeDir) + 'comics_daily';
if not DirectoryExists(Result) then
if not CreateDir(Result) then
raise Exception.Create('Could not create comics_daily directory.');
end;
procedure TForm1.UpdateButtonStates;
begin
PrevButton.Enabled := FGoComics.PrevComicUrl <> '';
NextButton.Enabled := FGoComics.NextComicUrl <> '';
//firstButton.Enabled := FCurrentDate <> FGoComics.FirstComicDate;
firstButton.Enabled := PrevButton.Enabled;
lastButton.Enabled := NextButton.Enabled;
zoomIn.Enabled := PrevButton.Enabled or NextButton.Enabled;
zoomOut.Enabled := PrevButton.Enabled or NextButton.Enabled;
{if NextButton.Enabled = False then
begin
lastButton.Enabled := False
end
else
begin
lastButton.Enabled := FCurrentDate <> FGoComics.LastComicDate
end;
}
end;
procedure TForm1.UpdateLayout;
var
FormWidth, FormHeight: Integer;
ComboBoxRight, SaveButtonLeft, SaveButtonRight: Integer;
begin
WriteLn(' -x-x-x-x update layout -x-x-x-x');
WriteLn('scr width: ', Screen.Width);
WriteLn('scr height: ', Screen.Height);
WriteLn('client width: ', ClientWidth);
WriteLn('client height: ', ClientHeight);
WriteLn('form width: ', FormWidth);
WriteLn('form height: ', FormHeight);
// Use Screen dimensions for layout calculation
FormWidth := ClientWidth;
FormHeight := ClientHeight;
WriteLn('-x-x-x-x-x-x-x-x-x-');
WriteLn('client width: ', ClientWidth);
WriteLn('client height: ', ClientHeight);
WriteLn('form width: ', FormWidth);
WriteLn('form height: ', FormHeight);
WriteLn('-x-x-x-x-x-x-x-x-x-');
WriteLn;
// Position ShowComicButton
ShowComicButton.Left := FormWidth - lastButton.Width - ShowComicButton.Width - Margin - Margin;
ShowComicButton.Top := FormHeight - ShowComicButton.Height - SaveComicButton.Height - PrevButton.Height - Margin * 3;
WriteLn('show button ', ShowComicButton.Left, ' ', ShowComicButton.Top);
// Position PrevButton and NextButton
PrevButton.Left := ShowComicButton.Left;
PrevButton.Top := ShowComicButton.Top + ShowComicButton.Height + Margin;
NextButton.Left := ShowComicButton.Left + ShowComicButton.Width - NextButton.Width;
NextButton.Top := PrevButton.Top;
lastButton.Left := FormWidth - LastButton.Width - Margin;
lastButton.Top := PrevButton.Top;
firstButton.Left := PrevButton.Left - firstButton.Width - Margin;
firstButton.Top := PrevButton.Top;
// Position SaveComicButton
SaveComicButton.Left := ShowComicButton.Left;
SaveComicButton.Top := PrevButton.Top + PrevButton.Height + Margin;
// Position ComboBox1
ComboBox1.Left := Margin;
ComboBox1.Top := firstButton.Top;
// Calculate right boundaries for overlap detection
ComboBoxRight := ComboBox1.Left + ComboBox1.Width;
SaveButtonLeft := SaveComicButton.Left;
SaveButtonRight := SaveComicButton.Left + SaveComicButton.Width;
zoomIn.Left := firstButton.Left;
zoomIn.Top := ShowComicButton.Top;
zoomOut.Left := lastButton.Left;
zoomOut.Top := ShowComicButton.Top;
if FormWidth < FormHeight then
begin
ComboBox1.Top := ShowComicButton.Top - ComboBox1.Height - Margin;
ComboBox1.Left := ShowComicButton.Left;
end;
// Resize and position Image1
FComic_Section := (ShowComicButton.Top - Margin) / ClientHeight;
Image1.SetBounds(0, 0, FormWidth, Round(FormHeight * FComic_Section));
WriteLn(' exiting update layout');
end;
// Methods for zooming and panning
procedure TForm1.Image1MouseWheel(Sender: TObject; Shift: TShiftState; WheelDelta: Integer; MousePos: TPoint; var Handled: Boolean);
var
OldScaleFactor: Double;
MouseOffsetX, MouseOffsetY: Integer;
begin
// Save the old scale factor
OldScaleFactor := FScaleFactor;
// Adjust the scale factor based on the mouse wheel direction
if WheelDelta > 0 then
FScaleFactor := FScaleFactor * 1.1
else
FScaleFactor := FScaleFactor / 1.1;
// Clamp the scale factor to reasonable limits
if FScaleFactor < 0.5 then
FScaleFactor := 0.5;
if FScaleFactor > 3.0 then
FScaleFactor := 3.0;
// Calculate the mouse offsets based on the old scale factor
MouseOffsetX := MousePos.X - FOffsetX;
MouseOffsetY := MousePos.Y - FOffsetY;
// Adjust the offsets to zoom by the mouse point
FOffsetX := Round(MouseOffsetX * (FScaleFactor / OldScaleFactor - 1)) + FOffsetX;
FOffsetY := Round(MouseOffsetY * (FScaleFactor / OldScaleFactor - 1)) + FOffsetY;
// Redraw the image with the new scale factor
ResizeImage;
Handled := True;
end;
procedure TForm1.ZoomImage(ZoomFactor: Double);
var
OldScaleFactor: Double;
MousePos: TPoint;
MouseOffsetX, MouseOffsetY: Integer;
begin
// Save the old scale factor
OldScaleFactor := FScaleFactor;
// Adjust the scale factor based on the zoom factor
FScaleFactor := FScaleFactor * ZoomFactor;
// Clamp the scale factor to reasonable limits
if FScaleFactor < 0.5 then
FScaleFactor := 0.5;
if FScaleFactor > 3.0 then
FScaleFactor := 3.0;
// Get the center point of the Image1
MousePos := Point(Image1.Width div 2, Image1.Height div 2);
// Calculate the mouse offsets based on the old scale factor
MouseOffsetX := MousePos.X - FOffsetX;
MouseOffsetY := MousePos.Y - FOffsetY;
// Adjust the offsets to zoom by the center point
FOffsetX := Round(MouseOffsetX * (FScaleFactor / OldScaleFactor - 1)) + FOffsetX;
FOffsetY := Round(MouseOffsetY * (FScaleFactor / OldScaleFactor - 1)) + FOffsetY;
// Redraw the image with the new scale factor
ResizeImage;
end;
procedure TForm1.zoomInClick(Sender: TObject);
begin
ZoomImage(1.1);
end;
procedure TForm1.zoomOutClick(Sender: TObject);
begin
ZoomImage(0.9);
end;
procedure TForm1.StartPan(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
// Initialize pan state
FIsPanning := True;
FLastMouseX := X;
FLastMouseY := Y;
end;
procedure TForm1.PerformPan(Sender: TObject; Shift: TShiftState; X, Y: Integer);
begin
if FIsPanning then
begin
// Calculate the offset based on the mouse movement
FOffsetX := FOffsetX + (X - FLastMouseX);
FOffsetY := FOffsetY + (Y - FLastMouseY);
// Redraw the image with the new offset
ResizeImage;
end;
FLastMouseX := X;
FLastMouseY := Y;
end;
procedure TForm1.EndPan(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
FIsPanning := False;
end;
end.