-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfluenti.cpp
More file actions
1236 lines (1164 loc) · 57.6 KB
/
Copy pathfluenti.cpp
File metadata and controls
1236 lines (1164 loc) · 57.6 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
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
#define UNICODE
#define _UNICODE
#include <objbase.h>
#include <windows.h>
#include <windowsx.h>
#include <gdiplus.h>
#include <wininet.h>
#include <commdlg.h>
#include <string>
#include <vector>
#include <sstream>
#pragma comment(lib,"gdiplus.lib")
#pragma comment(lib,"wininet.lib")
#pragma comment(lib,"user32.lib")
#pragma comment(lib,"gdi32.lib")
#pragma comment(lib,"comdlg32.lib")
#pragma comment(lib,"dwmapi.lib")
#include <dwmapi.h>
#pragma comment(lib,"ole32.lib")
#pragma comment(lib,"oleaut32.lib")
#pragma comment(lib,"winmm.lib")
#include <mmsystem.h>
using namespace Gdiplus;
// ── Window ───────────────────────────────────────────────────
const int WW=1000, WH=700;
// ── Colors (light theme like Python version) ─────────────────
#define CBG Color(255,18,18,22) // window bg
#define CWHITE Color(255,30,30,36)
#define CBORD Color(255,55,55,65) // border
#define CBORD2 Color(255, 80, 80, 95) // darker border
#define CTXT Color(255,220,222,228) // main text
#define CTXT2 Color(255,140,143,150) // secondary text
#define CTXT3 Color(255,90,92,100) // dim text
#define CBLUE Color(255, 60,140,255) // blue accent (customtkinter blue)
#define CBLUELT Color(255, 20, 40, 80) // light blue bg
#define CBLUEHV Color(255, 45,120,235) // blue hover
#define CBLUEDP Color(255, 35,100,210) // blue pressed
#define CHOV Color(255,40,40,50) // hover bg
#define CINPUT Color(255,24,24,30) // input bg
#define CBTNBG Color(255,38,38,48) // button bg
#define CBTNHV Color(255,50,50,62) // button hover
#define CSEP Color(255, 50, 50, 60) // separator
// ── Languages ────────────────────────────────────────────────
struct Lang{const wchar_t*display;const wchar_t*name;const char*code;};
Lang LG[]={
{L"russian", L"Русский", "ru"},
{L"english", L"Английский", "en"},
{L"german", L"Немецкий", "de"},
{L"french", L"Французский", "fr"},
{L"spanish", L"Испанский", "es"},
{L"italian", L"Итальянский", "it"},
{L"chinese", L"Китайский", "zh"},
{L"japanese", L"Японский", "ja"},
{L"korean", L"Корейский", "ko"},
{L"arabic", L"Арабский", "ar"},
{L"portuguese", L"Португальский","pt"},
{L"dutch", L"Нидерландский","nl"},
{L"turkish", L"Турецкий", "tr"},
{L"swedish", L"Шведский", "sv"},
{L"polish", L"Польский", "pl"},
{L"ukrainian", L"Украинский", "uk"},
};
const int LC=16;
// ── App state ─────────────────────────────────────────────────
HWND g_hwnd; ULONG_PTR g_gdip;
wchar_t g_fontName[64]=L"Segoe UI";
int g_src=0, g_dst=1; // russian→english
std::wstring g_srcT, g_dstT;
std::wstring g_stat=L"Готов к переводу";
bool g_busy=false, g_focus=false;
bool g_sdrop=false, g_ddrop=false;
int g_hovL=-1, g_hovB=-1;
bool g_caret=true; DWORD g_ctick=0;
bool g_ctxMenu=false; int g_ctxX=0,g_ctxY=0; bool g_ctxOnSrc=false;
int g_ctxHov=-1;
DWORD g_autoTick=0; bool g_autoArmed=false;
int g_view=0; // 0=main 1=history 2=saved 3=settings
// Settings state
bool g_autoTranslate=true;
int g_autoDelay=1; // seconds
bool g_saveHistory=true;
int g_maxHistory=100;
int g_fontIdx=0; // 0=Segoe UI 1=Arial 2=Tahoma 3=Verdana
int g_langIdx=0; // 0=Русский 1=English
int g_settingsFocus=-1; // which input is focused
std::wstring g_delayEdit=L"1";
std::wstring g_maxHistEdit=L"100";
bool g_delayFocus=false, g_maxHistFocus=false;
const wchar_t* FONTS[]= {L"Segoe UI",L"Arial",L"Tahoma",L"Verdana",L"Calibri"};
const int FONT_COUNT=5;
// UI language strings
struct UIText {
const wchar_t* app_title;
const wchar_t* settings_btn;
const wchar_t* back_btn;
const wchar_t* src_label;
const wchar_t* dst_label;
const wchar_t* translate_btn;
const wchar_t* translating;
const wchar_t* copy_btn;
const wchar_t* clear_btn;
const wchar_t* save_btn;
const wchar_t* history_btn;
const wchar_t* saved_btn;
const wchar_t* placeholder_src;
const wchar_t* placeholder_dst;
const wchar_t* settings_title;
const wchar_t* save_settings;
};
UIText UITEXT[]={
// Russian
{L"Fluenti - Переводчик",L"Настройки",L"< Назад",
L"Исходный текст",L"Перевод",L"Перевести",L"Переводится...",
UITEXT[g_langIdx].copy_btn,UITEXT[g_langIdx].clear_btn,UITEXT[g_langIdx].save_btn,UITEXT[g_langIdx].history_btn,UITEXT[g_langIdx].saved_btn,
L"Введите текст для перевода",L"Перевод",
L"Настройки",L"Сохранить настройки"},
// English
{L"Fluenti - Translator",L"Settings",L"< Back",
L"Source text",L"Translation",L"Translate",L"Translating...",
L"Copy",L"Clear",L"Save",L"History",L"Saved",
L"Enter text to translate",L"Translation",
L"Settings",L"Save settings"},
};
bool g_fontDrop=false, g_langDrop=false;
bool g_showHelp=false;
bool g_darkTheme=true;
bool g_isMaximized=false;
int g_charCount=0;
struct HistItem{std::wstring src,dst,srcLang,dstLang;};
std::vector<HistItem> g_hist;
std::vector<HistItem> g_saved;
struct Btn{float x,y,w,h;int id;};
std::vector<Btn> g_btns;
// ── String utils ─────────────────────────────────────────────
static std::string ue(const std::string&s){
std::string r;for(unsigned char c:s){if(isalnum(c)||c=='-'||c=='_'||c=='.'||c=='~')r+=c;else{char b[4];sprintf_s(b,"%%%02X",c);r+=b;}}return r;
}
static std::wstring u8w(const std::string&s){
if(s.empty())return L"";int n=MultiByteToWideChar(CP_UTF8,0,s.c_str(),-1,NULL,0);
std::wstring w(n,0);MultiByteToWideChar(CP_UTF8,0,s.c_str(),-1,&w[0],n);
if(!w.empty()&&!w.back())w.pop_back();return w;
}
static std::string wu8(const std::wstring&w){
if(w.empty())return"";int n=WideCharToMultiByte(CP_UTF8,0,w.c_str(),-1,NULL,0,NULL,NULL);
std::string s(n,0);WideCharToMultiByte(CP_UTF8,0,w.c_str(),-1,&s[0],n,NULL,NULL);
if(!s.empty()&&!s.back())s.pop_back();return s;
}
static std::string jget(const std::string&j,const std::string&k){
auto p=j.find("\""+k+"\"");if(p==std::string::npos)return"";
p=j.find(':',p);if(p==std::string::npos)return"";
while(++p<j.size()&&(j[p]==' '||j[p]=='\n'));if(p>=j.size())return"";
if(j[p]=='"'){p++;std::string v;
while(p<j.size()&&j[p]!='"'){if(j[p]=='\\'&&p+1<j.size()){p++;if(j[p]=='n')v+='\n';else if(j[p]=='t')v+='\t';else v+=j[p];}else v+=j[p];p++;}return v;}
std::string v;while(p<j.size()&&j[p]!=','&&j[p]!='}'&&j[p]!=']')v+=j[p++];return v;
}
static void utf8app(std::string&d,unsigned int cp){
if(cp<0x80)d+=(char)cp;
else if(cp<0x800){d+=(char)(0xC0|(cp>>6));d+=(char)(0x80|(cp&0x3F));}
else{d+=(char)(0xE0|(cp>>12));d+=(char)(0x80|((cp>>6)&0x3F));d+=(char)(0x80|(cp&0x3F));}
}
static bool isHex4(const std::string&t,size_t i){
if(i+4>t.size())return false;
for(int k=0;k<4;k++)if(!isxdigit((unsigned char)t[i+k]))return false;
return true;
}
static unsigned int parseHex4(const std::string&t,size_t i){
unsigned int cp=0;
for(int k=0;k<4;k++){cp<<=4;char c=t[i+k];
if(c>='0'&&c<='9')cp+=c-'0';
else if(c>='a'&&c<='f')cp+=c-'a'+10;
else cp+=c-'A'+10;}
return cp;
}
static std::wstring decode(const std::string&t){
std::string d;
for(size_t i=0;i<t.size();i++){
// ONLY handle \uXXXX (literal backslash + u + 4 hex digits)
if(t[i]==0x5C && i+5<t.size() && t[i+1]=='u' && isHex4(t,i+2)){
utf8app(d,parseHex4(t,i+2));
i+=5; continue;
}
if(t[i]=='&'){
if(i+4<=t.size()&&t.substr(i,4)=="<"){d+='<';i+=3;}
else if(i+4<=t.size()&&t.substr(i,4)==">"){d+='>';i+=3;}
else if(i+5<=t.size()&&t.substr(i,5)=="&"){d+='&';i+=4;}
else if(i+6<=t.size()&&t.substr(i,6)=="""){d+='"';i+=5;}
else d+=t[i];
} else d+=t[i];
}
// If result is ASCII only and contains "u04" patterns, try interpreting as raw unicode escapes
std::wstring result=u8w(d);
bool hasRawUni=false;
for(size_t i=0;i+4<d.size();i++){
if(d[i]=='u'&&isHex4(d,i+1)){
unsigned int cp=parseHex4(d,i+1);
if(cp>=0x400&&cp<=0x4FF){hasRawUni=true;break;} // Cyrillic range
}
}
if(hasRawUni){
// Re-parse treating bare uXXXX as unicode
std::string d2;
for(size_t i=0;i<d.size();i++){
if(d[i]=='u'&&i+4<d.size()&&isHex4(d,i+1)){
unsigned int cp=parseHex4(d,i+1);
if(cp>=0x80){utf8app(d2,cp);i+=4;continue;}
}
d2+=d[i];
}
return u8w(d2);
}
return result;
}
// ── Translation ───────────────────────────────────────────────
struct TP{std::wstring t;std::string s,d;};
DWORD WINAPI transT(LPVOID pv){
auto*p=(TP*)pv;
// Google Translate unofficial API (no key needed)
std::string txt=wu8(p->t);
std::string enc=ue(txt);
// Use translate.googleapis.com - free, no key
std::string path=std::string("/translate_a/single?client=gtx&sl=")+p->s+
"&tl="+p->d+"&dt=t&q="+enc;
HINTERNET hi=InternetOpenA("Mozilla/5.0",INTERNET_OPEN_TYPE_PRECONFIG,NULL,NULL,0);
std::string resp;
if(hi){
std::string url="https://translate.googleapis.com"+path;
HINTERNET hu=InternetOpenUrlA(hi,url.c_str(),NULL,0,
INTERNET_FLAG_SECURE|INTERNET_FLAG_RELOAD|INTERNET_FLAG_NO_CACHE_WRITE,0);
if(hu){
char buf[16384];DWORD rd;
while(InternetReadFile(hu,buf,sizeof(buf)-1,&rd)&&rd>0){buf[rd]=0;resp+=buf;}
InternetCloseHandle(hu);
}
InternetCloseHandle(hi);
}
if(!resp.empty()&&resp[0]=='['){
// Google response: [[["trans1","orig1",null,null,N],["trans2","orig2"...],...],null,"ru",...]
// We only want the FIRST array: resp[0] which contains sentence pairs
// Find the first [ then iterate through ["trans","orig",...] chunks
std::string result;
// Navigate into first nested array: [ [ [
size_t start=resp.find('[');
if(start!=std::string::npos) start=resp.find('[',start+1);
if(start==std::string::npos){g_stat=L"Ошибка парсинга";g_busy=false;delete p;InvalidateRect(g_hwnd,NULL,FALSE);return 0;}
size_t pos=start;
int chunkCount=0;
while(pos<resp.size()&&chunkCount<50){
// Find start of a chunk: ["
auto p1=resp.find("[\"",pos);
if(p1==std::string::npos)break;
// Make sure we haven't gone past the first outer array
// Count brackets to stay in scope
p1+=2;
// Extract first string (translated part)
std::string part;
size_t p2=p1;
while(p2<resp.size()){
if(resp[p2]==0x5C&&p2+1<resp.size()){
p2++;
if(resp[p2]=='n')part+='\n';
else if(resp[p2]=='t')part+='\t';
else if(resp[p2]=='r');
else part+=resp[p2];
p2++;
} else if(resp[p2]=='"'){
break;
} else {
part+=resp[p2++];
}
}
if(p2>=resp.size())break;
p2++; // skip closing "
// Next must be , and " (second string = original)
if(p2<resp.size()&&resp[p2]==','&&p2+1<resp.size()&&resp[p2+1]=='"'){
// Valid translation chunk - part length should be reasonable
if(!part.empty()&&part.size()<2000&&part!="null"){
result+=part;
}
pos=p2;
chunkCount++;
} else {
pos=p2;
}
}
if(!result.empty()){
// Remove all 32-char hex sequences Google inserts as checksums
std::string clean;
size_t ri=0;
while(ri<result.size()){
// Check if next 32 chars are all hex
if(ri+32<=result.size()){
bool isHash=true;
for(size_t hk=ri;hk<ri+32;hk++)
if(!isxdigit((unsigned char)result[hk])){isHash=false;break;}
if(isHash){ri+=32;continue;}
}
clean+=result[ri++];
}
result=clean;
while(!result.empty()&&(result.back()==' '||result.back()=='\n'||result.back()=='\r'))result.pop_back();
g_dstT=decode(result);
g_stat=L"Перевод завершён";
g_hist.insert(g_hist.begin(),{p->t,g_dstT,u8w(p->s),u8w(p->d)});
if(g_hist.size()>100)g_hist.resize(100);
}else{
g_stat=L"Ошибка перевода";
}
}else{
g_stat=L"Нет соединения";
}
g_busy=false;delete p;InvalidateRect(g_hwnd,NULL,FALSE);return 0;
}
static void doTrans(){
if(g_srcT.empty()||g_busy)return;
g_busy=true;g_dstT=L"";g_stat=L"Переводится...";
CreateThread(NULL,0,transT,new TP{g_srcT,LG[g_src].code,LG[g_dst].code},0,NULL);
InvalidateRect(g_hwnd,NULL,FALSE);
}
// ── GDI+ helpers ─────────────────────────────────────────────
static void FR(Graphics&G,Color c,float x,float y,float w,float h){SolidBrush b(c);G.FillRectangle(&b,x,y,w,h);}
static void FRR(Graphics&G,Color c,float x,float y,float w,float h,float r){
SolidBrush b(c);GraphicsPath p;
p.AddArc(x,y,r*2,r*2,180,90);p.AddArc(x+w-r*2,y,r*2,r*2,270,90);
p.AddArc(x+w-r*2,y+h-r*2,r*2,r*2,0,90);p.AddArc(x,y+h-r*2,r*2,r*2,90,90);
p.CloseFigure();G.FillPath(&b,&p);
}
static void SRR(Graphics&G,Color c,float x,float y,float w,float h,float lw,float r){
Pen p(c,lw);GraphicsPath path;
path.AddArc(x,y,r*2,r*2,180,90);path.AddArc(x+w-r*2,y,r*2,r*2,270,90);
path.AddArc(x+w-r*2,y+h-r*2,r*2,r*2,0,90);path.AddArc(x,y+h-r*2,r*2,r*2,90,90);
path.CloseFigure();G.DrawPath(&p,&path);
}
static void LN(Graphics&G,Color c,float x1,float y1,float x2,float y2,float lw=1.f){Pen p(c,lw);G.DrawLine(&p,x1,y1,x2,y2);}
static void DS(Graphics&G,const wchar_t*s,float x,float y,float w,float h,
Color c,float sz,bool bold=false,int al=0,bool wrap=false,bool top=false){
FontFamily ff(g_fontName);Font f(&ff,sz,bold?FontStyleBold:FontStyleRegular,UnitPixel);
SolidBrush b(c);StringFormat sf;
sf.SetAlignment(al==1?StringAlignmentCenter:al==2?StringAlignmentFar:StringAlignmentNear);
sf.SetLineAlignment(top?StringAlignmentNear:StringAlignmentCenter);
if(wrap){sf.SetTrimming(StringTrimmingNone);sf.SetFormatFlags(0);}
else sf.SetTrimming(StringTrimmingEllipsisCharacter);
RectF rc(x,y,w,h);G.DrawString(s,-1,&f,rc,&sf,&b);
}
static bool inB(int mx,int my,float x,float y,float w,float h){return mx>=x&&mx<=x+w&&my>=y&&my<=y+h;}
static void AB(float x,float y,float w,float h,int id){g_btns.push_back({x,y,w,h,id});}
// Draw a standard button (like customtkinter CTkButton)
static void BTN(Graphics&G,const wchar_t*label,float x,float y,float w,float h,
int id,bool primary=false,bool hov=false,bool sml=false){
float r=8.f;
Color bg=primary?(hov?CBLUEHV:CBLUE):(hov?CBTNHV:CBTNBG);
Color tc=primary?CWHITE:(hov?CTXT:CTXT2);
Color bc=primary?Color(0,0,0,0):(hov?CBORD2:CBORD);
FRR(G,bg,x,y,w,h,r);
if(!primary)SRR(G,bc,x,y,w,h,1.f,r);
DS(G,label,x,y,w,h,tc,sml?11.f:13.f,false,1);
AB(x,y,w,h,id);
}
// Draw a dropdown selector (like customtkinter CTkComboBox)
static void COMBO(Graphics&G,const wchar_t*val,float x,float y,float w,float h,int id,bool open,bool hov){
float r=8.f;
Color bg=hov?CHOV:CWHITE;
FRR(G,bg,x,y,w,h,r);
SRR(G,open?CBLUE:CBORD,x,y,w,h,open?2.f:1.f,r);
DS(G,val,x+12,y,w-32,h,CTXT,13);
// dropdown arrow
Pen ap(CTXT2,1.5f);ap.SetLineCap(LineCapRound,LineCapRound,DashCapRound);
float ax=x+w-16,ay=y+h/2;
PointF arr[]={{ax-5,ay-3},{ax,ay+2},{ax+5,ay-3}};
G.DrawLines(&ap,arr,3);
AB(x,y,w,h,id);
}
// Draw text area (like customtkinter CTkTextbox)
static void TEXTAREA(Graphics&G,float x,float y,float w,float h,bool focused){
float r=8.f;
FRR(G,CINPUT,x,y,w,h,r);
SRR(G,focused?CBLUE:CBORD,x,y,w,h,focused?2.f:1.f,r);
}
// ── Render ─────────────────────────────────────────────────────
// Apply theme colors
static void applyTheme(){
// Colors are macros - we use runtime variables instead
// Already handled via g_darkTheme checks in render
}
// TTS via PowerShell
struct TTSP{std::wstring text;};
static DWORD WINAPI ttsT(LPVOID pv){
auto*p=(TTSP*)pv;
// Escape for PowerShell single-quoted string
std::wstring safe;
for(wchar_t ch:p->text){
if(ch==39)safe+=std::wstring(2,39); // double single-quote
else safe+=ch;
}
// Build command: powershell -WindowStyle Hidden ...
std::wstring cmd=L"powershell -WindowStyle Hidden -Command \"Add-Type -AssemblyName System.Speech;$s=New-Object System.Speech.Synthesis.SpeechSynthesizer;$s.Speak([char]39+";
cmd+=L"'"+safe+L"'";
cmd+=L"+[char]39)\"";
// Simpler approach: write to temp ps1
wchar_t tmp[MAX_PATH]={};GetTempPathW(MAX_PATH,tmp);
std::wstring psf=std::wstring(tmp)+L"flu_tts.ps1";
std::wstring outtxt=safe; // reuse safe
// Write ps1
{
HANDLE hf=CreateFileW(psf.c_str(),GENERIC_WRITE,0,NULL,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL);
if(hf!=INVALID_HANDLE_VALUE){
std::wstring sc=L"Add-Type -AssemblyName System.Speech\r\n$s=New-Object System.Speech.Synthesis.SpeechSynthesizer\r\n$s.Speak('"+safe+L"')\r\n";
int n=WideCharToMultiByte(CP_UTF8,0,sc.c_str(),-1,NULL,0,NULL,NULL);
std::string u(n,0);WideCharToMultiByte(CP_UTF8,0,sc.c_str(),-1,&u[0],n,NULL,NULL);
DWORD wr;WriteFile(hf,"\xEF\xBB\xBF",3,&wr,NULL);
WriteFile(hf,u.c_str(),(DWORD)u.size()-1,&wr,NULL);
CloseHandle(hf);
}
}
std::wstring runcmd=L"powershell -WindowStyle Hidden -ExecutionPolicy Bypass -File \""+psf+L"\"";
STARTUPINFOW si={};si.cb=sizeof(si);si.dwFlags=STARTF_USESHOWWINDOW;si.wShowWindow=SW_HIDE;
PROCESS_INFORMATION pi={};
if(CreateProcessW(NULL,&runcmd[0],NULL,NULL,FALSE,CREATE_NO_WINDOW,NULL,NULL,&si,&pi)){
WaitForSingleObject(pi.hProcess,15000);
CloseHandle(pi.hProcess);CloseHandle(pi.hThread);
}
DeleteFileW(psf.c_str());
delete p;return 0;
}
static void doSpeak(const std::wstring&text){
if(text.empty())return;
CreateThread(NULL,0,ttsT,new TTSP{text},0,NULL);
}
struct SRTP{HWND hwnd;};
static DWORD WINAPI srT(LPVOID pv){
auto*p=(SRTP*)pv;
wchar_t tmp[MAX_PATH]={};
GetTempPathW(MAX_PATH,tmp);
std::wstring outf=std::wstring(tmp)+L"flu_sr.txt";
std::wstring psf=std::wstring(tmp)+L"flu_sr.py";
wchar_t dq=34;
std::wstring q(1,dq);
// Write Python script - same approach as original Fluenti.py
std::wstring sc;
sc+=L"import speech_recognition as sr\n";
sc+=L"import sys\n";
sc+=L"r=sr.Recognizer()\n";
sc+=L"try:\n";
sc+=L" with sr.Microphone() as s:\n";
sc+=L" r.adjust_for_ambient_noise(s,duration=0.5)\n";
sc+=L" audio=r.listen(s,timeout=10,phrase_time_limit=10)\n";
sc+=L" text=r.recognize_google(audio,language=\'ru-RU\')\n";
sc+=L" with open(sys.argv[1],\'w\',encoding=\'utf-8\') as f:\n";
sc+=L" f.write(text)\n";
sc+=L"except Exception as e:\n";
sc+=L" pass\n";
// Write script
HANDLE hf=CreateFileW(psf.c_str(),GENERIC_WRITE,0,NULL,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL);
if(hf!=INVALID_HANDLE_VALUE){
int n=WideCharToMultiByte(CP_UTF8,0,sc.c_str(),-1,NULL,0,NULL,NULL);
std::string u(n,0);
WideCharToMultiByte(CP_UTF8,0,sc.c_str(),-1,&u[0],n,NULL,NULL);
if(!u.empty()&&u.back()==0)u.pop_back();
DWORD wr;
WriteFile(hf,u.c_str(),(DWORD)u.size(),&wr,NULL);
CloseHandle(hf);
}
// Run: python flu_sr.py outfile
std::wstring cmd=L"python "+q+psf+q+L" "+q+outf+q;
STARTUPINFOW si2={}; si2.cb=sizeof(si2); si2.dwFlags=STARTF_USESHOWWINDOW; si2.wShowWindow=SW_HIDE;
PROCESS_INFORMATION pi2={};
if(CreateProcessW(NULL,&cmd[0],NULL,NULL,FALSE,CREATE_NO_WINDOW,NULL,NULL,&si2,&pi2)){
WaitForSingleObject(pi2.hProcess,20000);
CloseHandle(pi2.hProcess); CloseHandle(pi2.hThread);
}
// Read result
HANDLE hr=CreateFileW(outf.c_str(),GENERIC_READ,FILE_SHARE_READ,NULL,OPEN_EXISTING,0,NULL);
if(hr!=INVALID_HANDLE_VALUE){
DWORD sz=GetFileSize(hr,NULL);
if(sz>0&&sz<65536){
std::string buf(sz,0); DWORD rdb;
ReadFile(hr,&buf[0],sz,&rdb,NULL);
std::string ct=buf.substr(0,rdb);
while(!ct.empty()&&(ct.back()==13||ct.back()==10||ct.back()==32))ct.pop_back();
if(!ct.empty()){
std::wstring rec=u8w(ct);
if(!rec.empty()){
if(!g_srcT.empty()&&g_srcT.back()!=L' ')g_srcT+=L' ';
g_srcT+=rec;
g_charCount=(int)g_srcT.size();
g_autoArmed=true;
g_autoTick=GetTickCount();
}
}
}
CloseHandle(hr);
DeleteFileW(outf.c_str());
}
DeleteFileW(psf.c_str());
InvalidateRect(p->hwnd,NULL,FALSE);
delete p;
return 0;
}
void render(HDC hdc){
Bitmap bmp(WW,WH,PixelFormat32bppARGB);
Graphics G(&bmp);
G.SetSmoothingMode(SmoothingModeAntiAlias);
G.SetTextRenderingHint(TextRenderingHintClearTypeGridFit);
g_btns.clear();
// Background
FR(G,CBG,0,0,(float)WW,(float)WH);
if(g_view==0||g_view==1||g_view==2){
// ── HEADER ──────────────────────────────────────────
FR(G,CWHITE,0,0,(float)WW,68);
LN(G,CBORD,0,68,(float)WW,68);
// Logo "Fluenti" - blue bold
DS(G,L"Fluenti",24,0,200,68,CBLUE,28,true);
// Header buttons (right side)
if(g_view==0){
BTN(G,L"Настройки",(float)WW-150,18,130,32,10,false,(g_hovB==10));
}else{
BTN(G,L"< Перевод",(float)WW-150,18,130,32,11,false,(g_hovB==11));
}
// ── LANG BAR ─────────────────────────────────────────
FR(G,CWHITE,0,68,(float)WW,72);
LN(G,CBORD,0,140,(float)WW,140);
float lbx=24,lby=82,lbh=36;
// Source combo
COMBO(G,LG[g_src].display,lbx,lby,180,lbh,20,g_sdrop,(g_hovB==20));
// Swap button (like Python version - round with ⇌)
float spx=lbx+180+14,spy=lby;
bool sphov=(g_hovB==21);
FRR(G,sphov?CHOV:CWHITE,spx,spy,36,lbh,18.f);
SRR(G,sphov?CBLUE:CBORD,spx,spy,36,lbh,1.f,18.f);
DS(G,L"<>",spx,spy,36,lbh,CTXT,14,false,1);
AB(spx,spy,36,lbh,21);
// Dest combo
COMBO(G,LG[g_dst].display,spx+50,lby,180,lbh,22,g_ddrop,(g_hovB==22));
// Translate button (primary blue, rounded pill)
float tbx=(float)WW-170,tby=lby,tbw=150,tbh=lbh;
bool tbhov=(g_hovB==23);
FRR(G,tbhov?CBLUEHV:CBLUE,tbx,tby,tbw,tbh,18.f);
DS(G,g_busy?UITEXT[g_langIdx].translating:UITEXT[g_langIdx].translate_btn,tbx,tby,tbw,tbh,CWHITE,13,true,1);
AB(tbx,tby,tbw,tbh,23);
if(g_view==0){
// ── MAIN TRANSLATION VIEW ────────────────────────
float panY=148,panH=(float)WH-148-52;
float halfW=(float)WW/2.f-12;
// Panel labels row
float lblY=panY+8;float lblH=30;
DS(G,UITEXT[g_langIdx].src_label,24,lblY,200,lblH,CTXT2,12);
DS(G,UITEXT[g_langIdx].dst_label,(float)WW/2.f+16,lblY,200,lblH,CTXT2,12);
// Small round icon buttons aligned to right edge of each panel
float sr=14.f; // small radius
float lcy=lblY+lblH/2;
// Left panel right edge buttons: mic + file
auto smIconBtn=[&](int ico,float cx,float cy,float r,bool hov,int bid){
SolidBrush bb(Color(255,45,45,55));G.FillEllipse(&bb,cx-r,cy-r,r*2,r*2);
if(hov){Pen wb(Color(255,255,255,255),1.8f);G.DrawEllipse(&wb,cx-r,cy-r,r*2,r*2);}
Color ic=hov?Color(255,255,255,255):Color(255,140,143,150);
Pen ip(ic,1.4f);ip.SetLineCap(LineCapRound,LineCapRound,DashCapRound);
float s=r/13.f;
switch(ico){
case 0:{ // mic
G.DrawRectangle(&ip,cx-3*s,cy-5*s,6*s,7*s);
G.DrawArc(&ip,cx-5*s,cy-1*s,10*s,7*s,0,180);
G.DrawLine(&ip,cx,cy+6*s,cx,cy+9*s);
G.DrawLine(&ip,cx-3*s,cy+9*s,cx+3*s,cy+9*s);}break;
case 1:{ // folder/file
PointF f[]={{cx-6*s,cy-4*s},{cx-6*s,cy+5*s},{cx+6*s,cy+5*s},{cx+6*s,cy-2*s},{cx+2*s,cy-2*s},{cx,cy-5*s},{cx-3*s,cy-5*s},{cx-3*s,cy-4*s},{cx-6*s,cy-4*s}};
G.DrawLines(&ip,f,9);}break;
case 2:{ // speaker
PointF sp[]={{cx-5*s,cy-3*s},{cx-1*s,cy-3*s},{cx+3*s,cy-7*s},{cx+3*s,cy+7*s},{cx-1*s,cy+3*s},{cx-5*s,cy+3*s},{cx-5*s,cy-3*s}};
G.DrawLines(&ip,sp,7);
G.DrawArc(&ip,cx+1*s,cy-4*s,6*s,8*s,-40.f,80.f);}break;
}
AB(cx-r,cy-r,r*2,r*2,bid);
};
// Right edge of left panel
float leftEdge=(float)WW/2.f-8;
smIconBtn(0,leftEdge-sr*3-4,lcy,sr,(g_hovB==30),30); // mic
smIconBtn(1,leftEdge-sr-2, lcy,sr,(g_hovB==31),31); // file
// Right edge of right panel
float rightEdge=(float)WW-8;
smIconBtn(2,rightEdge-sr,lcy,sr,(g_hovB==32),32); // speaker
// Text areas - clear gap after labels
float taY=lblY+lblH+8;float taH=panH-lblH-24;
TEXTAREA(G,16,taY,halfW,taH,false);
TEXTAREA(G,(float)WW/2.f+8,taY,(float)WW-16-(float)WW/2.f-8,taH,false);
// gap line between panels
FR(G,CBG,(float)WW/2.f,taY,8,taH);
// Source text content
if(g_srcT.empty()){
DS(G,UITEXT[g_langIdx].placeholder_src,26,taY+14,(float)halfW-20,30,CTXT3,14);
}else{
FontFamily fft(g_fontName);Font tft(&fft,14,FontStyleRegular,UnitPixel);
SolidBrush tbt(CTXT);StringFormat sft;
sft.SetTrimming(StringTrimmingNone);sft.SetAlignment(StringAlignmentNear);sft.SetLineAlignment(StringAlignmentNear);
RectF trt(26,taY+14,(float)halfW-20,taH-28);
G.DrawString(g_srcT.c_str(),-1,&tft,trt,&sft,&tbt);
}
// Cursor
if(g_focus&&g_caret){
float curX=24,curY=taY+14;
if(!g_srcT.empty()){
FontFamily ffm(g_fontName);Font fm(&ffm,14,FontStyleRegular,UnitPixel);
StringFormat sfm;sfm.SetAlignment(StringAlignmentNear);sfm.SetLineAlignment(StringAlignmentNear);
int lines=1;for(auto ch:g_srcT)if(ch==L'\n')lines++;
std::wstring last=g_srcT;auto nl=g_srcT.rfind(L'\n');if(nl!=std::wstring::npos)last=g_srcT.substr(nl+1);
RectF bb;PointF o(24,taY+12);G.MeasureString(last.c_str(),-1,&fm,o,&sfm,&bb);
curX=24+min(bb.Width,(float)halfW-20);curY=taY+12+(lines-1)*20.f;
}
Pen cp(CBLUE,2.f);G.DrawLine(&cp,curX,curY,curX,curY+20);
}
// Dest text
if(g_busy){
DS(G,UITEXT[g_langIdx].translating,(float)WW/2.f+18,taY+14,(float)WW-40-(float)WW/2.f,30,CTXT3,14);
}else if(!g_dstT.empty()){
FontFamily ffd(g_fontName);Font tfd(&ffd,14,FontStyleRegular,UnitPixel);
SolidBrush tbd(CTXT);StringFormat sfd;
sfd.SetTrimming(StringTrimmingNone);sfd.SetAlignment(StringAlignmentNear);sfd.SetLineAlignment(StringAlignmentNear);
RectF trd((float)WW/2.f+18,taY+14,(float)WW-40-(float)WW/2.f,taH-28);
G.DrawString(g_dstT.c_str(),-1,&tfd,trd,&sfd,&tbd);
}else{
DS(G,UITEXT[g_langIdx].placeholder_dst,(float)WW/2.f+18,taY+14,(float)WW-40-(float)WW/2.f,30,CTXT3,14);
}
// Divider between panels
LN(G,CBORD,(float)WW/2.f,taY,(float)WW/2.f,taY+taH);
// ── BOTTOM TOOLBAR ────────────────────────────────
float bty=(float)WH-50;
FR(G,CWHITE,0,bty,(float)WW,50);
LN(G,CBORD,0,bty,(float)WW,bty);
// Icon-only round buttons with glow on hover
auto iconRoundBtn=[&](int ico,float cx,float cy,float r,bool hov,int bid){
// Circle bg
SolidBrush bgb(Color(255,45,45,55));G.FillEllipse(&bgb,cx-r,cy-r,r*2,r*2);
// White border on hover
if(hov){
Pen wb(Color(255,255,255,255),2.f);
G.DrawEllipse(&wb,cx-r,cy-r,r*2,r*2);
}
// Icon - smaller (s/15 instead of s/12)
Color ic=hov?Color(255,255,255,255):Color(255,140,143,150);
Pen ip(ic,1.5f);ip.SetLineCap(LineCapRound,LineCapRound,DashCapRound);
float s=r/15.f;
switch(ico){
case 0:// copy
G.DrawRectangle(&ip,cx-5*s,cy-2*s,8*s,8*s);
G.DrawRectangle(&ip,cx-2*s,cy-5*s,8*s,8*s);break;
case 1:// X clear
G.DrawLine(&ip,cx-5*s,cy-5*s,cx+5*s,cy+5*s);
G.DrawLine(&ip,cx+5*s,cy-5*s,cx-5*s,cy+5*s);break;
case 2:{ // star
PointF st[10];
for(int k=0;k<5;k++){float a1=(k*72-90)*3.14159f/180.f,a2=((k*72+36)-90)*3.14159f/180.f;
st[k*2]={cx+6*s*cosf(a1),cy+6*s*sinf(a1)};st[k*2+1]={cx+2.5f*s*cosf(a2),cy+2.5f*s*sinf(a2)};}
G.DrawPolygon(&ip,st,10);}break;
case 3:// clock/history
G.DrawEllipse(&ip,cx-6*s,cy-6*s,12*s,12*s);
G.DrawLine(&ip,cx,cy,cx,cy-4*s);
G.DrawLine(&ip,cx,cy,cx+3*s,cy+2*s);break;
case 4:{ // bookmark
PointF bm[]={{cx-5*s,cy-6*s},{cx+5*s,cy-6*s},{cx+5*s,cy+6*s},{cx,cy+1*s},{cx-5*s,cy+6*s},{cx-5*s,cy-6*s}};
G.DrawLines(&ip,bm,6);}break;
}
AB(cx-r,cy-r,r*2,r*2,bid);
};
float bcx=30,bcy=bty+25,br=18;
iconRoundBtn(0,bcx,bcy,br,(g_hovB==40),40);bcx+=50;
iconRoundBtn(1,bcx,bcy,br,(g_hovB==41),41);bcx+=50;
iconRoundBtn(2,bcx,bcy,br,(g_hovB==42),42);bcx+=50;
iconRoundBtn(3,bcx,bcy,br,(g_hovB==43),43);bcx+=50;
iconRoundBtn(4,bcx,bcy,br,(g_hovB==44),44);
// Char count right
std::wstring cc=std::to_wstring(g_charCount)+L" символов";
DS(G,cc.c_str(),(float)WW-120,bty,112,50,CTXT3,11,false,2);
// status hidden
}
else if(g_view==1){
// ── HISTORY VIEW ─────────────────────────────────
float cy=152;
FR(G,CBG,0,152,(float)WW,(float)WH-152);
if(g_hist.empty()){
DS(G,g_langIdx==0?L"История переводов пуста":L"Translation history is empty",0,(float)WH/2-20,(float)WW,40,CTXT3,15,false,1);
}else{
for(int i=0;i<(int)g_hist.size()&&cy<(float)WH-20;i++){
bool hov=(g_hovB==500+i);
FRR(G,hov?CBLUELT:CWHITE,16,cy,(float)WW-32,64,8.f);
SRR(G,hov?CBLUE:CBORD,16,cy,(float)WW-32,64,1.f,8.f);
// Lang labels
std::wstring ll=g_hist[i].srcLang+L" → "+g_hist[i].dstLang;
DS(G,ll.c_str(),28,cy+4,(float)(WW/2-40),18,CTXT3,10);
DS(G,g_hist[i].src.c_str(),28,cy+20,(float)(WW/2-40),20,CTXT,13);
DS(G,g_hist[i].dst.c_str(),28,cy+40,(float)(WW/2-40),20,CTXT2,12);
// Load button
BTN(G,(g_langIdx==0?L"Загрузить":L"Load"),(float)WW-110,cy+18,90,28,500+i,false,(g_hovB==500+i),true);
AB(16,cy,(float)WW-32,64,500+i);
cy+=72;
}
}
}
else if(g_view==2){
// ── SAVED VIEW ───────────────────────────────────
float cy=152;
FR(G,CBG,0,152,(float)WW,(float)WH-152);
if(g_saved.empty()){
DS(G,g_langIdx==0?L"Нет сохранённых переводов":L"No saved translations",0,(float)WH/2-20,(float)WW,40,CTXT3,15,false,1);
}else{
for(int i=0;i<(int)g_saved.size()&&cy<(float)WH-20;i++){
bool hov=(g_hovB==600+i);
FRR(G,hov?CBLUELT:CWHITE,16,cy,(float)WW-32,64,8.f);
SRR(G,hov?CBLUE:CBORD,16,cy,(float)WW-32,64,1.f,8.f);
DS(G,g_saved[i].src.c_str(),28,cy+8,(float)(WW/2-40),22,CTXT,13);
DS(G,g_saved[i].dst.c_str(),28,cy+30,(float)(WW/2-40),22,CTXT2,12);
BTN(G,(g_langIdx==0?L"Загрузить":L"Load"),(float)WW-220,cy+18,90,28,600+i,false,(g_hovB==600+i),true);
BTN(G,(g_langIdx==0?L"Удалить":L"Delete"), (float)WW-120,cy+18,90,28,610+i,false,(g_hovB==610+i),true);
AB(16,cy,(float)WW-32,64,600+i);
cy+=72;
}
}
}
}
else if(g_view==3){
FR(G,CBG,0,0,(float)WW,(float)WH);
FR(G,CWHITE,0,0,(float)WW,68);
LN(G,CBORD,0,68,(float)WW,68);
DS(G,L"Fluenti",24,0,200,68,CBLUE,28,true);
BTN(G,UITEXT[g_langIdx].back_btn,(float)WW-130,18,112,32,11,false,(g_hovB==11));
float sx=48,sw=(float)WW-96,sy=88;
DS(G,UITEXT[g_langIdx].settings_title,sx,sy,sw,32,CTXT,22,true); sy+=44;
LN(G,CBORD,sx,sy,sx+sw,sy); sy+=20;
float lx=sx,rx=sx+320,rw=200,rh=32;
// Toggle helper
auto sToggle=[&](bool val,float x,float y,int bid,bool hov){
float tw=46,th=26,r=13;
FRR(G,val?CBLUE:Color(255,60,60,75),x,y,tw,th,r);
float kx=val?x+tw-th+2:x+2;
SolidBrush kb(CWHITE);G.FillEllipse(&kb,kx,y+2,th-4,th-4);
if(hov){Pen hp(CTXT2,1.f);G.DrawEllipse(&hp,x,y,tw,th);}
AB(x,y,tw,th,bid);
};
// Input helper
auto sInput=[&](const std::wstring&val,float x,float y,float w,float h,bool focused,int bid){
FRR(G,Color(255,38,38,50),x,y,w,h,6.f);
SRR(G,focused?CBLUE:CBORD,x,y,w,h,focused?2.f:1.f,6.f);
DS(G,val.c_str(),x+8,y,w-16,h,CTXT,13);
if(focused&&g_caret){
FontFamily ff2(g_fontName);Font f2(&ff2,13,FontStyleRegular,UnitPixel);
StringFormat sf2;sf2.SetAlignment(StringAlignmentNear);sf2.SetLineAlignment(StringAlignmentNear);
RectF bb;PointF o(x+8,y+8);G.MeasureString(val.c_str(),-1,&f2,o,&sf2,&bb);
Pen cp(CBLUE,1.5f);G.DrawLine(&cp,x+8+bb.Width,y+5,x+8+bb.Width,y+h-5);
}
AB(x,y,w,h,bid);
};
// Combo helper
auto sCombo=[&](const wchar_t*val,float x,float y,float w,float h,bool open,bool hov,int bid){
FRR(G,hov?Color(255,50,50,65):Color(255,38,38,50),x,y,w,h,6.f);
SRR(G,open?CBLUE:CBORD,x,y,w,h,open?2.f:1.f,6.f);
DS(G,val,x+10,y,w-28,h,CTXT,13);
Pen ap(CTXT2,1.4f);ap.SetLineCap(LineCapRound,LineCapRound,DashCapRound);
float ax=x+w-14,ay=y+h/2;
PointF arr[]={{ax-4,ay-2},{ax,ay+2},{ax+4,ay-2}};G.DrawLines(&ap,arr,3);
AB(x,y,w,h,bid);
};
// Section: Интерфейс
DS(G,g_langIdx==0?L"Интерфейс":L"Interface",sx,sy,sw,22,CTXT2,12,true); sy+=32;
DS(G,g_langIdx==0?L"Язык интерфейса":L"Interface language",lx,sy+6,300,rh,CTXT,13);
sCombo(g_langIdx==0?L"Русский":L"English",rx,sy,rw,rh,g_langDrop,(g_hovB==710),710);
sy+=46;
DS(G,g_langIdx==0?L"Шрифт":L"Font",lx,sy+6,300,rh,CTXT,13);
sCombo(FONTS[g_fontIdx],rx,sy,rw,rh,g_fontDrop,(g_hovB==711),711);
sy+=46;
LN(G,CBORD,sx,sy,sx+sw,sy); sy+=20;
// Section: Перевод
DS(G,g_langIdx==0?L"Перевод":L"Translation",sx,sy,sw,22,CTXT2,12,true); sy+=32;
DS(G,g_langIdx==0?L"Автоперевод":L"Auto-translate",lx,sy+6,300,rh,CTXT,13);
DS(G,g_autoTranslate?L"Включён":L"Выключен",rx-90,sy,80,rh,g_autoTranslate?CBLUE:CTXT3,12,false,2);
sToggle(g_autoTranslate,rx+rw-46,sy+3,700,(g_hovB==700));
sy+=46;
DS(G,g_langIdx==0?L"Задержка автоперевода (сек)":L"Auto-translate delay (sec)",lx,sy+6,300,rh,CTXT,13);
sInput(g_delayEdit,rx,sy,60,rh,g_delayFocus,720);
BTN(G,L"-",rx+66,sy,30,rh,721,false,(g_hovB==721),true);
BTN(G,L"+",rx+100,sy,30,rh,722,false,(g_hovB==722),true);
sy+=46;
LN(G,CBORD,sx,sy,sx+sw,sy); sy+=20;
// Section: История
DS(G,UITEXT[g_langIdx].history_btn,sx,sy,sw,22,CTXT2,12,true); sy+=32;
DS(G,g_langIdx==0?L"Сохранять историю":L"Save history",lx,sy+6,300,rh,CTXT,13);
DS(G,g_saveHistory?L"Включено":L"Выключено",rx-90,sy,80,rh,g_saveHistory?CBLUE:CTXT3,12,false,2);
sToggle(g_saveHistory,rx+rw-46,sy+3,701,(g_hovB==701));
sy+=46;
DS(G,g_langIdx==0?L"Максимум записей":L"Max history entries",lx,sy+6,300,rh,CTXT,13);
sInput(g_maxHistEdit,rx,sy,60,rh,g_maxHistFocus,730);
BTN(G,L"-",rx+66,sy,30,rh,731,false,(g_hovB==731),true);
BTN(G,L"+",rx+100,sy,30,rh,732,false,(g_hovB==732),true);
sy+=46;
LN(G,CBORD,sx,sy,sx+sw,sy); sy+=24;
// Buttons row: Help + Save
float sbh=40;
BTN(G,(g_langIdx==0?L"Справка":L"Help"),sx,sy,120,sbh,703,false,(g_hovB==703));
float sbw=180,sbx=sx+sw/2-sbw/2;
FRR(G,(g_hovB==740)?CBLUEHV:CBLUE,sbx,sy,sbw,sbh,20.f);
DS(G,UITEXT[g_langIdx].save_settings,sbx,sy,sbw,sbh,CWHITE,13,true,1);
AB(sbx,sy,sbw,sbh,740); sy+=56;
// Help dialog overlay
if(g_showHelp){
float hw=500,hh=320,hx=(float)WW/2-hw/2,hy=(float)WH/2-hh/2;
SolidBrush dim(Color(160,0,0,0));G.FillRectangle(&dim,0.f,0.f,(float)WW,(float)WH);
FRR(G,Color(255,32,32,42),hx,hy,hw,hh,12.f);
SRR(G,CBORD2,hx,hy,hw,hh,1.f,12.f);
DS(G,L"Справка Fluenti",hx+24,hy+16,hw-48,32,CTXT,16,true);
LN(G,CBORD,hx+16,hy+52,hx+hw-16,hy+52);
float ty=hy+64;float tw=hw-48;
DS(G,L"Fluenti - переводчик на базе Google Translate.",hx+24,ty,tw,22,CTXT,13);ty+=26;
DS(G,L"Версия: 1.0",hx+24,ty,tw,20,CTXT2,12);ty+=22;
DS(G,L"Разработчик: Konstantin Gorbunov",hx+24,ty,tw,20,CTXT2,12);ty+=22;
DS(G,L"Компания: InoMotion",hx+24,ty,tw,20,CTXT2,12);ty+=22;
DS(G,L"Сайт: http://b91660kf.beget.tech",hx+24,ty,tw,20,CBLUE,12);ty+=30;
DS(G,L"Горячие клавиши:",hx+24,ty,tw,20,CTXT,13,true);ty+=24;
DS(G,L"Ctrl+Enter — перевести",hx+24,ty,tw,20,CTXT2,12);ty+=20;
DS(G,L"Esc — закрыть диалог",hx+24,ty,tw,20,CTXT2,12);ty+=30;
// Close button
FRR(G,(g_hovB==750)?CBLUEHV:CBLUE,hx+hw/2-60,hy+hh-52,120,36,18.f);
DS(G,g_langIdx==0?L"Закрыть":L"Close",hx+hw/2-60,hy+hh-52,120,36,CWHITE,13,true,1);
AB(hx+hw/2-60,hy+hh-52,120,36,750);
}
// Dropdown overlays
if(g_langDrop){
const wchar_t*langs[]={L"Русский",L"English"};
float dx=rx,dy=88+44+20+32+46,dw=rw,dh=2*30+8;
SolidBrush sh2(Color(60,0,0,0));G.FillRectangle(&sh2,dx+3,dy+3,dw,dh);
FRR(G,Color(255,38,38,50),dx,dy,dw,dh,6.f);SRR(G,CBLUE,dx,dy,dw,dh,1.5f,6.f);
for(int i=0;i<2;i++){
bool sel=(g_langIdx==i),hov=(g_hovL==i);
if(sel)FRR(G,CBLUELT,dx+4,dy+4+i*30,dw-8,28,4.f);
else if(hov)FRR(G,CHOV,dx+4,dy+4+i*30,dw-8,28,4.f);
DS(G,langs[i],dx+12,dy+4+i*30,dw-20,28,sel?CBLUE:CTXT,13,sel);
}
}
if(g_fontDrop){
float dx=rx,dy=88+44+20+32+46+46,dw=rw,dh=FONT_COUNT*30+8;
SolidBrush sh2(Color(60,0,0,0));G.FillRectangle(&sh2,dx+3,dy+3,dw,dh);
FRR(G,Color(255,38,38,50),dx,dy,dw,dh,6.f);SRR(G,CBLUE,dx,dy,dw,dh,1.5f,6.f);
for(int i=0;i<FONT_COUNT;i++){
bool sel=(g_fontIdx==i),hov=(g_hovL==i+10);
if(sel)FRR(G,CBLUELT,dx+4,dy+4+i*30,dw-8,28,4.f);
else if(hov)FRR(G,CHOV,dx+4,dy+4+i*30,dw-8,28,4.f);
FontFamily ffi(FONTS[i]);Font fi(&ffi,13,FontStyleRegular,UnitPixel);
SolidBrush fib(sel?CBLUE:CTXT);StringFormat sfi;sfi.SetLineAlignment(StringAlignmentCenter);
RectF frc(dx+12,dy+4+i*30,dw-20,28);G.DrawString(FONTS[i],-1,&fi,frc,&sfi,&fib);
}
}
}
// ── CONTEXT MENU ─────────────────────────────────────
if(g_ctxMenu&&g_view==0){
// Menu items: Paste, Copy, Cut(src only), Select All, Clear(src only)
struct MI{const wchar_t*ru;const wchar_t*en;bool srcOnly;};
MI items[]={{L"Вставить",L"Paste",false},{L"Копировать",L"Copy",false},
{L"Вырезать",L"Cut",true},{L"Выделить всё",L"Select all",true},{L"Очистить",L"Clear",true}};
int cnt=g_ctxOnSrc?5:2;
float mw2=160,mh=cnt*28+8;
float mx3=min((float)g_ctxX,(float)WW-mw2-4);
float my3=min((float)g_ctxY,(float)WH-mh-4);
SolidBrush sh3(Color(80,0,0,0));G.FillRectangle(&sh3,mx3+3,my3+3,mw2,mh);
FRR(G,CWHITE,mx3,my3,mw2,mh,6.f);
SRR(G,CBORD2,mx3,my3,mw2,mh,1.f,6.f);
for(int i=0;i<cnt;i++){
bool hov=(g_ctxHov==i);
if(hov)FRR(G,CBLUELT,mx3+4,my3+4+i*28,mw2-8,26,4.f);
DS(G,g_langIdx==0?items[i].ru:items[i].en,mx3+14,my3+4+i*28,mw2-20,26,hov?CBLUE:CTXT,13);
}
}
// ── DROPDOWNS ────────────────────────────────────────────
if(g_sdrop||g_ddrop){
float dx=g_sdrop?24.f:24.f+180.f+14.f+50.f;
float dy=118.f,dw=190.f,dh=(float)(LC*30+8);
// shadow
SolidBrush sh(Color(40,0,0,0));G.FillRectangle(&sh,dx+3,dy+3,dw,dh);
FRR(G,CWHITE,dx,dy,dw,dh,8.f);SRR(G,CBORD2,dx,dy,dw,dh,1.5f,8.f);
int cur=g_sdrop?g_src:g_dst;
for(int i=0;i<LC;i++){
float ly=dy+4+i*30.f;bool sel=(i==cur),hov=(g_hovL==i);
if(sel)FRR(G,CBLUELT,dx+4,ly+1,dw-8,28,6.f);
else if(hov)FRR(G,CHOV,dx+4,ly+1,dw-8,28,6.f);
DS(G,LG[i].display,dx+14,ly+2,dw-24,26,sel?CBLUE:CTXT,13,sel);
}
}
Graphics sc(hdc);sc.DrawImage(&bmp,0,0);
}
// ── Input ─────────────────────────────────────────────────────
void updateHover(int mx,int my){
int prev=g_hovB;g_hovB=-1;g_hovL=-1;
if(g_sdrop||g_ddrop){
float dx=g_sdrop?28.f:28.f+180.f+64.f,dy=122.f;
for(int i=0;i<LC;i++)if(inB(mx,my,dx+4,dy+4+i*30.f,182,28)){g_hovL=i;break;}
if(g_hovL>=0){if(g_hovB!=prev)InvalidateRect(g_hwnd,NULL,FALSE);return;}
}
if(g_langDrop){
float dx=48+320,dy=88+44+20+32+46;
for(int i=0;i<2;i++)if(inB(mx,my,dx+4,dy+4+i*30,196,28)){g_hovL=i;break;}
if(g_hovL>=0){if(g_hovB!=prev)InvalidateRect(g_hwnd,NULL,FALSE);return;}
}
if(g_fontDrop){
float dx=48+320,dy=88+44+20+32+46+46;
for(int i=0;i<FONT_COUNT;i++)if(inB(mx,my,dx+4,dy+4+i*30,196,28)){g_hovL=i+10;break;}
if(g_hovL>=0){if(g_hovB!=prev)InvalidateRect(g_hwnd,NULL,FALSE);return;}
}
for(auto&b:g_btns)if(inB(mx,my,b.x,b.y,b.w,b.h)){g_hovB=b.id;break;}
if(g_hovB!=prev)InvalidateRect(g_hwnd,NULL,FALSE);
}
void handleClick(int mx,int my){
// Context menu click
if(g_ctxMenu){
int cnt=g_ctxOnSrc?5:2;
float mx3=min((float)g_ctxX,(float)WW-164);
float my3=min((float)g_ctxY,(float)WH-(cnt*28+12));
for(int i=0;i<cnt;i++){
if(inB(mx,my,mx3+4,my3+4+i*28,152,26)){
if(i==0){ // Вставить
if(OpenClipboard(g_hwnd)){
HANDLE hd=GetClipboardData(CF_UNICODETEXT);
if(hd){wchar_t*txt=(wchar_t*)GlobalLock(hd);
if(txt&&g_ctxOnSrc){
g_srcT+=std::wstring(txt);
g_charCount=(int)g_srcT.size();
g_autoArmed=true;g_autoTick=GetTickCount();
}
GlobalUnlock(hd);
}
CloseClipboard();
}
}else if(i==1){ // Копировать
std::wstring&tgt=g_ctxOnSrc?g_srcT:g_dstT;
if(!tgt.empty()&&OpenClipboard(g_hwnd)){
EmptyClipboard();
HGLOBAL h=GlobalAlloc(GMEM_MOVEABLE,(tgt.size()+1)*2);
if(h){memcpy(GlobalLock(h),tgt.c_str(),(tgt.size()+1)*2);GlobalUnlock(h);SetClipboardData(CF_UNICODETEXT,h);}
CloseClipboard();
}
}else if(i==2){ // Вырезать