summaryrefslogtreecommitdiff
path: root/plugins/HistoryPlusPlus/CustomizeToolbar.pas
blob: 9637ac07062f72dba0fcd65cb12ba184be6aea60 (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
(*
  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 CustomizeToolbar;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Dialogs, CommCtrl,
  Forms, ComCtrls, StdCtrls, ExtCtrls, CheckLst, Buttons,
  hpp_global, HistoryControls,
  m_api, hpp_forms;

type
  TfmCustomizeToolbar = class(TForm)
    bnAdd: TButton;
    bnRemove: TButton;
    lbAdded: TListBox;
    lbAvailable: TListBox;
    laAvailable: TLabel;
    laAdded: TLabel;
    bnUp: TButton;
    bnDown: TButton;
    Bevel1: TBevel;
    bnOK: TButton;
    bnCancel: TButton;
    bnReset: TButton;
    tiScroll: TTimer;
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure lbAvailableDrawItem(Control: TWinControl; Index: Integer; Rect: TRect;
      State: TOwnerDrawState);
    procedure lbAddedDragOver(Sender, Source: TObject; X, Y: Integer; State: TDragState;
      var Accept: Boolean);
    procedure lbAddedDragDrop(Sender, Source: TObject; X, Y: Integer);
    procedure lbAvailableDragOver(Sender, Source: TObject; X, Y: Integer; State: TDragState;
      var Accept: Boolean);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure lbAvailableDragDrop(Sender, Source: TObject; X, Y: Integer);
    procedure FormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
    procedure bnResetClick(Sender: TObject);
    procedure OnWMChar(var Message: TWMChar); message WM_CHAR;
    procedure bnAddClick(Sender: TObject);
    procedure bnOKClick(Sender: TObject);
    procedure bnCancelClick(Sender: TObject);
    procedure tiScrollTimer(Sender: TObject);
    procedure lbAvailableClick(Sender: TObject);
    procedure bnUpClick(Sender: TObject);
    procedure bnDownClick(Sender: TObject);
    procedure bnRemoveClick(Sender: TObject);
  private
    ItemBmp: TBitmap;
    DragOverIndex: Integer;
    TimerScrollDirection: Integer;
    procedure FillButtons;
    procedure UpdateControlButtons;
    procedure TranslateForm;

    procedure AddItem(src: Integer; dst: Integer = -1);
    procedure RemoveItem(src: Integer);

    function GenerateToolbarString: AnsiString;
    procedure SaveToolbar(ToolbarStr: AnsiString);

    procedure HMIcons2Changed(var M: TMessage); message HM_NOTF_ICONS2CHANGED;
  public
    { Public declarations }
  end;

var
  fmCustomizeToolbar: TfmCustomizeToolbar = nil;

implementation

uses HistoryForm, hpp_database, hpp_options;

{$R *.dfm}

procedure TfmCustomizeToolbar.lbAddedDragDrop(Sender, Source: TObject; X, Y: Integer);
var
  src, dst: Integer;
begin
  tiScroll.Enabled := False;
  if Source = lbAvailable then
  begin
    src := lbAvailable.ItemIndex;
    dst := lbAdded.ItemAtPos(Point(X, Y), False);
    AddItem(src, dst);
  end
  else
  begin
    src := lbAdded.ItemIndex;
    dst := lbAdded.ItemAtPos(Point(X, Y), True);
    lbAdded.Items.Move(src, dst);
    lbAdded.ItemIndex := dst;
  end;
  lbAdded.SetFocus;

  UpdateControlButtons;
end;

procedure TfmCustomizeToolbar.lbAddedDragOver(Sender, Source: TObject; X, Y: Integer;
  State: TDragState; var Accept: Boolean);
var
  idx: Integer;
  r: TRect;
begin
  Accept := True;

  if (lbAdded.ClientHeight - Y) < 10 then
    TimerScrollDirection := 1
  else if Y < 10 then
    TimerScrollDirection := 2
  else
    TimerScrollDirection := 0;

  tiScroll.Enabled := (TimerScrollDirection <> 0);

  idx := DragOverIndex;
  if idx = lbAdded.Count then
    Dec(idx);
  r := lbAdded.ItemRect(idx);
  InvalidateRect(lbAdded.Handle, @r, False);
  DragOverIndex := lbAdded.ItemAtPos(Point(X, Y), False);
  idx := DragOverIndex;
  if idx = lbAdded.Count then
    Dec(idx);
  r := lbAdded.ItemRect(idx);
  InvalidateRect(lbAdded.Handle, @r, False);
  lbAdded.Update;
end;

procedure TfmCustomizeToolbar.lbAvailableClick(Sender: TObject);
begin
  UpdateControlButtons;
end;

procedure TfmCustomizeToolbar.lbAvailableDragDrop(Sender, Source: TObject; X, Y: Integer);
begin
  RemoveItem(lbAdded.ItemIndex);
  lbAvailable.SetFocus;
end;

procedure TfmCustomizeToolbar.lbAvailableDragOver(Sender, Source: TObject; X, Y: Integer;
  State: TDragState; var Accept: Boolean);
begin
  Accept := (Source = lbAdded) and (lbAdded.ItemIndex <> -1);
end;

procedure TfmCustomizeToolbar.lbAvailableDrawItem(Control: TWinControl; Index: Integer;
  Rect: TRect; State: TOwnerDrawState);
var
  txtW: String;
  r: TRect;
  r2: TRect;
  but: THppToolButton;
  fm: THistoryFrm;
  src, dst: Integer;
  lb: TListBox;
  can: TCanvas;
  tf: DWord;
  DrawLineTop, DrawLineBottom: Boolean;
begin
  if Control = lbAdded then
    lb := lbAdded
  else
    lb := lbAvailable;

  ItemBmp.Width := Rect.Right - Rect.Left;
  ItemBmp.Height := Rect.Bottom - Rect.Top;
  can := ItemBmp.Canvas;
  can.Font := lb.Font;

  r := can.ClipRect;
  if (odSelected in State) and (odFocused in State) then
  begin
    can.Brush.Color := clHighlight;
    can.Font.Color := clHighlightText;
  end
  else
  begin
    can.Brush.Color := clWindow;
    can.Font.Color := clWindowText;
  end;

  can.FillRect(r);

  tf := DT_SINGLELINE or DT_VCENTER or DT_NOPREFIX;
  txtW := lb.Items[Index];

  if (odSelected in State) and (not(odFocused in State)) then
  begin
    r2 := r;
    InflateRect(r2, -1, -1);
    can.Pen.Color := clHighlight;
    can.Rectangle(r2);
  end;

  if txtW <> '-' then
  begin
    r2 := r;
    r2.Left := r2.Left + 20 + 4;
    DrawText(can.Handle, PChar(txtW), Length(txtW), r2, tf);
    r2 := Classes.Rect(r.Left + 2, r.Top + 2, r.Left + 20 + 2, r.Bottom - 2);
    { can.Brush.Color := clBtnFace;
      can.FillRect(r2); }
    fm := THistoryFrm(Owner);
    if lb.Items.Objects[Index] is THppToolButton then
    begin
      but := THppToolButton(lb.Items.Objects[Index]);
      ImageList_Draw(fm.ilToolbar.Handle, but.ImageIndex, can.Handle, r2.Left + 2, r2.Top + 2,
        ILD_NORMAL);
    end
    else if lb.Items.Objects[Index] = fm.tbEventsFilter then
    begin
      DrawIconEx(can.Handle, r2.Left + 2, r2.Top + 2, hppIcons[HPP_ICON_DROPDOWNARROW].Handle,
        16, 16, 0, 0, DI_NORMAL);
    end
    else if lb.Items.Objects[Index] = fm.tbHistory then
    begin
      DrawIconEx(can.Handle, r2.Left + 2, r2.Top + 2, hppIcons[HPP_ICON_CONTACTHISTORY].Handle,
        16, 16, 0, 0, DI_NORMAL);
    end;
  end
  else
  begin
    r2 := Classes.Rect(r.Left, r.Top + ((r.Bottom - r.Top) div 2), r.Right, r.Bottom);
    r2.Bottom := r2.Top + 1;
    InflateRect(r2, -((r2.Right - r2.Left) div 10), 0);
    can.Pen.Color := can.Font.Color;
    can.MoveTo(r2.Left, r2.Top);
    can.LineTo(r2.Right, r2.Top);
  end;

  if (lbAdded.Dragging) or (lbAvailable.Dragging) and (lb = lbAdded) then
  begin
    DrawLineTop := False;
    DrawLineBottom := False;
    dst := DragOverIndex;
    can.Pen.Color := clHighlight;
    if lbAdded.Dragging then
    begin
      src := lbAdded.ItemIndex;
      if Index = dst then
      begin
        if (dst < src) then
          DrawLineTop := True
        else
          DrawLineBottom := True
      end;
    end
    else
    begin
      if Index = dst then
        DrawLineTop := True;
    end;
    if (dst = lb.Count) and (Index = lb.Count - 1) then
      DrawLineBottom := True;

    if DrawLineTop then
    begin
      can.MoveTo(r.Left, r.Top);
      can.LineTo(r.Right, r.Top);
    end;
    if DrawLineBottom then
    begin
      can.MoveTo(r.Left, r.Bottom - 1);
      can.LineTo(r.Right, r.Bottom - 1);
    end;
  end;

  BitBlt(lb.Canvas.Handle,Rect.Left,Rect.Top,ItemBmp.Width,ItemBmp.Height,can.Handle,0,0,SRCCOPY);
end;

procedure TfmCustomizeToolbar.OnWMChar(var Message: TWMChar);
begin
  if not(csDesigning in ComponentState) then
    with Message do
    begin
      Result := 1;
      if (Perform(WM_GETDLGCODE, 0, 0) and DLGC_WANTCHARS = 0) and
        (GetParentForm(Self).Perform(CM_DIALOGCHAR, CharCode, KeyData) <> 0) then
        Exit;
      Result := 0;
    end;
end;

procedure TfmCustomizeToolbar.RemoveItem(src: Integer);
begin
  if (src = -1) or (src > lbAdded.Count - 1) then
    Exit;

  if (lbAdded.Items.Objects[src] <> nil) then
  begin
    // delete last item -- separator
    lbAvailable.Items.Delete(lbAvailable.Items.Count - 1);
    // add item
    lbAvailable.AddItem(lbAdded.Items[src], lbAdded.Items.Objects[src]);
    // sort
    lbAvailable.Sorted := True;
    lbAvailable.Sorted := False;
    // add separator back
    lbAvailable.AddItem('-', nil);
  end;
  lbAvailable.ItemIndex := lbAvailable.Items.IndexOfObject(lbAdded.Items.Objects[src]);

  lbAdded.Items.Delete(src);
  if src < lbAdded.Count then
    lbAdded.ItemIndex := src
  else if src - 1 < lbAdded.Count then
    lbAdded.ItemIndex := src - 1;

  UpdateControlButtons;
end;

procedure TfmCustomizeToolbar.SaveToolbar(ToolbarStr: AnsiString);
begin
  if ToolbarStr = '' then
    ToolbarStr := DEF_HISTORY_TOOLBAR;
  if ToolbarStr = DEF_HISTORY_TOOLBAR then
    DBDeleteContactSetting(0, hppDBName, 'HistoryToolbar')
  else
    WriteDBStr(hppDBName, 'HistoryToolbar', ToolbarStr);
end;

procedure TfmCustomizeToolbar.tiScrollTimer(Sender: TObject);
begin
  case TimerScrollDirection of
    1: lbAdded.Perform(WM_VSCROLL, SB_LINEDOWN, 0);
    2: lbAdded.Perform(WM_VSCROLL, SB_LINEUP, 0)
  else
    tiScroll.Enabled := False;
  end;
end;

procedure TfmCustomizeToolbar.AddItem(src, dst: Integer);
begin
  if (src = -1) or (src > lbAvailable.Count - 1) then
    Exit;

  lbAdded.AddItem(lbAvailable.Items[src], lbAvailable.Items.Objects[src]);
  if lbAvailable.Items[src] <> '-' then
    lbAvailable.Items.Delete(src);
  if (dst <> lbAdded.Count - 1) and (dst <> -1) then
  begin
    lbAdded.Items.Move(lbAdded.Count - 1, dst);
    lbAdded.ItemIndex := dst;
  end
  else
    lbAdded.ItemIndex := lbAdded.Count - 1;
  if src < lbAvailable.Count then
    lbAvailable.ItemIndex := src
  else if src - 1 < lbAvailable.Count then
    lbAvailable.ItemIndex := src - 1;

  UpdateControlButtons;
end;

procedure TfmCustomizeToolbar.bnAddClick(Sender: TObject);
begin
  AddItem(lbAvailable.ItemIndex, lbAdded.ItemIndex);
end;

procedure TfmCustomizeToolbar.FillButtons;
var
  i: Integer;
  fm: THistoryFrm;
  but: TControl;
  txt: String;
begin
  lbAdded.Clear;
  lbAvailable.Clear;
  fm := THistoryFrm(Owner);

  for i := 0 to fm.Toolbar.ButtonCount - 1 do
  begin
    but := fm.Toolbar.Buttons[i];
    txt := '';
    if but is THppToolButton then
    begin
      if THppToolButton(but).Style in [tbsSeparator, tbsDivider] then
        txt := '-'
      else
        txt := THppToolButton(but).Hint
    end
    else if but = fm.tbEventsFilter then
      txt := TranslateW('Event Filters')
    else if but is TSpeedButton then
      txt := TSpeedButton(but).Hint;

    if txt <> '' then
    begin
      if but.Visible then
      begin
        if txt = '-' then
          lbAdded.AddItem(txt, nil)
        else
          lbAdded.AddItem(txt, but);
      end
      else
        lbAvailable.AddItem(txt, but);
    end;
  end;
  lbAvailable.Sorted := True;
  lbAvailable.Sorted := False;
  lbAvailable.AddItem('-', nil);

  if lbAdded.Count > 0 then
  begin
    lbAdded.ItemIndex := 0;
    if Visible then
      lbAdded.SetFocus
    else
      ActiveControl := lbAdded;
  end
  else
  begin
    lbAvailable.ItemIndex := 0;
    if Visible then
      lbAvailable.SetFocus
    else
      ActiveControl := lbAvailable;
  end;
  UpdateControlButtons;
end;

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

procedure TfmCustomizeToolbar.FormCreate(Sender: TObject);
begin
  fmCustomizeToolbar := Self;

  DesktopFont := True;
  MakeFontsParent(Self);
  TranslateForm;

  ItemBmp := TBitmap.Create;
  FillButtons;
end;

procedure TfmCustomizeToolbar.FormDestroy(Sender: TObject);
begin
  fmCustomizeToolbar := nil;
  ItemBmp.Free;
  try
    THistoryFrm(Owner).CustomizeToolbarForm := nil;
  except
    // "eat" exceptions if any
  end;
end;

procedure TfmCustomizeToolbar.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_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;

function TfmCustomizeToolbar.GenerateToolbarString: AnsiString;
var
  i: Integer;
  but: TControl;
  but_str: AnsiString;
  fm: THistoryFrm;
begin
  Result := '';
  fm := THistoryFrm(Owner);
  for i := 0 to lbAdded.Count - 1 do
  begin
    but := TControl(lbAdded.Items.Objects[i]);
    if      but = nil                then but_str := ' '
    else if but = fm.tbSessions      then but_str := '[SESS]'
    else if but = fm.tbBookmarks     then but_str := '[BOOK]'
    else if but = fm.tbSearch        then but_str := '[SEARCH]'
    else if but = fm.tbFilter        then but_str := '[FILTER]'
    else if but = fm.tbCopy          then but_str := '[COPY]'
    else if but = fm.tbDelete        then but_str := '[DELETE]'
    else if but = fm.tbSave          then but_str := '[SAVE]'
    else if but = fm.tbHistory       then but_str := '[HISTORY]'
    else if but = fm.tbHistorySearch then but_str := '[GLOBSEARCH]'
    else if but = fm.tbEventsFilter  then but_str := '[EVENTS]'
    else if but = fm.tbUserMenu      then but_str := '[USERMENU]'
    else if but = fm.tbUserDetails   then but_str := '[USERDETAILS]';
    Result := Result + but_str;
  end;
end;

procedure TfmCustomizeToolbar.HMIcons2Changed(var M: TMessage);
begin
  lbAvailable.Repaint;
  lbAdded.Repaint;
end;

procedure TfmCustomizeToolbar.bnOKClick(Sender: TObject);
begin
  SaveToolbar(GenerateToolbarString);
  NotifyAllForms(HM_NOTF_TOOLBARCHANGED, 0, 0);
  close;
end;

procedure TfmCustomizeToolbar.bnCancelClick(Sender: TObject);
begin
  close;
end;

procedure TfmCustomizeToolbar.bnDownClick(Sender: TObject);
var
  idx: Integer;
begin
  idx := lbAdded.ItemIndex;
  if (idx < 0) or (idx > lbAdded.Count - 1) then
    Exit;
  lbAdded.Items.Move(idx, idx + 1);
  lbAdded.ItemIndex := idx + 1;
  UpdateControlButtons;
end;

procedure TfmCustomizeToolbar.bnRemoveClick(Sender: TObject);
begin
  RemoveItem(lbAdded.ItemIndex);
end;

procedure TfmCustomizeToolbar.bnResetClick(Sender: TObject);
begin
  DBDeleteContactSetting(0, hppDBName, 'HistoryToolbar');
  NotifyAllForms(HM_NOTF_TOOLBARCHANGED, 0, 0);
  FillButtons;
  UpdateControlButtons;
end;

procedure TfmCustomizeToolbar.bnUpClick(Sender: TObject);
var
  idx: Integer;
begin
  idx := lbAdded.ItemIndex;
  if idx < 1 then
    Exit;
  lbAdded.Items.Move(idx, idx - 1);
  lbAdded.ItemIndex := idx - 1;
  UpdateControlButtons;
end;

procedure TfmCustomizeToolbar.TranslateForm;
begin
  Caption             := TranslateUnicodeString(Caption);
  laAvailable.Caption := TranslateUnicodeString(laAvailable.Caption);
  laAdded.Caption     := TranslateUnicodeString(laAdded.Caption);
  bnOK.Caption        := TranslateUnicodeString(bnOK.Caption);
  bnCancel.Caption    := TranslateUnicodeString(bnCancel.Caption);
  bnReset.Caption     := TranslateUnicodeString(bnReset.Caption);
  bnAdd.Caption       := TranslateUnicodeString(bnAdd.Caption);
  bnRemove.Caption    := TranslateUnicodeString(bnRemove.Caption);
  bnUp.Caption        := TranslateUnicodeString(bnUp.Caption);
  bnDown.Caption      := TranslateUnicodeString(bnDown.Caption);
end;

procedure TfmCustomizeToolbar.UpdateControlButtons;
begin
  bnAdd.Enabled    := (lbAvailable.ItemIndex <> -1);
  bnRemove.Enabled := (lbAdded.ItemIndex <> -1);
  bnUp.Enabled     := (lbAdded.ItemIndex <> -1) and (lbAdded.ItemIndex > 0);
  bnDown.Enabled   := (lbAdded.ItemIndex <> -1) and (lbAdded.ItemIndex < lbAdded.Count - 1);
  bnOK.Enabled     := (lbAdded.Count > 0);
end;

end.