summaryrefslogtreecommitdiff
path: root/plugins/HistoryPlusPlus/CustomizeFiltersForm.pas
blob: 7a42dd126484d55d5c4350baeddfca9622475207 (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
(*
    History++ plugin for Miranda IM: the free IM client for Microsoft* Windows*

    Copyright (C) 2006-2009 theMIROn, 2003-2006 Art Fedorov.
    History+ parts (C) 2001 Christian Kastner

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*)

unit CustomizeFiltersForm;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms,Dialogs,
  HistoryControls,m_api,
  StdCtrls, CheckLst,
  hpp_eventfilters, ExtCtrls;

type
  TfmCustomizeFilters = class(TForm)
    bnOK: TButton;
    bnCancel: TButton;
    gbFilter: THppGroupBox;
    edFilterName: THppEdit;
    clEvents: TCheckListBox;
    bnReset: TButton;
    rbExclude: TRadioButton;
    rbInclude: TRadioButton;
    gbFilters: THppGroupBox;
    lbFilters: TListBox;
    bnDown: TButton;
    bnUp: TButton;
    bnDelete: TButton;
    bnAdd: TButton;
    laFilterName: TLabel;
    edCustomEvent: THppEdit;
    cbCustomEvent: TCheckBox;
    paClient: TPanel;
    procedure FormCreate(Sender: TObject);
    procedure bnOKClick(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormDestroy(Sender: TObject);
    procedure lbFiltersClick(Sender: TObject);
    procedure edFilterNameChange(Sender: TObject);
    procedure bnAddClick(Sender: TObject);
    procedure bnCancelClick(Sender: TObject);
    procedure bnUpClick(Sender: TObject);
    procedure bnDownClick(Sender: TObject);
    procedure bnDeleteClick(Sender: TObject);
    procedure clEventsClickCheck(Sender: TObject);
    procedure bnResetClick(Sender: TObject);
    procedure rbPropertyClick(Sender: TObject);
    procedure lbFiltersDragDrop(Sender, Source: TObject; X, Y: Integer);
    procedure lbFiltersDragOver(Sender, Source: TObject; X, Y: Integer;
      State: TDragState; var Accept: Boolean);
    procedure clEventsDrawItem(Control: TWinControl; Index: Integer;
      Rect: TRect; State: TOwnerDrawState);
    procedure lbFiltersDrawItem(Control: TWinControl; Index: Integer;
      Rect: TRect; State: TOwnerDrawState);
    procedure FormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
    procedure cbCustomEventClick(Sender: TObject);
    procedure edCustomEventChange(Sender: TObject);
    procedure edEditKeyPress(Sender: TObject; var Key: Char);
  private
    LocalFilters: ThppEventFilterArray;

    IncOutWrong: Boolean;
    EventsWrong: Boolean;
    EventsHeaderIndex: Integer;

    DragOverIndex: Integer;

    procedure LoadLocalFilters;
    procedure SaveLocalFilters;
    procedure FillFiltersList;
    procedure FillEventsCheckListBox;

    procedure MoveItem(Src,Dst: Integer);
    procedure UpdateEventsState;
    procedure UpdateUpDownButtons;

    procedure TranslateForm;
  public
    { Public declarations }
  end;

var
  fmCustomizeFilters: TfmCustomizeFilters = nil;

implementation

uses
  hpp_forms, hpp_global, hpp_events, HistoryForm, {hpp_options,}
  TypInfo, Math, GlobalSearch;

const
  // commented to use all events in system history
  //IgnoreEvents: TMessageTypes = [mtSystem];
  IgnoreEvents: TMessageTypes = [];

{$R *.dfm}

procedure TfmCustomizeFilters.bnAddClick(Sender: TObject);
var
  NewNameFmt,NewName: String;
  NameExists: Boolean;
  num,i: Integer;
begin
  NewNameFmt := TranslateW('New Filter #%d');
  num := 1;
  while True do begin
    NewName := Format(NewNameFmt,[num]);
    NameExists := False;
    for i := 0 to Length(LocalFilters) - 1 do
      if NewName = LocalFilters[i].Name then
      begin
        NameExists := true;
        break;
      end;
    if not NameExists then break;
    Inc(num);
  end;

  i := Length(LocalFilters);
  SetLength(LocalFilters,i+1);
  LocalFilters[i].Name := NewName;
  LocalFilters[i].filMode := FM_INCLUDE;
  LocalFilters[i].filEvents := [mtIncoming,mtOutgoing,mtMessage,mtUrl,mtFile];
  LocalFilters[i].Events := GenerateEvents(LocalFilters[i].filMode,LocalFilters[i].filEvents);

  lbFilters.Items.Add(NewName);
  lbFilters.ItemIndex := i;
  lbFiltersClick(Self);
  if edFilterName.CanFocus then edFilterName.SetFocus;
end;

procedure TfmCustomizeFilters.bnCancelClick(Sender: TObject);
begin
  Close;
end;

procedure TfmCustomizeFilters.bnDeleteClick(Sender: TObject);
var
  n,i: Integer;
begin
  if lbFilters.ItemIndex = -1 then exit;
  n := lbFilters.ItemIndex;
  if (LocalFilters[n].filMode = FM_EXCLUDE) and
     (LocalFilters[n].filEvents = []) then
    exit; // don't delete Show All Events

  for i := n to Length(LocalFilters) - 2 do
    LocalFilters[i] := LocalFilters[i+1];
  SetLength(LocalFilters,Length(LocalFilters)-1);
  lbFilters.DeleteSelected;
  if n >= lbFilters.Count then
    Dec(n);
  lbFilters.ItemIndex := n;
  lbFiltersClick(Self);
end;

procedure TfmCustomizeFilters.bnDownClick(Sender: TObject);
var
  i: Integer;
begin
  if lbFilters.ItemIndex = -1 then exit;
  if lbFilters.ItemIndex = lbFilters.Count-1 then exit;
  i := lbFilters.ItemIndex;
  MoveItem(i,i+1);
end;

procedure TfmCustomizeFilters.bnOKClick(Sender: TObject);
begin
  SaveLocalFilters;
  Close;
end;

procedure TfmCustomizeFilters.bnResetClick(Sender: TObject);
begin
  CopyEventFilters(hppDefEventFilters,LocalFilters);

  FillFiltersList;
  FillEventsCheckListBox;

  SaveLocalFilters;

  if lbFilters.Items.Count > 0 then lbFilters.ItemIndex := 0;
  lbFiltersClick(Self);
end;

procedure TfmCustomizeFilters.bnUpClick(Sender: TObject);
var
  i: Integer;
begin
  if lbFilters.ItemIndex = -1 then exit;
  if lbFilters.ItemIndex = 0 then exit;
  i := lbFilters.ItemIndex;
  MoveItem(i,i-1);
end;

procedure TfmCustomizeFilters.clEventsClickCheck(Sender: TObject);
var
  n,i: Integer;
begin
  UpdateEventsState;
  if EventsWrong or IncOutWrong then exit;
  n := lbFilters.ItemIndex;
  if rbInclude.Checked then
    LocalFilters[n].filMode := FM_INCLUDE
  else
    LocalFilters[n].filMode := FM_EXCLUDE;
  LocalFilters[n].filEvents := [];
  for i := 0 to clEvents.Count - 1 do
  begin
    if clEvents.Header[i] then continue;
    if clEvents.Checked[i] then
      Include(LocalFilters[n].filEvents,TMessageType(Integer(clEvents.Items.Objects[i])));
  end;
  if cbCustomEvent.Checked then
    LocalFilters[n].filEvents := LocalFilters[n].filEvents + EventsCustom;
  LocalFilters[n].Events := GenerateEvents(LocalFilters[n].filMode,LocalFilters[n].filEvents);
end;

procedure TfmCustomizeFilters.clEventsDrawItem(Control: TWinControl;
  Index: Integer; Rect: TRect; State: TOwnerDrawState);
var
  txtW: String;
  r: TRect;
  tf: DWord;
  BrushColor: TColor;
begin
  BrushColor := clEvents.Canvas.Brush.Color;
  txtW := clEvents.Items[Index];
  r := Rect;
  tf := DT_SINGLELINE or DT_VCENTER or DT_NOPREFIX;
  InflateRect(r,-2,0);

  if clEvents.Header[Index] then
  begin
    if (EventsWrong) and (Index = EventsHeaderIndex) then
      if BrushColor = clEvents.HeaderBackgroundColor then clEvents.Canvas.Brush.Color := $008080FF;
    if (IncOutWrong) and (Index <> EventsHeaderIndex) then
      if BrushColor = clEvents.HeaderBackgroundColor then clEvents.Canvas.Brush.Color := $008080FF;
    clEvents.Canvas.FillRect(Rect);
    DrawText(clEvents.Canvas.Handle,PChar(txtW),Length(txtW),r,tf);
    clEvents.Canvas.Brush.Color := BrushColor;
    exit;
  end;

  if (EventsWrong) and (Index > EventsHeaderIndex) then
    if BrushColor = clEvents.Color then clEvents.Canvas.Brush.Color := $008080FF;
  if (IncOutWrong) and (Index < EventsHeaderIndex) then
    if BrushColor = clEvents.Color then clEvents.Canvas.Brush.Color := $008080FF;
  clEvents.Canvas.FillRect(Rect);
  DrawText(clEvents.Canvas.Handle,PChar(txtW),Length(txtW),r,tf);
  clEvents.Canvas.Brush.Color := BrushColor;
end;

procedure TfmCustomizeFilters.edFilterNameChange(Sender: TObject);
begin
  if lbFilters.ItemIndex = -1 then exit;
  if edFilterName.Text = '' then
    edFilterName.Color := $008080FF
  else
    edFilterName.Color := clWindow;
  if edFilterName.Text <> '' then
    LocalFilters[lbFilters.ItemIndex].Name := edFilterName.Text;
  lbFilters.Items.BeginUpdate;
  lbFilters.Items[lbFilters.ItemIndex] := LocalFilters[lbFilters.ItemIndex].Name;
  lbFilters.Items.EndUpdate;
end;

procedure TfmCustomizeFilters.FillEventsCheckListBox;
var
  mt: TMessageType;
  mt_name, pretty_name: String;
  i: Integer;
begin
  clEvents.Items.BeginUpdate;
  clEvents.Items.Clear;

  // add all types except mtOther (we'll add it at the end) and
  // message types in AlwaysExclude and AlwaysInclude
  for mt := Low(TMessageType) to High(TMessageType) do begin
    if (mt in EventsExclude) or (mt in EventsInclude) or (mt in IgnoreEvents) then continue;
    if mt = mtOther then continue; // we'll add mtOther at the end
    if mt in [mtIncoming,mtMessage] then begin // insert header before incoming and message
      if mt = mtIncoming then
        mt_name := TranslateW('Incoming & Outgoing')
      else
        mt_name := TranslateW('Events');
      i := clEvents.Items.Add(mt_name);
      EventsHeaderIndex := i;
      clEvents.Header[i] := True;
    end;

    //pretty_name := GetEnumName(TypeInfo(TMessageType),Ord(mt));
    //Delete(pretty_name,1,2);
    // find filter names if we have substitute
    //for i := 0 to Length(FilterNames) - 1 do
    //  if FilterNames[i].mt = mt then begin
    //    pretty_name := FilterNames[i].Name;
    //    break;
    //  end;
    pretty_name := TranslateUnicodeString(EventRecords[mt].Name{TRANSLATE-IGNORE});
    clEvents.Items.AddObject(pretty_name,Pointer(Ord(mt)));
  end;

  // add mtOther at the end
  mt := mtOther;
  //pretty_name := GetEnumName(TypeInfo(TMessageType),Ord(mt));
  //Delete(pretty_name,1,2);
  // find filter names if we have substitute
  //for i := 0 to Length(FilterNames) - 1 do
  //  if FilterNames[i].mt = mt then begin
  //    pretty_name := FilterNames[i].Name;
  //    break;
  //  end;
  pretty_name := TranslateUnicodeString(EventRecords[mt].Name{TRANSLATE-IGNORE});
  clEvents.Items.AddObject(pretty_name,Pointer(Ord(mt)));
  clEvents.Items.EndUpdate;
end;

procedure TfmCustomizeFilters.FillFiltersList;
var
  i: Integer;
begin
  lbFilters.Items.BeginUpdate;
  lbFilters.Items.Clear;
  for i := 0 to Length(LocalFilters) - 1 do
  begin
    lbFilters.Items.Add(LocalFilters[i].Name);
  end;
  //meEvents.Lines.Clear;
  lbFilters.Items.EndUpdate;
end;

procedure TfmCustomizeFilters.FormCreate(Sender: TObject);
begin
  fmCustomizeFilters := Self;

  DesktopFont := True;
  MakeFontsParent(Self);
  DoubleBuffered := True;
  MakeDoubleBufferedParent(Self);

  TranslateForm;

  LoadLocalFilters;
  FillFiltersList;
  FillEventsCheckListBox;

  if lbFilters.Items.Count > 0 then lbFilters.ItemIndex := 0;
  lbFiltersClick(Self);
  edFilterName.MaxLength := MAX_FILTER_NAME_LENGTH;
end;

procedure TfmCustomizeFilters.lbFiltersClick(Sender: TObject);
var
  i: Integer;
  Lock: Boolean;
begin
  if lbFilters.ItemIndex = -1 then exit;
  Lock := false;
  if Visible then Lock := LockWindowUpdate(Handle);
  try
    rbInclude.Checked := (LocalFilters[lbFilters.ItemIndex].filMode = FM_INCLUDE);
    rbExclude.Checked := (LocalFilters[lbFilters.ItemIndex].filMode = FM_EXCLUDE);
    for i := 0 to clEvents.Items.Count - 1 do begin
      if clEvents.Header[i] then continue;
      clEvents.Checked[i] := TMessageType(Pointer(clEvents.Items.Objects[i])) in LocalFilters[lbFilters.ItemIndex].filEvents;
    end;

    cbCustomEvent.Checked := (LocalFilters[lbFilters.ItemIndex].filEvents*EventsCustom = EventsCustom);
    edCustomEvent.Text := IntToStr(LocalFilters[lbFilters.ItemIndex].filCustom);

    edFilterName.Text := lbFilters.Items[lbFilters.ItemIndex];

    edFilterName.Enabled := (lbFilters.ItemIndex <> GetShowAllEventsIndex(LocalFilters));
    laFilterName.Enabled  := edFilterName.Enabled;
    rbInclude.Enabled     := edFilterName.Enabled;
    rbExclude.Enabled     := edFilterName.Enabled;
    clEvents.Enabled      := edFilterName.Enabled;
    cbCustomEvent.Enabled := edFilterName.Enabled;
    edCustomEvent.Enabled := edFilterName.Enabled and cbCustomEvent.Checked;
    bnDelete.Enabled := edFilterName.Enabled;
  finally
    UpdateUpDownButtons;
    UpdateEventsState;
    if Visible and Lock then LockWindowUpdate(0);
  end;
end;

procedure TfmCustomizeFilters.lbFiltersDragDrop(Sender, Source: TObject; X, Y: Integer);
var
  src,dst: Integer;
begin
  // we insert always *before* droped item, unless we drop on the empty area
  // in this case be insert dragged item at the end
  dst := lbFilters.ItemAtPos(Point(x,y),False);
  src := lbFilters.ItemIndex;
  if src = dst then exit;
  if src < dst then Dec(dst);
  if src = dst then exit;
  MoveItem(src,dst);
end;

procedure TfmCustomizeFilters.lbFiltersDragOver(Sender, Source: TObject; X,
  Y: Integer; State: TDragState; var Accept: Boolean);
var
  r: TRect;
  idx: Integer;
begin
  Accept := True;
  idx := DragOverIndex;
  if idx = lbFilters.Count then Dec(idx);
  r := lbFilters.ItemRect(idx);
  DragOverIndex := lbFilters.ItemAtPos(Point(x,y),False);
  InvalidateRect(lbFilters.Handle,@r,False);
  idx := DragOverIndex;
  if idx = lbFilters.Count then Dec(idx);
  r := lbFilters.ItemRect(idx);
  InvalidateRect(lbFilters.Handle,@r,False);
  lbFilters.Update;
end;

procedure TfmCustomizeFilters.lbFiltersDrawItem(Control: TWinControl;
  Index: Integer; Rect: TRect; State: TOwnerDrawState);
var
  BrushColor: TColor;
  txtW: String;
  r: TRect;
  tf: DWord;
  {src,}dst: Integer;
begin
  BrushColor := lbFilters.Canvas.Brush.Color;
  txtW := lbFilters.Items[Index];
  r := Rect;
  InflateRect(r,-2,0);
  lbFilters.Canvas.FillRect(Rect);
  tf := DT_SINGLELINE or DT_VCENTER or DT_NOPREFIX;
  DrawText(lbFilters.Canvas.Handle,PChar(txtW),Length(txtW),r,tf);
  if lbFilters.Dragging then begin
//    src := lbFilters.ItemIndex;
    dst := DragOverIndex;
    if (dst = lbFilters.Count) and (Index = lbFilters.Count-1) then
    begin
      lbFilters.Canvas.Brush.Color := clHighlight;
      r := Classes.Rect(Rect.Left,Rect.Bottom-1,Rect.Right,Rect.Bottom);
      lbFilters.Canvas.FillRect(r);
    end;
    if (dst = Index) then
    begin
      lbFilters.Canvas.Brush.Color := clHighlight;
      r := Classes.Rect(Rect.Left,Rect.Top,Rect.Right,Rect.Top+1);
      lbFilters.Canvas.FillRect(r);
    end;
  end;
  lbFilters.Canvas.Brush.Color := BrushColor;
end;

procedure TfmCustomizeFilters.LoadLocalFilters;
begin
  CopyEventFilters(hppEventFilters,LocalFilters);
end;

procedure TfmCustomizeFilters.MoveItem(Src, Dst: Integer);
var
  ef: ThppEventFilter;
  i: Integer;
begin
  if Src = Dst then exit;

  lbFilters.Items.Move(Src,Dst);

  ef := LocalFilters[Src];
  if Dst > Src then
    for i := Src to Dst-1 do
      LocalFilters[i] := LocalFilters[i+1]
  else
    for i := Src downto Dst+1 do
      LocalFilters[i] := LocalFilters[i-1];
  LocalFilters[Dst] := ef;

  lbFilters.ItemIndex := Dst;
  UpdateUpDownButtons;
end;

procedure TfmCustomizeFilters.rbPropertyClick(Sender: TObject);
var
  n: Integer;
begin
  n := lbFilters.ItemIndex;
  UpdateEventsState;
  if IncOutWrong or EventsWrong then exit;
  if rbInclude.Checked then
    LocalFilters[n].filMode := FM_INCLUDE
  else
    LocalFilters[n].filMode := FM_EXCLUDE;
  LocalFilters[n].Events := GenerateEvents(LocalFilters[n].filMode,LocalFilters[n].filEvents);
end;

procedure TfmCustomizeFilters.SaveLocalFilters;
begin
  CopyEventFilters(LocalFilters,hppEventFilters);
  WriteEventFilters;
end;

procedure TfmCustomizeFilters.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  Action := caFree;
end;

procedure TfmCustomizeFilters.FormDestroy(Sender: TObject);
begin
  fmCustomizeFilters := nil;
  try
    if Owner is THistoryFrm then
      THistoryFrm(Owner).CustomizeFiltersForm := nil
    else if Owner is TfmGlobalSearch then
      TfmGlobalSearch(Owner).CustomizeFiltersForm := nil;
  except
    // "eat" exceptions if any
  end;
end;

procedure TfmCustomizeFilters.FormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
var
  Mask: Integer;
begin
  with Sender as TWinControl do
    begin
      if Perform(CM_CHILDKEY, Key, LPARAM(Sender)) <> 0 then
        Exit;
      Mask := 0;
      case Key of
        VK_TAB:
          Mask := DLGC_WANTTAB;
        VK_LEFT, VK_RIGHT, VK_UP, VK_DOWN:
          // added to change radio buttons from keyboard
          if (Self.ActiveControl is TRadioButton) then Mask := DLGC_WANTARROWS;
        VK_RETURN, VK_EXECUTE, VK_ESCAPE, VK_CANCEL:
          Mask := DLGC_WANTALLKEYS;
      end;
      if (Mask <> 0)
        and (Perform(CM_WANTSPECIALKEY, Key, 0) = 0)
        and (Perform(WM_GETDLGCODE, 0, 0) and Mask = 0)
        and (Self.Perform(CM_DIALOGKEY, Key, 0) <> 0)
        then Exit;
    end;
end;

procedure TfmCustomizeFilters.TranslateForm;
begin
  Caption               := TranslateUnicodeString(Caption);
  gbFilters.Caption     := TranslateUnicodeString(gbFilters.Caption);
  bnAdd.Caption         := TranslateUnicodeString(bnAdd.Caption);
  bnDelete.Caption      := TranslateUnicodeString(bnDelete.Caption);
  bnUp.Caption          := TranslateUnicodeString(bnUp.Caption);
  bnDown.Caption        := TranslateUnicodeString(bnDown.Caption);
  gbFilter.Caption      := TranslateUnicodeString(gbFilter.Caption);
  laFilterName.Caption  := TranslateUnicodeString(laFilterName.Caption);
  rbInclude.Caption     := TranslateUnicodeString(rbInclude.Caption);
  rbExclude.Caption     := TranslateUnicodeString(rbExclude.Caption);
  cbCustomEvent.Caption := TranslateUnicodeString(cbCustomEvent.Caption);
  bnOK.Caption          := TranslateUnicodeString(bnOK.Caption);
  bnCancel.Caption      := TranslateUnicodeString(bnCancel.Caption);
  bnReset.Caption       := TranslateUnicodeString(bnReset.Caption);
end;

procedure TfmCustomizeFilters.UpdateEventsState;
var
  IncOutChecked,IncOutUnchecked,
  EventsChecked,EventsUnchecked: Boolean;
  InsideEvents: Boolean;
  InsideIncOut: Boolean;
//  HeadEvents: Integer;
  i: Integer;
begin
  if not clEvents.Enabled then begin
    IncOutWrong := False;
    EventsWrong := False;
    bnOK.Enabled := True;
    exit;
  end;
  IncOutChecked := True;
  IncOutUnchecked := True;
  EventsChecked := True;
  EventsUnchecked := True;
  InsideEvents := False;
  InsideIncOut := False;
//  HeadEvents := 0;
  for i := 0 to clEvents.Count - 1 do
  begin

    if clEvents.Header[i] then
    begin
      if InsideIncOut then
      begin
//        HeadEvents := i;
        InsideEvents := True;
      end else
        InsideIncOut := True;
      continue;
    end;

    if InsideEvents then
    begin
      if EventsChecked and (not clEvents.Checked[i]) then
        EventsChecked := False;
      if EventsUnchecked and clEvents.Checked[i] then
        EventsUnchecked := False;
      if (not EventsChecked) and (not EventsUnchecked) then break;
    end
    else
    begin
      if IncOutChecked and (not clEvents.Checked[i]) then
        IncOutChecked := False;
      if IncOutUnchecked and clEvents.Checked[i] then
        IncOutUnchecked := False;
    end;

  end;

  if EventsChecked and not cbCustomEvent.Checked then
    EventsChecked := False;
  if EventsUnchecked and cbCustomEvent.Checked then
    EventsUnchecked := False;

  if rbInclude.Checked then
  begin
    EventsWrong := EventsUnchecked;
    IncOutWrong := IncOutUnchecked;
  end
  else
  begin
    EventsWrong := EventsChecked;
    IncOutWrong := IncOutChecked;
  end;

  // we probably need some help text to show why the filter selection is wrong
  // explanation is given in comments below
  if (rbExclude.Checked) and (EventsUnchecked) and (IncOutUnchecked) then
  begin
    EventsWrong := True;
    IncOutWrong := True;
    // not allowed to duplicate Show All Events filter
  end
  else if (rbInclude.Checked) and (EventsChecked) and (IncOutChecked) then
  begin
    EventsWrong := True;
    IncOutWrong := True;
    // not allowed to quasi-duplicate Show All Events filter
  end
  else
  begin
    if (EventsWrong) or (IncOutWrong) then
      ;// no events will be shown
  end;

  clEvents.Repaint;
  bnOK.Enabled := not (EventsWrong or IncOutWrong);
end;

procedure TfmCustomizeFilters.UpdateUpDownButtons;
begin
  bnUp.Enabled := (lbFilters.ItemIndex <> 0);
  bnDown.Enabled := (lbFilters.ItemIndex <> lbFilters.Count-1);
end;

procedure TfmCustomizeFilters.cbCustomEventClick(Sender: TObject);
begin
  edCustomEvent.Enabled := cbCustomEvent.Checked;
  if lbFilters.ItemIndex = -1 then exit;
  edCustomEvent.Text := IntToStr(LocalFilters[lbFilters.ItemIndex].filCustom);
  clEvents.OnClickCheck(Self);
end;

procedure TfmCustomizeFilters.edCustomEventChange(Sender: TObject);
var
  CustomType: Integer;
begin
  if lbFilters.ItemIndex = -1 then exit;
  if not ((edCustomEvent.Text <> '') and
     TryStrToInt(edCustomEvent.Text,CustomType)) then CustomType := -1;
  if CustomType > $FFFF then
    CustomType := -1;
  if CustomType >= 0 then
  begin
    LocalFilters[lbFilters.ItemIndex].filCustom := Word(CustomType);
    edCustomEvent.Color := clWindow;
  end
  else
    edCustomEvent.Color := $008080FF;
  bnOK.Enabled := (CustomType >= 0);
end;

procedure TfmCustomizeFilters.edEditKeyPress(Sender: TObject; var Key: Char);
begin
  // to prevent ** BLING ** when press Enter
  // to prevent ** BLING ** when press Tab
  // to prevent ** BLING ** when press Esc
  if Ord(Key) in [VK_RETURN,VK_TAB,VK_ESCAPE] then Key := #0;
end;

end.