summaryrefslogtreecommitdiff
path: root/plugins/Utils.pas/mApiCardM.pas
blob: cad4b36b25e8b6fc50f0438acc3f7b225a0e255a (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
{service insertion code}
unit mApiCardM;

interface

uses windows,messages;

type
  tmApiCard = class
  private
    function  GetDescription:PAnsiChar; 
    function  GetResultType :PAnsiChar; 
    procedure SetCurrentService(item:PAnsiChar);
    function  GetWindowStatus:boolean;
  public
    constructor Create(fname:PAnsiChar; lparent:HWND=0);
    destructor Destroy; override;
    procedure FillList(combo:HWND; mode:integer=0);

    function NameFromList(cb:HWND):PAnsiChar;
    function HashToName(ahash:longword):PAnsiChar;
    function FillParams(wnd:HWND{;item:PAnsiChar};wparam:boolean):PAnsiChar;
    function GetParam(wparam:boolean):PAnsiChar;
    procedure Show;//(item:PAnsiChar);

    property Description:PAnsiChar read GetDescription;
    property ResultType :PAnsiChar read GetResultType;
    property Service    :PAnsiChar write SetCurrentService;
    property Event      :PAnsiChar write SetCurrentService;
    property IsShown    :boolean   read GetWindowStatus;
  private
    storage:pointer;
    current:pointer;
    namespace: array [0.. 63] of AnsiChar;
    parent,
    HelpWindow:HWND;
    isServiceHelp:boolean;

    procedure Update(item:PAnsiChar=nil);
  end;

function CreateServiceCard(parent:HWND=0):tmApiCard;
function CreateEventCard  (parent:HWND=0):tmApiCard;

implementation

{
  mirutils unit is for ConvertFileName function only
  m_api is for TranslateW and TrandlateDialogDefault
}
uses common,io,m_api,mirutils,memini,wrapper;

{$r mApiCard.res}

{$include i_card_const.inc}

const
  WM_UPDATEHELP = WM_USER+100;

const
  BufSize = 2048;

const
  ApiHlpFile = 'plugins\services.ini';
{
  ServiceHlpFile = 'plugins\services.ini';
  EventsHlpFile  = 'plugins\events.ini';
}
function tmApiCard.GetResultType:PAnsiChar;
var
  buf:array [0..2047] of AnsiChar;
  p:PAnsiChar;
begin
  if storage<>nil then
  begin
    StrCopy(buf,GetParamSectionStr(current,'return',''));
    p:=@buf;
    while p^ in sWordOnly do inc(p);
    p^:=#0;
    StrDup(result,@buf);
  end
  else
    result:=nil;
end;

function tmApiCard.GetDescription:PAnsiChar;
begin
  if storage<>nil then
  begin
    StrDup(result,GetParamSectionStr(current,'descr',''));
  end
  else
    result:=nil;
end;

function tmApiCard.GetWindowStatus:boolean;
begin
  result:=HelpWindow<>0;
end;

function tmApiCard.GetParam(wparam:boolean):PAnsiChar;
var
  paramname:PAnsiChar;
begin
  if storage=nil then
  begin
    result:=nil;
    exit;
  end;
  if wparam then
    paramname:='wparam'
  else
    paramname:='lparam';

  StrDup(result,GetParamSectionStr(current,paramname,''));
end;

function tmApiCard.FillParams(wnd:HWND{;item:PAnsiChar};wparam:boolean):PAnsiChar;
var
  buf :array [0..2047] of AnsiChar;
  bufw:array [0..2047] of WideChar;
  j:integer;
  p,pp,pc:PAnsiChar;
  tmp:PWideChar;
  paramname:PAnsiChar;
begin
  if storage=nil then
  begin
    result:=nil;
    exit;
  end;
  if wparam then
    paramname:='wparam'
  else
    paramname:='lparam';

  StrCopy(buf,GetParamSectionStr(current,paramname,''));
  StrDup(result,@buf);

  if wnd=0 then
    exit;

  SendMessage(wnd,CB_RESETCONTENT,0,0);
  if buf[0]<>#0 then
  begin
    p:=@buf;
    GetMem(tmp,BufSize*SizeOf(WideChar));
    repeat
      pc:=StrScan(p,'|');
      if pc<>nil then
        pc^:=#0;

      if (p^ in ['0'..'9']) or ((p^='-') and (p[1] in ['0'..'9'])) then
      begin
        j:=0;
        pp:=p;
        repeat
          bufw[j]:=WideChar(pp^);
          inc(j); inc(pp);
        until (pp^=#0) or (pp^=' ');
        if pp^<>#0 then
        begin
          bufw[j]:=' '; bufw[j+1]:='-'; bufw[j+2]:=' '; inc(j,3);
          FastAnsiToWideBuf(pp+1,tmp);
          StrCopyW(bufw+j,TranslateW(tmp));
          SendMessageW(wnd,CB_ADDSTRING,0,lparam(@bufw));
        end
        else
          SendMessageA(wnd,CB_ADDSTRING,0,lparam(p));
      end
      else
      begin
        FastAnsiToWideBuf(p,tmp);
        SendMessageW(wnd,CB_ADDSTRING,0,lparam(TranslateW(tmp)));
        if (p=@buf) and (StrCmp(p,'structure')=0) then
          break;
      end;
      p:=pc+1;
    until pc=nil;
    FreeMem(tmp);
  end;
  SendMessage(wnd,CB_SETCURSEL,0,0);
end;

function tmApiCard.HashToName(ahash:longword):PAnsiChar;
var
  p,pp:PAnsiChar;
begin
  result:=nil;
  if storage<>nil then
  begin
    p:=GetSectionList(storage,namespace);
    pp:=p;
    while p^<>#0 do
    begin
      if ahash=Hash(p,StrLen(p)) then
      begin
        StrDup(result,p);
      end;
      while p^<>#0 do inc(p); inc(p);
    end;
    FreeSectionList(pp);
  end;
end;

function tmApiCard.NameFromList(cb:HWND):PAnsiChar;
var
  buf:array [0..255] of AnsiChar;
  pc:PAnsiChar;
  idx:integer;
begin
  pc:=GetDlgText(cb,true);
  idx:=SendMessage(cb,CB_GETCURSEL,0,0);
  if idx<>CB_ERR then
  begin
    SendMessageA(cb,CB_GETLBTEXT,idx,lparam(@buf));
    // edit field is text from list
    if StrCmp(pc,@buf)=0 then
    begin
      mFreeMem(pc);
      result:=HashToName(CB_GetData(cb,idx));
      exit;
    end;
  end;
  // no select or changed text
  result:=pc;
end;

procedure tmApiCard.FillList(combo:HWND; mode:integer=0);
var
  tmpbuf:array [0..127] of AnsiChar;
  p,pp,pc:PAnsiChar;
  idx:integer;
begin
  if storage<>nil then
  begin
    SendMessage(combo,CB_RESETCONTENT,0,0);
    p:=GetSectionList(storage,namespace);
    pp:=p;
    while p^<>#0 do
    begin
      case mode of
        1: begin // just constant name
          pc:=GetParamStr(storage,p,'alias',nil,namespace);
          if pc=nil then
            pc:=p;
        end;
        2: begin // value (name)
          pc:=StrCopyE(tmpbuf,p);
          pc^:=' '; inc(pc);
          pc^:='('; inc(pc);
          pc:=StrCopyE(pc,GetParamStr(storage,p,'alias',nil,namespace));
          pc^:=')'; inc(pc);
          pc^:=#0;
          pc:=@tmpbuf;
        end;
        3: begin // name 'value'
          pc:=@tmpbuf;
          pc:=StrCopyE(pc,GetParamStr(storage,p,'alias',nil,namespace));
          pc^:=' '; inc(pc);
          pc^:=''''; inc(pc);
          pc:=StrCopyE(pc,p);
          pc^:=''''; inc(pc);
          pc^:=#0;
          pc:=@tmpbuf;
        end;
      else // just constant value
        pc:=p;
      end;
      idx:=SendMessageA(combo,CB_ADDSTRING,0,lparam(pc));
      SendMessageA(combo,CB_SETITEMDATA,idx,Hash(p,StrLen(p)));
      while p^<>#0 do inc(p); inc(p);
    end;
    FreeSectionList(pp);
    SendMessage(combo,CB_SETCURSEL,-1,0);
  end;
end;

function ServiceHelpDlg(Dialog:HWND;hMessage:uint;wParam:WPARAM;lParam:LPARAM):LRESULT; stdcall;
var
  buf:PAnsiChar;
  tmp:PWideChar;
  card:tmApiCard;
begin
  result:=0;
  case hMessage of
    WM_CLOSE: begin
      card:=tmApiCard(GetWindowLongPtr(Dialog,GWLP_USERDATA{DWLP_USER}));
      card.HelpWindow:=0;
      DestroyWindow(Dialog); //??
    end;

    WM_INITDIALOG: begin
      TranslateDialogDefault(Dialog);
      result:=1;
    end;

    WM_COMMAND: begin
      if (wParam shr 16)=BN_CLICKED then
      begin
        case loword(wParam) of
          IDOK,IDCANCEL: begin
            card:=tmApiCard(GetWindowLongPtr(Dialog,GWLP_USERDATA{DWLP_USER}));
            card.HelpWindow:=0;
            DestroyWindow(Dialog);
          end;
        end;
      end;
    end;

    WM_UPDATEHELP: begin
      with tmApiCard(lParam) do
      begin
        if (storage<>nil) and (lParam<>0) and (current<>nil) then
        begin
          GetMem(buf,BufSize);
          GetMem(tmp,BufSize*SizeOf(WideChar));

          SetDlgItemTextA(Dialog,IDC_HLP_SERVICE,GetSectionName(current));

          SetDlgItemTextA(Dialog,IDC_HLP_ALIAS,
              GetParamSectionStr(current,'alias',''));
          
          FastAnsiToWideBuf(GetParamSectionStr(current,'return','Undefined'),tmp);
          SetDlgItemTextW(Dialog,IDC_HLP_RETURN,TranslateW(tmp));

          FastAnsiToWideBuf(GetParamSectionStr(current,'descr','Undefined'),tmp);
          SetDlgItemTextW(Dialog,IDC_HLP_EFFECT,TranslateW(tmp));

          FastAnsiToWideBuf(GetParamSectionStr(current,'plugin',''),tmp);
          SetDlgItemTextW(Dialog,IDC_HLP_PLUGIN,TranslateW(tmp));

          // Parameters
          StrCopy(buf,GetParamSectionStr(current,'wparam','0'));
          if StrScan(buf,'|')<>nil then
          begin
            ShowWindow(GetDlgItem(Dialog,IDC_HLP_WPARAML),SW_SHOW);
            ShowWindow(GetDlgItem(Dialog,IDC_HLP_WPARAM ),SW_HIDE);
            FillParams(GetDlgItem(Dialog,IDC_HLP_WPARAML),true);
          end
          else
          begin
            ShowWindow(GetDlgItem(Dialog,IDC_HLP_WPARAML),SW_HIDE);
            ShowWindow(GetDlgItem(Dialog,IDC_HLP_WPARAM ),SW_SHOW);
            FastAnsiToWideBuf(buf,tmp);
            SetDlgItemTextW(Dialog,IDC_HLP_WPARAM,TranslateW(tmp));
          end;

          StrCopy(buf,GetParamSectionStr(current,'lparam','0'));
          if StrScan(buf,'|')<>nil then
          begin
            ShowWindow(GetDlgItem(Dialog,IDC_HLP_LPARAML),SW_SHOW);
            ShowWindow(GetDlgItem(Dialog,IDC_HLP_LPARAM ),SW_HIDE);
            FillParams(GetDlgItem(Dialog,IDC_HLP_LPARAML),false);
          end
          else
          begin
            ShowWindow(GetDlgItem(Dialog,IDC_HLP_LPARAML),SW_HIDE);
            ShowWindow(GetDlgItem(Dialog,IDC_HLP_LPARAM ),SW_SHOW);
            FastAnsiToWideBuf(buf,tmp);
            SetDlgItemTextW(Dialog,IDC_HLP_LPARAM,TranslateW(tmp));
          end;

          FreeMem(tmp);
          FreeMem(buf);
        end
        else
        begin
          SetDlgItemTextW(Dialog,IDC_HLP_SERVICE,nil);
          SetDlgItemTextW(Dialog,IDC_HLP_ALIAS  ,nil);
          SetDlgItemTextW(Dialog,IDC_HLP_PLUGIN ,nil);
          SetDlgItemTextW(Dialog,IDC_HLP_RETURN ,nil);
          SetDlgItemTextW(Dialog,IDC_HLP_EFFECT ,nil);
          SetDlgItemTextW(Dialog,IDC_HLP_WPARAM ,nil);
          SetDlgItemTextW(Dialog,IDC_HLP_LPARAM ,nil);
          SendDlgItemMessage(Dialog,IDC_HLP_WPARAML,CB_RESETCONTENT,0,0);
          SendDlgItemMessage(Dialog,IDC_HLP_LPARAML,CB_RESETCONTENT,0,0);
          ShowWindow(GetDlgItem(Dialog,IDC_HLP_WPARAML),SW_HIDE);
          ShowWindow(GetDlgItem(Dialog,IDC_HLP_LPARAML),SW_HIDE);
        end;
      end;
    end;
  end;
end;

procedure tmApiCard.SetCurrentService(item:PAnsiChar);
begin
  if (item=nil) or (item^=#0) then
    current:=nil
  else
    current:=SearchSection(storage,item,namespace);
end;

procedure tmApiCard.Update(item:PAnsiChar=nil);
begin
  SendMessage(HelpWindow,WM_UPDATEHELP,0,LPARAM(self));
end;

procedure tmApiCard.Show;
var
  note,
  title:PWideChar;
begin
  if HelpWindow=0 then
  begin
    HelpWindow:=CreateDialogW(hInstance,'IDD_MAPIHELP',//MAKEINTRESOURCEW(IDD_HELP),
                parent,@ServiceHelpDlg);
    if HelpWindow<>0 then
    begin
      SetWindowLongPtr(HelpWindow,GWLP_USERDATA{DWLP_USER},LONG_PTR(Self));
      if isServiceHelp then
      begin
        title:='Miranda service help';
        note :='''<proto>'' in service name will be replaced by protocol name for contact handle in parameter';
      end
      else
      begin
        title:='Miranda event help';
        note :='';
      end;
      SendMessageW(HelpWindow,WM_SETTEXT,0,LPARAM(TranslateW(title)));

      SendMessageW(GetDlgItem(HelpWindow,IDC_HLP_NOTE),WM_SETTEXT,0,LPARAM(TranslateW(note)));
    end;
  end
  else
  begin
{
    if parent<>GetParent(HelpWindow) then
      SetParent(HelpWindow,parent);
}
  end;
//  if title<>nil then
//    SendMessageW(HelpWindow,WM_SETTEXT,0,TranslateW(title));

  Update(current);
end;

constructor tmApiCard.Create(fname:PAnsiChar; lparent:HWND=0);
var
  INIFile: array [0..511] of AnsiChar;
begin
  inherited Create;

  StrCopy(@INIFile,fname);
  HelpWindow:=0;
  current:=nil;
  if fname<>nil then
  begin
    ConvertFileName(fname,@INIFile);
  //  CallService(MS_UTILS_PATHTOABSOLUTE,
  //    WPARAM(PAnsiChar(ServiceHlpFile)),LPARAM(INIFile));
    if GetFSize(PAnsiChar(@INIFile))=0 then
    begin
      INIFile[0]:=#0;
    end;
    parent:=lparent;
  end;
  storage:=OpenStorage(PAnsiChar(@INIFile));
end;

destructor tmApiCard.Destroy;
begin
  CloseStorage(storage);

  inherited;
end;

function CreateServiceCard(parent:HWND=0):tmApiCard;
begin
  result:=tmApiCard.Create(ApiHlpFile,parent);
  result.isServiceHelp:=true;
  StrCopy(result.namespace,'Service');
end;

function CreateEventCard(parent:HWND=0):tmApiCard;
begin
  result:=tmApiCard.Create(ApiHlpFile,parent);
  result.isServiceHelp:=false;
  StrCopy(result.namespace,'Event');
end;


//initialization
//finalization
end.