summaryrefslogtreecommitdiff
path: root/plugins/Utils.pas/sparam.pas
blob: 808493f2a5e534589dbf8d94a42a4de19f5519b1 (plain)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
{
  Parameter: CBN_EDITCHANGE on fields changing, BN_CLICKED on Struct changes
  Result   : CBN_EDITCHANGE on type   changing, BN_CLICKED on option changes
  ACF_FLAG_TEMPLATE saved in CB param of IDC_EDIT_PAR
  parameter type depends of ACF_FLAG_PAR
}
unit sparam;

interface

uses windows, m_api, awkservices;

//----- Parameter dialog -----

function CreateParamBlock(parent:HWND;x,y,width:integer;flags:dword=0):THANDLE;
function ClearParamFields(Dialog:HWND):HWND;
function FillParam       (Dialog:HWND;txt:PAnsiChar):integer;
function SetParamValue   (Dialog:HWND;    flags:dword;    value:pointer):boolean;
function GetParamValue   (Dialog:HWND;var flags:dword;var value:pointer):boolean;
function SetParamLabel   (Dialog:HWND; lbl:PWideChar):HWND;

//----- Parameter value -----

procedure SaveParamValue(    flags:dword;     param:pointer; module,setting:PAnsiChar);
procedure LoadParamValue(var flags:dword; var param:pointer; module,setting:PAnsiChar);
procedure ClearParam    (    flags:dword; var param);
function  DuplicateParam(    flags:dword; var sparam,dparam):dword;
{??
function  TranslateParam(param:uint_ptr;flags:dword;hContact:TMCONTACT):uint_ptr;
}

//----- Execution -----

function  PrepareParameter(flags:dword;const aparam:LPARAM; const data:tSubstData):LPARAM;
procedure ReleaseParameter(flags:dword;var   aparam:LPARAM);

//----- result dialog -----

function CreateResultBlock(parent:HWND;x,y,width:integer;flags:dword=0):THANDLE;
function ClearResultFields(Dialog:HWND):HWND;
function SetResultValue   (Dialog:HWND;flags:dword):integer;
function GetResultValue   (Dialog:HWND):dword;

//----- Old flags converting -----

function ConvertParamFlags (flags:dword):dword;
function ConvertResultFlags(flags:dword):dword;

implementation

uses
  messages,
  common, editwrapper, wrapper, syswin,
  sedit, strans,
  dbsettings,mirutils,mircontacts;

//----- Parameter dialog -----

const
  IDC_FLAG_PAR  = 2150;
  IDC_EDIT_PAR  = 2151;
  IDC_STRUCT    = 2152;
  IDC_LABEL_PAR = 2153;
  IDC_GROUP_PAR = 2154;

  IDC_RES_TYPE    = 2160;
  IDC_RES_FREEMEM = 2161;
  IDC_RES_SIGNED  = 2162;
  IDC_RES_HEXNUM  = 2163;


procedure InsertString(wnd:HWND;num:dword;str:PAnsiChar);
var
  buf:array [0..127] of WideChar;
begin
  SendMessageW(wnd,CB_SETITEMDATA,
      SendMessageW(wnd,CB_ADDSTRING,0,
          lparam(TranslateW(FastAnsiToWideBuf(str,buf)))),
      num);
end;

//----- Dialog functions -----

procedure MakeParamTypeList(wnd:HWND; flags:dword);
begin
  SendMessage(wnd,CB_RESETCONTENT,0,0);
  InsertString(wnd, ACF_TYPE_NUMBER , 'number value');
  InsertString(wnd, ACF_TYPE_STRING , 'ANSI string');
  InsertString(wnd, ACF_TYPE_UNICODE, 'Unicode string');
  if (flags and ACF_BLOCK_NOCURRENT)=0 then
    InsertString(wnd, ACF_TYPE_CURRENT, 'current contact');
  InsertString(wnd, ACF_TYPE_RESULT , 'last result');
  InsertString(wnd, ACF_TYPE_PARAM  , 'parameter');
  if (flags and ACF_BLOCK_NOSTRUCT)=0 then
    InsertString(wnd, ACF_TYPE_STRUCT, 'structure');
  SendMessage(wnd,CB_SETCURSEL,0,0);
end;

function IsParamNumber(txt:PAnsiChar):boolean;
begin
  if (txt[0] in ['0'..'9']) or ((txt[0]='-') and (txt[1] in ['0'..'9'])) or
    ((txt[0]='$') and (txt[1] in sHexNum)) or
    ((txt[0]='0') and (txt[1]='x') and (txt[2] in sHexNum)) then
    result:=true
  else
    result:=false;
end;

// Set parameter type by parameter template for non-numbers
function FixParam(buf:PAnsiChar):integer;
begin
  if      StrCmp(buf,'hContact'    )=0 then result:=ACF_TYPE_CURRENT
  else if StrCmp(buf,'parameter'   )=0 then result:=ACF_TYPE_PARAM
  else if StrCmp(buf,'result'      )=0 then result:=ACF_TYPE_RESULT
  else if StrCmp(buf,'structure'   )=0 then result:=ACF_TYPE_STRUCT
  else if StrCmp(buf,'Unicode text')=0 then result:=ACF_TYPE_UNICODE
  else                                      result:=ACF_TYPE_STRING;
end;

