-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathavoraiser.utils.pas
More file actions
958 lines (831 loc) · 27.7 KB
/
avoraiser.utils.pas
File metadata and controls
958 lines (831 loc) · 27.7 KB
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
(*
Avoraiser UI Framework - High-Performance WinAPI UI for Avocado
Avoraiser is a framework for creating modern user interfaces for the Polish programming language Avocado.
Copyright (c) 2025-2026 Dymitr Wygowski (Programista Art)
Project Created: 23.12.2025
Author: Dymitr Wygowski (Programista Art)
Contact: programista.art@gmail.com
GitHub: https://github.com/Programista-Art/Avoraiser
Version: 1.0
License: Dual-Licensed: Avoraiser Community / Commercial
See LICENSE.md for terms and conditions.
Module: avoraiser.utils
Description:
*)
unit avoraiser.utils;
{$mode ObjFPC}{$H+}
interface
uses
Windows, avoraiser.core;
//avoraiser.events,
type
SUBCLASSPROC = function(hWnd: HWND; uMsg: UINT; wParam: WPARAM; lParam: LPARAM;
uIdSubclass: UINT_PTR; dwRefData: DWORD_PTR): LRESULT; stdcall;
{KONWERSJE }
// Zwraca długość tekstu w oknie lub kontrolce (liczba znaków)
function get_window_text_length(Handle: HWND): Integer;
// Ustawia tekst dowolnej kontrolki (Button, Label, Window, Edit)
procedure set_text(Handle: HWND; const AText: string);
// Pobiera tekst z dowolnej kontrolki
function get_text(Handle: HWND): string;
//Konwersje (Zastępują SysUtils)
// Zamienia liczbę na zwykły string
function int_to_str(value: Integer): string; inline;
// Zamienia liczbę na UnicodeString (Do kontrolek WinAPI, np. SetWindowTextW)
function int_to_wide(value: Integer): UnicodeString; inline;
function str_to_int(const s: UnicodeString; default_val: Integer): Integer;
{POMOCNICZE FUNKCJE}
function rgb(R, G, B: Byte): DWORD; inline;
// Wstawiaj to wewnątrz długich pętli (np. while).
procedure process_messages;
function get_prop_color(hWnd: HWND; const PropName: string; DefaultCol: DWORD): DWORD;
function av_adjust_line_breaks(const s: UnicodeString): UnicodeString;
function write_html_to_file(const file_path: UnicodeString; const content: string): Boolean;
procedure int_to_pchar(Value: Integer; Buffer: PChar);
{Zamienia PChar na liczbę. W razie błędu zwraca Default}
function pchar_to_int(Str: PChar; Default: Integer = 0): Integer;
{Pobiera liczbę bezpośrednio z kontrolki (np. z Edit)}
function get_window_int(hWnd: HWND): Integer;
{Wpisuje liczbę bezpośrednio do kontrolki }
procedure set_window_int(hWnd: HWND; Value: Integer);
//Ustawia kolor kontrolkam (Edit, Memo, itp)
procedure set_control_color(Control: HWND; TextColor, BgColor: DWORD);
// Pobiera informacje o monitorze, na którym znajduje się okno
function get_window_monitor_info(Handle: HWND): TAvocadoMonitorInfo;
//geometria okna / kontrolki
function get_window_x(Handle: HWND): Integer;
function get_window_y(Handle: HWND): Integer;
function get_window_width(Handle: HWND): Integer;
function get_window_height(Handle: HWND): Integer;
procedure set_dark_mode(Handle: HWND; Enable: Boolean);
function get_window_title(Handle: HWND): string;
// Bezpiecznie niszczy dowolną kontrolkę lub okno
procedure destroy_window(Handle: HWND);
// Cień pod oknem (szczególnie dla Frameless)
procedure set_window_shadow(Handle: HWND; Enable: Boolean);
// aby móc zmieniać napis na pasku tytułu "w locie", np. pokazując postęp ładowania lub nazwę otwartego pliku?
procedure set_title(Handle: HWND; const NewTitle: string);
// Uruchamia timer. Jeśli TimerID = 0, system stworzy nowy ID. Interval w milisekundach.
function start_timer(Handle: HWND; TimerID: UINT_PTR; Interval: UINT): UINT_PTR;
// Zatrzymuje timer.
function stop_timer(Handle: HWND; TimerID: UINT_PTR): BOOL;
procedure set_on_query_end_session(Handle: HWND; Event: TAvocadoQueryEndSessionEvent);
procedure set_on_end_session(Handle: HWND; Event: TAvocadoEndSessionEvent);
function make_wparam(Low, High: Word): WPARAM; inline;
// Pozwala systemowi "odetchnąć" i obsłużyć kliknięcia/odświeżanie
procedure set_component_color(h_wnd: HWND; text_color, background_color: DWORD);
//Uniwersalne skalowanie czcionki
procedure set_component_auto_font(Handle: HWND; Height: Integer);
// Oblicza nowe wymiary tak, by obraz o wymiarach
procedure calculate_aspect_ratio(OrigW, OrigH, MaxW, MaxH: Integer; out NewW, NewH: Integer);
function SetWindowSubclass(hWnd: HWND; pfnSubclass: SUBCLASSPROC; uIdSubclass: UINT_PTR; dwRefData: DWORD_PTR): BOOL; stdcall; external 'comctl32.dll' name 'SetWindowSubclass';
function GetWindowSubclass(hWnd: HWND; pfnSubclass: SUBCLASSPROC; uIdSubclass: UINT_PTR; pdwRefData: PDWORD_PTR): BOOL; stdcall; external 'comctl32.dll' name 'GetWindowSubclass';
function RemoveWindowSubclass(hWnd: HWND; pfnSubclass: SUBCLASSPROC; uIdSubclass: UINT_PTR): BOOL; stdcall; external 'comctl32.dll' name 'RemoveWindowSubclass';
function DefSubclassProc(hWnd: HWND; uMsg: UINT; wParam: WPARAM; lParam: LPARAM): LRESULT; stdcall; external 'comctl32.dll' name 'DefSubclassProc';
procedure set_component_transparent(h_wnd: HWND; text_color: DWORD);
procedure set_component_text_color(Handle: HWND; Color: DWORD);
procedure track_mouse_leave(Handle: HWND);
//ustawienie czcionki
procedure set_component_size_font(hComponent: HWND; FontName: PWideChar; Size: Integer);
//ustawia kolor tla listboxa
procedure set_listbox_color(Handle: HWND; TextColor, BgColor: DWORD);
// Pokazuje (True) lub ukrywa (False) dowolny komponent/obiekt
procedure show_component(hComponent: HWND; Visible: Boolean);
function create_panel(Parent: HWND; X, Y, W, H: Integer; Color: DWORD): HWND;
//Przełącza widoczność (jak jest widoczny to ukrywa, jak ukryty to pokazuje)
procedure show_toggle_component(hComponent: HWND);
function color_to_argb(WinColor: DWORD): DWORD;
//Uniwersalna funkcja pomocnicz do tworzenia fontow
function create_ui_font(const font_name: PWideChar; size: Integer): HFONT;
function extract_file_ext(const file_name: UnicodeString): UnicodeString;
function same_text(const s1, s2: UnicodeString): Boolean;
procedure open_url(const url: UnicodeString);
function read_file_to_string(const file_path: UnicodeString): UnicodeString;
function get_file_name_only(const file_path: UnicodeString): UnicodeString;
function get_file_extension(const file_path: UnicodeString): UnicodeString;
// Zamiennik FileExists - sprawdza atrybuty pliku
function file_exists_win(const filename: UnicodeString): Boolean;
// Zamiennik ExtractFilePath - wycina ścieżkę z pełnej nazwy modułu
function get_app_path: UnicodeString;
implementation
function DwmExtendFrameIntoClientArea(hwnd: HWND; const pMarInset: MARGINS): HRESULT; stdcall; external 'dwmapi.dll';
function MonitorFromWindow(hwnd: HWND; dwFlags: DWORD): HMONITOR; stdcall; external 'user32.dll';
function GetMonitorInfoW(hMonitor: HMONITOR; lpmi: PMONITORINFO): BOOL; stdcall; external 'user32.dll';
function DwmSetWindowAttribute(hwnd: HWND; dwAttribute: DWORD; pvAttribute: LPCVOID; cbAttribute: DWORD): HRESULT; stdcall; external 'dwmapi.dll';
function ShellExecuteW(hwnd: HWND; lpOperation, lpFile, lpParameters, lpDirectory: PWideChar; nShowCmd: Integer): HINST; stdcall; external 'shell32.dll' name 'ShellExecuteW';
procedure set_dark_mode(Handle: HWND; Enable: Boolean);
var
DarkMode: Integer;
begin
if Enable then DarkMode := 1 else DarkMode := 0;
DwmSetWindowAttribute(Handle, DWMWA_USE_IMMERSIVE_DARK_MODE, @DarkMode, SizeOf(DarkMode));
end;
function get_window_title(Handle: HWND): string;
begin
Result := get_text(Handle);
end;
procedure destroy_window(Handle: HWND);
begin
if Handle <> 0 then
begin
Windows.DestroyWindow(Handle);
end;
end;
procedure set_window_shadow(Handle: HWND; Enable: Boolean);
var
m: MARGINS;
v: Integer;
begin
if Handle = 0 then Exit;
if Enable then
begin
// Rozszerzamy ramkę o 1 piksel z każdej strony -> System rysuje cień
m.cxLeftWidth := 1;
m.cxRightWidth := 1;
m.cyTopHeight := 1;
m.cyBottomHeight := 1;
DwmExtendFrameIntoClientArea(Handle, m);
// Dodatkowo upewniamy się, że styl pozwala na cień (CS_DROPSHADOW czasem pomaga)
// Ale DwmExtendFrameIntoClientArea jest ważniejsze w nowoczesnych Windowsach.
end
else
begin
// Zerujemy marginesy -> Brak cienia
m.cxLeftWidth := 0;
m.cxRightWidth := 0;
m.cyTopHeight := 0;
m.cyBottomHeight := 0;
DwmExtendFrameIntoClientArea(Handle, m);
end;
end;
procedure set_title(Handle: HWND; const NewTitle: string);
begin
if Handle <> 0 then
begin
SetWindowTextW(Handle, PWideChar(UnicodeString(NewTitle)));
end;
end;
function start_timer(Handle: HWND; TimerID: UINT_PTR; Interval: UINT): UINT_PTR;
begin
Result := SetTimer(Handle, TimerID, Interval, nil);
end;
function stop_timer(Handle: HWND; TimerID: UINT_PTR): BOOL;
begin
Result := KillTimer(Handle, TimerID);
end;
procedure set_on_query_end_session(Handle: HWND; Event: TAvocadoQueryEndSessionEvent);
begin
SetProp(Handle, AV_PROP_ON_QUERY_END, THandle(Event));
end;
procedure set_on_end_session(Handle: HWND; Event: TAvocadoEndSessionEvent);
begin
SetProp(Handle, AV_PROP_ON_END_SESSION, THandle(Event));
end;
function make_wparam(Low, High: Word): WPARAM;
begin
Result := (Low and $FFFF) or (DWORD(High) shl 16);
end;
procedure set_component_color(h_wnd: HWND; text_color, background_color: DWORD);
var
new_brush, old_brush: HBRUSH;
begin
if h_wnd = 0 then Exit;
SetPropW(h_wnd, AV_PROP_BG_COLOR, THandle(background_color + 1));
SetPropW(h_wnd, AV_PROP_TEXT_COLOR, THandle(text_color + 1));
old_brush := HBRUSH(GetPropW(h_wnd, AV_PROP_BRUSH));
if old_brush <> 0 then DeleteObject(old_brush);
new_brush := CreateSolidBrush(background_color);
SetPropW(h_wnd, AV_PROP_BRUSH, THandle(new_brush));
RedrawWindow(h_wnd, nil, 0, RDW_INVALIDATE or RDW_FRAME or RDW_ERASE or RDW_UPDATENOW);
end;
procedure set_component_auto_font(Handle: HWND; Height: Integer);
var
NewHeight: Integer;
NewFont, OldFont: HFONT;
begin
if Handle = 0 then Exit;
// Obliczamy wysokość (Height - margines)
NewHeight := -1 * (Height - 6);
// Zabezpieczenie przed zniknięciem tekstu
if NewHeight > -10 then NewHeight := -10;
NewFont := CreateFontW(NewHeight, 0, 0, 0, FW_NORMAL, 0, 0, 0,
DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS,
CLEARTYPE_QUALITY, DEFAULT_PITCH, 'Segoe UI');
// Sprzątamy starą czcionkę (korzystamy z uniwersalnego klucza z core!)
OldFont := HFONT(GetPropW(Handle, AV_PROP_AUTO_FONT));
if OldFont <> 0 then DeleteObject(OldFont);
//Zapisujemy i aplikujemy
SetPropW(Handle, AV_PROP_AUTO_FONT, THandle(NewFont));
SendMessageW(Handle, WM_SETFONT, WPARAM(NewFont), 1);
end;
procedure calculate_aspect_ratio(OrigW, OrigH, MaxW, MaxH: Integer; out NewW,
NewH: Integer);
var
RatioW, RatioH, MinRatio: Double;
begin
// Zabezpieczenie przed dzieleniem przez zero
if (OrigW <= 0) or (OrigH <= 0) then
begin
NewW := 0;
NewH := 0;
Exit;
end;
// Obliczamy współczynnik skali dla szerokości i wysokości
RatioW := MaxW / OrigW;
RatioH := MaxH / OrigH;
// Wybieramy mniejszy współczynnik, aby obraz na pewno nie wyszedł poza "pudełko"
if RatioW < RatioH then
MinRatio := RatioW
else
MinRatio := RatioH;
// Aplikujemy skalę i zaokrąglamy do pełnych pikseli
NewW := Round(OrigW * MinRatio);
NewH := Round(OrigH * MinRatio);
end;
procedure set_component_transparent(h_wnd: HWND; text_color: DWORD);
var
old_brush: HBRUSH;
begin
if h_wnd = 0 then Exit;
SetPropW(h_wnd, AV_PROP_TEXT_COLOR, THandle(text_color + 1));
old_brush := HBRUSH(GetPropW(h_wnd, AV_PROP_BRUSH));
if (old_brush <> 0) and (old_brush <> GetStockObject(NULL_BRUSH)) then
DeleteObject(old_brush);
SetPropW(h_wnd, AV_PROP_BRUSH, THandle(GetStockObject(NULL_BRUSH)));
InvalidateRect(h_wnd, nil, True);
end;
procedure set_component_text_color(Handle: HWND; Color: DWORD);
begin
if Handle = 0 then Exit;
SetPropW(Handle, AV_PROP_TEXT_COLOR, THandle(Color + 1));
InvalidateRect(Handle, nil, True);
UpdateWindow(Handle);
end;
procedure track_mouse_leave(Handle: HWND);
var
tme: TTrackMouseEvent; // Zmienna lokalna, ukryta tutaj
begin
if Handle = 0 then Exit;
if GetPropW(Handle, 'IsHover') = 0 then
begin
SetPropW(Handle, 'IsHover', 1);
tme.cbSize := SizeOf(TTrackMouseEvent); // Tutaj też zmiana
tme.dwFlags := TME_LEAVE;
tme.hwndTrack := Handle;
tme.dwHoverTime := 0;
// Teraz kompilator wie, że to funkcja, a tme to zmienna typu TTrackMouseEvent
TrackMouseEvent(tme);
InvalidateRect(Handle, nil, False);
end;
end;
procedure set_component_size_font(hComponent: HWND; FontName: PWideChar;Size: Integer);
var
hNewFont, hOldFontProp: HFONT;
myhDC: HDC;
hPrevFont: HGDIOBJ;
TextSize: TSize;
ClassName: array[0..255] of WideChar;
Buffer: array[0..511] of WideChar;
begin
if hComponent = 0 then Exit;
myhDC := GetDC(hComponent);
try
//Tworzymy nową czcionkę (DPI Aware + ClearType)
hNewFont := CreateFontW(
-MulDiv(Size, GetDeviceCaps(myhDC, LOGPIXELSY), 72),
0, 0, 0, FW_NORMAL, 0, 0, 0, DEFAULT_CHARSET,
OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS,
CLEARTYPE_QUALITY,
DEFAULT_PITCH or FF_DONTCARE, FontName);
finally
ReleaseDC(hComponent, myhDC);
end;
if hNewFont = 0 then Exit;
// Sprzątanie starej czcionki (Memory Leak prevention)
hOldFontProp := HFONT(GetPropW(hComponent, 'AvoFont'));
if hOldFontProp <> 0 then DeleteObject(hOldFontProp);
SetPropW(hComponent, 'AvoFont', THandle(hNewFont));
// Ustawiamy czcionkę w kontrolce
SendMessageW(hComponent, WM_SETFONT, WPARAM(hNewFont), LPARAM(True));
// auto-naprawa wyglądu (dla list i labeli)
if GetClassNameW(hComponent, ClassName, 255) > 0 then
begin
myhDC := GetDC(hComponent);
// Wybieramy nową czcionkę do kontekstu, żeby ją zmierzyć
hPrevFont := SelectObject(myhDC, hNewFont);
try
// LABEL (STATIC)
if lstrcmpiW(ClassName, 'STATIC') = 0 then
begin
// Pobieramy aktualny tekst labela
GetWindowTextW(hComponent, Buffer, 511);
if lstrlenW(Buffer) > 0 then
begin
// Mierzymy, ile miejsca zajmie ten tekst z nową czcionką
GetTextExtentPoint32W(myhDC, Buffer, lstrlenW(Buffer), TextSize);
SetWindowPos(hComponent, 0, 0, 0,
TextSize.cx + 2,
TextSize.cy + 2,
SWP_NOMOVE or SWP_NOZORDER);
end;
end
// LISTBOX (Wysokość wiersza)
else if lstrcmpiW(ClassName, 'LISTBOX') = 0 then
begin
GetTextExtentPoint32W(myhDC, 'Ay', 2, TextSize);
SendMessageW(hComponent, LB_SETITEMHEIGHT, 0, TextSize.cy + 6);
end;
finally
SelectObject(myhDC, hPrevFont);
ReleaseDC(hComponent, myhDC);
end;
InvalidateRect(hComponent, nil, True);
end;
end;
procedure set_listbox_color(Handle: HWND; TextColor, BgColor: DWORD);
var
OldBrush: HBRUSH;
begin
SetPropW(Handle, AV_PROP_TEXT_COLOR, THandle(TextColor + 1));
SetPropW(Handle, AV_PROP_BG_COLOR, THandle(BgColor)); // Kolor tła (jako wartość)
OldBrush := HBRUSH(GetPropW(Handle, AV_PROP_BRUSH));
if (OldBrush <> 0) and (OldBrush <> GetStockObject(NULL_BRUSH)) then
DeleteObject(OldBrush);
SetPropW(Handle, AV_PROP_BRUSH, THandle(CreateSolidBrush(BgColor)));
InvalidateRect(Handle, nil, True);
end;
procedure show_component(hComponent: HWND; Visible: Boolean);
begin
if hComponent = 0 then Exit;
if Visible then
ShowWindow(hComponent, SW_SHOW)
else
ShowWindow(hComponent, SW_HIDE);
end;
function create_panel(Parent: HWND; X, Y, W, H: Integer; Color: DWORD): HWND;
begin
Result := CreateWindowExW(
0, 'STATIC', nil,
WS_CHILD or WS_VISIBLE or SS_NOTIFY,
X, Y, W, H, Parent, 0, GetModuleHandle(nil), nil
);
if Result <> 0 then
begin
set_component_color(Result, 0, Color);
end;
end;
procedure show_toggle_component(hComponent: HWND);
begin
if hComponent = 0 then Exit;
// Sprawdzam czy jest widoczny (IsWindowVisible) i robimy na odwrót
if IsWindowVisible(hComponent) then
ShowWindow(hComponent, SW_HIDE)
else
ShowWindow(hComponent, SW_SHOW);
end;
function color_to_argb(WinColor: DWORD): DWORD;
var
R, G, B: Byte;
begin
R := WinColor and $FF;
G := (WinColor shr 8) and $FF;
B := (WinColor shr 16) and $FF;
// GDI+ wymaga AARRGGBB
Result := $FF000000 or (DWORD(R) shl 16) or (DWORD(G) shl 8) or DWORD(B);
end;
function create_ui_font(const font_name: PWideChar; size: Integer): HFONT;
var
pixel_height: Integer;
dc: HDC;
begin
// Pobieramy kontekst ekranu, aby poprawnie przeliczyć punkty (pt) na piksele (px)
dc := GetDC(0);
pixel_height := -MulDiv(size, GetDeviceCaps(dc, LOGPIXELSY), 72);
ReleaseDC(0, dc);
// Tworzymy nowoczesną czcionkę
Result := CreateFontW(
pixel_height,
0, 0, 0,
FW_NORMAL,
0, 0, 0,
DEFAULT_CHARSET,
OUT_DEFAULT_PRECIS,
CLIP_DEFAULT_PRECIS,
CLEARTYPE_QUALITY,
DEFAULT_PITCH or FF_DONTCARE,
font_name
);
end;
function extract_file_ext(const file_name: UnicodeString): UnicodeString;
var
i: Integer;
len: Integer;
begin
Result := '';
len := Length(file_name);
// Szukamy kropki od końca ciągu
for i := len downto 1 do
begin
// Jeśli znajdziemy kropkę, zwracamy wszystko od kropki do końca
if file_name[i] = '.' then
begin
Result := Copy(file_name, i, len - i + 1);
Exit;
end;
if (file_name[i] = '\') or (file_name[i] = '/') then
Exit;
end;
end;
function same_text(const s1, s2: UnicodeString): Boolean;
const
NORM_IGNORECASE = $00000001;
CSTR_EQUAL = 2;
LOCALE_USER_DEFAULT = $0400;
begin
// Jeśli długości są różne, to na pewno nie są to te same teksty
if Length(s1) <> Length(s2) then
begin
Result := False;
Exit;
end;
// Funkcja CompareStringW zwraca 2 (CSTR_EQUAL) jeśli teksty są identyczne
// przy ignorowaniu wielkości liter (NORM_IGNORECASE)
Result := CompareStringW(
LOCALE_USER_DEFAULT,
NORM_IGNORECASE,
PWideChar(s1),
Length(s1),
PWideChar(s2),
Length(s2)
) = CSTR_EQUAL;
end;
procedure open_url(const url: UnicodeString);
begin
if url = '' then Exit;
ShellExecuteW(0, 'open', PWideChar(url), nil, nil, SW_SHOWNORMAL);
end;
function read_file_to_string(const file_path: UnicodeString): UnicodeString;
var
h_file: THandle;
file_size, bytes_read: DWORD;
raw_buffer: PAnsiChar;
wide_len: Integer;
begin
Result := '';
h_file := CreateFileW(PWideChar(file_path), GENERIC_READ, FILE_SHARE_READ, nil, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
if h_file = INVALID_HANDLE_VALUE then Exit;
try
file_size := GetFileSize(h_file, nil);
if (file_size = 0) or (file_size = INVALID_FILE_SIZE) then Exit;
// Alokujemy surowy bufor pamięci
GetMem(raw_buffer, file_size);
try
if ReadFile(h_file, raw_buffer^, file_size, bytes_read, nil) then
begin
// Najpierw sprawdzamy, jak duży będzie tekst po konwersji na Unicode (UTF-16)
wide_len := MultiByteToWideChar(CP_UTF8, 0, raw_buffer, file_size, nil, 0);
if wide_len > 0 then
begin
SetLength(Result, wide_len);
// Właściwa konwersja do bufora Result
MultiByteToWideChar(CP_UTF8, 0, raw_buffer, file_size, PWideChar(Result), wide_len);
end;
end;
finally
FreeMem(raw_buffer);
end;
finally
CloseHandle(h_file);
end;
end;
function get_file_name_only(const file_path: UnicodeString): UnicodeString;
var
i: Integer;
begin
Result := file_path; // Domyślnie zwracamy całość, jeśli nie znajdziemy ukośnika
// Przeszukujemy od końca w poszukiwaniu '\' (Windows) lub '/' (Linux/URL)
for i := Length(file_path) downto 1 do
begin
if (file_path[i] = '\') or (file_path[i] = '/') then
begin
// Wycinamy wszystko, co znajduje się po ukośniku
Result := Copy(file_path, i + 1, Length(file_path) - i);
Break;
end;
end;
end;
function get_file_extension(const file_path: UnicodeString): UnicodeString;
var
i, dot_pos: Integer;
begin
Result := '';
dot_pos := 0;
// Szukamy kropki od końca stringa
for i := Length(file_path) downto 1 do
begin
// Jeśli napotkamy ukośnik przed kropką, to plik nie ma rozszerzenia
// (kropka mogła być częścią nazwy folderu wcześniej)
if (file_path[i] = '\') or (file_path[i] = '/') then
Break;
if file_path[i] = '.' then
begin
dot_pos := i;
Break;
end;
end;
// Jeśli znaleźliśmy kropkę i nie jest ona na samym końcu
if (dot_pos > 0) and (dot_pos < Length(file_path)) then
begin
// Wycinamy tekst po kropce
Result := Copy(file_path, dot_pos + 1, Length(file_path) - dot_pos);
end;
end;
function file_exists_win(const filename: UnicodeString): Boolean;
var
attr: DWORD;
begin
attr := GetFileAttributesW(PWideChar(filename));
Result := (attr <> $FFFFFFFF) and (attr and FILE_ATTRIBUTE_DIRECTORY = 0);
end;
function get_app_path: UnicodeString;
var
buf: array[0..MAX_PATH] of WideChar;
path: UnicodeString;
i: Integer;
begin
GetModuleFileNameW(0, buf, MAX_PATH);
path := UnicodeString(buf);
// Szukamy ostatniego ukośnika od końca
for i := Length(path) downto 1 do
begin
if path[i] = '\' then
begin
Result := Copy(path, 1, i);
Exit;
end;
end;
Result := '';
end;
procedure process_messages;
var
msg: TMsg;
begin
// PM_REMOVE oznacza: pobierz wiadomość i usuń ją z kolejki
while PeekMessageW(msg, 0, 0, 0, PM_REMOVE) do
begin
// Obsługa standardowa (translacja klawiszy, wysłanie do procedury okna)
TranslateMessage(msg);
DispatchMessageW(msg);
end;
end;
function get_prop_color(hWnd: HWND; const PropName: string; DefaultCol: DWORD): DWORD;
var
Value: THandle;
begin
Value := GetPropW(hWnd, PWideChar(UnicodeString(PropName)));
if Value <> 0 then
Result := DWORD(Value - 1)
else
Result := DefaultCol;
end;
function av_adjust_line_breaks(const s: UnicodeString): UnicodeString;
var
i, j, extra_chars: Integer;
begin
Result := '';
if s = '' then Exit;
// 1. Liczymy ile mamy znaków #13, które nie mają po sobie #10
extra_chars := 0;
for i := 1 to Length(s) do
begin
if (s[i] = #13) then
begin
if (i = Length(s)) or (s[i + 1] <> #10) then
Inc(extra_chars);
end;
end;
if extra_chars = 0 then
begin
Result := s;
Exit;
end;
// Alokujemy miejsce na nowy string
SetLength(Result, Length(s) + extra_chars);
//Przepisujemy znaki z poprawką
j := 1;
for i := 1 to Length(s) do
begin
Result[j] := s[i];
Inc(j);
// Jeśli to #13 i nie ma po nim #10, to wstawiamy #10
if (s[i] = #13) then
begin
if (i = Length(s)) or (s[i + 1] <> #10) then
begin
Result[j] := #10;
Inc(j);
end;
end;
end;
end;
function get_window_text_length(Handle: HWND): Integer;
begin
Result := 0;
if Handle <> 0 then
begin
Result := Windows.GetWindowTextLengthW(Handle);
end;
end;
procedure set_text(Handle: HWND; const AText: string);
begin
if Handle <> 0 then
begin
SetWindowTextW(Handle, PWideChar(WideString(AText)));
end;
end;
function get_text(Handle: HWND): string;
var
Len: Integer;
Buffer: WideString;
begin
Result := '';
if Handle = 0 then Exit;
//Sprawdzamy długość tekstu
Len := GetWindowTextLengthW(Handle);
if Len > 0 then
begin
// Rezerwujemy pamięć (+1 na znak null)
SetLength(Buffer, Len + 1);
// 3. Pobieramy tekst
GetWindowTextW(Handle, PWideChar(Buffer), Len + 1);
// Obcinamy nadmiar
SetLength(Buffer, Len);
// Konwertujemy WideString na UTF-8 String
Result := UTF8Encode(Buffer);
end;
end;
function int_to_str(value: Integer): string;
begin
System.Str(value, Result);
end;
function int_to_wide(value: Integer): UnicodeString;
begin
System.Str(value, Result);
end;
function str_to_int(const s: UnicodeString; default_val: Integer): Integer;
var
code: Integer;
begin
Val(s, Result, code);
if code <> 0 then
Result := default_val;
end;
function rgb(R, G, B: Byte): DWORD;
begin
Result := (B shl 16) or (G shl 8) or R;
end;
function write_html_to_file(const file_path: UnicodeString;
const content: string): Boolean;
const
// Sygnatura BOM UTF-8
UTF8_BOM: array[0..2] of Byte = ($EF, $BB, $BF);
var
h_file: THandle;
bytes_written: DWORD;
begin
Result := False;
if file_path = '' then Exit;
// Otwieramy plik
h_file := CreateFileW(
PWideChar(file_path),
GENERIC_WRITE,
0,
nil,
CREATE_ALWAYS,
FILE_ATTRIBUTE_NORMAL,
0
);
if h_file = INVALID_HANDLE_VALUE then Exit;
try
// 1. Zapisujemy BOM
WriteFile(h_file, UTF8_BOM, SizeOf(UTF8_BOM), bytes_written, nil);
// 2. Zapisujemy tekst BEZ ŻADNYCH KONWERSJI (1:1 z pamięci)
if Length(content) > 0 then
begin
Result := WriteFile(h_file, content[1], Length(content), bytes_written, nil);
end
else
Result := True;
finally
CloseHandle(h_file);
end;
end;
procedure int_to_pchar(Value: Integer; Buffer: PChar);
var
S: ShortString;
I: Integer;
begin
Str(Value, S);
for I := 1 to Length(S) do
begin
Buffer[I-1] := S[I];
end;
Buffer[Length(S)] := #0;
end;
function pchar_to_int(Str: PChar; Default: Integer): Integer;
var
Code: Integer;
TempS: ShortString;
begin
TempS := Str;
Val(TempS, Result, Code);
if Code <> 0 then
Result := Default;
end;
function get_window_int(hWnd: HWND): Integer;
var
Buf: array[0..31] of Char;
begin
GetWindowText(hWnd, Buf, 31);
Result := pchar_to_int(Buf, 0);
end;
procedure set_window_int(hWnd: HWND; Value: Integer);
var
Buf: array[0..31] of Char;
begin
int_to_pchar(Value, Buf);
SetWindowText(hWnd, Buf);
end;
procedure set_control_color(Control: HWND; TextColor, BgColor: DWORD);
var
NewBrush: HBRUSH;
OldBrush: HBRUSH;
begin
//Sprawdź, czy już jest pędzel i go usuń (żeby nie było wycieków przy zmianie koloru)
OldBrush := HBRUSH(GetProp(Control, 'AvocadoBrush'));
if OldBrush <> 0 then DeleteObject(OldBrush);
//Stwórz nowy pędzel tła
NewBrush := CreateSolidBrush(BgColor);
//Zapisz dane w kontrolce
SetProp(Control, 'AvocadoBrush', THandle(NewBrush));
SetProp(Control, 'AvocadoTextColor', THandle(TextColor));
SetProp(Control, 'AvocadoBgColor', THandle(BgColor));
//Wymuś odświeżenie kontrolki
InvalidateRect(Control, nil, True);
end;
function get_window_monitor_info(Handle: HWND): TAvocadoMonitorInfo;
var
HMon: HMONITOR;
MonInfo: MONITORINFO;
begin
// Zerowanie wyniku (na wypadek błędu)
FillChar(Result, SizeOf(Result), 0);
if Handle = 0 then Exit;
// 1. Pobieramy uchwyt monitora
HMon := MonitorFromWindow(Handle, MONITOR_DEFAULTTONEAREST);
if HMon <> 0 then
begin
FillChar(MonInfo, SizeOf(MonInfo), 0);
MonInfo.cbSize := SizeOf(MonInfo); // Jeśli rekord nie jest packed, SizeOf będzie błędne
// 2. Pobieramy dane
if GetMonitorInfoW(HMon, @MonInfo) then
begin
Result.MonitorRect := MonInfo.rcMonitor;
Result.WorkRect := MonInfo.rcWork;
Result.IsPrimary := (MonInfo.dwFlags and MONITORINFOF_PRIMARY) <> 0;
end;
end;
end;
function get_window_x(Handle: HWND): Integer;
var
Rect: TRect;
begin
Result := 0;
if (Handle <> 0) and GetWindowRect(Handle, Rect) then
Result := Rect.Left;
end;
function get_window_y(Handle: HWND): Integer;
var
Rect: TRect;
begin
Result := 0;
if (Handle <> 0) and GetWindowRect(Handle, Rect) then
Result := Rect.Top;
end;
function get_window_width(Handle: HWND): Integer;
var
Rect: TRect;
begin
Result := 0;
if (Handle <> 0) and GetWindowRect(Handle, Rect) then
Result := Rect.Right - Rect.Left;
end;
function get_window_height(Handle: HWND): Integer;
var
Rect: TRect;
begin
Result := 0;
if (Handle <> 0) and GetWindowRect(Handle, Rect) then
Result := Rect.Bottom - Rect.Top;
end;
end.