summaryrefslogtreecommitdiff
path: root/plugins/Actman30/iac_chain.pas
blob: a4736ab1ca6a05a2e694e2e23ea04ecbb966c81a (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
unit iac_chain;

interface

implementation

uses
  windows, messages, commctrl,
  global, iac_global, mirutils, m_api,
  dlgshare,lowlevelc,common,dbsettings, wrapper;

{$include m_actman.inc}
{$include i_cnst_chain.inc}
{$resource iac_chain.res}

const
  ACF_BYNAME     = $00000001; // Address action link by name, not Id
  ACF_NOWAIT     = $00000002; // Don't wait execution result, continue
  ACF_KEEPOLD    = $00000004; // Don't change LastResult value
  ACF_SAMETHREAD = $00000008; // Execute in same thread with waiting
const
  ioNoWait     = 'nowait';
  ioKeepOld    = 'keepold';
  ioSameThread = 'samethread';
const
  opt_chain   = 'chain';
  opt_actname = 'actname';
const
  NoChainText:PWideChar = 'not defined';

type
  tChainAction = class(tBaseAction)
  private
    id     :dword;
    actname:pWideChar;
  public
    constructor Create(uid:dword);
    destructor Destroy; override;
//    function  Clone:tBaseAction; override;
    function  DoAction(var WorkData:tWorkData):LRESULT; override;
    procedure Save(node:pointer;fmt:integer); override;
    procedure Load(node:pointer;fmt:integer); override;
  end;

//----- Support functions -----

//----- Object realization -----

constructor tChainAction.Create(uid:dword);
begin
  inherited Create(uid);

  id     :=0;
  actname:=nil;
end;

destructor tChainAction.Destroy;
begin
  if (flags and ACF_BYNAME)<>0 then
    mFreeMem(actname);

  inherited Destroy;
end;
{
function tChainAction.Clone:tBaseAction;
begin
  result:=tChainAction.Create(0);
  Duplicate(result);

  tChainAction(result).id:=id;
  StrDupW(tChainAction(result).actname,actname);
end;
}
function tChainAction.DoAction(var WorkData:tWorkData):LRESULT;
var
  params:tAct_Param;
  res:int_ptr;
begin
  result:=0;

  if (flags and ACF_BYNAME)<>0 then
  begin
    params.flags:=ACTP_BYNAME;
    params.Id   :=uint_ptr(actname);
  end
  else
  begin
    params.flags:=0;
    params.Id   :=id;
  end;
  if (flags and ACF_SAMETHREAD)<>0 then
    params.flags:=params.flags or ACTP_SAMETHREAD
  else if (flags and ACF_NOWAIT)=0 then
    params.flags:=params.flags or ACTP_WAIT;

  if (flags and ACF_KEEPOLD)=0 then
    params.flags:=params.flags or ACTP_KEEPRESULT;

  params.wParam:=WorkData.Parameter;
  params.lParam:=WorkData.LastResult;
  res:=CallService(MS_ACT_RUNPARAMS,0,tlparam(@params));

  if (flags and ACF_KEEPOLD)=0 then
  begin
    ClearResult(WorkData);
    if (flags and ACF_SAMETHREAD)=0 then
    begin
      WorkData.LastResult:=res;
      WorkData.ResultType:=rtInt;
    end
    else
    begin
      WorkData.LastResult:=params.lParam;
      WorkData.ResultType:=params.lPType;
    end;
  end;
end;

procedure tChainAction.Load(node:pointer;fmt:integer);
var
  section: array [0..127] of AnsiChar;
  pc:pAnsiChar;
begin
  inherited Load(node,fmt);
  case fmt of
    0: begin
      pc:=StrCopyE(section,pAnsiChar(node));
      if (flags and ACF_BYNAME)=0 then
      begin
        StrCopy(pc,opt_chain); id:=DBReadDWord(0,DBBranch,section);
      end
      else
      begin
        StrCopy(pc,opt_actname); actname:=DBReadUnicode(0,DBBranch,section);
      end;
    end;

    1: begin
      with xmlparser do
      begin
        StrDupW(actname,getText(HXML(node)));
        flags:=flags or ACF_BYNAME;

        if StrToInt(getAttrValue(HXML(node),ioNoWait))=1 then
          flags:=flags or ACF_NOWAIT;

        if StrToInt(getAttrValue(HXML(node),ioKeepOld))=1 then
          flags:=flags or ACF_KEEPOLD;

        if StrToInt(getAttrValue(HXML(node),ioSameThread))=1 then
          flags:=flags or ACF_SAMETHREAD;
      end;
    end;
{
    2: begin

      UF8ToWide(GetParamSectionStr(node,'name',nil),actname);
      flags:=flags or ACF_BYNAME;

      if GetParamSectionInt(node,ioNoWait)=1 then
        flags:=flags or ACF_NOWAIT;

      if GetParamSectionInt(node,ioKeepOld)=1 then
        flags:=flags or ACF_KEEPOLD;

      if GetParamSectionInt(node,ioSameThread)=1 then
        flags:=flags or ACF_SAMETHREAD;
    end;
}
  end;
end;

procedure tChainAction.Save(node:pointer;fmt:integer);
var
  section: array [0..127] of AnsiChar;
  pc:pAnsiChar;
begin
  inherited Save(node,fmt);
  case fmt of
    0: begin
      pc:=StrCopyE(section,pAnsiChar(node));
      if (flags and ACF_BYNAME)=0 then
      begin
        StrCopy(pc,opt_chain); DBWriteDWord(0,DBBranch,section,id);
      end
      else
      begin
        StrCopy(pc,opt_actname); DBWriteUnicode(0,DBBranch,section,actname);
      end;
    end;
{
    1: begin
    end;
}
{
    2: begin
    end;
}
{
    3: begin
      Out(node,['CallAction',actname,
                IFF(flags or ACF_SAMETHREAD,'samethread',''),
                IFF(flags or ACF_NOWAIT    ,'nowait',''),
                IFF(flags or ACF_KEEPOLD   ,'keepold','')
               ]);
    end;
}
  end;
end;

//----- Dialog realization -----

procedure FillChainList(Dialog:hwnd);
var
  wnd,list:HWND;
  i:integer;

  li:LV_ITEMW;
  Macro:pMacroRecord;
begin
  wnd:=GetDlgItem(Dialog,IDC_MACRO_LIST);

  SendMessage(wnd,CB_RESETCONTENT,0,0);
  SendMessage(wnd,CB_SETITEMDATA,
    SendMessageW(wnd,CB_ADDSTRING,0,lparam(TranslateW(NoChainText))),0);

  list:=MacroListWindow;
  li.mask      :=LVIF_PARAM;
  li.iSubItem  :=0;
  for i:=0 to SendMessage(list,LVM_GETITEMCOUNT,0,0)-1 do
  begin
    li.iItem:=i;
    SendMessageW(list,LVM_GETITEMW,0,tlparam(@li));
    Macro:=EditMacroList[loword(li.lParam)];
    SendMessage(wnd,CB_SETITEMDATA,
        SendMessageW(wnd,CB_ADDSTRING,0,lparam(@(Macro.descr))),Macro.id);
  end;

end;

procedure ClearFields(Dialog:HWND);
begin
  CheckDlgButton(Dialog,IDC_MACRO_NOWAIT    ,BST_UNCHECKED);
  CheckDlgButton(Dialog,IDC_MACRO_SAMETHREAD,BST_UNCHECKED);
  CheckDlgButton(Dialog,IDC_MACRO_KEEPOLD   ,BST_UNCHECKED);
end;

function DlgProc(Dialog:HWnd;hMessage:UINT;wParam:WPARAM;lParam:LPARAM):lresult; stdcall;
var
  tmp:dword;
  wnd:HWND;
  bb:boolean;
begin
  result:=0;

  case hMessage of
    WM_INITDIALOG: begin
      TranslateDialogDefault(Dialog);

      FillChainList(Dialog);
    end;

    WM_ACT_SETVALUE: begin
      ClearFields(Dialog);
      with tChainAction(lParam) do
      begin
        if (flags and ACF_BYNAME)<>0 then
          SendDlgItemMessageW(Dialog,IDC_MACRO_LIST,CB_SELECTSTRING,twparam(-1),tlparam(actname))
        else
          CB_SelectData(Dialog,IDC_MACRO_LIST,id);
        if (flags and ACF_KEEPOLD)<>0 then
          CheckDlgButton(Dialog,IDC_MACRO_KEEPOLD,BST_CHECKED);

        if (flags and ACF_SAMETHREAD)<>0 then
        begin
          bb:=false;
          CheckDlgButton(Dialog,IDC_MACRO_SAMETHREAD,BST_CHECKED);
        end
        else
        begin
          bb:=true;
          if (flags and ACF_NOWAIT)<>0 then
            CheckDlgButton(Dialog,IDC_MACRO_NOWAIT,BST_CHECKED);
        end;
        EnableWindow(GetDlgItem(Dialog,IDC_MACRO_NOWAIT),bb);
      end;
    end;

    WM_ACT_RESET: begin
      ClearFields(Dialog);
      CheckDlgButton(Dialog,IDC_MACRO_SAMETHREAD,BST_CHECKED);
      SendDlgItemMessage(Dialog,IDC_MACRO_LIST,CB_SETCURSEL,0,0);
    end;

    WM_ACT_SAVE: begin
      with tChainAction(lParam) do
      begin
        id:=CB_GetData(GetDlgItem(Dialog,IDC_MACRO_LIST));

        if IsDlgButtonChecked(Dialog,IDC_MACRO_SAMETHREAD)<>BST_UNCHECKED then
          flags:=flags or ACF_SAMETHREAD
        else if IsDlgButtonChecked(Dialog,IDC_MACRO_NOWAIT)<>BST_UNCHECKED then
          flags:=flags or ACF_NOWAIT;

        if IsDlgButtonChecked(Dialog,IDC_MACRO_KEEPOLD)<>BST_UNCHECKED then
          flags:=flags or ACF_KEEPOLD;
      end;
    end;

    WM_ACT_LISTCHANGE: begin
      if wParam=1 then
      begin
        wnd:=GetDlgItem(Dialog,IDC_MACRO_LIST);
        tmp:=CB_GetData(wnd);
        FillChainList(Dialog);
        CB_SelectData(wnd,tmp);
      end;
    end;
	
    WM_COMMAND: begin
      case wParam shr 16 of
        CBN_SELCHANGE: SendMessage(GetParent(GetParent(Dialog)),PSM_CHANGED,0,0);
        BN_CLICKED: begin
          if loword(wParam)=IDC_MACRO_SAMETHREAD then
          begin
            EnableWindow(GetDlgItem(Dialog,IDC_MACRO_NOWAIT),
                IsDlgButtonChecked(Dialog,IDC_MACRO_SAMETHREAD)=BST_UNCHECKED);
          end;

          SendMessage(GetParent(GetParent(Dialog)),PSM_CHANGED,0,0);
        end;
      end;
    end;

    WM_HELP: begin
      result:=1;
    end;
  end;
//  result:=DefWindowProc(Dialog,hMessage,wParam,lParam);
end;

//----- Export/interface functions -----

var
  vc:tActModule;

function CreateAction:tBaseAction;
begin
  result:=tChainAction.Create(vc.Hash);
end;

function CreateDialog(parent:HWND):HWND;
begin
  result:=CreateDialogW(hInstance,'IDD_ACTCHAIN',parent,@DlgProc);
end;

procedure Init;
begin
  vc.Next    :=ModuleLink;

  vc.Name    :='Chain';
  vc.Dialog  :=@CreateDialog;
  vc.Create  :=@CreateAction;
  vc.Icon    :='IDI_CHAIN';
  vc.Hash    :=0;

  ModuleLink :=@vc;
end;

begin
  Init;
end.