function FixParamControls(Dialog:HWND;atype:dword):dword;
var
  wnd,wnd1:HWND;
  pcw:PWideChar;
begin
  result:=atype;

  wnd :=GetDlgItem(Dialog,IDC_EDIT_PAR);
  wnd1:=GetDlgItem(Dialog,IDC_STRUCT);

  if atype=ACF_TYPE_STRUCT then
  begin
    ShowEditField(wnd ,SW_HIDE);
    ShowWindow   (wnd1,SW_SHOW);
  end
  else
  begin
    ShowEditField(wnd ,SW_SHOW);
    ShowWindow   (wnd1,SW_HIDE);

    if atype in [ACF_TYPE_CURRENT,ACF_TYPE_RESULT,ACF_TYPE_PARAM] then
      EnableEditField(wnd,false)
    else
    begin
      if atype=ACF_TYPE_NUMBER then
      begin
        if SendMessageW(wnd,WM_GETTEXTLENGTH,0,0)=0 then
        begin
          pcw:='0';
          SendMessageW(wnd,WM_SETTEXT,0,TLParam(pcw));
        end;
      end;
      EnableEditField(wnd,true);
    end;

  end;
end;

// get line from template
function GetParamLine(src:PAnsiChar;dst:PWideChar;var ltype:dword):PAnsiChar;
var
  pp,pc:PAnsiChar;
  j:integer;
  savechar:AnsiChar;
