summaryrefslogtreecommitdiff
path: root/plugins/Utils.pas/editwrapper.pas
blob: c6af1284e1500ea7ba0c66109e77686a96d74777 (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
unit editwrapper;

interface

uses windows,m_api;

// exported flags
const
  EF_SCRIPT = 1;         // right now, just "Variables" script
  EF_ALL    = EF_SCRIPT; // what can be changed in runtime
  EF_FORCES = $80;
  EF_FORCET = $40;
  EF_FORCE  = EF_FORCES or EF_FORCET;

function MakeEditField(Dialog:HWND; id:uint):HWND;
procedure SetEditFlags(wnd:HWND; mask, flags:dword); overload;
procedure SetEditFlags(Dialog:HWND; id:uint; mask,flags:dword); overload;
function GetEditFlags(wnd:HWND):dword; overload;
function GetEditFlags(Dialog:HWND; id:uint):dword; overload;
function EnableEditField(wnd:HWND; enable:boolean):boolean; overload;
function EnableEditField(Dialog:HWND; id:uint; enable:boolean):boolean; overload;
function ShowEditField(wnd:HWND; mode:integer):boolean;overload;
function ShowEditField(Dialog:HWND; id:uint; mode:integer):boolean;overload;

{
  -1 - cancel
   1 - script
   0 - new text
}
function ShowEditBox(parent:HWND;var text:pWideChar;title:pWideChar):int_ptr;


implementation

uses messages,commctrl,common,wrapper;

{$R editwrapper.res}
{$include 'i_text_const.inc'}

// internal flags
const
  EF_WRAP = 2;

// EditFields & EditForm -> Button -> [EditField,WinProc,flags]
type
  pUserData = ^tUserData;
  tUserData = record
    SavedProc    :pointer;
    LinkedControl:HWND;
    LinkedCtrlId :integer;
    flags        :dword;
  end;

procedure SetButtonTitle(btnwnd:HWND);
var
  title:pWideChar;
  ptr:pUserData;
begin
  ptr:=pUserData(GetWindowLongPtrW(btnwnd,GWLP_USERDATA));
  if (ptr^.flags and EF_SCRIPT)<>0 then
    title:='S'
  else
    title:='T';
  SendMessageW(btnwnd,WM_SETTEXT,0,tlParam(title));
end;

function EditDlgResizer(Dialog:HWND;lParam:LPARAM;urc:PUTILRESIZECONTROL):int; cdecl;
begin
  case urc^.wId of
    IDCANCEL:         result:=RD_ANCHORX_RIGHT or RD_ANCHORY_BOTTOM;
    IDOK:             result:=RD_ANCHORX_RIGHT or RD_ANCHORY_BOTTOM;
    IDC_SCRIPT_HELP:  result:=RD_ANCHORX_RIGHT or RD_ANCHORY_TOP;
    IDC_TEXT_SCRIPT:  result:=RD_ANCHORX_LEFT  or RD_ANCHORY_TOP;
    IDC_TEXT_WRAP:    result:=RD_ANCHORX_LEFT  or RD_ANCHORY_BOTTOM;
    IDC_TEXT_EDIT_W:  result:=RD_ANCHORX_WIDTH or RD_ANCHORY_HEIGHT;
    IDC_TEXT_EDIT_NW: result:=RD_ANCHORX_WIDTH or RD_ANCHORY_HEIGHT;
  else
    result:=0;
  end;
end;

// if need to change button text, will pass button (not edit field) handle as parameter
function EditWndProc(Dialog:HWND;hMessage:uint;wParam:WPARAM;lParam:LPARAM):LRESULT; stdcall;
var
  pc:pWideChar;
  btnwnd:HWND;
  ptr:pUserData;
  wnd,wnd1:HWND;
  vhi:TVARHELPINFO;
  cr:integer;
  idshow,idhide:integer;
  urd:TUTILRESIZEDIALOG;
begin
  result:=0;

  case hMessage of
    WM_DESTROY: begin
    end;

    WM_INITDIALOG: begin
      TranslateDialogDefault(Dialog);
      ptr:=pUserData(GetWindowLongPtrW(HWND(lParam),GWLP_USERDATA));

      SetWindowLongPtrW(Dialog,GWLP_USERDATA,lParam);
      pc:=GetDlgText(ptr^.LinkedControl);

      if (ptr^.flags and (EF_FORCES or EF_FORCET))<>0 then
        EnableWindow(GetDlgItem(Dialog,IDC_TEXT_SCRIPT),false);

      if (ptr^.flags and EF_SCRIPT)<>0 then
        CheckDlgButton(Dialog,IDC_TEXT_SCRIPT,BST_CHECKED);

      if (ptr^.flags and EF_WRAP)<>0 then
      begin
        CheckDlgButton(Dialog,IDC_TEXT_WRAP,BST_CHECKED);
        idshow:=IDC_TEXT_EDIT_W;
        idhide:=IDC_TEXT_EDIT_NW;
      end
      else
      begin
        idshow:=IDC_TEXT_EDIT_NW;
        idhide:=IDC_TEXT_EDIT_W;
      end;

      SetDlgItemTextW(Dialog,idshow,pc);
      mFreeMem(pc);
      ShowWindow(GetDlgItem(Dialog,idshow),SW_SHOW);
      ShowWindow(GetDlgItem(Dialog,idhide),SW_HIDE);

      wnd:=GetDlgItem(Dialog,IDC_SCRIPT_HELP);
      if ServiceExists(MS_VARS_FORMATSTRING)<>0 then
      begin
        SendMessage(wnd,BM_SETIMAGE,IMAGE_ICON,
            CallService(MS_VARS_GETSKINITEM,0,VSI_HELPICON));
        SendMessage(wnd,BUTTONADDTOOLTIP,
            CallService(MS_VARS_GETSKINITEM,0,VSI_HELPTIPTEXT),0);
{
        SendMessage(wnd,BM_SETIMAGE,IMAGE_ICON,
            CallService(MS_SKIN_LOADICON,SKINICON_OTHER_HELP,0));
}
      end
      else
      begin
        ShowWindow(wnd,SW_HIDE);
      end;
    end;

    WM_GETMINMAXINFO: begin
      with PMINMAXINFO(lParam)^ do
      begin
        ptMinTrackSize.x:=200;
        ptMinTrackSize.y:=180;
      end;
    end;

    WM_SIZE: begin
      FillChar(urd,SizeOf(TUTILRESIZEDIALOG),0);
      urd.cbSize    :=SizeOf(urd);
      urd.hwndDlg   :=Dialog;
      urd.hInstance :=hInstance;
      urd.lpTemplate:='IDD_EDITCONTROL';
      urd.lParam    :=0;
      urd.pfnResizer:=@EditDlgResizer;
      CallService(MS_UTILS_RESIZEDIALOG,0,tlparam(@urd));
    end;

    WM_COMMAND: begin
      case wParam shr 16 of
        BN_CLICKED: begin
          btnwnd:=GetWindowLongPtrW(Dialog,GWLP_USERDATA);
          ptr:=pUserData(GetWindowLongPtrW(btnwnd,GWLP_USERDATA));

          case loword(wParam) of
            IDC_TEXT_WRAP: begin
              if IsDlgButtonChecked(Dialog,IDC_TEXT_WRAP)<>BST_UNCHECKED then
              begin
                ptr^.flags:=ptr^.flags or EF_WRAP;
                idshow:=IDC_TEXT_EDIT_W;
                idhide:=IDC_TEXT_EDIT_NW;
              end
              else
              begin
                ptr^.flags:=ptr^.flags and not EF_WRAP;
                idshow:=IDC_TEXT_EDIT_NW;
                idhide:=IDC_TEXT_EDIT_W;
              end;
              wnd :=GetDlgItem(Dialog,idhide);
              wnd1:=GetDlgItem(Dialog,idshow);
              pc:=GetDlgText(wnd);
              SetDlgItemTextW(Dialog,idshow,pc);
              mFreeMem(pc);
              cr:=hiword(SendMessage(wnd,EM_GETSEL,0,0));
              SendMessage(wnd1,EM_SETSEL,-1,cr);
              ShowWindow(wnd1,SW_SHOW);
              ShowWindow(wnd ,SW_HIDE);

//              SetWindowLongPtr(wnd,GWL_STYLE,GetWindowLongPtr(wnd,GWL_STYLE) xor WS_HSCROLL);
//              SetWindowLongPtr(wnd,GWL_STYLE,GetWindowLongPtr(wnd,GWL_STYLE) xor ES_AUTOHSCROLL);

            end;

            IDC_SCRIPT_HELP: begin
              FillChar(vhi,SizeOf(vhi),0);
              with vhi do
              begin
                cbSize:=SizeOf(vhi);
                flags :=VHF_FULLDLG or VHF_SETLASTSUBJECT;
                if (ptr^.flags and EF_WRAP)<>0 then
                  hwndCtrl:=GetDlgItem(Dialog,IDC_TEXT_EDIT_W)
                else
                  hwndCtrl:=GetDlgItem(Dialog,IDC_TEXT_EDIT_NW);
              end;
              CallService(MS_VARS_SHOWHELPEX,Dialog,tlparam(@vhi));
            end;

            IDOK: begin
              ptr^.flags:=ptr^.flags and not EF_ALL; // /clear changing flags
              if IsDlgButtonChecked(Dialog,IDC_TEXT_SCRIPT)<>BST_UNCHECKED then
                ptr^.flags:=ptr^.flags or EF_SCRIPT;
              SetButtonTitle(btnwnd);
              //??
              if (ptr^.flags and EF_WRAP)<>0 then
                pc:=GetDlgText(Dialog,IDC_TEXT_EDIT_W)
              else
                pc:=GetDlgText(Dialog,IDC_TEXT_EDIT_NW);
              SendMessageW(ptr^.LinkedControl,WM_SETTEXT,0,tlParam(pc));
              mFreeMem(pc);

              // !! cheat
              SendMessage(GetParent(ptr^.LinkedControl),WM_COMMAND,
                  (EN_CHANGE shl 16)+ptr^.LinkedCtrlId,ptr^.LinkedControl);

              EndDialog(Dialog,1);
            end;

            IDCANCEL: begin // clear result / restore old value
              EndDialog(Dialog,0);
            end;
          end;
        end;
      end;
    end;
{
    WM_NOTIFY: begin
      case integer(PNMHdr(lParam)^.code) of
        PSN_APPLY: begin
        end;
      end;
    end;
}
  end;
end;

//----- Edit button processing -----

function EditControlProc(Dialog:HWND;hMessage:uint;wParam:WPARAM;lParam:LPARAM):LRESULT; stdcall;
var
  oldproc:pointer;
  ptr:pUserData;
begin
  ptr:=pUserData(GetWindowLongPtrW(Dialog,GWLP_USERDATA));
  oldproc:=ptr^.SavedProc;

  case hMessage of
    WM_DESTROY: begin
      SetWindowLongPtrW(Dialog,GWLP_WNDPROC,long_ptr(oldproc));
      mFreeMem(ptr);
    end;

    WM_INITDIALOG: begin
    end;

    WM_LBUTTONDOWN: begin
      DialogBoxParamW(hInstance,'IDD_EDITCONTROL',GetParent(Dialog),@EditWndProc,Dialog);
      result:=0;
      exit;
    end;
  end;

  result:=CallWindowProc(oldproc,Dialog,hMessage,wParam,lParam)
end;

function MakeEditField(Dialog:HWND; id:uint):HWND;
var
  rc,rcp:TRECT;
  ctrl:HWND;
  pu:pUserData;
begin
  ctrl:=GetDlgItem(Dialog,id);
  GetWindowRect(ctrl,rc ); // screen coords
  GetWindowRect(Dialog ,rcp); // screen coords of parent

  result:=CreateWindowW('BUTTON',nil,WS_CHILD+WS_VISIBLE+BS_PUSHBUTTON+BS_CENTER+BS_VCENTER,
          rc.left-rcp.left, rc.top-rcp.top+(rc.bottom-rc.top-16) div 2, 16,16,
          Dialog,0,hInstance,nil);
  if result<>0 then
  begin
    SetWindowLongPtrW(ctrl,GWLP_USERDATA,long_ptr(result));
    mGetMem(pu,SizeOf(tUserData));
    pu^.SavedProc    :=pointer(SetWindowLongPtrW(result,GWL_WNDPROC,long_ptr(@EditControlProc)));
    pu^.LinkedControl:=ctrl;
    pu^.LinkedCtrlId :=id;
    pu^.flags        :=0;
    SetWindowLongPtrW(result,GWLP_USERDATA,long_ptr(pu));
    inc(rc.left,20);
    MoveWindow(ctrl,
      rc.left-rcp.left, rc.top-rcp.top, rc.right-rc.left, rc.bottom-rc.top,
      false);
    SetButtonTitle(result);
  end;
end;

procedure SetEditFlags(wnd:HWND; mask, flags:dword);
var
  btnwnd:HWND;
  pu:pUserData;
begin
  if mask<>0 then
  begin
    btnwnd:=GetWindowLongPtrW(wnd,GWLP_USERDATA);
    if btnwnd<>0 then
    begin
      pu:=pUserData(GetWindowLongPtrW(btnwnd,GWLP_USERDATA));
      pu^.flags:=pu^.flags and not mask;
      pu^.flags:=pu^.flags or flags;
      if      (pu^.flags and EF_FORCES)<>0 then pu^.flags:=pu^.flags or EF_SCRIPT
      else if (pu^.flags and EF_FORCET)<>0 then pu^.flags:=pu^.flags and not EF_SCRIPT;
      SetButtonTitle(btnwnd);
    end;
  end;
end;
procedure SetEditFlags(Dialog:HWND; id:uint; mask, flags:dword);
begin
  SetEditFlags(GetDlgItem(Dialog,id),mask,flags);
end;

function GetEditFlags(wnd:HWND):dword;
var
  btnwnd:HWND;
  pu:pUserData;
begin
  btnwnd:=GetWindowLongPtrW(wnd,GWLP_USERDATA);
  if btnwnd<>0 then
  begin
    pu:=pUserData(GetWindowLongPtrW(btnwnd,GWLP_USERDATA));
    result:=pu^.flags and EF_ALL;
  end
  else
    result:=0;
end;
function GetEditFlags(Dialog:HWND; id:uint):dword;
begin
  result:=GetEditFlags(GetDlgItem(Dialog,id));
end;

function EnableEditField(wnd:HWND; enable:boolean):boolean;
var
  btnwnd:HWND;
begin
  btnwnd:=GetWindowLongPtrW(wnd,GWLP_USERDATA);
  if btnwnd<>0 then
    EnableWindow(btnwnd,enable);
  result:=EnableWindow(wnd,enable);
end;
function EnableEditField(Dialog:HWND; id:uint; enable:boolean):boolean;
begin
  result:=EnableEditField(GetDlgItem(Dialog,id),enable);
end;

function ShowEditField(wnd:HWND; mode:integer):boolean;
var
  btnwnd:HWND;
begin
  btnwnd:=GetWindowLongPtrW(wnd,GWLP_USERDATA);
  if btnwnd<>0 then
    ShowWindow(btnwnd,mode);
  result:=ShowWindow(wnd,mode);
end;
function ShowEditField(Dialog:HWND; id:uint; mode:integer):boolean;
begin
  result:=ShowEditField(GetDlgItem(Dialog,id),mode);
end;

//----- Separate Edit window -----

type
  pResultText = ^tResultText;
  tResultText = record
    text:pWideChar;
    typ :integer;
  end;
  pSepDlgParam = ^tSepDlgParam;
  tSepDlgParam = record
    title:pWideChar;
    text :pWideChar;
  end;

function EditWndProcSep(Dialog:HWND;hMessage:uint;wParam:WPARAM;lParam:LPARAM):LRESULT; stdcall;
var
  pc:pWideChar;
  wnd,wnd1:HWND;
  vhi:TVARHELPINFO;
  p:pResultText;
  cr:integer;
  idshow,idhide:integer;
  urd:TUTILRESIZEDIALOG;
begin
  result:=0;

  case hMessage of
    WM_DESTROY: begin
    end;

    WM_INITDIALOG: begin
      TranslateDialogDefault(Dialog);

      SetWindowTextW(Dialog,pSepDlgParam(lParam)^.title);

      idshow:=IDC_TEXT_EDIT_NW;
      idhide:=IDC_TEXT_EDIT_W;
      SetDlgItemTextW(Dialog,idshow,pSepDlgParam(lParam)^.text);
      ShowWindow(GetDlgItem(Dialog,idshow),SW_SHOW);
      ShowWindow(GetDlgItem(Dialog,idhide),SW_HIDE);

      wnd:=GetDlgItem(Dialog,IDC_SCRIPT_HELP);
      if ServiceExists(MS_VARS_FORMATSTRING)<>0 then
      begin
        SendMessage(wnd,BM_SETIMAGE,IMAGE_ICON,
            CallService(MS_VARS_GETSKINITEM,0,VSI_HELPICON));
        SendMessage(wnd,BUTTONADDTOOLTIP,
            CallService(MS_VARS_GETSKINITEM,0,VSI_HELPTIPTEXT),0);
{
        SendMessage(wnd,BM_SETIMAGE,IMAGE_ICON,
            CallService(MS_SKIN_LOADICON,SKINICON_OTHER_HELP,0));
}
      end
      else
      begin
        ShowWindow(wnd,SW_HIDE);
      end;
    end;

    WM_GETMINMAXINFO: begin
      with PMINMAXINFO(lParam)^ do
      begin
        ptMinTrackSize.x:=200;
        ptMinTrackSize.y:=180;
      end;
    end;

    WM_SIZE: begin
      FillChar(urd,SizeOf(TUTILRESIZEDIALOG),0);
      urd.cbSize    :=SizeOf(urd);
      urd.hwndDlg   :=Dialog;
      urd.hInstance :=hInstance;
      urd.lpTemplate:='IDD_EDITCONTROL';
      urd.lParam    :=0;
      urd.pfnResizer:=@EditDlgResizer;
      CallService(MS_UTILS_RESIZEDIALOG,0,tlparam(@urd));
    end;

    WM_COMMAND: begin
      case wParam shr 16 of
        BN_CLICKED: begin
          case loword(wParam) of
            IDC_TEXT_WRAP: begin
              if IsDlgButtonChecked(Dialog,IDC_TEXT_WRAP)<>BST_UNCHECKED then
              begin
                idshow:=IDC_TEXT_EDIT_W;
                idhide:=IDC_TEXT_EDIT_NW;
              end
              else
              begin
                idshow:=IDC_TEXT_EDIT_NW;
                idhide:=IDC_TEXT_EDIT_W;
              end;
              wnd :=GetDlgItem(Dialog,idhide);
              wnd1:=GetDlgItem(Dialog,idshow);
              pc:=GetDlgText(wnd);
              SetDlgItemTextW(Dialog,idshow,pc);
              mFreeMem(pc);
              cr:=hiword(SendMessage(wnd,EM_GETSEL,0,0));
              SendMessage(wnd1,EM_SETSEL,-1,cr);
              ShowWindow(wnd1,SW_SHOW);
              ShowWindow(wnd ,SW_HIDE);

//              SetWindowLongPtr(wnd,GWL_STYLE,GetWindowLongPtr(wnd,GWL_STYLE) xor WS_HSCROLL);
//              SetWindowLongPtr(wnd,GWL_STYLE,GetWindowLongPtr(wnd,GWL_STYLE) xor ES_AUTOHSCROLL);

            end;

            IDC_SCRIPT_HELP: begin
              FillChar(vhi,SizeOf(vhi),0);
              with vhi do
              begin
                cbSize:=SizeOf(vhi);
                flags :=VHF_FULLDLG or VHF_SETLASTSUBJECT;
                if IsDlgButtonChecked(Dialog,IDC_TEXT_WRAP)<>BST_UNCHECKED then
                  hwndCtrl:=GetDlgItem(Dialog,IDC_TEXT_EDIT_W)
                else
                  hwndCtrl:=GetDlgItem(Dialog,IDC_TEXT_EDIT_NW);
              end;
              CallService(MS_VARS_SHOWHELPEX,Dialog,tlparam(@vhi));
            end;

            IDOK: begin
              mGetMem(p,SizeOf(tResultText));

              if IsDlgButtonChecked(Dialog,IDC_TEXT_SCRIPT)<>BST_UNCHECKED then
                p^.typ:=1
              else
                p^.typ:=0;

              if IsDlgButtonChecked(Dialog,IDC_TEXT_WRAP)<>BST_UNCHECKED then
                p^.text:=GetDlgText(Dialog,IDC_TEXT_EDIT_W)
              else
                p^.text:=GetDlgText(Dialog,IDC_TEXT_EDIT_NW);

              EndDialog(Dialog,uint_ptr(p));
            end;

            IDCANCEL: begin // clear result / restore old value
              EndDialog(Dialog,0);
            end;
          end;
        end;
      end;
    end;
{
    WM_NOTIFY: begin
      case integer(PNMHdr(lParam)^.code) of
        PSN_APPLY: begin
        end;
      end;
    end;
}
  end;
end;

function ShowEditBox(parent:HWND;var text:pWideChar;title:pWideChar):int_ptr;
var
  tmp:pResultText;
  par:tSepDlgParam;
begin
  par.title:=title;
  par.text :=text;
  result:=DialogBoxParamW(hInstance,'IDD_EDITCONTROL',parent,@EditWndProcSep,tlparam(@par));
  if result<>0 then
  begin
    tmp:=pResultText(result);
    text  :=tmp^.text;
    result:=tmp^.typ;
    mFreeMem(tmp);
  end
  else
    result:=-1;
end;

end.