begin
  pc:=StrScan(src,'|');

  if pc<>nil then
  begin
    savechar:=pc^;
    pc^:=#0;
  end;

  if IsParamNumber(src) then
  begin
    j:=0;
    pp:=src;
    repeat
      dst[j]:=WideChar(pp^);
      inc(j); inc(pp);
    until (pp^=#0) or (pp^=' ');
    dst[j]:=WideChar(pp^); // anyway, #0 or " " needs
    if pp^<>#0 then
    begin
      dst[j+1]:='-'; dst[j+2]:=' '; inc(j,3);
      FastAnsiToWideBuf(pp+1,dst+j);
      StrCopyW(dst+j,TranslateW(dst+j));
    end;
    ltype:=ACF_TYPE_NUMBER;
  end
  else
  begin
    ltype:=FixParam(src);
    StrCopyW(dst,TranslateW(FastAnsiToWideBuf(src,dst)));
  end;

  if pc<>nil then
  begin
    pc^:=savechar;
    inc(pc);
  end;

  result:=pc;
end;

// Set parameter value by parameter template
function FillParam(Dialog:HWND;txt:PAnsiChar):integer;
var
  bufw:array [0..2047] of WideChar;
  wnd:HWND;
  p,pc:PAnsiChar;
  flags,ltype:dword;
begin
  wnd:=GetDlgItem(Dialog,IDC_EDIT_PAR);
  flags:=CB_GetData(wnd);
  CB_SetData(wnd,flags or ACF_FLAG_TEMPLATE);
  SendMessage(wnd,CB_RESETCONTENT,0,0);
  if (txt<>nil) and (txt^<>#0) then
  begin
    result:=-1;
    p:=txt;
    repeat
      pc:=GetParamLine(p,bufw,ltype);
      if result<0 then
        result:=ltype;
      SendMessageW(wnd,CB_ADDSTRING,0,lparam(@bufw));

      if result=ACF_TYPE_STRUCT then
        break
      else
        p:=pc;
    until pc=nil;
  end
  else
    result:=ACF_TYPE_NUMBER;
  SendMessage(wnd,CB_SETCURSEL,0,0);

  CB_SelectData(GetDlgItem(Dialog,IDC_FLAG_PAR),result);
  //!!!! need to set ACF_FLAG_TEMPLATE here
  FixParamControls(Dialog,result);
end;

function ClearParamFields(Dialog:HWND):HWND;
var
  wnd:HWND;
begin
  wnd:=GetDlgItem(Dialog,IDC_EDIT_PAR);
  SendMessage    (wnd,CB_RESETCONTENT,0,0);
  SetEditFlags   (wnd,EF_ALL,0);
  CB_SelectData(Dialog,IDC_FLAG_PAR,ACF_TYPE_NUMBER);
  FixParamControls(Dialog,ACF_TYPE_NUMBER);
  result:=Dialog;
end;

function DlgParamProc(Dialog:HWND;hMessage:uint;wParam:WPARAM;lParam:LPARAM):LRESULT; stdcall;
var
  wnd,wnd1:HWND;
  proc:pointer;
  pcw:PWideChar;
  pc,pc1:PAnsiChar;
  i:integer;
begin
  result:=0;

  case hMessage of
    WM_DESTROY: begin
      pc:=PAnsiChar(GetWindowLongPtrW(GetDlgItem(Dialog,IDC_STRUCT),GWLP_USERDATA));
      mFreeMem(pc);
    end;

    WM_SHOWWINDOW: begin
      // hide window by ShowWindow function
      if (lParam=0) and (wParam=0) then
      begin
        pc:=PAnsiChar(SetWindowLongPtrW(GetDlgItem(Dialog,IDC_STRUCT),GWLP_USERDATA,0));
        mFreeMem(pc);
      end;
    end;

    WM_COMMAND: begin
      case wParam shr 16 of
        CBN_EDITCHANGE,
        EN_CHANGE: begin
          SendMessage(GetParent(Dialog),WM_COMMAND,CBN_EDITCHANGE shl 16,Dialog);
        end;

        CBN_SELCHANGE:  begin
          SendMessage(GetParent(Dialog),WM_COMMAND,CBN_EDITCHANGE shl 16,Dialog);
          case loword(wParam) of
            IDC_FLAG_PAR: begin
              wnd :=GetDlgItem(Dialog,IDC_EDIT_PAR);
              wnd1:=GetDlgItem(Dialog,IDC_STRUCT);

              i:=CB_GetData(GetDlgItem(Dialog,loword(wParam)));

              if i=ACF_TYPE_STRUCT then
              begin
                ShowEditField(wnd ,SW_HIDE);
                ShowWindow   (wnd1,SW_SHOW);
              end
              else
              begin
                ShowEditField(wnd ,SW_SHOW);
                ShowWindow   (wnd1,SW_HIDE);

                if i in [ACF_TYPE_CURRENT,ACF_TYPE_RESULT,ACF_TYPE_PARAM] then
                  EnableEditField(wnd,false)
                else
                begin
                  if i=ACF_TYPE_NUMBER then
                  begin
                    pcw:='0';
                    SendMessageW(wnd,WM_SETTEXT,0,TLParam(pcw));
                  end;
                  EnableEditField(wnd,true);
                end;

              end;
            end;
          end;
        end;

        BN_CLICKED: begin
          case loword(wParam) of
            IDC_STRUCT: begin
              pc:=PAnsiChar(GetWindowLongPtrW(lParam,GWLP_USERDATA));
//!!!!
              pc1:=EditStructure(pc{,Dialog});
              if pc1<>nil then
              begin
                mFreeMem(pc);
                SetWindowLongPtrW(lParam,GWLP_USERDATA,long_ptr(pc1));
                SendMessage(GetParent(Dialog),WM_COMMAND,BN_CLICKED shl 16,Dialog);
              end;
            end;
          else
            // can be just edit field wrapper button, no need to react
          end;
        end;

      end;
    end;
  else
    proc:=pointer(GetWindowLongPtrW(Dialog,GWLP_USERDATA));
    result:=CallWindowProc(proc,Dialog,hMessage,wParam,lParam)
  end;
end;

//----- Common interface functions -----

function CreateParamBlock(parent:HWND;x,y,width:integer;flags:dword=0):THANDLE;
var
  hf:HFONT;
  group,ctrl:HWND;
  proc:pointer;
  rc:TRECT;
  fullline:bool;
  gx,dx,dy,xo,yo:integer;
  ux,uy:integer;
begin
  hf:=SendMessageW(parent,WM_GETFONT,0,0);
  GetUnitSize(parent,ux,uy);

  // block
  SetRect(rc,x,y,x+width,y+31);
  dx:=rc.right-rc.left;
  dy:=rc.bottom-rc.top;

  result:=CreateWindowExW(WS_EX_CONTROLPARENT,'STATIC',nil,WS_CHILD+WS_VISIBLE,
          x,y,dx,dy, parent,0,hInstance,nil);
  proc:=pointer(SetWindowLongPtrW(result,GWLP_WNDPROC,long_ptr(@DlgParamProc)));
  SetWindowLongPtrW(result,GWLP_USERDATA,long_ptr(proc));

  yo:=0;

  // group border
  if (flags and ACF_BLOCK_NOBORDER)=0 then
  begin
    group:=CreateWindowW('BUTTON','Param',WS_CHILD+WS_VISIBLE+WS_GROUP+BS_GROUPBOX,
          0,0,dx,dy, result,IDC_GROUP_PAR,hInstance,nil);
    SendMessageW(group,WM_SETFONT,hf,0);
    gx:=4;
    inc(yo,12);
  end
  else
  begin
    gx:=0;
  end;

  // label
  if (flags and ACF_BLOCK_NOSTATIC)=0 then
  begin

    if width<=150 then
    begin
      fullline:=true;
      rc.bottom:=11*uy div 8;
      xo:=dx-gx*2;
    end
    else
    begin
      fullline:=false;
      rc.bottom:=14*uy div 8; // same as param type combobox
      xo:=dx div 3;
    end;
    ctrl:=CreateWindowW('STATIC','Param type',WS_CHILD+WS_VISIBLE+SS_RIGHT+SS_CENTERIMAGE,
          gx,yo,xo-gx,rc.bottom, result,IDC_LABEL_PAR,hInstance,nil);
    SendMessageW(ctrl,WM_SETFONT,hf,0);
  end
  else
  begin
    fullline:=true;
  end;

  // param type
  rc.bottom:=14*uy div 8;
  if fullline then
  begin
    xo:=gx;
    if (flags and ACF_BLOCK_NOSTATIC)=0 then
      inc(yo,rc.bottom);
  end;
  ctrl:=CreateWindowW('COMBOBOX',nil,WS_CHILD+WS_VISIBLE+WS_VSCROLL+CBS_DROPDOWNLIST+CBS_AUTOHSCROLL,
        xo+2,yo,dx-xo-gx-2,56, result,IDC_FLAG_PAR,hInstance,nil);

  SendMessageW(ctrl,WM_SETFONT,hf,0);
  MakeParamTypeList(ctrl,flags);
  inc(yo,rc.bottom+2);

  // param value
  rc.bottom:=14*uy div 8;

  ctrl:=CreateWindowW('COMBOBOX',nil,WS_CHILD+WS_VISIBLE+WS_VSCROLL+CBS_DROPDOWN+CBS_AUTOHSCROLL,
        gx,yo,dx-gx*2,76, result,IDC_EDIT_PAR,hInstance,nil);
  SendMessageW(ctrl,WM_SETFONT,hf,0);
  MakeEditField(result,IDC_EDIT_PAR);

  ctrl:=CreateWindowW('BUTTON','Structure',WS_CHILD+WS_VISIBLE+BS_PUSHBUTTON,
        gx,yo,dx-gx*2,rc.bottom, result,IDC_STRUCT,hInstance,nil);
  SendMessageW(ctrl,WM_SETFONT,hf,0);
  inc(yo,rc.bottom+4);

  // resize group and dialog
  MoveWindow(result,x,y,dx,yo,false);
  if (flags and ACF_BLOCK_NOBORDER)=0 then
    MoveWindow(group,0,0,dx,yo,false);

  ClearParamFields(result);
end;

// if separate
function DestroyBlock(block:pointer):integer;
begin
  result:=0;
end;

function SetParamLabel(Dialog:HWND; lbl:PWideChar):HWND;
var
  wnd:HWND;
begin
  result:=Dialog;

  if Dialog=0 then
    exit;

  wnd:=GetDlgItem(Dialog,IDC_LABEL_PAR);
  if wnd<>0 then
    SendMessageW(wnd,WM_SETTEXT,0,LPARAM(lbl))
  else
  begin
    wnd:=GetDlgItem(Dialog,IDC_GROUP_PAR);
    if wnd<>0 then
      SendMessageW(wnd,WM_SETTEXT,0,LPARAM(lbl))
  end;
end;

function SetParamValue(Dialog:HWND;flags:dword;value:pointer):boolean;
var
  wnd,wnd1:HWND;
  pc:PAnsiChar;
  pcw:PWideChar;
  vtype:integer;
begin
  if Dialog=0 then
  begin
    result:=false;
    exit;
  end;

  result:=true;

  wnd:=GetDlgItem(Dialog,IDC_EDIT_PAR);
  if (flags and ACF_FLAG_TEMPLATE)<>0 then
  begin
    vtype:=FillParam(Dialog,value);
  end
  else
  begin
    CB_SetData(wnd,flags and not ACF_TYPE_MASK);
    vtype:=flags and ACF_TYPE_MASK;
    case vtype of
      ACF_TYPE_PARAM: begin
        SendMessageW(wnd,WM_SETTEXT,0,LPARAM(TranslateW('Parameter')));
        EnableWindow(wnd,false);
      end;
      ACF_TYPE_RESULT: begin
        SendMessageW(wnd,WM_SETTEXT,0,LPARAM(TranslateW('Last result')));
        EnableWindow(wnd,false);
      end;
      ACF_TYPE_CURRENT: begin
        SendMessageW(wnd,WM_SETTEXT,0,LPARAM(TranslateW('Current user')));
        EnableWindow(wnd,false);
      end;
      ACF_TYPE_STRUCT: begin
        ShowEditField(wnd,SW_HIDE);
        wnd1:=GetDlgItem(Dialog,IDC_STRUCT);
        ShowWindow(wnd1,SW_SHOW);
        // delete old value
        pc:=PAnsiChar(GetWindowLongPtrW(wnd1,GWLP_USERDATA));
        mFreeMem(pc);
        // set newly allocated
        SetWindowLongPtrW(wnd1,GWLP_USERDATA,long_ptr(StrDup(pc,PAnsiChar(value))));
    //!!!!!!!!
      end;
      ACF_TYPE_NUMBER: begin
        if value=nil then
          pcw:='0'
        else
          pcw:=value;
        SendMessageW(wnd,WM_SETTEXT,0,LPARAM(pcw));
      end;
      ACF_TYPE_UNICODE,
      ACF_TYPE_STRING: begin
        SendMessageW(wnd,WM_SETTEXT,0,LPARAM(value));
      end;
    end;
  end;
  SetEditFlags(wnd,EF_SCRIPT,ord((flags and ACF_FLAG_SCRIPT)<>0));
  
  CB_SelectData(GetDlgItem(Dialog,IDC_FLAG_PAR),vtype);
  FixParamControls(Dialog,vtype);
end;

//?? preserve ACF_TEMPLATE flag??
function GetParamValue(Dialog:HWND;var flags:dword;var value:pointer):boolean;
var
  wnd:HWND;
begin
  if Dialog=0 then
  begin
    result:=false;
    exit;
  end;

  result:=true;
  value:=nil;
  wnd:=GetDlgItem(Dialog,IDC_EDIT_PAR);
  flags:=CB_GetData(GetDlgItem(Dialog,IDC_FLAG_PAR));
  case flags of
    ACF_TYPE_UNICODE,
    ACF_TYPE_STRING,
    ACF_TYPE_NUMBER: begin
      value:=GetDlgText(wnd);
    end;
    ACF_TYPE_STRUCT: begin
      StrDup(PAnsiChar(value),
          PAnsiChar(GetWindowLongPtrW(GetDlgItem(Dialog,IDC_STRUCT),GWLP_USERDATA)));
    end;
  end;
  if (GetEditFlags(wnd) and EF_SCRIPT)<>0 then
     flags:=flags or ACF_FLAG_SCRIPT;
  // for example, ACF_FLAG_TEMPLATE
  flags:=flags or CB_GetData(wnd);
end;

//----- Parameter value -----

const
  ioflags:PAnsiChar = 'flags';
  iovalue:PAnsiChar = 'value';

procedure SaveParamValue(flags:dword; param:pointer; module,setting:PAnsiChar);
var
  buf:array [0..127] of AnsiChar;
  p:PAnsiChar;
begin
  p:=StrCopyE(buf,setting); p^:='/'; inc(p);
  StrCopy(p,ioflags); DBWriteDWord(0,module,buf,flags);
  StrCopy(p,iovalue);
  case flags and ACF_TYPE_MASK of
    ACF_TYPE_NUMBER,
    ACF_TYPE_STRING,
    ACF_TYPE_UNICODE: DBWriteUnicode(0,module,buf,param);
    ACF_TYPE_STRUCT : DBWriteString (0,module,buf,param);
  end;
end;

procedure LoadParamValue(var flags:dword; var param:pointer; module,setting:PAnsiChar);
var
  buf:array [0..127] of AnsiChar;
  p:PAnsiChar;
begin
  p:=StrCopyE(buf,setting); p^:='/'; inc(p);
  StrCopy(p,ioflags); flags:=DBReadDWord(0,module,buf);
  StrCopy(p,iovalue);
  case flags and ACF_TYPE_MASK of
    ACF_TYPE_NUMBER,
    ACF_TYPE_STRING,
    ACF_TYPE_UNICODE: param:=DBReadUnicode(0,module,buf);
    ACF_TYPE_STRUCT : param:=DBReadString (0,module,buf);
  end;
end;

procedure ClearParam(flags:dword; var param);
begin
{
  if not ((flags and ACF_TYPE_MASK) in [ACF_TYPE_CURRENT, ACF_TYPE_RESULT, ACF_TYPE_PARAM]) then
    mFreeMem(pointer(param));
}
  case flags of
    ACF_TYPE_NUMBER,
    ACF_TYPE_STRING,
    ACF_TYPE_UNICODE,
    ACF_TYPE_STRUCT : mFreeMem(pointer(param));
  end;
end;

//----- Execution -----

function PrepareParameter(flags:dword;const aparam:LPARAM; const data:tSubstData):LPARAM;
var
  tmp1:PWideChar;
  typ:dword;
begin
  typ:=flags and ACF_TYPE_MASK;
  case typ of
    ACF_TYPE_STRUCT: begin
      result:=uint_ptr(MakeStructure(pAnsiChar(aparam),
          data.Parameter,data.LastResult,data.ResultType=ACF_TYPE_NUMBER))
    end;

    ACF_TYPE_PARAM: begin
      result:=data.Parameter;
    end;

    ACF_TYPE_RESULT: begin
      result:=data.LastResult;
    end;

    ACF_TYPE_CURRENT: begin
      result:=WndToContact(WaitFocusedWndChild(GetForegroundwindow){GetFocus});
    end;

    ACF_TYPE_NUMBER,
    ACF_TYPE_STRING,
    ACF_TYPE_UNICODE: begin
      if (flags and ACF_FLAG_SCRIPT)<>0 then
        tmp1:=ParseVarString(pWideChar(aparam),data.Parameter)
      else
        tmp1:=pWideChar(aparam);

      if typ<>ACF_TYPE_NUMBER then
      begin
        if typ<>ACF_TYPE_UNICODE then
          WideToAnsi(tmp1,pAnsiChar(result),MirandaCP)
        else
          StrDupW(pWideChar(result),tmp1);
      end
      else
        result:=NumToInt(tmp1);

      if (flags and ACF_FLAG_SCRIPT)<>0 then
        mFreeMem(tmp1);
    end;
  else
    result:=0;
  end;
end;

procedure ReleaseParameter(flags:dword;var aparam:LPARAM);
begin
  case flags and ACF_TYPE_MASK of
    ACF_TYPE_STRING,
    ACF_TYPE_UNICODE: mFreeMem(pointer(aparam));
    ACF_TYPE_STRUCT : FreeStructure(aparam);
  end;
end;

//----- Additional functions -----

function DuplicateParam(flags:dword; var sparam,dparam):dword;
var
  tmpdst:array [0..2047] of WideChar;
  ltype:dword;
begin
  mFreeMem(dparam);

  if (flags and ACF_FLAG_TEMPLATE)<>0 then
  begin
    flags:=flags and not (ACF_FLAG_TEMPLATE or ACF_TYPE_MASK);
    GetParamLine(PAnsiChar(sparam),tmpdst,ltype);
    flags:=flags or ltype;
    case ltype of
      ACF_TYPE_NUMBER,
      ACF_TYPE_STRING,
      ACF_TYPE_UNICODE: begin
        StrDupW(PWideChar(dparam),PWideChar(@tmpdst));
      end;
      ACF_TYPE_STRUCT: begin
        StrDup(PAnsiChar(dparam),PAnsiChar(sparam)+10); //10=StrLen('structure|')
      end;
    end;
  end
  else
  begin
    case flags and ACF_TYPE_MASK of
      ACF_TYPE_NUMBER,
      ACF_TYPE_STRING,
      ACF_TYPE_UNICODE: begin
        StrDupW(PWideChar(dparam),PWideChar(sparam))
      end;
      ACF_TYPE_STRUCT: begin
        StrDup(PAnsiChar(dparam),PAnsiChar(sparam))
      end;
    end;
  end;
  result:=flags;
end;

{??
function TranslateParam(param:uint_ptr;flags:dword;hContact:TMCONTACT):uint_ptr;
var
  tmp1:PWideChar;
begin
  if (flags and ACF_SCRIPT_PARAM)<>0 then
    result:=uint_ptr(ParseVarString(PWideChar(param),hContact));

  tmp1:=PWideChar(result);
  case flags and ACF_TYPE_MASK of
    ACF_TYPE_NUMBER: begin
      result:=NumToInt(tmp1);
    end;
    ACF_TYPE_UNICODE: begin
      StrDupW(PWideChar(result),tmp1);
    end;
    ACF_TYPE_STRING,
    ACF_TYPE_STRUCT: begin
      WideToAnsi(tmp1,PAnsiChar(result),MirandaCP)
    end;
  end
  else

  if (flags and ACF_SCRIPT_PARAM)<>0 then
    mFreeMem(tmp1);
end;
}


//----- Result dialog -----

function ClearResultFields(Dialog:HWND):HWND;
var
  w:HWND;
begin
  result:=Dialog;

  if Dialog=0 then
    exit;

  CheckDlgButton(Dialog,IDC_RES_FREEMEM,BST_UNCHECKED);
  ShowWindow(GetDlgItem(Dialog,IDC_RES_FREEMEM),SW_HIDE);
  CB_SelectData(Dialog,IDC_RES_TYPE,ACF_TYPE_NUMBER);

  w:=GetDlgItem(Dialog,IDC_RES_HEXNUM);
  if w<>0 then
  begin
    ShowWindow(w,SW_SHOW);
    ShowWindow(GetDlgItem(Dialog,IDC_RES_SIGNED),SW_SHOW);
  end;
end;

procedure MakeResultTypeList(wnd:HWND;flags:dword);
begin
  SendMessage(wnd,CB_RESETCONTENT,0,0);
  InsertString(wnd, ACF_TYPE_NUMBER , 'number value');
  InsertString(wnd, ACF_TYPE_STRING , 'ANSI string');
  InsertString(wnd, ACF_TYPE_UNICODE, 'Unicode string');
  if (flags and ACF_BLOCK_NOSTRUCT)=0 then
    InsertString(wnd, ACF_TYPE_STRUCT, 'structure');
  SendMessage(wnd,CB_SETCURSEL,0,0);
end;

function DlgResultProc(Dialog:HWND;hMessage:uint;wParam:WPARAM;lParam:LPARAM):LRESULT; stdcall;
var
  proc:pointer;
  wnd:HWND;
  b:bool;
  i,j:integer;
begin
  result:=0;

  case hMessage of
    WM_COMMAND: begin
      case wParam shr 16 of
        BN_CLICKED: begin
          SendMessage(GetParent(Dialog),WM_COMMAND,BN_CLICKED shl 16,Dialog);
          case loword(wParam) of
            IDC_RES_SIGNED: begin
              if IsDlgButtonChecked(Dialog,IDC_RES_SIGNED)=BST_UNCHECKED then
              begin
                b:=true;
              end
              else
              begin
                b:=false;
              end;
              EnableWindow(GetDlgItem(Dialog,IDC_RES_HEXNUM),b);
            end;
            IDC_RES_HEXNUM: begin
              if IsDlgButtonChecked(Dialog,IDC_RES_HEXNUM)=BST_UNCHECKED then
              begin
                b:=true;
              end
              else
              begin
                b:=false;
              end;
              EnableWindow(GetDlgItem(Dialog,IDC_RES_SIGNED),b);
            end;
          end;
        end;

        CBN_SELCHANGE:  begin
          SendMessage(GetParent(Dialog),WM_COMMAND,CBN_EDITCHANGE shl 16,Dialog);
          case loword(wParam) of
            IDC_RES_TYPE: begin
              case CB_GetData(lParam) of
                ACF_TYPE_NUMBER: begin
                  i:=SW_HIDE;
                  j:=SW_SHOW;
                end;
                ACF_TYPE_STRUCT: begin
                  i:=SW_HIDE;
                  j:=SW_HIDE;
                end;
                ACF_TYPE_STRING,
                ACF_TYPE_UNICODE: begin
                  i:=SW_SHOW;
                  j:=SW_HIDE;
                end;
              end;
              ShowWindow(GetDlgItem(Dialog,IDC_RES_FREEMEM),i);
              wnd:=GetDlgItem(Dialog,IDC_RES_HEXNUM);
              if wnd<>0 then
              begin
                ShowWindow(wnd,j);
                ShowWindow(GetDlgItem(Dialog,IDC_RES_SIGNED),j);
              end;
            end;
          end;
        end;
      end;
    end;
  else
    proc:=pointer(GetWindowLongPtrW(Dialog,GWLP_USERDATA));
    result:=CallWindowProc(proc,Dialog,hMessage,wParam,lParam)
  end;
end;

function CreateResultBlock(parent:HWND;x,y,width:integer;flags:dword=0):THANDLE;
var
  hf:HFONT;
  ctrl,group:HWND;
  proc:pointer;
  rc:TRECT;
  fullline:bool;
  dx,dy,yo,gx,xo:integer;
  ux,uy:integer;
begin
  hf:=SendMessageW(parent,WM_GETFONT,0,0);
  GetUnitSize(parent,ux,uy);

  // block body
  SetRect(rc,x,y,x+width,y+53);
  dx:=rc.right-rc.left;
  dy:=rc.bottom-rc.top;

  result:=CreateWindowExW(WS_EX_CONTROLPARENT,'STATIC',nil,WS_CHILD+WS_VISIBLE,
          x,y,dx,dy, parent,0,hInstance,nil);
  proc:=pointer(SetWindowLongPtrW(result,GWLP_WNDPROC,long_ptr(@DlgResultProc)));
  SetWindowLongPtrW(result,GWLP_USERDATA,long_ptr(proc));

  yo:=0;

  // group border
  if (flags and ACF_BLOCK_NOBORDER)=0 then
  begin
    group:=CreateWindowW('BUTTON','Result',WS_CHILD+WS_VISIBLE+WS_GROUP+BS_GROUPBOX,
          0,0,dx,dy, result,0,hInstance,nil);
    SendMessageW(group,WM_SETFONT,hf,0);
    gx:=4;
    inc(yo,12);
  end
  else
  begin
    gx:=0;
  end;

  // label
  if (flags and ACF_BLOCK_NOSTATIC)=0 then
  begin
    if width<=150 then
    begin
      fullline:=true;
      rc.bottom:=11*uy div 8;
      xo:=dx-gx*2;
    end
    else
    begin
      fullline:=false;
      rc.bottom:=14*uy div 8; // same as param type combobox
      xo:=dx div 3;
    end;
    ctrl:=CreateWindowW('STATIC','Result type',WS_CHILD+WS_VISIBLE+SS_RIGHT+SS_CENTERIMAGE,
          gx,yo,xo-gx,rc.bottom, result,IDC_LABEL_PAR,hInstance,nil);
    SendMessageW(ctrl,WM_SETFONT,hf,0);
  end
  else
  begin
    fullline:=true;
  end;

  // result type
  rc.bottom:=14*uy div 8;
  if fullline then
  begin
    xo:=gx;
    if (flags and ACF_BLOCK_NOSTATIC)=0 then
      inc(yo,rc.bottom);
  end;
  ctrl:=CreateWindowW('COMBOBOX',nil,WS_CHILD+WS_VISIBLE+WS_VSCROLL+CBS_DROPDOWNLIST+CBS_AUTOHSCROLL,
        xo+2,yo,dx-xo-gx-2,76, result,IDC_RES_TYPE,hInstance,nil);

  SendMessageW(ctrl,WM_SETFONT,hf,0);
  MakeResultTypeList(ctrl,flags);
  inc(yo,rc.bottom+2);

  // 'free memory' checkbox
  rc.bottom:=11*uy div 8;

  ctrl:=CreateWindowW('BUTTON','Free memory',WS_CHILD+WS_VISIBLE+BS_AUTOCHECKBOX,
        gx,yo,dx-gx*2,rc.bottom, result,IDC_RES_FREEMEM,hInstance,nil);
  SendMessageW(ctrl,WM_SETFONT,hf,0);
  inc(yo,rc.bottom+4);

  if (flags and ACF_BLOCK_NOVISUAL)=0 then
  begin
    dec(yo,rc.bottom+4);

    ctrl:=CreateWindowW('BUTTON','Signed',WS_CHILD+WS_VISIBLE+BS_AUTOCHECKBOX,
          gx,yo,dx-gx*2,rc.bottom, result,IDC_RES_SIGNED,hInstance,nil);
    SendMessageW(ctrl,WM_SETFONT,hf,0);
    inc(yo,rc.bottom+2);

    ctrl:=CreateWindowW('BUTTON','As hex',WS_CHILD+WS_VISIBLE+BS_AUTOCHECKBOX,
          gx,yo,dx-gx*2,rc.bottom, result,IDC_RES_HEXNUM,hInstance,nil);
    SendMessageW(ctrl,WM_SETFONT,hf,0);
    inc(yo,rc.bottom+4);
  end;

  // resize group and dialog
  MoveWindow(result,x,y,dx,yo,false);
  if (flags and ACF_BLOCK_NOBORDER)=0 then
    MoveWindow(group,0,0,dx,yo,false);

  ClearResultFields(result);
end;

function SetResultValue(Dialog:HWND;flags:dword):integer;
var
  w:HWND;
  btn:cardinal;
  sh,sh1:integer;
begin
  if Dialog=0 then
  begin
    result:=ACF_TYPE_NUMBER;
    exit;
  end;

  // RESULT
  result:=flags and ACF_TYPE_MASK;
  
  sh :=SW_HIDE;
  sh1:=SW_HIDE;
  w:=GetDlgItem(Dialog,IDC_RES_HEXNUM);

  case result of
    ACF_TYPE_STRING,
    ACF_TYPE_UNICODE: begin
      sh:=SW_SHOW;

      if (flags and ACF_FLAG_FREEMEM)<>0 then
        btn:=BST_CHECKED
      else
        btn:=BST_UNCHECKED;
      CheckDlgButton(Dialog,IDC_RES_FREEMEM,btn);
    end;

    ACF_TYPE_NUMBER: begin
      if w<>0 then
      begin
        sh1:=SW_SHOW;
        if (flags and ACF_FLAG_SIGNED)<>0 then
          btn:=BST_CHECKED
        else
          btn:=BST_UNCHECKED;
        CheckDlgButton(Dialog,IDC_RES_SIGNED,btn);
        if (flags and ACF_FLAG_HEXNUM)<>0 then
          btn:=BST_CHECKED
        else
          btn:=BST_UNCHECKED;
        CheckDlgButton(Dialog,IDC_RES_HEXNUM,btn);
      end;
    end;
  end;

  ShowWindow(GetDlgItem(Dialog,IDC_RES_FREEMEM),sh);
  if w<>0 then
  begin
    ShowWindow(w,sh1);
    ShowWindow(GetDlgItem(Dialog,IDC_RES_SIGNED),sh1);
  end;
  CB_SelectData(Dialog,IDC_RES_TYPE,result);
end;

function GetResultValue(Dialog:HWND):dword;
begin
  if Dialog=0 then
  begin
    result:=ACF_TYPE_NUMBER;
    exit;
  end;

  result:=CB_GetData(GetDlgItem(Dialog,IDC_RES_TYPE));
  case result of
    ACF_TYPE_STRING,
    ACF_TYPE_UNICODE: begin
      if IsDlgButtonChecked(Dialog,IDC_RES_FREEMEM)=BST_CHECKED then
        result:=result or ACF_FLAG_FREEMEM;
    end;
    ACF_TYPE_NUMBER: begin
      if GetDlgItem(Dialog,IDC_RES_HEXNUM)<>0 then
      begin
        if IsDlgButtonChecked(Dialog,IDC_RES_SIGNED)=BST_CHECKED then
          result:=result or ACF_FLAG_SIGNED
        else if IsDlgButtonChecked(Dialog,IDC_RES_HEXNUM)=BST_CHECKED then
          result:=result or ACF_FLAG_HEXNUM;
      end;
    end;
  end;

end;

//--------------------------------

const
  // parameter flags
  ACF_OLD_NUMBER   = $00000001;
  ACF_OLD_UNICODE  = $00000002;
  ACF_OLD_CURRENT  = $00000004;

  ACF_OLD_RESULT   = $00000008;
  ACF_OLD_PARAM    = $00000010;
  ACF_OLD_STRUCT   = $00000020;
  ACF_PARTYPE  = ACF_OLD_NUMBER  or ACF_OLD_UNICODE or
                 ACF_OLD_CURRENT or ACF_OLD_RESULT  or
                 ACF_OLD_PARAM   or ACF_OLD_STRUCT;
  ACF_OLD_TEMPLATE     = $00000800;
  ACF_OLD_SCRIPT_PARAM = $00001000;
  // dummy
  ACF_OLD_STRING  = 0;
  ACF_OLD_RNUMBER = 0;

  ACF_OLD_RSTRING  = $00010000;
  ACF_OLD_RUNICODE = $00020000;
  ACF_OLD_RSTRUCT  = $00040000;
  ACF_OLD_RFREEMEM = $00080000;
  ACF_OLD_RHEXNUM  = $00100000;
  ACF_OLD_RSIGNED  = $00200000;

  ACF_RTYPE = ACF_OLD_RSTRING or ACF_OLD_RUNICODE or ACF_OLD_RSTRUCT;

  ACF_OLD_SCRIPT_SERVICE = $00800000;

function ConvertParamFlags(flags:dword):dword;
begin
  case flags and ACF_PARTYPE of
    ACF_OLD_NUMBER : result:=ACF_TYPE_NUMBER;
    ACF_OLD_UNICODE: result:=ACF_TYPE_UNICODE;
    ACF_OLD_CURRENT: result:=ACF_TYPE_CURRENT;
    ACF_OLD_RESULT : result:=ACF_TYPE_RESULT;
    ACF_OLD_PARAM  : result:=ACF_TYPE_PARAM;
    ACF_OLD_STRUCT : result:=ACF_TYPE_STRUCT;
    ACF_OLD_STRING : result:=ACF_TYPE_STRING;
  else
    result:=ACF_TYPE_NUMBER;
  end;
  if (flags and ACF_OLD_TEMPLATE    )<>0 then result:=result or ACF_FLAG_TEMPLATE;
  if (flags and ACF_OLD_SCRIPT_PARAM)<>0 then result:=result or ACF_FLAG_SCRIPT;
end;

function ConvertResultFlags(flags:dword):dword;
begin
  case flags and ACF_RTYPE of
    ACF_OLD_RNUMBER : result:=ACF_TYPE_NUMBER;
    ACF_OLD_RUNICODE: result:=ACF_TYPE_UNICODE;
    ACF_OLD_RSTRUCT : result:=ACF_TYPE_STRUCT;
    ACF_OLD_RSTRING : result:=ACF_TYPE_STRING;
  else
    result:=ACF_TYPE_NUMBER;
  end;
  if (flags and ACF_OLD_RFREEMEM)<>0 then result:=result or ACF_FLAG_FREEMEM;
  if (flags and ACF_OLD_RHEXNUM )<>0 then result:=result or ACF_FLAG_HEXNUM;
  if (flags and ACF_OLD_RSIGNED )<>0 then result:=result or ACF_FLAG_SIGNED;
  if (flags and ACF_OLD_SCRIPT_SERVICE)<>0 then result:=result or ACF_FLAG_SCRIPT;
end;

end.